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 "migration/misc.h" 36 #include "migration/postcopy-ram.h" 37 #include "monitor/monitor.h" 38 #include "net/net.h" 39 #include "net/vhost_net.h" 40 #include "qapi/error.h" 41 #include "qapi/qapi-commands-run-state.h" 42 #include "qapi/qapi-events-run-state.h" 43 #include "qemu/accel.h" 44 #include "qemu/error-report.h" 45 #include "qemu/job.h" 46 #include "qemu/log.h" 47 #include "qemu/module.h" 48 #include "qemu/plugin.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 "sysemu/cpus.h" 55 #include "sysemu/qtest.h" 56 #include "sysemu/replay.h" 57 #include "sysemu/reset.h" 58 #include "sysemu/runstate.h" 59 #include "sysemu/runstate-action.h" 60 #include "sysemu/sysemu.h" 61 #include "sysemu/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 81 { RUN_STATE_DEBUG, RUN_STATE_RUNNING }, 82 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE }, 83 { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH }, 84 85 { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR }, 86 { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR }, 87 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED }, 88 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING }, 89 { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN }, 90 { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED }, 91 { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG }, 92 { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED }, 93 { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE }, 94 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH }, 95 { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE }, 96 { RUN_STATE_INMIGRATE, RUN_STATE_COLO }, 97 98 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED }, 99 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE }, 100 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH }, 101 102 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING }, 103 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE }, 104 { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH }, 105 106 { RUN_STATE_PAUSED, RUN_STATE_RUNNING }, 107 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE }, 108 { RUN_STATE_PAUSED, RUN_STATE_POSTMIGRATE }, 109 { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH }, 110 { RUN_STATE_PAUSED, RUN_STATE_COLO}, 111 { RUN_STATE_PAUSED, RUN_STATE_SUSPENDED}, 112 113 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING }, 114 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE }, 115 { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH }, 116 117 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING }, 118 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE }, 119 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE }, 120 121 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING }, 122 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PAUSED }, 123 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE }, 124 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH }, 125 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_COLO }, 126 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_INTERNAL_ERROR }, 127 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_IO_ERROR }, 128 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_SHUTDOWN }, 129 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_SUSPENDED }, 130 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_WATCHDOG }, 131 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_GUEST_PANICKED }, 132 133 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING }, 134 { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH }, 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 154 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED }, 155 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE }, 156 { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH }, 157 { RUN_STATE_SHUTDOWN, RUN_STATE_COLO }, 158 159 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED }, 160 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED }, 161 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING }, 162 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE }, 163 { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH }, 164 { RUN_STATE_SUSPENDED, RUN_STATE_COLO}, 165 { RUN_STATE_SUSPENDED, RUN_STATE_PAUSED}, 166 167 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING }, 168 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE }, 169 { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH }, 170 { RUN_STATE_WATCHDOG, RUN_STATE_COLO}, 171 172 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING }, 173 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE }, 174 { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH }, 175 176 { RUN_STATE__MAX, RUN_STATE__MAX }, 177 }; 178 179 static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX]; 180 181 bool runstate_check(RunState state) 182 { 183 return current_run_state == state; 184 } 185 186 static void runstate_init(void) 187 { 188 const RunStateTransition *p; 189 190 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions)); 191 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE__MAX; p++) { 192 runstate_valid_transitions[p->from][p->to] = true; 193 } 194 195 qemu_mutex_init(&vmstop_lock); 196 } 197 198 /* This function will abort() on invalid state transitions */ 199 void runstate_set(RunState new_state) 200 { 201 assert(new_state < RUN_STATE__MAX); 202 203 trace_runstate_set(current_run_state, RunState_str(current_run_state), 204 new_state, RunState_str(new_state)); 205 206 if (current_run_state == new_state) { 207 return; 208 } 209 210 if (!runstate_valid_transitions[current_run_state][new_state]) { 211 error_report("invalid runstate transition: '%s' -> '%s'", 212 RunState_str(current_run_state), 213 RunState_str(new_state)); 214 abort(); 215 } 216 217 current_run_state = new_state; 218 } 219 220 RunState runstate_get(void) 221 { 222 return current_run_state; 223 } 224 225 bool runstate_is_running(void) 226 { 227 return runstate_check(RUN_STATE_RUNNING); 228 } 229 230 bool runstate_needs_reset(void) 231 { 232 return runstate_check(RUN_STATE_INTERNAL_ERROR) || 233 runstate_check(RUN_STATE_SHUTDOWN); 234 } 235 236 StatusInfo *qmp_query_status(Error **errp) 237 { 238 StatusInfo *info = g_malloc0(sizeof(*info)); 239 AccelState *accel = current_accel(); 240 241 /* 242 * We ignore errors, which will happen if the accelerator 243 * is not TCG. "singlestep" is meaningless for other accelerators, 244 * so we will set the StatusInfo field to false for those. 245 */ 246 info->singlestep = object_property_get_bool(OBJECT(accel), 247 "one-insn-per-tb", NULL); 248 info->running = runstate_is_running(); 249 info->status = current_run_state; 250 251 return info; 252 } 253 254 bool qemu_vmstop_requested(RunState *r) 255 { 256 qemu_mutex_lock(&vmstop_lock); 257 *r = vmstop_requested; 258 vmstop_requested = RUN_STATE__MAX; 259 qemu_mutex_unlock(&vmstop_lock); 260 return *r < RUN_STATE__MAX; 261 } 262 263 void qemu_system_vmstop_request_prepare(void) 264 { 265 qemu_mutex_lock(&vmstop_lock); 266 } 267 268 void qemu_system_vmstop_request(RunState state) 269 { 270 vmstop_requested = state; 271 qemu_mutex_unlock(&vmstop_lock); 272 qemu_notify_event(); 273 } 274 struct VMChangeStateEntry { 275 VMChangeStateHandler *cb; 276 VMChangeStateHandler *prepare_cb; 277 void *opaque; 278 QTAILQ_ENTRY(VMChangeStateEntry) entries; 279 int priority; 280 }; 281 282 static QTAILQ_HEAD(, VMChangeStateEntry) vm_change_state_head = 283 QTAILQ_HEAD_INITIALIZER(vm_change_state_head); 284 285 /** 286 * qemu_add_vm_change_state_handler_prio: 287 * @cb: the callback to invoke 288 * @opaque: user data passed to the callback 289 * @priority: low priorities execute first when the vm runs and the reverse is 290 * true when the vm stops 291 * 292 * Register a callback function that is invoked when the vm starts or stops 293 * running. 294 * 295 * Returns: an entry to be freed using qemu_del_vm_change_state_handler() 296 */ 297 VMChangeStateEntry *qemu_add_vm_change_state_handler_prio( 298 VMChangeStateHandler *cb, void *opaque, int priority) 299 { 300 return qemu_add_vm_change_state_handler_prio_full(cb, NULL, opaque, 301 priority); 302 } 303 304 /** 305 * qemu_add_vm_change_state_handler_prio_full: 306 * @cb: the main callback to invoke 307 * @prepare_cb: a callback to invoke before the main callback 308 * @opaque: user data passed to the callbacks 309 * @priority: low priorities execute first when the vm runs and the reverse is 310 * true when the vm stops 311 * 312 * Register a main callback function and an optional prepare callback function 313 * that are invoked when the vm starts or stops running. The main callback and 314 * the prepare callback are called in two separate phases: First all prepare 315 * callbacks are called and only then all main callbacks are called. As its 316 * name suggests, the prepare callback can be used to do some preparatory work 317 * before invoking the main callback. 318 * 319 * Returns: an entry to be freed using qemu_del_vm_change_state_handler() 320 */ 321 VMChangeStateEntry * 322 qemu_add_vm_change_state_handler_prio_full(VMChangeStateHandler *cb, 323 VMChangeStateHandler *prepare_cb, 324 void *opaque, int priority) 325 { 326 VMChangeStateEntry *e; 327 VMChangeStateEntry *other; 328 329 e = g_malloc0(sizeof(*e)); 330 e->cb = cb; 331 e->prepare_cb = prepare_cb; 332 e->opaque = opaque; 333 e->priority = priority; 334 335 /* Keep list sorted in ascending priority order */ 336 QTAILQ_FOREACH(other, &vm_change_state_head, entries) { 337 if (priority < other->priority) { 338 QTAILQ_INSERT_BEFORE(other, e, entries); 339 return e; 340 } 341 } 342 343 QTAILQ_INSERT_TAIL(&vm_change_state_head, e, entries); 344 return e; 345 } 346 347 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb, 348 void *opaque) 349 { 350 return qemu_add_vm_change_state_handler_prio(cb, opaque, 0); 351 } 352 353 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e) 354 { 355 QTAILQ_REMOVE(&vm_change_state_head, e, entries); 356 g_free(e); 357 } 358 359 void vm_state_notify(bool running, RunState state) 360 { 361 VMChangeStateEntry *e, *next; 362 363 trace_vm_state_notify(running, state, RunState_str(state)); 364 365 if (running) { 366 QTAILQ_FOREACH_SAFE(e, &vm_change_state_head, entries, next) { 367 if (e->prepare_cb) { 368 e->prepare_cb(e->opaque, running, state); 369 } 370 } 371 372 QTAILQ_FOREACH_SAFE(e, &vm_change_state_head, entries, next) { 373 e->cb(e->opaque, running, state); 374 } 375 } else { 376 QTAILQ_FOREACH_REVERSE_SAFE(e, &vm_change_state_head, entries, next) { 377 if (e->prepare_cb) { 378 e->prepare_cb(e->opaque, running, state); 379 } 380 } 381 382 QTAILQ_FOREACH_REVERSE_SAFE(e, &vm_change_state_head, entries, next) { 383 e->cb(e->opaque, running, state); 384 } 385 } 386 } 387 388 static ShutdownCause reset_requested; 389 static ShutdownCause shutdown_requested; 390 static int shutdown_exit_code = EXIT_SUCCESS; 391 static int shutdown_signal; 392 static pid_t shutdown_pid; 393 static int powerdown_requested; 394 static int debug_requested; 395 static int suspend_requested; 396 static WakeupReason wakeup_reason; 397 static NotifierList powerdown_notifiers = 398 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers); 399 static NotifierList suspend_notifiers = 400 NOTIFIER_LIST_INITIALIZER(suspend_notifiers); 401 static NotifierList wakeup_notifiers = 402 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers); 403 static NotifierList shutdown_notifiers = 404 NOTIFIER_LIST_INITIALIZER(shutdown_notifiers); 405 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE); 406 407 ShutdownCause qemu_shutdown_requested_get(void) 408 { 409 return shutdown_requested; 410 } 411 412 ShutdownCause qemu_reset_requested_get(void) 413 { 414 return reset_requested; 415 } 416 417 static int qemu_shutdown_requested(void) 418 { 419 return qatomic_xchg(&shutdown_requested, SHUTDOWN_CAUSE_NONE); 420 } 421 422 static void qemu_kill_report(void) 423 { 424 if (!qtest_driver() && shutdown_signal) { 425 if (shutdown_pid == 0) { 426 /* This happens for eg ^C at the terminal, so it's worth 427 * avoiding printing an odd message in that case. 428 */ 429 error_report("terminating on signal %d", shutdown_signal); 430 } else { 431 char *shutdown_cmd = qemu_get_pid_name(shutdown_pid); 432 433 error_report("terminating on signal %d from pid " FMT_pid " (%s)", 434 shutdown_signal, shutdown_pid, 435 shutdown_cmd ? shutdown_cmd : "<unknown process>"); 436 g_free(shutdown_cmd); 437 } 438 shutdown_signal = 0; 439 } 440 } 441 442 static ShutdownCause qemu_reset_requested(void) 443 { 444 ShutdownCause r = reset_requested; 445 446 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) { 447 reset_requested = SHUTDOWN_CAUSE_NONE; 448 return r; 449 } 450 return SHUTDOWN_CAUSE_NONE; 451 } 452 453 static int qemu_suspend_requested(void) 454 { 455 int r = suspend_requested; 456 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) { 457 suspend_requested = 0; 458 return r; 459 } 460 return false; 461 } 462 463 static WakeupReason qemu_wakeup_requested(void) 464 { 465 return wakeup_reason; 466 } 467 468 static int qemu_powerdown_requested(void) 469 { 470 int r = powerdown_requested; 471 powerdown_requested = 0; 472 return r; 473 } 474 475 static int qemu_debug_requested(void) 476 { 477 int r = debug_requested; 478 debug_requested = 0; 479 return r; 480 } 481 482 /* 483 * Reset the VM. Issue an event unless @reason is SHUTDOWN_CAUSE_NONE. 484 */ 485 void qemu_system_reset(ShutdownCause reason) 486 { 487 MachineClass *mc; 488 489 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL; 490 491 cpu_synchronize_all_states(); 492 493 if (mc && mc->reset) { 494 mc->reset(current_machine, reason); 495 } else { 496 qemu_devices_reset(reason); 497 } 498 switch (reason) { 499 case SHUTDOWN_CAUSE_NONE: 500 case SHUTDOWN_CAUSE_SUBSYSTEM_RESET: 501 case SHUTDOWN_CAUSE_SNAPSHOT_LOAD: 502 break; 503 default: 504 qapi_event_send_reset(shutdown_caused_by_guest(reason), reason); 505 } 506 cpu_synchronize_all_post_reset(); 507 vm_set_suspended(false); 508 } 509 510 /* 511 * Wake the VM after suspend. 512 */ 513 static void qemu_system_wakeup(void) 514 { 515 MachineClass *mc; 516 517 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL; 518 519 if (mc && mc->wakeup) { 520 mc->wakeup(current_machine); 521 } 522 } 523 524 void qemu_system_guest_panicked(GuestPanicInformation *info) 525 { 526 qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed"); 527 528 if (current_cpu) { 529 current_cpu->crash_occurred = true; 530 } 531 /* 532 * TODO: Currently the available panic actions are: none, pause, and 533 * shutdown, but in principle debug and reset could be supported as well. 534 * Investigate any potential use cases for the unimplemented actions. 535 */ 536 if (panic_action == PANIC_ACTION_PAUSE 537 || (panic_action == PANIC_ACTION_SHUTDOWN && shutdown_action == SHUTDOWN_ACTION_PAUSE)) { 538 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE, info); 539 vm_stop(RUN_STATE_GUEST_PANICKED); 540 } else if (panic_action == PANIC_ACTION_SHUTDOWN || 541 panic_action == PANIC_ACTION_EXIT_FAILURE) { 542 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF, info); 543 vm_stop(RUN_STATE_GUEST_PANICKED); 544 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_PANIC); 545 } else { 546 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_RUN, info); 547 } 548 549 if (info) { 550 if (info->type == GUEST_PANIC_INFORMATION_TYPE_HYPER_V) { 551 qemu_log_mask(LOG_GUEST_ERROR, "\nHV crash parameters: (%#"PRIx64 552 " %#"PRIx64" %#"PRIx64" %#"PRIx64" %#"PRIx64")\n", 553 info->u.hyper_v.arg1, 554 info->u.hyper_v.arg2, 555 info->u.hyper_v.arg3, 556 info->u.hyper_v.arg4, 557 info->u.hyper_v.arg5); 558 } else if (info->type == GUEST_PANIC_INFORMATION_TYPE_S390) { 559 qemu_log_mask(LOG_GUEST_ERROR, " on cpu %d: %s\n" 560 "PSW: 0x%016" PRIx64 " 0x%016" PRIx64"\n", 561 info->u.s390.core, 562 S390CrashReason_str(info->u.s390.reason), 563 info->u.s390.psw_mask, 564 info->u.s390.psw_addr); 565 } 566 qapi_free_GuestPanicInformation(info); 567 } 568 } 569 570 void qemu_system_guest_crashloaded(GuestPanicInformation *info) 571 { 572 qemu_log_mask(LOG_GUEST_ERROR, "Guest crash loaded"); 573 qapi_event_send_guest_crashloaded(GUEST_PANIC_ACTION_RUN, info); 574 qapi_free_GuestPanicInformation(info); 575 } 576 577 void qemu_system_reset_request(ShutdownCause reason) 578 { 579 if (reboot_action == REBOOT_ACTION_SHUTDOWN && 580 reason != SHUTDOWN_CAUSE_SUBSYSTEM_RESET) { 581 shutdown_requested = reason; 582 } else if (!cpus_are_resettable()) { 583 error_report("cpus are not resettable, terminating"); 584 shutdown_requested = reason; 585 } else { 586 reset_requested = reason; 587 } 588 cpu_stop_current(); 589 qemu_notify_event(); 590 } 591 592 static void qemu_system_suspend(void) 593 { 594 pause_all_vcpus(); 595 notifier_list_notify(&suspend_notifiers, NULL); 596 runstate_set(RUN_STATE_SUSPENDED); 597 qapi_event_send_suspend(); 598 } 599 600 void qemu_system_suspend_request(void) 601 { 602 if (runstate_check(RUN_STATE_SUSPENDED)) { 603 return; 604 } 605 suspend_requested = 1; 606 cpu_stop_current(); 607 qemu_notify_event(); 608 } 609 610 void qemu_register_suspend_notifier(Notifier *notifier) 611 { 612 notifier_list_add(&suspend_notifiers, notifier); 613 } 614 615 void qemu_system_wakeup_request(WakeupReason reason, Error **errp) 616 { 617 trace_system_wakeup_request(reason); 618 619 if (!runstate_check(RUN_STATE_SUSPENDED)) { 620 error_setg(errp, 621 "Unable to wake up: guest is not in suspended state"); 622 return; 623 } 624 if (!(wakeup_reason_mask & (1 << reason))) { 625 return; 626 } 627 runstate_set(RUN_STATE_RUNNING); 628 wakeup_reason = reason; 629 qemu_notify_event(); 630 } 631 632 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled) 633 { 634 if (enabled) { 635 wakeup_reason_mask |= (1 << reason); 636 } else { 637 wakeup_reason_mask &= ~(1 << reason); 638 } 639 } 640 641 void qemu_register_wakeup_notifier(Notifier *notifier) 642 { 643 notifier_list_add(&wakeup_notifiers, notifier); 644 } 645 646 static bool wakeup_suspend_enabled; 647 648 void qemu_register_wakeup_support(void) 649 { 650 wakeup_suspend_enabled = true; 651 } 652 653 bool qemu_wakeup_suspend_enabled(void) 654 { 655 return wakeup_suspend_enabled; 656 } 657 658 void qemu_system_killed(int signal, pid_t pid) 659 { 660 shutdown_signal = signal; 661 shutdown_pid = pid; 662 shutdown_action = SHUTDOWN_ACTION_POWEROFF; 663 664 /* Cannot call qemu_system_shutdown_request directly because 665 * we are in a signal handler. 666 */ 667 shutdown_requested = SHUTDOWN_CAUSE_HOST_SIGNAL; 668 qemu_notify_event(); 669 } 670 671 void qemu_system_shutdown_request_with_code(ShutdownCause reason, 672 int exit_code) 673 { 674 shutdown_exit_code = exit_code; 675 qemu_system_shutdown_request(reason); 676 } 677 678 void qemu_system_shutdown_request(ShutdownCause reason) 679 { 680 trace_qemu_system_shutdown_request(reason); 681 replay_shutdown_request(reason); 682 shutdown_requested = reason; 683 qemu_notify_event(); 684 } 685 686 static void qemu_system_powerdown(void) 687 { 688 qapi_event_send_powerdown(); 689 notifier_list_notify(&powerdown_notifiers, NULL); 690 } 691 692 static void qemu_system_shutdown(ShutdownCause cause) 693 { 694 qapi_event_send_shutdown(shutdown_caused_by_guest(cause), cause); 695 notifier_list_notify(&shutdown_notifiers, &cause); 696 } 697 698 void qemu_system_powerdown_request(void) 699 { 700 trace_qemu_system_powerdown_request(); 701 powerdown_requested = 1; 702 qemu_notify_event(); 703 } 704 705 void qemu_register_powerdown_notifier(Notifier *notifier) 706 { 707 notifier_list_add(&powerdown_notifiers, notifier); 708 } 709 710 void qemu_register_shutdown_notifier(Notifier *notifier) 711 { 712 notifier_list_add(&shutdown_notifiers, notifier); 713 } 714 715 void qemu_system_debug_request(void) 716 { 717 debug_requested = 1; 718 qemu_notify_event(); 719 } 720 721 static bool main_loop_should_exit(int *status) 722 { 723 RunState r; 724 ShutdownCause request; 725 726 if (qemu_debug_requested()) { 727 vm_stop(RUN_STATE_DEBUG); 728 } 729 if (qemu_suspend_requested()) { 730 qemu_system_suspend(); 731 } 732 request = qemu_shutdown_requested(); 733 if (request) { 734 qemu_kill_report(); 735 qemu_system_shutdown(request); 736 if (shutdown_action == SHUTDOWN_ACTION_PAUSE) { 737 vm_stop(RUN_STATE_SHUTDOWN); 738 } else { 739 if (shutdown_exit_code != EXIT_SUCCESS) { 740 *status = shutdown_exit_code; 741 } else if (request == SHUTDOWN_CAUSE_GUEST_PANIC && 742 panic_action == PANIC_ACTION_EXIT_FAILURE) { 743 *status = EXIT_FAILURE; 744 } 745 return true; 746 } 747 } 748 request = qemu_reset_requested(); 749 if (request) { 750 pause_all_vcpus(); 751 qemu_system_reset(request); 752 resume_all_vcpus(); 753 /* 754 * runstate can change in pause_all_vcpus() 755 * as iothread mutex is unlocked 756 */ 757 if (!runstate_check(RUN_STATE_RUNNING) && 758 !runstate_check(RUN_STATE_INMIGRATE) && 759 !runstate_check(RUN_STATE_FINISH_MIGRATE)) { 760 runstate_set(RUN_STATE_PRELAUNCH); 761 } 762 } 763 if (qemu_wakeup_requested()) { 764 pause_all_vcpus(); 765 qemu_system_wakeup(); 766 notifier_list_notify(&wakeup_notifiers, &wakeup_reason); 767 wakeup_reason = QEMU_WAKEUP_REASON_NONE; 768 resume_all_vcpus(); 769 qapi_event_send_wakeup(); 770 } 771 if (qemu_powerdown_requested()) { 772 qemu_system_powerdown(); 773 } 774 if (qemu_vmstop_requested(&r)) { 775 vm_stop(r); 776 } 777 return false; 778 } 779 780 int qemu_main_loop(void) 781 { 782 int status = EXIT_SUCCESS; 783 784 while (!main_loop_should_exit(&status)) { 785 main_loop_wait(false); 786 } 787 788 return status; 789 } 790 791 void qemu_add_exit_notifier(Notifier *notify) 792 { 793 notifier_list_add(&exit_notifiers, notify); 794 } 795 796 void qemu_remove_exit_notifier(Notifier *notify) 797 { 798 notifier_remove(notify); 799 } 800 801 static void qemu_run_exit_notifiers(void) 802 { 803 notifier_list_notify(&exit_notifiers, NULL); 804 } 805 806 void qemu_init_subsystems(void) 807 { 808 Error *err = NULL; 809 810 os_set_line_buffering(); 811 812 module_call_init(MODULE_INIT_TRACE); 813 814 qemu_init_cpu_list(); 815 qemu_init_cpu_loop(); 816 qemu_mutex_lock_iothread(); 817 818 atexit(qemu_run_exit_notifiers); 819 820 module_call_init(MODULE_INIT_QOM); 821 module_call_init(MODULE_INIT_MIGRATION); 822 823 runstate_init(); 824 precopy_infrastructure_init(); 825 postcopy_infrastructure_init(); 826 monitor_init_globals(); 827 828 if (qcrypto_init(&err) < 0) { 829 error_reportf_err(err, "cannot initialize crypto: "); 830 exit(1); 831 } 832 833 os_setup_early_signal_handling(); 834 835 bdrv_init_with_whitelist(); 836 socket_init(); 837 } 838 839 840 void qemu_cleanup(int status) 841 { 842 gdb_exit(status); 843 844 /* 845 * cleaning up the migration object cancels any existing migration 846 * try to do this early so that it also stops using devices. 847 */ 848 migration_shutdown(); 849 850 /* 851 * Close the exports before draining the block layer. The export 852 * drivers may have coroutines yielding on it, so we need to clean 853 * them up before the drain, as otherwise they may be get stuck in 854 * blk_wait_while_drained(). 855 */ 856 blk_exp_close_all(); 857 858 859 /* No more vcpu or device emulation activity beyond this point */ 860 vm_shutdown(); 861 replay_finish(); 862 863 /* 864 * We must cancel all block jobs while the block layer is drained, 865 * or cancelling will be affected by throttling and thus may block 866 * for an extended period of time. 867 * Begin the drained section after vm_shutdown() to avoid requests being 868 * stuck in the BlockBackend's request queue. 869 * We do not need to end this section, because we do not want any 870 * requests happening from here on anyway. 871 */ 872 bdrv_drain_all_begin(); 873 job_cancel_sync_all(); 874 bdrv_close_all(); 875 876 /* vhost-user must be cleaned up before chardevs. */ 877 tpm_cleanup(); 878 net_cleanup(); 879 audio_cleanup(); 880 monitor_cleanup(); 881 qemu_chr_cleanup(); 882 user_creatable_cleanup(); 883 /* TODO: unref root container, check all devices are ok */ 884 } 885