x86: Use do_kernel_power_off()
[linux-2.6-microblaze.git] / kernel / reboot.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  linux/kernel/reboot.c
4  *
5  *  Copyright (C) 2013  Linus Torvalds
6  */
7
8 #define pr_fmt(fmt)     "reboot: " fmt
9
10 #include <linux/atomic.h>
11 #include <linux/ctype.h>
12 #include <linux/export.h>
13 #include <linux/kexec.h>
14 #include <linux/kmod.h>
15 #include <linux/kmsg_dump.h>
16 #include <linux/reboot.h>
17 #include <linux/suspend.h>
18 #include <linux/syscalls.h>
19 #include <linux/syscore_ops.h>
20 #include <linux/uaccess.h>
21
22 /*
23  * this indicates whether you can reboot with ctrl-alt-del: the default is yes
24  */
25
26 int C_A_D = 1;
27 struct pid *cad_pid;
28 EXPORT_SYMBOL(cad_pid);
29
30 #if defined(CONFIG_ARM)
31 #define DEFAULT_REBOOT_MODE             = REBOOT_HARD
32 #else
33 #define DEFAULT_REBOOT_MODE
34 #endif
35 enum reboot_mode reboot_mode DEFAULT_REBOOT_MODE;
36 EXPORT_SYMBOL_GPL(reboot_mode);
37 enum reboot_mode panic_reboot_mode = REBOOT_UNDEFINED;
38
39 /*
40  * This variable is used privately to keep track of whether or not
41  * reboot_type is still set to its default value (i.e., reboot= hasn't
42  * been set on the command line).  This is needed so that we can
43  * suppress DMI scanning for reboot quirks.  Without it, it's
44  * impossible to override a faulty reboot quirk without recompiling.
45  */
46 int reboot_default = 1;
47 int reboot_cpu;
48 enum reboot_type reboot_type = BOOT_ACPI;
49 int reboot_force;
50
51 struct sys_off_handler {
52         struct notifier_block nb;
53         int (*sys_off_cb)(struct sys_off_data *data);
54         void *cb_data;
55         enum sys_off_mode mode;
56         bool blocking;
57         void *list;
58 };
59
60 /*
61  * Temporary stub that prevents linkage failure while we're in process
62  * of removing all uses of legacy pm_power_off() around the kernel.
63  */
64 void __weak (*pm_power_off)(void);
65
66 /*
67  * If set, this is used for preparing the system to power off.
68  */
69
70 void (*pm_power_off_prepare)(void);
71 EXPORT_SYMBOL_GPL(pm_power_off_prepare);
72
73 /**
74  *      emergency_restart - reboot the system
75  *
76  *      Without shutting down any hardware or taking any locks
77  *      reboot the system.  This is called when we know we are in
78  *      trouble so this is our best effort to reboot.  This is
79  *      safe to call in interrupt context.
80  */
81 void emergency_restart(void)
82 {
83         kmsg_dump(KMSG_DUMP_EMERG);
84         machine_emergency_restart();
85 }
86 EXPORT_SYMBOL_GPL(emergency_restart);
87
88 void kernel_restart_prepare(char *cmd)
89 {
90         blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
91         system_state = SYSTEM_RESTART;
92         usermodehelper_disable();
93         device_shutdown();
94 }
95
96 /**
97  *      register_reboot_notifier - Register function to be called at reboot time
98  *      @nb: Info about notifier function to be called
99  *
100  *      Registers a function with the list of functions
101  *      to be called at reboot time.
102  *
103  *      Currently always returns zero, as blocking_notifier_chain_register()
104  *      always returns zero.
105  */
106 int register_reboot_notifier(struct notifier_block *nb)
107 {
108         return blocking_notifier_chain_register(&reboot_notifier_list, nb);
109 }
110 EXPORT_SYMBOL(register_reboot_notifier);
111
112 /**
113  *      unregister_reboot_notifier - Unregister previously registered reboot notifier
114  *      @nb: Hook to be unregistered
115  *
116  *      Unregisters a previously registered reboot
117  *      notifier function.
118  *
119  *      Returns zero on success, or %-ENOENT on failure.
120  */
121 int unregister_reboot_notifier(struct notifier_block *nb)
122 {
123         return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
124 }
125 EXPORT_SYMBOL(unregister_reboot_notifier);
126
127 static void devm_unregister_reboot_notifier(struct device *dev, void *res)
128 {
129         WARN_ON(unregister_reboot_notifier(*(struct notifier_block **)res));
130 }
131
132 int devm_register_reboot_notifier(struct device *dev, struct notifier_block *nb)
133 {
134         struct notifier_block **rcnb;
135         int ret;
136
137         rcnb = devres_alloc(devm_unregister_reboot_notifier,
138                             sizeof(*rcnb), GFP_KERNEL);
139         if (!rcnb)
140                 return -ENOMEM;
141
142         ret = register_reboot_notifier(nb);
143         if (!ret) {
144                 *rcnb = nb;
145                 devres_add(dev, rcnb);
146         } else {
147                 devres_free(rcnb);
148         }
149
150         return ret;
151 }
152 EXPORT_SYMBOL(devm_register_reboot_notifier);
153
154 /*
155  *      Notifier list for kernel code which wants to be called
156  *      to restart the system.
157  */
158 static ATOMIC_NOTIFIER_HEAD(restart_handler_list);
159
160 /**
161  *      register_restart_handler - Register function to be called to reset
162  *                                 the system
163  *      @nb: Info about handler function to be called
164  *      @nb->priority:  Handler priority. Handlers should follow the
165  *                      following guidelines for setting priorities.
166  *                      0:      Restart handler of last resort,
167  *                              with limited restart capabilities
168  *                      128:    Default restart handler; use if no other
169  *                              restart handler is expected to be available,
170  *                              and/or if restart functionality is
171  *                              sufficient to restart the entire system
172  *                      255:    Highest priority restart handler, will
173  *                              preempt all other restart handlers
174  *
175  *      Registers a function with code to be called to restart the
176  *      system.
177  *
178  *      Registered functions will be called from machine_restart as last
179  *      step of the restart sequence (if the architecture specific
180  *      machine_restart function calls do_kernel_restart - see below
181  *      for details).
182  *      Registered functions are expected to restart the system immediately.
183  *      If more than one function is registered, the restart handler priority
184  *      selects which function will be called first.
185  *
186  *      Restart handlers are expected to be registered from non-architecture
187  *      code, typically from drivers. A typical use case would be a system
188  *      where restart functionality is provided through a watchdog. Multiple
189  *      restart handlers may exist; for example, one restart handler might
190  *      restart the entire system, while another only restarts the CPU.
191  *      In such cases, the restart handler which only restarts part of the
192  *      hardware is expected to register with low priority to ensure that
193  *      it only runs if no other means to restart the system is available.
194  *
195  *      Currently always returns zero, as atomic_notifier_chain_register()
196  *      always returns zero.
197  */
198 int register_restart_handler(struct notifier_block *nb)
199 {
200         return atomic_notifier_chain_register(&restart_handler_list, nb);
201 }
202 EXPORT_SYMBOL(register_restart_handler);
203
204 /**
205  *      unregister_restart_handler - Unregister previously registered
206  *                                   restart handler
207  *      @nb: Hook to be unregistered
208  *
209  *      Unregisters a previously registered restart handler function.
210  *
211  *      Returns zero on success, or %-ENOENT on failure.
212  */
213 int unregister_restart_handler(struct notifier_block *nb)
214 {
215         return atomic_notifier_chain_unregister(&restart_handler_list, nb);
216 }
217 EXPORT_SYMBOL(unregister_restart_handler);
218
219 /**
220  *      do_kernel_restart - Execute kernel restart handler call chain
221  *
222  *      Calls functions registered with register_restart_handler.
223  *
224  *      Expected to be called from machine_restart as last step of the restart
225  *      sequence.
226  *
227  *      Restarts the system immediately if a restart handler function has been
228  *      registered. Otherwise does nothing.
229  */
230 void do_kernel_restart(char *cmd)
231 {
232         atomic_notifier_call_chain(&restart_handler_list, reboot_mode, cmd);
233 }
234
235 void migrate_to_reboot_cpu(void)
236 {
237         /* The boot cpu is always logical cpu 0 */
238         int cpu = reboot_cpu;
239
240         cpu_hotplug_disable();
241
242         /* Make certain the cpu I'm about to reboot on is online */
243         if (!cpu_online(cpu))
244                 cpu = cpumask_first(cpu_online_mask);
245
246         /* Prevent races with other tasks migrating this task */
247         current->flags |= PF_NO_SETAFFINITY;
248
249         /* Make certain I only run on the appropriate processor */
250         set_cpus_allowed_ptr(current, cpumask_of(cpu));
251 }
252
253 /**
254  *      kernel_restart - reboot the system
255  *      @cmd: pointer to buffer containing command to execute for restart
256  *              or %NULL
257  *
258  *      Shutdown everything and perform a clean reboot.
259  *      This is not safe to call in interrupt context.
260  */
261 void kernel_restart(char *cmd)
262 {
263         kernel_restart_prepare(cmd);
264         migrate_to_reboot_cpu();
265         syscore_shutdown();
266         if (!cmd)
267                 pr_emerg("Restarting system\n");
268         else
269                 pr_emerg("Restarting system with command '%s'\n", cmd);
270         kmsg_dump(KMSG_DUMP_SHUTDOWN);
271         machine_restart(cmd);
272 }
273 EXPORT_SYMBOL_GPL(kernel_restart);
274
275 static void kernel_shutdown_prepare(enum system_states state)
276 {
277         blocking_notifier_call_chain(&reboot_notifier_list,
278                 (state == SYSTEM_HALT) ? SYS_HALT : SYS_POWER_OFF, NULL);
279         system_state = state;
280         usermodehelper_disable();
281         device_shutdown();
282 }
283 /**
284  *      kernel_halt - halt the system
285  *
286  *      Shutdown everything and perform a clean system halt.
287  */
288 void kernel_halt(void)
289 {
290         kernel_shutdown_prepare(SYSTEM_HALT);
291         migrate_to_reboot_cpu();
292         syscore_shutdown();
293         pr_emerg("System halted\n");
294         kmsg_dump(KMSG_DUMP_SHUTDOWN);
295         machine_halt();
296 }
297 EXPORT_SYMBOL_GPL(kernel_halt);
298
299 /*
300  *      Notifier list for kernel code which wants to be called
301  *      to prepare system for power off.
302  */
303 static BLOCKING_NOTIFIER_HEAD(power_off_prep_handler_list);
304
305 /*
306  *      Notifier list for kernel code which wants to be called
307  *      to power off system.
308  */
309 static ATOMIC_NOTIFIER_HEAD(power_off_handler_list);
310
311 static int sys_off_notify(struct notifier_block *nb,
312                           unsigned long mode, void *cmd)
313 {
314         struct sys_off_handler *handler;
315         struct sys_off_data data = {};
316
317         handler = container_of(nb, struct sys_off_handler, nb);
318         data.cb_data = handler->cb_data;
319         data.mode = mode;
320         data.cmd = cmd;
321
322         return handler->sys_off_cb(&data);
323 }
324
325 /**
326  *      register_sys_off_handler - Register sys-off handler
327  *      @mode: Sys-off mode
328  *      @priority: Handler priority
329  *      @callback: Callback function
330  *      @cb_data: Callback argument
331  *
332  *      Registers system power-off or restart handler that will be invoked
333  *      at the step corresponding to the given sys-off mode. Handler's callback
334  *      should return NOTIFY_DONE to permit execution of the next handler in
335  *      the call chain or NOTIFY_STOP to break the chain (in error case for
336  *      example).
337  *
338  *      Multiple handlers can be registered at the default priority level.
339  *
340  *      Only one handler can be registered at the non-default priority level,
341  *      otherwise ERR_PTR(-EBUSY) is returned.
342  *
343  *      Returns a new instance of struct sys_off_handler on success, or
344  *      an ERR_PTR()-encoded error code otherwise.
345  */
346 struct sys_off_handler *
347 register_sys_off_handler(enum sys_off_mode mode,
348                          int priority,
349                          int (*callback)(struct sys_off_data *data),
350                          void *cb_data)
351 {
352         struct sys_off_handler *handler;
353         int err;
354
355         handler = kzalloc(sizeof(*handler), GFP_KERNEL);
356         if (!handler)
357                 return ERR_PTR(-ENOMEM);
358
359         switch (mode) {
360         case SYS_OFF_MODE_POWER_OFF_PREPARE:
361                 handler->list = &power_off_prep_handler_list;
362                 handler->blocking = true;
363                 break;
364
365         case SYS_OFF_MODE_POWER_OFF:
366                 handler->list = &power_off_handler_list;
367                 break;
368
369         case SYS_OFF_MODE_RESTART:
370                 handler->list = &restart_handler_list;
371                 break;
372
373         default:
374                 kfree(handler);
375                 return ERR_PTR(-EINVAL);
376         }
377
378         handler->nb.notifier_call = sys_off_notify;
379         handler->nb.priority = priority;
380         handler->sys_off_cb = callback;
381         handler->cb_data = cb_data;
382         handler->mode = mode;
383
384         if (handler->blocking) {
385                 if (priority == SYS_OFF_PRIO_DEFAULT)
386                         err = blocking_notifier_chain_register(handler->list,
387                                                                &handler->nb);
388                 else
389                         err = blocking_notifier_chain_register_unique_prio(handler->list,
390                                                                            &handler->nb);
391         } else {
392                 if (priority == SYS_OFF_PRIO_DEFAULT)
393                         err = atomic_notifier_chain_register(handler->list,
394                                                              &handler->nb);
395                 else
396                         err = atomic_notifier_chain_register_unique_prio(handler->list,
397                                                                          &handler->nb);
398         }
399
400         if (err) {
401                 kfree(handler);
402                 return ERR_PTR(err);
403         }
404
405         return handler;
406 }
407 EXPORT_SYMBOL_GPL(register_sys_off_handler);
408
409 /**
410  *      unregister_sys_off_handler - Unregister sys-off handler
411  *      @handler: Sys-off handler
412  *
413  *      Unregisters given sys-off handler.
414  */
415 void unregister_sys_off_handler(struct sys_off_handler *handler)
416 {
417         int err;
418
419         if (!handler)
420                 return;
421
422         if (handler->blocking)
423                 err = blocking_notifier_chain_unregister(handler->list,
424                                                          &handler->nb);
425         else
426                 err = atomic_notifier_chain_unregister(handler->list,
427                                                        &handler->nb);
428
429         /* sanity check, shall never happen */
430         WARN_ON(err);
431
432         kfree(handler);
433 }
434 EXPORT_SYMBOL_GPL(unregister_sys_off_handler);
435
436 static void devm_unregister_sys_off_handler(void *data)
437 {
438         struct sys_off_handler *handler = data;
439
440         unregister_sys_off_handler(handler);
441 }
442
443 /**
444  *      devm_register_sys_off_handler - Register sys-off handler
445  *      @dev: Device that registers handler
446  *      @mode: Sys-off mode
447  *      @priority: Handler priority
448  *      @callback: Callback function
449  *      @cb_data: Callback argument
450  *
451  *      Registers resource-managed sys-off handler.
452  *
453  *      Returns zero on success, or error code on failure.
454  */
455 int devm_register_sys_off_handler(struct device *dev,
456                                   enum sys_off_mode mode,
457                                   int priority,
458                                   int (*callback)(struct sys_off_data *data),
459                                   void *cb_data)
460 {
461         struct sys_off_handler *handler;
462
463         handler = register_sys_off_handler(mode, priority, callback, cb_data);
464         if (IS_ERR(handler))
465                 return PTR_ERR(handler);
466
467         return devm_add_action_or_reset(dev, devm_unregister_sys_off_handler,
468                                         handler);
469 }
470 EXPORT_SYMBOL_GPL(devm_register_sys_off_handler);
471
472 static struct sys_off_handler *platform_power_off_handler;
473
474 static int platform_power_off_notify(struct sys_off_data *data)
475 {
476         void (*platform_power_power_off_cb)(void) = data->cb_data;
477
478         platform_power_power_off_cb();
479
480         return NOTIFY_DONE;
481 }
482
483 /**
484  *      register_platform_power_off - Register platform-level power-off callback
485  *      @power_off: Power-off callback
486  *
487  *      Registers power-off callback that will be called as last step
488  *      of the power-off sequence. This callback is expected to be invoked
489  *      for the last resort. Only one platform power-off callback is allowed
490  *      to be registered at a time.
491  *
492  *      Returns zero on success, or error code on failure.
493  */
494 int register_platform_power_off(void (*power_off)(void))
495 {
496         struct sys_off_handler *handler;
497
498         handler = register_sys_off_handler(SYS_OFF_MODE_POWER_OFF,
499                                            SYS_OFF_PRIO_PLATFORM,
500                                            platform_power_off_notify,
501                                            power_off);
502         if (IS_ERR(handler))
503                 return PTR_ERR(handler);
504
505         platform_power_off_handler = handler;
506
507         return 0;
508 }
509 EXPORT_SYMBOL_GPL(register_platform_power_off);
510
511 /**
512  *      unregister_platform_power_off - Unregister platform-level power-off callback
513  *      @power_off: Power-off callback
514  *
515  *      Unregisters previously registered platform power-off callback.
516  */
517 void unregister_platform_power_off(void (*power_off)(void))
518 {
519         if (platform_power_off_handler &&
520             platform_power_off_handler->cb_data == power_off) {
521                 unregister_sys_off_handler(platform_power_off_handler);
522                 platform_power_off_handler = NULL;
523         }
524 }
525 EXPORT_SYMBOL_GPL(unregister_platform_power_off);
526
527 static int legacy_pm_power_off_prepare(struct sys_off_data *data)
528 {
529         if (pm_power_off_prepare)
530                 pm_power_off_prepare();
531
532         return NOTIFY_DONE;
533 }
534
535 static int legacy_pm_power_off(struct sys_off_data *data)
536 {
537         if (pm_power_off)
538                 pm_power_off();
539
540         return NOTIFY_DONE;
541 }
542
543 /*
544  * Register sys-off handlers for legacy PM callbacks. This allows legacy
545  * PM callbacks co-exist with the new sys-off API.
546  *
547  * TODO: Remove legacy handlers once all legacy PM users will be switched
548  *       to the sys-off based APIs.
549  */
550 static int __init legacy_pm_init(void)
551 {
552         register_sys_off_handler(SYS_OFF_MODE_POWER_OFF_PREPARE,
553                                  SYS_OFF_PRIO_DEFAULT,
554                                  legacy_pm_power_off_prepare, NULL);
555
556         register_sys_off_handler(SYS_OFF_MODE_POWER_OFF, SYS_OFF_PRIO_DEFAULT,
557                                  legacy_pm_power_off, NULL);
558
559         return 0;
560 }
561 core_initcall(legacy_pm_init);
562
563 static void do_kernel_power_off_prepare(void)
564 {
565         blocking_notifier_call_chain(&power_off_prep_handler_list, 0, NULL);
566 }
567
568 /**
569  *      do_kernel_power_off - Execute kernel power-off handler call chain
570  *
571  *      Expected to be called as last step of the power-off sequence.
572  *
573  *      Powers off the system immediately if a power-off handler function has
574  *      been registered. Otherwise does nothing.
575  */
576 void do_kernel_power_off(void)
577 {
578         atomic_notifier_call_chain(&power_off_handler_list, 0, NULL);
579 }
580
581 /**
582  *      kernel_can_power_off - check whether system can be powered off
583  *
584  *      Returns true if power-off handler is registered and system can be
585  *      powered off, false otherwise.
586  */
587 bool kernel_can_power_off(void)
588 {
589         return !atomic_notifier_call_chain_is_empty(&power_off_handler_list);
590 }
591 EXPORT_SYMBOL_GPL(kernel_can_power_off);
592
593 /**
594  *      kernel_power_off - power_off the system
595  *
596  *      Shutdown everything and perform a clean system power_off.
597  */
598 void kernel_power_off(void)
599 {
600         kernel_shutdown_prepare(SYSTEM_POWER_OFF);
601         do_kernel_power_off_prepare();
602         migrate_to_reboot_cpu();
603         syscore_shutdown();
604         pr_emerg("Power down\n");
605         kmsg_dump(KMSG_DUMP_SHUTDOWN);
606         machine_power_off();
607 }
608 EXPORT_SYMBOL_GPL(kernel_power_off);
609
610 DEFINE_MUTEX(system_transition_mutex);
611
612 /*
613  * Reboot system call: for obvious reasons only root may call it,
614  * and even root needs to set up some magic numbers in the registers
615  * so that some mistake won't make this reboot the whole machine.
616  * You can also set the meaning of the ctrl-alt-del-key here.
617  *
618  * reboot doesn't sync: do that yourself before calling this.
619  */
620 SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
621                 void __user *, arg)
622 {
623         struct pid_namespace *pid_ns = task_active_pid_ns(current);
624         char buffer[256];
625         int ret = 0;
626
627         /* We only trust the superuser with rebooting the system. */
628         if (!ns_capable(pid_ns->user_ns, CAP_SYS_BOOT))
629                 return -EPERM;
630
631         /* For safety, we require "magic" arguments. */
632         if (magic1 != LINUX_REBOOT_MAGIC1 ||
633                         (magic2 != LINUX_REBOOT_MAGIC2 &&
634                         magic2 != LINUX_REBOOT_MAGIC2A &&
635                         magic2 != LINUX_REBOOT_MAGIC2B &&
636                         magic2 != LINUX_REBOOT_MAGIC2C))
637                 return -EINVAL;
638
639         /*
640          * If pid namespaces are enabled and the current task is in a child
641          * pid_namespace, the command is handled by reboot_pid_ns() which will
642          * call do_exit().
643          */
644         ret = reboot_pid_ns(pid_ns, cmd);
645         if (ret)
646                 return ret;
647
648         /* Instead of trying to make the power_off code look like
649          * halt when pm_power_off is not set do it the easy way.
650          */
651         if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !kernel_can_power_off())
652                 cmd = LINUX_REBOOT_CMD_HALT;
653
654         mutex_lock(&system_transition_mutex);
655         switch (cmd) {
656         case LINUX_REBOOT_CMD_RESTART:
657                 kernel_restart(NULL);
658                 break;
659
660         case LINUX_REBOOT_CMD_CAD_ON:
661                 C_A_D = 1;
662                 break;
663
664         case LINUX_REBOOT_CMD_CAD_OFF:
665                 C_A_D = 0;
666                 break;
667
668         case LINUX_REBOOT_CMD_HALT:
669                 kernel_halt();
670                 do_exit(0);
671
672         case LINUX_REBOOT_CMD_POWER_OFF:
673                 kernel_power_off();
674                 do_exit(0);
675                 break;
676
677         case LINUX_REBOOT_CMD_RESTART2:
678                 ret = strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1);
679                 if (ret < 0) {
680                         ret = -EFAULT;
681                         break;
682                 }
683                 buffer[sizeof(buffer) - 1] = '\0';
684
685                 kernel_restart(buffer);
686                 break;
687
688 #ifdef CONFIG_KEXEC_CORE
689         case LINUX_REBOOT_CMD_KEXEC:
690                 ret = kernel_kexec();
691                 break;
692 #endif
693
694 #ifdef CONFIG_HIBERNATION
695         case LINUX_REBOOT_CMD_SW_SUSPEND:
696                 ret = hibernate();
697                 break;
698 #endif
699
700         default:
701                 ret = -EINVAL;
702                 break;
703         }
704         mutex_unlock(&system_transition_mutex);
705         return ret;
706 }
707
708 static void deferred_cad(struct work_struct *dummy)
709 {
710         kernel_restart(NULL);
711 }
712
713 /*
714  * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
715  * As it's called within an interrupt, it may NOT sync: the only choice
716  * is whether to reboot at once, or just ignore the ctrl-alt-del.
717  */
718 void ctrl_alt_del(void)
719 {
720         static DECLARE_WORK(cad_work, deferred_cad);
721
722         if (C_A_D)
723                 schedule_work(&cad_work);
724         else
725                 kill_cad_pid(SIGINT, 1);
726 }
727
728 char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
729 static const char reboot_cmd[] = "/sbin/reboot";
730
731 static int run_cmd(const char *cmd)
732 {
733         char **argv;
734         static char *envp[] = {
735                 "HOME=/",
736                 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
737                 NULL
738         };
739         int ret;
740         argv = argv_split(GFP_KERNEL, cmd, NULL);
741         if (argv) {
742                 ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
743                 argv_free(argv);
744         } else {
745                 ret = -ENOMEM;
746         }
747
748         return ret;
749 }
750
751 static int __orderly_reboot(void)
752 {
753         int ret;
754
755         ret = run_cmd(reboot_cmd);
756
757         if (ret) {
758                 pr_warn("Failed to start orderly reboot: forcing the issue\n");
759                 emergency_sync();
760                 kernel_restart(NULL);
761         }
762
763         return ret;
764 }
765
766 static int __orderly_poweroff(bool force)
767 {
768         int ret;
769
770         ret = run_cmd(poweroff_cmd);
771
772         if (ret && force) {
773                 pr_warn("Failed to start orderly shutdown: forcing the issue\n");
774
775                 /*
776                  * I guess this should try to kick off some daemon to sync and
777                  * poweroff asap.  Or not even bother syncing if we're doing an
778                  * emergency shutdown?
779                  */
780                 emergency_sync();
781                 kernel_power_off();
782         }
783
784         return ret;
785 }
786
787 static bool poweroff_force;
788
789 static void poweroff_work_func(struct work_struct *work)
790 {
791         __orderly_poweroff(poweroff_force);
792 }
793
794 static DECLARE_WORK(poweroff_work, poweroff_work_func);
795
796 /**
797  * orderly_poweroff - Trigger an orderly system poweroff
798  * @force: force poweroff if command execution fails
799  *
800  * This may be called from any context to trigger a system shutdown.
801  * If the orderly shutdown fails, it will force an immediate shutdown.
802  */
803 void orderly_poweroff(bool force)
804 {
805         if (force) /* do not override the pending "true" */
806                 poweroff_force = true;
807         schedule_work(&poweroff_work);
808 }
809 EXPORT_SYMBOL_GPL(orderly_poweroff);
810
811 static void reboot_work_func(struct work_struct *work)
812 {
813         __orderly_reboot();
814 }
815
816 static DECLARE_WORK(reboot_work, reboot_work_func);
817
818 /**
819  * orderly_reboot - Trigger an orderly system reboot
820  *
821  * This may be called from any context to trigger a system reboot.
822  * If the orderly reboot fails, it will force an immediate reboot.
823  */
824 void orderly_reboot(void)
825 {
826         schedule_work(&reboot_work);
827 }
828 EXPORT_SYMBOL_GPL(orderly_reboot);
829
830 /**
831  * hw_failure_emergency_poweroff_func - emergency poweroff work after a known delay
832  * @work: work_struct associated with the emergency poweroff function
833  *
834  * This function is called in very critical situations to force
835  * a kernel poweroff after a configurable timeout value.
836  */
837 static void hw_failure_emergency_poweroff_func(struct work_struct *work)
838 {
839         /*
840          * We have reached here after the emergency shutdown waiting period has
841          * expired. This means orderly_poweroff has not been able to shut off
842          * the system for some reason.
843          *
844          * Try to shut down the system immediately using kernel_power_off
845          * if populated
846          */
847         pr_emerg("Hardware protection timed-out. Trying forced poweroff\n");
848         kernel_power_off();
849
850         /*
851          * Worst of the worst case trigger emergency restart
852          */
853         pr_emerg("Hardware protection shutdown failed. Trying emergency restart\n");
854         emergency_restart();
855 }
856
857 static DECLARE_DELAYED_WORK(hw_failure_emergency_poweroff_work,
858                             hw_failure_emergency_poweroff_func);
859
860 /**
861  * hw_failure_emergency_poweroff - Trigger an emergency system poweroff
862  *
863  * This may be called from any critical situation to trigger a system shutdown
864  * after a given period of time. If time is negative this is not scheduled.
865  */
866 static void hw_failure_emergency_poweroff(int poweroff_delay_ms)
867 {
868         if (poweroff_delay_ms <= 0)
869                 return;
870         schedule_delayed_work(&hw_failure_emergency_poweroff_work,
871                               msecs_to_jiffies(poweroff_delay_ms));
872 }
873
874 /**
875  * hw_protection_shutdown - Trigger an emergency system poweroff
876  *
877  * @reason:             Reason of emergency shutdown to be printed.
878  * @ms_until_forced:    Time to wait for orderly shutdown before tiggering a
879  *                      forced shudown. Negative value disables the forced
880  *                      shutdown.
881  *
882  * Initiate an emergency system shutdown in order to protect hardware from
883  * further damage. Usage examples include a thermal protection or a voltage or
884  * current regulator failures.
885  * NOTE: The request is ignored if protection shutdown is already pending even
886  * if the previous request has given a large timeout for forced shutdown.
887  * Can be called from any context.
888  */
889 void hw_protection_shutdown(const char *reason, int ms_until_forced)
890 {
891         static atomic_t allow_proceed = ATOMIC_INIT(1);
892
893         pr_emerg("HARDWARE PROTECTION shutdown (%s)\n", reason);
894
895         /* Shutdown should be initiated only once. */
896         if (!atomic_dec_and_test(&allow_proceed))
897                 return;
898
899         /*
900          * Queue a backup emergency shutdown in the event of
901          * orderly_poweroff failure
902          */
903         hw_failure_emergency_poweroff(ms_until_forced);
904         orderly_poweroff(true);
905 }
906 EXPORT_SYMBOL_GPL(hw_protection_shutdown);
907
908 static int __init reboot_setup(char *str)
909 {
910         for (;;) {
911                 enum reboot_mode *mode;
912
913                 /*
914                  * Having anything passed on the command line via
915                  * reboot= will cause us to disable DMI checking
916                  * below.
917                  */
918                 reboot_default = 0;
919
920                 if (!strncmp(str, "panic_", 6)) {
921                         mode = &panic_reboot_mode;
922                         str += 6;
923                 } else {
924                         mode = &reboot_mode;
925                 }
926
927                 switch (*str) {
928                 case 'w':
929                         *mode = REBOOT_WARM;
930                         break;
931
932                 case 'c':
933                         *mode = REBOOT_COLD;
934                         break;
935
936                 case 'h':
937                         *mode = REBOOT_HARD;
938                         break;
939
940                 case 's':
941                         /*
942                          * reboot_cpu is s[mp]#### with #### being the processor
943                          * to be used for rebooting. Skip 's' or 'smp' prefix.
944                          */
945                         str += str[1] == 'm' && str[2] == 'p' ? 3 : 1;
946
947                         if (isdigit(str[0])) {
948                                 int cpu = simple_strtoul(str, NULL, 0);
949
950                                 if (cpu >= num_possible_cpus()) {
951                                         pr_err("Ignoring the CPU number in reboot= option. "
952                                         "CPU %d exceeds possible cpu number %d\n",
953                                         cpu, num_possible_cpus());
954                                         break;
955                                 }
956                                 reboot_cpu = cpu;
957                         } else
958                                 *mode = REBOOT_SOFT;
959                         break;
960
961                 case 'g':
962                         *mode = REBOOT_GPIO;
963                         break;
964
965                 case 'b':
966                 case 'a':
967                 case 'k':
968                 case 't':
969                 case 'e':
970                 case 'p':
971                         reboot_type = *str;
972                         break;
973
974                 case 'f':
975                         reboot_force = 1;
976                         break;
977                 }
978
979                 str = strchr(str, ',');
980                 if (str)
981                         str++;
982                 else
983                         break;
984         }
985         return 1;
986 }
987 __setup("reboot=", reboot_setup);
988
989 #ifdef CONFIG_SYSFS
990
991 #define REBOOT_COLD_STR         "cold"
992 #define REBOOT_WARM_STR         "warm"
993 #define REBOOT_HARD_STR         "hard"
994 #define REBOOT_SOFT_STR         "soft"
995 #define REBOOT_GPIO_STR         "gpio"
996 #define REBOOT_UNDEFINED_STR    "undefined"
997
998 #define BOOT_TRIPLE_STR         "triple"
999 #define BOOT_KBD_STR            "kbd"
1000 #define BOOT_BIOS_STR           "bios"
1001 #define BOOT_ACPI_STR           "acpi"
1002 #define BOOT_EFI_STR            "efi"
1003 #define BOOT_PCI_STR            "pci"
1004
1005 static ssize_t mode_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
1006 {
1007         const char *val;
1008
1009         switch (reboot_mode) {
1010         case REBOOT_COLD:
1011                 val = REBOOT_COLD_STR;
1012                 break;
1013         case REBOOT_WARM:
1014                 val = REBOOT_WARM_STR;
1015                 break;
1016         case REBOOT_HARD:
1017                 val = REBOOT_HARD_STR;
1018                 break;
1019         case REBOOT_SOFT:
1020                 val = REBOOT_SOFT_STR;
1021                 break;
1022         case REBOOT_GPIO:
1023                 val = REBOOT_GPIO_STR;
1024                 break;
1025         default:
1026                 val = REBOOT_UNDEFINED_STR;
1027         }
1028
1029         return sprintf(buf, "%s\n", val);
1030 }
1031 static ssize_t mode_store(struct kobject *kobj, struct kobj_attribute *attr,
1032                           const char *buf, size_t count)
1033 {
1034         if (!capable(CAP_SYS_BOOT))
1035                 return -EPERM;
1036
1037         if (!strncmp(buf, REBOOT_COLD_STR, strlen(REBOOT_COLD_STR)))
1038                 reboot_mode = REBOOT_COLD;
1039         else if (!strncmp(buf, REBOOT_WARM_STR, strlen(REBOOT_WARM_STR)))
1040                 reboot_mode = REBOOT_WARM;
1041         else if (!strncmp(buf, REBOOT_HARD_STR, strlen(REBOOT_HARD_STR)))
1042                 reboot_mode = REBOOT_HARD;
1043         else if (!strncmp(buf, REBOOT_SOFT_STR, strlen(REBOOT_SOFT_STR)))
1044                 reboot_mode = REBOOT_SOFT;
1045         else if (!strncmp(buf, REBOOT_GPIO_STR, strlen(REBOOT_GPIO_STR)))
1046                 reboot_mode = REBOOT_GPIO;
1047         else
1048                 return -EINVAL;
1049
1050         reboot_default = 0;
1051
1052         return count;
1053 }
1054 static struct kobj_attribute reboot_mode_attr = __ATTR_RW(mode);
1055
1056 #ifdef CONFIG_X86
1057 static ssize_t force_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
1058 {
1059         return sprintf(buf, "%d\n", reboot_force);
1060 }
1061 static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr,
1062                           const char *buf, size_t count)
1063 {
1064         bool res;
1065
1066         if (!capable(CAP_SYS_BOOT))
1067                 return -EPERM;
1068
1069         if (kstrtobool(buf, &res))
1070                 return -EINVAL;
1071
1072         reboot_default = 0;
1073         reboot_force = res;
1074
1075         return count;
1076 }
1077 static struct kobj_attribute reboot_force_attr = __ATTR_RW(force);
1078
1079 static ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
1080 {
1081         const char *val;
1082
1083         switch (reboot_type) {
1084         case BOOT_TRIPLE:
1085                 val = BOOT_TRIPLE_STR;
1086                 break;
1087         case BOOT_KBD:
1088                 val = BOOT_KBD_STR;
1089                 break;
1090         case BOOT_BIOS:
1091                 val = BOOT_BIOS_STR;
1092                 break;
1093         case BOOT_ACPI:
1094                 val = BOOT_ACPI_STR;
1095                 break;
1096         case BOOT_EFI:
1097                 val = BOOT_EFI_STR;
1098                 break;
1099         case BOOT_CF9_FORCE:
1100                 val = BOOT_PCI_STR;
1101                 break;
1102         default:
1103                 val = REBOOT_UNDEFINED_STR;
1104         }
1105
1106         return sprintf(buf, "%s\n", val);
1107 }
1108 static ssize_t type_store(struct kobject *kobj, struct kobj_attribute *attr,
1109                           const char *buf, size_t count)
1110 {
1111         if (!capable(CAP_SYS_BOOT))
1112                 return -EPERM;
1113
1114         if (!strncmp(buf, BOOT_TRIPLE_STR, strlen(BOOT_TRIPLE_STR)))
1115                 reboot_type = BOOT_TRIPLE;
1116         else if (!strncmp(buf, BOOT_KBD_STR, strlen(BOOT_KBD_STR)))
1117                 reboot_type = BOOT_KBD;
1118         else if (!strncmp(buf, BOOT_BIOS_STR, strlen(BOOT_BIOS_STR)))
1119                 reboot_type = BOOT_BIOS;
1120         else if (!strncmp(buf, BOOT_ACPI_STR, strlen(BOOT_ACPI_STR)))
1121                 reboot_type = BOOT_ACPI;
1122         else if (!strncmp(buf, BOOT_EFI_STR, strlen(BOOT_EFI_STR)))
1123                 reboot_type = BOOT_EFI;
1124         else if (!strncmp(buf, BOOT_PCI_STR, strlen(BOOT_PCI_STR)))
1125                 reboot_type = BOOT_CF9_FORCE;
1126         else
1127                 return -EINVAL;
1128
1129         reboot_default = 0;
1130
1131         return count;
1132 }
1133 static struct kobj_attribute reboot_type_attr = __ATTR_RW(type);
1134 #endif
1135
1136 #ifdef CONFIG_SMP
1137 static ssize_t cpu_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
1138 {
1139         return sprintf(buf, "%d\n", reboot_cpu);
1140 }
1141 static ssize_t cpu_store(struct kobject *kobj, struct kobj_attribute *attr,
1142                           const char *buf, size_t count)
1143 {
1144         unsigned int cpunum;
1145         int rc;
1146
1147         if (!capable(CAP_SYS_BOOT))
1148                 return -EPERM;
1149
1150         rc = kstrtouint(buf, 0, &cpunum);
1151
1152         if (rc)
1153                 return rc;
1154
1155         if (cpunum >= num_possible_cpus())
1156                 return -ERANGE;
1157
1158         reboot_default = 0;
1159         reboot_cpu = cpunum;
1160
1161         return count;
1162 }
1163 static struct kobj_attribute reboot_cpu_attr = __ATTR_RW(cpu);
1164 #endif
1165
1166 static struct attribute *reboot_attrs[] = {
1167         &reboot_mode_attr.attr,
1168 #ifdef CONFIG_X86
1169         &reboot_force_attr.attr,
1170         &reboot_type_attr.attr,
1171 #endif
1172 #ifdef CONFIG_SMP
1173         &reboot_cpu_attr.attr,
1174 #endif
1175         NULL,
1176 };
1177
1178 static const struct attribute_group reboot_attr_group = {
1179         .attrs = reboot_attrs,
1180 };
1181
1182 static int __init reboot_ksysfs_init(void)
1183 {
1184         struct kobject *reboot_kobj;
1185         int ret;
1186
1187         reboot_kobj = kobject_create_and_add("reboot", kernel_kobj);
1188         if (!reboot_kobj)
1189                 return -ENOMEM;
1190
1191         ret = sysfs_create_group(reboot_kobj, &reboot_attr_group);
1192         if (ret) {
1193                 kobject_put(reboot_kobj);
1194                 return ret;
1195         }
1196
1197         return 0;
1198 }
1199 late_initcall(reboot_ksysfs_init);
1200
1201 #endif