]> git.proxmox.com Git - mirror_qemu.git/blame - hw/watchdog/watchdog.c
hw/watchdog/wdt_i6300esb: Convert away from old_mmio
[mirror_qemu.git] / hw / watchdog / watchdog.c
CommitLineData
9dd986cc
RJ
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
8167ee88 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
9dd986cc
RJ
18 *
19 * By Richard W.M. Jones (rjones@redhat.com).
20 */
21
0430891c 22#include "qemu/osdep.h"
1de7afc9
PB
23#include "qemu/option.h"
24#include "qemu/config-file.h"
25#include "qemu/queue.h"
e688df6b 26#include "qapi/error.h"
112ed241 27#include "qapi/qapi-commands-run-state.h"
9af23989 28#include "qapi/qapi-events-run-state.h"
9c17d615 29#include "sysemu/sysemu.h"
0d09e41a 30#include "sysemu/watchdog.h"
795dc6e4 31#include "hw/nmi.h"
f348b6d1 32#include "qemu/help_option.h"
9dd986cc 33
4c7f4426 34static WatchdogAction watchdog_action = WATCHDOG_ACTION_RESET;
72cf2d4f 35static QLIST_HEAD(watchdog_list, WatchdogTimerModel) watchdog_list;
9dd986cc
RJ
36
37void watchdog_add_model(WatchdogTimerModel *model)
38{
72cf2d4f 39 QLIST_INSERT_HEAD(&watchdog_list, model, entry);
9dd986cc
RJ
40}
41
42/* Returns:
43 * 0 = continue
44 * 1 = exit program with error
45 * 2 = exit program without error
46 */
47int select_watchdog(const char *p)
48{
49 WatchdogTimerModel *model;
09aaa160 50 QemuOpts *opts;
9dd986cc
RJ
51
52 /* -watchdog ? lists available devices and exits cleanly. */
c8057f95 53 if (is_help_option(p)) {
72cf2d4f 54 QLIST_FOREACH(model, &watchdog_list, entry) {
9dd986cc
RJ
55 fprintf(stderr, "\t%s\t%s\n",
56 model->wdt_name, model->wdt_description);
57 }
58 return 2;
59 }
60
72cf2d4f 61 QLIST_FOREACH(model, &watchdog_list, entry) {
9dd986cc 62 if (strcasecmp(model->wdt_name, p) == 0) {
09aaa160 63 /* add the device */
87ea75d5
PC
64 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
65 &error_abort);
f43e47db 66 qemu_opt_set(opts, "driver", p, &error_abort);
9dd986cc
RJ
67 return 0;
68 }
69 }
70
71 fprintf(stderr, "Unknown -watchdog device. Supported devices are:\n");
72cf2d4f 72 QLIST_FOREACH(model, &watchdog_list, entry) {
9dd986cc
RJ
73 fprintf(stderr, "\t%s\t%s\n",
74 model->wdt_name, model->wdt_description);
75 }
76 return 1;
77}
78
79int select_watchdog_action(const char *p)
80{
4c7f4426
MP
81 int action;
82 char *qapi_value;
9dd986cc 83
4c7f4426
MP
84 qapi_value = g_ascii_strdown(p, -1);
85 action = qapi_enum_parse(&WatchdogAction_lookup, qapi_value, -1, NULL);
86 g_free(qapi_value);
87 if (action < 0)
88 return -1;
f0df84c6 89 qmp_watchdog_set_action(action, &error_abort);
9dd986cc
RJ
90 return 0;
91}
92
4c7f4426 93WatchdogAction get_watchdog_action(void)
0d035b6c
BT
94{
95 return watchdog_action;
96}
97
9dd986cc
RJ
98/* This actually performs the "action" once a watchdog has expired,
99 * ie. reboot, shutdown, exit, etc.
100 */
101void watchdog_perform_action(void)
102{
2f44a08b 103 switch (watchdog_action) {
4c7f4426 104 case WATCHDOG_ACTION_RESET: /* same as 'system_reset' in monitor */
14d53b4f 105 qapi_event_send_watchdog(WATCHDOG_ACTION_RESET, &error_abort);
cf83f140 106 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
9dd986cc
RJ
107 break;
108
4c7f4426 109 case WATCHDOG_ACTION_SHUTDOWN: /* same as 'system_powerdown' in monitor */
14d53b4f 110 qapi_event_send_watchdog(WATCHDOG_ACTION_SHUTDOWN, &error_abort);
9dd986cc
RJ
111 qemu_system_powerdown_request();
112 break;
113
4c7f4426 114 case WATCHDOG_ACTION_POWEROFF: /* same as 'quit' command in monitor */
14d53b4f 115 qapi_event_send_watchdog(WATCHDOG_ACTION_POWEROFF, &error_abort);
9dd986cc 116 exit(0);
9dd986cc 117
4c7f4426 118 case WATCHDOG_ACTION_PAUSE: /* same as 'stop' command in monitor */
30e5210a
PB
119 /* In a timer callback, when vm_stop calls qemu_clock_enable
120 * you would get a deadlock. Bypass the problem.
121 */
122 qemu_system_vmstop_request_prepare();
14d53b4f 123 qapi_event_send_watchdog(WATCHDOG_ACTION_PAUSE, &error_abort);
30e5210a 124 qemu_system_vmstop_request(RUN_STATE_WATCHDOG);
9dd986cc
RJ
125 break;
126
4c7f4426 127 case WATCHDOG_ACTION_DEBUG:
14d53b4f 128 qapi_event_send_watchdog(WATCHDOG_ACTION_DEBUG, &error_abort);
9dd986cc
RJ
129 fprintf(stderr, "watchdog: timer fired\n");
130 break;
131
4c7f4426 132 case WATCHDOG_ACTION_NONE:
14d53b4f 133 qapi_event_send_watchdog(WATCHDOG_ACTION_NONE, &error_abort);
9dd986cc 134 break;
795dc6e4 135
4c7f4426 136 case WATCHDOG_ACTION_INJECT_NMI:
14d53b4f 137 qapi_event_send_watchdog(WATCHDOG_ACTION_INJECT_NMI,
795dc6e4 138 &error_abort);
f7e981f2 139 nmi_monitor_handle(0, NULL);
795dc6e4 140 break;
4c7f4426
MP
141
142 default:
143 assert(0);
9dd986cc
RJ
144 }
145}
f0df84c6
MP
146
147void qmp_watchdog_set_action(WatchdogAction action, Error **errp)
148{
149 watchdog_action = action;
150}