]> git.proxmox.com Git - mirror_qemu.git/blame - softmmu/runstate.c
tests: acpi: extend bridge tests with hotplugged bridges
[mirror_qemu.git] / softmmu / runstate.c
CommitLineData
ba87e434
PB
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"
1895b977 28#include "block/export.h"
ba87e434
PB
29#include "chardev/char.h"
30#include "crypto/cipher.h"
31#include "crypto/init.h"
32#include "exec/cpu-common.h"
ba87e434
PB
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"
ba87e434 43#include "qemu/error-report.h"
cd617484 44#include "qemu/log.h"
ba87e434 45#include "qemu/job.h"
bc2fbf93 46#include "qemu/log.h"
ba87e434
PB
47#include "qemu/module.h"
48#include "qemu/plugin.h"
49#include "qemu/sockets.h"
50#include "qemu/thread.h"
51#include "qom/object.h"
52#include "qom/object_interfaces.h"
53#include "sysemu/cpus.h"
54#include "sysemu/qtest.h"
55#include "sysemu/replay.h"
56#include "sysemu/reset.h"
57#include "sysemu/runstate.h"
e6dba048 58#include "sysemu/runstate-action.h"
ba87e434
PB
59#include "sysemu/sysemu.h"
60#include "sysemu/tpm.h"
61#include "trace.h"
62
63static NotifierList exit_notifiers =
64 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
65
66static RunState current_run_state = RUN_STATE_PRELAUNCH;
67
68/* We use RUN_STATE__MAX but any invalid value will do */
69static RunState vmstop_requested = RUN_STATE__MAX;
70static QemuMutex vmstop_lock;
71
72typedef struct {
73 RunState from;
74 RunState to;
75} RunStateTransition;
76
77static const RunStateTransition runstate_transitions_def[] = {
78 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
79
80 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
81 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
82 { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH },
83
84 { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
85 { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
86 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
87 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
88 { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
89 { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
90 { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
91 { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
92 { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
93 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
94 { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE },
95 { RUN_STATE_INMIGRATE, RUN_STATE_COLO },
96
97 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
98 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
99 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH },
100
101 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
102 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
103 { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH },
104
105 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
106 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
107 { RUN_STATE_PAUSED, RUN_STATE_POSTMIGRATE },
108 { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH },
109 { RUN_STATE_PAUSED, RUN_STATE_COLO},
110
111 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
112 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
113 { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH },
114
115 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
116 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
117 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
118
119 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
120 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PAUSED },
121 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
122 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH },
123 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_COLO},
124
125 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
126 { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
127
128 { RUN_STATE_COLO, RUN_STATE_RUNNING },
669846c5 129 { RUN_STATE_COLO, RUN_STATE_PRELAUNCH },
229620d5 130 { RUN_STATE_COLO, RUN_STATE_SHUTDOWN},
ba87e434
PB
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/* This function will abort() on invalid state transitions */
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
0a389509 223bool runstate_is_running(void)
ba87e434
PB
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 * qemu_add_vm_change_state_handler_prio:
277 * @cb: the callback to invoke
278 * @opaque: user data passed to the callback
279 * @priority: low priorities execute first when the vm runs and the reverse is
280 * true when the vm stops
281 *
282 * Register a callback function that is invoked when the vm starts or stops
283 * running.
284 *
285 * Returns: an entry to be freed using qemu_del_vm_change_state_handler()
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 /* Keep list sorted in ascending priority order */
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
538f0497 322void vm_state_notify(bool running, RunState state)
ba87e434
PB
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 /* This happens for eg ^C at the terminal, so it's worth
377 * avoiding printing an odd message in that case.
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 * Reset the VM. Issue an event unless @reason is SHUTDOWN_CAUSE_NONE.
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) {
7966d70f 444 mc->reset(current_machine, reason);
ba87e434 445 } else {
7966d70f 446 qemu_devices_reset(reason);
ba87e434 447 }
7966d70f
JD
448 switch (reason) {
449 case SHUTDOWN_CAUSE_NONE:
450 case SHUTDOWN_CAUSE_SUBSYSTEM_RESET:
451 case SHUTDOWN_CAUSE_SNAPSHOT_LOAD:
452 break;
453 default:
ba87e434
PB
454 qapi_event_send_reset(shutdown_caused_by_guest(reason), reason);
455 }
456 cpu_synchronize_all_post_reset();
457}
458
459/*
460 * Wake the VM after suspend.
461 */
462static void qemu_system_wakeup(void)
463{
464 MachineClass *mc;
465
466 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
467
468 if (mc && mc->wakeup) {
469 mc->wakeup(current_machine);
470 }
471}
472
473void qemu_system_guest_panicked(GuestPanicInformation *info)
474{
475 qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed");
476
477 if (current_cpu) {
478 current_cpu->crash_occurred = true;
479 }
c753e8e7
AJ
480 /*
481 * TODO: Currently the available panic actions are: none, pause, and
c27025e0 482 * shutdown, but in principle debug and reset could be supported as well.
c753e8e7
AJ
483 * Investigate any potential use cases for the unimplemented actions.
484 */
c27025e0
PB
485 if (panic_action == PANIC_ACTION_PAUSE
486 || (panic_action == PANIC_ACTION_SHUTDOWN && shutdown_action == SHUTDOWN_ACTION_PAUSE)) {
0ccc2c92 487 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE, info);
c753e8e7 488 vm_stop(RUN_STATE_GUEST_PANICKED);
0882caf4
IL
489 } else if (panic_action == PANIC_ACTION_SHUTDOWN ||
490 panic_action == PANIC_ACTION_EXIT_FAILURE) {
0ccc2c92 491 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF, info);
c753e8e7 492 vm_stop(RUN_STATE_GUEST_PANICKED);
ba87e434 493 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_PANIC);
c753e8e7 494 } else {
0ccc2c92 495 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_RUN, info);
ba87e434
PB
496 }
497
498 if (info) {
499 if (info->type == GUEST_PANIC_INFORMATION_TYPE_HYPER_V) {
500 qemu_log_mask(LOG_GUEST_ERROR, "\nHV crash parameters: (%#"PRIx64
501 " %#"PRIx64" %#"PRIx64" %#"PRIx64" %#"PRIx64")\n",
502 info->u.hyper_v.arg1,
503 info->u.hyper_v.arg2,
504 info->u.hyper_v.arg3,
505 info->u.hyper_v.arg4,
506 info->u.hyper_v.arg5);
507 } else if (info->type == GUEST_PANIC_INFORMATION_TYPE_S390) {
508 qemu_log_mask(LOG_GUEST_ERROR, " on cpu %d: %s\n"
509 "PSW: 0x%016" PRIx64 " 0x%016" PRIx64"\n",
510 info->u.s390.core,
511 S390CrashReason_str(info->u.s390.reason),
512 info->u.s390.psw_mask,
513 info->u.s390.psw_addr);
514 }
515 qapi_free_GuestPanicInformation(info);
516 }
517}
518
519void qemu_system_guest_crashloaded(GuestPanicInformation *info)
520{
521 qemu_log_mask(LOG_GUEST_ERROR, "Guest crash loaded");
0ccc2c92
MA
522 qapi_event_send_guest_crashloaded(GUEST_PANIC_ACTION_RUN, info);
523 qapi_free_GuestPanicInformation(info);
ba87e434
PB
524}
525
526void qemu_system_reset_request(ShutdownCause reason)
527{
e6dba048
AJ
528 if (reboot_action == REBOOT_ACTION_SHUTDOWN &&
529 reason != SHUTDOWN_CAUSE_SUBSYSTEM_RESET) {
ba87e434 530 shutdown_requested = reason;
92a5199b
TL
531 } else if (!cpus_are_resettable()) {
532 error_report("cpus are not resettable, terminating");
533 shutdown_requested = reason;
ba87e434
PB
534 } else {
535 reset_requested = reason;
536 }
537 cpu_stop_current();
538 qemu_notify_event();
539}
540
541static void qemu_system_suspend(void)
542{
543 pause_all_vcpus();
544 notifier_list_notify(&suspend_notifiers, NULL);
545 runstate_set(RUN_STATE_SUSPENDED);
546 qapi_event_send_suspend();
547}
548
549void qemu_system_suspend_request(void)
550{
551 if (runstate_check(RUN_STATE_SUSPENDED)) {
552 return;
553 }
554 suspend_requested = 1;
555 cpu_stop_current();
556 qemu_notify_event();
557}
558
559void qemu_register_suspend_notifier(Notifier *notifier)
560{
561 notifier_list_add(&suspend_notifiers, notifier);
562}
563
564void qemu_system_wakeup_request(WakeupReason reason, Error **errp)
565{
566 trace_system_wakeup_request(reason);
567
568 if (!runstate_check(RUN_STATE_SUSPENDED)) {
569 error_setg(errp,
570 "Unable to wake up: guest is not in suspended state");
571 return;
572 }
573 if (!(wakeup_reason_mask & (1 << reason))) {
574 return;
575 }
576 runstate_set(RUN_STATE_RUNNING);
577 wakeup_reason = reason;
578 qemu_notify_event();
579}
580
581void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
582{
583 if (enabled) {
584 wakeup_reason_mask |= (1 << reason);
585 } else {
586 wakeup_reason_mask &= ~(1 << reason);
587 }
588}
589
590void qemu_register_wakeup_notifier(Notifier *notifier)
591{
592 notifier_list_add(&wakeup_notifiers, notifier);
593}
594
595static bool wakeup_suspend_enabled;
596
597void qemu_register_wakeup_support(void)
598{
599 wakeup_suspend_enabled = true;
600}
601
602bool qemu_wakeup_suspend_enabled(void)
603{
604 return wakeup_suspend_enabled;
605}
606
607void qemu_system_killed(int signal, pid_t pid)
608{
609 shutdown_signal = signal;
610 shutdown_pid = pid;
e6dba048 611 shutdown_action = SHUTDOWN_ACTION_POWEROFF;
ba87e434
PB
612
613 /* Cannot call qemu_system_shutdown_request directly because
614 * we are in a signal handler.
615 */
616 shutdown_requested = SHUTDOWN_CAUSE_HOST_SIGNAL;
617 qemu_notify_event();
618}
619
620void qemu_system_shutdown_request(ShutdownCause reason)
621{
622 trace_qemu_system_shutdown_request(reason);
623 replay_shutdown_request(reason);
624 shutdown_requested = reason;
625 qemu_notify_event();
626}
627
628static void qemu_system_powerdown(void)
629{
630 qapi_event_send_powerdown();
631 notifier_list_notify(&powerdown_notifiers, NULL);
632}
633
634static void qemu_system_shutdown(ShutdownCause cause)
635{
636 qapi_event_send_shutdown(shutdown_caused_by_guest(cause), cause);
637 notifier_list_notify(&shutdown_notifiers, &cause);
638}
639
640void qemu_system_powerdown_request(void)
641{
642 trace_qemu_system_powerdown_request();
643 powerdown_requested = 1;
644 qemu_notify_event();
645}
646
647void qemu_register_powerdown_notifier(Notifier *notifier)
648{
649 notifier_list_add(&powerdown_notifiers, notifier);
650}
651
652void qemu_register_shutdown_notifier(Notifier *notifier)
653{
654 notifier_list_add(&shutdown_notifiers, notifier);
655}
656
657void qemu_system_debug_request(void)
658{
659 debug_requested = 1;
660 qemu_notify_event();
661}
662
0882caf4 663static bool main_loop_should_exit(int *status)
ba87e434
PB
664{
665 RunState r;
666 ShutdownCause request;
667
668 if (qemu_debug_requested()) {
669 vm_stop(RUN_STATE_DEBUG);
670 }
671 if (qemu_suspend_requested()) {
672 qemu_system_suspend();
673 }
674 request = qemu_shutdown_requested();
675 if (request) {
676 qemu_kill_report();
677 qemu_system_shutdown(request);
e6dba048 678 if (shutdown_action == SHUTDOWN_ACTION_PAUSE) {
ba87e434
PB
679 vm_stop(RUN_STATE_SHUTDOWN);
680 } else {
0882caf4
IL
681 if (request == SHUTDOWN_CAUSE_GUEST_PANIC &&
682 panic_action == PANIC_ACTION_EXIT_FAILURE) {
683 *status = EXIT_FAILURE;
684 }
ba87e434
PB
685 return true;
686 }
687 }
688 request = qemu_reset_requested();
689 if (request) {
690 pause_all_vcpus();
691 qemu_system_reset(request);
692 resume_all_vcpus();
693 /*
694 * runstate can change in pause_all_vcpus()
695 * as iothread mutex is unlocked
696 */
697 if (!runstate_check(RUN_STATE_RUNNING) &&
698 !runstate_check(RUN_STATE_INMIGRATE) &&
699 !runstate_check(RUN_STATE_FINISH_MIGRATE)) {
700 runstate_set(RUN_STATE_PRELAUNCH);
701 }
702 }
703 if (qemu_wakeup_requested()) {
704 pause_all_vcpus();
705 qemu_system_wakeup();
706 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
707 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
708 resume_all_vcpus();
709 qapi_event_send_wakeup();
710 }
711 if (qemu_powerdown_requested()) {
712 qemu_system_powerdown();
713 }
714 if (qemu_vmstop_requested(&r)) {
715 vm_stop(r);
716 }
717 return false;
718}
719
0882caf4 720int qemu_main_loop(void)
ba87e434 721{
0882caf4 722 int status = EXIT_SUCCESS;
ba87e434
PB
723#ifdef CONFIG_PROFILER
724 int64_t ti;
725#endif
0882caf4
IL
726
727 while (!main_loop_should_exit(&status)) {
ba87e434
PB
728#ifdef CONFIG_PROFILER
729 ti = profile_getclock();
730#endif
731 main_loop_wait(false);
732#ifdef CONFIG_PROFILER
733 dev_time += profile_getclock() - ti;
734#endif
735 }
0882caf4
IL
736
737 return status;
ba87e434
PB
738}
739
740void qemu_add_exit_notifier(Notifier *notify)
741{
742 notifier_list_add(&exit_notifiers, notify);
743}
744
745void qemu_remove_exit_notifier(Notifier *notify)
746{
747 notifier_remove(notify);
748}
749
750static void qemu_run_exit_notifiers(void)
751{
752 notifier_list_notify(&exit_notifiers, NULL);
753}
754
755void qemu_init_subsystems(void)
756{
6e1da3d3 757 Error *err = NULL;
ba87e434
PB
758
759 os_set_line_buffering();
760
761 module_call_init(MODULE_INIT_TRACE);
762
763 qemu_init_cpu_list();
764 qemu_init_cpu_loop();
765 qemu_mutex_lock_iothread();
766
767 atexit(qemu_run_exit_notifiers);
768
769 module_call_init(MODULE_INIT_QOM);
770 module_call_init(MODULE_INIT_MIGRATION);
771
772 runstate_init();
773 precopy_infrastructure_init();
774 postcopy_infrastructure_init();
775 monitor_init_globals();
776
777 if (qcrypto_init(&err) < 0) {
778 error_reportf_err(err, "cannot initialize crypto: ");
779 exit(1);
780 }
781
782 os_setup_early_signal_handling();
783
784 bdrv_init_with_whitelist();
785 socket_init();
786}
787
788
789void qemu_cleanup(void)
790{
5ef0317f 791 gdb_exit(0);
ba87e434
PB
792
793 /*
794 * cleaning up the migration object cancels any existing migration
795 * try to do this early so that it also stops using devices.
796 */
797 migration_shutdown();
798
1895b977
SL
799 /*
800 * Close the exports before draining the block layer. The export
801 * drivers may have coroutines yielding on it, so we need to clean
802 * them up before the drain, as otherwise they may be get stuck in
803 * blk_wait_while_drained().
804 */
805 blk_exp_close_all();
806
ba87e434
PB
807 /*
808 * We must cancel all block jobs while the block layer is drained,
809 * or cancelling will be affected by throttling and thus may block
810 * for an extended period of time.
811 * vm_shutdown() will bdrv_drain_all(), so we may as well include
812 * it in the drained section.
813 * We do not need to end this section, because we do not want any
814 * requests happening from here on anyway.
815 */
816 bdrv_drain_all_begin();
817
818 /* No more vcpu or device emulation activity beyond this point */
819 vm_shutdown();
820 replay_finish();
821
822 job_cancel_sync_all();
823 bdrv_close_all();
824
825 /* vhost-user must be cleaned up before chardevs. */
826 tpm_cleanup();
827 net_cleanup();
828 audio_cleanup();
829 monitor_cleanup();
830 qemu_chr_cleanup();
831 user_creatable_cleanup();
832 /* TODO: unref root container, check all devices are ok */
833}