Skip to contentSkip to conversation

target/arm: fix WFIT syndrome reporting

PATCH In review Alex Bennée 2 participants 11 messages Feb 24 – Feb 26

lore
Share & tools
lore
#0 lore raw

cover target/arm: fix WFIT syndrome reporting

This adds support for reporting the register value alongside the corrected syndrome information for trapped WFIT instructions.

Alex.

Alex Bennée (4):
target/arm: set the correct TI bits for WFIT traps target/arm: use FIELD_DP32 instead of open-coding syn_wfx target/arm: pass rd directly to wfit helper target/arm: report register in WFIT syndromes

 target/arm/syndrome.h          | 24 ++++++++++++++++++++----
 target/arm/tcg/helper-defs.h   |  2 +-
 target/arm/tcg/op_helper.c     |  8 +++++---
 target/arm/tcg/translate-a64.c |  2 +-
 4 files changed, 27 insertions(+), 9 deletions(-)
Signature · 1 line
2.47.3
#1 lore raw

PATCH 1/4 target/arm: set the correct TI bits for WFIT traps

The WFIT trap should be reported as 0b10.

---
 target/arm/tcg/op_helper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/arm/tcg/op_helper.c +1 -1
index 4d708635068..aa14f15eb62 100644
@@ -448,7 +448,7 @@ void HELPER(wfit)(CPUARMState *env, uint64_t timeout)
 
     if (target_el) {
         env->pc -= 4;
-        raise_exception(env, excp, syn_wfx(1, 0xe, 0, false), target_el);
+        raise_exception(env, excp, syn_wfx(1, 0xe, 2, false), target_el);
     }
 
     if (uadd64_overflow(timeout, offset, &nexttick)) {
Signature · 1 line
2.47.3
#2 lore raw

PATCH 2/4 target/arm: use FIELD_DP32 instead of open-coding syn_wfx

In reply to Alex Bennée

We can use the registerfields API to safely set the individual fields and avoid open coding magic numbers. The EC and IL fields are still driven by defines but we could convert them later.

---
 target/arm/syndrome.h | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
target/arm/syndrome.h +17 -3
index bff61f052cc..86a2d5ea9d0 100644
@@ -26,6 +26,7 @@
 #define TARGET_ARM_SYNDROME_H
 
 #include "qemu/bitops.h"
+#include "hw/core/registerfields.h"
 
 /* Valid Syndrome Register EC field values */
 enum arm_exception_class {
@@ -352,11 +353,24 @@ static inline uint32_t syn_breakpoint(int same_el)
         | ARM_EL_IL | 0x22;
 }
 
+FIELD(WFX_ISS, TI, 0, 2)
+FIELD(WFX_ISS, RV, 14, 1)
+FIELD(WFX_ISS, RN, 15, 5)
+FIELD(WFX_ISS, COND, 20, 4)
+FIELD(WFX_ISS, CV, 24, 1)
+
 static inline uint32_t syn_wfx(int cv, int cond, int ti, bool is_16bit)
 {
-    return (EC_WFX_TRAP << ARM_EL_EC_SHIFT) |
-           (is_16bit ? 0 : (1 << ARM_EL_IL_SHIFT)) |
-           (cv << 24) | (cond << 20) | ti;
+    uint32_t res = (EC_WFX_TRAP << ARM_EL_EC_SHIFT);
+
+    res = FIELD_DP32(res, WFX_ISS, CV, cv);
+    res = FIELD_DP32(res, WFX_ISS, COND, cond);
+    res = FIELD_DP32(res, WFX_ISS, TI, ti);
+
+    if (!is_16bit) {
+        res |= ARM_EL_IL;
+    }
+    return res;
 }
 
 static inline uint32_t syn_illegalstate(void)
Signature · 1 line
2.47.3
#3 lore raw

Re: [PATCH 2/4] target/arm: use FIELD_DP32 instead of open-coding syn_wfx

Quoted 19 lines · Alex Bennée

We can use the registerfields API to safely set the individual fields and avoid open coding magic numbers. The EC and IL fields are still driven by defines but we could convert them later.

target/arm/syndrome.h +7 -3
+FIELD(WFX_ISS, TI, 0, 2)
+FIELD(WFX_ISS, RV, 14, 1)
+FIELD(WFX_ISS, RN, 15, 5)
+FIELD(WFX_ISS, COND, 20, 4)
+FIELD(WFX_ISS, CV, 24, 1)
+
 static inline uint32_t syn_wfx(int cv, int cond, int ti, bool is_16bit)
 {
-    return (EC_WFX_TRAP << ARM_EL_EC_SHIFT) |
-           (is_16bit ? 0 : (1 << ARM_EL_IL_SHIFT)) |
-           (cv << 24) | (cond << 20) | ti;
+    uint32_t res = (EC_WFX_TRAP << ARM_EL_EC_SHIFT);

target/arm/syndrome.h static inline uint32_t syn_breakpoint(int same_el) · line 374

Quoted 10 lines
target/arm/syndrome.h +9 -0
+
+    res = FIELD_DP32(res, WFX_ISS, CV, cv);
+    res = FIELD_DP32(res, WFX_ISS, COND, cond);
+    res = FIELD_DP32(res, WFX_ISS, TI, ti);
+
+    if (!is_16bit) {
+        res |= ARM_EL_IL;
+    }
+    return res;
 }

I'm not hugely enthusiastic about this mostly because it means we would have one syn_foo function that's written totally differently to all the rest.

-- PMM

#4 lore raw

Re: [PATCH 2/4] target/arm: use FIELD_DP32 instead of open-coding syn_wfx

Quoted 26 lines · Peter Maydell
Quoted 25 lines · Alex Bennée

We can use the registerfields API to safely set the individual fields and avoid open coding magic numbers. The EC and IL fields are still driven by defines but we could convert them later.

target/arm/syndrome.h +13 -3
+FIELD(WFX_ISS, TI, 0, 2)
+FIELD(WFX_ISS, RV, 14, 1)
+FIELD(WFX_ISS, RN, 15, 5)
+FIELD(WFX_ISS, COND, 20, 4)
+FIELD(WFX_ISS, CV, 24, 1)
+
 static inline uint32_t syn_wfx(int cv, int cond, int ti, bool is_16bit)
 {
-    return (EC_WFX_TRAP << ARM_EL_EC_SHIFT) |
-           (is_16bit ? 0 : (1 << ARM_EL_IL_SHIFT)) |
-           (cv << 24) | (cond << 20) | ti;
+    uint32_t res = (EC_WFX_TRAP << ARM_EL_EC_SHIFT);
+
+    res = FIELD_DP32(res, WFX_ISS, CV, cv);
+    res = FIELD_DP32(res, WFX_ISS, COND, cond);
+    res = FIELD_DP32(res, WFX_ISS, TI, ti);
+
+    if (!is_16bit) {
Quoted 8 lines

target/arm/syndrome.h static inline uint32_t syn_breakpoint(int same_el) · line 374

Quoted 4 lines
target/arm/syndrome.h +3 -0
+        res |= ARM_EL_IL;
+    }
+    return res;
 }

I'm not hugely enthusiastic about this mostly because it means we would have one syn_foo function that's written totally differently to all the rest.

I did consider running through the rest of the file updating with proper field setting but I didn't want to bloat the series. I think it does help with readability compared to open coded constant shifts.

Quoted 2 lines

-- PMM

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#5 lore raw

PATCH 3/4 target/arm: pass rd directly to wfit helper

In reply to Alex Bennée

If we generate an exception for WFIT we could also fill out the rv/rn fields of the ISS. To facilitate this pass the register number directly and read the timeout value in the helper itself.

---
 target/arm/tcg/helper-defs.h   | 2 +-
 target/arm/tcg/op_helper.c     | 4 +++-
 target/arm/tcg/translate-a64.c | 2 +-
 3 files changed, 5 insertions(+), 3 deletions(-)
target/arm/tcg/helper-defs.h +1 -1
index 5a10a9fba3b..a9a062cf777 100644
@@ -55,7 +55,7 @@ DEF_HELPER_2(exception_pc_alignment, noreturn, env, vaddr)
 DEF_HELPER_1(setend, void, env)
 DEF_HELPER_2(wfi, void, env, i32)
 DEF_HELPER_1(wfe, void, env)
-DEF_HELPER_2(wfit, void, env, i64)
+DEF_HELPER_FLAGS_2(wfit, TCG_CALL_NO_WG, void, env, i32)
 DEF_HELPER_1(yield, void, env)
 DEF_HELPER_1(pre_hvc, void, env)
 DEF_HELPER_2(pre_smc, void, env, i32)
target/arm/tcg/op_helper.c +3 -1
index aa14f15eb62..28a1c7243ad 100644
@@ -409,7 +409,7 @@ void HELPER(wfi)(CPUARMState *env, uint32_t insn_len)
 #endif
 }
 
