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