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