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