Skip to contentSkip to conversation

hw/core/cpu: Expose cpu_is_big_endian() method

PATCH Superseded Philippe Mathieu-Daudé 2 participants 12 messages Oct 4, 2024

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover hw/core/cpu: Expose cpu_is_big_endian() method

Introduce CPUClass::is_big_endian() handler and expose the cpu_is_big_endian() method. This will be useful later when endianness will be propagated in the slow paths, allowing removal of various TARGET_BIG_ENDIAN uses.

Philippe Mathieu-Daudé (8):
exec/tswap: Massage target_needs_bswap() definition hw/core/cpu: Introduce CPUClass::is_big_endian() handler target/arm: Implement CPUClass::is_big_endian target/microblaze: Implement CPUClass::is_big_endian target/mips: Declare mips_env_is_bigendian() in 'internal.h' target/mips: Implement CPUClass::is_big_endian target/ppc: Register CPUClass::is_big_endian hw/core/cpu: Expose cpu_is_big_endian() method

 include/exec/tswap.h          |  2 +-
 include/hw/core/cpu.h         | 11 ++++++++++-
 target/mips/internal.h        |  5 +++++
 hw/core/cpu-common.c          | 14 ++++++++++++++
 target/arm/cpu.c              |  6 ++++++
 target/microblaze/cpu.c       |  8 ++++++++
 target/mips/cpu.c             |  6 ++++++
 target/mips/tcg/ldst_helper.c | 15 +++++----------
 target/ppc/cpu_init.c         |  5 +++--
 9 files changed, 58 insertions(+), 14 deletions(-)
Signature · 1 line
2.45.2
#1 lore raw

PATCH 1/8 exec/tswap: Massage target_needs_bswap() definition

Invert target_needs_bswap() comparison to match the COMPILING_PER_TARGET definition (2 lines upper).

---
 include/exec/tswap.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/exec/tswap.h +1 -1
index b7a41913475..ecd4faef015 100644
@@ -28,7 +28,7 @@ bool target_words_bigendian(void);
 #ifdef COMPILING_PER_TARGET
 #define target_needs_bswap()  (HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN)
 #else
-#define target_needs_bswap()  (target_words_bigendian() != HOST_BIG_ENDIAN)
+#define target_needs_bswap()  (HOST_BIG_ENDIAN != target_words_bigendian())
 #endif /* COMPILING_PER_TARGET */
 
 static inline uint16_t tswap16(uint16_t s)
Signature · 1 line
2.45.2
#2 lore raw

PATCH 2/8 hw/core/cpu: Introduce CPUClass::is_big_endian() handler

In reply to Philippe Mathieu-Daudé

Introduce the CPUClass::is_big_endian() handler and its common default.

---
 include/hw/core/cpu.h | 3 ++-
 hw/core/cpu-common.c  | 7 +++++++
 2 files changed, 9 insertions(+), 1 deletion(-)
