Hardware watchdog
[qemu] / hw / watchdog.c
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, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
19  * USA.
20  *
21  * By Richard W.M. Jones (rjones@redhat.com).
22  */
23
24 #include "qemu-common.h"
25 #include "sys-queue.h"
26 #include "sysemu.h"
27 #include "hw/watchdog.h"
28
29 static LIST_HEAD(watchdog_list, WatchdogTimerModel) watchdog_list;
30
31 void watchdog_add_model(WatchdogTimerModel *model)
32 {
33     LIST_INSERT_HEAD(&watchdog_list, model, entry);
34 }
35
36 /* Returns:
37  *   0 = continue
38  *   1 = exit program with error
39  *   2 = exit program without error
40  */
41 int select_watchdog(const char *p)
42 {
43     WatchdogTimerModel *model;
44
45     if (watchdog) {
46         fprintf(stderr,
47                  "qemu: only one watchdog option may be given\n");
48         return 1;
49     }
50
51     /* -watchdog ? lists available devices and exits cleanly. */
52     if (strcmp(p, "?") == 0) {
53         LIST_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     LIST_FOREACH(model, &watchdog_list, entry) {
61         if (strcasecmp(model->wdt_name, p) == 0) {
62             watchdog = model;
63             return 0;
64         }
65     }
66
67     fprintf(stderr, "Unknown -watchdog device. Supported devices are:\n");
68     LIST_FOREACH(model, &watchdog_list, entry) {
69         fprintf(stderr, "\t%s\t%s\n",
70                  model->wdt_name, model->wdt_description);
71     }
72     return 1;
73 }
74
75 int select_watchdog_action(const char *p)
76 {
77     if (strcasecmp(p, "reset") == 0)
78         watchdog_action = WDT_RESET;
79     else if (strcasecmp(p, "shutdown") == 0)
80         watchdog_action = WDT_SHUTDOWN;
81     else if (strcasecmp(p, "poweroff") == 0)
82         watchdog_action = WDT_POWEROFF;
83     else if (strcasecmp(p, "pause") == 0)
84         watchdog_action = WDT_PAUSE;
85     else if (strcasecmp(p, "debug") == 0)
86         watchdog_action = WDT_DEBUG;
87     else if (strcasecmp(p, "none") == 0)
88         watchdog_action = WDT_NONE;
89     else
90         return -1;
91
92     return 0;
93 }
94
95 /* This actually performs the "action" once a watchdog has expired,
96  * ie. reboot, shutdown, exit, etc.
97  */
98 void watchdog_perform_action(void)
99 {
100     switch(watchdog_action) {
101     case WDT_RESET:             /* same as 'system_reset' in monitor */
102         qemu_system_reset_request();
103         break;
104
105     case WDT_SHUTDOWN:          /* same as 'system_powerdown' in monitor */
106         qemu_system_powerdown_request();
107         break;
108
109     case WDT_POWEROFF:          /* same as 'quit' command in monitor */
110         exit(0);
111         break;
112
113     case WDT_PAUSE:             /* same as 'stop' command in monitor */
114         vm_stop(0);
115         break;
116
117     case WDT_DEBUG:
118         fprintf(stderr, "watchdog: timer fired\n");
119         break;
120
121     case WDT_NONE:
122         break;
123     }
124 }
125
126 void watchdog_pc_init(PCIBus *pci_bus)
127 {
128     if (watchdog)
129         watchdog->wdt_pc_init(pci_bus);
130 }
131
132 void register_watchdogs(void)
133 {
134     wdt_ib700_init();
135     wdt_i6300esb_init();
136 }