Skip to contentSkip to conversation

target/microblaze/translate: fix reversed load/store translation

PATCH In review Luc Michel 3 participants 7 messages Jul 10 – Jul 23

lore
Share & tools
lore

Luc Michel

author
#0 lore raw

PATCH 1/1 target/microblaze/translate: fix reversed load/store translation

Since 36a9529e60e0 the TCGv returned by compute_ldst_addr_type{a,b} for the address can be a target register or a constant. However do_{load,store} might modify it in the case of a reversed load/store.

Fix this by factoring out the reversed access logic and copying the address in a new TCGv_i32 if it needs to be modified.

Fixes 36a9529e60e0 (target/microblaze: Simplify compute_ldst_addr_type{a,b})
---
 target/microblaze/translate.c | 56 ++++++++++++++++++++---------------
 1 file changed, 32 insertions(+), 24 deletions(-)
target/microblaze/translate.c +32 -24
index 8b219afb5dd..9fb84e1bf9f 100644
@@ -701,30 +701,47 @@ static void gen_alignment_check_ea(DisasContext *dc, TCGv_i64 ea, int rb,
 static inline MemOp mo_endian(DisasContext *dc)
 {
     return dc->cfg->endi ? MO_LE : MO_BE;
 }
 
+static void handle_reversed_access(MemOp *mop, TCGv_i32 *addr)
+{
+    MemOp size = *mop & MO_SIZE;
+
+    /*
+     * When doing reverse accesses we need to do two things.
+     *
+     * 1. Reverse the address wrt endianness.
+     * 2. Byteswap the data lanes on the way back into the CPU core.
+     *
+     * Use a new TCGv_i32 to modify the address because the current one might be
+     * a register or a constant.
+     */
+
+    if (size > MO_8) {
+        *mop ^= MO_BSWAP;
+    }
+
+    if (size < MO_32) {
+        TCGv_i32 new_addr = tcg_temp_new_i32();
+
+        tcg_gen_xori_i32(new_addr, *addr, 3 - size);
+        *addr = new_addr;
+    }
+}
+
 static bool do_load(DisasContext *dc, int rd, TCGv_i32 addr, MemOp mop,
                     int mem_index, bool rev)
 {
+#ifndef CONFIG_USER_ONLY
     MemOp size = mop & MO_SIZE;
+#endif
 
     mop |= mo_endian(dc);
 
-    /*
-     * When doing reverse accesses we need to do two things.
-     *
-     * 1. Reverse the address wrt endianness.
-     * 2. Byteswap the data lanes on the way back into the CPU core.
-     */
     if (rev) {
-        if (size > MO_8) {
-            mop ^= MO_BSWAP;
-        }
-        if (size < MO_32) {
-            tcg_gen_xori_i32(addr, addr, 3 - size);
-        }
+        handle_reversed_access(&mop, &addr);
     }
 
     /*
      * For system mode, enforce alignment if the cpu configuration
      * requires it.  For user-mode, the Linux kernel will have fixed up
@@ -864,27 +881,18 @@ static bool trans_lwx(DisasContext *dc, arg_typea *arg)
 }
 
 static bool do_store(DisasContext *dc, int rd, TCGv_i32 addr, MemOp mop,
                      int mem_index, bool rev)
 {
+#ifndef CONFIG_USER_ONLY
     MemOp size = mop & MO_SIZE;
+#endif
 
     mop |= mo_endian(dc);
 
-    /*
-     * When doing reverse accesses we need to do two things.
-     *
-     * 1. Reverse the address wrt endianness.
-     * 2. Byteswap the data lanes on the way back into the CPU core.
-     */
     if (rev) {
-        if (size > MO_8) {
-            mop ^= MO_BSWAP;
-        }
-        if (size < MO_32) {
-            tcg_gen_xori_i32(addr, addr, 3 - size);
-        }
+        handle_reversed_access(&mop, &addr);
     }
 
     /*
      * For system mode, enforce alignment if the cpu configuration
      * requires it.  For user-mode, the Linux kernel will have fixed up
Signature · 1 line
2.53.0
author
#1 lore raw

Hello,

Gentle ping. The fix just consist in allocating a new TCG temp for the address if we need to modify it.

Thanks

Luc

Quoted 111 lines · Luc Michel

Since 36a9529e60e0 the TCGv returned by compute_ldst_addr_type{a,b} for the address can be a target register or a constant. However do_{load,store} might modify it in the case of a reversed load/store.

Fix this by factoring out the reversed access logic and copying the address in a new TCGv_i32 if it needs to be modified.

Fixes 36a9529e60e0 (target/microblaze: Simplify compute_ldst_addr_type{a,b})
---
 target/microblaze/translate.c | 56 ++++++++++++++++++++---------------
 1 file changed, 32 insertions(+), 24 deletions(-)
target/microblaze/translate.c +32 -24
index 8b219afb5dd..9fb84e1bf9f 100644
@@ -701,30 +701,47 @@ static void gen_alignment_check_ea(DisasContext *dc, TCGv_i64 ea, int rb,
 static inline MemOp mo_endian(DisasContext *dc)
 {
     return dc->cfg->endi ? MO_LE : MO_BE;
 }
 
+static void handle_reversed_access(MemOp *mop, TCGv_i32 *addr)
+{
+    MemOp size = *mop & MO_SIZE;
+
+    /*
+     * When doing reverse accesses we need to do two things.
+     *
+     * 1. Reverse the address wrt endianness.
+     * 2. Byteswap the data lanes on the way back into the CPU core.
+     *
+     * Use a new TCGv_i32 to modify the address because the current one might be
+     * a register or a constant.
+     */
+
+    if (size > MO_8) {
+        *mop ^= MO_BSWAP;
+    }
+
+    if (size < MO_32) {
+        TCGv_i32 new_addr = tcg_temp_new_i32();
+
+        tcg_gen_xori_i32(new_addr, *addr, 3 - size);
+        *addr = new_addr;
+    }
+}
+
 static bool do_load(DisasContext *dc, int rd, TCGv_i32 addr, MemOp mop,
                     int mem_index, bool rev)
 {
+#ifndef CONFIG_USER_ONLY
     MemOp size = mop & MO_SIZE;
+#endif
 
     mop |= mo_endian(dc);
 
-    /*
-     * When doing reverse accesses we need to do two things.
-     *
-     * 1. Reverse the address wrt endianness.
-     * 2. Byteswap the data lanes on the way back into the CPU core.
-     */
     if (rev) {
-        if (size > MO_8) {
-            mop ^= MO_BSWAP;
-        }
-        if (size < MO_32) {
-            tcg_gen_xori_i32(addr, addr, 3 - size);
-        }
+        handle_reversed_access(&mop, &addr);
     }
 
     /*
      * For system mode, enforce alignment if the cpu configuration
      * requires it.  For user-mode, the Linux kernel will have fixed up
@@ -864,27 +881,18 @@ static bool trans_lwx(DisasContext *dc, arg_typea *arg)
 }
 
 static bool do_store(DisasContext *dc, int rd, TCGv_i32 addr, MemOp mop,
                      int mem_index, bool rev)
 {
+#ifndef CONFIG_USER_ONLY
     MemOp size = mop & MO_SIZE;
+#endif
 
     mop |= mo_endian(dc);
 
-    /*
-     * When doing reverse accesses we need to do two things.
-     *
-     * 1. Reverse the address wrt endianness.
-     * 2. Byteswap the data lanes on the way back into the CPU core.
-     */
     if (rev) {
-        if (size > MO_8) {
-            mop ^= MO_BSWAP;
-        }
-        if (size < MO_32) {
-            tcg_gen_xori_i32(addr, addr, 3 - size);
-        }
+        handle_reversed_access(&mop, &addr);
     }
 
     /*
      * For system mode, enforce alignment if the cpu configuration
      * requires it.  For user-mode, the Linux kernel will have fixed up
Signature · 1 line
2.53.0
#2 lore raw
In reply to Luc Michel

Hi Luc,

Quoted 3 lines · Luc Michel

Since 36a9529e60e0 the TCGv returned by compute_ldst_addr_type{a,b} for the address can be a target register or a constant. However do_{load,store} might modify it in the case of a reversed load/store.

Register are modifiable, aren't they? IIUC compute_ldst_addr_typeb() is not used by the reversed opcodes and the only issue is the 0 constant in compute_ldst_addr_typea(). Is that analysis correct?

Quoted 8 lines

Fix this by factoring out the reversed access logic and copying the address in a new TCGv_i32 if it needs to be modified.

Fixes 36a9529e60e0 (target/microblaze: Simplify compute_ldst_addr_type{a,b})
---
  target/microblaze/translate.c | 56 ++++++++++++++++++++---------------
  1 file changed, 32 insertions(+), 24 deletions(-)
author
#3 lore raw
Quoted 10 lines · Philippe Mathieu-Daudé

Hi Luc,

Quoted 3 lines · Luc Michel

Since 36a9529e60e0 the TCGv returned by compute_ldst_addr_type{a,b} for the address can be a target register or a constant. However do_{load,store} might modify it in the case of a reversed load/store.

Register are modifiable, aren't they? IIUC compute_ldst_addr_typeb() is not used by the reversed opcodes and the only issue is the 0 constant in compute_ldst_addr_typea(). Is that analysis correct?

Hi Phil,

The address registers are never modified by a load/store instruction. (See e.g., https://docs.amd.com/r/en-US/ug984-vivado-microblaze-ref/lw )

36a9529e60e0 introduced an optimization that can return the global TCGv used for rA or rB storage instead of copying it.

The bug appears in the reversed load/store case, where the do_load/do_store functions treat the `addr' TCGv_i32 as a temp and modify it. Since 36a9529e60e0, addr can alias rA, rB, or a 0 TCG constant. Those 3 cases are buggy.

Thanks

Luc

Quoted 10 lines
Quoted 9 lines

Fix this by factoring out the reversed access logic and copying the address in a new TCGv_i32 if it needs to be modified.

Fixes 36a9529e60e0 (target/microblaze: Simplify compute_ldst_addr_type{a,b})
---
  target/microblaze/translate.c | 56 ++++++++++++++++++++---------------
  1 file changed, 32 insertions(+), 24 deletions(-)
#4 lore raw
Quoted 12 lines · Luc Michel
Quoted 10 lines · Philippe Mathieu-Daudé

Hi Luc,

Quoted 3 lines · Luc Michel

Since 36a9529e60e0 the TCGv returned by compute_ldst_addr_type{a,b} for the address can be a target register or a constant. However do_{load,store} might modify it in the case of a reversed load/store.

Register are modifiable, aren't they? IIUC compute_ldst_addr_typeb() is not used by the reversed opcodes and the only issue is the 0 constant in compute_ldst_addr_typea(). Is that analysis correct?

Quoted 7 lines

Hi Phil,

The address registers are never modified by a load/store instruction. (See e.g., https://docs.amd.com/r/en-US/ug984-vivado-microblaze-ref/lw )

36a9529e60e0 introduced an optimization that can return the global TCGv used for rA or rB storage instead of copying it.

Doh indeed we don't want to update these.

Quoted 21 lines

The bug appears in the reversed load/store case, where the do_load/do_store functions treat the `addr' TCGv_i32 as a temp and modify it. Since 36a9529e60e0, addr can alias rA, rB, or a 0 TCG constant. Those 3 cases are buggy.

Thanks

Luc

Quoted 10 lines
Quoted 9 lines

Fix this by factoring out the reversed access logic and copying the address in a new TCGv_i32 if it needs to be modified.

Fixes 36a9529e60e0 (target/microblaze: Simplify compute_ldst_addr_type{a,b})
---
   target/microblaze/translate.c | 56 ++++++++++++++++++++---------------
   1 file changed, 32 insertions(+), 24 deletions(-)
Reviewed-by on v1 1/1
#5 lore raw
In reply to Luc Michel
Quoted 9 lines · Luc Michel

Since 36a9529e60e0 the TCGv returned by compute_ldst_addr_type{a,b} for the address can be a target register or a constant. However do_{load,store} might modify it in the case of a reversed load/store.

Fix this by factoring out the reversed access logic and copying the address in a new TCGv_i32 if it needs to be modified.

Fixes 36a9529e60e0 (target/microblaze: Simplify compute_ldst_addr_type{a,b})

LGTM!

Quoted 102 lines
---
 target/microblaze/translate.c | 56 ++++++++++++++++++++---------------
 1 file changed, 32 insertions(+), 24 deletions(-)
target/microblaze/translate.c +32 -24
index 8b219afb5dd..9fb84e1bf9f 100644
@@ -701,30 +701,47 @@ static void gen_alignment_check_ea(DisasContext *dc, TCGv_i64 ea, int rb,
 static inline MemOp mo_endian(DisasContext *dc)
 {
     return dc->cfg->endi ? MO_LE : MO_BE;
 }
 
+static void handle_reversed_access(MemOp *mop, TCGv_i32 *addr)
+{
+    MemOp size = *mop & MO_SIZE;
+
+    /*
+     * When doing reverse accesses we need to do two things.
+     *
+     * 1. Reverse the address wrt endianness.
+     * 2. Byteswap the data lanes on the way back into the CPU core.
+     *
+     * Use a new TCGv_i32 to modify the address because the current one might be
+     * a register or a constant.
+     */
+
+    if (size > MO_8) {
+        *mop ^= MO_BSWAP;
+    }
+
+    if (size < MO_32) {
+        TCGv_i32 new_addr = tcg_temp_new_i32();
+
+        tcg_gen_xori_i32(new_addr, *addr, 3 - size);
+        *addr = new_addr;
+    }
+}
+
 static bool do_load(DisasContext *dc, int rd, TCGv_i32 addr, MemOp mop,
                     int mem_index, bool rev)
 {
+#ifndef CONFIG_USER_ONLY
     MemOp size = mop & MO_SIZE;
+#endif
 
     mop |= mo_endian(dc);
 
-    /*
-     * When doing reverse accesses we need to do two things.
-     *
-     * 1. Reverse the address wrt endianness.
-     * 2. Byteswap the data lanes on the way back into the CPU core.
-     */
     if (rev) {
-        if (size > MO_8) {
-            mop ^= MO_BSWAP;
-        }
-        if (size < MO_32) {
-            tcg_gen_xori_i32(addr, addr, 3 - size);
-        }
+        handle_reversed_access(&mop, &addr);
     }
 
     /*
      * For system mode, enforce alignment if the cpu configuration
      * requires it.  For user-mode, the Linux kernel will have fixed up
@@ -864,27 +881,18 @@ static bool trans_lwx(DisasContext *dc, arg_typea *arg)
 }
 
 static bool do_store(DisasContext *dc, int rd, TCGv_i32 addr, MemOp mop,
                      int mem_index, bool rev)
 {
+#ifndef CONFIG_USER_ONLY
     MemOp size = mop & MO_SIZE;
+#endif
 
     mop |= mo_endian(dc);
 
-    /*
-     * When doing reverse accesses we need to do two things.
-     *
-     * 1. Reverse the address wrt endianness.
-     * 2. Byteswap the data lanes on the way back into the CPU core.
-     */
     if (rev) {
-        if (size > MO_8) {
-            mop ^= MO_BSWAP;
-        }
-        if (size < MO_32) {
-            tcg_gen_xori_i32(addr, addr, 3 - size);
-        }
+        handle_reversed_access(&mop, &addr);
     }
 
     /*
      * For system mode, enforce alignment if the cpu configuration
      * requires it.  For user-mode, the Linux kernel will have fixed up
Signature · 1 line
2.53.0
#6 lore raw
In reply to Luc Michel

Hi Luc,

Quoted 4 lines · Luc Michel

Since 36a9529e60e0 the TCGv returned by compute_ldst_addr_type{a,b} for the address can be a target register or a constant. However do_{load,store} might modify it in the case of a reversed load/store.

Quoted 8 lines

Fix this by factoring out the reversed access logic and copying the address in a new TCGv_i32 if it needs to be modified.

Fixes 36a9529e60e0 (target/microblaze: Simplify compute_ldst_addr_type{a,b})
---
  target/microblaze/translate.c | 56 ++++++++++++++++++++---------------
  1 file changed, 32 insertions(+), 24 deletions(-)

The tests in our CI didn't catch this problem.
Do you have an image I can use to test your fix?