-void HELPER(wfit)(CPUARMState *env, uint64_t timeout)
+void HELPER(wfit)(CPUARMState *env, uint32_t rd)
 {
 #ifdef CONFIG_USER_ONLY
     /*
@@ -428,6 +428,8 @@ void HELPER(wfit)(CPUARMState *env, uint64_t timeout)
     int target_el = check_wfx_trap(env, false, &excp);
     /* The WFIT should time out when CNTVCT_EL0 >= the specified value. */
     uint64_t cntval = gt_get_countervalue(env);
+    uint64_t timeout = env->xregs[rd];
+
     /*
      * We want the value that we would get if we read CNTVCT_EL0 from
      * the current exception level, so the direct_access offset, not
target/arm/tcg/translate-a64.c +1 -1
index 5d261a5e32b..073454b9195 100644
@@ -2064,7 +2064,7 @@ static bool trans_WFIT(DisasContext *s, arg_WFIT *a)
     }
 
     gen_a64_update_pc(s, 4);
-    gen_helper_wfit(tcg_env, cpu_reg(s, a->rd));
+    gen_helper_wfit(tcg_env, tcg_constant_i32(a->rd));
     /* Go back to the main loop to check for interrupts */
     s->base.is_jmp = DISAS_EXIT;
     return true;
Signature · 1 line
2.47.3
Reviewed-by on v1 3/4
#6 lore raw

Re: [PATCH 3/4] target/arm: pass rd directly to wfit helper

Quoted 17 lines · Alex Bennée

If we generate an exception for WFIT we could also fill out the rv/rn fields of the ISS. To facilitate this pass the register number directly and read the timeout value in the helper itself.

---
 target/arm/tcg/helper-defs.h   | 2 +-
 target/arm/tcg/op_helper.c     | 4 +++-
 target/arm/tcg/translate-a64.c | 2 +-
 3 files changed, 5 insertions(+), 3 deletions(-)
target/arm/tcg/helper-defs.h +0 -0
index 5a10a9fba3b..a9a062cf777 100644

target/arm/tcg/helper-defs.h DEF_HELPER_2(exception_pc_alignment, noreturn, env, vaddr) · line 58

Quoted 5 lines
target/arm/tcg/helper-defs.h +1 -1
@@ -55,7 +55,7 @@ DEF_HELPER_2(exception_pc_alignment, noreturn, env, vaddr)
 DEF_HELPER_1(setend, void, env)
 DEF_HELPER_2(wfi, void, env, i32)
 DEF_HELPER_1(wfe, void, env)
-DEF_HELPER_2(wfit, void, env, i64)
+DEF_HELPER_FLAGS_2(wfit, TCG_CALL_NO_WG, void, env, i32)

Why the change to NO_WG ? The code change doesn't remove any writes to globals...

Quoted 30 lines
target/arm/tcg/helper-defs.h +0 -0
 DEF_HELPER_1(yield, void, env)
 DEF_HELPER_1(pre_hvc, void, env)
 DEF_HELPER_2(pre_smc, void, env, i32)
target/arm/tcg/op_helper.c +3 -1
index aa14f15eb62..28a1c7243ad 100644
@@ -409,7 +409,7 @@ void HELPER(wfi)(CPUARMState *env, uint32_t insn_len)
 #endif
 }
 
