Skip to contentSkip to conversation

target/arm: Fix return value from LDSMIN/LDSMAX 8/16 bit atomics

PATCH In review Peter Maydell 3 participants 3 messages Jun 2, 2023 – Jun 3, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/arm: Fix return value from LDSMIN/LDSMAX 8/16 bit atomics

The atomic memory operations are supposed to return the old memory data value in the destination register. This value is not sign-extended, even if the operation is the signed minimum or maximum. (In the pseudocode for the instructions the returned data value is passed to ZeroExtend() to create the value in the register.)

We got this wrong because we were doing a 32-to-64 zero extend on the result for 8 and 16 bit data values, rather than the correct amount of zero extension.

Fix the bug by using ext8u and ext16u for the MO_8 and MO_16 data sizes rather than ext32u.

---
 target/arm/tcg/translate-a64.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
target/arm/tcg/translate-a64.c +16 -2
index 741a6087399..075553e15f5 100644
@@ -3401,8 +3401,22 @@ static void disas_ldst_atomic(DisasContext *s, uint32_t insn,
      */
     fn(tcg_rt, clean_addr, tcg_rs, get_mem_index(s), mop);
 
-    if ((mop & MO_SIGN) && size != MO_64) {
-        tcg_gen_ext32u_i64(tcg_rt, tcg_rt);
+    if (mop & MO_SIGN) {
+        switch (size) {
+        case MO_8:
+            tcg_gen_ext8u_i64(tcg_rt, tcg_rt);
+            break;
+        case MO_16:
+            tcg_gen_ext16u_i64(tcg_rt, tcg_rt);
+            break;
+        case MO_32:
+            tcg_gen_ext32u_i64(tcg_rt, tcg_rt);
+            break;
+        case MO_64:
+            break;
+        default:
+            g_assert_not_reached();
+        }
     }
 }
 
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Peter Maydell

The atomic memory operations are supposed to return the old memory data value in the destination register. This value is not sign-extended, even if the operation is the signed minimum or maximum. (In the pseudocode for the instructions the returned data value is passed to ZeroExtend() to create the value in the register.)

We got this wrong because we were doing a 32-to-64 zero extend on the result for 8 and 16 bit data values, rather than the correct amount of zero extension.

Quoted 8 lines

Fix the bug by using ext8u and ext16u for the MO_8 and MO_16 data sizes rather than ext32u.

---
  target/arm/tcg/translate-a64.c | 18 ++++++++++++++++--
  1 file changed, 16 insertions(+), 2 deletions(-)
Reviewed-by on v1 1/1
#2 lore raw
In reply to Peter Maydell
Quoted 35 lines · Peter Maydell

The atomic memory operations are supposed to return the old memory data value in the destination register. This value is not sign-extended, even if the operation is the signed minimum or maximum. (In the pseudocode for the instructions the returned data value is passed to ZeroExtend() to create the value in the register.)

We got this wrong because we were doing a 32-to-64 zero extend on the result for 8 and 16 bit data values, rather than the correct amount of zero extension.

Fix the bug by using ext8u and ext16u for the MO_8 and MO_16 data sizes rather than ext32u.

---
  target/arm/tcg/translate-a64.c | 18 ++++++++++++++++--
  1 file changed, 16 insertions(+), 2 deletions(-)
target/arm/tcg/translate-a64.c +6 -2
index 741a6087399..075553e15f5 100644
@@ -3401,8 +3401,22 @@ static void disas_ldst_atomic(DisasContext *s, uint32_t insn,
       */
      fn(tcg_rt, clean_addr, tcg_rs, get_mem_index(s), mop);
 
-    if ((mop & MO_SIGN) && size != MO_64) {
-        tcg_gen_ext32u_i64(tcg_rt, tcg_rt);
+    if (mop & MO_SIGN) {
+        switch (size) {
+        case MO_8:
+            tcg_gen_ext8u_i64(tcg_rt, tcg_rt);
+            break;
+        case MO_16:

target/arm/tcg/translate-a64.c static void disas_ldst_atomic(DisasContext *s, uint32_t insn, · line 3419

Quoted 10 lines
target/arm/tcg/translate-a64.c +10 -0
+            tcg_gen_ext16u_i64(tcg_rt, tcg_rt);
+            break;
+        case MO_32:
+            tcg_gen_ext32u_i64(tcg_rt, tcg_rt);
+            break;
+        case MO_64:
+            break;
+        default:
+            g_assert_not_reached();
+        }

This reminds me that we have a function in tcg to handle this switch, but it isn't exposed. I keep meaning to do that...

r~