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 "gdbstub/syscalls.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/accel.h"
44 #include "qemu/error-report.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"
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
},
125 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_INTERNAL_ERROR
},
126 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_IO_ERROR
},
127 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_SHUTDOWN
},
128 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_SUSPENDED
},
129 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_WATCHDOG
},
130 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_GUEST_PANICKED
},
132 { RUN_STATE_RESTORE_VM
, RUN_STATE_RUNNING
},
133 { RUN_STATE_RESTORE_VM
, RUN_STATE_PRELAUNCH
},
135 { RUN_STATE_COLO
, RUN_STATE_RUNNING
},
136 { RUN_STATE_COLO
, RUN_STATE_PRELAUNCH
},
137 { RUN_STATE_COLO
, RUN_STATE_SHUTDOWN
},
139 { RUN_STATE_RUNNING
, RUN_STATE_DEBUG
},
140 { RUN_STATE_RUNNING
, RUN_STATE_INTERNAL_ERROR
},
141 { RUN_STATE_RUNNING
, RUN_STATE_IO_ERROR
},
142 { RUN_STATE_RUNNING
, RUN_STATE_PAUSED
},
143 { RUN_STATE_RUNNING
, RUN_STATE_FINISH_MIGRATE
},
144 { RUN_STATE_RUNNING
, RUN_STATE_RESTORE_VM
},
145 { RUN_STATE_RUNNING
, RUN_STATE_SAVE_VM
},
146 { RUN_STATE_RUNNING
, RUN_STATE_SHUTDOWN
},
147 { RUN_STATE_RUNNING
, RUN_STATE_WATCHDOG
},
148 { RUN_STATE_RUNNING
, RUN_STATE_GUEST_PANICKED
},
149 { RUN_STATE_RUNNING
, RUN_STATE_COLO
},
151 { RUN_STATE_SAVE_VM
, RUN_STATE_RUNNING
},
153 { RUN_STATE_SHUTDOWN
, RUN_STATE_PAUSED
},
154 { RUN_STATE_SHUTDOWN
, RUN_STATE_FINISH_MIGRATE
},
155 { RUN_STATE_SHUTDOWN
, RUN_STATE_PRELAUNCH
},
156 { RUN_STATE_SHUTDOWN
, RUN_STATE_COLO
},
158 { RUN_STATE_DEBUG
, RUN_STATE_SUSPENDED
},
159 { RUN_STATE_RUNNING
, RUN_STATE_SUSPENDED
},
160 { RUN_STATE_SUSPENDED
, RUN_STATE_RUNNING
},
161 { RUN_STATE_SUSPENDED
, RUN_STATE_FINISH_MIGRATE
},
162 { RUN_STATE_SUSPENDED
, RUN_STATE_PRELAUNCH
},
163 { RUN_STATE_SUSPENDED
, RUN_STATE_COLO
},
165 { RUN_STATE_WATCHDOG
, RUN_STATE_RUNNING
},
166 { RUN_STATE_WATCHDOG
, RUN_STATE_FINISH_MIGRATE
},
167 { RUN_STATE_WATCHDOG
, RUN_STATE_PRELAUNCH
},
168 { RUN_STATE_WATCHDOG
, RUN_STATE_COLO
},
170 { RUN_STATE_GUEST_PANICKED
, RUN_STATE_RUNNING
},
171 { RUN_STATE_GUEST_PANICKED
, RUN_STATE_FINISH_MIGRATE
},
172 { RUN_STATE_GUEST_PANICKED
, RUN_STATE_PRELAUNCH
},
174 { RUN_STATE__MAX
, RUN_STATE__MAX
},
177 static bool runstate_valid_transitions
[RUN_STATE__MAX
][RUN_STATE__MAX
];
179 bool runstate_check(RunState state
)
181 return current_run_state
== state
;
184 static void runstate_init(void)
186 const RunStateTransition
*p
;
188 memset(&runstate_valid_transitions
, 0, sizeof(runstate_valid_transitions
));
189 for (p
= &runstate_transitions_def
[0]; p
->from
!= RUN_STATE__MAX
; p
++) {
190 runstate_valid_transitions
[p
->from
][p
->to
] = true;
193 qemu_mutex_init(&vmstop_lock
);
196 /* This function will abort() on invalid state transitions */
197 void runstate_set(RunState new_state
)
199 assert(new_state
< RUN_STATE__MAX
);
201 trace_runstate_set(current_run_state
, RunState_str(current_run_state
),
202 new_state
, RunState_str(new_state
));
204 if (current_run_state
== new_state
) {
208 if (!runstate_valid_transitions
[current_run_state
][new_state
]) {
209 error_report("invalid runstate transition: '%s' -> '%s'",
210 RunState_str(current_run_state
),
211 RunState_str(new_state
));
215 current_run_state
= new_state
;
218 RunState
runstate_get(void)
220 return current_run_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
));
237 AccelState
*accel
= current_accel();
240 * We ignore errors, which will happen if the accelerator
241 * is not TCG. "singlestep" is meaningless for other accelerators,
242 * so we will set the StatusInfo field to false for those.
244 info
->singlestep
= object_property_get_bool(OBJECT(accel
),
245 "one-insn-per-tb", NULL
);
246 info
->running
= runstate_is_running();
247 info
->status
= current_run_state
;
252 bool qemu_vmstop_requested(RunState
*r
)
254 qemu_mutex_lock(&vmstop_lock
);
255 *r
= vmstop_requested
;
256 vmstop_requested
= RUN_STATE__MAX
;
257 qemu_mutex_unlock(&vmstop_lock
);
258 return *r
< RUN_STATE__MAX
;
261 void qemu_system_vmstop_request_prepare(void)
263 qemu_mutex_lock(&vmstop_lock
);
266 void qemu_system_vmstop_request(RunState state
)
268 vmstop_requested
= state
;
269 qemu_mutex_unlock(&vmstop_lock
);
272 struct VMChangeStateEntry
{
273 VMChangeStateHandler
*cb
;
274 VMChangeStateHandler
*prepare_cb
;
276 QTAILQ_ENTRY(VMChangeStateEntry
) entries
;
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
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 return qemu_add_vm_change_state_handler_prio_full(cb
, NULL
, opaque
,
303 * qemu_add_vm_change_state_handler_prio_full:
304 * @cb: the main callback to invoke
305 * @prepare_cb: a callback to invoke before the main callback
306 * @opaque: user data passed to the callbacks
307 * @priority: low priorities execute first when the vm runs and the reverse is
308 * true when the vm stops
310 * Register a main callback function and an optional prepare callback function
311 * that are invoked when the vm starts or stops running. The main callback and
312 * the prepare callback are called in two separate phases: First all prepare
313 * callbacks are called and only then all main callbacks are called. As its
314 * name suggests, the prepare callback can be used to do some preparatory work
315 * before invoking the main callback.
317 * Returns: an entry to be freed using qemu_del_vm_change_state_handler()
320 qemu_add_vm_change_state_handler_prio_full(VMChangeStateHandler
*cb
,
321 VMChangeStateHandler
*prepare_cb
,
322 void *opaque
, int priority
)
324 VMChangeStateEntry
*e
;
325 VMChangeStateEntry
*other
;
327 e
= g_malloc0(sizeof(*e
));
329 e
->prepare_cb
= prepare_cb
;
331 e
->priority
= priority
;
333 /* Keep list sorted in ascending priority order */
334 QTAILQ_FOREACH(other
, &vm_change_state_head
, entries
) {
335 if (priority
< other
->priority
) {
336 QTAILQ_INSERT_BEFORE(other
, e
, entries
);
341 QTAILQ_INSERT_TAIL(&vm_change_state_head
, e
, entries
);
345 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
348 return qemu_add_vm_change_state_handler_prio(cb
, opaque
, 0);
351 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
353 QTAILQ_REMOVE(&vm_change_state_head
, e
, entries
);
357 void vm_state_notify(bool running
, RunState state
)
359 VMChangeStateEntry
*e
, *next
;
361 trace_vm_state_notify(running
, state
, RunState_str(state
));
364 QTAILQ_FOREACH_SAFE(e
, &vm_change_state_head
, entries
, next
) {
366 e
->prepare_cb(e
->opaque
, running
, state
);
370 QTAILQ_FOREACH_SAFE(e
, &vm_change_state_head
, entries
, next
) {
371 e
->cb(e
->opaque
, running
, state
);
374 QTAILQ_FOREACH_REVERSE_SAFE(e
, &vm_change_state_head
, entries
, next
) {
376 e
->prepare_cb(e
->opaque
, running
, state
);
380 QTAILQ_FOREACH_REVERSE_SAFE(e
, &vm_change_state_head
, entries
, next
) {
381 e
->cb(e
->opaque
, running
, state
);
386 static ShutdownCause reset_requested
;
387 static ShutdownCause shutdown_requested
;
388 static int shutdown_exit_code
= EXIT_SUCCESS
;
389 static int shutdown_signal
;
390 static pid_t shutdown_pid
;
391 static int powerdown_requested
;
392 static int debug_requested
;
393 static int suspend_requested
;
394 static WakeupReason wakeup_reason
;
395 static NotifierList powerdown_notifiers
=
396 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers
);
397 static NotifierList suspend_notifiers
=
398 NOTIFIER_LIST_INITIALIZER(suspend_notifiers
);
399 static NotifierList wakeup_notifiers
=
400 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers
);
401 static NotifierList shutdown_notifiers
=
402 NOTIFIER_LIST_INITIALIZER(shutdown_notifiers
);
403 static uint32_t wakeup_reason_mask
= ~(1 << QEMU_WAKEUP_REASON_NONE
);
405 ShutdownCause
qemu_shutdown_requested_get(void)
407 return shutdown_requested
;
410 ShutdownCause
qemu_reset_requested_get(void)
412 return reset_requested
;
415 static int qemu_shutdown_requested(void)
417 return qatomic_xchg(&shutdown_requested
, SHUTDOWN_CAUSE_NONE
);
420 static void qemu_kill_report(void)
422 if (!qtest_driver() && shutdown_signal
) {
423 if (shutdown_pid
== 0) {
424 /* This happens for eg ^C at the terminal, so it's worth
425 * avoiding printing an odd message in that case.
427 error_report("terminating on signal %d", shutdown_signal
);
429 char *shutdown_cmd
= qemu_get_pid_name(shutdown_pid
);
431 error_report("terminating on signal %d from pid " FMT_pid
" (%s)",
432 shutdown_signal
, shutdown_pid
,
433 shutdown_cmd
? shutdown_cmd
: "<unknown process>");
434 g_free(shutdown_cmd
);
440 static ShutdownCause
qemu_reset_requested(void)
442 ShutdownCause r
= reset_requested
;
444 if (r
&& replay_checkpoint(CHECKPOINT_RESET_REQUESTED
)) {
445 reset_requested
= SHUTDOWN_CAUSE_NONE
;
448 return SHUTDOWN_CAUSE_NONE
;
451 static int qemu_suspend_requested(void)
453 int r
= suspend_requested
;
454 if (r
&& replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED
)) {
455 suspend_requested
= 0;
461 static WakeupReason
qemu_wakeup_requested(void)
463 return wakeup_reason
;
466 static int qemu_powerdown_requested(void)
468 int r
= powerdown_requested
;
469 powerdown_requested
= 0;
473 static int qemu_debug_requested(void)
475 int r
= debug_requested
;
481 * Reset the VM. Issue an event unless @reason is SHUTDOWN_CAUSE_NONE.
483 void qemu_system_reset(ShutdownCause reason
)
487 mc
= current_machine
? MACHINE_GET_CLASS(current_machine
) : NULL
;
489 cpu_synchronize_all_states();
491 if (mc
&& mc
->reset
) {
492 mc
->reset(current_machine
, reason
);
494 qemu_devices_reset(reason
);
497 case SHUTDOWN_CAUSE_NONE
:
498 case SHUTDOWN_CAUSE_SUBSYSTEM_RESET
:
499 case SHUTDOWN_CAUSE_SNAPSHOT_LOAD
:
502 qapi_event_send_reset(shutdown_caused_by_guest(reason
), reason
);
504 cpu_synchronize_all_post_reset();
508 * Wake the VM after suspend.
510 static void qemu_system_wakeup(void)
514 mc
= current_machine
? MACHINE_GET_CLASS(current_machine
) : NULL
;
516 if (mc
&& mc
->wakeup
) {
517 mc
->wakeup(current_machine
);
521 void qemu_system_guest_panicked(GuestPanicInformation
*info
)
523 qemu_log_mask(LOG_GUEST_ERROR
, "Guest crashed");
526 current_cpu
->crash_occurred
= true;
529 * TODO: Currently the available panic actions are: none, pause, and
530 * shutdown, but in principle debug and reset could be supported as well.
531 * Investigate any potential use cases for the unimplemented actions.
533 if (panic_action
== PANIC_ACTION_PAUSE
534 || (panic_action
== PANIC_ACTION_SHUTDOWN
&& shutdown_action
== SHUTDOWN_ACTION_PAUSE
)) {
535 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE
, info
);
536 vm_stop(RUN_STATE_GUEST_PANICKED
);
537 } else if (panic_action
== PANIC_ACTION_SHUTDOWN
||
538 panic_action
== PANIC_ACTION_EXIT_FAILURE
) {
539 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF
, info
);
540 vm_stop(RUN_STATE_GUEST_PANICKED
);
541 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_PANIC
);
543 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_RUN
, info
);
547 if (info
->type
== GUEST_PANIC_INFORMATION_TYPE_HYPER_V
) {
548 qemu_log_mask(LOG_GUEST_ERROR
, "\nHV crash parameters: (%#"PRIx64
549 " %#"PRIx64
" %#"PRIx64
" %#"PRIx64
" %#"PRIx64
")\n",
550 info
->u
.hyper_v
.arg1
,
551 info
->u
.hyper_v
.arg2
,
552 info
->u
.hyper_v
.arg3
,
553 info
->u
.hyper_v
.arg4
,
554 info
->u
.hyper_v
.arg5
);
555 } else if (info
->type
== GUEST_PANIC_INFORMATION_TYPE_S390
) {
556 qemu_log_mask(LOG_GUEST_ERROR
, " on cpu %d: %s\n"
557 "PSW: 0x%016" PRIx64
" 0x%016" PRIx64
"\n",
559 S390CrashReason_str(info
->u
.s390
.reason
),
560 info
->u
.s390
.psw_mask
,
561 info
->u
.s390
.psw_addr
);
563 qapi_free_GuestPanicInformation(info
);
567 void qemu_system_guest_crashloaded(GuestPanicInformation
*info
)
569 qemu_log_mask(LOG_GUEST_ERROR
, "Guest crash loaded");
570 qapi_event_send_guest_crashloaded(GUEST_PANIC_ACTION_RUN
, info
);
571 qapi_free_GuestPanicInformation(info
);
574 void qemu_system_reset_request(ShutdownCause reason
)
576 if (reboot_action
== REBOOT_ACTION_SHUTDOWN
&&
577 reason
!= SHUTDOWN_CAUSE_SUBSYSTEM_RESET
) {
578 shutdown_requested
= reason
;
579 } else if (!cpus_are_resettable()) {
580 error_report("cpus are not resettable, terminating");
581 shutdown_requested
= reason
;
583 reset_requested
= reason
;
589 static void qemu_system_suspend(void)
592 notifier_list_notify(&suspend_notifiers
, NULL
);
593 runstate_set(RUN_STATE_SUSPENDED
);
594 qapi_event_send_suspend();
597 void qemu_system_suspend_request(void)
599 if (runstate_check(RUN_STATE_SUSPENDED
)) {
602 suspend_requested
= 1;
607 void qemu_register_suspend_notifier(Notifier
*notifier
)
609 notifier_list_add(&suspend_notifiers
, notifier
);
612 void qemu_system_wakeup_request(WakeupReason reason
, Error
**errp
)
614 trace_system_wakeup_request(reason
);
616 if (!runstate_check(RUN_STATE_SUSPENDED
)) {
618 "Unable to wake up: guest is not in suspended state");
621 if (!(wakeup_reason_mask
& (1 << reason
))) {
624 runstate_set(RUN_STATE_RUNNING
);
625 wakeup_reason
= reason
;
629 void qemu_system_wakeup_enable(WakeupReason reason
, bool enabled
)
632 wakeup_reason_mask
|= (1 << reason
);
634 wakeup_reason_mask
&= ~(1 << reason
);
638 void qemu_register_wakeup_notifier(Notifier
*notifier
)
640 notifier_list_add(&wakeup_notifiers
, notifier
);
643 static bool wakeup_suspend_enabled
;
645 void qemu_register_wakeup_support(void)
647 wakeup_suspend_enabled
= true;
650 bool qemu_wakeup_suspend_enabled(void)
652 return wakeup_suspend_enabled
;
655 void qemu_system_killed(int signal
, pid_t pid
)
657 shutdown_signal
= signal
;
659 shutdown_action
= SHUTDOWN_ACTION_POWEROFF
;
661 /* Cannot call qemu_system_shutdown_request directly because
662 * we are in a signal handler.
664 shutdown_requested
= SHUTDOWN_CAUSE_HOST_SIGNAL
;
668 void qemu_system_shutdown_request_with_code(ShutdownCause reason
,
671 shutdown_exit_code
= exit_code
;
672 qemu_system_shutdown_request(reason
);
675 void qemu_system_shutdown_request(ShutdownCause reason
)
677 trace_qemu_system_shutdown_request(reason
);
678 replay_shutdown_request(reason
);
679 shutdown_requested
= reason
;
683 static void qemu_system_powerdown(void)
685 qapi_event_send_powerdown();
686 notifier_list_notify(&powerdown_notifiers
, NULL
);
689 static void qemu_system_shutdown(ShutdownCause cause
)
691 qapi_event_send_shutdown(shutdown_caused_by_guest(cause
), cause
);
692 notifier_list_notify(&shutdown_notifiers
, &cause
);
695 void qemu_system_powerdown_request(void)
697 trace_qemu_system_powerdown_request();
698 powerdown_requested
= 1;
702 void qemu_register_powerdown_notifier(Notifier
*notifier
)
704 notifier_list_add(&powerdown_notifiers
, notifier
);
707 void qemu_register_shutdown_notifier(Notifier
*notifier
)
709 notifier_list_add(&shutdown_notifiers
, notifier
);
712 void qemu_system_debug_request(void)
718 static bool main_loop_should_exit(int *status
)
721 ShutdownCause request
;
723 if (qemu_debug_requested()) {
724 vm_stop(RUN_STATE_DEBUG
);
726 if (qemu_suspend_requested()) {
727 qemu_system_suspend();
729 request
= qemu_shutdown_requested();
732 qemu_system_shutdown(request
);
733 if (shutdown_action
== SHUTDOWN_ACTION_PAUSE
) {
734 vm_stop(RUN_STATE_SHUTDOWN
);
736 if (shutdown_exit_code
!= EXIT_SUCCESS
) {
737 *status
= shutdown_exit_code
;
738 } else if (request
== SHUTDOWN_CAUSE_GUEST_PANIC
&&
739 panic_action
== PANIC_ACTION_EXIT_FAILURE
) {
740 *status
= EXIT_FAILURE
;
745 request
= qemu_reset_requested();
748 qemu_system_reset(request
);
751 * runstate can change in pause_all_vcpus()
752 * as iothread mutex is unlocked
754 if (!runstate_check(RUN_STATE_RUNNING
) &&
755 !runstate_check(RUN_STATE_INMIGRATE
) &&
756 !runstate_check(RUN_STATE_FINISH_MIGRATE
)) {
757 runstate_set(RUN_STATE_PRELAUNCH
);
760 if (qemu_wakeup_requested()) {
762 qemu_system_wakeup();
763 notifier_list_notify(&wakeup_notifiers
, &wakeup_reason
);
764 wakeup_reason
= QEMU_WAKEUP_REASON_NONE
;
766 qapi_event_send_wakeup();
768 if (qemu_powerdown_requested()) {
769 qemu_system_powerdown();
771 if (qemu_vmstop_requested(&r
)) {
777 int qemu_main_loop(void)
779 int status
= EXIT_SUCCESS
;
781 while (!main_loop_should_exit(&status
)) {
782 main_loop_wait(false);
788 void qemu_add_exit_notifier(Notifier
*notify
)
790 notifier_list_add(&exit_notifiers
, notify
);
793 void qemu_remove_exit_notifier(Notifier
*notify
)
795 notifier_remove(notify
);
798 static void qemu_run_exit_notifiers(void)
800 notifier_list_notify(&exit_notifiers
, NULL
);
803 void qemu_init_subsystems(void)
807 os_set_line_buffering();
809 module_call_init(MODULE_INIT_TRACE
);
811 qemu_init_cpu_list();
812 qemu_init_cpu_loop();
813 qemu_mutex_lock_iothread();
815 atexit(qemu_run_exit_notifiers
);
817 module_call_init(MODULE_INIT_QOM
);
818 module_call_init(MODULE_INIT_MIGRATION
);
821 precopy_infrastructure_init();
822 postcopy_infrastructure_init();
823 monitor_init_globals();
825 if (qcrypto_init(&err
) < 0) {
826 error_reportf_err(err
, "cannot initialize crypto: ");
830 os_setup_early_signal_handling();
832 bdrv_init_with_whitelist();
837 void qemu_cleanup(int status
)
842 * cleaning up the migration object cancels any existing migration
843 * try to do this early so that it also stops using devices.
845 migration_shutdown();
848 * Close the exports before draining the block layer. The export
849 * drivers may have coroutines yielding on it, so we need to clean
850 * them up before the drain, as otherwise they may be get stuck in
851 * blk_wait_while_drained().
856 /* No more vcpu or device emulation activity beyond this point */
861 * We must cancel all block jobs while the block layer is drained,
862 * or cancelling will be affected by throttling and thus may block
863 * for an extended period of time.
864 * Begin the drained section after vm_shutdown() to avoid requests being
865 * stuck in the BlockBackend's request queue.
866 * We do not need to end this section, because we do not want any
867 * requests happening from here on anyway.
869 bdrv_drain_all_begin();
870 job_cancel_sync_all();
873 /* vhost-user must be cleaned up before chardevs. */
879 user_creatable_cleanup();
880 /* TODO: unref root container, check all devices are ok */