include/hw/core/cpu.h +2 -1
index 04e9ad49968..22ef7a44e86 100644
@@ -150,6 +150,7 @@ struct CPUClass {
     ObjectClass *(*class_by_name)(const char *cpu_model);
     void (*parse_features)(const char *typename, char *str, Error **errp);
 
+    bool (*is_big_endian)(CPUState *cpu);
     bool (*has_work)(CPUState *cpu);
     int (*mmu_index)(CPUState *cpu, bool ifetch);
     int (*memory_rw_debug)(CPUState *cpu, vaddr addr,
@@ -749,7 +750,7 @@ int cpu_asidx_from_attrs(CPUState *cpu, MemTxAttrs attrs);
  */
 bool cpu_virtio_is_big_endian(CPUState *cpu);
 
-#endif /* CONFIG_USER_ONLY */
+#endif /* !CONFIG_USER_ONLY */
 
 /**
  * cpu_list_add:
hw/core/cpu-common.c +7 -0
index 7982ecd39a5..aa5ea9761e4 100644
@@ -26,6 +26,7 @@
 #include "qemu/main-loop.h"
 #include "exec/log.h"
 #include "exec/gdbstub.h"
+#include "exec/tswap.h"
 #include "sysemu/tcg.h"
 #include "hw/boards.h"
 #include "hw/qdev-properties.h"
@@ -138,6 +139,11 @@ static void cpu_common_reset_hold(Object *obj, ResetType type)
     cpu_exec_reset_hold(cpu);
 }
 
+static bool cpu_common_is_big_endian(CPUState *cs)
+{
+    return target_words_bigendian();
+}
+
 static bool cpu_common_has_work(CPUState *cs)
 {
     return false;
@@ -306,6 +312,7 @@ static void cpu_common_class_init(ObjectClass *klass, void *data)
 
     k->parse_features = cpu_common_parse_features;
     k->get_arch_id = cpu_common_get_arch_id;
+    k->is_big_endian = cpu_common_is_big_endian;
     k->has_work = cpu_common_has_work;
     k->gdb_read_register = cpu_common_gdb_read_register;
     k->gdb_write_register = cpu_common_gdb_write_register;
Signature · 1 line
2.45.2
#3 lore raw

Re: [PATCH 2/8] hw/core/cpu: Introduce CPUClass::is_big_endian() handler

Quoted 15 lines · Philippe Mathieu-Daudé

Introduce the CPUClass::is_big_endian() handler and its common default.

---
 include/hw/core/cpu.h | 3 ++-
 hw/core/cpu-common.c  | 7 +++++++
 2 files changed, 9 insertions(+), 1 deletion(-)
include/hw/core/cpu.h +0 -0
index 04e9ad49968..22ef7a44e86 100644

include/hw/core/cpu.h struct CPUClass { · line 156

Quoted 7 lines
include/hw/core/cpu.h +1 -0
@@ -150,6 +150,7 @@ struct CPUClass {
     ObjectClass *(*class_by_name)(const char *cpu_model);
     void (*parse_features)(const char *typename, char *str, Error **errp);
 
+    bool (*is_big_endian)(CPUState *cpu);
     bool (*has_work)(CPUState *cpu);
     int (*mmu_index)(CPUState *cpu, bool ifetch);
     int (*memory_rw_debug)(CPUState *cpu, vaddr addr,

What does this actually mean, though? Arm for instance has multiple different kinds of "big-endian" depending on the CPU: both BE32 and BE8, and data-endianness not necessarily being the same as instruction-endianness.

This series doesn't introduce any users of this new API. It's hard to say without seeing what the intended use is, but I would expect that probably they would want to be testing something else, depending on what they're trying to do.

It's pretty uncommon for anything out in the system to want to know what endianness a runtime-configurable CPU happens to be set to, because in real hardware a device has no way to tell. (This is why cpu_virtio_is_big_endian() is named the way it is -- to discourage anybody from trying to use it outside the virtio devices where we need it for legacy "the spec wasn't written thinking about endianness very hard" reasons.)

thanks
-- PMM

#4 lore raw

Re: [PATCH 2/8] hw/core/cpu: Introduce CPUClass::is_big_endian() handler

Quoted 19 lines · Peter Maydell
Quoted 18 lines · Philippe Mathieu-Daudé

Introduce the CPUClass::is_big_endian() handler and its common default.

---
  include/hw/core/cpu.h | 3 ++-
  hw/core/cpu-common.c  | 7 +++++++
  2 files changed, 9 insertions(+), 1 deletion(-)
include/hw/core/cpu.h +0 -0
index 04e9ad49968..22ef7a44e86 100644
@@ -150,6 +150,7 @@ struct CPUClass {
      ObjectClass *(*class_by_name)(const char *cpu_model);
      void (*parse_features)(const char *typename, char *str, Error **errp);
 
Quoted 9 lines

include/hw/core/cpu.h struct CPUClass { · line 156

Quoted 4 lines
include/hw/core/cpu.h +1 -0
+    bool (*is_big_endian)(CPUState *cpu);
      bool (*has_work)(CPUState *cpu);
      int (*mmu_index)(CPUState *cpu, bool ifetch);
      int (*memory_rw_debug)(CPUState *cpu, vaddr addr,

What does this actually mean, though? Arm for instance has multiple different kinds of "big-endian" depending on the CPU: both BE32 and BE8, and data-endianness not necessarily being the same as instruction-endianness.

This is to be used in the data bus (I was wondering whether using 'data' in the method name).

Quoted 4 lines

This series doesn't introduce any users of this new API. It's hard to say without seeing what the intended use is, but I would expect that probably they would want to be testing something else, depending on what they're trying to do.

I'm trying to split my branch in "~20 patches series"; I should post example of API consumers in a few days.

First conversion is the cpu_in/out() API in system/ioport.c, I'll try to post it ASAP so we can discuss there.

Quoted 8 lines

It's pretty uncommon for anything out in the system to want to know what endianness a runtime-configurable CPU happens to be set to, because in real hardware a device has no way to tell. (This is why cpu_virtio_is_big_endian() is named the way it is -- to discourage anybody from trying to use it outside the virtio devices where we need it for legacy "the spec wasn't written thinking about endianness very hard" reasons.)

I'm trying to convert implicit knowledge of target endianness to explicit one, propagated as argument from the machine.

#5 lore raw

Re: [PATCH 2/8] hw/core/cpu: Introduce CPUClass::is_big_endian() handler

Quoted 31 lines · Philippe Mathieu-Daudé
Quoted 19 lines · Peter Maydell
Quoted 18 lines · Philippe Mathieu-Daudé

Introduce the CPUClass::is_big_endian() handler and its common default.

---
  include/hw/core/cpu.h | 3 ++-
  hw/core/cpu-common.c  | 7 +++++++
  2 files changed, 9 insertions(+), 1 deletion(-)
include/hw/core/cpu.h +0 -0
index 04e9ad49968..22ef7a44e86 100644
@@ -150,6 +150,7 @@ struct CPUClass {
      ObjectClass *(*class_by_name)(const char *cpu_model);
      void (*parse_features)(const char *typename, char *str, Error **errp);
 
Quoted 9 lines

include/hw/core/cpu.h struct CPUClass { · line 156

Quoted 4 lines
include/hw/core/cpu.h +1 -0
+    bool (*is_big_endian)(CPUState *cpu);
      bool (*has_work)(CPUState *cpu);
      int (*mmu_index)(CPUState *cpu, bool ifetch);
      int (*memory_rw_debug)(CPUState *cpu, vaddr addr,

What does this actually mean, though? Arm for instance has multiple different kinds of "big-endian" depending on the CPU: both BE32 and BE8, and data-endianness not necessarily being the same as instruction-endianness.

Quoted 2 lines

This is to be used in the data bus (I was wondering whether using 'data' in the method name).

That sounds like what you actually want is (a non-compile-time version of) TARGET_ENDIANNESS, which is not related to the CPU's dynamic setting.

Quoted 10 lines
Quoted 4 lines

This series doesn't introduce any users of this new API. It's hard to say without seeing what the intended use is, but I would expect that probably they would want to be testing something else, depending on what they're trying to do.

I'm trying to split my branch in "~20 patches series"; I should post example of API consumers in a few days.

First conversion is the cpu_in/out() API in system/ioport.c, I'll try to post it ASAP so we can discuss there.

Yeah, I think we really need to look at the users, because my current feeling is "we don't want this API at all, the answer will always be to use something else".

I suspect for cpu_in/out the answer is "this API only makes sense for x86, and whatever extent it's built for anything else is accidental". We could probably define it as always-little-endian.

-- PMM

#6 lore raw

PATCH 3/8 target/arm: Implement CPUClass::is_big_endian

In reply to Philippe Mathieu-Daudé
---
 target/arm/cpu.c | 6 ++++++
 1 file changed, 6 insertions(+)
target/arm/cpu.c +6 -0
index 19191c23918..2bb87a9299f 100644
@@ -122,6 +122,11 @@ void arm_restore_state_to_opc(CPUState *cs,
 }
 #endif /* CONFIG_TCG */
 
+static bool arm_cpu_is_big_endian(CPUState *cs)
+{
+    return arm_cpu_data_is_big_endian(cpu_env(cs));
+}
+
 /*
  * With SCTLR_ELx.NMI == 0, IRQ with Superpriority is masked identically with
  * IRQ without Superpriority. Moreover, if the GIC is configured so that
@@ -2692,6 +2697,7 @@ static void arm_cpu_class_init(ObjectClass *oc, void *data)
                                        &acc->parent_phases);
 
     cc->class_by_name = arm_cpu_class_by_name;
+    cc->is_big_endian = arm_cpu_is_big_endian;
     cc->has_work = arm_cpu_has_work;
     cc->mmu_index = arm_cpu_mmu_index;
     cc->dump_state = arm_cpu_dump_state;
Signature · 1 line
2.45.2
#7 lore raw

PATCH 4/8 target/microblaze: Implement CPUClass::is_big_endian

In reply to Philippe Mathieu-Daudé
---
 target/microblaze/cpu.c | 8 ++++++++
 1 file changed, 8 insertions(+)
target/microblaze/cpu.c +8 -0
index 135947ee800..93c0c6d36c8 100644
@@ -119,6 +119,13 @@ static bool mb_cpu_has_work(CPUState *cs)
     return cs->interrupt_request & (CPU_INTERRUPT_HARD | CPU_INTERRUPT_NMI);
 }
 
+static bool mb_cpu_is_big_endian(CPUState *cs)
+{
+    MicroBlazeCPU *cpu = MICROBLAZE_CPU(cs);
+
+    return !cpu->cfg.endi;
+}
+
 static int mb_cpu_mmu_index(CPUState *cs, bool ifetch)
 {
     CPUMBState *env = cpu_env(cs);
@@ -433,6 +440,7 @@ static void mb_cpu_class_init(ObjectClass *oc, void *data)
                                        &mcc->parent_phases);
 
     cc->class_by_name = mb_cpu_class_by_name;
+    cc->is_big_endian = mb_cpu_is_big_endian;
     cc->has_work = mb_cpu_has_work;
     cc->mmu_index = mb_cpu_mmu_index;
     cc->dump_state = mb_cpu_dump_state;
Signature · 1 line
2.45.2
#8 lore raw

PATCH 5/8 target/mips: Declare mips_env_is_bigendian() in 'internal.h'

In reply to Philippe Mathieu-Daudé

In order to re-use cpu_is_bigendian(), declare it on "internal.h" after renaming it as mips_env_is_bigendian().

---
 target/mips/internal.h        |  5 +++++
 target/mips/tcg/ldst_helper.c | 15 +++++----------
 2 files changed, 10 insertions(+), 10 deletions(-)
target/mips/internal.h +5 -0
index a9a22ea00ec..84c8e5e1ae7 100644
@@ -225,6 +225,11 @@ static inline void mips_env_set_pc(CPUMIPSState *env, target_ulong value)
     }
 }
 
+static inline bool mips_env_is_bigendian(CPUMIPSState *env)
+{
+    return extract32(env->CP0_Config0, CP0C0_BE, 1);
+}
+
 static inline void restore_pamask(CPUMIPSState *env)
 {
     if (env->hflags & MIPS_HFLAG_ELPA) {
target/mips/tcg/ldst_helper.c +5 -10
index 97056d00a27..f92a923d7ad 100644
@@ -53,11 +53,6 @@ HELPER_LD_ATOMIC(lld, ldq, 0x7, (target_ulong))
 
 #endif /* !CONFIG_USER_ONLY */
 
-static inline bool cpu_is_bigendian(CPUMIPSState *env)
-{
-    return extract32(env->CP0_Config0, CP0C0_BE, 1);
-}
-
 static inline target_ulong get_lmask(CPUMIPSState *env,
                                      target_ulong value, unsigned bits)
 {
@@ -65,7 +60,7 @@ static inline target_ulong get_lmask(CPUMIPSState *env,
 
     value &= mask;
 
-    if (!cpu_is_bigendian(env)) {
+    if (!mips_env_is_bigendian(env)) {
         value ^= mask;
     }
 
@@ -76,7 +71,7 @@ void helper_swl(CPUMIPSState *env, target_ulong arg1, target_ulong arg2,
                 int mem_idx)
 {
     target_ulong lmask = get_lmask(env, arg2, 32);
-    int dir = cpu_is_bigendian(env) ? 1 : -1;
+    int dir = mips_env_is_bigendian(env) ? 1 : -1;
 
     cpu_stb_mmuidx_ra(env, arg2, (uint8_t)(arg1 >> 24), mem_idx, GETPC());
 
@@ -100,7 +95,7 @@ void helper_swr(CPUMIPSState *env, target_ulong arg1, target_ulong arg2,
                 int mem_idx)
 {
     target_ulong lmask = get_lmask(env, arg2, 32);
-    int dir = cpu_is_bigendian(env) ? 1 : -1;
+    int dir = mips_env_is_bigendian(env) ? 1 : -1;
 
     cpu_stb_mmuidx_ra(env, arg2, (uint8_t)arg1, mem_idx, GETPC());
 
@@ -130,7 +125,7 @@ void helper_sdl(CPUMIPSState *env, target_ulong arg1, target_ulong arg2,
                 int mem_idx)
 {
     target_ulong lmask = get_lmask(env, arg2, 64);
-    int dir = cpu_is_bigendian(env) ? 1 : -1;
+    int dir = mips_env_is_bigendian(env) ? 1 : -1;
 
     cpu_stb_mmuidx_ra(env, arg2, (uint8_t)(arg1 >> 56), mem_idx, GETPC());
 
@@ -174,7 +169,7 @@ void helper_sdr(CPUMIPSState *env, target_ulong arg1, target_ulong arg2,
                 int mem_idx)
 {
     target_ulong lmask = get_lmask(env, arg2, 64);
-    int dir = cpu_is_bigendian(env) ? 1 : -1;
+    int dir = mips_env_is_bigendian(env) ? 1 : -1;
 
     cpu_stb_mmuidx_ra(env, arg2, (uint8_t)arg1, mem_idx, GETPC());
 
Signature · 1 line
2.45.2
#9 lore raw

PATCH 6/8 target/mips: Implement CPUClass::is_big_endian

In reply to Philippe Mathieu-Daudé
---
 target/mips/cpu.c | 6 ++++++
 1 file changed, 6 insertions(+)
target/mips/cpu.c +6 -0
index 89655b1900f..ad70ad802a7 100644
@@ -132,6 +132,11 @@ static vaddr mips_cpu_get_pc(CPUState *cs)
     return cpu->env.active_tc.PC;
 }
 
+static bool mips_cpu_is_big_endian(CPUState *cs)
+{
+    return mips_env_is_bigendian(cpu_env(cs));
+}
+
 static bool mips_cpu_has_work(CPUState *cs)
 {
     CPUMIPSState *env = cpu_env(cs);
@@ -577,6 +582,7 @@ static void mips_cpu_class_init(ObjectClass *c, void *data)
                                        &mcc->parent_phases);
 
     cc->class_by_name = mips_cpu_class_by_name;
+    cc->is_big_endian = mips_cpu_is_big_endian;
     cc->has_work = mips_cpu_has_work;
     cc->mmu_index = mips_cpu_mmu_index;
     cc->dump_state = mips_cpu_dump_state;
Signature · 1 line
2.45.2
#10 lore raw

PATCH 7/8 target/ppc: Register CPUClass::is_big_endian

In reply to Philippe Mathieu-Daudé
---
 target/ppc/cpu_init.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
target/ppc/cpu_init.c +3 -2
index 23881d09e9f..39ec290cdc0 100644
@@ -7341,8 +7341,6 @@ static void ppc_cpu_reset_hold(Object *obj, ResetType type)
 #endif
 }
 
-#ifndef CONFIG_USER_ONLY
-
 static bool ppc_cpu_is_big_endian(CPUState *cs)
 {
     cpu_synchronize_state(cs);
@@ -7350,6 +7348,8 @@ static bool ppc_cpu_is_big_endian(CPUState *cs)
     return !FIELD_EX64(cpu_env(cs)->msr, MSR, LE);
 }
 
+#ifndef CONFIG_USER_ONLY
+
 static bool ppc_get_irq_stats(InterruptStatsProvider *obj,
                               uint64_t **irq_counts, unsigned int *nb_irqs)
 {
@@ -7515,6 +7515,7 @@ static void ppc_cpu_class_init(ObjectClass *oc, void *data)
                                        &pcc->parent_phases);
 
     cc->class_by_name = ppc_cpu_class_by_name;
+    cc->is_big_endian = ppc_cpu_is_big_endian;
     cc->has_work = ppc_cpu_has_work;
     cc->mmu_index = ppc_cpu_mmu_index;
     cc->dump_state = ppc_cpu_dump_state;
Signature · 1 line
2.45.2
#11 lore raw

PATCH 8/8 hw/core/cpu: Expose cpu_is_big_endian() method

In reply to Philippe Mathieu-Daudé
---
 include/hw/core/cpu.h | 8 ++++++++
 hw/core/cpu-common.c  | 7 +++++++
 2 files changed, 15 insertions(+)
include/hw/core/cpu.h +8 -0
index 22ef7a44e86..2550bc98dee 100644
@@ -608,6 +608,14 @@ extern __thread CPUState *current_cpu;
 extern bool mttcg_enabled;
 #define qemu_tcg_mttcg_enabled() (mttcg_enabled)
 
+/**
+ * cpu_is_big_endian:
+ * @cpu: The CPU whose state is to be inspected.
+ *
+ * Returns: %true if the CPU runs in big endianness, %false otherwise.
+ */
+bool cpu_is_big_endian(CPUState *cpu);
+
 /**
  * cpu_paging_enabled:
  * @cpu: The CPU whose state is to be inspected.
hw/core/cpu-common.c +7 -0
index aa5ea9761e4..277c4ee6720 100644
@@ -66,6 +66,13 @@ CPUState *cpu_create(const char *typename)
     return cpu;
 }
 
+bool cpu_is_big_endian(CPUState *cpu)
+{
+    CPUClass *cc = CPU_GET_CLASS(cpu);
+
+    return cc->is_big_endian(cpu);
+}
+
 /* Resetting the IRQ comes from across the code base so we take the
  * BQL here if we need to.  cpu_interrupt assumes it is held.*/
 void cpu_reset_interrupt(CPUState *cpu, int mask)
Signature · 1 line
2.45.2