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