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