linux-headers: update
[qemu.git] / hw / watchdog / watchdog.c
blob2aeaf1fbc9eb3b3eb1d8f950cf62dd3007c1bbf6
1 /*
2 * Virtual hardware watchdog.
4 * Copyright (C) 2009 Red Hat Inc.
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.
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.
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/>.
19 * By Richard W.M. Jones (rjones@redhat.com).
22 #include "qemu/osdep.h"
23 #include "qemu/option.h"
24 #include "qemu/config-file.h"
25 #include "qemu/queue.h"
26 #include "qapi/qmp/types.h"
27 #include "sysemu/sysemu.h"
28 #include "sysemu/watchdog.h"
29 #include "qapi-event.h"
30 #include "hw/nmi.h"
31 #include "qemu/help_option.h"
33 static int watchdog_action = WDT_RESET;
34 static QLIST_HEAD(watchdog_list, WatchdogTimerModel) watchdog_list;
36 void watchdog_add_model(WatchdogTimerModel *model)
38 QLIST_INSERT_HEAD(&watchdog_list, model, entry);
41 /* Returns:
42 * 0 = continue
43 * 1 = exit program with error
44 * 2 = exit program without error
46 int select_watchdog(const char *p)
48 WatchdogTimerModel *model;
49 QemuOpts *opts;
51 /* -watchdog ? lists available devices and exits cleanly. */
52 if (is_help_option(p)) {
53 QLIST_FOREACH(model, &watchdog_list, entry) {
54 fprintf(stderr, "\t%s\t%s\n",
55 model->wdt_name, model->wdt_description);
57 return 2;
60 QLIST_FOREACH(model, &watchdog_list, entry) {
61 if (strcasecmp(model->wdt_name, p) == 0) {
62 /* add the device */
63 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
64 &error_abort);
65 qemu_opt_set(opts, "driver", p, &error_abort);
66 return 0;
70 fprintf(stderr, "Unknown -watchdog device. Supported devices are:\n");
71 QLIST_FOREACH(model, &watchdog_list, entry) {
72 fprintf(stderr, "\t%s\t%s\n",
73 model->wdt_name, model->wdt_description);
75 return 1;
78 int select_watchdog_action(const char *p)
80 if (strcasecmp(p, "reset") == 0)
81 watchdog_action = WDT_RESET;
82 else if (strcasecmp(p, "shutdown") == 0)
83 watchdog_action = WDT_SHUTDOWN;
84 else if (strcasecmp(p, "poweroff") == 0)
85 watchdog_action = WDT_POWEROFF;
86 else if (strcasecmp(p, "pause") == 0)
87 watchdog_action = WDT_PAUSE;
88 else if (strcasecmp(p, "debug") == 0)
89 watchdog_action = WDT_DEBUG;
90 else if (strcasecmp(p, "none") == 0)
91 watchdog_action = WDT_NONE;
92 else if (strcasecmp(p, "inject-nmi") == 0)
93 watchdog_action = WDT_NMI;
94 else
95 return -1;
97 return 0;
100 int get_watchdog_action(void)
102 return watchdog_action;
105 /* This actually performs the "action" once a watchdog has expired,
106 * ie. reboot, shutdown, exit, etc.
108 void watchdog_perform_action(void)
110 switch (watchdog_action) {
111 case WDT_RESET: /* same as 'system_reset' in monitor */
112 qapi_event_send_watchdog(WATCHDOG_EXPIRATION_ACTION_RESET, &error_abort);
113 qemu_system_reset_request();
114 break;
116 case WDT_SHUTDOWN: /* same as 'system_powerdown' in monitor */
117 qapi_event_send_watchdog(WATCHDOG_EXPIRATION_ACTION_SHUTDOWN, &error_abort);
118 qemu_system_powerdown_request();
119 break;
121 case WDT_POWEROFF: /* same as 'quit' command in monitor */
122 qapi_event_send_watchdog(WATCHDOG_EXPIRATION_ACTION_POWEROFF, &error_abort);
123 exit(0);
125 case WDT_PAUSE: /* same as 'stop' command in monitor */
126 /* In a timer callback, when vm_stop calls qemu_clock_enable
127 * you would get a deadlock. Bypass the problem.
129 qemu_system_vmstop_request_prepare();
130 qapi_event_send_watchdog(WATCHDOG_EXPIRATION_ACTION_PAUSE, &error_abort);
131 qemu_system_vmstop_request(RUN_STATE_WATCHDOG);
132 break;
134 case WDT_DEBUG:
135 qapi_event_send_watchdog(WATCHDOG_EXPIRATION_ACTION_DEBUG, &error_abort);
136 fprintf(stderr, "watchdog: timer fired\n");
137 break;
139 case WDT_NONE:
140 qapi_event_send_watchdog(WATCHDOG_EXPIRATION_ACTION_NONE, &error_abort);
141 break;
143 case WDT_NMI:
144 qapi_event_send_watchdog(WATCHDOG_EXPIRATION_ACTION_INJECT_NMI,
145 &error_abort);
146 nmi_monitor_handle(0, NULL);
147 break;