| 1 | /* |
| 2 | * QEMU main system emulation loop |
| 3 | * |
| 4 | * Copyright (c) 2003-2020 QEMU contributors |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | #include "qemu/osdep.h" |
| 26 | #include "qemu/audio.h" |
| 27 | #include "block/block.h" |
| 28 | #include "block/export.h" |
| 29 | #include "chardev/char.h" |
| 30 | #include "crypto/cipher.h" |
| 31 | #include "crypto/init.h" |
| 32 | #include "exec/cpu-common.h" |
| 33 | #include "gdbstub/syscalls.h" |
| 34 | #include "hw/core/boards.h" |
| 35 | #include "hw/core/resettable.h" |
| 36 | #include "migration/misc.h" |
| 37 | #include "migration/postcopy-ram.h" |
| 38 | #include "monitor/monitor.h" |
| 39 | #include "net/net.h" |
| 40 | #include "net/vhost_net.h" |
| 41 | #include "qapi/error.h" |
| 42 | #include "qapi/qapi-commands-run-state.h" |
| 43 | #include "qapi/qapi-events-run-state.h" |
| 44 | #include "qemu/accel.h" |
| 45 | #include "accel/accel-ops.h" |
| 46 | #include "qemu/error-report.h" |
| 47 | #include "qemu/job.h" |
| 48 | #include "qemu/log.h" |
| 49 | #include "qemu/module.h" |
| 50 | #include "qemu/sockets.h" |
| 51 | #include "qemu/timer.h" |
| 52 | #include "qemu/thread.h" |
| 53 | #include "qom/object.h" |
| 54 | #include "qom/object_interfaces.h" |
| 55 | #include "system/cpu-timers.h" |
| 56 | #include "system/cpus.h" |
| 57 | #include "system/qtest.h" |
| 58 | #include "system/replay.h" |
| 59 | #include "system/reset.h" |
| 60 | #include "system/runstate.h" |
| 61 | #include "system/runstate-action.h" |
| 62 | #include "system/confidential-guest-support.h" |
| 63 | #include "system/system.h" |
| 64 | #include "system/tpm.h" |
| 65 | #include "ui/console.h" |
| 66 | |
| 67 | #include "trace.h" |
| 68 | |
| 69 | static NotifierList exit_notifiers = |
| 70 | NOTIFIER_LIST_INITIALIZER(exit_notifiers); |
| 71 | |
| 72 | static RunState current_run_state = RUN_STATE_PRELAUNCH; |
| 73 | |
| 74 | /* We use RUN_STATE__MAX but any invalid value will do */ |
| 75 | static RunState vmstop_requested = RUN_STATE__MAX; |
| 76 | static QemuMutex vmstop_lock; |
| 77 | |
| 78 | typedef struct { |
| 79 | RunState from; |
| 80 | RunState to; |
| 81 | } RunStateTransition; |
| 82 | |
| 83 | static const RunStateTransition runstate_transitions_def[] = { |
| 84 | { RUN_STATE_DEBUG, RUN_STATE_RUNNING }, |
| 85 | { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE }, |
| 86 | { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH }, |
| 87 | |
| 88 | { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR }, |
| 89 | { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR }, |
| 90 | { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED }, |
| 91 | { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING }, |
| 92 | { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN }, |
| 93 | { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED }, |
| 94 | { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG }, |
| 95 | { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED }, |
| 96 | { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE }, |
| 97 | { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH }, |
| 98 | { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE }, |
| 99 | { RUN_STATE_INMIGRATE, RUN_STATE_COLO }, |
| 100 | |
| 101 | { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED }, |
| 102 | { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE }, |
| 103 | { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH }, |
| 104 | |
| 105 | { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING }, |
| 106 | { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE }, |
| 107 | { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH }, |
| 108 | |
| 109 | { RUN_STATE_PAUSED, RUN_STATE_RUNNING }, |
| 110 | { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE }, |
| 111 | { RUN_STATE_PAUSED, RUN_STATE_POSTMIGRATE }, |
| 112 | { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH }, |
| 113 | { RUN_STATE_PAUSED, RUN_STATE_COLO}, |
| 114 | { RUN_STATE_PAUSED, RUN_STATE_SUSPENDED}, |
| 115 | |
| 116 | { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING }, |
| 117 | { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE }, |
| 118 | { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH }, |
| 119 | |
| 120 | { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING }, |
| 121 | { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE }, |
| 122 | { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE }, |
| 123 | { RUN_STATE_PRELAUNCH, RUN_STATE_SUSPENDED }, |
| 124 | |
| 125 | { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING }, |
| 126 | { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PAUSED }, |
| 127 | { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE }, |
| 128 | { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH }, |
| 129 | { RUN_STATE_FINISH_MIGRATE, RUN_STATE_COLO }, |
| 130 | { RUN_STATE_FINISH_MIGRATE, RUN_STATE_INTERNAL_ERROR }, |
| 131 | { RUN_STATE_FINISH_MIGRATE, RUN_STATE_IO_ERROR }, |
| 132 | { RUN_STATE_FINISH_MIGRATE, RUN_STATE_SHUTDOWN }, |
| 133 | { RUN_STATE_FINISH_MIGRATE, RUN_STATE_SUSPENDED }, |
| 134 | { RUN_STATE_FINISH_MIGRATE, RUN_STATE_WATCHDOG }, |
| 135 | { RUN_STATE_FINISH_MIGRATE, RUN_STATE_GUEST_PANICKED }, |
| 136 | |
| 137 | { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING }, |
| 138 | { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH }, |
| 139 | { RUN_STATE_RESTORE_VM, RUN_STATE_SUSPENDED }, |
| 140 | |
| 141 | { RUN_STATE_COLO, RUN_STATE_RUNNING }, |
| 142 | { RUN_STATE_COLO, RUN_STATE_PRELAUNCH }, |
| 143 | { RUN_STATE_COLO, RUN_STATE_SHUTDOWN}, |
| 144 | |
| 145 | { RUN_STATE_RUNNING, RUN_STATE_DEBUG }, |
| 146 | { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR }, |
| 147 | { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR }, |
| 148 | { RUN_STATE_RUNNING, RUN_STATE_PAUSED }, |
| 149 | { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE }, |
| 150 | { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM }, |
| 151 | { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM }, |
| 152 | { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN }, |
| 153 | { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG }, |
| 154 | { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED }, |
| 155 | { RUN_STATE_RUNNING, RUN_STATE_COLO}, |
| 156 | |
| 157 | { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING }, |
| 158 | { RUN_STATE_SAVE_VM, RUN_STATE_SUSPENDED }, |
| 159 | |
| 160 | { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED }, |
| 161 | { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE }, |
| 162 | { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH }, |
| 163 | { RUN_STATE_SHUTDOWN, RUN_STATE_COLO }, |
| 164 | |
| 165 | { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED }, |
| 166 | { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED }, |
| 167 | { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING }, |
| 168 | { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE }, |
| 169 | { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH }, |
| 170 | { RUN_STATE_SUSPENDED, RUN_STATE_COLO}, |
| 171 | { RUN_STATE_SUSPENDED, RUN_STATE_PAUSED}, |
| 172 | { RUN_STATE_SUSPENDED, RUN_STATE_SAVE_VM }, |
| 173 | { RUN_STATE_SUSPENDED, RUN_STATE_RESTORE_VM }, |
| 174 | { RUN_STATE_SUSPENDED, RUN_STATE_SHUTDOWN }, |
| 175 | |
| 176 | { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING }, |
| 177 | { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE }, |
| 178 | { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH }, |
| 179 | { RUN_STATE_WATCHDOG, RUN_STATE_COLO}, |
| 180 | |
| 181 | { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING }, |
| 182 | { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE }, |
| 183 | { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH }, |
| 184 | |
| 185 | { RUN_STATE__MAX, RUN_STATE__MAX }, |
| 186 | }; |
| 187 | |
| 188 | static const RunStateTransition replay_play_runstate_transitions_def[] = { |
| 189 | { RUN_STATE_SHUTDOWN, RUN_STATE_RUNNING}, |
| 190 | |
| 191 | { RUN_STATE__MAX, RUN_STATE__MAX }, |
| 192 | }; |
| 193 | |
| 194 | static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX]; |
| 195 | |
| 196 | bool runstate_check(RunState state) |
| 197 | { |
| 198 | return current_run_state == state; |
| 199 | } |
| 200 | |
| 201 | static void transitions_set_valid(const RunStateTransition *rst) |
| 202 | { |
| 203 | const RunStateTransition *p; |
| 204 | |
| 205 | for (p = rst; p->from != RUN_STATE__MAX; p++) { |
| 206 | runstate_valid_transitions[p->from][p->to] = true; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | void runstate_replay_enable(void) |
| 211 | { |
| 212 | assert(replay_mode != REPLAY_MODE_NONE); |
| 213 | |
| 214 | if (replay_mode == REPLAY_MODE_PLAY) { |
| 215 | /* |
| 216 | * When reverse-debugging, it is possible to move state from |
| 217 | * shutdown to running. |
| 218 | */ |
| 219 | transitions_set_valid(&replay_play_runstate_transitions_def[0]); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | static void runstate_init(void) |
| 224 | { |
| 225 | memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions)); |
| 226 | |
| 227 | transitions_set_valid(&runstate_transitions_def[0]); |
| 228 | |
| 229 | qemu_mutex_init(&vmstop_lock); |
| 230 | } |
| 231 | |
| 232 | /* This function will abort() on invalid state transitions */ |
| 233 | void runstate_set(RunState new_state) |
| 234 | { |
| 235 | assert(new_state < RUN_STATE__MAX); |
| 236 | |
| 237 | trace_runstate_set(current_run_state, RunState_str(current_run_state), |
| 238 | new_state, RunState_str(new_state)); |
| 239 | |
| 240 | if (current_run_state == new_state) { |
| 241 | return; |
| 242 | } |
| 243 | |
| 244 | if (!runstate_valid_transitions[current_run_state][new_state]) { |
| 245 | error_report("invalid runstate transition: '%s' -> '%s'", |
| 246 | RunState_str(current_run_state), |
| 247 | RunState_str(new_state)); |
| 248 | abort(); |
| 249 | } |
| 250 | |
| 251 | current_run_state = new_state; |
| 252 | } |
| 253 | |
| 254 | RunState runstate_get(void) |
| 255 | { |
| 256 | return current_run_state; |
| 257 | } |
| 258 | |
| 259 | bool runstate_is_running(void) |
| 260 | { |
| 261 | return runstate_check(RUN_STATE_RUNNING); |
| 262 | } |
| 263 | |
| 264 | bool runstate_needs_reset(void) |
| 265 | { |
| 266 | return runstate_check(RUN_STATE_INTERNAL_ERROR) || |
| 267 | runstate_check(RUN_STATE_SHUTDOWN); |
| 268 | } |
| 269 | |
| 270 | StatusInfo *qmp_query_status(Error **errp) |
| 271 | { |
| 272 | StatusInfo *info = g_malloc0(sizeof(*info)); |
| 273 | |
| 274 | info->running = runstate_is_running(); |
| 275 | info->status = current_run_state; |
| 276 | |
| 277 | return info; |
| 278 | } |
| 279 | |
| 280 | bool qemu_vmstop_requested(RunState *r) |
| 281 | { |
| 282 | qemu_mutex_lock(&vmstop_lock); |
| 283 | *r = vmstop_requested; |
| 284 | vmstop_requested = RUN_STATE__MAX; |
| 285 | qemu_mutex_unlock(&vmstop_lock); |
| 286 | return *r < RUN_STATE__MAX; |
| 287 | } |
| 288 | |
| 289 | void qemu_system_vmstop_request_prepare(void) |
| 290 | { |
| 291 | qemu_mutex_lock(&vmstop_lock); |
| 292 | } |
| 293 | |
| 294 | void qemu_system_vmstop_request(RunState state) |
| 295 | { |
| 296 | vmstop_requested = state; |
| 297 | qemu_mutex_unlock(&vmstop_lock); |
| 298 | qemu_notify_event(); |
| 299 | } |
| 300 | struct VMChangeStateEntry { |
| 301 | VMChangeStateHandler *cb; |
| 302 | VMChangeStateHandler *prepare_cb; |
| 303 | VMChangeStateHandlerWithRet *cb_ret; |
| 304 | void *opaque; |
| 305 | QTAILQ_ENTRY(VMChangeStateEntry) entries; |
| 306 | int priority; |
| 307 | }; |
| 308 | |
| 309 | static QTAILQ_HEAD(, VMChangeStateEntry) vm_change_state_head = |
| 310 | QTAILQ_HEAD_INITIALIZER(vm_change_state_head); |
| 311 | |
| 312 | VMChangeStateEntry *qemu_add_vm_change_state_handler_prio( |
| 313 | VMChangeStateHandler *cb, void *opaque, int priority) |
| 314 | { |
| 315 | return qemu_add_vm_change_state_handler_prio_full(cb, NULL, NULL, |
| 316 | opaque, priority); |
| 317 | } |
| 318 | |
| 319 | VMChangeStateEntry * |
| 320 | qemu_add_vm_change_state_handler_prio_full(VMChangeStateHandler *cb, |
| 321 | VMChangeStateHandler *prepare_cb, |
| 322 | VMChangeStateHandlerWithRet *cb_ret, |
| 323 | void *opaque, int priority) |
| 324 | { |
| 325 | VMChangeStateEntry *e; |
| 326 | VMChangeStateEntry *other; |
| 327 | |
| 328 | e = g_malloc0(sizeof(*e)); |
| 329 | e->cb = cb; |
| 330 | e->prepare_cb = prepare_cb; |
| 331 | e->cb_ret = cb_ret; |
| 332 | e->opaque = opaque; |
| 333 | e->priority = priority; |
| 334 | |
| 335 | /* Keep list sorted in ascending priority order */ |
| 336 | QTAILQ_FOREACH(other, &vm_change_state_head, entries) { |
| 337 | if (priority < other->priority) { |
| 338 | QTAILQ_INSERT_BEFORE(other, e, entries); |
| 339 | return e; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | QTAILQ_INSERT_TAIL(&vm_change_state_head, e, entries); |
| 344 | return e; |
| 345 | } |
| 346 | |
| 347 | VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb, |
| 348 | void *opaque) |
| 349 | { |
| 350 | return qemu_add_vm_change_state_handler_prio(cb, opaque, 0); |
| 351 | } |
| 352 | |
| 353 | void qemu_del_vm_change_state_handler(VMChangeStateEntry *e) |
| 354 | { |
| 355 | if (!e) { |
| 356 | return; |
| 357 | } |
| 358 | QTAILQ_REMOVE(&vm_change_state_head, e, entries); |
| 359 | g_free(e); |
| 360 | } |
| 361 | |
| 362 | int vm_state_notify(bool running, RunState state) |
| 363 | { |
| 364 | VMChangeStateEntry *e, *next; |
| 365 | int ret = 0; |
| 366 | |
| 367 | trace_vm_state_notify(running, state, RunState_str(state)); |
| 368 | |
| 369 | if (running) { |
| 370 | QTAILQ_FOREACH_SAFE(e, &vm_change_state_head, entries, next) { |
| 371 | if (e->prepare_cb) { |
| 372 | e->prepare_cb(e->opaque, running, state); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | QTAILQ_FOREACH_SAFE(e, &vm_change_state_head, entries, next) { |
| 377 | if (e->cb) { |
| 378 | e->cb(e->opaque, running, state); |
| 379 | } else if (e->cb_ret) { |
| 380 | /* |
| 381 | * Here ignore the return value of cb_ret because |
| 382 | * we only care about the stopping the device during |
| 383 | * the VM live migration to indicate whether the |
| 384 | * connection between qemu and backend is normal. |
| 385 | */ |
| 386 | e->cb_ret(e->opaque, running, state); |
| 387 | } |
| 388 | } |
| 389 | } else { |
| 390 | QTAILQ_FOREACH_REVERSE_SAFE(e, &vm_change_state_head, entries, next) { |
| 391 | if (e->prepare_cb) { |
| 392 | e->prepare_cb(e->opaque, running, state); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | QTAILQ_FOREACH_REVERSE_SAFE(e, &vm_change_state_head, entries, next) { |
| 397 | if (e->cb) { |
| 398 | e->cb(e->opaque, running, state); |
| 399 | } else if (e->cb_ret) { |
| 400 | /* |
| 401 | * We should execute all registered callbacks even if |
| 402 | * one of them returns failure, otherwise, some cleanup |
| 403 | * work of the device will be skipped. |
| 404 | */ |
| 405 | ret |= e->cb_ret(e->opaque, running, state); |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | return ret; |
| 410 | } |
| 411 | |
| 412 | /* |
| 413 | * True if the vm was previously suspended, and has not been woken or reset. |
| 414 | */ |
| 415 | static int vm_was_suspended; |
| 416 | |
| 417 | void vm_set_suspended(bool suspended) |
| 418 | { |
| 419 | vm_was_suspended = suspended; |
| 420 | } |
| 421 | |
| 422 | bool vm_get_suspended(void) |
| 423 | { |
| 424 | return vm_was_suspended; |
| 425 | } |
| 426 | |
| 427 | static int do_vm_stop(RunState state, bool send_stop) |
| 428 | { |
| 429 | int ret = 0; |
| 430 | RunState oldstate = runstate_get(); |
| 431 | |
| 432 | if (runstate_is_live(oldstate)) { |
| 433 | vm_was_suspended = (oldstate == RUN_STATE_SUSPENDED); |
| 434 | runstate_set(state); |
| 435 | cpu_disable_ticks(); |
| 436 | if (oldstate == RUN_STATE_RUNNING) { |
| 437 | pause_all_vcpus(); |
| 438 | } |
| 439 | ret = vm_state_notify(0, state); |
| 440 | if (send_stop) { |
| 441 | qapi_event_send_stop(); |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | bdrv_drain_all(); |
| 446 | /* |
| 447 | * Even if vm_state_notify() return failure, |
| 448 | * it would be better to flush as before. |
| 449 | */ |
| 450 | ret |= bdrv_flush_all(); |
| 451 | trace_vm_stop_flush_all(ret); |
| 452 | |
| 453 | return ret; |
| 454 | } |
| 455 | |
| 456 | /* |
| 457 | * Special vm_stop() variant for terminating the process. Historically clients |
| 458 | * did not expect a QMP STOP event and so we need to retain compatibility. |
| 459 | */ |
| 460 | int vm_shutdown(void) |
| 461 | { |
| 462 | return do_vm_stop(RUN_STATE_SHUTDOWN, false); |
| 463 | } |
| 464 | |
| 465 | |
| 466 | int vm_stop(RunState state) |
| 467 | { |
| 468 | if (qemu_in_vcpu_thread()) { |
| 469 | qemu_system_vmstop_request_prepare(); |
| 470 | qemu_system_vmstop_request(state); |
| 471 | /* |
| 472 | * FIXME: should not return to device code in case |
| 473 | * vm_stop() has been requested. |
| 474 | */ |
| 475 | cpu_stop_current(); |
| 476 | return 0; |
| 477 | } |
| 478 | |
| 479 | return do_vm_stop(state, true); |
| 480 | } |
| 481 | |
| 482 | /** |
| 483 | * Prepare for (re)starting the VM. |
| 484 | * Returns 0 if the vCPUs should be restarted, -1 on an error condition, |
| 485 | * and 1 otherwise. |
| 486 | */ |
| 487 | int vm_prepare_start(bool step_pending) |
| 488 | { |
| 489 | int ret = vm_was_suspended ? 1 : 0; |
| 490 | RunState state = vm_was_suspended ? RUN_STATE_SUSPENDED : RUN_STATE_RUNNING; |
| 491 | RunState requested; |
| 492 | |
| 493 | qemu_vmstop_requested(&requested); |
| 494 | if (runstate_is_running() && requested == RUN_STATE__MAX) { |
| 495 | return -1; |
| 496 | } |
| 497 | |
| 498 | /* |
| 499 | * Ensure that a STOP/RESUME pair of events is emitted if a |
| 500 | * vmstop request was pending. The BLOCK_IO_ERROR event, for |
| 501 | * example, according to documentation is always followed by |
| 502 | * the STOP event. |
| 503 | */ |
| 504 | if (runstate_is_running()) { |
| 505 | qapi_event_send_stop(); |
| 506 | qapi_event_send_resume(); |
| 507 | return -1; |
| 508 | } |
| 509 | |
| 510 | /* |
| 511 | * WHPX accelerator needs to know whether we are going to step |
| 512 | * any CPUs, before starting the first one. |
| 513 | */ |
| 514 | accel_pre_resume(MACHINE(qdev_get_machine()), step_pending); |
| 515 | |
| 516 | /* We are sending this now, but the CPUs will be resumed shortly later */ |
| 517 | qapi_event_send_resume(); |
| 518 | |
| 519 | cpu_enable_ticks(); |
| 520 | runstate_set(state); |
| 521 | vm_state_notify(1, state); |
| 522 | vm_was_suspended = false; |
| 523 | return ret; |
| 524 | } |
| 525 | |
| 526 | void vm_start(void) |
| 527 | { |
| 528 | if (!vm_prepare_start(false)) { |
| 529 | resume_all_vcpus(); |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | void vm_resume(RunState state) |
| 534 | { |
| 535 | if (runstate_is_live(state)) { |
| 536 | vm_start(); |
| 537 | } else { |
| 538 | runstate_set(state); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | /* |
| 543 | * does a state transition even if the VM is already stopped, |
| 544 | * current state is forgotten forever |
| 545 | */ |
| 546 | int vm_stop_force_state(RunState state) |
| 547 | { |
| 548 | if (runstate_is_live(runstate_get())) { |
| 549 | return vm_stop(state); |
| 550 | } else { |
| 551 | int ret; |
| 552 | runstate_set(state); |
| 553 | |
| 554 | bdrv_drain_all(); |
| 555 | /* |
| 556 | * Make sure to return an error if the flush in a previous vm_stop() |
| 557 | * failed. |
| 558 | */ |
| 559 | ret = bdrv_flush_all(); |
| 560 | trace_vm_stop_flush_all(ret); |
| 561 | return ret; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | static ShutdownCause reset_requested; |
| 566 | static ShutdownCause shutdown_requested; |
| 567 | static int shutdown_exit_code = EXIT_SUCCESS; |
| 568 | static int shutdown_signal; |
| 569 | static bool force_shutdown; |
| 570 | static pid_t shutdown_pid; |
| 571 | static int powerdown_requested; |
| 572 | static int debug_requested; |
| 573 | static int suspend_requested; |
| 574 | static WakeupReason wakeup_reason; |
| 575 | static NotifierList powerdown_notifiers = |
| 576 | NOTIFIER_LIST_INITIALIZER(powerdown_notifiers); |
| 577 | static NotifierList suspend_notifiers = |
| 578 | NOTIFIER_LIST_INITIALIZER(suspend_notifiers); |
| 579 | static NotifierList wakeup_notifiers = |
| 580 | NOTIFIER_LIST_INITIALIZER(wakeup_notifiers); |
| 581 | static NotifierList shutdown_notifiers = |
| 582 | NOTIFIER_LIST_INITIALIZER(shutdown_notifiers); |
| 583 | static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE); |
| 584 | |
| 585 | ShutdownCause qemu_shutdown_requested_get(void) |
| 586 | { |
| 587 | return shutdown_requested; |
| 588 | } |
| 589 | |
| 590 | bool qemu_force_shutdown_requested(void) |
| 591 | { |
| 592 | return force_shutdown; |
| 593 | } |
| 594 | |
| 595 | ShutdownCause qemu_reset_requested_get(void) |
| 596 | { |
| 597 | return reset_requested; |
| 598 | } |
| 599 | |
| 600 | static int qemu_shutdown_requested(void) |
| 601 | { |
| 602 | return qatomic_xchg(&shutdown_requested, SHUTDOWN_CAUSE_NONE); |
| 603 | } |
| 604 | |
| 605 | static void qemu_kill_report(void) |
| 606 | { |
| 607 | if (!qtest_driver() && shutdown_signal) { |
| 608 | if (shutdown_pid == 0) { |
| 609 | /* This happens for eg ^C at the terminal, so it's worth |
| 610 | * avoiding printing an odd message in that case. |
| 611 | */ |
| 612 | error_report("terminating on signal %d", shutdown_signal); |
| 613 | } else { |
| 614 | char *shutdown_cmd = qemu_get_pid_name(shutdown_pid); |
| 615 | |
| 616 | error_report("terminating on signal %d from pid " FMT_pid " (%s)", |
| 617 | shutdown_signal, shutdown_pid, |
| 618 | shutdown_cmd ? shutdown_cmd : "<unknown process>"); |
| 619 | g_free(shutdown_cmd); |
| 620 | } |
| 621 | shutdown_signal = 0; |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | static ShutdownCause qemu_reset_requested(void) |
| 626 | { |
| 627 | ShutdownCause r = reset_requested; |
| 628 | |
| 629 | if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) { |
| 630 | reset_requested = SHUTDOWN_CAUSE_NONE; |
| 631 | return r; |
| 632 | } |
| 633 | return SHUTDOWN_CAUSE_NONE; |
| 634 | } |
| 635 | |
| 636 | static int qemu_suspend_requested(void) |
| 637 | { |
| 638 | int r = suspend_requested; |
| 639 | if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) { |
| 640 | suspend_requested = 0; |
| 641 | return r; |
| 642 | } |
| 643 | return false; |
| 644 | } |
| 645 | |
| 646 | static WakeupReason qemu_wakeup_requested(void) |
| 647 | { |
| 648 | return wakeup_reason; |
| 649 | } |
| 650 | |
| 651 | static int qemu_powerdown_requested(void) |
| 652 | { |
| 653 | int r = powerdown_requested; |
| 654 | powerdown_requested = 0; |
| 655 | return r; |
| 656 | } |
| 657 | |
| 658 | static int qemu_debug_requested(void) |
| 659 | { |
| 660 | int r = debug_requested; |
| 661 | debug_requested = 0; |
| 662 | return r; |
| 663 | } |
| 664 | |
| 665 | /* |
| 666 | * Reset the VM. Issue an event unless @reason is SHUTDOWN_CAUSE_NONE. |
| 667 | */ |
| 668 | void qemu_system_reset(ShutdownCause reason) |
| 669 | { |
| 670 | MachineClass *mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL; |
| 671 | AccelClass *ac = ACCEL_GET_CLASS(current_accel()); |
| 672 | bool force_vmfd_change = |
| 673 | current_machine ? current_machine->new_accel_vmfd_on_reset : false; |
| 674 | bool guest_state_rebuilt = false; |
| 675 | int ret; |
| 676 | ResetType type; |
| 677 | |
| 678 | cpu_synchronize_all_states(); |
| 679 | |
| 680 | switch (reason) { |
| 681 | case SHUTDOWN_CAUSE_SNAPSHOT_LOAD: |
| 682 | type = RESET_TYPE_SNAPSHOT_LOAD; |
| 683 | break; |
| 684 | default: |
| 685 | type = RESET_TYPE_COLD; |
| 686 | } |
| 687 | |
| 688 | if ((reason == SHUTDOWN_CAUSE_GUEST_RESET || |
| 689 | reason == SHUTDOWN_CAUSE_HOST_QMP_SYSTEM_RESET) && |
| 690 | (force_vmfd_change || !cpus_are_resettable())) { |
| 691 | if (ac->rebuild_guest) { |
| 692 | ret = ac->rebuild_guest(current_machine); |
| 693 | if (ret < 0 && ret != -EOPNOTSUPP) { |
| 694 | error_report("unable to rebuild guest: %s(%d)", |
| 695 | strerror(-ret), ret); |
| 696 | vm_stop(RUN_STATE_INTERNAL_ERROR); |
| 697 | } else if (ret == -EOPNOTSUPP) { |
| 698 | error_report("accelerator does not support reset!"); |
| 699 | } else { |
| 700 | info_report("virtual machine state has been rebuilt with new " |
| 701 | "guest file handle."); |
| 702 | guest_state_rebuilt = true; |
| 703 | } |
| 704 | } else if (!cpus_are_resettable()) { |
| 705 | error_report("accelerator does not support reset!"); |
| 706 | } else { |
| 707 | error_report("accelerator does not support rebuilding guest state," |
| 708 | " proceeding with normal reset!"); |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | if (mc && mc->reset) { |
| 713 | mc->reset(current_machine, type); |
| 714 | } else { |
| 715 | qemu_devices_reset(type); |
| 716 | } |
| 717 | switch (reason) { |
| 718 | case SHUTDOWN_CAUSE_NONE: |
| 719 | case SHUTDOWN_CAUSE_SUBSYSTEM_RESET: |
| 720 | case SHUTDOWN_CAUSE_SNAPSHOT_LOAD: |
| 721 | break; |
| 722 | default: |
| 723 | qapi_event_send_reset(shutdown_caused_by_guest(reason), reason); |
| 724 | } |
| 725 | |
| 726 | /* |
| 727 | * Some boards use the machine reset callback to point CPUs to the firmware |
| 728 | * entry point. Assume that this is not the case for boards that support |
| 729 | * non-resettable CPUs (currently used only for confidential guests), in |
| 730 | * which case cpu_synchronize_all_post_init() is enough because |
| 731 | * it does _more_ than cpu_synchronize_all_post_reset(). |
| 732 | */ |
| 733 | if (cpus_are_resettable()) { |
| 734 | if (guest_state_rebuilt) { |
| 735 | /* |
| 736 | * If guest state has been rebuilt, then we |
| 737 | * need to sync full cpu state for non confidential guests post |
| 738 | * reset. |
| 739 | */ |
| 740 | cpu_synchronize_all_post_init(); |
| 741 | } else { |
| 742 | cpu_synchronize_all_post_reset(); |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | vm_set_suspended(false); |
| 747 | } |
| 748 | |
| 749 | /* |
| 750 | * Wake the VM after suspend. |
| 751 | */ |
| 752 | static void qemu_system_wakeup(void) |
| 753 | { |
| 754 | MachineClass *mc; |
| 755 | |
| 756 | mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL; |
| 757 | |
| 758 | if (mc && mc->wakeup) { |
| 759 | mc->wakeup(current_machine); |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | static char *tdx_parse_panic_message(char *message) |
| 764 | { |
| 765 | bool printable = false; |
| 766 | char *buf = NULL; |
| 767 | int len = 0, i; |
| 768 | |
| 769 | /* |
| 770 | * Although message is defined as a json string, we shouldn't |
| 771 | * unconditionally treat it as is because the guest generated it and |
| 772 | * it's not necessarily trustable. |
| 773 | */ |
| 774 | if (message) { |
| 775 | /* The caller guarantees the NULL-terminated string. */ |
| 776 | len = strlen(message); |
| 777 | |
| 778 | printable = len > 0; |
| 779 | for (i = 0; i < len; i++) { |
| 780 | if (!(0x20 <= message[i] && message[i] <= 0x7e)) { |
| 781 | printable = false; |
| 782 | break; |
| 783 | } |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | if (len == 0) { |
| 788 | buf = g_malloc(1); |
| 789 | buf[0] = '\0'; |
| 790 | } else { |
| 791 | if (!printable) { |
| 792 | /* 3 = length of "%02x " */ |
| 793 | buf = g_malloc(len * 3); |
| 794 | for (i = 0; i < len; i++) { |
| 795 | if (message[i] == '\0') { |
| 796 | break; |
| 797 | } else { |
| 798 | sprintf(buf + 3 * i, "%02x ", message[i]); |
| 799 | } |
| 800 | } |
| 801 | if (i > 0) { |
| 802 | /* replace the last ' '(space) to NULL */ |
| 803 | buf[i * 3 - 1] = '\0'; |
| 804 | } else { |
| 805 | buf[0] = '\0'; |
| 806 | } |
| 807 | } else { |
| 808 | buf = g_strdup(message); |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | return buf; |
| 813 | } |
| 814 | |
| 815 | void qemu_system_guest_panicked(GuestPanicInformation *info) |
| 816 | { |
| 817 | qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed"); |
| 818 | |
| 819 | if (current_cpu) { |
| 820 | current_cpu->crash_occurred = true; |
| 821 | } |
| 822 | /* |
| 823 | * TODO: Currently the available panic actions are: none, pause, and |
| 824 | * shutdown, but in principle debug and reset could be supported as well. |
| 825 | * Investigate any potential use cases for the unimplemented actions. |
| 826 | */ |
| 827 | if (panic_action == PANIC_ACTION_PAUSE |
| 828 | || (panic_action == PANIC_ACTION_SHUTDOWN && shutdown_action == SHUTDOWN_ACTION_PAUSE)) { |
| 829 | qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE, info); |
| 830 | vm_stop(RUN_STATE_GUEST_PANICKED); |
| 831 | } else if (panic_action == PANIC_ACTION_SHUTDOWN || |
| 832 | panic_action == PANIC_ACTION_EXIT_FAILURE) { |
| 833 | qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF, info); |
| 834 | vm_stop(RUN_STATE_GUEST_PANICKED); |
| 835 | qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_PANIC); |
| 836 | } else { |
| 837 | qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_RUN, info); |
| 838 | } |
| 839 | |
| 840 | if (info) { |
| 841 | if (info->type == GUEST_PANIC_INFORMATION_TYPE_HYPER_V) { |
| 842 | qemu_log_mask(LOG_GUEST_ERROR, "\nHV crash parameters: (%#"PRIx64 |
| 843 | " %#"PRIx64" %#"PRIx64" %#"PRIx64" %#"PRIx64")\n", |
| 844 | info->u.hyper_v.arg1, |
| 845 | info->u.hyper_v.arg2, |
| 846 | info->u.hyper_v.arg3, |
| 847 | info->u.hyper_v.arg4, |
| 848 | info->u.hyper_v.arg5); |
| 849 | } else if (info->type == GUEST_PANIC_INFORMATION_TYPE_S390) { |
| 850 | qemu_log_mask(LOG_GUEST_ERROR, " on cpu %d: %s\n" |
| 851 | "PSW: 0x%016" PRIx64 " 0x%016" PRIx64"\n", |
| 852 | info->u.s390.core, |
| 853 | S390CrashReason_str(info->u.s390.reason), |
| 854 | info->u.s390.psw_mask, |
| 855 | info->u.s390.psw_addr); |
| 856 | } else if (info->type == GUEST_PANIC_INFORMATION_TYPE_TDX) { |
| 857 | char *message = tdx_parse_panic_message(info->u.tdx.message); |
| 858 | qemu_log_mask(LOG_GUEST_ERROR, |
| 859 | "\nTDX guest reports fatal error." |
| 860 | " error code: 0x%" PRIx32 " error message:\"%s\"\n", |
| 861 | info->u.tdx.error_code, message); |
| 862 | g_free(message); |
| 863 | if (info->u.tdx.has_gpa) { |
| 864 | qemu_log_mask(LOG_GUEST_ERROR, "Additional error information " |
| 865 | "can be found at gpa page: 0x%" PRIx64 "\n", |
| 866 | info->u.tdx.gpa); |
| 867 | } |
| 868 | } else if (info->type == GUEST_PANIC_INFORMATION_TYPE_SEV) { |
| 869 | qemu_log_mask(LOG_GUEST_ERROR, "SEV termination (reason set: %d code: %d)", |
| 870 | info->u.sev.set, |
| 871 | info->u.sev.code); |
| 872 | } |
| 873 | |
| 874 | qapi_free_GuestPanicInformation(info); |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | void qemu_system_guest_crashloaded(GuestPanicInformation *info) |
| 879 | { |
| 880 | qemu_log_mask(LOG_GUEST_ERROR, "Guest crash loaded"); |
| 881 | qapi_event_send_guest_crashloaded(GUEST_PANIC_ACTION_RUN, info); |
| 882 | qapi_free_GuestPanicInformation(info); |
| 883 | } |
| 884 | |
| 885 | void qemu_system_guest_pvshutdown(void) |
| 886 | { |
| 887 | qapi_event_send_guest_pvshutdown(); |
| 888 | qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN); |
| 889 | } |
| 890 | |
| 891 | void qemu_system_reset_request(ShutdownCause reason) |
| 892 | { |
| 893 | if (reboot_action == REBOOT_ACTION_SHUTDOWN && |
| 894 | reason != SHUTDOWN_CAUSE_SUBSYSTEM_RESET) { |
| 895 | shutdown_requested = reason; |
| 896 | } else if (!cpus_are_resettable() && |
| 897 | !confidential_guest_can_rebuild_state(current_machine->cgs)) { |
| 898 | error_report("cpus are not resettable, terminating"); |
| 899 | shutdown_requested = reason; |
| 900 | } else { |
| 901 | reset_requested = reason; |
| 902 | } |
| 903 | cpu_stop_current(); |
| 904 | qemu_notify_event(); |
| 905 | } |
| 906 | |
| 907 | static void qemu_system_suspend(void) |
| 908 | { |
| 909 | pause_all_vcpus(); |
| 910 | notifier_list_notify(&suspend_notifiers, NULL); |
| 911 | runstate_set(RUN_STATE_SUSPENDED); |
| 912 | qapi_event_send_suspend(); |
| 913 | } |
| 914 | |
| 915 | void qemu_system_suspend_request(void) |
| 916 | { |
| 917 | if (runstate_check(RUN_STATE_SUSPENDED)) { |
| 918 | return; |
| 919 | } |
| 920 | suspend_requested = 1; |
| 921 | cpu_stop_current(); |
| 922 | qemu_notify_event(); |
| 923 | } |
| 924 | |
| 925 | void qemu_register_suspend_notifier(Notifier *notifier) |
| 926 | { |
| 927 | notifier_list_add(&suspend_notifiers, notifier); |
| 928 | } |
| 929 | |
| 930 | void qemu_system_wakeup_request(WakeupReason reason, Error **errp) |
| 931 | { |
| 932 | trace_system_wakeup_request(reason); |
| 933 | |
| 934 | if (!runstate_check(RUN_STATE_SUSPENDED)) { |
| 935 | error_setg(errp, |
| 936 | "Unable to wake up: guest is not in suspended state"); |
| 937 | return; |
| 938 | } |
| 939 | if (!(wakeup_reason_mask & (1 << reason))) { |
| 940 | return; |
| 941 | } |
| 942 | runstate_set(RUN_STATE_RUNNING); |
| 943 | wakeup_reason = reason; |
| 944 | qemu_notify_event(); |
| 945 | } |
| 946 | |
| 947 | void qemu_system_wakeup_enable(WakeupReason reason, bool enabled) |
| 948 | { |
| 949 | if (enabled) { |
| 950 | wakeup_reason_mask |= (1 << reason); |
| 951 | } else { |
| 952 | wakeup_reason_mask &= ~(1 << reason); |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | void qemu_register_wakeup_notifier(Notifier *notifier) |
| 957 | { |
| 958 | notifier_list_add(&wakeup_notifiers, notifier); |
| 959 | } |
| 960 | |
| 961 | static bool wakeup_suspend_enabled; |
| 962 | |
| 963 | void qemu_register_wakeup_support(void) |
| 964 | { |
| 965 | wakeup_suspend_enabled = true; |
| 966 | } |
| 967 | |
| 968 | bool qemu_wakeup_suspend_enabled(void) |
| 969 | { |
| 970 | return wakeup_suspend_enabled; |
| 971 | } |
| 972 | |
| 973 | void qemu_system_killed(int signal, pid_t pid) |
| 974 | { |
| 975 | shutdown_signal = signal; |
| 976 | shutdown_pid = pid; |
| 977 | shutdown_action = SHUTDOWN_ACTION_POWEROFF; |
| 978 | |
| 979 | /* Cannot call qemu_system_shutdown_request directly because |
| 980 | * we are in a signal handler. |
| 981 | */ |
| 982 | shutdown_requested = SHUTDOWN_CAUSE_HOST_SIGNAL; |
| 983 | force_shutdown = true; |
| 984 | qemu_notify_event(); |
| 985 | } |
| 986 | |
| 987 | void qemu_system_shutdown_request_with_code(ShutdownCause reason, |
| 988 | int exit_code) |
| 989 | { |
| 990 | shutdown_exit_code = exit_code; |
| 991 | qemu_system_shutdown_request(reason); |
| 992 | } |
| 993 | |
| 994 | void qemu_system_shutdown_request(ShutdownCause reason) |
| 995 | { |
| 996 | trace_qemu_system_shutdown_request(reason); |
| 997 | replay_shutdown_request(reason); |
| 998 | shutdown_requested = reason; |
| 999 | if (reason == SHUTDOWN_CAUSE_HOST_QMP_QUIT) { |
| 1000 | force_shutdown = true; |
| 1001 | } |
| 1002 | qemu_notify_event(); |
| 1003 | } |
| 1004 | |
| 1005 | static void qemu_system_powerdown(void) |
| 1006 | { |
| 1007 | qapi_event_send_powerdown(); |
| 1008 | notifier_list_notify(&powerdown_notifiers, NULL); |
| 1009 | } |
| 1010 | |
| 1011 | static void qemu_system_shutdown(ShutdownCause cause) |
| 1012 | { |
| 1013 | qapi_event_send_shutdown(shutdown_caused_by_guest(cause), cause); |
| 1014 | notifier_list_notify(&shutdown_notifiers, &cause); |
| 1015 | } |
| 1016 | |
| 1017 | void qemu_system_powerdown_request(void) |
| 1018 | { |
| 1019 | trace_qemu_system_powerdown_request(); |
| 1020 | powerdown_requested = 1; |
| 1021 | qemu_notify_event(); |
| 1022 | } |
| 1023 | |
| 1024 | void qemu_register_powerdown_notifier(Notifier *notifier) |
| 1025 | { |
| 1026 | notifier_list_add(&powerdown_notifiers, notifier); |
| 1027 | } |
| 1028 | |
| 1029 | void qemu_register_shutdown_notifier(Notifier *notifier) |
| 1030 | { |
| 1031 | notifier_list_add(&shutdown_notifiers, notifier); |
| 1032 | } |
| 1033 | |
| 1034 | void qemu_system_debug_request(void) |
| 1035 | { |
| 1036 | debug_requested = 1; |
| 1037 | qemu_notify_event(); |
| 1038 | } |
| 1039 | |
| 1040 | static bool main_loop_should_exit(int *status) |
| 1041 | { |
| 1042 | RunState r; |
| 1043 | ShutdownCause request; |
| 1044 | |
| 1045 | if (qemu_debug_requested()) { |
| 1046 | vm_stop(RUN_STATE_DEBUG); |
| 1047 | } |
| 1048 | if (qemu_suspend_requested()) { |
| 1049 | qemu_system_suspend(); |
| 1050 | } |
| 1051 | request = qemu_shutdown_requested(); |
| 1052 | if (request) { |
| 1053 | qemu_kill_report(); |
| 1054 | qemu_system_shutdown(request); |
| 1055 | if (shutdown_action == SHUTDOWN_ACTION_PAUSE) { |
| 1056 | vm_stop(RUN_STATE_SHUTDOWN); |
| 1057 | } else { |
| 1058 | if (shutdown_exit_code != EXIT_SUCCESS) { |
| 1059 | *status = shutdown_exit_code; |
| 1060 | } else if (request == SHUTDOWN_CAUSE_GUEST_PANIC && |
| 1061 | panic_action == PANIC_ACTION_EXIT_FAILURE) { |
| 1062 | *status = EXIT_FAILURE; |
| 1063 | } |
| 1064 | return true; |
| 1065 | } |
| 1066 | } |
| 1067 | request = qemu_reset_requested(); |
| 1068 | if (request) { |
| 1069 | pause_all_vcpus(); |
| 1070 | qemu_system_reset(request); |
| 1071 | resume_all_vcpus(); |
| 1072 | /* |
| 1073 | * runstate can change in pause_all_vcpus() |
| 1074 | * as iothread mutex is unlocked |
| 1075 | */ |
| 1076 | if (!runstate_check(RUN_STATE_RUNNING) && |
| 1077 | !runstate_check(RUN_STATE_INMIGRATE) && |
| 1078 | !runstate_check(RUN_STATE_FINISH_MIGRATE)) { |
| 1079 | runstate_set(RUN_STATE_PRELAUNCH); |
| 1080 | } |
| 1081 | } |
| 1082 | if (qemu_wakeup_requested()) { |
| 1083 | pause_all_vcpus(); |
| 1084 | qemu_system_wakeup(); |
| 1085 | notifier_list_notify(&wakeup_notifiers, &wakeup_reason); |
| 1086 | wakeup_reason = QEMU_WAKEUP_REASON_NONE; |
| 1087 | resume_all_vcpus(); |
| 1088 | qapi_event_send_wakeup(); |
| 1089 | } |
| 1090 | if (qemu_powerdown_requested()) { |
| 1091 | qemu_system_powerdown(); |
| 1092 | } |
| 1093 | if (qemu_vmstop_requested(&r)) { |
| 1094 | vm_stop(r); |
| 1095 | } |
| 1096 | return false; |
| 1097 | } |
| 1098 | |
| 1099 | int qemu_main_loop(void) |
| 1100 | { |
| 1101 | int status = EXIT_SUCCESS; |
| 1102 | |
| 1103 | while (!main_loop_should_exit(&status)) { |
| 1104 | main_loop_wait(false); |
| 1105 | } |
| 1106 | |
| 1107 | return status; |
| 1108 | } |
| 1109 | |
| 1110 | void qemu_add_exit_notifier(Notifier *notify) |
| 1111 | { |
| 1112 | notifier_list_add(&exit_notifiers, notify); |
| 1113 | } |
| 1114 | |
| 1115 | void qemu_remove_exit_notifier(Notifier *notify) |
| 1116 | { |
| 1117 | notifier_remove(notify); |
| 1118 | } |
| 1119 | |
| 1120 | static void qemu_run_exit_notifiers(void) |
| 1121 | { |
| 1122 | BQL_LOCK_GUARD(); |
| 1123 | notifier_list_notify(&exit_notifiers, NULL); |
| 1124 | } |
| 1125 | |
| 1126 | void qemu_init_subsystems(void) |
| 1127 | { |
| 1128 | Error *err = NULL; |
| 1129 | |
| 1130 | os_set_line_buffering(); |
| 1131 | |
| 1132 | module_call_init(MODULE_INIT_TRACE); |
| 1133 | |
| 1134 | qemu_init_cpu_list(); |
| 1135 | qemu_init_cpu_loop(); |
| 1136 | bql_lock(); |
| 1137 | |
| 1138 | atexit(qemu_run_exit_notifiers); |
| 1139 | |
| 1140 | module_call_init(MODULE_INIT_QOM); |
| 1141 | module_call_init(MODULE_INIT_MIGRATION); |
| 1142 | |
| 1143 | runstate_init(); |
| 1144 | precopy_infrastructure_init(); |
| 1145 | postcopy_infrastructure_init(); |
| 1146 | monitor_init_globals(); |
| 1147 | |
| 1148 | if (qcrypto_init(&err) < 0) { |
| 1149 | error_reportf_err(err, "cannot initialize crypto: "); |
| 1150 | exit(1); |
| 1151 | } |
| 1152 | |
| 1153 | os_setup_early_signal_handling(); |
| 1154 | |
| 1155 | bdrv_init_with_whitelist(); |
| 1156 | socket_init(); |
| 1157 | } |
| 1158 | |
| 1159 | |
| 1160 | void qemu_cleanup(int status) |
| 1161 | { |
| 1162 | gdb_exit(status); |
| 1163 | |
| 1164 | /* |
| 1165 | * cleaning up the migration object cancels any existing migration |
| 1166 | * try to do this early so that it also stops using devices. |
| 1167 | */ |
| 1168 | migration_shutdown(); |
| 1169 | |
| 1170 | /* |
| 1171 | * Close the exports before draining the block layer. The export |
| 1172 | * drivers may have coroutines yielding on it, so we need to clean |
| 1173 | * them up before the drain, as otherwise they may be get stuck in |
| 1174 | * blk_wait_while_drained(). |
| 1175 | */ |
| 1176 | blk_exp_close_all(); |
| 1177 | |
| 1178 | |
| 1179 | /* No more vcpu or device emulation activity beyond this point */ |
| 1180 | vm_shutdown(); |
| 1181 | replay_finish(); |
| 1182 | |
| 1183 | /* |
| 1184 | * We must cancel all block jobs while the block layer is drained, |
| 1185 | * or cancelling will be affected by throttling and thus may block |
| 1186 | * for an extended period of time. |
| 1187 | * Begin the drained section after vm_shutdown() to avoid requests being |
| 1188 | * stuck in the BlockBackend's request queue. |
| 1189 | * We do not need to end this section, because we do not want any |
| 1190 | * requests happening from here on anyway. |
| 1191 | */ |
| 1192 | bdrv_drain_all_begin(); |
| 1193 | job_cancel_sync_all(); |
| 1194 | bdrv_close_all(); |
| 1195 | |
| 1196 | /* vhost-user must be cleaned up before chardevs. */ |
| 1197 | tpm_cleanup(); |
| 1198 | net_cleanup(); |
| 1199 | audio_cleanup(); |
| 1200 | monitor_cleanup(); |
| 1201 | qemu_chr_cleanup(); |
| 1202 | qemu_display_cleanup(); |
| 1203 | user_creatable_cleanup(); |
| 1204 | /* TODO: unref root container, check all devices are ok */ |
| 1205 | } |