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