Skip to contentSkip to conversation

sysemu/replay: Restrict icount to TCG system emulation

PATCH Superseded Philippe Mathieu-Daudé 3 participants 15 messages Dec 7, 2023 – Dec 8, 2023

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover sysemu/replay: Restrict icount to TCG system emulation

Slightly simplify non-TCG and user emulation code.

Since v1:
- Introduce enum of icount modes
- Fix ARM INST_RETIRED event

Philippe Mathieu-Daudé (5):
sysemu/cpu-timers: Introduce ICountMode enumerator target/arm: Ensure icount is enabled when emulating INST_RETIRED util/async: Only call icount_notify_exit() if icount is enabled system/vl: Restrict icount to TCG emulation sysemu/replay: Restrict icount to system emulation

 include/sysemu/cpu-timers.h | 22 ++++++++++++++--------
 include/sysemu/replay.h     | 11 ++++++++---
 accel/tcg/icount-common.c   | 16 +++++++---------
 stubs/icount.c              | 29 ++---------------------------
 system/cpu-timers.c         |  2 +-
 system/vl.c                 |  6 +++++-
 target/arm/helper.c         |  5 ++++-
 util/async.c                | 16 +++++++++-------
 8 files changed, 50 insertions(+), 57 deletions(-)
Signature · 1 line
2.41.0
#1 lore raw

PATCH v2 1/5 sysemu/cpu-timers: Introduce ICountMode enumerator

Rather than having to lookup for what the 0, 1, 2, ... icount values are, use a enum definition.

---
 include/sysemu/cpu-timers.h | 20 +++++++++++++-------
 accel/tcg/icount-common.c   | 16 +++++++---------
 stubs/icount.c              |  2 +-
 system/cpu-timers.c         |  2 +-
 target/arm/helper.c         |  3 ++-
 5 files changed, 24 insertions(+), 19 deletions(-)
include/sysemu/cpu-timers.h +13 -7
index 2e786fe7fb..e909ffae47 100644
@@ -17,18 +17,24 @@ void cpu_timers_init(void);
 
 /* icount - Instruction Counter API */
 
-/*
- * icount enablement state:
+/**
+ * ICountMode: icount enablement state:
  *
- * 0 = Disabled - Do not count executed instructions.
- * 1 = Enabled - Fixed conversion of insn to ns via "shift" option
- * 2 = Enabled - Runtime adaptive algorithm to compute shift
+ * @ICOUNT_DISABLED: Disabled - Do not count executed instructions.
+ * @ICOUNT_PRECISE: Enabled - Fixed conversion of insn to ns via "shift" option
+ * @ICOUNT_ADAPTATIVE: Enabled - Runtime adaptive algorithm to compute shift
  */
+typedef enum {
+    ICOUNT_DISABLED = 0,
+    ICOUNT_PRECISE = 1,
+    ICOUNT_ADAPTATIVE = 2,
+} ICountMode;
+
 #ifdef CONFIG_TCG
-extern int use_icount;
+extern ICountMode use_icount;
 #define icount_enabled() (use_icount)
 #else
-#define icount_enabled() 0
+#define icount_enabled() ICOUNT_DISABLED
 #endif
 
 /*
accel/tcg/icount-common.c +7 -9
index ec57192be8..067c93a9ae 100644
@@ -49,21 +49,19 @@ static bool icount_sleep = true;
 /* Arbitrarily pick 1MIPS as the minimum allowable speed.  */
 #define MAX_ICOUNT_SHIFT 10
 
-/*
- * 0 = Do not count executed instructions.
- * 1 = Fixed conversion of insn to ns via "shift" option
- * 2 = Runtime adaptive algorithm to compute shift
- */
-int use_icount;
+/* Do not count executed instructions */
+ICountMode use_icount = ICOUNT_DISABLED;
 
 static void icount_enable_precise(void)
 {
-    use_icount = 1;
+    /* Fixed conversion of insn to ns via "shift" option */
+    use_icount = ICOUNT_PRECISE;
 }
 
 static void icount_enable_adaptive(void)
 {
-    use_icount = 2;
+    /* Runtime adaptive algorithm to compute shift */
+    use_icount = ICOUNT_ADAPTATIVE;
 }
 
 /*
@@ -256,7 +254,7 @@ static void icount_warp_rt(void)
         int64_t warp_delta;
 
         warp_delta = clock - timers_state.vm_clock_warp_start;
-        if (icount_enabled() == 2) {
+        if (icount_enabled() == ICOUNT_ADAPTATIVE) {
             /*
              * In adaptive mode, do not let QEMU_CLOCK_VIRTUAL run too far
              * ahead of real time (it might already be ahead so careful not
stubs/icount.c +1 -1
index 6df8c2bf7d..f8e6a014b8 100644
@@ -4,7 +4,7 @@
 
 /* icount - Instruction Counter API */
 
