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