Skip to contentSkip to conversation

target/ppc: Remove most MO_TE uses in cpu_ld/st_code/data()

PATCH Superseded Philippe Mathieu-Daudé 3 participants 28 messages Jan 22 – Feb 2

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 cover target/ppc: Remove most MO_TE uses in cpu_ld/st_code/data()

Since v4:
- Fixed incorrect rebase

Since v3 (DATA path):
- Remove MO_UNALN in do_HASH (rth)
- Complete CODE path

Philippe Mathieu-Daudé (11):
target/ppc: Inline cpu_ldl_data_ra() calls in ICBI helpers target/ppc: Inline cpu_ld/st_mmuidx_ra() calls in memory helpers target/ppc: Inline cpu_ld/st_data_ra() calls in do_hash() target/ppc: Expect page translation hash addresses to be aligned target/ppc: Introduce the mo_endian_env() helper target/ppc: Introduce ppc_code_endian() helper target/ppc: Inline need_byteswap() and replace translator_ldl_swap() target/ppc: Check endianness via env in ppc_disas_set_info() target/ppc: Introduce ppc_env_is_little_endian() helper target/ppc: Check endianness at runtime in mo_endian_env() target/ppc: Inline cpu_ldl_code() call in ppc_ldl_code()

 target/ppc/internal.h        | 11 +++++++
 target/ppc/cpu_init.c        |  9 ++----
 target/ppc/gdbstub.c         |  2 +-
 target/ppc/mem_helper.c      | 60 ++++++++++++++++++++++++++----------
 target/ppc/tcg-excp_helper.c | 27 +++++++---------
 target/ppc/translate.c       | 15 +++------
 6 files changed, 75 insertions(+), 49 deletions(-)
Signature · 1 line
2.52.0
#1 lore raw

PATCH v5 1/11 target/ppc: Inline cpu_ldl_data_ra() calls in ICBI helpers

Inline the cpu_ldl_data_ra() call in preparation of removing it. Since the returned value is discarded, don't bother to set the access endianness.

---
 target/ppc/mem_helper.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
target/ppc/mem_helper.c +8 -3
index 7318b02c46a..636ad769a93 100644
@@ -320,22 +320,27 @@ void helper_dcbzl(CPUPPCState *env, target_ulong addr)
 
 void helper_icbi(CPUPPCState *env, target_ulong addr)
 {
+    unsigned mmu_idx = cpu_mmu_index(env_cpu(env), false);
+    MemOpIdx oi = make_memop_idx(MO_UL | MO_UNALN, mmu_idx);
+
     addr &= ~(env->dcache_line_size - 1);
     /*
      * Invalidate one cache line :
      * PowerPC specification says this is to be treated like a load
      * (not a fetch) by the MMU. To be sure it will be so,
-     * do the load "by hand".
+     * do the load "by hand". As the returned data is not consumed,
+     * endianness is irrelevant.
      */
-    cpu_ldl_data_ra(env, addr, GETPC());
+    cpu_ldl_mmu(env, addr, oi, GETPC());
 }
 
 void helper_icbiep(CPUPPCState *env, target_ulong addr)
 {
 #if !defined(CONFIG_USER_ONLY)
+    MemOpIdx oi = make_memop_idx(MO_UL | MO_UNALN, PPC_TLB_EPID_LOAD);
     /* See comments above */
     addr &= ~(env->dcache_line_size - 1);
-    cpu_ldl_mmuidx_ra(env, addr, PPC_TLB_EPID_LOAD, GETPC());
+    cpu_ldl_mmu(env, addr, oi, GETPC());
 #endif
 }
 
Signature · 1 line
2.52.0
Reviewed-by Anton Johansson on v5 1/11
#2 lore raw

Re: [PATCH v5 01/11] target/ppc: Inline cpu_ldl_data_ra() calls in ICBI helpers

Quoted 8 lines · Philippe Mathieu-Daudé

Inline the cpu_ldl_data_ra() call in preparation of removing it. Since the returned value is discarded, don't bother to set the access endianness.

---
 target/ppc/mem_helper.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
#3 lore raw

PATCH v5 2/11 target/ppc: Inline cpu_ld/st_mmuidx_ra() calls in memory helpers

In reply to Philippe Mathieu-Daudé

In preparation of removing the cpu_ld*_mmuidx_ra() and cpu_st*_mmuidx_ra() calls, inline them.

---
 target/ppc/mem_helper.c | 41 ++++++++++++++++++++++++++++++++---------
 1 file changed, 32 insertions(+), 9 deletions(-)
target/ppc/mem_helper.c +32 -9
index 636ad769a93..90546967593 100644
@@ -88,8 +88,11 @@ void helper_lmw(CPUPPCState *env, target_ulong addr, uint32_t reg)
         }
     } else {
         /* Slow path -- at least some of the operation requires i/o.  */
+        MemOp op = MO_TE | MO_UL | MO_UNALN;
+        MemOpIdx oi = make_memop_idx(op, mmu_idx);
+
         for (; reg < 32; reg++) {
-            env->gpr[reg] = cpu_ldl_mmuidx_ra(env, addr, mmu_idx, raddr);
+            env->gpr[reg] = cpu_ldl_mmu(env, addr, oi, raddr);
             addr = addr_add(env, addr, 4);
         }
     }