-int use_icount;
+ICountMode use_icount = ICOUNT_DISABLED;
 
 void icount_update(CPUState *cpu)
 {
system/cpu-timers.c +1 -1
index 7452d97b67..6befb82e48 100644
@@ -154,7 +154,7 @@ static bool adjust_timers_state_needed(void *opaque)
 
 static bool icount_shift_state_needed(void *opaque)
 {
-    return icount_enabled() == 2;
+    return icount_enabled() == ICOUNT_ADAPTATIVE;
 }
 
 /*
target/arm/helper.c +2 -1
index 2746d3fdac..adb0960bba 100644
@@ -934,7 +934,8 @@ static int64_t cycles_ns_per(uint64_t cycles)
 
 static bool instructions_supported(CPUARMState *env)
 {
-    return icount_enabled() == 1; /* Precise instruction counting */
+    /* Precise instruction counting */
+    return icount_enabled() == ICOUNT_PRECISE;
 }
 
 static uint64_t instructions_get_count(CPUARMState *env)
Signature · 1 line
2.41.0
Reviewed-by on v2 1/5
#2 lore raw

Re: [PATCH v2 1/5] sysemu/cpu-timers: Introduce ICountMode enumerator

Quoted 26 lines · Philippe Mathieu-Daudé

Rather than having to lookup for what the 0, 1, 2, ... icount values are, use a enum definition.

---
  include/sysemu/cpu-timers.h | 20 +++++++++++++-------
  accel/tcg/icount-common.c   | 16 +++++++---------
  stubs/icount.c              |  2 +-
  system/cpu-timers.c         |  2 +-
  target/arm/helper.c         |  3 ++-
  5 files changed, 24 insertions(+), 19 deletions(-)
include/sysemu/cpu-timers.h +2 -3
index 2e786fe7fb..e909ffae47 100644
@@ -17,18 +17,24 @@ void cpu_timers_init(void);
 
  /* icount - Instruction Counter API */
 
-/*
- * icount enablement state:
+/**
+ * ICountMode: icount enablement state:
   *
- * 0 = Disabled - Do not count executed instructions.

include/sysemu/cpu-timers.h void cpu_timers_init(void); · line 30

Quoted 10 lines
include/sysemu/cpu-timers.h +7 -2
- * 1 = Enabled - Fixed conversion of insn to ns via "shift" option
- * 2 = Enabled - Runtime adaptive algorithm to compute shift
+ * @ICOUNT_DISABLED: Disabled - Do not count executed instructions.
+ * @ICOUNT_PRECISE: Enabled - Fixed conversion of insn to ns via "shift" option
+ * @ICOUNT_ADAPTATIVE: Enabled - Runtime adaptive algorithm to compute shift
   */
