xref: /qemu/hw/watchdog/watchdog.c (revision b2a3cbb8)
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/error.h"
27 #include "qapi/qapi-commands-run-state.h"
28 #include "qapi/qapi-events-run-state.h"
29 #include "sysemu/runstate.h"
30 #include "sysemu/watchdog.h"
31 #include "hw/nmi.h"
32 #include "qemu/help_option.h"
33 
34 static WatchdogAction watchdog_action = WATCHDOG_ACTION_RESET;
35 
36 WatchdogAction get_watchdog_action(void)
37 {
38     return watchdog_action;
39 }
40 
41 /* This actually performs the "action" once a watchdog has expired,
42  * ie. reboot, shutdown, exit, etc.
43  */
44 void watchdog_perform_action(void)
45 {
46     switch (watchdog_action) {
47     case WATCHDOG_ACTION_RESET:     /* same as 'system_reset' in monitor */
48         qapi_event_send_watchdog(WATCHDOG_ACTION_RESET);
49         qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
50         break;
51 
52     case WATCHDOG_ACTION_SHUTDOWN:  /* same as 'system_powerdown' in monitor */
53         qapi_event_send_watchdog(WATCHDOG_ACTION_SHUTDOWN);
54         qemu_system_powerdown_request();
55         break;
56 
57     case WATCHDOG_ACTION_POWEROFF:  /* same as 'quit' command in monitor */
58         qapi_event_send_watchdog(WATCHDOG_ACTION_POWEROFF);
59         exit(0);
60 
61     case WATCHDOG_ACTION_PAUSE:     /* same as 'stop' command in monitor */
62         /* In a timer callback, when vm_stop calls qemu_clock_enable
63          * you would get a deadlock.  Bypass the problem.
64          */
65         qemu_system_vmstop_request_prepare();
66         qapi_event_send_watchdog(WATCHDOG_ACTION_PAUSE);
67         qemu_system_vmstop_request(RUN_STATE_WATCHDOG);
68         break;
69 
70     case WATCHDOG_ACTION_DEBUG:
71         qapi_event_send_watchdog(WATCHDOG_ACTION_DEBUG);
72         fprintf(stderr, "watchdog: timer fired\n");
73         break;
74 
75     case WATCHDOG_ACTION_NONE:
76         qapi_event_send_watchdog(WATCHDOG_ACTION_NONE);
77         break;
78 
79     case WATCHDOG_ACTION_INJECT_NMI:
80         qapi_event_send_watchdog(WATCHDOG_ACTION_INJECT_NMI);
81         nmi_monitor_handle(0, NULL);
82         break;
83 
84     default:
85         assert(0);
86     }
87 }
88 
89 void qmp_watchdog_set_action(WatchdogAction action, Error **errp)
90 {
91     watchdog_action = action;
92 }
93