-void HELPER(wfit)(CPUARMState *env, uint64_t timeout)
+void HELPER(wfit)(CPUARMState *env, uint32_t rd)
 {
 #ifdef CONFIG_USER_ONLY
     /*
@@ -428,6 +428,8 @@ void HELPER(wfit)(CPUARMState *env, uint64_t timeout)
     int target_el = check_wfx_trap(env, false, &excp);
     /* The WFIT should time out when CNTVCT_EL0 >= the specified value. */
     uint64_t cntval = gt_get_countervalue(env);
+    uint64_t timeout = env->xregs[rd];
+
     /*
      * We want the value that we would get if we read CNTVCT_EL0 from
      * the current exception level, so the direct_access offset, not
target/arm/tcg/translate-a64.c +0 -0
index 5d261a5e32b..073454b9195 100644

target/arm/tcg/translate-a64.c static bool trans_WFIT(DisasContext *s, arg_WFIT *a) · line 2070

Quoted 8 lines
target/arm/tcg/translate-a64.c +1 -1
@@ -2064,7 +2064,7 @@ static bool trans_WFIT(DisasContext *s, arg_WFIT *a)
     }
 
     gen_a64_update_pc(s, 4);
-    gen_helper_wfit(tcg_env, cpu_reg(s, a->rd));
+    gen_helper_wfit(tcg_env, tcg_constant_i32(a->rd));
     /* Go back to the main loop to check for interrupts */
     s->base.is_jmp = DISAS_EXIT;
     return true;

Otherwise

You might squash this in to patch 4 if you like.

thanks
-- PMM

#7 lore raw

Re: [PATCH 3/4] target/arm: pass rd directly to wfit helper

Quoted 13 lines · Peter Maydell
Quoted 12 lines · Alex Bennée

If we generate an exception for WFIT we could also fill out the rv/rn fields of the ISS. To facilitate this pass the register number directly and read the timeout value in the helper itself.

---
 target/arm/tcg/helper-defs.h   | 2 +-
 target/arm/tcg/op_helper.c     | 4 +++-
 target/arm/tcg/translate-a64.c | 2 +-
 3 files changed, 5 insertions(+), 3 deletions(-)
Quoted 13 lines

target/arm/tcg/helper-defs.h DEF_HELPER_2(exception_pc_alignment, noreturn, env, vaddr) · line 58

Quoted 10 lines
target/arm/tcg/helper-defs.h +1 -1
index 5a10a9fba3b..a9a062cf777 100644
@@ -55,7 +55,7 @@ DEF_HELPER_2(exception_pc_alignment, noreturn, env, vaddr)
 DEF_HELPER_1(setend, void, env)
 DEF_HELPER_2(wfi, void, env, i32)
 DEF_HELPER_1(wfe, void, env)
-DEF_HELPER_2(wfit, void, env, i64)
+DEF_HELPER_FLAGS_2(wfit, TCG_CALL_NO_WG, void, env, i32)

Why the change to NO_WG ? The code change doesn't remove any writes to globals...

I thought that was the right way round - it doesn't write to any globals, but it does read so the helper needs to ensure globals are synced before the callback.

Quoted 47 lines
Quoted 31 lines
target/arm/tcg/helper-defs.h +0 -0
 DEF_HELPER_1(yield, void, env)
 DEF_HELPER_1(pre_hvc, void, env)
 DEF_HELPER_2(pre_smc, void, env, i32)
target/arm/tcg/op_helper.c +3 -1
index aa14f15eb62..28a1c7243ad 100644
@@ -409,7 +409,7 @@ void HELPER(wfi)(CPUARMState *env, uint32_t insn_len)
 #endif
 }
 
