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