xref: /linux-6.15/kernel/reboot.c (revision 6779db97)
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/atomic.h>
11 #include <linux/ctype.h>
12 #include <linux/export.h>
13 #include <linux/kexec.h>
14 #include <linux/kmod.h>
15 #include <linux/kmsg_dump.h>
16 #include <linux/reboot.h>
17 #include <linux/suspend.h>
18 #include <linux/syscalls.h>
19 #include <linux/syscore_ops.h>
20 #include <linux/uaccess.h>
21 
22 /*
23  * this indicates whether you can reboot with ctrl-alt-del: the default is yes
24  */
25 
26 int C_A_D = 1;
27 struct pid *cad_pid;
28 EXPORT_SYMBOL(cad_pid);
29 
30 #if defined(CONFIG_ARM)
31 #define DEFAULT_REBOOT_MODE		= REBOOT_HARD
32 #else
33 #define DEFAULT_REBOOT_MODE
34 #endif
35 enum reboot_mode reboot_mode DEFAULT_REBOOT_MODE;
36 EXPORT_SYMBOL_GPL(reboot_mode);
37 enum reboot_mode panic_reboot_mode = REBOOT_UNDEFINED;
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 struct sys_off_handler {
52 	struct notifier_block nb;
53 	int (*sys_off_cb)(struct sys_off_data *data);
54 	void *cb_data;
55 	enum sys_off_mode mode;
56 	bool blocking;
57 	void *list;
58 };
59 
60 /*
61  * Temporary stub that prevents linkage failure while we're in process
62  * of removing all uses of legacy pm_power_off() around the kernel.
63  */
64 void __weak (*pm_power_off)(void);
65 
66 /**
67  *	emergency_restart - reboot the system
68  *
69  *	Without shutting down any hardware or taking any locks
70  *	reboot the system.  This is called when we know we are in
71  *	trouble so this is our best effort to reboot.  This is
72  *	safe to call in interrupt context.
73  */
74 void emergency_restart(void)
75 {
76 	kmsg_dump(KMSG_DUMP_EMERG);
77 	machine_emergency_restart();
78 }
79 EXPORT_SYMBOL_GPL(emergency_restart);
80 
81 void kernel_restart_prepare(char *cmd)
82 {
83 	blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
84 	system_state = SYSTEM_RESTART;
85 	usermodehelper_disable();
86 	device_shutdown();
87 }
88 
89 /**
90  *	register_reboot_notifier - Register function to be called at reboot time
91  *	@nb: Info about notifier function to be called
92  *
93  *	Registers a function with the list of functions
94  *	to be called at reboot time.
95  *
96  *	Currently always returns zero, as blocking_notifier_chain_register()
97  *	always returns zero.
98  */
99 int register_reboot_notifier(struct notifier_block *nb)
100 {
101 	return blocking_notifier_chain_register(&reboot_notifier_list, nb);
102 }
103 EXPORT_SYMBOL(register_reboot_notifier);
104 
105 /**
106  *	unregister_reboot_notifier - Unregister previously registered reboot notifier
107  *	@nb: Hook to be unregistered
108  *
109  *	Unregisters a previously registered reboot
110  *	notifier function.
111  *
112  *	Returns zero on success, or %-ENOENT on failure.
113  */
114 int unregister_reboot_notifier(struct notifier_block *nb)
115 {
116 	return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
117 }
118 EXPORT_SYMBOL(unregister_reboot_notifier);
119 
120 static void devm_unregister_reboot_notifier(struct device *dev, void *res)
121 {
122 	WARN_ON(unregister_reboot_notifier(*(struct notifier_block **)res));
123 }
124 
125 int devm_register_reboot_notifier(struct device *dev, struct notifier_block *nb)
126 {
127 	struct notifier_block **rcnb;
128 	int ret;
129 
130 	rcnb = devres_alloc(devm_unregister_reboot_notifier,
131 			    sizeof(*rcnb), GFP_KERNEL);
132 	if (!rcnb)
133 		return -ENOMEM;
134 
135 	ret = register_reboot_notifier(nb);
136 	if (!ret) {
137 		*rcnb = nb;
138 		devres_add(dev, rcnb);
139 	} else {
140 		devres_free(rcnb);
141 	}
142 
143 	return ret;
144 }
145 EXPORT_SYMBOL(devm_register_reboot_notifier);
146 
147 /*
148  *	Notifier list for kernel code which wants to be called
149  *	to restart the system.
150  */
151 static ATOMIC_NOTIFIER_HEAD(restart_handler_list);
152 
153 /**
154  *	register_restart_handler - Register function to be called to reset
155  *				   the system
156  *	@nb: Info about handler function to be called
157  *	@nb->priority:	Handler priority. Handlers should follow the
158  *			following guidelines for setting priorities.
159  *			0:	Restart handler of last resort,
160  *				with limited restart capabilities
161  *			128:	Default restart handler; use if no other
162  *				restart handler is expected to be available,
163  *				and/or if restart functionality is
164  *				sufficient to restart the entire system
165  *			255:	Highest priority restart handler, will
166  *				preempt all other restart handlers
167  *
168  *	Registers a function with code to be called to restart the
169  *	system.
170  *
171  *	Registered functions will be called from machine_restart as last
172  *	step of the restart sequence (if the architecture specific
173  *	machine_restart function calls do_kernel_restart - see below
174  *	for details).
175  *	Registered functions are expected to restart the system immediately.
176  *	If more than one function is registered, the restart handler priority
177  *	selects which function will be called first.
178  *
179  *	Restart handlers are expected to be registered from non-architecture
180  *	code, typically from drivers. A typical use case would be a system
181  *	where restart functionality is provided through a watchdog. Multiple
182  *	restart handlers may exist; for example, one restart handler might
183  *	restart the entire system, while another only restarts the CPU.
184  *	In such cases, the restart handler which only restarts part of the
185  *	hardware is expected to register with low priority to ensure that
186  *	it only runs if no other means to restart the system is available.
187  *
188  *	Currently always returns zero, as atomic_notifier_chain_register()
189  *	always returns zero.
190  */
191 int register_restart_handler(struct notifier_block *nb)
192 {
193 	return atomic_notifier_chain_register(&restart_handler_list, nb);
194 }
195 EXPORT_SYMBOL(register_restart_handler);
196 
197 /**
198  *	unregister_restart_handler - Unregister previously registered
199  *				     restart handler
200  *	@nb: Hook to be unregistered
201  *
202  *	Unregisters a previously registered restart handler function.
203  *
204  *	Returns zero on success, or %-ENOENT on failure.
205  */
206 int unregister_restart_handler(struct notifier_block *nb)
207 {
208 	return atomic_notifier_chain_unregister(&restart_handler_list, nb);
209 }
210 EXPORT_SYMBOL(unregister_restart_handler);
211 
212 /**
213  *	do_kernel_restart - Execute kernel restart handler call chain
214  *
215  *	Calls functions registered with register_restart_handler.
216  *
217  *	Expected to be called from machine_restart as last step of the restart
218  *	sequence.
219  *
220  *	Restarts the system immediately if a restart handler function has been
221  *	registered. Otherwise does nothing.
222  */
223 void do_kernel_restart(char *cmd)
224 {
225 	atomic_notifier_call_chain(&restart_handler_list, reboot_mode, cmd);
226 }
227 
228 void migrate_to_reboot_cpu(void)
229 {
230 	/* The boot cpu is always logical cpu 0 */
231 	int cpu = reboot_cpu;
232 
233 	cpu_hotplug_disable();
234 
235 	/* Make certain the cpu I'm about to reboot on is online */
236 	if (!cpu_online(cpu))
237 		cpu = cpumask_first(cpu_online_mask);
238 
239 	/* Prevent races with other tasks migrating this task */
240 	current->flags |= PF_NO_SETAFFINITY;
241 
242 	/* Make certain I only run on the appropriate processor */
243 	set_cpus_allowed_ptr(current, cpumask_of(cpu));
244 }
245 
246 /**
247  *	kernel_restart - reboot the system
248  *	@cmd: pointer to buffer containing command to execute for restart
249  *		or %NULL
250  *
251  *	Shutdown everything and perform a clean reboot.
252  *	This is not safe to call in interrupt context.
253  */
254 void kernel_restart(char *cmd)
255 {
256 	kernel_restart_prepare(cmd);
257 	migrate_to_reboot_cpu();
258 	syscore_shutdown();
259 	if (!cmd)
260 		pr_emerg("Restarting system\n");
261 	else
262 		pr_emerg("Restarting system with command '%s'\n", cmd);
263 	kmsg_dump(KMSG_DUMP_SHUTDOWN);
264 	machine_restart(cmd);
265 }
266 EXPORT_SYMBOL_GPL(kernel_restart);
267 
268 static void kernel_shutdown_prepare(enum system_states state)
269 {
270 	blocking_notifier_call_chain(&reboot_notifier_list,
271 		(state == SYSTEM_HALT) ? SYS_HALT : SYS_POWER_OFF, NULL);
272 	system_state = state;
273 	usermodehelper_disable();
274 	device_shutdown();
275 }
276 /**
277  *	kernel_halt - halt the system
278  *
279  *	Shutdown everything and perform a clean system halt.
280  */
281 void kernel_halt(void)
282 {
283 	kernel_shutdown_prepare(SYSTEM_HALT);
284 	migrate_to_reboot_cpu();
285 	syscore_shutdown();
286 	pr_emerg("System halted\n");
287 	kmsg_dump(KMSG_DUMP_SHUTDOWN);
288 	machine_halt();
289 }
290 EXPORT_SYMBOL_GPL(kernel_halt);
291 
292 /*
293  *	Notifier list for kernel code which wants to be called
294  *	to prepare system for power off.
295  */
296 static BLOCKING_NOTIFIER_HEAD(power_off_prep_handler_list);
297 
298 /*
299  *	Notifier list for kernel code which wants to be called
300  *	to power off system.
301  */
302 static ATOMIC_NOTIFIER_HEAD(power_off_handler_list);
303 
304 static int sys_off_notify(struct notifier_block *nb,
305 			  unsigned long mode, void *cmd)
306 {
307 	struct sys_off_handler *handler;
308 	struct sys_off_data data = {};
309 
310 	handler = container_of(nb, struct sys_off_handler, nb);
311 	data.cb_data = handler->cb_data;
312 	data.mode = mode;
313 	data.cmd = cmd;
314 
315 	return handler->sys_off_cb(&data);
316 }
317 
318 /**
319  *	register_sys_off_handler - Register sys-off handler
320  *	@mode: Sys-off mode
321  *	@priority: Handler priority
322  *	@callback: Callback function
323  *	@cb_data: Callback argument
324  *
325  *	Registers system power-off or restart handler that will be invoked
326  *	at the step corresponding to the given sys-off mode. Handler's callback
327  *	should return NOTIFY_DONE to permit execution of the next handler in
328  *	the call chain or NOTIFY_STOP to break the chain (in error case for
329  *	example).
330  *
331  *	Multiple handlers can be registered at the default priority level.
332  *
333  *	Only one handler can be registered at the non-default priority level,
334  *	otherwise ERR_PTR(-EBUSY) is returned.
335  *
336  *	Returns a new instance of struct sys_off_handler on success, or
337  *	an ERR_PTR()-encoded error code otherwise.
338  */
339 struct sys_off_handler *
340 register_sys_off_handler(enum sys_off_mode mode,
341 			 int priority,
342 			 int (*callback)(struct sys_off_data *data),
343 			 void *cb_data)
344 {
345 	struct sys_off_handler *handler;
346 	int err;
347 
348 	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
349 	if (!handler)
350 		return ERR_PTR(-ENOMEM);
351 
352 	switch (mode) {
353 	case SYS_OFF_MODE_POWER_OFF_PREPARE:
354 		handler->list = &power_off_prep_handler_list;
355 		handler->blocking = true;
356 		break;
357 
358 	case SYS_OFF_MODE_POWER_OFF:
359 		handler->list = &power_off_handler_list;
360 		break;
361 
362 	case SYS_OFF_MODE_RESTART:
363 		handler->list = &restart_handler_list;
364 		break;
365 
366 	default:
367 		kfree(handler);
368 		return ERR_PTR(-EINVAL);
369 	}
370 
371 	handler->nb.notifier_call = sys_off_notify;
372 	handler->nb.priority = priority;
373 	handler->sys_off_cb = callback;
374 	handler->cb_data = cb_data;
375 	handler->mode = mode;
376 
377 	if (handler->blocking) {
378 		if (priority == SYS_OFF_PRIO_DEFAULT)
379 			err = blocking_notifier_chain_register(handler->list,
380 							       &handler->nb);
381 		else
382 			err = blocking_notifier_chain_register_unique_prio(handler->list,
383 									   &handler->nb);
384 	} else {
385 		if (priority == SYS_OFF_PRIO_DEFAULT)
386 			err = atomic_notifier_chain_register(handler->list,
387 							     &handler->nb);
388 		else
389 			err = atomic_notifier_chain_register_unique_prio(handler->list,
390 									 &handler->nb);
391 	}
392 
393 	if (err) {
394 		kfree(handler);
395 		return ERR_PTR(err);
396 	}
397 
398 	return handler;
399 }
400 EXPORT_SYMBOL_GPL(register_sys_off_handler);
401 
402 /**
403  *	unregister_sys_off_handler - Unregister sys-off handler
404  *	@handler: Sys-off handler
405  *
406  *	Unregisters given sys-off handler.
407  */
408 void unregister_sys_off_handler(struct sys_off_handler *handler)
409 {
410 	int err;
411 
412 	if (!handler)
413 		return;
414 
415 	if (handler->blocking)
416 		err = blocking_notifier_chain_unregister(handler->list,
417 							 &handler->nb);
418 	else
419 		err = atomic_notifier_chain_unregister(handler->list,
420 						       &handler->nb);
421 
422 	/* sanity check, shall never happen */
423 	WARN_ON(err);
424 
425 	kfree(handler);
426 }
427 EXPORT_SYMBOL_GPL(unregister_sys_off_handler);
428 
429 static void devm_unregister_sys_off_handler(void *data)
430 {
431 	struct sys_off_handler *handler = data;
432 
433 	unregister_sys_off_handler(handler);
434 }
435 
436 /**
437  *	devm_register_sys_off_handler - Register sys-off handler
438  *	@dev: Device that registers handler
439  *	@mode: Sys-off mode
440  *	@priority: Handler priority
441  *	@callback: Callback function
442  *	@cb_data: Callback argument
443  *
444  *	Registers resource-managed sys-off handler.
445  *
446  *	Returns zero on success, or error code on failure.
447  */
448 int devm_register_sys_off_handler(struct device *dev,
449 				  enum sys_off_mode mode,
450 				  int priority,
451 				  int (*callback)(struct sys_off_data *data),
452 				  void *cb_data)
453 {
454 	struct sys_off_handler *handler;
455 
456 	handler = register_sys_off_handler(mode, priority, callback, cb_data);
457 	if (IS_ERR(handler))
458 		return PTR_ERR(handler);
459 
460 	return devm_add_action_or_reset(dev, devm_unregister_sys_off_handler,
461 					handler);
462 }
463 EXPORT_SYMBOL_GPL(devm_register_sys_off_handler);
464 
465 /**
466  *	devm_register_power_off_handler - Register power-off handler
467  *	@dev: Device that registers callback
468  *	@callback: Callback function
469  *	@cb_data: Callback's argument
470  *
471  *	Registers resource-managed sys-off handler with a default priority
472  *	and using power-off mode.
473  *
474  *	Returns zero on success, or error code on failure.
475  */
476 int devm_register_power_off_handler(struct device *dev,
477 				    int (*callback)(struct sys_off_data *data),
478 				    void *cb_data)
479 {
480 	return devm_register_sys_off_handler(dev,
481 					     SYS_OFF_MODE_POWER_OFF,
482 					     SYS_OFF_PRIO_DEFAULT,
483 					     callback, cb_data);
484 }
485 EXPORT_SYMBOL_GPL(devm_register_power_off_handler);
486 
487 /**
488  *	devm_register_restart_handler - Register restart handler
489  *	@dev: Device that registers callback
490  *	@callback: Callback function
491  *	@cb_data: Callback's argument
492  *
493  *	Registers resource-managed sys-off handler with a default priority
494  *	and using restart mode.
495  *
496  *	Returns zero on success, or error code on failure.
497  */
498 int devm_register_restart_handler(struct device *dev,
499 				  int (*callback)(struct sys_off_data *data),
500 				  void *cb_data)
501 {
502 	return devm_register_sys_off_handler(dev,
503 					     SYS_OFF_MODE_RESTART,
504 					     SYS_OFF_PRIO_DEFAULT,
505 					     callback, cb_data);
506 }
507 EXPORT_SYMBOL_GPL(devm_register_restart_handler);
508 
509 static struct sys_off_handler *platform_power_off_handler;
510 
511 static int platform_power_off_notify(struct sys_off_data *data)
512 {
513 	void (*platform_power_power_off_cb)(void) = data->cb_data;
514 
515 	platform_power_power_off_cb();
516 
517 	return NOTIFY_DONE;
518 }
519 
520 /**
521  *	register_platform_power_off - Register platform-level power-off callback
522  *	@power_off: Power-off callback
523  *
524  *	Registers power-off callback that will be called as last step
525  *	of the power-off sequence. This callback is expected to be invoked
526  *	for the last resort. Only one platform power-off callback is allowed
527  *	to be registered at a time.
528  *
529  *	Returns zero on success, or error code on failure.
530  */
531 int register_platform_power_off(void (*power_off)(void))
532 {
533 	struct sys_off_handler *handler;
534 
535 	handler = register_sys_off_handler(SYS_OFF_MODE_POWER_OFF,
536 					   SYS_OFF_PRIO_PLATFORM,
537 					   platform_power_off_notify,
538 					   power_off);
539 	if (IS_ERR(handler))
540 		return PTR_ERR(handler);
541 
542 	platform_power_off_handler = handler;
543 
544 	return 0;
545 }
546 EXPORT_SYMBOL_GPL(register_platform_power_off);
547 
548 /**
549  *	unregister_platform_power_off - Unregister platform-level power-off callback
550  *	@power_off: Power-off callback
551  *
552  *	Unregisters previously registered platform power-off callback.
553  */
554 void unregister_platform_power_off(void (*power_off)(void))
555 {
556 	if (platform_power_off_handler &&
557 	    platform_power_off_handler->cb_data == power_off) {
558 		unregister_sys_off_handler(platform_power_off_handler);
559 		platform_power_off_handler = NULL;
560 	}
561 }
562 EXPORT_SYMBOL_GPL(unregister_platform_power_off);
563 
564 static int legacy_pm_power_off(struct sys_off_data *data)
565 {
566 	if (pm_power_off)
567 		pm_power_off();
568 
569 	return NOTIFY_DONE;
570 }
571 
572 /*
573  * Register sys-off handlers for legacy PM callbacks. This allows legacy
574  * PM callbacks co-exist with the new sys-off API.
575  *
576  * TODO: Remove legacy handlers once all legacy PM users will be switched
577  *       to the sys-off based APIs.
578  */
579 static int __init legacy_pm_init(void)
580 {
581 	register_sys_off_handler(SYS_OFF_MODE_POWER_OFF, SYS_OFF_PRIO_DEFAULT,
582 				 legacy_pm_power_off, NULL);
583 
584 	return 0;
585 }
586 core_initcall(legacy_pm_init);
587 
588 static void do_kernel_power_off_prepare(void)
589 {
590 	blocking_notifier_call_chain(&power_off_prep_handler_list, 0, NULL);
591 }
592 
593 /**
594  *	do_kernel_power_off - Execute kernel power-off handler call chain
595  *
596  *	Expected to be called as last step of the power-off sequence.
597  *
598  *	Powers off the system immediately if a power-off handler function has
599  *	been registered. Otherwise does nothing.
600  */
601 void do_kernel_power_off(void)
602 {
603 	atomic_notifier_call_chain(&power_off_handler_list, 0, NULL);
604 }
605 
606 /**
607  *	kernel_can_power_off - check whether system can be powered off
608  *
609  *	Returns true if power-off handler is registered and system can be
610  *	powered off, false otherwise.
611  */
612 bool kernel_can_power_off(void)
613 {
614 	return !atomic_notifier_call_chain_is_empty(&power_off_handler_list);
615 }
616 EXPORT_SYMBOL_GPL(kernel_can_power_off);
617 
618 /**
619  *	kernel_power_off - power_off the system
620  *
621  *	Shutdown everything and perform a clean system power_off.
622  */
623 void kernel_power_off(void)
624 {
625 	kernel_shutdown_prepare(SYSTEM_POWER_OFF);
626 	do_kernel_power_off_prepare();
627 	migrate_to_reboot_cpu();
628 	syscore_shutdown();
629 	pr_emerg("Power down\n");
630 	kmsg_dump(KMSG_DUMP_SHUTDOWN);
631 	machine_power_off();
632 }
633 EXPORT_SYMBOL_GPL(kernel_power_off);
634 
635 DEFINE_MUTEX(system_transition_mutex);
636 
637 /*
638  * Reboot system call: for obvious reasons only root may call it,
639  * and even root needs to set up some magic numbers in the registers
640  * so that some mistake won't make this reboot the whole machine.
641  * You can also set the meaning of the ctrl-alt-del-key here.
642  *
643  * reboot doesn't sync: do that yourself before calling this.
644  */
645 SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
646 		void __user *, arg)
647 {
648 	struct pid_namespace *pid_ns = task_active_pid_ns(current);
649 	char buffer[256];
650 	int ret = 0;
651 
652 	/* We only trust the superuser with rebooting the system. */
653 	if (!ns_capable(pid_ns->user_ns, CAP_SYS_BOOT))
654 		return -EPERM;
655 
656 	/* For safety, we require "magic" arguments. */
657 	if (magic1 != LINUX_REBOOT_MAGIC1 ||
658 			(magic2 != LINUX_REBOOT_MAGIC2 &&
659 			magic2 != LINUX_REBOOT_MAGIC2A &&
660 			magic2 != LINUX_REBOOT_MAGIC2B &&
661 			magic2 != LINUX_REBOOT_MAGIC2C))
662 		return -EINVAL;
663 
664 	/*
665 	 * If pid namespaces are enabled and the current task is in a child
666 	 * pid_namespace, the command is handled by reboot_pid_ns() which will
667 	 * call do_exit().
668 	 */
669 	ret = reboot_pid_ns(pid_ns, cmd);
670 	if (ret)
671 		return ret;
672 
673 	/* Instead of trying to make the power_off code look like
674 	 * halt when pm_power_off is not set do it the easy way.
675 	 */
676 	if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !kernel_can_power_off())
677 		cmd = LINUX_REBOOT_CMD_HALT;
678 
679 	mutex_lock(&system_transition_mutex);
680 	switch (cmd) {
681 	case LINUX_REBOOT_CMD_RESTART:
682 		kernel_restart(NULL);
683 		break;
684 
685 	case LINUX_REBOOT_CMD_CAD_ON:
686 		C_A_D = 1;
687 		break;
688 
689 	case LINUX_REBOOT_CMD_CAD_OFF:
690 		C_A_D = 0;
691 		break;
692 
693 	case LINUX_REBOOT_CMD_HALT:
694 		kernel_halt();
695 		do_exit(0);
696 
697 	case LINUX_REBOOT_CMD_POWER_OFF:
698 		kernel_power_off();
699 		do_exit(0);
700 		break;
701 
702 	case LINUX_REBOOT_CMD_RESTART2:
703 		ret = strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1);
704 		if (ret < 0) {
705 			ret = -EFAULT;
706 			break;
707 		}
708 		buffer[sizeof(buffer) - 1] = '\0';
709 
710 		kernel_restart(buffer);
711 		break;
712 
713 #ifdef CONFIG_KEXEC_CORE
714 	case LINUX_REBOOT_CMD_KEXEC:
715 		ret = kernel_kexec();
716 		break;
717 #endif
718 
719 #ifdef CONFIG_HIBERNATION
720 	case LINUX_REBOOT_CMD_SW_SUSPEND:
721 		ret = hibernate();
722 		break;
723 #endif
724 
725 	default:
726 		ret = -EINVAL;
727 		break;
728 	}
729 	mutex_unlock(&system_transition_mutex);
730 	return ret;
731 }
732 
733 static void deferred_cad(struct work_struct *dummy)
734 {
735 	kernel_restart(NULL);
736 }
737 
738 /*
739  * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
740  * As it's called within an interrupt, it may NOT sync: the only choice
741  * is whether to reboot at once, or just ignore the ctrl-alt-del.
742  */
743 void ctrl_alt_del(void)
744 {
745 	static DECLARE_WORK(cad_work, deferred_cad);
746 
747 	if (C_A_D)
748 		schedule_work(&cad_work);
749 	else
750 		kill_cad_pid(SIGINT, 1);
751 }
752 
753 char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
754 static const char reboot_cmd[] = "/sbin/reboot";
755 
756 static int run_cmd(const char *cmd)
757 {
758 	char **argv;
759 	static char *envp[] = {
760 		"HOME=/",
761 		"PATH=/sbin:/bin:/usr/sbin:/usr/bin",
762 		NULL
763 	};
764 	int ret;
765 	argv = argv_split(GFP_KERNEL, cmd, NULL);
766 	if (argv) {
767 		ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
768 		argv_free(argv);
769 	} else {
770 		ret = -ENOMEM;
771 	}
772 
773 	return ret;
774 }
775 
776 static int __orderly_reboot(void)
777 {
778 	int ret;
779 
780 	ret = run_cmd(reboot_cmd);
781 
782 	if (ret) {
783 		pr_warn("Failed to start orderly reboot: forcing the issue\n");
784 		emergency_sync();
785 		kernel_restart(NULL);
786 	}
787 
788 	return ret;
789 }
790 
791 static int __orderly_poweroff(bool force)
792 {
793 	int ret;
794 
795 	ret = run_cmd(poweroff_cmd);
796 
797 	if (ret && force) {
798 		pr_warn("Failed to start orderly shutdown: forcing the issue\n");
799 
800 		/*
801 		 * I guess this should try to kick off some daemon to sync and
802 		 * poweroff asap.  Or not even bother syncing if we're doing an
803 		 * emergency shutdown?
804 		 */
805 		emergency_sync();
806 		kernel_power_off();
807 	}
808 
809 	return ret;
810 }
811 
812 static bool poweroff_force;
813 
814 static void poweroff_work_func(struct work_struct *work)
815 {
816 	__orderly_poweroff(poweroff_force);
817 }
818 
819 static DECLARE_WORK(poweroff_work, poweroff_work_func);
820 
821 /**
822  * orderly_poweroff - Trigger an orderly system poweroff
823  * @force: force poweroff if command execution fails
824  *
825  * This may be called from any context to trigger a system shutdown.
826  * If the orderly shutdown fails, it will force an immediate shutdown.
827  */
828 void orderly_poweroff(bool force)
829 {
830 	if (force) /* do not override the pending "true" */
831 		poweroff_force = true;
832 	schedule_work(&poweroff_work);
833 }
834 EXPORT_SYMBOL_GPL(orderly_poweroff);
835 
836 static void reboot_work_func(struct work_struct *work)
837 {
838 	__orderly_reboot();
839 }
840 
841 static DECLARE_WORK(reboot_work, reboot_work_func);
842 
843 /**
844  * orderly_reboot - Trigger an orderly system reboot
845  *
846  * This may be called from any context to trigger a system reboot.
847  * If the orderly reboot fails, it will force an immediate reboot.
848  */
849 void orderly_reboot(void)
850 {
851 	schedule_work(&reboot_work);
852 }
853 EXPORT_SYMBOL_GPL(orderly_reboot);
854 
855 /**
856  * hw_failure_emergency_poweroff_func - emergency poweroff work after a known delay
857  * @work: work_struct associated with the emergency poweroff function
858  *
859  * This function is called in very critical situations to force
860  * a kernel poweroff after a configurable timeout value.
861  */
862 static void hw_failure_emergency_poweroff_func(struct work_struct *work)
863 {
864 	/*
865 	 * We have reached here after the emergency shutdown waiting period has
866 	 * expired. This means orderly_poweroff has not been able to shut off
867 	 * the system for some reason.
868 	 *
869 	 * Try to shut down the system immediately using kernel_power_off
870 	 * if populated
871 	 */
872 	pr_emerg("Hardware protection timed-out. Trying forced poweroff\n");
873 	kernel_power_off();
874 
875 	/*
876 	 * Worst of the worst case trigger emergency restart
877 	 */
878 	pr_emerg("Hardware protection shutdown failed. Trying emergency restart\n");
879 	emergency_restart();
880 }
881 
882 static DECLARE_DELAYED_WORK(hw_failure_emergency_poweroff_work,
883 			    hw_failure_emergency_poweroff_func);
884 
885 /**
886  * hw_failure_emergency_poweroff - Trigger an emergency system poweroff
887  *
888  * This may be called from any critical situation to trigger a system shutdown
889  * after a given period of time. If time is negative this is not scheduled.
890  */
891 static void hw_failure_emergency_poweroff(int poweroff_delay_ms)
892 {
893 	if (poweroff_delay_ms <= 0)
894 		return;
895 	schedule_delayed_work(&hw_failure_emergency_poweroff_work,
896 			      msecs_to_jiffies(poweroff_delay_ms));
897 }
898 
899 /**
900  * hw_protection_shutdown - Trigger an emergency system poweroff
901  *
902  * @reason:		Reason of emergency shutdown to be printed.
903  * @ms_until_forced:	Time to wait for orderly shutdown before tiggering a
904  *			forced shudown. Negative value disables the forced
905  *			shutdown.
906  *
907  * Initiate an emergency system shutdown in order to protect hardware from
908  * further damage. Usage examples include a thermal protection or a voltage or
909  * current regulator failures.
910  * NOTE: The request is ignored if protection shutdown is already pending even
911  * if the previous request has given a large timeout for forced shutdown.
912  * Can be called from any context.
913  */
914 void hw_protection_shutdown(const char *reason, int ms_until_forced)
915 {
916 	static atomic_t allow_proceed = ATOMIC_INIT(1);
917 
918 	pr_emerg("HARDWARE PROTECTION shutdown (%s)\n", reason);
919 
920 	/* Shutdown should be initiated only once. */
921 	if (!atomic_dec_and_test(&allow_proceed))
922 		return;
923 
924 	/*
925 	 * Queue a backup emergency shutdown in the event of
926 	 * orderly_poweroff failure
927 	 */
928 	hw_failure_emergency_poweroff(ms_until_forced);
929 	orderly_poweroff(true);
930 }
931 EXPORT_SYMBOL_GPL(hw_protection_shutdown);
932 
933 static int __init reboot_setup(char *str)
934 {
935 	for (;;) {
936 		enum reboot_mode *mode;
937 
938 		/*
939 		 * Having anything passed on the command line via
940 		 * reboot= will cause us to disable DMI checking
941 		 * below.
942 		 */
943 		reboot_default = 0;
944 
945 		if (!strncmp(str, "panic_", 6)) {
946 			mode = &panic_reboot_mode;
947 			str += 6;
948 		} else {
949 			mode = &reboot_mode;
950 		}
951 
952 		switch (*str) {
953 		case 'w':
954 			*mode = REBOOT_WARM;
955 			break;
956 
957 		case 'c':
958 			*mode = REBOOT_COLD;
959 			break;
960 
961 		case 'h':
962 			*mode = REBOOT_HARD;
963 			break;
964 
965 		case 's':
966 			/*
967 			 * reboot_cpu is s[mp]#### with #### being the processor
968 			 * to be used for rebooting. Skip 's' or 'smp' prefix.
969 			 */
970 			str += str[1] == 'm' && str[2] == 'p' ? 3 : 1;
971 
972 			if (isdigit(str[0])) {
973 				int cpu = simple_strtoul(str, NULL, 0);
974 
975 				if (cpu >= num_possible_cpus()) {
976 					pr_err("Ignoring the CPU number in reboot= option. "
977 					"CPU %d exceeds possible cpu number %d\n",
978 					cpu, num_possible_cpus());
979 					break;
980 				}
981 				reboot_cpu = cpu;
982 			} else
983 				*mode = REBOOT_SOFT;
984 			break;
985 
986 		case 'g':
987 			*mode = REBOOT_GPIO;
988 			break;
989 
990 		case 'b':
991 		case 'a':
992 		case 'k':
993 		case 't':
994 		case 'e':
995 		case 'p':
996 			reboot_type = *str;
997 			break;
998 
999 		case 'f':
1000 			reboot_force = 1;
1001 			break;
1002 		}
1003 
1004 		str = strchr(str, ',');
1005 		if (str)
1006 			str++;
1007 		else
1008 			break;
1009 	}
1010 	return 1;
1011 }
1012 __setup("reboot=", reboot_setup);
1013 
1014 #ifdef CONFIG_SYSFS
1015 
1016 #define REBOOT_COLD_STR		"cold"
1017 #define REBOOT_WARM_STR		"warm"
1018 #define REBOOT_HARD_STR		"hard"
1019 #define REBOOT_SOFT_STR		"soft"
1020 #define REBOOT_GPIO_STR		"gpio"
1021 #define REBOOT_UNDEFINED_STR	"undefined"
1022 
1023 #define BOOT_TRIPLE_STR		"triple"
1024 #define BOOT_KBD_STR		"kbd"
1025 #define BOOT_BIOS_STR		"bios"
1026 #define BOOT_ACPI_STR		"acpi"
1027 #define BOOT_EFI_STR		"efi"
1028 #define BOOT_PCI_STR		"pci"
1029 
1030 static ssize_t mode_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
1031 {
1032 	const char *val;
1033 
1034 	switch (reboot_mode) {
1035 	case REBOOT_COLD:
1036 		val = REBOOT_COLD_STR;
1037 		break;
1038 	case REBOOT_WARM:
1039 		val = REBOOT_WARM_STR;
1040 		break;
1041 	case REBOOT_HARD:
1042 		val = REBOOT_HARD_STR;
1043 		break;
1044 	case REBOOT_SOFT:
1045 		val = REBOOT_SOFT_STR;
1046 		break;
1047 	case REBOOT_GPIO:
1048 		val = REBOOT_GPIO_STR;
1049 		break;
1050 	default:
1051 		val = REBOOT_UNDEFINED_STR;
1052 	}
1053 
1054 	return sprintf(buf, "%s\n", val);
1055 }
1056 static ssize_t mode_store(struct kobject *kobj, struct kobj_attribute *attr,
1057 			  const char *buf, size_t count)
1058 {
1059 	if (!capable(CAP_SYS_BOOT))
1060 		return -EPERM;
1061 
1062 	if (!strncmp(buf, REBOOT_COLD_STR, strlen(REBOOT_COLD_STR)))
1063 		reboot_mode = REBOOT_COLD;
1064 	else if (!strncmp(buf, REBOOT_WARM_STR, strlen(REBOOT_WARM_STR)))
1065 		reboot_mode = REBOOT_WARM;
1066 	else if (!strncmp(buf, REBOOT_HARD_STR, strlen(REBOOT_HARD_STR)))
1067 		reboot_mode = REBOOT_HARD;
1068 	else if (!strncmp(buf, REBOOT_SOFT_STR, strlen(REBOOT_SOFT_STR)))
1069 		reboot_mode = REBOOT_SOFT;
1070 	else if (!strncmp(buf, REBOOT_GPIO_STR, strlen(REBOOT_GPIO_STR)))
1071 		reboot_mode = REBOOT_GPIO;
1072 	else
1073 		return -EINVAL;
1074 
1075 	reboot_default = 0;
1076 
1077 	return count;
1078 }
1079 static struct kobj_attribute reboot_mode_attr = __ATTR_RW(mode);
1080 
1081 #ifdef CONFIG_X86
1082 static ssize_t force_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
1083 {
1084 	return sprintf(buf, "%d\n", reboot_force);
1085 }
1086 static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr,
1087 			  const char *buf, size_t count)
1088 {
1089 	bool res;
1090 
1091 	if (!capable(CAP_SYS_BOOT))
1092 		return -EPERM;
1093 
1094 	if (kstrtobool(buf, &res))
1095 		return -EINVAL;
1096 
1097 	reboot_default = 0;
1098 	reboot_force = res;
1099 
1100 	return count;
1101 }
1102 static struct kobj_attribute reboot_force_attr = __ATTR_RW(force);
1103 
1104 static ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
1105 {
1106 	const char *val;
1107 
1108 	switch (reboot_type) {
1109 	case BOOT_TRIPLE:
1110 		val = BOOT_TRIPLE_STR;
1111 		break;
1112 	case BOOT_KBD:
1113 		val = BOOT_KBD_STR;
1114 		break;
1115 	case BOOT_BIOS:
1116 		val = BOOT_BIOS_STR;
1117 		break;
1118 	case BOOT_ACPI:
1119 		val = BOOT_ACPI_STR;
1120 		break;
1121 	case BOOT_EFI:
1122 		val = BOOT_EFI_STR;
1123 		break;
1124 	case BOOT_CF9_FORCE:
1125 		val = BOOT_PCI_STR;
1126 		break;
1127 	default:
1128 		val = REBOOT_UNDEFINED_STR;
1129 	}
1130 
1131 	return sprintf(buf, "%s\n", val);
1132 }
1133 static ssize_t type_store(struct kobject *kobj, struct kobj_attribute *attr,
1134 			  const char *buf, size_t count)
1135 {
1136 	if (!capable(CAP_SYS_BOOT))
1137 		return -EPERM;
1138 
1139 	if (!strncmp(buf, BOOT_TRIPLE_STR, strlen(BOOT_TRIPLE_STR)))
1140 		reboot_type = BOOT_TRIPLE;
1141 	else if (!strncmp(buf, BOOT_KBD_STR, strlen(BOOT_KBD_STR)))
1142 		reboot_type = BOOT_KBD;
1143 	else if (!strncmp(buf, BOOT_BIOS_STR, strlen(BOOT_BIOS_STR)))
1144 		reboot_type = BOOT_BIOS;
1145 	else if (!strncmp(buf, BOOT_ACPI_STR, strlen(BOOT_ACPI_STR)))
1146 		reboot_type = BOOT_ACPI;
1147 	else if (!strncmp(buf, BOOT_EFI_STR, strlen(BOOT_EFI_STR)))
1148 		reboot_type = BOOT_EFI;
1149 	else if (!strncmp(buf, BOOT_PCI_STR, strlen(BOOT_PCI_STR)))
1150 		reboot_type = BOOT_CF9_FORCE;
1151 	else
1152 		return -EINVAL;
1153 
1154 	reboot_default = 0;
1155 
1156 	return count;
1157 }
1158 static struct kobj_attribute reboot_type_attr = __ATTR_RW(type);
1159 #endif
1160 
1161 #ifdef CONFIG_SMP
1162 static ssize_t cpu_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
1163 {
1164 	return sprintf(buf, "%d\n", reboot_cpu);
1165 }
1166 static ssize_t cpu_store(struct kobject *kobj, struct kobj_attribute *attr,
1167 			  const char *buf, size_t count)
1168 {
1169 	unsigned int cpunum;
1170 	int rc;
1171 
1172 	if (!capable(CAP_SYS_BOOT))
1173 		return -EPERM;
1174 
1175 	rc = kstrtouint(buf, 0, &cpunum);
1176 
1177 	if (rc)
1178 		return rc;
1179 
1180 	if (cpunum >= num_possible_cpus())
1181 		return -ERANGE;
1182 
1183 	reboot_default = 0;
1184 	reboot_cpu = cpunum;
1185 
1186 	return count;
1187 }
1188 static struct kobj_attribute reboot_cpu_attr = __ATTR_RW(cpu);
1189 #endif
1190 
1191 static struct attribute *reboot_attrs[] = {
1192 	&reboot_mode_attr.attr,
1193 #ifdef CONFIG_X86
1194 	&reboot_force_attr.attr,
1195 	&reboot_type_attr.attr,
1196 #endif
1197 #ifdef CONFIG_SMP
1198 	&reboot_cpu_attr.attr,
1199 #endif
1200 	NULL,
1201 };
1202 
1203 static const struct attribute_group reboot_attr_group = {
1204 	.attrs = reboot_attrs,
1205 };
1206 
1207 static int __init reboot_ksysfs_init(void)
1208 {
1209 	struct kobject *reboot_kobj;
1210 	int ret;
1211 
1212 	reboot_kobj = kobject_create_and_add("reboot", kernel_kobj);
1213 	if (!reboot_kobj)
1214 		return -ENOMEM;
1215 
1216 	ret = sysfs_create_group(reboot_kobj, &reboot_attr_group);
1217 	if (ret) {
1218 		kobject_put(reboot_kobj);
1219 		return ret;
1220 	}
1221 
1222 	return 0;
1223 }
1224 late_initcall(reboot_ksysfs_init);
1225 
1226 #endif
1227