+typedef enum {
+    ICOUNT_DISABLED = 0,
+    ICOUNT_PRECISE = 1,
+    ICOUNT_ADAPTATIVE = 2,

No need for the assignments. Otherwise,

r~

#3 lore raw

PATCH v2 2/5 target/arm: Ensure icount is enabled when emulating INST_RETIRED

In reply to Philippe Mathieu-Daudé

pmu_init() register its event checking the pm_event::supported() handler. For INST_RETIRED, the event is only registered and the bit enabled in the PMU Common Event Identification register when icount is enabled as ICOUNT_PRECISE.

Assert the pm_event::get_count() and pm_event::ns_per_count() handler will only be called under this icount mode.

---
 target/arm/helper.c | 2 ++
 1 file changed, 2 insertions(+)
target/arm/helper.c +2 -0
index adb0960bba..333fd5f4bf 100644
@@ -940,11 +940,13 @@ static bool instructions_supported(CPUARMState *env)
 
 static uint64_t instructions_get_count(CPUARMState *env)
 {
+    assert(icount_enabled() == ICOUNT_PRECISE);
     return (uint64_t)icount_get_raw();
 }
 
 static int64_t instructions_ns_per(uint64_t icount)
 {
+    assert(icount_enabled() == ICOUNT_PRECISE);
     return icount_to_ns((int64_t)icount);
 }
 #endif
Signature · 1 line
2.41.0
#4 lore raw

Re: [PATCH v2 2/5] target/arm: Ensure icount is enabled when emulating INST_RETIRED

Quoted 21 lines · Philippe Mathieu-Daudé

pmu_init() register its event checking the pm_event::supported() handler. For INST_RETIRED, the event is only registered and the bit enabled in the PMU Common Event Identification register when icount is enabled as ICOUNT_PRECISE.

Assert the pm_event::get_count() and pm_event::ns_per_count() handler will only be called under this icount mode.

---
  target/arm/helper.c | 2 ++
  1 file changed, 2 insertions(+)
target/arm/helper.c +0 -0
index adb0960bba..333fd5f4bf 100644
@@ -940,11 +940,13 @@ static bool instructions_supported(CPUARMState *env)
 
  static uint64_t instructions_get_count(CPUARMState *env)
  {

target/arm/helper.c static bool instructions_supported(CPUARMState *env) · line 952

Quoted 10 lines
target/arm/helper.c +2 -0
+    assert(icount_enabled() == ICOUNT_PRECISE);
      return (uint64_t)icount_get_raw();
  }
 
  static int64_t instructions_ns_per(uint64_t icount)
  {
+    assert(icount_enabled() == ICOUNT_PRECISE);
      return icount_to_ns((int64_t)icount);
  }
  #endif

I don't think an assert is required -- that's exactly what the .supported field is for. If you think this needs additional clarification, a comment is sufficient.

r~

#5 lore raw

Re: [PATCH v2 2/5] target/arm: Ensure icount is enabled when emulating INST_RETIRED

Quoted 25 lines · Richard Henderson
Quoted 24 lines · Philippe Mathieu-Daudé

pmu_init() register its event checking the pm_event::supported() handler. For INST_RETIRED, the event is only registered and the bit enabled in the PMU Common Event Identification register when icount is enabled as ICOUNT_PRECISE.

Assert the pm_event::get_count() and pm_event::ns_per_count() handler will only be called under this icount mode.

---
  target/arm/helper.c | 2 ++

  1 file changed, 2 insertions(+)
target/arm/helper.c +1 -0
index adb0960bba..333fd5f4bf 100644
@@ -940,11 +940,13 @@ static bool instructions_supported(CPUARMState 
 *env)
   static uint64_t instructions_get_count(CPUARMState *env)
   {
+    assert(icount_enabled() == ICOUNT_PRECISE);
       return (uint64_t)icount_get_raw();
   }
Quoted 8 lines

target/arm/helper.c static bool instructions_supported(CPUARMState *env) · line 952

Quoted 6 lines
target/arm/helper.c +1 -0
   static int64_t instructions_ns_per(uint64_t icount)
   {
+    assert(icount_enabled() == ICOUNT_PRECISE);
       return icount_to_ns((int64_t)icount);
   }
   #endif

I don't think an assert is required -- that's exactly what the .supported field is for. If you think this needs additional clarification, a comment is sufficient.

Without this I'm getting this link failure with TCG disabled:

ld: Undefined symbols:
_icount_to_ns, referenced from:
_instructions_ns_per in target_arm_helper.c.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)

#6 lore raw

Re: [PATCH v2 2/5] target/arm: Ensure icount is enabled when emulating INST_RETIRED

Quoted 38 lines · Philippe Mathieu-Daudé
Quoted 27 lines · Richard Henderson
Quoted 26 lines · Philippe Mathieu-Daudé

pmu_init() register its event checking the pm_event::supported() handler. For INST_RETIRED, the event is only registered and the bit enabled in the PMU Common Event Identification register when icount is enabled as ICOUNT_PRECISE.

Assert the pm_event::get_count() and pm_event::ns_per_count() handler will only be called under this icount mode.

---
  target/arm/helper.c | 2 ++
  1 file changed, 2 insertions(+)
