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/config-file.h"
28 #include "monitor/monitor.h"
29 #include "qapi/error.h"
30 #include "qapi/qapi-commands-misc.h"
31 #include "qapi/qapi-events-run-state.h"
32 #include "qapi/qmp/qerror.h"
33 #include "qemu/error-report.h"
34 #include "qemu/qemu-print.h"
35 #include "sysemu/sysemu.h"
36 #include "sysemu/tcg.h"
37 #include "sysemu/block-backend.h"
38 #include "exec/gdbstub.h"
39 #include "sysemu/dma.h"
40 #include "sysemu/hw_accel.h"
41 #include "sysemu/kvm.h"
42 #include "sysemu/hax.h"
43 #include "sysemu/hvf.h"
44 #include "sysemu/whpx.h"
45 #include "exec/exec-all.h"
47 #include "qemu/thread.h"
48 #include "sysemu/cpus.h"
49 #include "sysemu/qtest.h"
50 #include "qemu/main-loop.h"
51 #include "qemu/option.h"
52 #include "qemu/bitmap.h"
53 #include "qemu/seqlock.h"
54 #include "qemu/guest-random.h"
57 #include "sysemu/replay.h"
58 #include "hw/boards.h"
62 #include <sys/prctl.h>
65 #define PR_MCE_KILL 33
68 #ifndef PR_MCE_KILL_SET
69 #define PR_MCE_KILL_SET 1
72 #ifndef PR_MCE_KILL_EARLY
73 #define PR_MCE_KILL_EARLY 1
76 #endif /* CONFIG_LINUX */
81 /* vcpu throttling controls */
82 static QEMUTimer
*throttle_timer
;
83 static unsigned int throttle_percentage
;
85 #define CPU_THROTTLE_PCT_MIN 1
86 #define CPU_THROTTLE_PCT_MAX 99
87 #define CPU_THROTTLE_TIMESLICE_NS 10000000
89 bool cpu_is_stopped(CPUState
*cpu
)
91 return cpu
->stopped
|| !runstate_is_running();
94 static bool cpu_thread_is_idle(CPUState
*cpu
)
96 if (cpu
->stop
|| cpu
->queued_work_first
) {
99 if (cpu_is_stopped(cpu
)) {
102 if (!cpu
->halted
|| cpu_has_work(cpu
) ||
103 kvm_halt_in_kernel()) {
109 static bool all_cpu_threads_idle(void)
114 if (!cpu_thread_is_idle(cpu
)) {
121 /***********************************************************/
122 /* guest cycle counter */
124 /* Protected by TimersState seqlock */
126 static bool icount_sleep
= true;
127 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
128 #define MAX_ICOUNT_SHIFT 10
130 typedef struct TimersState
{
131 /* Protected by BQL. */
132 int64_t cpu_ticks_prev
;
133 int64_t cpu_ticks_offset
;
135 /* Protect fields that can be respectively read outside the
136 * BQL, and written from multiple threads.
138 QemuSeqLock vm_clock_seqlock
;
139 QemuSpin vm_clock_lock
;
141 int16_t cpu_ticks_enabled
;
143 /* Conversion factor from emulated instructions to virtual clock ticks. */
144 int16_t icount_time_shift
;
146 /* Compensate for varying guest execution speed. */
147 int64_t qemu_icount_bias
;
149 int64_t vm_clock_warp_start
;
150 int64_t cpu_clock_offset
;
152 /* Only written by TCG thread */
155 /* for adjusting icount */
156 QEMUTimer
*icount_rt_timer
;
157 QEMUTimer
*icount_vm_timer
;
158 QEMUTimer
*icount_warp_timer
;
161 static TimersState timers_state
;
165 * We default to false if we know other options have been enabled
166 * which are currently incompatible with MTTCG. Otherwise when each
167 * guest (target) has been updated to support:
168 * - atomic instructions
169 * - memory ordering primitives (barriers)
170 * they can set the appropriate CONFIG flags in ${target}-softmmu.mak
172 * Once a guest architecture has been converted to the new primitives
173 * there are two remaining limitations to check.
175 * - The guest can't be oversized (e.g. 64 bit guest on 32 bit host)
176 * - The host must have a stronger memory order than the guest
178 * It may be possible in future to support strong guests on weak hosts
179 * but that will require tagging all load/stores in a guest with their
180 * implicit memory order requirements which would likely slow things
184 static bool check_tcg_memory_orders_compatible(void)
186 #if defined(TCG_GUEST_DEFAULT_MO) && defined(TCG_TARGET_DEFAULT_MO)
187 return (TCG_GUEST_DEFAULT_MO
& ~TCG_TARGET_DEFAULT_MO
) == 0;
193 static bool default_mttcg_enabled(void)
195 if (use_icount
|| TCG_OVERSIZED_GUEST
) {
198 #ifdef TARGET_SUPPORTS_MTTCG
199 return check_tcg_memory_orders_compatible();
206 void qemu_tcg_configure(QemuOpts
*opts
, Error
**errp
)
208 const char *t
= qemu_opt_get(opts
, "thread");
210 if (strcmp(t
, "multi") == 0) {
211 if (TCG_OVERSIZED_GUEST
) {
212 error_setg(errp
, "No MTTCG when guest word size > hosts");
213 } else if (use_icount
) {
214 error_setg(errp
, "No MTTCG when icount is enabled");
216 #ifndef TARGET_SUPPORTS_MTTCG
217 warn_report("Guest not yet converted to MTTCG - "
218 "you may get unexpected results");
220 if (!check_tcg_memory_orders_compatible()) {
221 warn_report("Guest expects a stronger memory ordering "
222 "than the host provides");
223 error_printf("This may cause strange/hard to debug errors\n");
225 mttcg_enabled
= true;
227 } else if (strcmp(t
, "single") == 0) {
228 mttcg_enabled
= false;
230 error_setg(errp
, "Invalid 'thread' setting %s", t
);
233 mttcg_enabled
= default_mttcg_enabled();
237 /* The current number of executed instructions is based on what we
238 * originally budgeted minus the current state of the decrementing
239 * icount counters in extra/u16.low.
241 static int64_t cpu_get_icount_executed(CPUState
*cpu
)
243 return (cpu
->icount_budget
-
244 (cpu_neg(cpu
)->icount_decr
.u16
.low
+ cpu
->icount_extra
));
248 * Update the global shared timer_state.qemu_icount to take into
249 * account executed instructions. This is done by the TCG vCPU
250 * thread so the main-loop can see time has moved forward.
252 static void cpu_update_icount_locked(CPUState
*cpu
)
254 int64_t executed
= cpu_get_icount_executed(cpu
);
255 cpu
->icount_budget
-= executed
;
257 atomic_set_i64(&timers_state
.qemu_icount
,
258 timers_state
.qemu_icount
+ executed
);
262 * Update the global shared timer_state.qemu_icount to take into
263 * account executed instructions. This is done by the TCG vCPU
264 * thread so the main-loop can see time has moved forward.
266 void cpu_update_icount(CPUState
*cpu
)
268 seqlock_write_lock(&timers_state
.vm_clock_seqlock
,
269 &timers_state
.vm_clock_lock
);
270 cpu_update_icount_locked(cpu
);
271 seqlock_write_unlock(&timers_state
.vm_clock_seqlock
,
272 &timers_state
.vm_clock_lock
);
275 static int64_t cpu_get_icount_raw_locked(void)
277 CPUState
*cpu
= current_cpu
;
279 if (cpu
&& cpu
->running
) {
280 if (!cpu
->can_do_io
) {
281 error_report("Bad icount read");
284 /* Take into account what has run */
285 cpu_update_icount_locked(cpu
);
287 /* The read is protected by the seqlock, but needs atomic64 to avoid UB */
288 return atomic_read_i64(&timers_state
.qemu_icount
);
291 static int64_t cpu_get_icount_locked(void)
293 int64_t icount
= cpu_get_icount_raw_locked();
294 return atomic_read_i64(&timers_state
.qemu_icount_bias
) +
295 cpu_icount_to_ns(icount
);
298 int64_t cpu_get_icount_raw(void)
304 start
= seqlock_read_begin(&timers_state
.vm_clock_seqlock
);
305 icount
= cpu_get_icount_raw_locked();
306 } while (seqlock_read_retry(&timers_state
.vm_clock_seqlock
, start
));
311 /* Return the virtual CPU time, based on the instruction counter. */
312 int64_t cpu_get_icount(void)
318 start
= seqlock_read_begin(&timers_state
.vm_clock_seqlock
);
319 icount
= cpu_get_icount_locked();
320 } while (seqlock_read_retry(&timers_state
.vm_clock_seqlock
, start
));
325 int64_t cpu_icount_to_ns(int64_t icount
)
327 return icount
<< atomic_read(&timers_state
.icount_time_shift
);
330 static int64_t cpu_get_ticks_locked(void)
332 int64_t ticks
= timers_state
.cpu_ticks_offset
;
333 if (timers_state
.cpu_ticks_enabled
) {
334 ticks
+= cpu_get_host_ticks();
337 if (timers_state
.cpu_ticks_prev
> ticks
) {
338 /* Non increasing ticks may happen if the host uses software suspend. */
339 timers_state
.cpu_ticks_offset
+= timers_state
.cpu_ticks_prev
- ticks
;
340 ticks
= timers_state
.cpu_ticks_prev
;
343 timers_state
.cpu_ticks_prev
= ticks
;
347 /* return the time elapsed in VM between vm_start and vm_stop. Unless
348 * icount is active, cpu_get_ticks() uses units of the host CPU cycle
351 int64_t cpu_get_ticks(void)
356 return cpu_get_icount();
359 qemu_spin_lock(&timers_state
.vm_clock_lock
);
360 ticks
= cpu_get_ticks_locked();
361 qemu_spin_unlock(&timers_state
.vm_clock_lock
);
365 static int64_t cpu_get_clock_locked(void)
369 time
= timers_state
.cpu_clock_offset
;
370 if (timers_state
.cpu_ticks_enabled
) {
377 /* Return the monotonic time elapsed in VM, i.e.,
378 * the time between vm_start and vm_stop
380 int64_t cpu_get_clock(void)
386 start
= seqlock_read_begin(&timers_state
.vm_clock_seqlock
);
387 ti
= cpu_get_clock_locked();
388 } while (seqlock_read_retry(&timers_state
.vm_clock_seqlock
, start
));
393 /* enable cpu_get_ticks()
394 * Caller must hold BQL which serves as mutex for vm_clock_seqlock.
396 void cpu_enable_ticks(void)
398 seqlock_write_lock(&timers_state
.vm_clock_seqlock
,
399 &timers_state
.vm_clock_lock
);
400 if (!timers_state
.cpu_ticks_enabled
) {
401 timers_state
.cpu_ticks_offset
-= cpu_get_host_ticks();
402 timers_state
.cpu_clock_offset
-= get_clock();
403 timers_state
.cpu_ticks_enabled
= 1;
405 seqlock_write_unlock(&timers_state
.vm_clock_seqlock
,
406 &timers_state
.vm_clock_lock
);
409 /* disable cpu_get_ticks() : the clock is stopped. You must not call
410 * cpu_get_ticks() after that.
411 * Caller must hold BQL which serves as mutex for vm_clock_seqlock.
413 void cpu_disable_ticks(void)
415 seqlock_write_lock(&timers_state
.vm_clock_seqlock
,
416 &timers_state
.vm_clock_lock
);
417 if (timers_state
.cpu_ticks_enabled
) {
418 timers_state
.cpu_ticks_offset
+= cpu_get_host_ticks();
419 timers_state
.cpu_clock_offset
= cpu_get_clock_locked();
420 timers_state
.cpu_ticks_enabled
= 0;
422 seqlock_write_unlock(&timers_state
.vm_clock_seqlock
,
423 &timers_state
.vm_clock_lock
);
426 /* Correlation between real and virtual time is always going to be
427 fairly approximate, so ignore small variation.
428 When the guest is idle real and virtual time will be aligned in
430 #define ICOUNT_WOBBLE (NANOSECONDS_PER_SECOND / 10)
432 static void icount_adjust(void)
438 /* Protected by TimersState mutex. */
439 static int64_t last_delta
;
441 /* If the VM is not running, then do nothing. */
442 if (!runstate_is_running()) {
446 seqlock_write_lock(&timers_state
.vm_clock_seqlock
,
447 &timers_state
.vm_clock_lock
);
448 cur_time
= cpu_get_clock_locked();
449 cur_icount
= cpu_get_icount_locked();
451 delta
= cur_icount
- cur_time
;
452 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
454 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
455 && timers_state
.icount_time_shift
> 0) {
456 /* The guest is getting too far ahead. Slow time down. */
457 atomic_set(&timers_state
.icount_time_shift
,
458 timers_state
.icount_time_shift
- 1);
461 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
462 && timers_state
.icount_time_shift
< MAX_ICOUNT_SHIFT
) {
463 /* The guest is getting too far behind. Speed time up. */
464 atomic_set(&timers_state
.icount_time_shift
,
465 timers_state
.icount_time_shift
+ 1);
468 atomic_set_i64(&timers_state
.qemu_icount_bias
,
469 cur_icount
- (timers_state
.qemu_icount
470 << timers_state
.icount_time_shift
));
471 seqlock_write_unlock(&timers_state
.vm_clock_seqlock
,
472 &timers_state
.vm_clock_lock
);
475 static void icount_adjust_rt(void *opaque
)
477 timer_mod(timers_state
.icount_rt_timer
,
478 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT
) + 1000);
482 static void icount_adjust_vm(void *opaque
)
484 timer_mod(timers_state
.icount_vm_timer
,
485 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
486 NANOSECONDS_PER_SECOND
/ 10);
490 static int64_t qemu_icount_round(int64_t count
)
492 int shift
= atomic_read(&timers_state
.icount_time_shift
);
493 return (count
+ (1 << shift
) - 1) >> shift
;
496 static void icount_warp_rt(void)
501 /* The icount_warp_timer is rescheduled soon after vm_clock_warp_start
502 * changes from -1 to another value, so the race here is okay.
505 seq
= seqlock_read_begin(&timers_state
.vm_clock_seqlock
);
506 warp_start
= timers_state
.vm_clock_warp_start
;
507 } while (seqlock_read_retry(&timers_state
.vm_clock_seqlock
, seq
));
509 if (warp_start
== -1) {
513 seqlock_write_lock(&timers_state
.vm_clock_seqlock
,
514 &timers_state
.vm_clock_lock
);
515 if (runstate_is_running()) {
516 int64_t clock
= REPLAY_CLOCK_LOCKED(REPLAY_CLOCK_VIRTUAL_RT
,
517 cpu_get_clock_locked());
520 warp_delta
= clock
- timers_state
.vm_clock_warp_start
;
521 if (use_icount
== 2) {
523 * In adaptive mode, do not let QEMU_CLOCK_VIRTUAL run too
524 * far ahead of real time.
526 int64_t cur_icount
= cpu_get_icount_locked();
527 int64_t delta
= clock
- cur_icount
;
528 warp_delta
= MIN(warp_delta
, delta
);
530 atomic_set_i64(&timers_state
.qemu_icount_bias
,
531 timers_state
.qemu_icount_bias
+ warp_delta
);
533 timers_state
.vm_clock_warp_start
= -1;
534 seqlock_write_unlock(&timers_state
.vm_clock_seqlock
,
535 &timers_state
.vm_clock_lock
);
537 if (qemu_clock_expired(QEMU_CLOCK_VIRTUAL
)) {
538 qemu_clock_notify(QEMU_CLOCK_VIRTUAL
);
542 static void icount_timer_cb(void *opaque
)
544 /* No need for a checkpoint because the timer already synchronizes
545 * with CHECKPOINT_CLOCK_VIRTUAL_RT.
550 void qtest_clock_warp(int64_t dest
)
552 int64_t clock
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
553 AioContext
*aio_context
;
554 assert(qtest_enabled());
555 aio_context
= qemu_get_aio_context();
556 while (clock
< dest
) {
557 int64_t deadline
= qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL
);
558 int64_t warp
= qemu_soonest_timeout(dest
- clock
, deadline
);
560 seqlock_write_lock(&timers_state
.vm_clock_seqlock
,
561 &timers_state
.vm_clock_lock
);
562 atomic_set_i64(&timers_state
.qemu_icount_bias
,
563 timers_state
.qemu_icount_bias
+ warp
);
564 seqlock_write_unlock(&timers_state
.vm_clock_seqlock
,
565 &timers_state
.vm_clock_lock
);
567 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL
);
568 timerlist_run_timers(aio_context
->tlg
.tl
[QEMU_CLOCK_VIRTUAL
]);
569 clock
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
571 qemu_clock_notify(QEMU_CLOCK_VIRTUAL
);
574 void qemu_start_warp_timer(void)
583 /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
584 * do not fire, so computing the deadline does not make sense.
586 if (!runstate_is_running()) {
590 if (replay_mode
!= REPLAY_MODE_PLAY
) {
591 if (!all_cpu_threads_idle()) {
595 if (qtest_enabled()) {
596 /* When testing, qtest commands advance icount. */
600 replay_checkpoint(CHECKPOINT_CLOCK_WARP_START
);
602 /* warp clock deterministically in record/replay mode */
603 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_START
)) {
604 /* vCPU is sleeping and warp can't be started.
605 It is probably a race condition: notification sent
606 to vCPU was processed in advance and vCPU went to sleep.
607 Therefore we have to wake it up for doing someting. */
608 if (replay_has_checkpoint()) {
609 qemu_clock_notify(QEMU_CLOCK_VIRTUAL
);
615 /* We want to use the earliest deadline from ALL vm_clocks */
616 clock
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT
);
617 deadline
= qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL
);
619 static bool notified
;
620 if (!icount_sleep
&& !notified
) {
621 warn_report("icount sleep disabled and no active timers");
629 * Ensure QEMU_CLOCK_VIRTUAL proceeds even when the virtual CPU goes to
630 * sleep. Otherwise, the CPU might be waiting for a future timer
631 * interrupt to wake it up, but the interrupt never comes because
632 * the vCPU isn't running any insns and thus doesn't advance the
633 * QEMU_CLOCK_VIRTUAL.
637 * We never let VCPUs sleep in no sleep icount mode.
638 * If there is a pending QEMU_CLOCK_VIRTUAL timer we just advance
639 * to the next QEMU_CLOCK_VIRTUAL event and notify it.
640 * It is useful when we want a deterministic execution time,
641 * isolated from host latencies.
643 seqlock_write_lock(&timers_state
.vm_clock_seqlock
,
644 &timers_state
.vm_clock_lock
);
645 atomic_set_i64(&timers_state
.qemu_icount_bias
,
646 timers_state
.qemu_icount_bias
+ deadline
);
647 seqlock_write_unlock(&timers_state
.vm_clock_seqlock
,
648 &timers_state
.vm_clock_lock
);
649 qemu_clock_notify(QEMU_CLOCK_VIRTUAL
);
652 * We do stop VCPUs and only advance QEMU_CLOCK_VIRTUAL after some
653 * "real" time, (related to the time left until the next event) has
654 * passed. The QEMU_CLOCK_VIRTUAL_RT clock will do this.
655 * This avoids that the warps are visible externally; for example,
656 * you will not be sending network packets continuously instead of
659 seqlock_write_lock(&timers_state
.vm_clock_seqlock
,
660 &timers_state
.vm_clock_lock
);
661 if (timers_state
.vm_clock_warp_start
== -1
662 || timers_state
.vm_clock_warp_start
> clock
) {
663 timers_state
.vm_clock_warp_start
= clock
;
665 seqlock_write_unlock(&timers_state
.vm_clock_seqlock
,
666 &timers_state
.vm_clock_lock
);
667 timer_mod_anticipate(timers_state
.icount_warp_timer
,
670 } else if (deadline
== 0) {
671 qemu_clock_notify(QEMU_CLOCK_VIRTUAL
);
675 static void qemu_account_warp_timer(void)
677 if (!use_icount
|| !icount_sleep
) {
681 /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
682 * do not fire, so computing the deadline does not make sense.
684 if (!runstate_is_running()) {
688 /* warp clock deterministically in record/replay mode */
689 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_ACCOUNT
)) {
693 timer_del(timers_state
.icount_warp_timer
);
697 static bool icount_state_needed(void *opaque
)
702 static bool warp_timer_state_needed(void *opaque
)
704 TimersState
*s
= opaque
;
705 return s
->icount_warp_timer
!= NULL
;
708 static bool adjust_timers_state_needed(void *opaque
)
710 TimersState
*s
= opaque
;
711 return s
->icount_rt_timer
!= NULL
;
715 * Subsection for warp timer migration is optional, because may not be created
717 static const VMStateDescription icount_vmstate_warp_timer
= {
718 .name
= "timer/icount/warp_timer",
720 .minimum_version_id
= 1,
721 .needed
= warp_timer_state_needed
,
722 .fields
= (VMStateField
[]) {
723 VMSTATE_INT64(vm_clock_warp_start
, TimersState
),
724 VMSTATE_TIMER_PTR(icount_warp_timer
, TimersState
),
725 VMSTATE_END_OF_LIST()
729 static const VMStateDescription icount_vmstate_adjust_timers
= {
730 .name
= "timer/icount/timers",
732 .minimum_version_id
= 1,
733 .needed
= adjust_timers_state_needed
,
734 .fields
= (VMStateField
[]) {
735 VMSTATE_TIMER_PTR(icount_rt_timer
, TimersState
),
736 VMSTATE_TIMER_PTR(icount_vm_timer
, TimersState
),
737 VMSTATE_END_OF_LIST()
742 * This is a subsection for icount migration.
744 static const VMStateDescription icount_vmstate_timers
= {
745 .name
= "timer/icount",
747 .minimum_version_id
= 1,
748 .needed
= icount_state_needed
,
749 .fields
= (VMStateField
[]) {
750 VMSTATE_INT64(qemu_icount_bias
, TimersState
),
751 VMSTATE_INT64(qemu_icount
, TimersState
),
752 VMSTATE_END_OF_LIST()
754 .subsections
= (const VMStateDescription
*[]) {
755 &icount_vmstate_warp_timer
,
756 &icount_vmstate_adjust_timers
,
761 static const VMStateDescription vmstate_timers
= {
764 .minimum_version_id
= 1,
765 .fields
= (VMStateField
[]) {
766 VMSTATE_INT64(cpu_ticks_offset
, TimersState
),
768 VMSTATE_INT64_V(cpu_clock_offset
, TimersState
, 2),
769 VMSTATE_END_OF_LIST()
771 .subsections
= (const VMStateDescription
*[]) {
772 &icount_vmstate_timers
,
777 static void cpu_throttle_thread(CPUState
*cpu
, run_on_cpu_data opaque
)
780 double throttle_ratio
;
783 if (!cpu_throttle_get_percentage()) {
787 pct
= (double)cpu_throttle_get_percentage()/100;
788 throttle_ratio
= pct
/ (1 - pct
);
789 sleeptime_ns
= (long)(throttle_ratio
* CPU_THROTTLE_TIMESLICE_NS
);
791 qemu_mutex_unlock_iothread();
792 g_usleep(sleeptime_ns
/ 1000); /* Convert ns to us for usleep call */
793 qemu_mutex_lock_iothread();
794 atomic_set(&cpu
->throttle_thread_scheduled
, 0);
797 static void cpu_throttle_timer_tick(void *opaque
)
802 /* Stop the timer if needed */
803 if (!cpu_throttle_get_percentage()) {
807 if (!atomic_xchg(&cpu
->throttle_thread_scheduled
, 1)) {
808 async_run_on_cpu(cpu
, cpu_throttle_thread
,
813 pct
= (double)cpu_throttle_get_percentage()/100;
814 timer_mod(throttle_timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT
) +
815 CPU_THROTTLE_TIMESLICE_NS
/ (1-pct
));
818 void cpu_throttle_set(int new_throttle_pct
)
820 /* Ensure throttle percentage is within valid range */
821 new_throttle_pct
= MIN(new_throttle_pct
, CPU_THROTTLE_PCT_MAX
);
822 new_throttle_pct
= MAX(new_throttle_pct
, CPU_THROTTLE_PCT_MIN
);
824 atomic_set(&throttle_percentage
, new_throttle_pct
);
826 timer_mod(throttle_timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT
) +
827 CPU_THROTTLE_TIMESLICE_NS
);
830 void cpu_throttle_stop(void)
832 atomic_set(&throttle_percentage
, 0);
835 bool cpu_throttle_active(void)
837 return (cpu_throttle_get_percentage() != 0);
840 int cpu_throttle_get_percentage(void)
842 return atomic_read(&throttle_percentage
);
845 void cpu_ticks_init(void)
847 seqlock_init(&timers_state
.vm_clock_seqlock
);
848 qemu_spin_init(&timers_state
.vm_clock_lock
);
849 vmstate_register(NULL
, 0, &vmstate_timers
, &timers_state
);
850 throttle_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL_RT
,
851 cpu_throttle_timer_tick
, NULL
);
854 void configure_icount(QemuOpts
*opts
, Error
**errp
)
857 char *rem_str
= NULL
;
859 option
= qemu_opt_get(opts
, "shift");
861 if (qemu_opt_get(opts
, "align") != NULL
) {
862 error_setg(errp
, "Please specify shift option when using align");
867 icount_sleep
= qemu_opt_get_bool(opts
, "sleep", true);
869 timers_state
.icount_warp_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL_RT
,
870 icount_timer_cb
, NULL
);
873 icount_align_option
= qemu_opt_get_bool(opts
, "align", false);
875 if (icount_align_option
&& !icount_sleep
) {
876 error_setg(errp
, "align=on and sleep=off are incompatible");
878 if (strcmp(option
, "auto") != 0) {
880 timers_state
.icount_time_shift
= strtol(option
, &rem_str
, 0);
881 if (errno
!= 0 || *rem_str
!= '\0' || !strlen(option
)) {
882 error_setg(errp
, "icount: Invalid shift value");
886 } else if (icount_align_option
) {
887 error_setg(errp
, "shift=auto and align=on are incompatible");
888 } else if (!icount_sleep
) {
889 error_setg(errp
, "shift=auto and sleep=off are incompatible");
894 /* 125MIPS seems a reasonable initial guess at the guest speed.
895 It will be corrected fairly quickly anyway. */
896 timers_state
.icount_time_shift
= 3;
898 /* Have both realtime and virtual time triggers for speed adjustment.
899 The realtime trigger catches emulated time passing too slowly,
900 the virtual time trigger catches emulated time passing too fast.
901 Realtime triggers occur even when idle, so use them less frequently
903 timers_state
.vm_clock_warp_start
= -1;
904 timers_state
.icount_rt_timer
= timer_new_ms(QEMU_CLOCK_VIRTUAL_RT
,
905 icount_adjust_rt
, NULL
);
906 timer_mod(timers_state
.icount_rt_timer
,
907 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT
) + 1000);
908 timers_state
.icount_vm_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
,
909 icount_adjust_vm
, NULL
);
910 timer_mod(timers_state
.icount_vm_timer
,
911 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
912 NANOSECONDS_PER_SECOND
/ 10);
915 /***********************************************************/
916 /* TCG vCPU kick timer
918 * The kick timer is responsible for moving single threaded vCPU
919 * emulation on to the next vCPU. If more than one vCPU is running a
920 * timer event with force a cpu->exit so the next vCPU can get
923 * The timer is removed if all vCPUs are idle and restarted again once
924 * idleness is complete.
927 static QEMUTimer
*tcg_kick_vcpu_timer
;
928 static CPUState
*tcg_current_rr_cpu
;
930 #define TCG_KICK_PERIOD (NANOSECONDS_PER_SECOND / 10)
932 static inline int64_t qemu_tcg_next_kick(void)
934 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + TCG_KICK_PERIOD
;
937 /* Kick the currently round-robin scheduled vCPU */
938 static void qemu_cpu_kick_rr_cpu(void)
942 cpu
= atomic_mb_read(&tcg_current_rr_cpu
);
946 } while (cpu
!= atomic_mb_read(&tcg_current_rr_cpu
));
949 static void do_nothing(CPUState
*cpu
, run_on_cpu_data unused
)
953 void qemu_timer_notify_cb(void *opaque
, QEMUClockType type
)
955 if (!use_icount
|| type
!= QEMU_CLOCK_VIRTUAL
) {
960 if (qemu_in_vcpu_thread()) {
961 /* A CPU is currently running; kick it back out to the
962 * tcg_cpu_exec() loop so it will recalculate its
963 * icount deadline immediately.
965 qemu_cpu_kick(current_cpu
);
966 } else if (first_cpu
) {
967 /* qemu_cpu_kick is not enough to kick a halted CPU out of
968 * qemu_tcg_wait_io_event. async_run_on_cpu, instead,
969 * causes cpu_thread_is_idle to return false. This way,
970 * handle_icount_deadline can run.
971 * If we have no CPUs at all for some reason, we don't
972 * need to do anything.
974 async_run_on_cpu(first_cpu
, do_nothing
, RUN_ON_CPU_NULL
);
978 static void kick_tcg_thread(void *opaque
)
980 timer_mod(tcg_kick_vcpu_timer
, qemu_tcg_next_kick());
981 qemu_cpu_kick_rr_cpu();
984 static void start_tcg_kick_timer(void)
986 assert(!mttcg_enabled
);
987 if (!tcg_kick_vcpu_timer
&& CPU_NEXT(first_cpu
)) {
988 tcg_kick_vcpu_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
,
989 kick_tcg_thread
, NULL
);
991 if (tcg_kick_vcpu_timer
&& !timer_pending(tcg_kick_vcpu_timer
)) {
992 timer_mod(tcg_kick_vcpu_timer
, qemu_tcg_next_kick());
996 static void stop_tcg_kick_timer(void)
998 assert(!mttcg_enabled
);
999 if (tcg_kick_vcpu_timer
&& timer_pending(tcg_kick_vcpu_timer
)) {
1000 timer_del(tcg_kick_vcpu_timer
);
1004 /***********************************************************/
1005 void hw_error(const char *fmt
, ...)
1011 fprintf(stderr
, "qemu: hardware error: ");
1012 vfprintf(stderr
, fmt
, ap
);
1013 fprintf(stderr
, "\n");
1015 fprintf(stderr
, "CPU #%d:\n", cpu
->cpu_index
);
1016 cpu_dump_state(cpu
, stderr
, CPU_DUMP_FPU
);
1022 void cpu_synchronize_all_states(void)
1027 cpu_synchronize_state(cpu
);
1028 /* TODO: move to cpu_synchronize_state() */
1029 if (hvf_enabled()) {
1030 hvf_cpu_synchronize_state(cpu
);
1035 void cpu_synchronize_all_post_reset(void)
1040 cpu_synchronize_post_reset(cpu
);
1041 /* TODO: move to cpu_synchronize_post_reset() */
1042 if (hvf_enabled()) {
1043 hvf_cpu_synchronize_post_reset(cpu
);
1048 void cpu_synchronize_all_post_init(void)
1053 cpu_synchronize_post_init(cpu
);
1054 /* TODO: move to cpu_synchronize_post_init() */
1055 if (hvf_enabled()) {
1056 hvf_cpu_synchronize_post_init(cpu
);
1061 void cpu_synchronize_all_pre_loadvm(void)
1066 cpu_synchronize_pre_loadvm(cpu
);
1070 static int do_vm_stop(RunState state
, bool send_stop
)
1074 if (runstate_is_running()) {
1075 cpu_disable_ticks();
1077 runstate_set(state
);
1078 vm_state_notify(0, state
);
1080 qapi_event_send_stop();
1085 replay_disable_events();
1086 ret
= bdrv_flush_all();
1091 /* Special vm_stop() variant for terminating the process. Historically clients
1092 * did not expect a QMP STOP event and so we need to retain compatibility.
1094 int vm_shutdown(void)
1096 return do_vm_stop(RUN_STATE_SHUTDOWN
, false);
1099 static bool cpu_can_run(CPUState
*cpu
)
1104 if (cpu_is_stopped(cpu
)) {
1110 static void cpu_handle_guest_debug(CPUState
*cpu
)
1112 gdb_set_stop_cpu(cpu
);
1113 qemu_system_debug_request();
1114 cpu
->stopped
= true;
1118 static void sigbus_reraise(void)
1121 struct sigaction action
;
1123 memset(&action
, 0, sizeof(action
));
1124 action
.sa_handler
= SIG_DFL
;
1125 if (!sigaction(SIGBUS
, &action
, NULL
)) {
1128 sigaddset(&set
, SIGBUS
);
1129 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
1131 perror("Failed to re-raise SIGBUS!\n");
1135 static void sigbus_handler(int n
, siginfo_t
*siginfo
, void *ctx
)
1137 if (siginfo
->si_code
!= BUS_MCEERR_AO
&& siginfo
->si_code
!= BUS_MCEERR_AR
) {
1142 /* Called asynchronously in VCPU thread. */
1143 if (kvm_on_sigbus_vcpu(current_cpu
, siginfo
->si_code
, siginfo
->si_addr
)) {
1147 /* Called synchronously (via signalfd) in main thread. */
1148 if (kvm_on_sigbus(siginfo
->si_code
, siginfo
->si_addr
)) {
1154 static void qemu_init_sigbus(void)
1156 struct sigaction action
;
1158 memset(&action
, 0, sizeof(action
));
1159 action
.sa_flags
= SA_SIGINFO
;
1160 action
.sa_sigaction
= sigbus_handler
;
1161 sigaction(SIGBUS
, &action
, NULL
);
1163 prctl(PR_MCE_KILL
, PR_MCE_KILL_SET
, PR_MCE_KILL_EARLY
, 0, 0);
1165 #else /* !CONFIG_LINUX */
1166 static void qemu_init_sigbus(void)
1169 #endif /* !CONFIG_LINUX */
1171 static QemuMutex qemu_global_mutex
;
1173 static QemuThread io_thread
;
1176 static QemuCond qemu_cpu_cond
;
1178 static QemuCond qemu_pause_cond
;
1180 void qemu_init_cpu_loop(void)
1183 qemu_cond_init(&qemu_cpu_cond
);
1184 qemu_cond_init(&qemu_pause_cond
);
1185 qemu_mutex_init(&qemu_global_mutex
);
1187 qemu_thread_get_self(&io_thread
);
1190 void run_on_cpu(CPUState
*cpu
, run_on_cpu_func func
, run_on_cpu_data data
)
1192 do_run_on_cpu(cpu
, func
, data
, &qemu_global_mutex
);
1195 static void qemu_kvm_destroy_vcpu(CPUState
*cpu
)
1197 if (kvm_destroy_vcpu(cpu
) < 0) {
1198 error_report("kvm_destroy_vcpu failed");
1203 static void qemu_tcg_destroy_vcpu(CPUState
*cpu
)
1207 static void qemu_cpu_stop(CPUState
*cpu
, bool exit
)
1209 g_assert(qemu_cpu_is_self(cpu
));
1211 cpu
->stopped
= true;
1215 qemu_cond_broadcast(&qemu_pause_cond
);
1218 static void qemu_wait_io_event_common(CPUState
*cpu
)
1220 atomic_mb_set(&cpu
->thread_kicked
, false);
1222 qemu_cpu_stop(cpu
, false);
1224 process_queued_cpu_work(cpu
);
1227 static void qemu_tcg_rr_wait_io_event(void)
1231 while (all_cpu_threads_idle()) {
1232 stop_tcg_kick_timer();
1233 qemu_cond_wait(first_cpu
->halt_cond
, &qemu_global_mutex
);
1236 start_tcg_kick_timer();
1239 qemu_wait_io_event_common(cpu
);
1243 static void qemu_wait_io_event(CPUState
*cpu
)
1245 while (cpu_thread_is_idle(cpu
)) {
1246 qemu_cond_wait(cpu
->halt_cond
, &qemu_global_mutex
);
1250 /* Eat dummy APC queued by qemu_cpu_kick_thread. */
1251 if (!tcg_enabled()) {
1255 qemu_wait_io_event_common(cpu
);
1258 static void *qemu_kvm_cpu_thread_fn(void *arg
)
1260 CPUState
*cpu
= arg
;
1263 rcu_register_thread();
1265 qemu_mutex_lock_iothread();
1266 qemu_thread_get_self(cpu
->thread
);
1267 cpu
->thread_id
= qemu_get_thread_id();
1271 r
= kvm_init_vcpu(cpu
);
1273 error_report("kvm_init_vcpu failed: %s", strerror(-r
));
1277 kvm_init_cpu_signals(cpu
);
1279 /* signal CPU creation */
1280 cpu
->created
= true;
1281 qemu_cond_signal(&qemu_cpu_cond
);
1282 qemu_guest_random_seed_thread_part2(cpu
->random_seed
);
1285 if (cpu_can_run(cpu
)) {
1286 r
= kvm_cpu_exec(cpu
);
1287 if (r
== EXCP_DEBUG
) {
1288 cpu_handle_guest_debug(cpu
);
1291 qemu_wait_io_event(cpu
);
1292 } while (!cpu
->unplug
|| cpu_can_run(cpu
));
1294 qemu_kvm_destroy_vcpu(cpu
);
1295 cpu
->created
= false;
1296 qemu_cond_signal(&qemu_cpu_cond
);
1297 qemu_mutex_unlock_iothread();
1298 rcu_unregister_thread();
1302 static void *qemu_dummy_cpu_thread_fn(void *arg
)
1305 error_report("qtest is not supported under Windows");
1308 CPUState
*cpu
= arg
;
1312 rcu_register_thread();
1314 qemu_mutex_lock_iothread();
1315 qemu_thread_get_self(cpu
->thread
);
1316 cpu
->thread_id
= qemu_get_thread_id();
1320 sigemptyset(&waitset
);
1321 sigaddset(&waitset
, SIG_IPI
);
1323 /* signal CPU creation */
1324 cpu
->created
= true;
1325 qemu_cond_signal(&qemu_cpu_cond
);
1326 qemu_guest_random_seed_thread_part2(cpu
->random_seed
);
1329 qemu_mutex_unlock_iothread();
1332 r
= sigwait(&waitset
, &sig
);
1333 } while (r
== -1 && (errno
== EAGAIN
|| errno
== EINTR
));
1338 qemu_mutex_lock_iothread();
1339 qemu_wait_io_event(cpu
);
1340 } while (!cpu
->unplug
);
1342 qemu_mutex_unlock_iothread();
1343 rcu_unregister_thread();
1348 static int64_t tcg_get_icount_limit(void)
1352 if (replay_mode
!= REPLAY_MODE_PLAY
) {
1353 deadline
= qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL
);
1355 /* Maintain prior (possibly buggy) behaviour where if no deadline
1356 * was set (as there is no QEMU_CLOCK_VIRTUAL timer) or it is more than
1357 * INT32_MAX nanoseconds ahead, we still use INT32_MAX
1360 if ((deadline
< 0) || (deadline
> INT32_MAX
)) {
1361 deadline
= INT32_MAX
;
1364 return qemu_icount_round(deadline
);
1366 return replay_get_instructions();
1370 static void handle_icount_deadline(void)
1372 assert(qemu_in_vcpu_thread());
1375 qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL
);
1377 if (deadline
== 0) {
1378 /* Wake up other AioContexts. */
1379 qemu_clock_notify(QEMU_CLOCK_VIRTUAL
);
1380 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL
);
1385 static void prepare_icount_for_run(CPUState
*cpu
)
1390 /* These should always be cleared by process_icount_data after
1391 * each vCPU execution. However u16.high can be raised
1392 * asynchronously by cpu_exit/cpu_interrupt/tcg_handle_interrupt
1394 g_assert(cpu_neg(cpu
)->icount_decr
.u16
.low
== 0);
1395 g_assert(cpu
->icount_extra
== 0);
1397 cpu
->icount_budget
= tcg_get_icount_limit();
1398 insns_left
= MIN(0xffff, cpu
->icount_budget
);
1399 cpu_neg(cpu
)->icount_decr
.u16
.low
= insns_left
;
1400 cpu
->icount_extra
= cpu
->icount_budget
- insns_left
;
1402 replay_mutex_lock();
1406 static void process_icount_data(CPUState
*cpu
)
1409 /* Account for executed instructions */
1410 cpu_update_icount(cpu
);
1412 /* Reset the counters */
1413 cpu_neg(cpu
)->icount_decr
.u16
.low
= 0;
1414 cpu
->icount_extra
= 0;
1415 cpu
->icount_budget
= 0;
1417 replay_account_executed_instructions();
1419 replay_mutex_unlock();
1424 static int tcg_cpu_exec(CPUState
*cpu
)
1427 #ifdef CONFIG_PROFILER
1431 assert(tcg_enabled());
1432 #ifdef CONFIG_PROFILER
1433 ti
= profile_getclock();
1435 cpu_exec_start(cpu
);
1436 ret
= cpu_exec(cpu
);
1438 #ifdef CONFIG_PROFILER
1439 atomic_set(&tcg_ctx
->prof
.cpu_exec_time
,
1440 tcg_ctx
->prof
.cpu_exec_time
+ profile_getclock() - ti
);
1445 /* Destroy any remaining vCPUs which have been unplugged and have
1448 static void deal_with_unplugged_cpus(void)
1453 if (cpu
->unplug
&& !cpu_can_run(cpu
)) {
1454 qemu_tcg_destroy_vcpu(cpu
);
1455 cpu
->created
= false;
1456 qemu_cond_signal(&qemu_cpu_cond
);
1462 /* Single-threaded TCG
1464 * In the single-threaded case each vCPU is simulated in turn. If
1465 * there is more than a single vCPU we create a simple timer to kick
1466 * the vCPU and ensure we don't get stuck in a tight loop in one vCPU.
1467 * This is done explicitly rather than relying on side-effects
1471 static void *qemu_tcg_rr_cpu_thread_fn(void *arg
)
1473 CPUState
*cpu
= arg
;
1475 assert(tcg_enabled());
1476 rcu_register_thread();
1477 tcg_register_thread();
1479 qemu_mutex_lock_iothread();
1480 qemu_thread_get_self(cpu
->thread
);
1482 cpu
->thread_id
= qemu_get_thread_id();
1483 cpu
->created
= true;
1485 qemu_cond_signal(&qemu_cpu_cond
);
1486 qemu_guest_random_seed_thread_part2(cpu
->random_seed
);
1488 /* wait for initial kick-off after machine start */
1489 while (first_cpu
->stopped
) {
1490 qemu_cond_wait(first_cpu
->halt_cond
, &qemu_global_mutex
);
1492 /* process any pending work */
1495 qemu_wait_io_event_common(cpu
);
1499 start_tcg_kick_timer();
1503 /* process any pending work */
1504 cpu
->exit_request
= 1;
1507 qemu_mutex_unlock_iothread();
1508 replay_mutex_lock();
1509 qemu_mutex_lock_iothread();
1510 /* Account partial waits to QEMU_CLOCK_VIRTUAL. */
1511 qemu_account_warp_timer();
1513 /* Run the timers here. This is much more efficient than
1514 * waking up the I/O thread and waiting for completion.
1516 handle_icount_deadline();
1518 replay_mutex_unlock();
1524 while (cpu
&& !cpu
->queued_work_first
&& !cpu
->exit_request
) {
1526 atomic_mb_set(&tcg_current_rr_cpu
, cpu
);
1529 qemu_clock_enable(QEMU_CLOCK_VIRTUAL
,
1530 (cpu
->singlestep_enabled
& SSTEP_NOTIMER
) == 0);
1532 if (cpu_can_run(cpu
)) {
1535 qemu_mutex_unlock_iothread();
1536 prepare_icount_for_run(cpu
);
1538 r
= tcg_cpu_exec(cpu
);
1540 process_icount_data(cpu
);
1541 qemu_mutex_lock_iothread();
1543 if (r
== EXCP_DEBUG
) {
1544 cpu_handle_guest_debug(cpu
);
1546 } else if (r
== EXCP_ATOMIC
) {
1547 qemu_mutex_unlock_iothread();
1548 cpu_exec_step_atomic(cpu
);
1549 qemu_mutex_lock_iothread();
1552 } else if (cpu
->stop
) {
1554 cpu
= CPU_NEXT(cpu
);
1559 cpu
= CPU_NEXT(cpu
);
1560 } /* while (cpu && !cpu->exit_request).. */
1562 /* Does not need atomic_mb_set because a spurious wakeup is okay. */
1563 atomic_set(&tcg_current_rr_cpu
, NULL
);
1565 if (cpu
&& cpu
->exit_request
) {
1566 atomic_mb_set(&cpu
->exit_request
, 0);
1569 if (use_icount
&& all_cpu_threads_idle()) {
1571 * When all cpus are sleeping (e.g in WFI), to avoid a deadlock
1572 * in the main_loop, wake it up in order to start the warp timer.
1574 qemu_notify_event();
1577 qemu_tcg_rr_wait_io_event();
1578 deal_with_unplugged_cpus();
1581 rcu_unregister_thread();
1585 static void *qemu_hax_cpu_thread_fn(void *arg
)
1587 CPUState
*cpu
= arg
;
1590 rcu_register_thread();
1591 qemu_mutex_lock_iothread();
1592 qemu_thread_get_self(cpu
->thread
);
1594 cpu
->thread_id
= qemu_get_thread_id();
1595 cpu
->created
= true;
1600 qemu_cond_signal(&qemu_cpu_cond
);
1601 qemu_guest_random_seed_thread_part2(cpu
->random_seed
);
1604 if (cpu_can_run(cpu
)) {
1605 r
= hax_smp_cpu_exec(cpu
);
1606 if (r
== EXCP_DEBUG
) {
1607 cpu_handle_guest_debug(cpu
);
1611 qemu_wait_io_event(cpu
);
1612 } while (!cpu
->unplug
|| cpu_can_run(cpu
));
1613 rcu_unregister_thread();
1617 /* The HVF-specific vCPU thread function. This one should only run when the host
1618 * CPU supports the VMX "unrestricted guest" feature. */
1619 static void *qemu_hvf_cpu_thread_fn(void *arg
)
1621 CPUState
*cpu
= arg
;
1625 assert(hvf_enabled());
1627 rcu_register_thread();
1629 qemu_mutex_lock_iothread();
1630 qemu_thread_get_self(cpu
->thread
);
1632 cpu
->thread_id
= qemu_get_thread_id();
1638 /* signal CPU creation */
1639 cpu
->created
= true;
1640 qemu_cond_signal(&qemu_cpu_cond
);
1641 qemu_guest_random_seed_thread_part2(cpu
->random_seed
);
1644 if (cpu_can_run(cpu
)) {
1645 r
= hvf_vcpu_exec(cpu
);
1646 if (r
== EXCP_DEBUG
) {
1647 cpu_handle_guest_debug(cpu
);
1650 qemu_wait_io_event(cpu
);
1651 } while (!cpu
->unplug
|| cpu_can_run(cpu
));
1653 hvf_vcpu_destroy(cpu
);
1654 cpu
->created
= false;
1655 qemu_cond_signal(&qemu_cpu_cond
);
1656 qemu_mutex_unlock_iothread();
1657 rcu_unregister_thread();
1661 static void *qemu_whpx_cpu_thread_fn(void *arg
)
1663 CPUState
*cpu
= arg
;
1666 rcu_register_thread();
1668 qemu_mutex_lock_iothread();
1669 qemu_thread_get_self(cpu
->thread
);
1670 cpu
->thread_id
= qemu_get_thread_id();
1673 r
= whpx_init_vcpu(cpu
);
1675 fprintf(stderr
, "whpx_init_vcpu failed: %s\n", strerror(-r
));
1679 /* signal CPU creation */
1680 cpu
->created
= true;
1681 qemu_cond_signal(&qemu_cpu_cond
);
1682 qemu_guest_random_seed_thread_part2(cpu
->random_seed
);
1685 if (cpu_can_run(cpu
)) {
1686 r
= whpx_vcpu_exec(cpu
);
1687 if (r
== EXCP_DEBUG
) {
1688 cpu_handle_guest_debug(cpu
);
1691 while (cpu_thread_is_idle(cpu
)) {
1692 qemu_cond_wait(cpu
->halt_cond
, &qemu_global_mutex
);
1694 qemu_wait_io_event_common(cpu
);
1695 } while (!cpu
->unplug
|| cpu_can_run(cpu
));
1697 whpx_destroy_vcpu(cpu
);
1698 cpu
->created
= false;
1699 qemu_cond_signal(&qemu_cpu_cond
);
1700 qemu_mutex_unlock_iothread();
1701 rcu_unregister_thread();
1706 static void CALLBACK
dummy_apc_func(ULONG_PTR unused
)
1711 /* Multi-threaded TCG
1713 * In the multi-threaded case each vCPU has its own thread. The TLS
1714 * variable current_cpu can be used deep in the code to find the
1715 * current CPUState for a given thread.
1718 static void *qemu_tcg_cpu_thread_fn(void *arg
)
1720 CPUState
*cpu
= arg
;
1722 assert(tcg_enabled());
1723 g_assert(!use_icount
);
1725 rcu_register_thread();
1726 tcg_register_thread();
1728 qemu_mutex_lock_iothread();
1729 qemu_thread_get_self(cpu
->thread
);
1731 cpu
->thread_id
= qemu_get_thread_id();
1732 cpu
->created
= true;
1735 qemu_cond_signal(&qemu_cpu_cond
);
1736 qemu_guest_random_seed_thread_part2(cpu
->random_seed
);
1738 /* process any pending work */
1739 cpu
->exit_request
= 1;
1742 if (cpu_can_run(cpu
)) {
1744 qemu_mutex_unlock_iothread();
1745 r
= tcg_cpu_exec(cpu
);
1746 qemu_mutex_lock_iothread();
1749 cpu_handle_guest_debug(cpu
);
1752 /* during start-up the vCPU is reset and the thread is
1753 * kicked several times. If we don't ensure we go back
1754 * to sleep in the halted state we won't cleanly
1755 * start-up when the vCPU is enabled.
1757 * cpu->halted should ensure we sleep in wait_io_event
1759 g_assert(cpu
->halted
);
1762 qemu_mutex_unlock_iothread();
1763 cpu_exec_step_atomic(cpu
);
1764 qemu_mutex_lock_iothread();
1766 /* Ignore everything else? */
1771 atomic_mb_set(&cpu
->exit_request
, 0);
1772 qemu_wait_io_event(cpu
);
1773 } while (!cpu
->unplug
|| cpu_can_run(cpu
));
1775 qemu_tcg_destroy_vcpu(cpu
);
1776 cpu
->created
= false;
1777 qemu_cond_signal(&qemu_cpu_cond
);
1778 qemu_mutex_unlock_iothread();
1779 rcu_unregister_thread();
1783 static void qemu_cpu_kick_thread(CPUState
*cpu
)
1788 if (cpu
->thread_kicked
) {
1791 cpu
->thread_kicked
= true;
1792 err
= pthread_kill(cpu
->thread
->thread
, SIG_IPI
);
1793 if (err
&& err
!= ESRCH
) {
1794 fprintf(stderr
, "qemu:%s: %s", __func__
, strerror(err
));
1798 if (!qemu_cpu_is_self(cpu
)) {
1799 if (whpx_enabled()) {
1800 whpx_vcpu_kick(cpu
);
1801 } else if (!QueueUserAPC(dummy_apc_func
, cpu
->hThread
, 0)) {
1802 fprintf(stderr
, "%s: QueueUserAPC failed with error %lu\n",
1803 __func__
, GetLastError());
1810 void qemu_cpu_kick(CPUState
*cpu
)
1812 qemu_cond_broadcast(cpu
->halt_cond
);
1813 if (tcg_enabled()) {
1815 /* NOP unless doing single-thread RR */
1816 qemu_cpu_kick_rr_cpu();
1818 if (hax_enabled()) {
1820 * FIXME: race condition with the exit_request check in
1823 cpu
->exit_request
= 1;
1825 qemu_cpu_kick_thread(cpu
);
1829 void qemu_cpu_kick_self(void)
1831 assert(current_cpu
);
1832 qemu_cpu_kick_thread(current_cpu
);
1835 bool qemu_cpu_is_self(CPUState
*cpu
)
1837 return qemu_thread_is_self(cpu
->thread
);
1840 bool qemu_in_vcpu_thread(void)
1842 return current_cpu
&& qemu_cpu_is_self(current_cpu
);
1845 static __thread
bool iothread_locked
= false;
1847 bool qemu_mutex_iothread_locked(void)
1849 return iothread_locked
;
1853 * The BQL is taken from so many places that it is worth profiling the
1854 * callers directly, instead of funneling them all through a single function.
1856 void qemu_mutex_lock_iothread_impl(const char *file
, int line
)
1858 QemuMutexLockFunc bql_lock
= atomic_read(&qemu_bql_mutex_lock_func
);
1860 g_assert(!qemu_mutex_iothread_locked());
1861 bql_lock(&qemu_global_mutex
, file
, line
);
1862 iothread_locked
= true;
1865 void qemu_mutex_unlock_iothread(void)
1867 g_assert(qemu_mutex_iothread_locked());
1868 iothread_locked
= false;
1869 qemu_mutex_unlock(&qemu_global_mutex
);
1872 static bool all_vcpus_paused(void)
1877 if (!cpu
->stopped
) {
1885 void pause_all_vcpus(void)
1889 qemu_clock_enable(QEMU_CLOCK_VIRTUAL
, false);
1891 if (qemu_cpu_is_self(cpu
)) {
1892 qemu_cpu_stop(cpu
, true);
1899 /* We need to drop the replay_lock so any vCPU threads woken up
1900 * can finish their replay tasks
1902 replay_mutex_unlock();
1904 while (!all_vcpus_paused()) {
1905 qemu_cond_wait(&qemu_pause_cond
, &qemu_global_mutex
);
1911 qemu_mutex_unlock_iothread();
1912 replay_mutex_lock();
1913 qemu_mutex_lock_iothread();
1916 void cpu_resume(CPUState
*cpu
)
1919 cpu
->stopped
= false;
1923 void resume_all_vcpus(void)
1927 qemu_clock_enable(QEMU_CLOCK_VIRTUAL
, true);
1933 void cpu_remove_sync(CPUState
*cpu
)
1938 qemu_mutex_unlock_iothread();
1939 qemu_thread_join(cpu
->thread
);
1940 qemu_mutex_lock_iothread();
1943 /* For temporary buffers for forming a name */
1944 #define VCPU_THREAD_NAME_SIZE 16
1946 static void qemu_tcg_init_vcpu(CPUState
*cpu
)
1948 char thread_name
[VCPU_THREAD_NAME_SIZE
];
1949 static QemuCond
*single_tcg_halt_cond
;
1950 static QemuThread
*single_tcg_cpu_thread
;
1951 static int tcg_region_inited
;
1953 assert(tcg_enabled());
1955 * Initialize TCG regions--once. Now is a good time, because:
1956 * (1) TCG's init context, prologue and target globals have been set up.
1957 * (2) qemu_tcg_mttcg_enabled() works now (TCG init code runs before the
1958 * -accel flag is processed, so the check doesn't work then).
1960 if (!tcg_region_inited
) {
1961 tcg_region_inited
= 1;
1965 if (qemu_tcg_mttcg_enabled() || !single_tcg_cpu_thread
) {
1966 cpu
->thread
= g_malloc0(sizeof(QemuThread
));
1967 cpu
->halt_cond
= g_malloc0(sizeof(QemuCond
));
1968 qemu_cond_init(cpu
->halt_cond
);
1970 if (qemu_tcg_mttcg_enabled()) {
1971 /* create a thread per vCPU with TCG (MTTCG) */
1972 parallel_cpus
= true;
1973 snprintf(thread_name
, VCPU_THREAD_NAME_SIZE
, "CPU %d/TCG",
1976 qemu_thread_create(cpu
->thread
, thread_name
, qemu_tcg_cpu_thread_fn
,
1977 cpu
, QEMU_THREAD_JOINABLE
);
1980 /* share a single thread for all cpus with TCG */
1981 snprintf(thread_name
, VCPU_THREAD_NAME_SIZE
, "ALL CPUs/TCG");
1982 qemu_thread_create(cpu
->thread
, thread_name
,
1983 qemu_tcg_rr_cpu_thread_fn
,
1984 cpu
, QEMU_THREAD_JOINABLE
);
1986 single_tcg_halt_cond
= cpu
->halt_cond
;
1987 single_tcg_cpu_thread
= cpu
->thread
;
1990 cpu
->hThread
= qemu_thread_get_handle(cpu
->thread
);
1993 /* For non-MTTCG cases we share the thread */
1994 cpu
->thread
= single_tcg_cpu_thread
;
1995 cpu
->halt_cond
= single_tcg_halt_cond
;
1996 cpu
->thread_id
= first_cpu
->thread_id
;
1998 cpu
->created
= true;
2002 static void qemu_hax_start_vcpu(CPUState
*cpu
)
2004 char thread_name
[VCPU_THREAD_NAME_SIZE
];
2006 cpu
->thread
= g_malloc0(sizeof(QemuThread
));
2007 cpu
->halt_cond
= g_malloc0(sizeof(QemuCond
));
2008 qemu_cond_init(cpu
->halt_cond
);
2010 snprintf(thread_name
, VCPU_THREAD_NAME_SIZE
, "CPU %d/HAX",
2012 qemu_thread_create(cpu
->thread
, thread_name
, qemu_hax_cpu_thread_fn
,
2013 cpu
, QEMU_THREAD_JOINABLE
);
2015 cpu
->hThread
= qemu_thread_get_handle(cpu
->thread
);
2019 static void qemu_kvm_start_vcpu(CPUState
*cpu
)
2021 char thread_name
[VCPU_THREAD_NAME_SIZE
];
2023 cpu
->thread
= g_malloc0(sizeof(QemuThread
));
2024 cpu
->halt_cond
= g_malloc0(sizeof(QemuCond
));
2025 qemu_cond_init(cpu
->halt_cond
);
2026 snprintf(thread_name
, VCPU_THREAD_NAME_SIZE
, "CPU %d/KVM",
2028 qemu_thread_create(cpu
->thread
, thread_name
, qemu_kvm_cpu_thread_fn
,
2029 cpu
, QEMU_THREAD_JOINABLE
);
2032 static void qemu_hvf_start_vcpu(CPUState
*cpu
)
2034 char thread_name
[VCPU_THREAD_NAME_SIZE
];
2036 /* HVF currently does not support TCG, and only runs in
2037 * unrestricted-guest mode. */
2038 assert(hvf_enabled());
2040 cpu
->thread
= g_malloc0(sizeof(QemuThread
));
2041 cpu
->halt_cond
= g_malloc0(sizeof(QemuCond
));
2042 qemu_cond_init(cpu
->halt_cond
);
2044 snprintf(thread_name
, VCPU_THREAD_NAME_SIZE
, "CPU %d/HVF",
2046 qemu_thread_create(cpu
->thread
, thread_name
, qemu_hvf_cpu_thread_fn
,
2047 cpu
, QEMU_THREAD_JOINABLE
);
2050 static void qemu_whpx_start_vcpu(CPUState
*cpu
)
2052 char thread_name
[VCPU_THREAD_NAME_SIZE
];
2054 cpu
->thread
= g_malloc0(sizeof(QemuThread
));
2055 cpu
->halt_cond
= g_malloc0(sizeof(QemuCond
));
2056 qemu_cond_init(cpu
->halt_cond
);
2057 snprintf(thread_name
, VCPU_THREAD_NAME_SIZE
, "CPU %d/WHPX",
2059 qemu_thread_create(cpu
->thread
, thread_name
, qemu_whpx_cpu_thread_fn
,
2060 cpu
, QEMU_THREAD_JOINABLE
);
2062 cpu
->hThread
= qemu_thread_get_handle(cpu
->thread
);
2066 static void qemu_dummy_start_vcpu(CPUState
*cpu
)
2068 char thread_name
[VCPU_THREAD_NAME_SIZE
];
2070 cpu
->thread
= g_malloc0(sizeof(QemuThread
));
2071 cpu
->halt_cond
= g_malloc0(sizeof(QemuCond
));
2072 qemu_cond_init(cpu
->halt_cond
);
2073 snprintf(thread_name
, VCPU_THREAD_NAME_SIZE
, "CPU %d/DUMMY",
2075 qemu_thread_create(cpu
->thread
, thread_name
, qemu_dummy_cpu_thread_fn
, cpu
,
2076 QEMU_THREAD_JOINABLE
);
2079 void qemu_init_vcpu(CPUState
*cpu
)
2081 cpu
->nr_cores
= smp_cores
;
2082 cpu
->nr_threads
= smp_threads
;
2083 cpu
->stopped
= true;
2084 cpu
->random_seed
= qemu_guest_random_seed_thread_part1();
2087 /* If the target cpu hasn't set up any address spaces itself,
2088 * give it the default one.
2091 cpu_address_space_init(cpu
, 0, "cpu-memory", cpu
->memory
);
2094 if (kvm_enabled()) {
2095 qemu_kvm_start_vcpu(cpu
);
2096 } else if (hax_enabled()) {
2097 qemu_hax_start_vcpu(cpu
);
2098 } else if (hvf_enabled()) {
2099 qemu_hvf_start_vcpu(cpu
);
2100 } else if (tcg_enabled()) {
2101 qemu_tcg_init_vcpu(cpu
);
2102 } else if (whpx_enabled()) {
2103 qemu_whpx_start_vcpu(cpu
);
2105 qemu_dummy_start_vcpu(cpu
);
2108 while (!cpu
->created
) {
2109 qemu_cond_wait(&qemu_cpu_cond
, &qemu_global_mutex
);
2113 void cpu_stop_current(void)
2116 current_cpu
->stop
= true;
2117 cpu_exit(current_cpu
);
2121 int vm_stop(RunState state
)
2123 if (qemu_in_vcpu_thread()) {
2124 qemu_system_vmstop_request_prepare();
2125 qemu_system_vmstop_request(state
);
2127 * FIXME: should not return to device code in case
2128 * vm_stop() has been requested.
2134 return do_vm_stop(state
, true);
2138 * Prepare for (re)starting the VM.
2139 * Returns -1 if the vCPUs are not to be restarted (e.g. if they are already
2140 * running or in case of an error condition), 0 otherwise.
2142 int vm_prepare_start(void)
2146 qemu_vmstop_requested(&requested
);
2147 if (runstate_is_running() && requested
== RUN_STATE__MAX
) {
2151 /* Ensure that a STOP/RESUME pair of events is emitted if a
2152 * vmstop request was pending. The BLOCK_IO_ERROR event, for
2153 * example, according to documentation is always followed by
2156 if (runstate_is_running()) {
2157 qapi_event_send_stop();
2158 qapi_event_send_resume();
2162 /* We are sending this now, but the CPUs will be resumed shortly later */
2163 qapi_event_send_resume();
2165 replay_enable_events();
2167 runstate_set(RUN_STATE_RUNNING
);
2168 vm_state_notify(1, RUN_STATE_RUNNING
);
2174 if (!vm_prepare_start()) {
2179 /* does a state transition even if the VM is already stopped,
2180 current state is forgotten forever */
2181 int vm_stop_force_state(RunState state
)
2183 if (runstate_is_running()) {
2184 return vm_stop(state
);
2186 runstate_set(state
);
2189 /* Make sure to return an error if the flush in a previous vm_stop()
2191 return bdrv_flush_all();
2195 void list_cpus(const char *optarg
)
2197 /* XXX: implement xxx_cpu_list for targets that still miss it */
2198 #if defined(cpu_list)
2203 CpuInfoList
*qmp_query_cpus(Error
**errp
)
2205 MachineState
*ms
= MACHINE(qdev_get_machine());
2206 MachineClass
*mc
= MACHINE_GET_CLASS(ms
);
2207 CpuInfoList
*head
= NULL
, *cur_item
= NULL
;
2212 #if defined(TARGET_I386)
2213 X86CPU
*x86_cpu
= X86_CPU(cpu
);
2214 CPUX86State
*env
= &x86_cpu
->env
;
2215 #elif defined(TARGET_PPC)
2216 PowerPCCPU
*ppc_cpu
= POWERPC_CPU(cpu
);
2217 CPUPPCState
*env
= &ppc_cpu
->env
;
2218 #elif defined(TARGET_SPARC)
2219 SPARCCPU
*sparc_cpu
= SPARC_CPU(cpu
);
2220 CPUSPARCState
*env
= &sparc_cpu
->env
;
2221 #elif defined(TARGET_RISCV)
2222 RISCVCPU
*riscv_cpu
= RISCV_CPU(cpu
);
2223 CPURISCVState
*env
= &riscv_cpu
->env
;
2224 #elif defined(TARGET_MIPS)
2225 MIPSCPU
*mips_cpu
= MIPS_CPU(cpu
);
2226 CPUMIPSState
*env
= &mips_cpu
->env
;
2227 #elif defined(TARGET_TRICORE)
2228 TriCoreCPU
*tricore_cpu
= TRICORE_CPU(cpu
);
2229 CPUTriCoreState
*env
= &tricore_cpu
->env
;
2230 #elif defined(TARGET_S390X)
2231 S390CPU
*s390_cpu
= S390_CPU(cpu
);
2232 CPUS390XState
*env
= &s390_cpu
->env
;
2235 cpu_synchronize_state(cpu
);
2237 info
= g_malloc0(sizeof(*info
));
2238 info
->value
= g_malloc0(sizeof(*info
->value
));
2239 info
->value
->CPU
= cpu
->cpu_index
;
2240 info
->value
->current
= (cpu
== first_cpu
);
2241 info
->value
->halted
= cpu
->halted
;
2242 info
->value
->qom_path
= object_get_canonical_path(OBJECT(cpu
));
2243 info
->value
->thread_id
= cpu
->thread_id
;
2244 #if defined(TARGET_I386)
2245 info
->value
->arch
= CPU_INFO_ARCH_X86
;
2246 info
->value
->u
.x86
.pc
= env
->eip
+ env
->segs
[R_CS
].base
;
2247 #elif defined(TARGET_PPC)
2248 info
->value
->arch
= CPU_INFO_ARCH_PPC
;
2249 info
->value
->u
.ppc
.nip
= env
->nip
;
2250 #elif defined(TARGET_SPARC)
2251 info
->value
->arch
= CPU_INFO_ARCH_SPARC
;
2252 info
->value
->u
.q_sparc
.pc
= env
->pc
;
2253 info
->value
->u
.q_sparc
.npc
= env
->npc
;
2254 #elif defined(TARGET_MIPS)
2255 info
->value
->arch
= CPU_INFO_ARCH_MIPS
;
2256 info
->value
->u
.q_mips
.PC
= env
->active_tc
.PC
;
2257 #elif defined(TARGET_TRICORE)
2258 info
->value
->arch
= CPU_INFO_ARCH_TRICORE
;
2259 info
->value
->u
.tricore
.PC
= env
->PC
;
2260 #elif defined(TARGET_S390X)
2261 info
->value
->arch
= CPU_INFO_ARCH_S390
;
2262 info
->value
->u
.s390
.cpu_state
= env
->cpu_state
;
2263 #elif defined(TARGET_RISCV)
2264 info
->value
->arch
= CPU_INFO_ARCH_RISCV
;
2265 info
->value
->u
.riscv
.pc
= env
->pc
;
2267 info
->value
->arch
= CPU_INFO_ARCH_OTHER
;
2269 info
->value
->has_props
= !!mc
->cpu_index_to_instance_props
;
2270 if (info
->value
->has_props
) {
2271 CpuInstanceProperties
*props
;
2272 props
= g_malloc0(sizeof(*props
));
2273 *props
= mc
->cpu_index_to_instance_props(ms
, cpu
->cpu_index
);
2274 info
->value
->props
= props
;
2277 /* XXX: waiting for the qapi to support GSList */
2279 head
= cur_item
= info
;
2281 cur_item
->next
= info
;
2289 static CpuInfoArch
sysemu_target_to_cpuinfo_arch(SysEmuTarget target
)
2292 * The @SysEmuTarget -> @CpuInfoArch mapping below is based on the
2293 * TARGET_ARCH -> TARGET_BASE_ARCH mapping in the "configure" script.
2296 case SYS_EMU_TARGET_I386
:
2297 case SYS_EMU_TARGET_X86_64
:
2298 return CPU_INFO_ARCH_X86
;
2300 case SYS_EMU_TARGET_PPC
:
2301 case SYS_EMU_TARGET_PPC64
:
2302 return CPU_INFO_ARCH_PPC
;
2304 case SYS_EMU_TARGET_SPARC
:
2305 case SYS_EMU_TARGET_SPARC64
:
2306 return CPU_INFO_ARCH_SPARC
;
2308 case SYS_EMU_TARGET_MIPS
:
2309 case SYS_EMU_TARGET_MIPSEL
:
2310 case SYS_EMU_TARGET_MIPS64
:
2311 case SYS_EMU_TARGET_MIPS64EL
:
2312 return CPU_INFO_ARCH_MIPS
;
2314 case SYS_EMU_TARGET_TRICORE
:
2315 return CPU_INFO_ARCH_TRICORE
;
2317 case SYS_EMU_TARGET_S390X
:
2318 return CPU_INFO_ARCH_S390
;
2320 case SYS_EMU_TARGET_RISCV32
:
2321 case SYS_EMU_TARGET_RISCV64
:
2322 return CPU_INFO_ARCH_RISCV
;
2325 return CPU_INFO_ARCH_OTHER
;
2329 static void cpustate_to_cpuinfo_s390(CpuInfoS390
*info
, const CPUState
*cpu
)
2332 S390CPU
*s390_cpu
= S390_CPU(cpu
);
2333 CPUS390XState
*env
= &s390_cpu
->env
;
2335 info
->cpu_state
= env
->cpu_state
;
2342 * fast means: we NEVER interrupt vCPU threads to retrieve
2343 * information from KVM.
2345 CpuInfoFastList
*qmp_query_cpus_fast(Error
**errp
)
2347 MachineState
*ms
= MACHINE(qdev_get_machine());
2348 MachineClass
*mc
= MACHINE_GET_CLASS(ms
);
2349 CpuInfoFastList
*head
= NULL
, *cur_item
= NULL
;
2350 SysEmuTarget target
= qapi_enum_parse(&SysEmuTarget_lookup
, TARGET_NAME
,
2355 CpuInfoFastList
*info
= g_malloc0(sizeof(*info
));
2356 info
->value
= g_malloc0(sizeof(*info
->value
));
2358 info
->value
->cpu_index
= cpu
->cpu_index
;
2359 info
->value
->qom_path
= object_get_canonical_path(OBJECT(cpu
));
2360 info
->value
->thread_id
= cpu
->thread_id
;
2362 info
->value
->has_props
= !!mc
->cpu_index_to_instance_props
;
2363 if (info
->value
->has_props
) {
2364 CpuInstanceProperties
*props
;
2365 props
= g_malloc0(sizeof(*props
));
2366 *props
= mc
->cpu_index_to_instance_props(ms
, cpu
->cpu_index
);
2367 info
->value
->props
= props
;
2370 info
->value
->arch
= sysemu_target_to_cpuinfo_arch(target
);
2371 info
->value
->target
= target
;
2372 if (target
== SYS_EMU_TARGET_S390X
) {
2373 cpustate_to_cpuinfo_s390(&info
->value
->u
.s390x
, cpu
);
2377 head
= cur_item
= info
;
2379 cur_item
->next
= info
;
2387 void qmp_memsave(int64_t addr
, int64_t size
, const char *filename
,
2388 bool has_cpu
, int64_t cpu_index
, Error
**errp
)
2394 int64_t orig_addr
= addr
, orig_size
= size
;
2400 cpu
= qemu_get_cpu(cpu_index
);
2402 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cpu-index",
2407 f
= fopen(filename
, "wb");
2409 error_setg_file_open(errp
, errno
, filename
);
2417 if (cpu_memory_rw_debug(cpu
, addr
, buf
, l
, 0) != 0) {
2418 error_setg(errp
, "Invalid addr 0x%016" PRIx64
"/size %" PRId64
2419 " specified", orig_addr
, orig_size
);
2422 if (fwrite(buf
, 1, l
, f
) != l
) {
2423 error_setg(errp
, QERR_IO_ERROR
);
2434 void qmp_pmemsave(int64_t addr
, int64_t size
, const char *filename
,
2441 f
= fopen(filename
, "wb");
2443 error_setg_file_open(errp
, errno
, filename
);
2451 cpu_physical_memory_read(addr
, buf
, l
);
2452 if (fwrite(buf
, 1, l
, f
) != l
) {
2453 error_setg(errp
, QERR_IO_ERROR
);
2464 void qmp_inject_nmi(Error
**errp
)
2466 nmi_monitor_handle(monitor_get_cpu_index(), errp
);
2469 void dump_drift_info(void)
2475 qemu_printf("Host - Guest clock %"PRIi64
" ms\n",
2476 (cpu_get_clock() - cpu_get_icount())/SCALE_MS
);
2477 if (icount_align_option
) {
2478 qemu_printf("Max guest delay %"PRIi64
" ms\n",
2479 -max_delay
/ SCALE_MS
);
2480 qemu_printf("Max guest advance %"PRIi64
" ms\n",
2481 max_advance
/ SCALE_MS
);
2483 qemu_printf("Max guest delay NA\n");
2484 qemu_printf("Max guest advance NA\n");