xref: /openbmc/linux/kernel/reboot.c (revision d46b3f5bc0fc265b7ef013e76d7d43f85e4b1e7c)
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 
101b3a5d02SRobin Holt #include <linux/ctype.h>
1115d94b82SRobin Holt #include <linux/export.h>
1215d94b82SRobin Holt #include <linux/kexec.h>
1315d94b82SRobin Holt #include <linux/kmod.h>
1415d94b82SRobin Holt #include <linux/kmsg_dump.h>
1515d94b82SRobin Holt #include <linux/reboot.h>
1615d94b82SRobin Holt #include <linux/suspend.h>
1715d94b82SRobin Holt #include <linux/syscalls.h>
1815d94b82SRobin Holt #include <linux/syscore_ops.h>
1915d94b82SRobin Holt #include <linux/uaccess.h>
2015d94b82SRobin Holt 
2115d94b82SRobin Holt /*
2215d94b82SRobin Holt  * this indicates whether you can reboot with ctrl-alt-del: the default is yes
2315d94b82SRobin Holt  */
2415d94b82SRobin Holt 
2515d94b82SRobin Holt int C_A_D = 1;
2615d94b82SRobin Holt struct pid *cad_pid;
2715d94b82SRobin Holt EXPORT_SYMBOL(cad_pid);
2815d94b82SRobin Holt 
29fb37409aSMike Rapoport #if defined(CONFIG_ARM)
301b3a5d02SRobin Holt #define DEFAULT_REBOOT_MODE		= REBOOT_HARD
311b3a5d02SRobin Holt #else
321b3a5d02SRobin Holt #define DEFAULT_REBOOT_MODE
331b3a5d02SRobin Holt #endif
341b3a5d02SRobin Holt enum reboot_mode reboot_mode DEFAULT_REBOOT_MODE;
35*d46b3f5bSShawn Guo EXPORT_SYMBOL_GPL(reboot_mode);
36b287a25aSAaro Koskinen enum reboot_mode panic_reboot_mode = REBOOT_UNDEFINED;
371b3a5d02SRobin Holt 
38e2f0b88eSChuansheng Liu /*
39e2f0b88eSChuansheng Liu  * This variable is used privately to keep track of whether or not
40e2f0b88eSChuansheng Liu  * reboot_type is still set to its default value (i.e., reboot= hasn't
41e2f0b88eSChuansheng Liu  * been set on the command line).  This is needed so that we can
42e2f0b88eSChuansheng Liu  * suppress DMI scanning for reboot quirks.  Without it, it's
43e2f0b88eSChuansheng Liu  * impossible to override a faulty reboot quirk without recompiling.
44e2f0b88eSChuansheng Liu  */
45e2f0b88eSChuansheng Liu int reboot_default = 1;
461b3a5d02SRobin Holt int reboot_cpu;
471b3a5d02SRobin Holt enum reboot_type reboot_type = BOOT_ACPI;
481b3a5d02SRobin Holt int reboot_force;
491b3a5d02SRobin Holt 
5015d94b82SRobin Holt /*
5115d94b82SRobin Holt  * If set, this is used for preparing the system to power off.
5215d94b82SRobin Holt  */
5315d94b82SRobin Holt 
5415d94b82SRobin Holt void (*pm_power_off_prepare)(void);
5574f008f2SOleksij Rempel EXPORT_SYMBOL_GPL(pm_power_off_prepare);
5615d94b82SRobin Holt 
5715d94b82SRobin Holt /**
5815d94b82SRobin Holt  *	emergency_restart - reboot the system
5915d94b82SRobin Holt  *
6015d94b82SRobin Holt  *	Without shutting down any hardware or taking any locks
6115d94b82SRobin Holt  *	reboot the system.  This is called when we know we are in
6215d94b82SRobin Holt  *	trouble so this is our best effort to reboot.  This is
6315d94b82SRobin Holt  *	safe to call in interrupt context.
6415d94b82SRobin Holt  */
6515d94b82SRobin Holt void emergency_restart(void)
6615d94b82SRobin Holt {
6715d94b82SRobin Holt 	kmsg_dump(KMSG_DUMP_EMERG);
6815d94b82SRobin Holt 	machine_emergency_restart();
6915d94b82SRobin Holt }
7015d94b82SRobin Holt EXPORT_SYMBOL_GPL(emergency_restart);
7115d94b82SRobin Holt 
7215d94b82SRobin Holt void kernel_restart_prepare(char *cmd)
7315d94b82SRobin Holt {
7415d94b82SRobin Holt 	blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
7515d94b82SRobin Holt 	system_state = SYSTEM_RESTART;
7615d94b82SRobin Holt 	usermodehelper_disable();
7715d94b82SRobin Holt 	device_shutdown();
7815d94b82SRobin Holt }
7915d94b82SRobin Holt 
8015d94b82SRobin Holt /**
8115d94b82SRobin Holt  *	register_reboot_notifier - Register function to be called at reboot time
8215d94b82SRobin Holt  *	@nb: Info about notifier function to be called
8315d94b82SRobin Holt  *
8415d94b82SRobin Holt  *	Registers a function with the list of functions
8515d94b82SRobin Holt  *	to be called at reboot time.
8615d94b82SRobin Holt  *
8715d94b82SRobin Holt  *	Currently always returns zero, as blocking_notifier_chain_register()
8815d94b82SRobin Holt  *	always returns zero.
8915d94b82SRobin Holt  */
9015d94b82SRobin Holt int register_reboot_notifier(struct notifier_block *nb)
9115d94b82SRobin Holt {
9215d94b82SRobin Holt 	return blocking_notifier_chain_register(&reboot_notifier_list, nb);
9315d94b82SRobin Holt }
9415d94b82SRobin Holt EXPORT_SYMBOL(register_reboot_notifier);
9515d94b82SRobin Holt 
9615d94b82SRobin Holt /**
9715d94b82SRobin Holt  *	unregister_reboot_notifier - Unregister previously registered reboot notifier
9815d94b82SRobin Holt  *	@nb: Hook to be unregistered
9915d94b82SRobin Holt  *
10015d94b82SRobin Holt  *	Unregisters a previously registered reboot
10115d94b82SRobin Holt  *	notifier function.
10215d94b82SRobin Holt  *
10315d94b82SRobin Holt  *	Returns zero on success, or %-ENOENT on failure.
10415d94b82SRobin Holt  */
10515d94b82SRobin Holt int unregister_reboot_notifier(struct notifier_block *nb)
10615d94b82SRobin Holt {
10715d94b82SRobin Holt 	return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
10815d94b82SRobin Holt }
10915d94b82SRobin Holt EXPORT_SYMBOL(unregister_reboot_notifier);
11015d94b82SRobin Holt 
1112d8364baSAndrey Smirnov static void devm_unregister_reboot_notifier(struct device *dev, void *res)
1122d8364baSAndrey Smirnov {
1132d8364baSAndrey Smirnov 	WARN_ON(unregister_reboot_notifier(*(struct notifier_block **)res));
1142d8364baSAndrey Smirnov }
1152d8364baSAndrey Smirnov 
1162d8364baSAndrey Smirnov int devm_register_reboot_notifier(struct device *dev, struct notifier_block *nb)
1172d8364baSAndrey Smirnov {
1182d8364baSAndrey Smirnov 	struct notifier_block **rcnb;
1192d8364baSAndrey Smirnov 	int ret;
1202d8364baSAndrey Smirnov 
1212d8364baSAndrey Smirnov 	rcnb = devres_alloc(devm_unregister_reboot_notifier,
1222d8364baSAndrey Smirnov 			    sizeof(*rcnb), GFP_KERNEL);
1232d8364baSAndrey Smirnov 	if (!rcnb)
1242d8364baSAndrey Smirnov 		return -ENOMEM;
1252d8364baSAndrey Smirnov 
1262d8364baSAndrey Smirnov 	ret = register_reboot_notifier(nb);
1272d8364baSAndrey Smirnov 	if (!ret) {
1282d8364baSAndrey Smirnov 		*rcnb = nb;
1292d8364baSAndrey Smirnov 		devres_add(dev, rcnb);
1302d8364baSAndrey Smirnov 	} else {
1312d8364baSAndrey Smirnov 		devres_free(rcnb);
1322d8364baSAndrey Smirnov 	}
1332d8364baSAndrey Smirnov 
1342d8364baSAndrey Smirnov 	return ret;
1352d8364baSAndrey Smirnov }
1362d8364baSAndrey Smirnov EXPORT_SYMBOL(devm_register_reboot_notifier);
1372d8364baSAndrey Smirnov 
138b63adb97SGuenter Roeck /*
139b63adb97SGuenter Roeck  *	Notifier list for kernel code which wants to be called
140b63adb97SGuenter Roeck  *	to restart the system.
141b63adb97SGuenter Roeck  */
142b63adb97SGuenter Roeck static ATOMIC_NOTIFIER_HEAD(restart_handler_list);
143b63adb97SGuenter Roeck 
144b63adb97SGuenter Roeck /**
145b63adb97SGuenter Roeck  *	register_restart_handler - Register function to be called to reset
146b63adb97SGuenter Roeck  *				   the system
147b63adb97SGuenter Roeck  *	@nb: Info about handler function to be called
148b63adb97SGuenter Roeck  *	@nb->priority:	Handler priority. Handlers should follow the
149b63adb97SGuenter Roeck  *			following guidelines for setting priorities.
150b63adb97SGuenter Roeck  *			0:	Restart handler of last resort,
151b63adb97SGuenter Roeck  *				with limited restart capabilities
152b63adb97SGuenter Roeck  *			128:	Default restart handler; use if no other
153b63adb97SGuenter Roeck  *				restart handler is expected to be available,
154b63adb97SGuenter Roeck  *				and/or if restart functionality is
155b63adb97SGuenter Roeck  *				sufficient to restart the entire system
156b63adb97SGuenter Roeck  *			255:	Highest priority restart handler, will
157b63adb97SGuenter Roeck  *				preempt all other restart handlers
158b63adb97SGuenter Roeck  *
159b63adb97SGuenter Roeck  *	Registers a function with code to be called to restart the
160b63adb97SGuenter Roeck  *	system.
161b63adb97SGuenter Roeck  *
162b63adb97SGuenter Roeck  *	Registered functions will be called from machine_restart as last
163b63adb97SGuenter Roeck  *	step of the restart sequence (if the architecture specific
164b63adb97SGuenter Roeck  *	machine_restart function calls do_kernel_restart - see below
165b63adb97SGuenter Roeck  *	for details).
166b63adb97SGuenter Roeck  *	Registered functions are expected to restart the system immediately.
167b63adb97SGuenter Roeck  *	If more than one function is registered, the restart handler priority
168b63adb97SGuenter Roeck  *	selects which function will be called first.
169b63adb97SGuenter Roeck  *
170b63adb97SGuenter Roeck  *	Restart handlers are expected to be registered from non-architecture
171b63adb97SGuenter Roeck  *	code, typically from drivers. A typical use case would be a system
172b63adb97SGuenter Roeck  *	where restart functionality is provided through a watchdog. Multiple
173b63adb97SGuenter Roeck  *	restart handlers may exist; for example, one restart handler might
174b63adb97SGuenter Roeck  *	restart the entire system, while another only restarts the CPU.
175b63adb97SGuenter Roeck  *	In such cases, the restart handler which only restarts part of the
176b63adb97SGuenter Roeck  *	hardware is expected to register with low priority to ensure that
177b63adb97SGuenter Roeck  *	it only runs if no other means to restart the system is available.
178b63adb97SGuenter Roeck  *
179b63adb97SGuenter Roeck  *	Currently always returns zero, as atomic_notifier_chain_register()
180b63adb97SGuenter Roeck  *	always returns zero.
181b63adb97SGuenter Roeck  */
182b63adb97SGuenter Roeck int register_restart_handler(struct notifier_block *nb)
183b63adb97SGuenter Roeck {
184b63adb97SGuenter Roeck 	return atomic_notifier_chain_register(&restart_handler_list, nb);
185b63adb97SGuenter Roeck }
186b63adb97SGuenter Roeck EXPORT_SYMBOL(register_restart_handler);
187b63adb97SGuenter Roeck 
188b63adb97SGuenter Roeck /**
189b63adb97SGuenter Roeck  *	unregister_restart_handler - Unregister previously registered
190b63adb97SGuenter Roeck  *				     restart handler
191b63adb97SGuenter Roeck  *	@nb: Hook to be unregistered
192b63adb97SGuenter Roeck  *
193b63adb97SGuenter Roeck  *	Unregisters a previously registered restart handler function.
194b63adb97SGuenter Roeck  *
195b63adb97SGuenter Roeck  *	Returns zero on success, or %-ENOENT on failure.
196b63adb97SGuenter Roeck  */
197b63adb97SGuenter Roeck int unregister_restart_handler(struct notifier_block *nb)
198b63adb97SGuenter Roeck {
199b63adb97SGuenter Roeck 	return atomic_notifier_chain_unregister(&restart_handler_list, nb);
200b63adb97SGuenter Roeck }
201b63adb97SGuenter Roeck EXPORT_SYMBOL(unregister_restart_handler);
202b63adb97SGuenter Roeck 
203b63adb97SGuenter Roeck /**
204b63adb97SGuenter Roeck  *	do_kernel_restart - Execute kernel restart handler call chain
205b63adb97SGuenter Roeck  *
206b63adb97SGuenter Roeck  *	Calls functions registered with register_restart_handler.
207b63adb97SGuenter Roeck  *
208b63adb97SGuenter Roeck  *	Expected to be called from machine_restart as last step of the restart
209b63adb97SGuenter Roeck  *	sequence.
210b63adb97SGuenter Roeck  *
211b63adb97SGuenter Roeck  *	Restarts the system immediately if a restart handler function has been
212b63adb97SGuenter Roeck  *	registered. Otherwise does nothing.
213b63adb97SGuenter Roeck  */
214b63adb97SGuenter Roeck void do_kernel_restart(char *cmd)
215b63adb97SGuenter Roeck {
216b63adb97SGuenter Roeck 	atomic_notifier_call_chain(&restart_handler_list, reboot_mode, cmd);
217b63adb97SGuenter Roeck }
218b63adb97SGuenter Roeck 
219c97102baSVivek Goyal void migrate_to_reboot_cpu(void)
22015d94b82SRobin Holt {
22115d94b82SRobin Holt 	/* The boot cpu is always logical cpu 0 */
2221b3a5d02SRobin Holt 	int cpu = reboot_cpu;
22315d94b82SRobin Holt 
22415d94b82SRobin Holt 	cpu_hotplug_disable();
22515d94b82SRobin Holt 
22615d94b82SRobin Holt 	/* Make certain the cpu I'm about to reboot on is online */
22715d94b82SRobin Holt 	if (!cpu_online(cpu))
22815d94b82SRobin Holt 		cpu = cpumask_first(cpu_online_mask);
22915d94b82SRobin Holt 
23015d94b82SRobin Holt 	/* Prevent races with other tasks migrating this task */
23115d94b82SRobin Holt 	current->flags |= PF_NO_SETAFFINITY;
23215d94b82SRobin Holt 
23315d94b82SRobin Holt 	/* Make certain I only run on the appropriate processor */
23415d94b82SRobin Holt 	set_cpus_allowed_ptr(current, cpumask_of(cpu));
23515d94b82SRobin Holt }
23615d94b82SRobin Holt 
23715d94b82SRobin Holt /**
23815d94b82SRobin Holt  *	kernel_restart - reboot the system
23915d94b82SRobin Holt  *	@cmd: pointer to buffer containing command to execute for restart
24015d94b82SRobin Holt  *		or %NULL
24115d94b82SRobin Holt  *
24215d94b82SRobin Holt  *	Shutdown everything and perform a clean reboot.
24315d94b82SRobin Holt  *	This is not safe to call in interrupt context.
24415d94b82SRobin Holt  */
24515d94b82SRobin Holt void kernel_restart(char *cmd)
24615d94b82SRobin Holt {
24715d94b82SRobin Holt 	kernel_restart_prepare(cmd);
24815d94b82SRobin Holt 	migrate_to_reboot_cpu();
24915d94b82SRobin Holt 	syscore_shutdown();
25015d94b82SRobin Holt 	if (!cmd)
251972ee83dSRobin Holt 		pr_emerg("Restarting system\n");
25215d94b82SRobin Holt 	else
253972ee83dSRobin Holt 		pr_emerg("Restarting system with command '%s'\n", cmd);
2546d3cf962SKees Cook 	kmsg_dump(KMSG_DUMP_SHUTDOWN);
25515d94b82SRobin Holt 	machine_restart(cmd);
25615d94b82SRobin Holt }
25715d94b82SRobin Holt EXPORT_SYMBOL_GPL(kernel_restart);
25815d94b82SRobin Holt 
25915d94b82SRobin Holt static void kernel_shutdown_prepare(enum system_states state)
26015d94b82SRobin Holt {
26115d94b82SRobin Holt 	blocking_notifier_call_chain(&reboot_notifier_list,
26215d94b82SRobin Holt 		(state == SYSTEM_HALT) ? SYS_HALT : SYS_POWER_OFF, NULL);
26315d94b82SRobin Holt 	system_state = state;
26415d94b82SRobin Holt 	usermodehelper_disable();
26515d94b82SRobin Holt 	device_shutdown();
26615d94b82SRobin Holt }
26715d94b82SRobin Holt /**
26815d94b82SRobin Holt  *	kernel_halt - halt the system
26915d94b82SRobin Holt  *
27015d94b82SRobin Holt  *	Shutdown everything and perform a clean system halt.
27115d94b82SRobin Holt  */
27215d94b82SRobin Holt void kernel_halt(void)
27315d94b82SRobin Holt {
27415d94b82SRobin Holt 	kernel_shutdown_prepare(SYSTEM_HALT);
27515d94b82SRobin Holt 	migrate_to_reboot_cpu();
27615d94b82SRobin Holt 	syscore_shutdown();
277972ee83dSRobin Holt 	pr_emerg("System halted\n");
2786d3cf962SKees Cook 	kmsg_dump(KMSG_DUMP_SHUTDOWN);
27915d94b82SRobin Holt 	machine_halt();
28015d94b82SRobin Holt }
28115d94b82SRobin Holt EXPORT_SYMBOL_GPL(kernel_halt);
28215d94b82SRobin Holt 
28315d94b82SRobin Holt /**
28415d94b82SRobin Holt  *	kernel_power_off - power_off the system
28515d94b82SRobin Holt  *
28615d94b82SRobin Holt  *	Shutdown everything and perform a clean system power_off.
28715d94b82SRobin Holt  */
28815d94b82SRobin Holt void kernel_power_off(void)
28915d94b82SRobin Holt {
29015d94b82SRobin Holt 	kernel_shutdown_prepare(SYSTEM_POWER_OFF);
29115d94b82SRobin Holt 	if (pm_power_off_prepare)
29215d94b82SRobin Holt 		pm_power_off_prepare();
29315d94b82SRobin Holt 	migrate_to_reboot_cpu();
29415d94b82SRobin Holt 	syscore_shutdown();
295972ee83dSRobin Holt 	pr_emerg("Power down\n");
2966d3cf962SKees Cook 	kmsg_dump(KMSG_DUMP_SHUTDOWN);
29715d94b82SRobin Holt 	machine_power_off();
29815d94b82SRobin Holt }
29915d94b82SRobin Holt EXPORT_SYMBOL_GPL(kernel_power_off);
30015d94b82SRobin Holt 
30155f2503cSPingfan Liu DEFINE_MUTEX(system_transition_mutex);
30215d94b82SRobin Holt 
30315d94b82SRobin Holt /*
30415d94b82SRobin Holt  * Reboot system call: for obvious reasons only root may call it,
30515d94b82SRobin Holt  * and even root needs to set up some magic numbers in the registers
30615d94b82SRobin Holt  * so that some mistake won't make this reboot the whole machine.
30715d94b82SRobin Holt  * You can also set the meaning of the ctrl-alt-del-key here.
30815d94b82SRobin Holt  *
30915d94b82SRobin Holt  * reboot doesn't sync: do that yourself before calling this.
31015d94b82SRobin Holt  */
31115d94b82SRobin Holt SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
31215d94b82SRobin Holt 		void __user *, arg)
31315d94b82SRobin Holt {
31415d94b82SRobin Holt 	struct pid_namespace *pid_ns = task_active_pid_ns(current);
31515d94b82SRobin Holt 	char buffer[256];
31615d94b82SRobin Holt 	int ret = 0;
31715d94b82SRobin Holt 
31815d94b82SRobin Holt 	/* We only trust the superuser with rebooting the system. */
31915d94b82SRobin Holt 	if (!ns_capable(pid_ns->user_ns, CAP_SYS_BOOT))
32015d94b82SRobin Holt 		return -EPERM;
32115d94b82SRobin Holt 
32215d94b82SRobin Holt 	/* For safety, we require "magic" arguments. */
32315d94b82SRobin Holt 	if (magic1 != LINUX_REBOOT_MAGIC1 ||
32415d94b82SRobin Holt 			(magic2 != LINUX_REBOOT_MAGIC2 &&
32515d94b82SRobin Holt 			magic2 != LINUX_REBOOT_MAGIC2A &&
32615d94b82SRobin Holt 			magic2 != LINUX_REBOOT_MAGIC2B &&
32715d94b82SRobin Holt 			magic2 != LINUX_REBOOT_MAGIC2C))
32815d94b82SRobin Holt 		return -EINVAL;
32915d94b82SRobin Holt 
33015d94b82SRobin Holt 	/*
33115d94b82SRobin Holt 	 * If pid namespaces are enabled and the current task is in a child
33215d94b82SRobin Holt 	 * pid_namespace, the command is handled by reboot_pid_ns() which will
33315d94b82SRobin Holt 	 * call do_exit().
33415d94b82SRobin Holt 	 */
33515d94b82SRobin Holt 	ret = reboot_pid_ns(pid_ns, cmd);
33615d94b82SRobin Holt 	if (ret)
33715d94b82SRobin Holt 		return ret;
33815d94b82SRobin Holt 
33915d94b82SRobin Holt 	/* Instead of trying to make the power_off code look like
34015d94b82SRobin Holt 	 * halt when pm_power_off is not set do it the easy way.
34115d94b82SRobin Holt 	 */
34215d94b82SRobin Holt 	if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
34315d94b82SRobin Holt 		cmd = LINUX_REBOOT_CMD_HALT;
34415d94b82SRobin Holt 
34555f2503cSPingfan Liu 	mutex_lock(&system_transition_mutex);
34615d94b82SRobin Holt 	switch (cmd) {
34715d94b82SRobin Holt 	case LINUX_REBOOT_CMD_RESTART:
34815d94b82SRobin Holt 		kernel_restart(NULL);
34915d94b82SRobin Holt 		break;
35015d94b82SRobin Holt 
35115d94b82SRobin Holt 	case LINUX_REBOOT_CMD_CAD_ON:
35215d94b82SRobin Holt 		C_A_D = 1;
35315d94b82SRobin Holt 		break;
35415d94b82SRobin Holt 
35515d94b82SRobin Holt 	case LINUX_REBOOT_CMD_CAD_OFF:
35615d94b82SRobin Holt 		C_A_D = 0;
35715d94b82SRobin Holt 		break;
35815d94b82SRobin Holt 
35915d94b82SRobin Holt 	case LINUX_REBOOT_CMD_HALT:
36015d94b82SRobin Holt 		kernel_halt();
36115d94b82SRobin Holt 		do_exit(0);
36215d94b82SRobin Holt 		panic("cannot halt");
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 
42015d94b82SRobin Holt char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
4217a54f46bSJoel Stanley static const char reboot_cmd[] = "/sbin/reboot";
42215d94b82SRobin Holt 
4237a54f46bSJoel Stanley static int run_cmd(const char *cmd)
42415d94b82SRobin Holt {
42515d94b82SRobin Holt 	char **argv;
42615d94b82SRobin Holt 	static char *envp[] = {
42715d94b82SRobin Holt 		"HOME=/",
42815d94b82SRobin Holt 		"PATH=/sbin:/bin:/usr/sbin:/usr/bin",
42915d94b82SRobin Holt 		NULL
43015d94b82SRobin Holt 	};
43115d94b82SRobin Holt 	int ret;
4327a54f46bSJoel Stanley 	argv = argv_split(GFP_KERNEL, cmd, NULL);
43315d94b82SRobin Holt 	if (argv) {
43415d94b82SRobin Holt 		ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
43515d94b82SRobin Holt 		argv_free(argv);
43615d94b82SRobin Holt 	} else {
43715d94b82SRobin Holt 		ret = -ENOMEM;
43815d94b82SRobin Holt 	}
43915d94b82SRobin Holt 
4407a54f46bSJoel Stanley 	return ret;
4417a54f46bSJoel Stanley }
4427a54f46bSJoel Stanley 
4437a54f46bSJoel Stanley static int __orderly_reboot(void)
4447a54f46bSJoel Stanley {
4457a54f46bSJoel Stanley 	int ret;
4467a54f46bSJoel Stanley 
4477a54f46bSJoel Stanley 	ret = run_cmd(reboot_cmd);
4487a54f46bSJoel Stanley 
4497a54f46bSJoel Stanley 	if (ret) {
4507a54f46bSJoel Stanley 		pr_warn("Failed to start orderly reboot: forcing the issue\n");
4517a54f46bSJoel Stanley 		emergency_sync();
4527a54f46bSJoel Stanley 		kernel_restart(NULL);
4537a54f46bSJoel Stanley 	}
4547a54f46bSJoel Stanley 
4557a54f46bSJoel Stanley 	return ret;
4567a54f46bSJoel Stanley }
4577a54f46bSJoel Stanley 
4587a54f46bSJoel Stanley static int __orderly_poweroff(bool force)
4597a54f46bSJoel Stanley {
4607a54f46bSJoel Stanley 	int ret;
4617a54f46bSJoel Stanley 
4627a54f46bSJoel Stanley 	ret = run_cmd(poweroff_cmd);
4637a54f46bSJoel Stanley 
46415d94b82SRobin Holt 	if (ret && force) {
465972ee83dSRobin Holt 		pr_warn("Failed to start orderly shutdown: forcing the issue\n");
4667a54f46bSJoel Stanley 
46715d94b82SRobin Holt 		/*
46815d94b82SRobin Holt 		 * I guess this should try to kick off some daemon to sync and
46915d94b82SRobin Holt 		 * poweroff asap.  Or not even bother syncing if we're doing an
47015d94b82SRobin Holt 		 * emergency shutdown?
47115d94b82SRobin Holt 		 */
47215d94b82SRobin Holt 		emergency_sync();
47315d94b82SRobin Holt 		kernel_power_off();
47415d94b82SRobin Holt 	}
47515d94b82SRobin Holt 
47615d94b82SRobin Holt 	return ret;
47715d94b82SRobin Holt }
47815d94b82SRobin Holt 
47915d94b82SRobin Holt static bool poweroff_force;
48015d94b82SRobin Holt 
48115d94b82SRobin Holt static void poweroff_work_func(struct work_struct *work)
48215d94b82SRobin Holt {
48315d94b82SRobin Holt 	__orderly_poweroff(poweroff_force);
48415d94b82SRobin Holt }
48515d94b82SRobin Holt 
48615d94b82SRobin Holt static DECLARE_WORK(poweroff_work, poweroff_work_func);
48715d94b82SRobin Holt 
48815d94b82SRobin Holt /**
48915d94b82SRobin Holt  * orderly_poweroff - Trigger an orderly system poweroff
49015d94b82SRobin Holt  * @force: force poweroff if command execution fails
49115d94b82SRobin Holt  *
49215d94b82SRobin Holt  * This may be called from any context to trigger a system shutdown.
49315d94b82SRobin Holt  * If the orderly shutdown fails, it will force an immediate shutdown.
49415d94b82SRobin Holt  */
4957a54f46bSJoel Stanley void orderly_poweroff(bool force)
49615d94b82SRobin Holt {
49715d94b82SRobin Holt 	if (force) /* do not override the pending "true" */
49815d94b82SRobin Holt 		poweroff_force = true;
49915d94b82SRobin Holt 	schedule_work(&poweroff_work);
50015d94b82SRobin Holt }
50115d94b82SRobin Holt EXPORT_SYMBOL_GPL(orderly_poweroff);
5021b3a5d02SRobin Holt 
5037a54f46bSJoel Stanley static void reboot_work_func(struct work_struct *work)
5047a54f46bSJoel Stanley {
5057a54f46bSJoel Stanley 	__orderly_reboot();
5067a54f46bSJoel Stanley }
5077a54f46bSJoel Stanley 
5087a54f46bSJoel Stanley static DECLARE_WORK(reboot_work, reboot_work_func);
5097a54f46bSJoel Stanley 
5107a54f46bSJoel Stanley /**
5117a54f46bSJoel Stanley  * orderly_reboot - Trigger an orderly system reboot
5127a54f46bSJoel Stanley  *
5137a54f46bSJoel Stanley  * This may be called from any context to trigger a system reboot.
5147a54f46bSJoel Stanley  * If the orderly reboot fails, it will force an immediate reboot.
5157a54f46bSJoel Stanley  */
5167a54f46bSJoel Stanley void orderly_reboot(void)
5177a54f46bSJoel Stanley {
5187a54f46bSJoel Stanley 	schedule_work(&reboot_work);
5197a54f46bSJoel Stanley }
5207a54f46bSJoel Stanley EXPORT_SYMBOL_GPL(orderly_reboot);
5217a54f46bSJoel Stanley 
5221b3a5d02SRobin Holt static int __init reboot_setup(char *str)
5231b3a5d02SRobin Holt {
5241b3a5d02SRobin Holt 	for (;;) {
525b287a25aSAaro Koskinen 		enum reboot_mode *mode;
526b287a25aSAaro Koskinen 
5271b3a5d02SRobin Holt 		/*
5281b3a5d02SRobin Holt 		 * Having anything passed on the command line via
5291b3a5d02SRobin Holt 		 * reboot= will cause us to disable DMI checking
5301b3a5d02SRobin Holt 		 * below.
5311b3a5d02SRobin Holt 		 */
5321b3a5d02SRobin Holt 		reboot_default = 0;
5331b3a5d02SRobin Holt 
534b287a25aSAaro Koskinen 		if (!strncmp(str, "panic_", 6)) {
535b287a25aSAaro Koskinen 			mode = &panic_reboot_mode;
536b287a25aSAaro Koskinen 			str += 6;
537b287a25aSAaro Koskinen 		} else {
538b287a25aSAaro Koskinen 			mode = &reboot_mode;
539b287a25aSAaro Koskinen 		}
540b287a25aSAaro Koskinen 
5411b3a5d02SRobin Holt 		switch (*str) {
5421b3a5d02SRobin Holt 		case 'w':
543b287a25aSAaro Koskinen 			*mode = REBOOT_WARM;
5441b3a5d02SRobin Holt 			break;
5451b3a5d02SRobin Holt 
5461b3a5d02SRobin Holt 		case 'c':
547b287a25aSAaro Koskinen 			*mode = REBOOT_COLD;
5481b3a5d02SRobin Holt 			break;
5491b3a5d02SRobin Holt 
5501b3a5d02SRobin Holt 		case 'h':
551b287a25aSAaro Koskinen 			*mode = REBOOT_HARD;
5521b3a5d02SRobin Holt 			break;
5531b3a5d02SRobin Holt 
5541b3a5d02SRobin Holt 		case 's':
555f9a90501SMatteo Croce 			/*
556f9a90501SMatteo Croce 			 * reboot_cpu is s[mp]#### with #### being the processor
557f9a90501SMatteo Croce 			 * to be used for rebooting. Skip 's' or 'smp' prefix.
558f9a90501SMatteo Croce 			 */
559f9a90501SMatteo Croce 			str += str[1] == 'm' && str[2] == 'p' ? 3 : 1;
560f9a90501SMatteo Croce 
561f9a90501SMatteo Croce 			if (isdigit(str[0])) {
562f9a90501SMatteo Croce 				int cpu = simple_strtoul(str, NULL, 0);
563f9a90501SMatteo Croce 
564f9a90501SMatteo Croce 				if (cpu >= num_possible_cpus()) {
565df5b0ab3SMatteo Croce 					pr_err("Ignoring the CPU number in reboot= option. "
566df5b0ab3SMatteo Croce 					"CPU %d exceeds possible cpu number %d\n",
567f9a90501SMatteo Croce 					cpu, num_possible_cpus());
568df5b0ab3SMatteo Croce 					break;
569df5b0ab3SMatteo Croce 				}
570f9a90501SMatteo Croce 				reboot_cpu = cpu;
571f9a90501SMatteo Croce 			} else
572f9a90501SMatteo Croce 				*mode = REBOOT_SOFT;
5731b3a5d02SRobin Holt 			break;
5748b92c4ffSMatteo Croce 
5751b3a5d02SRobin Holt 		case 'g':
576b287a25aSAaro Koskinen 			*mode = REBOOT_GPIO;
5771b3a5d02SRobin Holt 			break;
5781b3a5d02SRobin Holt 
5791b3a5d02SRobin Holt 		case 'b':
5801b3a5d02SRobin Holt 		case 'a':
5811b3a5d02SRobin Holt 		case 'k':
5821b3a5d02SRobin Holt 		case 't':
5831b3a5d02SRobin Holt 		case 'e':
5841b3a5d02SRobin Holt 		case 'p':
5851b3a5d02SRobin Holt 			reboot_type = *str;
5861b3a5d02SRobin Holt 			break;
5871b3a5d02SRobin Holt 
5881b3a5d02SRobin Holt 		case 'f':
5891b3a5d02SRobin Holt 			reboot_force = 1;
5901b3a5d02SRobin Holt 			break;
5911b3a5d02SRobin Holt 		}
5921b3a5d02SRobin Holt 
5931b3a5d02SRobin Holt 		str = strchr(str, ',');
5941b3a5d02SRobin Holt 		if (str)
5951b3a5d02SRobin Holt 			str++;
5961b3a5d02SRobin Holt 		else
5971b3a5d02SRobin Holt 			break;
5981b3a5d02SRobin Holt 	}
5991b3a5d02SRobin Holt 	return 1;
6001b3a5d02SRobin Holt }
6011b3a5d02SRobin Holt __setup("reboot=", reboot_setup);
6022c622ed0SMatteo Croce 
6032c622ed0SMatteo Croce #ifdef CONFIG_SYSFS
6042c622ed0SMatteo Croce 
6052c622ed0SMatteo Croce #define REBOOT_COLD_STR		"cold"
6062c622ed0SMatteo Croce #define REBOOT_WARM_STR		"warm"
6072c622ed0SMatteo Croce #define REBOOT_HARD_STR		"hard"
6082c622ed0SMatteo Croce #define REBOOT_SOFT_STR		"soft"
6092c622ed0SMatteo Croce #define REBOOT_GPIO_STR		"gpio"
6102c622ed0SMatteo Croce #define REBOOT_UNDEFINED_STR	"undefined"
6112c622ed0SMatteo Croce 
6122c622ed0SMatteo Croce #define BOOT_TRIPLE_STR		"triple"
6132c622ed0SMatteo Croce #define BOOT_KBD_STR		"kbd"
6142c622ed0SMatteo Croce #define BOOT_BIOS_STR		"bios"
6152c622ed0SMatteo Croce #define BOOT_ACPI_STR		"acpi"
6162c622ed0SMatteo Croce #define BOOT_EFI_STR		"efi"
6170c5c0179SMatteo Croce #define BOOT_PCI_STR		"pci"
6182c622ed0SMatteo Croce 
6192c622ed0SMatteo Croce static ssize_t mode_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
6202c622ed0SMatteo Croce {
6212c622ed0SMatteo Croce 	const char *val;
6222c622ed0SMatteo Croce 
6232c622ed0SMatteo Croce 	switch (reboot_mode) {
6242c622ed0SMatteo Croce 	case REBOOT_COLD:
6252c622ed0SMatteo Croce 		val = REBOOT_COLD_STR;
6262c622ed0SMatteo Croce 		break;
6272c622ed0SMatteo Croce 	case REBOOT_WARM:
6282c622ed0SMatteo Croce 		val = REBOOT_WARM_STR;
6292c622ed0SMatteo Croce 		break;
6302c622ed0SMatteo Croce 	case REBOOT_HARD:
6312c622ed0SMatteo Croce 		val = REBOOT_HARD_STR;
6322c622ed0SMatteo Croce 		break;
6332c622ed0SMatteo Croce 	case REBOOT_SOFT:
6342c622ed0SMatteo Croce 		val = REBOOT_SOFT_STR;
6352c622ed0SMatteo Croce 		break;
6362c622ed0SMatteo Croce 	case REBOOT_GPIO:
6372c622ed0SMatteo Croce 		val = REBOOT_GPIO_STR;
6382c622ed0SMatteo Croce 		break;
6392c622ed0SMatteo Croce 	default:
6402c622ed0SMatteo Croce 		val = REBOOT_UNDEFINED_STR;
6412c622ed0SMatteo Croce 	}
6422c622ed0SMatteo Croce 
6432c622ed0SMatteo Croce 	return sprintf(buf, "%s\n", val);
6442c622ed0SMatteo Croce }
6452c622ed0SMatteo Croce static ssize_t mode_store(struct kobject *kobj, struct kobj_attribute *attr,
6462c622ed0SMatteo Croce 			  const char *buf, size_t count)
6472c622ed0SMatteo Croce {
6482c622ed0SMatteo Croce 	if (!capable(CAP_SYS_BOOT))
6492c622ed0SMatteo Croce 		return -EPERM;
6502c622ed0SMatteo Croce 
6512c622ed0SMatteo Croce 	if (!strncmp(buf, REBOOT_COLD_STR, strlen(REBOOT_COLD_STR)))
6522c622ed0SMatteo Croce 		reboot_mode = REBOOT_COLD;
6532c622ed0SMatteo Croce 	else if (!strncmp(buf, REBOOT_WARM_STR, strlen(REBOOT_WARM_STR)))
6542c622ed0SMatteo Croce 		reboot_mode = REBOOT_WARM;
6552c622ed0SMatteo Croce 	else if (!strncmp(buf, REBOOT_HARD_STR, strlen(REBOOT_HARD_STR)))
6562c622ed0SMatteo Croce 		reboot_mode = REBOOT_HARD;
6572c622ed0SMatteo Croce 	else if (!strncmp(buf, REBOOT_SOFT_STR, strlen(REBOOT_SOFT_STR)))
6582c622ed0SMatteo Croce 		reboot_mode = REBOOT_SOFT;
6592c622ed0SMatteo Croce 	else if (!strncmp(buf, REBOOT_GPIO_STR, strlen(REBOOT_GPIO_STR)))
6602c622ed0SMatteo Croce 		reboot_mode = REBOOT_GPIO;
6612c622ed0SMatteo Croce 	else
6622c622ed0SMatteo Croce 		return -EINVAL;
6632c622ed0SMatteo Croce 
6641a9d079fSMatteo Croce 	reboot_default = 0;
6651a9d079fSMatteo Croce 
6662c622ed0SMatteo Croce 	return count;
6672c622ed0SMatteo Croce }
6682c622ed0SMatteo Croce static struct kobj_attribute reboot_mode_attr = __ATTR_RW(mode);
6692c622ed0SMatteo Croce 
67040247e55SMatteo Croce #ifdef CONFIG_X86
67140247e55SMatteo Croce static ssize_t force_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
67240247e55SMatteo Croce {
67340247e55SMatteo Croce 	return sprintf(buf, "%d\n", reboot_force);
67440247e55SMatteo Croce }
67540247e55SMatteo Croce static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr,
67640247e55SMatteo Croce 			  const char *buf, size_t count)
67740247e55SMatteo Croce {
67840247e55SMatteo Croce 	bool res;
67940247e55SMatteo Croce 
68040247e55SMatteo Croce 	if (!capable(CAP_SYS_BOOT))
68140247e55SMatteo Croce 		return -EPERM;
68240247e55SMatteo Croce 
68340247e55SMatteo Croce 	if (kstrtobool(buf, &res))
68440247e55SMatteo Croce 		return -EINVAL;
68540247e55SMatteo Croce 
68640247e55SMatteo Croce 	reboot_default = 0;
68740247e55SMatteo Croce 	reboot_force = res;
68840247e55SMatteo Croce 
68940247e55SMatteo Croce 	return count;
69040247e55SMatteo Croce }
69140247e55SMatteo Croce static struct kobj_attribute reboot_force_attr = __ATTR_RW(force);
69240247e55SMatteo Croce 
6932c622ed0SMatteo Croce static ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
6942c622ed0SMatteo Croce {
6952c622ed0SMatteo Croce 	const char *val;
6962c622ed0SMatteo Croce 
6972c622ed0SMatteo Croce 	switch (reboot_type) {
6982c622ed0SMatteo Croce 	case BOOT_TRIPLE:
6992c622ed0SMatteo Croce 		val = BOOT_TRIPLE_STR;
7002c622ed0SMatteo Croce 		break;
7012c622ed0SMatteo Croce 	case BOOT_KBD:
7022c622ed0SMatteo Croce 		val = BOOT_KBD_STR;
7032c622ed0SMatteo Croce 		break;
7042c622ed0SMatteo Croce 	case BOOT_BIOS:
7052c622ed0SMatteo Croce 		val = BOOT_BIOS_STR;
7062c622ed0SMatteo Croce 		break;
7072c622ed0SMatteo Croce 	case BOOT_ACPI:
7082c622ed0SMatteo Croce 		val = BOOT_ACPI_STR;
7092c622ed0SMatteo Croce 		break;
7102c622ed0SMatteo Croce 	case BOOT_EFI:
7112c622ed0SMatteo Croce 		val = BOOT_EFI_STR;
7122c622ed0SMatteo Croce 		break;
7132c622ed0SMatteo Croce 	case BOOT_CF9_FORCE:
7140c5c0179SMatteo Croce 		val = BOOT_PCI_STR;
7152c622ed0SMatteo Croce 		break;
7162c622ed0SMatteo Croce 	default:
7172c622ed0SMatteo Croce 		val = REBOOT_UNDEFINED_STR;
7182c622ed0SMatteo Croce 	}
7192c622ed0SMatteo Croce 
7202c622ed0SMatteo Croce 	return sprintf(buf, "%s\n", val);
7212c622ed0SMatteo Croce }
7222c622ed0SMatteo Croce static ssize_t type_store(struct kobject *kobj, struct kobj_attribute *attr,
7232c622ed0SMatteo Croce 			  const char *buf, size_t count)
7242c622ed0SMatteo Croce {
7252c622ed0SMatteo Croce 	if (!capable(CAP_SYS_BOOT))
7262c622ed0SMatteo Croce 		return -EPERM;
7272c622ed0SMatteo Croce 
7282c622ed0SMatteo Croce 	if (!strncmp(buf, BOOT_TRIPLE_STR, strlen(BOOT_TRIPLE_STR)))
7292c622ed0SMatteo Croce 		reboot_type = BOOT_TRIPLE;
7302c622ed0SMatteo Croce 	else if (!strncmp(buf, BOOT_KBD_STR, strlen(BOOT_KBD_STR)))
7312c622ed0SMatteo Croce 		reboot_type = BOOT_KBD;
7322c622ed0SMatteo Croce 	else if (!strncmp(buf, BOOT_BIOS_STR, strlen(BOOT_BIOS_STR)))
7332c622ed0SMatteo Croce 		reboot_type = BOOT_BIOS;
7342c622ed0SMatteo Croce 	else if (!strncmp(buf, BOOT_ACPI_STR, strlen(BOOT_ACPI_STR)))
7352c622ed0SMatteo Croce 		reboot_type = BOOT_ACPI;
7362c622ed0SMatteo Croce 	else if (!strncmp(buf, BOOT_EFI_STR, strlen(BOOT_EFI_STR)))
7372c622ed0SMatteo Croce 		reboot_type = BOOT_EFI;
7380c5c0179SMatteo Croce 	else if (!strncmp(buf, BOOT_PCI_STR, strlen(BOOT_PCI_STR)))
7392c622ed0SMatteo Croce 		reboot_type = BOOT_CF9_FORCE;
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_type_attr = __ATTR_RW(type);
74840247e55SMatteo Croce #endif
7492c622ed0SMatteo Croce 
75040247e55SMatteo Croce #ifdef CONFIG_SMP
7512c622ed0SMatteo Croce static ssize_t cpu_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
7522c622ed0SMatteo Croce {
7532c622ed0SMatteo Croce 	return sprintf(buf, "%d\n", reboot_cpu);
7542c622ed0SMatteo Croce }
7552c622ed0SMatteo Croce static ssize_t cpu_store(struct kobject *kobj, struct kobj_attribute *attr,
7562c622ed0SMatteo Croce 			  const char *buf, size_t count)
7572c622ed0SMatteo Croce {
7582c622ed0SMatteo Croce 	unsigned int cpunum;
7592c622ed0SMatteo Croce 	int rc;
7602c622ed0SMatteo Croce 
7612c622ed0SMatteo Croce 	if (!capable(CAP_SYS_BOOT))
7622c622ed0SMatteo Croce 		return -EPERM;
7632c622ed0SMatteo Croce 
7642c622ed0SMatteo Croce 	rc = kstrtouint(buf, 0, &cpunum);
7652c622ed0SMatteo Croce 
7662c622ed0SMatteo Croce 	if (rc)
7672c622ed0SMatteo Croce 		return rc;
7682c622ed0SMatteo Croce 
7692c622ed0SMatteo Croce 	if (cpunum >= num_possible_cpus())
7702c622ed0SMatteo Croce 		return -ERANGE;
7712c622ed0SMatteo Croce 
7721a9d079fSMatteo Croce 	reboot_default = 0;
7732c622ed0SMatteo Croce 	reboot_cpu = cpunum;
7742c622ed0SMatteo Croce 
7752c622ed0SMatteo Croce 	return count;
7762c622ed0SMatteo Croce }
7772c622ed0SMatteo Croce static struct kobj_attribute reboot_cpu_attr = __ATTR_RW(cpu);
77840247e55SMatteo Croce #endif
7792c622ed0SMatteo Croce 
7802c622ed0SMatteo Croce static struct attribute *reboot_attrs[] = {
7812c622ed0SMatteo Croce 	&reboot_mode_attr.attr,
78240247e55SMatteo Croce #ifdef CONFIG_X86
7832c622ed0SMatteo Croce 	&reboot_force_attr.attr,
78440247e55SMatteo Croce 	&reboot_type_attr.attr,
78540247e55SMatteo Croce #endif
78640247e55SMatteo Croce #ifdef CONFIG_SMP
78740247e55SMatteo Croce 	&reboot_cpu_attr.attr,
78840247e55SMatteo Croce #endif
7892c622ed0SMatteo Croce 	NULL,
7902c622ed0SMatteo Croce };
7912c622ed0SMatteo Croce 
7922c622ed0SMatteo Croce static const struct attribute_group reboot_attr_group = {
7932c622ed0SMatteo Croce 	.attrs = reboot_attrs,
7942c622ed0SMatteo Croce };
7952c622ed0SMatteo Croce 
7962c622ed0SMatteo Croce static int __init reboot_ksysfs_init(void)
7972c622ed0SMatteo Croce {
7982c622ed0SMatteo Croce 	struct kobject *reboot_kobj;
7992c622ed0SMatteo Croce 	int ret;
8002c622ed0SMatteo Croce 
8012c622ed0SMatteo Croce 	reboot_kobj = kobject_create_and_add("reboot", kernel_kobj);
8022c622ed0SMatteo Croce 	if (!reboot_kobj)
8032c622ed0SMatteo Croce 		return -ENOMEM;
8042c622ed0SMatteo Croce 
8052c622ed0SMatteo Croce 	ret = sysfs_create_group(reboot_kobj, &reboot_attr_group);
8062c622ed0SMatteo Croce 	if (ret) {
8072c622ed0SMatteo Croce 		kobject_put(reboot_kobj);
8082c622ed0SMatteo Croce 		return ret;
8092c622ed0SMatteo Croce 	}
8102c622ed0SMatteo Croce 
8112c622ed0SMatteo Croce 	return 0;
8122c622ed0SMatteo Croce }
8132c622ed0SMatteo Croce late_initcall(reboot_ksysfs_init);
8142c622ed0SMatteo Croce 
8152c622ed0SMatteo Croce #endif
816