-void HELPER(wfit)(CPUARMState *env, uint64_t timeout)
+void HELPER(wfit)(CPUARMState *env, uint32_t rd)
 {
 #ifdef CONFIG_USER_ONLY
     /*
@@ -428,6 +428,8 @@ void HELPER(wfit)(CPUARMState *env, uint64_t timeout)
     int target_el = check_wfx_trap(env, false, &excp);
     /* The WFIT should time out when CNTVCT_EL0 >= the specified value. */
     uint64_t cntval = gt_get_countervalue(env);
+    uint64_t timeout = env->xregs[rd];
+
     /*
      * We want the value that we would get if we read CNTVCT_EL0 from
      * the current exception level, so the direct_access offset, not
target/arm/tcg/translate-a64.c +0 -0
index 5d261a5e32b..073454b9195 100644

target/arm/tcg/translate-a64.c static bool trans_WFIT(DisasContext *s, arg_WFIT *a) · line 2070

Quoted 8 lines
target/arm/tcg/translate-a64.c +1 -1
@@ -2064,7 +2064,7 @@ static bool trans_WFIT(DisasContext *s, arg_WFIT *a)
     }
 
     gen_a64_update_pc(s, 4);
-    gen_helper_wfit(tcg_env, cpu_reg(s, a->rd));
+    gen_helper_wfit(tcg_env, tcg_constant_i32(a->rd));
     /* Go back to the main loop to check for interrupts */
     s->base.is_jmp = DISAS_EXIT;
     return true;

Otherwise

You might squash this in to patch 4 if you like.

thanks
-- PMM

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#8 lore raw

Re: [PATCH 3/4] target/arm: pass rd directly to wfit helper

Quoted 30 lines · Alex Bennée
Quoted 13 lines · Peter Maydell
Quoted 12 lines · Alex Bennée

If we generate an exception for WFIT we could also fill out the rv/rn fields of the ISS. To facilitate this pass the register number directly and read the timeout value in the helper itself.

---
 target/arm/tcg/helper-defs.h   | 2 +-
 target/arm/tcg/op_helper.c     | 4 +++-
 target/arm/tcg/translate-a64.c | 2 +-
 3 files changed, 5 insertions(+), 3 deletions(-)
Quoted 13 lines

target/arm/tcg/helper-defs.h DEF_HELPER_2(exception_pc_alignment, noreturn, env, vaddr) · line 58

Quoted 10 lines
target/arm/tcg/helper-defs.h +1 -1
index 5a10a9fba3b..a9a062cf777 100644
@@ -55,7 +55,7 @@ DEF_HELPER_2(exception_pc_alignment, noreturn, env, vaddr)
 DEF_HELPER_1(setend, void, env)
 DEF_HELPER_2(wfi, void, env, i32)
 DEF_HELPER_1(wfe, void, env)
-DEF_HELPER_2(wfit, void, env, i64)
+DEF_HELPER_FLAGS_2(wfit, TCG_CALL_NO_WG, void, env, i32)

Why the change to NO_WG ? The code change doesn't remove any writes to globals...

Quoted 3 lines

I thought that was the right way round - it doesn't write to any globals, but it does read so the helper needs to ensure globals are synced before the callback.

The default if you use a DEF_HELPER macro that doesn't specify flags is "reading and writing to globals is OK" (it's a zero flags argument). So this change goes from "flags 0" to "flags NO_WG" and doesn't make any difference to how TCG thinks of its behaviour w.r.t. reading globals.

