shutdown.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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", "halt the system"),
  42. SHELL_CMD_INITIALIZER ("shutdown_reboot", shutdown_shell_reboot,
  43. "shutdown_reboot", "reboot the system"),
  44. };
  45. static int __init
  46. shutdown_setup_shell (void)
  47. {
  48. SHELL_REGISTER_CMDS (shutdown_shell_cmds, shell_get_main_cmd_set ());
  49. return (0);
  50. }
  51. INIT_OP_DEFINE (shutdown_setup_shell,
  52. INIT_OP_DEP (shell_setup, true),
  53. INIT_OP_DEP (shutdown_setup, true));
  54. #endif
  55. static int __init
  56. shutdown_bootstrap (void)
  57. {
  58. plist_init (&shutdown_ops_list);
  59. return (0);
  60. }
  61. INIT_OP_DEFINE (shutdown_bootstrap);
  62. static int __init
  63. shutdown_setup (void)
  64. {
  65. return (0);
  66. }
  67. INIT_OP_DEFINE (shutdown_setup,
  68. INIT_OP_DEP (boot_setup_shutdown, true),
  69. INIT_OP_DEP (cpu_setup, true),
  70. INIT_OP_DEP (printf_setup, true),
  71. INIT_OP_DEP (shutdown_bootstrap, true));
  72. void __init
  73. shutdown_register (struct shutdown_ops *ops, unsigned int priority)
  74. {
  75. plist_node_init (&ops->node, priority);
  76. plist_add (&shutdown_ops_list, &ops->node);
  77. }
  78. static void
  79. shutdown_halt_other_cpus (void)
  80. {
  81. cpu_intr_disable ();
  82. cpu_halt_broadcast ();
  83. }
  84. void
  85. shutdown_halt (void)
  86. {
  87. shutdown_halt_other_cpus();
  88. printf ("shutdown: system halted\n");
  89. cpu_halt ();
  90. }
  91. void
  92. shutdown_reboot (void)
  93. {
  94. if (plist_empty (&shutdown_ops_list))
  95. {
  96. printf ("shutdown: no reset operation available, halting\n");
  97. shutdown_halt();
  98. }
  99. shutdown_halt_other_cpus ();
  100. printf ("shutdown: rebooting...\n");
  101. struct shutdown_ops *ops;
  102. plist_for_each_entry_reverse (&shutdown_ops_list, ops, node)
  103. ops->reset ();
  104. cpu_halt ();
  105. }