target/arm/helper.c +1 -0
index adb0960bba..333fd5f4bf 100644
@@ -940,11 +940,13 @@ static bool instructions_supported(CPUARMState
 *env)
  static uint64_t instructions_get_count(CPUARMState *env)
  {
+    assert(icount_enabled() == ICOUNT_PRECISE);
      return (uint64_t)icount_get_raw();
  }
  static int64_t instructions_ns_per(uint64_t icount)
  {
Quoted 8 lines

target/arm/helper.c static bool instructions_supported(CPUARMState *env) · line 952

Quoted 4 lines
target/arm/helper.c +1 -0
+    assert(icount_enabled() == ICOUNT_PRECISE);
      return icount_to_ns((int64_t)icount);
  }
  #endif

I don't think an assert is required -- that's exactly what the .supported field is for. If you think this needs additional clarification, a comment is sufficient.

Quoted 7 lines

Without this I'm getting this link failure with TCG disabled:

ld: Undefined symbols:
_icount_to_ns, referenced from:
_instructions_ns_per in target_arm_helper.c.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I think we should fix this earlier by not trying to enable these TCG-only PMU event types in a non-TCG config.

-- PMM

#7 lore raw

Re: [PATCH v2 2/5] target/arm: Ensure icount is enabled when emulating INST_RETIRED

Hi Peter,

Quoted 41 lines · Peter Maydell
Quoted 40 lines · Philippe Mathieu-Daudé
Quoted 27 lines · Richard Henderson
Quoted 26 lines · Philippe Mathieu-Daudé

pmu_init() register its event checking the pm_event::supported() handler. For INST_RETIRED, the event is only registered and the bit enabled in the PMU Common Event Identification register when icount is enabled as ICOUNT_PRECISE.

Assert the pm_event::get_count() and pm_event::ns_per_count() handler will only be called under this icount mode.

---
   target/arm/helper.c | 2 ++
   1 file changed, 2 insertions(+)
target/arm/helper.c +1 -0
index adb0960bba..333fd5f4bf 100644
@@ -940,11 +940,13 @@ static bool instructions_supported(CPUARMState
 *env)
   static uint64_t instructions_get_count(CPUARMState *env)
   {
+    assert(icount_enabled() == ICOUNT_PRECISE);
       return (uint64_t)icount_get_raw();
   }
   static int64_t instructions_ns_per(uint64_t icount)
   {
Quoted 8 lines

target/arm/helper.c static bool instructions_supported(CPUARMState *env) · line 952

Quoted 4 lines
target/arm/helper.c +1 -0
+    assert(icount_enabled() == ICOUNT_PRECISE);
       return icount_to_ns((int64_t)icount);
   }
   #endif

I don't think an assert is required -- that's exactly what the .supported field is for. If you think this needs additional clarification, a comment is sufficient.

Without this I'm getting this link failure with TCG disabled:

Quoted 8 lines
Quoted 5 lines

ld: Undefined symbols:
_icount_to_ns, referenced from:
_instructions_ns_per in target_arm_helper.c.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I think we should fix this earlier by not trying to enable these TCG-only PMU event types in a non-TCG config.

I agree... but (as discussed yesterday on IRC), this is a bigger rework.

This icount cleanup blocks 60+ patches on top :/ Since I have a v3 addressing Richard's comments already done, I'll post it, mentioning the PMU issue in the cover; then see if cleaning it isn't too invasive. If I end in another rabbit hole, I'll suggest to accept this current patch and clean the technical debt later, again.

Thanks,

Phil.

#8 lore raw

Re: [PATCH v2 2/5] target/arm: Ensure icount is enabled when emulating INST_RETIRED

Quoted 50 lines · Philippe Mathieu-Daudé

Hi Peter,

Quoted 47 lines · Peter Maydell
Quoted 45 lines · Philippe Mathieu-Daudé
Quoted 27 lines · Richard Henderson
Quoted 26 lines · Philippe Mathieu-Daudé

pmu_init() register its event checking the pm_event::supported() handler. For INST_RETIRED, the event is only registered and the bit enabled in the PMU Common Event Identification register when icount is enabled as ICOUNT_PRECISE.

Assert the pm_event::get_count() and pm_event::ns_per_count() handler will only be called under this icount mode.

---
   target/arm/helper.c | 2 ++

   1 file changed, 2 insertions(+)
