Skip to contentSkip to conversation

hw/ppc: Replace tswap32() by stl_endian_p()

PATCH In review Philippe Mathieu-Daudé 2 participants 6 messages Sep 30, 2024

lore
Share & tools
lore
#0 lore raw

cover hw/ppc: Replace tswap32() by stl_endian_p()

Expose ppc_cpu_is_big_endian(), then use it calling the target agnostic stl_endian_p() method, removing the use of the target specific tswap32() method.

Philippe Mathieu-Daudé (3):
target/ppc: Expose ppc_cpu_is_big_endian() prototype in 'cpu.h' hw/ppc/sam460ex: Replace tswap32() by stl_endian_p() hw/ppc/virtex_ml507: Replace tswap32() by stl_endian_p()

 target/ppc/cpu.h      | 2 ++
 hw/ppc/sam460ex.c     | 5 +++--
 hw/ppc/virtex_ml507.c | 2 +-
 target/ppc/cpu_init.c | 2 +-
 4 files changed, 7 insertions(+), 4 deletions(-)
Signature · 1 line
2.45.2
#1 lore raw

PATCH 1/3 target/ppc: Expose ppc_cpu_is_big_endian() prototype in 'cpu.h'

Expose ppc_cpu_is_big_endian() so it can be used by hw/ code.

---
 target/ppc/cpu.h      | 2 ++
 target/ppc/cpu_init.c | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)
target/ppc/cpu.h +2 -0
index 321ed2da75..4a4911502d 100644
@@ -3007,6 +3007,8 @@ static inline bool ppc_interrupts_little_endian(PowerPCCPU *cpu, bool hv)
 
     return ile;
 }
+
+bool ppc_cpu_is_big_endian(CPUState *cs);
 #endif
 
 void dump_mmu(CPUPPCState *env);
target/ppc/cpu_init.c +1 -1
index 23881d09e9..00e0a8bf2c 100644
@@ -7343,7 +7343,7 @@ static void ppc_cpu_reset_hold(Object *obj, ResetType type)
 
 #ifndef CONFIG_USER_ONLY
 