I think you're right that the helper doesn't need to be able to write globals, but making a stricter promise about what the helper does is something that's a distinct change from adding the rn info to the syndrome.

-- PMM

#9 lore raw

PATCH 4/4 target/arm: report register in WFIT syndromes

In reply to Alex Bennée

Now we have the register number we can report it in the syndrome.

---
 target/arm/syndrome.h      | 4 +++-
 target/arm/tcg/op_helper.c | 4 ++--
 2 files changed, 5 insertions(+), 3 deletions(-)
target/arm/syndrome.h +3 -1
index 86a2d5ea9d0..086ac76d7f1 100644
@@ -359,12 +359,14 @@ FIELD(WFX_ISS, RN, 15, 5)
 FIELD(WFX_ISS, COND, 20, 4)
 FIELD(WFX_ISS, CV, 24, 1)
 
-static inline uint32_t syn_wfx(int cv, int cond, int ti, bool is_16bit)
+static inline uint32_t syn_wfx(int cv, int cond, int rn, int rv, int ti, bool is_16bit)
 {
     uint32_t res = (EC_WFX_TRAP << ARM_EL_EC_SHIFT);
 
     res = FIELD_DP32(res, WFX_ISS, CV, cv);
     res = FIELD_DP32(res, WFX_ISS, COND, cond);
+    res = FIELD_DP32(res, WFX_ISS, RN, rn);
+    res = FIELD_DP32(res, WFX_ISS, RV, rv);
     res = FIELD_DP32(res, WFX_ISS, TI, ti);
 
     if (!is_16bit) {
target/arm/tcg/op_helper.c +2 -2
index 28a1c7243ad..a68cdf6de23 100644
@@ -399,7 +399,7 @@ void HELPER(wfi)(CPUARMState *env, uint32_t insn_len)
             env->regs[15] -= insn_len;
         }
 
-        raise_exception(env, excp, syn_wfx(1, 0xe, 0, insn_len == 2),
+        raise_exception(env, excp, syn_wfx(1, 0xe, 0, 0, 0, insn_len == 2),
                         target_el);
     }
 
@@ -450,7 +450,7 @@ void HELPER(wfit)(CPUARMState *env, uint32_t rd)
 
     if (target_el) {
         env->pc -= 4;
-        raise_exception(env, excp, syn_wfx(1, 0xe, 2, false), target_el);
+        raise_exception(env, excp, syn_wfx(1, 0xe, rd, 1, 2, false), target_el);
     }
 
     if (uadd64_overflow(timeout, offset, &nexttick)) {
Signature · 1 line
2.47.3
Reviewed-by on v1 4/4
#10 lore raw

Re: [PATCH 4/4] target/arm: report register in WFIT syndromes

Quoted 8 lines · Alex Bennée

Now we have the register number we can report it in the syndrome.

---
 target/arm/syndrome.h      | 4 +++-
 target/arm/tcg/op_helper.c | 4 ++--
 2 files changed, 5 insertions(+), 3 deletions(-)

thanks
-- PMM