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