Statistics
| Branch: | Revision:

root / hw / watchdog.c @ 88b3be20

History | View | Annotate | Download (3.9 kB)

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-common.h"
23
#include "sys-queue.h"
24
#include "sysemu.h"
25
#include "hw/watchdog.h"
26

    
27
/* Possible values for action parameter. */
28
#define WDT_RESET        1        /* Hard reset. */
29
#define WDT_SHUTDOWN     2        /* Shutdown. */
30
#define WDT_POWEROFF     3        /* Quit. */
31
#define WDT_PAUSE        4        /* Pause. */
32
#define WDT_DEBUG        5        /* Prints a message and continues running. */
33
#define WDT_NONE         6        /* Do nothing. */
34

    
35
static WatchdogTimerModel *watchdog;
36
static int watchdog_action = WDT_RESET;
37
static LIST_HEAD(watchdog_list, WatchdogTimerModel) watchdog_list;
38

    
39
void watchdog_add_model(WatchdogTimerModel *model)
40
{
41
    LIST_INSERT_HEAD(&watchdog_list, model, entry);
42
}
43

    
44
/* Returns:
45
 *   0 = continue
46
 *   1 = exit program with error
47
 *   2 = exit program without error
48
 */
49
int select_watchdog(const char *p)
50
{
51
    WatchdogTimerModel *model;
52

    
53
    if (watchdog) {
54
        fprintf(stderr,
55
                 "qemu: only one watchdog option may be given\n");
56
        return 1;
57
    }
58

    
59
    /* -watchdog ? lists available devices and exits cleanly. */
60
    if (strcmp(p, "?") == 0) {
61
        LIST_FOREACH(model, &watchdog_list, entry) {
62
            fprintf(stderr, "\t%s\t%s\n",
63
                     model->wdt_name, model->wdt_description);
64
        }
65
        return 2;
66
    }
67

    
68
    LIST_FOREACH(model, &watchdog_list, entry) {
69
        if (strcasecmp(model->wdt_name, p) == 0) {
70
            watchdog = model;
71
            return 0;
72
        }
73
    }
74

    
75
    fprintf(stderr, "Unknown -watchdog device. Supported devices are:\n");
76
    LIST_FOREACH(model, &watchdog_list, entry) {
77
        fprintf(stderr, "\t%s\t%s\n",
78
                 model->wdt_name, model->wdt_description);
79
    }
80
    return 1;
81
}
82

    
83
int select_watchdog_action(const char *p)
84
{
85
    if (strcasecmp(p, "reset") == 0)
86
        watchdog_action = WDT_RESET;
87
    else if (strcasecmp(p, "shutdown") == 0)
88
        watchdog_action = WDT_SHUTDOWN;
89
    else if (strcasecmp(p, "poweroff") == 0)
90
        watchdog_action = WDT_POWEROFF;
91
    else if (strcasecmp(p, "pause") == 0)
92
        watchdog_action = WDT_PAUSE;
93
    else if (strcasecmp(p, "debug") == 0)
94
        watchdog_action = WDT_DEBUG;
95
    else if (strcasecmp(p, "none") == 0)
96
        watchdog_action = WDT_NONE;
97
    else
98
        return -1;
99

    
100
    return 0;
101
}
102

    
103
/* This actually performs the "action" once a watchdog has expired,
104
 * ie. reboot, shutdown, exit, etc.
105
 */
106
void watchdog_perform_action(void)
107
{
108
    switch(watchdog_action) {
109
    case WDT_RESET:             /* same as 'system_reset' in monitor */
110
        qemu_system_reset_request();
111
        break;
112

    
113
    case WDT_SHUTDOWN:          /* same as 'system_powerdown' in monitor */
114
        qemu_system_powerdown_request();
115
        break;
116

    
117
    case WDT_POWEROFF:          /* same as 'quit' command in monitor */
118
        exit(0);
119
        break;
120

    
121
    case WDT_PAUSE:             /* same as 'stop' command in monitor */
122
        vm_stop(0);
123
        break;
124

    
125
    case WDT_DEBUG:
126
        fprintf(stderr, "watchdog: timer fired\n");
127
        break;
128

    
129
    case WDT_NONE:
130
        break;
131
    }
132
}
133

    
134
void watchdog_pc_init(PCIBus *pci_bus)
135
{
136
    if (watchdog)
137
        watchdog->wdt_pc_init(pci_bus);
138
}
139

    
140
void register_watchdogs(void)
141
{
142
    wdt_ib700_init();
143
    wdt_i6300esb_init();
144
}