Skip to contentSkip to conversation

target/arm: a few timer fixes while checking FEAT_ECV_POFF

PATCH Superseded Alex Bennée 2 participants 8 messages Jun 10

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover target/arm: a few timer fixes while checking FEAT_ECV_POFF

While I was checking what was needed for FEAT_ECV_POFF I discovered a few potential correctness issues. We had actually already done the work for the feature so the final patch is just documentation.

v2
- added target/arm: gate check on scr_el3 behind ARM_FEATURE_EL3 check
- ensure gt_recalc_timer has updated hcr_el2

The following still need review:

target/arm: gate check on scr_el3 behind ARM_FEATURE_EL3 check

Alex.

Alex Bennée (6):
target/arm: split evaluation of CNTHCTL timer IRQ masks target/arm: trigger timer recalculation when toggling CNTHCTL:ECV target/arm: trigger timer recalc on SCR:ECVEN change target/arm: gate check on scr_el3 behind ARM_FEATURE_EL3 check target/arm: trigger timer recalc on HCR:(E2H|TGE) changes docs/system: add FEAT_ECV_POFF to the emulation list

 docs/system/arm/emulation.rst |  1 +
 target/arm/helper.c           | 29 ++++++++++++++++++++++++++++-
 2 files changed, 29 insertions(+), 1 deletion(-)
Signature · 1 line
2.47.3
#1 lore raw

PATCH v2 1/6 target/arm: split evaluation of CNTHCTL timer IRQ masks

Whether the physical and virtual timer IRQs are masked are independent of each other so the checking of CNTHCTL:CNTPMASK shouldn't depend of not changing CNTVMASK.

While unlikely to be seen in real life we should still make sure we behave correctly.

---
 target/arm/helper.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
target/arm/helper.c +2 -1
index 9dd8fdfa41b..f4aae0a4072 100644
@@ -1802,7 +1802,8 @@ static void gt_cnthctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
 
     if ((oldval ^ value) & R_CNTHCTL_CNTVMASK_MASK) {
         gt_update_irq(cpu, GTIMER_VIRT);
-    } else if ((oldval ^ value) & R_CNTHCTL_CNTPMASK_MASK) {
+    }
+    if ((oldval ^ value) & R_CNTHCTL_CNTPMASK_MASK) {
         gt_update_irq(cpu, GTIMER_PHYS);
     }
 }
Signature · 1 line
2.47.3
#2 lore raw

PATCH v2 2/6 target/arm: trigger timer recalculation when toggling CNTHCTL:ECV

In reply to Alex Bennée

When toggling the state of ECV we affect the offset applied to timers. As a result we should trigger a recalculation of the timer value to take into account the new offset.

---
 target/arm/helper.c | 3 +++
 1 file changed, 3 insertions(+)
target/arm/helper.c +3 -0
index f4aae0a4072..5c640853060 100644
@@ -1806,6 +1806,9 @@ static void gt_cnthctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
     if ((oldval ^ value) & R_CNTHCTL_CNTPMASK_MASK) {
         gt_update_irq(cpu, GTIMER_PHYS);
     }
+    if ((oldval ^ value) & R_CNTHCTL_ECV_MASK) {
+        gt_recalc_timer(cpu, GTIMER_PHYS);
+    }
 }
 
 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
Signature · 1 line
2.47.3
#3 lore raw

PATCH v2 3/6 target/arm: trigger timer recalc on SCR:ECVEN change

In reply to Alex Bennée

Toggling the ECVEN state affects the offset calculated in gt_phys_raw_cnt_offset so we should trigger a re-calculation on its change.

---
 target/arm/helper.c | 9 +++++++++
 1 file changed, 9 insertions(+)
target/arm/helper.c +9 -0
index 5c640853060..22f5117ee54 100644
@@ -37,6 +37,9 @@
 #include "qemu/plugin.h"
 
 static void switch_mode(CPUARMState *env, int mode);
