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 "audio/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/boards.h" 35 #include "hw/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 "qemu/error-report.h" 46 #include "qemu/job.h" 47 #include "qemu/log.h" 48 #include "qemu/module.h" 49 #include "qemu/sockets.h" 50 #include "qemu/timer.h" 51 #include "qemu/thread.h" 52 #include "qom/object.h" 53 #include "qom/object_interfaces.h" 54 #include "system/cpus.h" 55 #include "system/qtest.h" 56 #include "system/replay.h" 57 #include "system/reset.h" 58 #include "system/runstate.h" 59 #include "system/runstate-action.h" 60 #include "system/system.h" 61 #include "system/tpm.h" 62 #include "trace.h" 63 64 static NotifierList exit_notifiers = 65 NOTIFIER_LIST_INITIALIZER(exit_notifiers); 66 67 static RunState current_run_state = RUN_STATE_PRELAUNCH; 68 69 /* We use RUN_STATE__MAX but any invalid value will do */ 70 static RunState vmstop_requested = RUN_STATE__MAX; 71 static QemuMutex vmstop_lock; 72 73 typedef struct { 74 RunState from; 75 RunState to; 76 } RunStateTransition; 77 78 static const RunStateTransition runstate_transitions_def[] = { 79 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE }, 80 { RUN_STATE_PRELAUNCH, RUN_STATE_SUSPENDED }, 81 82 { RUN_STATE_DEBUG, RUN_STATE_RUNNING }, 83 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE }, 84 { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH }, 85 86 { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR }, 87 { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR }, 88 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED }, 89 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING }, 90 { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN }, 91 { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED }, 92 { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG }, 93 { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED }, 94 { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE }, 95 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH }, 96 { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE }, 97 { RUN_STATE_INMIGRATE, RUN_STATE_COLO }, 98 99 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED }, 100 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE }, 101 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH }, 102 103 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING }, 104 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE }, 105 { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH }, 106 107 { RUN_STATE_PAUSED, RUN_STATE_RUNNING }, 108 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE }, 109 { RUN_STATE_PAUSED, RUN_STATE_POSTMIGRATE }, 110 { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH }, 111 { RUN_STATE_PAUSED, RUN_STATE_COLO}, 112 { RUN_STATE_PAUSED, RUN_STATE_SUSPENDED}, 113 114 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING }, 115 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE }, 116 { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH }, 117 118 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING }, 119 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE }, 120 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE }, 121 122 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING }, 123 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PAUSED }, 124 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE }, 125 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH }, 126 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_COLO }, 127 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_INTERNAL_ERROR }, 128 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_IO_ERROR }, 129 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_SHUTDOWN }, 130 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_SUSPENDED }, 131 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_WATCHDOG }, 132 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_GUEST_PANICKED }, 133 134 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING }, 135 { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH }, 136 { RUN_STATE_RESTORE_VM, RUN_STATE_SUSPENDED }, 137 138 { RUN_STATE_COLO, RUN_STATE_RUNNING }, 139 { RUN_STATE_COLO, RUN_STATE_PRELAUNCH }, 140 { RUN_STATE_COLO, RUN_STATE_SHUTDOWN}, 141 142 { RUN_STATE_RUNNING, RUN_STATE_DEBUG }, 143 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR }, 144 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR }, 145 { RUN_STATE_RUNNING, RUN_STATE_PAUSED }, 146 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE }, 147 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM }, 148 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM }, 149 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN }, 150 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG }, 151 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED }, 152 { RUN_STATE_RUNNING, RUN_STATE_COLO}, 153 154 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING }, 155 { RUN_STATE_SAVE_VM, RUN_STATE_SUSPENDED }, 156 157 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED }, 158 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE }, 159 { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH }, 160 { RUN_STATE_SHUTDOWN, RUN_STATE_COLO }, 161 162 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED }, 163 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED }, 164 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING }, 165 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE }, 166 { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH }, 167 { RUN_STATE_SUSPENDED, RUN_STATE_COLO}, 168 { RUN_STATE_SUSPENDED, RUN_STATE_PAUSED}, 169 { RUN_STATE_SUSPENDED, RUN_STATE_SAVE_VM }, 170 { RUN_STATE_SUSPENDED, RUN_STATE_RESTORE_VM }, 171 { RUN_STATE_SUSPENDED, RUN_STATE_SHUTDOWN }, 172 173 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING }, 174 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE }, 175 { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH }, 176 { RUN_STATE_WATCHDOG, RUN_STATE_COLO}, 177 178 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING }, 179 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE }, 180 { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH }, 181 182 { RUN_STATE__MAX, RUN_STATE__MAX }, 183 }; 184 185 static const RunStateTransition replay_play_runstate_transitions_def[] = { 186 { RUN_STATE_SHUTDOWN, RUN_STATE_RUNNING}, 187 188 { RUN_STATE__MAX, RUN_STATE__MAX }, 189 }; 190 191 static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX]; 192 193 bool runstate_check(RunState state) 194 { 195 return current_run_state == state; 196 } 197 198 static void transitions_set_valid(const RunStateTransition *rst) 199 { 200 const RunStateTransition *p; 201 202 for (p = rst; p->from != RUN_STATE__MAX; p++) { 203 runstate_valid_transitions[p->from][p->to] = true; 204 } 205 } 206 207 void runstate_replay_enable(void) 208 { 209 assert(replay_mode != REPLAY_MODE_NONE); 210 211 if (replay_mode == REPLAY_MODE_PLAY) { 212 /* 213 * When reverse-debugging, it is possible to move state from 214 * shutdown to running. 215 */ 216 transitions_set_valid(&replay_play_runstate_transitions_def[0]); 217 } 218 } 219 220 static void runstate_init(void) 221 { 222 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions)); 223 224 transitions_set_valid(&runstate_transitions_def[0]); 225 226 qemu_mutex_init(&vmstop_lock); 227 } 228 229 /* This function will abort() on invalid state transitions */ 230 void runstate_set(RunState new_state) 231 { 232 assert(new_state < RUN_STATE__MAX); 233 234 trace_runstate_set(current_run_state, RunState_str(current_run_state), 235 new_state, RunState_str(new_state)); 236 237 if (current_run_state == new_state) { 238 return; 239 } 240 241 if (!runstate_valid_transitions[current_run_state][new_state]) { 242 error_report("invalid runstate transition: '%s' -> '%s'", 243 RunState_str(current_run_state), 244 RunState_str(new_state)); 245 abort(); 246 } 247 248 current_run_state = new_state; 249 } 250 251 RunState runstate_get(void) 252 { 253 return current_run_state; 254 } 255 256 bool runstate_is_running(void) 257 { 258 return runstate_check(RUN_STATE_RUNNING); 259 } 260 261 bool runstate_needs_reset(void) 262 { 263 return runstate_check(RUN_STATE_INTERNAL_ERROR) || 264 runstate_check(RUN_STATE_SHUTDOWN); 265 } 266 267 StatusInfo *qmp_query_status(Error **errp) 268 { 269 StatusInfo *info = g_malloc0(sizeof(*info)); 270 271 info->running = runstate_is_running(); 272 info->status = current_run_state; 273 274 return info; 275 } 276 277 bool qemu_vmstop_requested(RunState *r) 278 { 279 qemu_mutex_lock(&vmstop_lock); 280 *r = vmstop_requested; 281 vmstop_requested = RUN_STATE__MAX; 282 qemu_mutex_unlock(&vmstop_lock); 283 return *r < RUN_STATE__MAX; 284 } 285 286 void qemu_system_vmstop_request_prepare(void) 287 { 288 qemu_mutex_lock(&vmstop_lock); 289 } 290 291 void qemu_system_vmstop_request(RunState state) 292 { 293 vmstop_requested = state; 294 qemu_mutex_unlock(&vmstop_lock); 295 qemu_notify_event(); 296 } 297 struct VMChangeStateEntry { 298 VMChangeStateHandler *cb; 299 VMChangeStateHandler *prepare_cb; 300 VMChangeStateHandlerWithRet *cb_ret; 301 void *opaque; 302 QTAILQ_ENTRY(VMChangeStateEntry) entries; 303 int priority; 304 }; 305 306 static QTAILQ_HEAD(, VMChangeStateEntry) vm_change_state_head = 307 QTAILQ_HEAD_INITIALIZER(vm_change_state_head); 308 309 /** 310 * qemu_add_vm_change_state_handler_prio: 311 * @cb: the callback to invoke 312 * @opaque: user data passed to the callback 313 * @priority: low priorities execute first when the vm runs and the reverse is 314 * true when the vm stops 315 * 316 * Register a callback function that is invoked when the vm starts or stops 317 * running. 318 * 319 * Returns: an entry to be freed using qemu_del_vm_change_state_handler() 320 */ 321 VMChangeStateEntry *qemu_add_vm_change_state_handler_prio( 322 VMChangeStateHandler *cb, void *opaque, int priority) 323 { 324 return qemu_add_vm_change_state_handler_prio_full(cb, NULL, NULL, 325 opaque, priority); 326 } 327 328 /** 329 * qemu_add_vm_change_state_handler_prio_full: 330 * @cb: the main callback to invoke 331 * @prepare_cb: a callback to invoke before the main callback 332 * @cb_ret: the main callback to invoke with return value 333 * @opaque: user data passed to the callbacks 334 * @priority: low priorities execute first when the vm runs and the reverse is 335 * true when the vm stops 336 * 337 * Register a main callback function and an optional prepare callback function 338 * that are invoked when the vm starts or stops running. The main callback and 339 * the prepare callback are called in two separate phases: First all prepare 340 * callbacks are called and only then all main callbacks are called. As its 341 * name suggests, the prepare callback can be used to do some preparatory work 342 * before invoking the main callback. 343 * 344 * Returns: an entry to be freed using qemu_del_vm_change_state_handler() 345 */ 346 VMChangeStateEntry * 347 qemu_add_vm_change_state_handler_prio_full(VMChangeStateHandler *cb, 348 VMChangeStateHandler *prepare_cb, 349 VMChangeStateHandlerWithRet *cb_ret, 350 void *opaque, int priority) 351 { 352 VMChangeStateEntry *e; 353 VMChangeStateEntry *other; 354 355 e = g_malloc0(sizeof(*e)); 356 e->cb = cb; 357 e->prepare_cb = prepare_cb; 358 e->cb_ret = cb_ret; 359 e->opaque = opaque; 360 e->priority = priority; 361 362 /* Keep list sorted in ascending priority order */ 363 QTAILQ_FOREACH(other, &vm_change_state_head, entries) { 364 if (priority < other->priority) { 365 QTAILQ_INSERT_BEFORE(other, e, entries); 366 return e; 367 } 368 } 369 370 QTAILQ_INSERT_TAIL(&vm_change_state_head, e, entries); 371 return e; 372 } 373 374 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb, 375 void *opaque) 376 { 377 return qemu_add_vm_change_state_handler_prio(cb, opaque, 0); 378 } 379 380 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e) 381 { 382 QTAILQ_REMOVE(&vm_change_state_head, e, entries); 383 g_free(e); 384 } 385 386 int vm_state_notify(bool running, RunState state) 387 { 388 VMChangeStateEntry *e, *next; 389 int ret = 0; 390 391 trace_vm_state_notify(running, state, RunState_str(state)); 392 393 if (running) { 394 QTAILQ_FOREACH_SAFE(e, &vm_change_state_head, entries, next) { 395 if (e->prepare_cb) { 396 e->prepare_cb(e->opaque, running, state); 397 } 398 } 399 400 QTAILQ_FOREACH_SAFE(e, &vm_change_state_head, entries, next) { 401 if (e->cb) { 402 e->cb(e->opaque, running, state); 403 } else if (e->cb_ret) { 404 /* 405 * Here ignore the return value of cb_ret because 406 * we only care about the stopping the device during 407 * the VM live migration to indicate whether the 408 * connection between qemu and backend is normal. 409 */ 410 e->cb_ret(e->opaque, running, state); 411 } 412 } 413 } else { 414 QTAILQ_FOREACH_REVERSE_SAFE(e, &vm_change_state_head, entries, next) { 415 if (e->prepare_cb) { 416 e->prepare_cb(e->opaque, running, state); 417 } 418 } 419 420 QTAILQ_FOREACH_REVERSE_SAFE(e, &vm_change_state_head, entries, next) { 421 if (e->cb) { 422 e->cb(e->opaque, running, state); 423 } else if (e->cb_ret) { 424 /* 425 * We should execute all registered callbacks even if 426 * one of them returns failure, otherwise, some cleanup 427 * work of the device will be skipped. 428 */ 429 ret |= e->cb_ret(e->opaque, running, state); 430 } 431 } 432 } 433 return ret; 434 } 435 436 static ShutdownCause reset_requested; 437 static ShutdownCause shutdown_requested; 438 static int shutdown_exit_code = EXIT_SUCCESS; 439 static int shutdown_signal; 440 static bool force_shutdown; 441 static pid_t shutdown_pid; 442 static int powerdown_requested; 443 static int debug_requested; 444 static int suspend_requested; 445 static WakeupReason wakeup_reason; 446 static NotifierList powerdown_notifiers = 447 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers); 448 static NotifierList suspend_notifiers = 449 NOTIFIER_LIST_INITIALIZER(suspend_notifiers); 450 static NotifierList wakeup_notifiers = 451 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers); 452 static NotifierList shutdown_notifiers = 453 NOTIFIER_LIST_INITIALIZER(shutdown_notifiers); 454 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE); 455 456 ShutdownCause qemu_shutdown_requested_get(void) 457 { 458 return shutdown_requested; 459 } 460 461 bool qemu_force_shutdown_requested(void) 462 { 463 return force_shutdown; 464 } 465 466 ShutdownCause qemu_reset_requested_get(void) 467 { 468 return reset_requested; 469 } 470 471 static int qemu_shutdown_requested(void) 472 { 473 return qatomic_xchg(&shutdown_requested, SHUTDOWN_CAUSE_NONE); 474 } 475 476 static void qemu_kill_report(void) 477 { 478 if (!qtest_driver() && shutdown_signal) { 479 if (shutdown_pid == 0) { 480 /* This happens for eg ^C at the terminal, so it's worth 481 * avoiding printing an odd message in that case. 482 */ 483 error_report("terminating on signal %d", shutdown_signal); 484 } else { 485 char *shutdown_cmd = qemu_get_pid_name(shutdown_pid); 486 487 error_report("terminating on signal %d from pid " FMT_pid " (%s)", 488 shutdown_signal, shutdown_pid, 489 shutdown_cmd ? shutdown_cmd : "<unknown process>"); 490 g_free(shutdown_cmd); 491 } 492 shutdown_signal = 0; 493 } 494 } 495 496 static ShutdownCause qemu_reset_requested(void) 497 { 498 ShutdownCause r = reset_requested; 499 500 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) { 501 reset_requested = SHUTDOWN_CAUSE_NONE; 502 return r; 503 } 504 return SHUTDOWN_CAUSE_NONE; 505 } 506 507 static int qemu_suspend_requested(void) 508 { 509 int r = suspend_requested; 510 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) { 511 suspend_requested = 0; 512 return r; 513 } 514 return false; 515 } 516 517 static WakeupReason qemu_wakeup_requested(void) 518 { 519 return wakeup_reason; 520 } 521 522 static int qemu_powerdown_requested(void) 523 { 524 int r = powerdown_requested; 525 powerdown_requested = 0; 526 return r; 527 } 528 529 static int qemu_debug_requested(void) 530 { 531 int r = debug_requested; 532 debug_requested = 0; 533 return r; 534 } 535 536 /* 537 * Reset the VM. Issue an event unless @reason is SHUTDOWN_CAUSE_NONE. 538 */ 539 void qemu_system_reset(ShutdownCause reason) 540 { 541 MachineClass *mc; 542 ResetType type; 543 544 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL; 545 546 cpu_synchronize_all_states(); 547 548 switch (reason) { 549 case SHUTDOWN_CAUSE_SNAPSHOT_LOAD: 550 type = RESET_TYPE_SNAPSHOT_LOAD; 551 break; 552 default: 553 type = RESET_TYPE_COLD; 554 } 555 if (mc && mc->reset) { 556 mc->reset(current_machine, type); 557 } else { 558 qemu_devices_reset(type); 559 } 560 switch (reason) { 561 case SHUTDOWN_CAUSE_NONE: 562 case SHUTDOWN_CAUSE_SUBSYSTEM_RESET: 563 case SHUTDOWN_CAUSE_SNAPSHOT_LOAD: 564 break; 565 default: 566 qapi_event_send_reset(shutdown_caused_by_guest(reason), reason); 567 } 568 569 /* 570 * Some boards use the machine reset callback to point CPUs to the firmware 571 * entry point. Assume that this is not the case for boards that support 572 * non-resettable CPUs (currently used only for confidential guests), in 573 * which case cpu_synchronize_all_post_init() is enough because 574 * it does _more_ than cpu_synchronize_all_post_reset(). 575 */ 576 if (cpus_are_resettable()) { 577 cpu_synchronize_all_post_reset(); 578 } else { 579 assert(runstate_check(RUN_STATE_PRELAUNCH)); 580 } 581 582 vm_set_suspended(false); 583 } 584 585 /* 586 * Wake the VM after suspend. 587 */ 588 static void qemu_system_wakeup(void) 589 { 590 MachineClass *mc; 591 592 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL; 593 594 if (mc && mc->wakeup) { 595 mc->wakeup(current_machine); 596 } 597 } 598 599 static char *tdx_parse_panic_message(char *message) 600 { 601 bool printable = false; 602 char *buf = NULL; 603 int len = 0, i; 604 605 /* 606 * Although message is defined as a json string, we shouldn't 607 * unconditionally treat it as is because the guest generated it and 608 * it's not necessarily trustable. 609 */ 610 if (message) { 611 /* The caller guarantees the NULL-terminated string. */ 612 len = strlen(message); 613 614 printable = len > 0; 615 for (i = 0; i < len; i++) { 616 if (!(0x20 <= message[i] && message[i] <= 0x7e)) { 617 printable = false; 618 break; 619 } 620 } 621 } 622 623 if (len == 0) { 624 buf = g_malloc(1); 625 buf[0] = '\0'; 626 } else { 627 if (!printable) { 628 /* 3 = length of "%02x " */ 629 buf = g_malloc(len * 3); 630 for (i = 0; i < len; i++) { 631 if (message[i] == '\0') { 632 break; 633 } else { 634 sprintf(buf + 3 * i, "%02x ", message[i]); 635 } 636 } 637 if (i > 0) { 638 /* replace the last ' '(space) to NULL */ 639 buf[i * 3 - 1] = '\0'; 640 } else { 641 buf[0] = '\0'; 642 } 643 } else { 644 buf = g_strdup(message); 645 } 646 } 647 648 return buf; 649 } 650 651 void qemu_system_guest_panicked(GuestPanicInformation *info) 652 { 653 qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed"); 654 655 if (current_cpu) { 656 current_cpu->crash_occurred = true; 657 } 658 /* 659 * TODO: Currently the available panic actions are: none, pause, and 660 * shutdown, but in principle debug and reset could be supported as well. 661 * Investigate any potential use cases for the unimplemented actions. 662 */ 663 if (panic_action == PANIC_ACTION_PAUSE 664 || (panic_action == PANIC_ACTION_SHUTDOWN && shutdown_action == SHUTDOWN_ACTION_PAUSE)) { 665 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE, info); 666 vm_stop(RUN_STATE_GUEST_PANICKED); 667 } else if (panic_action == PANIC_ACTION_SHUTDOWN || 668 panic_action == PANIC_ACTION_EXIT_FAILURE) { 669 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF, info); 670 vm_stop(RUN_STATE_GUEST_PANICKED); 671 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_PANIC); 672 } else { 673 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_RUN, info); 674 } 675 676 if (info) { 677 if (info->type == GUEST_PANIC_INFORMATION_TYPE_HYPER_V) { 678 qemu_log_mask(LOG_GUEST_ERROR, "\nHV crash parameters: (%#"PRIx64 679 " %#"PRIx64" %#"PRIx64" %#"PRIx64" %#"PRIx64")\n", 680 info->u.hyper_v.arg1, 681 info->u.hyper_v.arg2, 682 info->u.hyper_v.arg3, 683 info->u.hyper_v.arg4, 684 info->u.hyper_v.arg5); 685 } else if (info->type == GUEST_PANIC_INFORMATION_TYPE_S390) { 686 qemu_log_mask(LOG_GUEST_ERROR, " on cpu %d: %s\n" 687 "PSW: 0x%016" PRIx64 " 0x%016" PRIx64"\n", 688 info->u.s390.core, 689 S390CrashReason_str(info->u.s390.reason), 690 info->u.s390.psw_mask, 691 info->u.s390.psw_addr); 692 } else if (info->type == GUEST_PANIC_INFORMATION_TYPE_TDX) { 693 char *message = tdx_parse_panic_message(info->u.tdx.message); 694 qemu_log_mask(LOG_GUEST_ERROR, 695 "\nTDX guest reports fatal error." 696 " error code: 0x%" PRIx32 " error message:\"%s\"\n", 697 info->u.tdx.error_code, message); 698 g_free(message); 699 if (info->u.tdx.gpa != -1ull) { 700 qemu_log_mask(LOG_GUEST_ERROR, "Additional error information " 701 "can be found at gpa page: 0x%" PRIx64 "\n", 702 info->u.tdx.gpa); 703 } 704 } 705 706 qapi_free_GuestPanicInformation(info); 707 } 708 } 709 710 void qemu_system_guest_crashloaded(GuestPanicInformation *info) 711 { 712 qemu_log_mask(LOG_GUEST_ERROR, "Guest crash loaded"); 713 qapi_event_send_guest_crashloaded(GUEST_PANIC_ACTION_RUN, info); 714 qapi_free_GuestPanicInformation(info); 715 } 716 717 void qemu_system_guest_pvshutdown(void) 718 { 719 qapi_event_send_guest_pvshutdown(); 720 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN); 721 } 722 723 void qemu_system_reset_request(ShutdownCause reason) 724 { 725 if (reboot_action == REBOOT_ACTION_SHUTDOWN && 726 reason != SHUTDOWN_CAUSE_SUBSYSTEM_RESET) { 727 shutdown_requested = reason; 728 } else if (!cpus_are_resettable()) { 729 error_report("cpus are not resettable, terminating"); 730 shutdown_requested = reason; 731 } else { 732 reset_requested = reason; 733 } 734 cpu_stop_current(); 735 qemu_notify_event(); 736 } 737 738 static void qemu_system_suspend(void) 739 { 740 pause_all_vcpus(); 741 notifier_list_notify(&suspend_notifiers, NULL); 742 runstate_set(RUN_STATE_SUSPENDED); 743 qapi_event_send_suspend(); 744 } 745 746 void qemu_system_suspend_request(void) 747 { 748 if (runstate_check(RUN_STATE_SUSPENDED)) { 749 return; 750 } 751 suspend_requested = 1; 752 cpu_stop_current(); 753 qemu_notify_event(); 754 } 755 756 void qemu_register_suspend_notifier(Notifier *notifier) 757 { 758 notifier_list_add(&suspend_notifiers, notifier); 759 } 760 761 void qemu_system_wakeup_request(WakeupReason reason, Error **errp) 762 { 763 trace_system_wakeup_request(reason); 764 765 if (!runstate_check(RUN_STATE_SUSPENDED)) { 766 error_setg(errp, 767 "Unable to wake up: guest is not in suspended state"); 768 return; 769 } 770 if (!(wakeup_reason_mask & (1 << reason))) { 771 return; 772 } 773 runstate_set(RUN_STATE_RUNNING); 774 wakeup_reason = reason; 775 qemu_notify_event(); 776 } 777 778 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled) 779 { 780 if (enabled) { 781 wakeup_reason_mask |= (1 << reason); 782 } else { 783 wakeup_reason_mask &= ~(1 << reason); 784 } 785 } 786 787 void qemu_register_wakeup_notifier(Notifier *notifier) 788 { 789 notifier_list_add(&wakeup_notifiers, notifier); 790 } 791 792 static bool wakeup_suspend_enabled; 793 794 void qemu_register_wakeup_support(void) 795 { 796 wakeup_suspend_enabled = true; 797 } 798 799 bool qemu_wakeup_suspend_enabled(void) 800 { 801 return wakeup_suspend_enabled; 802 } 803 804 void qemu_system_killed(int signal, pid_t pid) 805 { 806 shutdown_signal = signal; 807 shutdown_pid = pid; 808 shutdown_action = SHUTDOWN_ACTION_POWEROFF; 809 810 /* Cannot call qemu_system_shutdown_request directly because 811 * we are in a signal handler. 812 */ 813 shutdown_requested = SHUTDOWN_CAUSE_HOST_SIGNAL; 814 force_shutdown = true; 815 qemu_notify_event(); 816 } 817 818 void qemu_system_shutdown_request_with_code(ShutdownCause reason, 819 int exit_code) 820 { 821 shutdown_exit_code = exit_code; 822 qemu_system_shutdown_request(reason); 823 } 824 825 void qemu_system_shutdown_request(ShutdownCause reason) 826 { 827 trace_qemu_system_shutdown_request(reason); 828 replay_shutdown_request(reason); 829 shutdown_requested = reason; 830 if (reason == SHUTDOWN_CAUSE_HOST_QMP_QUIT) { 831 force_shutdown = true; 832 } 833 qemu_notify_event(); 834 } 835 836 static void qemu_system_powerdown(void) 837 { 838 qapi_event_send_powerdown(); 839 notifier_list_notify(&powerdown_notifiers, NULL); 840 } 841 842 static void qemu_system_shutdown(ShutdownCause cause) 843 { 844 qapi_event_send_shutdown(shutdown_caused_by_guest(cause), cause); 845 notifier_list_notify(&shutdown_notifiers, &cause); 846 } 847 848 void qemu_system_powerdown_request(void) 849 { 850 trace_qemu_system_powerdown_request(); 851 powerdown_requested = 1; 852 qemu_notify_event(); 853 } 854 855 void qemu_register_powerdown_notifier(Notifier *notifier) 856 { 857 notifier_list_add(&powerdown_notifiers, notifier); 858 } 859 860 void qemu_register_shutdown_notifier(Notifier *notifier) 861 { 862 notifier_list_add(&shutdown_notifiers, notifier); 863 } 864 865 void qemu_system_debug_request(void) 866 { 867 debug_requested = 1; 868 qemu_notify_event(); 869 } 870 871 static bool main_loop_should_exit(int *status) 872 { 873 RunState r; 874 ShutdownCause request; 875 876 if (qemu_debug_requested()) { 877 vm_stop(RUN_STATE_DEBUG); 878 } 879 if (qemu_suspend_requested()) { 880 qemu_system_suspend(); 881 } 882 request = qemu_shutdown_requested(); 883 if (request) { 884 qemu_kill_report(); 885 qemu_system_shutdown(request); 886 if (shutdown_action == SHUTDOWN_ACTION_PAUSE) { 887 vm_stop(RUN_STATE_SHUTDOWN); 888 } else { 889 if (shutdown_exit_code != EXIT_SUCCESS) { 890 *status = shutdown_exit_code; 891 } else if (request == SHUTDOWN_CAUSE_GUEST_PANIC && 892 panic_action == PANIC_ACTION_EXIT_FAILURE) { 893 *status = EXIT_FAILURE; 894 } 895 return true; 896 } 897 } 898 request = qemu_reset_requested(); 899 if (request) { 900 pause_all_vcpus(); 901 qemu_system_reset(request); 902 resume_all_vcpus(); 903 /* 904 * runstate can change in pause_all_vcpus() 905 * as iothread mutex is unlocked 906 */ 907 if (!runstate_check(RUN_STATE_RUNNING) && 908 !runstate_check(RUN_STATE_INMIGRATE) && 909 !runstate_check(RUN_STATE_FINISH_MIGRATE)) { 910 runstate_set(RUN_STATE_PRELAUNCH); 911 } 912 } 913 if (qemu_wakeup_requested()) { 914 pause_all_vcpus(); 915 qemu_system_wakeup(); 916 notifier_list_notify(&wakeup_notifiers, &wakeup_reason); 917 wakeup_reason = QEMU_WAKEUP_REASON_NONE; 918 resume_all_vcpus(); 919 qapi_event_send_wakeup(); 920 } 921 if (qemu_powerdown_requested()) { 922 qemu_system_powerdown(); 923 } 924 if (qemu_vmstop_requested(&r)) { 925 vm_stop(r); 926 } 927 return false; 928 } 929 930 int qemu_main_loop(void) 931 { 932 int status = EXIT_SUCCESS; 933 934 while (!main_loop_should_exit(&status)) { 935 main_loop_wait(false); 936 } 937 938 return status; 939 } 940 941 void qemu_add_exit_notifier(Notifier *notify) 942 { 943 notifier_list_add(&exit_notifiers, notify); 944 } 945 946 void qemu_remove_exit_notifier(Notifier *notify) 947 { 948 notifier_remove(notify); 949 } 950 951 static void qemu_run_exit_notifiers(void) 952 { 953 BQL_LOCK_GUARD(); 954 notifier_list_notify(&exit_notifiers, NULL); 955 } 956 957 void qemu_init_subsystems(void) 958 { 959 Error *err = NULL; 960 961 os_set_line_buffering(); 962 963 module_call_init(MODULE_INIT_TRACE); 964 965 qemu_init_cpu_list(); 966 qemu_init_cpu_loop(); 967 bql_lock(); 968 969 atexit(qemu_run_exit_notifiers); 970 971 module_call_init(MODULE_INIT_QOM); 972 module_call_init(MODULE_INIT_MIGRATION); 973 974 runstate_init(); 975 precopy_infrastructure_init(); 976 postcopy_infrastructure_init(); 977 monitor_init_globals(); 978 979 if (qcrypto_init(&err) < 0) { 980 error_reportf_err(err, "cannot initialize crypto: "); 981 exit(1); 982 } 983 984 os_setup_early_signal_handling(); 985 986 bdrv_init_with_whitelist(); 987 socket_init(); 988 } 989 990 991 void qemu_cleanup(int status) 992 { 993 gdb_exit(status); 994 995 /* 996 * cleaning up the migration object cancels any existing migration 997 * try to do this early so that it also stops using devices. 998 */ 999 migration_shutdown(); 1000 1001 /* 1002 * Close the exports before draining the block layer. The export 1003 * drivers may have coroutines yielding on it, so we need to clean 1004 * them up before the drain, as otherwise they may be get stuck in 1005 * blk_wait_while_drained(). 1006 */ 1007 blk_exp_close_all(); 1008 1009 1010 /* No more vcpu or device emulation activity beyond this point */ 1011 vm_shutdown(); 1012 replay_finish(); 1013 1014 /* 1015 * We must cancel all block jobs while the block layer is drained, 1016 * or cancelling will be affected by throttling and thus may block 1017 * for an extended period of time. 1018 * Begin the drained section after vm_shutdown() to avoid requests being 1019 * stuck in the BlockBackend's request queue. 1020 * We do not need to end this section, because we do not want any 1021 * requests happening from here on anyway. 1022 */ 1023 bdrv_drain_all_begin(); 1024 job_cancel_sync_all(); 1025 bdrv_close_all(); 1026 1027 /* vhost-user must be cleaned up before chardevs. */ 1028 tpm_cleanup(); 1029 net_cleanup(); 1030 audio_cleanup(); 1031 monitor_cleanup(); 1032 qemu_chr_cleanup(); 1033 user_creatable_cleanup(); 1034 /* TODO: unref root container, check all devices are ok */ 1035 } 1036