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 "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 32 /* Possible values for action parameter. */ 33 #define WDT_RESET 1 /* Hard reset. */ 34 #define WDT_SHUTDOWN 2 /* Shutdown. */ 35 #define WDT_POWEROFF 3 /* Quit. */ 36 #define WDT_PAUSE 4 /* Pause. */ 37 #define WDT_DEBUG 5 /* Prints a message and continues running. */ 38 #define WDT_NONE 6 /* Do nothing. */ 39 #define WDT_NMI 7 /* Inject nmi into the guest */ 40 41 static int watchdog_action = WDT_RESET; 42 static QLIST_HEAD(watchdog_list, WatchdogTimerModel) watchdog_list; 43 44 void watchdog_add_model(WatchdogTimerModel *model) 45 { 46 QLIST_INSERT_HEAD(&watchdog_list, model, entry); 47 } 48 49 /* Returns: 50 * 0 = continue 51 * 1 = exit program with error 52 * 2 = exit program without error 53 */ 54 int select_watchdog(const char *p) 55 { 56 WatchdogTimerModel *model; 57 QemuOpts *opts; 58 59 /* -watchdog ? lists available devices and exits cleanly. */ 60 if (is_help_option(p)) { 61 QLIST_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 QLIST_FOREACH(model, &watchdog_list, entry) { 69 if (strcasecmp(model->wdt_name, p) == 0) { 70 /* add the device */ 71 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0, 72 &error_abort); 73 qemu_opt_set(opts, "driver", p, &error_abort); 74 return 0; 75 } 76 } 77 78 fprintf(stderr, "Unknown -watchdog device. Supported devices are:\n"); 79 QLIST_FOREACH(model, &watchdog_list, entry) { 80 fprintf(stderr, "\t%s\t%s\n", 81 model->wdt_name, model->wdt_description); 82 } 83 return 1; 84 } 85 86 int select_watchdog_action(const char *p) 87 { 88 if (strcasecmp(p, "reset") == 0) 89 watchdog_action = WDT_RESET; 90 else if (strcasecmp(p, "shutdown") == 0) 91 watchdog_action = WDT_SHUTDOWN; 92 else if (strcasecmp(p, "poweroff") == 0) 93 watchdog_action = WDT_POWEROFF; 94 else if (strcasecmp(p, "pause") == 0) 95 watchdog_action = WDT_PAUSE; 96 else if (strcasecmp(p, "debug") == 0) 97 watchdog_action = WDT_DEBUG; 98 else if (strcasecmp(p, "none") == 0) 99 watchdog_action = WDT_NONE; 100 else if (strcasecmp(p, "inject-nmi") == 0) 101 watchdog_action = WDT_NMI; 102 else 103 return -1; 104 105 return 0; 106 } 107 108 /* This actually performs the "action" once a watchdog has expired, 109 * ie. reboot, shutdown, exit, etc. 110 */ 111 void watchdog_perform_action(void) 112 { 113 switch (watchdog_action) { 114 case WDT_RESET: /* same as 'system_reset' in monitor */ 115 qapi_event_send_watchdog(WATCHDOG_EXPIRATION_ACTION_RESET, &error_abort); 116 qemu_system_reset_request(); 117 break; 118 119 case WDT_SHUTDOWN: /* same as 'system_powerdown' in monitor */ 120 qapi_event_send_watchdog(WATCHDOG_EXPIRATION_ACTION_SHUTDOWN, &error_abort); 121 qemu_system_powerdown_request(); 122 break; 123 124 case WDT_POWEROFF: /* same as 'quit' command in monitor */ 125 qapi_event_send_watchdog(WATCHDOG_EXPIRATION_ACTION_POWEROFF, &error_abort); 126 exit(0); 127 128 case WDT_PAUSE: /* same as 'stop' command in monitor */ 129 /* In a timer callback, when vm_stop calls qemu_clock_enable 130 * you would get a deadlock. Bypass the problem. 131 */ 132 qemu_system_vmstop_request_prepare(); 133 qapi_event_send_watchdog(WATCHDOG_EXPIRATION_ACTION_PAUSE, &error_abort); 134 qemu_system_vmstop_request(RUN_STATE_WATCHDOG); 135 break; 136 137 case WDT_DEBUG: 138 qapi_event_send_watchdog(WATCHDOG_EXPIRATION_ACTION_DEBUG, &error_abort); 139 fprintf(stderr, "watchdog: timer fired\n"); 140 break; 141 142 case WDT_NONE: 143 qapi_event_send_watchdog(WATCHDOG_EXPIRATION_ACTION_NONE, &error_abort); 144 break; 145 146 case WDT_NMI: 147 qapi_event_send_watchdog(WATCHDOG_EXPIRATION_ACTION_INJECT_NMI, 148 &error_abort); 149 inject_nmi(); 150 break; 151 } 152 } 153