qemu/hw/watchdog/watchdog.c
<<
>>
Prefs
   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
  17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
  18 *
  19 * By Richard W.M. Jones (rjones@redhat.com).
  20 */
  21
  22#include "qemu/osdep.h"
  23#include "qemu/option.h"
  24#include "qemu/config-file.h"
  25#include "qemu/queue.h"
  26#include "qapi/qmp/types.h"
  27#include "sysemu/sysemu.h"
  28#include "sysemu/watchdog.h"
  29#include "qapi-event.h"
  30#include "hw/nmi.h"
  31#include "qemu/help_option.h"
  32
  33static int watchdog_action = WDT_RESET;
  34static QLIST_HEAD(watchdog_list, WatchdogTimerModel) watchdog_list;
  35
  36void watchdog_add_model(WatchdogTimerModel *model)
  37{
  38    QLIST_INSERT_HEAD(&watchdog_list, model, entry);
  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;
  49    QemuOpts *opts;
  50
  51    /* -watchdog ? lists available devices and exits cleanly. */
  52    if (is_help_option(p)) {
  53        QLIST_FOREACH(model, &watchdog_list, entry) {
  54            fprintf(stderr, "\t%s\t%s\n",
  55                     model->wdt_name, model->wdt_description);
  56        }
  57        return 2;
  58    }
  59
  60    QLIST_FOREACH(model, &watchdog_list, entry) {
  61        if (strcasecmp(model->wdt_name, p) == 0) {
  62            /* add the device */
  63            opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
  64                                    &error_abort);
  65            qemu_opt_set(opts, "driver", p, &error_abort);
  66            return 0;
  67        }
  68    }
  69
  70    fprintf(stderr, "Unknown -watchdog device. Supported devices are:\n");
  71    QLIST_FOREACH(model, &watchdog_list, entry) {
  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;
  92    else if (strcasecmp(p, "inject-nmi") == 0)
  93        watchdog_action = WDT_NMI;
  94    else
  95        return -1;
  96
  97    return 0;
  98}
  99
 100int get_watchdog_action(void)
 101{
 102    return watchdog_action;
 103}
 104
 105/* This actually performs the "action" once a watchdog has expired,
 106 * ie. reboot, shutdown, exit, etc.
 107 */
 108void watchdog_perform_action(void)
 109{
 110    switch (watchdog_action) {
 111    case WDT_RESET:             /* same as 'system_reset' in monitor */
 112        qapi_event_send_watchdog(WATCHDOG_EXPIRATION_ACTION_RESET, &error_abort);
 113        qemu_system_reset_request();
 114        break;
 115
 116    case WDT_SHUTDOWN:          /* same as 'system_powerdown' in monitor */
 117        qapi_event_send_watchdog(WATCHDOG_EXPIRATION_ACTION_SHUTDOWN, &error_abort);
 118        qemu_system_powerdown_request();
 119        break;
 120
 121    case WDT_POWEROFF:          /* same as 'quit' command in monitor */
 122        qapi_event_send_watchdog(WATCHDOG_EXPIRATION_ACTION_POWEROFF, &error_abort);
 123        exit(0);
 124
 125    case WDT_PAUSE:             /* same as 'stop' command in monitor */
 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();
 130        qapi_event_send_watchdog(WATCHDOG_EXPIRATION_ACTION_PAUSE, &error_abort);
 131        qemu_system_vmstop_request(RUN_STATE_WATCHDOG);
 132        break;
 133
 134    case WDT_DEBUG:
 135        qapi_event_send_watchdog(WATCHDOG_EXPIRATION_ACTION_DEBUG, &error_abort);
 136        fprintf(stderr, "watchdog: timer fired\n");
 137        break;
 138
 139    case WDT_NONE:
 140        qapi_event_send_watchdog(WATCHDOG_EXPIRATION_ACTION_NONE, &error_abort);
 141        break;
 142
 143    case WDT_NMI:
 144        qapi_event_send_watchdog(WATCHDOG_EXPIRATION_ACTION_INJECT_NMI,
 145                                 &error_abort);
 146        inject_nmi();
 147        break;
 148    }
 149}
 150