Skip to contentSkip to conversation

target/arm: implement SEL2 physical and virtual timers

PATCH Superseded Alex Bennée 2 participants 10 messages Dec 6, 2024 – Dec 18, 2024

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover target/arm: implement SEL2 physical and virtual timers

Follow Peter's review I've split this into a several patches as there are some other fixes that should be made to other EL2 times that shouldn't be rolled together.

v1
- improve GTIMER docs
- fix gt_recalc bug
- address review comments for the main patch
- cc qemu-stable (no rush for 9.2.0)

Please review.

Alex.

Alex Bennée (3):
target/arm: document the architectural names of our GTIMERs target/arm: ensure cntvoff_el2 also used for EL2 virt timer target/arm: implement SEL2 physical and virtual timers

 include/hw/arm/bsa.h |   2 +
 target/arm/cpu.h     |   2 +
 target/arm/gtimer.h  |  14 ++--
 hw/arm/sbsa-ref.c    |   2 +
 hw/arm/virt.c        |   2 +
 target/arm/cpu.c     |   4 +
 target/arm/helper.c  | 179 +++++++++++++++++++++++++++++++++++++++++--
 7 files changed, 194 insertions(+), 11 deletions(-)
Signature · 1 line
2.39.5
#1 lore raw

PATCH 1/3 target/arm: document the architectural names of our GTIMERs

As we are about to add more physical and virtual timers lets make it clear what each timer does.

---
 target/arm/gtimer.h | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
target/arm/gtimer.h +5 -5
index b992941bef..de016e6da3 100644
@@ -10,11 +10,11 @@
 #define TARGET_ARM_GTIMER_H
 
 enum {
-    GTIMER_PHYS     = 0,
-    GTIMER_VIRT     = 1,
-    GTIMER_HYP      = 2,
-    GTIMER_SEC      = 3,
-    GTIMER_HYPVIRT  = 4,
+    GTIMER_PHYS     = 0, /* EL1 physical timer */
+    GTIMER_VIRT     = 1, /* EL1 virtual timer */
+    GTIMER_HYP      = 2, /* EL2 physical timer */
+    GTIMER_SEC      = 3, /* EL3 physical timer */
+    GTIMER_HYPVIRT  = 4, /* EL2 virtual timer */
 #define NUM_GTIMERS   5
 };
 
Signature · 1 line
2.39.5
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] target/arm: document the architectural names of our GTIMERs

Quoted 20 lines · Alex Bennée

As we are about to add more physical and virtual timers lets make it clear what each timer does.

---
 target/arm/gtimer.h | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
target/arm/gtimer.h +0 -2
index b992941bef..de016e6da3 100644
@@ -10,11 +10,11 @@
 #define TARGET_ARM_GTIMER_H
 
 enum {
-    GTIMER_PHYS     = 0,
-    GTIMER_VIRT     = 1,

target/arm/gtimer.h line 19

Quoted 10 lines
target/arm/gtimer.h +5 -3
-    GTIMER_HYP      = 2,
-    GTIMER_SEC      = 3,
-    GTIMER_HYPVIRT  = 4,
+    GTIMER_PHYS     = 0, /* EL1 physical timer */
+    GTIMER_VIRT     = 1, /* EL1 virtual timer */
+    GTIMER_HYP      = 2, /* EL2 physical timer */
+    GTIMER_SEC      = 3, /* EL3 physical timer */
+    GTIMER_HYPVIRT  = 4, /* EL2 virtual timer */
 #define NUM_GTIMERS   5
 };

thanks
-- PMM

#3 lore raw

PATCH 2/3 target/arm: ensure cntvoff_el2 also used for EL2 virt timer

In reply to Alex Bennée

We were missing this case and will shortly be adding another. Re-arrange the code and use a switch statement to group the virtual timers.

---
 target/arm/helper.c | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)
