qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/8] misc: Prefer evaluating TARGET_BIG_ENDIAN in C
@ 2025-04-17 13:09 Philippe Mathieu-Daudé
  2025-04-17 13:09 ` [PATCH 1/8] linux-user/elfload: Use target_needs_bswap() Philippe Mathieu-Daudé
                   ` (8 more replies)
  0 siblings, 9 replies; 20+ messages in thread
From: Philippe Mathieu-Daudé @ 2025-04-17 13:09 UTC (permalink / raw)
  To: qemu-devel; +Cc: Richard Henderson, Philippe Mathieu-Daudé

Few single-binary cleanups before switching to the
TargetInfo API. Sending as a preliminary cleanup
because can be reviewed out of context.

Philippe Mathieu-Daudé (8):
  linux-user/elfload: Use target_needs_bswap()
  accel/kvm: Use target_needs_bswap()
  target/mips: Check CPU endianness at runtime using env_is_bigendian()
  target/ppc: Evaluate TARGET_BIG_ENDIAN at compile time
  target/xtensa: Evaluate TARGET_BIG_ENDIAN at compile time
  hw/mips: Evaluate TARGET_BIG_ENDIAN at compile time
  hw/microblaze: Evaluate TARGET_BIG_ENDIAN at compile time
  gdbstub/helpers: Evaluate TARGET_BIG_ENDIAN at compile time

 include/gdbstub/helpers.h           | 11 +----
 accel/kvm/kvm-all.c                 | 29 ++++++-------
 hw/microblaze/petalogix_ml605_mmu.c | 12 +++---
 hw/microblaze/xlnx-zynqmp-pmu.c     | 12 +++---
 hw/mips/jazz.c                      | 10 ++---
 hw/mips/malta.c                     | 21 +++-------
 hw/mips/mipssim.c                   | 10 ++---
 linux-user/elfload.c                | 63 ++++++++++++++++++-----------
 target/mips/tcg/msa_helper.c        | 34 ++++++++--------
 target/ppc/cpu_init.c               | 12 +++---
 target/ppc/mem_helper.c             |  6 +--
 target/ppc/translate.c              |  6 +--
 target/xtensa/translate.c           | 10 ++---
 13 files changed, 109 insertions(+), 127 deletions(-)

-- 
2.47.1



^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH 1/8] linux-user/elfload: Use target_needs_bswap()
  2025-04-17 13:09 [PATCH 0/8] misc: Prefer evaluating TARGET_BIG_ENDIAN in C Philippe Mathieu-Daudé
@ 2025-04-17 13:09 ` Philippe Mathieu-Daudé
  2025-04-18 17:44   ` Richard Henderson
  2025-04-17 13:09 ` [PATCH 2/8] accel/kvm: " Philippe Mathieu-Daudé
                   ` (7 subsequent siblings)
  8 siblings, 1 reply; 20+ messages in thread
From: Philippe Mathieu-Daudé @ 2025-04-17 13:09 UTC (permalink / raw)
  To: qemu-devel; +Cc: Richard Henderson, Philippe Mathieu-Daudé

Check whether we need to swap at runtime using
target_needs_bswap().

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 linux-user/elfload.c | 63 +++++++++++++++++++++++++++-----------------
 1 file changed, 39 insertions(+), 24 deletions(-)

diff --git a/linux-user/elfload.c b/linux-user/elfload.c
index 99811af5e7b..fbfdec2f17c 100644
--- a/linux-user/elfload.c
+++ b/linux-user/elfload.c
@@ -12,6 +12,7 @@
 #include "exec/page-protection.h"
 #include "exec/mmap-lock.h"
 #include "exec/translation-block.h"
+#include "exec/tswap.h"
 #include "user/guest-base.h"
 #include "user-internals.h"
 #include "signal-common.h"
@@ -2122,9 +2123,12 @@ static inline void memcpy_fromfs(void * to, const void * from, unsigned long n)
     memcpy(to, from, n);
 }
 
