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