Skip to contentSkip to conversation

accel/tcg: Remove some MO_TE uses in cpu_ld{uw, l, q}_code()

PATCH Superseded Philippe Mathieu-Daudé 1 participant 7 messages Nov 20, 2025

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

cover accel/tcg: Remove some MO_TE uses in cpu_ld{uw, l, q}_code()

Hi Richard,

Quick attempt to remove MO_TE uses for fixed-endianness targets.

For the other targets I started to propagate MemOp in the call chain, but since IIUC it is constant, I wonder if it isn't simpler to have a CPUClass::code_endian_mo field, set once with a target_big_endian() call. I can post another RFC to show, WDYT?

Regards,

Phil.

Philippe Mathieu-Daudé (5):
accel/tcg: Add endianness variants of cpu_ld{uw,l,q}_code() target/alpha: Use little-endian variant of cpu_ldl_code() target/loongarch: Use little-endian variant of cpu_ldl_code() target/sparc: Use big-endian variant of cpu_ldl_code() target/s390x: Use big-endian variant of cpu_ld{uw,l}_code()

 include/accel/tcg/cpu-ldst.h   | 43 +++++++++++++++++++++++++++++-----
 target/alpha/mem_helper.c      |  2 +-
 target/loongarch/tcg/tcg_cpu.c |  2 +-
 target/s390x/tcg/mem_helper.c  |  6 ++---
 target/sparc/int32_helper.c    |  2 +-
 5 files changed, 43 insertions(+), 12 deletions(-)
Signature · 1 line
2.51.0
#1 lore raw

PATCH 1/5 accel/tcg: Add endianness variants of cpu_ld{uw, l, q}_code()

Allow target with fixed-endianness to not use the MO_TE definition.

---
 include/accel/tcg/cpu-ldst.h | 43 +++++++++++++++++++++++++++++++-----
 1 file changed, 37 insertions(+), 6 deletions(-)
include/accel/tcg/cpu-ldst.h +37 -6
index 0de7f5eaa6b..e4ec4e7d367 100644
@@ -481,25 +481,56 @@ static inline uint32_t cpu_ldub_code(CPUArchState *env, abi_ptr addr)
     return cpu_ldb_code_mmu(env, addr, oi, 0);
 }
 
-static inline uint32_t cpu_lduw_code(CPUArchState *env, abi_ptr addr)
+static inline uint32_t cpu_lduw_le_code(CPUArchState *env, abi_ptr addr)
 {
     CPUState *cs = env_cpu(env);
-    MemOpIdx oi = make_memop_idx(MO_TEUW, cpu_mmu_index(cs, true));
+    MemOpIdx oi = make_memop_idx(MO_LEUW, cpu_mmu_index(cs, true));
     return cpu_ldw_code_mmu(env, addr, oi, 0);
 }
 
-static inline uint32_t cpu_ldl_code(CPUArchState *env, abi_ptr addr)
+static inline uint32_t cpu_ldl_le_code(CPUArchState *env, abi_ptr addr)
 {
     CPUState *cs = env_cpu(env);
-    MemOpIdx oi = make_memop_idx(MO_TEUL, cpu_mmu_index(cs, true));
+    MemOpIdx oi = make_memop_idx(MO_LEUL, cpu_mmu_index(cs, true));
     return cpu_ldl_code_mmu(env, addr, oi, 0);
 }
 
-static inline uint64_t cpu_ldq_code(CPUArchState *env, abi_ptr addr)
+static inline uint64_t cpu_ldq_le_code(CPUArchState *env, abi_ptr addr)
 {
     CPUState *cs = env_cpu(env);
-    MemOpIdx oi = make_memop_idx(MO_TEUQ, cpu_mmu_index(cs, true));
+    MemOpIdx oi = make_memop_idx(MO_LEUQ, cpu_mmu_index(cs, true));
     return cpu_ldq_code_mmu(env, addr, oi, 0);
 }
 
+static inline uint32_t cpu_lduw_be_code(CPUArchState *env, abi_ptr addr)
+{
+    CPUState *cs = env_cpu(env);
+    MemOpIdx oi = make_memop_idx(MO_BEUW, cpu_mmu_index(cs, true));
+    return cpu_ldw_code_mmu(env, addr, oi, 0);
+}
+
+static inline uint32_t cpu_ldl_be_code(CPUArchState *env, abi_ptr addr)
+{
+    CPUState *cs = env_cpu(env);
+    MemOpIdx oi = make_memop_idx(MO_BEUL, cpu_mmu_index(cs, true));
+    return cpu_ldl_code_mmu(env, addr, oi, 0);
+}
+
+static inline uint64_t cpu_ldq_be_code(CPUArchState *env, abi_ptr addr)
+{
+    CPUState *cs = env_cpu(env);
+    MemOpIdx oi = make_memop_idx(MO_BEUQ, cpu_mmu_index(cs, true));
+    return cpu_ldq_code_mmu(env, addr, oi, 0);
+}
+
+#if TARGET_BIG_ENDIAN
+# define cpu_lduw_code        cpu_lduw_be_code
+# define cpu_ldl_code         cpu_ldl_be_code
+# define cpu_ldq_code         cpu_ldq_be_code
+#else
+# define cpu_lduw_code        cpu_lduw_le_code
+# define cpu_ldl_code         cpu_ldl_le_code
+# define cpu_ldq_code         cpu_ldq_le_code
+#endif
+
 #endif /* ACCEL_TCG_CPU_LDST_H */
