xref: /openbmc/linux/kernel/reboot.c (revision 764aaf44cd64dd1f760268ee0b22d2dc53cd5bc0)
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 
5115d94b82SRobin Holt /*
5215d94b82SRobin Holt  * If set, this is used for preparing the system to power off.
5315d94b82SRobin Holt  */
5415d94b82SRobin Holt 
5515d94b82SRobin Holt void (*pm_power_off_prepare)(void);
5674f008f2SOleksij Rempel EXPORT_SYMBOL_GPL(pm_power_off_prepare);
5715d94b82SRobin Holt 
5815d94b82SRobin Holt /**
5915d94b82SRobin Holt  *	emergency_restart - reboot the system
6015d94b82SRobin Holt  *
6115d94b82SRobin Holt  *	Without shutting down any hardware or taking any locks
6215d94b82SRobin Holt  *	reboot the system.  This is called when we know we are in
6315d94b82SRobin Holt  *	trouble so this is our best effort to reboot.  This is
6415d94b82SRobin Holt  *	safe to call in interrupt context.
6515d94b82SRobin Holt  */
6615d94b82SRobin Holt void emergency_restart(void)
6715d94b82SRobin Holt {
6815d94b82SRobin Holt 	kmsg_dump(KMSG_DUMP_EMERG);
6915d94b82SRobin Holt 	machine_emergency_restart();
7015d94b82SRobin Holt }
7115d94b82SRobin Holt EXPORT_SYMBOL_GPL(emergency_restart);
7215d94b82SRobin Holt 
7315d94b82SRobin Holt void kernel_restart_prepare(char *cmd)
7415d94b82SRobin Holt {
7515d94b82SRobin Holt 	blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
7615d94b82SRobin Holt 	system_state = SYSTEM_RESTART;
7715d94b82SRobin Holt 	usermodehelper_disable();
7815d94b82SRobin Holt 	device_shutdown();
7915d94b82SRobin Holt }
8015d94b82SRobin Holt 
8115d94b82SRobin Holt /**
8215d94b82SRobin Holt  *	register_reboot_notifier - Register function to be called at reboot time
8315d94b82SRobin Holt  *	@nb: Info about notifier function to be called
8415d94b82SRobin Holt  *
8515d94b82SRobin Holt  *	Registers a function with the list of functions
8615d94b82SRobin Holt  *	to be called at reboot time.
8715d94b82SRobin Holt  *
8815d94b82SRobin Holt  *	Currently always returns zero, as blocking_notifier_chain_register()
8915d94b82SRobin Holt  *	always returns zero.
9015d94b82SRobin Holt  */
9115d94b82SRobin Holt int register_reboot_notifier(struct notifier_block *nb)
9215d94b82SRobin Holt {
9315d94b82SRobin Holt 	return blocking_notifier_chain_register(&reboot_notifier_list, nb);
9415d94b82SRobin Holt }
9515d94b82SRobin Holt EXPORT_SYMBOL(register_reboot_notifier);
9615d94b82SRobin Holt 
9715d94b82SRobin Holt /**
9815d94b82SRobin Holt  *	unregister_reboot_notifier - Unregister previously registered reboot notifier
9915d94b82SRobin Holt  *	@nb: Hook to be unregistered
10015d94b82SRobin Holt  *
10115d94b82SRobin Holt  *	Unregisters a previously registered reboot
10215d94b82SRobin Holt  *	notifier function.
10315d94b82SRobin Holt  *
10415d94b82SRobin Holt  *	Returns zero on success, or %-ENOENT on failure.
10515d94b82SRobin Holt  */
10615d94b82SRobin Holt int unregister_reboot_notifier(struct notifier_block *nb)
10715d94b82SRobin Holt {
10815d94b82SRobin Holt 	return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
10915d94b82SRobin Holt }
11015d94b82SRobin Holt EXPORT_SYMBOL(unregister_reboot_notifier);
11115d94b82SRobin Holt 
1122d8364baSAndrey Smirnov static void devm_unregister_reboot_notifier(struct device *dev, void *res)
1132d8364baSAndrey Smirnov {
1142d8364baSAndrey Smirnov 	WARN_ON(unregister_reboot_notifier(*(struct notifier_block **)res));
1152d8364baSAndrey Smirnov }
1162d8364baSAndrey Smirnov 
1172d8364baSAndrey Smirnov int devm_register_reboot_notifier(struct device *dev, struct notifier_block *nb)
1182d8364baSAndrey Smirnov {
1192d8364baSAndrey Smirnov 	struct notifier_block **rcnb;
1202d8364baSAndrey Smirnov 	int ret;
1212d8364baSAndrey Smirnov 
1222d8364baSAndrey Smirnov 	rcnb = devres_alloc(devm_unregister_reboot_notifier,
1232d8364baSAndrey Smirnov 			    sizeof(*rcnb), GFP_KERNEL);
1242d8364baSAndrey Smirnov 	if (!rcnb)
1252d8364baSAndrey Smirnov 		return -ENOMEM;
1262d8364baSAndrey Smirnov 
1272d8364baSAndrey Smirnov 	ret = register_reboot_notifier(nb);
1282d8364baSAndrey Smirnov 	if (!ret) {
1292d8364baSAndrey Smirnov 		*rcnb = nb;
1302d8364baSAndrey Smirnov 		devres_add(dev, rcnb);
1312d8364baSAndrey Smirnov 	} else {
1322d8364baSAndrey Smirnov 		devres_free(rcnb);
1332d8364baSAndrey Smirnov 	}
1342d8364baSAndrey Smirnov 
1352d8364baSAndrey Smirnov 	return ret;
1362d8364baSAndrey Smirnov }
1372d8364baSAndrey Smirnov EXPORT_SYMBOL(devm_register_reboot_notifier);
1382d8364baSAndrey Smirnov 
139b63adb97SGuenter Roeck /*
140b63adb97SGuenter Roeck  *	Notifier list for kernel code which wants to be called
141b63adb97SGuenter Roeck  *	to restart the system.
142b63adb97SGuenter Roeck  */
143b63adb97SGuenter Roeck static ATOMIC_NOTIFIER_HEAD(restart_handler_list);
144b63adb97SGuenter Roeck 
145b63adb97SGuenter Roeck /**
146b63adb97SGuenter Roeck  *	register_restart_handler - Register function to be called to reset
147b63adb97SGuenter Roeck  *				   the system
148b63adb97SGuenter Roeck  *	@nb: Info about handler function to be called
149b63adb97SGuenter Roeck  *	@nb->priority:	Handler priority. Handlers should follow the
150b63adb97SGuenter Roeck  *			following guidelines for setting priorities.
151b63adb97SGuenter Roeck  *			0:	Restart handler of last resort,
152b63adb97SGuenter Roeck  *				with limited restart capabilities
153b63adb97SGuenter Roeck  *			128:	Default restart handler; use if no other
154b63adb97SGuenter Roeck  *				restart handler is expected to be available,
155b63adb97SGuenter Roeck  *				and/or if restart functionality is
156b63adb97SGuenter Roeck  *				sufficient to restart the entire system
157b63adb97SGuenter Roeck  *			255:	Highest priority restart handler, will
158b63adb97SGuenter Roeck  *				preempt all other restart handlers
159b63adb97SGuenter Roeck  *
160b63adb97SGuenter Roeck  *	Registers a function with code to be called to restart the
161b63adb97SGuenter Roeck  *	system.
162b63adb97SGuenter Roeck  *
163b63adb97SGuenter Roeck  *	Registered functions will be called from machine_restart as last
164b63adb97SGuenter Roeck  *	step of the restart sequence (if the architecture specific
165b63adb97SGuenter Roeck  *	machine_restart function calls do_kernel_restart - see below
166b63adb97SGuenter Roeck  *	for details).
167b63adb97SGuenter Roeck  *	Registered functions are expected to restart the system immediately.
168b63adb97SGuenter Roeck  *	If more than one function is registered, the restart handler priority
169b63adb97SGuenter Roeck  *	selects which function will be called first.
170b63adb97SGuenter Roeck  *
171b63adb97SGuenter Roeck  *	Restart handlers are expected to be registered from non-architecture
172b63adb97SGuenter Roeck  *	code, typically from drivers. A typical use case would be a system
173b63adb97SGuenter Roeck  *	where restart functionality is provided through a watchdog. Multiple
174b63adb97SGuenter Roeck  *	restart handlers may exist; for example, one restart handler might
175b63adb97SGuenter Roeck  *	restart the entire system, while another only restarts the CPU.
176b63adb97SGuenter Roeck  *	In such cases, the restart handler which only restarts part of the
177b63adb97SGuenter Roeck  *	hardware is expected to register with low priority to ensure that
178b63adb97SGuenter Roeck  *	it only runs if no other means to restart the system is available.
179b63adb97SGuenter Roeck  *
180b63adb97SGuenter Roeck  *	Currently always returns zero, as atomic_notifier_chain_register()
181b63adb97SGuenter Roeck  *	always returns zero.
182b63adb97SGuenter Roeck  */
183b63adb97SGuenter Roeck int register_restart_handler(struct notifier_block *nb)
184b63adb97SGuenter Roeck {
185b63adb97SGuenter Roeck 	return atomic_notifier_chain_register(&restart_handler_list, nb);
186b63adb97SGuenter Roeck }
187b63adb97SGuenter Roeck EXPORT_SYMBOL(register_restart_handler);
188b63adb97SGuenter Roeck 
189b63adb97SGuenter Roeck /**
190b63adb97SGuenter Roeck  *	unregister_restart_handler - Unregister previously registered
191b63adb97SGuenter Roeck  *				     restart handler
192b63adb97SGuenter Roeck  *	@nb: Hook to be unregistered
193b63adb97SGuenter Roeck  *
194b63adb97SGuenter Roeck  *	Unregisters a previously registered restart handler function.
195b63adb97SGuenter Roeck  *
196b63adb97SGuenter Roeck  *	Returns zero on success, or %-ENOENT on failure.
197b63adb97SGuenter Roeck  */
198b63adb97SGuenter Roeck int unregister_restart_handler(struct notifier_block *nb)
199b63adb97SGuenter Roeck {
200b63adb97SGuenter Roeck 	return atomic_notifier_chain_unregister(&restart_handler_list, nb);
201b63adb97SGuenter Roeck }
202b63adb97SGuenter Roeck EXPORT_SYMBOL(unregister_restart_handler);
203b63adb97SGuenter Roeck 
204b63adb97SGuenter Roeck /**
205b63adb97SGuenter Roeck  *	do_kernel_restart - Execute kernel restart handler call chain
206b63adb97SGuenter Roeck  *
207b63adb97SGuenter Roeck  *	Calls functions registered with register_restart_handler.
208b63adb97SGuenter Roeck  *
209b63adb97SGuenter Roeck  *	Expected to be called from machine_restart as last step of the restart
210b63adb97SGuenter Roeck  *	sequence.
211b63adb97SGuenter Roeck  *
212b63adb97SGuenter Roeck  *	Restarts the system immediately if a restart handler function has been
213b63adb97SGuenter Roeck  *	registered. Otherwise does nothing.
214b63adb97SGuenter Roeck  */
215b63adb97SGuenter Roeck void do_kernel_restart(char *cmd)
216b63adb97SGuenter Roeck {
217b63adb97SGuenter Roeck 	atomic_notifier_call_chain(&restart_handler_list, reboot_mode, cmd);
218b63adb97SGuenter Roeck }
219b63adb97SGuenter Roeck 
220c97102baSVivek Goyal void migrate_to_reboot_cpu(void)
22115d94b82SRobin Holt {
22215d94b82SRobin Holt 	/* The boot cpu is always logical cpu 0 */
2231b3a5d02SRobin Holt 	int cpu = reboot_cpu;
22415d94b82SRobin Holt 
22515d94b82SRobin Holt 	cpu_hotplug_disable();
22615d94b82SRobin Holt 
22715d94b82SRobin Holt 	/* Make certain the cpu I'm about to reboot on is online */
22815d94b82SRobin Holt 	if (!cpu_online(cpu))
22915d94b82SRobin Holt 		cpu = cpumask_first(cpu_online_mask);
23015d94b82SRobin Holt 
23115d94b82SRobin Holt 	/* Prevent races with other tasks migrating this task */
23215d94b82SRobin Holt 	current->flags |= PF_NO_SETAFFINITY;
23315d94b82SRobin Holt 
23415d94b82SRobin Holt 	/* Make certain I only run on the appropriate processor */
23515d94b82SRobin Holt 	set_cpus_allowed_ptr(current, cpumask_of(cpu));
23615d94b82SRobin Holt }
23715d94b82SRobin Holt 
23815d94b82SRobin Holt /**
23915d94b82SRobin Holt  *	kernel_restart - reboot the system
24015d94b82SRobin Holt  *	@cmd: pointer to buffer containing command to execute for restart
24115d94b82SRobin Holt  *		or %NULL
24215d94b82SRobin Holt  *
24315d94b82SRobin Holt  *	Shutdown everything and perform a clean reboot.
24415d94b82SRobin Holt  *	This is not safe to call in interrupt context.
24515d94b82SRobin Holt  */
24615d94b82SRobin Holt void kernel_restart(char *cmd)
24715d94b82SRobin Holt {
24815d94b82SRobin Holt 	kernel_restart_prepare(cmd);
24915d94b82SRobin Holt 	migrate_to_reboot_cpu();
25015d94b82SRobin Holt 	syscore_shutdown();
25115d94b82SRobin Holt 	if (!cmd)
252972ee83dSRobin Holt 		pr_emerg("Restarting system\n");
25315d94b82SRobin Holt 	else
254972ee83dSRobin Holt 		pr_emerg("Restarting system with command '%s'\n", cmd);
2556d3cf962SKees Cook 	kmsg_dump(KMSG_DUMP_SHUTDOWN);
25615d94b82SRobin Holt 	machine_restart(cmd);
25715d94b82SRobin Holt }
25815d94b82SRobin Holt EXPORT_SYMBOL_GPL(kernel_restart);
25915d94b82SRobin Holt 
26015d94b82SRobin Holt static void kernel_shutdown_prepare(enum system_states state)
26115d94b82SRobin Holt {
26215d94b82SRobin Holt 	blocking_notifier_call_chain(&reboot_notifier_list,
26315d94b82SRobin Holt 		(state == SYSTEM_HALT) ? SYS_HALT : SYS_POWER_OFF, NULL);
26415d94b82SRobin Holt 	system_state = state;
26515d94b82SRobin Holt 	usermodehelper_disable();
26615d94b82SRobin Holt 	device_shutdown();
26715d94b82SRobin Holt }
26815d94b82SRobin Holt /**
26915d94b82SRobin Holt  *	kernel_halt - halt the system
27015d94b82SRobin Holt  *
27115d94b82SRobin Holt  *	Shutdown everything and perform a clean system halt.
27215d94b82SRobin Holt  */
27315d94b82SRobin Holt void kernel_halt(void)
27415d94b82SRobin Holt {
27515d94b82SRobin Holt 	kernel_shutdown_prepare(SYSTEM_HALT);
27615d94b82SRobin Holt 	migrate_to_reboot_cpu();
27715d94b82SRobin Holt 	syscore_shutdown();
278972ee83dSRobin Holt 	pr_emerg("System halted\n");
2796d3cf962SKees Cook 	kmsg_dump(KMSG_DUMP_SHUTDOWN);
28015d94b82SRobin Holt 	machine_halt();
28115d94b82SRobin Holt }
28215d94b82SRobin Holt EXPORT_SYMBOL_GPL(kernel_halt);
28315d94b82SRobin Holt 
28415d94b82SRobin Holt /**
28515d94b82SRobin Holt  *	kernel_power_off - power_off the system
28615d94b82SRobin Holt  *
28715d94b82SRobin Holt  *	Shutdown everything and perform a clean system power_off.
28815d94b82SRobin Holt  */
28915d94b82SRobin Holt void kernel_power_off(void)
29015d94b82SRobin Holt {
29115d94b82SRobin Holt 	kernel_shutdown_prepare(SYSTEM_POWER_OFF);
29215d94b82SRobin Holt 	if (pm_power_off_prepare)
29315d94b82SRobin Holt 		pm_power_off_prepare();
29415d94b82SRobin Holt 	migrate_to_reboot_cpu();
29515d94b82SRobin Holt 	syscore_shutdown();
296972ee83dSRobin Holt 	pr_emerg("Power down\n");
2976d3cf962SKees Cook 	kmsg_dump(KMSG_DUMP_SHUTDOWN);
29815d94b82SRobin Holt 	machine_power_off();
29915d94b82SRobin Holt }
30015d94b82SRobin Holt EXPORT_SYMBOL_GPL(kernel_power_off);
30115d94b82SRobin Holt 
30255f2503cSPingfan Liu DEFINE_MUTEX(system_transition_mutex);
30315d94b82SRobin Holt 
30415d94b82SRobin Holt /*
30515d94b82SRobin Holt  * Reboot system call: for obvious reasons only root may call it,
30615d94b82SRobin Holt  * and even root needs to set up some magic numbers in the registers
30715d94b82SRobin Holt  * so that some mistake won't make this reboot the whole machine.
30815d94b82SRobin Holt  * You can also set the meaning of the ctrl-alt-del-key here.
30915d94b82SRobin Holt  *
31015d94b82SRobin Holt  * reboot doesn't sync: do that yourself before calling this.
31115d94b82SRobin Holt  */
31215d94b82SRobin Holt SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
31315d94b82SRobin Holt 		void __user *, arg)
31415d94b82SRobin Holt {
31515d94b82SRobin Holt 	struct pid_namespace *pid_ns = task_active_pid_ns(current);
31615d94b82SRobin Holt 	char buffer[256];
31715d94b82SRobin Holt 	int ret = 0;
31815d94b82SRobin Holt 
31915d94b82SRobin Holt 	/* We only trust the superuser with rebooting the system. */
32015d94b82SRobin Holt 	if (!ns_capable(pid_ns->user_ns, CAP_SYS_BOOT))
32115d94b82SRobin Holt 		return -EPERM;
32215d94b82SRobin Holt 
32315d94b82SRobin Holt 	/* For safety, we require "magic" arguments. */
32415d94b82SRobin Holt 	if (magic1 != LINUX_REBOOT_MAGIC1 ||
32515d94b82SRobin Holt 			(magic2 != LINUX_REBOOT_MAGIC2 &&
32615d94b82SRobin Holt 			magic2 != LINUX_REBOOT_MAGIC2A &&
32715d94b82SRobin Holt 			magic2 != LINUX_REBOOT_MAGIC2B &&
32815d94b82SRobin Holt 			magic2 != LINUX_REBOOT_MAGIC2C))
32915d94b82SRobin Holt 		return -EINVAL;
33015d94b82SRobin Holt 
33115d94b82SRobin Holt 	/*
33215d94b82SRobin Holt 	 * If pid namespaces are enabled and the current task is in a child
33315d94b82SRobin Holt 	 * pid_namespace, the command is handled by reboot_pid_ns() which will
33415d94b82SRobin Holt 	 * call do_exit().
33515d94b82SRobin Holt 	 */
33615d94b82SRobin Holt 	ret = reboot_pid_ns(pid_ns, cmd);
33715d94b82SRobin Holt 	if (ret)
33815d94b82SRobin Holt 		return ret;
33915d94b82SRobin Holt 
34015d94b82SRobin Holt 	/* Instead of trying to make the power_off code look like
34115d94b82SRobin Holt 	 * halt when pm_power_off is not set do it the easy way.
34215d94b82SRobin Holt 	 */
34315d94b82SRobin Holt 	if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
34415d94b82SRobin Holt 		cmd = LINUX_REBOOT_CMD_HALT;
34515d94b82SRobin Holt 
34655f2503cSPingfan Liu 	mutex_lock(&system_transition_mutex);
34715d94b82SRobin Holt 	switch (cmd) {
34815d94b82SRobin Holt 	case LINUX_REBOOT_CMD_RESTART:
34915d94b82SRobin Holt 		kernel_restart(NULL);
35015d94b82SRobin Holt 		break;
35115d94b82SRobin Holt 
35215d94b82SRobin Holt 	case LINUX_REBOOT_CMD_CAD_ON:
35315d94b82SRobin Holt 		C_A_D = 1;
35415d94b82SRobin Holt 		break;
35515d94b82SRobin Holt 
35615d94b82SRobin Holt 	case LINUX_REBOOT_CMD_CAD_OFF:
35715d94b82SRobin Holt 		C_A_D = 0;
35815d94b82SRobin Holt 		break;
35915d94b82SRobin Holt 
36015d94b82SRobin Holt 	case LINUX_REBOOT_CMD_HALT:
36115d94b82SRobin Holt 		kernel_halt();
36215d94b82SRobin Holt 		do_exit(0);
36315d94b82SRobin Holt 
36415d94b82SRobin Holt 	case LINUX_REBOOT_CMD_POWER_OFF:
36515d94b82SRobin Holt 		kernel_power_off();
36615d94b82SRobin Holt 		do_exit(0);
36715d94b82SRobin Holt 		break;
36815d94b82SRobin Holt 
36915d94b82SRobin Holt 	case LINUX_REBOOT_CMD_RESTART2:
370972ee83dSRobin Holt 		ret = strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1);
371972ee83dSRobin Holt 		if (ret < 0) {
37215d94b82SRobin Holt 			ret = -EFAULT;
37315d94b82SRobin Holt 			break;
37415d94b82SRobin Holt 		}
37515d94b82SRobin Holt 		buffer[sizeof(buffer) - 1] = '\0';
37615d94b82SRobin Holt 
37715d94b82SRobin Holt 		kernel_restart(buffer);
37815d94b82SRobin Holt 		break;
37915d94b82SRobin Holt 
3802965faa5SDave Young #ifdef CONFIG_KEXEC_CORE
38115d94b82SRobin Holt 	case LINUX_REBOOT_CMD_KEXEC:
38215d94b82SRobin Holt 		ret = kernel_kexec();
38315d94b82SRobin Holt 		break;
38415d94b82SRobin Holt #endif
38515d94b82SRobin Holt 
38615d94b82SRobin Holt #ifdef CONFIG_HIBERNATION
38715d94b82SRobin Holt 	case LINUX_REBOOT_CMD_SW_SUSPEND:
38815d94b82SRobin Holt 		ret = hibernate();
38915d94b82SRobin Holt 		break;
39015d94b82SRobin Holt #endif
39115d94b82SRobin Holt 
39215d94b82SRobin Holt 	default:
39315d94b82SRobin Holt 		ret = -EINVAL;
39415d94b82SRobin Holt 		break;
39515d94b82SRobin Holt 	}
39655f2503cSPingfan Liu 	mutex_unlock(&system_transition_mutex);
39715d94b82SRobin Holt 	return ret;
39815d94b82SRobin Holt }
39915d94b82SRobin Holt 
40015d94b82SRobin Holt static void deferred_cad(struct work_struct *dummy)
40115d94b82SRobin Holt {
40215d94b82SRobin Holt 	kernel_restart(NULL);
40315d94b82SRobin Holt }
40415d94b82SRobin Holt 
40515d94b82SRobin Holt /*
40615d94b82SRobin Holt  * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
40715d94b82SRobin Holt  * As it's called within an interrupt, it may NOT sync: the only choice
40815d94b82SRobin Holt  * is whether to reboot at once, or just ignore the ctrl-alt-del.
40915d94b82SRobin Holt  */
41015d94b82SRobin Holt void ctrl_alt_del(void)
41115d94b82SRobin Holt {
41215d94b82SRobin Holt 	static DECLARE_WORK(cad_work, deferred_cad);
41315d94b82SRobin Holt 
41415d94b82SRobin Holt 	if (C_A_D)
41515d94b82SRobin Holt 		schedule_work(&cad_work);
41615d94b82SRobin Holt 	else
41715d94b82SRobin Holt 		kill_cad_pid(SIGINT, 1);
41815d94b82SRobin Holt }
41915d94b82SRobin Holt 
42006d17766Stangmeng #define POWEROFF_CMD_PATH_LEN  256
42106d17766Stangmeng static char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
4227a54f46bSJoel Stanley static const char reboot_cmd[] = "/sbin/reboot";
42315d94b82SRobin Holt 
4247a54f46bSJoel Stanley static int run_cmd(const char *cmd)
42515d94b82SRobin Holt {
42615d94b82SRobin Holt 	char **argv;
42715d94b82SRobin Holt 	static char *envp[] = {
42815d94b82SRobin Holt 		"HOME=/",
42915d94b82SRobin Holt 		"PATH=/sbin:/bin:/usr/sbin:/usr/bin",
43015d94b82SRobin Holt 		NULL
43115d94b82SRobin Holt 	};
43215d94b82SRobin Holt 	int ret;
4337a54f46bSJoel Stanley 	argv = argv_split(GFP_KERNEL, cmd, NULL);
43415d94b82SRobin Holt 	if (argv) {
43515d94b82SRobin Holt 		ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
43615d94b82SRobin Holt 		argv_free(argv);
43715d94b82SRobin Holt 	} else {
43815d94b82SRobin Holt 		ret = -ENOMEM;
43915d94b82SRobin Holt 	}
44015d94b82SRobin Holt 
4417a54f46bSJoel Stanley 	return ret;
4427a54f46bSJoel Stanley }
4437a54f46bSJoel Stanley 
4447a54f46bSJoel Stanley static int __orderly_reboot(void)
4457a54f46bSJoel Stanley {
4467a54f46bSJoel Stanley 	int ret;
4477a54f46bSJoel Stanley 
4487a54f46bSJoel Stanley 	ret = run_cmd(reboot_cmd);
4497a54f46bSJoel Stanley 
4507a54f46bSJoel Stanley 	if (ret) {
4517a54f46bSJoel Stanley 		pr_warn("Failed to start orderly reboot: forcing the issue\n");
4527a54f46bSJoel Stanley 		emergency_sync();
4537a54f46bSJoel Stanley 		kernel_restart(NULL);
4547a54f46bSJoel Stanley 	}
4557a54f46bSJoel Stanley 
4567a54f46bSJoel Stanley 	return ret;
4577a54f46bSJoel Stanley }
4587a54f46bSJoel Stanley 
4597a54f46bSJoel Stanley static int __orderly_poweroff(bool force)
4607a54f46bSJoel Stanley {
4617a54f46bSJoel Stanley 	int ret;
4627a54f46bSJoel Stanley 
4637a54f46bSJoel Stanley 	ret = run_cmd(poweroff_cmd);
4647a54f46bSJoel Stanley 
46515d94b82SRobin Holt 	if (ret && force) {
466972ee83dSRobin Holt 		pr_warn("Failed to start orderly shutdown: forcing the issue\n");
4677a54f46bSJoel Stanley 
46815d94b82SRobin Holt 		/*
46915d94b82SRobin Holt 		 * I guess this should try to kick off some daemon to sync and
47015d94b82SRobin Holt 		 * poweroff asap.  Or not even bother syncing if we're doing an
47115d94b82SRobin Holt 		 * emergency shutdown?
47215d94b82SRobin Holt 		 */
47315d94b82SRobin Holt 		emergency_sync();
47415d94b82SRobin Holt 		kernel_power_off();
47515d94b82SRobin Holt 	}
47615d94b82SRobin Holt 
47715d94b82SRobin Holt 	return ret;
47815d94b82SRobin Holt }
47915d94b82SRobin Holt 
48015d94b82SRobin Holt static bool poweroff_force;
48115d94b82SRobin Holt 
48215d94b82SRobin Holt static void poweroff_work_func(struct work_struct *work)
48315d94b82SRobin Holt {
48415d94b82SRobin Holt 	__orderly_poweroff(poweroff_force);
48515d94b82SRobin Holt }
48615d94b82SRobin Holt 
48715d94b82SRobin Holt static DECLARE_WORK(poweroff_work, poweroff_work_func);
48815d94b82SRobin Holt 
48915d94b82SRobin Holt /**
49015d94b82SRobin Holt  * orderly_poweroff - Trigger an orderly system poweroff
49115d94b82SRobin Holt  * @force: force poweroff if command execution fails
49215d94b82SRobin Holt  *
49315d94b82SRobin Holt  * This may be called from any context to trigger a system shutdown.
49415d94b82SRobin Holt  * If the orderly shutdown fails, it will force an immediate shutdown.
49515d94b82SRobin Holt  */
4967a54f46bSJoel Stanley void orderly_poweroff(bool force)
49715d94b82SRobin Holt {
49815d94b82SRobin Holt 	if (force) /* do not override the pending "true" */
49915d94b82SRobin Holt 		poweroff_force = true;
50015d94b82SRobin Holt 	schedule_work(&poweroff_work);
50115d94b82SRobin Holt }
50215d94b82SRobin Holt EXPORT_SYMBOL_GPL(orderly_poweroff);
5031b3a5d02SRobin Holt 
5047a54f46bSJoel Stanley static void reboot_work_func(struct work_struct *work)
5057a54f46bSJoel Stanley {
5067a54f46bSJoel Stanley 	__orderly_reboot();
5077a54f46bSJoel Stanley }
5087a54f46bSJoel Stanley 
5097a54f46bSJoel Stanley static DECLARE_WORK(reboot_work, reboot_work_func);
5107a54f46bSJoel Stanley 
5117a54f46bSJoel Stanley /**
5127a54f46bSJoel Stanley  * orderly_reboot - Trigger an orderly system reboot
5137a54f46bSJoel Stanley  *
5147a54f46bSJoel Stanley  * This may be called from any context to trigger a system reboot.
5157a54f46bSJoel Stanley  * If the orderly reboot fails, it will force an immediate reboot.
5167a54f46bSJoel Stanley  */
5177a54f46bSJoel Stanley void orderly_reboot(void)
5187a54f46bSJoel Stanley {
5197a54f46bSJoel Stanley 	schedule_work(&reboot_work);
5207a54f46bSJoel Stanley }
5217a54f46bSJoel Stanley EXPORT_SYMBOL_GPL(orderly_reboot);
5227a54f46bSJoel Stanley 
523dfa19b11SMatti Vaittinen /**
524dfa19b11SMatti Vaittinen  * hw_failure_emergency_poweroff_func - emergency poweroff work after a known delay
525dfa19b11SMatti Vaittinen  * @work: work_struct associated with the emergency poweroff function
526dfa19b11SMatti Vaittinen  *
527dfa19b11SMatti Vaittinen  * This function is called in very critical situations to force
528dfa19b11SMatti Vaittinen  * a kernel poweroff after a configurable timeout value.
529dfa19b11SMatti Vaittinen  */
530dfa19b11SMatti Vaittinen static void hw_failure_emergency_poweroff_func(struct work_struct *work)
531dfa19b11SMatti Vaittinen {
532dfa19b11SMatti Vaittinen 	/*
533dfa19b11SMatti Vaittinen 	 * We have reached here after the emergency shutdown waiting period has
534dfa19b11SMatti Vaittinen 	 * expired. This means orderly_poweroff has not been able to shut off
535dfa19b11SMatti Vaittinen 	 * the system for some reason.
536dfa19b11SMatti Vaittinen 	 *
537dfa19b11SMatti Vaittinen 	 * Try to shut down the system immediately using kernel_power_off
538dfa19b11SMatti Vaittinen 	 * if populated
539dfa19b11SMatti Vaittinen 	 */
540dfa19b11SMatti Vaittinen 	pr_emerg("Hardware protection timed-out. Trying forced poweroff\n");
541dfa19b11SMatti Vaittinen 	kernel_power_off();
542dfa19b11SMatti Vaittinen 
543dfa19b11SMatti Vaittinen 	/*
544dfa19b11SMatti Vaittinen 	 * Worst of the worst case trigger emergency restart
545dfa19b11SMatti Vaittinen 	 */
546dfa19b11SMatti Vaittinen 	pr_emerg("Hardware protection shutdown failed. Trying emergency restart\n");
547dfa19b11SMatti Vaittinen 	emergency_restart();
548dfa19b11SMatti Vaittinen }
549dfa19b11SMatti Vaittinen 
550dfa19b11SMatti Vaittinen static DECLARE_DELAYED_WORK(hw_failure_emergency_poweroff_work,
551dfa19b11SMatti Vaittinen 			    hw_failure_emergency_poweroff_func);
552dfa19b11SMatti Vaittinen 
553dfa19b11SMatti Vaittinen /**
554dfa19b11SMatti Vaittinen  * hw_failure_emergency_poweroff - Trigger an emergency system poweroff
555dfa19b11SMatti Vaittinen  *
556dfa19b11SMatti Vaittinen  * This may be called from any critical situation to trigger a system shutdown
557dfa19b11SMatti Vaittinen  * after a given period of time. If time is negative this is not scheduled.
558dfa19b11SMatti Vaittinen  */
559dfa19b11SMatti Vaittinen static void hw_failure_emergency_poweroff(int poweroff_delay_ms)
560dfa19b11SMatti Vaittinen {
561dfa19b11SMatti Vaittinen 	if (poweroff_delay_ms <= 0)
562dfa19b11SMatti Vaittinen 		return;
563dfa19b11SMatti Vaittinen 	schedule_delayed_work(&hw_failure_emergency_poweroff_work,
564dfa19b11SMatti Vaittinen 			      msecs_to_jiffies(poweroff_delay_ms));
565dfa19b11SMatti Vaittinen }
566dfa19b11SMatti Vaittinen 
567dfa19b11SMatti Vaittinen /**
568dfa19b11SMatti Vaittinen  * hw_protection_shutdown - Trigger an emergency system poweroff
569dfa19b11SMatti Vaittinen  *
570dfa19b11SMatti Vaittinen  * @reason:		Reason of emergency shutdown to be printed.
571dfa19b11SMatti Vaittinen  * @ms_until_forced:	Time to wait for orderly shutdown before tiggering a
572dfa19b11SMatti Vaittinen  *			forced shudown. Negative value disables the forced
573dfa19b11SMatti Vaittinen  *			shutdown.
574dfa19b11SMatti Vaittinen  *
575dfa19b11SMatti Vaittinen  * Initiate an emergency system shutdown in order to protect hardware from
576dfa19b11SMatti Vaittinen  * further damage. Usage examples include a thermal protection or a voltage or
577dfa19b11SMatti Vaittinen  * current regulator failures.
578dfa19b11SMatti Vaittinen  * NOTE: The request is ignored if protection shutdown is already pending even
579dfa19b11SMatti Vaittinen  * if the previous request has given a large timeout for forced shutdown.
580dfa19b11SMatti Vaittinen  * Can be called from any context.
581dfa19b11SMatti Vaittinen  */
582dfa19b11SMatti Vaittinen void hw_protection_shutdown(const char *reason, int ms_until_forced)
583dfa19b11SMatti Vaittinen {
584dfa19b11SMatti Vaittinen 	static atomic_t allow_proceed = ATOMIC_INIT(1);
585dfa19b11SMatti Vaittinen 
586dfa19b11SMatti Vaittinen 	pr_emerg("HARDWARE PROTECTION shutdown (%s)\n", reason);
587dfa19b11SMatti Vaittinen 
588dfa19b11SMatti Vaittinen 	/* Shutdown should be initiated only once. */
589dfa19b11SMatti Vaittinen 	if (!atomic_dec_and_test(&allow_proceed))
590dfa19b11SMatti Vaittinen 		return;
591dfa19b11SMatti Vaittinen 
592dfa19b11SMatti Vaittinen 	/*
593dfa19b11SMatti Vaittinen 	 * Queue a backup emergency shutdown in the event of
594dfa19b11SMatti Vaittinen 	 * orderly_poweroff failure
595dfa19b11SMatti Vaittinen 	 */
596dfa19b11SMatti Vaittinen 	hw_failure_emergency_poweroff(ms_until_forced);
597dfa19b11SMatti Vaittinen 	orderly_poweroff(true);
598dfa19b11SMatti Vaittinen }
599dfa19b11SMatti Vaittinen EXPORT_SYMBOL_GPL(hw_protection_shutdown);
600dfa19b11SMatti Vaittinen 
6011b3a5d02SRobin Holt static int __init reboot_setup(char *str)
6021b3a5d02SRobin Holt {
6031b3a5d02SRobin Holt 	for (;;) {
604b287a25aSAaro Koskinen 		enum reboot_mode *mode;
605b287a25aSAaro Koskinen 
6061b3a5d02SRobin Holt 		/*
6071b3a5d02SRobin Holt 		 * Having anything passed on the command line via
6081b3a5d02SRobin Holt 		 * reboot= will cause us to disable DMI checking
6091b3a5d02SRobin Holt 		 * below.
6101b3a5d02SRobin Holt 		 */
6111b3a5d02SRobin Holt 		reboot_default = 0;
6121b3a5d02SRobin Holt 
613b287a25aSAaro Koskinen 		if (!strncmp(str, "panic_", 6)) {
614b287a25aSAaro Koskinen 			mode = &panic_reboot_mode;
615b287a25aSAaro Koskinen 			str += 6;
616b287a25aSAaro Koskinen 		} else {
617b287a25aSAaro Koskinen 			mode = &reboot_mode;
618b287a25aSAaro Koskinen 		}
619b287a25aSAaro Koskinen 
6201b3a5d02SRobin Holt 		switch (*str) {
6211b3a5d02SRobin Holt 		case 'w':
622b287a25aSAaro Koskinen 			*mode = REBOOT_WARM;
6231b3a5d02SRobin Holt 			break;
6241b3a5d02SRobin Holt 
6251b3a5d02SRobin Holt 		case 'c':
626b287a25aSAaro Koskinen 			*mode = REBOOT_COLD;
6271b3a5d02SRobin Holt 			break;
6281b3a5d02SRobin Holt 
6291b3a5d02SRobin Holt 		case 'h':
630b287a25aSAaro Koskinen 			*mode = REBOOT_HARD;
6311b3a5d02SRobin Holt 			break;
6321b3a5d02SRobin Holt 
6331b3a5d02SRobin Holt 		case 's':
634f9a90501SMatteo Croce 			/*
635f9a90501SMatteo Croce 			 * reboot_cpu is s[mp]#### with #### being the processor
636f9a90501SMatteo Croce 			 * to be used for rebooting. Skip 's' or 'smp' prefix.
637f9a90501SMatteo Croce 			 */
638f9a90501SMatteo Croce 			str += str[1] == 'm' && str[2] == 'p' ? 3 : 1;
639f9a90501SMatteo Croce 
640f9a90501SMatteo Croce 			if (isdigit(str[0])) {
641f9a90501SMatteo Croce 				int cpu = simple_strtoul(str, NULL, 0);
642f9a90501SMatteo Croce 
643f9a90501SMatteo Croce 				if (cpu >= num_possible_cpus()) {
644df5b0ab3SMatteo Croce 					pr_err("Ignoring the CPU number in reboot= option. "
645df5b0ab3SMatteo Croce 					"CPU %d exceeds possible cpu number %d\n",
646f9a90501SMatteo Croce 					cpu, num_possible_cpus());
647df5b0ab3SMatteo Croce 					break;
648df5b0ab3SMatteo Croce 				}
649f9a90501SMatteo Croce 				reboot_cpu = cpu;
650f9a90501SMatteo Croce 			} else
651f9a90501SMatteo Croce 				*mode = REBOOT_SOFT;
6521b3a5d02SRobin Holt 			break;
6538b92c4ffSMatteo Croce 
6541b3a5d02SRobin Holt 		case 'g':
655b287a25aSAaro Koskinen 			*mode = REBOOT_GPIO;
6561b3a5d02SRobin Holt 			break;
6571b3a5d02SRobin Holt 
6581b3a5d02SRobin Holt 		case 'b':
6591b3a5d02SRobin Holt 		case 'a':
6601b3a5d02SRobin Holt 		case 'k':
6611b3a5d02SRobin Holt 		case 't':
6621b3a5d02SRobin Holt 		case 'e':
6631b3a5d02SRobin Holt 		case 'p':
6641b3a5d02SRobin Holt 			reboot_type = *str;
6651b3a5d02SRobin Holt 			break;
6661b3a5d02SRobin Holt 
6671b3a5d02SRobin Holt 		case 'f':
6681b3a5d02SRobin Holt 			reboot_force = 1;
6691b3a5d02SRobin Holt 			break;
6701b3a5d02SRobin Holt 		}
6711b3a5d02SRobin Holt 
6721b3a5d02SRobin Holt 		str = strchr(str, ',');
6731b3a5d02SRobin Holt 		if (str)
6741b3a5d02SRobin Holt 			str++;
6751b3a5d02SRobin Holt 		else
6761b3a5d02SRobin Holt 			break;
6771b3a5d02SRobin Holt 	}
6781b3a5d02SRobin Holt 	return 1;
6791b3a5d02SRobin Holt }
6801b3a5d02SRobin Holt __setup("reboot=", reboot_setup);
6812c622ed0SMatteo Croce 
6822c622ed0SMatteo Croce #ifdef CONFIG_SYSFS
6832c622ed0SMatteo Croce 
6842c622ed0SMatteo Croce #define REBOOT_COLD_STR		"cold"
6852c622ed0SMatteo Croce #define REBOOT_WARM_STR		"warm"
6862c622ed0SMatteo Croce #define REBOOT_HARD_STR		"hard"
6872c622ed0SMatteo Croce #define REBOOT_SOFT_STR		"soft"
6882c622ed0SMatteo Croce #define REBOOT_GPIO_STR		"gpio"
6892c622ed0SMatteo Croce #define REBOOT_UNDEFINED_STR	"undefined"
6902c622ed0SMatteo Croce 
6912c622ed0SMatteo Croce #define BOOT_TRIPLE_STR		"triple"
6922c622ed0SMatteo Croce #define BOOT_KBD_STR		"kbd"
6932c622ed0SMatteo Croce #define BOOT_BIOS_STR		"bios"
6942c622ed0SMatteo Croce #define BOOT_ACPI_STR		"acpi"
6952c622ed0SMatteo Croce #define BOOT_EFI_STR		"efi"
6960c5c0179SMatteo Croce #define BOOT_PCI_STR		"pci"
6972c622ed0SMatteo Croce 
6982c622ed0SMatteo Croce static ssize_t mode_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
6992c622ed0SMatteo Croce {
7002c622ed0SMatteo Croce 	const char *val;
7012c622ed0SMatteo Croce 
7022c622ed0SMatteo Croce 	switch (reboot_mode) {
7032c622ed0SMatteo Croce 	case REBOOT_COLD:
7042c622ed0SMatteo Croce 		val = REBOOT_COLD_STR;
7052c622ed0SMatteo Croce 		break;
7062c622ed0SMatteo Croce 	case REBOOT_WARM:
7072c622ed0SMatteo Croce 		val = REBOOT_WARM_STR;
7082c622ed0SMatteo Croce 		break;
7092c622ed0SMatteo Croce 	case REBOOT_HARD:
7102c622ed0SMatteo Croce 		val = REBOOT_HARD_STR;
7112c622ed0SMatteo Croce 		break;
7122c622ed0SMatteo Croce 	case REBOOT_SOFT:
7132c622ed0SMatteo Croce 		val = REBOOT_SOFT_STR;
7142c622ed0SMatteo Croce 		break;
7152c622ed0SMatteo Croce 	case REBOOT_GPIO:
7162c622ed0SMatteo Croce 		val = REBOOT_GPIO_STR;
7172c622ed0SMatteo Croce 		break;
7182c622ed0SMatteo Croce 	default:
7192c622ed0SMatteo Croce 		val = REBOOT_UNDEFINED_STR;
7202c622ed0SMatteo Croce 	}
7212c622ed0SMatteo Croce 
7222c622ed0SMatteo Croce 	return sprintf(buf, "%s\n", val);
7232c622ed0SMatteo Croce }
7242c622ed0SMatteo Croce static ssize_t mode_store(struct kobject *kobj, struct kobj_attribute *attr,
7252c622ed0SMatteo Croce 			  const char *buf, size_t count)
7262c622ed0SMatteo Croce {
7272c622ed0SMatteo Croce 	if (!capable(CAP_SYS_BOOT))
7282c622ed0SMatteo Croce 		return -EPERM;
7292c622ed0SMatteo Croce 
7302c622ed0SMatteo Croce 	if (!strncmp(buf, REBOOT_COLD_STR, strlen(REBOOT_COLD_STR)))
7312c622ed0SMatteo Croce 		reboot_mode = REBOOT_COLD;
7322c622ed0SMatteo Croce 	else if (!strncmp(buf, REBOOT_WARM_STR, strlen(REBOOT_WARM_STR)))
7332c622ed0SMatteo Croce 		reboot_mode = REBOOT_WARM;
7342c622ed0SMatteo Croce 	else if (!strncmp(buf, REBOOT_HARD_STR, strlen(REBOOT_HARD_STR)))
7352c622ed0SMatteo Croce 		reboot_mode = REBOOT_HARD;
7362c622ed0SMatteo Croce 	else if (!strncmp(buf, REBOOT_SOFT_STR, strlen(REBOOT_SOFT_STR)))
7372c622ed0SMatteo Croce 		reboot_mode = REBOOT_SOFT;
7382c622ed0SMatteo Croce 	else if (!strncmp(buf, REBOOT_GPIO_STR, strlen(REBOOT_GPIO_STR)))
7392c622ed0SMatteo Croce 		reboot_mode = REBOOT_GPIO;
7402c622ed0SMatteo Croce 	else
7412c622ed0SMatteo Croce 		return -EINVAL;
7422c622ed0SMatteo Croce 
7431a9d079fSMatteo Croce 	reboot_default = 0;
7441a9d079fSMatteo Croce 
7452c622ed0SMatteo Croce 	return count;
7462c622ed0SMatteo Croce }
7472c622ed0SMatteo Croce static struct kobj_attribute reboot_mode_attr = __ATTR_RW(mode);
7482c622ed0SMatteo Croce 
74940247e55SMatteo Croce #ifdef CONFIG_X86
75040247e55SMatteo Croce static ssize_t force_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
75140247e55SMatteo Croce {
75240247e55SMatteo Croce 	return sprintf(buf, "%d\n", reboot_force);
75340247e55SMatteo Croce }
75440247e55SMatteo Croce static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr,
75540247e55SMatteo Croce 			  const char *buf, size_t count)
75640247e55SMatteo Croce {
75740247e55SMatteo Croce 	bool res;
75840247e55SMatteo Croce 
75940247e55SMatteo Croce 	if (!capable(CAP_SYS_BOOT))
76040247e55SMatteo Croce 		return -EPERM;
76140247e55SMatteo Croce 
76240247e55SMatteo Croce 	if (kstrtobool(buf, &res))
76340247e55SMatteo Croce 		return -EINVAL;
76440247e55SMatteo Croce 
76540247e55SMatteo Croce 	reboot_default = 0;
76640247e55SMatteo Croce 	reboot_force = res;
76740247e55SMatteo Croce 
76840247e55SMatteo Croce 	return count;
76940247e55SMatteo Croce }
77040247e55SMatteo Croce static struct kobj_attribute reboot_force_attr = __ATTR_RW(force);
77140247e55SMatteo Croce 
7722c622ed0SMatteo Croce static ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
7732c622ed0SMatteo Croce {
7742c622ed0SMatteo Croce 	const char *val;
7752c622ed0SMatteo Croce 
7762c622ed0SMatteo Croce 	switch (reboot_type) {
7772c622ed0SMatteo Croce 	case BOOT_TRIPLE:
7782c622ed0SMatteo Croce 		val = BOOT_TRIPLE_STR;
7792c622ed0SMatteo Croce 		break;
7802c622ed0SMatteo Croce 	case BOOT_KBD:
7812c622ed0SMatteo Croce 		val = BOOT_KBD_STR;
7822c622ed0SMatteo Croce 		break;
7832c622ed0SMatteo Croce 	case BOOT_BIOS:
7842c622ed0SMatteo Croce 		val = BOOT_BIOS_STR;
7852c622ed0SMatteo Croce 		break;
7862c622ed0SMatteo Croce 	case BOOT_ACPI:
7872c622ed0SMatteo Croce 		val = BOOT_ACPI_STR;
7882c622ed0SMatteo Croce 		break;
7892c622ed0SMatteo Croce 	case BOOT_EFI:
7902c622ed0SMatteo Croce 		val = BOOT_EFI_STR;
7912c622ed0SMatteo Croce 		break;
7922c622ed0SMatteo Croce 	case BOOT_CF9_FORCE:
7930c5c0179SMatteo Croce 		val = BOOT_PCI_STR;
7942c622ed0SMatteo Croce 		break;
7952c622ed0SMatteo Croce 	default:
7962c622ed0SMatteo Croce 		val = REBOOT_UNDEFINED_STR;
7972c622ed0SMatteo Croce 	}
7982c622ed0SMatteo Croce 
7992c622ed0SMatteo Croce 	return sprintf(buf, "%s\n", val);
8002c622ed0SMatteo Croce }
8012c622ed0SMatteo Croce static ssize_t type_store(struct kobject *kobj, struct kobj_attribute *attr,
8022c622ed0SMatteo Croce 			  const char *buf, size_t count)
8032c622ed0SMatteo Croce {
8042c622ed0SMatteo Croce 	if (!capable(CAP_SYS_BOOT))
8052c622ed0SMatteo Croce 		return -EPERM;
8062c622ed0SMatteo Croce 
8072c622ed0SMatteo Croce 	if (!strncmp(buf, BOOT_TRIPLE_STR, strlen(BOOT_TRIPLE_STR)))
8082c622ed0SMatteo Croce 		reboot_type = BOOT_TRIPLE;
8092c622ed0SMatteo Croce 	else if (!strncmp(buf, BOOT_KBD_STR, strlen(BOOT_KBD_STR)))
8102c622ed0SMatteo Croce 		reboot_type = BOOT_KBD;
8112c622ed0SMatteo Croce 	else if (!strncmp(buf, BOOT_BIOS_STR, strlen(BOOT_BIOS_STR)))
8122c622ed0SMatteo Croce 		reboot_type = BOOT_BIOS;
8132c622ed0SMatteo Croce 	else if (!strncmp(buf, BOOT_ACPI_STR, strlen(BOOT_ACPI_STR)))
8142c622ed0SMatteo Croce 		reboot_type = BOOT_ACPI;
8152c622ed0SMatteo Croce 	else if (!strncmp(buf, BOOT_EFI_STR, strlen(BOOT_EFI_STR)))
8162c622ed0SMatteo Croce 		reboot_type = BOOT_EFI;
8170c5c0179SMatteo Croce 	else if (!strncmp(buf, BOOT_PCI_STR, strlen(BOOT_PCI_STR)))
8182c622ed0SMatteo Croce 		reboot_type = BOOT_CF9_FORCE;
8192c622ed0SMatteo Croce 	else
8202c622ed0SMatteo Croce 		return -EINVAL;
8212c622ed0SMatteo Croce 
8221a9d079fSMatteo Croce 	reboot_default = 0;
8231a9d079fSMatteo Croce 
8242c622ed0SMatteo Croce 	return count;
8252c622ed0SMatteo Croce }
8262c622ed0SMatteo Croce static struct kobj_attribute reboot_type_attr = __ATTR_RW(type);
82740247e55SMatteo Croce #endif
8282c622ed0SMatteo Croce 
82940247e55SMatteo Croce #ifdef CONFIG_SMP
8302c622ed0SMatteo Croce static ssize_t cpu_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
8312c622ed0SMatteo Croce {
8322c622ed0SMatteo Croce 	return sprintf(buf, "%d\n", reboot_cpu);
8332c622ed0SMatteo Croce }
8342c622ed0SMatteo Croce static ssize_t cpu_store(struct kobject *kobj, struct kobj_attribute *attr,
8352c622ed0SMatteo Croce 			  const char *buf, size_t count)
8362c622ed0SMatteo Croce {
8372c622ed0SMatteo Croce 	unsigned int cpunum;
8382c622ed0SMatteo Croce 	int rc;
8392c622ed0SMatteo Croce 
8402c622ed0SMatteo Croce 	if (!capable(CAP_SYS_BOOT))
8412c622ed0SMatteo Croce 		return -EPERM;
8422c622ed0SMatteo Croce 
8432c622ed0SMatteo Croce 	rc = kstrtouint(buf, 0, &cpunum);
8442c622ed0SMatteo Croce 
8452c622ed0SMatteo Croce 	if (rc)
8462c622ed0SMatteo Croce 		return rc;
8472c622ed0SMatteo Croce 
8482c622ed0SMatteo Croce 	if (cpunum >= num_possible_cpus())
8492c622ed0SMatteo Croce 		return -ERANGE;
8502c622ed0SMatteo Croce 
8511a9d079fSMatteo Croce 	reboot_default = 0;
8522c622ed0SMatteo Croce 	reboot_cpu = cpunum;
8532c622ed0SMatteo Croce 
8542c622ed0SMatteo Croce 	return count;
8552c622ed0SMatteo Croce }
8562c622ed0SMatteo Croce static struct kobj_attribute reboot_cpu_attr = __ATTR_RW(cpu);
85740247e55SMatteo Croce #endif
8582c622ed0SMatteo Croce 
8592c622ed0SMatteo Croce static struct attribute *reboot_attrs[] = {
8602c622ed0SMatteo Croce 	&reboot_mode_attr.attr,
86140247e55SMatteo Croce #ifdef CONFIG_X86
8622c622ed0SMatteo Croce 	&reboot_force_attr.attr,
86340247e55SMatteo Croce 	&reboot_type_attr.attr,
86440247e55SMatteo Croce #endif
86540247e55SMatteo Croce #ifdef CONFIG_SMP
86640247e55SMatteo Croce 	&reboot_cpu_attr.attr,
86740247e55SMatteo Croce #endif
8682c622ed0SMatteo Croce 	NULL,
8692c622ed0SMatteo Croce };
8702c622ed0SMatteo Croce 
871*764aaf44SYueHaibing #ifdef CONFIG_SYSCTL
872*764aaf44SYueHaibing static struct ctl_table kern_reboot_table[] = {
873*764aaf44SYueHaibing 	{
874*764aaf44SYueHaibing 		.procname       = "poweroff_cmd",
875*764aaf44SYueHaibing 		.data           = &poweroff_cmd,
876*764aaf44SYueHaibing 		.maxlen         = POWEROFF_CMD_PATH_LEN,
877*764aaf44SYueHaibing 		.mode           = 0644,
878*764aaf44SYueHaibing 		.proc_handler   = proc_dostring,
879*764aaf44SYueHaibing 	},
880*764aaf44SYueHaibing 	{
881*764aaf44SYueHaibing 		.procname       = "ctrl-alt-del",
882*764aaf44SYueHaibing 		.data           = &C_A_D,
883*764aaf44SYueHaibing 		.maxlen         = sizeof(int),
884*764aaf44SYueHaibing 		.mode           = 0644,
885*764aaf44SYueHaibing 		.proc_handler   = proc_dointvec,
886*764aaf44SYueHaibing 	},
887*764aaf44SYueHaibing 	{ }
888*764aaf44SYueHaibing };
889*764aaf44SYueHaibing 
890*764aaf44SYueHaibing static void __init kernel_reboot_sysctls_init(void)
891*764aaf44SYueHaibing {
892*764aaf44SYueHaibing 	register_sysctl_init("kernel", kern_reboot_table);
893*764aaf44SYueHaibing }
894*764aaf44SYueHaibing #else
895*764aaf44SYueHaibing #define kernel_reboot_sysctls_init() do { } while (0)
896*764aaf44SYueHaibing #endif /* CONFIG_SYSCTL */
897*764aaf44SYueHaibing 
8982c622ed0SMatteo Croce static const struct attribute_group reboot_attr_group = {
8992c622ed0SMatteo Croce 	.attrs = reboot_attrs,
9002c622ed0SMatteo Croce };
9012c622ed0SMatteo Croce 
9022c622ed0SMatteo Croce static int __init reboot_ksysfs_init(void)
9032c622ed0SMatteo Croce {
9042c622ed0SMatteo Croce 	struct kobject *reboot_kobj;
9052c622ed0SMatteo Croce 	int ret;
9062c622ed0SMatteo Croce 
9072c622ed0SMatteo Croce 	reboot_kobj = kobject_create_and_add("reboot", kernel_kobj);
9082c622ed0SMatteo Croce 	if (!reboot_kobj)
9092c622ed0SMatteo Croce 		return -ENOMEM;
9102c622ed0SMatteo Croce 
9112c622ed0SMatteo Croce 	ret = sysfs_create_group(reboot_kobj, &reboot_attr_group);
9122c622ed0SMatteo Croce 	if (ret) {
9132c622ed0SMatteo Croce 		kobject_put(reboot_kobj);
9142c622ed0SMatteo Croce 		return ret;
9152c622ed0SMatteo Croce 	}
9162c622ed0SMatteo Croce 
91706d17766Stangmeng 	kernel_reboot_sysctls_init();
91806d17766Stangmeng 
9192c622ed0SMatteo Croce 	return 0;
9202c622ed0SMatteo Croce }
9212c622ed0SMatteo Croce late_initcall(reboot_ksysfs_init);
9222c622ed0SMatteo Croce 
9232c622ed0SMatteo Croce #endif
924