xref: /openbmc/linux/kernel/reboot.c (revision 2b8c612c6102f751e6e3e1bd425f64e9d3d3f638)
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 
2606d17766Stangmeng static 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 };
5915d94b82SRobin Holt 
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);
6515d94b82SRobin Holt 
6615d94b82SRobin Holt /**
6715d94b82SRobin Holt  *	emergency_restart - reboot the system
6815d94b82SRobin Holt  *
6915d94b82SRobin Holt  *	Without shutting down any hardware or taking any locks
7015d94b82SRobin Holt  *	reboot the system.  This is called when we know we are in
7115d94b82SRobin Holt  *	trouble so this is our best effort to reboot.  This is
7215d94b82SRobin Holt  *	safe to call in interrupt context.
7315d94b82SRobin Holt  */
7415d94b82SRobin Holt void emergency_restart(void)
7515d94b82SRobin Holt {
7615d94b82SRobin Holt 	kmsg_dump(KMSG_DUMP_EMERG);
7715d94b82SRobin Holt 	machine_emergency_restart();
7815d94b82SRobin Holt }
7915d94b82SRobin Holt EXPORT_SYMBOL_GPL(emergency_restart);
8015d94b82SRobin Holt 
8115d94b82SRobin Holt void kernel_restart_prepare(char *cmd)
8215d94b82SRobin Holt {
8315d94b82SRobin Holt 	blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
8415d94b82SRobin Holt 	system_state = SYSTEM_RESTART;
8515d94b82SRobin Holt 	usermodehelper_disable();
8615d94b82SRobin Holt 	device_shutdown();
8715d94b82SRobin Holt }
8815d94b82SRobin Holt 
8915d94b82SRobin Holt /**
9015d94b82SRobin Holt  *	register_reboot_notifier - Register function to be called at reboot time
9115d94b82SRobin Holt  *	@nb: Info about notifier function to be called
9215d94b82SRobin Holt  *
9315d94b82SRobin Holt  *	Registers a function with the list of functions
9415d94b82SRobin Holt  *	to be called at reboot time.
9515d94b82SRobin Holt  *
9615d94b82SRobin Holt  *	Currently always returns zero, as blocking_notifier_chain_register()
9715d94b82SRobin Holt  *	always returns zero.
9815d94b82SRobin Holt  */
9915d94b82SRobin Holt int register_reboot_notifier(struct notifier_block *nb)
10015d94b82SRobin Holt {
10115d94b82SRobin Holt 	return blocking_notifier_chain_register(&reboot_notifier_list, nb);
10215d94b82SRobin Holt }
10315d94b82SRobin Holt EXPORT_SYMBOL(register_reboot_notifier);
10415d94b82SRobin Holt 
10515d94b82SRobin Holt /**
10615d94b82SRobin Holt  *	unregister_reboot_notifier - Unregister previously registered reboot notifier
10715d94b82SRobin Holt  *	@nb: Hook to be unregistered
10815d94b82SRobin Holt  *
10915d94b82SRobin Holt  *	Unregisters a previously registered reboot
11015d94b82SRobin Holt  *	notifier function.
11115d94b82SRobin Holt  *
11215d94b82SRobin Holt  *	Returns zero on success, or %-ENOENT on failure.
11315d94b82SRobin Holt  */
11415d94b82SRobin Holt int unregister_reboot_notifier(struct notifier_block *nb)
11515d94b82SRobin Holt {
11615d94b82SRobin Holt 	return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
11715d94b82SRobin Holt }
11815d94b82SRobin Holt EXPORT_SYMBOL(unregister_reboot_notifier);
11915d94b82SRobin Holt 
1202d8364baSAndrey Smirnov static void devm_unregister_reboot_notifier(struct device *dev, void *res)
1212d8364baSAndrey Smirnov {
1222d8364baSAndrey Smirnov 	WARN_ON(unregister_reboot_notifier(*(struct notifier_block **)res));
1232d8364baSAndrey Smirnov }
1242d8364baSAndrey Smirnov 
1252d8364baSAndrey Smirnov int devm_register_reboot_notifier(struct device *dev, struct notifier_block *nb)
1262d8364baSAndrey Smirnov {
1272d8364baSAndrey Smirnov 	struct notifier_block **rcnb;
1282d8364baSAndrey Smirnov 	int ret;
1292d8364baSAndrey Smirnov 
1302d8364baSAndrey Smirnov 	rcnb = devres_alloc(devm_unregister_reboot_notifier,
1312d8364baSAndrey Smirnov 			    sizeof(*rcnb), GFP_KERNEL);
1322d8364baSAndrey Smirnov 	if (!rcnb)
1332d8364baSAndrey Smirnov 		return -ENOMEM;
1342d8364baSAndrey Smirnov 
1352d8364baSAndrey Smirnov 	ret = register_reboot_notifier(nb);
1362d8364baSAndrey Smirnov 	if (!ret) {
1372d8364baSAndrey Smirnov 		*rcnb = nb;
1382d8364baSAndrey Smirnov 		devres_add(dev, rcnb);
1392d8364baSAndrey Smirnov 	} else {
1402d8364baSAndrey Smirnov 		devres_free(rcnb);
1412d8364baSAndrey Smirnov 	}
1422d8364baSAndrey Smirnov 
1432d8364baSAndrey Smirnov 	return ret;
1442d8364baSAndrey Smirnov }
1452d8364baSAndrey Smirnov EXPORT_SYMBOL(devm_register_reboot_notifier);
1462d8364baSAndrey Smirnov 
147b63adb97SGuenter Roeck /*
148b63adb97SGuenter Roeck  *	Notifier list for kernel code which wants to be called
149b63adb97SGuenter Roeck  *	to restart the system.
150b63adb97SGuenter Roeck  */
151b63adb97SGuenter Roeck static ATOMIC_NOTIFIER_HEAD(restart_handler_list);
152b63adb97SGuenter Roeck 
153b63adb97SGuenter Roeck /**
154b63adb97SGuenter Roeck  *	register_restart_handler - Register function to be called to reset
155b63adb97SGuenter Roeck  *				   the system
156b63adb97SGuenter Roeck  *	@nb: Info about handler function to be called
157b63adb97SGuenter Roeck  *	@nb->priority:	Handler priority. Handlers should follow the
158b63adb97SGuenter Roeck  *			following guidelines for setting priorities.
159b63adb97SGuenter Roeck  *			0:	Restart handler of last resort,
160b63adb97SGuenter Roeck  *				with limited restart capabilities
161b63adb97SGuenter Roeck  *			128:	Default restart handler; use if no other
162b63adb97SGuenter Roeck  *				restart handler is expected to be available,
163b63adb97SGuenter Roeck  *				and/or if restart functionality is
164b63adb97SGuenter Roeck  *				sufficient to restart the entire system
165b63adb97SGuenter Roeck  *			255:	Highest priority restart handler, will
166b63adb97SGuenter Roeck  *				preempt all other restart handlers
167b63adb97SGuenter Roeck  *
168b63adb97SGuenter Roeck  *	Registers a function with code to be called to restart the
169b63adb97SGuenter Roeck  *	system.
170b63adb97SGuenter Roeck  *
171b63adb97SGuenter Roeck  *	Registered functions will be called from machine_restart as last
172b63adb97SGuenter Roeck  *	step of the restart sequence (if the architecture specific
173b63adb97SGuenter Roeck  *	machine_restart function calls do_kernel_restart - see below
174b63adb97SGuenter Roeck  *	for details).
175b63adb97SGuenter Roeck  *	Registered functions are expected to restart the system immediately.
176b63adb97SGuenter Roeck  *	If more than one function is registered, the restart handler priority
177b63adb97SGuenter Roeck  *	selects which function will be called first.
178b63adb97SGuenter Roeck  *
179b63adb97SGuenter Roeck  *	Restart handlers are expected to be registered from non-architecture
180b63adb97SGuenter Roeck  *	code, typically from drivers. A typical use case would be a system
181b63adb97SGuenter Roeck  *	where restart functionality is provided through a watchdog. Multiple
182b63adb97SGuenter Roeck  *	restart handlers may exist; for example, one restart handler might
183b63adb97SGuenter Roeck  *	restart the entire system, while another only restarts the CPU.
184b63adb97SGuenter Roeck  *	In such cases, the restart handler which only restarts part of the
185b63adb97SGuenter Roeck  *	hardware is expected to register with low priority to ensure that
186b63adb97SGuenter Roeck  *	it only runs if no other means to restart the system is available.
187b63adb97SGuenter Roeck  *
188b63adb97SGuenter Roeck  *	Currently always returns zero, as atomic_notifier_chain_register()
189b63adb97SGuenter Roeck  *	always returns zero.
190b63adb97SGuenter Roeck  */
191b63adb97SGuenter Roeck int register_restart_handler(struct notifier_block *nb)
192b63adb97SGuenter Roeck {
193b63adb97SGuenter Roeck 	return atomic_notifier_chain_register(&restart_handler_list, nb);
194b63adb97SGuenter Roeck }
195b63adb97SGuenter Roeck EXPORT_SYMBOL(register_restart_handler);
196b63adb97SGuenter Roeck 
197b63adb97SGuenter Roeck /**
198b63adb97SGuenter Roeck  *	unregister_restart_handler - Unregister previously registered
199b63adb97SGuenter Roeck  *				     restart handler
200b63adb97SGuenter Roeck  *	@nb: Hook to be unregistered
201b63adb97SGuenter Roeck  *
202b63adb97SGuenter Roeck  *	Unregisters a previously registered restart handler function.
203b63adb97SGuenter Roeck  *
204b63adb97SGuenter Roeck  *	Returns zero on success, or %-ENOENT on failure.
205b63adb97SGuenter Roeck  */
206b63adb97SGuenter Roeck int unregister_restart_handler(struct notifier_block *nb)
207b63adb97SGuenter Roeck {
208b63adb97SGuenter Roeck 	return atomic_notifier_chain_unregister(&restart_handler_list, nb);
209b63adb97SGuenter Roeck }
210b63adb97SGuenter Roeck EXPORT_SYMBOL(unregister_restart_handler);
211b63adb97SGuenter Roeck 
212b63adb97SGuenter Roeck /**
213b63adb97SGuenter Roeck  *	do_kernel_restart - Execute kernel restart handler call chain
214b63adb97SGuenter Roeck  *
215b63adb97SGuenter Roeck  *	Calls functions registered with register_restart_handler.
216b63adb97SGuenter Roeck  *
217b63adb97SGuenter Roeck  *	Expected to be called from machine_restart as last step of the restart
218b63adb97SGuenter Roeck  *	sequence.
219b63adb97SGuenter Roeck  *
220b63adb97SGuenter Roeck  *	Restarts the system immediately if a restart handler function has been
221b63adb97SGuenter Roeck  *	registered. Otherwise does nothing.
222b63adb97SGuenter Roeck  */
223b63adb97SGuenter Roeck void do_kernel_restart(char *cmd)
224b63adb97SGuenter Roeck {
225b63adb97SGuenter Roeck 	atomic_notifier_call_chain(&restart_handler_list, reboot_mode, cmd);
226b63adb97SGuenter Roeck }
227b63adb97SGuenter Roeck 
228c97102baSVivek Goyal void migrate_to_reboot_cpu(void)
22915d94b82SRobin Holt {
23015d94b82SRobin Holt 	/* The boot cpu is always logical cpu 0 */
2311b3a5d02SRobin Holt 	int cpu = reboot_cpu;
23215d94b82SRobin Holt 
23315d94b82SRobin Holt 	cpu_hotplug_disable();
23415d94b82SRobin Holt 
23515d94b82SRobin Holt 	/* Make certain the cpu I'm about to reboot on is online */
23615d94b82SRobin Holt 	if (!cpu_online(cpu))
23715d94b82SRobin Holt 		cpu = cpumask_first(cpu_online_mask);
23815d94b82SRobin Holt 
23915d94b82SRobin Holt 	/* Prevent races with other tasks migrating this task */
24015d94b82SRobin Holt 	current->flags |= PF_NO_SETAFFINITY;
24115d94b82SRobin Holt 
24215d94b82SRobin Holt 	/* Make certain I only run on the appropriate processor */
24315d94b82SRobin Holt 	set_cpus_allowed_ptr(current, cpumask_of(cpu));
24415d94b82SRobin Holt }
24515d94b82SRobin Holt 
24615d94b82SRobin Holt /**
24715d94b82SRobin Holt  *	kernel_restart - reboot the system
24815d94b82SRobin Holt  *	@cmd: pointer to buffer containing command to execute for restart
24915d94b82SRobin Holt  *		or %NULL
25015d94b82SRobin Holt  *
25115d94b82SRobin Holt  *	Shutdown everything and perform a clean reboot.
25215d94b82SRobin Holt  *	This is not safe to call in interrupt context.
25315d94b82SRobin Holt  */
25415d94b82SRobin Holt void kernel_restart(char *cmd)
25515d94b82SRobin Holt {
25615d94b82SRobin Holt 	kernel_restart_prepare(cmd);
25715d94b82SRobin Holt 	migrate_to_reboot_cpu();
25815d94b82SRobin Holt 	syscore_shutdown();
25915d94b82SRobin Holt 	if (!cmd)
260972ee83dSRobin Holt 		pr_emerg("Restarting system\n");
26115d94b82SRobin Holt 	else
262972ee83dSRobin Holt 		pr_emerg("Restarting system with command '%s'\n", cmd);
2636d3cf962SKees Cook 	kmsg_dump(KMSG_DUMP_SHUTDOWN);
26415d94b82SRobin Holt 	machine_restart(cmd);
26515d94b82SRobin Holt }
26615d94b82SRobin Holt EXPORT_SYMBOL_GPL(kernel_restart);
26715d94b82SRobin Holt 
26815d94b82SRobin Holt static void kernel_shutdown_prepare(enum system_states state)
26915d94b82SRobin Holt {
27015d94b82SRobin Holt 	blocking_notifier_call_chain(&reboot_notifier_list,
27115d94b82SRobin Holt 		(state == SYSTEM_HALT) ? SYS_HALT : SYS_POWER_OFF, NULL);
27215d94b82SRobin Holt 	system_state = state;
27315d94b82SRobin Holt 	usermodehelper_disable();
27415d94b82SRobin Holt 	device_shutdown();
27515d94b82SRobin Holt }
27615d94b82SRobin Holt /**
27715d94b82SRobin Holt  *	kernel_halt - halt the system
27815d94b82SRobin Holt  *
27915d94b82SRobin Holt  *	Shutdown everything and perform a clean system halt.
28015d94b82SRobin Holt  */
28115d94b82SRobin Holt void kernel_halt(void)
28215d94b82SRobin Holt {
28315d94b82SRobin Holt 	kernel_shutdown_prepare(SYSTEM_HALT);
28415d94b82SRobin Holt 	migrate_to_reboot_cpu();
28515d94b82SRobin Holt 	syscore_shutdown();
286972ee83dSRobin Holt 	pr_emerg("System halted\n");
2876d3cf962SKees Cook 	kmsg_dump(KMSG_DUMP_SHUTDOWN);
28815d94b82SRobin Holt 	machine_halt();
28915d94b82SRobin Holt }
29015d94b82SRobin Holt EXPORT_SYMBOL_GPL(kernel_halt);
29115d94b82SRobin Holt 
292232edc2fSDmitry Osipenko /*
293232edc2fSDmitry Osipenko  *	Notifier list for kernel code which wants to be called
294232edc2fSDmitry Osipenko  *	to prepare system for power off.
295232edc2fSDmitry Osipenko  */
296232edc2fSDmitry Osipenko static BLOCKING_NOTIFIER_HEAD(power_off_prep_handler_list);
297232edc2fSDmitry Osipenko 
298232edc2fSDmitry Osipenko /*
299232edc2fSDmitry Osipenko  *	Notifier list for kernel code which wants to be called
300232edc2fSDmitry Osipenko  *	to power off system.
301232edc2fSDmitry Osipenko  */
302232edc2fSDmitry Osipenko static ATOMIC_NOTIFIER_HEAD(power_off_handler_list);
303232edc2fSDmitry Osipenko 
304232edc2fSDmitry Osipenko static int sys_off_notify(struct notifier_block *nb,
305232edc2fSDmitry Osipenko 			  unsigned long mode, void *cmd)
306232edc2fSDmitry Osipenko {
307232edc2fSDmitry Osipenko 	struct sys_off_handler *handler;
308232edc2fSDmitry Osipenko 	struct sys_off_data data = {};
309232edc2fSDmitry Osipenko 
310232edc2fSDmitry Osipenko 	handler = container_of(nb, struct sys_off_handler, nb);
311232edc2fSDmitry Osipenko 	data.cb_data = handler->cb_data;
312232edc2fSDmitry Osipenko 	data.mode = mode;
313232edc2fSDmitry Osipenko 	data.cmd = cmd;
314232edc2fSDmitry Osipenko 
315232edc2fSDmitry Osipenko 	return handler->sys_off_cb(&data);
316232edc2fSDmitry Osipenko }
317232edc2fSDmitry Osipenko 
318587b9bfeSDmitry Osipenko static struct sys_off_handler platform_sys_off_handler;
319587b9bfeSDmitry Osipenko 
320587b9bfeSDmitry Osipenko static struct sys_off_handler *alloc_sys_off_handler(int priority)
321587b9bfeSDmitry Osipenko {
322587b9bfeSDmitry Osipenko 	struct sys_off_handler *handler;
323*2b8c612cSDmitry Osipenko 	gfp_t flags;
324587b9bfeSDmitry Osipenko 
325587b9bfeSDmitry Osipenko 	/*
326587b9bfeSDmitry Osipenko 	 * Platforms like m68k can't allocate sys_off handler dynamically
327587b9bfeSDmitry Osipenko 	 * at the early boot time because memory allocator isn't available yet.
328587b9bfeSDmitry Osipenko 	 */
329587b9bfeSDmitry Osipenko 	if (priority == SYS_OFF_PRIO_PLATFORM) {
330587b9bfeSDmitry Osipenko 		handler = &platform_sys_off_handler;
331587b9bfeSDmitry Osipenko 		if (handler->cb_data)
332587b9bfeSDmitry Osipenko 			return ERR_PTR(-EBUSY);
333587b9bfeSDmitry Osipenko 	} else {
334*2b8c612cSDmitry Osipenko 		if (system_state > SYSTEM_RUNNING)
335*2b8c612cSDmitry Osipenko 			flags = GFP_ATOMIC;
336*2b8c612cSDmitry Osipenko 		else
337*2b8c612cSDmitry Osipenko 			flags = GFP_KERNEL;
338*2b8c612cSDmitry Osipenko 
339*2b8c612cSDmitry Osipenko 		handler = kzalloc(sizeof(*handler), flags);
340587b9bfeSDmitry Osipenko 		if (!handler)
341587b9bfeSDmitry Osipenko 			return ERR_PTR(-ENOMEM);
342587b9bfeSDmitry Osipenko 	}
343587b9bfeSDmitry Osipenko 
344587b9bfeSDmitry Osipenko 	return handler;
345587b9bfeSDmitry Osipenko }
346587b9bfeSDmitry Osipenko 
347587b9bfeSDmitry Osipenko static void free_sys_off_handler(struct sys_off_handler *handler)
348587b9bfeSDmitry Osipenko {
349587b9bfeSDmitry Osipenko 	if (handler == &platform_sys_off_handler)
350587b9bfeSDmitry Osipenko 		memset(handler, 0, sizeof(*handler));
351587b9bfeSDmitry Osipenko 	else
352587b9bfeSDmitry Osipenko 		kfree(handler);
353587b9bfeSDmitry Osipenko }
354587b9bfeSDmitry Osipenko 
355232edc2fSDmitry Osipenko /**
356232edc2fSDmitry Osipenko  *	register_sys_off_handler - Register sys-off handler
357232edc2fSDmitry Osipenko  *	@mode: Sys-off mode
358232edc2fSDmitry Osipenko  *	@priority: Handler priority
359232edc2fSDmitry Osipenko  *	@callback: Callback function
360232edc2fSDmitry Osipenko  *	@cb_data: Callback argument
361232edc2fSDmitry Osipenko  *
362232edc2fSDmitry Osipenko  *	Registers system power-off or restart handler that will be invoked
363232edc2fSDmitry Osipenko  *	at the step corresponding to the given sys-off mode. Handler's callback
364232edc2fSDmitry Osipenko  *	should return NOTIFY_DONE to permit execution of the next handler in
365232edc2fSDmitry Osipenko  *	the call chain or NOTIFY_STOP to break the chain (in error case for
366232edc2fSDmitry Osipenko  *	example).
367232edc2fSDmitry Osipenko  *
368232edc2fSDmitry Osipenko  *	Multiple handlers can be registered at the default priority level.
369232edc2fSDmitry Osipenko  *
370232edc2fSDmitry Osipenko  *	Only one handler can be registered at the non-default priority level,
371232edc2fSDmitry Osipenko  *	otherwise ERR_PTR(-EBUSY) is returned.
372232edc2fSDmitry Osipenko  *
373232edc2fSDmitry Osipenko  *	Returns a new instance of struct sys_off_handler on success, or
374232edc2fSDmitry Osipenko  *	an ERR_PTR()-encoded error code otherwise.
375232edc2fSDmitry Osipenko  */
376232edc2fSDmitry Osipenko struct sys_off_handler *
377232edc2fSDmitry Osipenko register_sys_off_handler(enum sys_off_mode mode,
378232edc2fSDmitry Osipenko 			 int priority,
379232edc2fSDmitry Osipenko 			 int (*callback)(struct sys_off_data *data),
380232edc2fSDmitry Osipenko 			 void *cb_data)
381232edc2fSDmitry Osipenko {
382232edc2fSDmitry Osipenko 	struct sys_off_handler *handler;
383232edc2fSDmitry Osipenko 	int err;
384232edc2fSDmitry Osipenko 
385587b9bfeSDmitry Osipenko 	handler = alloc_sys_off_handler(priority);
386587b9bfeSDmitry Osipenko 	if (IS_ERR(handler))
387587b9bfeSDmitry Osipenko 		return handler;
388232edc2fSDmitry Osipenko 
389232edc2fSDmitry Osipenko 	switch (mode) {
390232edc2fSDmitry Osipenko 	case SYS_OFF_MODE_POWER_OFF_PREPARE:
391232edc2fSDmitry Osipenko 		handler->list = &power_off_prep_handler_list;
392232edc2fSDmitry Osipenko 		handler->blocking = true;
393232edc2fSDmitry Osipenko 		break;
394232edc2fSDmitry Osipenko 
395232edc2fSDmitry Osipenko 	case SYS_OFF_MODE_POWER_OFF:
396232edc2fSDmitry Osipenko 		handler->list = &power_off_handler_list;
397232edc2fSDmitry Osipenko 		break;
398232edc2fSDmitry Osipenko 
399232edc2fSDmitry Osipenko 	case SYS_OFF_MODE_RESTART:
400232edc2fSDmitry Osipenko 		handler->list = &restart_handler_list;
401232edc2fSDmitry Osipenko 		break;
402232edc2fSDmitry Osipenko 
403232edc2fSDmitry Osipenko 	default:
404587b9bfeSDmitry Osipenko 		free_sys_off_handler(handler);
405232edc2fSDmitry Osipenko 		return ERR_PTR(-EINVAL);
406232edc2fSDmitry Osipenko 	}
407232edc2fSDmitry Osipenko 
408232edc2fSDmitry Osipenko 	handler->nb.notifier_call = sys_off_notify;
409232edc2fSDmitry Osipenko 	handler->nb.priority = priority;
410232edc2fSDmitry Osipenko 	handler->sys_off_cb = callback;
411232edc2fSDmitry Osipenko 	handler->cb_data = cb_data;
412232edc2fSDmitry Osipenko 	handler->mode = mode;
413232edc2fSDmitry Osipenko 
414232edc2fSDmitry Osipenko 	if (handler->blocking) {
415232edc2fSDmitry Osipenko 		if (priority == SYS_OFF_PRIO_DEFAULT)
416232edc2fSDmitry Osipenko 			err = blocking_notifier_chain_register(handler->list,
417232edc2fSDmitry Osipenko 							       &handler->nb);
418232edc2fSDmitry Osipenko 		else
419232edc2fSDmitry Osipenko 			err = blocking_notifier_chain_register_unique_prio(handler->list,
420232edc2fSDmitry Osipenko 									   &handler->nb);
421232edc2fSDmitry Osipenko 	} else {
422232edc2fSDmitry Osipenko 		if (priority == SYS_OFF_PRIO_DEFAULT)
423232edc2fSDmitry Osipenko 			err = atomic_notifier_chain_register(handler->list,
424232edc2fSDmitry Osipenko 							     &handler->nb);
425232edc2fSDmitry Osipenko 		else
426232edc2fSDmitry Osipenko 			err = atomic_notifier_chain_register_unique_prio(handler->list,
427232edc2fSDmitry Osipenko 									 &handler->nb);
428232edc2fSDmitry Osipenko 	}
429232edc2fSDmitry Osipenko 
430232edc2fSDmitry Osipenko 	if (err) {
431587b9bfeSDmitry Osipenko 		free_sys_off_handler(handler);
432232edc2fSDmitry Osipenko 		return ERR_PTR(err);
433232edc2fSDmitry Osipenko 	}
434232edc2fSDmitry Osipenko 
435232edc2fSDmitry Osipenko 	return handler;
436232edc2fSDmitry Osipenko }
437232edc2fSDmitry Osipenko EXPORT_SYMBOL_GPL(register_sys_off_handler);
438232edc2fSDmitry Osipenko 
439232edc2fSDmitry Osipenko /**
440232edc2fSDmitry Osipenko  *	unregister_sys_off_handler - Unregister sys-off handler
441232edc2fSDmitry Osipenko  *	@handler: Sys-off handler
442232edc2fSDmitry Osipenko  *
443232edc2fSDmitry Osipenko  *	Unregisters given sys-off handler.
444232edc2fSDmitry Osipenko  */
445232edc2fSDmitry Osipenko void unregister_sys_off_handler(struct sys_off_handler *handler)
446232edc2fSDmitry Osipenko {
447232edc2fSDmitry Osipenko 	int err;
448232edc2fSDmitry Osipenko 
449*2b8c612cSDmitry Osipenko 	if (IS_ERR_OR_NULL(handler))
450232edc2fSDmitry Osipenko 		return;
451232edc2fSDmitry Osipenko 
452232edc2fSDmitry Osipenko 	if (handler->blocking)
453232edc2fSDmitry Osipenko 		err = blocking_notifier_chain_unregister(handler->list,
454232edc2fSDmitry Osipenko 							 &handler->nb);
455232edc2fSDmitry Osipenko 	else
456232edc2fSDmitry Osipenko 		err = atomic_notifier_chain_unregister(handler->list,
457232edc2fSDmitry Osipenko 						       &handler->nb);
458232edc2fSDmitry Osipenko 
459232edc2fSDmitry Osipenko 	/* sanity check, shall never happen */
460232edc2fSDmitry Osipenko 	WARN_ON(err);
461232edc2fSDmitry Osipenko 
462587b9bfeSDmitry Osipenko 	free_sys_off_handler(handler);
463232edc2fSDmitry Osipenko }
464232edc2fSDmitry Osipenko EXPORT_SYMBOL_GPL(unregister_sys_off_handler);
465232edc2fSDmitry Osipenko 
466232edc2fSDmitry Osipenko static void devm_unregister_sys_off_handler(void *data)
467232edc2fSDmitry Osipenko {
468232edc2fSDmitry Osipenko 	struct sys_off_handler *handler = data;
469232edc2fSDmitry Osipenko 
470232edc2fSDmitry Osipenko 	unregister_sys_off_handler(handler);
471232edc2fSDmitry Osipenko }
472232edc2fSDmitry Osipenko 
473232edc2fSDmitry Osipenko /**
474232edc2fSDmitry Osipenko  *	devm_register_sys_off_handler - Register sys-off handler
475232edc2fSDmitry Osipenko  *	@dev: Device that registers handler
476232edc2fSDmitry Osipenko  *	@mode: Sys-off mode
477232edc2fSDmitry Osipenko  *	@priority: Handler priority
478232edc2fSDmitry Osipenko  *	@callback: Callback function
479232edc2fSDmitry Osipenko  *	@cb_data: Callback argument
480232edc2fSDmitry Osipenko  *
481232edc2fSDmitry Osipenko  *	Registers resource-managed sys-off handler.
482232edc2fSDmitry Osipenko  *
483232edc2fSDmitry Osipenko  *	Returns zero on success, or error code on failure.
484232edc2fSDmitry Osipenko  */
485232edc2fSDmitry Osipenko int devm_register_sys_off_handler(struct device *dev,
486232edc2fSDmitry Osipenko 				  enum sys_off_mode mode,
487232edc2fSDmitry Osipenko 				  int priority,
488232edc2fSDmitry Osipenko 				  int (*callback)(struct sys_off_data *data),
489232edc2fSDmitry Osipenko 				  void *cb_data)
490232edc2fSDmitry Osipenko {
491232edc2fSDmitry Osipenko 	struct sys_off_handler *handler;
492232edc2fSDmitry Osipenko 
493232edc2fSDmitry Osipenko 	handler = register_sys_off_handler(mode, priority, callback, cb_data);
494232edc2fSDmitry Osipenko 	if (IS_ERR(handler))
495232edc2fSDmitry Osipenko 		return PTR_ERR(handler);
496232edc2fSDmitry Osipenko 
497232edc2fSDmitry Osipenko 	return devm_add_action_or_reset(dev, devm_unregister_sys_off_handler,
498232edc2fSDmitry Osipenko 					handler);
499232edc2fSDmitry Osipenko }
500232edc2fSDmitry Osipenko EXPORT_SYMBOL_GPL(devm_register_sys_off_handler);
501232edc2fSDmitry Osipenko 
502d2c54153SDmitry Osipenko /**
503d2c54153SDmitry Osipenko  *	devm_register_power_off_handler - Register power-off handler
504d2c54153SDmitry Osipenko  *	@dev: Device that registers callback
505d2c54153SDmitry Osipenko  *	@callback: Callback function
506d2c54153SDmitry Osipenko  *	@cb_data: Callback's argument
507d2c54153SDmitry Osipenko  *
508d2c54153SDmitry Osipenko  *	Registers resource-managed sys-off handler with a default priority
509d2c54153SDmitry Osipenko  *	and using power-off mode.
510d2c54153SDmitry Osipenko  *
511d2c54153SDmitry Osipenko  *	Returns zero on success, or error code on failure.
512d2c54153SDmitry Osipenko  */
513d2c54153SDmitry Osipenko int devm_register_power_off_handler(struct device *dev,
514d2c54153SDmitry Osipenko 				    int (*callback)(struct sys_off_data *data),
515d2c54153SDmitry Osipenko 				    void *cb_data)
516d2c54153SDmitry Osipenko {
517d2c54153SDmitry Osipenko 	return devm_register_sys_off_handler(dev,
518d2c54153SDmitry Osipenko 					     SYS_OFF_MODE_POWER_OFF,
519d2c54153SDmitry Osipenko 					     SYS_OFF_PRIO_DEFAULT,
520d2c54153SDmitry Osipenko 					     callback, cb_data);
521d2c54153SDmitry Osipenko }
522d2c54153SDmitry Osipenko EXPORT_SYMBOL_GPL(devm_register_power_off_handler);
523d2c54153SDmitry Osipenko 
5246779db97SDmitry Osipenko /**
5256779db97SDmitry Osipenko  *	devm_register_restart_handler - Register restart handler
5266779db97SDmitry Osipenko  *	@dev: Device that registers callback
5276779db97SDmitry Osipenko  *	@callback: Callback function
5286779db97SDmitry Osipenko  *	@cb_data: Callback's argument
5296779db97SDmitry Osipenko  *
5306779db97SDmitry Osipenko  *	Registers resource-managed sys-off handler with a default priority
5316779db97SDmitry Osipenko  *	and using restart mode.
5326779db97SDmitry Osipenko  *
5336779db97SDmitry Osipenko  *	Returns zero on success, or error code on failure.
5346779db97SDmitry Osipenko  */
5356779db97SDmitry Osipenko int devm_register_restart_handler(struct device *dev,
5366779db97SDmitry Osipenko 				  int (*callback)(struct sys_off_data *data),
5376779db97SDmitry Osipenko 				  void *cb_data)
5386779db97SDmitry Osipenko {
5396779db97SDmitry Osipenko 	return devm_register_sys_off_handler(dev,
5406779db97SDmitry Osipenko 					     SYS_OFF_MODE_RESTART,
5416779db97SDmitry Osipenko 					     SYS_OFF_PRIO_DEFAULT,
5426779db97SDmitry Osipenko 					     callback, cb_data);
5436779db97SDmitry Osipenko }
5446779db97SDmitry Osipenko EXPORT_SYMBOL_GPL(devm_register_restart_handler);
5456779db97SDmitry Osipenko 
546fb61375eSDmitry Osipenko static struct sys_off_handler *platform_power_off_handler;
547fb61375eSDmitry Osipenko 
548fb61375eSDmitry Osipenko static int platform_power_off_notify(struct sys_off_data *data)
549fb61375eSDmitry Osipenko {
550fb61375eSDmitry Osipenko 	void (*platform_power_power_off_cb)(void) = data->cb_data;
551fb61375eSDmitry Osipenko 
552fb61375eSDmitry Osipenko 	platform_power_power_off_cb();
553fb61375eSDmitry Osipenko 
554fb61375eSDmitry Osipenko 	return NOTIFY_DONE;
555fb61375eSDmitry Osipenko }
556fb61375eSDmitry Osipenko 
557fb61375eSDmitry Osipenko /**
558fb61375eSDmitry Osipenko  *	register_platform_power_off - Register platform-level power-off callback
559fb61375eSDmitry Osipenko  *	@power_off: Power-off callback
560fb61375eSDmitry Osipenko  *
561fb61375eSDmitry Osipenko  *	Registers power-off callback that will be called as last step
562fb61375eSDmitry Osipenko  *	of the power-off sequence. This callback is expected to be invoked
563fb61375eSDmitry Osipenko  *	for the last resort. Only one platform power-off callback is allowed
564fb61375eSDmitry Osipenko  *	to be registered at a time.
565fb61375eSDmitry Osipenko  *
566fb61375eSDmitry Osipenko  *	Returns zero on success, or error code on failure.
567fb61375eSDmitry Osipenko  */
568fb61375eSDmitry Osipenko int register_platform_power_off(void (*power_off)(void))
569fb61375eSDmitry Osipenko {
570fb61375eSDmitry Osipenko 	struct sys_off_handler *handler;
571fb61375eSDmitry Osipenko 
572fb61375eSDmitry Osipenko 	handler = register_sys_off_handler(SYS_OFF_MODE_POWER_OFF,
573fb61375eSDmitry Osipenko 					   SYS_OFF_PRIO_PLATFORM,
574fb61375eSDmitry Osipenko 					   platform_power_off_notify,
575fb61375eSDmitry Osipenko 					   power_off);
576fb61375eSDmitry Osipenko 	if (IS_ERR(handler))
577fb61375eSDmitry Osipenko 		return PTR_ERR(handler);
578fb61375eSDmitry Osipenko 
579fb61375eSDmitry Osipenko 	platform_power_off_handler = handler;
580fb61375eSDmitry Osipenko 
581fb61375eSDmitry Osipenko 	return 0;
582fb61375eSDmitry Osipenko }
583fb61375eSDmitry Osipenko EXPORT_SYMBOL_GPL(register_platform_power_off);
584fb61375eSDmitry Osipenko 
585fb61375eSDmitry Osipenko /**
586fb61375eSDmitry Osipenko  *	unregister_platform_power_off - Unregister platform-level power-off callback
587fb61375eSDmitry Osipenko  *	@power_off: Power-off callback
588fb61375eSDmitry Osipenko  *
589fb61375eSDmitry Osipenko  *	Unregisters previously registered platform power-off callback.
590fb61375eSDmitry Osipenko  */
591fb61375eSDmitry Osipenko void unregister_platform_power_off(void (*power_off)(void))
592fb61375eSDmitry Osipenko {
593fb61375eSDmitry Osipenko 	if (platform_power_off_handler &&
594fb61375eSDmitry Osipenko 	    platform_power_off_handler->cb_data == power_off) {
595fb61375eSDmitry Osipenko 		unregister_sys_off_handler(platform_power_off_handler);
596fb61375eSDmitry Osipenko 		platform_power_off_handler = NULL;
597fb61375eSDmitry Osipenko 	}
598fb61375eSDmitry Osipenko }
599fb61375eSDmitry Osipenko EXPORT_SYMBOL_GPL(unregister_platform_power_off);
600fb61375eSDmitry Osipenko 
6017b9a3de9SDmitry Osipenko static int legacy_pm_power_off(struct sys_off_data *data)
6027b9a3de9SDmitry Osipenko {
6037b9a3de9SDmitry Osipenko 	if (pm_power_off)
6047b9a3de9SDmitry Osipenko 		pm_power_off();
6057b9a3de9SDmitry Osipenko 
6067b9a3de9SDmitry Osipenko 	return NOTIFY_DONE;
6077b9a3de9SDmitry Osipenko }
6087b9a3de9SDmitry Osipenko 
6097b9a3de9SDmitry Osipenko static void do_kernel_power_off_prepare(void)
6107b9a3de9SDmitry Osipenko {
6117b9a3de9SDmitry Osipenko 	blocking_notifier_call_chain(&power_off_prep_handler_list, 0, NULL);
6127b9a3de9SDmitry Osipenko }
6137b9a3de9SDmitry Osipenko 
61415d94b82SRobin Holt /**
6152b6aa733SDmitry Osipenko  *	do_kernel_power_off - Execute kernel power-off handler call chain
6162b6aa733SDmitry Osipenko  *
6172b6aa733SDmitry Osipenko  *	Expected to be called as last step of the power-off sequence.
6182b6aa733SDmitry Osipenko  *
6192b6aa733SDmitry Osipenko  *	Powers off the system immediately if a power-off handler function has
6202b6aa733SDmitry Osipenko  *	been registered. Otherwise does nothing.
6212b6aa733SDmitry Osipenko  */
6222b6aa733SDmitry Osipenko void do_kernel_power_off(void)
6232b6aa733SDmitry Osipenko {
624*2b8c612cSDmitry Osipenko 	struct sys_off_handler *sys_off = NULL;
625*2b8c612cSDmitry Osipenko 
626*2b8c612cSDmitry Osipenko 	/*
627*2b8c612cSDmitry Osipenko 	 * Register sys-off handlers for legacy PM callback. This allows
628*2b8c612cSDmitry Osipenko 	 * legacy PM callbacks temporary co-exist with the new sys-off API.
629*2b8c612cSDmitry Osipenko 	 *
630*2b8c612cSDmitry Osipenko 	 * TODO: Remove legacy handlers once all legacy PM users will be
631*2b8c612cSDmitry Osipenko 	 *       switched to the sys-off based APIs.
632*2b8c612cSDmitry Osipenko 	 */
633*2b8c612cSDmitry Osipenko 	if (pm_power_off)
634*2b8c612cSDmitry Osipenko 		sys_off = register_sys_off_handler(SYS_OFF_MODE_POWER_OFF,
635*2b8c612cSDmitry Osipenko 						   SYS_OFF_PRIO_DEFAULT,
636*2b8c612cSDmitry Osipenko 						   legacy_pm_power_off, NULL);
637*2b8c612cSDmitry Osipenko 
6382b6aa733SDmitry Osipenko 	atomic_notifier_call_chain(&power_off_handler_list, 0, NULL);
639*2b8c612cSDmitry Osipenko 
640*2b8c612cSDmitry Osipenko 	unregister_sys_off_handler(sys_off);
6412b6aa733SDmitry Osipenko }
6422b6aa733SDmitry Osipenko 
6432b6aa733SDmitry Osipenko /**
6440e2110d2SDmitry Osipenko  *	kernel_can_power_off - check whether system can be powered off
6450e2110d2SDmitry Osipenko  *
6460e2110d2SDmitry Osipenko  *	Returns true if power-off handler is registered and system can be
6470e2110d2SDmitry Osipenko  *	powered off, false otherwise.
6480e2110d2SDmitry Osipenko  */
6490e2110d2SDmitry Osipenko bool kernel_can_power_off(void)
6500e2110d2SDmitry Osipenko {
651*2b8c612cSDmitry Osipenko 	return !atomic_notifier_call_chain_is_empty(&power_off_handler_list) ||
652*2b8c612cSDmitry Osipenko 		pm_power_off;
6530e2110d2SDmitry Osipenko }
6540e2110d2SDmitry Osipenko EXPORT_SYMBOL_GPL(kernel_can_power_off);
6550e2110d2SDmitry Osipenko 
65615d94b82SRobin Holt /**
65715d94b82SRobin Holt  *	kernel_power_off - power_off the system
65815d94b82SRobin Holt  *
65915d94b82SRobin Holt  *	Shutdown everything and perform a clean system power_off.
66015d94b82SRobin Holt  */
66115d94b82SRobin Holt void kernel_power_off(void)
66215d94b82SRobin Holt {
66315d94b82SRobin Holt 	kernel_shutdown_prepare(SYSTEM_POWER_OFF);
6647b9a3de9SDmitry Osipenko 	do_kernel_power_off_prepare();
66515d94b82SRobin Holt 	migrate_to_reboot_cpu();
66615d94b82SRobin Holt 	syscore_shutdown();
667972ee83dSRobin Holt 	pr_emerg("Power down\n");
6686d3cf962SKees Cook 	kmsg_dump(KMSG_DUMP_SHUTDOWN);
66915d94b82SRobin Holt 	machine_power_off();
67015d94b82SRobin Holt }
67115d94b82SRobin Holt EXPORT_SYMBOL_GPL(kernel_power_off);
67215d94b82SRobin Holt 
67355f2503cSPingfan Liu DEFINE_MUTEX(system_transition_mutex);
67415d94b82SRobin Holt 
67515d94b82SRobin Holt /*
67615d94b82SRobin Holt  * Reboot system call: for obvious reasons only root may call it,
67715d94b82SRobin Holt  * and even root needs to set up some magic numbers in the registers
67815d94b82SRobin Holt  * so that some mistake won't make this reboot the whole machine.
67915d94b82SRobin Holt  * You can also set the meaning of the ctrl-alt-del-key here.
68015d94b82SRobin Holt  *
68115d94b82SRobin Holt  * reboot doesn't sync: do that yourself before calling this.
68215d94b82SRobin Holt  */
68315d94b82SRobin Holt SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
68415d94b82SRobin Holt 		void __user *, arg)
68515d94b82SRobin Holt {
68615d94b82SRobin Holt 	struct pid_namespace *pid_ns = task_active_pid_ns(current);
68715d94b82SRobin Holt 	char buffer[256];
68815d94b82SRobin Holt 	int ret = 0;
68915d94b82SRobin Holt 
69015d94b82SRobin Holt 	/* We only trust the superuser with rebooting the system. */
69115d94b82SRobin Holt 	if (!ns_capable(pid_ns->user_ns, CAP_SYS_BOOT))
69215d94b82SRobin Holt 		return -EPERM;
69315d94b82SRobin Holt 
69415d94b82SRobin Holt 	/* For safety, we require "magic" arguments. */
69515d94b82SRobin Holt 	if (magic1 != LINUX_REBOOT_MAGIC1 ||
69615d94b82SRobin Holt 			(magic2 != LINUX_REBOOT_MAGIC2 &&
69715d94b82SRobin Holt 			magic2 != LINUX_REBOOT_MAGIC2A &&
69815d94b82SRobin Holt 			magic2 != LINUX_REBOOT_MAGIC2B &&
69915d94b82SRobin Holt 			magic2 != LINUX_REBOOT_MAGIC2C))
70015d94b82SRobin Holt 		return -EINVAL;
70115d94b82SRobin Holt 
70215d94b82SRobin Holt 	/*
70315d94b82SRobin Holt 	 * If pid namespaces are enabled and the current task is in a child
70415d94b82SRobin Holt 	 * pid_namespace, the command is handled by reboot_pid_ns() which will
70515d94b82SRobin Holt 	 * call do_exit().
70615d94b82SRobin Holt 	 */
70715d94b82SRobin Holt 	ret = reboot_pid_ns(pid_ns, cmd);
70815d94b82SRobin Holt 	if (ret)
70915d94b82SRobin Holt 		return ret;
71015d94b82SRobin Holt 
71115d94b82SRobin Holt 	/* Instead of trying to make the power_off code look like
71215d94b82SRobin Holt 	 * halt when pm_power_off is not set do it the easy way.
71315d94b82SRobin Holt 	 */
7140e2110d2SDmitry Osipenko 	if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !kernel_can_power_off())
71515d94b82SRobin Holt 		cmd = LINUX_REBOOT_CMD_HALT;
71615d94b82SRobin Holt 
71755f2503cSPingfan Liu 	mutex_lock(&system_transition_mutex);
71815d94b82SRobin Holt 	switch (cmd) {
71915d94b82SRobin Holt 	case LINUX_REBOOT_CMD_RESTART:
72015d94b82SRobin Holt 		kernel_restart(NULL);
72115d94b82SRobin Holt 		break;
72215d94b82SRobin Holt 
72315d94b82SRobin Holt 	case LINUX_REBOOT_CMD_CAD_ON:
72415d94b82SRobin Holt 		C_A_D = 1;
72515d94b82SRobin Holt 		break;
72615d94b82SRobin Holt 
72715d94b82SRobin Holt 	case LINUX_REBOOT_CMD_CAD_OFF:
72815d94b82SRobin Holt 		C_A_D = 0;
72915d94b82SRobin Holt 		break;
73015d94b82SRobin Holt 
73115d94b82SRobin Holt 	case LINUX_REBOOT_CMD_HALT:
73215d94b82SRobin Holt 		kernel_halt();
73315d94b82SRobin Holt 		do_exit(0);
73415d94b82SRobin Holt 
73515d94b82SRobin Holt 	case LINUX_REBOOT_CMD_POWER_OFF:
73615d94b82SRobin Holt 		kernel_power_off();
73715d94b82SRobin Holt 		do_exit(0);
73815d94b82SRobin Holt 		break;
73915d94b82SRobin Holt 
74015d94b82SRobin Holt 	case LINUX_REBOOT_CMD_RESTART2:
741972ee83dSRobin Holt 		ret = strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1);
742972ee83dSRobin Holt 		if (ret < 0) {
74315d94b82SRobin Holt 			ret = -EFAULT;
74415d94b82SRobin Holt 			break;
74515d94b82SRobin Holt 		}
74615d94b82SRobin Holt 		buffer[sizeof(buffer) - 1] = '\0';
74715d94b82SRobin Holt 
74815d94b82SRobin Holt 		kernel_restart(buffer);
74915d94b82SRobin Holt 		break;
75015d94b82SRobin Holt 
7512965faa5SDave Young #ifdef CONFIG_KEXEC_CORE
75215d94b82SRobin Holt 	case LINUX_REBOOT_CMD_KEXEC:
75315d94b82SRobin Holt 		ret = kernel_kexec();
75415d94b82SRobin Holt 		break;
75515d94b82SRobin Holt #endif
75615d94b82SRobin Holt 
75715d94b82SRobin Holt #ifdef CONFIG_HIBERNATION
75815d94b82SRobin Holt 	case LINUX_REBOOT_CMD_SW_SUSPEND:
75915d94b82SRobin Holt 		ret = hibernate();
76015d94b82SRobin Holt 		break;
76115d94b82SRobin Holt #endif
76215d94b82SRobin Holt 
76315d94b82SRobin Holt 	default:
76415d94b82SRobin Holt 		ret = -EINVAL;
76515d94b82SRobin Holt 		break;
76615d94b82SRobin Holt 	}
76755f2503cSPingfan Liu 	mutex_unlock(&system_transition_mutex);
76815d94b82SRobin Holt 	return ret;
76915d94b82SRobin Holt }
77015d94b82SRobin Holt 
77115d94b82SRobin Holt static void deferred_cad(struct work_struct *dummy)
77215d94b82SRobin Holt {
77315d94b82SRobin Holt 	kernel_restart(NULL);
77415d94b82SRobin Holt }
77515d94b82SRobin Holt 
77615d94b82SRobin Holt /*
77715d94b82SRobin Holt  * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
77815d94b82SRobin Holt  * As it's called within an interrupt, it may NOT sync: the only choice
77915d94b82SRobin Holt  * is whether to reboot at once, or just ignore the ctrl-alt-del.
78015d94b82SRobin Holt  */
78115d94b82SRobin Holt void ctrl_alt_del(void)
78215d94b82SRobin Holt {
78315d94b82SRobin Holt 	static DECLARE_WORK(cad_work, deferred_cad);
78415d94b82SRobin Holt 
78515d94b82SRobin Holt 	if (C_A_D)
78615d94b82SRobin Holt 		schedule_work(&cad_work);
78715d94b82SRobin Holt 	else
78815d94b82SRobin Holt 		kill_cad_pid(SIGINT, 1);
78915d94b82SRobin Holt }
79015d94b82SRobin Holt 
79106d17766Stangmeng #define POWEROFF_CMD_PATH_LEN  256
79206d17766Stangmeng static char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
7937a54f46bSJoel Stanley static const char reboot_cmd[] = "/sbin/reboot";
79415d94b82SRobin Holt 
7957a54f46bSJoel Stanley static int run_cmd(const char *cmd)
79615d94b82SRobin Holt {
79715d94b82SRobin Holt 	char **argv;
79815d94b82SRobin Holt 	static char *envp[] = {
79915d94b82SRobin Holt 		"HOME=/",
80015d94b82SRobin Holt 		"PATH=/sbin:/bin:/usr/sbin:/usr/bin",
80115d94b82SRobin Holt 		NULL
80215d94b82SRobin Holt 	};
80315d94b82SRobin Holt 	int ret;
8047a54f46bSJoel Stanley 	argv = argv_split(GFP_KERNEL, cmd, NULL);
80515d94b82SRobin Holt 	if (argv) {
80615d94b82SRobin Holt 		ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
80715d94b82SRobin Holt 		argv_free(argv);
80815d94b82SRobin Holt 	} else {
80915d94b82SRobin Holt 		ret = -ENOMEM;
81015d94b82SRobin Holt 	}
81115d94b82SRobin Holt 
8127a54f46bSJoel Stanley 	return ret;
8137a54f46bSJoel Stanley }
8147a54f46bSJoel Stanley 
8157a54f46bSJoel Stanley static int __orderly_reboot(void)
8167a54f46bSJoel Stanley {
8177a54f46bSJoel Stanley 	int ret;
8187a54f46bSJoel Stanley 
8197a54f46bSJoel Stanley 	ret = run_cmd(reboot_cmd);
8207a54f46bSJoel Stanley 
8217a54f46bSJoel Stanley 	if (ret) {
8222bb2b7b5SJohn Ogness 		printk_prefer_direct_enter();
8237a54f46bSJoel Stanley 		pr_warn("Failed to start orderly reboot: forcing the issue\n");
8247a54f46bSJoel Stanley 		emergency_sync();
8257a54f46bSJoel Stanley 		kernel_restart(NULL);
8262bb2b7b5SJohn Ogness 		printk_prefer_direct_exit();
8277a54f46bSJoel Stanley 	}
8287a54f46bSJoel Stanley 
8297a54f46bSJoel Stanley 	return ret;
8307a54f46bSJoel Stanley }
8317a54f46bSJoel Stanley 
8327a54f46bSJoel Stanley static int __orderly_poweroff(bool force)
8337a54f46bSJoel Stanley {
8347a54f46bSJoel Stanley 	int ret;
8357a54f46bSJoel Stanley 
8367a54f46bSJoel Stanley 	ret = run_cmd(poweroff_cmd);
8377a54f46bSJoel Stanley 
83815d94b82SRobin Holt 	if (ret && force) {
8392bb2b7b5SJohn Ogness 		printk_prefer_direct_enter();
840972ee83dSRobin Holt 		pr_warn("Failed to start orderly shutdown: forcing the issue\n");
8417a54f46bSJoel Stanley 
84215d94b82SRobin Holt 		/*
84315d94b82SRobin Holt 		 * I guess this should try to kick off some daemon to sync and
84415d94b82SRobin Holt 		 * poweroff asap.  Or not even bother syncing if we're doing an
84515d94b82SRobin Holt 		 * emergency shutdown?
84615d94b82SRobin Holt 		 */
84715d94b82SRobin Holt 		emergency_sync();
84815d94b82SRobin Holt 		kernel_power_off();
8492bb2b7b5SJohn Ogness 		printk_prefer_direct_exit();
85015d94b82SRobin Holt 	}
85115d94b82SRobin Holt 
85215d94b82SRobin Holt 	return ret;
85315d94b82SRobin Holt }
85415d94b82SRobin Holt 
85515d94b82SRobin Holt static bool poweroff_force;
85615d94b82SRobin Holt 
85715d94b82SRobin Holt static void poweroff_work_func(struct work_struct *work)
85815d94b82SRobin Holt {
85915d94b82SRobin Holt 	__orderly_poweroff(poweroff_force);
86015d94b82SRobin Holt }
86115d94b82SRobin Holt 
86215d94b82SRobin Holt static DECLARE_WORK(poweroff_work, poweroff_work_func);
86315d94b82SRobin Holt 
86415d94b82SRobin Holt /**
86515d94b82SRobin Holt  * orderly_poweroff - Trigger an orderly system poweroff
86615d94b82SRobin Holt  * @force: force poweroff if command execution fails
86715d94b82SRobin Holt  *
86815d94b82SRobin Holt  * This may be called from any context to trigger a system shutdown.
86915d94b82SRobin Holt  * If the orderly shutdown fails, it will force an immediate shutdown.
87015d94b82SRobin Holt  */
8717a54f46bSJoel Stanley void orderly_poweroff(bool force)
87215d94b82SRobin Holt {
87315d94b82SRobin Holt 	if (force) /* do not override the pending "true" */
87415d94b82SRobin Holt 		poweroff_force = true;
87515d94b82SRobin Holt 	schedule_work(&poweroff_work);
87615d94b82SRobin Holt }
87715d94b82SRobin Holt EXPORT_SYMBOL_GPL(orderly_poweroff);
8781b3a5d02SRobin Holt 
8797a54f46bSJoel Stanley static void reboot_work_func(struct work_struct *work)
8807a54f46bSJoel Stanley {
8817a54f46bSJoel Stanley 	__orderly_reboot();
8827a54f46bSJoel Stanley }
8837a54f46bSJoel Stanley 
8847a54f46bSJoel Stanley static DECLARE_WORK(reboot_work, reboot_work_func);
8857a54f46bSJoel Stanley 
8867a54f46bSJoel Stanley /**
8877a54f46bSJoel Stanley  * orderly_reboot - Trigger an orderly system reboot
8887a54f46bSJoel Stanley  *
8897a54f46bSJoel Stanley  * This may be called from any context to trigger a system reboot.
8907a54f46bSJoel Stanley  * If the orderly reboot fails, it will force an immediate reboot.
8917a54f46bSJoel Stanley  */
8927a54f46bSJoel Stanley void orderly_reboot(void)
8937a54f46bSJoel Stanley {
8947a54f46bSJoel Stanley 	schedule_work(&reboot_work);
8957a54f46bSJoel Stanley }
8967a54f46bSJoel Stanley EXPORT_SYMBOL_GPL(orderly_reboot);
8977a54f46bSJoel Stanley 
898dfa19b11SMatti Vaittinen /**
899dfa19b11SMatti Vaittinen  * hw_failure_emergency_poweroff_func - emergency poweroff work after a known delay
900dfa19b11SMatti Vaittinen  * @work: work_struct associated with the emergency poweroff function
901dfa19b11SMatti Vaittinen  *
902dfa19b11SMatti Vaittinen  * This function is called in very critical situations to force
903dfa19b11SMatti Vaittinen  * a kernel poweroff after a configurable timeout value.
904dfa19b11SMatti Vaittinen  */
905dfa19b11SMatti Vaittinen static void hw_failure_emergency_poweroff_func(struct work_struct *work)
906dfa19b11SMatti Vaittinen {
9072bb2b7b5SJohn Ogness 	printk_prefer_direct_enter();
9082bb2b7b5SJohn Ogness 
909dfa19b11SMatti Vaittinen 	/*
910dfa19b11SMatti Vaittinen 	 * We have reached here after the emergency shutdown waiting period has
911dfa19b11SMatti Vaittinen 	 * expired. This means orderly_poweroff has not been able to shut off
912dfa19b11SMatti Vaittinen 	 * the system for some reason.
913dfa19b11SMatti Vaittinen 	 *
914dfa19b11SMatti Vaittinen 	 * Try to shut down the system immediately using kernel_power_off
915dfa19b11SMatti Vaittinen 	 * if populated
916dfa19b11SMatti Vaittinen 	 */
917dfa19b11SMatti Vaittinen 	pr_emerg("Hardware protection timed-out. Trying forced poweroff\n");
918dfa19b11SMatti Vaittinen 	kernel_power_off();
919dfa19b11SMatti Vaittinen 
920dfa19b11SMatti Vaittinen 	/*
921dfa19b11SMatti Vaittinen 	 * Worst of the worst case trigger emergency restart
922dfa19b11SMatti Vaittinen 	 */
923dfa19b11SMatti Vaittinen 	pr_emerg("Hardware protection shutdown failed. Trying emergency restart\n");
924dfa19b11SMatti Vaittinen 	emergency_restart();
9252bb2b7b5SJohn Ogness 
9262bb2b7b5SJohn Ogness 	printk_prefer_direct_exit();
927dfa19b11SMatti Vaittinen }
928dfa19b11SMatti Vaittinen 
929dfa19b11SMatti Vaittinen static DECLARE_DELAYED_WORK(hw_failure_emergency_poweroff_work,
930dfa19b11SMatti Vaittinen 			    hw_failure_emergency_poweroff_func);
931dfa19b11SMatti Vaittinen 
932dfa19b11SMatti Vaittinen /**
933dfa19b11SMatti Vaittinen  * hw_failure_emergency_poweroff - Trigger an emergency system poweroff
934dfa19b11SMatti Vaittinen  *
935dfa19b11SMatti Vaittinen  * This may be called from any critical situation to trigger a system shutdown
936dfa19b11SMatti Vaittinen  * after a given period of time. If time is negative this is not scheduled.
937dfa19b11SMatti Vaittinen  */
938dfa19b11SMatti Vaittinen static void hw_failure_emergency_poweroff(int poweroff_delay_ms)
939dfa19b11SMatti Vaittinen {
940dfa19b11SMatti Vaittinen 	if (poweroff_delay_ms <= 0)
941dfa19b11SMatti Vaittinen 		return;
942dfa19b11SMatti Vaittinen 	schedule_delayed_work(&hw_failure_emergency_poweroff_work,
943dfa19b11SMatti Vaittinen 			      msecs_to_jiffies(poweroff_delay_ms));
944dfa19b11SMatti Vaittinen }
945dfa19b11SMatti Vaittinen 
946dfa19b11SMatti Vaittinen /**
947dfa19b11SMatti Vaittinen  * hw_protection_shutdown - Trigger an emergency system poweroff
948dfa19b11SMatti Vaittinen  *
949dfa19b11SMatti Vaittinen  * @reason:		Reason of emergency shutdown to be printed.
950dfa19b11SMatti Vaittinen  * @ms_until_forced:	Time to wait for orderly shutdown before tiggering a
951dfa19b11SMatti Vaittinen  *			forced shudown. Negative value disables the forced
952dfa19b11SMatti Vaittinen  *			shutdown.
953dfa19b11SMatti Vaittinen  *
954dfa19b11SMatti Vaittinen  * Initiate an emergency system shutdown in order to protect hardware from
955dfa19b11SMatti Vaittinen  * further damage. Usage examples include a thermal protection or a voltage or
956dfa19b11SMatti Vaittinen  * current regulator failures.
957dfa19b11SMatti Vaittinen  * NOTE: The request is ignored if protection shutdown is already pending even
958dfa19b11SMatti Vaittinen  * if the previous request has given a large timeout for forced shutdown.
959dfa19b11SMatti Vaittinen  * Can be called from any context.
960dfa19b11SMatti Vaittinen  */
961dfa19b11SMatti Vaittinen void hw_protection_shutdown(const char *reason, int ms_until_forced)
962dfa19b11SMatti Vaittinen {
963dfa19b11SMatti Vaittinen 	static atomic_t allow_proceed = ATOMIC_INIT(1);
964dfa19b11SMatti Vaittinen 
9652bb2b7b5SJohn Ogness 	printk_prefer_direct_enter();
9662bb2b7b5SJohn Ogness 
967dfa19b11SMatti Vaittinen 	pr_emerg("HARDWARE PROTECTION shutdown (%s)\n", reason);
968dfa19b11SMatti Vaittinen 
969dfa19b11SMatti Vaittinen 	/* Shutdown should be initiated only once. */
970dfa19b11SMatti Vaittinen 	if (!atomic_dec_and_test(&allow_proceed))
9712bb2b7b5SJohn Ogness 		goto out;
972dfa19b11SMatti Vaittinen 
973dfa19b11SMatti Vaittinen 	/*
974dfa19b11SMatti Vaittinen 	 * Queue a backup emergency shutdown in the event of
975dfa19b11SMatti Vaittinen 	 * orderly_poweroff failure
976dfa19b11SMatti Vaittinen 	 */
977dfa19b11SMatti Vaittinen 	hw_failure_emergency_poweroff(ms_until_forced);
978dfa19b11SMatti Vaittinen 	orderly_poweroff(true);
9792bb2b7b5SJohn Ogness out:
9802bb2b7b5SJohn Ogness 	printk_prefer_direct_exit();
981dfa19b11SMatti Vaittinen }
982dfa19b11SMatti Vaittinen EXPORT_SYMBOL_GPL(hw_protection_shutdown);
983dfa19b11SMatti Vaittinen 
9841b3a5d02SRobin Holt static int __init reboot_setup(char *str)
9851b3a5d02SRobin Holt {
9861b3a5d02SRobin Holt 	for (;;) {
987b287a25aSAaro Koskinen 		enum reboot_mode *mode;
988b287a25aSAaro Koskinen 
9891b3a5d02SRobin Holt 		/*
9901b3a5d02SRobin Holt 		 * Having anything passed on the command line via
9911b3a5d02SRobin Holt 		 * reboot= will cause us to disable DMI checking
9921b3a5d02SRobin Holt 		 * below.
9931b3a5d02SRobin Holt 		 */
9941b3a5d02SRobin Holt 		reboot_default = 0;
9951b3a5d02SRobin Holt 
996b287a25aSAaro Koskinen 		if (!strncmp(str, "panic_", 6)) {
997b287a25aSAaro Koskinen 			mode = &panic_reboot_mode;
998b287a25aSAaro Koskinen 			str += 6;
999b287a25aSAaro Koskinen 		} else {
1000b287a25aSAaro Koskinen 			mode = &reboot_mode;
1001b287a25aSAaro Koskinen 		}
1002b287a25aSAaro Koskinen 
10031b3a5d02SRobin Holt 		switch (*str) {
10041b3a5d02SRobin Holt 		case 'w':
1005b287a25aSAaro Koskinen 			*mode = REBOOT_WARM;
10061b3a5d02SRobin Holt 			break;
10071b3a5d02SRobin Holt 
10081b3a5d02SRobin Holt 		case 'c':
1009b287a25aSAaro Koskinen 			*mode = REBOOT_COLD;
10101b3a5d02SRobin Holt 			break;
10111b3a5d02SRobin Holt 
10121b3a5d02SRobin Holt 		case 'h':
1013b287a25aSAaro Koskinen 			*mode = REBOOT_HARD;
10141b3a5d02SRobin Holt 			break;
10151b3a5d02SRobin Holt 
10161b3a5d02SRobin Holt 		case 's':
1017f9a90501SMatteo Croce 			/*
1018f9a90501SMatteo Croce 			 * reboot_cpu is s[mp]#### with #### being the processor
1019f9a90501SMatteo Croce 			 * to be used for rebooting. Skip 's' or 'smp' prefix.
1020f9a90501SMatteo Croce 			 */
1021f9a90501SMatteo Croce 			str += str[1] == 'm' && str[2] == 'p' ? 3 : 1;
1022f9a90501SMatteo Croce 
1023f9a90501SMatteo Croce 			if (isdigit(str[0])) {
1024f9a90501SMatteo Croce 				int cpu = simple_strtoul(str, NULL, 0);
1025f9a90501SMatteo Croce 
1026f9a90501SMatteo Croce 				if (cpu >= num_possible_cpus()) {
1027df5b0ab3SMatteo Croce 					pr_err("Ignoring the CPU number in reboot= option. "
1028df5b0ab3SMatteo Croce 					"CPU %d exceeds possible cpu number %d\n",
1029f9a90501SMatteo Croce 					cpu, num_possible_cpus());
1030df5b0ab3SMatteo Croce 					break;
1031df5b0ab3SMatteo Croce 				}
1032f9a90501SMatteo Croce 				reboot_cpu = cpu;
1033f9a90501SMatteo Croce 			} else
1034f9a90501SMatteo Croce 				*mode = REBOOT_SOFT;
10351b3a5d02SRobin Holt 			break;
10368b92c4ffSMatteo Croce 
10371b3a5d02SRobin Holt 		case 'g':
1038b287a25aSAaro Koskinen 			*mode = REBOOT_GPIO;
10391b3a5d02SRobin Holt 			break;
10401b3a5d02SRobin Holt 
10411b3a5d02SRobin Holt 		case 'b':
10421b3a5d02SRobin Holt 		case 'a':
10431b3a5d02SRobin Holt 		case 'k':
10441b3a5d02SRobin Holt 		case 't':
10451b3a5d02SRobin Holt 		case 'e':
10461b3a5d02SRobin Holt 		case 'p':
10471b3a5d02SRobin Holt 			reboot_type = *str;
10481b3a5d02SRobin Holt 			break;
10491b3a5d02SRobin Holt 
10501b3a5d02SRobin Holt 		case 'f':
10511b3a5d02SRobin Holt 			reboot_force = 1;
10521b3a5d02SRobin Holt 			break;
10531b3a5d02SRobin Holt 		}
10541b3a5d02SRobin Holt 
10551b3a5d02SRobin Holt 		str = strchr(str, ',');
10561b3a5d02SRobin Holt 		if (str)
10571b3a5d02SRobin Holt 			str++;
10581b3a5d02SRobin Holt 		else
10591b3a5d02SRobin Holt 			break;
10601b3a5d02SRobin Holt 	}
10611b3a5d02SRobin Holt 	return 1;
10621b3a5d02SRobin Holt }
10631b3a5d02SRobin Holt __setup("reboot=", reboot_setup);
10642c622ed0SMatteo Croce 
10652c622ed0SMatteo Croce #ifdef CONFIG_SYSFS
10662c622ed0SMatteo Croce 
10672c622ed0SMatteo Croce #define REBOOT_COLD_STR		"cold"
10682c622ed0SMatteo Croce #define REBOOT_WARM_STR		"warm"
10692c622ed0SMatteo Croce #define REBOOT_HARD_STR		"hard"
10702c622ed0SMatteo Croce #define REBOOT_SOFT_STR		"soft"
10712c622ed0SMatteo Croce #define REBOOT_GPIO_STR		"gpio"
10722c622ed0SMatteo Croce #define REBOOT_UNDEFINED_STR	"undefined"
10732c622ed0SMatteo Croce 
10742c622ed0SMatteo Croce #define BOOT_TRIPLE_STR		"triple"
10752c622ed0SMatteo Croce #define BOOT_KBD_STR		"kbd"
10762c622ed0SMatteo Croce #define BOOT_BIOS_STR		"bios"
10772c622ed0SMatteo Croce #define BOOT_ACPI_STR		"acpi"
10782c622ed0SMatteo Croce #define BOOT_EFI_STR		"efi"
10790c5c0179SMatteo Croce #define BOOT_PCI_STR		"pci"
10802c622ed0SMatteo Croce 
10812c622ed0SMatteo Croce static ssize_t mode_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
10822c622ed0SMatteo Croce {
10832c622ed0SMatteo Croce 	const char *val;
10842c622ed0SMatteo Croce 
10852c622ed0SMatteo Croce 	switch (reboot_mode) {
10862c622ed0SMatteo Croce 	case REBOOT_COLD:
10872c622ed0SMatteo Croce 		val = REBOOT_COLD_STR;
10882c622ed0SMatteo Croce 		break;
10892c622ed0SMatteo Croce 	case REBOOT_WARM:
10902c622ed0SMatteo Croce 		val = REBOOT_WARM_STR;
10912c622ed0SMatteo Croce 		break;
10922c622ed0SMatteo Croce 	case REBOOT_HARD:
10932c622ed0SMatteo Croce 		val = REBOOT_HARD_STR;
10942c622ed0SMatteo Croce 		break;
10952c622ed0SMatteo Croce 	case REBOOT_SOFT:
10962c622ed0SMatteo Croce 		val = REBOOT_SOFT_STR;
10972c622ed0SMatteo Croce 		break;
10982c622ed0SMatteo Croce 	case REBOOT_GPIO:
10992c622ed0SMatteo Croce 		val = REBOOT_GPIO_STR;
11002c622ed0SMatteo Croce 		break;
11012c622ed0SMatteo Croce 	default:
11022c622ed0SMatteo Croce 		val = REBOOT_UNDEFINED_STR;
11032c622ed0SMatteo Croce 	}
11042c622ed0SMatteo Croce 
11052c622ed0SMatteo Croce 	return sprintf(buf, "%s\n", val);
11062c622ed0SMatteo Croce }
11072c622ed0SMatteo Croce static ssize_t mode_store(struct kobject *kobj, struct kobj_attribute *attr,
11082c622ed0SMatteo Croce 			  const char *buf, size_t count)
11092c622ed0SMatteo Croce {
11102c622ed0SMatteo Croce 	if (!capable(CAP_SYS_BOOT))
11112c622ed0SMatteo Croce 		return -EPERM;
11122c622ed0SMatteo Croce 
11132c622ed0SMatteo Croce 	if (!strncmp(buf, REBOOT_COLD_STR, strlen(REBOOT_COLD_STR)))
11142c622ed0SMatteo Croce 		reboot_mode = REBOOT_COLD;
11152c622ed0SMatteo Croce 	else if (!strncmp(buf, REBOOT_WARM_STR, strlen(REBOOT_WARM_STR)))
11162c622ed0SMatteo Croce 		reboot_mode = REBOOT_WARM;
11172c622ed0SMatteo Croce 	else if (!strncmp(buf, REBOOT_HARD_STR, strlen(REBOOT_HARD_STR)))
11182c622ed0SMatteo Croce 		reboot_mode = REBOOT_HARD;
11192c622ed0SMatteo Croce 	else if (!strncmp(buf, REBOOT_SOFT_STR, strlen(REBOOT_SOFT_STR)))
11202c622ed0SMatteo Croce 		reboot_mode = REBOOT_SOFT;
11212c622ed0SMatteo Croce 	else if (!strncmp(buf, REBOOT_GPIO_STR, strlen(REBOOT_GPIO_STR)))
11222c622ed0SMatteo Croce 		reboot_mode = REBOOT_GPIO;
11232c622ed0SMatteo Croce 	else
11242c622ed0SMatteo Croce 		return -EINVAL;
11252c622ed0SMatteo Croce 
11261a9d079fSMatteo Croce 	reboot_default = 0;
11271a9d079fSMatteo Croce 
11282c622ed0SMatteo Croce 	return count;
11292c622ed0SMatteo Croce }
11302c622ed0SMatteo Croce static struct kobj_attribute reboot_mode_attr = __ATTR_RW(mode);
11312c622ed0SMatteo Croce 
113240247e55SMatteo Croce #ifdef CONFIG_X86
113340247e55SMatteo Croce static ssize_t force_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
113440247e55SMatteo Croce {
113540247e55SMatteo Croce 	return sprintf(buf, "%d\n", reboot_force);
113640247e55SMatteo Croce }
113740247e55SMatteo Croce static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr,
113840247e55SMatteo Croce 			  const char *buf, size_t count)
113940247e55SMatteo Croce {
114040247e55SMatteo Croce 	bool res;
114140247e55SMatteo Croce 
114240247e55SMatteo Croce 	if (!capable(CAP_SYS_BOOT))
114340247e55SMatteo Croce 		return -EPERM;
114440247e55SMatteo Croce 
114540247e55SMatteo Croce 	if (kstrtobool(buf, &res))
114640247e55SMatteo Croce 		return -EINVAL;
114740247e55SMatteo Croce 
114840247e55SMatteo Croce 	reboot_default = 0;
114940247e55SMatteo Croce 	reboot_force = res;
115040247e55SMatteo Croce 
115140247e55SMatteo Croce 	return count;
115240247e55SMatteo Croce }
115340247e55SMatteo Croce static struct kobj_attribute reboot_force_attr = __ATTR_RW(force);
115440247e55SMatteo Croce 
11552c622ed0SMatteo Croce static ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
11562c622ed0SMatteo Croce {
11572c622ed0SMatteo Croce 	const char *val;
11582c622ed0SMatteo Croce 
11592c622ed0SMatteo Croce 	switch (reboot_type) {
11602c622ed0SMatteo Croce 	case BOOT_TRIPLE:
11612c622ed0SMatteo Croce 		val = BOOT_TRIPLE_STR;
11622c622ed0SMatteo Croce 		break;
11632c622ed0SMatteo Croce 	case BOOT_KBD:
11642c622ed0SMatteo Croce 		val = BOOT_KBD_STR;
11652c622ed0SMatteo Croce 		break;
11662c622ed0SMatteo Croce 	case BOOT_BIOS:
11672c622ed0SMatteo Croce 		val = BOOT_BIOS_STR;
11682c622ed0SMatteo Croce 		break;
11692c622ed0SMatteo Croce 	case BOOT_ACPI:
11702c622ed0SMatteo Croce 		val = BOOT_ACPI_STR;
11712c622ed0SMatteo Croce 		break;
11722c622ed0SMatteo Croce 	case BOOT_EFI:
11732c622ed0SMatteo Croce 		val = BOOT_EFI_STR;
11742c622ed0SMatteo Croce 		break;
11752c622ed0SMatteo Croce 	case BOOT_CF9_FORCE:
11760c5c0179SMatteo Croce 		val = BOOT_PCI_STR;
11772c622ed0SMatteo Croce 		break;
11782c622ed0SMatteo Croce 	default:
11792c622ed0SMatteo Croce 		val = REBOOT_UNDEFINED_STR;
11802c622ed0SMatteo Croce 	}
11812c622ed0SMatteo Croce 
11822c622ed0SMatteo Croce 	return sprintf(buf, "%s\n", val);
11832c622ed0SMatteo Croce }
11842c622ed0SMatteo Croce static ssize_t type_store(struct kobject *kobj, struct kobj_attribute *attr,
11852c622ed0SMatteo Croce 			  const char *buf, size_t count)
11862c622ed0SMatteo Croce {
11872c622ed0SMatteo Croce 	if (!capable(CAP_SYS_BOOT))
11882c622ed0SMatteo Croce 		return -EPERM;
11892c622ed0SMatteo Croce 
11902c622ed0SMatteo Croce 	if (!strncmp(buf, BOOT_TRIPLE_STR, strlen(BOOT_TRIPLE_STR)))
11912c622ed0SMatteo Croce 		reboot_type = BOOT_TRIPLE;
11922c622ed0SMatteo Croce 	else if (!strncmp(buf, BOOT_KBD_STR, strlen(BOOT_KBD_STR)))
11932c622ed0SMatteo Croce 		reboot_type = BOOT_KBD;
11942c622ed0SMatteo Croce 	else if (!strncmp(buf, BOOT_BIOS_STR, strlen(BOOT_BIOS_STR)))
11952c622ed0SMatteo Croce 		reboot_type = BOOT_BIOS;
11962c622ed0SMatteo Croce 	else if (!strncmp(buf, BOOT_ACPI_STR, strlen(BOOT_ACPI_STR)))
11972c622ed0SMatteo Croce 		reboot_type = BOOT_ACPI;
11982c622ed0SMatteo Croce 	else if (!strncmp(buf, BOOT_EFI_STR, strlen(BOOT_EFI_STR)))
11992c622ed0SMatteo Croce 		reboot_type = BOOT_EFI;
12000c5c0179SMatteo Croce 	else if (!strncmp(buf, BOOT_PCI_STR, strlen(BOOT_PCI_STR)))
12012c622ed0SMatteo Croce 		reboot_type = BOOT_CF9_FORCE;
12022c622ed0SMatteo Croce 	else
12032c622ed0SMatteo Croce 		return -EINVAL;
12042c622ed0SMatteo Croce 
12051a9d079fSMatteo Croce 	reboot_default = 0;
12061a9d079fSMatteo Croce 
12072c622ed0SMatteo Croce 	return count;
12082c622ed0SMatteo Croce }
12092c622ed0SMatteo Croce static struct kobj_attribute reboot_type_attr = __ATTR_RW(type);
121040247e55SMatteo Croce #endif
12112c622ed0SMatteo Croce 
121240247e55SMatteo Croce #ifdef CONFIG_SMP
12132c622ed0SMatteo Croce static ssize_t cpu_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
12142c622ed0SMatteo Croce {
12152c622ed0SMatteo Croce 	return sprintf(buf, "%d\n", reboot_cpu);
12162c622ed0SMatteo Croce }
12172c622ed0SMatteo Croce static ssize_t cpu_store(struct kobject *kobj, struct kobj_attribute *attr,
12182c622ed0SMatteo Croce 			  const char *buf, size_t count)
12192c622ed0SMatteo Croce {
12202c622ed0SMatteo Croce 	unsigned int cpunum;
12212c622ed0SMatteo Croce 	int rc;
12222c622ed0SMatteo Croce 
12232c622ed0SMatteo Croce 	if (!capable(CAP_SYS_BOOT))
12242c622ed0SMatteo Croce 		return -EPERM;
12252c622ed0SMatteo Croce 
12262c622ed0SMatteo Croce 	rc = kstrtouint(buf, 0, &cpunum);
12272c622ed0SMatteo Croce 
12282c622ed0SMatteo Croce 	if (rc)
12292c622ed0SMatteo Croce 		return rc;
12302c622ed0SMatteo Croce 
12312c622ed0SMatteo Croce 	if (cpunum >= num_possible_cpus())
12322c622ed0SMatteo Croce 		return -ERANGE;
12332c622ed0SMatteo Croce 
12341a9d079fSMatteo Croce 	reboot_default = 0;
12352c622ed0SMatteo Croce 	reboot_cpu = cpunum;
12362c622ed0SMatteo Croce 
12372c622ed0SMatteo Croce 	return count;
12382c622ed0SMatteo Croce }
12392c622ed0SMatteo Croce static struct kobj_attribute reboot_cpu_attr = __ATTR_RW(cpu);
124040247e55SMatteo Croce #endif
12412c622ed0SMatteo Croce 
12422c622ed0SMatteo Croce static struct attribute *reboot_attrs[] = {
12432c622ed0SMatteo Croce 	&reboot_mode_attr.attr,
124440247e55SMatteo Croce #ifdef CONFIG_X86
12452c622ed0SMatteo Croce 	&reboot_force_attr.attr,
124640247e55SMatteo Croce 	&reboot_type_attr.attr,
124740247e55SMatteo Croce #endif
124840247e55SMatteo Croce #ifdef CONFIG_SMP
124940247e55SMatteo Croce 	&reboot_cpu_attr.attr,
125040247e55SMatteo Croce #endif
12512c622ed0SMatteo Croce 	NULL,
12522c622ed0SMatteo Croce };
12532c622ed0SMatteo Croce 
1254764aaf44SYueHaibing #ifdef CONFIG_SYSCTL
1255764aaf44SYueHaibing static struct ctl_table kern_reboot_table[] = {
1256764aaf44SYueHaibing 	{
1257764aaf44SYueHaibing 		.procname       = "poweroff_cmd",
1258764aaf44SYueHaibing 		.data           = &poweroff_cmd,
1259764aaf44SYueHaibing 		.maxlen         = POWEROFF_CMD_PATH_LEN,
1260764aaf44SYueHaibing 		.mode           = 0644,
1261764aaf44SYueHaibing 		.proc_handler   = proc_dostring,
1262764aaf44SYueHaibing 	},
1263764aaf44SYueHaibing 	{
1264764aaf44SYueHaibing 		.procname       = "ctrl-alt-del",
1265764aaf44SYueHaibing 		.data           = &C_A_D,
1266764aaf44SYueHaibing 		.maxlen         = sizeof(int),
1267764aaf44SYueHaibing 		.mode           = 0644,
1268764aaf44SYueHaibing 		.proc_handler   = proc_dointvec,
1269764aaf44SYueHaibing 	},
1270764aaf44SYueHaibing 	{ }
1271764aaf44SYueHaibing };
1272764aaf44SYueHaibing 
1273764aaf44SYueHaibing static void __init kernel_reboot_sysctls_init(void)
1274764aaf44SYueHaibing {
1275764aaf44SYueHaibing 	register_sysctl_init("kernel", kern_reboot_table);
1276764aaf44SYueHaibing }
1277764aaf44SYueHaibing #else
1278764aaf44SYueHaibing #define kernel_reboot_sysctls_init() do { } while (0)
1279764aaf44SYueHaibing #endif /* CONFIG_SYSCTL */
1280764aaf44SYueHaibing 
12812c622ed0SMatteo Croce static const struct attribute_group reboot_attr_group = {
12822c622ed0SMatteo Croce 	.attrs = reboot_attrs,
12832c622ed0SMatteo Croce };
12842c622ed0SMatteo Croce 
12852c622ed0SMatteo Croce static int __init reboot_ksysfs_init(void)
12862c622ed0SMatteo Croce {
12872c622ed0SMatteo Croce 	struct kobject *reboot_kobj;
12882c622ed0SMatteo Croce 	int ret;
12892c622ed0SMatteo Croce 
12902c622ed0SMatteo Croce 	reboot_kobj = kobject_create_and_add("reboot", kernel_kobj);
12912c622ed0SMatteo Croce 	if (!reboot_kobj)
12922c622ed0SMatteo Croce 		return -ENOMEM;
12932c622ed0SMatteo Croce 
12942c622ed0SMatteo Croce 	ret = sysfs_create_group(reboot_kobj, &reboot_attr_group);
12952c622ed0SMatteo Croce 	if (ret) {
12962c622ed0SMatteo Croce 		kobject_put(reboot_kobj);
12972c622ed0SMatteo Croce 		return ret;
12982c622ed0SMatteo Croce 	}
12992c622ed0SMatteo Croce 
130006d17766Stangmeng 	kernel_reboot_sysctls_init();
130106d17766Stangmeng 
13022c622ed0SMatteo Croce 	return 0;
13032c622ed0SMatteo Croce }
13042c622ed0SMatteo Croce late_initcall(reboot_ksysfs_init);
13052c622ed0SMatteo Croce 
13062c622ed0SMatteo Croce #endif
1307