watchdog.c 4.6 KB
Newer Older
R
Richard W.M. Jones 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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
17
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
R
Richard W.M. Jones 已提交
18 19 20 21
 *
 * By Richard W.M. Jones (rjones@redhat.com).
 */

P
Peter Maydell 已提交
22
#include "qemu/osdep.h"
23 24 25
#include "qemu/option.h"
#include "qemu/config-file.h"
#include "qemu/queue.h"
26
#include "qapi/qmp/types.h"
27
#include "sysemu/sysemu.h"
P
Paolo Bonzini 已提交
28
#include "sysemu/watchdog.h"
W
Wenchao Xia 已提交
29
#include "qapi-event.h"
30
#include "hw/nmi.h"
31
#include "qemu/help_option.h"
R
Richard W.M. Jones 已提交
32

33
static int watchdog_action = WDT_RESET;
B
Blue Swirl 已提交
34
static QLIST_HEAD(watchdog_list, WatchdogTimerModel) watchdog_list;
R
Richard W.M. Jones 已提交
35 36 37

void watchdog_add_model(WatchdogTimerModel *model)
{
B
Blue Swirl 已提交
38
    QLIST_INSERT_HEAD(&watchdog_list, model, entry);
R
Richard W.M. Jones 已提交
39 40 41 42 43 44 45 46 47 48
}

/* Returns:
 *   0 = continue
 *   1 = exit program with error
 *   2 = exit program without error
 */
int select_watchdog(const char *p)
{
    WatchdogTimerModel *model;
M
Markus Armbruster 已提交
49
    QemuOpts *opts;
R
Richard W.M. Jones 已提交
50 51

    /* -watchdog ? lists available devices and exits cleanly. */
52
    if (is_help_option(p)) {
B
Blue Swirl 已提交
53
        QLIST_FOREACH(model, &watchdog_list, entry) {
R
Richard W.M. Jones 已提交
54 55 56 57 58 59
            fprintf(stderr, "\t%s\t%s\n",
                     model->wdt_name, model->wdt_description);
        }
        return 2;
    }

B
Blue Swirl 已提交
60
    QLIST_FOREACH(model, &watchdog_list, entry) {
R
Richard W.M. Jones 已提交
61
        if (strcasecmp(model->wdt_name, p) == 0) {
M
Markus Armbruster 已提交
62
            /* add the device */
63 64
            opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
                                    &error_abort);
65
            qemu_opt_set(opts, "driver", p, &error_abort);
R
Richard W.M. Jones 已提交
66 67 68 69 70
            return 0;
        }
    }

    fprintf(stderr, "Unknown -watchdog device. Supported devices are:\n");
B
Blue Swirl 已提交
71
    QLIST_FOREACH(model, &watchdog_list, entry) {
R
Richard W.M. Jones 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
        fprintf(stderr, "\t%s\t%s\n",
                 model->wdt_name, model->wdt_description);
    }
    return 1;
}

int select_watchdog_action(const char *p)
{
    if (strcasecmp(p, "reset") == 0)
        watchdog_action = WDT_RESET;
    else if (strcasecmp(p, "shutdown") == 0)
        watchdog_action = WDT_SHUTDOWN;
    else if (strcasecmp(p, "poweroff") == 0)
        watchdog_action = WDT_POWEROFF;
    else if (strcasecmp(p, "pause") == 0)
        watchdog_action = WDT_PAUSE;
    else if (strcasecmp(p, "debug") == 0)
        watchdog_action = WDT_DEBUG;
    else if (strcasecmp(p, "none") == 0)
        watchdog_action = WDT_NONE;
92 93
    else if (strcasecmp(p, "inject-nmi") == 0)
        watchdog_action = WDT_NMI;
R
Richard W.M. Jones 已提交
94 95 96 97 98 99
    else
        return -1;

    return 0;
}

100 101 102 103 104
int get_watchdog_action(void)
{
    return watchdog_action;
}

R
Richard W.M. Jones 已提交
105 106 107 108 109
/* This actually performs the "action" once a watchdog has expired,
 * ie. reboot, shutdown, exit, etc.
 */
void watchdog_perform_action(void)
{
W
Wenchao Xia 已提交
110
    switch (watchdog_action) {
R
Richard W.M. Jones 已提交
111
    case WDT_RESET:             /* same as 'system_reset' in monitor */
W
Wenchao Xia 已提交
112
        qapi_event_send_watchdog(WATCHDOG_EXPIRATION_ACTION_RESET, &error_abort);
R
Richard W.M. Jones 已提交
113 114 115 116
        qemu_system_reset_request();
        break;

    case WDT_SHUTDOWN:          /* same as 'system_powerdown' in monitor */
W
Wenchao Xia 已提交
117
        qapi_event_send_watchdog(WATCHDOG_EXPIRATION_ACTION_SHUTDOWN, &error_abort);
R
Richard W.M. Jones 已提交
118 119 120 121
        qemu_system_powerdown_request();
        break;

    case WDT_POWEROFF:          /* same as 'quit' command in monitor */
W
Wenchao Xia 已提交
122
        qapi_event_send_watchdog(WATCHDOG_EXPIRATION_ACTION_POWEROFF, &error_abort);
R
Richard W.M. Jones 已提交
123 124 125
        exit(0);

    case WDT_PAUSE:             /* same as 'stop' command in monitor */
126 127 128 129
        /* In a timer callback, when vm_stop calls qemu_clock_enable
         * you would get a deadlock.  Bypass the problem.
         */
        qemu_system_vmstop_request_prepare();
W
Wenchao Xia 已提交
130
        qapi_event_send_watchdog(WATCHDOG_EXPIRATION_ACTION_PAUSE, &error_abort);
131
        qemu_system_vmstop_request(RUN_STATE_WATCHDOG);
R
Richard W.M. Jones 已提交
132 133 134
        break;

    case WDT_DEBUG:
W
Wenchao Xia 已提交
135
        qapi_event_send_watchdog(WATCHDOG_EXPIRATION_ACTION_DEBUG, &error_abort);
R
Richard W.M. Jones 已提交
136 137 138 139
        fprintf(stderr, "watchdog: timer fired\n");
        break;

    case WDT_NONE:
W
Wenchao Xia 已提交
140
        qapi_event_send_watchdog(WATCHDOG_EXPIRATION_ACTION_NONE, &error_abort);
R
Richard W.M. Jones 已提交
141
        break;
142 143 144 145

    case WDT_NMI:
        qapi_event_send_watchdog(WATCHDOG_EXPIRATION_ACTION_INJECT_NMI,
                                 &error_abort);
146
        nmi_monitor_handle(0, NULL);
147
        break;
R
Richard W.M. Jones 已提交
148 149
    }
}