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-misc.h"
30 #include "qapi/qapi-events-run-state.h"
31 #include "qapi/qmp/qerror.h"
32 #include "exec/gdbstub.h"
33 #include "sysemu/hw_accel.h"
34 #include "exec/exec-all.h"
35 #include "qemu/thread.h"
36 #include "qemu/plugin.h"
37 #include "sysemu/cpus.h"
38 #include "qemu/guest-random.h"
40 #include "sysemu/replay.h"
41 #include "sysemu/runstate.h"
42 #include "sysemu/cpu-timers.h"
43 #include "hw/boards.h"
48 #include <sys/prctl.h>
51 #define PR_MCE_KILL 33
54 #ifndef PR_MCE_KILL_SET
55 #define PR_MCE_KILL_SET 1
58 #ifndef PR_MCE_KILL_EARLY
59 #define PR_MCE_KILL_EARLY 1
62 #endif /* CONFIG_LINUX */
64 static QemuMutex qemu_global_mutex
;
66 bool cpu_is_stopped(CPUState
*cpu
)
68 return cpu
->stopped
|| !runstate_is_running();
71 bool cpu_work_list_empty(CPUState
*cpu
)
75 qemu_mutex_lock(&cpu
->work_mutex
);
76 ret
= QSIMPLEQ_EMPTY(&cpu
->work_list
);
77 qemu_mutex_unlock(&cpu
->work_mutex
);
81 bool cpu_thread_is_idle(CPUState
*cpu
)
83 if (cpu
->stop
|| !cpu_work_list_empty(cpu
)) {
86 if (cpu_is_stopped(cpu
)) {
89 if (!cpu
->halted
|| cpu_has_work(cpu
) ||
90 kvm_halt_in_kernel()) {
96 bool all_cpu_threads_idle(void)
101 if (!cpu_thread_is_idle(cpu
)) {
108 /***********************************************************/
109 void hw_error(const char *fmt
, ...)
115 fprintf(stderr
, "qemu: hardware error: ");
116 vfprintf(stderr
, fmt
, ap
);
117 fprintf(stderr
, "\n");
119 fprintf(stderr
, "CPU #%d:\n", cpu
->cpu_index
);
120 cpu_dump_state(cpu
, stderr
, CPU_DUMP_FPU
);
127 * The chosen accelerator is supposed to register this.
129 static const CpusAccel
*cpus_accel
;
131 void cpu_synchronize_all_states(void)
136 cpu_synchronize_state(cpu
);
140 void cpu_synchronize_all_post_reset(void)
145 cpu_synchronize_post_reset(cpu
);
149 void cpu_synchronize_all_post_init(void)
154 cpu_synchronize_post_init(cpu
);
158 void cpu_synchronize_all_pre_loadvm(void)
163 cpu_synchronize_pre_loadvm(cpu
);
167 void cpu_synchronize_state(CPUState
*cpu
)
169 if (cpus_accel
->synchronize_state
) {
170 cpus_accel
->synchronize_state(cpu
);
174 void cpu_synchronize_post_reset(CPUState
*cpu
)
176 if (cpus_accel
->synchronize_post_reset
) {
177 cpus_accel
->synchronize_post_reset(cpu
);
181 void cpu_synchronize_post_init(CPUState
*cpu
)
183 if (cpus_accel
->synchronize_post_init
) {
184 cpus_accel
->synchronize_post_init(cpu
);
188 void cpu_synchronize_pre_loadvm(CPUState
*cpu
)
190 if (cpus_accel
->synchronize_pre_loadvm
) {
191 cpus_accel
->synchronize_pre_loadvm(cpu
);
195 int64_t cpus_get_virtual_clock(void)
200 * need to check that cpus_accel is not NULL, because qcow2 calls
201 * qemu_get_clock_ns(CLOCK_VIRTUAL) without any accel initialized and
202 * with ticks disabled in some io-tests:
203 * 030 040 041 060 099 120 127 140 156 161 172 181 191 192 195 203 229 249 256 267
209 if (cpus_accel
&& cpus_accel
->get_virtual_clock
) {
210 return cpus_accel
->get_virtual_clock();
212 return cpu_get_clock();
216 * return the time elapsed in VM between vm_start and vm_stop. Unless
217 * icount is active, cpus_get_elapsed_ticks() uses units of the host CPU cycle
220 int64_t cpus_get_elapsed_ticks(void)
222 if (cpus_accel
->get_elapsed_ticks
) {
223 return cpus_accel
->get_elapsed_ticks();
225 return cpu_get_ticks();
228 static void generic_handle_interrupt(CPUState
*cpu
, int mask
)
230 cpu
->interrupt_request
|= mask
;
232 if (!qemu_cpu_is_self(cpu
)) {
237 void cpu_interrupt(CPUState
*cpu
, int mask
)
239 if (cpus_accel
->handle_interrupt
) {
240 cpus_accel
->handle_interrupt(cpu
, mask
);
242 generic_handle_interrupt(cpu
, mask
);
246 static int do_vm_stop(RunState state
, bool send_stop
)
250 if (runstate_is_running()) {
254 vm_state_notify(0, state
);
256 qapi_event_send_stop();
261 ret
= bdrv_flush_all();
266 /* Special vm_stop() variant for terminating the process. Historically clients
267 * did not expect a QMP STOP event and so we need to retain compatibility.
269 int vm_shutdown(void)
271 return do_vm_stop(RUN_STATE_SHUTDOWN
, false);
274 bool cpu_can_run(CPUState
*cpu
)
279 if (cpu_is_stopped(cpu
)) {
285 void cpu_handle_guest_debug(CPUState
*cpu
)
287 gdb_set_stop_cpu(cpu
);
288 qemu_system_debug_request();
293 static void sigbus_reraise(void)
296 struct sigaction action
;
298 memset(&action
, 0, sizeof(action
));
299 action
.sa_handler
= SIG_DFL
;
300 if (!sigaction(SIGBUS
, &action
, NULL
)) {
303 sigaddset(&set
, SIGBUS
);
304 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
306 perror("Failed to re-raise SIGBUS!\n");
310 static void sigbus_handler(int n
, siginfo_t
*siginfo
, void *ctx
)
312 if (siginfo
->si_code
!= BUS_MCEERR_AO
&& siginfo
->si_code
!= BUS_MCEERR_AR
) {
317 /* Called asynchronously in VCPU thread. */
318 if (kvm_on_sigbus_vcpu(current_cpu
, siginfo
->si_code
, siginfo
->si_addr
)) {
322 /* Called synchronously (via signalfd) in main thread. */
323 if (kvm_on_sigbus(siginfo
->si_code
, siginfo
->si_addr
)) {
329 static void qemu_init_sigbus(void)
331 struct sigaction action
;
333 memset(&action
, 0, sizeof(action
));
334 action
.sa_flags
= SA_SIGINFO
;
335 action
.sa_sigaction
= sigbus_handler
;
336 sigaction(SIGBUS
, &action
, NULL
);
338 prctl(PR_MCE_KILL
, PR_MCE_KILL_SET
, PR_MCE_KILL_EARLY
, 0, 0);
340 #else /* !CONFIG_LINUX */
341 static void qemu_init_sigbus(void)
344 #endif /* !CONFIG_LINUX */
346 static QemuThread io_thread
;
349 static QemuCond qemu_cpu_cond
;
351 static QemuCond qemu_pause_cond
;
353 void qemu_init_cpu_loop(void)
356 qemu_cond_init(&qemu_cpu_cond
);
357 qemu_cond_init(&qemu_pause_cond
);
358 qemu_mutex_init(&qemu_global_mutex
);
360 qemu_thread_get_self(&io_thread
);
363 void run_on_cpu(CPUState
*cpu
, run_on_cpu_func func
, run_on_cpu_data data
)
365 do_run_on_cpu(cpu
, func
, data
, &qemu_global_mutex
);
368 static void qemu_cpu_stop(CPUState
*cpu
, bool exit
)
370 g_assert(qemu_cpu_is_self(cpu
));
376 qemu_cond_broadcast(&qemu_pause_cond
);
379 void qemu_wait_io_event_common(CPUState
*cpu
)
381 qatomic_mb_set(&cpu
->thread_kicked
, false);
383 qemu_cpu_stop(cpu
, false);
385 process_queued_cpu_work(cpu
);
388 void qemu_wait_io_event(CPUState
*cpu
)
392 while (cpu_thread_is_idle(cpu
)) {
395 qemu_plugin_vcpu_idle_cb(cpu
);
397 qemu_cond_wait(cpu
->halt_cond
, &qemu_global_mutex
);
400 qemu_plugin_vcpu_resume_cb(cpu
);
404 /* Eat dummy APC queued by cpus_kick_thread. */
409 qemu_wait_io_event_common(cpu
);
412 void cpus_kick_thread(CPUState
*cpu
)
417 if (cpu
->thread_kicked
) {
420 cpu
->thread_kicked
= true;
421 err
= pthread_kill(cpu
->thread
->thread
, SIG_IPI
);
422 if (err
&& err
!= ESRCH
) {
423 fprintf(stderr
, "qemu:%s: %s", __func__
, strerror(err
));
429 void qemu_cpu_kick(CPUState
*cpu
)
431 qemu_cond_broadcast(cpu
->halt_cond
);
432 if (cpus_accel
->kick_vcpu_thread
) {
433 cpus_accel
->kick_vcpu_thread(cpu
);
434 } else { /* default */
435 cpus_kick_thread(cpu
);
439 void qemu_cpu_kick_self(void)
442 cpus_kick_thread(current_cpu
);
445 bool qemu_cpu_is_self(CPUState
*cpu
)
447 return qemu_thread_is_self(cpu
->thread
);
450 bool qemu_in_vcpu_thread(void)
452 return current_cpu
&& qemu_cpu_is_self(current_cpu
);
455 static __thread
bool iothread_locked
= false;
457 bool qemu_mutex_iothread_locked(void)
459 return iothread_locked
;
463 * The BQL is taken from so many places that it is worth profiling the
464 * callers directly, instead of funneling them all through a single function.
466 void qemu_mutex_lock_iothread_impl(const char *file
, int line
)
468 QemuMutexLockFunc bql_lock
= qatomic_read(&qemu_bql_mutex_lock_func
);
470 g_assert(!qemu_mutex_iothread_locked());
471 bql_lock(&qemu_global_mutex
, file
, line
);
472 iothread_locked
= true;
475 void qemu_mutex_unlock_iothread(void)
477 g_assert(qemu_mutex_iothread_locked());
478 iothread_locked
= false;
479 qemu_mutex_unlock(&qemu_global_mutex
);
482 void qemu_cond_wait_iothread(QemuCond
*cond
)
484 qemu_cond_wait(cond
, &qemu_global_mutex
);
487 void qemu_cond_timedwait_iothread(QemuCond
*cond
, int ms
)
489 qemu_cond_timedwait(cond
, &qemu_global_mutex
, ms
);
492 /* signal CPU creation */
493 void cpu_thread_signal_created(CPUState
*cpu
)
496 qemu_cond_signal(&qemu_cpu_cond
);
499 /* signal CPU destruction */
500 void cpu_thread_signal_destroyed(CPUState
*cpu
)
502 cpu
->created
= false;
503 qemu_cond_signal(&qemu_cpu_cond
);
507 static bool all_vcpus_paused(void)
520 void pause_all_vcpus(void)
524 qemu_clock_enable(QEMU_CLOCK_VIRTUAL
, false);
526 if (qemu_cpu_is_self(cpu
)) {
527 qemu_cpu_stop(cpu
, true);
534 /* We need to drop the replay_lock so any vCPU threads woken up
535 * can finish their replay tasks
537 replay_mutex_unlock();
539 while (!all_vcpus_paused()) {
540 qemu_cond_wait(&qemu_pause_cond
, &qemu_global_mutex
);
546 qemu_mutex_unlock_iothread();
548 qemu_mutex_lock_iothread();
551 void cpu_resume(CPUState
*cpu
)
554 cpu
->stopped
= false;
558 void resume_all_vcpus(void)
562 if (!runstate_is_running()) {
566 qemu_clock_enable(QEMU_CLOCK_VIRTUAL
, true);
572 void cpu_remove_sync(CPUState
*cpu
)
577 qemu_mutex_unlock_iothread();
578 qemu_thread_join(cpu
->thread
);
579 qemu_mutex_lock_iothread();
582 void cpus_register_accel(const CpusAccel
*ca
)
585 assert(ca
->create_vcpu_thread
!= NULL
); /* mandatory */
589 void qemu_init_vcpu(CPUState
*cpu
)
591 MachineState
*ms
= MACHINE(qdev_get_machine());
593 cpu
->nr_cores
= ms
->smp
.cores
;
594 cpu
->nr_threads
= ms
->smp
.threads
;
596 cpu
->random_seed
= qemu_guest_random_seed_thread_part1();
599 /* If the target cpu hasn't set up any address spaces itself,
600 * give it the default one.
603 cpu_address_space_init(cpu
, 0, "cpu-memory", cpu
->memory
);
606 /* accelerators all implement the CpusAccel interface */
607 g_assert(cpus_accel
!= NULL
&& cpus_accel
->create_vcpu_thread
!= NULL
);
608 cpus_accel
->create_vcpu_thread(cpu
);
610 while (!cpu
->created
) {
611 qemu_cond_wait(&qemu_cpu_cond
, &qemu_global_mutex
);
615 void cpu_stop_current(void)
618 current_cpu
->stop
= true;
619 cpu_exit(current_cpu
);
623 int vm_stop(RunState state
)
625 if (qemu_in_vcpu_thread()) {
626 qemu_system_vmstop_request_prepare();
627 qemu_system_vmstop_request(state
);
629 * FIXME: should not return to device code in case
630 * vm_stop() has been requested.
636 return do_vm_stop(state
, true);
640 * Prepare for (re)starting the VM.
641 * Returns -1 if the vCPUs are not to be restarted (e.g. if they are already
642 * running or in case of an error condition), 0 otherwise.
644 int vm_prepare_start(void)
648 qemu_vmstop_requested(&requested
);
649 if (runstate_is_running() && requested
== RUN_STATE__MAX
) {
653 /* Ensure that a STOP/RESUME pair of events is emitted if a
654 * vmstop request was pending. The BLOCK_IO_ERROR event, for
655 * example, according to documentation is always followed by
658 if (runstate_is_running()) {
659 qapi_event_send_stop();
660 qapi_event_send_resume();
664 /* We are sending this now, but the CPUs will be resumed shortly later */
665 qapi_event_send_resume();
668 runstate_set(RUN_STATE_RUNNING
);
669 vm_state_notify(1, RUN_STATE_RUNNING
);
675 if (!vm_prepare_start()) {
680 /* does a state transition even if the VM is already stopped,
681 current state is forgotten forever */
682 int vm_stop_force_state(RunState state
)
684 if (runstate_is_running()) {
685 return vm_stop(state
);
690 /* Make sure to return an error if the flush in a previous vm_stop()
692 return bdrv_flush_all();
696 void list_cpus(const char *optarg
)
698 /* XXX: implement xxx_cpu_list for targets that still miss it */
699 #if defined(cpu_list)
704 void qmp_memsave(int64_t addr
, int64_t size
, const char *filename
,
705 bool has_cpu
, int64_t cpu_index
, Error
**errp
)
711 int64_t orig_addr
= addr
, orig_size
= size
;
717 cpu
= qemu_get_cpu(cpu_index
);
719 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cpu-index",
724 f
= fopen(filename
, "wb");
726 error_setg_file_open(errp
, errno
, filename
);
734 if (cpu_memory_rw_debug(cpu
, addr
, buf
, l
, 0) != 0) {
735 error_setg(errp
, "Invalid addr 0x%016" PRIx64
"/size %" PRId64
736 " specified", orig_addr
, orig_size
);
739 if (fwrite(buf
, 1, l
, f
) != l
) {
740 error_setg(errp
, QERR_IO_ERROR
);
751 void qmp_pmemsave(int64_t addr
, int64_t size
, const char *filename
,
758 f
= fopen(filename
, "wb");
760 error_setg_file_open(errp
, errno
, filename
);
768 cpu_physical_memory_read(addr
, buf
, l
);
769 if (fwrite(buf
, 1, l
, f
) != l
) {
770 error_setg(errp
, QERR_IO_ERROR
);
781 void qmp_inject_nmi(Error
**errp
)
783 nmi_monitor_handle(monitor_get_cpu_index(), errp
);