accel/tcg: Remove cpu_atomic_{ld,st}o_*_mmu
[qemu/kevin.git] / softmmu / runstate.c
blob2f2396c819e49ff29f9799b563ed9a423aa223f4
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 "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"
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;
73 typedef struct {
74 RunState from;
75 RunState to;
76 } RunStateTransition;
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_PRELAUNCH },
131 { RUN_STATE_COLO, RUN_STATE_SHUTDOWN},
133 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
134 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
135 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
136 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
137 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
138 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
139 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
140 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
141 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
142 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
143 { RUN_STATE_RUNNING, RUN_STATE_COLO},
145 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
147 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
148 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
149 { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH },
150 { RUN_STATE_SHUTDOWN, RUN_STATE_COLO },
152 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
153 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
154 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
155 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
156 { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
157 { RUN_STATE_SUSPENDED, RUN_STATE_COLO},
159 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
160 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
161 { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH },
162 { RUN_STATE_WATCHDOG, RUN_STATE_COLO},
164 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
165 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
166 { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH },
168 { RUN_STATE__MAX, RUN_STATE__MAX },
171 static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
173 bool runstate_check(RunState state)
175 return current_run_state == state;
178 bool runstate_store(char *str, size_t size)
180 const char *state = RunState_str(current_run_state);
181 size_t len = strlen(state) + 1;
183 if (len > size) {
184 return false;
186 memcpy(str, state, len);
187 return true;
190 static void runstate_init(void)
192 const RunStateTransition *p;
194 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
195 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE__MAX; p++) {
196 runstate_valid_transitions[p->from][p->to] = true;
199 qemu_mutex_init(&vmstop_lock);
202 /* This function will abort() on invalid state transitions */
203 void runstate_set(RunState new_state)
205 assert(new_state < RUN_STATE__MAX);
207 trace_runstate_set(current_run_state, RunState_str(current_run_state),
208 new_state, RunState_str(new_state));
210 if (current_run_state == new_state) {
211 return;
214 if (!runstate_valid_transitions[current_run_state][new_state]) {
215 error_report("invalid runstate transition: '%s' -> '%s'",
216 RunState_str(current_run_state),
217 RunState_str(new_state));
218 abort();
221 current_run_state = new_state;
224 bool runstate_is_running(void)
226 return runstate_check(RUN_STATE_RUNNING);
229 bool runstate_needs_reset(void)
231 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
232 runstate_check(RUN_STATE_SHUTDOWN);
235 StatusInfo *qmp_query_status(Error **errp)
237 StatusInfo *info = g_malloc0(sizeof(*info));
238 AccelState *accel = current_accel();
241 * We ignore errors, which will happen if the accelerator
242 * is not TCG. "singlestep" is meaningless for other accelerators,
243 * so we will set the StatusInfo field to false for those.
245 info->singlestep = object_property_get_bool(OBJECT(accel),
246 "one-insn-per-tb", NULL);
247 info->running = runstate_is_running();
248 info->status = current_run_state;
250 return info;
253 bool qemu_vmstop_requested(RunState *r)
255 qemu_mutex_lock(&vmstop_lock);
256 *r = vmstop_requested;
257 vmstop_requested = RUN_STATE__MAX;
258 qemu_mutex_unlock(&vmstop_lock);
259 return *r < RUN_STATE__MAX;
262 void qemu_system_vmstop_request_prepare(void)
264 qemu_mutex_lock(&vmstop_lock);
267 void qemu_system_vmstop_request(RunState state)
269 vmstop_requested = state;
270 qemu_mutex_unlock(&vmstop_lock);
271 qemu_notify_event();
273 struct VMChangeStateEntry {
274 VMChangeStateHandler *cb;
275 void *opaque;
276 QTAILQ_ENTRY(VMChangeStateEntry) entries;
277 int priority;
280 static QTAILQ_HEAD(, VMChangeStateEntry) vm_change_state_head =
281 QTAILQ_HEAD_INITIALIZER(vm_change_state_head);
284 * qemu_add_vm_change_state_handler_prio:
285 * @cb: the callback to invoke
286 * @opaque: user data passed to the callback
287 * @priority: low priorities execute first when the vm runs and the reverse is
288 * true when the vm stops
290 * Register a callback function that is invoked when the vm starts or stops
291 * running.
293 * Returns: an entry to be freed using qemu_del_vm_change_state_handler()
295 VMChangeStateEntry *qemu_add_vm_change_state_handler_prio(
296 VMChangeStateHandler *cb, void *opaque, int priority)
298 VMChangeStateEntry *e;
299 VMChangeStateEntry *other;
301 e = g_malloc0(sizeof(*e));
302 e->cb = cb;
303 e->opaque = opaque;
304 e->priority = priority;
306 /* Keep list sorted in ascending priority order */
307 QTAILQ_FOREACH(other, &vm_change_state_head, entries) {
308 if (priority < other->priority) {
309 QTAILQ_INSERT_BEFORE(other, e, entries);
310 return e;
314 QTAILQ_INSERT_TAIL(&vm_change_state_head, e, entries);
315 return e;
318 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
319 void *opaque)
321 return qemu_add_vm_change_state_handler_prio(cb, opaque, 0);
324 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
326 QTAILQ_REMOVE(&vm_change_state_head, e, entries);
327 g_free(e);
330 void vm_state_notify(bool running, RunState state)
332 VMChangeStateEntry *e, *next;
334 trace_vm_state_notify(running, state, RunState_str(state));
336 if (running) {
337 QTAILQ_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
338 e->cb(e->opaque, running, state);
340 } else {
341 QTAILQ_FOREACH_REVERSE_SAFE(e, &vm_change_state_head, entries, next) {
342 e->cb(e->opaque, running, state);
347 static ShutdownCause reset_requested;
348 static ShutdownCause shutdown_requested;
349 static int shutdown_signal;
350 static pid_t shutdown_pid;
351 static int powerdown_requested;
352 static int debug_requested;
353 static int suspend_requested;
354 static WakeupReason wakeup_reason;
355 static NotifierList powerdown_notifiers =
356 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
357 static NotifierList suspend_notifiers =
358 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
359 static NotifierList wakeup_notifiers =
360 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
361 static NotifierList shutdown_notifiers =
362 NOTIFIER_LIST_INITIALIZER(shutdown_notifiers);
363 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
365 ShutdownCause qemu_shutdown_requested_get(void)
367 return shutdown_requested;
370 ShutdownCause qemu_reset_requested_get(void)
372 return reset_requested;
375 static int qemu_shutdown_requested(void)
377 return qatomic_xchg(&shutdown_requested, SHUTDOWN_CAUSE_NONE);
380 static void qemu_kill_report(void)
382 if (!qtest_driver() && shutdown_signal) {
383 if (shutdown_pid == 0) {
384 /* This happens for eg ^C at the terminal, so it's worth
385 * avoiding printing an odd message in that case.
387 error_report("terminating on signal %d", shutdown_signal);
388 } else {
389 char *shutdown_cmd = qemu_get_pid_name(shutdown_pid);
391 error_report("terminating on signal %d from pid " FMT_pid " (%s)",
392 shutdown_signal, shutdown_pid,
393 shutdown_cmd ? shutdown_cmd : "<unknown process>");
394 g_free(shutdown_cmd);
396 shutdown_signal = 0;
400 static ShutdownCause qemu_reset_requested(void)
402 ShutdownCause r = reset_requested;
404 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
405 reset_requested = SHUTDOWN_CAUSE_NONE;
406 return r;
408 return SHUTDOWN_CAUSE_NONE;
411 static int qemu_suspend_requested(void)
413 int r = suspend_requested;
414 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
415 suspend_requested = 0;
416 return r;
418 return false;
421 static WakeupReason qemu_wakeup_requested(void)
423 return wakeup_reason;
426 static int qemu_powerdown_requested(void)
428 int r = powerdown_requested;
429 powerdown_requested = 0;
430 return r;
433 static int qemu_debug_requested(void)
435 int r = debug_requested;
436 debug_requested = 0;
437 return r;
441 * Reset the VM. Issue an event unless @reason is SHUTDOWN_CAUSE_NONE.
443 void qemu_system_reset(ShutdownCause reason)
445 MachineClass *mc;
447 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
449 cpu_synchronize_all_states();
451 if (mc && mc->reset) {
452 mc->reset(current_machine, reason);
453 } else {
454 qemu_devices_reset(reason);
456 switch (reason) {
457 case SHUTDOWN_CAUSE_NONE:
458 case SHUTDOWN_CAUSE_SUBSYSTEM_RESET:
459 case SHUTDOWN_CAUSE_SNAPSHOT_LOAD:
460 break;
461 default:
462 qapi_event_send_reset(shutdown_caused_by_guest(reason), reason);
464 cpu_synchronize_all_post_reset();
468 * Wake the VM after suspend.
470 static void qemu_system_wakeup(void)
472 MachineClass *mc;
474 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
476 if (mc && mc->wakeup) {
477 mc->wakeup(current_machine);
481 void qemu_system_guest_panicked(GuestPanicInformation *info)
483 qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed");
485 if (current_cpu) {
486 current_cpu->crash_occurred = true;
489 * TODO: Currently the available panic actions are: none, pause, and
490 * shutdown, but in principle debug and reset could be supported as well.
491 * Investigate any potential use cases for the unimplemented actions.
493 if (panic_action == PANIC_ACTION_PAUSE
494 || (panic_action == PANIC_ACTION_SHUTDOWN && shutdown_action == SHUTDOWN_ACTION_PAUSE)) {
495 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE, info);
496 vm_stop(RUN_STATE_GUEST_PANICKED);
497 } else if (panic_action == PANIC_ACTION_SHUTDOWN ||
498 panic_action == PANIC_ACTION_EXIT_FAILURE) {
499 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF, info);
500 vm_stop(RUN_STATE_GUEST_PANICKED);
501 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_PANIC);
502 } else {
503 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_RUN, info);
506 if (info) {
507 if (info->type == GUEST_PANIC_INFORMATION_TYPE_HYPER_V) {
508 qemu_log_mask(LOG_GUEST_ERROR, "\nHV crash parameters: (%#"PRIx64
509 " %#"PRIx64" %#"PRIx64" %#"PRIx64" %#"PRIx64")\n",
510 info->u.hyper_v.arg1,
511 info->u.hyper_v.arg2,
512 info->u.hyper_v.arg3,
513 info->u.hyper_v.arg4,
514 info->u.hyper_v.arg5);
515 } else if (info->type == GUEST_PANIC_INFORMATION_TYPE_S390) {
516 qemu_log_mask(LOG_GUEST_ERROR, " on cpu %d: %s\n"
517 "PSW: 0x%016" PRIx64 " 0x%016" PRIx64"\n",
518 info->u.s390.core,
519 S390CrashReason_str(info->u.s390.reason),
520 info->u.s390.psw_mask,
521 info->u.s390.psw_addr);
523 qapi_free_GuestPanicInformation(info);
527 void qemu_system_guest_crashloaded(GuestPanicInformation *info)
529 qemu_log_mask(LOG_GUEST_ERROR, "Guest crash loaded");
530 qapi_event_send_guest_crashloaded(GUEST_PANIC_ACTION_RUN, info);
531 qapi_free_GuestPanicInformation(info);
534 void qemu_system_reset_request(ShutdownCause reason)
536 if (reboot_action == REBOOT_ACTION_SHUTDOWN &&
537 reason != SHUTDOWN_CAUSE_SUBSYSTEM_RESET) {
538 shutdown_requested = reason;
539 } else if (!cpus_are_resettable()) {
540 error_report("cpus are not resettable, terminating");
541 shutdown_requested = reason;
542 } else {
543 reset_requested = reason;
545 cpu_stop_current();
546 qemu_notify_event();
549 static void qemu_system_suspend(void)
551 pause_all_vcpus();
552 notifier_list_notify(&suspend_notifiers, NULL);
553 runstate_set(RUN_STATE_SUSPENDED);
554 qapi_event_send_suspend();
557 void qemu_system_suspend_request(void)
559 if (runstate_check(RUN_STATE_SUSPENDED)) {
560 return;
562 suspend_requested = 1;
563 cpu_stop_current();
564 qemu_notify_event();
567 void qemu_register_suspend_notifier(Notifier *notifier)
569 notifier_list_add(&suspend_notifiers, notifier);
572 void qemu_system_wakeup_request(WakeupReason reason, Error **errp)
574 trace_system_wakeup_request(reason);
576 if (!runstate_check(RUN_STATE_SUSPENDED)) {
577 error_setg(errp,
578 "Unable to wake up: guest is not in suspended state");
579 return;
581 if (!(wakeup_reason_mask & (1 << reason))) {
582 return;
584 runstate_set(RUN_STATE_RUNNING);
585 wakeup_reason = reason;
586 qemu_notify_event();
589 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
591 if (enabled) {
592 wakeup_reason_mask |= (1 << reason);
593 } else {
594 wakeup_reason_mask &= ~(1 << reason);
598 void qemu_register_wakeup_notifier(Notifier *notifier)
600 notifier_list_add(&wakeup_notifiers, notifier);
603 static bool wakeup_suspend_enabled;
605 void qemu_register_wakeup_support(void)
607 wakeup_suspend_enabled = true;
610 bool qemu_wakeup_suspend_enabled(void)
612 return wakeup_suspend_enabled;
615 void qemu_system_killed(int signal, pid_t pid)
617 shutdown_signal = signal;
618 shutdown_pid = pid;
619 shutdown_action = SHUTDOWN_ACTION_POWEROFF;
621 /* Cannot call qemu_system_shutdown_request directly because
622 * we are in a signal handler.
624 shutdown_requested = SHUTDOWN_CAUSE_HOST_SIGNAL;
625 qemu_notify_event();
628 void qemu_system_shutdown_request(ShutdownCause reason)
630 trace_qemu_system_shutdown_request(reason);
631 replay_shutdown_request(reason);
632 shutdown_requested = reason;
633 qemu_notify_event();
636 static void qemu_system_powerdown(void)
638 qapi_event_send_powerdown();
639 notifier_list_notify(&powerdown_notifiers, NULL);
642 static void qemu_system_shutdown(ShutdownCause cause)
644 qapi_event_send_shutdown(shutdown_caused_by_guest(cause), cause);
645 notifier_list_notify(&shutdown_notifiers, &cause);
648 void qemu_system_powerdown_request(void)
650 trace_qemu_system_powerdown_request();
651 powerdown_requested = 1;
652 qemu_notify_event();
655 void qemu_register_powerdown_notifier(Notifier *notifier)
657 notifier_list_add(&powerdown_notifiers, notifier);
660 void qemu_register_shutdown_notifier(Notifier *notifier)
662 notifier_list_add(&shutdown_notifiers, notifier);
665 void qemu_system_debug_request(void)
667 debug_requested = 1;
668 qemu_notify_event();
671 static bool main_loop_should_exit(int *status)
673 RunState r;
674 ShutdownCause request;
676 if (qemu_debug_requested()) {
677 vm_stop(RUN_STATE_DEBUG);
679 if (qemu_suspend_requested()) {
680 qemu_system_suspend();
682 request = qemu_shutdown_requested();
683 if (request) {
684 qemu_kill_report();
685 qemu_system_shutdown(request);
686 if (shutdown_action == SHUTDOWN_ACTION_PAUSE) {
687 vm_stop(RUN_STATE_SHUTDOWN);
688 } else {
689 if (request == SHUTDOWN_CAUSE_GUEST_PANIC &&
690 panic_action == PANIC_ACTION_EXIT_FAILURE) {
691 *status = EXIT_FAILURE;
693 return true;
696 request = qemu_reset_requested();
697 if (request) {
698 pause_all_vcpus();
699 qemu_system_reset(request);
700 resume_all_vcpus();
702 * runstate can change in pause_all_vcpus()
703 * as iothread mutex is unlocked
705 if (!runstate_check(RUN_STATE_RUNNING) &&
706 !runstate_check(RUN_STATE_INMIGRATE) &&
707 !runstate_check(RUN_STATE_FINISH_MIGRATE)) {
708 runstate_set(RUN_STATE_PRELAUNCH);
711 if (qemu_wakeup_requested()) {
712 pause_all_vcpus();
713 qemu_system_wakeup();
714 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
715 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
716 resume_all_vcpus();
717 qapi_event_send_wakeup();
719 if (qemu_powerdown_requested()) {
720 qemu_system_powerdown();
722 if (qemu_vmstop_requested(&r)) {
723 vm_stop(r);
725 return false;
728 int qemu_main_loop(void)
730 int status = EXIT_SUCCESS;
731 #ifdef CONFIG_PROFILER
732 int64_t ti;
733 #endif
735 while (!main_loop_should_exit(&status)) {
736 #ifdef CONFIG_PROFILER
737 ti = profile_getclock();
738 #endif
739 main_loop_wait(false);
740 #ifdef CONFIG_PROFILER
741 dev_time += profile_getclock() - ti;
742 #endif
745 return status;
748 void qemu_add_exit_notifier(Notifier *notify)
750 notifier_list_add(&exit_notifiers, notify);
753 void qemu_remove_exit_notifier(Notifier *notify)
755 notifier_remove(notify);
758 static void qemu_run_exit_notifiers(void)
760 notifier_list_notify(&exit_notifiers, NULL);
763 void qemu_init_subsystems(void)
765 Error *err = NULL;
767 os_set_line_buffering();
769 module_call_init(MODULE_INIT_TRACE);
771 qemu_init_cpu_list();
772 qemu_init_cpu_loop();
773 qemu_mutex_lock_iothread();
775 atexit(qemu_run_exit_notifiers);
777 module_call_init(MODULE_INIT_QOM);
778 module_call_init(MODULE_INIT_MIGRATION);
780 runstate_init();
781 precopy_infrastructure_init();
782 postcopy_infrastructure_init();
783 monitor_init_globals();
785 if (qcrypto_init(&err) < 0) {
786 error_reportf_err(err, "cannot initialize crypto: ");
787 exit(1);
790 os_setup_early_signal_handling();
792 bdrv_init_with_whitelist();
793 socket_init();
797 void qemu_cleanup(void)
799 gdb_exit(0);
802 * cleaning up the migration object cancels any existing migration
803 * try to do this early so that it also stops using devices.
805 migration_shutdown();
808 * Close the exports before draining the block layer. The export
809 * drivers may have coroutines yielding on it, so we need to clean
810 * them up before the drain, as otherwise they may be get stuck in
811 * blk_wait_while_drained().
813 blk_exp_close_all();
816 * We must cancel all block jobs while the block layer is drained,
817 * or cancelling will be affected by throttling and thus may block
818 * for an extended period of time.
819 * vm_shutdown() will bdrv_drain_all(), so we may as well include
820 * it in the drained section.
821 * We do not need to end this section, because we do not want any
822 * requests happening from here on anyway.
824 bdrv_drain_all_begin();
826 /* No more vcpu or device emulation activity beyond this point */
827 vm_shutdown();
828 replay_finish();
830 job_cancel_sync_all();
831 bdrv_close_all();
833 /* vhost-user must be cleaned up before chardevs. */
834 tpm_cleanup();
835 net_cleanup();
836 audio_cleanup();
837 monitor_cleanup();
838 qemu_chr_cleanup();
839 user_creatable_cleanup();
840 /* TODO: unref root container, check all devices are ok */