+#ifndef CONFIG_USER_ONLY
+static void gt_recalc_timer(ARMCPU *cpu, int timeridx);
+#endif
 
 int compare_u64(const void *a, const void *b)
 {
@@ -821,6 +824,12 @@ static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
     changed = env->cp15.scr_el3 ^ value;
     env->cp15.scr_el3 = value;
 
+#ifndef CONFIG_USER_ONLY
+    if (changed & SCR_ECVEN) {
+        gt_recalc_timer(cpu, GTIMER_PHYS);
+    }
+#endif
+
     /*
      * If SCR_EL3.{NS,NSE} changes, i.e. change of security state,
      * we must invalidate all TLBs below EL3.
Signature · 1 line
2.47.3
#4 lore raw

PATCH v2 4/6 target/arm: gate check on scr_el3 behind ARM_FEATURE_EL3 check

In reply to Alex Bennée

We shouldn't be reading SCR_EL3 unless ARM_FEATURE_EL3 is enabled for this system. If it is not there cannot be any offset so just return 0.

---
 target/arm/helper.c | 4 ++++
 1 file changed, 4 insertions(+)
target/arm/helper.c +4 -0
index 22f5117ee54..8946d2aad28 100644
@@ -1413,6 +1413,10 @@ void gt_rme_post_el_change(ARMCPU *cpu, void *ignored)
 
 static uint64_t gt_phys_raw_cnt_offset(CPUARMState *env)
 {
+    if (!arm_feature(env, ARM_FEATURE_EL3)) {
+        return 0;
+    }
+
     if ((env->cp15.scr_el3 & SCR_ECVEN) &&
         FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, ECV) &&
         arm_is_el2_enabled(env) &&
Signature · 1 line
2.47.3
#5 lore raw

Re: [PATCH v2 4/6] target/arm: gate check on scr_el3 behind ARM_FEATURE_EL3 check

Quoted 14 lines · Alex Bennée

We shouldn't be reading SCR_EL3 unless ARM_FEATURE_EL3 is enabled for this system. If it is not there cannot be any offset so just return 0.

---
  target/arm/helper.c | 4 ++++
  1 file changed, 4 insertions(+)
target/arm/helper.c +0 -0
index 22f5117ee54..8946d2aad28 100644

target/arm/helper.c void gt_rme_post_el_change(ARMCPU *cpu, void *ignored) · line 1422

Quoted 10 lines
target/arm/helper.c +4 -0
@@ -1413,6 +1413,10 @@ void gt_rme_post_el_change(ARMCPU *cpu, void *ignored)
 
  static uint64_t gt_phys_raw_cnt_offset(CPUARMState *env)
  {
+    if (!arm_feature(env, ARM_FEATURE_EL3)) {
+        return 0;
+    }
+
      if ((env->cp15.scr_el3 & SCR_ECVEN) &&
          FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, ECV) &&
          arm_is_el2_enabled(env) &&

No, like this:

    if ((!arm_feature(env, ARM_FEATURE_EL3) || (env->cp15.scr_el3 & SCR_ECVEN))
        && FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, ECV)
        && arm_is_el2_enabled(env)
        ...

r~

#6 lore raw

PATCH v2 5/6 target/arm: trigger timer recalc on HCR:(E2H|TGE) changes

In reply to Alex Bennée

Toggling the HCR state affects the offset calculated in gt_phys_raw_cnt_offset so we should trigger a re-calculation on its change.

---
v2
- defer calling gt_recalc_timer until after we set hcr_el2

---
 target/arm/helper.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
target/arm/helper.c +10 -0
index 8946d2aad28..f35fe83723c 100644
@@ -3712,6 +3712,7 @@ static const ARMCPRegInfo v8_aa32_el1_reginfo[] = {
 static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
 {
     ARMCPU *cpu = env_archcpu(env);
+    bool hcr_change_timer;
 
     if (arm_feature(env, ARM_FEATURE_V8)) {
         valid_mask |= MAKE_64BIT_MASK(0, 34);  /* ARMv8.0 */
@@ -3803,6 +3804,10 @@ static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
         (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT | HCR_FWB | HCR_NV | HCR_NV1)) {
         tlb_flush(CPU(cpu));
     }
+    hcr_change_timer = (env->cp15.hcr_el2 ^ value) &
+                       (HCR_E2H | HCR_TGE);
+
+    /* update */
     env->cp15.hcr_el2 = value;
 
     /*
@@ -3824,6 +3829,11 @@ static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
         arm_cpu_update_vinmi(cpu);
         arm_cpu_update_vfnmi(cpu);
     }
+    if (hcr_change_timer) {
+#ifndef CONFIG_USER_ONLY
+        gt_recalc_timer(cpu, GTIMER_PHYS);
+#endif
+    }
 }
 
 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
Signature · 1 line
2.47.3
#7 lore raw

PATCH v2 6/6 docs/system: add FEAT_ECV_POFF to the emulation list

In reply to Alex Bennée

We already had this implemented since 2808d3b38a5 (target/arm:
Implement FEAT_ECV CNTPOFF_EL2 handling) but it has its own feature name now. Add it to the list.

---
 docs/system/arm/emulation.rst | 1 +
 1 file changed, 1 insertion(+)
docs/system/arm/emulation.rst +1 -0
index 18c63559679..3622e3889c7 100644
@@ -60,6 +60,7 @@ the following architecture extensions:
 - FEAT_E2H0 (Programming of HCR_EL2.E2H)
 - FEAT_EBF16 (AArch64 Extended BFloat16 instructions)
 - FEAT_ECV (Enhanced Counter Virtualization)
+- FEAT_ECV_POFF (Enhanced Counter Virtualization Physical Offset)
 - FEAT_EL0 (Support for execution at EL0)
 - FEAT_EL1 (Support for execution at EL1)
 - FEAT_EL2 (Support for execution at EL2)
Signature · 1 line
2.47.3