Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  linux/kernel/reboot.c
0004  *
0005  *  Copyright (C) 2013  Linus Torvalds
0006  */
0007 
0008 #define pr_fmt(fmt) "reboot: " fmt
0009 
0010 #include <linux/atomic.h>
0011 #include <linux/ctype.h>
0012 #include <linux/export.h>
0013 #include <linux/kexec.h>
0014 #include <linux/kmod.h>
0015 #include <linux/kmsg_dump.h>
0016 #include <linux/reboot.h>
0017 #include <linux/suspend.h>
0018 #include <linux/syscalls.h>
0019 #include <linux/syscore_ops.h>
0020 #include <linux/uaccess.h>
0021 
0022 /*
0023  * this indicates whether you can reboot with ctrl-alt-del: the default is yes
0024  */
0025 
0026 static int C_A_D = 1;
0027 struct pid *cad_pid;
0028 EXPORT_SYMBOL(cad_pid);
0029 
0030 #if defined(CONFIG_ARM)
0031 #define DEFAULT_REBOOT_MODE     = REBOOT_HARD
0032 #else
0033 #define DEFAULT_REBOOT_MODE
0034 #endif
0035 enum reboot_mode reboot_mode DEFAULT_REBOOT_MODE;
0036 EXPORT_SYMBOL_GPL(reboot_mode);
0037 enum reboot_mode panic_reboot_mode = REBOOT_UNDEFINED;
0038 
0039 /*
0040  * This variable is used privately to keep track of whether or not
0041  * reboot_type is still set to its default value (i.e., reboot= hasn't
0042  * been set on the command line).  This is needed so that we can
0043  * suppress DMI scanning for reboot quirks.  Without it, it's
0044  * impossible to override a faulty reboot quirk without recompiling.
0045  */
0046 int reboot_default = 1;
0047 int reboot_cpu;
0048 enum reboot_type reboot_type = BOOT_ACPI;
0049 int reboot_force;
0050 
0051 struct sys_off_handler {
0052     struct notifier_block nb;
0053     int (*sys_off_cb)(struct sys_off_data *data);
0054     void *cb_data;
0055     enum sys_off_mode mode;
0056     bool blocking;
0057     void *list;
0058 };
0059 
0060 /*
0061  * Temporary stub that prevents linkage failure while we're in process
0062  * of removing all uses of legacy pm_power_off() around the kernel.
0063  */
0064 void __weak (*pm_power_off)(void);
0065 
0066 /**
0067  *  emergency_restart - reboot the system
0068  *
0069  *  Without shutting down any hardware or taking any locks
0070  *  reboot the system.  This is called when we know we are in
0071  *  trouble so this is our best effort to reboot.  This is
0072  *  safe to call in interrupt context.
0073  */
0074 void emergency_restart(void)
0075 {
0076     kmsg_dump(KMSG_DUMP_EMERG);
0077     machine_emergency_restart();
0078 }
0079 EXPORT_SYMBOL_GPL(emergency_restart);
0080 
0081 void kernel_restart_prepare(char *cmd)
0082 {
0083     blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
0084     system_state = SYSTEM_RESTART;
0085     usermodehelper_disable();
0086     device_shutdown();
0087 }
0088 
0089 /**
0090  *  register_reboot_notifier - Register function to be called at reboot time
0091  *  @nb: Info about notifier function to be called
0092  *
0093  *  Registers a function with the list of functions
0094  *  to be called at reboot time.
0095  *
0096  *  Currently always returns zero, as blocking_notifier_chain_register()
0097  *  always returns zero.
0098  */
0099 int register_reboot_notifier(struct notifier_block *nb)
0100 {
0101     return blocking_notifier_chain_register(&reboot_notifier_list, nb);
0102 }
0103 EXPORT_SYMBOL(register_reboot_notifier);
0104 
0105 /**
0106  *  unregister_reboot_notifier - Unregister previously registered reboot notifier
0107  *  @nb: Hook to be unregistered
0108  *
0109  *  Unregisters a previously registered reboot
0110  *  notifier function.
0111  *
0112  *  Returns zero on success, or %-ENOENT on failure.
0113  */
0114 int unregister_reboot_notifier(struct notifier_block *nb)
0115 {
0116     return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
0117 }
0118 EXPORT_SYMBOL(unregister_reboot_notifier);
0119 
0120 static void devm_unregister_reboot_notifier(struct device *dev, void *res)
0121 {
0122     WARN_ON(unregister_reboot_notifier(*(struct notifier_block **)res));
0123 }
0124 
0125 int devm_register_reboot_notifier(struct device *dev, struct notifier_block *nb)
0126 {
0127     struct notifier_block **rcnb;
0128     int ret;
0129 
0130     rcnb = devres_alloc(devm_unregister_reboot_notifier,
0131                 sizeof(*rcnb), GFP_KERNEL);
0132     if (!rcnb)
0133         return -ENOMEM;
0134 
0135     ret = register_reboot_notifier(nb);
0136     if (!ret) {
0137         *rcnb = nb;
0138         devres_add(dev, rcnb);
0139     } else {
0140         devres_free(rcnb);
0141     }
0142 
0143     return ret;
0144 }
0145 EXPORT_SYMBOL(devm_register_reboot_notifier);
0146 
0147 /*
0148  *  Notifier list for kernel code which wants to be called
0149  *  to restart the system.
0150  */
0151 static ATOMIC_NOTIFIER_HEAD(restart_handler_list);
0152 
0153 /**
0154  *  register_restart_handler - Register function to be called to reset
0155  *                 the system
0156  *  @nb: Info about handler function to be called
0157  *  @nb->priority:  Handler priority. Handlers should follow the
0158  *          following guidelines for setting priorities.
0159  *          0:  Restart handler of last resort,
0160  *              with limited restart capabilities
0161  *          128:    Default restart handler; use if no other
0162  *              restart handler is expected to be available,
0163  *              and/or if restart functionality is
0164  *              sufficient to restart the entire system
0165  *          255:    Highest priority restart handler, will
0166  *              preempt all other restart handlers
0167  *
0168  *  Registers a function with code to be called to restart the
0169  *  system.
0170  *
0171  *  Registered functions will be called from machine_restart as last
0172  *  step of the restart sequence (if the architecture specific
0173  *  machine_restart function calls do_kernel_restart - see below
0174  *  for details).
0175  *  Registered functions are expected to restart the system immediately.
0176  *  If more than one function is registered, the restart handler priority
0177  *  selects which function will be called first.
0178  *
0179  *  Restart handlers are expected to be registered from non-architecture
0180  *  code, typically from drivers. A typical use case would be a system
0181  *  where restart functionality is provided through a watchdog. Multiple
0182  *  restart handlers may exist; for example, one restart handler might
0183  *  restart the entire system, while another only restarts the CPU.
0184  *  In such cases, the restart handler which only restarts part of the
0185  *  hardware is expected to register with low priority to ensure that
0186  *  it only runs if no other means to restart the system is available.
0187  *
0188  *  Currently always returns zero, as atomic_notifier_chain_register()
0189  *  always returns zero.
0190  */
0191 int register_restart_handler(struct notifier_block *nb)
0192 {
0193     return atomic_notifier_chain_register(&restart_handler_list, nb);
0194 }
0195 EXPORT_SYMBOL(register_restart_handler);
0196 
0197 /**
0198  *  unregister_restart_handler - Unregister previously registered
0199  *                   restart handler
0200  *  @nb: Hook to be unregistered
0201  *
0202  *  Unregisters a previously registered restart handler function.
0203  *
0204  *  Returns zero on success, or %-ENOENT on failure.
0205  */
0206 int unregister_restart_handler(struct notifier_block *nb)
0207 {
0208     return atomic_notifier_chain_unregister(&restart_handler_list, nb);
0209 }
0210 EXPORT_SYMBOL(unregister_restart_handler);
0211 
0212 /**
0213  *  do_kernel_restart - Execute kernel restart handler call chain
0214  *
0215  *  Calls functions registered with register_restart_handler.
0216  *
0217  *  Expected to be called from machine_restart as last step of the restart
0218  *  sequence.
0219  *
0220  *  Restarts the system immediately if a restart handler function has been
0221  *  registered. Otherwise does nothing.
0222  */
0223 void do_kernel_restart(char *cmd)
0224 {
0225     atomic_notifier_call_chain(&restart_handler_list, reboot_mode, cmd);
0226 }
0227 
0228 void migrate_to_reboot_cpu(void)
0229 {
0230     /* The boot cpu is always logical cpu 0 */
0231     int cpu = reboot_cpu;
0232 
0233     cpu_hotplug_disable();
0234 
0235     /* Make certain the cpu I'm about to reboot on is online */
0236     if (!cpu_online(cpu))
0237         cpu = cpumask_first(cpu_online_mask);
0238 
0239     /* Prevent races with other tasks migrating this task */
0240     current->flags |= PF_NO_SETAFFINITY;
0241 
0242     /* Make certain I only run on the appropriate processor */
0243     set_cpus_allowed_ptr(current, cpumask_of(cpu));
0244 }
0245 
0246 /**
0247  *  kernel_restart - reboot the system
0248  *  @cmd: pointer to buffer containing command to execute for restart
0249  *      or %NULL
0250  *
0251  *  Shutdown everything and perform a clean reboot.
0252  *  This is not safe to call in interrupt context.
0253  */
0254 void kernel_restart(char *cmd)
0255 {
0256     kernel_restart_prepare(cmd);
0257     migrate_to_reboot_cpu();
0258     syscore_shutdown();
0259     if (!cmd)
0260         pr_emerg("Restarting system\n");
0261     else
0262         pr_emerg("Restarting system with command '%s'\n", cmd);
0263     kmsg_dump(KMSG_DUMP_SHUTDOWN);
0264     machine_restart(cmd);
0265 }
0266 EXPORT_SYMBOL_GPL(kernel_restart);
0267 
0268 static void kernel_shutdown_prepare(enum system_states state)
0269 {
0270     blocking_notifier_call_chain(&reboot_notifier_list,
0271         (state == SYSTEM_HALT) ? SYS_HALT : SYS_POWER_OFF, NULL);
0272     system_state = state;
0273     usermodehelper_disable();
0274     device_shutdown();
0275 }
0276 /**
0277  *  kernel_halt - halt the system
0278  *
0279  *  Shutdown everything and perform a clean system halt.
0280  */
0281 void kernel_halt(void)
0282 {
0283     kernel_shutdown_prepare(SYSTEM_HALT);
0284     migrate_to_reboot_cpu();
0285     syscore_shutdown();
0286     pr_emerg("System halted\n");
0287     kmsg_dump(KMSG_DUMP_SHUTDOWN);
0288     machine_halt();
0289 }
0290 EXPORT_SYMBOL_GPL(kernel_halt);
0291 
0292 /*
0293  *  Notifier list for kernel code which wants to be called
0294  *  to prepare system for power off.
0295  */
0296 static BLOCKING_NOTIFIER_HEAD(power_off_prep_handler_list);
0297 
0298 /*
0299  *  Notifier list for kernel code which wants to be called
0300  *  to power off system.
0301  */
0302 static ATOMIC_NOTIFIER_HEAD(power_off_handler_list);
0303 
0304 static int sys_off_notify(struct notifier_block *nb,
0305               unsigned long mode, void *cmd)
0306 {
0307     struct sys_off_handler *handler;
0308     struct sys_off_data data = {};
0309 
0310     handler = container_of(nb, struct sys_off_handler, nb);
0311     data.cb_data = handler->cb_data;
0312     data.mode = mode;
0313     data.cmd = cmd;
0314 
0315     return handler->sys_off_cb(&data);
0316 }
0317 
0318 static struct sys_off_handler platform_sys_off_handler;
0319 
0320 static struct sys_off_handler *alloc_sys_off_handler(int priority)
0321 {
0322     struct sys_off_handler *handler;
0323     gfp_t flags;
0324 
0325     /*
0326      * Platforms like m68k can't allocate sys_off handler dynamically
0327      * at the early boot time because memory allocator isn't available yet.
0328      */
0329     if (priority == SYS_OFF_PRIO_PLATFORM) {
0330         handler = &platform_sys_off_handler;
0331         if (handler->cb_data)
0332             return ERR_PTR(-EBUSY);
0333     } else {
0334         if (system_state > SYSTEM_RUNNING)
0335             flags = GFP_ATOMIC;
0336         else
0337             flags = GFP_KERNEL;
0338 
0339         handler = kzalloc(sizeof(*handler), flags);
0340         if (!handler)
0341             return ERR_PTR(-ENOMEM);
0342     }
0343 
0344     return handler;
0345 }
0346 
0347 static void free_sys_off_handler(struct sys_off_handler *handler)
0348 {
0349     if (handler == &platform_sys_off_handler)
0350         memset(handler, 0, sizeof(*handler));
0351     else
0352         kfree(handler);
0353 }
0354 
0355 /**
0356  *  register_sys_off_handler - Register sys-off handler
0357  *  @mode: Sys-off mode
0358  *  @priority: Handler priority
0359  *  @callback: Callback function
0360  *  @cb_data: Callback argument
0361  *
0362  *  Registers system power-off or restart handler that will be invoked
0363  *  at the step corresponding to the given sys-off mode. Handler's callback
0364  *  should return NOTIFY_DONE to permit execution of the next handler in
0365  *  the call chain or NOTIFY_STOP to break the chain (in error case for
0366  *  example).
0367  *
0368  *  Multiple handlers can be registered at the default priority level.
0369  *
0370  *  Only one handler can be registered at the non-default priority level,
0371  *  otherwise ERR_PTR(-EBUSY) is returned.
0372  *
0373  *  Returns a new instance of struct sys_off_handler on success, or
0374  *  an ERR_PTR()-encoded error code otherwise.
0375  */
0376 struct sys_off_handler *
0377 register_sys_off_handler(enum sys_off_mode mode,
0378              int priority,
0379              int (*callback)(struct sys_off_data *data),
0380              void *cb_data)
0381 {
0382     struct sys_off_handler *handler;
0383     int err;
0384 
0385     handler = alloc_sys_off_handler(priority);
0386     if (IS_ERR(handler))
0387         return handler;
0388 
0389     switch (mode) {
0390     case SYS_OFF_MODE_POWER_OFF_PREPARE:
0391         handler->list = &power_off_prep_handler_list;
0392         handler->blocking = true;
0393         break;
0394 
0395     case SYS_OFF_MODE_POWER_OFF:
0396         handler->list = &power_off_handler_list;
0397         break;
0398 
0399     case SYS_OFF_MODE_RESTART:
0400         handler->list = &restart_handler_list;
0401         break;
0402 
0403     default:
0404         free_sys_off_handler(handler);
0405         return ERR_PTR(-EINVAL);
0406     }
0407 
0408     handler->nb.notifier_call = sys_off_notify;
0409     handler->nb.priority = priority;
0410     handler->sys_off_cb = callback;
0411     handler->cb_data = cb_data;
0412     handler->mode = mode;
0413 
0414     if (handler->blocking) {
0415         if (priority == SYS_OFF_PRIO_DEFAULT)
0416             err = blocking_notifier_chain_register(handler->list,
0417                                    &handler->nb);
0418         else
0419             err = blocking_notifier_chain_register_unique_prio(handler->list,
0420                                        &handler->nb);
0421     } else {
0422         if (priority == SYS_OFF_PRIO_DEFAULT)
0423             err = atomic_notifier_chain_register(handler->list,
0424                                  &handler->nb);
0425         else
0426             err = atomic_notifier_chain_register_unique_prio(handler->list,
0427                                      &handler->nb);
0428     }
0429 
0430     if (err) {
0431         free_sys_off_handler(handler);
0432         return ERR_PTR(err);
0433     }
0434 
0435     return handler;
0436 }
0437 EXPORT_SYMBOL_GPL(register_sys_off_handler);
0438 
0439 /**
0440  *  unregister_sys_off_handler - Unregister sys-off handler
0441  *  @handler: Sys-off handler
0442  *
0443  *  Unregisters given sys-off handler.
0444  */
0445 void unregister_sys_off_handler(struct sys_off_handler *handler)
0446 {
0447     int err;
0448 
0449     if (IS_ERR_OR_NULL(handler))
0450         return;
0451 
0452     if (handler->blocking)
0453         err = blocking_notifier_chain_unregister(handler->list,
0454                              &handler->nb);
0455     else
0456         err = atomic_notifier_chain_unregister(handler->list,
0457                                &handler->nb);
0458 
0459     /* sanity check, shall never happen */
0460     WARN_ON(err);
0461 
0462     free_sys_off_handler(handler);
0463 }
0464 EXPORT_SYMBOL_GPL(unregister_sys_off_handler);
0465 
0466 static void devm_unregister_sys_off_handler(void *data)
0467 {
0468     struct sys_off_handler *handler = data;
0469 
0470     unregister_sys_off_handler(handler);
0471 }
0472 
0473 /**
0474  *  devm_register_sys_off_handler - Register sys-off handler
0475  *  @dev: Device that registers handler
0476  *  @mode: Sys-off mode
0477  *  @priority: Handler priority
0478  *  @callback: Callback function
0479  *  @cb_data: Callback argument
0480  *
0481  *  Registers resource-managed sys-off handler.
0482  *
0483  *  Returns zero on success, or error code on failure.
0484  */
0485 int devm_register_sys_off_handler(struct device *dev,
0486                   enum sys_off_mode mode,
0487                   int priority,
0488                   int (*callback)(struct sys_off_data *data),
0489                   void *cb_data)
0490 {
0491     struct sys_off_handler *handler;
0492 
0493     handler = register_sys_off_handler(mode, priority, callback, cb_data);
0494     if (IS_ERR(handler))
0495         return PTR_ERR(handler);
0496 
0497     return devm_add_action_or_reset(dev, devm_unregister_sys_off_handler,
0498                     handler);
0499 }
0500 EXPORT_SYMBOL_GPL(devm_register_sys_off_handler);
0501 
0502 /**
0503  *  devm_register_power_off_handler - Register power-off handler
0504  *  @dev: Device that registers callback
0505  *  @callback: Callback function
0506  *  @cb_data: Callback's argument
0507  *
0508  *  Registers resource-managed sys-off handler with a default priority
0509  *  and using power-off mode.
0510  *
0511  *  Returns zero on success, or error code on failure.
0512  */
0513 int devm_register_power_off_handler(struct device *dev,
0514                     int (*callback)(struct sys_off_data *data),
0515                     void *cb_data)
0516 {
0517     return devm_register_sys_off_handler(dev,
0518                          SYS_OFF_MODE_POWER_OFF,
0519                          SYS_OFF_PRIO_DEFAULT,
0520                          callback, cb_data);
0521 }
0522 EXPORT_SYMBOL_GPL(devm_register_power_off_handler);
0523 
0524 /**
0525  *  devm_register_restart_handler - Register restart handler
0526  *  @dev: Device that registers callback
0527  *  @callback: Callback function
0528  *  @cb_data: Callback's argument
0529  *
0530  *  Registers resource-managed sys-off handler with a default priority
0531  *  and using restart mode.
0532  *
0533  *  Returns zero on success, or error code on failure.
0534  */
0535 int devm_register_restart_handler(struct device *dev,
0536                   int (*callback)(struct sys_off_data *data),
0537                   void *cb_data)
0538 {
0539     return devm_register_sys_off_handler(dev,
0540                          SYS_OFF_MODE_RESTART,
0541                          SYS_OFF_PRIO_DEFAULT,
0542                          callback, cb_data);
0543 }
0544 EXPORT_SYMBOL_GPL(devm_register_restart_handler);
0545 
0546 static struct sys_off_handler *platform_power_off_handler;
0547 
0548 static int platform_power_off_notify(struct sys_off_data *data)
0549 {
0550     void (*platform_power_power_off_cb)(void) = data->cb_data;
0551 
0552     platform_power_power_off_cb();
0553 
0554     return NOTIFY_DONE;
0555 }
0556 
0557 /**
0558  *  register_platform_power_off - Register platform-level power-off callback
0559  *  @power_off: Power-off callback
0560  *
0561  *  Registers power-off callback that will be called as last step
0562  *  of the power-off sequence. This callback is expected to be invoked
0563  *  for the last resort. Only one platform power-off callback is allowed
0564  *  to be registered at a time.
0565  *
0566  *  Returns zero on success, or error code on failure.
0567  */
0568 int register_platform_power_off(void (*power_off)(void))
0569 {
0570     struct sys_off_handler *handler;
0571 
0572     handler = register_sys_off_handler(SYS_OFF_MODE_POWER_OFF,
0573                        SYS_OFF_PRIO_PLATFORM,
0574                        platform_power_off_notify,
0575                        power_off);
0576     if (IS_ERR(handler))
0577         return PTR_ERR(handler);
0578 
0579     platform_power_off_handler = handler;
0580 
0581     return 0;
0582 }
0583 EXPORT_SYMBOL_GPL(register_platform_power_off);
0584 
0585 /**
0586  *  unregister_platform_power_off - Unregister platform-level power-off callback
0587  *  @power_off: Power-off callback
0588  *
0589  *  Unregisters previously registered platform power-off callback.
0590  */
0591 void unregister_platform_power_off(void (*power_off)(void))
0592 {
0593     if (platform_power_off_handler &&
0594         platform_power_off_handler->cb_data == power_off) {
0595         unregister_sys_off_handler(platform_power_off_handler);
0596         platform_power_off_handler = NULL;
0597     }
0598 }
0599 EXPORT_SYMBOL_GPL(unregister_platform_power_off);
0600 
0601 static int legacy_pm_power_off(struct sys_off_data *data)
0602 {
0603     if (pm_power_off)
0604         pm_power_off();
0605 
0606     return NOTIFY_DONE;
0607 }
0608 
0609 static void do_kernel_power_off_prepare(void)
0610 {
0611     blocking_notifier_call_chain(&power_off_prep_handler_list, 0, NULL);
0612 }
0613 
0614 /**
0615  *  do_kernel_power_off - Execute kernel power-off handler call chain
0616  *
0617  *  Expected to be called as last step of the power-off sequence.
0618  *
0619  *  Powers off the system immediately if a power-off handler function has
0620  *  been registered. Otherwise does nothing.
0621  */
0622 void do_kernel_power_off(void)
0623 {
0624     struct sys_off_handler *sys_off = NULL;
0625 
0626     /*
0627      * Register sys-off handlers for legacy PM callback. This allows
0628      * legacy PM callbacks temporary co-exist with the new sys-off API.
0629      *
0630      * TODO: Remove legacy handlers once all legacy PM users will be
0631      *       switched to the sys-off based APIs.
0632      */
0633     if (pm_power_off)
0634         sys_off = register_sys_off_handler(SYS_OFF_MODE_POWER_OFF,
0635                            SYS_OFF_PRIO_DEFAULT,
0636                            legacy_pm_power_off, NULL);
0637 
0638     atomic_notifier_call_chain(&power_off_handler_list, 0, NULL);
0639 
0640     unregister_sys_off_handler(sys_off);
0641 }
0642 
0643 /**
0644  *  kernel_can_power_off - check whether system can be powered off
0645  *
0646  *  Returns true if power-off handler is registered and system can be
0647  *  powered off, false otherwise.
0648  */
0649 bool kernel_can_power_off(void)
0650 {
0651     return !atomic_notifier_call_chain_is_empty(&power_off_handler_list) ||
0652         pm_power_off;
0653 }
0654 EXPORT_SYMBOL_GPL(kernel_can_power_off);
0655 
0656 /**
0657  *  kernel_power_off - power_off the system
0658  *
0659  *  Shutdown everything and perform a clean system power_off.
0660  */
0661 void kernel_power_off(void)
0662 {
0663     kernel_shutdown_prepare(SYSTEM_POWER_OFF);
0664     do_kernel_power_off_prepare();
0665     migrate_to_reboot_cpu();
0666     syscore_shutdown();
0667     pr_emerg("Power down\n");
0668     kmsg_dump(KMSG_DUMP_SHUTDOWN);
0669     machine_power_off();
0670 }
0671 EXPORT_SYMBOL_GPL(kernel_power_off);
0672 
0673 DEFINE_MUTEX(system_transition_mutex);
0674 
0675 /*
0676  * Reboot system call: for obvious reasons only root may call it,
0677  * and even root needs to set up some magic numbers in the registers
0678  * so that some mistake won't make this reboot the whole machine.
0679  * You can also set the meaning of the ctrl-alt-del-key here.
0680  *
0681  * reboot doesn't sync: do that yourself before calling this.
0682  */
0683 SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
0684         void __user *, arg)
0685 {
0686     struct pid_namespace *pid_ns = task_active_pid_ns(current);
0687     char buffer[256];
0688     int ret = 0;
0689 
0690     /* We only trust the superuser with rebooting the system. */
0691     if (!ns_capable(pid_ns->user_ns, CAP_SYS_BOOT))
0692         return -EPERM;
0693 
0694     /* For safety, we require "magic" arguments. */
0695     if (magic1 != LINUX_REBOOT_MAGIC1 ||
0696             (magic2 != LINUX_REBOOT_MAGIC2 &&
0697             magic2 != LINUX_REBOOT_MAGIC2A &&
0698             magic2 != LINUX_REBOOT_MAGIC2B &&
0699             magic2 != LINUX_REBOOT_MAGIC2C))
0700         return -EINVAL;
0701 
0702     /*
0703      * If pid namespaces are enabled and the current task is in a child
0704      * pid_namespace, the command is handled by reboot_pid_ns() which will
0705      * call do_exit().
0706      */
0707     ret = reboot_pid_ns(pid_ns, cmd);
0708     if (ret)
0709         return ret;
0710 
0711     /* Instead of trying to make the power_off code look like
0712      * halt when pm_power_off is not set do it the easy way.
0713      */
0714     if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !kernel_can_power_off())
0715         cmd = LINUX_REBOOT_CMD_HALT;
0716 
0717     mutex_lock(&system_transition_mutex);
0718     switch (cmd) {
0719     case LINUX_REBOOT_CMD_RESTART:
0720         kernel_restart(NULL);
0721         break;
0722 
0723     case LINUX_REBOOT_CMD_CAD_ON:
0724         C_A_D = 1;
0725         break;
0726 
0727     case LINUX_REBOOT_CMD_CAD_OFF:
0728         C_A_D = 0;
0729         break;
0730 
0731     case LINUX_REBOOT_CMD_HALT:
0732         kernel_halt();
0733         do_exit(0);
0734 
0735     case LINUX_REBOOT_CMD_POWER_OFF:
0736         kernel_power_off();
0737         do_exit(0);
0738         break;
0739 
0740     case LINUX_REBOOT_CMD_RESTART2:
0741         ret = strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1);
0742         if (ret < 0) {
0743             ret = -EFAULT;
0744             break;
0745         }
0746         buffer[sizeof(buffer) - 1] = '\0';
0747 
0748         kernel_restart(buffer);
0749         break;
0750 
0751 #ifdef CONFIG_KEXEC_CORE
0752     case LINUX_REBOOT_CMD_KEXEC:
0753         ret = kernel_kexec();
0754         break;
0755 #endif
0756 
0757 #ifdef CONFIG_HIBERNATION
0758     case LINUX_REBOOT_CMD_SW_SUSPEND:
0759         ret = hibernate();
0760         break;
0761 #endif
0762 
0763     default:
0764         ret = -EINVAL;
0765         break;
0766     }
0767     mutex_unlock(&system_transition_mutex);
0768     return ret;
0769 }
0770 
0771 static void deferred_cad(struct work_struct *dummy)
0772 {
0773     kernel_restart(NULL);
0774 }
0775 
0776 /*
0777  * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
0778  * As it's called within an interrupt, it may NOT sync: the only choice
0779  * is whether to reboot at once, or just ignore the ctrl-alt-del.
0780  */
0781 void ctrl_alt_del(void)
0782 {
0783     static DECLARE_WORK(cad_work, deferred_cad);
0784 
0785     if (C_A_D)
0786         schedule_work(&cad_work);
0787     else
0788         kill_cad_pid(SIGINT, 1);
0789 }
0790 
0791 #define POWEROFF_CMD_PATH_LEN  256
0792 static char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
0793 static const char reboot_cmd[] = "/sbin/reboot";
0794 
0795 static int run_cmd(const char *cmd)
0796 {
0797     char **argv;
0798     static char *envp[] = {
0799         "HOME=/",
0800         "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
0801         NULL
0802     };
0803     int ret;
0804     argv = argv_split(GFP_KERNEL, cmd, NULL);
0805     if (argv) {
0806         ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
0807         argv_free(argv);
0808     } else {
0809         ret = -ENOMEM;
0810     }
0811 
0812     return ret;
0813 }
0814 
0815 static int __orderly_reboot(void)
0816 {
0817     int ret;
0818 
0819     ret = run_cmd(reboot_cmd);
0820 
0821     if (ret) {
0822         pr_warn("Failed to start orderly reboot: forcing the issue\n");
0823         emergency_sync();
0824         kernel_restart(NULL);
0825     }
0826 
0827     return ret;
0828 }
0829 
0830 static int __orderly_poweroff(bool force)
0831 {
0832     int ret;
0833 
0834     ret = run_cmd(poweroff_cmd);
0835 
0836     if (ret && force) {
0837         pr_warn("Failed to start orderly shutdown: forcing the issue\n");
0838 
0839         /*
0840          * I guess this should try to kick off some daemon to sync and
0841          * poweroff asap.  Or not even bother syncing if we're doing an
0842          * emergency shutdown?
0843          */
0844         emergency_sync();
0845         kernel_power_off();
0846     }
0847 
0848     return ret;
0849 }
0850 
0851 static bool poweroff_force;
0852 
0853 static void poweroff_work_func(struct work_struct *work)
0854 {
0855     __orderly_poweroff(poweroff_force);
0856 }
0857 
0858 static DECLARE_WORK(poweroff_work, poweroff_work_func);
0859 
0860 /**
0861  * orderly_poweroff - Trigger an orderly system poweroff
0862  * @force: force poweroff if command execution fails
0863  *
0864  * This may be called from any context to trigger a system shutdown.
0865  * If the orderly shutdown fails, it will force an immediate shutdown.
0866  */
0867 void orderly_poweroff(bool force)
0868 {
0869     if (force) /* do not override the pending "true" */
0870         poweroff_force = true;
0871     schedule_work(&poweroff_work);
0872 }
0873 EXPORT_SYMBOL_GPL(orderly_poweroff);
0874 
0875 static void reboot_work_func(struct work_struct *work)
0876 {
0877     __orderly_reboot();
0878 }
0879 
0880 static DECLARE_WORK(reboot_work, reboot_work_func);
0881 
0882 /**
0883  * orderly_reboot - Trigger an orderly system reboot
0884  *
0885  * This may be called from any context to trigger a system reboot.
0886  * If the orderly reboot fails, it will force an immediate reboot.
0887  */
0888 void orderly_reboot(void)
0889 {
0890     schedule_work(&reboot_work);
0891 }
0892 EXPORT_SYMBOL_GPL(orderly_reboot);
0893 
0894 /**
0895  * hw_failure_emergency_poweroff_func - emergency poweroff work after a known delay
0896  * @work: work_struct associated with the emergency poweroff function
0897  *
0898  * This function is called in very critical situations to force
0899  * a kernel poweroff after a configurable timeout value.
0900  */
0901 static void hw_failure_emergency_poweroff_func(struct work_struct *work)
0902 {
0903     /*
0904      * We have reached here after the emergency shutdown waiting period has
0905      * expired. This means orderly_poweroff has not been able to shut off
0906      * the system for some reason.
0907      *
0908      * Try to shut down the system immediately using kernel_power_off
0909      * if populated
0910      */
0911     pr_emerg("Hardware protection timed-out. Trying forced poweroff\n");
0912     kernel_power_off();
0913 
0914     /*
0915      * Worst of the worst case trigger emergency restart
0916      */
0917     pr_emerg("Hardware protection shutdown failed. Trying emergency restart\n");
0918     emergency_restart();
0919 }
0920 
0921 static DECLARE_DELAYED_WORK(hw_failure_emergency_poweroff_work,
0922                 hw_failure_emergency_poweroff_func);
0923 
0924 /**
0925  * hw_failure_emergency_poweroff - Trigger an emergency system poweroff
0926  *
0927  * This may be called from any critical situation to trigger a system shutdown
0928  * after a given period of time. If time is negative this is not scheduled.
0929  */
0930 static void hw_failure_emergency_poweroff(int poweroff_delay_ms)
0931 {
0932     if (poweroff_delay_ms <= 0)
0933         return;
0934     schedule_delayed_work(&hw_failure_emergency_poweroff_work,
0935                   msecs_to_jiffies(poweroff_delay_ms));
0936 }
0937 
0938 /**
0939  * hw_protection_shutdown - Trigger an emergency system poweroff
0940  *
0941  * @reason:     Reason of emergency shutdown to be printed.
0942  * @ms_until_forced:    Time to wait for orderly shutdown before tiggering a
0943  *          forced shudown. Negative value disables the forced
0944  *          shutdown.
0945  *
0946  * Initiate an emergency system shutdown in order to protect hardware from
0947  * further damage. Usage examples include a thermal protection or a voltage or
0948  * current regulator failures.
0949  * NOTE: The request is ignored if protection shutdown is already pending even
0950  * if the previous request has given a large timeout for forced shutdown.
0951  * Can be called from any context.
0952  */
0953 void hw_protection_shutdown(const char *reason, int ms_until_forced)
0954 {
0955     static atomic_t allow_proceed = ATOMIC_INIT(1);
0956 
0957     pr_emerg("HARDWARE PROTECTION shutdown (%s)\n", reason);
0958 
0959     /* Shutdown should be initiated only once. */
0960     if (!atomic_dec_and_test(&allow_proceed))
0961         return;
0962 
0963     /*
0964      * Queue a backup emergency shutdown in the event of
0965      * orderly_poweroff failure
0966      */
0967     hw_failure_emergency_poweroff(ms_until_forced);
0968     orderly_poweroff(true);
0969 }
0970 EXPORT_SYMBOL_GPL(hw_protection_shutdown);
0971 
0972 static int __init reboot_setup(char *str)
0973 {
0974     for (;;) {
0975         enum reboot_mode *mode;
0976 
0977         /*
0978          * Having anything passed on the command line via
0979          * reboot= will cause us to disable DMI checking
0980          * below.
0981          */
0982         reboot_default = 0;
0983 
0984         if (!strncmp(str, "panic_", 6)) {
0985             mode = &panic_reboot_mode;
0986             str += 6;
0987         } else {
0988             mode = &reboot_mode;
0989         }
0990 
0991         switch (*str) {
0992         case 'w':
0993             *mode = REBOOT_WARM;
0994             break;
0995 
0996         case 'c':
0997             *mode = REBOOT_COLD;
0998             break;
0999 
1000         case 'h':
1001             *mode = REBOOT_HARD;
1002             break;
1003 
1004         case 's':
1005             /*
1006              * reboot_cpu is s[mp]#### with #### being the processor
1007              * to be used for rebooting. Skip 's' or 'smp' prefix.
1008              */
1009             str += str[1] == 'm' && str[2] == 'p' ? 3 : 1;
1010 
1011             if (isdigit(str[0])) {
1012                 int cpu = simple_strtoul(str, NULL, 0);
1013 
1014                 if (cpu >= num_possible_cpus()) {
1015                     pr_err("Ignoring the CPU number in reboot= option. "
1016                     "CPU %d exceeds possible cpu number %d\n",
1017                     cpu, num_possible_cpus());
1018                     break;
1019                 }
1020                 reboot_cpu = cpu;
1021             } else
1022                 *mode = REBOOT_SOFT;
1023             break;
1024 
1025         case 'g':
1026             *mode = REBOOT_GPIO;
1027             break;
1028 
1029         case 'b':
1030         case 'a':
1031         case 'k':
1032         case 't':
1033         case 'e':
1034         case 'p':
1035             reboot_type = *str;
1036             break;
1037 
1038         case 'f':
1039             reboot_force = 1;
1040             break;
1041         }
1042 
1043         str = strchr(str, ',');
1044         if (str)
1045             str++;
1046         else
1047             break;
1048     }
1049     return 1;
1050 }
1051 __setup("reboot=", reboot_setup);
1052 
1053 #ifdef CONFIG_SYSFS
1054 
1055 #define REBOOT_COLD_STR     "cold"
1056 #define REBOOT_WARM_STR     "warm"
1057 #define REBOOT_HARD_STR     "hard"
1058 #define REBOOT_SOFT_STR     "soft"
1059 #define REBOOT_GPIO_STR     "gpio"
1060 #define REBOOT_UNDEFINED_STR    "undefined"
1061 
1062 #define BOOT_TRIPLE_STR     "triple"
1063 #define BOOT_KBD_STR        "kbd"
1064 #define BOOT_BIOS_STR       "bios"
1065 #define BOOT_ACPI_STR       "acpi"
1066 #define BOOT_EFI_STR        "efi"
1067 #define BOOT_PCI_STR        "pci"
1068 
1069 static ssize_t mode_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
1070 {
1071     const char *val;
1072 
1073     switch (reboot_mode) {
1074     case REBOOT_COLD:
1075         val = REBOOT_COLD_STR;
1076         break;
1077     case REBOOT_WARM:
1078         val = REBOOT_WARM_STR;
1079         break;
1080     case REBOOT_HARD:
1081         val = REBOOT_HARD_STR;
1082         break;
1083     case REBOOT_SOFT:
1084         val = REBOOT_SOFT_STR;
1085         break;
1086     case REBOOT_GPIO:
1087         val = REBOOT_GPIO_STR;
1088         break;
1089     default:
1090         val = REBOOT_UNDEFINED_STR;
1091     }
1092 
1093     return sprintf(buf, "%s\n", val);
1094 }
1095 static ssize_t mode_store(struct kobject *kobj, struct kobj_attribute *attr,
1096               const char *buf, size_t count)
1097 {
1098     if (!capable(CAP_SYS_BOOT))
1099         return -EPERM;
1100 
1101     if (!strncmp(buf, REBOOT_COLD_STR, strlen(REBOOT_COLD_STR)))
1102         reboot_mode = REBOOT_COLD;
1103     else if (!strncmp(buf, REBOOT_WARM_STR, strlen(REBOOT_WARM_STR)))
1104         reboot_mode = REBOOT_WARM;
1105     else if (!strncmp(buf, REBOOT_HARD_STR, strlen(REBOOT_HARD_STR)))
1106         reboot_mode = REBOOT_HARD;
1107     else if (!strncmp(buf, REBOOT_SOFT_STR, strlen(REBOOT_SOFT_STR)))
1108         reboot_mode = REBOOT_SOFT;
1109     else if (!strncmp(buf, REBOOT_GPIO_STR, strlen(REBOOT_GPIO_STR)))
1110         reboot_mode = REBOOT_GPIO;
1111     else
1112         return -EINVAL;
1113 
1114     reboot_default = 0;
1115 
1116     return count;
1117 }
1118 static struct kobj_attribute reboot_mode_attr = __ATTR_RW(mode);
1119 
1120 #ifdef CONFIG_X86
1121 static ssize_t force_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
1122 {
1123     return sprintf(buf, "%d\n", reboot_force);
1124 }
1125 static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr,
1126               const char *buf, size_t count)
1127 {
1128     bool res;
1129 
1130     if (!capable(CAP_SYS_BOOT))
1131         return -EPERM;
1132 
1133     if (kstrtobool(buf, &res))
1134         return -EINVAL;
1135 
1136     reboot_default = 0;
1137     reboot_force = res;
1138 
1139     return count;
1140 }
1141 static struct kobj_attribute reboot_force_attr = __ATTR_RW(force);
1142 
1143 static ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
1144 {
1145     const char *val;
1146 
1147     switch (reboot_type) {
1148     case BOOT_TRIPLE:
1149         val = BOOT_TRIPLE_STR;
1150         break;
1151     case BOOT_KBD:
1152         val = BOOT_KBD_STR;
1153         break;
1154     case BOOT_BIOS:
1155         val = BOOT_BIOS_STR;
1156         break;
1157     case BOOT_ACPI:
1158         val = BOOT_ACPI_STR;
1159         break;
1160     case BOOT_EFI:
1161         val = BOOT_EFI_STR;
1162         break;
1163     case BOOT_CF9_FORCE:
1164         val = BOOT_PCI_STR;
1165         break;
1166     default:
1167         val = REBOOT_UNDEFINED_STR;
1168     }
1169 
1170     return sprintf(buf, "%s\n", val);
1171 }
1172 static ssize_t type_store(struct kobject *kobj, struct kobj_attribute *attr,
1173               const char *buf, size_t count)
1174 {
1175     if (!capable(CAP_SYS_BOOT))
1176         return -EPERM;
1177 
1178     if (!strncmp(buf, BOOT_TRIPLE_STR, strlen(BOOT_TRIPLE_STR)))
1179         reboot_type = BOOT_TRIPLE;
1180     else if (!strncmp(buf, BOOT_KBD_STR, strlen(BOOT_KBD_STR)))
1181         reboot_type = BOOT_KBD;
1182     else if (!strncmp(buf, BOOT_BIOS_STR, strlen(BOOT_BIOS_STR)))
1183         reboot_type = BOOT_BIOS;
1184     else if (!strncmp(buf, BOOT_ACPI_STR, strlen(BOOT_ACPI_STR)))
1185         reboot_type = BOOT_ACPI;
1186     else if (!strncmp(buf, BOOT_EFI_STR, strlen(BOOT_EFI_STR)))
1187         reboot_type = BOOT_EFI;
1188     else if (!strncmp(buf, BOOT_PCI_STR, strlen(BOOT_PCI_STR)))
1189         reboot_type = BOOT_CF9_FORCE;
1190     else
1191         return -EINVAL;
1192 
1193     reboot_default = 0;
1194 
1195     return count;
1196 }
1197 static struct kobj_attribute reboot_type_attr = __ATTR_RW(type);
1198 #endif
1199 
1200 #ifdef CONFIG_SMP
1201 static ssize_t cpu_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
1202 {
1203     return sprintf(buf, "%d\n", reboot_cpu);
1204 }
1205 static ssize_t cpu_store(struct kobject *kobj, struct kobj_attribute *attr,
1206               const char *buf, size_t count)
1207 {
1208     unsigned int cpunum;
1209     int rc;
1210 
1211     if (!capable(CAP_SYS_BOOT))
1212         return -EPERM;
1213 
1214     rc = kstrtouint(buf, 0, &cpunum);
1215 
1216     if (rc)
1217         return rc;
1218 
1219     if (cpunum >= num_possible_cpus())
1220         return -ERANGE;
1221 
1222     reboot_default = 0;
1223     reboot_cpu = cpunum;
1224 
1225     return count;
1226 }
1227 static struct kobj_attribute reboot_cpu_attr = __ATTR_RW(cpu);
1228 #endif
1229 
1230 static struct attribute *reboot_attrs[] = {
1231     &reboot_mode_attr.attr,
1232 #ifdef CONFIG_X86
1233     &reboot_force_attr.attr,
1234     &reboot_type_attr.attr,
1235 #endif
1236 #ifdef CONFIG_SMP
1237     &reboot_cpu_attr.attr,
1238 #endif
1239     NULL,
1240 };
1241 
1242 #ifdef CONFIG_SYSCTL
1243 static struct ctl_table kern_reboot_table[] = {
1244     {
1245         .procname       = "poweroff_cmd",
1246         .data           = &poweroff_cmd,
1247         .maxlen         = POWEROFF_CMD_PATH_LEN,
1248         .mode           = 0644,
1249         .proc_handler   = proc_dostring,
1250     },
1251     {
1252         .procname       = "ctrl-alt-del",
1253         .data           = &C_A_D,
1254         .maxlen         = sizeof(int),
1255         .mode           = 0644,
1256         .proc_handler   = proc_dointvec,
1257     },
1258     { }
1259 };
1260 
1261 static void __init kernel_reboot_sysctls_init(void)
1262 {
1263     register_sysctl_init("kernel", kern_reboot_table);
1264 }
1265 #else
1266 #define kernel_reboot_sysctls_init() do { } while (0)
1267 #endif /* CONFIG_SYSCTL */
1268 
1269 static const struct attribute_group reboot_attr_group = {
1270     .attrs = reboot_attrs,
1271 };
1272 
1273 static int __init reboot_ksysfs_init(void)
1274 {
1275     struct kobject *reboot_kobj;
1276     int ret;
1277 
1278     reboot_kobj = kobject_create_and_add("reboot", kernel_kobj);
1279     if (!reboot_kobj)
1280         return -ENOMEM;
1281 
1282     ret = sysfs_create_group(reboot_kobj, &reboot_attr_group);
1283     if (ret) {
1284         kobject_put(reboot_kobj);
1285         return ret;
1286     }
1287 
1288     kernel_reboot_sysctls_init();
1289 
1290     return 0;
1291 }
1292 late_initcall(reboot_ksysfs_init);
1293 
1294 #endif