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