watchdog.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Virtual hardware watchdog.
  3. *
  4. * Copyright (C) 2009 Red Hat Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  18. *
  19. * By Richard W.M. Jones (rjones@redhat.com).
  20. */
  21. #include "qemu/osdep.h"
  22. #include "qemu/option.h"
  23. #include "qemu/config-file.h"
  24. #include "qemu/queue.h"
  25. #include "qapi/error.h"
  26. #include "qapi/qapi-commands-run-state.h"
  27. #include "qapi/qapi-events-run-state.h"
  28. #include "sysemu/runstate.h"
  29. #include "sysemu/watchdog.h"
  30. #include "hw/nmi.h"
  31. #include "qemu/help_option.h"
  32. static WatchdogAction watchdog_action = WATCHDOG_ACTION_RESET;
  33. static QLIST_HEAD(, WatchdogTimerModel) watchdog_list;
  34. void watchdog_add_model(WatchdogTimerModel *model)
  35. {
  36. QLIST_INSERT_HEAD(&watchdog_list, model, entry);
  37. }
  38. /* Returns:
  39. * 0 = continue
  40. * 1 = exit program with error
  41. * 2 = exit program without error
  42. */
  43. int select_watchdog(const char *p)
  44. {
  45. WatchdogTimerModel *model;
  46. QemuOpts *opts;
  47. /* -watchdog ? lists available devices and exits cleanly. */
  48. if (is_help_option(p)) {
  49. QLIST_FOREACH(model, &watchdog_list, entry) {
  50. fprintf(stderr, "\t%s\t%s\n",
  51. model->wdt_name, model->wdt_description);
  52. }
  53. return 2;
  54. }
  55. QLIST_FOREACH(model, &watchdog_list, entry) {
  56. if (strcasecmp(model->wdt_name, p) == 0) {
  57. /* add the device */
  58. opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
  59. &error_abort);
  60. qemu_opt_set(opts, "driver", p, &error_abort);
  61. return 0;
  62. }
  63. }
  64. fprintf(stderr, "Unknown -watchdog device. Supported devices are:\n");
  65. QLIST_FOREACH(model, &watchdog_list, entry) {
  66. fprintf(stderr, "\t%s\t%s\n",
  67. model->wdt_name, model->wdt_description);
  68. }
  69. return 1;
  70. }
  71. int select_watchdog_action(const char *p)
  72. {
  73. int action;
  74. char *qapi_value;
  75. qapi_value = g_ascii_strdown(p, -1);
  76. action = qapi_enum_parse(&WatchdogAction_lookup, qapi_value, -1, NULL);
  77. g_free(qapi_value);
  78. if (action < 0)
  79. return -1;
  80. qmp_watchdog_set_action(action, &error_abort);
  81. return 0;
  82. }
  83. WatchdogAction get_watchdog_action(void)
  84. {
  85. return watchdog_action;
  86. }
  87. /* This actually performs the "action" once a watchdog has expired,
  88. * ie. reboot, shutdown, exit, etc.
  89. */
  90. void watchdog_perform_action(void)
  91. {
  92. switch (watchdog_action) {
  93. case WATCHDOG_ACTION_RESET: /* same as 'system_reset' in monitor */
  94. qapi_event_send_watchdog(WATCHDOG_ACTION_RESET);
  95. qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
  96. break;
  97. case WATCHDOG_ACTION_SHUTDOWN: /* same as 'system_powerdown' in monitor */
  98. qapi_event_send_watchdog(WATCHDOG_ACTION_SHUTDOWN);
  99. qemu_system_powerdown_request();
  100. break;
  101. case WATCHDOG_ACTION_POWEROFF: /* same as 'quit' command in monitor */
  102. qapi_event_send_watchdog(WATCHDOG_ACTION_POWEROFF);
  103. exit(0);
  104. case WATCHDOG_ACTION_PAUSE: /* same as 'stop' command in monitor */
  105. /* In a timer callback, when vm_stop calls qemu_clock_enable
  106. * you would get a deadlock. Bypass the problem.
  107. */
  108. qemu_system_vmstop_request_prepare();
  109. qapi_event_send_watchdog(WATCHDOG_ACTION_PAUSE);
  110. qemu_system_vmstop_request(RUN_STATE_WATCHDOG);
  111. break;
  112. case WATCHDOG_ACTION_DEBUG:
  113. qapi_event_send_watchdog(WATCHDOG_ACTION_DEBUG);
  114. fprintf(stderr, "watchdog: timer fired\n");
  115. break;
  116. case WATCHDOG_ACTION_NONE:
  117. qapi_event_send_watchdog(WATCHDOG_ACTION_NONE);
  118. break;
  119. case WATCHDOG_ACTION_INJECT_NMI:
  120. qapi_event_send_watchdog(WATCHDOG_ACTION_INJECT_NMI);
  121. nmi_monitor_handle(0, NULL);
  122. break;
  123. default:
  124. assert(0);
  125. }
  126. }
  127. void qmp_watchdog_set_action(WatchdogAction action, Error **errp)
  128. {
  129. watchdog_action = action;
  130. }