Skip to contentSkip to conversation

target/arm: a few timer fixes while checking FEAT_ECV_POFF

PATCH Applied Alex Bennée 3 participants 9 messages Jun 24 – Jun 26

lore
Share & tools
lore
#0 lore raw

v3 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.

v3
- tweaked the FEATURE_EL3 check per Richard

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           | 33 ++++++++++++++++++++++++++++-----
 2 files changed, 29 insertions(+), 5 deletions(-)
Signature · 1 line
2.47.3
#1 lore raw

PATCH v3 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 a234aa031c0..3d52cdc4a5e 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 v3 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 3d52cdc4a5e..3f0707126b5 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 v3 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 3f0707126b5..9b843857e5d 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 v3 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, if it is then we check SCR_ECVEN allows tweaking the offset.

---
v2
- don't bail early - if there is no EL3 we still honour CNTHCTL_EL2

---
 target/arm/helper.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
target/arm/helper.c +4 -4
index 9b843857e5d..4273e3c5395 100644
@@ -1413,10 +1413,10 @@ void gt_rme_post_el_change(ARMCPU *cpu, void *ignored)
 
 static uint64_t gt_phys_raw_cnt_offset(CPUARMState *env)
 {
-    if ((env->cp15.scr_el3 & SCR_ECVEN) &&
-        FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, ECV) &&
-        arm_is_el2_enabled(env) &&
-        (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
+    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)
+        && (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
         return env->cp15.cntpoff_el2;
     }
     return 0;
Signature · 1 line
2.47.3
Reviewed-by on v3 4/6
#5 lore raw

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

Quoted 6 lines · Alex Bennée

We shouldn't be reading SCR_EL3 unless ARM_FEATURE_EL3 is enabled, if it is then we check SCR_ECVEN allows tweaking the offset.

Quoted 6 lines

---
v2
- don't bail early - if there is no EL3 we still honour CNTHCTL_EL2

---
  target/arm/helper.c | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)

r~

#6 lore raw

PATCH v3 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 4273e3c5395..b163dc5764f 100644
@@ -3716,6 +3716,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 */
@@ -3807,6 +3808,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;
 
     /*
@@ -3828,6 +3833,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 v3 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 a8072ddb671..2faed5c289b 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
#8 lore raw
In reply to Alex Bennée
Quoted 8 lines · Alex Bennée

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.

v3
- tweaked the FEATURE_EL3 check per Richard

Applied to target-arm.next, thanks.

-- PMM