target/arm/helper.c +1 -0
index adb0960bba..333fd5f4bf 100644
@@ -940,11 +940,13 @@ static bool instructions_supported(CPUARMState
 *env)
    static uint64_t instructions_get_count(CPUARMState *env)
    {
+    assert(icount_enabled() == ICOUNT_PRECISE);
        return (uint64_t)icount_get_raw();
    }
    static int64_t instructions_ns_per(uint64_t icount)
    {
Quoted 8 lines

target/arm/helper.c static bool instructions_supported(CPUARMState *env) · line 952

Quoted 4 lines
target/arm/helper.c +1 -0
+    assert(icount_enabled() == ICOUNT_PRECISE);
        return icount_to_ns((int64_t)icount);
    }
    #endif

I don't think an assert is required -- that's exactly what the .supported field is for. If you think this needs additional clarification, a comment is sufficient.

Without this I'm getting this link failure with TCG disabled:

ld: Undefined symbols:
    _icount_to_ns, referenced from:
        _instructions_ns_per in target_arm_helper.c.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Quoted 4 lines
Quoted 2 lines

I think we should fix this earlier by not trying to enable these TCG-only PMU event types in a non-TCG config.

I agree... but (as discussed yesterday on IRC), this is a bigger rework.

Giving it a try, I figured HVF emulates PMC (cycle counter) within some vPMU, containing "a single event source: the cycle counter" (per Alex).
Some helpers are duplicated, such pmu_update_irq().

pmu_counter_enabled() diff (-KVM +HVF):

/*
* Returns true if the counter (pass 31 for PMCCNTR) should count
events using
* the current EL, security state, and register configuration. */
static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)

 {
     uint64_t filter;
-    bool e, p, u, nsk, nsu, nsh, m;
-    bool enabled, prohibited = false, filtered;
-    bool secure = arm_is_secure(env);
+    bool enabled, filtered = true;
     int el = arm_current_el(env);

- uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
- uint8_t hpmn = mdcr_el2 & MDCR_HPMN;

- if (!arm_feature(env, ARM_FEATURE_PMU)) {
- return false;
- }
-
- if (!arm_feature(env, ARM_FEATURE_EL2) ||
- (counter < hpmn || counter == 31)) {
- e = env->cp15.c9_pmcr & PMCRE;
- } else {
- e = mdcr_el2 & MDCR_HPME;
- }
- enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
-
- /* Is event counting prohibited? */
- if (el == 2 && (counter < hpmn || counter == 31)) {
- prohibited = mdcr_el2 & MDCR_HPMD;
- }
- if (secure) {
- prohibited = prohibited || !(env->cp15.mdcr_el3 & MDCR_SPME);
- }
-
- if (counter == 31) {
- /*

-         * The cycle counter defaults to running. PMCR.DP says "disable
-         * the cycle counter when event counting is prohibited".
-         * Some MDCR bits disable the cycle counter specifically.
-         */

- prohibited = prohibited && env->cp15.c9_pmcr & PMCRDP;
- if (cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
- if (secure) {
- prohibited = prohibited || (env->cp15.mdcr_el3 & MDCR_SCCD);
- }
- if (el == 2) {
- prohibited = prohibited || (mdcr_el2 & MDCR_HCCD);
- }
- }
- }
+ enabled = (env->cp15.c9_pmcr & PMCRE) &&
+ (env->cp15.c9_pmcnten & (1 << counter));

     if (counter == 31) {
         filter = env->cp15.pmccfiltr_el0;
     } else {
         filter = env->cp15.c14_pmevtyper[counter];
     }
-    p   = filter & PMXEVTYPER_P;
-    u   = filter & PMXEVTYPER_U;
-    nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
-    nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
-    nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
-    m   = arm_el_is_aa64(env, 1) &&
-              arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
-
     if (el == 0) {
-        filtered = secure ? u : u != nsu;
+        filtered = filter & PMXEVTYPER_U;
     } else if (el == 1) {

- filtered = secure ? p : p != nsk;
- } else if (el == 2) {
- filtered = !nsh;
- } else { /* EL3 */
- filtered = m != p;

+        filtered = filter & PMXEVTYPER_P;
     }

     if (counter != 31) {
         /*
          * If not checking PMCCNTR, ensure the counter is setup to an 
event we
          * support
          */
         uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
-        if (!event_supported(event)) {
+        if (!pmu_event_supported(event)) {
             return false;
         }
     }

- return enabled && !prohibited && !filtered;
+ return enabled && !filtered;
}

I could link HVF without PMU a few surgery such:
---
@@ -1493,12 +1486,12 @@ static int hvf_sysreg_write(CPUState *cpu, uint32_t reg, uint64_t val)

     switch (reg) {
     case SYSREG_PMCCNTR_EL0:
-        pmu_op_start(env);
+        pmccntr_op_start(env);
         env->cp15.c15_ccnt = val;
-        pmu_op_finish(env);
+        pmccntr_op_finish(env);
         break;
     case SYSREG_PMCR_EL0:

- pmu_op_start(env);
+ pmccntr_op_start(env);

---

I'll try to split as:

- target/arm/pmu_common_helper.c (?)
- target/arm/pmc_helper.c
- target/arm/tcg/pmu_helper.c

Ideally pmu_counter_enabled() should be unified, but I don't feel confident enough to do it.

Regards,

Phil.

#9 lore raw

PATCH v2 3/5 util/async: Only call icount_notify_exit() if icount is enabled

In reply to Philippe Mathieu-Daudé
---
 stubs/icount.c |  2 +-
 util/async.c   | 16 +++++++++-------
 2 files changed, 10 insertions(+), 8 deletions(-)
stubs/icount.c +1 -1
index f8e6a014b8..a5202e2dd9 100644
@@ -43,7 +43,7 @@ void icount_account_warp_timer(void)
 {
     abort();
 }
-
 void icount_notify_exit(void)
 {
+    abort();
 }
util/async.c +9 -7
index 8f90ddc304..9007642c27 100644
@@ -94,13 +94,15 @@ static void aio_bh_enqueue(QEMUBH *bh, unsigned new_flags)
     }
 
     aio_notify(ctx);
-    /*
-     * Workaround for record/replay.
-     * vCPU execution should be suspended when new BH is set.
-     * This is needed to avoid guest timeouts caused
-     * by the long cycles of the execution.
-     */
-    icount_notify_exit();
+    if (unlikely(icount_enabled())) {
+        /*
+         * Workaround for record/replay.
+         * vCPU execution should be suspended when new BH is set.
+         * This is needed to avoid guest timeouts caused
+         * by the long cycles of the execution.
+         */
+        icount_notify_exit();
+    }
 }
 
 /* Only called from aio_bh_poll() and aio_ctx_finalize() */
