]> git.proxmox.com Git - mirror_qemu.git/blame - system/runstate.c
Merge tag 'pull-request-2024-01-19' of https://gitlab.com/thuth/qemu into staging
[mirror_qemu.git] / system / 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"
c566080c 33#include "gdbstub/syscalls.h"
ba87e434
PB
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"
93cbd6c9 43#include "qemu/accel.h"
ba87e434
PB
44#include "qemu/error-report.h"
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"
533206f0 50#include "qemu/timer.h"
ba87e434
PB
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"
e6dba048 59#include "sysemu/runstate-action.h"
ba87e434
PB
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/* We use RUN_STATE__MAX but any invalid value will do */
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 },
58b10570 80 { RUN_STATE_PRELAUNCH, RUN_STATE_SUSPENDED },
ba87e434
PB
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},
b9ae473d 112 { RUN_STATE_PAUSED, RUN_STATE_SUSPENDED},
ba87e434
PB
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 },
a4c6275a
VSO
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 },
ba87e434
PB
133
134 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
135 { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
58b10570 136 { RUN_STATE_RESTORE_VM, RUN_STATE_SUSPENDED },
ba87e434
PB
137
138 { RUN_STATE_COLO, RUN_STATE_RUNNING },
669846c5 139 { RUN_STATE_COLO, RUN_STATE_PRELAUNCH },
229620d5 140 { RUN_STATE_COLO, RUN_STATE_SHUTDOWN},
ba87e434
PB
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 },
58b10570 155 { RUN_STATE_SAVE_VM, RUN_STATE_SUSPENDED },
ba87e434
PB
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},
b9ae473d 168 { RUN_STATE_SUSPENDED, RUN_STATE_PAUSED},
58b10570
SS
169 { RUN_STATE_SUSPENDED, RUN_STATE_SAVE_VM },
170 { RUN_STATE_SUSPENDED, RUN_STATE_RESTORE_VM },
49a50206 171 { RUN_STATE_SUSPENDED, RUN_STATE_SHUTDOWN },
ba87e434
PB
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
185static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
186
187bool runstate_check(RunState state)
188{
189 return current_run_state == state;
190}
191
ba87e434
PB
192static 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 */
205void 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
242b74eb
VSO
226RunState runstate_get(void)
227{
228 return current_run_state;
229}
230
0a389509 231bool runstate_is_running(void)
ba87e434
PB
232{
233 return runstate_check(RUN_STATE_RUNNING);
234}
235
236bool runstate_needs_reset(void)
237{
238 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
239 runstate_check(RUN_STATE_SHUTDOWN);
240}
241
242StatusInfo *qmp_query_status(Error **errp)
243{
244 StatusInfo *info = g_malloc0(sizeof(*info));
fe17522d 245
ba87e434 246 info->running = runstate_is_running();
ba87e434
PB
247 info->status = current_run_state;
248
249 return info;
250}
251
252bool qemu_vmstop_requested(RunState *r)
253{
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;
259}
260
261void qemu_system_vmstop_request_prepare(void)
262{
263 qemu_mutex_lock(&vmstop_lock);
264}
265
266void qemu_system_vmstop_request(RunState state)
267{
268 vmstop_requested = state;
269 qemu_mutex_unlock(&vmstop_lock);
270 qemu_notify_event();
271}
272struct VMChangeStateEntry {
273 VMChangeStateHandler *cb;
9d3103c8 274 VMChangeStateHandler *prepare_cb;
ba87e434
PB
275 void *opaque;
276 QTAILQ_ENTRY(VMChangeStateEntry) entries;
277 int priority;
278};
279
280static QTAILQ_HEAD(, VMChangeStateEntry) vm_change_state_head =
281 QTAILQ_HEAD_INITIALIZER(vm_change_state_head);
282
283/**
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
289 *
290 * Register a callback function that is invoked when the vm starts or stops
291 * running.
292 *
293 * Returns: an entry to be freed using qemu_del_vm_change_state_handler()
294 */
295VMChangeStateEntry *qemu_add_vm_change_state_handler_prio(
296 VMChangeStateHandler *cb, void *opaque, int priority)
9d3103c8
AH
297{
298 return qemu_add_vm_change_state_handler_prio_full(cb, NULL, opaque,
299 priority);
300}
301
302/**
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
309 *
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.
316 *
317 * Returns: an entry to be freed using qemu_del_vm_change_state_handler()
318 */
319VMChangeStateEntry *
320qemu_add_vm_change_state_handler_prio_full(VMChangeStateHandler *cb,
321 VMChangeStateHandler *prepare_cb,
322 void *opaque, int priority)
ba87e434
PB
323{
324 VMChangeStateEntry *e;
325 VMChangeStateEntry *other;
326
327 e = g_malloc0(sizeof(*e));
328 e->cb = cb;
9d3103c8 329 e->prepare_cb = prepare_cb;
ba87e434
PB
330 e->opaque = opaque;
331 e->priority = priority;
332
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);
337 return e;
338 }
339 }
340
341 QTAILQ_INSERT_TAIL(&vm_change_state_head, e, entries);
342 return e;
343}
344
345VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
346 void *opaque)
347{
348 return qemu_add_vm_change_state_handler_prio(cb, opaque, 0);
349}
350
351void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
352{
353 QTAILQ_REMOVE(&vm_change_state_head, e, entries);
354 g_free(e);
355}
356
538f0497 357void vm_state_notify(bool running, RunState state)
ba87e434
PB
358{
359 VMChangeStateEntry *e, *next;
360
361 trace_vm_state_notify(running, state, RunState_str(state));
362
363 if (running) {
9d3103c8
AH
364 QTAILQ_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
365 if (e->prepare_cb) {
366 e->prepare_cb(e->opaque, running, state);
367 }
368 }
369
ba87e434
PB
370 QTAILQ_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
371 e->cb(e->opaque, running, state);
372 }
373 } else {
9d3103c8
AH
374 QTAILQ_FOREACH_REVERSE_SAFE(e, &vm_change_state_head, entries, next) {
375 if (e->prepare_cb) {
376 e->prepare_cb(e->opaque, running, state);
377 }
378 }
379
ba87e434
PB
380 QTAILQ_FOREACH_REVERSE_SAFE(e, &vm_change_state_head, entries, next) {
381 e->cb(e->opaque, running, state);
382 }
383 }
384}
385
386static ShutdownCause reset_requested;
387static ShutdownCause shutdown_requested;
0386f39b 388static int shutdown_exit_code = EXIT_SUCCESS;
ba87e434
PB
389static int shutdown_signal;
390static pid_t shutdown_pid;
391static int powerdown_requested;
392static int debug_requested;
393static int suspend_requested;
394static WakeupReason wakeup_reason;
395static NotifierList powerdown_notifiers =
396 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
397static NotifierList suspend_notifiers =
398 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
399static NotifierList wakeup_notifiers =
400 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
401static NotifierList shutdown_notifiers =
402 NOTIFIER_LIST_INITIALIZER(shutdown_notifiers);
403static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
404
405ShutdownCause qemu_shutdown_requested_get(void)
406{
407 return shutdown_requested;
408}
409
410ShutdownCause qemu_reset_requested_get(void)
411{
412 return reset_requested;
413}
414
415static int qemu_shutdown_requested(void)
416{
417 return qatomic_xchg(&shutdown_requested, SHUTDOWN_CAUSE_NONE);
418}
419
420static void qemu_kill_report(void)
421{
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.
426 */
427 error_report("terminating on signal %d", shutdown_signal);
428 } else {
429 char *shutdown_cmd = qemu_get_pid_name(shutdown_pid);
430
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);
435 }
436 shutdown_signal = 0;
437 }
438}
439
440static ShutdownCause qemu_reset_requested(void)
441{
442 ShutdownCause r = reset_requested;
443
444 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
445 reset_requested = SHUTDOWN_CAUSE_NONE;
446 return r;
447 }
448 return SHUTDOWN_CAUSE_NONE;
449}
450
451static int qemu_suspend_requested(void)
452{
453 int r = suspend_requested;
454 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
455 suspend_requested = 0;
456 return r;
457 }
458 return false;
459}
460
461static WakeupReason qemu_wakeup_requested(void)
462{
463 return wakeup_reason;
464}
465
466static int qemu_powerdown_requested(void)
467{
468 int r = powerdown_requested;
469 powerdown_requested = 0;
470 return r;
471}
472
473static int qemu_debug_requested(void)
474{
475 int r = debug_requested;
476 debug_requested = 0;
477 return r;
478}
479
480/*
481 * Reset the VM. Issue an event unless @reason is SHUTDOWN_CAUSE_NONE.
482 */
483void qemu_system_reset(ShutdownCause reason)
484{
485 MachineClass *mc;
486
487 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
488
489 cpu_synchronize_all_states();
490
491 if (mc && mc->reset) {
7966d70f 492 mc->reset(current_machine, reason);
ba87e434 493 } else {
7966d70f 494 qemu_devices_reset(reason);
ba87e434 495 }
7966d70f
JD
496 switch (reason) {
497 case SHUTDOWN_CAUSE_NONE:
498 case SHUTDOWN_CAUSE_SUBSYSTEM_RESET:
499 case SHUTDOWN_CAUSE_SNAPSHOT_LOAD:
500 break;
501 default:
ba87e434
PB
502 qapi_event_send_reset(shutdown_caused_by_guest(reason), reason);
503 }
504 cpu_synchronize_all_post_reset();
b9ae473d 505 vm_set_suspended(false);
ba87e434
PB
506}
507
508/*
509 * Wake the VM after suspend.
510 */
511static void qemu_system_wakeup(void)
512{
513 MachineClass *mc;
514
515 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
516
517 if (mc && mc->wakeup) {
518 mc->wakeup(current_machine);
519 }
520}
521
522void qemu_system_guest_panicked(GuestPanicInformation *info)
523{
524 qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed");
525
526 if (current_cpu) {
527 current_cpu->crash_occurred = true;
528 }
c753e8e7
AJ
529 /*
530 * TODO: Currently the available panic actions are: none, pause, and
c27025e0 531 * shutdown, but in principle debug and reset could be supported as well.
c753e8e7
AJ
532 * Investigate any potential use cases for the unimplemented actions.
533 */
c27025e0
PB
534 if (panic_action == PANIC_ACTION_PAUSE
535 || (panic_action == PANIC_ACTION_SHUTDOWN && shutdown_action == SHUTDOWN_ACTION_PAUSE)) {
0ccc2c92 536 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE, info);
c753e8e7 537 vm_stop(RUN_STATE_GUEST_PANICKED);
0882caf4
IL
538 } else if (panic_action == PANIC_ACTION_SHUTDOWN ||
539 panic_action == PANIC_ACTION_EXIT_FAILURE) {
0ccc2c92 540 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF, info);
c753e8e7 541 vm_stop(RUN_STATE_GUEST_PANICKED);
ba87e434 542 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_PANIC);
c753e8e7 543 } else {
0ccc2c92 544 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_RUN, info);
ba87e434
PB
545 }
546
547 if (info) {
548 if (info->type == GUEST_PANIC_INFORMATION_TYPE_HYPER_V) {
549 qemu_log_mask(LOG_GUEST_ERROR, "\nHV crash parameters: (%#"PRIx64
550 " %#"PRIx64" %#"PRIx64" %#"PRIx64" %#"PRIx64")\n",
551 info->u.hyper_v.arg1,
552 info->u.hyper_v.arg2,
553 info->u.hyper_v.arg3,
554 info->u.hyper_v.arg4,
555 info->u.hyper_v.arg5);
556 } else if (info->type == GUEST_PANIC_INFORMATION_TYPE_S390) {
557 qemu_log_mask(LOG_GUEST_ERROR, " on cpu %d: %s\n"
558 "PSW: 0x%016" PRIx64 " 0x%016" PRIx64"\n",
559 info->u.s390.core,
560 S390CrashReason_str(info->u.s390.reason),
561 info->u.s390.psw_mask,
562 info->u.s390.psw_addr);
563 }
564 qapi_free_GuestPanicInformation(info);
565 }
566}
567
568void qemu_system_guest_crashloaded(GuestPanicInformation *info)
569{
570 qemu_log_mask(LOG_GUEST_ERROR, "Guest crash loaded");
0ccc2c92
MA
571 qapi_event_send_guest_crashloaded(GUEST_PANIC_ACTION_RUN, info);
572 qapi_free_GuestPanicInformation(info);
ba87e434
PB
573}
574
575void qemu_system_reset_request(ShutdownCause reason)
576{
e6dba048
AJ
577 if (reboot_action == REBOOT_ACTION_SHUTDOWN &&
578 reason != SHUTDOWN_CAUSE_SUBSYSTEM_RESET) {
ba87e434 579 shutdown_requested = reason;
92a5199b
TL
580 } else if (!cpus_are_resettable()) {
581 error_report("cpus are not resettable, terminating");
582 shutdown_requested = reason;
ba87e434
PB
583 } else {
584 reset_requested = reason;
585 }
586 cpu_stop_current();
587 qemu_notify_event();
588}
589
590static void qemu_system_suspend(void)
591{
592 pause_all_vcpus();
593 notifier_list_notify(&suspend_notifiers, NULL);
594 runstate_set(RUN_STATE_SUSPENDED);
595 qapi_event_send_suspend();
596}
597
598void qemu_system_suspend_request(void)
599{
600 if (runstate_check(RUN_STATE_SUSPENDED)) {
601 return;
602 }
603 suspend_requested = 1;
604 cpu_stop_current();
605 qemu_notify_event();
606}
607
608void qemu_register_suspend_notifier(Notifier *notifier)
609{
610 notifier_list_add(&suspend_notifiers, notifier);
611}
612
613void qemu_system_wakeup_request(WakeupReason reason, Error **errp)
614{
615 trace_system_wakeup_request(reason);
616
617 if (!runstate_check(RUN_STATE_SUSPENDED)) {
618 error_setg(errp,
619 "Unable to wake up: guest is not in suspended state");
620 return;
621 }
622 if (!(wakeup_reason_mask & (1 << reason))) {
623 return;
624 }
625 runstate_set(RUN_STATE_RUNNING);
626 wakeup_reason = reason;
627 qemu_notify_event();
628}
629
630void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
631{
632 if (enabled) {
633 wakeup_reason_mask |= (1 << reason);
634 } else {
635 wakeup_reason_mask &= ~(1 << reason);
636 }
637}
638
639void qemu_register_wakeup_notifier(Notifier *notifier)
640{
641 notifier_list_add(&wakeup_notifiers, notifier);
642}
643
644static bool wakeup_suspend_enabled;
645
646void qemu_register_wakeup_support(void)
647{
648 wakeup_suspend_enabled = true;
649}
650
651bool qemu_wakeup_suspend_enabled(void)
652{
653 return wakeup_suspend_enabled;
654}
655
656void qemu_system_killed(int signal, pid_t pid)
657{
658 shutdown_signal = signal;
659 shutdown_pid = pid;
e6dba048 660 shutdown_action = SHUTDOWN_ACTION_POWEROFF;
ba87e434
PB
661
662 /* Cannot call qemu_system_shutdown_request directly because
663 * we are in a signal handler.
664 */
665 shutdown_requested = SHUTDOWN_CAUSE_HOST_SIGNAL;
666 qemu_notify_event();
667}
668
0386f39b
CC
669void qemu_system_shutdown_request_with_code(ShutdownCause reason,
670 int exit_code)
671{
672 shutdown_exit_code = exit_code;
673 qemu_system_shutdown_request(reason);
674}
675
ba87e434
PB
676void qemu_system_shutdown_request(ShutdownCause reason)
677{
678 trace_qemu_system_shutdown_request(reason);
679 replay_shutdown_request(reason);
680 shutdown_requested = reason;
681 qemu_notify_event();
682}
683
684static void qemu_system_powerdown(void)
685{
686 qapi_event_send_powerdown();
687 notifier_list_notify(&powerdown_notifiers, NULL);
688}
689
690static void qemu_system_shutdown(ShutdownCause cause)
691{
692 qapi_event_send_shutdown(shutdown_caused_by_guest(cause), cause);
693 notifier_list_notify(&shutdown_notifiers, &cause);
694}
695
696void qemu_system_powerdown_request(void)
697{
698 trace_qemu_system_powerdown_request();
699 powerdown_requested = 1;
700 qemu_notify_event();
701}
702
703void qemu_register_powerdown_notifier(Notifier *notifier)
704{
705 notifier_list_add(&powerdown_notifiers, notifier);
706}
707
708void qemu_register_shutdown_notifier(Notifier *notifier)
709{
710 notifier_list_add(&shutdown_notifiers, notifier);
711}
712
713void qemu_system_debug_request(void)
714{
715 debug_requested = 1;
716 qemu_notify_event();
717}
718
0882caf4 719static bool main_loop_should_exit(int *status)
ba87e434
PB
720{
721 RunState r;
722 ShutdownCause request;
723
724 if (qemu_debug_requested()) {
725 vm_stop(RUN_STATE_DEBUG);
726 }
727 if (qemu_suspend_requested()) {
728 qemu_system_suspend();
729 }
730 request = qemu_shutdown_requested();
731 if (request) {
732 qemu_kill_report();
733 qemu_system_shutdown(request);
e6dba048 734 if (shutdown_action == SHUTDOWN_ACTION_PAUSE) {
ba87e434
PB
735 vm_stop(RUN_STATE_SHUTDOWN);
736 } else {
0386f39b
CC
737 if (shutdown_exit_code != EXIT_SUCCESS) {
738 *status = shutdown_exit_code;
739 } else if (request == SHUTDOWN_CAUSE_GUEST_PANIC &&
0882caf4
IL
740 panic_action == PANIC_ACTION_EXIT_FAILURE) {
741 *status = EXIT_FAILURE;
742 }
ba87e434
PB
743 return true;
744 }
745 }
746 request = qemu_reset_requested();
747 if (request) {
748 pause_all_vcpus();
749 qemu_system_reset(request);
750 resume_all_vcpus();
751 /*
752 * runstate can change in pause_all_vcpus()
753 * as iothread mutex is unlocked
754 */
755 if (!runstate_check(RUN_STATE_RUNNING) &&
756 !runstate_check(RUN_STATE_INMIGRATE) &&
757 !runstate_check(RUN_STATE_FINISH_MIGRATE)) {
758 runstate_set(RUN_STATE_PRELAUNCH);
759 }
760 }
761 if (qemu_wakeup_requested()) {
762 pause_all_vcpus();
763 qemu_system_wakeup();
764 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
765 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
766 resume_all_vcpus();
767 qapi_event_send_wakeup();
768 }
769 if (qemu_powerdown_requested()) {
770 qemu_system_powerdown();
771 }
772 if (qemu_vmstop_requested(&r)) {
773 vm_stop(r);
774 }
775 return false;
776}
777
0882caf4 778int qemu_main_loop(void)
ba87e434 779{
0882caf4 780 int status = EXIT_SUCCESS;
0882caf4
IL
781
782 while (!main_loop_should_exit(&status)) {
ba87e434 783 main_loop_wait(false);
ba87e434 784 }
0882caf4
IL
785
786 return status;
ba87e434
PB
787}
788
789void qemu_add_exit_notifier(Notifier *notify)
790{
791 notifier_list_add(&exit_notifiers, notify);
792}
793
794void qemu_remove_exit_notifier(Notifier *notify)
795{
796 notifier_remove(notify);
797}
798
799static void qemu_run_exit_notifiers(void)
800{
801 notifier_list_notify(&exit_notifiers, NULL);
802}
803
804void qemu_init_subsystems(void)
805{
6e1da3d3 806 Error *err = NULL;
ba87e434
PB
807
808 os_set_line_buffering();
809
810 module_call_init(MODULE_INIT_TRACE);
811
812 qemu_init_cpu_list();
813 qemu_init_cpu_loop();
195801d7 814 bql_lock();
ba87e434
PB
815
816 atexit(qemu_run_exit_notifiers);
817
818 module_call_init(MODULE_INIT_QOM);
819 module_call_init(MODULE_INIT_MIGRATION);
820
821 runstate_init();
822 precopy_infrastructure_init();
823 postcopy_infrastructure_init();
824 monitor_init_globals();
825
826 if (qcrypto_init(&err) < 0) {
827 error_reportf_err(err, "cannot initialize crypto: ");
828 exit(1);
829 }
830
831 os_setup_early_signal_handling();
832
833 bdrv_init_with_whitelist();
834 socket_init();
835}
836
837
66bbe3e9 838void qemu_cleanup(int status)
ba87e434 839{
66bbe3e9 840 gdb_exit(status);
ba87e434
PB
841
842 /*
843 * cleaning up the migration object cancels any existing migration
844 * try to do this early so that it also stops using devices.
845 */
846 migration_shutdown();
847
1895b977
SL
848 /*
849 * Close the exports before draining the block layer. The export
850 * drivers may have coroutines yielding on it, so we need to clean
851 * them up before the drain, as otherwise they may be get stuck in
852 * blk_wait_while_drained().
853 */
854 blk_exp_close_all();
855
ca2a5e63
FE
856
857 /* No more vcpu or device emulation activity beyond this point */
858 vm_shutdown();
859 replay_finish();
860
ba87e434
PB
861 /*
862 * We must cancel all block jobs while the block layer is drained,
863 * or cancelling will be affected by throttling and thus may block
864 * for an extended period of time.
ca2a5e63
FE
865 * Begin the drained section after vm_shutdown() to avoid requests being
866 * stuck in the BlockBackend's request queue.
ba87e434
PB
867 * We do not need to end this section, because we do not want any
868 * requests happening from here on anyway.
869 */
870 bdrv_drain_all_begin();
ba87e434
PB
871 job_cancel_sync_all();
872 bdrv_close_all();
873
874 /* vhost-user must be cleaned up before chardevs. */
875 tpm_cleanup();
876 net_cleanup();
877 audio_cleanup();
878 monitor_cleanup();
879 qemu_chr_cleanup();
880 user_creatable_cleanup();
881 /* TODO: unref root container, check all devices are ok */
882}