blob: dce7c1db147144d46c905474dffce0e9fe7ca7c2 [file] [log] [blame]
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001/*
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
Blue Swirl8167ee82009-07-16 20:47:01 +000017 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010018 *
19 * By Richard W.M. Jones (rjones@redhat.com).
20 */
21
Peter Maydell04308912016-01-26 18:17:30 +000022#include "qemu/osdep.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010023#include "qemu/option.h"
24#include "qemu/config-file.h"
25#include "qemu/queue.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010026#include "qapi/error.h"
Markus Armbruster112ed242018-02-26 17:13:27 -060027#include "qapi/qapi-commands-run-state.h"
Markus Armbruster9af23982018-02-11 10:36:01 +010028#include "qapi/qapi-events-run-state.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010029#include "sysemu/sysemu.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010030#include "sysemu/watchdog.h"
Mao Chuan Li795dc6e2015-02-05 18:28:36 +080031#include "hw/nmi.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020032#include "qemu/help_option.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010033
Michal Privoznik4c7f4422017-09-07 10:05:25 +020034static WatchdogAction watchdog_action = WATCHDOG_ACTION_RESET;
Paolo Bonzinib58deb32018-12-06 11:58:10 +010035static QLIST_HEAD(, WatchdogTimerModel) watchdog_list;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010036
37void watchdog_add_model(WatchdogTimerModel *model)
38{
Blue Swirl72cf2d42009-09-12 07:36:22 +000039 QLIST_INSERT_HEAD(&watchdog_list, model, entry);
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010040}
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;
Markus Armbruster09aaa162009-08-21 10:31:34 +020050 QemuOpts *opts;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010051
52 /* -watchdog ? lists available devices and exits cleanly. */
Peter Maydellc8057f92012-08-02 13:45:54 +010053 if (is_help_option(p)) {
Blue Swirl72cf2d42009-09-12 07:36:22 +000054 QLIST_FOREACH(model, &watchdog_list, entry) {
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010055 fprintf(stderr, "\t%s\t%s\n",
56 model->wdt_name, model->wdt_description);
57 }
58 return 2;
59 }
60
Blue Swirl72cf2d42009-09-12 07:36:22 +000061 QLIST_FOREACH(model, &watchdog_list, entry) {
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010062 if (strcasecmp(model->wdt_name, p) == 0) {
Markus Armbruster09aaa162009-08-21 10:31:34 +020063 /* add the device */
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -080064 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
65 &error_abort);
Markus Armbrusterf43e47d2015-02-12 17:52:20 +010066 qemu_opt_set(opts, "driver", p, &error_abort);
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010067 return 0;
68 }
69 }
70
71 fprintf(stderr, "Unknown -watchdog device. Supported devices are:\n");
Blue Swirl72cf2d42009-09-12 07:36:22 +000072 QLIST_FOREACH(model, &watchdog_list, entry) {
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010073 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{
Michal Privoznik4c7f4422017-09-07 10:05:25 +020081 int action;
82 char *qapi_value;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010083
Michal Privoznik4c7f4422017-09-07 10:05:25 +020084 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;
Michal Privoznikf0df84c2017-09-07 10:05:26 +020089 qmp_watchdog_set_action(action, &error_abort);
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010090 return 0;
91}
92
Michal Privoznik4c7f4422017-09-07 10:05:25 +020093WatchdogAction get_watchdog_action(void)
Bo Tu0d035b62016-01-19 08:34:41 +010094{
95 return watchdog_action;
96}
97
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010098/* This actually performs the "action" once a watchdog has expired,
99 * ie. reboot, shutdown, exit, etc.
100 */
101void watchdog_perform_action(void)
102{
Wenchao Xia2f44a082014-06-24 16:34:00 -0700103 switch (watchdog_action) {
Michal Privoznik4c7f4422017-09-07 10:05:25 +0200104 case WATCHDOG_ACTION_RESET: /* same as 'system_reset' in monitor */
Peter Xu3ab72382018-08-15 21:37:37 +0800105 qapi_event_send_watchdog(WATCHDOG_ACTION_RESET);
Eric Blakecf83f142017-05-15 16:41:13 -0500106 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100107 break;
108
Michal Privoznik4c7f4422017-09-07 10:05:25 +0200109 case WATCHDOG_ACTION_SHUTDOWN: /* same as 'system_powerdown' in monitor */
Peter Xu3ab72382018-08-15 21:37:37 +0800110 qapi_event_send_watchdog(WATCHDOG_ACTION_SHUTDOWN);
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100111 qemu_system_powerdown_request();
112 break;
113
Michal Privoznik4c7f4422017-09-07 10:05:25 +0200114 case WATCHDOG_ACTION_POWEROFF: /* same as 'quit' command in monitor */
Peter Xu3ab72382018-08-15 21:37:37 +0800115 qapi_event_send_watchdog(WATCHDOG_ACTION_POWEROFF);
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100116 exit(0);
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100117
Michal Privoznik4c7f4422017-09-07 10:05:25 +0200118 case WATCHDOG_ACTION_PAUSE: /* same as 'stop' command in monitor */
Paolo Bonzini30e52102014-06-27 16:31:07 +0200119 /* 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();
Peter Xu3ab72382018-08-15 21:37:37 +0800123 qapi_event_send_watchdog(WATCHDOG_ACTION_PAUSE);
Paolo Bonzini30e52102014-06-27 16:31:07 +0200124 qemu_system_vmstop_request(RUN_STATE_WATCHDOG);
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100125 break;
126
Michal Privoznik4c7f4422017-09-07 10:05:25 +0200127 case WATCHDOG_ACTION_DEBUG:
Peter Xu3ab72382018-08-15 21:37:37 +0800128 qapi_event_send_watchdog(WATCHDOG_ACTION_DEBUG);
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100129 fprintf(stderr, "watchdog: timer fired\n");
130 break;
131
Michal Privoznik4c7f4422017-09-07 10:05:25 +0200132 case WATCHDOG_ACTION_NONE:
Peter Xu3ab72382018-08-15 21:37:37 +0800133 qapi_event_send_watchdog(WATCHDOG_ACTION_NONE);
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100134 break;
Mao Chuan Li795dc6e2015-02-05 18:28:36 +0800135
Michal Privoznik4c7f4422017-09-07 10:05:25 +0200136 case WATCHDOG_ACTION_INJECT_NMI:
Peter Xu3ab72382018-08-15 21:37:37 +0800137 qapi_event_send_watchdog(WATCHDOG_ACTION_INJECT_NMI);
Bandan Dasf7e981f2016-05-20 12:28:36 -0400138 nmi_monitor_handle(0, NULL);
Mao Chuan Li795dc6e2015-02-05 18:28:36 +0800139 break;
Michal Privoznik4c7f4422017-09-07 10:05:25 +0200140
141 default:
142 assert(0);
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100143 }
144}
Michal Privoznikf0df84c2017-09-07 10:05:26 +0200145
146void qmp_watchdog_set_action(WatchdogAction action, Error **errp)
147{
148 watchdog_action = action;
149}