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 pid_t shutdown_pid; 441 static int powerdown_requested; 442 static int debug_requested; 443 static int suspend_requested; 444 static WakeupReason wakeup_reason; 445 static NotifierList powerdown_notifiers = 446 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers); 447 static NotifierList suspend_notifiers = 448 NOTIFIER_LIST_INITIALIZER(suspend_notifiers); 449 static NotifierList wakeup_notifiers = 450 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers); 451 static NotifierList shutdown_notifiers = 452 NOTIFIER_LIST_INITIALIZER(shutdown_notifiers); 453 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE); 454 455 ShutdownCause qemu_shutdown_requested_get(void) 456 { 457 return shutdown_requested; 458 } 459 460 ShutdownCause qemu_reset_requested_get(void) 461 { 462 return reset_requested; 463 } 464 465 static int qemu_shutdown_requested(void) 466 { 467 return qatomic_xchg(&shutdown_requested, SHUTDOWN_CAUSE_NONE); 468 } 469 470 static void qemu_kill_report(void) 471 { 472 if (!qtest_driver() && shutdown_signal) { 473 if (shutdown_pid == 0) { 474 /* This happens for eg ^C at the terminal, so it's worth 475 * avoiding printing an odd message in that case. 476 */ 477 error_report("terminating on signal %d", shutdown_signal); 478 } else { 479 char *shutdown_cmd = qemu_get_pid_name(shutdown_pid); 480 481 error_report("terminating on signal %d from pid " FMT_pid " (%s)", 482 shutdown_signal, shutdown_pid, 483 shutdown_cmd ? shutdown_cmd : "<unknown process>"); 484 g_free(shutdown_cmd); 485 } 486 shutdown_signal = 0; 487 } 488 } 489 490 static ShutdownCause qemu_reset_requested(void) 491 { 492 ShutdownCause r = reset_requested; 493 494 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) { 495 reset_requested = SHUTDOWN_CAUSE_NONE; 496 return r; 497 } 498 return SHUTDOWN_CAUSE_NONE; 499 } 500 501 static int qemu_suspend_requested(void) 502 { 503 int r = suspend_requested; 504 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) { 505 suspend_requested = 0; 506 return r; 507 } 508 return false; 509 } 510 511 static WakeupReason qemu_wakeup_requested(void) 512 { 513 return wakeup_reason; 514 } 515 516 static int qemu_powerdown_requested(void) 517 { 518 int r = powerdown_requested; 519 powerdown_requested = 0; 520 return r; 521 } 522 523 static int qemu_debug_requested(void) 524 { 525 int r = debug_requested; 526 debug_requested = 0; 527 return r; 528 } 529 530 /* 531 * Reset the VM. Issue an event unless @reason is SHUTDOWN_CAUSE_NONE. 532 */ 533 void qemu_system_reset(ShutdownCause reason) 534 { 535 MachineClass *mc; 536 ResetType type; 537 538 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL; 539 540 cpu_synchronize_all_states(); 541 542 switch (reason) { 543 case SHUTDOWN_CAUSE_SNAPSHOT_LOAD: 544 type = RESET_TYPE_SNAPSHOT_LOAD; 545 break; 546 default: 547 type = RESET_TYPE_COLD; 548 } 549 if (mc && mc->reset) { 550 mc->reset(current_machine, type); 551 } else { 552 qemu_devices_reset(type); 553 } 554 switch (reason) { 555 case SHUTDOWN_CAUSE_NONE: 556 case SHUTDOWN_CAUSE_SUBSYSTEM_RESET: 557 case SHUTDOWN_CAUSE_SNAPSHOT_LOAD: 558 break; 559 default: 560 qapi_event_send_reset(shutdown_caused_by_guest(reason), reason); 561 } 562 563 /* 564 * Some boards use the machine reset callback to point CPUs to the firmware 565 * entry point. Assume that this is not the case for boards that support 566 * non-resettable CPUs (currently used only for confidential guests), in 567 * which case cpu_synchronize_all_post_init() is enough because 568 * it does _more_ than cpu_synchronize_all_post_reset(). 569 */ 570 if (cpus_are_resettable()) { 571 cpu_synchronize_all_post_reset(); 572 } else { 573 assert(runstate_check(RUN_STATE_PRELAUNCH)); 574 } 575 576 vm_set_suspended(false); 577 } 578 579 /* 580 * Wake the VM after suspend. 581 */ 582 static void qemu_system_wakeup(void) 583 { 584 MachineClass *mc; 585 586 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL; 587 588 if (mc && mc->wakeup) { 589 mc->wakeup(current_machine); 590 } 591 } 592 593 static char *tdx_parse_panic_message(char *message) 594 { 595 bool printable = false; 596 char *buf = NULL; 597 int len = 0, i; 598 599 /* 600 * Although message is defined as a json string, we shouldn't 601 * unconditionally treat it as is because the guest generated it and 602 * it's not necessarily trustable. 603 */ 604 if (message) { 605 /* The caller guarantees the NULL-terminated string. */ 606 len = strlen(message); 607 608 printable = len > 0; 609 for (i = 0; i < len; i++) { 610 if (!(0x20 <= message[i] && message[i] <= 0x7e)) { 611 printable = false; 612 break; 613 } 614 } 615 } 616 617 if (len == 0) { 618 buf = g_malloc(1); 619 buf[0] = '\0'; 620 } else { 621 if (!printable) { 622 /* 3 = length of "%02x " */ 623 buf = g_malloc(len * 3); 624 for (i = 0; i < len; i++) { 625 if (message[i] == '\0') { 626 break; 627 } else { 628 sprintf(buf + 3 * i, "%02x ", message[i]); 629 } 630 } 631 if (i > 0) { 632 /* replace the last ' '(space) to NULL */ 633 buf[i * 3 - 1] = '\0'; 634 } else { 635 buf[0] = '\0'; 636 } 637 } else { 638 buf = g_strdup(message); 639 } 640 } 641 642 return buf; 643 } 644 645 void qemu_system_guest_panicked(GuestPanicInformation *info) 646 { 647 qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed"); 648 649 if (current_cpu) { 650 current_cpu->crash_occurred = true; 651 } 652 /* 653 * TODO: Currently the available panic actions are: none, pause, and 654 * shutdown, but in principle debug and reset could be supported as well. 655 * Investigate any potential use cases for the unimplemented actions. 656 */ 657 if (panic_action == PANIC_ACTION_PAUSE 658 || (panic_action == PANIC_ACTION_SHUTDOWN && shutdown_action == SHUTDOWN_ACTION_PAUSE)) { 659 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE, info); 660 vm_stop(RUN_STATE_GUEST_PANICKED); 661 } else if (panic_action == PANIC_ACTION_SHUTDOWN || 662 panic_action == PANIC_ACTION_EXIT_FAILURE) { 663 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF, info); 664 vm_stop(RUN_STATE_GUEST_PANICKED); 665 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_PANIC); 666 } else { 667 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_RUN, info); 668 } 669 670 if (info) { 671 if (info->type == GUEST_PANIC_INFORMATION_TYPE_HYPER_V) { 672 qemu_log_mask(LOG_GUEST_ERROR, "\nHV crash parameters: (%#"PRIx64 673 " %#"PRIx64" %#"PRIx64" %#"PRIx64" %#"PRIx64")\n", 674 info->u.hyper_v.arg1, 675 info->u.hyper_v.arg2, 676 info->u.hyper_v.arg3, 677 info->u.hyper_v.arg4, 678 info->u.hyper_v.arg5); 679 } else if (info->type == GUEST_PANIC_INFORMATION_TYPE_S390) { 680 qemu_log_mask(LOG_GUEST_ERROR, " on cpu %d: %s\n" 681 "PSW: 0x%016" PRIx64 " 0x%016" PRIx64"\n", 682 info->u.s390.core, 683 S390CrashReason_str(info->u.s390.reason), 684 info->u.s390.psw_mask, 685 info->u.s390.psw_addr); 686 } else if (info->type == GUEST_PANIC_INFORMATION_TYPE_TDX) { 687 char *message = tdx_parse_panic_message(info->u.tdx.message); 688 qemu_log_mask(LOG_GUEST_ERROR, 689 "\nTDX guest reports fatal error." 690 " error code: 0x%" PRIx32 " error message:\"%s\"\n", 691 info->u.tdx.error_code, message); 692 g_free(message); 693 if (info->u.tdx.gpa != -1ull) { 694 qemu_log_mask(LOG_GUEST_ERROR, "Additional error information " 695 "can be found at gpa page: 0x%" PRIx64 "\n", 696 info->u.tdx.gpa); 697 } 698 } 699 700 qapi_free_GuestPanicInformation(info); 701 } 702 } 703 704 void qemu_system_guest_crashloaded(GuestPanicInformation *info) 705 { 706 qemu_log_mask(LOG_GUEST_ERROR, "Guest crash loaded"); 707 qapi_event_send_guest_crashloaded(GUEST_PANIC_ACTION_RUN, info); 708 qapi_free_GuestPanicInformation(info); 709 } 710 711 void qemu_system_guest_pvshutdown(void) 712 { 713 qapi_event_send_guest_pvshutdown(); 714 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN); 715 } 716 717 void qemu_system_reset_request(ShutdownCause reason) 718 { 719 if (reboot_action == REBOOT_ACTION_SHUTDOWN && 720 reason != SHUTDOWN_CAUSE_SUBSYSTEM_RESET) { 721 shutdown_requested = reason; 722 } else if (!cpus_are_resettable()) { 723 error_report("cpus are not resettable, terminating"); 724 shutdown_requested = reason; 725 } else { 726 reset_requested = reason; 727 } 728 cpu_stop_current(); 729 qemu_notify_event(); 730 } 731 732 static void qemu_system_suspend(void) 733 { 734 pause_all_vcpus(); 735 notifier_list_notify(&suspend_notifiers, NULL); 736 runstate_set(RUN_STATE_SUSPENDED); 737 qapi_event_send_suspend(); 738 } 739 740 void qemu_system_suspend_request(void) 741 { 742 if (runstate_check(RUN_STATE_SUSPENDED)) { 743 return; 744 } 745 suspend_requested = 1; 746 cpu_stop_current(); 747 qemu_notify_event(); 748 } 749 750 void qemu_register_suspend_notifier(Notifier *notifier) 751 { 752 notifier_list_add(&suspend_notifiers, notifier); 753 } 754 755 void qemu_system_wakeup_request(WakeupReason reason, Error **errp) 756 { 757 trace_system_wakeup_request(reason); 758 759 if (!runstate_check(RUN_STATE_SUSPENDED)) { 760 error_setg(errp, 761 "Unable to wake up: guest is not in suspended state"); 762 return; 763 } 764 if (!(wakeup_reason_mask & (1 << reason))) { 765 return; 766 } 767 runstate_set(RUN_STATE_RUNNING); 768 wakeup_reason = reason; 769 qemu_notify_event(); 770 } 771 772 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled) 773 { 774 if (enabled) { 775 wakeup_reason_mask |= (1 << reason); 776 } else { 777 wakeup_reason_mask &= ~(1 << reason); 778 } 779 } 780 781 void qemu_register_wakeup_notifier(Notifier *notifier) 782 { 783 notifier_list_add(&wakeup_notifiers, notifier); 784 } 785 786 static bool wakeup_suspend_enabled; 787 788 void qemu_register_wakeup_support(void) 789 { 790 wakeup_suspend_enabled = true; 791 } 792 793 bool qemu_wakeup_suspend_enabled(void) 794 { 795 return wakeup_suspend_enabled; 796 } 797 798 void qemu_system_killed(int signal, pid_t pid) 799 { 800 shutdown_signal = signal; 801 shutdown_pid = pid; 802 shutdown_action = SHUTDOWN_ACTION_POWEROFF; 803 804 /* Cannot call qemu_system_shutdown_request directly because 805 * we are in a signal handler. 806 */ 807 shutdown_requested = SHUTDOWN_CAUSE_HOST_SIGNAL; 808 qemu_notify_event(); 809 } 810 811 void qemu_system_shutdown_request_with_code(ShutdownCause reason, 812 int exit_code) 813 { 814 shutdown_exit_code = exit_code; 815 qemu_system_shutdown_request(reason); 816 } 817 818 void qemu_system_shutdown_request(ShutdownCause reason) 819 { 820 trace_qemu_system_shutdown_request(reason); 821 replay_shutdown_request(reason); 822 shutdown_requested = reason; 823 qemu_notify_event(); 824 } 825 826 static void qemu_system_powerdown(void) 827 { 828 qapi_event_send_powerdown(); 829 notifier_list_notify(&powerdown_notifiers, NULL); 830 } 831 832 static void qemu_system_shutdown(ShutdownCause cause) 833 { 834 qapi_event_send_shutdown(shutdown_caused_by_guest(cause), cause); 835 notifier_list_notify(&shutdown_notifiers, &cause); 836 } 837 838 void qemu_system_powerdown_request(void) 839 { 840 trace_qemu_system_powerdown_request(); 841 powerdown_requested = 1; 842 qemu_notify_event(); 843 } 844 845 void qemu_register_powerdown_notifier(Notifier *notifier) 846 { 847 notifier_list_add(&powerdown_notifiers, notifier); 848 } 849 850 void qemu_register_shutdown_notifier(Notifier *notifier) 851 { 852 notifier_list_add(&shutdown_notifiers, notifier); 853 } 854 855 void qemu_system_debug_request(void) 856 { 857 debug_requested = 1; 858 qemu_notify_event(); 859 } 860 861 static bool main_loop_should_exit(int *status) 862 { 863 RunState r; 864 ShutdownCause request; 865 866 if (qemu_debug_requested()) { 867 vm_stop(RUN_STATE_DEBUG); 868 } 869 if (qemu_suspend_requested()) { 870 qemu_system_suspend(); 871 } 872 request = qemu_shutdown_requested(); 873 if (request) { 874 qemu_kill_report(); 875 qemu_system_shutdown(request); 876 if (shutdown_action == SHUTDOWN_ACTION_PAUSE) { 877 vm_stop(RUN_STATE_SHUTDOWN); 878 } else { 879 if (shutdown_exit_code != EXIT_SUCCESS) { 880 *status = shutdown_exit_code; 881 } else if (request == SHUTDOWN_CAUSE_GUEST_PANIC && 882 panic_action == PANIC_ACTION_EXIT_FAILURE) { 883 *status = EXIT_FAILURE; 884 } 885 return true; 886 } 887 } 888 request = qemu_reset_requested(); 889 if (request) { 890 pause_all_vcpus(); 891 qemu_system_reset(request); 892 resume_all_vcpus(); 893 /* 894 * runstate can change in pause_all_vcpus() 895 * as iothread mutex is unlocked 896 */ 897 if (!runstate_check(RUN_STATE_RUNNING) && 898 !runstate_check(RUN_STATE_INMIGRATE) && 899 !runstate_check(RUN_STATE_FINISH_MIGRATE)) { 900 runstate_set(RUN_STATE_PRELAUNCH); 901 } 902 } 903 if (qemu_wakeup_requested()) { 904 pause_all_vcpus(); 905 qemu_system_wakeup(); 906 notifier_list_notify(&wakeup_notifiers, &wakeup_reason); 907 wakeup_reason = QEMU_WAKEUP_REASON_NONE; 908 resume_all_vcpus(); 909 qapi_event_send_wakeup(); 910 } 911 if (qemu_powerdown_requested()) { 912 qemu_system_powerdown(); 913 } 914 if (qemu_vmstop_requested(&r)) { 915 vm_stop(r); 916 } 917 return false; 918 } 919 920 int qemu_main_loop(void) 921 { 922 int status = EXIT_SUCCESS; 923 924 while (!main_loop_should_exit(&status)) { 925 main_loop_wait(false); 926 } 927 928 return status; 929 } 930 931 void qemu_add_exit_notifier(Notifier *notify) 932 { 933 notifier_list_add(&exit_notifiers, notify); 934 } 935 936 void qemu_remove_exit_notifier(Notifier *notify) 937 { 938 notifier_remove(notify); 939 } 940 941 static void qemu_run_exit_notifiers(void) 942 { 943 BQL_LOCK_GUARD(); 944 notifier_list_notify(&exit_notifiers, NULL); 945 } 946 947 void qemu_init_subsystems(void) 948 { 949 Error *err = NULL; 950 951 os_set_line_buffering(); 952 953 module_call_init(MODULE_INIT_TRACE); 954 955 qemu_init_cpu_list(); 956 qemu_init_cpu_loop(); 957 bql_lock(); 958 959 atexit(qemu_run_exit_notifiers); 960 961 module_call_init(MODULE_INIT_QOM); 962 module_call_init(MODULE_INIT_MIGRATION); 963 964 runstate_init(); 965 precopy_infrastructure_init(); 966 postcopy_infrastructure_init(); 967 monitor_init_globals(); 968 969 if (qcrypto_init(&err) < 0) { 970 error_reportf_err(err, "cannot initialize crypto: "); 971 exit(1); 972 } 973 974 os_setup_early_signal_handling(); 975 976 bdrv_init_with_whitelist(); 977 socket_init(); 978 } 979 980 981 void qemu_cleanup(int status) 982 { 983 gdb_exit(status); 984 985 /* 986 * cleaning up the migration object cancels any existing migration 987 * try to do this early so that it also stops using devices. 988 */ 989 migration_shutdown(); 990 991 /* 992 * Close the exports before draining the block layer. The export 993 * drivers may have coroutines yielding on it, so we need to clean 994 * them up before the drain, as otherwise they may be get stuck in 995 * blk_wait_while_drained(). 996 */ 997 blk_exp_close_all(); 998 999 1000 /* No more vcpu or device emulation activity beyond this point */ 1001 vm_shutdown(); 1002 replay_finish(); 1003 1004 /* 1005 * We must cancel all block jobs while the block layer is drained, 1006 * or cancelling will be affected by throttling and thus may block 1007 * for an extended period of time. 1008 * Begin the drained section after vm_shutdown() to avoid requests being 1009 * stuck in the BlockBackend's request queue. 1010 * We do not need to end this section, because we do not want any 1011 * requests happening from here on anyway. 1012 */ 1013 bdrv_drain_all_begin(); 1014 job_cancel_sync_all(); 1015 bdrv_close_all(); 1016 1017 /* vhost-user must be cleaned up before chardevs. */ 1018 tpm_cleanup(); 1019 net_cleanup(); 1020 audio_cleanup(); 1021 monitor_cleanup(); 1022 qemu_chr_cleanup(); 1023 user_creatable_cleanup(); 1024 /* TODO: unref root container, check all devices are ok */ 1025 } 1026