xref: /OK3568_Linux_fs/kernel/kernel/reboot.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  linux/kernel/reboot.c
4  *
5  *  Copyright (C) 2013  Linus Torvalds
6  */
7 
8 #define pr_fmt(fmt)	"reboot: " fmt
9 
10 #include <linux/ctype.h>
11 #include <linux/export.h>
12 #include <linux/kexec.h>
13 #include <linux/kmod.h>
14 #include <linux/kmsg_dump.h>
15 #include <linux/reboot.h>
16 #include <linux/suspend.h>
17 #include <linux/syscalls.h>
18 #include <linux/syscore_ops.h>
19 #include <linux/uaccess.h>
20 
21 /*
22  * this indicates whether you can reboot with ctrl-alt-del: the default is yes
23  */
24 
25 int C_A_D = 1;
26 struct pid *cad_pid;
27 EXPORT_SYMBOL(cad_pid);
28 
29 #if defined(CONFIG_ARM)
30 #define DEFAULT_REBOOT_MODE		= REBOOT_HARD
31 #else
32 #define DEFAULT_REBOOT_MODE
33 #endif
34 enum reboot_mode reboot_mode DEFAULT_REBOOT_MODE;
35 EXPORT_SYMBOL_GPL(reboot_mode);
36 enum reboot_mode panic_reboot_mode = REBOOT_UNDEFINED;
37 EXPORT_SYMBOL_GPL(panic_reboot_mode);
38 
39 /*
40  * This variable is used privately to keep track of whether or not
41  * reboot_type is still set to its default value (i.e., reboot= hasn't
42  * been set on the command line).  This is needed so that we can
43  * suppress DMI scanning for reboot quirks.  Without it, it's
44  * impossible to override a faulty reboot quirk without recompiling.
45  */
46 int reboot_default = 1;
47 int reboot_cpu;
48 enum reboot_type reboot_type = BOOT_ACPI;
49 int reboot_force;
50 
51 /*
52  * If set, this is used for preparing the system to power off.
53  */
54 
55 void (*pm_power_off_prepare)(void);
56 EXPORT_SYMBOL_GPL(pm_power_off_prepare);
57 
58 /**
59  *	emergency_restart - reboot the system
60  *
61  *	Without shutting down any hardware or taking any locks
62  *	reboot the system.  This is called when we know we are in
63  *	trouble so this is our best effort to reboot.  This is
64  *	safe to call in interrupt context.
65  */
emergency_restart(void)66 void emergency_restart(void)
67 {
68 	kmsg_dump(KMSG_DUMP_EMERG);
69 	machine_emergency_restart();
70 }
71 EXPORT_SYMBOL_GPL(emergency_restart);
72 
kernel_restart_prepare(char * cmd)73 void kernel_restart_prepare(char *cmd)
74 {
75 	blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
76 	system_state = SYSTEM_RESTART;
77 	usermodehelper_disable();
78 	device_shutdown();
79 }
80 
81 /**
82  *	register_reboot_notifier - Register function to be called at reboot time
83  *	@nb: Info about notifier function to be called
84  *
85  *	Registers a function with the list of functions
86  *	to be called at reboot time.
87  *
88  *	Currently always returns zero, as blocking_notifier_chain_register()
89  *	always returns zero.
90  */
register_reboot_notifier(struct notifier_block * nb)91 int register_reboot_notifier(struct notifier_block *nb)
92 {
93 	return blocking_notifier_chain_register(&reboot_notifier_list, nb);
94 }
95 EXPORT_SYMBOL(register_reboot_notifier);
96 
97 /**
98  *	unregister_reboot_notifier - Unregister previously registered reboot notifier
99  *	@nb: Hook to be unregistered
100  *
101  *	Unregisters a previously registered reboot
102  *	notifier function.
103  *
104  *	Returns zero on success, or %-ENOENT on failure.
105  */
unregister_reboot_notifier(struct notifier_block * nb)106 int unregister_reboot_notifier(struct notifier_block *nb)
107 {
108 	return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
109 }
110 EXPORT_SYMBOL(unregister_reboot_notifier);
111 
devm_unregister_reboot_notifier(struct device * dev,void * res)112 static void devm_unregister_reboot_notifier(struct device *dev, void *res)
113 {
114 	WARN_ON(unregister_reboot_notifier(*(struct notifier_block **)res));
115 }
116 
devm_register_reboot_notifier(struct device * dev,struct notifier_block * nb)117 int devm_register_reboot_notifier(struct device *dev, struct notifier_block *nb)
118 {
119 	struct notifier_block **rcnb;
120 	int ret;
121 
122 	rcnb = devres_alloc(devm_unregister_reboot_notifier,
123 			    sizeof(*rcnb), GFP_KERNEL);
124 	if (!rcnb)
125 		return -ENOMEM;
126 
127 	ret = register_reboot_notifier(nb);
128 	if (!ret) {
129 		*rcnb = nb;
130 		devres_add(dev, rcnb);
131 	} else {
132 		devres_free(rcnb);
133 	}
134 
135 	return ret;
136 }
137 EXPORT_SYMBOL(devm_register_reboot_notifier);
138 
139 /*
140  *	Notifier list for kernel code which wants to be called
141  *	to restart the system.
142  */
143 static ATOMIC_NOTIFIER_HEAD(restart_handler_list);
144 
145 /**
146  *	register_restart_handler - Register function to be called to reset
147  *				   the system
148  *	@nb: Info about handler function to be called
149  *	@nb->priority:	Handler priority. Handlers should follow the
150  *			following guidelines for setting priorities.
151  *			0:	Restart handler of last resort,
152  *				with limited restart capabilities
153  *			128:	Default restart handler; use if no other
154  *				restart handler is expected to be available,
155  *				and/or if restart functionality is
156  *				sufficient to restart the entire system
157  *			255:	Highest priority restart handler, will
158  *				preempt all other restart handlers
159  *
160  *	Registers a function with code to be called to restart the
161  *	system.
162  *
163  *	Registered functions will be called from machine_restart as last
164  *	step of the restart sequence (if the architecture specific
165  *	machine_restart function calls do_kernel_restart - see below
166  *	for details).
167  *	Registered functions are expected to restart the system immediately.
168  *	If more than one function is registered, the restart handler priority
169  *	selects which function will be called first.
170  *
171  *	Restart handlers are expected to be registered from non-architecture
172  *	code, typically from drivers. A typical use case would be a system
173  *	where restart functionality is provided through a watchdog. Multiple
174  *	restart handlers may exist; for example, one restart handler might
175  *	restart the entire system, while another only restarts the CPU.
176  *	In such cases, the restart handler which only restarts part of the
177  *	hardware is expected to register with low priority to ensure that
178  *	it only runs if no other means to restart the system is available.
179  *
180  *	Currently always returns zero, as atomic_notifier_chain_register()
181  *	always returns zero.
182  */
register_restart_handler(struct notifier_block * nb)183 int register_restart_handler(struct notifier_block *nb)
184 {
185 	return atomic_notifier_chain_register(&restart_handler_list, nb);
186 }
187 EXPORT_SYMBOL(register_restart_handler);
188 
189 /**
190  *	unregister_restart_handler - Unregister previously registered
191  *				     restart handler
192  *	@nb: Hook to be unregistered
193  *
194  *	Unregisters a previously registered restart handler function.
195  *
196  *	Returns zero on success, or %-ENOENT on failure.
197  */
unregister_restart_handler(struct notifier_block * nb)198 int unregister_restart_handler(struct notifier_block *nb)
199 {
200 	return atomic_notifier_chain_unregister(&restart_handler_list, nb);
201 }
202 EXPORT_SYMBOL(unregister_restart_handler);
203 
204 /**
205  *	do_kernel_restart - Execute kernel restart handler call chain
206  *
207  *	Calls functions registered with register_restart_handler.
208  *
209  *	Expected to be called from machine_restart as last step of the restart
210  *	sequence.
211  *
212  *	Restarts the system immediately if a restart handler function has been
213  *	registered. Otherwise does nothing.
214  */
do_kernel_restart(char * cmd)215 void do_kernel_restart(char *cmd)
216 {
217 	atomic_notifier_call_chain(&restart_handler_list, reboot_mode, cmd);
218 }
219 
220 #ifdef CONFIG_NO_GKI
221 static ATOMIC_NOTIFIER_HEAD(pre_restart_handler_list);
222 
register_pre_restart_handler(struct notifier_block * nb)223 int register_pre_restart_handler(struct notifier_block *nb)
224 {
225 	return atomic_notifier_chain_register(&pre_restart_handler_list, nb);
226 }
227 EXPORT_SYMBOL(register_pre_restart_handler);
228 
unregister_pre_restart_handler(struct notifier_block * nb)229 int unregister_pre_restart_handler(struct notifier_block *nb)
230 {
231 	return atomic_notifier_chain_unregister(&pre_restart_handler_list, nb);
232 }
233 EXPORT_SYMBOL(unregister_pre_restart_handler);
234 
do_kernel_pre_restart(char * cmd)235 void do_kernel_pre_restart(char *cmd)
236 {
237 	atomic_notifier_call_chain(&pre_restart_handler_list, reboot_mode, cmd);
238 }
239 #endif
240 
migrate_to_reboot_cpu(void)241 void migrate_to_reboot_cpu(void)
242 {
243 	/* The boot cpu is always logical cpu 0 */
244 	int cpu = reboot_cpu;
245 
246 	cpu_hotplug_disable();
247 
248 	/* Make certain the cpu I'm about to reboot on is online */
249 	if (!cpu_online(cpu))
250 		cpu = cpumask_first(cpu_online_mask);
251 
252 	/* Prevent races with other tasks migrating this task */
253 	current->flags |= PF_NO_SETAFFINITY;
254 
255 	/* Make certain I only run on the appropriate processor */
256 	set_cpus_allowed_ptr(current, cpumask_of(cpu));
257 }
258 
259 /**
260  *	kernel_restart - reboot the system
261  *	@cmd: pointer to buffer containing command to execute for restart
262  *		or %NULL
263  *
264  *	Shutdown everything and perform a clean reboot.
265  *	This is not safe to call in interrupt context.
266  */
kernel_restart(char * cmd)267 void kernel_restart(char *cmd)
268 {
269 	kernel_restart_prepare(cmd);
270 	migrate_to_reboot_cpu();
271 	syscore_shutdown();
272 	if (!cmd)
273 		pr_emerg("Restarting system\n");
274 	else
275 		pr_emerg("Restarting system with command '%s'\n", cmd);
276 	kmsg_dump(KMSG_DUMP_SHUTDOWN);
277 	machine_restart(cmd);
278 }
279 EXPORT_SYMBOL_GPL(kernel_restart);
280 
kernel_shutdown_prepare(enum system_states state)281 static void kernel_shutdown_prepare(enum system_states state)
282 {
283 	blocking_notifier_call_chain(&reboot_notifier_list,
284 		(state == SYSTEM_HALT) ? SYS_HALT : SYS_POWER_OFF, NULL);
285 	system_state = state;
286 	usermodehelper_disable();
287 	device_shutdown();
288 }
289 /**
290  *	kernel_halt - halt the system
291  *
292  *	Shutdown everything and perform a clean system halt.
293  */
kernel_halt(void)294 void kernel_halt(void)
295 {
296 	kernel_shutdown_prepare(SYSTEM_HALT);
297 	migrate_to_reboot_cpu();
298 	syscore_shutdown();
299 	pr_emerg("System halted\n");
300 	kmsg_dump(KMSG_DUMP_SHUTDOWN);
301 	machine_halt();
302 }
303 EXPORT_SYMBOL_GPL(kernel_halt);
304 
305 /**
306  *	kernel_power_off - power_off the system
307  *
308  *	Shutdown everything and perform a clean system power_off.
309  */
kernel_power_off(void)310 void kernel_power_off(void)
311 {
312 	kernel_shutdown_prepare(SYSTEM_POWER_OFF);
313 	if (pm_power_off_prepare)
314 		pm_power_off_prepare();
315 	migrate_to_reboot_cpu();
316 	syscore_shutdown();
317 	pr_emerg("Power down\n");
318 	kmsg_dump(KMSG_DUMP_SHUTDOWN);
319 	machine_power_off();
320 }
321 EXPORT_SYMBOL_GPL(kernel_power_off);
322 
323 DEFINE_MUTEX(system_transition_mutex);
324 
325 /*
326  * Reboot system call: for obvious reasons only root may call it,
327  * and even root needs to set up some magic numbers in the registers
328  * so that some mistake won't make this reboot the whole machine.
329  * You can also set the meaning of the ctrl-alt-del-key here.
330  *
331  * reboot doesn't sync: do that yourself before calling this.
332  */
SYSCALL_DEFINE4(reboot,int,magic1,int,magic2,unsigned int,cmd,void __user *,arg)333 SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
334 		void __user *, arg)
335 {
336 	struct pid_namespace *pid_ns = task_active_pid_ns(current);
337 	char buffer[256];
338 	int ret = 0;
339 
340 	/* We only trust the superuser with rebooting the system. */
341 	if (!ns_capable(pid_ns->user_ns, CAP_SYS_BOOT))
342 		return -EPERM;
343 
344 	/* For safety, we require "magic" arguments. */
345 	if (magic1 != LINUX_REBOOT_MAGIC1 ||
346 			(magic2 != LINUX_REBOOT_MAGIC2 &&
347 			magic2 != LINUX_REBOOT_MAGIC2A &&
348 			magic2 != LINUX_REBOOT_MAGIC2B &&
349 			magic2 != LINUX_REBOOT_MAGIC2C))
350 		return -EINVAL;
351 
352 	/*
353 	 * If pid namespaces are enabled and the current task is in a child
354 	 * pid_namespace, the command is handled by reboot_pid_ns() which will
355 	 * call do_exit().
356 	 */
357 	ret = reboot_pid_ns(pid_ns, cmd);
358 	if (ret)
359 		return ret;
360 
361 	/* Instead of trying to make the power_off code look like
362 	 * halt when pm_power_off is not set do it the easy way.
363 	 */
364 	if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
365 		cmd = LINUX_REBOOT_CMD_HALT;
366 
367 	mutex_lock(&system_transition_mutex);
368 	switch (cmd) {
369 	case LINUX_REBOOT_CMD_RESTART:
370 		kernel_restart(NULL);
371 		break;
372 
373 	case LINUX_REBOOT_CMD_CAD_ON:
374 		C_A_D = 1;
375 		break;
376 
377 	case LINUX_REBOOT_CMD_CAD_OFF:
378 		C_A_D = 0;
379 		break;
380 
381 	case LINUX_REBOOT_CMD_HALT:
382 		kernel_halt();
383 		do_exit(0);
384 		panic("cannot halt");
385 
386 	case LINUX_REBOOT_CMD_POWER_OFF:
387 		kernel_power_off();
388 		do_exit(0);
389 		break;
390 
391 	case LINUX_REBOOT_CMD_RESTART2:
392 		ret = strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1);
393 		if (ret < 0) {
394 			ret = -EFAULT;
395 			break;
396 		}
397 		buffer[sizeof(buffer) - 1] = '\0';
398 
399 		kernel_restart(buffer);
400 		break;
401 
402 #ifdef CONFIG_KEXEC_CORE
403 	case LINUX_REBOOT_CMD_KEXEC:
404 		ret = kernel_kexec();
405 		break;
406 #endif
407 
408 #ifdef CONFIG_HIBERNATION
409 	case LINUX_REBOOT_CMD_SW_SUSPEND:
410 		ret = hibernate();
411 		break;
412 #endif
413 
414 	default:
415 		ret = -EINVAL;
416 		break;
417 	}
418 	mutex_unlock(&system_transition_mutex);
419 	return ret;
420 }
421 
deferred_cad(struct work_struct * dummy)422 static void deferred_cad(struct work_struct *dummy)
423 {
424 	kernel_restart(NULL);
425 }
426 
427 /*
428  * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
429  * As it's called within an interrupt, it may NOT sync: the only choice
430  * is whether to reboot at once, or just ignore the ctrl-alt-del.
431  */
ctrl_alt_del(void)432 void ctrl_alt_del(void)
433 {
434 	static DECLARE_WORK(cad_work, deferred_cad);
435 
436 	if (C_A_D)
437 		schedule_work(&cad_work);
438 	else
439 		kill_cad_pid(SIGINT, 1);
440 }
441 
442 char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
443 static const char reboot_cmd[] = "/sbin/reboot";
444 
run_cmd(const char * cmd)445 static int run_cmd(const char *cmd)
446 {
447 	char **argv;
448 	static char *envp[] = {
449 		"HOME=/",
450 		"PATH=/sbin:/bin:/usr/sbin:/usr/bin",
451 		NULL
452 	};
453 	int ret;
454 	argv = argv_split(GFP_KERNEL, cmd, NULL);
455 	if (argv) {
456 		ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
457 		argv_free(argv);
458 	} else {
459 		ret = -ENOMEM;
460 	}
461 
462 	return ret;
463 }
464 
__orderly_reboot(void)465 static int __orderly_reboot(void)
466 {
467 	int ret;
468 
469 	ret = run_cmd(reboot_cmd);
470 
471 	if (ret) {
472 		pr_warn("Failed to start orderly reboot: forcing the issue\n");
473 		emergency_sync();
474 		kernel_restart(NULL);
475 	}
476 
477 	return ret;
478 }
479 
__orderly_poweroff(bool force)480 static int __orderly_poweroff(bool force)
481 {
482 	int ret;
483 
484 	ret = run_cmd(poweroff_cmd);
485 
486 	if (ret && force) {
487 		pr_warn("Failed to start orderly shutdown: forcing the issue\n");
488 
489 		/*
490 		 * I guess this should try to kick off some daemon to sync and
491 		 * poweroff asap.  Or not even bother syncing if we're doing an
492 		 * emergency shutdown?
493 		 */
494 		emergency_sync();
495 		kernel_power_off();
496 	}
497 
498 	return ret;
499 }
500 
501 static bool poweroff_force;
502 
poweroff_work_func(struct work_struct * work)503 static void poweroff_work_func(struct work_struct *work)
504 {
505 	__orderly_poweroff(poweroff_force);
506 }
507 
508 static DECLARE_WORK(poweroff_work, poweroff_work_func);
509 
510 /**
511  * orderly_poweroff - Trigger an orderly system poweroff
512  * @force: force poweroff if command execution fails
513  *
514  * This may be called from any context to trigger a system shutdown.
515  * If the orderly shutdown fails, it will force an immediate shutdown.
516  */
orderly_poweroff(bool force)517 void orderly_poweroff(bool force)
518 {
519 	if (force) /* do not override the pending "true" */
520 		poweroff_force = true;
521 	schedule_work(&poweroff_work);
522 }
523 EXPORT_SYMBOL_GPL(orderly_poweroff);
524 
reboot_work_func(struct work_struct * work)525 static void reboot_work_func(struct work_struct *work)
526 {
527 	__orderly_reboot();
528 }
529 
530 static DECLARE_WORK(reboot_work, reboot_work_func);
531 
532 /**
533  * orderly_reboot - Trigger an orderly system reboot
534  *
535  * This may be called from any context to trigger a system reboot.
536  * If the orderly reboot fails, it will force an immediate reboot.
537  */
orderly_reboot(void)538 void orderly_reboot(void)
539 {
540 	schedule_work(&reboot_work);
541 }
542 EXPORT_SYMBOL_GPL(orderly_reboot);
543 
reboot_setup(char * str)544 static int __init reboot_setup(char *str)
545 {
546 	for (;;) {
547 		enum reboot_mode *mode;
548 
549 		/*
550 		 * Having anything passed on the command line via
551 		 * reboot= will cause us to disable DMI checking
552 		 * below.
553 		 */
554 		reboot_default = 0;
555 
556 		if (!strncmp(str, "panic_", 6)) {
557 			mode = &panic_reboot_mode;
558 			str += 6;
559 		} else {
560 			mode = &reboot_mode;
561 		}
562 
563 		switch (*str) {
564 		case 'w':
565 			*mode = REBOOT_WARM;
566 			break;
567 
568 		case 'c':
569 			*mode = REBOOT_COLD;
570 			break;
571 
572 		case 'h':
573 			*mode = REBOOT_HARD;
574 			break;
575 
576 		case 's':
577 			if (isdigit(*(str+1)))
578 				reboot_cpu = simple_strtoul(str+1, NULL, 0);
579 			else if (str[1] == 'm' && str[2] == 'p' &&
580 							isdigit(*(str+3)))
581 				reboot_cpu = simple_strtoul(str+3, NULL, 0);
582 			else
583 				*mode = REBOOT_SOFT;
584 			if (reboot_cpu >= num_possible_cpus()) {
585 				pr_err("Ignoring the CPU number in reboot= option. "
586 				       "CPU %d exceeds possible cpu number %d\n",
587 				       reboot_cpu, num_possible_cpus());
588 				reboot_cpu = 0;
589 				break;
590 			}
591 			break;
592 
593 		case 'g':
594 			*mode = REBOOT_GPIO;
595 			break;
596 
597 		case 'b':
598 		case 'a':
599 		case 'k':
600 		case 't':
601 		case 'e':
602 		case 'p':
603 			reboot_type = *str;
604 			break;
605 
606 		case 'f':
607 			reboot_force = 1;
608 			break;
609 		}
610 
611 		str = strchr(str, ',');
612 		if (str)
613 			str++;
614 		else
615 			break;
616 	}
617 	return 1;
618 }
619 __setup("reboot=", reboot_setup);
620