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 "monitor/monitor.h"
27 #include "qemu/coroutine-tls.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/cpu-common.h"
36 #include "qemu/thread.h"
37 #include "qemu/main-loop.h"
38 #include "qemu/plugin.h"
39 #include "sysemu/cpus.h"
40 #include "qemu/guest-random.h"
42 #include "sysemu/replay.h"
43 #include "sysemu/runstate.h"
44 #include "sysemu/cpu-timers.h"
45 #include "sysemu/whpx.h"
46 #include "hw/boards.h"
52 #include <sys/prctl.h>
55 #define PR_MCE_KILL 33
58 #ifndef PR_MCE_KILL_SET
59 #define PR_MCE_KILL_SET 1
62 #ifndef PR_MCE_KILL_EARLY
63 #define PR_MCE_KILL_EARLY 1
66 #endif /* CONFIG_LINUX */
68 /* The Big QEMU Lock (BQL) */
72 * The chosen accelerator is supposed to register this.
74 static const AccelOpsClass
*cpus_accel
;
76 bool cpu_is_stopped(CPUState
*cpu
)
78 return cpu
->stopped
|| !runstate_is_running();
81 bool cpu_work_list_empty(CPUState
*cpu
)
83 return QSIMPLEQ_EMPTY_ATOMIC(&cpu
->work_list
);
86 bool cpu_thread_is_idle(CPUState
*cpu
)
88 if (cpu
->stop
|| !cpu_work_list_empty(cpu
)) {
91 if (cpu_is_stopped(cpu
)) {
94 if (!cpu
->halted
|| cpu_has_work(cpu
)) {
97 if (cpus_accel
->cpu_thread_is_idle
) {
98 return cpus_accel
->cpu_thread_is_idle(cpu
);
103 bool all_cpu_threads_idle(void)
108 if (!cpu_thread_is_idle(cpu
)) {
115 /***********************************************************/
116 void hw_error(const char *fmt
, ...)
122 fprintf(stderr
, "qemu: hardware error: ");
123 vfprintf(stderr
, fmt
, ap
);
124 fprintf(stderr
, "\n");
126 fprintf(stderr
, "CPU #%d:\n", cpu
->cpu_index
);
127 cpu_dump_state(cpu
, stderr
, CPU_DUMP_FPU
);
133 void cpu_synchronize_all_states(void)
138 cpu_synchronize_state(cpu
);
142 void cpu_synchronize_all_post_reset(void)
147 cpu_synchronize_post_reset(cpu
);
151 void cpu_synchronize_all_post_init(void)
156 cpu_synchronize_post_init(cpu
);
160 void cpu_synchronize_all_pre_loadvm(void)
165 cpu_synchronize_pre_loadvm(cpu
);
169 void cpu_synchronize_state(CPUState
*cpu
)
171 if (cpus_accel
->synchronize_state
) {
172 cpus_accel
->synchronize_state(cpu
);
176 void cpu_synchronize_post_reset(CPUState
*cpu
)
178 if (cpus_accel
->synchronize_post_reset
) {
179 cpus_accel
->synchronize_post_reset(cpu
);
183 void cpu_synchronize_post_init(CPUState
*cpu
)
185 if (cpus_accel
->synchronize_post_init
) {
186 cpus_accel
->synchronize_post_init(cpu
);
190 void cpu_synchronize_pre_loadvm(CPUState
*cpu
)
192 if (cpus_accel
->synchronize_pre_loadvm
) {
193 cpus_accel
->synchronize_pre_loadvm(cpu
);
197 bool cpus_are_resettable(void)
199 if (cpus_accel
->cpus_are_resettable
) {
200 return cpus_accel
->cpus_are_resettable();
205 void cpu_exec_reset_hold(CPUState
*cpu
)
207 if (cpus_accel
->cpu_reset_hold
) {
208 cpus_accel
->cpu_reset_hold(cpu
);
212 int64_t cpus_get_virtual_clock(void)
217 * need to check that cpus_accel is not NULL, because qcow2 calls
218 * qemu_get_clock_ns(CLOCK_VIRTUAL) without any accel initialized and
219 * with ticks disabled in some io-tests:
220 * 030 040 041 060 099 120 127 140 156 161 172 181 191 192 195 203 229 249 256 267
226 if (cpus_accel
&& cpus_accel
->get_virtual_clock
) {
227 return cpus_accel
->get_virtual_clock();
229 return cpu_get_clock();
233 * return the time elapsed in VM between vm_start and vm_stop. Unless
234 * icount is active, cpus_get_elapsed_ticks() uses units of the host CPU cycle
237 int64_t cpus_get_elapsed_ticks(void)
239 if (cpus_accel
->get_elapsed_ticks
) {
240 return cpus_accel
->get_elapsed_ticks();
242 return cpu_get_ticks();
245 static void generic_handle_interrupt(CPUState
*cpu
, int mask
)
247 cpu
->interrupt_request
|= mask
;
249 if (!qemu_cpu_is_self(cpu
)) {
254 void cpu_interrupt(CPUState
*cpu
, int mask
)
256 if (cpus_accel
->handle_interrupt
) {
257 cpus_accel
->handle_interrupt(cpu
, mask
);
259 generic_handle_interrupt(cpu
, mask
);
264 * True if the vm was previously suspended, and has not been woken or reset.
266 static int vm_was_suspended
;
268 void vm_set_suspended(bool suspended
)
270 vm_was_suspended
= suspended
;
273 bool vm_get_suspended(void)
275 return vm_was_suspended
;
278 static int do_vm_stop(RunState state
, bool send_stop
)
281 RunState oldstate
= runstate_get();
283 if (runstate_is_live(oldstate
)) {
284 vm_was_suspended
= (oldstate
== RUN_STATE_SUSPENDED
);
287 if (oldstate
== RUN_STATE_RUNNING
) {
290 vm_state_notify(0, state
);
292 qapi_event_send_stop();
297 ret
= bdrv_flush_all();
298 trace_vm_stop_flush_all(ret
);
303 /* Special vm_stop() variant for terminating the process. Historically clients
304 * did not expect a QMP STOP event and so we need to retain compatibility.
306 int vm_shutdown(void)
308 return do_vm_stop(RUN_STATE_SHUTDOWN
, false);
311 bool cpu_can_run(CPUState
*cpu
)
316 if (cpu_is_stopped(cpu
)) {
322 void cpu_handle_guest_debug(CPUState
*cpu
)
324 if (replay_running_debug()) {
325 if (!cpu
->singlestep_enabled
) {
327 * Report about the breakpoint and
328 * make a single step to skip it
331 cpu_single_step(cpu
, SSTEP_ENABLE
);
333 cpu_single_step(cpu
, 0);
336 gdb_set_stop_cpu(cpu
);
337 qemu_system_debug_request();
343 static void sigbus_reraise(void)
346 struct sigaction action
;
348 memset(&action
, 0, sizeof(action
));
349 action
.sa_handler
= SIG_DFL
;
350 if (!sigaction(SIGBUS
, &action
, NULL
)) {
353 sigaddset(&set
, SIGBUS
);
354 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
356 perror("Failed to re-raise SIGBUS!");
360 static void sigbus_handler(int n
, siginfo_t
*siginfo
, void *ctx
)
362 if (siginfo
->si_code
!= BUS_MCEERR_AO
&& siginfo
->si_code
!= BUS_MCEERR_AR
) {
367 /* Called asynchronously in VCPU thread. */
368 if (kvm_on_sigbus_vcpu(current_cpu
, siginfo
->si_code
, siginfo
->si_addr
)) {
372 /* Called synchronously (via signalfd) in main thread. */
373 if (kvm_on_sigbus(siginfo
->si_code
, siginfo
->si_addr
)) {
379 static void qemu_init_sigbus(void)
381 struct sigaction action
;
384 * ALERT: when modifying this, take care that SIGBUS forwarding in
385 * qemu_prealloc_mem() will continue working as expected.
387 memset(&action
, 0, sizeof(action
));
388 action
.sa_flags
= SA_SIGINFO
;
389 action
.sa_sigaction
= sigbus_handler
;
390 sigaction(SIGBUS
, &action
, NULL
);
392 prctl(PR_MCE_KILL
, PR_MCE_KILL_SET
, PR_MCE_KILL_EARLY
, 0, 0);
394 #else /* !CONFIG_LINUX */
395 static void qemu_init_sigbus(void)
398 #endif /* !CONFIG_LINUX */
400 static QemuThread io_thread
;
403 static QemuCond qemu_cpu_cond
;
405 static QemuCond qemu_pause_cond
;
407 void qemu_init_cpu_loop(void)
410 qemu_cond_init(&qemu_cpu_cond
);
411 qemu_cond_init(&qemu_pause_cond
);
412 qemu_mutex_init(&bql
);
414 qemu_thread_get_self(&io_thread
);
417 void run_on_cpu(CPUState
*cpu
, run_on_cpu_func func
, run_on_cpu_data data
)
419 do_run_on_cpu(cpu
, func
, data
, &bql
);
422 static void qemu_cpu_stop(CPUState
*cpu
, bool exit
)
424 g_assert(qemu_cpu_is_self(cpu
));
430 qemu_cond_broadcast(&qemu_pause_cond
);
433 void qemu_wait_io_event_common(CPUState
*cpu
)
435 qatomic_set_mb(&cpu
->thread_kicked
, false);
437 qemu_cpu_stop(cpu
, false);
439 process_queued_cpu_work(cpu
);
442 void qemu_wait_io_event(CPUState
*cpu
)
446 while (cpu_thread_is_idle(cpu
)) {
449 qemu_plugin_vcpu_idle_cb(cpu
);
451 qemu_cond_wait(cpu
->halt_cond
, &bql
);
454 qemu_plugin_vcpu_resume_cb(cpu
);
457 qemu_wait_io_event_common(cpu
);
460 void cpus_kick_thread(CPUState
*cpu
)
462 if (cpu
->thread_kicked
) {
465 cpu
->thread_kicked
= true;
468 int err
= pthread_kill(cpu
->thread
->thread
, SIG_IPI
);
469 if (err
&& err
!= ESRCH
) {
470 fprintf(stderr
, "qemu:%s: %s", __func__
, strerror(err
));
474 qemu_sem_post(&cpu
->sem
);
478 void qemu_cpu_kick(CPUState
*cpu
)
480 qemu_cond_broadcast(cpu
->halt_cond
);
481 if (cpus_accel
->kick_vcpu_thread
) {
482 cpus_accel
->kick_vcpu_thread(cpu
);
483 } else { /* default */
484 cpus_kick_thread(cpu
);
488 void qemu_cpu_kick_self(void)
491 cpus_kick_thread(current_cpu
);
494 bool qemu_cpu_is_self(CPUState
*cpu
)
496 return qemu_thread_is_self(cpu
->thread
);
499 bool qemu_in_vcpu_thread(void)
501 return current_cpu
&& qemu_cpu_is_self(current_cpu
);
504 QEMU_DEFINE_STATIC_CO_TLS(bool, bql_locked
)
506 bool bql_locked(void)
508 return get_bql_locked();
511 bool qemu_in_main_thread(void)
517 * The BQL is taken from so many places that it is worth profiling the
518 * callers directly, instead of funneling them all through a single function.
520 void bql_lock_impl(const char *file
, int line
)
522 QemuMutexLockFunc bql_lock_fn
= qatomic_read(&bql_mutex_lock_func
);
524 g_assert(!bql_locked());
525 bql_lock_fn(&bql
, file
, line
);
526 set_bql_locked(true);
529 void bql_unlock(void)
531 g_assert(bql_locked());
532 set_bql_locked(false);
533 qemu_mutex_unlock(&bql
);
536 void qemu_cond_wait_bql(QemuCond
*cond
)
538 qemu_cond_wait(cond
, &bql
);
541 void qemu_cond_timedwait_bql(QemuCond
*cond
, int ms
)
543 qemu_cond_timedwait(cond
, &bql
, ms
);
546 /* signal CPU creation */
547 void cpu_thread_signal_created(CPUState
*cpu
)
550 qemu_cond_signal(&qemu_cpu_cond
);
553 /* signal CPU destruction */
554 void cpu_thread_signal_destroyed(CPUState
*cpu
)
556 cpu
->created
= false;
557 qemu_cond_signal(&qemu_cpu_cond
);
561 static bool all_vcpus_paused(void)
574 void pause_all_vcpus(void)
578 qemu_clock_enable(QEMU_CLOCK_VIRTUAL
, false);
580 if (qemu_cpu_is_self(cpu
)) {
581 qemu_cpu_stop(cpu
, true);
588 /* We need to drop the replay_lock so any vCPU threads woken up
589 * can finish their replay tasks
591 replay_mutex_unlock();
593 while (!all_vcpus_paused()) {
594 qemu_cond_wait(&qemu_pause_cond
, &bql
);
605 void cpu_resume(CPUState
*cpu
)
608 cpu
->stopped
= false;
612 void resume_all_vcpus(void)
616 if (!runstate_is_running()) {
620 qemu_clock_enable(QEMU_CLOCK_VIRTUAL
, true);
626 void cpu_remove_sync(CPUState
*cpu
)
632 qemu_thread_join(cpu
->thread
);
636 void cpus_register_accel(const AccelOpsClass
*ops
)
639 assert(ops
->create_vcpu_thread
!= NULL
); /* mandatory */
643 const AccelOpsClass
*cpus_get_accel(void)
645 /* broken if we call this early */
650 void qemu_init_vcpu(CPUState
*cpu
)
652 MachineState
*ms
= MACHINE(qdev_get_machine());
654 cpu
->nr_cores
= machine_topo_get_cores_per_socket(ms
);
655 cpu
->nr_threads
= ms
->smp
.threads
;
657 cpu
->random_seed
= qemu_guest_random_seed_thread_part1();
660 /* If the target cpu hasn't set up any address spaces itself,
661 * give it the default one.
664 cpu_address_space_init(cpu
, 0, "cpu-memory", cpu
->memory
);
667 /* accelerators all implement the AccelOpsClass */
668 g_assert(cpus_accel
!= NULL
&& cpus_accel
->create_vcpu_thread
!= NULL
);
669 cpus_accel
->create_vcpu_thread(cpu
);
671 while (!cpu
->created
) {
672 qemu_cond_wait(&qemu_cpu_cond
, &bql
);
676 void cpu_stop_current(void)
679 current_cpu
->stop
= true;
680 cpu_exit(current_cpu
);
684 int vm_stop(RunState state
)
686 if (qemu_in_vcpu_thread()) {
687 qemu_system_vmstop_request_prepare();
688 qemu_system_vmstop_request(state
);
690 * FIXME: should not return to device code in case
691 * vm_stop() has been requested.
697 return do_vm_stop(state
, true);
701 * Prepare for (re)starting the VM.
702 * Returns 0 if the vCPUs should be restarted, -1 on an error condition,
705 int vm_prepare_start(bool step_pending
)
707 int ret
= vm_was_suspended
? 1 : 0;
708 RunState state
= vm_was_suspended
? RUN_STATE_SUSPENDED
: RUN_STATE_RUNNING
;
711 qemu_vmstop_requested(&requested
);
712 if (runstate_is_running() && requested
== RUN_STATE__MAX
) {
716 /* Ensure that a STOP/RESUME pair of events is emitted if a
717 * vmstop request was pending. The BLOCK_IO_ERROR event, for
718 * example, according to documentation is always followed by
721 if (runstate_is_running()) {
722 qapi_event_send_stop();
723 qapi_event_send_resume();
728 * WHPX accelerator needs to know whether we are going to step
729 * any CPUs, before starting the first one.
731 if (cpus_accel
->synchronize_pre_resume
) {
732 cpus_accel
->synchronize_pre_resume(step_pending
);
735 /* We are sending this now, but the CPUs will be resumed shortly later */
736 qapi_event_send_resume();
740 vm_state_notify(1, state
);
741 vm_was_suspended
= false;
747 if (!vm_prepare_start(false)) {
752 void vm_resume(RunState state
)
754 if (runstate_is_live(state
)) {
761 /* does a state transition even if the VM is already stopped,
762 current state is forgotten forever */
763 int vm_stop_force_state(RunState state
)
765 if (runstate_is_live(runstate_get())) {
766 return vm_stop(state
);
772 /* Make sure to return an error if the flush in a previous vm_stop()
774 ret
= bdrv_flush_all();
775 trace_vm_stop_flush_all(ret
);
780 void qmp_memsave(int64_t addr
, int64_t size
, const char *filename
,
781 bool has_cpu
, int64_t cpu_index
, Error
**errp
)
787 int64_t orig_addr
= addr
, orig_size
= size
;
793 cpu
= qemu_get_cpu(cpu_index
);
795 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cpu-index",
800 f
= fopen(filename
, "wb");
802 error_setg_file_open(errp
, errno
, filename
);
810 if (cpu_memory_rw_debug(cpu
, addr
, buf
, l
, 0) != 0) {
811 error_setg(errp
, "Invalid addr 0x%016" PRIx64
"/size %" PRId64
812 " specified", orig_addr
, orig_size
);
815 if (fwrite(buf
, 1, l
, f
) != l
) {
816 error_setg(errp
, "writing memory to '%s' failed",
828 void qmp_pmemsave(int64_t addr
, int64_t size
, const char *filename
,
835 f
= fopen(filename
, "wb");
837 error_setg_file_open(errp
, errno
, filename
);
845 cpu_physical_memory_read(addr
, buf
, l
);
846 if (fwrite(buf
, 1, l
, f
) != l
) {
847 error_setg(errp
, "writing memory to '%s' failed",
859 void qmp_inject_nmi(Error
**errp
)
861 nmi_monitor_handle(monitor_get_cpu_index(monitor_cur()), errp
);