xref: /openbmc/qemu/system/runstate.c (revision f96b157ebb93f94cd56ebbc99bc20982b8fd86ef)
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 "hw/resettable.h"
36 #include "migration/misc.h"
37 #include "migration/postcopy-ram.h"
38 #include "monitor/monitor.h"
39 #include "net/net.h"
40 #include "net/vhost_net.h"
41 #include "qapi/error.h"
42 #include "qapi/qapi-commands-run-state.h"
43 #include "qapi/qapi-events-run-state.h"
44 #include "qemu/accel.h"
45 #include "qemu/error-report.h"
46 #include "qemu/job.h"
47 #include "qemu/log.h"
48 #include "qemu/module.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 "system/cpus.h"
55 #include "system/qtest.h"
56 #include "system/replay.h"
57 #include "system/reset.h"
58 #include "system/runstate.h"
59 #include "system/runstate-action.h"
60 #include "system/system.h"
61 #include "system/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 const RunStateTransition replay_play_runstate_transitions_def[] = {
186     { RUN_STATE_SHUTDOWN, RUN_STATE_RUNNING},
187 
188     { RUN_STATE__MAX, RUN_STATE__MAX },
189 };
190 
191 static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
192 
runstate_check(RunState state)193 bool runstate_check(RunState state)
194 {
195     return current_run_state == state;
196 }
197 
transitions_set_valid(const RunStateTransition * rst)198 static void transitions_set_valid(const RunStateTransition *rst)
199 {
200     const RunStateTransition *p;
201 
202     for (p = rst; p->from != RUN_STATE__MAX; p++) {
203         runstate_valid_transitions[p->from][p->to] = true;
204     }
205 }
206 
runstate_replay_enable(void)207 void runstate_replay_enable(void)
208 {
209     assert(replay_mode != REPLAY_MODE_NONE);
210 
211     if (replay_mode == REPLAY_MODE_PLAY) {
212         /*
213          * When reverse-debugging, it is possible to move state from
214          * shutdown to running.
215          */
216         transitions_set_valid(&replay_play_runstate_transitions_def[0]);
217     }
218 }
219 
runstate_init(void)220 static void runstate_init(void)
221 {
222     memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
223 
224     transitions_set_valid(&runstate_transitions_def[0]);
225 
226     qemu_mutex_init(&vmstop_lock);
227 }
228 
229 /* This function will abort() on invalid state transitions */
runstate_set(RunState new_state)230 void runstate_set(RunState new_state)
231 {
232     assert(new_state < RUN_STATE__MAX);
233 
234     trace_runstate_set(current_run_state, RunState_str(current_run_state),
235                        new_state, RunState_str(new_state));
236 
237     if (current_run_state == new_state) {
238         return;
239     }
240 
241     if (!runstate_valid_transitions[current_run_state][new_state]) {
242         error_report("invalid runstate transition: '%s' -> '%s'",
243                      RunState_str(current_run_state),
244                      RunState_str(new_state));
245         abort();
246     }
247 
248     current_run_state = new_state;
249 }
250 
runstate_get(void)251 RunState runstate_get(void)
252 {
253     return current_run_state;
254 }
255 
runstate_is_running(void)256 bool runstate_is_running(void)
257 {
258     return runstate_check(RUN_STATE_RUNNING);
259 }
260 
runstate_needs_reset(void)261 bool runstate_needs_reset(void)
262 {
263     return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
264         runstate_check(RUN_STATE_SHUTDOWN);
265 }
266 
qmp_query_status(Error ** errp)267 StatusInfo *qmp_query_status(Error **errp)
268 {
269     StatusInfo *info = g_malloc0(sizeof(*info));
270 
271     info->running = runstate_is_running();
272     info->status = current_run_state;
273 
274     return info;
275 }
276 
qemu_vmstop_requested(RunState * r)277 bool qemu_vmstop_requested(RunState *r)
278 {
279     qemu_mutex_lock(&vmstop_lock);
280     *r = vmstop_requested;
281     vmstop_requested = RUN_STATE__MAX;
282     qemu_mutex_unlock(&vmstop_lock);
283     return *r < RUN_STATE__MAX;
284 }
285 
qemu_system_vmstop_request_prepare(void)286 void qemu_system_vmstop_request_prepare(void)
287 {
288     qemu_mutex_lock(&vmstop_lock);
289 }
290 
qemu_system_vmstop_request(RunState state)291 void qemu_system_vmstop_request(RunState state)
292 {
293     vmstop_requested = state;
294     qemu_mutex_unlock(&vmstop_lock);
295     qemu_notify_event();
296 }
297 struct VMChangeStateEntry {
298     VMChangeStateHandler *cb;
299     VMChangeStateHandler *prepare_cb;
300     VMChangeStateHandlerWithRet *cb_ret;
301     void *opaque;
302     QTAILQ_ENTRY(VMChangeStateEntry) entries;
303     int priority;
304 };
305 
306 static QTAILQ_HEAD(, VMChangeStateEntry) vm_change_state_head =
307     QTAILQ_HEAD_INITIALIZER(vm_change_state_head);
308 
qemu_add_vm_change_state_handler_prio(VMChangeStateHandler * cb,void * opaque,int priority)309 VMChangeStateEntry *qemu_add_vm_change_state_handler_prio(
310         VMChangeStateHandler *cb, void *opaque, int priority)
311 {
312     return qemu_add_vm_change_state_handler_prio_full(cb, NULL, NULL,
313                                                       opaque, priority);
314 }
315 
316 VMChangeStateEntry *
qemu_add_vm_change_state_handler_prio_full(VMChangeStateHandler * cb,VMChangeStateHandler * prepare_cb,VMChangeStateHandlerWithRet * cb_ret,void * opaque,int priority)317 qemu_add_vm_change_state_handler_prio_full(VMChangeStateHandler *cb,
318                                            VMChangeStateHandler *prepare_cb,
319                                            VMChangeStateHandlerWithRet *cb_ret,
320                                            void *opaque, int priority)
321 {
322     VMChangeStateEntry *e;
323     VMChangeStateEntry *other;
324 
325     e = g_malloc0(sizeof(*e));
326     e->cb = cb;
327     e->prepare_cb = prepare_cb;
328     e->cb_ret = cb_ret;
329     e->opaque = opaque;
330     e->priority = priority;
331 
332     /* Keep list sorted in ascending priority order */
333     QTAILQ_FOREACH(other, &vm_change_state_head, entries) {
334         if (priority < other->priority) {
335             QTAILQ_INSERT_BEFORE(other, e, entries);
336             return e;
337         }
338     }
339 
340     QTAILQ_INSERT_TAIL(&vm_change_state_head, e, entries);
341     return e;
342 }
343 
qemu_add_vm_change_state_handler(VMChangeStateHandler * cb,void * opaque)344 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
345                                                      void *opaque)
346 {
347     return qemu_add_vm_change_state_handler_prio(cb, opaque, 0);
348 }
349 
qemu_del_vm_change_state_handler(VMChangeStateEntry * e)350 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
351 {
352     QTAILQ_REMOVE(&vm_change_state_head, e, entries);
353     g_free(e);
354 }
355 
vm_state_notify(bool running,RunState state)356 int vm_state_notify(bool running, RunState state)
357 {
358     VMChangeStateEntry *e, *next;
359     int ret = 0;
360 
361     trace_vm_state_notify(running, state, RunState_str(state));
362 
363     if (running) {
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 
370         QTAILQ_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
371             if (e->cb) {
372                 e->cb(e->opaque, running, state);
373             } else if (e->cb_ret) {
374                 /*
375                  * Here ignore the return value of cb_ret because
376                  * we only care about the stopping the device during
377                  * the VM live migration to indicate whether the
378                  * connection between qemu and backend is normal.
379                  */
380                 e->cb_ret(e->opaque, running, state);
381             }
382         }
383     } else {
384         QTAILQ_FOREACH_REVERSE_SAFE(e, &vm_change_state_head, entries, next) {
385             if (e->prepare_cb) {
386                 e->prepare_cb(e->opaque, running, state);
387             }
388         }
389 
390         QTAILQ_FOREACH_REVERSE_SAFE(e, &vm_change_state_head, entries, next) {
391             if (e->cb) {
392                 e->cb(e->opaque, running, state);
393             } else if (e->cb_ret) {
394                 /*
395                  * We should execute all registered callbacks even if
396                  * one of them returns failure, otherwise, some cleanup
397                  * work of the device will be skipped.
398                  */
399                 ret |= e->cb_ret(e->opaque, running, state);
400             }
401         }
402     }
403     return ret;
404 }
405 
406 static ShutdownCause reset_requested;
407 static ShutdownCause shutdown_requested;
408 static int shutdown_exit_code = EXIT_SUCCESS;
409 static int shutdown_signal;
410 static bool force_shutdown;
411 static pid_t shutdown_pid;
412 static int powerdown_requested;
413 static int debug_requested;
414 static int suspend_requested;
415 static WakeupReason wakeup_reason;
416 static NotifierList powerdown_notifiers =
417     NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
418 static NotifierList suspend_notifiers =
419     NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
420 static NotifierList wakeup_notifiers =
421     NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
422 static NotifierList shutdown_notifiers =
423     NOTIFIER_LIST_INITIALIZER(shutdown_notifiers);
424 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
425 
qemu_shutdown_requested_get(void)426 ShutdownCause qemu_shutdown_requested_get(void)
427 {
428     return shutdown_requested;
429 }
430 
qemu_force_shutdown_requested(void)431 bool qemu_force_shutdown_requested(void)
432 {
433     return force_shutdown;
434 }
435 
qemu_reset_requested_get(void)436 ShutdownCause qemu_reset_requested_get(void)
437 {
438     return reset_requested;
439 }
440 
qemu_shutdown_requested(void)441 static int qemu_shutdown_requested(void)
442 {
443     return qatomic_xchg(&shutdown_requested, SHUTDOWN_CAUSE_NONE);
444 }
445 
qemu_kill_report(void)446 static void qemu_kill_report(void)
447 {
448     if (!qtest_driver() && shutdown_signal) {
449         if (shutdown_pid == 0) {
450             /* This happens for eg ^C at the terminal, so it's worth
451              * avoiding printing an odd message in that case.
452              */
453             error_report("terminating on signal %d", shutdown_signal);
454         } else {
455             char *shutdown_cmd = qemu_get_pid_name(shutdown_pid);
456 
457             error_report("terminating on signal %d from pid " FMT_pid " (%s)",
458                          shutdown_signal, shutdown_pid,
459                          shutdown_cmd ? shutdown_cmd : "<unknown process>");
460             g_free(shutdown_cmd);
461         }
462         shutdown_signal = 0;
463     }
464 }
465 
qemu_reset_requested(void)466 static ShutdownCause qemu_reset_requested(void)
467 {
468     ShutdownCause r = reset_requested;
469 
470     if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
471         reset_requested = SHUTDOWN_CAUSE_NONE;
472         return r;
473     }
474     return SHUTDOWN_CAUSE_NONE;
475 }
476 
qemu_suspend_requested(void)477 static int qemu_suspend_requested(void)
478 {
479     int r = suspend_requested;
480     if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
481         suspend_requested = 0;
482         return r;
483     }
484     return false;
485 }
486 
qemu_wakeup_requested(void)487 static WakeupReason qemu_wakeup_requested(void)
488 {
489     return wakeup_reason;
490 }
491 
qemu_powerdown_requested(void)492 static int qemu_powerdown_requested(void)
493 {
494     int r = powerdown_requested;
495     powerdown_requested = 0;
496     return r;
497 }
498 
qemu_debug_requested(void)499 static int qemu_debug_requested(void)
500 {
501     int r = debug_requested;
502     debug_requested = 0;
503     return r;
504 }
505 
506 /*
507  * Reset the VM. Issue an event unless @reason is SHUTDOWN_CAUSE_NONE.
508  */
qemu_system_reset(ShutdownCause reason)509 void qemu_system_reset(ShutdownCause reason)
510 {
511     MachineClass *mc;
512     ResetType type;
513 
514     mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
515 
516     cpu_synchronize_all_states();
517 
518     switch (reason) {
519     case SHUTDOWN_CAUSE_SNAPSHOT_LOAD:
520         type = RESET_TYPE_SNAPSHOT_LOAD;
521         break;
522     default:
523         type = RESET_TYPE_COLD;
524     }
525     if (mc && mc->reset) {
526         mc->reset(current_machine, type);
527     } else {
528         qemu_devices_reset(type);
529     }
530     switch (reason) {
531     case SHUTDOWN_CAUSE_NONE:
532     case SHUTDOWN_CAUSE_SUBSYSTEM_RESET:
533     case SHUTDOWN_CAUSE_SNAPSHOT_LOAD:
534         break;
535     default:
536         qapi_event_send_reset(shutdown_caused_by_guest(reason), reason);
537     }
538 
539     /*
540      * Some boards use the machine reset callback to point CPUs to the firmware
541      * entry point.  Assume that this is not the case for boards that support
542      * non-resettable CPUs (currently used only for confidential guests), in
543      * which case cpu_synchronize_all_post_init() is enough because
544      * it does _more_  than cpu_synchronize_all_post_reset().
545      */
546     if (cpus_are_resettable()) {
547         cpu_synchronize_all_post_reset();
548     } else {
549         assert(runstate_check(RUN_STATE_PRELAUNCH));
550     }
551 
552     vm_set_suspended(false);
553 }
554 
555 /*
556  * Wake the VM after suspend.
557  */
qemu_system_wakeup(void)558 static void qemu_system_wakeup(void)
559 {
560     MachineClass *mc;
561 
562     mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
563 
564     if (mc && mc->wakeup) {
565         mc->wakeup(current_machine);
566     }
567 }
568 
tdx_parse_panic_message(char * message)569 static char *tdx_parse_panic_message(char *message)
570 {
571     bool printable = false;
572     char *buf = NULL;
573     int len = 0, i;
574 
575     /*
576      * Although message is defined as a json string, we shouldn't
577      * unconditionally treat it as is because the guest generated it and
578      * it's not necessarily trustable.
579      */
580     if (message) {
581         /* The caller guarantees the NULL-terminated string. */
582         len = strlen(message);
583 
584         printable = len > 0;
585         for (i = 0; i < len; i++) {
586             if (!(0x20 <= message[i] && message[i] <= 0x7e)) {
587                 printable = false;
588                 break;
589             }
590         }
591     }
592 
593     if (len == 0) {
594         buf = g_malloc(1);
595         buf[0] = '\0';
596     } else {
597         if (!printable) {
598             /* 3 = length of "%02x " */
599             buf = g_malloc(len * 3);
600             for (i = 0; i < len; i++) {
601                 if (message[i] == '\0') {
602                     break;
603                 } else {
604                     sprintf(buf + 3 * i, "%02x ", message[i]);
605                 }
606             }
607             if (i > 0) {
608                 /* replace the last ' '(space) to NULL */
609                 buf[i * 3 - 1] = '\0';
610             } else {
611                 buf[0] = '\0';
612             }
613         } else {
614             buf = g_strdup(message);
615         }
616     }
617 
618     return buf;
619 }
620 
qemu_system_guest_panicked(GuestPanicInformation * info)621 void qemu_system_guest_panicked(GuestPanicInformation *info)
622 {
623     qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed");
624 
625     if (current_cpu) {
626         current_cpu->crash_occurred = true;
627     }
628     /*
629      * TODO:  Currently the available panic actions are: none, pause, and
630      * shutdown, but in principle debug and reset could be supported as well.
631      * Investigate any potential use cases for the unimplemented actions.
632      */
633     if (panic_action == PANIC_ACTION_PAUSE
634         || (panic_action == PANIC_ACTION_SHUTDOWN && shutdown_action == SHUTDOWN_ACTION_PAUSE)) {
635         qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE, info);
636         vm_stop(RUN_STATE_GUEST_PANICKED);
637     } else if (panic_action == PANIC_ACTION_SHUTDOWN ||
638                panic_action == PANIC_ACTION_EXIT_FAILURE) {
639         qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF, info);
640         vm_stop(RUN_STATE_GUEST_PANICKED);
641         qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_PANIC);
642     } else {
643         qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_RUN, info);
644     }
645 
646     if (info) {
647         if (info->type == GUEST_PANIC_INFORMATION_TYPE_HYPER_V) {
648             qemu_log_mask(LOG_GUEST_ERROR, "\nHV crash parameters: (%#"PRIx64
649                           " %#"PRIx64" %#"PRIx64" %#"PRIx64" %#"PRIx64")\n",
650                           info->u.hyper_v.arg1,
651                           info->u.hyper_v.arg2,
652                           info->u.hyper_v.arg3,
653                           info->u.hyper_v.arg4,
654                           info->u.hyper_v.arg5);
655         } else if (info->type == GUEST_PANIC_INFORMATION_TYPE_S390) {
656             qemu_log_mask(LOG_GUEST_ERROR, " on cpu %d: %s\n"
657                           "PSW: 0x%016" PRIx64 " 0x%016" PRIx64"\n",
658                           info->u.s390.core,
659                           S390CrashReason_str(info->u.s390.reason),
660                           info->u.s390.psw_mask,
661                           info->u.s390.psw_addr);
662         } else if (info->type == GUEST_PANIC_INFORMATION_TYPE_TDX) {
663             char *message = tdx_parse_panic_message(info->u.tdx.message);
664             qemu_log_mask(LOG_GUEST_ERROR,
665                           "\nTDX guest reports fatal error."
666                           " error code: 0x%" PRIx32 " error message:\"%s\"\n",
667                           info->u.tdx.error_code, message);
668             g_free(message);
669             if (info->u.tdx.gpa != -1ull) {
670                 qemu_log_mask(LOG_GUEST_ERROR, "Additional error information "
671                               "can be found at gpa page: 0x%" PRIx64 "\n",
672                               info->u.tdx.gpa);
673             }
674         }
675 
676         qapi_free_GuestPanicInformation(info);
677     }
678 }
679 
qemu_system_guest_crashloaded(GuestPanicInformation * info)680 void qemu_system_guest_crashloaded(GuestPanicInformation *info)
681 {
682     qemu_log_mask(LOG_GUEST_ERROR, "Guest crash loaded");
683     qapi_event_send_guest_crashloaded(GUEST_PANIC_ACTION_RUN, info);
684     qapi_free_GuestPanicInformation(info);
685 }
686 
qemu_system_guest_pvshutdown(void)687 void qemu_system_guest_pvshutdown(void)
688 {
689     qapi_event_send_guest_pvshutdown();
690     qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
691 }
692 
qemu_system_reset_request(ShutdownCause reason)693 void qemu_system_reset_request(ShutdownCause reason)
694 {
695     if (reboot_action == REBOOT_ACTION_SHUTDOWN &&
696         reason != SHUTDOWN_CAUSE_SUBSYSTEM_RESET) {
697         shutdown_requested = reason;
698     } else if (!cpus_are_resettable()) {
699         error_report("cpus are not resettable, terminating");
700         shutdown_requested = reason;
701     } else {
702         reset_requested = reason;
703     }
704     cpu_stop_current();
705     qemu_notify_event();
706 }
707 
qemu_system_suspend(void)708 static void qemu_system_suspend(void)
709 {
710     pause_all_vcpus();
711     notifier_list_notify(&suspend_notifiers, NULL);
712     runstate_set(RUN_STATE_SUSPENDED);
713     qapi_event_send_suspend();
714 }
715 
qemu_system_suspend_request(void)716 void qemu_system_suspend_request(void)
717 {
718     if (runstate_check(RUN_STATE_SUSPENDED)) {
719         return;
720     }
721     suspend_requested = 1;
722     cpu_stop_current();
723     qemu_notify_event();
724 }
725 
qemu_register_suspend_notifier(Notifier * notifier)726 void qemu_register_suspend_notifier(Notifier *notifier)
727 {
728     notifier_list_add(&suspend_notifiers, notifier);
729 }
730 
qemu_system_wakeup_request(WakeupReason reason,Error ** errp)731 void qemu_system_wakeup_request(WakeupReason reason, Error **errp)
732 {
733     trace_system_wakeup_request(reason);
734 
735     if (!runstate_check(RUN_STATE_SUSPENDED)) {
736         error_setg(errp,
737                    "Unable to wake up: guest is not in suspended state");
738         return;
739     }
740     if (!(wakeup_reason_mask & (1 << reason))) {
741         return;
742     }
743     runstate_set(RUN_STATE_RUNNING);
744     wakeup_reason = reason;
745     qemu_notify_event();
746 }
747 
qemu_system_wakeup_enable(WakeupReason reason,bool enabled)748 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
749 {
750     if (enabled) {
751         wakeup_reason_mask |= (1 << reason);
752     } else {
753         wakeup_reason_mask &= ~(1 << reason);
754     }
755 }
756 
qemu_register_wakeup_notifier(Notifier * notifier)757 void qemu_register_wakeup_notifier(Notifier *notifier)
758 {
759     notifier_list_add(&wakeup_notifiers, notifier);
760 }
761 
762 static bool wakeup_suspend_enabled;
763 
qemu_register_wakeup_support(void)764 void qemu_register_wakeup_support(void)
765 {
766     wakeup_suspend_enabled = true;
767 }
768 
qemu_wakeup_suspend_enabled(void)769 bool qemu_wakeup_suspend_enabled(void)
770 {
771     return wakeup_suspend_enabled;
772 }
773 
qemu_system_killed(int signal,pid_t pid)774 void qemu_system_killed(int signal, pid_t pid)
775 {
776     shutdown_signal = signal;
777     shutdown_pid = pid;
778     shutdown_action = SHUTDOWN_ACTION_POWEROFF;
779 
780     /* Cannot call qemu_system_shutdown_request directly because
781      * we are in a signal handler.
782      */
783     shutdown_requested = SHUTDOWN_CAUSE_HOST_SIGNAL;
784     force_shutdown = true;
785     qemu_notify_event();
786 }
787 
qemu_system_shutdown_request_with_code(ShutdownCause reason,int exit_code)788 void qemu_system_shutdown_request_with_code(ShutdownCause reason,
789                                             int exit_code)
790 {
791     shutdown_exit_code = exit_code;
792     qemu_system_shutdown_request(reason);
793 }
794 
qemu_system_shutdown_request(ShutdownCause reason)795 void qemu_system_shutdown_request(ShutdownCause reason)
796 {
797     trace_qemu_system_shutdown_request(reason);
798     replay_shutdown_request(reason);
799     shutdown_requested = reason;
800     if (reason == SHUTDOWN_CAUSE_HOST_QMP_QUIT) {
801         force_shutdown = true;
802     }
803     qemu_notify_event();
804 }
805 
qemu_system_powerdown(void)806 static void qemu_system_powerdown(void)
807 {
808     qapi_event_send_powerdown();
809     notifier_list_notify(&powerdown_notifiers, NULL);
810 }
811 
qemu_system_shutdown(ShutdownCause cause)812 static void qemu_system_shutdown(ShutdownCause cause)
813 {
814     qapi_event_send_shutdown(shutdown_caused_by_guest(cause), cause);
815     notifier_list_notify(&shutdown_notifiers, &cause);
816 }
817 
qemu_system_powerdown_request(void)818 void qemu_system_powerdown_request(void)
819 {
820     trace_qemu_system_powerdown_request();
821     powerdown_requested = 1;
822     qemu_notify_event();
823 }
824 
qemu_register_powerdown_notifier(Notifier * notifier)825 void qemu_register_powerdown_notifier(Notifier *notifier)
826 {
827     notifier_list_add(&powerdown_notifiers, notifier);
828 }
829 
qemu_register_shutdown_notifier(Notifier * notifier)830 void qemu_register_shutdown_notifier(Notifier *notifier)
831 {
832     notifier_list_add(&shutdown_notifiers, notifier);
833 }
834 
qemu_system_debug_request(void)835 void qemu_system_debug_request(void)
836 {
837     debug_requested = 1;
838     qemu_notify_event();
839 }
840 
main_loop_should_exit(int * status)841 static bool main_loop_should_exit(int *status)
842 {
843     RunState r;
844     ShutdownCause request;
845 
846     if (qemu_debug_requested()) {
847         vm_stop(RUN_STATE_DEBUG);
848     }
849     if (qemu_suspend_requested()) {
850         qemu_system_suspend();
851     }
852     request = qemu_shutdown_requested();
853     if (request) {
854         qemu_kill_report();
855         qemu_system_shutdown(request);
856         if (shutdown_action == SHUTDOWN_ACTION_PAUSE) {
857             vm_stop(RUN_STATE_SHUTDOWN);
858         } else {
859             if (shutdown_exit_code != EXIT_SUCCESS) {
860                 *status = shutdown_exit_code;
861             } else if (request == SHUTDOWN_CAUSE_GUEST_PANIC &&
862                 panic_action == PANIC_ACTION_EXIT_FAILURE) {
863                 *status = EXIT_FAILURE;
864             }
865             return true;
866         }
867     }
868     request = qemu_reset_requested();
869     if (request) {
870         pause_all_vcpus();
871         qemu_system_reset(request);
872         resume_all_vcpus();
873         /*
874          * runstate can change in pause_all_vcpus()
875          * as iothread mutex is unlocked
876          */
877         if (!runstate_check(RUN_STATE_RUNNING) &&
878                 !runstate_check(RUN_STATE_INMIGRATE) &&
879                 !runstate_check(RUN_STATE_FINISH_MIGRATE)) {
880             runstate_set(RUN_STATE_PRELAUNCH);
881         }
882     }
883     if (qemu_wakeup_requested()) {
884         pause_all_vcpus();
885         qemu_system_wakeup();
886         notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
887         wakeup_reason = QEMU_WAKEUP_REASON_NONE;
888         resume_all_vcpus();
889         qapi_event_send_wakeup();
890     }
891     if (qemu_powerdown_requested()) {
892         qemu_system_powerdown();
893     }
894     if (qemu_vmstop_requested(&r)) {
895         vm_stop(r);
896     }
897     return false;
898 }
899 
qemu_main_loop(void)900 int qemu_main_loop(void)
901 {
902     int status = EXIT_SUCCESS;
903 
904     while (!main_loop_should_exit(&status)) {
905         main_loop_wait(false);
906     }
907 
908     return status;
909 }
910 
qemu_add_exit_notifier(Notifier * notify)911 void qemu_add_exit_notifier(Notifier *notify)
912 {
913     notifier_list_add(&exit_notifiers, notify);
914 }
915 
qemu_remove_exit_notifier(Notifier * notify)916 void qemu_remove_exit_notifier(Notifier *notify)
917 {
918     notifier_remove(notify);
919 }
920 
qemu_run_exit_notifiers(void)921 static void qemu_run_exit_notifiers(void)
922 {
923     BQL_LOCK_GUARD();
924     notifier_list_notify(&exit_notifiers, NULL);
925 }
926 
qemu_init_subsystems(void)927 void qemu_init_subsystems(void)
928 {
929     Error *err = NULL;
930 
931     os_set_line_buffering();
932 
933     module_call_init(MODULE_INIT_TRACE);
934 
935     qemu_init_cpu_list();
936     qemu_init_cpu_loop();
937     bql_lock();
938 
939     atexit(qemu_run_exit_notifiers);
940 
941     module_call_init(MODULE_INIT_QOM);
942     module_call_init(MODULE_INIT_MIGRATION);
943 
944     runstate_init();
945     precopy_infrastructure_init();
946     postcopy_infrastructure_init();
947     monitor_init_globals();
948 
949     if (qcrypto_init(&err) < 0) {
950         error_reportf_err(err, "cannot initialize crypto: ");
951         exit(1);
952     }
953 
954     os_setup_early_signal_handling();
955 
956     bdrv_init_with_whitelist();
957     socket_init();
958 }
959 
960 
qemu_cleanup(int status)961 void qemu_cleanup(int status)
962 {
963     gdb_exit(status);
964 
965     /*
966      * cleaning up the migration object cancels any existing migration
967      * try to do this early so that it also stops using devices.
968      */
969     migration_shutdown();
970 
971     /*
972      * Close the exports before draining the block layer. The export
973      * drivers may have coroutines yielding on it, so we need to clean
974      * them up before the drain, as otherwise they may be get stuck in
975      * blk_wait_while_drained().
976      */
977     blk_exp_close_all();
978 
979 
980     /* No more vcpu or device emulation activity beyond this point */
981     vm_shutdown();
982     replay_finish();
983 
984     /*
985      * We must cancel all block jobs while the block layer is drained,
986      * or cancelling will be affected by throttling and thus may block
987      * for an extended period of time.
988      * Begin the drained section after vm_shutdown() to avoid requests being
989      * stuck in the BlockBackend's request queue.
990      * We do not need to end this section, because we do not want any
991      * requests happening from here on anyway.
992      */
993     bdrv_drain_all_begin();
994     job_cancel_sync_all();
995     bdrv_close_all();
996 
997     /* vhost-user must be cleaned up before chardevs.  */
998     tpm_cleanup();
999     net_cleanup();
1000     audio_cleanup();
1001     monitor_cleanup();
1002     qemu_chr_cleanup();
1003     user_creatable_cleanup();
1004     /* TODO: unref root container, check all devices are ok */
1005 }
1006