Skip to contentSkip to conversation

target/arm: UNDEF various things for pre-v6T2

PATCH In review Peter Maydell 2 participants 9 messages Aug 21

lore
Share & tools
lore
#0 lore raw

cover target/arm: UNDEF various things for pre-v6T2

We were missing feature checks for various parts of the 16-bit Thumb encoding space, with the result that we didn't UNDEF when we should for these on pre-v6T2 CPUs:
* CBZ/CBNZ
* IT
* the "hint space" (which doesn't become NOPs until v6T2)

This patchset fixes up those mistakes so we UNDEF when the hardware does. M-profile makes this a little more complicated, so the precise test is different in the three cases.

thanks
-- PMM

Peter Maydell (3):
target/arm: Make CBZ/CBNZ UNDEF before v6T2 target/arm: Make IT insn undef when not present target/arm: Make Thumb T1 hint space UNDEF before v6T2

 target/arm/tcg/t16.decode  |  3 +++
 target/arm/tcg/translate.c | 37 ++++++++++++++++++++++++++++++++++++-
 2 files changed, 39 insertions(+), 1 deletion(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/3 target/arm: Make CBZ/CBNZ UNDEF before v6T2

The Thumb CBZ/CBNZ encodings are only introduced in v6T2 (for A-profile) and v7M (for M-profile). We missed out the feature-check in trans_CBZ(), so don't UNDEF on v5T or v6M. Add the missing check:
conveniently, the ARM_FEATURE_THUMB2 feature is true for exactly the cases where CBZ is implemented.

Fixes 9ee6e8bb853bde ("ARMv7 support.")
---
 target/arm/tcg/translate.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
target/arm/tcg/translate.c +8 -1
index 055f3c5b403..df98dc2e34d 100644
@@ -5783,7 +5783,14 @@ static bool trans_TBH(DisasContext *s, arg_tbranch *a)
 
 static bool trans_CBZ(DisasContext *s, arg_CBZ *a)
 {
-    TCGv_i32 tmp = load_reg(s, a->rn);
+    TCGv_i32 tmp;
+
+    /* CBZ was introduced in v6T2 and v7M */
+    if (!arm_dc_feature(s, ARM_FEATURE_THUMB2)) {
+        return false;
+    }
+
+    tmp = load_reg(s, a->rn);
 
     arm_gen_condlabel(s);
     tcg_gen_brcondi_i32(a->nz ? TCG_COND_EQ : TCG_COND_NE,
Signature · 1 line
2.43.0
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] target/arm: Make CBZ/CBNZ UNDEF before v6T2

Quoted 6 lines · Peter Maydell

The Thumb CBZ/CBNZ encodings are only introduced in v6T2 (for A-profile) and v7M (for M-profile). We missed out the feature-check in trans_CBZ(), so don't UNDEF on v5T or v6M. Add the missing check:
conveniently, the ARM_FEATURE_THUMB2 feature is true for exactly the cases where CBZ is implemented.

Quoted 6 lines
Fixes 9ee6e8bb853bde ("ARMv7 support.")
---
  target/arm/tcg/translate.c | 9 ++++++++-
  1 file changed, 8 insertions(+), 1 deletion(-)

r~

#3 lore raw

PATCH 2/3 target/arm: Make IT insn undef when not present

In reply to Peter Maydell

The IT insn is introduced for A-profile starting in v6T2; for M-profile it is present when the Main Extension is implemented (which includes v7M and excludes v6M). We were missing the feature-check, so fail to UNDEF on earlier cores.

Add the missing check.

Fixes 9ee6e8bb853bde ("ARMv7 support.")
---
 target/arm/tcg/translate.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
target/arm/tcg/translate.c +10 -0
index df98dc2e34d..103e2fe7c73 100644
@@ -6038,6 +6038,16 @@ static bool trans_IT(DisasContext *s, arg_IT *a)
 {
     int cond_mask = a->cond_mask;
 
+    /*
+     * IT insn introduced in v6T2 for A-profile; it is only present
+     * on M-profile if the Main Extension is implemented.
+     */
+    if (!arm_dc_feature(s, ARM_FEATURE_THUMB2) ||
+        (arm_dc_feature(s, ARM_FEATURE_M) &&
+         !arm_dc_feature(s, ARM_FEATURE_M_MAIN))) {
+        return false;
+    }
+
     /*
      * No actual code generated for this insn, just setup state.
      *
Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH 2/3] target/arm: Make IT insn undef when not present

Quoted 21 lines · Peter Maydell

The IT insn is introduced for A-profile starting in v6T2; for M-profile it is present when the Main Extension is implemented (which includes v7M and excludes v6M). We were missing the feature-check, so fail to UNDEF on earlier cores.

Add the missing check.

Fixes 9ee6e8bb853bde ("ARMv7 support.")
---
  target/arm/tcg/translate.c | 10 ++++++++++
  1 file changed, 10 insertions(+)
target/arm/tcg/translate.c +0 -0
index df98dc2e34d..103e2fe7c73 100644
@@ -6038,6 +6038,16 @@ static bool trans_IT(DisasContext *s, arg_IT *a)
  {
      int cond_mask = a->cond_mask;

target/arm/tcg/translate.c static bool trans_IT(DisasContext *s, arg_IT *a) · line 6049

Quoted 10 lines
target/arm/tcg/translate.c +9 -0
 
+    /*
+     * IT insn introduced in v6T2 for A-profile; it is only present
+     * on M-profile if the Main Extension is implemented.
+     */
+    if (!arm_dc_feature(s, ARM_FEATURE_THUMB2) ||
+        (arm_dc_feature(s, ARM_FEATURE_M) &&
+         !arm_dc_feature(s, ARM_FEATURE_M_MAIN))) {
+        return false;
+    }

In practice this is going to be the same as just thumb2, because v7M has v7 which implies thumb2, and all v7M have m_main.

Is it clearer as

!(arm_dc_feature(s, ARM_FEATURE_M)
? arm_dc_feature(s, ARM_FEATURE_M_MAIN) : arm_dc_feature(s, ARM_FEATURE_THUMB2))

?

r~

#5 lore raw

Re: [PATCH 2/3] target/arm: Make IT insn undef when not present

Quoted 24 lines · Richard Henderson
Quoted 22 lines · Peter Maydell

The IT insn is introduced for A-profile starting in v6T2; for M-profile it is present when the Main Extension is implemented (which includes v7M and excludes v6M). We were missing the feature-check, so fail to UNDEF on earlier cores.

Add the missing check.

Fixes 9ee6e8bb853bde ("ARMv7 support.")
---
  target/arm/tcg/translate.c | 10 ++++++++++
  1 file changed, 10 insertions(+)
target/arm/tcg/translate.c +0 -0
index df98dc2e34d..103e2fe7c73 100644
@@ -6038,6 +6038,16 @@ static bool trans_IT(DisasContext *s, arg_IT *a)
  {
      int cond_mask = a->cond_mask;
 
Quoted 11 lines

target/arm/tcg/translate.c static bool trans_IT(DisasContext *s, arg_IT *a) · line 6049

Quoted 9 lines
target/arm/tcg/translate.c +9 -0
+    /*
+     * IT insn introduced in v6T2 for A-profile; it is only present
+     * on M-profile if the Main Extension is implemented.
+     */
+    if (!arm_dc_feature(s, ARM_FEATURE_THUMB2) ||
+        (arm_dc_feature(s, ARM_FEATURE_M) &&
+         !arm_dc_feature(s, ARM_FEATURE_M_MAIN))) {
+        return false;
+    }

In practice this is going to be the same as just thumb2, because v7M has v7 which implies thumb2, and all v7M have m_main.

v8M without the main extension exists, though we don't model any of those CPUs at the moment. That will have ARM_FEATURE_THUMB2 (because FEATURE_V8 implies FEATURE_V7 and FEATURE_V7 implies FEATURE_THUMB2).

Quoted 7 lines

Is it clearer as

!(arm_dc_feature(s, ARM_FEATURE_M)
? arm_dc_feature(s, ARM_FEATURE_M_MAIN) : arm_dc_feature(s, ARM_FEATURE_THUMB2))

?

Yeah, I think so. (I actually wrote it first with nested if()s roughly like that.)

-- PMM

Reviewed-by
#6 lore raw

Re: [PATCH 2/3] target/arm: Make IT insn undef when not present

Quoted 7 lines · Peter Maydell
Quoted 2 lines

In practice this is going to be the same as just thumb2, because v7M has v7 which implies thumb2, and all v7M have m_main.

v8M without the main extension exists, though we don't model any of those CPUs at the moment. That will have ARM_FEATURE_THUMB2 (because FEATURE_V8 implies FEATURE_V7 and FEATURE_V7 implies FEATURE_THUMB2).

Right-o.

Quoted 10 lines
Quoted 7 lines

Is it clearer as

!(arm_dc_feature(s, ARM_FEATURE_M)
? arm_dc_feature(s, ARM_FEATURE_M_MAIN) : arm_dc_feature(s, ARM_FEATURE_THUMB2))

?

Yeah, I think so. (I actually wrote it first with nested if()s roughly like that.)

Excellent. With that

r~

#7 lore raw

PATCH 3/3 target/arm: Make Thumb T1 hint space UNDEF before v6T2

In reply to Peter Maydell

The "hint space" is a region of the encoding space which is defined to NOP if not specified as an architected instruction, so that future instructions can be added there which fall back to NOPs on older CPUs. In the A32 encoding, this hint space was carved out of the MSR (imm) insn by using the fact that a field_mask (bits [19:15]) of 0b0000 meant that an MSR (imm) would set no parts of the CPSR from the immediate, so it was always NOP on existing CPUs.

For the T1 encoding, the hint space is in a range that used to UNDEF in Armv5, and so the hint insns and the NOP region must all UNDEF before v6T2. Rather than putting this check in the trans functions for each hint insn and for the NOP space (which is a lot of places, and awkward since those trans functions are often shared with the A64 and A32 encodings), put in a decode line that covers the whole space which we check before any of the hints and which will explicitly UNDEF if necessary.

Cc: [email protected]
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4208

---
 target/arm/tcg/t16.decode  |  3 +++
 target/arm/tcg/translate.c | 18 ++++++++++++++++++
 2 files changed, 21 insertions(+)
target/arm/tcg/t16.decode +3 -0
index 9a8f89538ac..43cd36e949c 100644
@@ -224,6 +224,9 @@ REVSH           1011 1010 11 ... ...            @rdm
 
 {
   {
+    # Before v6T2 this was not NOP space and must UNDEF
+    MAYBE_UNDEF_T1_HINT 1011 1111 ---- 0000
+
     YIELD       1011 1111 0001 0000
     WFE         1011 1111 0010 0000
     WFI         1011 1111 0011 0000
target/arm/tcg/translate.c +18 -0
index 103e2fe7c73..e889c6c6136 100644
@@ -3339,6 +3339,24 @@ static bool trans_NOP(DisasContext *s, arg_NOP *a)
     return true;
 }
 
+static bool trans_MAYBE_UNDEF_T1_HINT(DisasContext *s,
+                                      arg_MAYBE_UNDEF_T1_HINT *a)
+{
+    /*
+     * The Thumb T1 encoding hint space was only defined starting
+     * in v6T2 for A-profile. For M-profile it always exists, even
+     * in v6M.
+     */
+    if (arm_dc_feature(s, ARM_FEATURE_M) ||
+        arm_dc_feature(s, ARM_FEATURE_THUMB2)) {
+        /* Allow decode to fall through to the hint insns and NOP space */
+        return false;
+    }
+    /* On the earlier cores, we must UNDEF */
+    unallocated_encoding(s);
+    return true;
+}
+
 static bool trans_MSR_imm(DisasContext *s, arg_MSR_imm *a)
 {
     uint32_t val = ror32(a->imm, a->rot * 2);
Signature · 1 line
2.43.0
Reviewed-by on v1 3/3
#8 lore raw

Re: [PATCH 3/3] target/arm: Make Thumb T1 hint space UNDEF before v6T2

Quoted 17 lines · Peter Maydell

The "hint space" is a region of the encoding space which is defined to NOP if not specified as an architected instruction, so that future instructions can be added there which fall back to NOPs on older CPUs. In the A32 encoding, this hint space was carved out of the MSR (imm) insn by using the fact that a field_mask (bits [19:15]) of 0b0000 meant that an MSR (imm) would set no parts of the CPSR from the immediate, so it was always NOP on existing CPUs.

For the T1 encoding, the hint space is in a range that used to UNDEF in Armv5, and so the hint insns and the NOP region must all UNDEF before v6T2. Rather than putting this check in the trans functions for each hint insn and for the NOP space (which is a lot of places, and awkward since those trans functions are often shared with the A64 and A32 encodings), put in a decode line that covers the whole space which we check before any of the hints and which will explicitly UNDEF if necessary.

Quoted 7 lines

Cc:[email protected] Resolves:https://gitlab.com/qemu-project/qemu/-/work_items/4208

---
  target/arm/tcg/t16.decode  |  3 +++
  target/arm/tcg/translate.c | 18 ++++++++++++++++++
  2 files changed, 21 insertions(+)

r~