Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 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 |
Blue Swirl | 8167ee8 | 2009-07-16 20:47:01 +0000 | [diff] [blame] | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 18 | * |
| 19 | * By Richard W.M. Jones (rjones@redhat.com). |
| 20 | */ |
| 21 | |
Peter Maydell | 0430891 | 2016-01-26 18:17:30 +0000 | [diff] [blame] | 22 | #include "qemu/osdep.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 23 | #include "qemu/option.h" |
| 24 | #include "qemu/config-file.h" |
| 25 | #include "qemu/queue.h" |
Markus Armbruster | e688df6 | 2018-02-01 12:18:31 +0100 | [diff] [blame] | 26 | #include "qapi/error.h" |
Markus Armbruster | 112ed24 | 2018-02-26 17:13:27 -0600 | [diff] [blame] | 27 | #include "qapi/qapi-commands-run-state.h" |
Markus Armbruster | 9af2398 | 2018-02-11 10:36:01 +0100 | [diff] [blame] | 28 | #include "qapi/qapi-events-run-state.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 29 | #include "sysemu/sysemu.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 30 | #include "sysemu/watchdog.h" |
Mao Chuan Li | 795dc6e | 2015-02-05 18:28:36 +0800 | [diff] [blame] | 31 | #include "hw/nmi.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 32 | #include "qemu/help_option.h" |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 33 | |
Michal Privoznik | 4c7f442 | 2017-09-07 10:05:25 +0200 | [diff] [blame] | 34 | static WatchdogAction watchdog_action = WATCHDOG_ACTION_RESET; |
Paolo Bonzini | b58deb3 | 2018-12-06 11:58:10 +0100 | [diff] [blame] | 35 | static QLIST_HEAD(, WatchdogTimerModel) watchdog_list; |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 36 | |
| 37 | void watchdog_add_model(WatchdogTimerModel *model) |
| 38 | { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 39 | QLIST_INSERT_HEAD(&watchdog_list, model, entry); |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | /* Returns: |
| 43 | * 0 = continue |
| 44 | * 1 = exit program with error |
| 45 | * 2 = exit program without error |
| 46 | */ |
| 47 | int select_watchdog(const char *p) |
| 48 | { |
| 49 | WatchdogTimerModel *model; |
Markus Armbruster | 09aaa16 | 2009-08-21 10:31:34 +0200 | [diff] [blame] | 50 | QemuOpts *opts; |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 51 | |
| 52 | /* -watchdog ? lists available devices and exits cleanly. */ |
Peter Maydell | c8057f9 | 2012-08-02 13:45:54 +0100 | [diff] [blame] | 53 | if (is_help_option(p)) { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 54 | QLIST_FOREACH(model, &watchdog_list, entry) { |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 55 | fprintf(stderr, "\t%s\t%s\n", |
| 56 | model->wdt_name, model->wdt_description); |
| 57 | } |
| 58 | return 2; |
| 59 | } |
| 60 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 61 | QLIST_FOREACH(model, &watchdog_list, entry) { |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 62 | if (strcasecmp(model->wdt_name, p) == 0) { |
Markus Armbruster | 09aaa16 | 2009-08-21 10:31:34 +0200 | [diff] [blame] | 63 | /* add the device */ |
Peter Crosthwaite | 87ea75d | 2014-01-01 18:49:17 -0800 | [diff] [blame] | 64 | opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0, |
| 65 | &error_abort); |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 66 | qemu_opt_set(opts, "driver", p, &error_abort); |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 67 | return 0; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | fprintf(stderr, "Unknown -watchdog device. Supported devices are:\n"); |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 72 | QLIST_FOREACH(model, &watchdog_list, entry) { |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 73 | fprintf(stderr, "\t%s\t%s\n", |
| 74 | model->wdt_name, model->wdt_description); |
| 75 | } |
| 76 | return 1; |
| 77 | } |
| 78 | |
| 79 | int select_watchdog_action(const char *p) |
| 80 | { |
Michal Privoznik | 4c7f442 | 2017-09-07 10:05:25 +0200 | [diff] [blame] | 81 | int action; |
| 82 | char *qapi_value; |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 83 | |
Michal Privoznik | 4c7f442 | 2017-09-07 10:05:25 +0200 | [diff] [blame] | 84 | qapi_value = g_ascii_strdown(p, -1); |
| 85 | action = qapi_enum_parse(&WatchdogAction_lookup, qapi_value, -1, NULL); |
| 86 | g_free(qapi_value); |
| 87 | if (action < 0) |
| 88 | return -1; |
Michal Privoznik | f0df84c | 2017-09-07 10:05:26 +0200 | [diff] [blame] | 89 | qmp_watchdog_set_action(action, &error_abort); |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 90 | return 0; |
| 91 | } |
| 92 | |
Michal Privoznik | 4c7f442 | 2017-09-07 10:05:25 +0200 | [diff] [blame] | 93 | WatchdogAction get_watchdog_action(void) |
Bo Tu | 0d035b6 | 2016-01-19 08:34:41 +0100 | [diff] [blame] | 94 | { |
| 95 | return watchdog_action; |
| 96 | } |
| 97 | |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 98 | /* This actually performs the "action" once a watchdog has expired, |
| 99 | * ie. reboot, shutdown, exit, etc. |
| 100 | */ |
| 101 | void watchdog_perform_action(void) |
| 102 | { |
Wenchao Xia | 2f44a08 | 2014-06-24 16:34:00 -0700 | [diff] [blame] | 103 | switch (watchdog_action) { |
Michal Privoznik | 4c7f442 | 2017-09-07 10:05:25 +0200 | [diff] [blame] | 104 | case WATCHDOG_ACTION_RESET: /* same as 'system_reset' in monitor */ |
Peter Xu | 3ab7238 | 2018-08-15 21:37:37 +0800 | [diff] [blame] | 105 | qapi_event_send_watchdog(WATCHDOG_ACTION_RESET); |
Eric Blake | cf83f14 | 2017-05-15 16:41:13 -0500 | [diff] [blame] | 106 | qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET); |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 107 | break; |
| 108 | |
Michal Privoznik | 4c7f442 | 2017-09-07 10:05:25 +0200 | [diff] [blame] | 109 | case WATCHDOG_ACTION_SHUTDOWN: /* same as 'system_powerdown' in monitor */ |
Peter Xu | 3ab7238 | 2018-08-15 21:37:37 +0800 | [diff] [blame] | 110 | qapi_event_send_watchdog(WATCHDOG_ACTION_SHUTDOWN); |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 111 | qemu_system_powerdown_request(); |
| 112 | break; |
| 113 | |
Michal Privoznik | 4c7f442 | 2017-09-07 10:05:25 +0200 | [diff] [blame] | 114 | case WATCHDOG_ACTION_POWEROFF: /* same as 'quit' command in monitor */ |
Peter Xu | 3ab7238 | 2018-08-15 21:37:37 +0800 | [diff] [blame] | 115 | qapi_event_send_watchdog(WATCHDOG_ACTION_POWEROFF); |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 116 | exit(0); |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 117 | |
Michal Privoznik | 4c7f442 | 2017-09-07 10:05:25 +0200 | [diff] [blame] | 118 | case WATCHDOG_ACTION_PAUSE: /* same as 'stop' command in monitor */ |
Paolo Bonzini | 30e5210 | 2014-06-27 16:31:07 +0200 | [diff] [blame] | 119 | /* In a timer callback, when vm_stop calls qemu_clock_enable |
| 120 | * you would get a deadlock. Bypass the problem. |
| 121 | */ |
| 122 | qemu_system_vmstop_request_prepare(); |
Peter Xu | 3ab7238 | 2018-08-15 21:37:37 +0800 | [diff] [blame] | 123 | qapi_event_send_watchdog(WATCHDOG_ACTION_PAUSE); |
Paolo Bonzini | 30e5210 | 2014-06-27 16:31:07 +0200 | [diff] [blame] | 124 | qemu_system_vmstop_request(RUN_STATE_WATCHDOG); |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 125 | break; |
| 126 | |
Michal Privoznik | 4c7f442 | 2017-09-07 10:05:25 +0200 | [diff] [blame] | 127 | case WATCHDOG_ACTION_DEBUG: |
Peter Xu | 3ab7238 | 2018-08-15 21:37:37 +0800 | [diff] [blame] | 128 | qapi_event_send_watchdog(WATCHDOG_ACTION_DEBUG); |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 129 | fprintf(stderr, "watchdog: timer fired\n"); |
| 130 | break; |
| 131 | |
Michal Privoznik | 4c7f442 | 2017-09-07 10:05:25 +0200 | [diff] [blame] | 132 | case WATCHDOG_ACTION_NONE: |
Peter Xu | 3ab7238 | 2018-08-15 21:37:37 +0800 | [diff] [blame] | 133 | qapi_event_send_watchdog(WATCHDOG_ACTION_NONE); |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 134 | break; |
Mao Chuan Li | 795dc6e | 2015-02-05 18:28:36 +0800 | [diff] [blame] | 135 | |
Michal Privoznik | 4c7f442 | 2017-09-07 10:05:25 +0200 | [diff] [blame] | 136 | case WATCHDOG_ACTION_INJECT_NMI: |
Peter Xu | 3ab7238 | 2018-08-15 21:37:37 +0800 | [diff] [blame] | 137 | qapi_event_send_watchdog(WATCHDOG_ACTION_INJECT_NMI); |
Bandan Das | f7e981f | 2016-05-20 12:28:36 -0400 | [diff] [blame] | 138 | nmi_monitor_handle(0, NULL); |
Mao Chuan Li | 795dc6e | 2015-02-05 18:28:36 +0800 | [diff] [blame] | 139 | break; |
Michal Privoznik | 4c7f442 | 2017-09-07 10:05:25 +0200 | [diff] [blame] | 140 | |
| 141 | default: |
| 142 | assert(0); |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 143 | } |
| 144 | } |
Michal Privoznik | f0df84c | 2017-09-07 10:05:26 +0200 | [diff] [blame] | 145 | |
| 146 | void qmp_watchdog_set_action(WatchdogAction action, Error **errp) |
| 147 | { |
| 148 | watchdog_action = action; |
| 149 | } |