]> git.proxmox.com Git - mirror_qemu.git/blame - hw/watchdog/watchdog.c
shutdown: Add source information to SHUTDOWN and RESET
[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"
7b1b5d19 26#include "qapi/qmp/types.h"
9c17d615 27#include "sysemu/sysemu.h"
0d09e41a 28#include "sysemu/watchdog.h"
99eaf09c 29#include "qapi-event.h"
795dc6e4 30#include "hw/nmi.h"
f348b6d1 31#include "qemu/help_option.h"
9dd986cc 32
88b3be20 33static int watchdog_action = WDT_RESET;
72cf2d4f 34static QLIST_HEAD(watchdog_list, WatchdogTimerModel) watchdog_list;
9dd986cc
RJ
35
36void watchdog_add_model(WatchdogTimerModel *model)
37{
72cf2d4f 38 QLIST_INSERT_HEAD(&watchdog_list, model, entry);
9dd986cc
RJ
39}
40
41/* Returns:
42 * 0 = continue
43 * 1 = exit program with error
44 * 2 = exit program without error
45 */
46int select_watchdog(const char *p)
47{
48 WatchdogTimerModel *model;
09aaa160 49 QemuOpts *opts;
9dd986cc
RJ
50
51 /* -watchdog ? lists available devices and exits cleanly. */
c8057f95 52 if (is_help_option(p)) {
72cf2d4f 53 QLIST_FOREACH(model, &watchdog_list, entry) {
9dd986cc
RJ
54 fprintf(stderr, "\t%s\t%s\n",
55 model->wdt_name, model->wdt_description);
56 }
57 return 2;
58 }
59
72cf2d4f 60 QLIST_FOREACH(model, &watchdog_list, entry) {
9dd986cc 61 if (strcasecmp(model->wdt_name, p) == 0) {
09aaa160 62 /* add the device */
87ea75d5
PC
63 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
64 &error_abort);
f43e47db 65 qemu_opt_set(opts, "driver", p, &error_abort);
9dd986cc
RJ
66 return 0;
67 }
68 }
69
70 fprintf(stderr, "Unknown -watchdog device. Supported devices are:\n");
72cf2d4f 71 QLIST_FOREACH(model, &watchdog_list, entry) {
9dd986cc
RJ
72 fprintf(stderr, "\t%s\t%s\n",
73 model->wdt_name, model->wdt_description);
74 }
75 return 1;
76}
77
78int select_watchdog_action(const char *p)
79{
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;
795dc6e4
MCL
92 else if (strcasecmp(p, "inject-nmi") == 0)
93 watchdog_action = WDT_NMI;
9dd986cc
RJ
94 else
95 return -1;
96
97 return 0;
98}
99
0d035b6c
BT
100int get_watchdog_action(void)
101{
102 return watchdog_action;
103}
104
9dd986cc
RJ
105/* This actually performs the "action" once a watchdog has expired,
106 * ie. reboot, shutdown, exit, etc.
107 */
108void watchdog_perform_action(void)
109{
2f44a08b 110 switch (watchdog_action) {
9dd986cc 111 case WDT_RESET: /* same as 'system_reset' in monitor */
99eaf09c 112 qapi_event_send_watchdog(WATCHDOG_EXPIRATION_ACTION_RESET, &error_abort);
cf83f140 113 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
9dd986cc
RJ
114 break;
115
116 case WDT_SHUTDOWN: /* same as 'system_powerdown' in monitor */
99eaf09c 117 qapi_event_send_watchdog(WATCHDOG_EXPIRATION_ACTION_SHUTDOWN, &error_abort);
9dd986cc
RJ
118 qemu_system_powerdown_request();
119 break;
120
121 case WDT_POWEROFF: /* same as 'quit' command in monitor */
99eaf09c 122 qapi_event_send_watchdog(WATCHDOG_EXPIRATION_ACTION_POWEROFF, &error_abort);
9dd986cc 123 exit(0);
9dd986cc
RJ
124
125 case WDT_PAUSE: /* same as 'stop' command in monitor */
30e5210a
PB
126 /* In a timer callback, when vm_stop calls qemu_clock_enable
127 * you would get a deadlock. Bypass the problem.
128 */
129 qemu_system_vmstop_request_prepare();
99eaf09c 130 qapi_event_send_watchdog(WATCHDOG_EXPIRATION_ACTION_PAUSE, &error_abort);
30e5210a 131 qemu_system_vmstop_request(RUN_STATE_WATCHDOG);
9dd986cc
RJ
132 break;
133
134 case WDT_DEBUG:
99eaf09c 135 qapi_event_send_watchdog(WATCHDOG_EXPIRATION_ACTION_DEBUG, &error_abort);
9dd986cc
RJ
136 fprintf(stderr, "watchdog: timer fired\n");
137 break;
138
139 case WDT_NONE:
99eaf09c 140 qapi_event_send_watchdog(WATCHDOG_EXPIRATION_ACTION_NONE, &error_abort);
9dd986cc 141 break;
795dc6e4
MCL
142
143 case WDT_NMI:
144 qapi_event_send_watchdog(WATCHDOG_EXPIRATION_ACTION_INJECT_NMI,
145 &error_abort);
f7e981f2 146 nmi_monitor_handle(0, NULL);
795dc6e4 147 break;
9dd986cc
RJ
148 }
149}