1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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 "exec/gdbstub.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-common.h"
44#include "qemu/error-report.h"
45#include "qemu/log.h"
46#include "qemu/job.h"
47#include "qemu/log.h"
48#include "qemu/module.h"
49#include "qemu/plugin.h"
50#include "qemu/sockets.h"
51#include "qemu/thread.h"
52#include "qom/object.h"
53#include "qom/object_interfaces.h"
54#include "sysemu/cpus.h"
55#include "sysemu/qtest.h"
56#include "sysemu/replay.h"
57#include "sysemu/reset.h"
58#include "sysemu/runstate.h"
59#include "sysemu/runstate-action.h"
60#include "sysemu/sysemu.h"
61#include "sysemu/tpm.h"
62#include "trace.h"
63
64static NotifierList exit_notifiers =
65 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
66
67static RunState current_run_state = RUN_STATE_PRELAUNCH;
68
69
70static RunState vmstop_requested = RUN_STATE__MAX;
71static QemuMutex vmstop_lock;
72
73typedef struct {
74 RunState from;
75 RunState to;
76} RunStateTransition;
77
78static const RunStateTransition runstate_transitions_def[] = {
79 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
80
81 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
82 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
83 { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH },
84
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 },
97
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 },
101
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 },
105
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},
111
112 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
113 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
114 { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH },
115
116 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
117 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
118 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
119
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
126 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
127 { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
128
129 { RUN_STATE_COLO, RUN_STATE_RUNNING },
130 { RUN_STATE_COLO, RUN_STATE_SHUTDOWN},
131
132 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
133 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
134 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
135 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
136 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
137 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
138 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
139 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
140 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
141 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
142 { RUN_STATE_RUNNING, RUN_STATE_COLO},
143
144 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
145
146 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
147 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
148 { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH },
149 { RUN_STATE_SHUTDOWN, RUN_STATE_COLO },
150
151 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
152 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
153 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
154 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
155 { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
156 { RUN_STATE_SUSPENDED, RUN_STATE_COLO},
157
158 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
159 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
160 { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH },
161 { RUN_STATE_WATCHDOG, RUN_STATE_COLO},
162
163 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
164 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
165 { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH },
166
167 { RUN_STATE__MAX, RUN_STATE__MAX },
168};
169
170static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
171
172bool runstate_check(RunState state)
173{
174 return current_run_state == state;
175}
176
177bool runstate_store(char *str, size_t size)
178{
179 const char *state = RunState_str(current_run_state);
180 size_t len = strlen(state) + 1;
181
182 if (len > size) {
183 return false;
184 }
185 memcpy(str, state, len);
186 return true;
187}
188
189static void runstate_init(void)
190{
191 const RunStateTransition *p;
192
193 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
194 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE__MAX; p++) {
195 runstate_valid_transitions[p->from][p->to] = true;
196 }
197
198 qemu_mutex_init(&vmstop_lock);
199}
200
201
202void runstate_set(RunState new_state)
203{
204 assert(new_state < RUN_STATE__MAX);
205
206 trace_runstate_set(current_run_state, RunState_str(current_run_state),
207 new_state, RunState_str(new_state));
208
209 if (current_run_state == new_state) {
210 return;
211 }
212
213 if (!runstate_valid_transitions[current_run_state][new_state]) {
214 error_report("invalid runstate transition: '%s' -> '%s'",
215 RunState_str(current_run_state),
216 RunState_str(new_state));
217 abort();
218 }
219
220 current_run_state = new_state;
221}
222
223bool runstate_is_running(void)
224{
225 return runstate_check(RUN_STATE_RUNNING);
226}
227
228bool runstate_needs_reset(void)
229{
230 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
231 runstate_check(RUN_STATE_SHUTDOWN);
232}
233
234StatusInfo *qmp_query_status(Error **errp)
235{
236 StatusInfo *info = g_malloc0(sizeof(*info));
237
238 info->running = runstate_is_running();
239 info->singlestep = singlestep;
240 info->status = current_run_state;
241
242 return info;
243}
244
245bool qemu_vmstop_requested(RunState *r)
246{
247 qemu_mutex_lock(&vmstop_lock);
248 *r = vmstop_requested;
249 vmstop_requested = RUN_STATE__MAX;
250 qemu_mutex_unlock(&vmstop_lock);
251 return *r < RUN_STATE__MAX;
252}
253
254void qemu_system_vmstop_request_prepare(void)
255{
256 qemu_mutex_lock(&vmstop_lock);
257}
258
259void qemu_system_vmstop_request(RunState state)
260{
261 vmstop_requested = state;
262 qemu_mutex_unlock(&vmstop_lock);
263 qemu_notify_event();
264}
265struct VMChangeStateEntry {
266 VMChangeStateHandler *cb;
267 void *opaque;
268 QTAILQ_ENTRY(VMChangeStateEntry) entries;
269 int priority;
270};
271
272static QTAILQ_HEAD(, VMChangeStateEntry) vm_change_state_head =
273 QTAILQ_HEAD_INITIALIZER(vm_change_state_head);
274
275
276
277
278
279
280
281
282
283
284
285
286
287VMChangeStateEntry *qemu_add_vm_change_state_handler_prio(
288 VMChangeStateHandler *cb, void *opaque, int priority)
289{
290 VMChangeStateEntry *e;
291 VMChangeStateEntry *other;
292
293 e = g_malloc0(sizeof(*e));
294 e->cb = cb;
295 e->opaque = opaque;
296 e->priority = priority;
297
298
299 QTAILQ_FOREACH(other, &vm_change_state_head, entries) {
300 if (priority < other->priority) {
301 QTAILQ_INSERT_BEFORE(other, e, entries);
302 return e;
303 }
304 }
305
306 QTAILQ_INSERT_TAIL(&vm_change_state_head, e, entries);
307 return e;
308}
309
310VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
311 void *opaque)
312{
313 return qemu_add_vm_change_state_handler_prio(cb, opaque, 0);
314}
315
316void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
317{
318 QTAILQ_REMOVE(&vm_change_state_head, e, entries);
319 g_free(e);
320}
321
322void vm_state_notify(bool running, RunState state)
323{
324 VMChangeStateEntry *e, *next;
325
326 trace_vm_state_notify(running, state, RunState_str(state));
327
328 if (running) {
329 QTAILQ_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
330 e->cb(e->opaque, running, state);
331 }
332 } else {
333 QTAILQ_FOREACH_REVERSE_SAFE(e, &vm_change_state_head, entries, next) {
334 e->cb(e->opaque, running, state);
335 }
336 }
337}
338
339static ShutdownCause reset_requested;
340static ShutdownCause shutdown_requested;
341static int shutdown_signal;
342static pid_t shutdown_pid;
343static int powerdown_requested;
344static int debug_requested;
345static int suspend_requested;
346static WakeupReason wakeup_reason;
347static NotifierList powerdown_notifiers =
348 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
349static NotifierList suspend_notifiers =
350 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
351static NotifierList wakeup_notifiers =
352 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
353static NotifierList shutdown_notifiers =
354 NOTIFIER_LIST_INITIALIZER(shutdown_notifiers);
355static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
356
357ShutdownCause qemu_shutdown_requested_get(void)
358{
359 return shutdown_requested;
360}
361
362ShutdownCause qemu_reset_requested_get(void)
363{
364 return reset_requested;
365}
366
367static int qemu_shutdown_requested(void)
368{
369 return qatomic_xchg(&shutdown_requested, SHUTDOWN_CAUSE_NONE);
370}
371
372static void qemu_kill_report(void)
373{
374 if (!qtest_driver() && shutdown_signal) {
375 if (shutdown_pid == 0) {
376
377
378
379 error_report("terminating on signal %d", shutdown_signal);
380 } else {
381 char *shutdown_cmd = qemu_get_pid_name(shutdown_pid);
382
383 error_report("terminating on signal %d from pid " FMT_pid " (%s)",
384 shutdown_signal, shutdown_pid,
385 shutdown_cmd ? shutdown_cmd : "<unknown process>");
386 g_free(shutdown_cmd);
387 }
388 shutdown_signal = 0;
389 }
390}
391
392static ShutdownCause qemu_reset_requested(void)
393{
394 ShutdownCause r = reset_requested;
395
396 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
397 reset_requested = SHUTDOWN_CAUSE_NONE;
398 return r;
399 }
400 return SHUTDOWN_CAUSE_NONE;
401}
402
403static int qemu_suspend_requested(void)
404{
405 int r = suspend_requested;
406 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
407 suspend_requested = 0;
408 return r;
409 }
410 return false;
411}
412
413static WakeupReason qemu_wakeup_requested(void)
414{
415 return wakeup_reason;
416}
417
418static int qemu_powerdown_requested(void)
419{
420 int r = powerdown_requested;
421 powerdown_requested = 0;
422 return r;
423}
424
425static int qemu_debug_requested(void)
426{
427 int r = debug_requested;
428 debug_requested = 0;
429 return r;
430}
431
432
433
434
435void qemu_system_reset(ShutdownCause reason)
436{
437 MachineClass *mc;
438
439 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
440
441 cpu_synchronize_all_states();
442
443 if (mc && mc->reset) {
444 mc->reset(current_machine);
445 } else {
446 qemu_devices_reset();
447 }
448 if (reason && reason != SHUTDOWN_CAUSE_SUBSYSTEM_RESET) {
449 qapi_event_send_reset(shutdown_caused_by_guest(reason), reason);
450 }
451 cpu_synchronize_all_post_reset();
452}
453
454
455
456
457static void qemu_system_wakeup(void)
458{
459 MachineClass *mc;
460
461 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
462
463 if (mc && mc->wakeup) {
464 mc->wakeup(current_machine);
465 }
466}
467
468void qemu_system_guest_panicked(GuestPanicInformation *info)
469{
470 qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed");
471
472 if (current_cpu) {
473 current_cpu->crash_occurred = true;
474 }
475
476
477
478
479
480 if (panic_action == PANIC_ACTION_PAUSE
481 || (panic_action == PANIC_ACTION_SHUTDOWN && shutdown_action == SHUTDOWN_ACTION_PAUSE)) {
482 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE,
483 !!info, info);
484 vm_stop(RUN_STATE_GUEST_PANICKED);
485 } else if (panic_action == PANIC_ACTION_SHUTDOWN) {
486 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF,
487 !!info, info);
488 vm_stop(RUN_STATE_GUEST_PANICKED);
489 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_PANIC);
490 } else {
491 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_RUN,
492 !!info, info);
493 }
494
495 if (info) {
496 if (info->type == GUEST_PANIC_INFORMATION_TYPE_HYPER_V) {
497 qemu_log_mask(LOG_GUEST_ERROR, "\nHV crash parameters: (%#"PRIx64
498 " %#"PRIx64" %#"PRIx64" %#"PRIx64" %#"PRIx64")\n",
499 info->u.hyper_v.arg1,
500 info->u.hyper_v.arg2,
501 info->u.hyper_v.arg3,
502 info->u.hyper_v.arg4,
503 info->u.hyper_v.arg5);
504 } else if (info->type == GUEST_PANIC_INFORMATION_TYPE_S390) {
505 qemu_log_mask(LOG_GUEST_ERROR, " on cpu %d: %s\n"
506 "PSW: 0x%016" PRIx64 " 0x%016" PRIx64"\n",
507 info->u.s390.core,
508 S390CrashReason_str(info->u.s390.reason),
509 info->u.s390.psw_mask,
510 info->u.s390.psw_addr);
511 }
512 qapi_free_GuestPanicInformation(info);
513 }
514}
515
516void qemu_system_guest_crashloaded(GuestPanicInformation *info)
517{
518 qemu_log_mask(LOG_GUEST_ERROR, "Guest crash loaded");
519
520 qapi_event_send_guest_crashloaded(GUEST_PANIC_ACTION_RUN,
521 !!info, info);
522
523 if (info) {
524 qapi_free_GuestPanicInformation(info);
525 }
526}
527
528void qemu_system_reset_request(ShutdownCause reason)
529{
530 if (reboot_action == REBOOT_ACTION_SHUTDOWN &&
531 reason != SHUTDOWN_CAUSE_SUBSYSTEM_RESET) {
532 shutdown_requested = reason;
533 } else if (!cpus_are_resettable()) {
534 error_report("cpus are not resettable, terminating");
535 shutdown_requested = reason;
536 } else {
537 reset_requested = reason;
538 }
539 cpu_stop_current();
540 qemu_notify_event();
541}
542
543static void qemu_system_suspend(void)
544{
545 pause_all_vcpus();
546 notifier_list_notify(&suspend_notifiers, NULL);
547 runstate_set(RUN_STATE_SUSPENDED);
548 qapi_event_send_suspend();
549}
550
551void qemu_system_suspend_request(void)
552{
553 if (runstate_check(RUN_STATE_SUSPENDED)) {
554 return;
555 }
556 suspend_requested = 1;
557 cpu_stop_current();
558 qemu_notify_event();
559}
560
561void qemu_register_suspend_notifier(Notifier *notifier)
562{
563 notifier_list_add(&suspend_notifiers, notifier);
564}
565
566void qemu_system_wakeup_request(WakeupReason reason, Error **errp)
567{
568 trace_system_wakeup_request(reason);
569
570 if (!runstate_check(RUN_STATE_SUSPENDED)) {
571 error_setg(errp,
572 "Unable to wake up: guest is not in suspended state");
573 return;
574 }
575 if (!(wakeup_reason_mask & (1 << reason))) {
576 return;
577 }
578 runstate_set(RUN_STATE_RUNNING);
579 wakeup_reason = reason;
580 qemu_notify_event();
581}
582
583void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
584{
585 if (enabled) {
586 wakeup_reason_mask |= (1 << reason);
587 } else {
588 wakeup_reason_mask &= ~(1 << reason);
589 }
590}
591
592void qemu_register_wakeup_notifier(Notifier *notifier)
593{
594 notifier_list_add(&wakeup_notifiers, notifier);
595}
596
597static bool wakeup_suspend_enabled;
598
599void qemu_register_wakeup_support(void)
600{
601 wakeup_suspend_enabled = true;
602}
603
604bool qemu_wakeup_suspend_enabled(void)
605{
606 return wakeup_suspend_enabled;
607}
608
609void qemu_system_killed(int signal, pid_t pid)
610{
611 shutdown_signal = signal;
612 shutdown_pid = pid;
613 shutdown_action = SHUTDOWN_ACTION_POWEROFF;
614
615
616
617
618 shutdown_requested = SHUTDOWN_CAUSE_HOST_SIGNAL;
619 qemu_notify_event();
620}
621
622void qemu_system_shutdown_request(ShutdownCause reason)
623{
624 trace_qemu_system_shutdown_request(reason);
625 replay_shutdown_request(reason);
626 shutdown_requested = reason;
627 qemu_notify_event();
628}
629
630static void qemu_system_powerdown(void)
631{
632 qapi_event_send_powerdown();
633 notifier_list_notify(&powerdown_notifiers, NULL);
634}
635
636static void qemu_system_shutdown(ShutdownCause cause)
637{
638 qapi_event_send_shutdown(shutdown_caused_by_guest(cause), cause);
639 notifier_list_notify(&shutdown_notifiers, &cause);
640}
641
642void qemu_system_powerdown_request(void)
643{
644 trace_qemu_system_powerdown_request();
645 powerdown_requested = 1;
646 qemu_notify_event();
647}
648
649void qemu_register_powerdown_notifier(Notifier *notifier)
650{
651 notifier_list_add(&powerdown_notifiers, notifier);
652}
653
654void qemu_register_shutdown_notifier(Notifier *notifier)
655{
656 notifier_list_add(&shutdown_notifiers, notifier);
657}
658
659void qemu_system_debug_request(void)
660{
661 debug_requested = 1;
662 qemu_notify_event();
663}
664
665static bool main_loop_should_exit(void)
666{
667 RunState r;
668 ShutdownCause request;
669
670 if (qemu_debug_requested()) {
671 vm_stop(RUN_STATE_DEBUG);
672 }
673 if (qemu_suspend_requested()) {
674 qemu_system_suspend();
675 }
676 request = qemu_shutdown_requested();
677 if (request) {
678 qemu_kill_report();
679 qemu_system_shutdown(request);
680 if (shutdown_action == SHUTDOWN_ACTION_PAUSE) {
681 vm_stop(RUN_STATE_SHUTDOWN);
682 } else {
683 return true;
684 }
685 }
686 request = qemu_reset_requested();
687 if (request) {
688 pause_all_vcpus();
689 qemu_system_reset(request);
690 resume_all_vcpus();
691
692
693
694
695 if (!runstate_check(RUN_STATE_RUNNING) &&
696 !runstate_check(RUN_STATE_INMIGRATE) &&
697 !runstate_check(RUN_STATE_FINISH_MIGRATE)) {
698 runstate_set(RUN_STATE_PRELAUNCH);
699 }
700 }
701 if (qemu_wakeup_requested()) {
702 pause_all_vcpus();
703 qemu_system_wakeup();
704 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
705 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
706 resume_all_vcpus();
707 qapi_event_send_wakeup();
708 }
709 if (qemu_powerdown_requested()) {
710 qemu_system_powerdown();
711 }
712 if (qemu_vmstop_requested(&r)) {
713 vm_stop(r);
714 }
715 return false;
716}
717
718void qemu_main_loop(void)
719{
720#ifdef CONFIG_PROFILER
721 int64_t ti;
722#endif
723 while (!main_loop_should_exit()) {
724#ifdef CONFIG_PROFILER
725 ti = profile_getclock();
726#endif
727 main_loop_wait(false);
728#ifdef CONFIG_PROFILER
729 dev_time += profile_getclock() - ti;
730#endif
731 }
732}
733
734void qemu_add_exit_notifier(Notifier *notify)
735{
736 notifier_list_add(&exit_notifiers, notify);
737}
738
739void qemu_remove_exit_notifier(Notifier *notify)
740{
741 notifier_remove(notify);
742}
743
744static void qemu_run_exit_notifiers(void)
745{
746 notifier_list_notify(&exit_notifiers, NULL);
747}
748
749void qemu_init_subsystems(void)
750{
751 Error *err = NULL;
752
753 os_set_line_buffering();
754
755 module_call_init(MODULE_INIT_TRACE);
756
757 qemu_init_cpu_list();
758 qemu_init_cpu_loop();
759 qemu_mutex_lock_iothread();
760
761 atexit(qemu_run_exit_notifiers);
762
763 module_call_init(MODULE_INIT_QOM);
764 module_call_init(MODULE_INIT_MIGRATION);
765
766 runstate_init();
767 precopy_infrastructure_init();
768 postcopy_infrastructure_init();
769 monitor_init_globals();
770
771 if (qcrypto_init(&err) < 0) {
772 error_reportf_err(err, "cannot initialize crypto: ");
773 exit(1);
774 }
775
776 os_setup_early_signal_handling();
777
778 bdrv_init_with_whitelist();
779 socket_init();
780}
781
782
783void qemu_cleanup(void)
784{
785 gdb_exit(0);
786
787
788
789
790
791 migration_shutdown();
792
793
794
795
796
797
798
799 blk_exp_close_all();
800
801
802
803
804
805
806
807
808
809
810 bdrv_drain_all_begin();
811
812
813 vm_shutdown();
814 replay_finish();
815
816 job_cancel_sync_all();
817 bdrv_close_all();
818
819
820 tpm_cleanup();
821 net_cleanup();
822 audio_cleanup();
823 monitor_cleanup();
824 qemu_chr_cleanup();
825 user_creatable_cleanup();
826
827}
828