shutdown.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (c) 2017 Richard Braun.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <stdio.h>
  18. #include <kern/init.h>
  19. #include <kern/plist.h>
  20. #include <kern/shell.h>
  21. #include <kern/shutdown.h>
  22. #include <machine/boot.h>
  23. #include <machine/cpu.h>
  24. static struct plist shutdown_ops_list;
  25. #ifdef CONFIG_SHELL
  26. static void
  27. shutdown_shell_halt (struct shell *shell __unused,
  28. int argc __unused, char **argv __unused)
  29. {
  30. shutdown_halt ();
  31. }
  32. static void
  33. shutdown_shell_reboot (struct shell *shell __unused,
  34. int argc __unused, char **argv __unused)
  35. {
  36. shutdown_reboot ();
  37. }
  38. static struct shell_cmd shutdown_shell_cmds[] =
  39. {
  40. SHELL_CMD_INITIALIZER ("shutdown_halt", shutdown_shell_halt,
  41. "shutdown_halt",
  42. "halt the system"),
  43. SHELL_CMD_INITIALIZER ("shutdown_reboot", shutdown_shell_reboot,
  44. "shutdown_reboot",
  45. "reboot the system"),
  46. };
  47. static int __init
  48. shutdown_setup_shell (void)
  49. {
  50. SHELL_REGISTER_CMDS (shutdown_shell_cmds, shell_get_main_cmd_set ());
  51. return (0);
  52. }
  53. INIT_OP_DEFINE (shutdown_setup_shell,
  54. INIT_OP_DEP (shell_setup, true),
  55. INIT_OP_DEP (shutdown_setup, true));
  56. #endif
  57. static int __init
  58. shutdown_bootstrap (void)
  59. {
  60. plist_init (&shutdown_ops_list);
  61. return (0);
  62. }
  63. INIT_OP_DEFINE (shutdown_bootstrap);
  64. static int __init
  65. shutdown_setup (void)
  66. {
  67. return (0);
  68. }
  69. INIT_OP_DEFINE (shutdown_setup,
  70. INIT_OP_DEP (boot_setup_shutdown, true),
  71. INIT_OP_DEP (cpu_setup, true),
  72. INIT_OP_DEP (printf_setup, true),
  73. INIT_OP_DEP (shutdown_bootstrap, true));
  74. void __init
  75. shutdown_register (struct shutdown_ops *ops, unsigned int priority)
  76. {
  77. plist_node_init (&ops->node, priority);
  78. plist_add (&shutdown_ops_list, &ops->node);
  79. }
  80. static void
  81. shutdown_halt_other_cpus (void)
  82. {
  83. cpu_intr_disable ();
  84. cpu_halt_broadcast ();
  85. }
  86. void
  87. shutdown_halt (void)
  88. {
  89. shutdown_halt_other_cpus();
  90. printf ("shutdown: system halted\n");
  91. cpu_halt ();
  92. }
  93. void
  94. shutdown_reboot (void)
  95. {
  96. if (plist_empty (&shutdown_ops_list))
  97. {
  98. printf ("shutdown: no reset operation available, halting\n");
  99. shutdown_halt();
  100. }
  101. shutdown_halt_other_cpus ();
  102. printf ("shutdown: rebooting...\n");
  103. struct shutdown_ops *ops;
  104. plist_for_each_entry_reverse (&shutdown_ops_list, ops, node)
  105. ops->reset ();
  106. cpu_halt ();
  107. }