target/arm/helper.c +16 -5
index f38eb054c0..cd147b717a 100644
@@ -2732,16 +2732,27 @@ static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
     ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
 
     if (gt->ctl & 1) {
+        uint64_t count = gt_get_countervalue(&cpu->env);
+        uint64_t offset;
+        uint64_t nexttick;
+        int istatus;
+
         /*
          * Timer enabled: calculate and set current ISTATUS, irq, and
          * reset timer to when ISTATUS next has to change
          */
-        uint64_t offset = timeridx == GTIMER_VIRT ?
-            cpu->env.cp15.cntvoff_el2 : gt_phys_raw_cnt_offset(&cpu->env);
-        uint64_t count = gt_get_countervalue(&cpu->env);
+        switch (timeridx) {
+        case GTIMER_VIRT:
+        case GTIMER_HYPVIRT:
+            offset = cpu->env.cp15.cntvoff_el2;
+            break;
+        default:
+            offset =gt_phys_raw_cnt_offset(&cpu->env);
+            break;
+        }
+
         /* Note that this must be unsigned 64 bit arithmetic: */
-        int istatus = count - offset >= gt->cval;
-        uint64_t nexttick;
+        istatus = count - offset >= gt->cval;
 
         gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
 
Signature · 1 line
2.39.5
#4 lore raw

PATCH 3/3 target/arm: implement SEL2 physical and virtual timers

In reply to Alex Bennée

When FEAT_SEL2 was implemented the SEL2 timers where missed. This shows up when building the latest Hafnium with SPMC_AT_EL=2. The actual implementation utilises the same logic as the rest of the timers so all we need to do is:

- define the timers and their access functions
- conditionally add the correct system registers
- create a new accessfn as the rules are subtly different to the existing secure timer

Fixes e9152ee91c (target/arm: add ARMv8.4-SEL2 system registers)
Cc Andrei Homescu <[email protected]>
Cc Arve Hjønnevåg <[email protected]>
Cc Rémi Denis-Courmont <[email protected]>

---
v1
- add better comments to GTIMER descriptions
- also define new timers for sbsa-ref
- don't conditionally gate qemu_timer creation on the feature
- take cntvoff_el2 int account for SEC_VEL2 in gt_recalc/g_tval_[read|write]

---
 include/hw/arm/bsa.h |   2 +
 target/arm/cpu.h     |   2 +
 target/arm/gtimer.h  |   4 +-
 hw/arm/sbsa-ref.c    |   2 +
 hw/arm/virt.c        |   2 +
 target/arm/cpu.c     |   4 ++
 target/arm/helper.c  | 158 +++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 173 insertions(+), 1 deletion(-)
include/hw/arm/bsa.h +2 -0
index 8eaab603c0..b4ecca1b1c 100644
@@ -22,6 +22,8 @@
 #define QEMU_ARM_BSA_H
 
 /* These are architectural INTID values */
+#define ARCH_TIMER_S_VIRT_EL2_IRQ  19
+#define ARCH_TIMER_S_EL2_IRQ       20
 #define VIRTUAL_PMU_IRQ            23
 #define ARCH_GIC_MAINT_IRQ         25
 #define ARCH_TIMER_NS_EL2_IRQ      26
target/arm/cpu.h +2 -0
index d86e641280..10b5354d6f 100644
@@ -1139,6 +1139,8 @@ void arm_gt_vtimer_cb(void *opaque);
 void arm_gt_htimer_cb(void *opaque);
 void arm_gt_stimer_cb(void *opaque);
 void arm_gt_hvtimer_cb(void *opaque);
+void arm_gt_sel2timer_cb(void *opaque);
+void arm_gt_sel2vtimer_cb(void *opaque);
 
 unsigned int gt_cntfrq_period_ns(ARMCPU *cpu);
 void gt_rme_post_el_change(ARMCPU *cpu, void *opaque);
target/arm/gtimer.h +3 -1
index de016e6da3..f8f7425a5f 100644
@@ -15,7 +15,9 @@ enum {
     GTIMER_HYP      = 2, /* EL2 physical timer */
     GTIMER_SEC      = 3, /* EL3 physical timer */
     GTIMER_HYPVIRT  = 4, /* EL2 virtual timer */
-#define NUM_GTIMERS   5
+    GTIMER_SEC_PEL2 = 5, /* Secure EL2 physical timer */
+    GTIMER_SEC_VEL2 = 6, /* Secure EL2 virtual timer */
+#define NUM_GTIMERS   7
 };
 
 #endif
hw/arm/sbsa-ref.c +2 -0
index e3195d5449..5dc36b3cee 100644
@@ -484,6 +484,8 @@ static void create_gic(SBSAMachineState *sms, MemoryRegion *mem)
             [GTIMER_HYP]  = ARCH_TIMER_NS_EL2_IRQ,
             [GTIMER_SEC]  = ARCH_TIMER_S_EL1_IRQ,
             [GTIMER_HYPVIRT] = ARCH_TIMER_NS_EL2_VIRT_IRQ,
+            [GTIMER_SEC_PEL2] = ARCH_TIMER_S_EL2_IRQ,
+            [GTIMER_SEC_VEL2] = ARCH_TIMER_S_VIRT_EL2_IRQ,
         };
 
         for (irq = 0; irq < ARRAY_SIZE(timer_irq); irq++) {
hw/arm/virt.c +2 -0
index 1a381e9a2b..451d154459 100644
@@ -873,6 +873,8 @@ static void create_gic(VirtMachineState *vms, MemoryRegion *mem)
             [GTIMER_HYP]  = ARCH_TIMER_NS_EL2_IRQ,
             [GTIMER_SEC]  = ARCH_TIMER_S_EL1_IRQ,
             [GTIMER_HYPVIRT] = ARCH_TIMER_NS_EL2_VIRT_IRQ,
+            [GTIMER_SEC_PEL2] = ARCH_TIMER_S_EL2_IRQ,
+            [GTIMER_SEC_VEL2] = ARCH_TIMER_S_VIRT_EL2_IRQ,
         };
 
         for (unsigned irq = 0; irq < ARRAY_SIZE(timer_irq); irq++) {
target/arm/cpu.c +4 -0
index 6938161b95..d15916c436 100644
@@ -2078,6 +2078,10 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
                                               arm_gt_stimer_cb, cpu);
         cpu->gt_timer[GTIMER_HYPVIRT] = timer_new(QEMU_CLOCK_VIRTUAL, scale,
                                                   arm_gt_hvtimer_cb, cpu);
