reboot.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*
  2. * linux/kernel/reboot.c
  3. *
  4. * Copyright (C) 2013 Linus Torvalds
  5. */
  6. #define pr_fmt(fmt) "reboot: " fmt
  7. #include <linux/ctype.h>
  8. #include <linux/export.h>
  9. #include <linux/kexec.h>
  10. #include <linux/kmod.h>
  11. #include <linux/kmsg_dump.h>
  12. #include <linux/reboot.h>
  13. #include <linux/suspend.h>
  14. #include <linux/syscalls.h>
  15. #include <linux/syscore_ops.h>
  16. #include <linux/uaccess.h>
  17. /*
  18. * this indicates whether you can reboot with ctrl-alt-del: the default is yes
  19. */
  20. int C_A_D = 1;
  21. struct pid *cad_pid;
  22. EXPORT_SYMBOL(cad_pid);
  23. #if defined(CONFIG_ARM) || defined(CONFIG_UNICORE32)
  24. #define DEFAULT_REBOOT_MODE = REBOOT_HARD
  25. #else
  26. #define DEFAULT_REBOOT_MODE
  27. #endif
  28. enum reboot_mode reboot_mode DEFAULT_REBOOT_MODE;
  29. /*
  30. * This variable is used privately to keep track of whether or not
  31. * reboot_type is still set to its default value (i.e., reboot= hasn't
  32. * been set on the command line). This is needed so that we can
  33. * suppress DMI scanning for reboot quirks. Without it, it's
  34. * impossible to override a faulty reboot quirk without recompiling.
  35. */
  36. int reboot_default = 1;
  37. int reboot_cpu;
  38. enum reboot_type reboot_type = BOOT_ACPI;
  39. int reboot_force;
  40. /*
  41. * If set, this is used for preparing the system to power off.
  42. */
  43. void (*pm_power_off_prepare)(void);
  44. EXPORT_SYMBOL_GPL(pm_power_off_prepare);
  45. /**
  46. * emergency_restart - reboot the system
  47. *
  48. * Without shutting down any hardware or taking any locks
  49. * reboot the system. This is called when we know we are in
  50. * trouble so this is our best effort to reboot. This is
  51. * safe to call in interrupt context.
  52. */
  53. void emergency_restart(void)
  54. {
  55. kmsg_dump(KMSG_DUMP_EMERG);
  56. machine_emergency_restart();
  57. }
  58. EXPORT_SYMBOL_GPL(emergency_restart);
  59. void kernel_restart_prepare(char *cmd)
  60. {
  61. blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
  62. system_state = SYSTEM_RESTART;
  63. usermodehelper_disable();
  64. device_shutdown();
  65. }
  66. /**
  67. * register_reboot_notifier - Register function to be called at reboot time
  68. * @nb: Info about notifier function to be called
  69. *
  70. * Registers a function with the list of functions
  71. * to be called at reboot time.
  72. *
  73. * Currently always returns zero, as blocking_notifier_chain_register()
  74. * always returns zero.
  75. */
  76. int register_reboot_notifier(struct notifier_block *nb)
  77. {
  78. return blocking_notifier_chain_register(&reboot_notifier_list, nb);
  79. }
  80. EXPORT_SYMBOL(register_reboot_notifier);
  81. /**
  82. * unregister_reboot_notifier - Unregister previously registered reboot notifier
  83. * @nb: Hook to be unregistered
  84. *
  85. * Unregisters a previously registered reboot
  86. * notifier function.
  87. *
  88. * Returns zero on success, or %-ENOENT on failure.
  89. */
  90. int unregister_reboot_notifier(struct notifier_block *nb)
  91. {
  92. return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
  93. }
  94. EXPORT_SYMBOL(unregister_reboot_notifier);
  95. static void devm_unregister_reboot_notifier(struct device *dev, void *res)
  96. {
  97. WARN_ON(unregister_reboot_notifier(*(struct notifier_block **)res));
  98. }
  99. int devm_register_reboot_notifier(struct device *dev, struct notifier_block *nb)
  100. {
  101. struct notifier_block **rcnb;
  102. int ret;
  103. rcnb = devres_alloc(devm_unregister_reboot_notifier,
  104. sizeof(*rcnb), GFP_KERNEL);
  105. if (!rcnb)
  106. return -ENOMEM;
  107. ret = register_reboot_notifier(nb);
  108. if (!ret) {
  109. *rcnb = nb;
  110. devres_add(dev, rcnb);
  111. } else {
  112. devres_free(rcnb);
  113. }
  114. return ret;
  115. }
  116. EXPORT_SYMBOL(devm_register_reboot_notifier);
  117. /*
  118. * Notifier list for kernel code which wants to be called
  119. * to restart the system.
  120. */
  121. static ATOMIC_NOTIFIER_HEAD(restart_handler_list);
  122. /**
  123. * register_restart_handler - Register function to be called to reset
  124. * the system
  125. * @nb: Info about handler function to be called
  126. * @nb->priority: Handler priority. Handlers should follow the
  127. * following guidelines for setting priorities.
  128. * 0: Restart handler of last resort,
  129. * with limited restart capabilities
  130. * 128: Default restart handler; use if no other
  131. * restart handler is expected to be available,
  132. * and/or if restart functionality is
  133. * sufficient to restart the entire system
  134. * 255: Highest priority restart handler, will
  135. * preempt all other restart handlers
  136. *
  137. * Registers a function with code to be called to restart the
  138. * system.
  139. *
  140. * Registered functions will be called from machine_restart as last
  141. * step of the restart sequence (if the architecture specific
  142. * machine_restart function calls do_kernel_restart - see below
  143. * for details).
  144. * Registered functions are expected to restart the system immediately.
  145. * If more than one function is registered, the restart handler priority
  146. * selects which function will be called first.
  147. *
  148. * Restart handlers are expected to be registered from non-architecture
  149. * code, typically from drivers. A typical use case would be a system
  150. * where restart functionality is provided through a watchdog. Multiple
  151. * restart handlers may exist; for example, one restart handler might
  152. * restart the entire system, while another only restarts the CPU.
  153. * In such cases, the restart handler which only restarts part of the
  154. * hardware is expected to register with low priority to ensure that
  155. * it only runs if no other means to restart the system is available.
  156. *
  157. * Currently always returns zero, as atomic_notifier_chain_register()
  158. * always returns zero.
  159. */
  160. int register_restart_handler(struct notifier_block *nb)
  161. {
  162. return atomic_notifier_chain_register(&restart_handler_list, nb);
  163. }
  164. EXPORT_SYMBOL(register_restart_handler);
  165. /**
  166. * unregister_restart_handler - Unregister previously registered
  167. * restart handler
  168. * @nb: Hook to be unregistered
  169. *
  170. * Unregisters a previously registered restart handler function.
  171. *
  172. * Returns zero on success, or %-ENOENT on failure.
  173. */
  174. int unregister_restart_handler(struct notifier_block *nb)
  175. {
  176. return atomic_notifier_chain_unregister(&restart_handler_list, nb);
  177. }
  178. EXPORT_SYMBOL(unregister_restart_handler);
  179. /**
  180. * do_kernel_restart - Execute kernel restart handler call chain
  181. *
  182. * Calls functions registered with register_restart_handler.
  183. *
  184. * Expected to be called from machine_restart as last step of the restart
  185. * sequence.
  186. *
  187. * Restarts the system immediately if a restart handler function has been
  188. * registered. Otherwise does nothing.
  189. */
  190. void do_kernel_restart(char *cmd)
  191. {
  192. atomic_notifier_call_chain(&restart_handler_list, reboot_mode, cmd);
  193. }
  194. void migrate_to_reboot_cpu(void)
  195. {
  196. /* The boot cpu is always logical cpu 0 */
  197. int cpu = reboot_cpu;
  198. cpu_hotplug_disable();
  199. /* Make certain the cpu I'm about to reboot on is online */
  200. if (!cpu_online(cpu))
  201. cpu = cpumask_first(cpu_online_mask);
  202. /* Prevent races with other tasks migrating this task */
  203. current->flags |= PF_NO_SETAFFINITY;
  204. /* Make certain I only run on the appropriate processor */
  205. set_cpus_allowed_ptr(current, cpumask_of(cpu));
  206. }
  207. /**
  208. * kernel_restart - reboot the system
  209. * @cmd: pointer to buffer containing command to execute for restart
  210. * or %NULL
  211. *
  212. * Shutdown everything and perform a clean reboot.
  213. * This is not safe to call in interrupt context.
  214. */
  215. void kernel_restart(char *cmd)
  216. {
  217. kernel_restart_prepare(cmd);
  218. migrate_to_reboot_cpu();
  219. syscore_shutdown();
  220. if (!cmd)
  221. pr_emerg("Restarting system\n");
  222. else
  223. pr_emerg("Restarting system with command '%s'\n", cmd);
  224. kmsg_dump(KMSG_DUMP_RESTART);
  225. machine_restart(cmd);
  226. }
  227. EXPORT_SYMBOL_GPL(kernel_restart);
  228. static void kernel_shutdown_prepare(enum system_states state)
  229. {
  230. blocking_notifier_call_chain(&reboot_notifier_list,
  231. (state == SYSTEM_HALT) ? SYS_HALT : SYS_POWER_OFF, NULL);
  232. system_state = state;
  233. usermodehelper_disable();
  234. device_shutdown();
  235. }
  236. /**
  237. * kernel_halt - halt the system
  238. *
  239. * Shutdown everything and perform a clean system halt.
  240. */
  241. void kernel_halt(void)
  242. {
  243. kernel_shutdown_prepare(SYSTEM_HALT);
  244. migrate_to_reboot_cpu();
  245. syscore_shutdown();
  246. pr_emerg("System halted\n");
  247. kmsg_dump(KMSG_DUMP_HALT);
  248. machine_halt();
  249. }
  250. EXPORT_SYMBOL_GPL(kernel_halt);
  251. /**
  252. * kernel_power_off - power_off the system
  253. *
  254. * Shutdown everything and perform a clean system power_off.
  255. */
  256. void kernel_power_off(void)
  257. {
  258. kernel_shutdown_prepare(SYSTEM_POWER_OFF);
  259. if (pm_power_off_prepare)
  260. pm_power_off_prepare();
  261. migrate_to_reboot_cpu();
  262. syscore_shutdown();
  263. pr_emerg("Power down\n");
  264. kmsg_dump(KMSG_DUMP_POWEROFF);
  265. machine_power_off();
  266. }
  267. EXPORT_SYMBOL_GPL(kernel_power_off);
  268. DEFINE_MUTEX(system_transition_mutex);
  269. /*
  270. * Reboot system call: for obvious reasons only root may call it,
  271. * and even root needs to set up some magic numbers in the registers
  272. * so that some mistake won't make this reboot the whole machine.
  273. * You can also set the meaning of the ctrl-alt-del-key here.
  274. *
  275. * reboot doesn't sync: do that yourself before calling this.
  276. */
  277. SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
  278. void __user *, arg)
  279. {
  280. struct pid_namespace *pid_ns = task_active_pid_ns(current);
  281. char buffer[256];
  282. int ret = 0;
  283. /* We only trust the superuser with rebooting the system. */
  284. if (!ns_capable(pid_ns->user_ns, CAP_SYS_BOOT))
  285. return -EPERM;
  286. /* For safety, we require "magic" arguments. */
  287. if (magic1 != LINUX_REBOOT_MAGIC1 ||
  288. (magic2 != LINUX_REBOOT_MAGIC2 &&
  289. magic2 != LINUX_REBOOT_MAGIC2A &&
  290. magic2 != LINUX_REBOOT_MAGIC2B &&
  291. magic2 != LINUX_REBOOT_MAGIC2C))
  292. return -EINVAL;
  293. /*
  294. * If pid namespaces are enabled and the current task is in a child
  295. * pid_namespace, the command is handled by reboot_pid_ns() which will
  296. * call do_exit().
  297. */
  298. ret = reboot_pid_ns(pid_ns, cmd);
  299. if (ret)
  300. return ret;
  301. /* Instead of trying to make the power_off code look like
  302. * halt when pm_power_off is not set do it the easy way.
  303. */
  304. if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
  305. cmd = LINUX_REBOOT_CMD_HALT;
  306. mutex_lock(&system_transition_mutex);
  307. switch (cmd) {
  308. case LINUX_REBOOT_CMD_RESTART:
  309. kernel_restart(NULL);
  310. break;
  311. case LINUX_REBOOT_CMD_CAD_ON:
  312. C_A_D = 1;
  313. break;
  314. case LINUX_REBOOT_CMD_CAD_OFF:
  315. C_A_D = 0;
  316. break;
  317. case LINUX_REBOOT_CMD_HALT:
  318. kernel_halt();
  319. do_exit(0);
  320. panic("cannot halt");
  321. case LINUX_REBOOT_CMD_POWER_OFF:
  322. kernel_power_off();
  323. do_exit(0);
  324. break;
  325. case LINUX_REBOOT_CMD_RESTART2:
  326. ret = strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1);
  327. if (ret < 0) {
  328. ret = -EFAULT;
  329. break;
  330. }
  331. buffer[sizeof(buffer) - 1] = '\0';
  332. kernel_restart(buffer);
  333. break;
  334. #ifdef CONFIG_KEXEC_CORE
  335. case LINUX_REBOOT_CMD_KEXEC:
  336. ret = kernel_kexec();
  337. break;
  338. #endif
  339. #ifdef CONFIG_HIBERNATION
  340. case LINUX_REBOOT_CMD_SW_SUSPEND:
  341. ret = hibernate();
  342. break;
  343. #endif
  344. default:
  345. ret = -EINVAL;
  346. break;
  347. }
  348. mutex_unlock(&system_transition_mutex);
  349. return ret;
  350. }
  351. static void deferred_cad(struct work_struct *dummy)
  352. {
  353. kernel_restart(NULL);
  354. }
  355. /*
  356. * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
  357. * As it's called within an interrupt, it may NOT sync: the only choice
  358. * is whether to reboot at once, or just ignore the ctrl-alt-del.
  359. */
  360. void ctrl_alt_del(void)
  361. {
  362. static DECLARE_WORK(cad_work, deferred_cad);
  363. if (C_A_D)
  364. schedule_work(&cad_work);
  365. else
  366. kill_cad_pid(SIGINT, 1);
  367. }
  368. char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
  369. static const char reboot_cmd[] = "/sbin/reboot";
  370. static int run_cmd(const char *cmd)
  371. {
  372. char **argv;
  373. static char *envp[] = {
  374. "HOME=/",
  375. "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
  376. NULL
  377. };
  378. int ret;
  379. argv = argv_split(GFP_KERNEL, cmd, NULL);
  380. if (argv) {
  381. ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
  382. argv_free(argv);
  383. } else {
  384. ret = -ENOMEM;
  385. }
  386. return ret;
  387. }
  388. static int __orderly_reboot(void)
  389. {
  390. int ret;
  391. ret = run_cmd(reboot_cmd);
  392. if (ret) {
  393. pr_warn("Failed to start orderly reboot: forcing the issue\n");
  394. emergency_sync();
  395. kernel_restart(NULL);
  396. }
  397. return ret;
  398. }
  399. static int __orderly_poweroff(bool force)
  400. {
  401. int ret;
  402. ret = run_cmd(poweroff_cmd);
  403. if (ret && force) {
  404. pr_warn("Failed to start orderly shutdown: forcing the issue\n");
  405. /*
  406. * I guess this should try to kick off some daemon to sync and
  407. * poweroff asap. Or not even bother syncing if we're doing an
  408. * emergency shutdown?
  409. */
  410. emergency_sync();
  411. kernel_power_off();
  412. }
  413. return ret;
  414. }
  415. static bool poweroff_force;
  416. static void poweroff_work_func(struct work_struct *work)
  417. {
  418. __orderly_poweroff(poweroff_force);
  419. }
  420. static DECLARE_WORK(poweroff_work, poweroff_work_func);
  421. /**
  422. * orderly_poweroff - Trigger an orderly system poweroff
  423. * @force: force poweroff if command execution fails
  424. *
  425. * This may be called from any context to trigger a system shutdown.
  426. * If the orderly shutdown fails, it will force an immediate shutdown.
  427. */
  428. void orderly_poweroff(bool force)
  429. {
  430. if (force) /* do not override the pending "true" */
  431. poweroff_force = true;
  432. schedule_work(&poweroff_work);
  433. }
  434. EXPORT_SYMBOL_GPL(orderly_poweroff);
  435. static void reboot_work_func(struct work_struct *work)
  436. {
  437. __orderly_reboot();
  438. }
  439. static DECLARE_WORK(reboot_work, reboot_work_func);
  440. /**
  441. * orderly_reboot - Trigger an orderly system reboot
  442. *
  443. * This may be called from any context to trigger a system reboot.
  444. * If the orderly reboot fails, it will force an immediate reboot.
  445. */
  446. void orderly_reboot(void)
  447. {
  448. schedule_work(&reboot_work);
  449. }
  450. EXPORT_SYMBOL_GPL(orderly_reboot);
  451. static int __init reboot_setup(char *str)
  452. {
  453. for (;;) {
  454. /*
  455. * Having anything passed on the command line via
  456. * reboot= will cause us to disable DMI checking
  457. * below.
  458. */
  459. reboot_default = 0;
  460. switch (*str) {
  461. case 'w':
  462. reboot_mode = REBOOT_WARM;
  463. break;
  464. case 'c':
  465. reboot_mode = REBOOT_COLD;
  466. break;
  467. case 'h':
  468. reboot_mode = REBOOT_HARD;
  469. break;
  470. case 's':
  471. {
  472. int rc;
  473. if (isdigit(*(str+1))) {
  474. rc = kstrtoint(str+1, 0, &reboot_cpu);
  475. if (rc)
  476. return rc;
  477. } else if (str[1] == 'm' && str[2] == 'p' &&
  478. isdigit(*(str+3))) {
  479. rc = kstrtoint(str+3, 0, &reboot_cpu);
  480. if (rc)
  481. return rc;
  482. } else
  483. reboot_mode = REBOOT_SOFT;
  484. break;
  485. }
  486. case 'g':
  487. reboot_mode = REBOOT_GPIO;
  488. break;
  489. case 'b':
  490. case 'a':
  491. case 'k':
  492. case 't':
  493. case 'e':
  494. case 'p':
  495. reboot_type = *str;
  496. break;
  497. case 'f':
  498. reboot_force = 1;
  499. break;
  500. }
  501. str = strchr(str, ',');
  502. if (str)
  503. str++;
  504. else
  505. break;
  506. }
  507. return 1;
  508. }
  509. __setup("reboot=", reboot_setup);