4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #include "qemu/osdep.h"
26 #include "qemu-common.h"
27 #include "monitor/monitor.h"
28 #include "qapi/error.h"
29 #include "qapi/qapi-commands-machine.h"
30 #include "qapi/qapi-commands-misc.h"
31 #include "qapi/qapi-events-run-state.h"
32 #include "qapi/qmp/qerror.h"
33 #include "exec/gdbstub.h"
34 #include "sysemu/hw_accel.h"
35 #include "exec/exec-all.h"
36 #include "qemu/thread.h"
37 #include "qemu/plugin.h"
38 #include "sysemu/cpus.h"
39 #include "qemu/guest-random.h"
41 #include "sysemu/replay.h"
42 #include "sysemu/runstate.h"
43 #include "sysemu/cpu-timers.h"
44 #include "hw/boards.h"
49 #include <sys/prctl.h>
52 #define PR_MCE_KILL 33
55 #ifndef PR_MCE_KILL_SET
56 #define PR_MCE_KILL_SET 1
59 #ifndef PR_MCE_KILL_EARLY
60 #define PR_MCE_KILL_EARLY 1
63 #endif /* CONFIG_LINUX */
65 static QemuMutex qemu_global_mutex
;
67 bool cpu_is_stopped(CPUState
*cpu
)
69 return cpu
->stopped
|| !runstate_is_running();
72 bool cpu_work_list_empty(CPUState
*cpu
)
76 qemu_mutex_lock(&cpu
->work_mutex
);
77 ret
= QSIMPLEQ_EMPTY(&cpu
->work_list
);
78 qemu_mutex_unlock(&cpu
->work_mutex
);
82 bool cpu_thread_is_idle(CPUState
*cpu
)
84 if (cpu
->stop
|| !cpu_work_list_empty(cpu
)) {
87 if (cpu_is_stopped(cpu
)) {
90 if (!cpu
->halted
|| cpu_has_work(cpu
) ||
91 kvm_halt_in_kernel()) {
97 bool all_cpu_threads_idle(void)
102 if (!cpu_thread_is_idle(cpu
)) {
109 /***********************************************************/
110 void hw_error(const char *fmt
, ...)
116 fprintf(stderr
, "qemu: hardware error: ");
117 vfprintf(stderr
, fmt
, ap
);
118 fprintf(stderr
, "\n");
120 fprintf(stderr
, "CPU #%d:\n", cpu
->cpu_index
);
121 cpu_dump_state(cpu
, stderr
, CPU_DUMP_FPU
);
128 * The chosen accelerator is supposed to register this.
130 static const CpusAccel
*cpus_accel
;
132 void cpu_synchronize_all_states(void)
137 cpu_synchronize_state(cpu
);
141 void cpu_synchronize_all_post_reset(void)
146 cpu_synchronize_post_reset(cpu
);
150 void cpu_synchronize_all_post_init(void)
155 cpu_synchronize_post_init(cpu
);
159 void cpu_synchronize_all_pre_loadvm(void)
164 cpu_synchronize_pre_loadvm(cpu
);
168 void cpu_synchronize_state(CPUState
*cpu
)
170 if (cpus_accel
->synchronize_state
) {
171 cpus_accel
->synchronize_state(cpu
);
175 void cpu_synchronize_post_reset(CPUState
*cpu
)
177 if (cpus_accel
->synchronize_post_reset
) {
178 cpus_accel
->synchronize_post_reset(cpu
);
182 void cpu_synchronize_post_init(CPUState
*cpu
)
184 if (cpus_accel
->synchronize_post_init
) {
185 cpus_accel
->synchronize_post_init(cpu
);
189 void cpu_synchronize_pre_loadvm(CPUState
*cpu
)
191 if (cpus_accel
->synchronize_pre_loadvm
) {
192 cpus_accel
->synchronize_pre_loadvm(cpu
);
196 int64_t cpus_get_virtual_clock(void)
201 * need to check that cpus_accel is not NULL, because qcow2 calls
202 * qemu_get_clock_ns(CLOCK_VIRTUAL) without any accel initialized and
203 * with ticks disabled in some io-tests:
204 * 030 040 041 060 099 120 127 140 156 161 172 181 191 192 195 203 229 249 256 267
210 if (cpus_accel
&& cpus_accel
->get_virtual_clock
) {
211 return cpus_accel
->get_virtual_clock();
213 return cpu_get_clock();
217 * return the time elapsed in VM between vm_start and vm_stop. Unless
218 * icount is active, cpus_get_elapsed_ticks() uses units of the host CPU cycle
221 int64_t cpus_get_elapsed_ticks(void)
223 if (cpus_accel
->get_elapsed_ticks
) {
224 return cpus_accel
->get_elapsed_ticks();
226 return cpu_get_ticks();
229 static void generic_handle_interrupt(CPUState
*cpu
, int mask
)
231 cpu
->interrupt_request
|= mask
;
233 if (!qemu_cpu_is_self(cpu
)) {
238 void cpu_interrupt(CPUState
*cpu
, int mask
)
240 if (cpus_accel
->handle_interrupt
) {
241 cpus_accel
->handle_interrupt(cpu
, mask
);
243 generic_handle_interrupt(cpu
, mask
);
247 static int do_vm_stop(RunState state
, bool send_stop
)
251 if (runstate_is_running()) {
255 vm_state_notify(0, state
);
257 qapi_event_send_stop();
262 ret
= bdrv_flush_all();
267 /* Special vm_stop() variant for terminating the process. Historically clients
268 * did not expect a QMP STOP event and so we need to retain compatibility.
270 int vm_shutdown(void)
272 return do_vm_stop(RUN_STATE_SHUTDOWN
, false);
275 bool cpu_can_run(CPUState
*cpu
)
280 if (cpu_is_stopped(cpu
)) {
286 void cpu_handle_guest_debug(CPUState
*cpu
)
288 if (replay_running_debug()) {
289 if (!cpu
->singlestep_enabled
) {
291 * Report about the breakpoint and
292 * make a single step to skip it
295 cpu_single_step(cpu
, SSTEP_ENABLE
);
297 cpu_single_step(cpu
, 0);
300 gdb_set_stop_cpu(cpu
);
301 qemu_system_debug_request();
307 static void sigbus_reraise(void)
310 struct sigaction action
;
312 memset(&action
, 0, sizeof(action
));
313 action
.sa_handler
= SIG_DFL
;
314 if (!sigaction(SIGBUS
, &action
, NULL
)) {
317 sigaddset(&set
, SIGBUS
);
318 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
320 perror("Failed to re-raise SIGBUS!\n");
324 static void sigbus_handler(int n
, siginfo_t
*siginfo
, void *ctx
)
326 if (siginfo
->si_code
!= BUS_MCEERR_AO
&& siginfo
->si_code
!= BUS_MCEERR_AR
) {
331 /* Called asynchronously in VCPU thread. */
332 if (kvm_on_sigbus_vcpu(current_cpu
, siginfo
->si_code
, siginfo
->si_addr
)) {
336 /* Called synchronously (via signalfd) in main thread. */
337 if (kvm_on_sigbus(siginfo
->si_code
, siginfo
->si_addr
)) {
343 static void qemu_init_sigbus(void)
345 struct sigaction action
;
347 memset(&action
, 0, sizeof(action
));
348 action
.sa_flags
= SA_SIGINFO
;
349 action
.sa_sigaction
= sigbus_handler
;
350 sigaction(SIGBUS
, &action
, NULL
);
352 prctl(PR_MCE_KILL
, PR_MCE_KILL_SET
, PR_MCE_KILL_EARLY
, 0, 0);
354 #else /* !CONFIG_LINUX */
355 static void qemu_init_sigbus(void)
358 #endif /* !CONFIG_LINUX */
360 static QemuThread io_thread
;
363 static QemuCond qemu_cpu_cond
;
365 static QemuCond qemu_pause_cond
;
367 void qemu_init_cpu_loop(void)
370 qemu_cond_init(&qemu_cpu_cond
);
371 qemu_cond_init(&qemu_pause_cond
);
372 qemu_mutex_init(&qemu_global_mutex
);
374 qemu_thread_get_self(&io_thread
);
377 void run_on_cpu(CPUState
*cpu
, run_on_cpu_func func
, run_on_cpu_data data
)
379 do_run_on_cpu(cpu
, func
, data
, &qemu_global_mutex
);
382 static void qemu_cpu_stop(CPUState
*cpu
, bool exit
)
384 g_assert(qemu_cpu_is_self(cpu
));
390 qemu_cond_broadcast(&qemu_pause_cond
);
393 void qemu_wait_io_event_common(CPUState
*cpu
)
395 qatomic_mb_set(&cpu
->thread_kicked
, false);
397 qemu_cpu_stop(cpu
, false);
399 process_queued_cpu_work(cpu
);
402 void qemu_wait_io_event(CPUState
*cpu
)
406 while (cpu_thread_is_idle(cpu
)) {
409 qemu_plugin_vcpu_idle_cb(cpu
);
411 qemu_cond_wait(cpu
->halt_cond
, &qemu_global_mutex
);
414 qemu_plugin_vcpu_resume_cb(cpu
);
418 /* Eat dummy APC queued by cpus_kick_thread. */
423 qemu_wait_io_event_common(cpu
);
426 void cpus_kick_thread(CPUState
*cpu
)
431 if (cpu
->thread_kicked
) {
434 cpu
->thread_kicked
= true;
435 err
= pthread_kill(cpu
->thread
->thread
, SIG_IPI
);
436 if (err
&& err
!= ESRCH
) {
437 fprintf(stderr
, "qemu:%s: %s", __func__
, strerror(err
));
443 void qemu_cpu_kick(CPUState
*cpu
)
445 qemu_cond_broadcast(cpu
->halt_cond
);
446 if (cpus_accel
->kick_vcpu_thread
) {
447 cpus_accel
->kick_vcpu_thread(cpu
);
448 } else { /* default */
449 cpus_kick_thread(cpu
);
453 void qemu_cpu_kick_self(void)
456 cpus_kick_thread(current_cpu
);
459 bool qemu_cpu_is_self(CPUState
*cpu
)
461 return qemu_thread_is_self(cpu
->thread
);
464 bool qemu_in_vcpu_thread(void)
466 return current_cpu
&& qemu_cpu_is_self(current_cpu
);
469 static __thread
bool iothread_locked
= false;
471 bool qemu_mutex_iothread_locked(void)
473 return iothread_locked
;
477 * The BQL is taken from so many places that it is worth profiling the
478 * callers directly, instead of funneling them all through a single function.
480 void qemu_mutex_lock_iothread_impl(const char *file
, int line
)
482 QemuMutexLockFunc bql_lock
= qatomic_read(&qemu_bql_mutex_lock_func
);
484 g_assert(!qemu_mutex_iothread_locked());
485 bql_lock(&qemu_global_mutex
, file
, line
);
486 iothread_locked
= true;
489 void qemu_mutex_unlock_iothread(void)
491 g_assert(qemu_mutex_iothread_locked());
492 iothread_locked
= false;
493 qemu_mutex_unlock(&qemu_global_mutex
);
496 void qemu_cond_wait_iothread(QemuCond
*cond
)
498 qemu_cond_wait(cond
, &qemu_global_mutex
);
501 void qemu_cond_timedwait_iothread(QemuCond
*cond
, int ms
)
503 qemu_cond_timedwait(cond
, &qemu_global_mutex
, ms
);
506 /* signal CPU creation */
507 void cpu_thread_signal_created(CPUState
*cpu
)
510 qemu_cond_signal(&qemu_cpu_cond
);
513 /* signal CPU destruction */
514 void cpu_thread_signal_destroyed(CPUState
*cpu
)
516 cpu
->created
= false;
517 qemu_cond_signal(&qemu_cpu_cond
);
521 static bool all_vcpus_paused(void)
534 void pause_all_vcpus(void)
538 qemu_clock_enable(QEMU_CLOCK_VIRTUAL
, false);
540 if (qemu_cpu_is_self(cpu
)) {
541 qemu_cpu_stop(cpu
, true);
548 /* We need to drop the replay_lock so any vCPU threads woken up
549 * can finish their replay tasks
551 replay_mutex_unlock();
553 while (!all_vcpus_paused()) {
554 qemu_cond_wait(&qemu_pause_cond
, &qemu_global_mutex
);
560 qemu_mutex_unlock_iothread();
562 qemu_mutex_lock_iothread();
565 void cpu_resume(CPUState
*cpu
)
568 cpu
->stopped
= false;
572 void resume_all_vcpus(void)
576 if (!runstate_is_running()) {
580 qemu_clock_enable(QEMU_CLOCK_VIRTUAL
, true);
586 void cpu_remove_sync(CPUState
*cpu
)
591 qemu_mutex_unlock_iothread();
592 qemu_thread_join(cpu
->thread
);
593 qemu_mutex_lock_iothread();
596 void cpus_register_accel(const CpusAccel
*ca
)
599 assert(ca
->create_vcpu_thread
!= NULL
); /* mandatory */
603 void qemu_init_vcpu(CPUState
*cpu
)
605 MachineState
*ms
= MACHINE(qdev_get_machine());
607 cpu
->nr_cores
= ms
->smp
.cores
;
608 cpu
->nr_threads
= ms
->smp
.threads
;
610 cpu
->random_seed
= qemu_guest_random_seed_thread_part1();
613 /* If the target cpu hasn't set up any address spaces itself,
614 * give it the default one.
617 cpu_address_space_init(cpu
, 0, "cpu-memory", cpu
->memory
);
620 /* accelerators all implement the CpusAccel interface */
621 g_assert(cpus_accel
!= NULL
&& cpus_accel
->create_vcpu_thread
!= NULL
);
622 cpus_accel
->create_vcpu_thread(cpu
);
624 while (!cpu
->created
) {
625 qemu_cond_wait(&qemu_cpu_cond
, &qemu_global_mutex
);
629 void cpu_stop_current(void)
632 current_cpu
->stop
= true;
633 cpu_exit(current_cpu
);
637 int vm_stop(RunState state
)
639 if (qemu_in_vcpu_thread()) {
640 qemu_system_vmstop_request_prepare();
641 qemu_system_vmstop_request(state
);
643 * FIXME: should not return to device code in case
644 * vm_stop() has been requested.
650 return do_vm_stop(state
, true);
654 * Prepare for (re)starting the VM.
655 * Returns -1 if the vCPUs are not to be restarted (e.g. if they are already
656 * running or in case of an error condition), 0 otherwise.
658 int vm_prepare_start(void)
662 qemu_vmstop_requested(&requested
);
663 if (runstate_is_running() && requested
== RUN_STATE__MAX
) {
667 /* Ensure that a STOP/RESUME pair of events is emitted if a
668 * vmstop request was pending. The BLOCK_IO_ERROR event, for
669 * example, according to documentation is always followed by
672 if (runstate_is_running()) {
673 qapi_event_send_stop();
674 qapi_event_send_resume();
678 /* We are sending this now, but the CPUs will be resumed shortly later */
679 qapi_event_send_resume();
682 runstate_set(RUN_STATE_RUNNING
);
683 vm_state_notify(1, RUN_STATE_RUNNING
);
689 if (!vm_prepare_start()) {
694 /* does a state transition even if the VM is already stopped,
695 current state is forgotten forever */
696 int vm_stop_force_state(RunState state
)
698 if (runstate_is_running()) {
699 return vm_stop(state
);
704 /* Make sure to return an error if the flush in a previous vm_stop()
706 return bdrv_flush_all();
710 void list_cpus(const char *optarg
)
712 /* XXX: implement xxx_cpu_list for targets that still miss it */
713 #if defined(cpu_list)
718 void qmp_memsave(int64_t addr
, int64_t size
, const char *filename
,
719 bool has_cpu
, int64_t cpu_index
, Error
**errp
)
725 int64_t orig_addr
= addr
, orig_size
= size
;
731 cpu
= qemu_get_cpu(cpu_index
);
733 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cpu-index",
738 f
= fopen(filename
, "wb");
740 error_setg_file_open(errp
, errno
, filename
);
748 if (cpu_memory_rw_debug(cpu
, addr
, buf
, l
, 0) != 0) {
749 error_setg(errp
, "Invalid addr 0x%016" PRIx64
"/size %" PRId64
750 " specified", orig_addr
, orig_size
);
753 if (fwrite(buf
, 1, l
, f
) != l
) {
754 error_setg(errp
, QERR_IO_ERROR
);
765 void qmp_pmemsave(int64_t addr
, int64_t size
, const char *filename
,
772 f
= fopen(filename
, "wb");
774 error_setg_file_open(errp
, errno
, filename
);
782 cpu_physical_memory_read(addr
, buf
, l
);
783 if (fwrite(buf
, 1, l
, f
) != l
) {
784 error_setg(errp
, QERR_IO_ERROR
);
795 void qmp_inject_nmi(Error
**errp
)
797 nmi_monitor_handle(monitor_get_cpu_index(monitor_cur()), errp
);