Signature · 1 line
2.41.0
#10 lore raw

Re: [PATCH v2 3/5] util/async: Only call icount_notify_exit() if icount is enabled

Quoted 33 lines · Philippe Mathieu-Daudé
---
  stubs/icount.c |  2 +-
  util/async.c   | 16 +++++++++-------
  2 files changed, 10 insertions(+), 8 deletions(-)
stubs/icount.c +1 -1
index f8e6a014b8..a5202e2dd9 100644
@@ -43,7 +43,7 @@ void icount_account_warp_timer(void)
  {
      abort();
  }
-
  void icount_notify_exit(void)
  {
+    abort();
  }
util/async.c +0 -6
index 8f90ddc304..9007642c27 100644
@@ -94,13 +94,15 @@ static void aio_bh_enqueue(QEMUBH *bh, unsigned new_flags)
      }
 
      aio_notify(ctx);
-    /*
-     * Workaround for record/replay.
-     * vCPU execution should be suspended when new BH is set.
-     * This is needed to avoid guest timeouts caused
-     * by the long cycles of the execution.
-     */

util/async.c static void aio_bh_enqueue(QEMUBH *bh, unsigned new_flags) · line 105

Quoted 10 lines
util/async.c +9 -1
-    icount_notify_exit();
+    if (unlikely(icount_enabled())) {
+        /*
+         * Workaround for record/replay.
+         * vCPU execution should be suspended when new BH is set.
+         * This is needed to avoid guest timeouts caused
+         * by the long cycles of the execution.
+         */
+        icount_notify_exit();
+    }

If you're going to do this, remove the test in the non-stub icount_notify_exit.

r~

#11 lore raw

PATCH v2 4/5 system/vl: Restrict icount to TCG emulation

In reply to Philippe Mathieu-Daudé
---
 stubs/icount.c | 6 ------
 system/vl.c    | 6 +++++-
 2 files changed, 5 insertions(+), 7 deletions(-)
