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>
21 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
26 EXPORT_SYMBOL(cad_pid
);
28 #if defined(CONFIG_ARM) || defined(CONFIG_UNICORE32)
29 #define DEFAULT_REBOOT_MODE = REBOOT_HARD
31 #define DEFAULT_REBOOT_MODE
33 enum reboot_mode reboot_mode DEFAULT_REBOOT_MODE
;
36 * This variable is used privately to keep track of whether or not
37 * reboot_type is still set to its default value (i.e., reboot= hasn't
38 * been set on the command line). This is needed so that we can
39 * suppress DMI scanning for reboot quirks. Without it, it's
40 * impossible to override a faulty reboot quirk without recompiling.
42 int reboot_default
= 1;
44 enum reboot_type reboot_type
= BOOT_ACPI
;
46 int ignore_fs_panic
= 0; // To prevent kernel panic by EIO during shutdown
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
;
74 /* user process should be freezed before device shutdown */
75 events_check_enabled
= false;
78 usermodehelper_disable();
84 * register_reboot_notifier - Register function to be called at reboot time
85 * @nb: Info about notifier function to be called
87 * Registers a function with the list of functions
88 * to be called at reboot time.
90 * Currently always returns zero, as blocking_notifier_chain_register()
91 * always returns zero.
93 int register_reboot_notifier(struct notifier_block
*nb
)
95 return blocking_notifier_chain_register(&reboot_notifier_list
, nb
);
97 EXPORT_SYMBOL(register_reboot_notifier
);
100 * unregister_reboot_notifier - Unregister previously registered reboot notifier
101 * @nb: Hook to be unregistered
103 * Unregisters a previously registered reboot
106 * Returns zero on success, or %-ENOENT on failure.
108 int unregister_reboot_notifier(struct notifier_block
*nb
)
110 return blocking_notifier_chain_unregister(&reboot_notifier_list
, nb
);
112 EXPORT_SYMBOL(unregister_reboot_notifier
);
115 * Notifier list for kernel code which wants to be called
116 * to restart the system.
118 static ATOMIC_NOTIFIER_HEAD(restart_handler_list
);
121 * register_restart_handler - Register function to be called to reset
123 * @nb: Info about handler function to be called
124 * @nb->priority: Handler priority. Handlers should follow the
125 * following guidelines for setting priorities.
126 * 0: Restart handler of last resort,
127 * with limited restart capabilities
128 * 128: Default restart handler; use if no other
129 * restart handler is expected to be available,
130 * and/or if restart functionality is
131 * sufficient to restart the entire system
132 * 255: Highest priority restart handler, will
133 * preempt all other restart handlers
135 * Registers a function with code to be called to restart the
138 * Registered functions will be called from machine_restart as last
139 * step of the restart sequence (if the architecture specific
140 * machine_restart function calls do_kernel_restart - see below
142 * Registered functions are expected to restart the system immediately.
143 * If more than one function is registered, the restart handler priority
144 * selects which function will be called first.
146 * Restart handlers are expected to be registered from non-architecture
147 * code, typically from drivers. A typical use case would be a system
148 * where restart functionality is provided through a watchdog. Multiple
149 * restart handlers may exist; for example, one restart handler might
150 * restart the entire system, while another only restarts the CPU.
151 * In such cases, the restart handler which only restarts part of the
152 * hardware is expected to register with low priority to ensure that
153 * it only runs if no other means to restart the system is available.
155 * Currently always returns zero, as atomic_notifier_chain_register()
156 * always returns zero.
158 int register_restart_handler(struct notifier_block
*nb
)
160 return atomic_notifier_chain_register(&restart_handler_list
, nb
);
162 EXPORT_SYMBOL(register_restart_handler
);
165 * unregister_restart_handler - Unregister previously registered
167 * @nb: Hook to be unregistered
169 * Unregisters a previously registered restart handler function.
171 * Returns zero on success, or %-ENOENT on failure.
173 int unregister_restart_handler(struct notifier_block
*nb
)
175 return atomic_notifier_chain_unregister(&restart_handler_list
, nb
);
177 EXPORT_SYMBOL(unregister_restart_handler
);
180 * do_kernel_restart - Execute kernel restart handler call chain
182 * Calls functions registered with register_restart_handler.
184 * Expected to be called from machine_restart as last step of the restart
187 * Restarts the system immediately if a restart handler function has been
188 * registered. Otherwise does nothing.
190 void do_kernel_restart(char *cmd
)
192 atomic_notifier_call_chain(&restart_handler_list
, reboot_mode
, cmd
);
195 void migrate_to_reboot_cpu(void)
197 /* The boot cpu is always logical cpu 0 */
198 int cpu
= reboot_cpu
;
200 cpu_hotplug_disable();
202 /* Make certain the cpu I'm about to reboot on is online */
203 if (!cpu_online(cpu
))
204 cpu
= cpumask_first(cpu_online_mask
);
206 /* Prevent races with other tasks migrating this task */
207 current
->flags
|= PF_NO_SETAFFINITY
;
209 /* Make certain I only run on the appropriate processor */
210 set_cpus_allowed_ptr(current
, cpumask_of(cpu
));
214 * kernel_restart - reboot the system
215 * @cmd: pointer to buffer containing command to execute for restart
218 * Shutdown everything and perform a clean reboot.
219 * This is not safe to call in interrupt context.
221 void kernel_restart(char *cmd
)
223 kernel_restart_prepare(cmd
);
224 migrate_to_reboot_cpu();
227 pr_emerg("Restarting system\n");
229 pr_emerg("Restarting system with command '%s'\n", cmd
);
230 kmsg_dump(KMSG_DUMP_RESTART
);
231 machine_restart(cmd
);
233 EXPORT_SYMBOL_GPL(kernel_restart
);
235 static void kernel_shutdown_prepare(enum system_states state
)
237 blocking_notifier_call_chain(&reboot_notifier_list
,
238 (state
== SYSTEM_HALT
) ? SYS_HALT
: SYS_POWER_OFF
, NULL
);
239 system_state
= state
;
241 /* user process should be freezed before device shutdown */
242 events_check_enabled
= false;
245 usermodehelper_disable();
250 * kernel_halt - halt the system
252 * Shutdown everything and perform a clean system halt.
254 void kernel_halt(void)
256 kernel_shutdown_prepare(SYSTEM_HALT
);
257 migrate_to_reboot_cpu();
259 pr_emerg("System halted\n");
260 kmsg_dump(KMSG_DUMP_HALT
);
263 EXPORT_SYMBOL_GPL(kernel_halt
);
266 * kernel_power_off - power_off the system
268 * Shutdown everything and perform a clean system power_off.
270 void kernel_power_off(void)
272 kernel_shutdown_prepare(SYSTEM_POWER_OFF
);
273 if (pm_power_off_prepare
)
274 pm_power_off_prepare();
275 migrate_to_reboot_cpu();
277 pr_emerg("Power down\n");
278 kmsg_dump(KMSG_DUMP_POWEROFF
);
281 EXPORT_SYMBOL_GPL(kernel_power_off
);
283 static DEFINE_MUTEX(reboot_mutex
);
286 * Reboot system call: for obvious reasons only root may call it,
287 * and even root needs to set up some magic numbers in the registers
288 * so that some mistake won't make this reboot the whole machine.
289 * You can also set the meaning of the ctrl-alt-del-key here.
291 * reboot doesn't sync: do that yourself before calling this.
293 SYSCALL_DEFINE4(reboot
, int, magic1
, int, magic2
, unsigned int, cmd
,
296 struct pid_namespace
*pid_ns
= task_active_pid_ns(current
);
300 /* We only trust the superuser with rebooting the system. */
301 if (!ns_capable(pid_ns
->user_ns
, CAP_SYS_BOOT
))
304 /* For safety, we require "magic" arguments. */
305 if (magic1
!= LINUX_REBOOT_MAGIC1
||
306 (magic2
!= LINUX_REBOOT_MAGIC2
&&
307 magic2
!= LINUX_REBOOT_MAGIC2A
&&
308 magic2
!= LINUX_REBOOT_MAGIC2B
&&
309 magic2
!= LINUX_REBOOT_MAGIC2C
))
313 * If pid namespaces are enabled and the current task is in a child
314 * pid_namespace, the command is handled by reboot_pid_ns() which will
317 ret
= reboot_pid_ns(pid_ns
, cmd
);
321 /* Instead of trying to make the power_off code look like
322 * halt when pm_power_off is not set do it the easy way.
324 if ((cmd
== LINUX_REBOOT_CMD_POWER_OFF
) && !pm_power_off
)
325 cmd
= LINUX_REBOOT_CMD_HALT
;
327 mutex_lock(&reboot_mutex
);
329 case LINUX_REBOOT_CMD_RESTART
:
330 kernel_restart(NULL
);
333 case LINUX_REBOOT_CMD_CAD_ON
:
337 case LINUX_REBOOT_CMD_CAD_OFF
:
341 case LINUX_REBOOT_CMD_HALT
:
344 panic("cannot halt");
346 case LINUX_REBOOT_CMD_POWER_OFF
:
351 case LINUX_REBOOT_CMD_RESTART2
:
352 ret
= strncpy_from_user(&buffer
[0], arg
, sizeof(buffer
) - 1);
357 buffer
[sizeof(buffer
) - 1] = '\0';
359 kernel_restart(buffer
);
362 #ifdef CONFIG_KEXEC_CORE
363 case LINUX_REBOOT_CMD_KEXEC
:
364 ret
= kernel_kexec();
368 #ifdef CONFIG_HIBERNATION
369 case LINUX_REBOOT_CMD_SW_SUSPEND
:
378 mutex_unlock(&reboot_mutex
);
382 static void deferred_cad(struct work_struct
*dummy
)
384 kernel_restart(NULL
);
388 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
389 * As it's called within an interrupt, it may NOT sync: the only choice
390 * is whether to reboot at once, or just ignore the ctrl-alt-del.
392 void ctrl_alt_del(void)
394 static DECLARE_WORK(cad_work
, deferred_cad
);
397 schedule_work(&cad_work
);
399 kill_cad_pid(SIGINT
, 1);
402 char poweroff_cmd
[POWEROFF_CMD_PATH_LEN
] = "/sbin/poweroff";
403 static const char reboot_cmd
[] = "/sbin/reboot";
405 static int run_cmd(const char *cmd
)
408 static char *envp
[] = {
410 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
414 argv
= argv_split(GFP_KERNEL
, cmd
, NULL
);
416 ret
= call_usermodehelper(argv
[0], argv
, envp
, UMH_WAIT_EXEC
);
425 static int __orderly_reboot(void)
429 ret
= run_cmd(reboot_cmd
);
432 pr_warn("Failed to start orderly reboot: forcing the issue\n");
434 kernel_restart(NULL
);
440 static int __orderly_poweroff(bool force
)
444 ret
= run_cmd(poweroff_cmd
);
447 pr_warn("Failed to start orderly shutdown: forcing the issue\n");
450 * I guess this should try to kick off some daemon to sync and
451 * poweroff asap. Or not even bother syncing if we're doing an
452 * emergency shutdown?
461 static bool poweroff_force
;
463 static void poweroff_work_func(struct work_struct
*work
)
465 __orderly_poweroff(poweroff_force
);
468 static DECLARE_WORK(poweroff_work
, poweroff_work_func
);
471 * orderly_poweroff - Trigger an orderly system poweroff
472 * @force: force poweroff if command execution fails
474 * This may be called from any context to trigger a system shutdown.
475 * If the orderly shutdown fails, it will force an immediate shutdown.
477 void orderly_poweroff(bool force
)
479 if (force
) /* do not override the pending "true" */
480 poweroff_force
= true;
481 schedule_work(&poweroff_work
);
483 EXPORT_SYMBOL_GPL(orderly_poweroff
);
485 static void reboot_work_func(struct work_struct
*work
)
490 static DECLARE_WORK(reboot_work
, reboot_work_func
);
493 * orderly_reboot - Trigger an orderly system reboot
495 * This may be called from any context to trigger a system reboot.
496 * If the orderly reboot fails, it will force an immediate reboot.
498 void orderly_reboot(void)
500 schedule_work(&reboot_work
);
502 EXPORT_SYMBOL_GPL(orderly_reboot
);
504 static int __init
reboot_setup(char *str
)
508 * Having anything passed on the command line via
509 * reboot= will cause us to disable DMI checking
516 reboot_mode
= REBOOT_WARM
;
520 reboot_mode
= REBOOT_COLD
;
524 reboot_mode
= REBOOT_HARD
;
531 if (isdigit(*(str
+1))) {
532 rc
= kstrtoint(str
+1, 0, &reboot_cpu
);
535 } else if (str
[1] == 'm' && str
[2] == 'p' &&
537 rc
= kstrtoint(str
+3, 0, &reboot_cpu
);
541 reboot_mode
= REBOOT_SOFT
;
545 reboot_mode
= REBOOT_GPIO
;
562 str
= strchr(str
, ',');
570 __setup("reboot=", reboot_setup
);