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