stubs/icount.c +0 -6
index a5202e2dd9..b060b03a73 100644
@@ -1,5 +1,4 @@
 #include "qemu/osdep.h"
-#include "qapi/error.h"
 #include "sysemu/cpu-timers.h"
 
 /* icount - Instruction Counter API */
@@ -10,11 +9,6 @@ void icount_update(CPUState *cpu)
 {
     abort();
 }
-void icount_configure(QemuOpts *opts, Error **errp)
-{
-    /* signal error */
-    error_setg(errp, "cannot configure icount, TCG support not available");
-}
 int64_t icount_get_raw(void)
 {
     abort();
system/vl.c +5 -1
index 2bcd9efb9a..8c99c5f681 100644
@@ -2270,7 +2270,11 @@ static void user_register_global_props(void)
 
 static int do_configure_icount(void *opaque, QemuOpts *opts, Error **errp)
 {
-    icount_configure(opts, errp);
+    if (tcg_enabled()) {
+        icount_configure(opts, errp);
+    } else {
+        error_setg(errp, "cannot configure icount, TCG support not available");
+    }
     return 0;
 }
 
Signature · 1 line
2.41.0
#12 lore raw

Re: [PATCH v2 4/5] system/vl: Restrict icount to TCG emulation

Quoted 34 lines · Philippe Mathieu-Daudé
---
  stubs/icount.c | 6 ------
  system/vl.c    | 6 +++++-
  2 files changed, 5 insertions(+), 7 deletions(-)
stubs/icount.c +0 -6
index a5202e2dd9..b060b03a73 100644
@@ -1,5 +1,4 @@
  #include "qemu/osdep.h"
-#include "qapi/error.h"
  #include "sysemu/cpu-timers.h"
 
  /* icount - Instruction Counter API */
@@ -10,11 +9,6 @@ void icount_update(CPUState *cpu)
  {
      abort();
  }
-void icount_configure(QemuOpts *opts, Error **errp)
-{
-    /* signal error */
-    error_setg(errp, "cannot configure icount, TCG support not available");
-}
  int64_t icount_get_raw(void)
  {
      abort();
system/vl.c +0 -0
index 2bcd9efb9a..8c99c5f681 100644
@@ -2270,7 +2270,11 @@ static void user_register_global_props(void)
 

system/vl.c static void user_register_global_props(void) · line 2279

Quoted 10 lines
system/vl.c +5 -1
  static int do_configure_icount(void *opaque, QemuOpts *opts, Error **errp)
  {
-    icount_configure(opts, errp);
+    if (tcg_enabled()) {
+        icount_configure(opts, errp);
+    } else {
+        error_setg(errp, "cannot configure icount, TCG support not available");
+    }
      return 0;
  }

This is called before the accelerator is chosen -- even before the set of available accelerators is even found. Indeed, that's the very next thing that configure_accelerators does.

OTOH, I don't see why icount_configure is being called so early.

r~

Quoted 1 line
#13 lore raw

Re: [PATCH v2 4/5] system/vl: Restrict icount to TCG emulation

Quoted 48 lines · Richard Henderson
Quoted 34 lines · Philippe Mathieu-Daudé

---
  stubs/icount.c | 6 ------
  system/vl.c    | 6 +++++-

  2 files changed, 5 insertions(+), 7 deletions(-)
stubs/icount.c +0 -6
index a5202e2dd9..b060b03a73 100644
@@ -1,5 +1,4 @@
   #include "qemu/osdep.h"
-#include "qapi/error.h"
   #include "sysemu/cpu-timers.h"
   /* icount - Instruction Counter API */
@@ -10,11 +9,6 @@ void icount_update(CPUState *cpu)
   {
       abort();
   }
-void icount_configure(QemuOpts *opts, Error **errp)
-{
-    /* signal error */
-    error_setg(errp, "cannot configure icount, TCG support not 
 available");
-}
   int64_t icount_get_raw(void)
   {

      abort();

system/vl.c +0 -0
index 2bcd9efb9a..8c99c5f681 100644
@@ -2270,7 +2270,11 @@ static void user_register_global_props(void)
   static int do_configure_icount(void *opaque, QemuOpts *opts, Error **errp)

system/vl.c static void user_register_global_props(void) · line 2279

Quoted 10 lines
system/vl.c +5 -1
   {
-    icount_configure(opts, errp);
+    if (tcg_enabled()) {
+        icount_configure(opts, errp);
+    } else {
+        error_setg(errp, "cannot configure icount, TCG support not 
 available");
+    }
       return 0;
   }

This is called before the accelerator is chosen -- even before the set of available accelerators is even found.  Indeed, that's the very next thing that configure_accelerators does.

Quoted 1 line

OTOH, I don't see why icount_configure is being called so early.

See commit 7f8b6126e7:

vl: move icount configuration earlier

Once qemu_tcg_configure is turned into a QOM property setter, it will not be able to set a default value for mttcg_enabled. Setting the default will move to the TCG instance_init function, which currently runs before "-icount" is processed.

However, it is harmless to do configure_icount for all accelerators; we will just fail later if a non-TCG accelerator is selected. So do that.

But few commits after we have 28a0961757 ("vl: merge -accel processing into configure_accelerators"), so it shouldn't be an issue now. I'll consolidate.

#14 lore raw

PATCH v2 5/5 sysemu/replay: Restrict icount to system emulation

In reply to Philippe Mathieu-Daudé
---
 include/sysemu/cpu-timers.h |  2 +-
 include/sysemu/replay.h     | 11 ++++++++---
 stubs/icount.c              | 19 -------------------
 3 files changed, 9 insertions(+), 23 deletions(-)
include/sysemu/cpu-timers.h +1 -1
index e909ffae47..25dfc76599 100644
@@ -30,7 +30,7 @@ typedef enum {
     ICOUNT_ADAPTATIVE = 2,
 } ICountMode;
 
-#ifdef CONFIG_TCG
+#if defined(CONFIG_TCG) && !defined(CONFIG_USER_ONLY)
 extern ICountMode use_icount;
 #define icount_enabled() (use_icount)
 #else
include/sysemu/replay.h +8 -3
index 08aae5869f..8102fa54f0 100644
@@ -1,6 +1,3 @@
-#ifndef SYSEMU_REPLAY_H
-#define SYSEMU_REPLAY_H
-
 /*
  * QEMU replay (system interface)
  *
@@ -11,6 +8,12 @@
  * See the COPYING file in the top-level directory.
  *
  */
+#ifndef SYSEMU_REPLAY_H
+#define SYSEMU_REPLAY_H
+
+#ifdef CONFIG_USER_ONLY
+#error Cannot include this header from user emulation
+#endif
 
 #include "exec/replay-core.h"
 #include "qapi/qapi-types-misc.h"
@@ -79,12 +82,14 @@ int64_t replay_save_clock(ReplayClockKind kind, int64_t clock,
 int64_t replay_read_clock(ReplayClockKind kind, int64_t raw_icount);
 /*! Saves or reads the clock depending on the current replay mode. */
 #define REPLAY_CLOCK(clock, value)                                      \
+    !icount_enabled() ? (value) :                                       \
     (replay_mode == REPLAY_MODE_PLAY                                    \
         ? replay_read_clock((clock), icount_get_raw())                  \
         : replay_mode == REPLAY_MODE_RECORD                             \
             ? replay_save_clock((clock), (value), icount_get_raw())     \
             : (value))
 #define REPLAY_CLOCK_LOCKED(clock, value)                               \
+    !icount_enabled() ? (value) :                                       \
     (replay_mode == REPLAY_MODE_PLAY                                    \
         ? replay_read_clock((clock), icount_get_raw_locked())           \
         : replay_mode == REPLAY_MODE_RECORD                             \
stubs/icount.c +0 -19
index b060b03a73..9a29084ecc 100644
@@ -5,30 +5,11 @@
 
 ICountMode use_icount = ICOUNT_DISABLED;
 
-void icount_update(CPUState *cpu)
-{
-    abort();
-}
 int64_t icount_get_raw(void)
 {
     abort();
     return 0;
 }
-int64_t icount_get(void)
-{
-    abort();
-    return 0;
-}
-int64_t icount_to_ns(int64_t icount)
-{
-    abort();
-    return 0;
-}
-int64_t icount_round(int64_t count)
-{
-    abort();
-    return 0;
-}
 void icount_start_warp_timer(void)
 {
     abort();
Signature · 1 line
2.41.0