]>
Commit | Line | Data |
---|---|---|
296af7c9 BS |
1 | /* |
2 | * QEMU System Emulator | |
3 | * | |
4 | * Copyright (c) 2003-2008 Fabrice Bellard | |
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 | ||
7b31bbc2 | 25 | #include "qemu/osdep.h" |
83c9089e | 26 | #include "monitor/monitor.h" |
d5d2b15e | 27 | #include "qemu/coroutine-tls.h" |
e688df6b | 28 | #include "qapi/error.h" |
df7a1f48 | 29 | #include "qapi/qapi-commands-machine.h" |
112ed241 | 30 | #include "qapi/qapi-commands-misc.h" |
9af23989 | 31 | #include "qapi/qapi-events-run-state.h" |
a4e15de9 | 32 | #include "qapi/qmp/qerror.h" |
022c62cb | 33 | #include "exec/gdbstub.h" |
b3946626 | 34 | #include "sysemu/hw_accel.h" |
73842ef0 | 35 | #include "exec/cpu-common.h" |
1de7afc9 | 36 | #include "qemu/thread.h" |
e2c1c34f | 37 | #include "qemu/main-loop.h" |
30865f31 | 38 | #include "qemu/plugin.h" |
9c17d615 | 39 | #include "sysemu/cpus.h" |
9c09a251 | 40 | #include "qemu/guest-random.h" |
9cb805fd | 41 | #include "hw/nmi.h" |
8b427044 | 42 | #include "sysemu/replay.h" |
54d31236 | 43 | #include "sysemu/runstate.h" |
740b1759 | 44 | #include "sysemu/cpu-timers.h" |
faf20793 | 45 | #include "sysemu/whpx.h" |
5cc8767d | 46 | #include "hw/boards.h" |
650d103d | 47 | #include "hw/hw.h" |
8af3f5c6 | 48 | #include "trace.h" |
0ff0fc19 | 49 | |
6d9cb73c JK |
50 | #ifdef CONFIG_LINUX |
51 | ||
52 | #include <sys/prctl.h> | |
53 | ||
c0532a76 MT |
54 | #ifndef PR_MCE_KILL |
55 | #define PR_MCE_KILL 33 | |
56 | #endif | |
57 | ||
6d9cb73c JK |
58 | #ifndef PR_MCE_KILL_SET |
59 | #define PR_MCE_KILL_SET 1 | |
60 | #endif | |
61 | ||
62 | #ifndef PR_MCE_KILL_EARLY | |
63 | #define PR_MCE_KILL_EARLY 1 | |
64 | #endif | |
65 | ||
66 | #endif /* CONFIG_LINUX */ | |
67 | ||
195801d7 SH |
68 | /* The Big QEMU Lock (BQL) */ |
69 | static QemuMutex bql; | |
bd1f7ff4 | 70 | |
5f74af8b PMD |
71 | /* |
72 | * The chosen accelerator is supposed to register this. | |
73 | */ | |
74 | static const AccelOpsClass *cpus_accel; | |
75 | ||
321bc0b2 TC |
76 | bool cpu_is_stopped(CPUState *cpu) |
77 | { | |
78 | return cpu->stopped || !runstate_is_running(); | |
79 | } | |
80 | ||
430065da | 81 | bool cpu_work_list_empty(CPUState *cpu) |
0c0fcc20 | 82 | { |
25e82fb7 | 83 | return QSIMPLEQ_EMPTY_ATOMIC(&cpu->work_list); |
0c0fcc20 EC |
84 | } |
85 | ||
430065da | 86 | bool cpu_thread_is_idle(CPUState *cpu) |
ac873f1e | 87 | { |
0c0fcc20 | 88 | if (cpu->stop || !cpu_work_list_empty(cpu)) { |
ac873f1e PM |
89 | return false; |
90 | } | |
321bc0b2 | 91 | if (cpu_is_stopped(cpu)) { |
ac873f1e PM |
92 | return true; |
93 | } | |
ad7d684d | 94 | if (!cpu->halted || cpu_has_work(cpu)) { |
ac873f1e PM |
95 | return false; |
96 | } | |
ad7d684d PMD |
97 | if (cpus_accel->cpu_thread_is_idle) { |
98 | return cpus_accel->cpu_thread_is_idle(cpu); | |
99 | } | |
ac873f1e PM |
100 | return true; |
101 | } | |
102 | ||
740b1759 | 103 | bool all_cpu_threads_idle(void) |
ac873f1e | 104 | { |
182735ef | 105 | CPUState *cpu; |
ac873f1e | 106 | |
bdc44640 | 107 | CPU_FOREACH(cpu) { |
182735ef | 108 | if (!cpu_thread_is_idle(cpu)) { |
ac873f1e PM |
109 | return false; |
110 | } | |
111 | } | |
112 | return true; | |
113 | } | |
114 | ||
296af7c9 BS |
115 | /***********************************************************/ |
116 | void hw_error(const char *fmt, ...) | |
117 | { | |
118 | va_list ap; | |
55e5c285 | 119 | CPUState *cpu; |
296af7c9 BS |
120 | |
121 | va_start(ap, fmt); | |
122 | fprintf(stderr, "qemu: hardware error: "); | |
123 | vfprintf(stderr, fmt, ap); | |
124 | fprintf(stderr, "\n"); | |
bdc44640 | 125 | CPU_FOREACH(cpu) { |
55e5c285 | 126 | fprintf(stderr, "CPU #%d:\n", cpu->cpu_index); |
90c84c56 | 127 | cpu_dump_state(cpu, stderr, CPU_DUMP_FPU); |
296af7c9 BS |
128 | } |
129 | va_end(ap); | |
130 | abort(); | |
131 | } | |
132 | ||
133 | void cpu_synchronize_all_states(void) | |
134 | { | |
182735ef | 135 | CPUState *cpu; |
296af7c9 | 136 | |
bdc44640 | 137 | CPU_FOREACH(cpu) { |
182735ef | 138 | cpu_synchronize_state(cpu); |
296af7c9 BS |
139 | } |
140 | } | |
141 | ||
142 | void cpu_synchronize_all_post_reset(void) | |
143 | { | |
182735ef | 144 | CPUState *cpu; |
296af7c9 | 145 | |
bdc44640 | 146 | CPU_FOREACH(cpu) { |
182735ef | 147 | cpu_synchronize_post_reset(cpu); |
296af7c9 BS |
148 | } |
149 | } | |
150 | ||
151 | void cpu_synchronize_all_post_init(void) | |
152 | { | |
182735ef | 153 | CPUState *cpu; |
296af7c9 | 154 | |
bdc44640 | 155 | CPU_FOREACH(cpu) { |
182735ef | 156 | cpu_synchronize_post_init(cpu); |
296af7c9 BS |
157 | } |
158 | } | |
159 | ||
75e972da DG |
160 | void cpu_synchronize_all_pre_loadvm(void) |
161 | { | |
162 | CPUState *cpu; | |
163 | ||
164 | CPU_FOREACH(cpu) { | |
165 | cpu_synchronize_pre_loadvm(cpu); | |
166 | } | |
167 | } | |
168 | ||
430065da CF |
169 | void cpu_synchronize_state(CPUState *cpu) |
170 | { | |
994aa172 | 171 | if (cpus_accel->synchronize_state) { |
430065da CF |
172 | cpus_accel->synchronize_state(cpu); |
173 | } | |
430065da CF |
174 | } |
175 | ||
176 | void cpu_synchronize_post_reset(CPUState *cpu) | |
177 | { | |
994aa172 | 178 | if (cpus_accel->synchronize_post_reset) { |
430065da CF |
179 | cpus_accel->synchronize_post_reset(cpu); |
180 | } | |
430065da CF |
181 | } |
182 | ||
183 | void cpu_synchronize_post_init(CPUState *cpu) | |
184 | { | |
994aa172 | 185 | if (cpus_accel->synchronize_post_init) { |
430065da CF |
186 | cpus_accel->synchronize_post_init(cpu); |
187 | } | |
430065da CF |
188 | } |
189 | ||
190 | void cpu_synchronize_pre_loadvm(CPUState *cpu) | |
191 | { | |
994aa172 | 192 | if (cpus_accel->synchronize_pre_loadvm) { |
430065da CF |
193 | cpus_accel->synchronize_pre_loadvm(cpu); |
194 | } | |
430065da CF |
195 | } |
196 | ||
92a5199b TL |
197 | bool cpus_are_resettable(void) |
198 | { | |
39196355 PMD |
199 | if (cpus_accel->cpus_are_resettable) { |
200 | return cpus_accel->cpus_are_resettable(); | |
201 | } | |
202 | return true; | |
92a5199b TL |
203 | } |
204 | ||
1b5120d7 PMD |
205 | void cpu_exec_reset_hold(CPUState *cpu) |
206 | { | |
207 | if (cpus_accel->cpu_reset_hold) { | |
208 | cpus_accel->cpu_reset_hold(cpu); | |
209 | } | |
210 | } | |
211 | ||
430065da CF |
212 | int64_t cpus_get_virtual_clock(void) |
213 | { | |
994aa172 CF |
214 | /* |
215 | * XXX | |
216 | * | |
217 | * need to check that cpus_accel is not NULL, because qcow2 calls | |
218 | * qemu_get_clock_ns(CLOCK_VIRTUAL) without any accel initialized and | |
219 | * with ticks disabled in some io-tests: | |
220 | * 030 040 041 060 099 120 127 140 156 161 172 181 191 192 195 203 229 249 256 267 | |
221 | * | |
222 | * is this expected? | |
223 | * | |
224 | * XXX | |
225 | */ | |
430065da CF |
226 | if (cpus_accel && cpus_accel->get_virtual_clock) { |
227 | return cpus_accel->get_virtual_clock(); | |
228 | } | |
430065da CF |
229 | return cpu_get_clock(); |
230 | } | |
231 | ||
232 | /* | |
233 | * return the time elapsed in VM between vm_start and vm_stop. Unless | |
234 | * icount is active, cpus_get_elapsed_ticks() uses units of the host CPU cycle | |
235 | * counter. | |
236 | */ | |
237 | int64_t cpus_get_elapsed_ticks(void) | |
238 | { | |
994aa172 | 239 | if (cpus_accel->get_elapsed_ticks) { |
430065da CF |
240 | return cpus_accel->get_elapsed_ticks(); |
241 | } | |
430065da CF |
242 | return cpu_get_ticks(); |
243 | } | |
244 | ||
bb4776be CF |
245 | static void generic_handle_interrupt(CPUState *cpu, int mask) |
246 | { | |
247 | cpu->interrupt_request |= mask; | |
248 | ||
249 | if (!qemu_cpu_is_self(cpu)) { | |
250 | qemu_cpu_kick(cpu); | |
251 | } | |
252 | } | |
253 | ||
254 | void cpu_interrupt(CPUState *cpu, int mask) | |
255 | { | |
256 | if (cpus_accel->handle_interrupt) { | |
257 | cpus_accel->handle_interrupt(cpu, mask); | |
258 | } else { | |
259 | generic_handle_interrupt(cpu, mask); | |
260 | } | |
261 | } | |
262 | ||
f06f316d SS |
263 | /* |
264 | * True if the vm was previously suspended, and has not been woken or reset. | |
265 | */ | |
266 | static int vm_was_suspended; | |
267 | ||
268 | void vm_set_suspended(bool suspended) | |
269 | { | |
270 | vm_was_suspended = suspended; | |
271 | } | |
272 | ||
273 | bool vm_get_suspended(void) | |
274 | { | |
275 | return vm_was_suspended; | |
276 | } | |
277 | ||
4486e89c | 278 | static int do_vm_stop(RunState state, bool send_stop) |
296af7c9 | 279 | { |
56983463 | 280 | int ret = 0; |
b9ae473d | 281 | RunState oldstate = runstate_get(); |
56983463 | 282 | |
b9ae473d SS |
283 | if (runstate_is_live(oldstate)) { |
284 | vm_was_suspended = (oldstate == RUN_STATE_SUSPENDED); | |
f962cac4 | 285 | runstate_set(state); |
296af7c9 | 286 | cpu_disable_ticks(); |
b9ae473d SS |
287 | if (oldstate == RUN_STATE_RUNNING) { |
288 | pause_all_vcpus(); | |
289 | } | |
1dfb4dd9 | 290 | vm_state_notify(0, state); |
4486e89c | 291 | if (send_stop) { |
3ab72385 | 292 | qapi_event_send_stop(); |
4486e89c | 293 | } |
296af7c9 | 294 | } |
56983463 | 295 | |
594a45ce | 296 | bdrv_drain_all(); |
22af08ea | 297 | ret = bdrv_flush_all(); |
8af3f5c6 | 298 | trace_vm_stop_flush_all(ret); |
594a45ce | 299 | |
56983463 | 300 | return ret; |
296af7c9 BS |
301 | } |
302 | ||
4486e89c SH |
303 | /* Special vm_stop() variant for terminating the process. Historically clients |
304 | * did not expect a QMP STOP event and so we need to retain compatibility. | |
305 | */ | |
306 | int vm_shutdown(void) | |
307 | { | |
308 | return do_vm_stop(RUN_STATE_SHUTDOWN, false); | |
309 | } | |
310 | ||
430065da | 311 | bool cpu_can_run(CPUState *cpu) |
296af7c9 | 312 | { |
4fdeee7c | 313 | if (cpu->stop) { |
a1fcaa73 | 314 | return false; |
0ab07c62 | 315 | } |
321bc0b2 | 316 | if (cpu_is_stopped(cpu)) { |
a1fcaa73 | 317 | return false; |
0ab07c62 | 318 | } |
a1fcaa73 | 319 | return true; |
296af7c9 BS |
320 | } |
321 | ||
430065da | 322 | void cpu_handle_guest_debug(CPUState *cpu) |
83f338f7 | 323 | { |
fda8458b PD |
324 | if (replay_running_debug()) { |
325 | if (!cpu->singlestep_enabled) { | |
cda38259 PD |
326 | /* |
327 | * Report about the breakpoint and | |
328 | * make a single step to skip it | |
329 | */ | |
330 | replay_breakpoint(); | |
fda8458b PD |
331 | cpu_single_step(cpu, SSTEP_ENABLE); |
332 | } else { | |
333 | cpu_single_step(cpu, 0); | |
334 | } | |
335 | } else { | |
336 | gdb_set_stop_cpu(cpu); | |
337 | qemu_system_debug_request(); | |
338 | cpu->stopped = true; | |
339 | } | |
3c638d06 JK |
340 | } |
341 | ||
6d9cb73c JK |
342 | #ifdef CONFIG_LINUX |
343 | static void sigbus_reraise(void) | |
344 | { | |
345 | sigset_t set; | |
346 | struct sigaction action; | |
347 | ||
348 | memset(&action, 0, sizeof(action)); | |
349 | action.sa_handler = SIG_DFL; | |
350 | if (!sigaction(SIGBUS, &action, NULL)) { | |
351 | raise(SIGBUS); | |
352 | sigemptyset(&set); | |
353 | sigaddset(&set, SIGBUS); | |
a2d1761d | 354 | pthread_sigmask(SIG_UNBLOCK, &set, NULL); |
6d9cb73c | 355 | } |
eb1960aa | 356 | perror("Failed to re-raise SIGBUS!"); |
6d9cb73c JK |
357 | abort(); |
358 | } | |
359 | ||
d98d4072 | 360 | static void sigbus_handler(int n, siginfo_t *siginfo, void *ctx) |
6d9cb73c | 361 | { |
a16fc07e PB |
362 | if (siginfo->si_code != BUS_MCEERR_AO && siginfo->si_code != BUS_MCEERR_AR) { |
363 | sigbus_reraise(); | |
364 | } | |
365 | ||
2ae41db2 PB |
366 | if (current_cpu) { |
367 | /* Called asynchronously in VCPU thread. */ | |
368 | if (kvm_on_sigbus_vcpu(current_cpu, siginfo->si_code, siginfo->si_addr)) { | |
369 | sigbus_reraise(); | |
370 | } | |
371 | } else { | |
372 | /* Called synchronously (via signalfd) in main thread. */ | |
373 | if (kvm_on_sigbus(siginfo->si_code, siginfo->si_addr)) { | |
374 | sigbus_reraise(); | |
375 | } | |
6d9cb73c JK |
376 | } |
377 | } | |
378 | ||
379 | static void qemu_init_sigbus(void) | |
380 | { | |
381 | struct sigaction action; | |
382 | ||
29b838c0 DH |
383 | /* |
384 | * ALERT: when modifying this, take care that SIGBUS forwarding in | |
6556aadc | 385 | * qemu_prealloc_mem() will continue working as expected. |
29b838c0 | 386 | */ |
6d9cb73c JK |
387 | memset(&action, 0, sizeof(action)); |
388 | action.sa_flags = SA_SIGINFO; | |
d98d4072 | 389 | action.sa_sigaction = sigbus_handler; |
6d9cb73c JK |
390 | sigaction(SIGBUS, &action, NULL); |
391 | ||
392 | prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0); | |
393 | } | |
6d9cb73c | 394 | #else /* !CONFIG_LINUX */ |
6d9cb73c JK |
395 | static void qemu_init_sigbus(void) |
396 | { | |
397 | } | |
a16fc07e | 398 | #endif /* !CONFIG_LINUX */ |
ff48eb5f | 399 | |
296af7c9 BS |
400 | static QemuThread io_thread; |
401 | ||
296af7c9 BS |
402 | /* cpu creation */ |
403 | static QemuCond qemu_cpu_cond; | |
404 | /* system init */ | |
296af7c9 BS |
405 | static QemuCond qemu_pause_cond; |
406 | ||
d3b12f5d | 407 | void qemu_init_cpu_loop(void) |
296af7c9 | 408 | { |
6d9cb73c | 409 | qemu_init_sigbus(); |
ed94592b | 410 | qemu_cond_init(&qemu_cpu_cond); |
ed94592b | 411 | qemu_cond_init(&qemu_pause_cond); |
195801d7 | 412 | qemu_mutex_init(&bql); |
296af7c9 | 413 | |
b7680cb6 | 414 | qemu_thread_get_self(&io_thread); |
296af7c9 BS |
415 | } |
416 | ||
14e6fe12 | 417 | void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data) |
e82bcec2 | 418 | { |
195801d7 | 419 | do_run_on_cpu(cpu, func, data, &bql); |
3c02270d CV |
420 | } |
421 | ||
ebd05fea DH |
422 | static void qemu_cpu_stop(CPUState *cpu, bool exit) |
423 | { | |
424 | g_assert(qemu_cpu_is_self(cpu)); | |
425 | cpu->stop = false; | |
426 | cpu->stopped = true; | |
427 | if (exit) { | |
428 | cpu_exit(cpu); | |
429 | } | |
430 | qemu_cond_broadcast(&qemu_pause_cond); | |
431 | } | |
432 | ||
430065da | 433 | void qemu_wait_io_event_common(CPUState *cpu) |
296af7c9 | 434 | { |
06831001 | 435 | qatomic_set_mb(&cpu->thread_kicked, false); |
4fdeee7c | 436 | if (cpu->stop) { |
ebd05fea | 437 | qemu_cpu_stop(cpu, false); |
296af7c9 | 438 | } |
a5403c69 | 439 | process_queued_cpu_work(cpu); |
37257942 AB |
440 | } |
441 | ||
430065da | 442 | void qemu_wait_io_event(CPUState *cpu) |
296af7c9 | 443 | { |
30865f31 EC |
444 | bool slept = false; |
445 | ||
a98ae1d8 | 446 | while (cpu_thread_is_idle(cpu)) { |
30865f31 EC |
447 | if (!slept) { |
448 | slept = true; | |
449 | qemu_plugin_vcpu_idle_cb(cpu); | |
450 | } | |
195801d7 | 451 | qemu_cond_wait(cpu->halt_cond, &bql); |
16400322 | 452 | } |
30865f31 EC |
453 | if (slept) { |
454 | qemu_plugin_vcpu_resume_cb(cpu); | |
455 | } | |
296af7c9 | 456 | |
c97d6d2c SAGDR |
457 | qemu_wait_io_event_common(cpu); |
458 | } | |
459 | ||
430065da | 460 | void cpus_kick_thread(CPUState *cpu) |
cc015e9a | 461 | { |
e0c38211 PB |
462 | if (cpu->thread_kicked) { |
463 | return; | |
9102deda | 464 | } |
e0c38211 | 465 | cpu->thread_kicked = true; |
c9923550 XC |
466 | |
467 | #ifndef _WIN32 | |
468 | int err = pthread_kill(cpu->thread->thread, SIG_IPI); | |
d455ebc4 | 469 | if (err && err != ESRCH) { |
cc015e9a PB |
470 | fprintf(stderr, "qemu:%s: %s", __func__, strerror(err)); |
471 | exit(1); | |
472 | } | |
c9923550 XC |
473 | #else |
474 | qemu_sem_post(&cpu->sem); | |
e0c38211 PB |
475 | #endif |
476 | } | |
ed9164a3 | 477 | |
c08d7424 | 478 | void qemu_cpu_kick(CPUState *cpu) |
296af7c9 | 479 | { |
f5c121b8 | 480 | qemu_cond_broadcast(cpu->halt_cond); |
994aa172 | 481 | if (cpus_accel->kick_vcpu_thread) { |
430065da | 482 | cpus_accel->kick_vcpu_thread(cpu); |
e92558e4 | 483 | } else { /* default */ |
430065da | 484 | cpus_kick_thread(cpu); |
e0c38211 | 485 | } |
296af7c9 BS |
486 | } |
487 | ||
46d62fac | 488 | void qemu_cpu_kick_self(void) |
296af7c9 | 489 | { |
4917cf44 | 490 | assert(current_cpu); |
430065da | 491 | cpus_kick_thread(current_cpu); |
296af7c9 BS |
492 | } |
493 | ||
60e82579 | 494 | bool qemu_cpu_is_self(CPUState *cpu) |
296af7c9 | 495 | { |
814e612e | 496 | return qemu_thread_is_self(cpu->thread); |
296af7c9 BS |
497 | } |
498 | ||
79e2b9ae | 499 | bool qemu_in_vcpu_thread(void) |
aa723c23 | 500 | { |
4917cf44 | 501 | return current_cpu && qemu_cpu_is_self(current_cpu); |
aa723c23 JQ |
502 | } |
503 | ||
195801d7 | 504 | QEMU_DEFINE_STATIC_CO_TLS(bool, bql_locked) |
afbe7053 | 505 | |
195801d7 | 506 | bool bql_locked(void) |
afbe7053 | 507 | { |
195801d7 | 508 | return get_bql_locked(); |
afbe7053 PB |
509 | } |
510 | ||
6538692e EGE |
511 | bool qemu_in_main_thread(void) |
512 | { | |
195801d7 | 513 | return bql_locked(); |
6538692e EGE |
514 | } |
515 | ||
cb764d06 EC |
516 | /* |
517 | * The BQL is taken from so many places that it is worth profiling the | |
518 | * callers directly, instead of funneling them all through a single function. | |
519 | */ | |
195801d7 | 520 | void bql_lock_impl(const char *file, int line) |
296af7c9 | 521 | { |
195801d7 | 522 | QemuMutexLockFunc bql_lock_fn = qatomic_read(&bql_mutex_lock_func); |
cb764d06 | 523 | |
195801d7 SH |
524 | g_assert(!bql_locked()); |
525 | bql_lock_fn(&bql, file, line); | |
526 | set_bql_locked(true); | |
296af7c9 BS |
527 | } |
528 | ||
195801d7 | 529 | void bql_unlock(void) |
296af7c9 | 530 | { |
195801d7 SH |
531 | g_assert(bql_locked()); |
532 | set_bql_locked(false); | |
533 | qemu_mutex_unlock(&bql); | |
296af7c9 BS |
534 | } |
535 | ||
7c754c78 | 536 | void qemu_cond_wait_bql(QemuCond *cond) |
19e067e0 | 537 | { |
195801d7 | 538 | qemu_cond_wait(cond, &bql); |
19e067e0 AP |
539 | } |
540 | ||
7c754c78 | 541 | void qemu_cond_timedwait_bql(QemuCond *cond, int ms) |
b0c3cf94 | 542 | { |
195801d7 | 543 | qemu_cond_timedwait(cond, &bql, ms); |
b0c3cf94 CF |
544 | } |
545 | ||
430065da CF |
546 | /* signal CPU creation */ |
547 | void cpu_thread_signal_created(CPUState *cpu) | |
548 | { | |
549 | cpu->created = true; | |
550 | qemu_cond_signal(&qemu_cpu_cond); | |
551 | } | |
552 | ||
553 | /* signal CPU destruction */ | |
554 | void cpu_thread_signal_destroyed(CPUState *cpu) | |
555 | { | |
556 | cpu->created = false; | |
557 | qemu_cond_signal(&qemu_cpu_cond); | |
558 | } | |
559 | ||
560 | ||
e8faee06 | 561 | static bool all_vcpus_paused(void) |
296af7c9 | 562 | { |
bdc44640 | 563 | CPUState *cpu; |
296af7c9 | 564 | |
bdc44640 | 565 | CPU_FOREACH(cpu) { |
182735ef | 566 | if (!cpu->stopped) { |
e8faee06 | 567 | return false; |
0ab07c62 | 568 | } |
296af7c9 BS |
569 | } |
570 | ||
e8faee06 | 571 | return true; |
296af7c9 BS |
572 | } |
573 | ||
574 | void pause_all_vcpus(void) | |
575 | { | |
bdc44640 | 576 | CPUState *cpu; |
296af7c9 | 577 | |
40daca54 | 578 | qemu_clock_enable(QEMU_CLOCK_VIRTUAL, false); |
bdc44640 | 579 | CPU_FOREACH(cpu) { |
ebd05fea DH |
580 | if (qemu_cpu_is_self(cpu)) { |
581 | qemu_cpu_stop(cpu, true); | |
582 | } else { | |
583 | cpu->stop = true; | |
584 | qemu_cpu_kick(cpu); | |
585 | } | |
d798e974 JK |
586 | } |
587 | ||
d759c951 AB |
588 | /* We need to drop the replay_lock so any vCPU threads woken up |
589 | * can finish their replay tasks | |
590 | */ | |
591 | replay_mutex_unlock(); | |
592 | ||
296af7c9 | 593 | while (!all_vcpus_paused()) { |
195801d7 | 594 | qemu_cond_wait(&qemu_pause_cond, &bql); |
bdc44640 | 595 | CPU_FOREACH(cpu) { |
182735ef | 596 | qemu_cpu_kick(cpu); |
296af7c9 BS |
597 | } |
598 | } | |
d759c951 | 599 | |
195801d7 | 600 | bql_unlock(); |
d759c951 | 601 | replay_mutex_lock(); |
195801d7 | 602 | bql_lock(); |
296af7c9 BS |
603 | } |
604 | ||
2993683b IM |
605 | void cpu_resume(CPUState *cpu) |
606 | { | |
607 | cpu->stop = false; | |
608 | cpu->stopped = false; | |
609 | qemu_cpu_kick(cpu); | |
610 | } | |
611 | ||
296af7c9 BS |
612 | void resume_all_vcpus(void) |
613 | { | |
bdc44640 | 614 | CPUState *cpu; |
296af7c9 | 615 | |
f962cac4 LM |
616 | if (!runstate_is_running()) { |
617 | return; | |
618 | } | |
619 | ||
40daca54 | 620 | qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true); |
bdc44640 | 621 | CPU_FOREACH(cpu) { |
182735ef | 622 | cpu_resume(cpu); |
296af7c9 BS |
623 | } |
624 | } | |
625 | ||
dbadee4f | 626 | void cpu_remove_sync(CPUState *cpu) |
4c055ab5 GZ |
627 | { |
628 | cpu->stop = true; | |
629 | cpu->unplug = true; | |
630 | qemu_cpu_kick(cpu); | |
195801d7 | 631 | bql_unlock(); |
dbadee4f | 632 | qemu_thread_join(cpu->thread); |
195801d7 | 633 | bql_lock(); |
2c579042 BR |
634 | } |
635 | ||
b86f59c7 | 636 | void cpus_register_accel(const AccelOpsClass *ops) |
430065da | 637 | { |
b86f59c7 CF |
638 | assert(ops != NULL); |
639 | assert(ops->create_vcpu_thread != NULL); /* mandatory */ | |
640 | cpus_accel = ops; | |
430065da CF |
641 | } |
642 | ||
ae7467b1 AB |
643 | const AccelOpsClass *cpus_get_accel(void) |
644 | { | |
645 | /* broken if we call this early */ | |
646 | assert(cpus_accel); | |
647 | return cpus_accel; | |
648 | } | |
649 | ||
c643bed9 | 650 | void qemu_init_vcpu(CPUState *cpu) |
296af7c9 | 651 | { |
5cc8767d LX |
652 | MachineState *ms = MACHINE(qdev_get_machine()); |
653 | ||
958ac3c4 | 654 | cpu->nr_cores = machine_topo_get_cores_per_socket(ms); |
5cc8767d | 655 | cpu->nr_threads = ms->smp.threads; |
f324e766 | 656 | cpu->stopped = true; |
9c09a251 | 657 | cpu->random_seed = qemu_guest_random_seed_thread_part1(); |
56943e8c PM |
658 | |
659 | if (!cpu->as) { | |
660 | /* If the target cpu hasn't set up any address spaces itself, | |
661 | * give it the default one. | |
662 | */ | |
12ebc9a7 | 663 | cpu->num_ases = 1; |
80ceb07a | 664 | cpu_address_space_init(cpu, 0, "cpu-memory", cpu->memory); |
56943e8c PM |
665 | } |
666 | ||
b86f59c7 | 667 | /* accelerators all implement the AccelOpsClass */ |
994aa172 CF |
668 | g_assert(cpus_accel != NULL && cpus_accel->create_vcpu_thread != NULL); |
669 | cpus_accel->create_vcpu_thread(cpu); | |
81e96311 DH |
670 | |
671 | while (!cpu->created) { | |
195801d7 | 672 | qemu_cond_wait(&qemu_cpu_cond, &bql); |
81e96311 | 673 | } |
296af7c9 BS |
674 | } |
675 | ||
b4a3d965 | 676 | void cpu_stop_current(void) |
296af7c9 | 677 | { |
4917cf44 | 678 | if (current_cpu) { |
0ec7e677 PM |
679 | current_cpu->stop = true; |
680 | cpu_exit(current_cpu); | |
b4a3d965 | 681 | } |
296af7c9 BS |
682 | } |
683 | ||
56983463 | 684 | int vm_stop(RunState state) |
296af7c9 | 685 | { |
aa723c23 | 686 | if (qemu_in_vcpu_thread()) { |
74892d24 | 687 | qemu_system_vmstop_request_prepare(); |
1dfb4dd9 | 688 | qemu_system_vmstop_request(state); |
296af7c9 BS |
689 | /* |
690 | * FIXME: should not return to device code in case | |
691 | * vm_stop() has been requested. | |
692 | */ | |
b4a3d965 | 693 | cpu_stop_current(); |
56983463 | 694 | return 0; |
296af7c9 | 695 | } |
56983463 | 696 | |
4486e89c | 697 | return do_vm_stop(state, true); |
296af7c9 BS |
698 | } |
699 | ||
2d76e823 CI |
700 | /** |
701 | * Prepare for (re)starting the VM. | |
b9ae473d SS |
702 | * Returns 0 if the vCPUs should be restarted, -1 on an error condition, |
703 | * and 1 otherwise. | |
2d76e823 | 704 | */ |
d7482ffe | 705 | int vm_prepare_start(bool step_pending) |
2d76e823 | 706 | { |
b9ae473d SS |
707 | int ret = vm_was_suspended ? 1 : 0; |
708 | RunState state = vm_was_suspended ? RUN_STATE_SUSPENDED : RUN_STATE_RUNNING; | |
2d76e823 | 709 | RunState requested; |
2d76e823 CI |
710 | |
711 | qemu_vmstop_requested(&requested); | |
712 | if (runstate_is_running() && requested == RUN_STATE__MAX) { | |
713 | return -1; | |
714 | } | |
715 | ||
716 | /* Ensure that a STOP/RESUME pair of events is emitted if a | |
717 | * vmstop request was pending. The BLOCK_IO_ERROR event, for | |
718 | * example, according to documentation is always followed by | |
719 | * the STOP event. | |
720 | */ | |
721 | if (runstate_is_running()) { | |
3ab72385 PX |
722 | qapi_event_send_stop(); |
723 | qapi_event_send_resume(); | |
f056158d | 724 | return -1; |
2d76e823 CI |
725 | } |
726 | ||
d7482ffe IS |
727 | /* |
728 | * WHPX accelerator needs to know whether we are going to step | |
729 | * any CPUs, before starting the first one. | |
730 | */ | |
731 | if (cpus_accel->synchronize_pre_resume) { | |
732 | cpus_accel->synchronize_pre_resume(step_pending); | |
733 | } | |
734 | ||
2d76e823 | 735 | /* We are sending this now, but the CPUs will be resumed shortly later */ |
3ab72385 | 736 | qapi_event_send_resume(); |
f056158d | 737 | |
f056158d | 738 | cpu_enable_ticks(); |
b9ae473d SS |
739 | runstate_set(state); |
740 | vm_state_notify(1, state); | |
741 | vm_was_suspended = false; | |
742 | return ret; | |
2d76e823 CI |
743 | } |
744 | ||
745 | void vm_start(void) | |
746 | { | |
d7482ffe | 747 | if (!vm_prepare_start(false)) { |
2d76e823 CI |
748 | resume_all_vcpus(); |
749 | } | |
750 | } | |
751 | ||
9ff5e79f SS |
752 | void vm_resume(RunState state) |
753 | { | |
754 | if (runstate_is_live(state)) { | |
755 | vm_start(); | |
756 | } else { | |
757 | runstate_set(state); | |
758 | } | |
759 | } | |
760 | ||
8a9236f1 LC |
761 | /* does a state transition even if the VM is already stopped, |
762 | current state is forgotten forever */ | |
56983463 | 763 | int vm_stop_force_state(RunState state) |
8a9236f1 | 764 | { |
b9ae473d | 765 | if (runstate_is_live(runstate_get())) { |
56983463 | 766 | return vm_stop(state); |
8a9236f1 | 767 | } else { |
8af3f5c6 | 768 | int ret; |
8a9236f1 | 769 | runstate_set(state); |
b2780d32 WC |
770 | |
771 | bdrv_drain_all(); | |
594a45ce KW |
772 | /* Make sure to return an error if the flush in a previous vm_stop() |
773 | * failed. */ | |
8af3f5c6 DB |
774 | ret = bdrv_flush_all(); |
775 | trace_vm_stop_flush_all(ret); | |
776 | return ret; | |
8a9236f1 LC |
777 | } |
778 | } | |
779 | ||
0cfd6a9a LC |
780 | void qmp_memsave(int64_t addr, int64_t size, const char *filename, |
781 | bool has_cpu, int64_t cpu_index, Error **errp) | |
782 | { | |
783 | FILE *f; | |
784 | uint32_t l; | |
55e5c285 | 785 | CPUState *cpu; |
0cfd6a9a | 786 | uint8_t buf[1024]; |
0dc9daf0 | 787 | int64_t orig_addr = addr, orig_size = size; |
0cfd6a9a LC |
788 | |
789 | if (!has_cpu) { | |
790 | cpu_index = 0; | |
791 | } | |
792 | ||
151d1322 AF |
793 | cpu = qemu_get_cpu(cpu_index); |
794 | if (cpu == NULL) { | |
c6bd8c70 MA |
795 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index", |
796 | "a CPU number"); | |
0cfd6a9a LC |
797 | return; |
798 | } | |
799 | ||
800 | f = fopen(filename, "wb"); | |
801 | if (!f) { | |
618da851 | 802 | error_setg_file_open(errp, errno, filename); |
0cfd6a9a LC |
803 | return; |
804 | } | |
805 | ||
806 | while (size != 0) { | |
807 | l = sizeof(buf); | |
808 | if (l > size) | |
809 | l = size; | |
2f4d0f59 | 810 | if (cpu_memory_rw_debug(cpu, addr, buf, l, 0) != 0) { |
0dc9daf0 BP |
811 | error_setg(errp, "Invalid addr 0x%016" PRIx64 "/size %" PRId64 |
812 | " specified", orig_addr, orig_size); | |
2f4d0f59 AK |
813 | goto exit; |
814 | } | |
0cfd6a9a | 815 | if (fwrite(buf, 1, l, f) != l) { |
c6bd8c70 | 816 | error_setg(errp, QERR_IO_ERROR); |
0cfd6a9a LC |
817 | goto exit; |
818 | } | |
819 | addr += l; | |
820 | size -= l; | |
821 | } | |
822 | ||
823 | exit: | |
824 | fclose(f); | |
825 | } | |
6d3962bf LC |
826 | |
827 | void qmp_pmemsave(int64_t addr, int64_t size, const char *filename, | |
828 | Error **errp) | |
829 | { | |
830 | FILE *f; | |
831 | uint32_t l; | |
832 | uint8_t buf[1024]; | |
833 | ||
834 | f = fopen(filename, "wb"); | |
835 | if (!f) { | |
618da851 | 836 | error_setg_file_open(errp, errno, filename); |
6d3962bf LC |
837 | return; |
838 | } | |
839 | ||
840 | while (size != 0) { | |
841 | l = sizeof(buf); | |
842 | if (l > size) | |
843 | l = size; | |
eb6282f2 | 844 | cpu_physical_memory_read(addr, buf, l); |
6d3962bf | 845 | if (fwrite(buf, 1, l, f) != l) { |
c6bd8c70 | 846 | error_setg(errp, QERR_IO_ERROR); |
6d3962bf LC |
847 | goto exit; |
848 | } | |
849 | addr += l; | |
850 | size -= l; | |
851 | } | |
852 | ||
853 | exit: | |
854 | fclose(f); | |
855 | } | |
ab49ab5c LC |
856 | |
857 | void qmp_inject_nmi(Error **errp) | |
858 | { | |
947e4744 | 859 | nmi_monitor_handle(monitor_get_cpu_index(monitor_cur()), errp); |
ab49ab5c | 860 | } |
27498bef | 861 |