+        cpu->gt_timer[GTIMER_SEC_PEL2] = timer_new(QEMU_CLOCK_VIRTUAL, scale,
+                                                   arm_gt_sel2timer_cb, cpu);
+        cpu->gt_timer[GTIMER_SEC_VEL2] = timer_new(QEMU_CLOCK_VIRTUAL, scale,
+                                                   arm_gt_sel2vtimer_cb, cpu);
     }
 #endif
 
target/arm/helper.c +158 -0

200 lines not on this page · raw message

Signature · 1 line
2.39.5
#5 lore raw
Quoted 24 lines · Alex Bennée

When FEAT_SEL2 was implemented the SEL2 timers where missed. This shows up when building the latest Hafnium with SPMC_AT_EL=2. The actual implementation utilises the same logic as the rest of the timers so all we need to do is:

- define the timers and their access functions
- conditionally add the correct system registers
- create a new accessfn as the rules are subtly different to the existing secure timer

Fixes e9152ee91c (target/arm: add ARMv8.4-SEL2 system registers)
Cc Andrei Homescu <[email protected]>
Cc Arve Hjønnevåg <[email protected]>
Cc Rémi Denis-Courmont <[email protected]>

---
v1
- add better comments to GTIMER descriptions
- also define new timers for sbsa-ref
- don't conditionally gate qemu_timer creation on the feature
- take cntvoff_el2 int account for SEC_VEL2 in gt_recalc/g_tval_[read|write]

Quoted 6 lines
---
 include/hw/arm/bsa.h |   2 +
 target/arm/cpu.h     |   2 +
 target/arm/gtimer.h  |   4 +-
 hw/arm/sbsa-ref.c    |   2 +
 hw/arm/virt.c        |   2 +

I would put the board changes in their own patch(es).