Signature · 1 line
2.51.0
#2 lore raw

PATCH 2/5 target/alpha: Use little-endian variant of cpu_ldl_code()

In reply to Philippe Mathieu-Daudé

Alpha is always little-endian.

---
 target/alpha/mem_helper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/alpha/mem_helper.c +1 -1
index 2113fe33ae2..d04d086b59d 100644
@@ -30,7 +30,7 @@ static void do_unaligned_access(CPUAlphaState *env, vaddr addr, uintptr_t retadd
     cpu_restore_state(env_cpu(env), retaddr);
 
     pc = env->pc;
-    insn = cpu_ldl_code(env, pc);
+    insn = cpu_ldl_le_code(env, pc);
 
     env->trap_arg0 = addr;
     env->trap_arg1 = insn >> 26;                /* opcode */
Signature · 1 line
2.51.0
#3 lore raw

PATCH 3/5 target/loongarch: Use little-endian variant of cpu_ldl_code()

In reply to Philippe Mathieu-Daudé

LoongArch is always little-endian.

---
 target/loongarch/tcg/tcg_cpu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/loongarch/tcg/tcg_cpu.c +1 -1
index 9d077c56d9d..3f4b5f9258e 100644
@@ -140,7 +140,7 @@ static void loongarch_cpu_do_interrupt(CPUState *cs)
     }
 
     if (update_badinstr) {
-        env->CSR_BADI = cpu_ldl_code(env, env->pc);
+        env->CSR_BADI = cpu_ldl_le_code(env, env->pc);
     }
 
     /* Save PLV and IE */
Signature · 1 line
2.51.0
#4 lore raw

PATCH 4/5 target/sparc: Use big-endian variant of cpu_ldl_code()

In reply to Philippe Mathieu-Daudé

SPARC is always big-endian.

---
 target/sparc/int32_helper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/sparc/int32_helper.c +1 -1
index b29f693a6bf..a777d17a01c 100644
@@ -151,7 +151,7 @@ void sparc_cpu_do_interrupt(CPUState *cs)
         if (!env->fsr_qne) {
             env->fsr_qne = FSR_QNE;
             env->fq.s.addr = env->pc;
-            env->fq.s.insn = cpu_ldl_code(env, env->pc);
+            env->fq.s.insn = cpu_ldl_be_code(env, env->pc);
         }
         env->pc = env->npc;
         env->npc = env->npc + 4;
Signature · 1 line
2.51.0
#5 lore raw

PATCH 5/5 target/s390x: Use big-endian variant of cpu_ld{uw, l}_code()

In reply to Philippe Mathieu-Daudé

S390x is always big-endian.

---
 target/s390x/tcg/mem_helper.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
target/s390x/tcg/mem_helper.c +3 -3
index 24675fc818d..84d901c2008 100644
@@ -2437,7 +2437,7 @@ void HELPER(ex)(CPUS390XState *env, uint32_t ilen, uint64_t r1, uint64_t addr)
         tcg_s390_program_interrupt(env, PGM_SPECIFICATION, GETPC());
     }
 
-    insn = cpu_lduw_code(env, addr);
+    insn = cpu_lduw_be_code(env, addr);
     opc = insn >> 8;
 
     /* Or in the contents of R1[56:63].  */
@@ -2449,10 +2449,10 @@ void HELPER(ex)(CPUS390XState *env, uint32_t ilen, uint64_t r1, uint64_t addr)
     case 2:
         break;
     case 4:
-        insn |= (uint64_t)cpu_lduw_code(env, addr + 2) << 32;
+        insn |= (uint64_t)cpu_lduw_be_code(env, addr + 2) << 32;
         break;
     case 6:
-        insn |= (uint64_t)(uint32_t)cpu_ldl_code(env, addr + 2) << 16;
+        insn |= (uint64_t)(uint32_t)cpu_ldl_be_code(env, addr + 2) << 16;
         break;
     default:
         g_assert_not_reached();
Signature · 1 line
2.51.0
#6 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 9 lines · Philippe Mathieu-Daudé

Hi Richard,

Quick attempt to remove MO_TE uses for fixed-endianness targets.

For the other targets I started to propagate MemOp in the call chain, but since IIUC it is constant, I wonder if it isn't simpler to have a CPUClass::code_endian_mo field, set once with a target_big_endian() call. I can post another RFC to show, WDYT?

Not needed, figured a simpler way, posted as v2.