-#if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN
 static void bswap_ehdr(struct elfhdr *ehdr)
 {
+    if (!target_needs_bswap()) {
+        return;
+    }
+
     bswap16s(&ehdr->e_type);            /* Object file type */
     bswap16s(&ehdr->e_machine);         /* Architecture */
     bswap32s(&ehdr->e_version);         /* Object file version */
@@ -2142,8 +2146,11 @@ static void bswap_ehdr(struct elfhdr *ehdr)
 
 static void bswap_phdr(struct elf_phdr *phdr, int phnum)
 {
-    int i;
-    for (i = 0; i < phnum; ++i, ++phdr) {
+    if (!target_needs_bswap()) {
+        return;
+    }
+
+    for (int i = 0; i < phnum; ++i, ++phdr) {
         bswap32s(&phdr->p_type);        /* Segment type */
         bswap32s(&phdr->p_flags);       /* Segment flags */
         bswaptls(&phdr->p_offset);      /* Segment file offset */
@@ -2157,8 +2164,11 @@ static void bswap_phdr(struct elf_phdr *phdr, int phnum)
 
 static void bswap_shdr(struct elf_shdr *shdr, int shnum)
 {
-    int i;
-    for (i = 0; i < shnum; ++i, ++shdr) {
+    if (!target_needs_bswap()) {
+        return;
+    }
+
+    for (int i = 0; i < shnum; ++i, ++shdr) {
         bswap32s(&shdr->sh_name);
         bswap32s(&shdr->sh_type);
         bswaptls(&shdr->sh_flags);
@@ -2174,6 +2184,10 @@ static void bswap_shdr(struct elf_shdr *shdr, int shnum)
 
 static void bswap_sym(struct elf_sym *sym)
 {
+    if (!target_needs_bswap()) {
+        return;
+    }
+
     bswap32s(&sym->st_name);
     bswaptls(&sym->st_value);
     bswaptls(&sym->st_size);
@@ -2183,6 +2197,10 @@ static void bswap_sym(struct elf_sym *sym)
 #ifdef TARGET_MIPS
 static void bswap_mips_abiflags(Mips_elf_abiflags_v0 *abiflags)
 {
+    if (!target_needs_bswap()) {
+        return;
+    }
+
     bswap16s(&abiflags->version);
     bswap32s(&abiflags->ases);
     bswap32s(&abiflags->isa_ext);
@@ -2190,15 +2208,6 @@ static void bswap_mips_abiflags(Mips_elf_abiflags_v0 *abiflags)
     bswap32s(&abiflags->flags2);
 }
 #endif
-#else
-static inline void bswap_ehdr(struct elfhdr *ehdr) { }
-static inline void bswap_phdr(struct elf_phdr *phdr, int phnum) { }
-static inline void bswap_shdr(struct elf_shdr *shdr, int shnum) { }
-static inline void bswap_sym(struct elf_sym *sym) { }
-#ifdef TARGET_MIPS
-static inline void bswap_mips_abiflags(Mips_elf_abiflags_v0 *abiflags) { }
-#endif
-#endif
 
 #ifdef USE_ELF_CORE_DUMP
 static int elf_core_dump(int, const CPUArchState *);
@@ -3144,11 +3153,11 @@ static bool parse_elf_properties(const ImageSource *src,
      * The contents of a valid PT_GNU_PROPERTY is a sequence of uint32_t.
      * Swap most of them now, beyond the header and namesz.
      */
-#if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN
-    for (int i = 4; i < n / 4; i++) {
-        bswap32s(note.data + i);
+    if (target_needs_bswap()) {
+        for (int i = 4; i < n / 4; i++) {
+            bswap32s(note.data + i);
+        }
     }
-#endif
 
     /*
      * Note that nhdr is 3 words, and that the "name" described by namesz
@@ -4000,9 +4009,12 @@ struct target_elf_prpsinfo {
     char    pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
 };
 
-#if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN
 static void bswap_prstatus(struct target_elf_prstatus *prstatus)
 {
+    if (!target_needs_bswap()) {
+        return;
+    }
+
     prstatus->pr_info.si_signo = tswap32(prstatus->pr_info.si_signo);
     prstatus->pr_info.si_code = tswap32(prstatus->pr_info.si_code);
     prstatus->pr_info.si_errno = tswap32(prstatus->pr_info.si_errno);
@@ -4020,6 +4032,10 @@ static void bswap_prstatus(struct target_elf_prstatus *prstatus)
 
 static void bswap_psinfo(struct target_elf_prpsinfo *psinfo)
 {
+    if (!target_needs_bswap()) {
+        return;
+    }
+
     psinfo->pr_flag = tswapal(psinfo->pr_flag);
     psinfo->pr_uid = tswap16(psinfo->pr_uid);
     psinfo->pr_gid = tswap16(psinfo->pr_gid);
@@ -4031,15 +4047,14 @@ static void bswap_psinfo(struct target_elf_prpsinfo *psinfo)
 
 static void bswap_note(struct elf_note *en)
 {
+    if (!target_needs_bswap()) {
+        return;
+    }
+
     bswap32s(&en->n_namesz);
     bswap32s(&en->n_descsz);
     bswap32s(&en->n_type);
 }
-#else
-static inline void bswap_prstatus(struct target_elf_prstatus *p) { }
-static inline void bswap_psinfo(struct target_elf_prpsinfo *p) {}
-static inline void bswap_note(struct elf_note *en) { }
-#endif /* HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN */
 
 /*
  * Calculate file (dump) size of given memory region.
-- 
2.47.1



^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH 2/8] accel/kvm: Use target_needs_bswap()
  2025-04-17 13:09 [PATCH 0/8] misc: Prefer evaluating TARGET_BIG_ENDIAN in C Philippe Mathieu-Daudé
  2025-04-17 13:09 ` [PATCH 1/8] linux-user/elfload: Use target_needs_bswap() Philippe Mathieu-Daudé
@ 2025-04-17 13:09 ` Philippe Mathieu-Daudé
  2025-04-18 17:47   ` Richard Henderson
  2025-04-17 13:09 ` [PATCH 3/8] target/mips: Check CPU endianness at runtime using env_is_bigendian() Philippe Mathieu-Daudé
                   ` (6 subsequent siblings)
  8 siblings, 1 reply; 20+ messages in thread
From: Philippe Mathieu-Daudé @ 2025-04-17 13:09 UTC (permalink / raw)
  To: qemu-devel; +Cc: Richard Henderson, Philippe Mathieu-Daudé

Check whether we need to swap at runtime using
target_needs_bswap().

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 accel/kvm/kvm-all.c | 29 +++++++++++++++--------------
 1 file changed, 15 insertions(+), 14 deletions(-)

diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c
index 7c5d1a98bc4..28a32afb209 100644
--- a/accel/kvm/kvm-all.c
+++ b/accel/kvm/kvm-all.c
@@ -33,6 +33,7 @@
 #include "system/cpus.h"
 #include "system/accel-blocker.h"
 #include "qemu/bswap.h"
+#include "exec/tswap.h"
 #include "system/memory.h"
 #include "system/ram_addr.h"
 #include "qemu/event_notifier.h"
@@ -1319,21 +1320,21 @@ bool kvm_hwpoisoned_mem(void)
 
 static uint32_t adjust_ioeventfd_endianness(uint32_t val, uint32_t size)
 {
-#if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN
-    /* The kernel expects ioeventfd values in HOST_BIG_ENDIAN
-     * endianness, but the memory core hands them in target endianness.
-     * For example, PPC is always treated as big-endian even if running
-     * on KVM and on PPC64LE.  Correct here.
-     */
-    switch (size) {
-    case 2:
-        val = bswap16(val);
-        break;
-    case 4:
-        val = bswap32(val);
-        break;
+    if (target_needs_bswap()) {
+        /* The kernel expects ioeventfd values in HOST_BIG_ENDIAN
+         * endianness, but the memory core hands them in target endianness.
+         * For example, PPC is always treated as big-endian even if running
+         * on KVM and on PPC64LE.  Correct here, swapping back.
+         */
+        switch (size) {
+        case 2:
+            val = bswap16(val);
+            break;
+        case 4:
+            val = bswap32(val);
+            break;
+        }
     }
-#endif
     return val;
 }
 
-- 
2.47.1



^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH 3/8] target/mips: Check CPU endianness at runtime using env_is_bigendian()
  2025-04-17 13:09 [PATCH 0/8] misc: Prefer evaluating TARGET_BIG_ENDIAN in C Philippe Mathieu-Daudé
  2025-04-17 13:09 ` [PATCH 1/8] linux-user/elfload: Use target_needs_bswap() Philippe Mathieu-Daudé
  2025-04-17 13:09 ` [PATCH 2/8] accel/kvm: " Philippe Mathieu-Daudé
@ 2025-04-17 13:09 ` Philippe Mathieu-Daudé
  2025-04-18 17:53   ` Richard Henderson
  2025-04-17 13:10 ` [PATCH 4/8] target/ppc: Evaluate TARGET_BIG_ENDIAN at compile time Philippe Mathieu-Daudé
                   ` (5 subsequent siblings)
  8 siblings, 1 reply; 20+ messages in thread
From: Philippe Mathieu-Daudé @ 2025-04-17 13:09 UTC (permalink / raw)
  To: qemu-devel; +Cc: Richard Henderson, Philippe Mathieu-Daudé

Since CPU endianness can be toggled at runtime before resetting,
checking the endianness at build time preprocessing the
TARGET_BIG_ENDIAN definition isn't correct. We have to call
mips_env_is_bigendian() to get the CPU endianness at runtime.

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 target/mips/tcg/msa_helper.c | 34 ++++++++++++++++------------------
 1 file changed, 16 insertions(+), 18 deletions(-)

diff --git a/target/mips/tcg/msa_helper.c b/target/mips/tcg/msa_helper.c
index 14de4a71ff6..e349344647c 100644
--- a/target/mips/tcg/msa_helper.c
+++ b/target/mips/tcg/msa_helper.c
@@ -8212,7 +8212,6 @@ void helper_msa_ffint_u_df(CPUMIPSState *env, uint32_t df, uint32_t wd,
 /* Element-by-element access macros */
 #define DF_ELEMENTS(df) (MSA_WRLEN / DF_BITS(df))
 
-#if TARGET_BIG_ENDIAN
 static inline uint64_t bswap16x4(uint64_t x)
 {
     uint64_t m = 0x00ff00ff00ff00ffull;
@@ -8223,7 +8222,6 @@ static inline uint64_t bswap32x2(uint64_t x)
 {
     return ror64(bswap64(x), 32);
 }
-#endif
 
 void helper_msa_ld_b(CPUMIPSState *env, uint32_t wd,
                      target_ulong addr)
@@ -8252,10 +8250,10 @@ void helper_msa_ld_h(CPUMIPSState *env, uint32_t wd,
      */
     d0 = cpu_ldq_le_data_ra(env, addr + 0, ra);
     d1 = cpu_ldq_le_data_ra(env, addr + 8, ra);
-#if TARGET_BIG_ENDIAN
-    d0 = bswap16x4(d0);
-    d1 = bswap16x4(d1);
-#endif
+    if (mips_env_is_bigendian(env)) {
+        d0 = bswap16x4(d0);
+        d1 = bswap16x4(d1);
+    }
     pwd->d[0] = d0;
     pwd->d[1] = d1;
 }
@@ -8273,10 +8271,10 @@ void helper_msa_ld_w(CPUMIPSState *env, uint32_t wd,
      */
     d0 = cpu_ldq_le_data_ra(env, addr + 0, ra);
     d1 = cpu_ldq_le_data_ra(env, addr + 8, ra);
-#if TARGET_BIG_ENDIAN
-    d0 = bswap32x2(d0);
-    d1 = bswap32x2(d1);
-#endif
+    if (mips_env_is_bigendian(env)) {
+        d0 = bswap32x2(d0);
+        d1 = bswap32x2(d1);
+    }
     pwd->d[0] = d0;
     pwd->d[1] = d1;
 }
@@ -8339,10 +8337,10 @@ void helper_msa_st_h(CPUMIPSState *env, uint32_t wd,
     /* Store 8 bytes at a time.  See helper_msa_ld_h. */
     d0 = pwd->d[0];
     d1 = pwd->d[1];
-#if TARGET_BIG_ENDIAN
-    d0 = bswap16x4(d0);
-    d1 = bswap16x4(d1);
-#endif
+    if (mips_env_is_bigendian(env)) {
+        d0 = bswap16x4(d0);
+        d1 = bswap16x4(d1);
+    }
     cpu_stq_le_data_ra(env, addr + 0, d0, ra);
     cpu_stq_le_data_ra(env, addr + 8, d1, ra);
 }
@@ -8360,10 +8358,10 @@ void helper_msa_st_w(CPUMIPSState *env, uint32_t wd,
     /* Store 8 bytes at a time.  See helper_msa_ld_w. */
     d0 = pwd->d[0];
     d1 = pwd->d[1];
-#if TARGET_BIG_ENDIAN
-    d0 = bswap32x2(d0);
-    d1 = bswap32x2(d1);
-#endif
+    if (mips_env_is_bigendian(env)) {
+        d0 = bswap32x2(d0);
+        d1 = bswap32x2(d1);
+    }
     cpu_stq_le_data_ra(env, addr + 0, d0, ra);
     cpu_stq_le_data_ra(env, addr + 8, d1, ra);
 }
-- 
2.47.1



^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH 4/8] target/ppc: Evaluate TARGET_BIG_ENDIAN at compile time
  2025-04-17 13:09 [PATCH 0/8] misc: Prefer evaluating TARGET_BIG_ENDIAN in C Philippe Mathieu-Daudé
                   ` (2 preceding siblings ...)
  2025-04-17 13:09 ` [PATCH 3/8] target/mips: Check CPU endianness at runtime using env_is_bigendian() Philippe Mathieu-Daudé
@ 2025-04-17 13:10 ` Philippe Mathieu-Daudé
  2025-04-17 19:26   ` Pierrick Bouvier
  2025-04-18 17:56   ` Richard Henderson
  2025-04-17 13:10 ` [PATCH 5/8] target/xtensa: " Philippe Mathieu-Daudé
                   ` (4 subsequent siblings)
  8 siblings, 2 replies; 20+ messages in thread
From: Philippe Mathieu-Daudé @ 2025-04-17 13:10 UTC (permalink / raw)
  To: qemu-devel; +Cc: Richard Henderson, Philippe Mathieu-Daudé

Rather than evaluating TARGET_BIG_ENDIAN at preprocessing
time via #ifdef'ry, do it in C at compile time

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 target/ppc/cpu_init.c   | 12 ++++++------
 target/ppc/mem_helper.c |  6 +-----
 target/ppc/translate.c  |  6 +-----
 3 files changed, 8 insertions(+), 16 deletions(-)

diff --git a/target/ppc/cpu_init.c b/target/ppc/cpu_init.c
index 077991ed535..bbab411a07c 100644
--- a/target/ppc/cpu_init.c
+++ b/target/ppc/cpu_init.c
@@ -7262,14 +7262,14 @@ static void ppc_cpu_reset_hold(Object *obj, ResetType type)
 #if defined(TARGET_PPC64)
     msr |= (target_ulong)1 << MSR_TM; /* Transactional memory */
 #endif
-#if !TARGET_BIG_ENDIAN
-    msr |= (target_ulong)1 << MSR_LE; /* Little-endian user mode */
-    if (!((env->msr_mask >> MSR_LE) & 1)) {
-        fprintf(stderr, "Selected CPU does not support little-endian.\n");
-        exit(1);
+    if (!TARGET_BIG_ENDIAN) {
+        msr |= (target_ulong)1 << MSR_LE; /* Little-endian user mode */
+        if (!((env->msr_mask >> MSR_LE) & 1)) {
+            fprintf(stderr, "Selected CPU does not support little-endian.\n");
+            exit(1);
+        }
     }
 #endif
-#endif
 
 #if defined(TARGET_PPC64)
     if (mmu_is_64bit(env->mmu_model)) {
diff --git a/target/ppc/mem_helper.c b/target/ppc/mem_helper.c
index d7e8d678f4b..cc3ed29a35b 100644
--- a/target/ppc/mem_helper.c
+++ b/target/ppc/mem_helper.c
@@ -32,11 +32,7 @@
 
 static inline bool needs_byteswap(const CPUPPCState *env)
 {
-#if TARGET_BIG_ENDIAN
-  return FIELD_EX64(env->msr, MSR, LE);
-#else
-  return !FIELD_EX64(env->msr, MSR, LE);
-#endif
+  return TARGET_BIG_ENDIAN ^ FIELD_EX64(env->msr, MSR, LE);
 }
 
 /*****************************************************************************/
diff --git a/target/ppc/translate.c b/target/ppc/translate.c
index 399107d319a..828b850b40e 100644
--- a/target/ppc/translate.c
+++ b/target/ppc/translate.c
@@ -213,11 +213,7 @@ struct DisasContext {
 /* 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 TARGET_BIG_ENDIAN ^ ctx->le_mode;
 }
 
 /* True when active word size < size of target_long.  */
-- 
2.47.1



^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH 5/8] target/xtensa: Evaluate TARGET_BIG_ENDIAN at compile time
  2025-04-17 13:09 [PATCH 0/8] misc: Prefer evaluating TARGET_BIG_ENDIAN in C Philippe Mathieu-Daudé
                   ` (3 preceding siblings ...)
  2025-04-17 13:10 ` [PATCH 4/8] target/ppc: Evaluate TARGET_BIG_ENDIAN at compile time Philippe Mathieu-Daudé
@ 2025-04-17 13:10 ` Philippe Mathieu-Daudé
  2025-04-18 17:56   ` Richard Henderson
  2025-04-17 13:10 ` [PATCH 6/8] hw/mips: " Philippe Mathieu-Daudé
                   ` (3 subsequent siblings)
  8 siblings, 1 reply; 20+ messages in thread
From: Philippe Mathieu-Daudé @ 2025-04-17 13:10 UTC (permalink / raw)
  To: qemu-devel; +Cc: Richard Henderson, Philippe Mathieu-Daudé

Rather than evaluating TARGET_BIG_ENDIAN at preprocessing
time via #ifdef'ry, do it in C at compile time

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 target/xtensa/translate.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/target/xtensa/translate.c b/target/xtensa/translate.c
index 5ebd4a512c9..2af83c07c2e 100644
--- a/target/xtensa/translate.c
+++ b/target/xtensa/translate.c
@@ -1395,11 +1395,11 @@ static void translate_bbi(DisasContext *dc, const OpcodeArg arg[],
                           const uint32_t par[])
 {
     TCGv_i32 tmp = tcg_temp_new_i32();
-#if TARGET_BIG_ENDIAN
-    tcg_gen_andi_i32(tmp, arg[0].in, 0x80000000u >> arg[1].imm);
-#else
-    tcg_gen_andi_i32(tmp, arg[0].in, 0x00000001u << arg[1].imm);
-#endif
+    if (TARGET_BIG_ENDIAN) {
+        tcg_gen_andi_i32(tmp, arg[0].in, 0x80000000u >> arg[1].imm);
+    } else {
+        tcg_gen_andi_i32(tmp, arg[0].in, 0x00000001u << arg[1].imm);
+    }
     gen_brcondi(dc, par[0], tmp, 0, arg[2].imm);
 }
 
-- 
2.47.1



^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH 6/8] hw/mips: Evaluate TARGET_BIG_ENDIAN at compile time
  2025-04-17 13:09 [PATCH 0/8] misc: Prefer evaluating TARGET_BIG_ENDIAN in C Philippe Mathieu-Daudé
                   ` (4 preceding siblings ...)
  2025-04-17 13:10 ` [PATCH 5/8] target/xtensa: " Philippe Mathieu-Daudé
@ 2025-04-17 13:10 ` Philippe Mathieu-Daudé
  2025-04-18 17:57   ` Richard Henderson
  2025-04-17 13:10 ` [PATCH 7/8] hw/microblaze: " Philippe Mathieu-Daudé
                   ` (2 subsequent siblings)
  8 siblings, 1 reply; 20+ messages in thread
From: Philippe Mathieu-Daudé @ 2025-04-17 13:10 UTC (permalink / raw)
  To: qemu-devel; +Cc: Richard Henderson, Philippe Mathieu-Daudé

Rather than evaluating TARGET_BIG_ENDIAN at preprocessing
time via #ifdef'ry, do it in C at compile time

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 hw/mips/jazz.c    | 10 +++-------
 hw/mips/malta.c   | 21 ++++++---------------
 hw/mips/mipssim.c | 10 +++-------
 3 files changed, 12 insertions(+), 29 deletions(-)

diff --git a/hw/mips/jazz.c b/hw/mips/jazz.c
index 1700c3765de..34879334037 100644
--- a/hw/mips/jazz.c
+++ b/hw/mips/jazz.c
@@ -59,12 +59,6 @@ enum jazz_model_e {
     JAZZ_PICA61,
 };
 
-#if TARGET_BIG_ENDIAN
-#define BIOS_FILENAME "mips_bios.bin"
-#else
-#define BIOS_FILENAME "mipsel_bios.bin"
-#endif
-
 static void main_cpu_reset(void *opaque)
 {
     MIPSCPU *cpu = opaque;
@@ -168,6 +162,8 @@ static void mips_jazz_init_net(IOMMUMemoryRegion *rc4030_dma_mr,
 static void mips_jazz_init(MachineState *machine,
                            enum jazz_model_e jazz_model)
 {
+    const char *bios_name = TARGET_BIG_ENDIAN ? "mips_bios.bin"
+                                              : "mipsel_bios.bin";
     MemoryRegion *address_space = get_system_memory();
     char *filename;
     int bios_size, n;
@@ -245,7 +241,7 @@ static void mips_jazz_init(MachineState *machine,
     memory_region_add_subregion(address_space, 0xfff00000LL, bios2);
 
     /* load the BIOS image. */
-    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, machine->firmware ?: BIOS_FILENAME);
+    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, machine->firmware ?: bios_name);
     if (filename) {
         bios_size = load_image_targphys(filename, 0xfff00000LL,
                                         MAGNUM_BIOS_SIZE);
diff --git a/hw/mips/malta.c b/hw/mips/malta.c
index 8e9cea70b13..cbdbb210568 100644
--- a/hw/mips/malta.c
+++ b/hw/mips/malta.c
@@ -94,12 +94,6 @@ typedef struct {
     bool display_inited;
 } MaltaFPGAState;
 
-#if TARGET_BIG_ENDIAN
-#define BIOS_FILENAME "mips_bios.bin"
-#else
-#define BIOS_FILENAME "mipsel_bios.bin"
-#endif
-
 #define TYPE_MIPS_MALTA "mips-malta"
 OBJECT_DECLARE_SIMPLE_TYPE(MaltaState, MIPS_MALTA)
 
@@ -383,11 +377,7 @@ static uint64_t malta_fpga_read(void *opaque, hwaddr addr,
 
     /* STATUS Register */
     case 0x00208:
-#if TARGET_BIG_ENDIAN
-        val = 0x00000012;
-#else
-        val = 0x00000010;
-#endif
+        val = TARGET_BIG_ENDIAN ? 0x00000012 : 0x00000010;
         break;
 
     /* JMPRS Register */
@@ -1177,9 +1167,12 @@ void mips_malta_init(MachineState *machine)
         target_long bios_size = FLASH_SIZE;
         /* Load firmware from flash. */
         if (!dinfo) {
+            const char *bios_name = TARGET_BIG_ENDIAN ? "mips_bios.bin"
+                                                        : "mipsel_bios.bin";
+
             /* Load a BIOS image. */
             filename = qemu_find_file(QEMU_FILE_TYPE_BIOS,
-                                      machine->firmware ?: BIOS_FILENAME);
+                                      machine->firmware ?: bios_name);
             if (filename) {
                 bios_size = load_image_targphys(filename, FLASH_ADDRESS,
                                                 BIOS_SIZE);
@@ -1197,8 +1190,7 @@ void mips_malta_init(MachineState *machine)
          * In little endian mode the 32bit words in the bios are swapped,
          * a neat trick which allows bi-endian firmware.
          */
-#if !TARGET_BIG_ENDIAN
-        {
+        if (!TARGET_BIG_ENDIAN) {
             uint32_t *end, *addr;
             const size_t swapsize = MIN(bios_size, 0x3e0000);
             addr = rom_ptr(FLASH_ADDRESS, swapsize);
@@ -1211,7 +1203,6 @@ void mips_malta_init(MachineState *machine)
                 addr++;
             }
         }
-#endif
     }
 
     /*
diff --git a/hw/mips/mipssim.c b/hw/mips/mipssim.c
index b6dabf2893a..46ab7f86c7f 100644
--- a/hw/mips/mipssim.c
+++ b/hw/mips/mipssim.c
@@ -46,12 +46,6 @@
 
 #define BIOS_SIZE (4 * MiB)
 
-#if TARGET_BIG_ENDIAN
-#define BIOS_FILENAME "mips_bios.bin"
-#else
-#define BIOS_FILENAME "mipsel_bios.bin"
-#endif
-
 static struct _loaderparams {
     int ram_size;
     const char *kernel_filename;
@@ -143,6 +137,8 @@ mips_mipssim_init(MachineState *machine)
     const char *kernel_filename = machine->kernel_filename;
     const char *kernel_cmdline = machine->kernel_cmdline;
     const char *initrd_filename = machine->initrd_filename;
+    const char *bios_name = TARGET_BIG_ENDIAN ? "mips_bios.bin"
+                                              : "mipsel_bios.bin";
     char *filename;
     MemoryRegion *address_space_mem = get_system_memory();
     MemoryRegion *isa = g_new(MemoryRegion, 1);
@@ -179,7 +175,7 @@ mips_mipssim_init(MachineState *machine)
     /* Map the BIOS / boot exception handler. */
     memory_region_add_subregion(address_space_mem, 0x1fc00000LL, bios);
     /* Load a BIOS / boot exception handler image. */
-    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, machine->firmware ?: BIOS_FILENAME);
+    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, machine->firmware ?: bios_name);
     if (filename) {
         bios_size = load_image_targphys(filename, 0x1fc00000LL, BIOS_SIZE);
         g_free(filename);
-- 
2.47.1



^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH 7/8] hw/microblaze: Evaluate TARGET_BIG_ENDIAN at compile time
  2025-04-17 13:09 [PATCH 0/8] misc: Prefer evaluating TARGET_BIG_ENDIAN in C Philippe Mathieu-Daudé
                   ` (5 preceding siblings ...)
  2025-04-17 13:10 ` [PATCH 6/8] hw/mips: " Philippe Mathieu-Daudé
@ 2025-04-17 13:10 ` Philippe Mathieu-Daudé
  2025-04-18 17:58   ` Richard Henderson
  2025-04-17 13:10 ` [PATCH 8/8] gdbstub/helpers: " Philippe Mathieu-Daudé
  2025-04-25 15:17 ` [PATCH 0/8] misc: Prefer evaluating TARGET_BIG_ENDIAN in C Philippe Mathieu-Daudé
  8 siblings, 1 reply; 20+ messages in thread
From: Philippe Mathieu-Daudé @ 2025-04-17 13:10 UTC (permalink / raw)
  To: qemu-devel; +Cc: Richard Henderson, Philippe Mathieu-Daudé

Rather than evaluating TARGET_BIG_ENDIAN at preprocessing
time via #ifdef'ry, do it in C at compile time

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 hw/microblaze/petalogix_ml605_mmu.c | 12 ++++++------
 hw/microblaze/xlnx-zynqmp-pmu.c     | 12 ++++++------
 2 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/hw/microblaze/petalogix_ml605_mmu.c b/hw/microblaze/petalogix_ml605_mmu.c
index c887c7a99ed..bea6b689fd1 100644
--- a/hw/microblaze/petalogix_ml605_mmu.c
+++ b/hw/microblaze/petalogix_ml605_mmu.c
@@ -218,12 +218,12 @@ petalogix_ml605_init(MachineState *machine)
 
 static void petalogix_ml605_machine_init(MachineClass *mc)
 {
-#if TARGET_BIG_ENDIAN
-    mc->desc = "PetaLogix linux refdesign for xilinx ml605 (big endian)";
-    mc->deprecation_reason = "big endian support is not tested";
-#else
-    mc->desc = "PetaLogix linux refdesign for xilinx ml605 (little endian)";
-#endif
+    if (TARGET_BIG_ENDIAN) {
+        mc->desc = "PetaLogix linux refdesign for xilinx ml605 (big endian)";
+        mc->deprecation_reason = "big endian support is not tested";
+    } else {
+        mc->desc = "PetaLogix linux refdesign for xilinx ml605 (little endian)";
+    }
     mc->init = petalogix_ml605_init;
 }
 
diff --git a/hw/microblaze/xlnx-zynqmp-pmu.c b/hw/microblaze/xlnx-zynqmp-pmu.c
index 0922c652957..2efacc53feb 100644
--- a/hw/microblaze/xlnx-zynqmp-pmu.c
+++ b/hw/microblaze/xlnx-zynqmp-pmu.c
@@ -181,12 +181,12 @@ static void xlnx_zynqmp_pmu_init(MachineState *machine)
 
 static void xlnx_zynqmp_pmu_machine_init(MachineClass *mc)
 {
-#if TARGET_BIG_ENDIAN
-    mc->desc = "Xilinx ZynqMP PMU machine (big endian)";
-    mc->deprecation_reason = "big endian support is not tested";
-#else
-    mc->desc = "Xilinx ZynqMP PMU machine (little endian)";
-#endif
+    if (TARGET_BIG_ENDIAN) {
+        mc->desc = "Xilinx ZynqMP PMU machine (big endian)";
+        mc->deprecation_reason = "big endian support is not tested";
+    } else {
+        mc->desc = "Xilinx ZynqMP PMU machine (little endian)";
+    }
     mc->init = xlnx_zynqmp_pmu_init;
 }
 
-- 
2.47.1



^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH 8/8] gdbstub/helpers: Evaluate TARGET_BIG_ENDIAN at compile time
  2025-04-17 13:09 [PATCH 0/8] misc: Prefer evaluating TARGET_BIG_ENDIAN in C Philippe Mathieu-Daudé
                   ` (6 preceding siblings ...)
  2025-04-17 13:10 ` [PATCH 7/8] hw/microblaze: " Philippe Mathieu-Daudé
@ 2025-04-17 13:10 ` Philippe Mathieu-Daudé
  2025-04-17 16:18   ` Alex Bennée
  2025-04-18 18:01   ` Richard Henderson
  2025-04-25 15:17 ` [PATCH 0/8] misc: Prefer evaluating TARGET_BIG_ENDIAN in C Philippe Mathieu-Daudé
  8 siblings, 2 replies; 20+ messages in thread
From: Philippe Mathieu-Daudé @ 2025-04-17 13:10 UTC (permalink / raw)
  To: qemu-devel; +Cc: Richard Henderson, Philippe Mathieu-Daudé

Rather than evaluating TARGET_BIG_ENDIAN at preprocessing
time via #ifdef'ry, do it in C at compile time

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 include/gdbstub/helpers.h | 11 ++---------
 1 file changed, 2 insertions(+), 9 deletions(-)

diff --git a/include/gdbstub/helpers.h b/include/gdbstub/helpers.h
index 6f7cc48adcb..c33d5dfca3e 100644
--- a/include/gdbstub/helpers.h
+++ b/include/gdbstub/helpers.h
@@ -56,17 +56,10 @@ static inline int gdb_get_reg128(GByteArray *buf, uint64_t val_hi,
                                  uint64_t val_lo)
 {
     uint64_t to_quad;
-#if TARGET_BIG_ENDIAN
-    to_quad = tswap64(val_hi);
+    to_quad = tswap64(TARGET_BIG_ENDIAN ? val_hi : val_lo);
     g_byte_array_append(buf, (uint8_t *) &to_quad, 8);
-    to_quad = tswap64(val_lo);
+    to_quad = tswap64(TARGET_BIG_ENDIAN ? val_lo : val_hi);
     g_byte_array_append(buf, (uint8_t *) &to_quad, 8);
-#else
-    to_quad = tswap64(val_lo);
-    g_byte_array_append(buf, (uint8_t *) &to_quad, 8);
-    to_quad = tswap64(val_hi);
-    g_byte_array_append(buf, (uint8_t *) &to_quad, 8);
-#endif
     return 16;
 }
 
-- 
2.47.1



^ permalink raw reply related	[flat|nested] 20+ messages in thread

* Re: [PATCH 8/8] gdbstub/helpers: Evaluate TARGET_BIG_ENDIAN at compile time
  2025-04-17 13:10 ` [PATCH 8/8] gdbstub/helpers: " Philippe Mathieu-Daudé
@ 2025-04-17 16:18   ` Alex Bennée
  2025-04-18 18:01   ` Richard Henderson
  1 sibling, 0 replies; 20+ messages in thread
From: Alex Bennée @ 2025-04-17 16:18 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé; +Cc: qemu-devel, Richard Henderson

Philippe Mathieu-Daudé <philmd@linaro.org> writes:

> Rather than evaluating TARGET_BIG_ENDIAN at preprocessing
> time via #ifdef'ry, do it in C at compile time
>
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>

Acked-by: Alex Bennée <alex.bennee@linaro.org>

-- 
Alex Bennée
Virtualisation Tech Lead @ Linaro


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 4/8] target/ppc: Evaluate TARGET_BIG_ENDIAN at compile time
  2025-04-17 13:10 ` [PATCH 4/8] target/ppc: Evaluate TARGET_BIG_ENDIAN at compile time Philippe Mathieu-Daudé
@ 2025-04-17 19:26   ` Pierrick Bouvier
  2025-04-18 17:56   ` Richard Henderson
  1 sibling, 0 replies; 20+ messages in thread
From: Pierrick Bouvier @ 2025-04-17 19:26 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, qemu-devel; +Cc: Richard Henderson

On 4/17/25 06:10, Philippe Mathieu-Daudé wrote:
> Rather than evaluating TARGET_BIG_ENDIAN at preprocessing
> time via #ifdef'ry, do it in C at compile time
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
>   target/ppc/cpu_init.c   | 12 ++++++------
>   target/ppc/mem_helper.c |  6 +-----
>   target/ppc/translate.c  |  6 +-----
>   3 files changed, 8 insertions(+), 16 deletions(-)
> 
> diff --git a/target/ppc/cpu_init.c b/target/ppc/cpu_init.c
> index 077991ed535..bbab411a07c 100644
> --- a/target/ppc/cpu_init.c
> +++ b/target/ppc/cpu_init.c
> @@ -7262,14 +7262,14 @@ static void ppc_cpu_reset_hold(Object *obj, ResetType type)
>   #if defined(TARGET_PPC64)
>       msr |= (target_ulong)1 << MSR_TM; /* Transactional memory */
>   #endif
> -#if !TARGET_BIG_ENDIAN
> -    msr |= (target_ulong)1 << MSR_LE; /* Little-endian user mode */
> -    if (!((env->msr_mask >> MSR_LE) & 1)) {
> -        fprintf(stderr, "Selected CPU does not support little-endian.\n");
> -        exit(1);
> +    if (!TARGET_BIG_ENDIAN) {
> +        msr |= (target_ulong)1 << MSR_LE; /* Little-endian user mode */
> +        if (!((env->msr_mask >> MSR_LE) & 1)) {
> +            fprintf(stderr, "Selected CPU does not support little-endian.\n");
> +            exit(1);
> +        }
>       }
>   #endif
> -#endif
>   
>   #if defined(TARGET_PPC64)
>       if (mmu_is_64bit(env->mmu_model)) {
> diff --git a/target/ppc/mem_helper.c b/target/ppc/mem_helper.c
> index d7e8d678f4b..cc3ed29a35b 100644
> --- a/target/ppc/mem_helper.c
> +++ b/target/ppc/mem_helper.c
> @@ -32,11 +32,7 @@
>   
>   static inline bool needs_byteswap(const CPUPPCState *env)
>   {
> -#if TARGET_BIG_ENDIAN
> -  return FIELD_EX64(env->msr, MSR, LE);
> -#else
> -  return !FIELD_EX64(env->msr, MSR, LE);
> -#endif
> +  return TARGET_BIG_ENDIAN ^ FIELD_EX64(env->msr, MSR, LE);
>   }
>   
>   /*****************************************************************************/
> diff --git a/target/ppc/translate.c b/target/ppc/translate.c
> index 399107d319a..828b850b40e 100644
> --- a/target/ppc/translate.c
> +++ b/target/ppc/translate.c
> @@ -213,11 +213,7 @@ struct DisasContext {
>   /* 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 TARGET_BIG_ENDIAN ^ ctx->le_mode;

Maybe I'm missing something, but with {TARGET_BIG_ENDIAN: 1, 
ctx->le_mode: 1}, it would return 0, while it should be 1 instead.

It would be more readable to use a if or a ternary expression.
Same for previous function needs_byteswap.

>   }
>   
>   /* True when active word size < size of target_long.  */


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 1/8] linux-user/elfload: Use target_needs_bswap()
  2025-04-17 13:09 ` [PATCH 1/8] linux-user/elfload: Use target_needs_bswap() Philippe Mathieu-Daudé
@ 2025-04-18 17:44   ` Richard Henderson
  0 siblings, 0 replies; 20+ messages in thread
From: Richard Henderson @ 2025-04-18 17:44 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, qemu-devel

On 4/17/25 06:09, Philippe Mathieu-Daudé wrote:
> Check whether we need to swap at runtime using
> target_needs_bswap().
> 
> Signed-off-by: Philippe Mathieu-Daudé<philmd@linaro.org>
> ---
>   linux-user/elfload.c | 63 +++++++++++++++++++++++++++-----------------
>   1 file changed, 39 insertions(+), 24 deletions(-)

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

r~


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 2/8] accel/kvm: Use target_needs_bswap()
  2025-04-17 13:09 ` [PATCH 2/8] accel/kvm: " Philippe Mathieu-Daudé
@ 2025-04-18 17:47   ` Richard Henderson
  0 siblings, 0 replies; 20+ messages in thread
From: Richard Henderson @ 2025-04-18 17:47 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, qemu-devel

On 4/17/25 06:09, Philippe Mathieu-Daudé wrote:
> Check whether we need to swap at runtime using
> target_needs_bswap().
> 
> Signed-off-by: Philippe Mathieu-Daudé<philmd@linaro.org>
> ---
>   accel/kvm/kvm-all.c | 29 +++++++++++++++--------------
>   1 file changed, 15 insertions(+), 14 deletions(-)

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

r~


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 3/8] target/mips: Check CPU endianness at runtime using env_is_bigendian()
  2025-04-17 13:09 ` [PATCH 3/8] target/mips: Check CPU endianness at runtime using env_is_bigendian() Philippe Mathieu-Daudé
@ 2025-04-18 17:53   ` Richard Henderson
  0 siblings, 0 replies; 20+ messages in thread
From: Richard Henderson @ 2025-04-18 17:53 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, qemu-devel

On 4/17/25 06:09, Philippe Mathieu-Daudé wrote:
> Since CPU endianness can be toggled at runtime before resetting,
> checking the endianness at build time preprocessing the
> TARGET_BIG_ENDIAN definition isn't correct. We have to call
> mips_env_is_bigendian() to get the CPU endianness at runtime.
> 
> Signed-off-by: Philippe Mathieu-Daudé<philmd@linaro.org>
> ---
>   target/mips/tcg/msa_helper.c | 34 ++++++++++++++++------------------
>   1 file changed, 16 insertions(+), 18 deletions(-)

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

r~


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 4/8] target/ppc: Evaluate TARGET_BIG_ENDIAN at compile time
  2025-04-17 13:10 ` [PATCH 4/8] target/ppc: Evaluate TARGET_BIG_ENDIAN at compile time Philippe Mathieu-Daudé
  2025-04-17 19:26   ` Pierrick Bouvier
@ 2025-04-18 17:56   ` Richard Henderson
  1 sibling, 0 replies; 20+ messages in thread
From: Richard Henderson @ 2025-04-18 17:56 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, qemu-devel

On 4/17/25 06:10, Philippe Mathieu-Daudé wrote:
>   static inline bool needs_byteswap(const CPUPPCState *env)
>   {
> -#if TARGET_BIG_ENDIAN
> -  return FIELD_EX64(env->msr, MSR, LE);
> -#else
> -  return !FIELD_EX64(env->msr, MSR, LE);
> -#endif
> +  return TARGET_BIG_ENDIAN ^ FIELD_EX64(env->msr, MSR, LE);
>   }

This is wrong.  You wanted

    !TARGET_BIG_ENDIAN ^ ...

Likewise in the other instance in translate.c.


r~


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 5/8] target/xtensa: Evaluate TARGET_BIG_ENDIAN at compile time
  2025-04-17 13:10 ` [PATCH 5/8] target/xtensa: " Philippe Mathieu-Daudé
@ 2025-04-18 17:56   ` Richard Henderson
  0 siblings, 0 replies; 20+ messages in thread
From: Richard Henderson @ 2025-04-18 17:56 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, qemu-devel

On 4/17/25 06:10, Philippe Mathieu-Daudé wrote:
> Rather than evaluating TARGET_BIG_ENDIAN at preprocessing
> time via #ifdef'ry, do it in C at compile time
> 
> Signed-off-by: Philippe Mathieu-Daudé<philmd@linaro.org>
> ---
>   target/xtensa/translate.c | 10 +++++-----
>   1 file changed, 5 insertions(+), 5 deletions(-)

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

r~


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 6/8] hw/mips: Evaluate TARGET_BIG_ENDIAN at compile time
  2025-04-17 13:10 ` [PATCH 6/8] hw/mips: " Philippe Mathieu-Daudé
@ 2025-04-18 17:57   ` Richard Henderson
  0 siblings, 0 replies; 20+ messages in thread
From: Richard Henderson @ 2025-04-18 17:57 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, qemu-devel

On 4/17/25 06:10, Philippe Mathieu-Daudé wrote:
> Rather than evaluating TARGET_BIG_ENDIAN at preprocessing
> time via #ifdef'ry, do it in C at compile time
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
>   hw/mips/jazz.c    | 10 +++-------
>   hw/mips/malta.c   | 21 ++++++---------------
>   hw/mips/mipssim.c | 10 +++-------
>   3 files changed, 12 insertions(+), 29 deletions(-)

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

r~


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 7/8] hw/microblaze: Evaluate TARGET_BIG_ENDIAN at compile time
  2025-04-17 13:10 ` [PATCH 7/8] hw/microblaze: " Philippe Mathieu-Daudé
@ 2025-04-18 17:58   ` Richard Henderson
  0 siblings, 0 replies; 20+ messages in thread
From: Richard Henderson @ 2025-04-18 17:58 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, qemu-devel

On 4/17/25 06:10, Philippe Mathieu-Daudé wrote:
> Rather than evaluating TARGET_BIG_ENDIAN at preprocessing
> time via #ifdef'ry, do it in C at compile time
> 
> Signed-off-by: Philippe Mathieu-Daudé<philmd@linaro.org>
> ---
>   hw/microblaze/petalogix_ml605_mmu.c | 12 ++++++------
>   hw/microblaze/xlnx-zynqmp-pmu.c     | 12 ++++++------
>   2 files changed, 12 insertions(+), 12 deletions(-)

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

r~


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 8/8] gdbstub/helpers: Evaluate TARGET_BIG_ENDIAN at compile time
  2025-04-17 13:10 ` [PATCH 8/8] gdbstub/helpers: " Philippe Mathieu-Daudé
  2025-04-17 16:18   ` Alex Bennée
@ 2025-04-18 18:01   ` Richard Henderson
  1 sibling, 0 replies; 20+ messages in thread
From: Richard Henderson @ 2025-04-18 18:01 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, qemu-devel

On 4/17/25 06:10, Philippe Mathieu-Daudé wrote:
> Rather than evaluating TARGET_BIG_ENDIAN at preprocessing
> time via #ifdef'ry, do it in C at compile time
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
>   include/gdbstub/helpers.h | 11 ++---------
>   1 file changed, 2 insertions(+), 9 deletions(-)
> 
> diff --git a/include/gdbstub/helpers.h b/include/gdbstub/helpers.h
> index 6f7cc48adcb..c33d5dfca3e 100644
> --- a/include/gdbstub/helpers.h
> +++ b/include/gdbstub/helpers.h
> @@ -56,17 +56,10 @@ static inline int gdb_get_reg128(GByteArray *buf, uint64_t val_hi,
>                                    uint64_t val_lo)
>   {
>       uint64_t to_quad;
> -#if TARGET_BIG_ENDIAN
> -    to_quad = tswap64(val_hi);
> +    to_quad = tswap64(TARGET_BIG_ENDIAN ? val_hi : val_lo);
>       g_byte_array_append(buf, (uint8_t *) &to_quad, 8);
> -    to_quad = tswap64(val_lo);
> +    to_quad = tswap64(TARGET_BIG_ENDIAN ? val_lo : val_hi);
>       g_byte_array_append(buf, (uint8_t *) &to_quad, 8);
> -#else
> -    to_quad = tswap64(val_lo);
> -    g_byte_array_append(buf, (uint8_t *) &to_quad, 8);
> -    to_quad = tswap64(val_hi);
> -    g_byte_array_append(buf, (uint8_t *) &to_quad, 8);
> -#endif
>       return 16;
>   }
>   

I'm not keen on using both TARGET_BIG_ENDIAN and tswap.
I think this ought to be

     uint64_t v0, v1;

     if (TARGET_BIG_ENDIAN) {
         v0 = cpu_to_be64(val_hi);
         v1 = cpu_to_be64(val_lo);
     } else {
         v0 = cpu_to_le64(val_lo);
         v1 = cpu_to_le64(val_hi);
     }
     g_byte_array_append(buf, (uint8_t *)&v0, 8);
     g_byte_array_append(buf, (uint8_t *)&v1, 8);


r~


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH 0/8] misc: Prefer evaluating TARGET_BIG_ENDIAN in C
  2025-04-17 13:09 [PATCH 0/8] misc: Prefer evaluating TARGET_BIG_ENDIAN in C Philippe Mathieu-Daudé
                   ` (7 preceding siblings ...)
  2025-04-17 13:10 ` [PATCH 8/8] gdbstub/helpers: " Philippe Mathieu-Daudé
@ 2025-04-25 15:17 ` Philippe Mathieu-Daudé
  8 siblings, 0 replies; 20+ messages in thread
From: Philippe Mathieu-Daudé @ 2025-04-25 15:17 UTC (permalink / raw)
  To: qemu-devel; +Cc: Richard Henderson

On 17/4/25 15:09, Philippe Mathieu-Daudé wrote:

> Philippe Mathieu-Daudé (8):
>    linux-user/elfload: Use target_needs_bswap()
>    accel/kvm: Use target_needs_bswap()
>    target/mips: Check CPU endianness at runtime using env_is_bigendian()

>    target/xtensa: Evaluate TARGET_BIG_ENDIAN at compile time
>    hw/mips: Evaluate TARGET_BIG_ENDIAN at compile time
>    hw/microblaze: Evaluate TARGET_BIG_ENDIAN at compile time

Reviewed patches queued, thanks.


^ permalink raw reply	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2025-04-25 15:18 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-17 13:09 [PATCH 0/8] misc: Prefer evaluating TARGET_BIG_ENDIAN in C Philippe Mathieu-Daudé
2025-04-17 13:09 ` [PATCH 1/8] linux-user/elfload: Use target_needs_bswap() Philippe Mathieu-Daudé
2025-04-18 17:44   ` Richard Henderson
2025-04-17 13:09 ` [PATCH 2/8] accel/kvm: " Philippe Mathieu-Daudé
2025-04-18 17:47   ` Richard Henderson
2025-04-17 13:09 ` [PATCH 3/8] target/mips: Check CPU endianness at runtime using env_is_bigendian() Philippe Mathieu-Daudé
2025-04-18 17:53   ` Richard Henderson
2025-04-17 13:10 ` [PATCH 4/8] target/ppc: Evaluate TARGET_BIG_ENDIAN at compile time Philippe Mathieu-Daudé
2025-04-17 19:26   ` Pierrick Bouvier
2025-04-18 17:56   ` Richard Henderson
2025-04-17 13:10 ` [PATCH 5/8] target/xtensa: " Philippe Mathieu-Daudé
2025-04-18 17:56   ` Richard Henderson
2025-04-17 13:10 ` [PATCH 6/8] hw/mips: " Philippe Mathieu-Daudé
2025-04-18 17:57   ` Richard Henderson
2025-04-17 13:10 ` [PATCH 7/8] hw/microblaze: " Philippe Mathieu-Daudé
2025-04-18 17:58   ` Richard Henderson
2025-04-17 13:10 ` [PATCH 8/8] gdbstub/helpers: " Philippe Mathieu-Daudé
2025-04-17 16:18   ` Alex Bennée
2025-04-18 18:01   ` Richard Henderson
2025-04-25 15:17 ` [PATCH 0/8] misc: Prefer evaluating TARGET_BIG_ENDIAN in C Philippe Mathieu-Daudé

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).