Quoted 28 lines
target/arm/helper.c +20 -0
index cd147b717a..f82503304e 100644
@@ -2668,6 +2668,41 @@ static CPAccessResult gt_stimer_access(CPUARMState *env,
     }
 }
 
+static CPAccessResult gt_sel2timer_access(CPUARMState *env,
+                                          const ARMCPRegInfo *ri,
+                                          bool isread)
+{
+    /*
+     * The AArch64 register view of the secure EL2 timers are mostly
+     * accessible from EL3 and EL2 although can also be trapped to EL2
+     * from EL1 depending on nested virt config.
+     */
+    switch (arm_current_el(env)) {
+    case 0:
+        return CP_ACCESS_TRAP;
+    case 1:
+        if (!arm_is_secure(env)) {
+            return CP_ACCESS_TRAP;
+        } else if (arm_hcr_el2_eff(env) & HCR_NV) {
+            return CP_ACCESS_TRAP_EL2;
+        }
+        return CP_ACCESS_TRAP;
+    case 2:

target/arm/helper.c static CPAccessResult gt_stimer_access(CPUARMState *env, · line 2700

Quoted 10 lines
target/arm/helper.c +10 -0
+        if (!arm_is_secure(env)) {
+            return CP_ACCESS_TRAP;
+        }
+        return CP_ACCESS_OK;
+    case 3:
+        if (env->cp15.scr_el3 & SCR_EEL2) {
+            return CP_ACCESS_OK;
+        } else {
+            return CP_ACCESS_TRAP;
+        }

These should generally be using CP_ACCESS_TRAP_UNCATEGORIZED, not CP_ACCESS_TRAP. The pseudocode uses "UNDEF", which means it wants ESR to be reported as an uncategorized-exception (classic UNDEF), not as a "trapped system register access".

Almost always a trapped-sysreg-access is directed to a specific EL; an UNDEF is never directed to a specific EL but always to the usual destination for exceptions. I should probably check whether the other uses of CP_ACCESS_TRAP are correct or just bugs we haven't noticed yet...

target/arm/helper.c static CPAccessResult gt_stimer_access(CPUARMState *env, · line 2704

Quoted 4 lines
target/arm/helper.c +4 -0
+    default:
+        g_assert_not_reached();
+    }
+}

thanks
-- PMM

#6 lore raw
Quoted 32 lines · Peter Maydell
Quoted 24 lines · Alex Bennée

When FEAT_SEL2 was implemented the SEL2 timers where missed. This shows up when building the latest Hafnium with SPMC_AT_EL=2. The actual implementation utilises the same logic as the rest of the timers so all we need to do is:

- define the timers and their access functions
- conditionally add the correct system registers
- create a new accessfn as the rules are subtly different to the existing secure timer

Fixes e9152ee91c (target/arm: add ARMv8.4-SEL2 system registers)
Cc Andrei Homescu <[email protected]>
Cc Arve Hjønnevåg <[email protected]>
Cc Rémi Denis-Courmont <[email protected]>

---
v1
- add better comments to GTIMER descriptions
- also define new timers for sbsa-ref
- don't conditionally gate qemu_timer creation on the feature
- take cntvoff_el2 int account for SEC_VEL2 in gt_recalc/g_tval_[read|write]

Quoted 6 lines
---
 include/hw/arm/bsa.h |   2 +
 target/arm/cpu.h     |   2 +
 target/arm/gtimer.h  |   4 +-
 hw/arm/sbsa-ref.c    |   2 +
 hw/arm/virt.c        |   2 +
Quoted 1 line

I would put the board changes in their own patch(es).

Won't that break bisection?

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#7 lore raw
Quoted 35 lines · Alex Bennée
Quoted 32 lines · Peter Maydell
Quoted 30 lines · Alex Bennée

When FEAT_SEL2 was implemented the SEL2 timers where missed. This shows up when building the latest Hafnium with SPMC_AT_EL=2. The actual implementation utilises the same logic as the rest of the timers so all we need to do is:

- define the timers and their access functions
- conditionally add the correct system registers
- create a new accessfn as the rules are subtly different to the existing secure timer

Fixes e9152ee91c (target/arm: add ARMv8.4-SEL2 system registers)
Cc Andrei Homescu <[email protected]>
Cc Arve Hjønnevåg <[email protected]>
Cc Rémi Denis-Courmont <[email protected]>

---
v1
- add better comments to GTIMER descriptions
- also define new timers for sbsa-ref
- don't conditionally gate qemu_timer creation on the feature
- take cntvoff_el2 int account for SEC_VEL2 in gt_recalc/g_tval_[read|write]

---
 include/hw/arm/bsa.h |   2 +
 target/arm/cpu.h     |   2 +
 target/arm/gtimer.h  |   4 +-
 hw/arm/sbsa-ref.c    |   2 +
 hw/arm/virt.c        |   2 +
Quoted 3 lines
Quoted 1 line

I would put the board changes in their own patch(es).

Won't that break bisection?

Any guest code attempting to use this timer currently is not going to work because the registers don't even exist. So there's no previous working state that would be broken.

thanks
-- PMM

#8 lore raw
In reply to Alex Bennée
Quoted 16 lines · Alex Bennée

When FEAT_SEL2 was implemented the SEL2 timers where missed. This shows up when building the latest Hafnium with SPMC_AT_EL=2. The actual implementation utilises the same logic as the rest of the timers so all we need to do is:

- define the timers and their access functions
- conditionally add the correct system registers
- create a new accessfn as the rules are subtly different to the existing secure timer

include/hw/arm/bsa.h +0 -0
index 8eaab603c0..b4ecca1b1c 100644

include/hw/arm/bsa.h line 25

Quoted 4 lines
include/hw/arm/bsa.h +1 -0
@@ -22,6 +22,8 @@
 #define QEMU_ARM_BSA_H
 
 /* These are architectural INTID values */
+#define ARCH_TIMER_S_VIRT_EL2_IRQ  19

Can we call this ARM_TIMER_S_EL2_VIRT_IRQ please? We currently have ARCH_TIMER_NS_EL2_VIRT_IRQ so we should be consistent about where in the name we put the "VIRT" bit.

include/hw/arm/bsa.h line 29

Quoted 4 lines
include/hw/arm/bsa.h +1 -0
+#define ARCH_TIMER_S_EL2_IRQ       20
 #define VIRTUAL_PMU_IRQ            23
 #define ARCH_GIC_MAINT_IRQ         25
 #define ARCH_TIMER_NS_EL2_IRQ      26

-- PMM

#9 lore raw
Quoted 19 lines · Peter Maydell
Quoted 18 lines · Alex Bennée

When FEAT_SEL2 was implemented the SEL2 timers where missed. This shows up when building the latest Hafnium with SPMC_AT_EL=2. The actual implementation utilises the same logic as the rest of the timers so all we need to do is:

- define the timers and their access functions
- conditionally add the correct system registers
- create a new accessfn as the rules are subtly different to the existing secure timer

include/hw/arm/bsa.h +0 -0
index 8eaab603c0..b4ecca1b1c 100644
@@ -22,6 +22,8 @@
 #define QEMU_ARM_BSA_H
 
Quoted 4 lines

include/hw/arm/bsa.h line 25

Quoted 2 lines
include/hw/arm/bsa.h +1 -0
 /* These are architectural INTID values */
+#define ARCH_TIMER_S_VIRT_EL2_IRQ  19

Can we call this ARM_TIMER_S_EL2_VIRT_IRQ please?

I'm going to assume you mean ARCH_TIMER_S_EL2_VIRT_IRQ ;-)

Quoted 10 lines

We currently have ARCH_TIMER_NS_EL2_VIRT_IRQ
so we should be consistent about where in
the name we put the "VIRT" bit.

include/hw/arm/bsa.h line 29

Quoted 4 lines
include/hw/arm/bsa.h +1 -0
+#define ARCH_TIMER_S_EL2_IRQ       20
 #define VIRTUAL_PMU_IRQ            23
 #define ARCH_GIC_MAINT_IRQ         25
 #define ARCH_TIMER_NS_EL2_IRQ      26

-- PMM

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro