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 void qemu_system_guest_panicked(GuestPanicInformation *info) 594 { 595 qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed"); 596 597 if (current_cpu) { 598 current_cpu->crash_occurred = true; 599 } 600 /* 601 * TODO: Currently the available panic actions are: none, pause, and 602 * shutdown, but in principle debug and reset could be supported as well. 603 * Investigate any potential use cases for the unimplemented actions. 604 */ 605 if (panic_action == PANIC_ACTION_PAUSE 606 || (panic_action == PANIC_ACTION_SHUTDOWN && shutdown_action == SHUTDOWN_ACTION_PAUSE)) { 607 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE, info); 608 vm_stop(RUN_STATE_GUEST_PANICKED); 609 } else if (panic_action == PANIC_ACTION_SHUTDOWN || 610 panic_action == PANIC_ACTION_EXIT_FAILURE) { 611 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF, info); 612 vm_stop(RUN_STATE_GUEST_PANICKED); 613 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_PANIC); 614 } else { 615 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_RUN, info); 616 } 617 618 if (info) { 619 if (info->type == GUEST_PANIC_INFORMATION_TYPE_HYPER_V) { 620 qemu_log_mask(LOG_GUEST_ERROR, "\nHV crash parameters: (%#"PRIx64 621 " %#"PRIx64" %#"PRIx64" %#"PRIx64" %#"PRIx64")\n", 622 info->u.hyper_v.arg1, 623 info->u.hyper_v.arg2, 624 info->u.hyper_v.arg3, 625 info->u.hyper_v.arg4, 626 info->u.hyper_v.arg5); 627 } else if (info->type == GUEST_PANIC_INFORMATION_TYPE_S390) { 628 qemu_log_mask(LOG_GUEST_ERROR, " on cpu %d: %s\n" 629 "PSW: 0x%016" PRIx64 " 0x%016" PRIx64"\n", 630 info->u.s390.core, 631 S390CrashReason_str(info->u.s390.reason), 632 info->u.s390.psw_mask, 633 info->u.s390.psw_addr); 634 } 635 qapi_free_GuestPanicInformation(info); 636 } 637 } 638 639 void qemu_system_guest_crashloaded(GuestPanicInformation *info) 640 { 641 qemu_log_mask(LOG_GUEST_ERROR, "Guest crash loaded"); 642 qapi_event_send_guest_crashloaded(GUEST_PANIC_ACTION_RUN, info); 643 qapi_free_GuestPanicInformation(info); 644 } 645 646 void qemu_system_guest_pvshutdown(void) 647 { 648 qapi_event_send_guest_pvshutdown(); 649 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN); 650 } 651 652 void qemu_system_reset_request(ShutdownCause reason) 653 { 654 if (reboot_action == REBOOT_ACTION_SHUTDOWN && 655 reason != SHUTDOWN_CAUSE_SUBSYSTEM_RESET) { 656 shutdown_requested = reason; 657 } else if (!cpus_are_resettable()) { 658 error_report("cpus are not resettable, terminating"); 659 shutdown_requested = reason; 660 } else { 661 reset_requested = reason; 662 } 663 cpu_stop_current(); 664 qemu_notify_event(); 665 } 666 667 static void qemu_system_suspend(void) 668 { 669 pause_all_vcpus(); 670 notifier_list_notify(&suspend_notifiers, NULL); 671 runstate_set(RUN_STATE_SUSPENDED); 672 qapi_event_send_suspend(); 673 } 674 675 void qemu_system_suspend_request(void) 676 { 677 if (runstate_check(RUN_STATE_SUSPENDED)) { 678 return; 679 } 680 suspend_requested = 1; 681 cpu_stop_current(); 682 qemu_notify_event(); 683 } 684 685 void qemu_register_suspend_notifier(Notifier *notifier) 686 { 687 notifier_list_add(&suspend_notifiers, notifier); 688 } 689 690 void qemu_system_wakeup_request(WakeupReason reason, Error **errp) 691 { 692 trace_system_wakeup_request(reason); 693 694 if (!runstate_check(RUN_STATE_SUSPENDED)) { 695 error_setg(errp, 696 "Unable to wake up: guest is not in suspended state"); 697 return; 698 } 699 if (!(wakeup_reason_mask & (1 << reason))) { 700 return; 701 } 702 runstate_set(RUN_STATE_RUNNING); 703 wakeup_reason = reason; 704 qemu_notify_event(); 705 } 706 707 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled) 708 { 709 if (enabled) { 710 wakeup_reason_mask |= (1 << reason); 711 } else { 712 wakeup_reason_mask &= ~(1 << reason); 713 } 714 } 715 716 void qemu_register_wakeup_notifier(Notifier *notifier) 717 { 718 notifier_list_add(&wakeup_notifiers, notifier); 719 } 720 721 static bool wakeup_suspend_enabled; 722 723 void qemu_register_wakeup_support(void) 724 { 725 wakeup_suspend_enabled = true; 726 } 727 728 bool qemu_wakeup_suspend_enabled(void) 729 { 730 return wakeup_suspend_enabled; 731 } 732 733 void qemu_system_killed(int signal, pid_t pid) 734 { 735 shutdown_signal = signal; 736 shutdown_pid = pid; 737 shutdown_action = SHUTDOWN_ACTION_POWEROFF; 738 739 /* Cannot call qemu_system_shutdown_request directly because 740 * we are in a signal handler. 741 */ 742 shutdown_requested = SHUTDOWN_CAUSE_HOST_SIGNAL; 743 qemu_notify_event(); 744 } 745 746 void qemu_system_shutdown_request_with_code(ShutdownCause reason, 747 int exit_code) 748 { 749 shutdown_exit_code = exit_code; 750 qemu_system_shutdown_request(reason); 751 } 752 753 void qemu_system_shutdown_request(ShutdownCause reason) 754 { 755 trace_qemu_system_shutdown_request(reason); 756 replay_shutdown_request(reason); 757 shutdown_requested = reason; 758 qemu_notify_event(); 759 } 760 761 static void qemu_system_powerdown(void) 762 { 763 qapi_event_send_powerdown(); 764 notifier_list_notify(&powerdown_notifiers, NULL); 765 } 766 767 static void qemu_system_shutdown(ShutdownCause cause) 768 { 769 qapi_event_send_shutdown(shutdown_caused_by_guest(cause), cause); 770 notifier_list_notify(&shutdown_notifiers, &cause); 771 } 772 773 void qemu_system_powerdown_request(void) 774 { 775 trace_qemu_system_powerdown_request(); 776 powerdown_requested = 1; 777 qemu_notify_event(); 778 } 779 780 void qemu_register_powerdown_notifier(Notifier *notifier) 781 { 782 notifier_list_add(&powerdown_notifiers, notifier); 783 } 784 785 void qemu_register_shutdown_notifier(Notifier *notifier) 786 { 787 notifier_list_add(&shutdown_notifiers, notifier); 788 } 789 790 void qemu_system_debug_request(void) 791 { 792 debug_requested = 1; 793 qemu_notify_event(); 794 } 795 796 static bool main_loop_should_exit(int *status) 797 { 798 RunState r; 799 ShutdownCause request; 800 801 if (qemu_debug_requested()) { 802 vm_stop(RUN_STATE_DEBUG); 803 } 804 if (qemu_suspend_requested()) { 805 qemu_system_suspend(); 806 } 807 request = qemu_shutdown_requested(); 808 if (request) { 809 qemu_kill_report(); 810 qemu_system_shutdown(request); 811 if (shutdown_action == SHUTDOWN_ACTION_PAUSE) { 812 vm_stop(RUN_STATE_SHUTDOWN); 813 } else { 814 if (shutdown_exit_code != EXIT_SUCCESS) { 815 *status = shutdown_exit_code; 816 } else if (request == SHUTDOWN_CAUSE_GUEST_PANIC && 817 panic_action == PANIC_ACTION_EXIT_FAILURE) { 818 *status = EXIT_FAILURE; 819 } 820 return true; 821 } 822 } 823 request = qemu_reset_requested(); 824 if (request) { 825 pause_all_vcpus(); 826 qemu_system_reset(request); 827 resume_all_vcpus(); 828 /* 829 * runstate can change in pause_all_vcpus() 830 * as iothread mutex is unlocked 831 */ 832 if (!runstate_check(RUN_STATE_RUNNING) && 833 !runstate_check(RUN_STATE_INMIGRATE) && 834 !runstate_check(RUN_STATE_FINISH_MIGRATE)) { 835 runstate_set(RUN_STATE_PRELAUNCH); 836 } 837 } 838 if (qemu_wakeup_requested()) { 839 pause_all_vcpus(); 840 qemu_system_wakeup(); 841 notifier_list_notify(&wakeup_notifiers, &wakeup_reason); 842 wakeup_reason = QEMU_WAKEUP_REASON_NONE; 843 resume_all_vcpus(); 844 qapi_event_send_wakeup(); 845 } 846 if (qemu_powerdown_requested()) { 847 qemu_system_powerdown(); 848 } 849 if (qemu_vmstop_requested(&r)) { 850 vm_stop(r); 851 } 852 return false; 853 } 854 855 int qemu_main_loop(void) 856 { 857 int status = EXIT_SUCCESS; 858 859 while (!main_loop_should_exit(&status)) { 860 main_loop_wait(false); 861 } 862 863 return status; 864 } 865 866 void qemu_add_exit_notifier(Notifier *notify) 867 { 868 notifier_list_add(&exit_notifiers, notify); 869 } 870 871 void qemu_remove_exit_notifier(Notifier *notify) 872 { 873 notifier_remove(notify); 874 } 875 876 static void qemu_run_exit_notifiers(void) 877 { 878 BQL_LOCK_GUARD(); 879 notifier_list_notify(&exit_notifiers, NULL); 880 } 881 882 void qemu_init_subsystems(void) 883 { 884 Error *err = NULL; 885 886 os_set_line_buffering(); 887 888 module_call_init(MODULE_INIT_TRACE); 889 890 qemu_init_cpu_list(); 891 qemu_init_cpu_loop(); 892 bql_lock(); 893 894 atexit(qemu_run_exit_notifiers); 895 896 module_call_init(MODULE_INIT_QOM); 897 module_call_init(MODULE_INIT_MIGRATION); 898 899 runstate_init(); 900 precopy_infrastructure_init(); 901 postcopy_infrastructure_init(); 902 monitor_init_globals(); 903 904 if (qcrypto_init(&err) < 0) { 905 error_reportf_err(err, "cannot initialize crypto: "); 906 exit(1); 907 } 908 909 os_setup_early_signal_handling(); 910 911 bdrv_init_with_whitelist(); 912 socket_init(); 913 } 914 915 916 void qemu_cleanup(int status) 917 { 918 gdb_exit(status); 919 920 /* 921 * cleaning up the migration object cancels any existing migration 922 * try to do this early so that it also stops using devices. 923 */ 924 migration_shutdown(); 925 926 /* 927 * Close the exports before draining the block layer. The export 928 * drivers may have coroutines yielding on it, so we need to clean 929 * them up before the drain, as otherwise they may be get stuck in 930 * blk_wait_while_drained(). 931 */ 932 blk_exp_close_all(); 933 934 935 /* No more vcpu or device emulation activity beyond this point */ 936 vm_shutdown(); 937 replay_finish(); 938 939 /* 940 * We must cancel all block jobs while the block layer is drained, 941 * or cancelling will be affected by throttling and thus may block 942 * for an extended period of time. 943 * Begin the drained section after vm_shutdown() to avoid requests being 944 * stuck in the BlockBackend's request queue. 945 * We do not need to end this section, because we do not want any 946 * requests happening from here on anyway. 947 */ 948 bdrv_drain_all_begin(); 949 job_cancel_sync_all(); 950 bdrv_close_all(); 951 952 /* vhost-user must be cleaned up before chardevs. */ 953 tpm_cleanup(); 954 net_cleanup(); 955 audio_cleanup(); 956 monitor_cleanup(); 957 qemu_chr_cleanup(); 958 user_creatable_cleanup(); 959 /* TODO: unref root container, check all devices are ok */ 960 } 961