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/block-backend.h"
37 #include "exec/gdbstub.h"
38 #include "sysemu/dma.h"
39 #include "sysemu/hw_accel.h"
40 #include "sysemu/kvm.h"
41 #include "sysemu/hax.h"
42 #include "sysemu/hvf.h"
43 #include "sysemu/whpx.h"
44 #include "exec/exec-all.h"
46 #include "qemu/thread.h"
47 #include "sysemu/cpus.h"
48 #include "sysemu/qtest.h"
49 #include "qemu/main-loop.h"
50 #include "qemu/option.h"
51 #include "qemu/bitmap.h"
52 #include "qemu/seqlock.h"
55 #include "sysemu/replay.h"
56 #include "hw/boards.h"
60 #include <sys/prctl.h>
63 #define PR_MCE_KILL 33
66 #ifndef PR_MCE_KILL_SET
67 #define PR_MCE_KILL_SET 1
70 #ifndef PR_MCE_KILL_EARLY
71 #define PR_MCE_KILL_EARLY 1
74 #endif /* CONFIG_LINUX */
79 /* vcpu throttling controls */
80 static QEMUTimer
*throttle_timer
;
81 static unsigned int throttle_percentage
;
83 #define CPU_THROTTLE_PCT_MIN 1
84 #define CPU_THROTTLE_PCT_MAX 99
85 #define CPU_THROTTLE_TIMESLICE_NS 10000000
87 bool cpu_is_stopped(CPUState
*cpu
)
89 return cpu
->stopped
|| !runstate_is_running();
92 static bool cpu_thread_is_idle(CPUState
*cpu
)
94 if (cpu
->stop
|| cpu
->queued_work_first
) {
97 if (cpu_is_stopped(cpu
)) {
100 if (!cpu
->halted
|| cpu_has_work(cpu
) ||
101 kvm_halt_in_kernel()) {
107 static bool all_cpu_threads_idle(void)
112 if (!cpu_thread_is_idle(cpu
)) {
119 /***********************************************************/
120 /* guest cycle counter */
122 /* Protected by TimersState seqlock */
124 static bool icount_sleep
= true;
125 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
126 #define MAX_ICOUNT_SHIFT 10
128 typedef struct TimersState
{
129 /* Protected by BQL. */
130 int64_t cpu_ticks_prev
;
131 int64_t cpu_ticks_offset
;
133 /* Protect fields that can be respectively read outside the
134 * BQL, and written from multiple threads.
136 QemuSeqLock vm_clock_seqlock
;
137 QemuSpin vm_clock_lock
;
139 int16_t cpu_ticks_enabled
;
141 /* Conversion factor from emulated instructions to virtual clock ticks. */
142 int16_t icount_time_shift
;
144 /* Compensate for varying guest execution speed. */
145 int64_t qemu_icount_bias
;
147 int64_t vm_clock_warp_start
;
148 int64_t cpu_clock_offset
;
150 /* Only written by TCG thread */
153 /* for adjusting icount */
154 QEMUTimer
*icount_rt_timer
;
155 QEMUTimer
*icount_vm_timer
;
156 QEMUTimer
*icount_warp_timer
;
159 static TimersState timers_state
;
163 * We default to false if we know other options have been enabled
164 * which are currently incompatible with MTTCG. Otherwise when each
165 * guest (target) has been updated to support:
166 * - atomic instructions
167 * - memory ordering primitives (barriers)
168 * they can set the appropriate CONFIG flags in ${target}-softmmu.mak
170 * Once a guest architecture has been converted to the new primitives
171 * there are two remaining limitations to check.
173 * - The guest can't be oversized (e.g. 64 bit guest on 32 bit host)
174 * - The host must have a stronger memory order than the guest
176 * It may be possible in future to support strong guests on weak hosts
177 * but that will require tagging all load/stores in a guest with their
178 * implicit memory order requirements which would likely slow things
182 static bool check_tcg_memory_orders_compatible(void)
184 #if defined(TCG_GUEST_DEFAULT_MO) && defined(TCG_TARGET_DEFAULT_MO)
185 return (TCG_GUEST_DEFAULT_MO
& ~TCG_TARGET_DEFAULT_MO
) == 0;
191 static bool default_mttcg_enabled(void)
193 if (use_icount
|| TCG_OVERSIZED_GUEST
) {
196 #ifdef TARGET_SUPPORTS_MTTCG
197 return check_tcg_memory_orders_compatible();
204 void qemu_tcg_configure(QemuOpts
*opts
, Error
**errp
)
206 const char *t
= qemu_opt_get(opts
, "thread");
208 if (strcmp(t
, "multi") == 0) {
209 if (TCG_OVERSIZED_GUEST
) {
210 error_setg(errp
, "No MTTCG when guest word size > hosts");
211 } else if (use_icount
) {
212 error_setg(errp
, "No MTTCG when icount is enabled");
214 #ifndef TARGET_SUPPORTS_MTTCG
215 warn_report("Guest not yet converted to MTTCG - "
216 "you may get unexpected results");
218 if (!check_tcg_memory_orders_compatible()) {
219 warn_report("Guest expects a stronger memory ordering "
220 "than the host provides");
221 error_printf("This may cause strange/hard to debug errors\n");
223 mttcg_enabled
= true;
225 } else if (strcmp(t
, "single") == 0) {
226 mttcg_enabled
= false;
228 error_setg(errp
, "Invalid 'thread' setting %s", t
);
231 mttcg_enabled
= default_mttcg_enabled();
235 /* The current number of executed instructions is based on what we
236 * originally budgeted minus the current state of the decrementing
237 * icount counters in extra/u16.low.
239 static int64_t cpu_get_icount_executed(CPUState
*cpu
)
241 return cpu
->icount_budget
- (cpu
->icount_decr
.u16
.low
+ cpu
->icount_extra
);
245 * Update the global shared timer_state.qemu_icount to take into
246 * account executed instructions. This is done by the TCG vCPU
247 * thread so the main-loop can see time has moved forward.
249 static void cpu_update_icount_locked(CPUState
*cpu
)
251 int64_t executed
= cpu_get_icount_executed(cpu
);
252 cpu
->icount_budget
-= executed
;
254 atomic_set_i64(&timers_state
.qemu_icount
,
255 timers_state
.qemu_icount
+ executed
);
259 * Update the global shared timer_state.qemu_icount to take into
260 * account executed instructions. This is done by the TCG vCPU
261 * thread so the main-loop can see time has moved forward.
263 void cpu_update_icount(CPUState
*cpu
)
265 seqlock_write_lock(&timers_state
.vm_clock_seqlock
,
266 &timers_state
.vm_clock_lock
);
267 cpu_update_icount_locked(cpu
);
268 seqlock_write_unlock(&timers_state
.vm_clock_seqlock
,
269 &timers_state
.vm_clock_lock
);
272 static int64_t cpu_get_icount_raw_locked(void)
274 CPUState
*cpu
= current_cpu
;
276 if (cpu
&& cpu
->running
) {
277 if (!cpu
->can_do_io
) {
278 error_report("Bad icount read");
281 /* Take into account what has run */
282 cpu_update_icount_locked(cpu
);
284 /* The read is protected by the seqlock, but needs atomic64 to avoid UB */
285 return atomic_read_i64(&timers_state
.qemu_icount
);
288 static int64_t cpu_get_icount_locked(void)
290 int64_t icount
= cpu_get_icount_raw_locked();
291 return atomic_read_i64(&timers_state
.qemu_icount_bias
) +
292 cpu_icount_to_ns(icount
);
295 int64_t cpu_get_icount_raw(void)
301 start
= seqlock_read_begin(&timers_state
.vm_clock_seqlock
);
302 icount
= cpu_get_icount_raw_locked();
303 } while (seqlock_read_retry(&timers_state
.vm_clock_seqlock
, start
));
308 /* Return the virtual CPU time, based on the instruction counter. */
309 int64_t cpu_get_icount(void)
315 start
= seqlock_read_begin(&timers_state
.vm_clock_seqlock
);
316 icount
= cpu_get_icount_locked();
317 } while (seqlock_read_retry(&timers_state
.vm_clock_seqlock
, start
));
322 int64_t cpu_icount_to_ns(int64_t icount
)
324 return icount
<< atomic_read(&timers_state
.icount_time_shift
);
327 static int64_t cpu_get_ticks_locked(void)
329 int64_t ticks
= timers_state
.cpu_ticks_offset
;
330 if (timers_state
.cpu_ticks_enabled
) {
331 ticks
+= cpu_get_host_ticks();
334 if (timers_state
.cpu_ticks_prev
> ticks
) {
335 /* Non increasing ticks may happen if the host uses software suspend. */
336 timers_state
.cpu_ticks_offset
+= timers_state
.cpu_ticks_prev
- ticks
;
337 ticks
= timers_state
.cpu_ticks_prev
;
340 timers_state
.cpu_ticks_prev
= ticks
;
344 /* return the time elapsed in VM between vm_start and vm_stop. Unless
345 * icount is active, cpu_get_ticks() uses units of the host CPU cycle
348 int64_t cpu_get_ticks(void)
353 return cpu_get_icount();
356 qemu_spin_lock(&timers_state
.vm_clock_lock
);
357 ticks
= cpu_get_ticks_locked();
358 qemu_spin_unlock(&timers_state
.vm_clock_lock
);
362 static int64_t cpu_get_clock_locked(void)
366 time
= timers_state
.cpu_clock_offset
;
367 if (timers_state
.cpu_ticks_enabled
) {
374 /* Return the monotonic time elapsed in VM, i.e.,
375 * the time between vm_start and vm_stop
377 int64_t cpu_get_clock(void)
383 start
= seqlock_read_begin(&timers_state
.vm_clock_seqlock
);
384 ti
= cpu_get_clock_locked();
385 } while (seqlock_read_retry(&timers_state
.vm_clock_seqlock
, start
));
390 /* enable cpu_get_ticks()
391 * Caller must hold BQL which serves as mutex for vm_clock_seqlock.
393 void cpu_enable_ticks(void)
395 seqlock_write_lock(&timers_state
.vm_clock_seqlock
,
396 &timers_state
.vm_clock_lock
);
397 if (!timers_state
.cpu_ticks_enabled
) {
398 timers_state
.cpu_ticks_offset
-= cpu_get_host_ticks();
399 timers_state
.cpu_clock_offset
-= get_clock();
400 timers_state
.cpu_ticks_enabled
= 1;
402 seqlock_write_unlock(&timers_state
.vm_clock_seqlock
,
403 &timers_state
.vm_clock_lock
);
406 /* disable cpu_get_ticks() : the clock is stopped. You must not call
407 * cpu_get_ticks() after that.
408 * Caller must hold BQL which serves as mutex for vm_clock_seqlock.
410 void cpu_disable_ticks(void)
412 seqlock_write_lock(&timers_state
.vm_clock_seqlock
,
413 &timers_state
.vm_clock_lock
);
414 if (timers_state
.cpu_ticks_enabled
) {
415 timers_state
.cpu_ticks_offset
+= cpu_get_host_ticks();
416 timers_state
.cpu_clock_offset
= cpu_get_clock_locked();
417 timers_state
.cpu_ticks_enabled
= 0;
419 seqlock_write_unlock(&timers_state
.vm_clock_seqlock
,
420 &timers_state
.vm_clock_lock
);
423 /* Correlation between real and virtual time is always going to be
424 fairly approximate, so ignore small variation.
425 When the guest is idle real and virtual time will be aligned in
427 #define ICOUNT_WOBBLE (NANOSECONDS_PER_SECOND / 10)
429 static void icount_adjust(void)
435 /* Protected by TimersState mutex. */
436 static int64_t last_delta
;
438 /* If the VM is not running, then do nothing. */
439 if (!runstate_is_running()) {
443 seqlock_write_lock(&timers_state
.vm_clock_seqlock
,
444 &timers_state
.vm_clock_lock
);
445 cur_time
= cpu_get_clock_locked();
446 cur_icount
= cpu_get_icount_locked();
448 delta
= cur_icount
- cur_time
;
449 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
451 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
452 && timers_state
.icount_time_shift
> 0) {
453 /* The guest is getting too far ahead. Slow time down. */
454 atomic_set(&timers_state
.icount_time_shift
,
455 timers_state
.icount_time_shift
- 1);
458 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
459 && timers_state
.icount_time_shift
< MAX_ICOUNT_SHIFT
) {
460 /* The guest is getting too far behind. Speed time up. */
461 atomic_set(&timers_state
.icount_time_shift
,
462 timers_state
.icount_time_shift
+ 1);
465 atomic_set_i64(&timers_state
.qemu_icount_bias
,
466 cur_icount
- (timers_state
.qemu_icount
467 << timers_state
.icount_time_shift
));
468 seqlock_write_unlock(&timers_state
.vm_clock_seqlock
,
469 &timers_state
.vm_clock_lock
);
472 static void icount_adjust_rt(void *opaque
)
474 timer_mod(timers_state
.icount_rt_timer
,
475 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT
) + 1000);
479 static void icount_adjust_vm(void *opaque
)
481 timer_mod(timers_state
.icount_vm_timer
,
482 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
483 NANOSECONDS_PER_SECOND
/ 10);
487 static int64_t qemu_icount_round(int64_t count
)
489 int shift
= atomic_read(&timers_state
.icount_time_shift
);
490 return (count
+ (1 << shift
) - 1) >> shift
;
493 static void icount_warp_rt(void)
498 /* The icount_warp_timer is rescheduled soon after vm_clock_warp_start
499 * changes from -1 to another value, so the race here is okay.
502 seq
= seqlock_read_begin(&timers_state
.vm_clock_seqlock
);
503 warp_start
= timers_state
.vm_clock_warp_start
;
504 } while (seqlock_read_retry(&timers_state
.vm_clock_seqlock
, seq
));
506 if (warp_start
== -1) {
510 seqlock_write_lock(&timers_state
.vm_clock_seqlock
,
511 &timers_state
.vm_clock_lock
);
512 if (runstate_is_running()) {
513 int64_t clock
= REPLAY_CLOCK_LOCKED(REPLAY_CLOCK_VIRTUAL_RT
,
514 cpu_get_clock_locked());
517 warp_delta
= clock
- timers_state
.vm_clock_warp_start
;
518 if (use_icount
== 2) {
520 * In adaptive mode, do not let QEMU_CLOCK_VIRTUAL run too
521 * far ahead of real time.
523 int64_t cur_icount
= cpu_get_icount_locked();
524 int64_t delta
= clock
- cur_icount
;
525 warp_delta
= MIN(warp_delta
, delta
);
527 atomic_set_i64(&timers_state
.qemu_icount_bias
,
528 timers_state
.qemu_icount_bias
+ warp_delta
);
530 timers_state
.vm_clock_warp_start
= -1;
531 seqlock_write_unlock(&timers_state
.vm_clock_seqlock
,
532 &timers_state
.vm_clock_lock
);
534 if (qemu_clock_expired(QEMU_CLOCK_VIRTUAL
)) {
535 qemu_clock_notify(QEMU_CLOCK_VIRTUAL
);
539 static void icount_timer_cb(void *opaque
)
541 /* No need for a checkpoint because the timer already synchronizes
542 * with CHECKPOINT_CLOCK_VIRTUAL_RT.
547 void qtest_clock_warp(int64_t dest
)
549 int64_t clock
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
550 AioContext
*aio_context
;
551 assert(qtest_enabled());
552 aio_context
= qemu_get_aio_context();
553 while (clock
< dest
) {
554 int64_t deadline
= qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL
);
555 int64_t warp
= qemu_soonest_timeout(dest
- clock
, deadline
);
557 seqlock_write_lock(&timers_state
.vm_clock_seqlock
,
558 &timers_state
.vm_clock_lock
);
559 atomic_set_i64(&timers_state
.qemu_icount_bias
,
560 timers_state
.qemu_icount_bias
+ warp
);
561 seqlock_write_unlock(&timers_state
.vm_clock_seqlock
,
562 &timers_state
.vm_clock_lock
);
564 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL
);
565 timerlist_run_timers(aio_context
->tlg
.tl
[QEMU_CLOCK_VIRTUAL
]);
566 clock
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
568 qemu_clock_notify(QEMU_CLOCK_VIRTUAL
);
571 void qemu_start_warp_timer(void)
580 /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
581 * do not fire, so computing the deadline does not make sense.
583 if (!runstate_is_running()) {
587 if (replay_mode
!= REPLAY_MODE_PLAY
) {
588 if (!all_cpu_threads_idle()) {
592 if (qtest_enabled()) {
593 /* When testing, qtest commands advance icount. */
597 replay_checkpoint(CHECKPOINT_CLOCK_WARP_START
);
599 /* warp clock deterministically in record/replay mode */
600 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_START
)) {
601 /* vCPU is sleeping and warp can't be started.
602 It is probably a race condition: notification sent
603 to vCPU was processed in advance and vCPU went to sleep.
604 Therefore we have to wake it up for doing someting. */
605 if (replay_has_checkpoint()) {
606 qemu_clock_notify(QEMU_CLOCK_VIRTUAL
);
612 /* We want to use the earliest deadline from ALL vm_clocks */
613 clock
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT
);
614 deadline
= qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL
);
616 static bool notified
;
617 if (!icount_sleep
&& !notified
) {
618 warn_report("icount sleep disabled and no active timers");
626 * Ensure QEMU_CLOCK_VIRTUAL proceeds even when the virtual CPU goes to
627 * sleep. Otherwise, the CPU might be waiting for a future timer
628 * interrupt to wake it up, but the interrupt never comes because
629 * the vCPU isn't running any insns and thus doesn't advance the
630 * QEMU_CLOCK_VIRTUAL.
634 * We never let VCPUs sleep in no sleep icount mode.
635 * If there is a pending QEMU_CLOCK_VIRTUAL timer we just advance
636 * to the next QEMU_CLOCK_VIRTUAL event and notify it.
637 * It is useful when we want a deterministic execution time,
638 * isolated from host latencies.
640 seqlock_write_lock(&timers_state
.vm_clock_seqlock
,
641 &timers_state
.vm_clock_lock
);
642 atomic_set_i64(&timers_state
.qemu_icount_bias
,
643 timers_state
.qemu_icount_bias
+ deadline
);
644 seqlock_write_unlock(&timers_state
.vm_clock_seqlock
,
645 &timers_state
.vm_clock_lock
);
646 qemu_clock_notify(QEMU_CLOCK_VIRTUAL
);
649 * We do stop VCPUs and only advance QEMU_CLOCK_VIRTUAL after some
650 * "real" time, (related to the time left until the next event) has
651 * passed. The QEMU_CLOCK_VIRTUAL_RT clock will do this.
652 * This avoids that the warps are visible externally; for example,
653 * you will not be sending network packets continuously instead of
656 seqlock_write_lock(&timers_state
.vm_clock_seqlock
,
657 &timers_state
.vm_clock_lock
);
658 if (timers_state
.vm_clock_warp_start
== -1
659 || timers_state
.vm_clock_warp_start
> clock
) {
660 timers_state
.vm_clock_warp_start
= clock
;
662 seqlock_write_unlock(&timers_state
.vm_clock_seqlock
,
663 &timers_state
.vm_clock_lock
);
664 timer_mod_anticipate(timers_state
.icount_warp_timer
,
667 } else if (deadline
== 0) {
668 qemu_clock_notify(QEMU_CLOCK_VIRTUAL
);
672 static void qemu_account_warp_timer(void)
674 if (!use_icount
|| !icount_sleep
) {
678 /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
679 * do not fire, so computing the deadline does not make sense.
681 if (!runstate_is_running()) {
685 /* warp clock deterministically in record/replay mode */
686 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_ACCOUNT
)) {
690 timer_del(timers_state
.icount_warp_timer
);
694 static bool icount_state_needed(void *opaque
)
699 static bool warp_timer_state_needed(void *opaque
)
701 TimersState
*s
= opaque
;
702 return s
->icount_warp_timer
!= NULL
;
705 static bool adjust_timers_state_needed(void *opaque
)
707 TimersState
*s
= opaque
;
708 return s
->icount_rt_timer
!= NULL
;
712 * Subsection for warp timer migration is optional, because may not be created
714 static const VMStateDescription icount_vmstate_warp_timer
= {
715 .name
= "timer/icount/warp_timer",
717 .minimum_version_id
= 1,
718 .needed
= warp_timer_state_needed
,
719 .fields
= (VMStateField
[]) {
720 VMSTATE_INT64(vm_clock_warp_start
, TimersState
),
721 VMSTATE_TIMER_PTR(icount_warp_timer
, TimersState
),
722 VMSTATE_END_OF_LIST()
726 static const VMStateDescription icount_vmstate_adjust_timers
= {
727 .name
= "timer/icount/timers",
729 .minimum_version_id
= 1,
730 .needed
= adjust_timers_state_needed
,
731 .fields
= (VMStateField
[]) {
732 VMSTATE_TIMER_PTR(icount_rt_timer
, TimersState
),
733 VMSTATE_TIMER_PTR(icount_vm_timer
, TimersState
),
734 VMSTATE_END_OF_LIST()
739 * This is a subsection for icount migration.
741 static const VMStateDescription icount_vmstate_timers
= {
742 .name
= "timer/icount",
744 .minimum_version_id
= 1,
745 .needed
= icount_state_needed
,
746 .fields
= (VMStateField
[]) {
747 VMSTATE_INT64(qemu_icount_bias
, TimersState
),
748 VMSTATE_INT64(qemu_icount
, TimersState
),
749 VMSTATE_END_OF_LIST()
751 .subsections
= (const VMStateDescription
*[]) {
752 &icount_vmstate_warp_timer
,
753 &icount_vmstate_adjust_timers
,
758 static const VMStateDescription vmstate_timers
= {
761 .minimum_version_id
= 1,
762 .fields
= (VMStateField
[]) {
763 VMSTATE_INT64(cpu_ticks_offset
, TimersState
),
765 VMSTATE_INT64_V(cpu_clock_offset
, TimersState
, 2),
766 VMSTATE_END_OF_LIST()
768 .subsections
= (const VMStateDescription
*[]) {
769 &icount_vmstate_timers
,
774 static void cpu_throttle_thread(CPUState
*cpu
, run_on_cpu_data opaque
)
777 double throttle_ratio
;
780 if (!cpu_throttle_get_percentage()) {
784 pct
= (double)cpu_throttle_get_percentage()/100;
785 throttle_ratio
= pct
/ (1 - pct
);
786 sleeptime_ns
= (long)(throttle_ratio
* CPU_THROTTLE_TIMESLICE_NS
);
788 qemu_mutex_unlock_iothread();
789 g_usleep(sleeptime_ns
/ 1000); /* Convert ns to us for usleep call */
790 qemu_mutex_lock_iothread();
791 atomic_set(&cpu
->throttle_thread_scheduled
, 0);
794 static void cpu_throttle_timer_tick(void *opaque
)
799 /* Stop the timer if needed */
800 if (!cpu_throttle_get_percentage()) {
804 if (!atomic_xchg(&cpu
->throttle_thread_scheduled
, 1)) {
805 async_run_on_cpu(cpu
, cpu_throttle_thread
,
810 pct
= (double)cpu_throttle_get_percentage()/100;
811 timer_mod(throttle_timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT
) +
812 CPU_THROTTLE_TIMESLICE_NS
/ (1-pct
));
815 void cpu_throttle_set(int new_throttle_pct
)
817 /* Ensure throttle percentage is within valid range */
818 new_throttle_pct
= MIN(new_throttle_pct
, CPU_THROTTLE_PCT_MAX
);
819 new_throttle_pct
= MAX(new_throttle_pct
, CPU_THROTTLE_PCT_MIN
);
821 atomic_set(&throttle_percentage
, new_throttle_pct
);
823 timer_mod(throttle_timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT
) +
824 CPU_THROTTLE_TIMESLICE_NS
);
827 void cpu_throttle_stop(void)
829 atomic_set(&throttle_percentage
, 0);
832 bool cpu_throttle_active(void)
834 return (cpu_throttle_get_percentage() != 0);
837 int cpu_throttle_get_percentage(void)
839 return atomic_read(&throttle_percentage
);
842 void cpu_ticks_init(void)
844 seqlock_init(&timers_state
.vm_clock_seqlock
);
845 qemu_spin_init(&timers_state
.vm_clock_lock
);
846 vmstate_register(NULL
, 0, &vmstate_timers
, &timers_state
);
847 throttle_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL_RT
,
848 cpu_throttle_timer_tick
, NULL
);
851 void configure_icount(QemuOpts
*opts
, Error
**errp
)
854 char *rem_str
= NULL
;
856 option
= qemu_opt_get(opts
, "shift");
858 if (qemu_opt_get(opts
, "align") != NULL
) {
859 error_setg(errp
, "Please specify shift option when using align");
864 icount_sleep
= qemu_opt_get_bool(opts
, "sleep", true);
866 timers_state
.icount_warp_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL_RT
,
867 icount_timer_cb
, NULL
);
870 icount_align_option
= qemu_opt_get_bool(opts
, "align", false);
872 if (icount_align_option
&& !icount_sleep
) {
873 error_setg(errp
, "align=on and sleep=off are incompatible");
875 if (strcmp(option
, "auto") != 0) {
877 timers_state
.icount_time_shift
= strtol(option
, &rem_str
, 0);
878 if (errno
!= 0 || *rem_str
!= '\0' || !strlen(option
)) {
879 error_setg(errp
, "icount: Invalid shift value");
883 } else if (icount_align_option
) {
884 error_setg(errp
, "shift=auto and align=on are incompatible");
885 } else if (!icount_sleep
) {
886 error_setg(errp
, "shift=auto and sleep=off are incompatible");
891 /* 125MIPS seems a reasonable initial guess at the guest speed.
892 It will be corrected fairly quickly anyway. */
893 timers_state
.icount_time_shift
= 3;
895 /* Have both realtime and virtual time triggers for speed adjustment.
896 The realtime trigger catches emulated time passing too slowly,
897 the virtual time trigger catches emulated time passing too fast.
898 Realtime triggers occur even when idle, so use them less frequently
900 timers_state
.vm_clock_warp_start
= -1;
901 timers_state
.icount_rt_timer
= timer_new_ms(QEMU_CLOCK_VIRTUAL_RT
,
902 icount_adjust_rt
, NULL
);
903 timer_mod(timers_state
.icount_rt_timer
,
904 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT
) + 1000);
905 timers_state
.icount_vm_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
,
906 icount_adjust_vm
, NULL
);
907 timer_mod(timers_state
.icount_vm_timer
,
908 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
909 NANOSECONDS_PER_SECOND
/ 10);
912 /***********************************************************/
913 /* TCG vCPU kick timer
915 * The kick timer is responsible for moving single threaded vCPU
916 * emulation on to the next vCPU. If more than one vCPU is running a
917 * timer event with force a cpu->exit so the next vCPU can get
920 * The timer is removed if all vCPUs are idle and restarted again once
921 * idleness is complete.
924 static QEMUTimer
*tcg_kick_vcpu_timer
;
925 static CPUState
*tcg_current_rr_cpu
;
927 #define TCG_KICK_PERIOD (NANOSECONDS_PER_SECOND / 10)
929 static inline int64_t qemu_tcg_next_kick(void)
931 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + TCG_KICK_PERIOD
;
934 /* Kick the currently round-robin scheduled vCPU */
935 static void qemu_cpu_kick_rr_cpu(void)
939 cpu
= atomic_mb_read(&tcg_current_rr_cpu
);
943 } while (cpu
!= atomic_mb_read(&tcg_current_rr_cpu
));
946 static void do_nothing(CPUState
*cpu
, run_on_cpu_data unused
)
950 void qemu_timer_notify_cb(void *opaque
, QEMUClockType type
)
952 if (!use_icount
|| type
!= QEMU_CLOCK_VIRTUAL
) {
957 if (qemu_in_vcpu_thread()) {
958 /* A CPU is currently running; kick it back out to the
959 * tcg_cpu_exec() loop so it will recalculate its
960 * icount deadline immediately.
962 qemu_cpu_kick(current_cpu
);
963 } else if (first_cpu
) {
964 /* qemu_cpu_kick is not enough to kick a halted CPU out of
965 * qemu_tcg_wait_io_event. async_run_on_cpu, instead,
966 * causes cpu_thread_is_idle to return false. This way,
967 * handle_icount_deadline can run.
968 * If we have no CPUs at all for some reason, we don't
969 * need to do anything.
971 async_run_on_cpu(first_cpu
, do_nothing
, RUN_ON_CPU_NULL
);
975 static void kick_tcg_thread(void *opaque
)
977 timer_mod(tcg_kick_vcpu_timer
, qemu_tcg_next_kick());
978 qemu_cpu_kick_rr_cpu();
981 static void start_tcg_kick_timer(void)
983 assert(!mttcg_enabled
);
984 if (!tcg_kick_vcpu_timer
&& CPU_NEXT(first_cpu
)) {
985 tcg_kick_vcpu_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
,
986 kick_tcg_thread
, NULL
);
988 if (tcg_kick_vcpu_timer
&& !timer_pending(tcg_kick_vcpu_timer
)) {
989 timer_mod(tcg_kick_vcpu_timer
, qemu_tcg_next_kick());
993 static void stop_tcg_kick_timer(void)
995 assert(!mttcg_enabled
);
996 if (tcg_kick_vcpu_timer
&& timer_pending(tcg_kick_vcpu_timer
)) {
997 timer_del(tcg_kick_vcpu_timer
);
1001 /***********************************************************/
1002 void hw_error(const char *fmt
, ...)
1008 fprintf(stderr
, "qemu: hardware error: ");
1009 vfprintf(stderr
, fmt
, ap
);
1010 fprintf(stderr
, "\n");
1012 fprintf(stderr
, "CPU #%d:\n", cpu
->cpu_index
);
1013 cpu_dump_state(cpu
, stderr
, CPU_DUMP_FPU
);
1019 void cpu_synchronize_all_states(void)
1024 cpu_synchronize_state(cpu
);
1025 /* TODO: move to cpu_synchronize_state() */
1026 if (hvf_enabled()) {
1027 hvf_cpu_synchronize_state(cpu
);
1032 void cpu_synchronize_all_post_reset(void)
1037 cpu_synchronize_post_reset(cpu
);
1038 /* TODO: move to cpu_synchronize_post_reset() */
1039 if (hvf_enabled()) {
1040 hvf_cpu_synchronize_post_reset(cpu
);
1045 void cpu_synchronize_all_post_init(void)
1050 cpu_synchronize_post_init(cpu
);
1051 /* TODO: move to cpu_synchronize_post_init() */
1052 if (hvf_enabled()) {
1053 hvf_cpu_synchronize_post_init(cpu
);
1058 void cpu_synchronize_all_pre_loadvm(void)
1063 cpu_synchronize_pre_loadvm(cpu
);
1067 static int do_vm_stop(RunState state
, bool send_stop
)
1071 if (runstate_is_running()) {
1072 cpu_disable_ticks();
1074 runstate_set(state
);
1075 vm_state_notify(0, state
);
1077 qapi_event_send_stop();
1082 replay_disable_events();
1083 ret
= bdrv_flush_all();
1088 /* Special vm_stop() variant for terminating the process. Historically clients
1089 * did not expect a QMP STOP event and so we need to retain compatibility.
1091 int vm_shutdown(void)
1093 return do_vm_stop(RUN_STATE_SHUTDOWN
, false);
1096 static bool cpu_can_run(CPUState
*cpu
)
1101 if (cpu_is_stopped(cpu
)) {
1107 static void cpu_handle_guest_debug(CPUState
*cpu
)
1109 gdb_set_stop_cpu(cpu
);
1110 qemu_system_debug_request();
1111 cpu
->stopped
= true;
1115 static void sigbus_reraise(void)
1118 struct sigaction action
;
1120 memset(&action
, 0, sizeof(action
));
1121 action
.sa_handler
= SIG_DFL
;
1122 if (!sigaction(SIGBUS
, &action
, NULL
)) {
1125 sigaddset(&set
, SIGBUS
);
1126 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
1128 perror("Failed to re-raise SIGBUS!\n");
1132 static void sigbus_handler(int n
, siginfo_t
*siginfo
, void *ctx
)
1134 if (siginfo
->si_code
!= BUS_MCEERR_AO
&& siginfo
->si_code
!= BUS_MCEERR_AR
) {
1139 /* Called asynchronously in VCPU thread. */
1140 if (kvm_on_sigbus_vcpu(current_cpu
, siginfo
->si_code
, siginfo
->si_addr
)) {
1144 /* Called synchronously (via signalfd) in main thread. */
1145 if (kvm_on_sigbus(siginfo
->si_code
, siginfo
->si_addr
)) {
1151 static void qemu_init_sigbus(void)
1153 struct sigaction action
;
1155 memset(&action
, 0, sizeof(action
));
1156 action
.sa_flags
= SA_SIGINFO
;
1157 action
.sa_sigaction
= sigbus_handler
;
1158 sigaction(SIGBUS
, &action
, NULL
);
1160 prctl(PR_MCE_KILL
, PR_MCE_KILL_SET
, PR_MCE_KILL_EARLY
, 0, 0);
1162 #else /* !CONFIG_LINUX */
1163 static void qemu_init_sigbus(void)
1166 #endif /* !CONFIG_LINUX */
1168 static QemuMutex qemu_global_mutex
;
1170 static QemuThread io_thread
;
1173 static QemuCond qemu_cpu_cond
;
1175 static QemuCond qemu_pause_cond
;
1177 void qemu_init_cpu_loop(void)
1180 qemu_cond_init(&qemu_cpu_cond
);
1181 qemu_cond_init(&qemu_pause_cond
);
1182 qemu_mutex_init(&qemu_global_mutex
);
1184 qemu_thread_get_self(&io_thread
);
1187 void run_on_cpu(CPUState
*cpu
, run_on_cpu_func func
, run_on_cpu_data data
)
1189 do_run_on_cpu(cpu
, func
, data
, &qemu_global_mutex
);
1192 static void qemu_kvm_destroy_vcpu(CPUState
*cpu
)
1194 if (kvm_destroy_vcpu(cpu
) < 0) {
1195 error_report("kvm_destroy_vcpu failed");
1200 static void qemu_tcg_destroy_vcpu(CPUState
*cpu
)
1204 static void qemu_cpu_stop(CPUState
*cpu
, bool exit
)
1206 g_assert(qemu_cpu_is_self(cpu
));
1208 cpu
->stopped
= true;
1212 qemu_cond_broadcast(&qemu_pause_cond
);
1215 static void qemu_wait_io_event_common(CPUState
*cpu
)
1217 atomic_mb_set(&cpu
->thread_kicked
, false);
1219 qemu_cpu_stop(cpu
, false);
1221 process_queued_cpu_work(cpu
);
1224 static void qemu_tcg_rr_wait_io_event(void)
1228 while (all_cpu_threads_idle()) {
1229 stop_tcg_kick_timer();
1230 qemu_cond_wait(first_cpu
->halt_cond
, &qemu_global_mutex
);
1233 start_tcg_kick_timer();
1236 qemu_wait_io_event_common(cpu
);
1240 static void qemu_wait_io_event(CPUState
*cpu
)
1242 while (cpu_thread_is_idle(cpu
)) {
1243 qemu_cond_wait(cpu
->halt_cond
, &qemu_global_mutex
);
1247 /* Eat dummy APC queued by qemu_cpu_kick_thread. */
1248 if (!tcg_enabled()) {
1252 qemu_wait_io_event_common(cpu
);
1255 static void *qemu_kvm_cpu_thread_fn(void *arg
)
1257 CPUState
*cpu
= arg
;
1260 rcu_register_thread();
1262 qemu_mutex_lock_iothread();
1263 qemu_thread_get_self(cpu
->thread
);
1264 cpu
->thread_id
= qemu_get_thread_id();
1268 r
= kvm_init_vcpu(cpu
);
1270 error_report("kvm_init_vcpu failed: %s", strerror(-r
));
1274 kvm_init_cpu_signals(cpu
);
1276 /* signal CPU creation */
1277 cpu
->created
= true;
1278 qemu_cond_signal(&qemu_cpu_cond
);
1281 if (cpu_can_run(cpu
)) {
1282 r
= kvm_cpu_exec(cpu
);
1283 if (r
== EXCP_DEBUG
) {
1284 cpu_handle_guest_debug(cpu
);
1287 qemu_wait_io_event(cpu
);
1288 } while (!cpu
->unplug
|| cpu_can_run(cpu
));
1290 qemu_kvm_destroy_vcpu(cpu
);
1291 cpu
->created
= false;
1292 qemu_cond_signal(&qemu_cpu_cond
);
1293 qemu_mutex_unlock_iothread();
1294 rcu_unregister_thread();
1298 static void *qemu_dummy_cpu_thread_fn(void *arg
)
1301 error_report("qtest is not supported under Windows");
1304 CPUState
*cpu
= arg
;
1308 rcu_register_thread();
1310 qemu_mutex_lock_iothread();
1311 qemu_thread_get_self(cpu
->thread
);
1312 cpu
->thread_id
= qemu_get_thread_id();
1316 sigemptyset(&waitset
);
1317 sigaddset(&waitset
, SIG_IPI
);
1319 /* signal CPU creation */
1320 cpu
->created
= true;
1321 qemu_cond_signal(&qemu_cpu_cond
);
1324 qemu_mutex_unlock_iothread();
1327 r
= sigwait(&waitset
, &sig
);
1328 } while (r
== -1 && (errno
== EAGAIN
|| errno
== EINTR
));
1333 qemu_mutex_lock_iothread();
1334 qemu_wait_io_event(cpu
);
1335 } while (!cpu
->unplug
);
1337 qemu_mutex_unlock_iothread();
1338 rcu_unregister_thread();
1343 static int64_t tcg_get_icount_limit(void)
1347 if (replay_mode
!= REPLAY_MODE_PLAY
) {
1348 deadline
= qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL
);
1350 /* Maintain prior (possibly buggy) behaviour where if no deadline
1351 * was set (as there is no QEMU_CLOCK_VIRTUAL timer) or it is more than
1352 * INT32_MAX nanoseconds ahead, we still use INT32_MAX
1355 if ((deadline
< 0) || (deadline
> INT32_MAX
)) {
1356 deadline
= INT32_MAX
;
1359 return qemu_icount_round(deadline
);
1361 return replay_get_instructions();
1365 static void handle_icount_deadline(void)
1367 assert(qemu_in_vcpu_thread());
1370 qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL
);
1372 if (deadline
== 0) {
1373 /* Wake up other AioContexts. */
1374 qemu_clock_notify(QEMU_CLOCK_VIRTUAL
);
1375 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL
);
1380 static void prepare_icount_for_run(CPUState
*cpu
)
1385 /* These should always be cleared by process_icount_data after
1386 * each vCPU execution. However u16.high can be raised
1387 * asynchronously by cpu_exit/cpu_interrupt/tcg_handle_interrupt
1389 g_assert(cpu
->icount_decr
.u16
.low
== 0);
1390 g_assert(cpu
->icount_extra
== 0);
1392 cpu
->icount_budget
= tcg_get_icount_limit();
1393 insns_left
= MIN(0xffff, cpu
->icount_budget
);
1394 cpu
->icount_decr
.u16
.low
= insns_left
;
1395 cpu
->icount_extra
= cpu
->icount_budget
- insns_left
;
1397 replay_mutex_lock();
1401 static void process_icount_data(CPUState
*cpu
)
1404 /* Account for executed instructions */
1405 cpu_update_icount(cpu
);
1407 /* Reset the counters */
1408 cpu
->icount_decr
.u16
.low
= 0;
1409 cpu
->icount_extra
= 0;
1410 cpu
->icount_budget
= 0;
1412 replay_account_executed_instructions();
1414 replay_mutex_unlock();
1419 static int tcg_cpu_exec(CPUState
*cpu
)
1422 #ifdef CONFIG_PROFILER
1426 assert(tcg_enabled());
1427 #ifdef CONFIG_PROFILER
1428 ti
= profile_getclock();
1430 cpu_exec_start(cpu
);
1431 ret
= cpu_exec(cpu
);
1433 #ifdef CONFIG_PROFILER
1434 atomic_set(&tcg_ctx
->prof
.cpu_exec_time
,
1435 tcg_ctx
->prof
.cpu_exec_time
+ profile_getclock() - ti
);
1440 /* Destroy any remaining vCPUs which have been unplugged and have
1443 static void deal_with_unplugged_cpus(void)
1448 if (cpu
->unplug
&& !cpu_can_run(cpu
)) {
1449 qemu_tcg_destroy_vcpu(cpu
);
1450 cpu
->created
= false;
1451 qemu_cond_signal(&qemu_cpu_cond
);
1457 /* Single-threaded TCG
1459 * In the single-threaded case each vCPU is simulated in turn. If
1460 * there is more than a single vCPU we create a simple timer to kick
1461 * the vCPU and ensure we don't get stuck in a tight loop in one vCPU.
1462 * This is done explicitly rather than relying on side-effects
1466 static void *qemu_tcg_rr_cpu_thread_fn(void *arg
)
1468 CPUState
*cpu
= arg
;
1470 assert(tcg_enabled());
1471 rcu_register_thread();
1472 tcg_register_thread();
1474 qemu_mutex_lock_iothread();
1475 qemu_thread_get_self(cpu
->thread
);
1477 cpu
->thread_id
= qemu_get_thread_id();
1478 cpu
->created
= true;
1480 qemu_cond_signal(&qemu_cpu_cond
);
1482 /* wait for initial kick-off after machine start */
1483 while (first_cpu
->stopped
) {
1484 qemu_cond_wait(first_cpu
->halt_cond
, &qemu_global_mutex
);
1486 /* process any pending work */
1489 qemu_wait_io_event_common(cpu
);
1493 start_tcg_kick_timer();
1497 /* process any pending work */
1498 cpu
->exit_request
= 1;
1501 qemu_mutex_unlock_iothread();
1502 replay_mutex_lock();
1503 qemu_mutex_lock_iothread();
1504 /* Account partial waits to QEMU_CLOCK_VIRTUAL. */
1505 qemu_account_warp_timer();
1507 /* Run the timers here. This is much more efficient than
1508 * waking up the I/O thread and waiting for completion.
1510 handle_icount_deadline();
1512 replay_mutex_unlock();
1518 while (cpu
&& !cpu
->queued_work_first
&& !cpu
->exit_request
) {
1520 atomic_mb_set(&tcg_current_rr_cpu
, cpu
);
1523 qemu_clock_enable(QEMU_CLOCK_VIRTUAL
,
1524 (cpu
->singlestep_enabled
& SSTEP_NOTIMER
) == 0);
1526 if (cpu_can_run(cpu
)) {
1529 qemu_mutex_unlock_iothread();
1530 prepare_icount_for_run(cpu
);
1532 r
= tcg_cpu_exec(cpu
);
1534 process_icount_data(cpu
);
1535 qemu_mutex_lock_iothread();
1537 if (r
== EXCP_DEBUG
) {
1538 cpu_handle_guest_debug(cpu
);
1540 } else if (r
== EXCP_ATOMIC
) {
1541 qemu_mutex_unlock_iothread();
1542 cpu_exec_step_atomic(cpu
);
1543 qemu_mutex_lock_iothread();
1546 } else if (cpu
->stop
) {
1548 cpu
= CPU_NEXT(cpu
);
1553 cpu
= CPU_NEXT(cpu
);
1554 } /* while (cpu && !cpu->exit_request).. */
1556 /* Does not need atomic_mb_set because a spurious wakeup is okay. */
1557 atomic_set(&tcg_current_rr_cpu
, NULL
);
1559 if (cpu
&& cpu
->exit_request
) {
1560 atomic_mb_set(&cpu
->exit_request
, 0);
1563 if (use_icount
&& all_cpu_threads_idle()) {
1565 * When all cpus are sleeping (e.g in WFI), to avoid a deadlock
1566 * in the main_loop, wake it up in order to start the warp timer.
1568 qemu_notify_event();
1571 qemu_tcg_rr_wait_io_event();
1572 deal_with_unplugged_cpus();
1575 rcu_unregister_thread();
1579 static void *qemu_hax_cpu_thread_fn(void *arg
)
1581 CPUState
*cpu
= arg
;
1584 rcu_register_thread();
1585 qemu_mutex_lock_iothread();
1586 qemu_thread_get_self(cpu
->thread
);
1588 cpu
->thread_id
= qemu_get_thread_id();
1589 cpu
->created
= true;
1594 qemu_cond_signal(&qemu_cpu_cond
);
1597 if (cpu_can_run(cpu
)) {
1598 r
= hax_smp_cpu_exec(cpu
);
1599 if (r
== EXCP_DEBUG
) {
1600 cpu_handle_guest_debug(cpu
);
1604 qemu_wait_io_event(cpu
);
1605 } while (!cpu
->unplug
|| cpu_can_run(cpu
));
1606 rcu_unregister_thread();
1610 /* The HVF-specific vCPU thread function. This one should only run when the host
1611 * CPU supports the VMX "unrestricted guest" feature. */
1612 static void *qemu_hvf_cpu_thread_fn(void *arg
)
1614 CPUState
*cpu
= arg
;
1618 assert(hvf_enabled());
1620 rcu_register_thread();
1622 qemu_mutex_lock_iothread();
1623 qemu_thread_get_self(cpu
->thread
);
1625 cpu
->thread_id
= qemu_get_thread_id();
1631 /* signal CPU creation */
1632 cpu
->created
= true;
1633 qemu_cond_signal(&qemu_cpu_cond
);
1636 if (cpu_can_run(cpu
)) {
1637 r
= hvf_vcpu_exec(cpu
);
1638 if (r
== EXCP_DEBUG
) {
1639 cpu_handle_guest_debug(cpu
);
1642 qemu_wait_io_event(cpu
);
1643 } while (!cpu
->unplug
|| cpu_can_run(cpu
));
1645 hvf_vcpu_destroy(cpu
);
1646 cpu
->created
= false;
1647 qemu_cond_signal(&qemu_cpu_cond
);
1648 qemu_mutex_unlock_iothread();
1649 rcu_unregister_thread();
1653 static void *qemu_whpx_cpu_thread_fn(void *arg
)
1655 CPUState
*cpu
= arg
;
1658 rcu_register_thread();
1660 qemu_mutex_lock_iothread();
1661 qemu_thread_get_self(cpu
->thread
);
1662 cpu
->thread_id
= qemu_get_thread_id();
1665 r
= whpx_init_vcpu(cpu
);
1667 fprintf(stderr
, "whpx_init_vcpu failed: %s\n", strerror(-r
));
1671 /* signal CPU creation */
1672 cpu
->created
= true;
1673 qemu_cond_signal(&qemu_cpu_cond
);
1676 if (cpu_can_run(cpu
)) {
1677 r
= whpx_vcpu_exec(cpu
);
1678 if (r
== EXCP_DEBUG
) {
1679 cpu_handle_guest_debug(cpu
);
1682 while (cpu_thread_is_idle(cpu
)) {
1683 qemu_cond_wait(cpu
->halt_cond
, &qemu_global_mutex
);
1685 qemu_wait_io_event_common(cpu
);
1686 } while (!cpu
->unplug
|| cpu_can_run(cpu
));
1688 whpx_destroy_vcpu(cpu
);
1689 cpu
->created
= false;
1690 qemu_cond_signal(&qemu_cpu_cond
);
1691 qemu_mutex_unlock_iothread();
1692 rcu_unregister_thread();
1697 static void CALLBACK
dummy_apc_func(ULONG_PTR unused
)
1702 /* Multi-threaded TCG
1704 * In the multi-threaded case each vCPU has its own thread. The TLS
1705 * variable current_cpu can be used deep in the code to find the
1706 * current CPUState for a given thread.
1709 static void *qemu_tcg_cpu_thread_fn(void *arg
)
1711 CPUState
*cpu
= arg
;
1713 assert(tcg_enabled());
1714 g_assert(!use_icount
);
1716 rcu_register_thread();
1717 tcg_register_thread();
1719 qemu_mutex_lock_iothread();
1720 qemu_thread_get_self(cpu
->thread
);
1722 cpu
->thread_id
= qemu_get_thread_id();
1723 cpu
->created
= true;
1726 qemu_cond_signal(&qemu_cpu_cond
);
1728 /* process any pending work */
1729 cpu
->exit_request
= 1;
1732 if (cpu_can_run(cpu
)) {
1734 qemu_mutex_unlock_iothread();
1735 r
= tcg_cpu_exec(cpu
);
1736 qemu_mutex_lock_iothread();
1739 cpu_handle_guest_debug(cpu
);
1742 /* during start-up the vCPU is reset and the thread is
1743 * kicked several times. If we don't ensure we go back
1744 * to sleep in the halted state we won't cleanly
1745 * start-up when the vCPU is enabled.
1747 * cpu->halted should ensure we sleep in wait_io_event
1749 g_assert(cpu
->halted
);
1752 qemu_mutex_unlock_iothread();
1753 cpu_exec_step_atomic(cpu
);
1754 qemu_mutex_lock_iothread();
1756 /* Ignore everything else? */
1761 atomic_mb_set(&cpu
->exit_request
, 0);
1762 qemu_wait_io_event(cpu
);
1763 } while (!cpu
->unplug
|| cpu_can_run(cpu
));
1765 qemu_tcg_destroy_vcpu(cpu
);
1766 cpu
->created
= false;
1767 qemu_cond_signal(&qemu_cpu_cond
);
1768 qemu_mutex_unlock_iothread();
1769 rcu_unregister_thread();
1773 static void qemu_cpu_kick_thread(CPUState
*cpu
)
1778 if (cpu
->thread_kicked
) {
1781 cpu
->thread_kicked
= true;
1782 err
= pthread_kill(cpu
->thread
->thread
, SIG_IPI
);
1783 if (err
&& err
!= ESRCH
) {
1784 fprintf(stderr
, "qemu:%s: %s", __func__
, strerror(err
));
1788 if (!qemu_cpu_is_self(cpu
)) {
1789 if (whpx_enabled()) {
1790 whpx_vcpu_kick(cpu
);
1791 } else if (!QueueUserAPC(dummy_apc_func
, cpu
->hThread
, 0)) {
1792 fprintf(stderr
, "%s: QueueUserAPC failed with error %lu\n",
1793 __func__
, GetLastError());
1800 void qemu_cpu_kick(CPUState
*cpu
)
1802 qemu_cond_broadcast(cpu
->halt_cond
);
1803 if (tcg_enabled()) {
1805 /* NOP unless doing single-thread RR */
1806 qemu_cpu_kick_rr_cpu();
1808 if (hax_enabled()) {
1810 * FIXME: race condition with the exit_request check in
1813 cpu
->exit_request
= 1;
1815 qemu_cpu_kick_thread(cpu
);
1819 void qemu_cpu_kick_self(void)
1821 assert(current_cpu
);
1822 qemu_cpu_kick_thread(current_cpu
);
1825 bool qemu_cpu_is_self(CPUState
*cpu
)
1827 return qemu_thread_is_self(cpu
->thread
);
1830 bool qemu_in_vcpu_thread(void)
1832 return current_cpu
&& qemu_cpu_is_self(current_cpu
);
1835 static __thread
bool iothread_locked
= false;
1837 bool qemu_mutex_iothread_locked(void)
1839 return iothread_locked
;
1843 * The BQL is taken from so many places that it is worth profiling the
1844 * callers directly, instead of funneling them all through a single function.
1846 void qemu_mutex_lock_iothread_impl(const char *file
, int line
)
1848 QemuMutexLockFunc bql_lock
= atomic_read(&qemu_bql_mutex_lock_func
);
1850 g_assert(!qemu_mutex_iothread_locked());
1851 bql_lock(&qemu_global_mutex
, file
, line
);
1852 iothread_locked
= true;
1855 void qemu_mutex_unlock_iothread(void)
1857 g_assert(qemu_mutex_iothread_locked());
1858 iothread_locked
= false;
1859 qemu_mutex_unlock(&qemu_global_mutex
);
1862 static bool all_vcpus_paused(void)
1867 if (!cpu
->stopped
) {
1875 void pause_all_vcpus(void)
1879 qemu_clock_enable(QEMU_CLOCK_VIRTUAL
, false);
1881 if (qemu_cpu_is_self(cpu
)) {
1882 qemu_cpu_stop(cpu
, true);
1889 /* We need to drop the replay_lock so any vCPU threads woken up
1890 * can finish their replay tasks
1892 replay_mutex_unlock();
1894 while (!all_vcpus_paused()) {
1895 qemu_cond_wait(&qemu_pause_cond
, &qemu_global_mutex
);
1901 qemu_mutex_unlock_iothread();
1902 replay_mutex_lock();
1903 qemu_mutex_lock_iothread();
1906 void cpu_resume(CPUState
*cpu
)
1909 cpu
->stopped
= false;
1913 void resume_all_vcpus(void)
1917 qemu_clock_enable(QEMU_CLOCK_VIRTUAL
, true);
1923 void cpu_remove_sync(CPUState
*cpu
)
1928 qemu_mutex_unlock_iothread();
1929 qemu_thread_join(cpu
->thread
);
1930 qemu_mutex_lock_iothread();
1933 /* For temporary buffers for forming a name */
1934 #define VCPU_THREAD_NAME_SIZE 16
1936 static void qemu_tcg_init_vcpu(CPUState
*cpu
)
1938 char thread_name
[VCPU_THREAD_NAME_SIZE
];
1939 static QemuCond
*single_tcg_halt_cond
;
1940 static QemuThread
*single_tcg_cpu_thread
;
1941 static int tcg_region_inited
;
1943 assert(tcg_enabled());
1945 * Initialize TCG regions--once. Now is a good time, because:
1946 * (1) TCG's init context, prologue and target globals have been set up.
1947 * (2) qemu_tcg_mttcg_enabled() works now (TCG init code runs before the
1948 * -accel flag is processed, so the check doesn't work then).
1950 if (!tcg_region_inited
) {
1951 tcg_region_inited
= 1;
1955 if (qemu_tcg_mttcg_enabled() || !single_tcg_cpu_thread
) {
1956 cpu
->thread
= g_malloc0(sizeof(QemuThread
));
1957 cpu
->halt_cond
= g_malloc0(sizeof(QemuCond
));
1958 qemu_cond_init(cpu
->halt_cond
);
1960 if (qemu_tcg_mttcg_enabled()) {
1961 /* create a thread per vCPU with TCG (MTTCG) */
1962 parallel_cpus
= true;
1963 snprintf(thread_name
, VCPU_THREAD_NAME_SIZE
, "CPU %d/TCG",
1966 qemu_thread_create(cpu
->thread
, thread_name
, qemu_tcg_cpu_thread_fn
,
1967 cpu
, QEMU_THREAD_JOINABLE
);
1970 /* share a single thread for all cpus with TCG */
1971 snprintf(thread_name
, VCPU_THREAD_NAME_SIZE
, "ALL CPUs/TCG");
1972 qemu_thread_create(cpu
->thread
, thread_name
,
1973 qemu_tcg_rr_cpu_thread_fn
,
1974 cpu
, QEMU_THREAD_JOINABLE
);
1976 single_tcg_halt_cond
= cpu
->halt_cond
;
1977 single_tcg_cpu_thread
= cpu
->thread
;
1980 cpu
->hThread
= qemu_thread_get_handle(cpu
->thread
);
1983 /* For non-MTTCG cases we share the thread */
1984 cpu
->thread
= single_tcg_cpu_thread
;
1985 cpu
->halt_cond
= single_tcg_halt_cond
;
1986 cpu
->thread_id
= first_cpu
->thread_id
;
1988 cpu
->created
= true;
1992 static void qemu_hax_start_vcpu(CPUState
*cpu
)
1994 char thread_name
[VCPU_THREAD_NAME_SIZE
];
1996 cpu
->thread
= g_malloc0(sizeof(QemuThread
));
1997 cpu
->halt_cond
= g_malloc0(sizeof(QemuCond
));
1998 qemu_cond_init(cpu
->halt_cond
);
2000 snprintf(thread_name
, VCPU_THREAD_NAME_SIZE
, "CPU %d/HAX",
2002 qemu_thread_create(cpu
->thread
, thread_name
, qemu_hax_cpu_thread_fn
,
2003 cpu
, QEMU_THREAD_JOINABLE
);
2005 cpu
->hThread
= qemu_thread_get_handle(cpu
->thread
);
2009 static void qemu_kvm_start_vcpu(CPUState
*cpu
)
2011 char thread_name
[VCPU_THREAD_NAME_SIZE
];
2013 cpu
->thread
= g_malloc0(sizeof(QemuThread
));
2014 cpu
->halt_cond
= g_malloc0(sizeof(QemuCond
));
2015 qemu_cond_init(cpu
->halt_cond
);
2016 snprintf(thread_name
, VCPU_THREAD_NAME_SIZE
, "CPU %d/KVM",
2018 qemu_thread_create(cpu
->thread
, thread_name
, qemu_kvm_cpu_thread_fn
,
2019 cpu
, QEMU_THREAD_JOINABLE
);
2022 static void qemu_hvf_start_vcpu(CPUState
*cpu
)
2024 char thread_name
[VCPU_THREAD_NAME_SIZE
];
2026 /* HVF currently does not support TCG, and only runs in
2027 * unrestricted-guest mode. */
2028 assert(hvf_enabled());
2030 cpu
->thread
= g_malloc0(sizeof(QemuThread
));
2031 cpu
->halt_cond
= g_malloc0(sizeof(QemuCond
));
2032 qemu_cond_init(cpu
->halt_cond
);
2034 snprintf(thread_name
, VCPU_THREAD_NAME_SIZE
, "CPU %d/HVF",
2036 qemu_thread_create(cpu
->thread
, thread_name
, qemu_hvf_cpu_thread_fn
,
2037 cpu
, QEMU_THREAD_JOINABLE
);
2040 static void qemu_whpx_start_vcpu(CPUState
*cpu
)
2042 char thread_name
[VCPU_THREAD_NAME_SIZE
];
2044 cpu
->thread
= g_malloc0(sizeof(QemuThread
));
2045 cpu
->halt_cond
= g_malloc0(sizeof(QemuCond
));
2046 qemu_cond_init(cpu
->halt_cond
);
2047 snprintf(thread_name
, VCPU_THREAD_NAME_SIZE
, "CPU %d/WHPX",
2049 qemu_thread_create(cpu
->thread
, thread_name
, qemu_whpx_cpu_thread_fn
,
2050 cpu
, QEMU_THREAD_JOINABLE
);
2052 cpu
->hThread
= qemu_thread_get_handle(cpu
->thread
);
2056 static void qemu_dummy_start_vcpu(CPUState
*cpu
)
2058 char thread_name
[VCPU_THREAD_NAME_SIZE
];
2060 cpu
->thread
= g_malloc0(sizeof(QemuThread
));
2061 cpu
->halt_cond
= g_malloc0(sizeof(QemuCond
));
2062 qemu_cond_init(cpu
->halt_cond
);
2063 snprintf(thread_name
, VCPU_THREAD_NAME_SIZE
, "CPU %d/DUMMY",
2065 qemu_thread_create(cpu
->thread
, thread_name
, qemu_dummy_cpu_thread_fn
, cpu
,
2066 QEMU_THREAD_JOINABLE
);
2069 void qemu_init_vcpu(CPUState
*cpu
)
2071 cpu
->nr_cores
= smp_cores
;
2072 cpu
->nr_threads
= smp_threads
;
2073 cpu
->stopped
= true;
2076 /* If the target cpu hasn't set up any address spaces itself,
2077 * give it the default one.
2080 cpu_address_space_init(cpu
, 0, "cpu-memory", cpu
->memory
);
2083 if (kvm_enabled()) {
2084 qemu_kvm_start_vcpu(cpu
);
2085 } else if (hax_enabled()) {
2086 qemu_hax_start_vcpu(cpu
);
2087 } else if (hvf_enabled()) {
2088 qemu_hvf_start_vcpu(cpu
);
2089 } else if (tcg_enabled()) {
2090 qemu_tcg_init_vcpu(cpu
);
2091 } else if (whpx_enabled()) {
2092 qemu_whpx_start_vcpu(cpu
);
2094 qemu_dummy_start_vcpu(cpu
);
2097 while (!cpu
->created
) {
2098 qemu_cond_wait(&qemu_cpu_cond
, &qemu_global_mutex
);
2102 void cpu_stop_current(void)
2105 current_cpu
->stop
= true;
2106 cpu_exit(current_cpu
);
2110 int vm_stop(RunState state
)
2112 if (qemu_in_vcpu_thread()) {
2113 qemu_system_vmstop_request_prepare();
2114 qemu_system_vmstop_request(state
);
2116 * FIXME: should not return to device code in case
2117 * vm_stop() has been requested.
2123 return do_vm_stop(state
, true);
2127 * Prepare for (re)starting the VM.
2128 * Returns -1 if the vCPUs are not to be restarted (e.g. if they are already
2129 * running or in case of an error condition), 0 otherwise.
2131 int vm_prepare_start(void)
2135 qemu_vmstop_requested(&requested
);
2136 if (runstate_is_running() && requested
== RUN_STATE__MAX
) {
2140 /* Ensure that a STOP/RESUME pair of events is emitted if a
2141 * vmstop request was pending. The BLOCK_IO_ERROR event, for
2142 * example, according to documentation is always followed by
2145 if (runstate_is_running()) {
2146 qapi_event_send_stop();
2147 qapi_event_send_resume();
2151 /* We are sending this now, but the CPUs will be resumed shortly later */
2152 qapi_event_send_resume();
2154 replay_enable_events();
2156 runstate_set(RUN_STATE_RUNNING
);
2157 vm_state_notify(1, RUN_STATE_RUNNING
);
2163 if (!vm_prepare_start()) {
2168 /* does a state transition even if the VM is already stopped,
2169 current state is forgotten forever */
2170 int vm_stop_force_state(RunState state
)
2172 if (runstate_is_running()) {
2173 return vm_stop(state
);
2175 runstate_set(state
);
2178 /* Make sure to return an error if the flush in a previous vm_stop()
2180 return bdrv_flush_all();
2184 void list_cpus(const char *optarg
)
2186 /* XXX: implement xxx_cpu_list for targets that still miss it */
2187 #if defined(cpu_list)
2192 CpuInfoList
*qmp_query_cpus(Error
**errp
)
2194 MachineState
*ms
= MACHINE(qdev_get_machine());
2195 MachineClass
*mc
= MACHINE_GET_CLASS(ms
);
2196 CpuInfoList
*head
= NULL
, *cur_item
= NULL
;
2201 #if defined(TARGET_I386)
2202 X86CPU
*x86_cpu
= X86_CPU(cpu
);
2203 CPUX86State
*env
= &x86_cpu
->env
;
2204 #elif defined(TARGET_PPC)
2205 PowerPCCPU
*ppc_cpu
= POWERPC_CPU(cpu
);
2206 CPUPPCState
*env
= &ppc_cpu
->env
;
2207 #elif defined(TARGET_SPARC)
2208 SPARCCPU
*sparc_cpu
= SPARC_CPU(cpu
);
2209 CPUSPARCState
*env
= &sparc_cpu
->env
;
2210 #elif defined(TARGET_RISCV)
2211 RISCVCPU
*riscv_cpu
= RISCV_CPU(cpu
);
2212 CPURISCVState
*env
= &riscv_cpu
->env
;
2213 #elif defined(TARGET_MIPS)
2214 MIPSCPU
*mips_cpu
= MIPS_CPU(cpu
);
2215 CPUMIPSState
*env
= &mips_cpu
->env
;
2216 #elif defined(TARGET_TRICORE)
2217 TriCoreCPU
*tricore_cpu
= TRICORE_CPU(cpu
);
2218 CPUTriCoreState
*env
= &tricore_cpu
->env
;
2219 #elif defined(TARGET_S390X)
2220 S390CPU
*s390_cpu
= S390_CPU(cpu
);
2221 CPUS390XState
*env
= &s390_cpu
->env
;
2224 cpu_synchronize_state(cpu
);
2226 info
= g_malloc0(sizeof(*info
));
2227 info
->value
= g_malloc0(sizeof(*info
->value
));
2228 info
->value
->CPU
= cpu
->cpu_index
;
2229 info
->value
->current
= (cpu
== first_cpu
);
2230 info
->value
->halted
= cpu
->halted
;
2231 info
->value
->qom_path
= object_get_canonical_path(OBJECT(cpu
));
2232 info
->value
->thread_id
= cpu
->thread_id
;
2233 #if defined(TARGET_I386)
2234 info
->value
->arch
= CPU_INFO_ARCH_X86
;
2235 info
->value
->u
.x86
.pc
= env
->eip
+ env
->segs
[R_CS
].base
;
2236 #elif defined(TARGET_PPC)
2237 info
->value
->arch
= CPU_INFO_ARCH_PPC
;
2238 info
->value
->u
.ppc
.nip
= env
->nip
;
2239 #elif defined(TARGET_SPARC)
2240 info
->value
->arch
= CPU_INFO_ARCH_SPARC
;
2241 info
->value
->u
.q_sparc
.pc
= env
->pc
;
2242 info
->value
->u
.q_sparc
.npc
= env
->npc
;
2243 #elif defined(TARGET_MIPS)
2244 info
->value
->arch
= CPU_INFO_ARCH_MIPS
;
2245 info
->value
->u
.q_mips
.PC
= env
->active_tc
.PC
;
2246 #elif defined(TARGET_TRICORE)
2247 info
->value
->arch
= CPU_INFO_ARCH_TRICORE
;
2248 info
->value
->u
.tricore
.PC
= env
->PC
;
2249 #elif defined(TARGET_S390X)
2250 info
->value
->arch
= CPU_INFO_ARCH_S390
;
2251 info
->value
->u
.s390
.cpu_state
= env
->cpu_state
;
2252 #elif defined(TARGET_RISCV)
2253 info
->value
->arch
= CPU_INFO_ARCH_RISCV
;
2254 info
->value
->u
.riscv
.pc
= env
->pc
;
2256 info
->value
->arch
= CPU_INFO_ARCH_OTHER
;
2258 info
->value
->has_props
= !!mc
->cpu_index_to_instance_props
;
2259 if (info
->value
->has_props
) {
2260 CpuInstanceProperties
*props
;
2261 props
= g_malloc0(sizeof(*props
));
2262 *props
= mc
->cpu_index_to_instance_props(ms
, cpu
->cpu_index
);
2263 info
->value
->props
= props
;
2266 /* XXX: waiting for the qapi to support GSList */
2268 head
= cur_item
= info
;
2270 cur_item
->next
= info
;
2278 static CpuInfoArch
sysemu_target_to_cpuinfo_arch(SysEmuTarget target
)
2281 * The @SysEmuTarget -> @CpuInfoArch mapping below is based on the
2282 * TARGET_ARCH -> TARGET_BASE_ARCH mapping in the "configure" script.
2285 case SYS_EMU_TARGET_I386
:
2286 case SYS_EMU_TARGET_X86_64
:
2287 return CPU_INFO_ARCH_X86
;
2289 case SYS_EMU_TARGET_PPC
:
2290 case SYS_EMU_TARGET_PPC64
:
2291 return CPU_INFO_ARCH_PPC
;
2293 case SYS_EMU_TARGET_SPARC
:
2294 case SYS_EMU_TARGET_SPARC64
:
2295 return CPU_INFO_ARCH_SPARC
;
2297 case SYS_EMU_TARGET_MIPS
:
2298 case SYS_EMU_TARGET_MIPSEL
:
2299 case SYS_EMU_TARGET_MIPS64
:
2300 case SYS_EMU_TARGET_MIPS64EL
:
2301 return CPU_INFO_ARCH_MIPS
;
2303 case SYS_EMU_TARGET_TRICORE
:
2304 return CPU_INFO_ARCH_TRICORE
;
2306 case SYS_EMU_TARGET_S390X
:
2307 return CPU_INFO_ARCH_S390
;
2309 case SYS_EMU_TARGET_RISCV32
:
2310 case SYS_EMU_TARGET_RISCV64
:
2311 return CPU_INFO_ARCH_RISCV
;
2314 return CPU_INFO_ARCH_OTHER
;
2318 static void cpustate_to_cpuinfo_s390(CpuInfoS390
*info
, const CPUState
*cpu
)
2321 S390CPU
*s390_cpu
= S390_CPU(cpu
);
2322 CPUS390XState
*env
= &s390_cpu
->env
;
2324 info
->cpu_state
= env
->cpu_state
;
2331 * fast means: we NEVER interrupt vCPU threads to retrieve
2332 * information from KVM.
2334 CpuInfoFastList
*qmp_query_cpus_fast(Error
**errp
)
2336 MachineState
*ms
= MACHINE(qdev_get_machine());
2337 MachineClass
*mc
= MACHINE_GET_CLASS(ms
);
2338 CpuInfoFastList
*head
= NULL
, *cur_item
= NULL
;
2339 SysEmuTarget target
= qapi_enum_parse(&SysEmuTarget_lookup
, TARGET_NAME
,
2344 CpuInfoFastList
*info
= g_malloc0(sizeof(*info
));
2345 info
->value
= g_malloc0(sizeof(*info
->value
));
2347 info
->value
->cpu_index
= cpu
->cpu_index
;
2348 info
->value
->qom_path
= object_get_canonical_path(OBJECT(cpu
));
2349 info
->value
->thread_id
= cpu
->thread_id
;
2351 info
->value
->has_props
= !!mc
->cpu_index_to_instance_props
;
2352 if (info
->value
->has_props
) {
2353 CpuInstanceProperties
*props
;
2354 props
= g_malloc0(sizeof(*props
));
2355 *props
= mc
->cpu_index_to_instance_props(ms
, cpu
->cpu_index
);
2356 info
->value
->props
= props
;
2359 info
->value
->arch
= sysemu_target_to_cpuinfo_arch(target
);
2360 info
->value
->target
= target
;
2361 if (target
== SYS_EMU_TARGET_S390X
) {
2362 cpustate_to_cpuinfo_s390(&info
->value
->u
.s390x
, cpu
);
2366 head
= cur_item
= info
;
2368 cur_item
->next
= info
;
2376 void qmp_memsave(int64_t addr
, int64_t size
, const char *filename
,
2377 bool has_cpu
, int64_t cpu_index
, Error
**errp
)
2383 int64_t orig_addr
= addr
, orig_size
= size
;
2389 cpu
= qemu_get_cpu(cpu_index
);
2391 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cpu-index",
2396 f
= fopen(filename
, "wb");
2398 error_setg_file_open(errp
, errno
, filename
);
2406 if (cpu_memory_rw_debug(cpu
, addr
, buf
, l
, 0) != 0) {
2407 error_setg(errp
, "Invalid addr 0x%016" PRIx64
"/size %" PRId64
2408 " specified", orig_addr
, orig_size
);
2411 if (fwrite(buf
, 1, l
, f
) != l
) {
2412 error_setg(errp
, QERR_IO_ERROR
);
2423 void qmp_pmemsave(int64_t addr
, int64_t size
, const char *filename
,
2430 f
= fopen(filename
, "wb");
2432 error_setg_file_open(errp
, errno
, filename
);
2440 cpu_physical_memory_read(addr
, buf
, l
);
2441 if (fwrite(buf
, 1, l
, f
) != l
) {
2442 error_setg(errp
, QERR_IO_ERROR
);
2453 void qmp_inject_nmi(Error
**errp
)
2455 nmi_monitor_handle(monitor_get_cpu_index(), errp
);
2458 void dump_drift_info(void)
2464 qemu_printf("Host - Guest clock %"PRIi64
" ms\n",
2465 (cpu_get_clock() - cpu_get_icount())/SCALE_MS
);
2466 if (icount_align_option
) {
2467 qemu_printf("Max guest delay %"PRIi64
" ms\n",
2468 -max_delay
/ SCALE_MS
);
2469 qemu_printf("Max guest advance %"PRIi64
" ms\n",
2470 max_advance
/ SCALE_MS
);
2472 qemu_printf("Max guest delay NA\n");
2473 qemu_printf("Max guest advance NA\n");