-static bool ppc_cpu_is_big_endian(CPUState *cs)
+bool ppc_cpu_is_big_endian(CPUState *cs)
 {
     cpu_synchronize_state(cs);
 
Signature · 1 line
2.45.2
#2 lore raw

PATCH 2/3 hw/ppc/sam460ex: Replace tswap32() by stl_endian_p()

In reply to Philippe Mathieu-Daudé

Replace the target-specific tswap32() call by stl_endian_p() which does the same but takes the endianness as argument, thus is target-agnostic.
Get the vCPU endianness calling ppc_cpu_is_big_endian().

---
 hw/ppc/sam460ex.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
hw/ppc/sam460ex.c +3 -2
index 8dc75fb9f0..6257ddbec6 100644
@@ -248,10 +248,11 @@ static void mmubooke_create_initial_mapping(CPUPPCState *env,
 static void main_cpu_reset(void *opaque)
 {
     PowerPCCPU *cpu = opaque;
+    CPUState *cs = CPU(cpu);
     CPUPPCState *env = &cpu->env;
     struct boot_info *bi = env->load_info;
 
-    cpu_reset(CPU(cpu));
+    cpu_reset(cs);
 
     /* either we have a kernel to boot or we jump to U-Boot */
     if (bi->entry != UBOOT_ENTRY) {
@@ -261,7 +262,7 @@ static void main_cpu_reset(void *opaque)
 
         /* Create a mapping for the kernel.  */
         mmubooke_create_initial_mapping(env, 0, 0);
-        env->gpr[6] = tswap32(EPAPR_MAGIC);
+        stl_endian_p(ppc_cpu_is_big_endian(cs), &env->gpr[6], EPAPR_MAGIC);
         env->gpr[7] = (16 * MiB) - 8; /* bi->ima_size; */
 
     } else {
Signature · 1 line
2.45.2
#3 lore raw

Re: [PATCH 2/3] hw/ppc/sam460ex: Replace tswap32() by stl_endian_p()

Quoted 28 lines · Philippe Mathieu-Daudé

Replace the target-specific tswap32() call by stl_endian_p() which does the same but takes the endianness as argument, thus is target-agnostic.
Get the vCPU endianness calling ppc_cpu_is_big_endian().

---
hw/ppc/sam460ex.c | 5 +++--

1 file changed, 3 insertions(+), 2 deletions(-)
hw/ppc/sam460ex.c +2 -1
index 8dc75fb9f0..6257ddbec6 100644
@@ -248,10 +248,11 @@ static void mmubooke_create_initial_mapping(CPUPPCState *env,
 static void main_cpu_reset(void *opaque)
 {
    PowerPCCPU *cpu = opaque;
+    CPUState *cs = CPU(cpu);
    CPUPPCState *env = &cpu->env;
    struct boot_info *bi = env->load_info;
 
-    cpu_reset(CPU(cpu));
+    cpu_reset(cs);
 
    /* either we have a kernel to boot or we jump to U-Boot */
    if (bi->entry != UBOOT_ENTRY) {

hw/ppc/sam460ex.c static void main_cpu_reset(void *opaque) · line 265

Quoted 5 lines
hw/ppc/sam460ex.c +1 -1
@@ -261,7 +262,7 @@ static void main_cpu_reset(void *opaque)
 
        /* Create a mapping for the kernel.  */
        mmubooke_create_initial_mapping(env, 0, 0);
-        env->gpr[6] = tswap32(EPAPR_MAGIC);
+        stl_endian_p(ppc_cpu_is_big_endian(cs), &env->gpr[6], EPAPR_MAGIC);

I think this slightly changes behaviour even if getting the same result. The ppc_cpu_is_big_endian() checks the CPU bit for current mode while previously tswap32 only checks for host endianness vs. big endian which is the default mode for PPC which is what the CPU is in during boot where this data is used. So even with checking the bit it'd be the same but the check is not needed. I think you could/should just have hard coded big endian here to preserve the current behaviour.

(There were some discussion a while back if this EPAPR_MAGIC is correct or needed at all but preserving current behaviour is a good enough for now.)

Regards,
BALATON Zoltan

Quoted 4 lines
hw/ppc/sam460ex.c +0 -0
        env->gpr[7] = (16 * MiB) - 8; /* bi->ima_size; */
 
    } else {
#4 lore raw

PATCH 3/3 hw/ppc/virtex_ml507: Replace tswap32() by stl_endian_p()

In reply to Philippe Mathieu-Daudé

Replace the target-specific tswap32() call by stl_endian_p() which does the same but takes the endianness as argument, thus is target-agnostic.
Get the vCPU endianness calling ppc_cpu_is_big_endian().

---
 hw/ppc/virtex_ml507.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/ppc/virtex_ml507.c +1 -1
index c49da1f46f..135dcbde06 100644
@@ -141,7 +141,7 @@ static void main_cpu_reset(void *opaque)
 
     /* Create a mapping for the kernel.  */
     mmubooke_create_initial_mapping(env, 0, 0);
-    env->gpr[6] = tswap32(EPAPR_MAGIC);
+    stl_endian_p(ppc_cpu_is_big_endian(CPU(cpu)), &env->gpr[6], EPAPR_MAGIC);
     env->gpr[7] = bi->ima_size;
 }
 
Signature · 1 line
2.45.2
#5 lore raw

Re: [PATCH 3/3] hw/ppc/virtex_ml507: Replace tswap32() by stl_endian_p()

Quoted 4 lines · Philippe Mathieu-Daudé

Replace the target-specific tswap32() call by stl_endian_p() which does the same but takes the endianness as argument, thus is target-agnostic.
Get the vCPU endianness calling ppc_cpu_is_big_endian().

I don't know about virtex but maybe the same comment applies as for sam460ex.

Regards,
BALATON Zoltan

Quoted 19 lines

---
hw/ppc/virtex_ml507.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
hw/ppc/virtex_ml507.c +1 -1
index c49da1f46f..135dcbde06 100644
@@ -141,7 +141,7 @@ static void main_cpu_reset(void *opaque)
 
    /* Create a mapping for the kernel.  */
    mmubooke_create_initial_mapping(env, 0, 0);
-    env->gpr[6] = tswap32(EPAPR_MAGIC);
+    stl_endian_p(ppc_cpu_is_big_endian(CPU(cpu)), &env->gpr[6], EPAPR_MAGIC);
    env->gpr[7] = bi->ima_size;
 }