2 * QEMU main system emulation loop
4 * Copyright (c) 2003-2020 QEMU contributors
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
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 "exec/gdbstub.h"
34 #include "hw/boards.h"
35 #include "migration/misc.h"
36 #include "migration/postcopy-ram.h"
37 #include "monitor/monitor.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-common.h"
44 #include "qemu/error-report.h"
48 #include "qemu/module.h"
49 #include "qemu/plugin.h"
50 #include "qemu/sockets.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"
64 static NotifierList exit_notifiers
=
65 NOTIFIER_LIST_INITIALIZER(exit_notifiers
);
67 static RunState current_run_state
= RUN_STATE_PRELAUNCH
;
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
;
78 static const RunStateTransition runstate_transitions_def
[] = {
79 { RUN_STATE_PRELAUNCH
, RUN_STATE_INMIGRATE
},
81 { RUN_STATE_DEBUG
, RUN_STATE_RUNNING
},
82 { RUN_STATE_DEBUG
, RUN_STATE_FINISH_MIGRATE
},
83 { RUN_STATE_DEBUG
, RUN_STATE_PRELAUNCH
},
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
},
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
},
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
},
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
},
112 { RUN_STATE_POSTMIGRATE
, RUN_STATE_RUNNING
},
113 { RUN_STATE_POSTMIGRATE
, RUN_STATE_FINISH_MIGRATE
},
114 { RUN_STATE_POSTMIGRATE
, RUN_STATE_PRELAUNCH
},
116 { RUN_STATE_PRELAUNCH
, RUN_STATE_RUNNING
},
117 { RUN_STATE_PRELAUNCH
, RUN_STATE_FINISH_MIGRATE
},
118 { RUN_STATE_PRELAUNCH
, RUN_STATE_INMIGRATE
},
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
},
126 { RUN_STATE_RESTORE_VM
, RUN_STATE_RUNNING
},
127 { RUN_STATE_RESTORE_VM
, RUN_STATE_PRELAUNCH
},
129 { RUN_STATE_COLO
, RUN_STATE_RUNNING
},
130 { RUN_STATE_COLO
, RUN_STATE_SHUTDOWN
},
132 { RUN_STATE_RUNNING
, RUN_STATE_DEBUG
},
133 { RUN_STATE_RUNNING
, RUN_STATE_INTERNAL_ERROR
},
134 { RUN_STATE_RUNNING
, RUN_STATE_IO_ERROR
},
135 { RUN_STATE_RUNNING
, RUN_STATE_PAUSED
},
136 { RUN_STATE_RUNNING
, RUN_STATE_FINISH_MIGRATE
},
137 { RUN_STATE_RUNNING
, RUN_STATE_RESTORE_VM
},
138 { RUN_STATE_RUNNING
, RUN_STATE_SAVE_VM
},
139 { RUN_STATE_RUNNING
, RUN_STATE_SHUTDOWN
},
140 { RUN_STATE_RUNNING
, RUN_STATE_WATCHDOG
},
141 { RUN_STATE_RUNNING
, RUN_STATE_GUEST_PANICKED
},
142 { RUN_STATE_RUNNING
, RUN_STATE_COLO
},
144 { RUN_STATE_SAVE_VM
, RUN_STATE_RUNNING
},
146 { RUN_STATE_SHUTDOWN
, RUN_STATE_PAUSED
},
147 { RUN_STATE_SHUTDOWN
, RUN_STATE_FINISH_MIGRATE
},
148 { RUN_STATE_SHUTDOWN
, RUN_STATE_PRELAUNCH
},
149 { RUN_STATE_SHUTDOWN
, RUN_STATE_COLO
},
151 { RUN_STATE_DEBUG
, RUN_STATE_SUSPENDED
},
152 { RUN_STATE_RUNNING
, RUN_STATE_SUSPENDED
},
153 { RUN_STATE_SUSPENDED
, RUN_STATE_RUNNING
},
154 { RUN_STATE_SUSPENDED
, RUN_STATE_FINISH_MIGRATE
},
155 { RUN_STATE_SUSPENDED
, RUN_STATE_PRELAUNCH
},
156 { RUN_STATE_SUSPENDED
, RUN_STATE_COLO
},
158 { RUN_STATE_WATCHDOG
, RUN_STATE_RUNNING
},
159 { RUN_STATE_WATCHDOG
, RUN_STATE_FINISH_MIGRATE
},
160 { RUN_STATE_WATCHDOG
, RUN_STATE_PRELAUNCH
},
161 { RUN_STATE_WATCHDOG
, RUN_STATE_COLO
},
163 { RUN_STATE_GUEST_PANICKED
, RUN_STATE_RUNNING
},
164 { RUN_STATE_GUEST_PANICKED
, RUN_STATE_FINISH_MIGRATE
},
165 { RUN_STATE_GUEST_PANICKED
, RUN_STATE_PRELAUNCH
},
167 { RUN_STATE__MAX
, RUN_STATE__MAX
},
170 static bool runstate_valid_transitions
[RUN_STATE__MAX
][RUN_STATE__MAX
];
172 bool runstate_check(RunState state
)
174 return current_run_state
== state
;
177 bool runstate_store(char *str
, size_t size
)
179 const char *state
= RunState_str(current_run_state
);
180 size_t len
= strlen(state
) + 1;
185 memcpy(str
, state
, len
);
189 static void runstate_init(void)
191 const RunStateTransition
*p
;
193 memset(&runstate_valid_transitions
, 0, sizeof(runstate_valid_transitions
));
194 for (p
= &runstate_transitions_def
[0]; p
->from
!= RUN_STATE__MAX
; p
++) {
195 runstate_valid_transitions
[p
->from
][p
->to
] = true;
198 qemu_mutex_init(&vmstop_lock
);
201 /* This function will abort() on invalid state transitions */
202 void runstate_set(RunState new_state
)
204 assert(new_state
< RUN_STATE__MAX
);
206 trace_runstate_set(current_run_state
, RunState_str(current_run_state
),
207 new_state
, RunState_str(new_state
));
209 if (current_run_state
== new_state
) {
213 if (!runstate_valid_transitions
[current_run_state
][new_state
]) {
214 error_report("invalid runstate transition: '%s' -> '%s'",
215 RunState_str(current_run_state
),
216 RunState_str(new_state
));
220 current_run_state
= new_state
;
223 bool runstate_is_running(void)
225 return runstate_check(RUN_STATE_RUNNING
);
228 bool runstate_needs_reset(void)
230 return runstate_check(RUN_STATE_INTERNAL_ERROR
) ||
231 runstate_check(RUN_STATE_SHUTDOWN
);
234 StatusInfo
*qmp_query_status(Error
**errp
)
236 StatusInfo
*info
= g_malloc0(sizeof(*info
));
238 info
->running
= runstate_is_running();
239 info
->singlestep
= singlestep
;
240 info
->status
= current_run_state
;
245 bool qemu_vmstop_requested(RunState
*r
)
247 qemu_mutex_lock(&vmstop_lock
);
248 *r
= vmstop_requested
;
249 vmstop_requested
= RUN_STATE__MAX
;
250 qemu_mutex_unlock(&vmstop_lock
);
251 return *r
< RUN_STATE__MAX
;
254 void qemu_system_vmstop_request_prepare(void)
256 qemu_mutex_lock(&vmstop_lock
);
259 void qemu_system_vmstop_request(RunState state
)
261 vmstop_requested
= state
;
262 qemu_mutex_unlock(&vmstop_lock
);
265 struct VMChangeStateEntry
{
266 VMChangeStateHandler
*cb
;
268 QTAILQ_ENTRY(VMChangeStateEntry
) entries
;
272 static QTAILQ_HEAD(, VMChangeStateEntry
) vm_change_state_head
=
273 QTAILQ_HEAD_INITIALIZER(vm_change_state_head
);
276 * qemu_add_vm_change_state_handler_prio:
277 * @cb: the callback to invoke
278 * @opaque: user data passed to the callback
279 * @priority: low priorities execute first when the vm runs and the reverse is
280 * true when the vm stops
282 * Register a callback function that is invoked when the vm starts or stops
285 * Returns: an entry to be freed using qemu_del_vm_change_state_handler()
287 VMChangeStateEntry
*qemu_add_vm_change_state_handler_prio(
288 VMChangeStateHandler
*cb
, void *opaque
, int priority
)
290 VMChangeStateEntry
*e
;
291 VMChangeStateEntry
*other
;
293 e
= g_malloc0(sizeof(*e
));
296 e
->priority
= priority
;
298 /* Keep list sorted in ascending priority order */
299 QTAILQ_FOREACH(other
, &vm_change_state_head
, entries
) {
300 if (priority
< other
->priority
) {
301 QTAILQ_INSERT_BEFORE(other
, e
, entries
);
306 QTAILQ_INSERT_TAIL(&vm_change_state_head
, e
, entries
);
310 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
313 return qemu_add_vm_change_state_handler_prio(cb
, opaque
, 0);
316 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
318 QTAILQ_REMOVE(&vm_change_state_head
, e
, entries
);
322 void vm_state_notify(bool running
, RunState state
)
324 VMChangeStateEntry
*e
, *next
;
326 trace_vm_state_notify(running
, state
, RunState_str(state
));
329 QTAILQ_FOREACH_SAFE(e
, &vm_change_state_head
, entries
, next
) {
330 e
->cb(e
->opaque
, running
, state
);
333 QTAILQ_FOREACH_REVERSE_SAFE(e
, &vm_change_state_head
, entries
, next
) {
334 e
->cb(e
->opaque
, running
, state
);
339 static ShutdownCause reset_requested
;
340 static ShutdownCause shutdown_requested
;
341 static int shutdown_signal
;
342 static pid_t shutdown_pid
;
343 static int powerdown_requested
;
344 static int debug_requested
;
345 static int suspend_requested
;
346 static WakeupReason wakeup_reason
;
347 static NotifierList powerdown_notifiers
=
348 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers
);
349 static NotifierList suspend_notifiers
=
350 NOTIFIER_LIST_INITIALIZER(suspend_notifiers
);
351 static NotifierList wakeup_notifiers
=
352 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers
);
353 static NotifierList shutdown_notifiers
=
354 NOTIFIER_LIST_INITIALIZER(shutdown_notifiers
);
355 static uint32_t wakeup_reason_mask
= ~(1 << QEMU_WAKEUP_REASON_NONE
);
357 ShutdownCause
qemu_shutdown_requested_get(void)
359 return shutdown_requested
;
362 ShutdownCause
qemu_reset_requested_get(void)
364 return reset_requested
;
367 static int qemu_shutdown_requested(void)
369 return qatomic_xchg(&shutdown_requested
, SHUTDOWN_CAUSE_NONE
);
372 static void qemu_kill_report(void)
374 if (!qtest_driver() && shutdown_signal
) {
375 if (shutdown_pid
== 0) {
376 /* This happens for eg ^C at the terminal, so it's worth
377 * avoiding printing an odd message in that case.
379 error_report("terminating on signal %d", shutdown_signal
);
381 char *shutdown_cmd
= qemu_get_pid_name(shutdown_pid
);
383 error_report("terminating on signal %d from pid " FMT_pid
" (%s)",
384 shutdown_signal
, shutdown_pid
,
385 shutdown_cmd
? shutdown_cmd
: "<unknown process>");
386 g_free(shutdown_cmd
);
392 static ShutdownCause
qemu_reset_requested(void)
394 ShutdownCause r
= reset_requested
;
396 if (r
&& replay_checkpoint(CHECKPOINT_RESET_REQUESTED
)) {
397 reset_requested
= SHUTDOWN_CAUSE_NONE
;
400 return SHUTDOWN_CAUSE_NONE
;
403 static int qemu_suspend_requested(void)
405 int r
= suspend_requested
;
406 if (r
&& replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED
)) {
407 suspend_requested
= 0;
413 static WakeupReason
qemu_wakeup_requested(void)
415 return wakeup_reason
;
418 static int qemu_powerdown_requested(void)
420 int r
= powerdown_requested
;
421 powerdown_requested
= 0;
425 static int qemu_debug_requested(void)
427 int r
= debug_requested
;
433 * Reset the VM. Issue an event unless @reason is SHUTDOWN_CAUSE_NONE.
435 void qemu_system_reset(ShutdownCause reason
)
439 mc
= current_machine
? MACHINE_GET_CLASS(current_machine
) : NULL
;
441 cpu_synchronize_all_states();
443 if (mc
&& mc
->reset
) {
444 mc
->reset(current_machine
);
446 qemu_devices_reset();
448 if (reason
&& reason
!= SHUTDOWN_CAUSE_SUBSYSTEM_RESET
) {
449 qapi_event_send_reset(shutdown_caused_by_guest(reason
), reason
);
451 cpu_synchronize_all_post_reset();
455 * Wake the VM after suspend.
457 static void qemu_system_wakeup(void)
461 mc
= current_machine
? MACHINE_GET_CLASS(current_machine
) : NULL
;
463 if (mc
&& mc
->wakeup
) {
464 mc
->wakeup(current_machine
);
468 void qemu_system_guest_panicked(GuestPanicInformation
*info
)
470 qemu_log_mask(LOG_GUEST_ERROR
, "Guest crashed");
473 current_cpu
->crash_occurred
= true;
476 * TODO: Currently the available panic actions are: none, pause, and
477 * shutdown, but in principle debug and reset could be supported as well.
478 * Investigate any potential use cases for the unimplemented actions.
480 if (panic_action
== PANIC_ACTION_PAUSE
481 || (panic_action
== PANIC_ACTION_SHUTDOWN
&& shutdown_action
== SHUTDOWN_ACTION_PAUSE
)) {
482 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE
,
484 vm_stop(RUN_STATE_GUEST_PANICKED
);
485 } else if (panic_action
== PANIC_ACTION_SHUTDOWN
) {
486 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF
,
488 vm_stop(RUN_STATE_GUEST_PANICKED
);
489 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_PANIC
);
491 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_RUN
,
496 if (info
->type
== GUEST_PANIC_INFORMATION_TYPE_HYPER_V
) {
497 qemu_log_mask(LOG_GUEST_ERROR
, "\nHV crash parameters: (%#"PRIx64
498 " %#"PRIx64
" %#"PRIx64
" %#"PRIx64
" %#"PRIx64
")\n",
499 info
->u
.hyper_v
.arg1
,
500 info
->u
.hyper_v
.arg2
,
501 info
->u
.hyper_v
.arg3
,
502 info
->u
.hyper_v
.arg4
,
503 info
->u
.hyper_v
.arg5
);
504 } else if (info
->type
== GUEST_PANIC_INFORMATION_TYPE_S390
) {
505 qemu_log_mask(LOG_GUEST_ERROR
, " on cpu %d: %s\n"
506 "PSW: 0x%016" PRIx64
" 0x%016" PRIx64
"\n",
508 S390CrashReason_str(info
->u
.s390
.reason
),
509 info
->u
.s390
.psw_mask
,
510 info
->u
.s390
.psw_addr
);
512 qapi_free_GuestPanicInformation(info
);
516 void qemu_system_guest_crashloaded(GuestPanicInformation
*info
)
518 qemu_log_mask(LOG_GUEST_ERROR
, "Guest crash loaded");
520 qapi_event_send_guest_crashloaded(GUEST_PANIC_ACTION_RUN
,
524 qapi_free_GuestPanicInformation(info
);
528 void qemu_system_reset_request(ShutdownCause reason
)
530 if (reboot_action
== REBOOT_ACTION_SHUTDOWN
&&
531 reason
!= SHUTDOWN_CAUSE_SUBSYSTEM_RESET
) {
532 shutdown_requested
= reason
;
533 } else if (!cpus_are_resettable()) {
534 error_report("cpus are not resettable, terminating");
535 shutdown_requested
= reason
;
537 reset_requested
= reason
;
543 static void qemu_system_suspend(void)
546 notifier_list_notify(&suspend_notifiers
, NULL
);
547 runstate_set(RUN_STATE_SUSPENDED
);
548 qapi_event_send_suspend();
551 void qemu_system_suspend_request(void)
553 if (runstate_check(RUN_STATE_SUSPENDED
)) {
556 suspend_requested
= 1;
561 void qemu_register_suspend_notifier(Notifier
*notifier
)
563 notifier_list_add(&suspend_notifiers
, notifier
);
566 void qemu_system_wakeup_request(WakeupReason reason
, Error
**errp
)
568 trace_system_wakeup_request(reason
);
570 if (!runstate_check(RUN_STATE_SUSPENDED
)) {
572 "Unable to wake up: guest is not in suspended state");
575 if (!(wakeup_reason_mask
& (1 << reason
))) {
578 runstate_set(RUN_STATE_RUNNING
);
579 wakeup_reason
= reason
;
583 void qemu_system_wakeup_enable(WakeupReason reason
, bool enabled
)
586 wakeup_reason_mask
|= (1 << reason
);
588 wakeup_reason_mask
&= ~(1 << reason
);
592 void qemu_register_wakeup_notifier(Notifier
*notifier
)
594 notifier_list_add(&wakeup_notifiers
, notifier
);
597 static bool wakeup_suspend_enabled
;
599 void qemu_register_wakeup_support(void)
601 wakeup_suspend_enabled
= true;
604 bool qemu_wakeup_suspend_enabled(void)
606 return wakeup_suspend_enabled
;
609 void qemu_system_killed(int signal
, pid_t pid
)
611 shutdown_signal
= signal
;
613 shutdown_action
= SHUTDOWN_ACTION_POWEROFF
;
615 /* Cannot call qemu_system_shutdown_request directly because
616 * we are in a signal handler.
618 shutdown_requested
= SHUTDOWN_CAUSE_HOST_SIGNAL
;
622 void qemu_system_shutdown_request(ShutdownCause reason
)
624 trace_qemu_system_shutdown_request(reason
);
625 replay_shutdown_request(reason
);
626 shutdown_requested
= reason
;
630 static void qemu_system_powerdown(void)
632 qapi_event_send_powerdown();
633 notifier_list_notify(&powerdown_notifiers
, NULL
);
636 static void qemu_system_shutdown(ShutdownCause cause
)
638 qapi_event_send_shutdown(shutdown_caused_by_guest(cause
), cause
);
639 notifier_list_notify(&shutdown_notifiers
, &cause
);
642 void qemu_system_powerdown_request(void)
644 trace_qemu_system_powerdown_request();
645 powerdown_requested
= 1;
649 void qemu_register_powerdown_notifier(Notifier
*notifier
)
651 notifier_list_add(&powerdown_notifiers
, notifier
);
654 void qemu_register_shutdown_notifier(Notifier
*notifier
)
656 notifier_list_add(&shutdown_notifiers
, notifier
);
659 void qemu_system_debug_request(void)
665 static bool main_loop_should_exit(void)
668 ShutdownCause request
;
670 if (qemu_debug_requested()) {
671 vm_stop(RUN_STATE_DEBUG
);
673 if (qemu_suspend_requested()) {
674 qemu_system_suspend();
676 request
= qemu_shutdown_requested();
679 qemu_system_shutdown(request
);
680 if (shutdown_action
== SHUTDOWN_ACTION_PAUSE
) {
681 vm_stop(RUN_STATE_SHUTDOWN
);
686 request
= qemu_reset_requested();
689 qemu_system_reset(request
);
692 * runstate can change in pause_all_vcpus()
693 * as iothread mutex is unlocked
695 if (!runstate_check(RUN_STATE_RUNNING
) &&
696 !runstate_check(RUN_STATE_INMIGRATE
) &&
697 !runstate_check(RUN_STATE_FINISH_MIGRATE
)) {
698 runstate_set(RUN_STATE_PRELAUNCH
);
701 if (qemu_wakeup_requested()) {
703 qemu_system_wakeup();
704 notifier_list_notify(&wakeup_notifiers
, &wakeup_reason
);
705 wakeup_reason
= QEMU_WAKEUP_REASON_NONE
;
707 qapi_event_send_wakeup();
709 if (qemu_powerdown_requested()) {
710 qemu_system_powerdown();
712 if (qemu_vmstop_requested(&r
)) {
718 void qemu_main_loop(void)
720 #ifdef CONFIG_PROFILER
723 while (!main_loop_should_exit()) {
724 #ifdef CONFIG_PROFILER
725 ti
= profile_getclock();
727 main_loop_wait(false);
728 #ifdef CONFIG_PROFILER
729 dev_time
+= profile_getclock() - ti
;
734 void qemu_add_exit_notifier(Notifier
*notify
)
736 notifier_list_add(&exit_notifiers
, notify
);
739 void qemu_remove_exit_notifier(Notifier
*notify
)
741 notifier_remove(notify
);
744 static void qemu_run_exit_notifiers(void)
746 notifier_list_notify(&exit_notifiers
, NULL
);
749 void qemu_init_subsystems(void)
753 os_set_line_buffering();
755 module_call_init(MODULE_INIT_TRACE
);
757 qemu_init_cpu_list();
758 qemu_init_cpu_loop();
759 qemu_mutex_lock_iothread();
761 atexit(qemu_run_exit_notifiers
);
763 module_call_init(MODULE_INIT_QOM
);
764 module_call_init(MODULE_INIT_MIGRATION
);
767 precopy_infrastructure_init();
768 postcopy_infrastructure_init();
769 monitor_init_globals();
771 if (qcrypto_init(&err
) < 0) {
772 error_reportf_err(err
, "cannot initialize crypto: ");
776 os_setup_early_signal_handling();
778 bdrv_init_with_whitelist();
783 void qemu_cleanup(void)
788 * cleaning up the migration object cancels any existing migration
789 * try to do this early so that it also stops using devices.
791 migration_shutdown();
794 * Close the exports before draining the block layer. The export
795 * drivers may have coroutines yielding on it, so we need to clean
796 * them up before the drain, as otherwise they may be get stuck in
797 * blk_wait_while_drained().
802 * We must cancel all block jobs while the block layer is drained,
803 * or cancelling will be affected by throttling and thus may block
804 * for an extended period of time.
805 * vm_shutdown() will bdrv_drain_all(), so we may as well include
806 * it in the drained section.
807 * We do not need to end this section, because we do not want any
808 * requests happening from here on anyway.
810 bdrv_drain_all_begin();
812 /* No more vcpu or device emulation activity beyond this point */
816 job_cancel_sync_all();
819 /* vhost-user must be cleaned up before chardevs. */
825 user_creatable_cleanup();
826 /* TODO: unref root container, check all devices are ok */