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