2 * linux/kernel/reboot.c
4 * Copyright (C) 2013 Linus Torvalds
7 #define pr_fmt(fmt) "reboot: " fmt
9 #include <linux/ctype.h>
10 #include <linux/export.h>
11 #include <linux/kexec.h>
12 #include <linux/kmod.h>
13 #include <linux/kmsg_dump.h>
14 #include <linux/reboot.h>
15 #include <linux/suspend.h>
16 #include <linux/syscalls.h>
17 #include <linux/syscore_ops.h>
18 #include <linux/uaccess.h>
19 #include <linux/freezer.h>
22 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
27 EXPORT_SYMBOL(cad_pid);
29 #if defined(CONFIG_ARM) || defined(CONFIG_UNICORE32)
30 #define DEFAULT_REBOOT_MODE = REBOOT_HARD
32 #define DEFAULT_REBOOT_MODE
34 enum reboot_mode reboot_mode DEFAULT_REBOOT_MODE;
37 * This variable is used privately to keep track of whether or not
38 * reboot_type is still set to its default value (i.e., reboot= hasn't
39 * been set on the command line). This is needed so that we can
40 * suppress DMI scanning for reboot quirks. Without it, it's
41 * impossible to override a faulty reboot quirk without recompiling.
43 int reboot_default = 1;
45 enum reboot_type reboot_type = BOOT_ACPI;
49 * If set, this is used for preparing the system to power off.
52 void (*pm_power_off_prepare)(void);
55 * emergency_restart - reboot the system
57 * Without shutting down any hardware or taking any locks
58 * reboot the system. This is called when we know we are in
59 * trouble so this is our best effort to reboot. This is
60 * safe to call in interrupt context.
62 void emergency_restart(void)
64 kmsg_dump(KMSG_DUMP_EMERG);
65 machine_emergency_restart();
67 EXPORT_SYMBOL_GPL(emergency_restart);
69 void kernel_restart_prepare(char *cmd)
71 blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
72 system_state = SYSTEM_RESTART;
73 usermodehelper_disable();
78 * register_reboot_notifier - Register function to be called at reboot time
79 * @nb: Info about notifier function to be called
81 * Registers a function with the list of functions
82 * to be called at reboot time.
84 * Currently always returns zero, as blocking_notifier_chain_register()
85 * always returns zero.
87 int register_reboot_notifier(struct notifier_block *nb)
89 return blocking_notifier_chain_register(&reboot_notifier_list, nb);
91 EXPORT_SYMBOL(register_reboot_notifier);
94 * unregister_reboot_notifier - Unregister previously registered reboot notifier
95 * @nb: Hook to be unregistered
97 * Unregisters a previously registered reboot
100 * Returns zero on success, or %-ENOENT on failure.
102 int unregister_reboot_notifier(struct notifier_block *nb)
104 return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
106 EXPORT_SYMBOL(unregister_reboot_notifier);
109 * Notifier list for kernel code which wants to be called
110 * to restart the system.
112 static ATOMIC_NOTIFIER_HEAD(restart_handler_list);
115 * register_restart_handler - Register function to be called to reset
117 * @nb: Info about handler function to be called
118 * @nb->priority: Handler priority. Handlers should follow the
119 * following guidelines for setting priorities.
120 * 0: Restart handler of last resort,
121 * with limited restart capabilities
122 * 128: Default restart handler; use if no other
123 * restart handler is expected to be available,
124 * and/or if restart functionality is
125 * sufficient to restart the entire system
126 * 255: Highest priority restart handler, will
127 * preempt all other restart handlers
129 * Registers a function with code to be called to restart the
132 * Registered functions will be called from machine_restart as last
133 * step of the restart sequence (if the architecture specific
134 * machine_restart function calls do_kernel_restart - see below
136 * Registered functions are expected to restart the system immediately.
137 * If more than one function is registered, the restart handler priority
138 * selects which function will be called first.
140 * Restart handlers are expected to be registered from non-architecture
141 * code, typically from drivers. A typical use case would be a system
142 * where restart functionality is provided through a watchdog. Multiple
143 * restart handlers may exist; for example, one restart handler might
144 * restart the entire system, while another only restarts the CPU.
145 * In such cases, the restart handler which only restarts part of the
146 * hardware is expected to register with low priority to ensure that
147 * it only runs if no other means to restart the system is available.
149 * Currently always returns zero, as atomic_notifier_chain_register()
150 * always returns zero.
152 int register_restart_handler(struct notifier_block *nb)
154 return atomic_notifier_chain_register(&restart_handler_list, nb);
156 EXPORT_SYMBOL(register_restart_handler);
159 * unregister_restart_handler - Unregister previously registered
161 * @nb: Hook to be unregistered
163 * Unregisters a previously registered restart handler function.
165 * Returns zero on success, or %-ENOENT on failure.
167 int unregister_restart_handler(struct notifier_block *nb)
169 return atomic_notifier_chain_unregister(&restart_handler_list, nb);
171 EXPORT_SYMBOL(unregister_restart_handler);
174 * do_kernel_restart - Execute kernel restart handler call chain
176 * Calls functions registered with register_restart_handler.
178 * Expected to be called from machine_restart as last step of the restart
181 * Restarts the system immediately if a restart handler function has been
182 * registered. Otherwise does nothing.
184 void do_kernel_restart(char *cmd)
186 atomic_notifier_call_chain(&restart_handler_list, reboot_mode, cmd);
189 void migrate_to_reboot_cpu(void)
191 /* The boot cpu is always logical cpu 0 */
192 int cpu = reboot_cpu;
194 cpu_hotplug_disable();
196 /* Make certain the cpu I'm about to reboot on is online */
197 if (!cpu_online(cpu))
198 cpu = cpumask_first(cpu_online_mask);
200 /* Prevent races with other tasks migrating this task */
201 current->flags |= PF_NO_SETAFFINITY;
203 /* Make certain I only run on the appropriate processor */
204 set_cpus_allowed_ptr(current, cpumask_of(cpu));
208 * kernel_restart - reboot the system
209 * @cmd: pointer to buffer containing command to execute for restart
212 * Shutdown everything and perform a clean reboot.
213 * This is not safe to call in interrupt context.
215 void kernel_restart(char *cmd)
217 freeze_processes_ignore_wakeup();
218 kernel_restart_prepare(cmd);
219 migrate_to_reboot_cpu();
222 pr_emerg("Restarting system\n");
224 pr_emerg("Restarting system with command '%s'\n", cmd);
225 kmsg_dump(KMSG_DUMP_RESTART);
226 machine_restart(cmd);
228 EXPORT_SYMBOL_GPL(kernel_restart);
230 static void kernel_shutdown_prepare(enum system_states state)
232 blocking_notifier_call_chain(&reboot_notifier_list,
233 (state == SYSTEM_HALT) ? SYS_HALT : SYS_POWER_OFF, NULL);
234 system_state = state;
235 usermodehelper_disable();
239 * kernel_halt - halt the system
241 * Shutdown everything and perform a clean system halt.
243 void kernel_halt(void)
245 kernel_shutdown_prepare(SYSTEM_HALT);
246 migrate_to_reboot_cpu();
248 pr_emerg("System halted\n");
249 kmsg_dump(KMSG_DUMP_HALT);
252 EXPORT_SYMBOL_GPL(kernel_halt);
255 * kernel_power_off - power_off the system
257 * Shutdown everything and perform a clean system power_off.
259 void kernel_power_off(void)
261 freeze_processes_ignore_wakeup();
262 kernel_shutdown_prepare(SYSTEM_POWER_OFF);
263 if (pm_power_off_prepare)
264 pm_power_off_prepare();
265 migrate_to_reboot_cpu();
267 pr_emerg("Power down\n");
268 kmsg_dump(KMSG_DUMP_POWEROFF);
271 EXPORT_SYMBOL_GPL(kernel_power_off);
273 static DEFINE_MUTEX(reboot_mutex);
276 * Reboot system call: for obvious reasons only root may call it,
277 * and even root needs to set up some magic numbers in the registers
278 * so that some mistake won't make this reboot the whole machine.
279 * You can also set the meaning of the ctrl-alt-del-key here.
281 * reboot doesn't sync: do that yourself before calling this.
283 SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
286 struct pid_namespace *pid_ns = task_active_pid_ns(current);
290 /* We only trust the superuser with rebooting the system. */
291 if (!ns_capable(pid_ns->user_ns, CAP_SYS_BOOT))
294 /* For safety, we require "magic" arguments. */
295 if (magic1 != LINUX_REBOOT_MAGIC1 ||
296 (magic2 != LINUX_REBOOT_MAGIC2 &&
297 magic2 != LINUX_REBOOT_MAGIC2A &&
298 magic2 != LINUX_REBOOT_MAGIC2B &&
299 magic2 != LINUX_REBOOT_MAGIC2C))
303 * If pid namespaces are enabled and the current task is in a child
304 * pid_namespace, the command is handled by reboot_pid_ns() which will
307 ret = reboot_pid_ns(pid_ns, cmd);
311 /* Instead of trying to make the power_off code look like
312 * halt when pm_power_off is not set do it the easy way.
314 if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
315 cmd = LINUX_REBOOT_CMD_HALT;
317 mutex_lock(&reboot_mutex);
319 case LINUX_REBOOT_CMD_RESTART:
320 kernel_restart(NULL);
323 case LINUX_REBOOT_CMD_CAD_ON:
327 case LINUX_REBOOT_CMD_CAD_OFF:
331 case LINUX_REBOOT_CMD_HALT:
334 panic("cannot halt");
336 case LINUX_REBOOT_CMD_POWER_OFF:
341 case LINUX_REBOOT_CMD_RESTART2:
342 ret = strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1);
347 buffer[sizeof(buffer) - 1] = '\0';
349 kernel_restart(buffer);
353 case LINUX_REBOOT_CMD_KEXEC:
354 ret = kernel_kexec();
358 #ifdef CONFIG_HIBERNATION
359 case LINUX_REBOOT_CMD_SW_SUSPEND:
368 mutex_unlock(&reboot_mutex);
372 static void deferred_cad(struct work_struct *dummy)
374 kernel_restart(NULL);
378 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
379 * As it's called within an interrupt, it may NOT sync: the only choice
380 * is whether to reboot at once, or just ignore the ctrl-alt-del.
382 void ctrl_alt_del(void)
384 static DECLARE_WORK(cad_work, deferred_cad);
387 schedule_work(&cad_work);
389 kill_cad_pid(SIGINT, 1);
392 char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
394 static int __orderly_poweroff(bool force)
397 static char *envp[] = {
399 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
404 argv = argv_split(GFP_KERNEL, poweroff_cmd, NULL);
406 ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
413 pr_warn("Failed to start orderly shutdown: forcing the issue\n");
415 * I guess this should try to kick off some daemon to sync and
416 * poweroff asap. Or not even bother syncing if we're doing an
417 * emergency shutdown?
426 static bool poweroff_force;
428 static void poweroff_work_func(struct work_struct *work)
430 __orderly_poweroff(poweroff_force);
433 static DECLARE_WORK(poweroff_work, poweroff_work_func);
436 * orderly_poweroff - Trigger an orderly system poweroff
437 * @force: force poweroff if command execution fails
439 * This may be called from any context to trigger a system shutdown.
440 * If the orderly shutdown fails, it will force an immediate shutdown.
442 int orderly_poweroff(bool force)
444 if (force) /* do not override the pending "true" */
445 poweroff_force = true;
446 schedule_work(&poweroff_work);
449 EXPORT_SYMBOL_GPL(orderly_poweroff);
451 static int __init reboot_setup(char *str)
455 * Having anything passed on the command line via
456 * reboot= will cause us to disable DMI checking
463 reboot_mode = REBOOT_WARM;
467 reboot_mode = REBOOT_COLD;
471 reboot_mode = REBOOT_HARD;
478 if (isdigit(*(str+1))) {
479 rc = kstrtoint(str+1, 0, &reboot_cpu);
482 } else if (str[1] == 'm' && str[2] == 'p' &&
484 rc = kstrtoint(str+3, 0, &reboot_cpu);
488 reboot_mode = REBOOT_SOFT;
492 reboot_mode = REBOOT_GPIO;
509 str = strchr(str, ',');
517 __setup("reboot=", reboot_setup);