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/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 "sysemu/whpx.h"
45 #include "hw/boards.h"
51 #include <sys/prctl.h>
54 #define PR_MCE_KILL 33
57 #ifndef PR_MCE_KILL_SET
58 #define PR_MCE_KILL_SET 1
61 #ifndef PR_MCE_KILL_EARLY
62 #define PR_MCE_KILL_EARLY 1
65 #endif /* CONFIG_LINUX */
67 static QemuMutex qemu_global_mutex
;
70 * The chosen accelerator is supposed to register this.
72 static const AccelOpsClass
*cpus_accel
;
74 bool cpu_is_stopped(CPUState
*cpu
)
76 return cpu
->stopped
|| !runstate_is_running();
79 bool cpu_work_list_empty(CPUState
*cpu
)
81 return QSIMPLEQ_EMPTY_ATOMIC(&cpu
->work_list
);
84 bool cpu_thread_is_idle(CPUState
*cpu
)
86 if (cpu
->stop
|| !cpu_work_list_empty(cpu
)) {
89 if (cpu_is_stopped(cpu
)) {
92 if (!cpu
->halted
|| cpu_has_work(cpu
)) {
95 if (cpus_accel
->cpu_thread_is_idle
) {
96 return cpus_accel
->cpu_thread_is_idle(cpu
);
101 bool all_cpu_threads_idle(void)
106 if (!cpu_thread_is_idle(cpu
)) {
113 /***********************************************************/
114 void hw_error(const char *fmt
, ...)
120 fprintf(stderr
, "qemu: hardware error: ");
121 vfprintf(stderr
, fmt
, ap
);
122 fprintf(stderr
, "\n");
124 fprintf(stderr
, "CPU #%d:\n", cpu
->cpu_index
);
125 cpu_dump_state(cpu
, stderr
, CPU_DUMP_FPU
);
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 bool cpus_are_resettable(void)
197 if (cpus_accel
->cpus_are_resettable
) {
198 return cpus_accel
->cpus_are_resettable();
203 int64_t cpus_get_virtual_clock(void)
208 * need to check that cpus_accel is not NULL, because qcow2 calls
209 * qemu_get_clock_ns(CLOCK_VIRTUAL) without any accel initialized and
210 * with ticks disabled in some io-tests:
211 * 030 040 041 060 099 120 127 140 156 161 172 181 191 192 195 203 229 249 256 267
217 if (cpus_accel
&& cpus_accel
->get_virtual_clock
) {
218 return cpus_accel
->get_virtual_clock();
220 return cpu_get_clock();
224 * return the time elapsed in VM between vm_start and vm_stop. Unless
225 * icount is active, cpus_get_elapsed_ticks() uses units of the host CPU cycle
228 int64_t cpus_get_elapsed_ticks(void)
230 if (cpus_accel
->get_elapsed_ticks
) {
231 return cpus_accel
->get_elapsed_ticks();
233 return cpu_get_ticks();
236 static void generic_handle_interrupt(CPUState
*cpu
, int mask
)
238 cpu
->interrupt_request
|= mask
;
240 if (!qemu_cpu_is_self(cpu
)) {
245 void cpu_interrupt(CPUState
*cpu
, int mask
)
247 if (cpus_accel
->handle_interrupt
) {
248 cpus_accel
->handle_interrupt(cpu
, mask
);
250 generic_handle_interrupt(cpu
, mask
);
254 static int do_vm_stop(RunState state
, bool send_stop
)
258 if (runstate_is_running()) {
262 vm_state_notify(0, state
);
264 qapi_event_send_stop();
269 ret
= bdrv_flush_all();
270 trace_vm_stop_flush_all(ret
);
275 /* Special vm_stop() variant for terminating the process. Historically clients
276 * did not expect a QMP STOP event and so we need to retain compatibility.
278 int vm_shutdown(void)
280 return do_vm_stop(RUN_STATE_SHUTDOWN
, false);
283 bool cpu_can_run(CPUState
*cpu
)
288 if (cpu_is_stopped(cpu
)) {
294 void cpu_handle_guest_debug(CPUState
*cpu
)
296 if (replay_running_debug()) {
297 if (!cpu
->singlestep_enabled
) {
299 * Report about the breakpoint and
300 * make a single step to skip it
303 cpu_single_step(cpu
, SSTEP_ENABLE
);
305 cpu_single_step(cpu
, 0);
308 gdb_set_stop_cpu(cpu
);
309 qemu_system_debug_request();
315 static void sigbus_reraise(void)
318 struct sigaction action
;
320 memset(&action
, 0, sizeof(action
));
321 action
.sa_handler
= SIG_DFL
;
322 if (!sigaction(SIGBUS
, &action
, NULL
)) {
325 sigaddset(&set
, SIGBUS
);
326 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
328 perror("Failed to re-raise SIGBUS!");
332 static void sigbus_handler(int n
, siginfo_t
*siginfo
, void *ctx
)
334 if (siginfo
->si_code
!= BUS_MCEERR_AO
&& siginfo
->si_code
!= BUS_MCEERR_AR
) {
339 /* Called asynchronously in VCPU thread. */
340 if (kvm_on_sigbus_vcpu(current_cpu
, siginfo
->si_code
, siginfo
->si_addr
)) {
344 /* Called synchronously (via signalfd) in main thread. */
345 if (kvm_on_sigbus(siginfo
->si_code
, siginfo
->si_addr
)) {
351 static void qemu_init_sigbus(void)
353 struct sigaction action
;
356 * ALERT: when modifying this, take care that SIGBUS forwarding in
357 * qemu_prealloc_mem() will continue working as expected.
359 memset(&action
, 0, sizeof(action
));
360 action
.sa_flags
= SA_SIGINFO
;
361 action
.sa_sigaction
= sigbus_handler
;
362 sigaction(SIGBUS
, &action
, NULL
);
364 prctl(PR_MCE_KILL
, PR_MCE_KILL_SET
, PR_MCE_KILL_EARLY
, 0, 0);
366 #else /* !CONFIG_LINUX */
367 static void qemu_init_sigbus(void)
370 #endif /* !CONFIG_LINUX */
372 static QemuThread io_thread
;
375 static QemuCond qemu_cpu_cond
;
377 static QemuCond qemu_pause_cond
;
379 void qemu_init_cpu_loop(void)
382 qemu_cond_init(&qemu_cpu_cond
);
383 qemu_cond_init(&qemu_pause_cond
);
384 qemu_mutex_init(&qemu_global_mutex
);
386 qemu_thread_get_self(&io_thread
);
389 void run_on_cpu(CPUState
*cpu
, run_on_cpu_func func
, run_on_cpu_data data
)
391 do_run_on_cpu(cpu
, func
, data
, &qemu_global_mutex
);
394 static void qemu_cpu_stop(CPUState
*cpu
, bool exit
)
396 g_assert(qemu_cpu_is_self(cpu
));
402 qemu_cond_broadcast(&qemu_pause_cond
);
405 void qemu_wait_io_event_common(CPUState
*cpu
)
407 qatomic_mb_set(&cpu
->thread_kicked
, false);
409 qemu_cpu_stop(cpu
, false);
411 process_queued_cpu_work(cpu
);
414 void qemu_wait_io_event(CPUState
*cpu
)
418 while (cpu_thread_is_idle(cpu
)) {
421 qemu_plugin_vcpu_idle_cb(cpu
);
423 qemu_cond_wait(cpu
->halt_cond
, &qemu_global_mutex
);
426 qemu_plugin_vcpu_resume_cb(cpu
);
430 /* Eat dummy APC queued by cpus_kick_thread. */
435 qemu_wait_io_event_common(cpu
);
438 void cpus_kick_thread(CPUState
*cpu
)
443 if (cpu
->thread_kicked
) {
446 cpu
->thread_kicked
= true;
447 err
= pthread_kill(cpu
->thread
->thread
, SIG_IPI
);
448 if (err
&& err
!= ESRCH
) {
449 fprintf(stderr
, "qemu:%s: %s", __func__
, strerror(err
));
455 void qemu_cpu_kick(CPUState
*cpu
)
457 qemu_cond_broadcast(cpu
->halt_cond
);
458 if (cpus_accel
->kick_vcpu_thread
) {
459 cpus_accel
->kick_vcpu_thread(cpu
);
460 } else { /* default */
461 cpus_kick_thread(cpu
);
465 void qemu_cpu_kick_self(void)
468 cpus_kick_thread(current_cpu
);
471 bool qemu_cpu_is_self(CPUState
*cpu
)
473 return qemu_thread_is_self(cpu
->thread
);
476 bool qemu_in_vcpu_thread(void)
478 return current_cpu
&& qemu_cpu_is_self(current_cpu
);
481 QEMU_DEFINE_STATIC_CO_TLS(bool, iothread_locked
)
483 bool qemu_mutex_iothread_locked(void)
485 return get_iothread_locked();
488 bool qemu_in_main_thread(void)
490 return qemu_mutex_iothread_locked();
494 * The BQL is taken from so many places that it is worth profiling the
495 * callers directly, instead of funneling them all through a single function.
497 void qemu_mutex_lock_iothread_impl(const char *file
, int line
)
499 QemuMutexLockFunc bql_lock
= qatomic_read(&qemu_bql_mutex_lock_func
);
501 g_assert(!qemu_mutex_iothread_locked());
502 bql_lock(&qemu_global_mutex
, file
, line
);
503 set_iothread_locked(true);
506 void qemu_mutex_unlock_iothread(void)
508 g_assert(qemu_mutex_iothread_locked());
509 set_iothread_locked(false);
510 qemu_mutex_unlock(&qemu_global_mutex
);
513 void qemu_cond_wait_iothread(QemuCond
*cond
)
515 qemu_cond_wait(cond
, &qemu_global_mutex
);
518 void qemu_cond_timedwait_iothread(QemuCond
*cond
, int ms
)
520 qemu_cond_timedwait(cond
, &qemu_global_mutex
, ms
);
523 /* signal CPU creation */
524 void cpu_thread_signal_created(CPUState
*cpu
)
527 qemu_cond_signal(&qemu_cpu_cond
);
530 /* signal CPU destruction */
531 void cpu_thread_signal_destroyed(CPUState
*cpu
)
533 cpu
->created
= false;
534 qemu_cond_signal(&qemu_cpu_cond
);
538 static bool all_vcpus_paused(void)
551 void pause_all_vcpus(void)
555 qemu_clock_enable(QEMU_CLOCK_VIRTUAL
, false);
557 if (qemu_cpu_is_self(cpu
)) {
558 qemu_cpu_stop(cpu
, true);
565 /* We need to drop the replay_lock so any vCPU threads woken up
566 * can finish their replay tasks
568 replay_mutex_unlock();
570 while (!all_vcpus_paused()) {
571 qemu_cond_wait(&qemu_pause_cond
, &qemu_global_mutex
);
577 qemu_mutex_unlock_iothread();
579 qemu_mutex_lock_iothread();
582 void cpu_resume(CPUState
*cpu
)
585 cpu
->stopped
= false;
589 void resume_all_vcpus(void)
593 if (!runstate_is_running()) {
597 qemu_clock_enable(QEMU_CLOCK_VIRTUAL
, true);
603 void cpu_remove_sync(CPUState
*cpu
)
608 qemu_mutex_unlock_iothread();
609 qemu_thread_join(cpu
->thread
);
610 qemu_mutex_lock_iothread();
613 void cpus_register_accel(const AccelOpsClass
*ops
)
616 assert(ops
->create_vcpu_thread
!= NULL
); /* mandatory */
620 const AccelOpsClass
*cpus_get_accel(void)
622 /* broken if we call this early */
627 void qemu_init_vcpu(CPUState
*cpu
)
629 MachineState
*ms
= MACHINE(qdev_get_machine());
631 cpu
->nr_cores
= ms
->smp
.cores
;
632 cpu
->nr_threads
= ms
->smp
.threads
;
634 cpu
->random_seed
= qemu_guest_random_seed_thread_part1();
637 /* If the target cpu hasn't set up any address spaces itself,
638 * give it the default one.
641 cpu_address_space_init(cpu
, 0, "cpu-memory", cpu
->memory
);
644 /* accelerators all implement the AccelOpsClass */
645 g_assert(cpus_accel
!= NULL
&& cpus_accel
->create_vcpu_thread
!= NULL
);
646 cpus_accel
->create_vcpu_thread(cpu
);
648 while (!cpu
->created
) {
649 qemu_cond_wait(&qemu_cpu_cond
, &qemu_global_mutex
);
653 void cpu_stop_current(void)
656 current_cpu
->stop
= true;
657 cpu_exit(current_cpu
);
661 int vm_stop(RunState state
)
663 if (qemu_in_vcpu_thread()) {
664 qemu_system_vmstop_request_prepare();
665 qemu_system_vmstop_request(state
);
667 * FIXME: should not return to device code in case
668 * vm_stop() has been requested.
674 return do_vm_stop(state
, true);
678 * Prepare for (re)starting the VM.
679 * Returns -1 if the vCPUs are not to be restarted (e.g. if they are already
680 * running or in case of an error condition), 0 otherwise.
682 int vm_prepare_start(bool step_pending
)
686 qemu_vmstop_requested(&requested
);
687 if (runstate_is_running() && requested
== RUN_STATE__MAX
) {
691 /* Ensure that a STOP/RESUME pair of events is emitted if a
692 * vmstop request was pending. The BLOCK_IO_ERROR event, for
693 * example, according to documentation is always followed by
696 if (runstate_is_running()) {
697 qapi_event_send_stop();
698 qapi_event_send_resume();
703 * WHPX accelerator needs to know whether we are going to step
704 * any CPUs, before starting the first one.
706 if (cpus_accel
->synchronize_pre_resume
) {
707 cpus_accel
->synchronize_pre_resume(step_pending
);
710 /* We are sending this now, but the CPUs will be resumed shortly later */
711 qapi_event_send_resume();
714 runstate_set(RUN_STATE_RUNNING
);
715 vm_state_notify(1, RUN_STATE_RUNNING
);
721 if (!vm_prepare_start(false)) {
726 /* does a state transition even if the VM is already stopped,
727 current state is forgotten forever */
728 int vm_stop_force_state(RunState state
)
730 if (runstate_is_running()) {
731 return vm_stop(state
);
737 /* Make sure to return an error if the flush in a previous vm_stop()
739 ret
= bdrv_flush_all();
740 trace_vm_stop_flush_all(ret
);
745 void qmp_memsave(int64_t addr
, int64_t size
, const char *filename
,
746 bool has_cpu
, int64_t cpu_index
, Error
**errp
)
752 int64_t orig_addr
= addr
, orig_size
= size
;
758 cpu
= qemu_get_cpu(cpu_index
);
760 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cpu-index",
765 f
= fopen(filename
, "wb");
767 error_setg_file_open(errp
, errno
, filename
);
775 if (cpu_memory_rw_debug(cpu
, addr
, buf
, l
, 0) != 0) {
776 error_setg(errp
, "Invalid addr 0x%016" PRIx64
"/size %" PRId64
777 " specified", orig_addr
, orig_size
);
780 if (fwrite(buf
, 1, l
, f
) != l
) {
781 error_setg(errp
, QERR_IO_ERROR
);
792 void qmp_pmemsave(int64_t addr
, int64_t size
, const char *filename
,
799 f
= fopen(filename
, "wb");
801 error_setg_file_open(errp
, errno
, filename
);
809 cpu_physical_memory_read(addr
, buf
, l
);
810 if (fwrite(buf
, 1, l
, f
) != l
) {
811 error_setg(errp
, QERR_IO_ERROR
);
822 void qmp_inject_nmi(Error
**errp
)
824 nmi_monitor_handle(monitor_get_cpu_index(monitor_cur()), errp
);