xref: /openbmc/linux/kernel/reboot.c (revision fb61375ecfba49e153af561402f49f6fe3bebd39)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
215d94b82SRobin Holt /*
315d94b82SRobin Holt  *  linux/kernel/reboot.c
415d94b82SRobin Holt  *
515d94b82SRobin Holt  *  Copyright (C) 2013  Linus Torvalds
615d94b82SRobin Holt  */
715d94b82SRobin Holt 
8972ee83dSRobin Holt #define pr_fmt(fmt)	"reboot: " fmt
9972ee83dSRobin Holt 
10dfa19b11SMatti Vaittinen #include <linux/atomic.h>
111b3a5d02SRobin Holt #include <linux/ctype.h>
1215d94b82SRobin Holt #include <linux/export.h>
1315d94b82SRobin Holt #include <linux/kexec.h>
1415d94b82SRobin Holt #include <linux/kmod.h>
1515d94b82SRobin Holt #include <linux/kmsg_dump.h>
1615d94b82SRobin Holt #include <linux/reboot.h>
1715d94b82SRobin Holt #include <linux/suspend.h>
1815d94b82SRobin Holt #include <linux/syscalls.h>
1915d94b82SRobin Holt #include <linux/syscore_ops.h>
2015d94b82SRobin Holt #include <linux/uaccess.h>
2115d94b82SRobin Holt 
2215d94b82SRobin Holt /*
2315d94b82SRobin Holt  * this indicates whether you can reboot with ctrl-alt-del: the default is yes
2415d94b82SRobin Holt  */
2515d94b82SRobin Holt 
2615d94b82SRobin Holt int C_A_D = 1;
2715d94b82SRobin Holt struct pid *cad_pid;
2815d94b82SRobin Holt EXPORT_SYMBOL(cad_pid);
2915d94b82SRobin Holt 
30fb37409aSMike Rapoport #if defined(CONFIG_ARM)
311b3a5d02SRobin Holt #define DEFAULT_REBOOT_MODE		= REBOOT_HARD
321b3a5d02SRobin Holt #else
331b3a5d02SRobin Holt #define DEFAULT_REBOOT_MODE
341b3a5d02SRobin Holt #endif
351b3a5d02SRobin Holt enum reboot_mode reboot_mode DEFAULT_REBOOT_MODE;
36d46b3f5bSShawn Guo EXPORT_SYMBOL_GPL(reboot_mode);
37b287a25aSAaro Koskinen enum reboot_mode panic_reboot_mode = REBOOT_UNDEFINED;
381b3a5d02SRobin Holt 
39e2f0b88eSChuansheng Liu /*
40e2f0b88eSChuansheng Liu  * This variable is used privately to keep track of whether or not
41e2f0b88eSChuansheng Liu  * reboot_type is still set to its default value (i.e., reboot= hasn't
42e2f0b88eSChuansheng Liu  * been set on the command line).  This is needed so that we can
43e2f0b88eSChuansheng Liu  * suppress DMI scanning for reboot quirks.  Without it, it's
44e2f0b88eSChuansheng Liu  * impossible to override a faulty reboot quirk without recompiling.
45e2f0b88eSChuansheng Liu  */
46e2f0b88eSChuansheng Liu int reboot_default = 1;
471b3a5d02SRobin Holt int reboot_cpu;
481b3a5d02SRobin Holt enum reboot_type reboot_type = BOOT_ACPI;
491b3a5d02SRobin Holt int reboot_force;
501b3a5d02SRobin Holt 
51232edc2fSDmitry Osipenko struct sys_off_handler {
52232edc2fSDmitry Osipenko 	struct notifier_block nb;
53232edc2fSDmitry Osipenko 	int (*sys_off_cb)(struct sys_off_data *data);
54232edc2fSDmitry Osipenko 	void *cb_data;
55232edc2fSDmitry Osipenko 	enum sys_off_mode mode;
56232edc2fSDmitry Osipenko 	bool blocking;
57232edc2fSDmitry Osipenko 	void *list;
58232edc2fSDmitry Osipenko };
59232edc2fSDmitry Osipenko 
6015d94b82SRobin Holt /*
615d34b41aSDmitry Osipenko  * Temporary stub that prevents linkage failure while we're in process
625d34b41aSDmitry Osipenko  * of removing all uses of legacy pm_power_off() around the kernel.
635d34b41aSDmitry Osipenko  */
645d34b41aSDmitry Osipenko void __weak (*pm_power_off)(void);
655d34b41aSDmitry Osipenko 
665d34b41aSDmitry Osipenko /*
6715d94b82SRobin Holt  * If set, this is used for preparing the system to power off.
6815d94b82SRobin Holt  */
6915d94b82SRobin Holt 
7015d94b82SRobin Holt void (*pm_power_off_prepare)(void);
7174f008f2SOleksij Rempel EXPORT_SYMBOL_GPL(pm_power_off_prepare);
7215d94b82SRobin Holt 
7315d94b82SRobin Holt /**
7415d94b82SRobin Holt  *	emergency_restart - reboot the system
7515d94b82SRobin Holt  *
7615d94b82SRobin Holt  *	Without shutting down any hardware or taking any locks
7715d94b82SRobin Holt  *	reboot the system.  This is called when we know we are in
7815d94b82SRobin Holt  *	trouble so this is our best effort to reboot.  This is
7915d94b82SRobin Holt  *	safe to call in interrupt context.
8015d94b82SRobin Holt  */
8115d94b82SRobin Holt void emergency_restart(void)
8215d94b82SRobin Holt {
8315d94b82SRobin Holt 	kmsg_dump(KMSG_DUMP_EMERG);
8415d94b82SRobin Holt 	machine_emergency_restart();
8515d94b82SRobin Holt }
8615d94b82SRobin Holt EXPORT_SYMBOL_GPL(emergency_restart);
8715d94b82SRobin Holt 
8815d94b82SRobin Holt void kernel_restart_prepare(char *cmd)
8915d94b82SRobin Holt {
9015d94b82SRobin Holt 	blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
9115d94b82SRobin Holt 	system_state = SYSTEM_RESTART;
9215d94b82SRobin Holt 	usermodehelper_disable();
9315d94b82SRobin Holt 	device_shutdown();
9415d94b82SRobin Holt }
9515d94b82SRobin Holt 
9615d94b82SRobin Holt /**
9715d94b82SRobin Holt  *	register_reboot_notifier - Register function to be called at reboot time
9815d94b82SRobin Holt  *	@nb: Info about notifier function to be called
9915d94b82SRobin Holt  *
10015d94b82SRobin Holt  *	Registers a function with the list of functions
10115d94b82SRobin Holt  *	to be called at reboot time.
10215d94b82SRobin Holt  *
10315d94b82SRobin Holt  *	Currently always returns zero, as blocking_notifier_chain_register()
10415d94b82SRobin Holt  *	always returns zero.
10515d94b82SRobin Holt  */
10615d94b82SRobin Holt int register_reboot_notifier(struct notifier_block *nb)
10715d94b82SRobin Holt {
10815d94b82SRobin Holt 	return blocking_notifier_chain_register(&reboot_notifier_list, nb);
10915d94b82SRobin Holt }
11015d94b82SRobin Holt EXPORT_SYMBOL(register_reboot_notifier);
11115d94b82SRobin Holt 
11215d94b82SRobin Holt /**
11315d94b82SRobin Holt  *	unregister_reboot_notifier - Unregister previously registered reboot notifier
11415d94b82SRobin Holt  *	@nb: Hook to be unregistered
11515d94b82SRobin Holt  *
11615d94b82SRobin Holt  *	Unregisters a previously registered reboot
11715d94b82SRobin Holt  *	notifier function.
11815d94b82SRobin Holt  *
11915d94b82SRobin Holt  *	Returns zero on success, or %-ENOENT on failure.
12015d94b82SRobin Holt  */
12115d94b82SRobin Holt int unregister_reboot_notifier(struct notifier_block *nb)
12215d94b82SRobin Holt {
12315d94b82SRobin Holt 	return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
12415d94b82SRobin Holt }
12515d94b82SRobin Holt EXPORT_SYMBOL(unregister_reboot_notifier);
12615d94b82SRobin Holt 
1272d8364baSAndrey Smirnov static void devm_unregister_reboot_notifier(struct device *dev, void *res)
1282d8364baSAndrey Smirnov {
1292d8364baSAndrey Smirnov 	WARN_ON(unregister_reboot_notifier(*(struct notifier_block **)res));
1302d8364baSAndrey Smirnov }
1312d8364baSAndrey Smirnov 
1322d8364baSAndrey Smirnov int devm_register_reboot_notifier(struct device *dev, struct notifier_block *nb)
1332d8364baSAndrey Smirnov {
1342d8364baSAndrey Smirnov 	struct notifier_block **rcnb;
1352d8364baSAndrey Smirnov 	int ret;
1362d8364baSAndrey Smirnov 
1372d8364baSAndrey Smirnov 	rcnb = devres_alloc(devm_unregister_reboot_notifier,
1382d8364baSAndrey Smirnov 			    sizeof(*rcnb), GFP_KERNEL);
1392d8364baSAndrey Smirnov 	if (!rcnb)
1402d8364baSAndrey Smirnov 		return -ENOMEM;
1412d8364baSAndrey Smirnov 
1422d8364baSAndrey Smirnov 	ret = register_reboot_notifier(nb);
1432d8364baSAndrey Smirnov 	if (!ret) {
1442d8364baSAndrey Smirnov 		*rcnb = nb;
1452d8364baSAndrey Smirnov 		devres_add(dev, rcnb);
1462d8364baSAndrey Smirnov 	} else {
1472d8364baSAndrey Smirnov 		devres_free(rcnb);
1482d8364baSAndrey Smirnov 	}
1492d8364baSAndrey Smirnov 
1502d8364baSAndrey Smirnov 	return ret;
1512d8364baSAndrey Smirnov }
1522d8364baSAndrey Smirnov EXPORT_SYMBOL(devm_register_reboot_notifier);
1532d8364baSAndrey Smirnov 
154b63adb97SGuenter Roeck /*
155b63adb97SGuenter Roeck  *	Notifier list for kernel code which wants to be called
156b63adb97SGuenter Roeck  *	to restart the system.
157b63adb97SGuenter Roeck  */
158b63adb97SGuenter Roeck static ATOMIC_NOTIFIER_HEAD(restart_handler_list);
159b63adb97SGuenter Roeck 
160b63adb97SGuenter Roeck /**
161b63adb97SGuenter Roeck  *	register_restart_handler - Register function to be called to reset
162b63adb97SGuenter Roeck  *				   the system
163b63adb97SGuenter Roeck  *	@nb: Info about handler function to be called
164b63adb97SGuenter Roeck  *	@nb->priority:	Handler priority. Handlers should follow the
165b63adb97SGuenter Roeck  *			following guidelines for setting priorities.
166b63adb97SGuenter Roeck  *			0:	Restart handler of last resort,
167b63adb97SGuenter Roeck  *				with limited restart capabilities
168b63adb97SGuenter Roeck  *			128:	Default restart handler; use if no other
169b63adb97SGuenter Roeck  *				restart handler is expected to be available,
170b63adb97SGuenter Roeck  *				and/or if restart functionality is
171b63adb97SGuenter Roeck  *				sufficient to restart the entire system
172b63adb97SGuenter Roeck  *			255:	Highest priority restart handler, will
173b63adb97SGuenter Roeck  *				preempt all other restart handlers
174b63adb97SGuenter Roeck  *
175b63adb97SGuenter Roeck  *	Registers a function with code to be called to restart the
176b63adb97SGuenter Roeck  *	system.
177b63adb97SGuenter Roeck  *
178b63adb97SGuenter Roeck  *	Registered functions will be called from machine_restart as last
179b63adb97SGuenter Roeck  *	step of the restart sequence (if the architecture specific
180b63adb97SGuenter Roeck  *	machine_restart function calls do_kernel_restart - see below
181b63adb97SGuenter Roeck  *	for details).
182b63adb97SGuenter Roeck  *	Registered functions are expected to restart the system immediately.
183b63adb97SGuenter Roeck  *	If more than one function is registered, the restart handler priority
184b63adb97SGuenter Roeck  *	selects which function will be called first.
185b63adb97SGuenter Roeck  *
186b63adb97SGuenter Roeck  *	Restart handlers are expected to be registered from non-architecture
187b63adb97SGuenter Roeck  *	code, typically from drivers. A typical use case would be a system
188b63adb97SGuenter Roeck  *	where restart functionality is provided through a watchdog. Multiple
189b63adb97SGuenter Roeck  *	restart handlers may exist; for example, one restart handler might
190b63adb97SGuenter Roeck  *	restart the entire system, while another only restarts the CPU.
191b63adb97SGuenter Roeck  *	In such cases, the restart handler which only restarts part of the
192b63adb97SGuenter Roeck  *	hardware is expected to register with low priority to ensure that
193b63adb97SGuenter Roeck  *	it only runs if no other means to restart the system is available.
194b63adb97SGuenter Roeck  *
195b63adb97SGuenter Roeck  *	Currently always returns zero, as atomic_notifier_chain_register()
196b63adb97SGuenter Roeck  *	always returns zero.
197b63adb97SGuenter Roeck  */
198b63adb97SGuenter Roeck int register_restart_handler(struct notifier_block *nb)
199b63adb97SGuenter Roeck {
200b63adb97SGuenter Roeck 	return atomic_notifier_chain_register(&restart_handler_list, nb);
201b63adb97SGuenter Roeck }
202b63adb97SGuenter Roeck EXPORT_SYMBOL(register_restart_handler);
203b63adb97SGuenter Roeck 
204b63adb97SGuenter Roeck /**
205b63adb97SGuenter Roeck  *	unregister_restart_handler - Unregister previously registered
206b63adb97SGuenter Roeck  *				     restart handler
207b63adb97SGuenter Roeck  *	@nb: Hook to be unregistered
208b63adb97SGuenter Roeck  *
209b63adb97SGuenter Roeck  *	Unregisters a previously registered restart handler function.
210b63adb97SGuenter Roeck  *
211b63adb97SGuenter Roeck  *	Returns zero on success, or %-ENOENT on failure.
212b63adb97SGuenter Roeck  */
213b63adb97SGuenter Roeck int unregister_restart_handler(struct notifier_block *nb)
214b63adb97SGuenter Roeck {
215b63adb97SGuenter Roeck 	return atomic_notifier_chain_unregister(&restart_handler_list, nb);
216b63adb97SGuenter Roeck }
217b63adb97SGuenter Roeck EXPORT_SYMBOL(unregister_restart_handler);
218b63adb97SGuenter Roeck 
219b63adb97SGuenter Roeck /**
220b63adb97SGuenter Roeck  *	do_kernel_restart - Execute kernel restart handler call chain
221b63adb97SGuenter Roeck  *
222b63adb97SGuenter Roeck  *	Calls functions registered with register_restart_handler.
223b63adb97SGuenter Roeck  *
224b63adb97SGuenter Roeck  *	Expected to be called from machine_restart as last step of the restart
225b63adb97SGuenter Roeck  *	sequence.
226b63adb97SGuenter Roeck  *
227b63adb97SGuenter Roeck  *	Restarts the system immediately if a restart handler function has been
228b63adb97SGuenter Roeck  *	registered. Otherwise does nothing.
229b63adb97SGuenter Roeck  */
230b63adb97SGuenter Roeck void do_kernel_restart(char *cmd)
231b63adb97SGuenter Roeck {
232b63adb97SGuenter Roeck 	atomic_notifier_call_chain(&restart_handler_list, reboot_mode, cmd);
233b63adb97SGuenter Roeck }
234b63adb97SGuenter Roeck 
235c97102baSVivek Goyal void migrate_to_reboot_cpu(void)
23615d94b82SRobin Holt {
23715d94b82SRobin Holt 	/* The boot cpu is always logical cpu 0 */
2381b3a5d02SRobin Holt 	int cpu = reboot_cpu;
23915d94b82SRobin Holt 
24015d94b82SRobin Holt 	cpu_hotplug_disable();
24115d94b82SRobin Holt 
24215d94b82SRobin Holt 	/* Make certain the cpu I'm about to reboot on is online */
24315d94b82SRobin Holt 	if (!cpu_online(cpu))
24415d94b82SRobin Holt 		cpu = cpumask_first(cpu_online_mask);
24515d94b82SRobin Holt 
24615d94b82SRobin Holt 	/* Prevent races with other tasks migrating this task */
24715d94b82SRobin Holt 	current->flags |= PF_NO_SETAFFINITY;
24815d94b82SRobin Holt 
24915d94b82SRobin Holt 	/* Make certain I only run on the appropriate processor */
25015d94b82SRobin Holt 	set_cpus_allowed_ptr(current, cpumask_of(cpu));
25115d94b82SRobin Holt }
25215d94b82SRobin Holt 
25315d94b82SRobin Holt /**
25415d94b82SRobin Holt  *	kernel_restart - reboot the system
25515d94b82SRobin Holt  *	@cmd: pointer to buffer containing command to execute for restart
25615d94b82SRobin Holt  *		or %NULL
25715d94b82SRobin Holt  *
25815d94b82SRobin Holt  *	Shutdown everything and perform a clean reboot.
25915d94b82SRobin Holt  *	This is not safe to call in interrupt context.
26015d94b82SRobin Holt  */
26115d94b82SRobin Holt void kernel_restart(char *cmd)
26215d94b82SRobin Holt {
26315d94b82SRobin Holt 	kernel_restart_prepare(cmd);
26415d94b82SRobin Holt 	migrate_to_reboot_cpu();
26515d94b82SRobin Holt 	syscore_shutdown();
26615d94b82SRobin Holt 	if (!cmd)
267972ee83dSRobin Holt 		pr_emerg("Restarting system\n");
26815d94b82SRobin Holt 	else
269972ee83dSRobin Holt 		pr_emerg("Restarting system with command '%s'\n", cmd);
2706d3cf962SKees Cook 	kmsg_dump(KMSG_DUMP_SHUTDOWN);
27115d94b82SRobin Holt 	machine_restart(cmd);
27215d94b82SRobin Holt }
27315d94b82SRobin Holt EXPORT_SYMBOL_GPL(kernel_restart);
27415d94b82SRobin Holt 
27515d94b82SRobin Holt static void kernel_shutdown_prepare(enum system_states state)
27615d94b82SRobin Holt {
27715d94b82SRobin Holt 	blocking_notifier_call_chain(&reboot_notifier_list,
27815d94b82SRobin Holt 		(state == SYSTEM_HALT) ? SYS_HALT : SYS_POWER_OFF, NULL);
27915d94b82SRobin Holt 	system_state = state;
28015d94b82SRobin Holt 	usermodehelper_disable();
28115d94b82SRobin Holt 	device_shutdown();
28215d94b82SRobin Holt }
28315d94b82SRobin Holt /**
28415d94b82SRobin Holt  *	kernel_halt - halt the system
28515d94b82SRobin Holt  *
28615d94b82SRobin Holt  *	Shutdown everything and perform a clean system halt.
28715d94b82SRobin Holt  */
28815d94b82SRobin Holt void kernel_halt(void)
28915d94b82SRobin Holt {
29015d94b82SRobin Holt 	kernel_shutdown_prepare(SYSTEM_HALT);
29115d94b82SRobin Holt 	migrate_to_reboot_cpu();
29215d94b82SRobin Holt 	syscore_shutdown();
293972ee83dSRobin Holt 	pr_emerg("System halted\n");
2946d3cf962SKees Cook 	kmsg_dump(KMSG_DUMP_SHUTDOWN);
29515d94b82SRobin Holt 	machine_halt();
29615d94b82SRobin Holt }
29715d94b82SRobin Holt EXPORT_SYMBOL_GPL(kernel_halt);
29815d94b82SRobin Holt 
299232edc2fSDmitry Osipenko /*
300232edc2fSDmitry Osipenko  *	Notifier list for kernel code which wants to be called
301232edc2fSDmitry Osipenko  *	to prepare system for power off.
302232edc2fSDmitry Osipenko  */
303232edc2fSDmitry Osipenko static BLOCKING_NOTIFIER_HEAD(power_off_prep_handler_list);
304232edc2fSDmitry Osipenko 
305232edc2fSDmitry Osipenko /*
306232edc2fSDmitry Osipenko  *	Notifier list for kernel code which wants to be called
307232edc2fSDmitry Osipenko  *	to power off system.
308232edc2fSDmitry Osipenko  */
309232edc2fSDmitry Osipenko static ATOMIC_NOTIFIER_HEAD(power_off_handler_list);
310232edc2fSDmitry Osipenko 
311232edc2fSDmitry Osipenko static int sys_off_notify(struct notifier_block *nb,
312232edc2fSDmitry Osipenko 			  unsigned long mode, void *cmd)
313232edc2fSDmitry Osipenko {
314232edc2fSDmitry Osipenko 	struct sys_off_handler *handler;
315232edc2fSDmitry Osipenko 	struct sys_off_data data = {};
316232edc2fSDmitry Osipenko 
317232edc2fSDmitry Osipenko 	handler = container_of(nb, struct sys_off_handler, nb);
318232edc2fSDmitry Osipenko 	data.cb_data = handler->cb_data;
319232edc2fSDmitry Osipenko 	data.mode = mode;
320232edc2fSDmitry Osipenko 	data.cmd = cmd;
321232edc2fSDmitry Osipenko 
322232edc2fSDmitry Osipenko 	return handler->sys_off_cb(&data);
323232edc2fSDmitry Osipenko }
324232edc2fSDmitry Osipenko 
325232edc2fSDmitry Osipenko /**
326232edc2fSDmitry Osipenko  *	register_sys_off_handler - Register sys-off handler
327232edc2fSDmitry Osipenko  *	@mode: Sys-off mode
328232edc2fSDmitry Osipenko  *	@priority: Handler priority
329232edc2fSDmitry Osipenko  *	@callback: Callback function
330232edc2fSDmitry Osipenko  *	@cb_data: Callback argument
331232edc2fSDmitry Osipenko  *
332232edc2fSDmitry Osipenko  *	Registers system power-off or restart handler that will be invoked
333232edc2fSDmitry Osipenko  *	at the step corresponding to the given sys-off mode. Handler's callback
334232edc2fSDmitry Osipenko  *	should return NOTIFY_DONE to permit execution of the next handler in
335232edc2fSDmitry Osipenko  *	the call chain or NOTIFY_STOP to break the chain (in error case for
336232edc2fSDmitry Osipenko  *	example).
337232edc2fSDmitry Osipenko  *
338232edc2fSDmitry Osipenko  *	Multiple handlers can be registered at the default priority level.
339232edc2fSDmitry Osipenko  *
340232edc2fSDmitry Osipenko  *	Only one handler can be registered at the non-default priority level,
341232edc2fSDmitry Osipenko  *	otherwise ERR_PTR(-EBUSY) is returned.
342232edc2fSDmitry Osipenko  *
343232edc2fSDmitry Osipenko  *	Returns a new instance of struct sys_off_handler on success, or
344232edc2fSDmitry Osipenko  *	an ERR_PTR()-encoded error code otherwise.
345232edc2fSDmitry Osipenko  */
346232edc2fSDmitry Osipenko struct sys_off_handler *
347232edc2fSDmitry Osipenko register_sys_off_handler(enum sys_off_mode mode,
348232edc2fSDmitry Osipenko 			 int priority,
349232edc2fSDmitry Osipenko 			 int (*callback)(struct sys_off_data *data),
350232edc2fSDmitry Osipenko 			 void *cb_data)
351232edc2fSDmitry Osipenko {
352232edc2fSDmitry Osipenko 	struct sys_off_handler *handler;
353232edc2fSDmitry Osipenko 	int err;
354232edc2fSDmitry Osipenko 
355232edc2fSDmitry Osipenko 	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
356232edc2fSDmitry Osipenko 	if (!handler)
357232edc2fSDmitry Osipenko 		return ERR_PTR(-ENOMEM);
358232edc2fSDmitry Osipenko 
359232edc2fSDmitry Osipenko 	switch (mode) {
360232edc2fSDmitry Osipenko 	case SYS_OFF_MODE_POWER_OFF_PREPARE:
361232edc2fSDmitry Osipenko 		handler->list = &power_off_prep_handler_list;
362232edc2fSDmitry Osipenko 		handler->blocking = true;
363232edc2fSDmitry Osipenko 		break;
364232edc2fSDmitry Osipenko 
365232edc2fSDmitry Osipenko 	case SYS_OFF_MODE_POWER_OFF:
366232edc2fSDmitry Osipenko 		handler->list = &power_off_handler_list;
367232edc2fSDmitry Osipenko 		break;
368232edc2fSDmitry Osipenko 
369232edc2fSDmitry Osipenko 	case SYS_OFF_MODE_RESTART:
370232edc2fSDmitry Osipenko 		handler->list = &restart_handler_list;
371232edc2fSDmitry Osipenko 		break;
372232edc2fSDmitry Osipenko 
373232edc2fSDmitry Osipenko 	default:
374232edc2fSDmitry Osipenko 		kfree(handler);
375232edc2fSDmitry Osipenko 		return ERR_PTR(-EINVAL);
376232edc2fSDmitry Osipenko 	}
377232edc2fSDmitry Osipenko 
378232edc2fSDmitry Osipenko 	handler->nb.notifier_call = sys_off_notify;
379232edc2fSDmitry Osipenko 	handler->nb.priority = priority;
380232edc2fSDmitry Osipenko 	handler->sys_off_cb = callback;
381232edc2fSDmitry Osipenko 	handler->cb_data = cb_data;
382232edc2fSDmitry Osipenko 	handler->mode = mode;
383232edc2fSDmitry Osipenko 
384232edc2fSDmitry Osipenko 	if (handler->blocking) {
385232edc2fSDmitry Osipenko 		if (priority == SYS_OFF_PRIO_DEFAULT)
386232edc2fSDmitry Osipenko 			err = blocking_notifier_chain_register(handler->list,
387232edc2fSDmitry Osipenko 							       &handler->nb);
388232edc2fSDmitry Osipenko 		else
389232edc2fSDmitry Osipenko 			err = blocking_notifier_chain_register_unique_prio(handler->list,
390232edc2fSDmitry Osipenko 									   &handler->nb);
391232edc2fSDmitry Osipenko 	} else {
392232edc2fSDmitry Osipenko 		if (priority == SYS_OFF_PRIO_DEFAULT)
393232edc2fSDmitry Osipenko 			err = atomic_notifier_chain_register(handler->list,
394232edc2fSDmitry Osipenko 							     &handler->nb);
395232edc2fSDmitry Osipenko 		else
396232edc2fSDmitry Osipenko 			err = atomic_notifier_chain_register_unique_prio(handler->list,
397232edc2fSDmitry Osipenko 									 &handler->nb);
398232edc2fSDmitry Osipenko 	}
399232edc2fSDmitry Osipenko 
400232edc2fSDmitry Osipenko 	if (err) {
401232edc2fSDmitry Osipenko 		kfree(handler);
402232edc2fSDmitry Osipenko 		return ERR_PTR(err);
403232edc2fSDmitry Osipenko 	}
404232edc2fSDmitry Osipenko 
405232edc2fSDmitry Osipenko 	return handler;
406232edc2fSDmitry Osipenko }
407232edc2fSDmitry Osipenko EXPORT_SYMBOL_GPL(register_sys_off_handler);
408232edc2fSDmitry Osipenko 
409232edc2fSDmitry Osipenko /**
410232edc2fSDmitry Osipenko  *	unregister_sys_off_handler - Unregister sys-off handler
411232edc2fSDmitry Osipenko  *	@handler: Sys-off handler
412232edc2fSDmitry Osipenko  *
413232edc2fSDmitry Osipenko  *	Unregisters given sys-off handler.
414232edc2fSDmitry Osipenko  */
415232edc2fSDmitry Osipenko void unregister_sys_off_handler(struct sys_off_handler *handler)
416232edc2fSDmitry Osipenko {
417232edc2fSDmitry Osipenko 	int err;
418232edc2fSDmitry Osipenko 
419232edc2fSDmitry Osipenko 	if (!handler)
420232edc2fSDmitry Osipenko 		return;
421232edc2fSDmitry Osipenko 
422232edc2fSDmitry Osipenko 	if (handler->blocking)
423232edc2fSDmitry Osipenko 		err = blocking_notifier_chain_unregister(handler->list,
424232edc2fSDmitry Osipenko 							 &handler->nb);
425232edc2fSDmitry Osipenko 	else
426232edc2fSDmitry Osipenko 		err = atomic_notifier_chain_unregister(handler->list,
427232edc2fSDmitry Osipenko 						       &handler->nb);
428232edc2fSDmitry Osipenko 
429232edc2fSDmitry Osipenko 	/* sanity check, shall never happen */
430232edc2fSDmitry Osipenko 	WARN_ON(err);
431232edc2fSDmitry Osipenko 
432232edc2fSDmitry Osipenko 	kfree(handler);
433232edc2fSDmitry Osipenko }
434232edc2fSDmitry Osipenko EXPORT_SYMBOL_GPL(unregister_sys_off_handler);
435232edc2fSDmitry Osipenko 
436232edc2fSDmitry Osipenko static void devm_unregister_sys_off_handler(void *data)
437232edc2fSDmitry Osipenko {
438232edc2fSDmitry Osipenko 	struct sys_off_handler *handler = data;
439232edc2fSDmitry Osipenko 
440232edc2fSDmitry Osipenko 	unregister_sys_off_handler(handler);
441232edc2fSDmitry Osipenko }
442232edc2fSDmitry Osipenko 
443232edc2fSDmitry Osipenko /**
444232edc2fSDmitry Osipenko  *	devm_register_sys_off_handler - Register sys-off handler
445232edc2fSDmitry Osipenko  *	@dev: Device that registers handler
446232edc2fSDmitry Osipenko  *	@mode: Sys-off mode
447232edc2fSDmitry Osipenko  *	@priority: Handler priority
448232edc2fSDmitry Osipenko  *	@callback: Callback function
449232edc2fSDmitry Osipenko  *	@cb_data: Callback argument
450232edc2fSDmitry Osipenko  *
451232edc2fSDmitry Osipenko  *	Registers resource-managed sys-off handler.
452232edc2fSDmitry Osipenko  *
453232edc2fSDmitry Osipenko  *	Returns zero on success, or error code on failure.
454232edc2fSDmitry Osipenko  */
455232edc2fSDmitry Osipenko int devm_register_sys_off_handler(struct device *dev,
456232edc2fSDmitry Osipenko 				  enum sys_off_mode mode,
457232edc2fSDmitry Osipenko 				  int priority,
458232edc2fSDmitry Osipenko 				  int (*callback)(struct sys_off_data *data),
459232edc2fSDmitry Osipenko 				  void *cb_data)
460232edc2fSDmitry Osipenko {
461232edc2fSDmitry Osipenko 	struct sys_off_handler *handler;
462232edc2fSDmitry Osipenko 
463232edc2fSDmitry Osipenko 	handler = register_sys_off_handler(mode, priority, callback, cb_data);
464232edc2fSDmitry Osipenko 	if (IS_ERR(handler))
465232edc2fSDmitry Osipenko 		return PTR_ERR(handler);
466232edc2fSDmitry Osipenko 
467232edc2fSDmitry Osipenko 	return devm_add_action_or_reset(dev, devm_unregister_sys_off_handler,
468232edc2fSDmitry Osipenko 					handler);
469232edc2fSDmitry Osipenko }
470232edc2fSDmitry Osipenko EXPORT_SYMBOL_GPL(devm_register_sys_off_handler);
471232edc2fSDmitry Osipenko 
472*fb61375eSDmitry Osipenko static struct sys_off_handler *platform_power_off_handler;
473*fb61375eSDmitry Osipenko 
474*fb61375eSDmitry Osipenko static int platform_power_off_notify(struct sys_off_data *data)
475*fb61375eSDmitry Osipenko {
476*fb61375eSDmitry Osipenko 	void (*platform_power_power_off_cb)(void) = data->cb_data;
477*fb61375eSDmitry Osipenko 
478*fb61375eSDmitry Osipenko 	platform_power_power_off_cb();
479*fb61375eSDmitry Osipenko 
480*fb61375eSDmitry Osipenko 	return NOTIFY_DONE;
481*fb61375eSDmitry Osipenko }
482*fb61375eSDmitry Osipenko 
483*fb61375eSDmitry Osipenko /**
484*fb61375eSDmitry Osipenko  *	register_platform_power_off - Register platform-level power-off callback
485*fb61375eSDmitry Osipenko  *	@power_off: Power-off callback
486*fb61375eSDmitry Osipenko  *
487*fb61375eSDmitry Osipenko  *	Registers power-off callback that will be called as last step
488*fb61375eSDmitry Osipenko  *	of the power-off sequence. This callback is expected to be invoked
489*fb61375eSDmitry Osipenko  *	for the last resort. Only one platform power-off callback is allowed
490*fb61375eSDmitry Osipenko  *	to be registered at a time.
491*fb61375eSDmitry Osipenko  *
492*fb61375eSDmitry Osipenko  *	Returns zero on success, or error code on failure.
493*fb61375eSDmitry Osipenko  */
494*fb61375eSDmitry Osipenko int register_platform_power_off(void (*power_off)(void))
495*fb61375eSDmitry Osipenko {
496*fb61375eSDmitry Osipenko 	struct sys_off_handler *handler;
497*fb61375eSDmitry Osipenko 
498*fb61375eSDmitry Osipenko 	handler = register_sys_off_handler(SYS_OFF_MODE_POWER_OFF,
499*fb61375eSDmitry Osipenko 					   SYS_OFF_PRIO_PLATFORM,
500*fb61375eSDmitry Osipenko 					   platform_power_off_notify,
501*fb61375eSDmitry Osipenko 					   power_off);
502*fb61375eSDmitry Osipenko 	if (IS_ERR(handler))
503*fb61375eSDmitry Osipenko 		return PTR_ERR(handler);
504*fb61375eSDmitry Osipenko 
505*fb61375eSDmitry Osipenko 	platform_power_off_handler = handler;
506*fb61375eSDmitry Osipenko 
507*fb61375eSDmitry Osipenko 	return 0;
508*fb61375eSDmitry Osipenko }
509*fb61375eSDmitry Osipenko EXPORT_SYMBOL_GPL(register_platform_power_off);
510*fb61375eSDmitry Osipenko 
511*fb61375eSDmitry Osipenko /**
512*fb61375eSDmitry Osipenko  *	unregister_platform_power_off - Unregister platform-level power-off callback
513*fb61375eSDmitry Osipenko  *	@power_off: Power-off callback
514*fb61375eSDmitry Osipenko  *
515*fb61375eSDmitry Osipenko  *	Unregisters previously registered platform power-off callback.
516*fb61375eSDmitry Osipenko  */
517*fb61375eSDmitry Osipenko void unregister_platform_power_off(void (*power_off)(void))
518*fb61375eSDmitry Osipenko {
519*fb61375eSDmitry Osipenko 	if (platform_power_off_handler &&
520*fb61375eSDmitry Osipenko 	    platform_power_off_handler->cb_data == power_off) {
521*fb61375eSDmitry Osipenko 		unregister_sys_off_handler(platform_power_off_handler);
522*fb61375eSDmitry Osipenko 		platform_power_off_handler = NULL;
523*fb61375eSDmitry Osipenko 	}
524*fb61375eSDmitry Osipenko }
525*fb61375eSDmitry Osipenko EXPORT_SYMBOL_GPL(unregister_platform_power_off);
526*fb61375eSDmitry Osipenko 
5277b9a3de9SDmitry Osipenko static int legacy_pm_power_off_prepare(struct sys_off_data *data)
5287b9a3de9SDmitry Osipenko {
5297b9a3de9SDmitry Osipenko 	if (pm_power_off_prepare)
5307b9a3de9SDmitry Osipenko 		pm_power_off_prepare();
5317b9a3de9SDmitry Osipenko 
5327b9a3de9SDmitry Osipenko 	return NOTIFY_DONE;
5337b9a3de9SDmitry Osipenko }
5347b9a3de9SDmitry Osipenko 
5357b9a3de9SDmitry Osipenko static int legacy_pm_power_off(struct sys_off_data *data)
5367b9a3de9SDmitry Osipenko {
5377b9a3de9SDmitry Osipenko 	if (pm_power_off)
5387b9a3de9SDmitry Osipenko 		pm_power_off();
5397b9a3de9SDmitry Osipenko 
5407b9a3de9SDmitry Osipenko 	return NOTIFY_DONE;
5417b9a3de9SDmitry Osipenko }
5427b9a3de9SDmitry Osipenko 
5437b9a3de9SDmitry Osipenko /*
5447b9a3de9SDmitry Osipenko  * Register sys-off handlers for legacy PM callbacks. This allows legacy
5457b9a3de9SDmitry Osipenko  * PM callbacks co-exist with the new sys-off API.
5467b9a3de9SDmitry Osipenko  *
5477b9a3de9SDmitry Osipenko  * TODO: Remove legacy handlers once all legacy PM users will be switched
5487b9a3de9SDmitry Osipenko  *       to the sys-off based APIs.
5497b9a3de9SDmitry Osipenko  */
5507b9a3de9SDmitry Osipenko static int __init legacy_pm_init(void)
5517b9a3de9SDmitry Osipenko {
5527b9a3de9SDmitry Osipenko 	register_sys_off_handler(SYS_OFF_MODE_POWER_OFF_PREPARE,
5537b9a3de9SDmitry Osipenko 				 SYS_OFF_PRIO_DEFAULT,
5547b9a3de9SDmitry Osipenko 				 legacy_pm_power_off_prepare, NULL);
5557b9a3de9SDmitry Osipenko 
5567b9a3de9SDmitry Osipenko 	register_sys_off_handler(SYS_OFF_MODE_POWER_OFF, SYS_OFF_PRIO_DEFAULT,
5577b9a3de9SDmitry Osipenko 				 legacy_pm_power_off, NULL);
5587b9a3de9SDmitry Osipenko 
5597b9a3de9SDmitry Osipenko 	return 0;
5607b9a3de9SDmitry Osipenko }
5617b9a3de9SDmitry Osipenko core_initcall(legacy_pm_init);
5627b9a3de9SDmitry Osipenko 
5637b9a3de9SDmitry Osipenko static void do_kernel_power_off_prepare(void)
5647b9a3de9SDmitry Osipenko {
5657b9a3de9SDmitry Osipenko 	blocking_notifier_call_chain(&power_off_prep_handler_list, 0, NULL);
5667b9a3de9SDmitry Osipenko }
5677b9a3de9SDmitry Osipenko 
56815d94b82SRobin Holt /**
5692b6aa733SDmitry Osipenko  *	do_kernel_power_off - Execute kernel power-off handler call chain
5702b6aa733SDmitry Osipenko  *
5712b6aa733SDmitry Osipenko  *	Expected to be called as last step of the power-off sequence.
5722b6aa733SDmitry Osipenko  *
5732b6aa733SDmitry Osipenko  *	Powers off the system immediately if a power-off handler function has
5742b6aa733SDmitry Osipenko  *	been registered. Otherwise does nothing.
5752b6aa733SDmitry Osipenko  */
5762b6aa733SDmitry Osipenko void do_kernel_power_off(void)
5772b6aa733SDmitry Osipenko {
5782b6aa733SDmitry Osipenko 	atomic_notifier_call_chain(&power_off_handler_list, 0, NULL);
5792b6aa733SDmitry Osipenko }
5802b6aa733SDmitry Osipenko 
5812b6aa733SDmitry Osipenko /**
5820e2110d2SDmitry Osipenko  *	kernel_can_power_off - check whether system can be powered off
5830e2110d2SDmitry Osipenko  *
5840e2110d2SDmitry Osipenko  *	Returns true if power-off handler is registered and system can be
5850e2110d2SDmitry Osipenko  *	powered off, false otherwise.
5860e2110d2SDmitry Osipenko  */
5870e2110d2SDmitry Osipenko bool kernel_can_power_off(void)
5880e2110d2SDmitry Osipenko {
5890e2110d2SDmitry Osipenko 	return !atomic_notifier_call_chain_is_empty(&power_off_handler_list);
5900e2110d2SDmitry Osipenko }
5910e2110d2SDmitry Osipenko EXPORT_SYMBOL_GPL(kernel_can_power_off);
5920e2110d2SDmitry Osipenko 
5930e2110d2SDmitry Osipenko /**
59415d94b82SRobin Holt  *	kernel_power_off - power_off the system
59515d94b82SRobin Holt  *
59615d94b82SRobin Holt  *	Shutdown everything and perform a clean system power_off.
59715d94b82SRobin Holt  */
59815d94b82SRobin Holt void kernel_power_off(void)
59915d94b82SRobin Holt {
60015d94b82SRobin Holt 	kernel_shutdown_prepare(SYSTEM_POWER_OFF);
6017b9a3de9SDmitry Osipenko 	do_kernel_power_off_prepare();
60215d94b82SRobin Holt 	migrate_to_reboot_cpu();
60315d94b82SRobin Holt 	syscore_shutdown();
604972ee83dSRobin Holt 	pr_emerg("Power down\n");
6056d3cf962SKees Cook 	kmsg_dump(KMSG_DUMP_SHUTDOWN);
60615d94b82SRobin Holt 	machine_power_off();
60715d94b82SRobin Holt }
60815d94b82SRobin Holt EXPORT_SYMBOL_GPL(kernel_power_off);
60915d94b82SRobin Holt 
61055f2503cSPingfan Liu DEFINE_MUTEX(system_transition_mutex);
61115d94b82SRobin Holt 
61215d94b82SRobin Holt /*
61315d94b82SRobin Holt  * Reboot system call: for obvious reasons only root may call it,
61415d94b82SRobin Holt  * and even root needs to set up some magic numbers in the registers
61515d94b82SRobin Holt  * so that some mistake won't make this reboot the whole machine.
61615d94b82SRobin Holt  * You can also set the meaning of the ctrl-alt-del-key here.
61715d94b82SRobin Holt  *
61815d94b82SRobin Holt  * reboot doesn't sync: do that yourself before calling this.
61915d94b82SRobin Holt  */
62015d94b82SRobin Holt SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
62115d94b82SRobin Holt 		void __user *, arg)
62215d94b82SRobin Holt {
62315d94b82SRobin Holt 	struct pid_namespace *pid_ns = task_active_pid_ns(current);
62415d94b82SRobin Holt 	char buffer[256];
62515d94b82SRobin Holt 	int ret = 0;
62615d94b82SRobin Holt 
62715d94b82SRobin Holt 	/* We only trust the superuser with rebooting the system. */
62815d94b82SRobin Holt 	if (!ns_capable(pid_ns->user_ns, CAP_SYS_BOOT))
62915d94b82SRobin Holt 		return -EPERM;
63015d94b82SRobin Holt 
63115d94b82SRobin Holt 	/* For safety, we require "magic" arguments. */
63215d94b82SRobin Holt 	if (magic1 != LINUX_REBOOT_MAGIC1 ||
63315d94b82SRobin Holt 			(magic2 != LINUX_REBOOT_MAGIC2 &&
63415d94b82SRobin Holt 			magic2 != LINUX_REBOOT_MAGIC2A &&
63515d94b82SRobin Holt 			magic2 != LINUX_REBOOT_MAGIC2B &&
63615d94b82SRobin Holt 			magic2 != LINUX_REBOOT_MAGIC2C))
63715d94b82SRobin Holt 		return -EINVAL;
63815d94b82SRobin Holt 
63915d94b82SRobin Holt 	/*
64015d94b82SRobin Holt 	 * If pid namespaces are enabled and the current task is in a child
64115d94b82SRobin Holt 	 * pid_namespace, the command is handled by reboot_pid_ns() which will
64215d94b82SRobin Holt 	 * call do_exit().
64315d94b82SRobin Holt 	 */
64415d94b82SRobin Holt 	ret = reboot_pid_ns(pid_ns, cmd);
64515d94b82SRobin Holt 	if (ret)
64615d94b82SRobin Holt 		return ret;
64715d94b82SRobin Holt 
64815d94b82SRobin Holt 	/* Instead of trying to make the power_off code look like
64915d94b82SRobin Holt 	 * halt when pm_power_off is not set do it the easy way.
65015d94b82SRobin Holt 	 */
6510e2110d2SDmitry Osipenko 	if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !kernel_can_power_off())
65215d94b82SRobin Holt 		cmd = LINUX_REBOOT_CMD_HALT;
65315d94b82SRobin Holt 
65455f2503cSPingfan Liu 	mutex_lock(&system_transition_mutex);
65515d94b82SRobin Holt 	switch (cmd) {
65615d94b82SRobin Holt 	case LINUX_REBOOT_CMD_RESTART:
65715d94b82SRobin Holt 		kernel_restart(NULL);
65815d94b82SRobin Holt 		break;
65915d94b82SRobin Holt 
66015d94b82SRobin Holt 	case LINUX_REBOOT_CMD_CAD_ON:
66115d94b82SRobin Holt 		C_A_D = 1;
66215d94b82SRobin Holt 		break;
66315d94b82SRobin Holt 
66415d94b82SRobin Holt 	case LINUX_REBOOT_CMD_CAD_OFF:
66515d94b82SRobin Holt 		C_A_D = 0;
66615d94b82SRobin Holt 		break;
66715d94b82SRobin Holt 
66815d94b82SRobin Holt 	case LINUX_REBOOT_CMD_HALT:
66915d94b82SRobin Holt 		kernel_halt();
67015d94b82SRobin Holt 		do_exit(0);
67115d94b82SRobin Holt 
67215d94b82SRobin Holt 	case LINUX_REBOOT_CMD_POWER_OFF:
67315d94b82SRobin Holt 		kernel_power_off();
67415d94b82SRobin Holt 		do_exit(0);
67515d94b82SRobin Holt 		break;
67615d94b82SRobin Holt 
67715d94b82SRobin Holt 	case LINUX_REBOOT_CMD_RESTART2:
678972ee83dSRobin Holt 		ret = strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1);
679972ee83dSRobin Holt 		if (ret < 0) {
68015d94b82SRobin Holt 			ret = -EFAULT;
68115d94b82SRobin Holt 			break;
68215d94b82SRobin Holt 		}
68315d94b82SRobin Holt 		buffer[sizeof(buffer) - 1] = '\0';
68415d94b82SRobin Holt 
68515d94b82SRobin Holt 		kernel_restart(buffer);
68615d94b82SRobin Holt 		break;
68715d94b82SRobin Holt 
6882965faa5SDave Young #ifdef CONFIG_KEXEC_CORE
68915d94b82SRobin Holt 	case LINUX_REBOOT_CMD_KEXEC:
69015d94b82SRobin Holt 		ret = kernel_kexec();
69115d94b82SRobin Holt 		break;
69215d94b82SRobin Holt #endif
69315d94b82SRobin Holt 
69415d94b82SRobin Holt #ifdef CONFIG_HIBERNATION
69515d94b82SRobin Holt 	case LINUX_REBOOT_CMD_SW_SUSPEND:
69615d94b82SRobin Holt 		ret = hibernate();
69715d94b82SRobin Holt 		break;
69815d94b82SRobin Holt #endif
69915d94b82SRobin Holt 
70015d94b82SRobin Holt 	default:
70115d94b82SRobin Holt 		ret = -EINVAL;
70215d94b82SRobin Holt 		break;
70315d94b82SRobin Holt 	}
70455f2503cSPingfan Liu 	mutex_unlock(&system_transition_mutex);
70515d94b82SRobin Holt 	return ret;
70615d94b82SRobin Holt }
70715d94b82SRobin Holt 
70815d94b82SRobin Holt static void deferred_cad(struct work_struct *dummy)
70915d94b82SRobin Holt {
71015d94b82SRobin Holt 	kernel_restart(NULL);
71115d94b82SRobin Holt }
71215d94b82SRobin Holt 
71315d94b82SRobin Holt /*
71415d94b82SRobin Holt  * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
71515d94b82SRobin Holt  * As it's called within an interrupt, it may NOT sync: the only choice
71615d94b82SRobin Holt  * is whether to reboot at once, or just ignore the ctrl-alt-del.
71715d94b82SRobin Holt  */
71815d94b82SRobin Holt void ctrl_alt_del(void)
71915d94b82SRobin Holt {
72015d94b82SRobin Holt 	static DECLARE_WORK(cad_work, deferred_cad);
72115d94b82SRobin Holt 
72215d94b82SRobin Holt 	if (C_A_D)
72315d94b82SRobin Holt 		schedule_work(&cad_work);
72415d94b82SRobin Holt 	else
72515d94b82SRobin Holt 		kill_cad_pid(SIGINT, 1);
72615d94b82SRobin Holt }
72715d94b82SRobin Holt 
72815d94b82SRobin Holt char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
7297a54f46bSJoel Stanley static const char reboot_cmd[] = "/sbin/reboot";
73015d94b82SRobin Holt 
7317a54f46bSJoel Stanley static int run_cmd(const char *cmd)
73215d94b82SRobin Holt {
73315d94b82SRobin Holt 	char **argv;
73415d94b82SRobin Holt 	static char *envp[] = {
73515d94b82SRobin Holt 		"HOME=/",
73615d94b82SRobin Holt 		"PATH=/sbin:/bin:/usr/sbin:/usr/bin",
73715d94b82SRobin Holt 		NULL
73815d94b82SRobin Holt 	};
73915d94b82SRobin Holt 	int ret;
7407a54f46bSJoel Stanley 	argv = argv_split(GFP_KERNEL, cmd, NULL);
74115d94b82SRobin Holt 	if (argv) {
74215d94b82SRobin Holt 		ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
74315d94b82SRobin Holt 		argv_free(argv);
74415d94b82SRobin Holt 	} else {
74515d94b82SRobin Holt 		ret = -ENOMEM;
74615d94b82SRobin Holt 	}
74715d94b82SRobin Holt 
7487a54f46bSJoel Stanley 	return ret;
7497a54f46bSJoel Stanley }
7507a54f46bSJoel Stanley 
7517a54f46bSJoel Stanley static int __orderly_reboot(void)
7527a54f46bSJoel Stanley {
7537a54f46bSJoel Stanley 	int ret;
7547a54f46bSJoel Stanley 
7557a54f46bSJoel Stanley 	ret = run_cmd(reboot_cmd);
7567a54f46bSJoel Stanley 
7577a54f46bSJoel Stanley 	if (ret) {
7587a54f46bSJoel Stanley 		pr_warn("Failed to start orderly reboot: forcing the issue\n");
7597a54f46bSJoel Stanley 		emergency_sync();
7607a54f46bSJoel Stanley 		kernel_restart(NULL);
7617a54f46bSJoel Stanley 	}
7627a54f46bSJoel Stanley 
7637a54f46bSJoel Stanley 	return ret;
7647a54f46bSJoel Stanley }
7657a54f46bSJoel Stanley 
7667a54f46bSJoel Stanley static int __orderly_poweroff(bool force)
7677a54f46bSJoel Stanley {
7687a54f46bSJoel Stanley 	int ret;
7697a54f46bSJoel Stanley 
7707a54f46bSJoel Stanley 	ret = run_cmd(poweroff_cmd);
7717a54f46bSJoel Stanley 
77215d94b82SRobin Holt 	if (ret && force) {
773972ee83dSRobin Holt 		pr_warn("Failed to start orderly shutdown: forcing the issue\n");
7747a54f46bSJoel Stanley 
77515d94b82SRobin Holt 		/*
77615d94b82SRobin Holt 		 * I guess this should try to kick off some daemon to sync and
77715d94b82SRobin Holt 		 * poweroff asap.  Or not even bother syncing if we're doing an
77815d94b82SRobin Holt 		 * emergency shutdown?
77915d94b82SRobin Holt 		 */
78015d94b82SRobin Holt 		emergency_sync();
78115d94b82SRobin Holt 		kernel_power_off();
78215d94b82SRobin Holt 	}
78315d94b82SRobin Holt 
78415d94b82SRobin Holt 	return ret;
78515d94b82SRobin Holt }
78615d94b82SRobin Holt 
78715d94b82SRobin Holt static bool poweroff_force;
78815d94b82SRobin Holt 
78915d94b82SRobin Holt static void poweroff_work_func(struct work_struct *work)
79015d94b82SRobin Holt {
79115d94b82SRobin Holt 	__orderly_poweroff(poweroff_force);
79215d94b82SRobin Holt }
79315d94b82SRobin Holt 
79415d94b82SRobin Holt static DECLARE_WORK(poweroff_work, poweroff_work_func);
79515d94b82SRobin Holt 
79615d94b82SRobin Holt /**
79715d94b82SRobin Holt  * orderly_poweroff - Trigger an orderly system poweroff
79815d94b82SRobin Holt  * @force: force poweroff if command execution fails
79915d94b82SRobin Holt  *
80015d94b82SRobin Holt  * This may be called from any context to trigger a system shutdown.
80115d94b82SRobin Holt  * If the orderly shutdown fails, it will force an immediate shutdown.
80215d94b82SRobin Holt  */
8037a54f46bSJoel Stanley void orderly_poweroff(bool force)
80415d94b82SRobin Holt {
80515d94b82SRobin Holt 	if (force) /* do not override the pending "true" */
80615d94b82SRobin Holt 		poweroff_force = true;
80715d94b82SRobin Holt 	schedule_work(&poweroff_work);
80815d94b82SRobin Holt }
80915d94b82SRobin Holt EXPORT_SYMBOL_GPL(orderly_poweroff);
8101b3a5d02SRobin Holt 
8117a54f46bSJoel Stanley static void reboot_work_func(struct work_struct *work)
8127a54f46bSJoel Stanley {
8137a54f46bSJoel Stanley 	__orderly_reboot();
8147a54f46bSJoel Stanley }
8157a54f46bSJoel Stanley 
8167a54f46bSJoel Stanley static DECLARE_WORK(reboot_work, reboot_work_func);
8177a54f46bSJoel Stanley 
8187a54f46bSJoel Stanley /**
8197a54f46bSJoel Stanley  * orderly_reboot - Trigger an orderly system reboot
8207a54f46bSJoel Stanley  *
8217a54f46bSJoel Stanley  * This may be called from any context to trigger a system reboot.
8227a54f46bSJoel Stanley  * If the orderly reboot fails, it will force an immediate reboot.
8237a54f46bSJoel Stanley  */
8247a54f46bSJoel Stanley void orderly_reboot(void)
8257a54f46bSJoel Stanley {
8267a54f46bSJoel Stanley 	schedule_work(&reboot_work);
8277a54f46bSJoel Stanley }
8287a54f46bSJoel Stanley EXPORT_SYMBOL_GPL(orderly_reboot);
8297a54f46bSJoel Stanley 
830dfa19b11SMatti Vaittinen /**
831dfa19b11SMatti Vaittinen  * hw_failure_emergency_poweroff_func - emergency poweroff work after a known delay
832dfa19b11SMatti Vaittinen  * @work: work_struct associated with the emergency poweroff function
833dfa19b11SMatti Vaittinen  *
834dfa19b11SMatti Vaittinen  * This function is called in very critical situations to force
835dfa19b11SMatti Vaittinen  * a kernel poweroff after a configurable timeout value.
836dfa19b11SMatti Vaittinen  */
837dfa19b11SMatti Vaittinen static void hw_failure_emergency_poweroff_func(struct work_struct *work)
838dfa19b11SMatti Vaittinen {
839dfa19b11SMatti Vaittinen 	/*
840dfa19b11SMatti Vaittinen 	 * We have reached here after the emergency shutdown waiting period has
841dfa19b11SMatti Vaittinen 	 * expired. This means orderly_poweroff has not been able to shut off
842dfa19b11SMatti Vaittinen 	 * the system for some reason.
843dfa19b11SMatti Vaittinen 	 *
844dfa19b11SMatti Vaittinen 	 * Try to shut down the system immediately using kernel_power_off
845dfa19b11SMatti Vaittinen 	 * if populated
846dfa19b11SMatti Vaittinen 	 */
847dfa19b11SMatti Vaittinen 	pr_emerg("Hardware protection timed-out. Trying forced poweroff\n");
848dfa19b11SMatti Vaittinen 	kernel_power_off();
849dfa19b11SMatti Vaittinen 
850dfa19b11SMatti Vaittinen 	/*
851dfa19b11SMatti Vaittinen 	 * Worst of the worst case trigger emergency restart
852dfa19b11SMatti Vaittinen 	 */
853dfa19b11SMatti Vaittinen 	pr_emerg("Hardware protection shutdown failed. Trying emergency restart\n");
854dfa19b11SMatti Vaittinen 	emergency_restart();
855dfa19b11SMatti Vaittinen }
856dfa19b11SMatti Vaittinen 
857dfa19b11SMatti Vaittinen static DECLARE_DELAYED_WORK(hw_failure_emergency_poweroff_work,
858dfa19b11SMatti Vaittinen 			    hw_failure_emergency_poweroff_func);
859dfa19b11SMatti Vaittinen 
860dfa19b11SMatti Vaittinen /**
861dfa19b11SMatti Vaittinen  * hw_failure_emergency_poweroff - Trigger an emergency system poweroff
862dfa19b11SMatti Vaittinen  *
863dfa19b11SMatti Vaittinen  * This may be called from any critical situation to trigger a system shutdown
864dfa19b11SMatti Vaittinen  * after a given period of time. If time is negative this is not scheduled.
865dfa19b11SMatti Vaittinen  */
866dfa19b11SMatti Vaittinen static void hw_failure_emergency_poweroff(int poweroff_delay_ms)
867dfa19b11SMatti Vaittinen {
868dfa19b11SMatti Vaittinen 	if (poweroff_delay_ms <= 0)
869dfa19b11SMatti Vaittinen 		return;
870dfa19b11SMatti Vaittinen 	schedule_delayed_work(&hw_failure_emergency_poweroff_work,
871dfa19b11SMatti Vaittinen 			      msecs_to_jiffies(poweroff_delay_ms));
872dfa19b11SMatti Vaittinen }
873dfa19b11SMatti Vaittinen 
874dfa19b11SMatti Vaittinen /**
875dfa19b11SMatti Vaittinen  * hw_protection_shutdown - Trigger an emergency system poweroff
876dfa19b11SMatti Vaittinen  *
877dfa19b11SMatti Vaittinen  * @reason:		Reason of emergency shutdown to be printed.
878dfa19b11SMatti Vaittinen  * @ms_until_forced:	Time to wait for orderly shutdown before tiggering a
879dfa19b11SMatti Vaittinen  *			forced shudown. Negative value disables the forced
880dfa19b11SMatti Vaittinen  *			shutdown.
881dfa19b11SMatti Vaittinen  *
882dfa19b11SMatti Vaittinen  * Initiate an emergency system shutdown in order to protect hardware from
883dfa19b11SMatti Vaittinen  * further damage. Usage examples include a thermal protection or a voltage or
884dfa19b11SMatti Vaittinen  * current regulator failures.
885dfa19b11SMatti Vaittinen  * NOTE: The request is ignored if protection shutdown is already pending even
886dfa19b11SMatti Vaittinen  * if the previous request has given a large timeout for forced shutdown.
887dfa19b11SMatti Vaittinen  * Can be called from any context.
888dfa19b11SMatti Vaittinen  */
889dfa19b11SMatti Vaittinen void hw_protection_shutdown(const char *reason, int ms_until_forced)
890dfa19b11SMatti Vaittinen {
891dfa19b11SMatti Vaittinen 	static atomic_t allow_proceed = ATOMIC_INIT(1);
892dfa19b11SMatti Vaittinen 
893dfa19b11SMatti Vaittinen 	pr_emerg("HARDWARE PROTECTION shutdown (%s)\n", reason);
894dfa19b11SMatti Vaittinen 
895dfa19b11SMatti Vaittinen 	/* Shutdown should be initiated only once. */
896dfa19b11SMatti Vaittinen 	if (!atomic_dec_and_test(&allow_proceed))
897dfa19b11SMatti Vaittinen 		return;
898dfa19b11SMatti Vaittinen 
899dfa19b11SMatti Vaittinen 	/*
900dfa19b11SMatti Vaittinen 	 * Queue a backup emergency shutdown in the event of
901dfa19b11SMatti Vaittinen 	 * orderly_poweroff failure
902dfa19b11SMatti Vaittinen 	 */
903dfa19b11SMatti Vaittinen 	hw_failure_emergency_poweroff(ms_until_forced);
904dfa19b11SMatti Vaittinen 	orderly_poweroff(true);
905dfa19b11SMatti Vaittinen }
906dfa19b11SMatti Vaittinen EXPORT_SYMBOL_GPL(hw_protection_shutdown);
907dfa19b11SMatti Vaittinen 
9081b3a5d02SRobin Holt static int __init reboot_setup(char *str)
9091b3a5d02SRobin Holt {
9101b3a5d02SRobin Holt 	for (;;) {
911b287a25aSAaro Koskinen 		enum reboot_mode *mode;
912b287a25aSAaro Koskinen 
9131b3a5d02SRobin Holt 		/*
9141b3a5d02SRobin Holt 		 * Having anything passed on the command line via
9151b3a5d02SRobin Holt 		 * reboot= will cause us to disable DMI checking
9161b3a5d02SRobin Holt 		 * below.
9171b3a5d02SRobin Holt 		 */
9181b3a5d02SRobin Holt 		reboot_default = 0;
9191b3a5d02SRobin Holt 
920b287a25aSAaro Koskinen 		if (!strncmp(str, "panic_", 6)) {
921b287a25aSAaro Koskinen 			mode = &panic_reboot_mode;
922b287a25aSAaro Koskinen 			str += 6;
923b287a25aSAaro Koskinen 		} else {
924b287a25aSAaro Koskinen 			mode = &reboot_mode;
925b287a25aSAaro Koskinen 		}
926b287a25aSAaro Koskinen 
9271b3a5d02SRobin Holt 		switch (*str) {
9281b3a5d02SRobin Holt 		case 'w':
929b287a25aSAaro Koskinen 			*mode = REBOOT_WARM;
9301b3a5d02SRobin Holt 			break;
9311b3a5d02SRobin Holt 
9321b3a5d02SRobin Holt 		case 'c':
933b287a25aSAaro Koskinen 			*mode = REBOOT_COLD;
9341b3a5d02SRobin Holt 			break;
9351b3a5d02SRobin Holt 
9361b3a5d02SRobin Holt 		case 'h':
937b287a25aSAaro Koskinen 			*mode = REBOOT_HARD;
9381b3a5d02SRobin Holt 			break;
9391b3a5d02SRobin Holt 
9401b3a5d02SRobin Holt 		case 's':
941f9a90501SMatteo Croce 			/*
942f9a90501SMatteo Croce 			 * reboot_cpu is s[mp]#### with #### being the processor
943f9a90501SMatteo Croce 			 * to be used for rebooting. Skip 's' or 'smp' prefix.
944f9a90501SMatteo Croce 			 */
945f9a90501SMatteo Croce 			str += str[1] == 'm' && str[2] == 'p' ? 3 : 1;
946f9a90501SMatteo Croce 
947f9a90501SMatteo Croce 			if (isdigit(str[0])) {
948f9a90501SMatteo Croce 				int cpu = simple_strtoul(str, NULL, 0);
949f9a90501SMatteo Croce 
950f9a90501SMatteo Croce 				if (cpu >= num_possible_cpus()) {
951df5b0ab3SMatteo Croce 					pr_err("Ignoring the CPU number in reboot= option. "
952df5b0ab3SMatteo Croce 					"CPU %d exceeds possible cpu number %d\n",
953f9a90501SMatteo Croce 					cpu, num_possible_cpus());
954df5b0ab3SMatteo Croce 					break;
955df5b0ab3SMatteo Croce 				}
956f9a90501SMatteo Croce 				reboot_cpu = cpu;
957f9a90501SMatteo Croce 			} else
958f9a90501SMatteo Croce 				*mode = REBOOT_SOFT;
9591b3a5d02SRobin Holt 			break;
9608b92c4ffSMatteo Croce 
9611b3a5d02SRobin Holt 		case 'g':
962b287a25aSAaro Koskinen 			*mode = REBOOT_GPIO;
9631b3a5d02SRobin Holt 			break;
9641b3a5d02SRobin Holt 
9651b3a5d02SRobin Holt 		case 'b':
9661b3a5d02SRobin Holt 		case 'a':
9671b3a5d02SRobin Holt 		case 'k':
9681b3a5d02SRobin Holt 		case 't':
9691b3a5d02SRobin Holt 		case 'e':
9701b3a5d02SRobin Holt 		case 'p':
9711b3a5d02SRobin Holt 			reboot_type = *str;
9721b3a5d02SRobin Holt 			break;
9731b3a5d02SRobin Holt 
9741b3a5d02SRobin Holt 		case 'f':
9751b3a5d02SRobin Holt 			reboot_force = 1;
9761b3a5d02SRobin Holt 			break;
9771b3a5d02SRobin Holt 		}
9781b3a5d02SRobin Holt 
9791b3a5d02SRobin Holt 		str = strchr(str, ',');
9801b3a5d02SRobin Holt 		if (str)
9811b3a5d02SRobin Holt 			str++;
9821b3a5d02SRobin Holt 		else
9831b3a5d02SRobin Holt 			break;
9841b3a5d02SRobin Holt 	}
9851b3a5d02SRobin Holt 	return 1;
9861b3a5d02SRobin Holt }
9871b3a5d02SRobin Holt __setup("reboot=", reboot_setup);
9882c622ed0SMatteo Croce 
9892c622ed0SMatteo Croce #ifdef CONFIG_SYSFS
9902c622ed0SMatteo Croce 
9912c622ed0SMatteo Croce #define REBOOT_COLD_STR		"cold"
9922c622ed0SMatteo Croce #define REBOOT_WARM_STR		"warm"
9932c622ed0SMatteo Croce #define REBOOT_HARD_STR		"hard"
9942c622ed0SMatteo Croce #define REBOOT_SOFT_STR		"soft"
9952c622ed0SMatteo Croce #define REBOOT_GPIO_STR		"gpio"
9962c622ed0SMatteo Croce #define REBOOT_UNDEFINED_STR	"undefined"
9972c622ed0SMatteo Croce 
9982c622ed0SMatteo Croce #define BOOT_TRIPLE_STR		"triple"
9992c622ed0SMatteo Croce #define BOOT_KBD_STR		"kbd"
10002c622ed0SMatteo Croce #define BOOT_BIOS_STR		"bios"
10012c622ed0SMatteo Croce #define BOOT_ACPI_STR		"acpi"
10022c622ed0SMatteo Croce #define BOOT_EFI_STR		"efi"
10030c5c0179SMatteo Croce #define BOOT_PCI_STR		"pci"
10042c622ed0SMatteo Croce 
10052c622ed0SMatteo Croce static ssize_t mode_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
10062c622ed0SMatteo Croce {
10072c622ed0SMatteo Croce 	const char *val;
10082c622ed0SMatteo Croce 
10092c622ed0SMatteo Croce 	switch (reboot_mode) {
10102c622ed0SMatteo Croce 	case REBOOT_COLD:
10112c622ed0SMatteo Croce 		val = REBOOT_COLD_STR;
10122c622ed0SMatteo Croce 		break;
10132c622ed0SMatteo Croce 	case REBOOT_WARM:
10142c622ed0SMatteo Croce 		val = REBOOT_WARM_STR;
10152c622ed0SMatteo Croce 		break;
10162c622ed0SMatteo Croce 	case REBOOT_HARD:
10172c622ed0SMatteo Croce 		val = REBOOT_HARD_STR;
10182c622ed0SMatteo Croce 		break;
10192c622ed0SMatteo Croce 	case REBOOT_SOFT:
10202c622ed0SMatteo Croce 		val = REBOOT_SOFT_STR;
10212c622ed0SMatteo Croce 		break;
10222c622ed0SMatteo Croce 	case REBOOT_GPIO:
10232c622ed0SMatteo Croce 		val = REBOOT_GPIO_STR;
10242c622ed0SMatteo Croce 		break;
10252c622ed0SMatteo Croce 	default:
10262c622ed0SMatteo Croce 		val = REBOOT_UNDEFINED_STR;
10272c622ed0SMatteo Croce 	}
10282c622ed0SMatteo Croce 
10292c622ed0SMatteo Croce 	return sprintf(buf, "%s\n", val);
10302c622ed0SMatteo Croce }
10312c622ed0SMatteo Croce static ssize_t mode_store(struct kobject *kobj, struct kobj_attribute *attr,
10322c622ed0SMatteo Croce 			  const char *buf, size_t count)
10332c622ed0SMatteo Croce {
10342c622ed0SMatteo Croce 	if (!capable(CAP_SYS_BOOT))
10352c622ed0SMatteo Croce 		return -EPERM;
10362c622ed0SMatteo Croce 
10372c622ed0SMatteo Croce 	if (!strncmp(buf, REBOOT_COLD_STR, strlen(REBOOT_COLD_STR)))
10382c622ed0SMatteo Croce 		reboot_mode = REBOOT_COLD;
10392c622ed0SMatteo Croce 	else if (!strncmp(buf, REBOOT_WARM_STR, strlen(REBOOT_WARM_STR)))
10402c622ed0SMatteo Croce 		reboot_mode = REBOOT_WARM;
10412c622ed0SMatteo Croce 	else if (!strncmp(buf, REBOOT_HARD_STR, strlen(REBOOT_HARD_STR)))
10422c622ed0SMatteo Croce 		reboot_mode = REBOOT_HARD;
10432c622ed0SMatteo Croce 	else if (!strncmp(buf, REBOOT_SOFT_STR, strlen(REBOOT_SOFT_STR)))
10442c622ed0SMatteo Croce 		reboot_mode = REBOOT_SOFT;
10452c622ed0SMatteo Croce 	else if (!strncmp(buf, REBOOT_GPIO_STR, strlen(REBOOT_GPIO_STR)))
10462c622ed0SMatteo Croce 		reboot_mode = REBOOT_GPIO;
10472c622ed0SMatteo Croce 	else
10482c622ed0SMatteo Croce 		return -EINVAL;
10492c622ed0SMatteo Croce 
10501a9d079fSMatteo Croce 	reboot_default = 0;
10511a9d079fSMatteo Croce 
10522c622ed0SMatteo Croce 	return count;
10532c622ed0SMatteo Croce }
10542c622ed0SMatteo Croce static struct kobj_attribute reboot_mode_attr = __ATTR_RW(mode);
10552c622ed0SMatteo Croce 
105640247e55SMatteo Croce #ifdef CONFIG_X86
105740247e55SMatteo Croce static ssize_t force_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
105840247e55SMatteo Croce {
105940247e55SMatteo Croce 	return sprintf(buf, "%d\n", reboot_force);
106040247e55SMatteo Croce }
106140247e55SMatteo Croce static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr,
106240247e55SMatteo Croce 			  const char *buf, size_t count)
106340247e55SMatteo Croce {
106440247e55SMatteo Croce 	bool res;
106540247e55SMatteo Croce 
106640247e55SMatteo Croce 	if (!capable(CAP_SYS_BOOT))
106740247e55SMatteo Croce 		return -EPERM;
106840247e55SMatteo Croce 
106940247e55SMatteo Croce 	if (kstrtobool(buf, &res))
107040247e55SMatteo Croce 		return -EINVAL;
107140247e55SMatteo Croce 
107240247e55SMatteo Croce 	reboot_default = 0;
107340247e55SMatteo Croce 	reboot_force = res;
107440247e55SMatteo Croce 
107540247e55SMatteo Croce 	return count;
107640247e55SMatteo Croce }
107740247e55SMatteo Croce static struct kobj_attribute reboot_force_attr = __ATTR_RW(force);
107840247e55SMatteo Croce 
10792c622ed0SMatteo Croce static ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
10802c622ed0SMatteo Croce {
10812c622ed0SMatteo Croce 	const char *val;
10822c622ed0SMatteo Croce 
10832c622ed0SMatteo Croce 	switch (reboot_type) {
10842c622ed0SMatteo Croce 	case BOOT_TRIPLE:
10852c622ed0SMatteo Croce 		val = BOOT_TRIPLE_STR;
10862c622ed0SMatteo Croce 		break;
10872c622ed0SMatteo Croce 	case BOOT_KBD:
10882c622ed0SMatteo Croce 		val = BOOT_KBD_STR;
10892c622ed0SMatteo Croce 		break;
10902c622ed0SMatteo Croce 	case BOOT_BIOS:
10912c622ed0SMatteo Croce 		val = BOOT_BIOS_STR;
10922c622ed0SMatteo Croce 		break;
10932c622ed0SMatteo Croce 	case BOOT_ACPI:
10942c622ed0SMatteo Croce 		val = BOOT_ACPI_STR;
10952c622ed0SMatteo Croce 		break;
10962c622ed0SMatteo Croce 	case BOOT_EFI:
10972c622ed0SMatteo Croce 		val = BOOT_EFI_STR;
10982c622ed0SMatteo Croce 		break;
10992c622ed0SMatteo Croce 	case BOOT_CF9_FORCE:
11000c5c0179SMatteo Croce 		val = BOOT_PCI_STR;
11012c622ed0SMatteo Croce 		break;
11022c622ed0SMatteo Croce 	default:
11032c622ed0SMatteo Croce 		val = REBOOT_UNDEFINED_STR;
11042c622ed0SMatteo Croce 	}
11052c622ed0SMatteo Croce 
11062c622ed0SMatteo Croce 	return sprintf(buf, "%s\n", val);
11072c622ed0SMatteo Croce }
11082c622ed0SMatteo Croce static ssize_t type_store(struct kobject *kobj, struct kobj_attribute *attr,
11092c622ed0SMatteo Croce 			  const char *buf, size_t count)
11102c622ed0SMatteo Croce {
11112c622ed0SMatteo Croce 	if (!capable(CAP_SYS_BOOT))
11122c622ed0SMatteo Croce 		return -EPERM;
11132c622ed0SMatteo Croce 
11142c622ed0SMatteo Croce 	if (!strncmp(buf, BOOT_TRIPLE_STR, strlen(BOOT_TRIPLE_STR)))
11152c622ed0SMatteo Croce 		reboot_type = BOOT_TRIPLE;
11162c622ed0SMatteo Croce 	else if (!strncmp(buf, BOOT_KBD_STR, strlen(BOOT_KBD_STR)))
11172c622ed0SMatteo Croce 		reboot_type = BOOT_KBD;
11182c622ed0SMatteo Croce 	else if (!strncmp(buf, BOOT_BIOS_STR, strlen(BOOT_BIOS_STR)))
11192c622ed0SMatteo Croce 		reboot_type = BOOT_BIOS;
11202c622ed0SMatteo Croce 	else if (!strncmp(buf, BOOT_ACPI_STR, strlen(BOOT_ACPI_STR)))
11212c622ed0SMatteo Croce 		reboot_type = BOOT_ACPI;
11222c622ed0SMatteo Croce 	else if (!strncmp(buf, BOOT_EFI_STR, strlen(BOOT_EFI_STR)))
11232c622ed0SMatteo Croce 		reboot_type = BOOT_EFI;
11240c5c0179SMatteo Croce 	else if (!strncmp(buf, BOOT_PCI_STR, strlen(BOOT_PCI_STR)))
11252c622ed0SMatteo Croce 		reboot_type = BOOT_CF9_FORCE;
11262c622ed0SMatteo Croce 	else
11272c622ed0SMatteo Croce 		return -EINVAL;
11282c622ed0SMatteo Croce 
11291a9d079fSMatteo Croce 	reboot_default = 0;
11301a9d079fSMatteo Croce 
11312c622ed0SMatteo Croce 	return count;
11322c622ed0SMatteo Croce }
11332c622ed0SMatteo Croce static struct kobj_attribute reboot_type_attr = __ATTR_RW(type);
113440247e55SMatteo Croce #endif
11352c622ed0SMatteo Croce 
113640247e55SMatteo Croce #ifdef CONFIG_SMP
11372c622ed0SMatteo Croce static ssize_t cpu_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
11382c622ed0SMatteo Croce {
11392c622ed0SMatteo Croce 	return sprintf(buf, "%d\n", reboot_cpu);
11402c622ed0SMatteo Croce }
11412c622ed0SMatteo Croce static ssize_t cpu_store(struct kobject *kobj, struct kobj_attribute *attr,
11422c622ed0SMatteo Croce 			  const char *buf, size_t count)
11432c622ed0SMatteo Croce {
11442c622ed0SMatteo Croce 	unsigned int cpunum;
11452c622ed0SMatteo Croce 	int rc;
11462c622ed0SMatteo Croce 
11472c622ed0SMatteo Croce 	if (!capable(CAP_SYS_BOOT))
11482c622ed0SMatteo Croce 		return -EPERM;
11492c622ed0SMatteo Croce 
11502c622ed0SMatteo Croce 	rc = kstrtouint(buf, 0, &cpunum);
11512c622ed0SMatteo Croce 
11522c622ed0SMatteo Croce 	if (rc)
11532c622ed0SMatteo Croce 		return rc;
11542c622ed0SMatteo Croce 
11552c622ed0SMatteo Croce 	if (cpunum >= num_possible_cpus())
11562c622ed0SMatteo Croce 		return -ERANGE;
11572c622ed0SMatteo Croce 
11581a9d079fSMatteo Croce 	reboot_default = 0;
11592c622ed0SMatteo Croce 	reboot_cpu = cpunum;
11602c622ed0SMatteo Croce 
11612c622ed0SMatteo Croce 	return count;
11622c622ed0SMatteo Croce }
11632c622ed0SMatteo Croce static struct kobj_attribute reboot_cpu_attr = __ATTR_RW(cpu);
116440247e55SMatteo Croce #endif
11652c622ed0SMatteo Croce 
11662c622ed0SMatteo Croce static struct attribute *reboot_attrs[] = {
11672c622ed0SMatteo Croce 	&reboot_mode_attr.attr,
116840247e55SMatteo Croce #ifdef CONFIG_X86
11692c622ed0SMatteo Croce 	&reboot_force_attr.attr,
117040247e55SMatteo Croce 	&reboot_type_attr.attr,
117140247e55SMatteo Croce #endif
117240247e55SMatteo Croce #ifdef CONFIG_SMP
117340247e55SMatteo Croce 	&reboot_cpu_attr.attr,
117440247e55SMatteo Croce #endif
11752c622ed0SMatteo Croce 	NULL,
11762c622ed0SMatteo Croce };
11772c622ed0SMatteo Croce 
11782c622ed0SMatteo Croce static const struct attribute_group reboot_attr_group = {
11792c622ed0SMatteo Croce 	.attrs = reboot_attrs,
11802c622ed0SMatteo Croce };
11812c622ed0SMatteo Croce 
11822c622ed0SMatteo Croce static int __init reboot_ksysfs_init(void)
11832c622ed0SMatteo Croce {
11842c622ed0SMatteo Croce 	struct kobject *reboot_kobj;
11852c622ed0SMatteo Croce 	int ret;
11862c622ed0SMatteo Croce 
11872c622ed0SMatteo Croce 	reboot_kobj = kobject_create_and_add("reboot", kernel_kobj);
11882c622ed0SMatteo Croce 	if (!reboot_kobj)
11892c622ed0SMatteo Croce 		return -ENOMEM;
11902c622ed0SMatteo Croce 
11912c622ed0SMatteo Croce 	ret = sysfs_create_group(reboot_kobj, &reboot_attr_group);
11922c622ed0SMatteo Croce 	if (ret) {
11932c622ed0SMatteo Croce 		kobject_put(reboot_kobj);
11942c622ed0SMatteo Croce 		return ret;
11952c622ed0SMatteo Croce 	}
11962c622ed0SMatteo Croce 
11972c622ed0SMatteo Croce 	return 0;
11982c622ed0SMatteo Croce }
11992c622ed0SMatteo Croce late_initcall(reboot_ksysfs_init);
12002c622ed0SMatteo Croce 
12012c622ed0SMatteo Croce #endif
1202