block/io: improve bdrv_check_request: check qiov too
[qemu/kevin.git] / softmmu / runstate.c
bloba7fcb603f7110a48fd1746ed46d505a2fe229b2b
1 /*
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
22 * THE SOFTWARE.
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/exec-all.h"
34 #include "exec/gdbstub.h"
35 #include "hw/boards.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-common.h"
45 #include "qemu/error-report.h"
46 #include "qemu/job.h"
47 #include "qemu/module.h"
48 #include "qemu/plugin.h"
49 #include "qemu/sockets.h"
50 #include "qemu/thread.h"
51 #include "qom/object.h"
52 #include "qom/object_interfaces.h"
53 #include "sysemu/cpus.h"
54 #include "sysemu/qtest.h"
55 #include "sysemu/replay.h"
56 #include "sysemu/reset.h"
57 #include "sysemu/runstate.h"
58 #include "sysemu/runstate-action.h"
59 #include "sysemu/sysemu.h"
60 #include "sysemu/tpm.h"
61 #include "trace.h"
63 static NotifierList exit_notifiers =
64 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
66 static RunState current_run_state = RUN_STATE_PRELAUNCH;
68 /* We use RUN_STATE__MAX but any invalid value will do */
69 static RunState vmstop_requested = RUN_STATE__MAX;
70 static QemuMutex vmstop_lock;
72 typedef struct {
73 RunState from;
74 RunState to;
75 } RunStateTransition;
77 static const RunStateTransition runstate_transitions_def[] = {
78 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
80 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
81 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
82 { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH },
84 { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
85 { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
86 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
87 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
88 { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
89 { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
90 { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
91 { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
92 { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
93 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
94 { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE },
95 { RUN_STATE_INMIGRATE, RUN_STATE_COLO },
97 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
98 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
99 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH },
101 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
102 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
103 { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH },
105 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
106 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
107 { RUN_STATE_PAUSED, RUN_STATE_POSTMIGRATE },
108 { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH },
109 { RUN_STATE_PAUSED, RUN_STATE_COLO},
111 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
112 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
113 { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH },
115 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
116 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
117 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
119 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
120 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PAUSED },
121 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
122 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH },
123 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_COLO},
125 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
126 { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
128 { RUN_STATE_COLO, RUN_STATE_RUNNING },
130 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
131 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
132 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
133 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
134 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
135 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
136 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
137 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
138 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
139 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
140 { RUN_STATE_RUNNING, RUN_STATE_COLO},
142 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
144 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
145 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
146 { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH },
147 { RUN_STATE_SHUTDOWN, RUN_STATE_COLO },
149 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
150 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
151 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
152 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
153 { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
154 { RUN_STATE_SUSPENDED, RUN_STATE_COLO},
156 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
157 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
158 { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH },
159 { RUN_STATE_WATCHDOG, RUN_STATE_COLO},
161 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
162 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
163 { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH },
165 { RUN_STATE__MAX, RUN_STATE__MAX },
168 static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
170 bool runstate_check(RunState state)
172 return current_run_state == state;
175 bool runstate_store(char *str, size_t size)
177 const char *state = RunState_str(current_run_state);
178 size_t len = strlen(state) + 1;
180 if (len > size) {
181 return false;
183 memcpy(str, state, len);
184 return true;
187 static void runstate_init(void)
189 const RunStateTransition *p;
191 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
192 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE__MAX; p++) {
193 runstate_valid_transitions[p->from][p->to] = true;
196 qemu_mutex_init(&vmstop_lock);
199 /* This function will abort() on invalid state transitions */
200 void runstate_set(RunState new_state)
202 assert(new_state < RUN_STATE__MAX);
204 trace_runstate_set(current_run_state, RunState_str(current_run_state),
205 new_state, RunState_str(new_state));
207 if (current_run_state == new_state) {
208 return;
211 if (!runstate_valid_transitions[current_run_state][new_state]) {
212 error_report("invalid runstate transition: '%s' -> '%s'",
213 RunState_str(current_run_state),
214 RunState_str(new_state));
215 abort();
218 current_run_state = new_state;
221 int runstate_is_running(void)
223 return runstate_check(RUN_STATE_RUNNING);
226 bool runstate_needs_reset(void)
228 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
229 runstate_check(RUN_STATE_SHUTDOWN);
232 StatusInfo *qmp_query_status(Error **errp)
234 StatusInfo *info = g_malloc0(sizeof(*info));
236 info->running = runstate_is_running();
237 info->singlestep = singlestep;
238 info->status = current_run_state;
240 return info;
243 bool qemu_vmstop_requested(RunState *r)
245 qemu_mutex_lock(&vmstop_lock);
246 *r = vmstop_requested;
247 vmstop_requested = RUN_STATE__MAX;
248 qemu_mutex_unlock(&vmstop_lock);
249 return *r < RUN_STATE__MAX;
252 void qemu_system_vmstop_request_prepare(void)
254 qemu_mutex_lock(&vmstop_lock);
257 void qemu_system_vmstop_request(RunState state)
259 vmstop_requested = state;
260 qemu_mutex_unlock(&vmstop_lock);
261 qemu_notify_event();
263 struct VMChangeStateEntry {
264 VMChangeStateHandler *cb;
265 void *opaque;
266 QTAILQ_ENTRY(VMChangeStateEntry) entries;
267 int priority;
270 static QTAILQ_HEAD(, VMChangeStateEntry) vm_change_state_head =
271 QTAILQ_HEAD_INITIALIZER(vm_change_state_head);
274 * qemu_add_vm_change_state_handler_prio:
275 * @cb: the callback to invoke
276 * @opaque: user data passed to the callback
277 * @priority: low priorities execute first when the vm runs and the reverse is
278 * true when the vm stops
280 * Register a callback function that is invoked when the vm starts or stops
281 * running.
283 * Returns: an entry to be freed using qemu_del_vm_change_state_handler()
285 VMChangeStateEntry *qemu_add_vm_change_state_handler_prio(
286 VMChangeStateHandler *cb, void *opaque, int priority)
288 VMChangeStateEntry *e;
289 VMChangeStateEntry *other;
291 e = g_malloc0(sizeof(*e));
292 e->cb = cb;
293 e->opaque = opaque;
294 e->priority = priority;
296 /* Keep list sorted in ascending priority order */
297 QTAILQ_FOREACH(other, &vm_change_state_head, entries) {
298 if (priority < other->priority) {
299 QTAILQ_INSERT_BEFORE(other, e, entries);
300 return e;
304 QTAILQ_INSERT_TAIL(&vm_change_state_head, e, entries);
305 return e;
308 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
309 void *opaque)
311 return qemu_add_vm_change_state_handler_prio(cb, opaque, 0);
314 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
316 QTAILQ_REMOVE(&vm_change_state_head, e, entries);
317 g_free(e);
320 void vm_state_notify(int running, RunState state)
322 VMChangeStateEntry *e, *next;
324 trace_vm_state_notify(running, state, RunState_str(state));
326 if (running) {
327 QTAILQ_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
328 e->cb(e->opaque, running, state);
330 } else {
331 QTAILQ_FOREACH_REVERSE_SAFE(e, &vm_change_state_head, entries, next) {
332 e->cb(e->opaque, running, state);
337 static ShutdownCause reset_requested;
338 static ShutdownCause shutdown_requested;
339 static int shutdown_signal;
340 static pid_t shutdown_pid;
341 static int powerdown_requested;
342 static int debug_requested;
343 static int suspend_requested;
344 static WakeupReason wakeup_reason;
345 static NotifierList powerdown_notifiers =
346 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
347 static NotifierList suspend_notifiers =
348 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
349 static NotifierList wakeup_notifiers =
350 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
351 static NotifierList shutdown_notifiers =
352 NOTIFIER_LIST_INITIALIZER(shutdown_notifiers);
353 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
355 ShutdownCause qemu_shutdown_requested_get(void)
357 return shutdown_requested;
360 ShutdownCause qemu_reset_requested_get(void)
362 return reset_requested;
365 static int qemu_shutdown_requested(void)
367 return qatomic_xchg(&shutdown_requested, SHUTDOWN_CAUSE_NONE);
370 static void qemu_kill_report(void)
372 if (!qtest_driver() && shutdown_signal) {
373 if (shutdown_pid == 0) {
374 /* This happens for eg ^C at the terminal, so it's worth
375 * avoiding printing an odd message in that case.
377 error_report("terminating on signal %d", shutdown_signal);
378 } else {
379 char *shutdown_cmd = qemu_get_pid_name(shutdown_pid);
381 error_report("terminating on signal %d from pid " FMT_pid " (%s)",
382 shutdown_signal, shutdown_pid,
383 shutdown_cmd ? shutdown_cmd : "<unknown process>");
384 g_free(shutdown_cmd);
386 shutdown_signal = 0;
390 static ShutdownCause qemu_reset_requested(void)
392 ShutdownCause r = reset_requested;
394 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
395 reset_requested = SHUTDOWN_CAUSE_NONE;
396 return r;
398 return SHUTDOWN_CAUSE_NONE;
401 static int qemu_suspend_requested(void)
403 int r = suspend_requested;
404 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
405 suspend_requested = 0;
406 return r;
408 return false;
411 static WakeupReason qemu_wakeup_requested(void)
413 return wakeup_reason;
416 static int qemu_powerdown_requested(void)
418 int r = powerdown_requested;
419 powerdown_requested = 0;
420 return r;
423 static int qemu_debug_requested(void)
425 int r = debug_requested;
426 debug_requested = 0;
427 return r;
431 * Reset the VM. Issue an event unless @reason is SHUTDOWN_CAUSE_NONE.
433 void qemu_system_reset(ShutdownCause reason)
435 MachineClass *mc;
437 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
439 cpu_synchronize_all_states();
441 if (mc && mc->reset) {
442 mc->reset(current_machine);
443 } else {
444 qemu_devices_reset();
446 if (reason && reason != SHUTDOWN_CAUSE_SUBSYSTEM_RESET) {
447 qapi_event_send_reset(shutdown_caused_by_guest(reason), reason);
449 cpu_synchronize_all_post_reset();
453 * Wake the VM after suspend.
455 static void qemu_system_wakeup(void)
457 MachineClass *mc;
459 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
461 if (mc && mc->wakeup) {
462 mc->wakeup(current_machine);
466 void qemu_system_guest_panicked(GuestPanicInformation *info)
468 qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed");
470 if (current_cpu) {
471 current_cpu->crash_occurred = true;
474 * TODO: Currently the available panic actions are: none, pause, and
475 * shutdown, but in principle debug and reset could be supported as well.
476 * Investigate any potential use cases for the unimplemented actions.
478 if (panic_action == PANIC_ACTION_PAUSE
479 || (panic_action == PANIC_ACTION_SHUTDOWN && shutdown_action == SHUTDOWN_ACTION_PAUSE)) {
480 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE,
481 !!info, info);
482 vm_stop(RUN_STATE_GUEST_PANICKED);
483 } else if (panic_action == PANIC_ACTION_SHUTDOWN) {
484 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF,
485 !!info, info);
486 vm_stop(RUN_STATE_GUEST_PANICKED);
487 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_PANIC);
488 } else {
489 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_RUN,
490 !!info, info);
493 if (info) {
494 if (info->type == GUEST_PANIC_INFORMATION_TYPE_HYPER_V) {
495 qemu_log_mask(LOG_GUEST_ERROR, "\nHV crash parameters: (%#"PRIx64
496 " %#"PRIx64" %#"PRIx64" %#"PRIx64" %#"PRIx64")\n",
497 info->u.hyper_v.arg1,
498 info->u.hyper_v.arg2,
499 info->u.hyper_v.arg3,
500 info->u.hyper_v.arg4,
501 info->u.hyper_v.arg5);
502 } else if (info->type == GUEST_PANIC_INFORMATION_TYPE_S390) {
503 qemu_log_mask(LOG_GUEST_ERROR, " on cpu %d: %s\n"
504 "PSW: 0x%016" PRIx64 " 0x%016" PRIx64"\n",
505 info->u.s390.core,
506 S390CrashReason_str(info->u.s390.reason),
507 info->u.s390.psw_mask,
508 info->u.s390.psw_addr);
510 qapi_free_GuestPanicInformation(info);
514 void qemu_system_guest_crashloaded(GuestPanicInformation *info)
516 qemu_log_mask(LOG_GUEST_ERROR, "Guest crash loaded");
518 qapi_event_send_guest_crashloaded(GUEST_PANIC_ACTION_RUN,
519 !!info, info);
521 if (info) {
522 qapi_free_GuestPanicInformation(info);
526 void qemu_system_reset_request(ShutdownCause reason)
528 if (reboot_action == REBOOT_ACTION_SHUTDOWN &&
529 reason != SHUTDOWN_CAUSE_SUBSYSTEM_RESET) {
530 shutdown_requested = reason;
531 } else {
532 reset_requested = reason;
534 cpu_stop_current();
535 qemu_notify_event();
538 static void qemu_system_suspend(void)
540 pause_all_vcpus();
541 notifier_list_notify(&suspend_notifiers, NULL);
542 runstate_set(RUN_STATE_SUSPENDED);
543 qapi_event_send_suspend();
546 void qemu_system_suspend_request(void)
548 if (runstate_check(RUN_STATE_SUSPENDED)) {
549 return;
551 suspend_requested = 1;
552 cpu_stop_current();
553 qemu_notify_event();
556 void qemu_register_suspend_notifier(Notifier *notifier)
558 notifier_list_add(&suspend_notifiers, notifier);
561 void qemu_system_wakeup_request(WakeupReason reason, Error **errp)
563 trace_system_wakeup_request(reason);
565 if (!runstate_check(RUN_STATE_SUSPENDED)) {
566 error_setg(errp,
567 "Unable to wake up: guest is not in suspended state");
568 return;
570 if (!(wakeup_reason_mask & (1 << reason))) {
571 return;
573 runstate_set(RUN_STATE_RUNNING);
574 wakeup_reason = reason;
575 qemu_notify_event();
578 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
580 if (enabled) {
581 wakeup_reason_mask |= (1 << reason);
582 } else {
583 wakeup_reason_mask &= ~(1 << reason);
587 void qemu_register_wakeup_notifier(Notifier *notifier)
589 notifier_list_add(&wakeup_notifiers, notifier);
592 static bool wakeup_suspend_enabled;
594 void qemu_register_wakeup_support(void)
596 wakeup_suspend_enabled = true;
599 bool qemu_wakeup_suspend_enabled(void)
601 return wakeup_suspend_enabled;
604 void qemu_system_killed(int signal, pid_t pid)
606 shutdown_signal = signal;
607 shutdown_pid = pid;
608 shutdown_action = SHUTDOWN_ACTION_POWEROFF;
610 /* Cannot call qemu_system_shutdown_request directly because
611 * we are in a signal handler.
613 shutdown_requested = SHUTDOWN_CAUSE_HOST_SIGNAL;
614 qemu_notify_event();
617 void qemu_system_shutdown_request(ShutdownCause reason)
619 trace_qemu_system_shutdown_request(reason);
620 replay_shutdown_request(reason);
621 shutdown_requested = reason;
622 qemu_notify_event();
625 static void qemu_system_powerdown(void)
627 qapi_event_send_powerdown();
628 notifier_list_notify(&powerdown_notifiers, NULL);
631 static void qemu_system_shutdown(ShutdownCause cause)
633 qapi_event_send_shutdown(shutdown_caused_by_guest(cause), cause);
634 notifier_list_notify(&shutdown_notifiers, &cause);
637 void qemu_system_powerdown_request(void)
639 trace_qemu_system_powerdown_request();
640 powerdown_requested = 1;
641 qemu_notify_event();
644 void qemu_register_powerdown_notifier(Notifier *notifier)
646 notifier_list_add(&powerdown_notifiers, notifier);
649 void qemu_register_shutdown_notifier(Notifier *notifier)
651 notifier_list_add(&shutdown_notifiers, notifier);
654 void qemu_system_debug_request(void)
656 debug_requested = 1;
657 qemu_notify_event();
660 static bool main_loop_should_exit(void)
662 RunState r;
663 ShutdownCause request;
665 if (qemu_debug_requested()) {
666 vm_stop(RUN_STATE_DEBUG);
668 if (qemu_suspend_requested()) {
669 qemu_system_suspend();
671 request = qemu_shutdown_requested();
672 if (request) {
673 qemu_kill_report();
674 qemu_system_shutdown(request);
675 if (shutdown_action == SHUTDOWN_ACTION_PAUSE) {
676 vm_stop(RUN_STATE_SHUTDOWN);
677 } else {
678 return true;
681 request = qemu_reset_requested();
682 if (request) {
683 pause_all_vcpus();
684 qemu_system_reset(request);
685 resume_all_vcpus();
687 * runstate can change in pause_all_vcpus()
688 * as iothread mutex is unlocked
690 if (!runstate_check(RUN_STATE_RUNNING) &&
691 !runstate_check(RUN_STATE_INMIGRATE) &&
692 !runstate_check(RUN_STATE_FINISH_MIGRATE)) {
693 runstate_set(RUN_STATE_PRELAUNCH);
696 if (qemu_wakeup_requested()) {
697 pause_all_vcpus();
698 qemu_system_wakeup();
699 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
700 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
701 resume_all_vcpus();
702 qapi_event_send_wakeup();
704 if (qemu_powerdown_requested()) {
705 qemu_system_powerdown();
707 if (qemu_vmstop_requested(&r)) {
708 vm_stop(r);
710 return false;
713 void qemu_main_loop(void)
715 #ifdef CONFIG_PROFILER
716 int64_t ti;
717 #endif
718 while (!main_loop_should_exit()) {
719 #ifdef CONFIG_PROFILER
720 ti = profile_getclock();
721 #endif
722 main_loop_wait(false);
723 #ifdef CONFIG_PROFILER
724 dev_time += profile_getclock() - ti;
725 #endif
729 void qemu_add_exit_notifier(Notifier *notify)
731 notifier_list_add(&exit_notifiers, notify);
734 void qemu_remove_exit_notifier(Notifier *notify)
736 notifier_remove(notify);
739 static void qemu_run_exit_notifiers(void)
741 notifier_list_notify(&exit_notifiers, NULL);
744 void qemu_init_subsystems(void)
746 Error *err;
748 os_set_line_buffering();
750 module_call_init(MODULE_INIT_TRACE);
752 qemu_init_cpu_list();
753 qemu_init_cpu_loop();
754 qemu_mutex_lock_iothread();
756 atexit(qemu_run_exit_notifiers);
758 module_call_init(MODULE_INIT_QOM);
759 module_call_init(MODULE_INIT_MIGRATION);
761 runstate_init();
762 precopy_infrastructure_init();
763 postcopy_infrastructure_init();
764 monitor_init_globals();
766 if (qcrypto_init(&err) < 0) {
767 error_reportf_err(err, "cannot initialize crypto: ");
768 exit(1);
771 os_setup_early_signal_handling();
773 bdrv_init_with_whitelist();
774 socket_init();
778 void qemu_cleanup(void)
780 gdb_exit(0);
783 * cleaning up the migration object cancels any existing migration
784 * try to do this early so that it also stops using devices.
786 migration_shutdown();
789 * Close the exports before draining the block layer. The export
790 * drivers may have coroutines yielding on it, so we need to clean
791 * them up before the drain, as otherwise they may be get stuck in
792 * blk_wait_while_drained().
794 blk_exp_close_all();
797 * We must cancel all block jobs while the block layer is drained,
798 * or cancelling will be affected by throttling and thus may block
799 * for an extended period of time.
800 * vm_shutdown() will bdrv_drain_all(), so we may as well include
801 * it in the drained section.
802 * We do not need to end this section, because we do not want any
803 * requests happening from here on anyway.
805 bdrv_drain_all_begin();
807 /* No more vcpu or device emulation activity beyond this point */
808 vm_shutdown();
809 replay_finish();
811 job_cancel_sync_all();
812 bdrv_close_all();
814 /* vhost-user must be cleaned up before chardevs. */
815 tpm_cleanup();
816 net_cleanup();
817 audio_cleanup();
818 monitor_cleanup();
819 qemu_chr_cleanup();
820 user_creatable_cleanup();
821 /* TODO: unref root container, check all devices are ok */