@@ -111,7 +114,10 @@ void helper_stmw(CPUPPCState *env, target_ulong addr, uint32_t reg)
     } else {
         /* Slow path -- at least some of the operation requires i/o.  */
         for (; reg < 32; reg++) {
-            cpu_stl_mmuidx_ra(env, addr, env->gpr[reg], mmu_idx, raddr);
+            MemOp op = MO_TE | MO_UL | MO_UNALN;
+            MemOpIdx oi = make_memop_idx(op, mmu_idx);
+
+            cpu_stl_mmu(env, addr, env->gpr[reg], oi, raddr);
             addr = addr_add(env, addr, 4);
         }
     }
@@ -152,9 +158,12 @@ static void do_lsw(CPUPPCState *env, target_ulong addr, uint32_t nb,
             break;
         }
     } else {
+        MemOp op = MO_TE | MO_UL | MO_UNALN;
+        MemOpIdx oi = make_memop_idx(op, mmu_idx);
+
         /* Slow path -- at least some of the operation requires i/o.  */
         for (; nb > 3; nb -= 4) {
-            env->gpr[reg] = cpu_ldl_mmuidx_ra(env, addr, mmu_idx, raddr);
+            env->gpr[reg] = cpu_ldl_mmu(env, addr, oi, raddr);
             reg = (reg + 1) % 32;
             addr = addr_add(env, addr, 4);
         }
@@ -165,10 +174,14 @@ static void do_lsw(CPUPPCState *env, target_ulong addr, uint32_t nb,
             val = cpu_ldub_mmuidx_ra(env, addr, mmu_idx, raddr) << 24;
             break;
         case 2:
-            val = cpu_lduw_mmuidx_ra(env, addr, mmu_idx, raddr) << 16;
+            op = MO_TE | MO_UW | MO_UNALN;
+            oi = make_memop_idx(op, mmu_idx);
+            val = cpu_ldw_mmu(env, addr, oi, raddr) << 16;
             break;
         case 3:
-            val = cpu_lduw_mmuidx_ra(env, addr, mmu_idx, raddr) << 16;
+            op = MO_TE | MO_UW | MO_UNALN;
+            oi = make_memop_idx(op, mmu_idx);
+            val = cpu_ldw_mmu(env, addr, oi, raddr) << 16;
             addr = addr_add(env, addr, 2);
             val |= cpu_ldub_mmuidx_ra(env, addr, mmu_idx, raddr) << 8;
             break;
@@ -241,8 +254,11 @@ void helper_stsw(CPUPPCState *env, target_ulong addr, uint32_t nb,
             break;
         }
     } else {
+        MemOp op = MO_TE | MO_UL | MO_UNALN;
+        MemOpIdx oi = make_memop_idx(op, mmu_idx);
+
         for (; nb > 3; nb -= 4) {
-            cpu_stl_mmuidx_ra(env, addr, env->gpr[reg], mmu_idx, raddr);
+            cpu_stl_mmu(env, addr, env->gpr[reg], oi, raddr);
             reg = (reg + 1) % 32;
             addr = addr_add(env, addr, 4);
         }
@@ -252,10 +268,14 @@ void helper_stsw(CPUPPCState *env, target_ulong addr, uint32_t nb,
             cpu_stb_mmuidx_ra(env, addr, val >> 24, mmu_idx, raddr);
             break;
         case 2:
-            cpu_stw_mmuidx_ra(env, addr, val >> 16, mmu_idx, raddr);
+            op = MO_TE | MO_UW | MO_UNALN;
+            oi = make_memop_idx(op, mmu_idx);
+            cpu_stw_mmu(env, addr, val >> 16, oi, raddr);
             break;
         case 3:
-            cpu_stw_mmuidx_ra(env, addr, val >> 16, mmu_idx, raddr);
+            op = MO_TE | MO_UW | MO_UNALN;
+            oi = make_memop_idx(op, mmu_idx);
+            cpu_stw_mmu(env, addr, val >> 16, oi, raddr);
             addr = addr_add(env, addr, 2);
             cpu_stb_mmuidx_ra(env, addr, val >> 8, mmu_idx, raddr);
             break;
@@ -284,8 +304,11 @@ static void dcbz_common(CPUPPCState *env, target_ulong addr,
     haddr = probe_write(env, addr, dcbz_size, mmu_idx, retaddr);
     if (unlikely(!haddr)) {
         /* Slow path */
+        MemOp op = MO_TE | MO_UQ | MO_UNALN;
+        MemOpIdx oi = make_memop_idx(op, mmu_idx);
+
         for (int i = 0; i < dcbz_size; i += 8) {
-            cpu_stq_mmuidx_ra(env, addr + i, 0, mmu_idx, retaddr);
+            cpu_stq_mmu(env, addr + i, 0, oi, retaddr);
         }
         return;
     }
Signature · 1 line
2.52.0
Reviewed-by Anton Johansson on v5 2/11
#4 lore raw

Re: [PATCH v5 02/11] target/ppc: Inline cpu_ld/st_mmuidx_ra() calls in memory helpers

Quoted 7 lines · Philippe Mathieu-Daudé

In preparation of removing the cpu_ld*_mmuidx_ra() and cpu_st*_mmuidx_ra() calls, inline them.

---
 target/ppc/mem_helper.c | 41 ++++++++++++++++++++++++++++++++---------
 1 file changed, 32 insertions(+), 9 deletions(-)
#5 lore raw

PATCH v5 3/11 target/ppc: Inline cpu_ld/st_data_ra() calls in do_hash()

In reply to Philippe Mathieu-Daudé

In preparation of removing the cpu_ld*_data_ra() and cpu_st*_data_ra() calls, inline them. No logical change intended.

We note the page translation hash address is expected to be aligned, so the MO_UNALN flag is misleading. Next commit will remove it.

---
 target/ppc/tcg-excp_helper.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
target/ppc/tcg-excp_helper.c +7 -3
index edecfb85725..6f5d82af636 100644
@@ -160,14 +160,18 @@ static void do_hash(CPUPPCState *env, target_ulong ea, target_ulong ra,
                     target_ulong rb, uint64_t key, bool store)
 {
     uint64_t calculated_hash = hash_digest(ra, rb, key), loaded_hash;
+    unsigned mmu_idx = cpu_mmu_index(env_cpu(env), false);
+    MemOp op = MO_TE | MO_UQ | MO_UNALN;
+    MemOpIdx oi = make_memop_idx(op, mmu_idx);
+    uintptr_t retaddr = GETPC();
 
     if (store) {
-        cpu_stq_data_ra(env, ea, calculated_hash, GETPC());
+        cpu_stq_mmu(env, ea, calculated_hash, oi, retaddr);
     } else {
-        loaded_hash = cpu_ldq_data_ra(env, ea, GETPC());
+        loaded_hash = cpu_ldq_mmu(env, ea, oi, retaddr);
         if (loaded_hash != calculated_hash) {
             raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
-                POWERPC_EXCP_TRAP, GETPC());
+                                   POWERPC_EXCP_TRAP, retaddr);
         }
     }
 }
Signature · 1 line
2.52.0
Reviewed-by Anton Johansson on v5 3/11
#6 lore raw

Re: [PATCH v5 03/11] target/ppc: Inline cpu_ld/st_data_ra() calls in do_hash()

Quoted 4 lines · Philippe Mathieu-Daudé

In preparation of removing the cpu_ld*_data_ra() and cpu_st*_data_ra() calls, inline them. No logical change intended.

Quoted 8 lines

We note the page translation hash address is expected to be aligned, so the MO_UNALN flag is misleading. Next commit will remove it.

---
 target/ppc/tcg-excp_helper.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
#7 lore raw

PATCH v5 4/11 target/ppc: Expect page translation hash addresses to be aligned

In reply to Philippe Mathieu-Daudé

The page translation hash addresses are aligned:
remove the misleading MO_UNALN flag.

---
 target/ppc/tcg-excp_helper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/ppc/tcg-excp_helper.c +1 -1
index 6f5d82af636..a8c67364669 100644
@@ -161,7 +161,7 @@ static void do_hash(CPUPPCState *env, target_ulong ea, target_ulong ra,
 {
     uint64_t calculated_hash = hash_digest(ra, rb, key), loaded_hash;
     unsigned mmu_idx = cpu_mmu_index(env_cpu(env), false);
-    MemOp op = MO_TE | MO_UQ | MO_UNALN;
+    MemOp op = MO_TE | MO_UQ;
     MemOpIdx oi = make_memop_idx(op, mmu_idx);
     uintptr_t retaddr = GETPC();
 
Signature · 1 line
2.52.0
Reviewed-by Anton Johansson on v5 4/11
#8 lore raw

Re: [PATCH v5 04/11] target/ppc: Expect page translation hash addresses to be aligned

Quoted 8 lines · Philippe Mathieu-Daudé

The page translation hash addresses are aligned:
remove the misleading MO_UNALN flag.

---
 target/ppc/tcg-excp_helper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
#9 lore raw

PATCH v5 5/11 target/ppc: Introduce the mo_endian_env() helper

In reply to Philippe Mathieu-Daudé

Introduce mo_endian_env() which returns the endian MemOp from the vCPU env pointer. Keep it hardcoded as MO_TE.

---
 target/ppc/internal.h        |  6 ++++++
 target/ppc/mem_helper.c      | 18 +++++++++---------
 target/ppc/tcg-excp_helper.c |  2 +-
 3 files changed, 16 insertions(+), 10 deletions(-)
target/ppc/internal.h +6 -0
index 58f315ffcf5..09333bed342 100644
@@ -19,10 +19,16 @@
 #define PPC_INTERNAL_H
 
 #include "exec/breakpoint.h"
+#include "exec/memop.h"
 #include "hw/core/registerfields.h"
 #include "exec/page-protection.h"
 #include "accel/tcg/tb-cpu-state.h"
 
+static inline MemOp mo_endian_env(const CPUPPCState *env)
+{
+    return MO_TE;
+}
+
 /* PM instructions */
 typedef enum {
     PPC_PM_DOZE,
target/ppc/mem_helper.c +9 -9
index 90546967593..89718441310 100644
@@ -88,7 +88,7 @@ void helper_lmw(CPUPPCState *env, target_ulong addr, uint32_t reg)
         }
     } else {
         /* Slow path -- at least some of the operation requires i/o.  */
-        MemOp op = MO_TE | MO_UL | MO_UNALN;
+        MemOp op = mo_endian_env(env) | MO_UL | MO_UNALN;
         MemOpIdx oi = make_memop_idx(op, mmu_idx);
 
         for (; reg < 32; reg++) {
@@ -114,7 +114,7 @@ void helper_stmw(CPUPPCState *env, target_ulong addr, uint32_t reg)
     } else {
         /* Slow path -- at least some of the operation requires i/o.  */
         for (; reg < 32; reg++) {
-            MemOp op = MO_TE | MO_UL | MO_UNALN;
+            MemOp op = mo_endian_env(env) | MO_UL | MO_UNALN;
             MemOpIdx oi = make_memop_idx(op, mmu_idx);
 
             cpu_stl_mmu(env, addr, env->gpr[reg], oi, raddr);
@@ -158,7 +158,7 @@ static void do_lsw(CPUPPCState *env, target_ulong addr, uint32_t nb,
             break;
         }
     } else {
-        MemOp op = MO_TE | MO_UL | MO_UNALN;
+        MemOp op = mo_endian_env(env) | MO_UL | MO_UNALN;
         MemOpIdx oi = make_memop_idx(op, mmu_idx);
 
         /* Slow path -- at least some of the operation requires i/o.  */
@@ -174,12 +174,12 @@ static void do_lsw(CPUPPCState *env, target_ulong addr, uint32_t nb,
             val = cpu_ldub_mmuidx_ra(env, addr, mmu_idx, raddr) << 24;
             break;
         case 2:
-            op = MO_TE | MO_UW | MO_UNALN;
+            op = mo_endian_env(env) | MO_UW | MO_UNALN;
             oi = make_memop_idx(op, mmu_idx);
             val = cpu_ldw_mmu(env, addr, oi, raddr) << 16;
             break;
         case 3:
-            op = MO_TE | MO_UW | MO_UNALN;
+            op = mo_endian_env(env) | MO_UW | MO_UNALN;
             oi = make_memop_idx(op, mmu_idx);
             val = cpu_ldw_mmu(env, addr, oi, raddr) << 16;
             addr = addr_add(env, addr, 2);
@@ -254,7 +254,7 @@ void helper_stsw(CPUPPCState *env, target_ulong addr, uint32_t nb,
             break;
         }
     } else {
-        MemOp op = MO_TE | MO_UL | MO_UNALN;
+        MemOp op = mo_endian_env(env) | MO_UL | MO_UNALN;
         MemOpIdx oi = make_memop_idx(op, mmu_idx);
 
         for (; nb > 3; nb -= 4) {
@@ -268,12 +268,12 @@ void helper_stsw(CPUPPCState *env, target_ulong addr, uint32_t nb,
             cpu_stb_mmuidx_ra(env, addr, val >> 24, mmu_idx, raddr);
             break;
         case 2:
-            op = MO_TE | MO_UW | MO_UNALN;
+            op = mo_endian_env(env) | MO_UW | MO_UNALN;
             oi = make_memop_idx(op, mmu_idx);
             cpu_stw_mmu(env, addr, val >> 16, oi, raddr);
             break;
         case 3:
-            op = MO_TE | MO_UW | MO_UNALN;
+            op = mo_endian_env(env) | MO_UW | MO_UNALN;
             oi = make_memop_idx(op, mmu_idx);
             cpu_stw_mmu(env, addr, val >> 16, oi, raddr);
             addr = addr_add(env, addr, 2);
@@ -304,7 +304,7 @@ static void dcbz_common(CPUPPCState *env, target_ulong addr,
     haddr = probe_write(env, addr, dcbz_size, mmu_idx, retaddr);
     if (unlikely(!haddr)) {
         /* Slow path */
-        MemOp op = MO_TE | MO_UQ | MO_UNALN;
+        MemOp op = mo_endian_env(env) | MO_UQ | MO_UNALN;
         MemOpIdx oi = make_memop_idx(op, mmu_idx);
 
         for (int i = 0; i < dcbz_size; i += 8) {
target/ppc/tcg-excp_helper.c +1 -1
index a8c67364669..77d21523a5b 100644
@@ -161,7 +161,7 @@ static void do_hash(CPUPPCState *env, target_ulong ea, target_ulong ra,
 {
     uint64_t calculated_hash = hash_digest(ra, rb, key), loaded_hash;
     unsigned mmu_idx = cpu_mmu_index(env_cpu(env), false);
-    MemOp op = MO_TE | MO_UQ;
+    MemOp op = mo_endian_env(env) | MO_UQ;
     MemOpIdx oi = make_memop_idx(op, mmu_idx);
     uintptr_t retaddr = GETPC();
 
Signature · 1 line
2.52.0
Reviewed-by Anton Johansson on v5 5/11
#10 lore raw

Re: [PATCH v5 05/11] target/ppc: Introduce the mo_endian_env() helper

Quoted 9 lines · Philippe Mathieu-Daudé

Introduce mo_endian_env() which returns the endian MemOp from the vCPU env pointer. Keep it hardcoded as MO_TE.

---
 target/ppc/internal.h        |  6 ++++++
 target/ppc/mem_helper.c      | 18 +++++++++---------
 target/ppc/tcg-excp_helper.c |  2 +-
 3 files changed, 16 insertions(+), 10 deletions(-)

Minor comment but the difference between mo_endian_env() and ppc_code_endian() is a bit unclear from the names. I think especially since mo_endian_env() is used in ppc_ld_code() later on. Maybe ppc_code_endian() -> ppc_disas_endian() || ppc_tb_endian()?

Regardless of namechange, just throwing out my 2c

#11 lore raw

Re: [PATCH v5 05/11] target/ppc: Introduce the mo_endian_env() helper

Quoted 11 lines · Anton Johansson
Quoted 9 lines · Philippe Mathieu-Daudé

Introduce mo_endian_env() which returns the endian MemOp from the vCPU env pointer. Keep it hardcoded as MO_TE.

---
  target/ppc/internal.h        |  6 ++++++
  target/ppc/mem_helper.c      | 18 +++++++++---------
  target/ppc/tcg-excp_helper.c |  2 +-
  3 files changed, 16 insertions(+), 10 deletions(-)
Quoted 4 lines

Minor comment but the difference between mo_endian_env() and ppc_code_endian() is a bit unclear from the names. I think especially since mo_endian_env() is used in ppc_ld_code() later on. Maybe ppc_code_endian() -> ppc_disas_endian() || ppc_tb_endian()?

Or mo_endian_env() -> ppc_data_endian()?

Quoted 4 lines

Regardless of namechange, just throwing out my 2c

Thanks!

#12 lore raw

PATCH v5 6/11 target/ppc: Introduce ppc_code_endian() helper

In reply to Philippe Mathieu-Daudé

Introduce the ppc_code_endian() helper which returns the MemOp endianness for the CODE path.

Use it in need_byteswap(), removing one TARGET_BIG_ENDIAN.

---
 target/ppc/translate.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
target/ppc/translate.c +6 -5
index 17e6d07c8c2..4a9199a4473 100644
@@ -214,14 +214,15 @@ static inline bool is_ppe(const DisasContext *ctx)
     return !!(ctx->flags & POWERPC_FLAG_PPE42);
 }
 
+static inline MemOp ppc_code_endian(const DisasContext *ctx)
+{
+    return MO_BE ^ (ctx->le_mode * MO_BSWAP);
+}
+
 /* Return true iff byteswap is needed in a scalar memop */
 static inline bool need_byteswap(const DisasContext *ctx)
 {
-#if TARGET_BIG_ENDIAN
-     return ctx->le_mode;
-#else
-     return !ctx->le_mode;
-#endif
+    return ppc_code_endian(ctx) != MO_TE;
 }
 
 /* True when active word size < size of target_long.  */
Signature · 1 line
2.52.0
Reviewed-by Anton Johansson on v5 6/11
#13 lore raw

Re: [PATCH v5 06/11] target/ppc: Introduce ppc_code_endian() helper

Quoted 15 lines · Philippe Mathieu-Daudé

Introduce the ppc_code_endian() helper which returns the MemOp endianness for the CODE path.

Use it in need_byteswap(), removing one TARGET_BIG_ENDIAN.

---
 target/ppc/translate.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
target/ppc/translate.c +0 -0
index 17e6d07c8c2..4a9199a4473 100644

target/ppc/translate.c static inline bool is_ppe(const DisasContext *ctx) · line 220

Quoted 7 lines
target/ppc/translate.c +4 -0
@@ -214,14 +214,15 @@ static inline bool is_ppe(const DisasContext *ctx)
     return !!(ctx->flags & POWERPC_FLAG_PPE42);
 }
 
+static inline MemOp ppc_code_endian(const DisasContext *ctx)
+{
+    return MO_BE ^ (ctx->le_mode * MO_BSWAP);
+}

This was not super obvious to me, is this a common pattern?

Either way the behaviour is correct:

#14 lore raw

Re: [PATCH v5 06/11] target/ppc: Introduce ppc_code_endian() helper

Quoted 19 lines · Anton Johansson
Quoted 18 lines · Philippe Mathieu-Daudé

Introduce the ppc_code_endian() helper which returns the MemOp endianness for the CODE path.

Use it in need_byteswap(), removing one TARGET_BIG_ENDIAN.

---
 target/ppc/translate.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
target/ppc/translate.c +0 -0
index 17e6d07c8c2..4a9199a4473 100644
@@ -214,14 +214,15 @@ static inline bool is_ppe(const DisasContext *ctx)
     return !!(ctx->flags & POWERPC_FLAG_PPE42);
 }
 
Quoted 6 lines

target/ppc/translate.c static inline bool is_ppe(const DisasContext *ctx) · line 220

Quoted 4 lines
target/ppc/translate.c +4 -0
+static inline MemOp ppc_code_endian(const DisasContext *ctx)
+{
+    return MO_BE ^ (ctx->le_mode * MO_BSWAP);
+}

This was not super obvious to me, is this a common pattern?

Ignore this comment, I see we do it for translator_ld*()!:)

#15 lore raw

Re: [PATCH v5 06/11] target/ppc: Introduce ppc_code_endian() helper

Quoted 20 lines · Anton Johansson
Quoted 19 lines · Anton Johansson
Quoted 18 lines · Philippe Mathieu-Daudé

Introduce the ppc_code_endian() helper which returns the MemOp endianness for the CODE path.

Use it in need_byteswap(), removing one TARGET_BIG_ENDIAN.

---
  target/ppc/translate.c | 11 ++++++-----
  1 file changed, 6 insertions(+), 5 deletions(-)
target/ppc/translate.c +0 -0
index 17e6d07c8c2..4a9199a4473 100644
@@ -214,14 +214,15 @@ static inline bool is_ppe(const DisasContext *ctx)
      return !!(ctx->flags & POWERPC_FLAG_PPE42);
  }
 
Quoted 8 lines
Quoted 6 lines

target/ppc/translate.c static inline bool is_ppe(const DisasContext *ctx) · line 220

Quoted 4 lines
target/ppc/translate.c +4 -0
+static inline MemOp ppc_code_endian(const DisasContext *ctx)
+{
+    return MO_BE ^ (ctx->le_mode * MO_BSWAP);
+}

This was not super obvious to me, is this a common pattern?

Ignore this comment, I see we do it for translator_ld*()!:)

I agree that even though it's used there, it's far from being obvious when reading it :)

#16 lore raw

Re: [PATCH v5 06/11] target/ppc: Introduce ppc_code_endian() helper

Quoted 28 lines · Pierrick Bouvier
Quoted 27 lines · Anton Johansson
Quoted 25 lines · Anton Johansson
Quoted 15 lines · Philippe Mathieu-Daudé

Introduce the ppc_code_endian() helper which returns the MemOp endianness for the CODE path.

Use it in need_byteswap(), removing one TARGET_BIG_ENDIAN.

---
  target/ppc/translate.c | 11 ++++++-----

  1 file changed, 6 insertions(+), 5 deletions(-)
target/ppc/translate.c +0 -0
index 17e6d07c8c2..4a9199a4473 100644

target/ppc/translate.c static inline bool is_ppe(const DisasContext · line 220

Quoted 7 lines
target/ppc/translate.c +4 -0
@@ -214,14 +214,15 @@ static inline bool is_ppe(const DisasContext 
 *ctx)
       return !!(ctx->flags & POWERPC_FLAG_PPE42);
   }
+static inline MemOp ppc_code_endian(const DisasContext *ctx)
+{
+    return MO_BE ^ (ctx->le_mode * MO_BSWAP);
+}

This was not super obvious to me, is this a common pattern?

Quoted 3 lines
Quoted 1 line

Ignore this comment, I see we do it for translator_ld*()!:)

I agree that even though it's used there, it's far from being obvious when reading it :)

In order to clarify, I amended:

---
Note, the target MemOp endianness can be evaluated as (see commit 5c43a750b67 "accel/tcg: Implement translator_ld*_end"):

MO_TE ^ (do_swap * MO_BSWAP)

For PPC we use the DisasContext::le_mode field to swap the default (big-endian) order, so to get the PPC MemOp endianness we can directly use:

MO_BE ^ (ctx->le_mode * MO_BSWAP)
---

#17 lore raw

PATCH v5 7/11 target/ppc: Inline need_byteswap() and replace translator_ldl_swap()

In reply to Philippe Mathieu-Daudé

Rather than using a boolean with translator_ldl_swap(), get the MemOp endianness with ppc_code_endian() and pass it to translator_ldl_end().

---
 target/ppc/translate.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)
target/ppc/translate.c +3 -9
index 4a9199a4473..2ea18e7981c 100644
@@ -219,12 +219,6 @@ static inline MemOp ppc_code_endian(const DisasContext *ctx)
     return MO_BE ^ (ctx->le_mode * MO_BSWAP);
 }
 
-/* Return true iff byteswap is needed in a scalar memop */
-static inline bool need_byteswap(const DisasContext *ctx)
-{
-    return ppc_code_endian(ctx) != MO_TE;
-}
-
 /* True when active word size < size of target_long.  */
 #ifdef TARGET_PPC64
 # define NARROW_MODE(C)  (!(C)->sf_mode)
@@ -6589,6 +6583,7 @@ static void ppc_tr_translate_insn(DisasContextBase *dcbase, CPUState *cs)
     DisasContext *ctx = container_of(dcbase, DisasContext, base);
     PowerPCCPU *cpu = POWERPC_CPU(cs);
     CPUPPCState *env = cpu_env(cs);
+    MemOp mo_endian = ppc_code_endian(ctx);
     target_ulong pc;
     uint32_t insn;
     bool ok;
@@ -6598,7 +6593,7 @@ static void ppc_tr_translate_insn(DisasContextBase *dcbase, CPUState *cs)
               ctx->base.pc_next, ctx->mem_idx, (int)msr_ir);
 
     ctx->cia = pc = ctx->base.pc_next;
-    insn = translator_ldl_swap(env, dcbase, pc, need_byteswap(ctx));
+    insn = translator_ldl_end(env, dcbase, pc, mo_endian);
     ctx->base.pc_next = pc += 4;
 
     if (!is_prefix_insn(ctx, insn)) {
@@ -6614,8 +6609,7 @@ static void ppc_tr_translate_insn(DisasContextBase *dcbase, CPUState *cs)
         gen_exception_err(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_INSN);
         ok = true;
     } else {
-        uint32_t insn2 = translator_ldl_swap(env, dcbase, pc,
-                                             need_byteswap(ctx));
+        uint32_t insn2 = translator_ldl_end(env, dcbase, pc, mo_endian);
         ctx->base.pc_next = pc += 4;
         ok = decode_insn64(ctx, deposit64(insn2, 32, 32, insn));
     }
Signature · 1 line
2.52.0
Reviewed-by Anton Johansson on v5 7/11
#18 lore raw

Re: [PATCH v5 07/11] target/ppc: Inline need_byteswap() and replace translator_ldl_swap()

Quoted 8 lines · Philippe Mathieu-Daudé

Rather than using a boolean with translator_ldl_swap(), get the MemOp endianness with ppc_code_endian() and pass it to translator_ldl_end().

---
 target/ppc/translate.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)

Nice!

#19 lore raw

PATCH v5 8/11 target/ppc: Check endianness via env in ppc_disas_set_info()

In reply to Philippe Mathieu-Daudé

disas_set_info() shouldn't bother with env->hflags, access env->msr directly.

---
 target/ppc/cpu_init.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/ppc/cpu_init.c +1 -1
index 58816c51a74..0808284b722 100644
@@ -7456,7 +7456,7 @@ static void ppc_disas_set_info(CPUState *cs, disassemble_info *info)
 {
     CPUPPCState *env = cpu_env(cs);
 
-    if ((env->hflags >> MSR_LE) & 1) {
+    if ((env->msr >> MSR_LE) & 1) {
         info->endian = BFD_ENDIAN_LITTLE;
     } else {
         info->endian = BFD_ENDIAN_BIG;
Signature · 1 line
2.52.0
#21 lore raw

PATCH v5 9/11 target/ppc: Introduce ppc_env_is_little_endian() helper

In reply to Philippe Mathieu-Daudé

Centralize endianness check on MSR via a common helper.

---
 target/ppc/internal.h   | 5 +++++
 target/ppc/cpu_init.c   | 9 +++------
 target/ppc/gdbstub.c    | 2 +-
 target/ppc/mem_helper.c | 8 ++++----
 4 files changed, 13 insertions(+), 11 deletions(-)
target/ppc/internal.h +5 -0
index 09333bed342..714629079b5 100644
@@ -24,6 +24,11 @@
 #include "exec/page-protection.h"
 #include "accel/tcg/tb-cpu-state.h"
 
+static inline bool ppc_env_is_little_endian(const CPUPPCState *env)
+{
+    return FIELD_EX64(env->msr, MSR, LE);
+}
+
 static inline MemOp mo_endian_env(const CPUPPCState *env)
 {
     return MO_TE;
target/ppc/cpu_init.c +3 -6
index 0808284b722..c5cec7c2ed9 100644
@@ -7364,7 +7364,7 @@ static bool ppc_cpu_is_big_endian(CPUState *cs)
 {
     cpu_synchronize_state(cs);
 
-    return !FIELD_EX64(cpu_env(cs)->msr, MSR, LE);
+    return !ppc_env_is_little_endian(cpu_env(cs));
 }
 
 static bool ppc_get_irq_stats(InterruptStatsProvider *obj,
@@ -7456,11 +7456,8 @@ static void ppc_disas_set_info(CPUState *cs, disassemble_info *info)
 {
     CPUPPCState *env = cpu_env(cs);
 
-    if ((env->msr >> MSR_LE) & 1) {
-        info->endian = BFD_ENDIAN_LITTLE;
-    } else {
-        info->endian = BFD_ENDIAN_BIG;
-    }
+    info->endian = ppc_env_is_little_endian(env) ? BFD_ENDIAN_LITTLE
+                                                 : BFD_ENDIAN_BIG;
     info->mach = env->bfd_mach;
     if (!env->bfd_mach) {
 #ifdef TARGET_PPC64
target/ppc/gdbstub.c +1 -1
index 3b28d4e21c7..b19c0f1ea9c 100644
@@ -84,7 +84,7 @@ static int ppc_gdb_register_len(int n)
 void ppc_maybe_bswap_register(CPUPPCState *env, uint8_t *mem_buf, int len)
 {
 #ifndef CONFIG_USER_ONLY
-    if (!FIELD_EX64(env->msr, MSR, LE)) {
+    if (!ppc_env_is_little_endian(env)) {
         /* do nothing */
     } else if (len == 4) {
         bswap32s((uint32_t *)mem_buf);
target/ppc/mem_helper.c +4 -4
index 89718441310..31057c6440a 100644
@@ -420,7 +420,7 @@ target_ulong helper_lscbx(CPUPPCState *env, target_ulong addr, uint32_t reg,
         int adjust = HI_IDX * (n_elems - 1);                    \
         int sh = sizeof(r->element[0]) >> 1;                    \
         int index = (addr & 0xf) >> sh;                         \
-        bool byteswap = FIELD_EX64(env->msr, MSR, LE);          \
+        bool byteswap = ppc_env_is_little_endian(env);          \
                                                                 \
         if (byteswap) {                                         \
             index = n_elems - index - 1;                        \
@@ -446,7 +446,7 @@ LVE(LVEWX, cpu_ldl_be_data_ra, bswap32, u32)
         int adjust = HI_IDX * (n_elems - 1);                            \
         int sh = sizeof(r->element[0]) >> 1;                            \
         int index = (addr & 0xf) >> sh;                                 \
-        bool byteswap = FIELD_EX64(env->msr, MSR, LE);                  \
+        bool byteswap = ppc_env_is_little_endian(env);                  \
                                                                         \
         if (byteswap) {                                                 \
             index = n_elems - index - 1;                                \
@@ -479,7 +479,7 @@ void helper_##name(CPUPPCState *env, target_ulong addr,                 \
     t.s128 = int128_zero();                                             \
     if (nb) {                                                           \
         nb = (nb >= 16) ? 16 : nb;                                      \
-        if (FIELD_EX64(env->msr, MSR, LE) && !lj) {                     \
+        if (ppc_env_is_little_endian(env) && !lj) {                     \
             for (i = 16; i > 16 - nb; i--) {                            \
                 t.VsrB(i - 1) = cpu_ldub_data_ra(env, addr, GETPC());   \
                 addr = addr_add(env, addr, 1);                          \
@@ -510,7 +510,7 @@ void helper_##name(CPUPPCState *env, target_ulong addr,           \
     }                                                             \
                                                                   \
     nb = (nb >= 16) ? 16 : nb;                                    \
-    if (FIELD_EX64(env->msr, MSR, LE) && !lj) {                   \
+    if (ppc_env_is_little_endian(env) && !lj) {                   \
         for (i = 16; i > 16 - nb; i--) {                          \
             cpu_stb_data_ra(env, addr, xt->VsrB(i - 1), GETPC()); \
             addr = addr_add(env, addr, 1);                        \
Signature · 1 line
2.52.0
Reviewed-by Anton Johansson on v5 9/11
#22 lore raw

Re: [PATCH v5 09/11] target/ppc: Introduce ppc_env_is_little_endian() helper

Quoted 9 lines · Philippe Mathieu-Daudé

Centralize endianness check on MSR via a common helper.

---
 target/ppc/internal.h   | 5 +++++
 target/ppc/cpu_init.c   | 9 +++------
 target/ppc/gdbstub.c    | 2 +-
 target/ppc/mem_helper.c | 8 ++++----
 4 files changed, 13 insertions(+), 11 deletions(-)
#23 lore raw

PATCH v5 10/11 target/ppc: Check endianness at runtime in mo_endian_env()

In reply to Philippe Mathieu-Daudé

Rather a runtime endianness check via env MSR over a build-time one.

Now CPU can change endianness at runtime.

---
 target/ppc/internal.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/ppc/internal.h +1 -1
index 714629079b5..7dfca0b41a3 100644
@@ -31,7 +31,7 @@ static inline bool ppc_env_is_little_endian(const CPUPPCState *env)
 
 static inline MemOp mo_endian_env(const CPUPPCState *env)
 {
-    return MO_TE;
+    return ppc_env_is_little_endian(env) ? MO_LE : MO_BE;
 }
 
 /* PM instructions */
Signature · 1 line
2.52.0
Reviewed-by Anton Johansson on v5 10/11
#24 lore raw

Re: [PATCH v5 10/11] target/ppc: Check endianness at runtime in mo_endian_env()

Quoted 9 lines · Philippe Mathieu-Daudé

Rather a runtime endianness check via env MSR over a build-time one.

Now CPU can change endianness at runtime.

---
 target/ppc/internal.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
#25 lore raw

PATCH v5 11/11 target/ppc: Inline cpu_ldl_code() call in ppc_ldl_code()

In reply to Philippe Mathieu-Daudé

In preparation of removing the cpu_ldl_code wrapper, inline it.

Get the runtime endianness with mo_endian_env(), passing it to cpu_ldl_code_mmu(). No need to swap versus qemu-system binary anymore.

---
 target/ppc/tcg-excp_helper.c | 17 ++++-------------
 1 file changed, 4 insertions(+), 13 deletions(-)
target/ppc/tcg-excp_helper.c +4 -13
index 77d21523a5b..955afc40a83 100644
@@ -428,22 +428,13 @@ G_NORETURN void powerpc_checkstop(CPUPPCState *env, const char *reason)
     cpu_loop_exit_noexc(cs);
 }
 
-/* Return true iff byteswap is needed to load instruction */
-static inline bool insn_need_byteswap(CPUArchState *env)
-{
-    /* SYSTEM builds TARGET_BIG_ENDIAN. Need to swap when MSR[LE] is set */
-    return !!(env->msr & ((target_ulong)1 << MSR_LE));
-}
-
 uint32_t ppc_ldl_code(CPUArchState *env, target_ulong addr)
 {
-    uint32_t insn = cpu_ldl_code(env, addr);
+    CPUState *cs = env_cpu(env);
+    MemOp op_end = mo_endian_env(env);
+    MemOpIdx oi = make_memop_idx(MO_UL | op_end, cpu_mmu_index(cs, true));
 
-    if (insn_need_byteswap(env)) {
-        insn = bswap32(insn);
-    }
-
-    return insn;
+    return cpu_ldl_code_mmu(env, addr, oi, 0);
 }
 
 #if defined(TARGET_PPC64)
Signature · 1 line
2.52.0
Reviewed-by Anton Johansson on v5 11/11
#26 lore raw

Re: [PATCH v5 11/11] target/ppc: Inline cpu_ldl_code() call in ppc_ldl_code()

Quoted 10 lines · Philippe Mathieu-Daudé

In preparation of removing the cpu_ldl_code wrapper, inline it.

Get the runtime endianness with mo_endian_env(), passing it to cpu_ldl_code_mmu(). No need to swap versus qemu-system binary anymore.

---
 target/ppc/tcg-excp_helper.c | 17 ++++-------------
 1 file changed, 4 insertions(+), 13 deletions(-)
#27 lore raw
In reply to Philippe Mathieu-Daudé

Ping for review?

Quoted 28 lines · Philippe Mathieu-Daudé

Since v4:
- Fixed incorrect rebase

Since v3 (DATA path):
- Remove MO_UNALN in do_HASH (rth)
- Complete CODE path

Philippe Mathieu-Daudé (11):
target/ppc: Inline cpu_ldl_data_ra() calls in ICBI helpers target/ppc: Inline cpu_ld/st_mmuidx_ra() calls in memory helpers target/ppc: Inline cpu_ld/st_data_ra() calls in do_hash() target/ppc: Expect page translation hash addresses to be aligned target/ppc: Introduce the mo_endian_env() helper target/ppc: Introduce ppc_code_endian() helper target/ppc: Inline need_byteswap() and replace translator_ldl_swap() target/ppc: Check endianness via env in ppc_disas_set_info() target/ppc: Introduce ppc_env_is_little_endian() helper target/ppc: Check endianness at runtime in mo_endian_env() target/ppc: Inline cpu_ldl_code() call in ppc_ldl_code()

  target/ppc/internal.h        | 11 +++++++
  target/ppc/cpu_init.c        |  9 ++----
  target/ppc/gdbstub.c         |  2 +-
  target/ppc/mem_helper.c      | 60 ++++++++++++++++++++++++++----------
  target/ppc/tcg-excp_helper.c | 27 +++++++---------
  target/ppc/translate.c       | 15 +++------
  6 files changed, 75 insertions(+), 49 deletions(-)