watchdog.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 "system/runstate.h"
  29. #include "system/watchdog.h"
  30. #include "hw/nmi.h"
  31. #include "qemu/help_option.h"
  32. #include "trace.h"
  33. static WatchdogAction watchdog_action = WATCHDOG_ACTION_RESET;
  34. WatchdogAction get_watchdog_action(void)
  35. {
  36. return watchdog_action;
  37. }
  38. /* This actually performs the "action" once a watchdog has expired,
  39. * ie. reboot, shutdown, exit, etc.
  40. */
  41. void watchdog_perform_action(void)
  42. {
  43. trace_watchdog_perform_action(watchdog_action);
  44. switch (watchdog_action) {
  45. case WATCHDOG_ACTION_RESET: /* same as 'system_reset' in monitor */
  46. qapi_event_send_watchdog(WATCHDOG_ACTION_RESET);
  47. qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
  48. break;
  49. case WATCHDOG_ACTION_SHUTDOWN: /* same as 'system_powerdown' in monitor */
  50. qapi_event_send_watchdog(WATCHDOG_ACTION_SHUTDOWN);
  51. qemu_system_powerdown_request();
  52. break;
  53. case WATCHDOG_ACTION_POWEROFF: /* same as 'quit' command in monitor */
  54. qapi_event_send_watchdog(WATCHDOG_ACTION_POWEROFF);
  55. exit(0);
  56. case WATCHDOG_ACTION_PAUSE: /* same as 'stop' command in monitor */
  57. /* In a timer callback, when vm_stop calls qemu_clock_enable
  58. * you would get a deadlock. Bypass the problem.
  59. */
  60. qemu_system_vmstop_request_prepare();
  61. qapi_event_send_watchdog(WATCHDOG_ACTION_PAUSE);
  62. qemu_system_vmstop_request(RUN_STATE_WATCHDOG);
  63. break;
  64. case WATCHDOG_ACTION_DEBUG:
  65. qapi_event_send_watchdog(WATCHDOG_ACTION_DEBUG);
  66. fprintf(stderr, "watchdog: timer fired\n");
  67. break;
  68. case WATCHDOG_ACTION_NONE:
  69. qapi_event_send_watchdog(WATCHDOG_ACTION_NONE);
  70. break;
  71. case WATCHDOG_ACTION_INJECT_NMI:
  72. qapi_event_send_watchdog(WATCHDOG_ACTION_INJECT_NMI);
  73. nmi_monitor_handle(0, NULL);
  74. break;
  75. default:
  76. g_assert_not_reached();
  77. }
  78. }
  79. void qmp_watchdog_set_action(WatchdogAction action, Error **errp)
  80. {
  81. watchdog_action = action;
  82. trace_watchdog_set_action(watchdog_action);
  83. }