2009-04-25 20:56:19 +08:00
|
|
|
/*
|
|
|
|
* Virtual hardware watchdog.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009 Red Hat Inc.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2009-07-17 04:47:01 +08:00
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
2009-04-25 20:56:19 +08:00
|
|
|
*
|
|
|
|
* By Richard W.M. Jones (rjones@redhat.com).
|
|
|
|
*/
|
|
|
|
|
2016-01-27 02:17:30 +08:00
|
|
|
#include "qemu/osdep.h"
|
2012-12-18 01:20:00 +08:00
|
|
|
#include "qemu/option.h"
|
|
|
|
#include "qemu/config-file.h"
|
|
|
|
#include "qemu/queue.h"
|
2012-12-18 01:19:43 +08:00
|
|
|
#include "qapi/qmp/types.h"
|
2012-12-18 01:20:04 +08:00
|
|
|
#include "sysemu/sysemu.h"
|
2013-02-06 00:06:20 +08:00
|
|
|
#include "sysemu/watchdog.h"
|
2014-06-18 14:43:42 +08:00
|
|
|
#include "qapi-event.h"
|
2015-02-05 18:28:36 +08:00
|
|
|
#include "hw/nmi.h"
|
2016-03-21 01:16:19 +08:00
|
|
|
#include "qemu/help_option.h"
|
2017-09-07 16:05:26 +08:00
|
|
|
#include "qmp-commands.h"
|
2009-04-25 20:56:19 +08:00
|
|
|
|
2017-09-07 16:05:25 +08:00
|
|
|
static WatchdogAction watchdog_action = WATCHDOG_ACTION_RESET;
|
2009-09-12 15:36:22 +08:00
|
|
|
static QLIST_HEAD(watchdog_list, WatchdogTimerModel) watchdog_list;
|
2009-04-25 20:56:19 +08:00
|
|
|
|
|
|
|
void watchdog_add_model(WatchdogTimerModel *model)
|
|
|
|
{
|
2009-09-12 15:36:22 +08:00
|
|
|
QLIST_INSERT_HEAD(&watchdog_list, model, entry);
|
2009-04-25 20:56:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns:
|
|
|
|
* 0 = continue
|
|
|
|
* 1 = exit program with error
|
|
|
|
* 2 = exit program without error
|
|
|
|
*/
|
|
|
|
int select_watchdog(const char *p)
|
|
|
|
{
|
|
|
|
WatchdogTimerModel *model;
|
2009-08-21 16:31:34 +08:00
|
|
|
QemuOpts *opts;
|
2009-04-25 20:56:19 +08:00
|
|
|
|
|
|
|
/* -watchdog ? lists available devices and exits cleanly. */
|
2012-08-02 20:45:54 +08:00
|
|
|
if (is_help_option(p)) {
|
2009-09-12 15:36:22 +08:00
|
|
|
QLIST_FOREACH(model, &watchdog_list, entry) {
|
2009-04-25 20:56:19 +08:00
|
|
|
fprintf(stderr, "\t%s\t%s\n",
|
|
|
|
model->wdt_name, model->wdt_description);
|
|
|
|
}
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2009-09-12 15:36:22 +08:00
|
|
|
QLIST_FOREACH(model, &watchdog_list, entry) {
|
2009-04-25 20:56:19 +08:00
|
|
|
if (strcasecmp(model->wdt_name, p) == 0) {
|
2009-08-21 16:31:34 +08:00
|
|
|
/* add the device */
|
2014-01-02 10:49:17 +08:00
|
|
|
opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
|
|
|
|
&error_abort);
|
2015-02-13 00:52:20 +08:00
|
|
|
qemu_opt_set(opts, "driver", p, &error_abort);
|
2009-04-25 20:56:19 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "Unknown -watchdog device. Supported devices are:\n");
|
2009-09-12 15:36:22 +08:00
|
|
|
QLIST_FOREACH(model, &watchdog_list, entry) {
|
2009-04-25 20:56:19 +08:00
|
|
|
fprintf(stderr, "\t%s\t%s\n",
|
|
|
|
model->wdt_name, model->wdt_description);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int select_watchdog_action(const char *p)
|
|
|
|
{
|
2017-09-07 16:05:25 +08:00
|
|
|
int action;
|
|
|
|
char *qapi_value;
|
2009-04-25 20:56:19 +08:00
|
|
|
|
2017-09-07 16:05:25 +08:00
|
|
|
qapi_value = g_ascii_strdown(p, -1);
|
|
|
|
action = qapi_enum_parse(&WatchdogAction_lookup, qapi_value, -1, NULL);
|
|
|
|
g_free(qapi_value);
|
|
|
|
if (action < 0)
|
|
|
|
return -1;
|
2017-09-07 16:05:26 +08:00
|
|
|
qmp_watchdog_set_action(action, &error_abort);
|
2009-04-25 20:56:19 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-09-07 16:05:25 +08:00
|
|
|
WatchdogAction get_watchdog_action(void)
|
2016-01-19 15:34:41 +08:00
|
|
|
{
|
|
|
|
return watchdog_action;
|
|
|
|
}
|
|
|
|
|
2009-04-25 20:56:19 +08:00
|
|
|
/* This actually performs the "action" once a watchdog has expired,
|
|
|
|
* ie. reboot, shutdown, exit, etc.
|
|
|
|
*/
|
|
|
|
void watchdog_perform_action(void)
|
|
|
|
{
|
2014-06-25 07:34:00 +08:00
|
|
|
switch (watchdog_action) {
|
2017-09-07 16:05:25 +08:00
|
|
|
case WATCHDOG_ACTION_RESET: /* same as 'system_reset' in monitor */
|
2017-09-07 16:05:24 +08:00
|
|
|
qapi_event_send_watchdog(WATCHDOG_ACTION_RESET, &error_abort);
|
2017-05-16 05:41:13 +08:00
|
|
|
qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
|
2009-04-25 20:56:19 +08:00
|
|
|
break;
|
|
|
|
|
2017-09-07 16:05:25 +08:00
|
|
|
case WATCHDOG_ACTION_SHUTDOWN: /* same as 'system_powerdown' in monitor */
|
2017-09-07 16:05:24 +08:00
|
|
|
qapi_event_send_watchdog(WATCHDOG_ACTION_SHUTDOWN, &error_abort);
|
2009-04-25 20:56:19 +08:00
|
|
|
qemu_system_powerdown_request();
|
|
|
|
break;
|
|
|
|
|
2017-09-07 16:05:25 +08:00
|
|
|
case WATCHDOG_ACTION_POWEROFF: /* same as 'quit' command in monitor */
|
2017-09-07 16:05:24 +08:00
|
|
|
qapi_event_send_watchdog(WATCHDOG_ACTION_POWEROFF, &error_abort);
|
2009-04-25 20:56:19 +08:00
|
|
|
exit(0);
|
|
|
|
|
2017-09-07 16:05:25 +08:00
|
|
|
case WATCHDOG_ACTION_PAUSE: /* same as 'stop' command in monitor */
|
2014-06-27 22:31:07 +08:00
|
|
|
/* In a timer callback, when vm_stop calls qemu_clock_enable
|
|
|
|
* you would get a deadlock. Bypass the problem.
|
|
|
|
*/
|
|
|
|
qemu_system_vmstop_request_prepare();
|
2017-09-07 16:05:24 +08:00
|
|
|
qapi_event_send_watchdog(WATCHDOG_ACTION_PAUSE, &error_abort);
|
2014-06-27 22:31:07 +08:00
|
|
|
qemu_system_vmstop_request(RUN_STATE_WATCHDOG);
|
2009-04-25 20:56:19 +08:00
|
|
|
break;
|
|
|
|
|
2017-09-07 16:05:25 +08:00
|
|
|
case WATCHDOG_ACTION_DEBUG:
|
2017-09-07 16:05:24 +08:00
|
|
|
qapi_event_send_watchdog(WATCHDOG_ACTION_DEBUG, &error_abort);
|
2009-04-25 20:56:19 +08:00
|
|
|
fprintf(stderr, "watchdog: timer fired\n");
|
|
|
|
break;
|
|
|
|
|
2017-09-07 16:05:25 +08:00
|
|
|
case WATCHDOG_ACTION_NONE:
|
2017-09-07 16:05:24 +08:00
|
|
|
qapi_event_send_watchdog(WATCHDOG_ACTION_NONE, &error_abort);
|
2009-04-25 20:56:19 +08:00
|
|
|
break;
|
2015-02-05 18:28:36 +08:00
|
|
|
|
2017-09-07 16:05:25 +08:00
|
|
|
case WATCHDOG_ACTION_INJECT_NMI:
|
2017-09-07 16:05:24 +08:00
|
|
|
qapi_event_send_watchdog(WATCHDOG_ACTION_INJECT_NMI,
|
2015-02-05 18:28:36 +08:00
|
|
|
&error_abort);
|
2016-05-21 00:28:36 +08:00
|
|
|
nmi_monitor_handle(0, NULL);
|
2015-02-05 18:28:36 +08:00
|
|
|
break;
|
2017-09-07 16:05:25 +08:00
|
|
|
|
|
|
|
default:
|
|
|
|
assert(0);
|
2009-04-25 20:56:19 +08:00
|
|
|
}
|
|
|
|
}
|
2017-09-07 16:05:26 +08:00
|
|
|
|
|
|
|
void qmp_watchdog_set_action(WatchdogAction action, Error **errp)
|
|
|
|
{
|
|
|
|
watchdog_action = action;
|
|
|
|
}
|