Skip to contentSkip to conversation

target/mips: Make 'cpu-qom.h' target agnostic

PATCH In review Philippe Mathieu-Daudé 4 participants 20 messages Mar 25, 2025 – Mar 27, 2025

lore
Share & tools
lore
#0 lore raw

cover target/mips: Make 'cpu-qom.h' target agnostic

- Remove the TARGET_MIPS64 use in 'cpu-qom.h' to make it target agnostic
- Introduce mips_cpu_is_64bit() as a runtime check for compile time TARGET_MIPS64 definition
- Replace the ldtul_p() gdbstub call by ldn_p()
- Rename few symbols to avoid future linkage clash

Philippe Mathieu-Daudé (8):
cpus: Open code OBJECT_DECLARE_TYPE() in OBJECT_DECLARE_CPU_TYPE() target/mips: Declare CPU QOM types using DEFINE_TYPES() macro target/mips: Make MIPS_CPU common to new MIPS32_CPU / MIPS64_CPU types target/mips: Prefix MMU API with 'mips_'
target/mips: Replace ldtul_p() -> ldn_p(sizeof(target_ulong)) target/mips: Introduce mips_cpu_is_64bit() helper target/mips: Get CPU register size using mips_cpu_is_64bit() target/mips: Introduce mips_env_64bit_enabled() helper

 include/hw/core/cpu.h               |  7 ++++-
 target/mips/cpu-qom.h               | 14 +++++-----
 target/mips/internal.h              |  5 ++++
 target/mips/tcg/tcg-internal.h      |  2 +-
 target/mips/cpu.c                   | 42 ++++++++++++++++++++---------
 target/mips/gdbstub.c               | 14 +++++++---
 target/mips/tcg/system/tlb_helper.c |  2 +-
 7 files changed, 61 insertions(+), 25 deletions(-)
Signature · 1 line
2.47.1
#1 lore raw

PATCH 1/8 cpus: Open code OBJECT_DECLARE_TYPE() in OBJECT_DECLARE_CPU_TYPE()

Since the OBJECT_DECLARE_CPU_TYPE() macro uses the abstract ArchCPU type, when declaring multiple CPUs of the same ArchCPU type we get an error related to the indirect G_DEFINE_AUTOPTR_CLEANUP_FUNC() use within OBJECT_DECLARE_TYPE():

  target/mips/cpu-qom.h:31:1: error: redefinition of 'glib_autoptr_clear_ArchCPU'
  OBJECT_DECLARE_CPU_TYPE(MIPS64CPU, MIPSCPUClass, MIPS64_CPU)
  ^
  include/hw/core/cpu.h:82:5: note: expanded from macro 'OBJECT_DECLARE_CPU_TYPE'
      OBJECT_DECLARE_TYPE(ArchCPU, CpuClassType, CPU_MODULE_OBJ_NAME);
      ^
  include/qom/object.h:237:5: note: expanded from macro 'OBJECT_DECLARE_TYPE'
      G_DEFINE_AUTOPTR_CLEANUP_FUNC(InstanceType, object_unref) \
      ^
  /usr/include/glib-2.0/glib/gmacros.h:1371:3: note: expanded from macro 'G_DEFINE_AUTOPTR_CLEANUP_FUNC'
    _GLIB_DEFINE_AUTOPTR_CLEANUP_FUNCS(TypeName, TypeName, func)
    ^
  /usr/include/glib-2.0/glib/gmacros.h:1354:36: note: expanded from macro '_GLIB_DEFINE_AUTOPTR_CLEANUP_FUNCS'
    static G_GNUC_UNUSED inline void _GLIB_AUTOPTR_CLEAR_FUNC_NAME(TypeName) (TypeName *_ptr)                     \
                                     ^
  /usr/include/glib-2.0/glib/gmacros.h:1338:49: note: expanded from macro '_GLIB_AUTOPTR_CLEAR_FUNC_NAME'
  #define _GLIB_AUTOPTR_CLEAR_FUNC_NAME(TypeName) glib_autoptr_clear_##TypeName
                                                  ^
  <scratch space>:54:1: note: expanded from here
  glib_autoptr_clear_ArchCPU
  ^
  target/mips/cpu-qom.h:30:1: note: previous definition is here
  OBJECT_DECLARE_CPU_TYPE(MIPS32CPU, MIPSCPUClass, MIPS32_CPU)
  ^

Avoid that problem by expanding the OBJECT_DECLARE_TYPE() macro within OBJECT_DECLARE_CPU_TYPE().

---
 include/hw/core/cpu.h | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
include/hw/core/cpu.h +6 -1
index 5d11d26556a..01e03f267cc 100644
@@ -82,7 +82,12 @@ DECLARE_CLASS_CHECKERS(CPUClass, CPU,
  */
 #define OBJECT_DECLARE_CPU_TYPE(CpuInstanceType, CpuClassType, CPU_MODULE_OBJ_NAME) \
     typedef struct ArchCPU CpuInstanceType; \
-    OBJECT_DECLARE_TYPE(ArchCPU, CpuClassType, CPU_MODULE_OBJ_NAME);
+    typedef struct CpuClassType CpuClassType; \
+    \
+    G_DEFINE_AUTOPTR_CLEANUP_FUNC(CpuInstanceType, object_unref) \
+    \
+    DECLARE_OBJ_CHECKERS(CpuInstanceType, CpuClassType, \
+                         CPU_MODULE_OBJ_NAME, TYPE_##CPU_MODULE_OBJ_NAME)
 
 typedef struct CPUWatchpoint CPUWatchpoint;
 
Signature · 1 line
2.47.1
#2 lore raw

PATCH 2/8 target/mips: Declare CPU QOM types using DEFINE_TYPES() macro

In reply to Philippe Mathieu-Daudé

When multiple QOM types are registered in the same file, it is simpler to use the the DEFINE_TYPES() macro. In particular because type array declared with such macro are easier to review.

In few commits we are going to add more types, so replace the type_register_static() to ease further reviews.

---
 target/mips/cpu.c | 23 +++++++++++++----------
 1 file changed, 13 insertions(+), 10 deletions(-)
target/mips/cpu.c +13 -10
index b207106dd79..097554fd8ae 100644
@@ -597,17 +597,21 @@ static void mips_cpu_class_init(ObjectClass *c, void *data)
 #endif /* CONFIG_TCG */
 }
 
-static const TypeInfo mips_cpu_type_info = {
-    .name = TYPE_MIPS_CPU,
-    .parent = TYPE_CPU,
-    .instance_size = sizeof(MIPSCPU),
-    .instance_align = __alignof(MIPSCPU),
-    .instance_init = mips_cpu_initfn,
-    .abstract = true,
-    .class_size = sizeof(MIPSCPUClass),
-    .class_init = mips_cpu_class_init,
+static const TypeInfo mips_cpu_types[] = {
+    {
+        .name           = TYPE_MIPS_CPU,
+        .parent         = TYPE_CPU,
+        .instance_size  = sizeof(MIPSCPU),
+        .instance_align = __alignof(MIPSCPU),
+        .instance_init  = mips_cpu_initfn,
+        .abstract       = true,
+        .class_size     = sizeof(MIPSCPUClass),
+        .class_init     = mips_cpu_class_init,
+    }
 };
 
+DEFINE_TYPES(mips_cpu_types)
+
 static void mips_cpu_cpudef_class_init(ObjectClass *oc, void *data)
 {
     MIPSCPUClass *mcc = MIPS_CPU_CLASS(oc);
@@ -632,7 +636,6 @@ static void mips_cpu_register_types(void)
 {
     int i;
 
-    type_register_static(&mips_cpu_type_info);
     for (i = 0; i < mips_defs_number; i++) {
         mips_register_cpudef_type(&mips_defs[i]);
     }
Signature · 1 line
2.47.1
Reviewed-by on v1 2/8
#3 lore raw

Re: [PATCH-for-10.1 2/8] target/mips: Declare CPU QOM types using DEFINE_TYPES() macro

Quoted 50 lines · Philippe Mathieu-Daudé

When multiple QOM types are registered in the same file, it is simpler to use the the DEFINE_TYPES() macro. In particular because type array declared with such macro are easier to review.

In few commits we are going to add more types, so replace the type_register_static() to ease further reviews.

---
  target/mips/cpu.c | 23 +++++++++++++----------
  1 file changed, 13 insertions(+), 10 deletions(-)
target/mips/cpu.c +13 -9
index b207106dd79..097554fd8ae 100644
@@ -597,17 +597,21 @@ static void mips_cpu_class_init(ObjectClass *c, void *data)
  #endif /* CONFIG_TCG */
  }
 
-static const TypeInfo mips_cpu_type_info = {
-    .name = TYPE_MIPS_CPU,
-    .parent = TYPE_CPU,
-    .instance_size = sizeof(MIPSCPU),
-    .instance_align = __alignof(MIPSCPU),
-    .instance_init = mips_cpu_initfn,
-    .abstract = true,
-    .class_size = sizeof(MIPSCPUClass),
-    .class_init = mips_cpu_class_init,
+static const TypeInfo mips_cpu_types[] = {
+    {
+        .name           = TYPE_MIPS_CPU,
+        .parent         = TYPE_CPU,
+        .instance_size  = sizeof(MIPSCPU),
+        .instance_align = __alignof(MIPSCPU),
+        .instance_init  = mips_cpu_initfn,
+        .abstract       = true,
+        .class_size     = sizeof(MIPSCPUClass),
+        .class_init     = mips_cpu_class_init,
+    }
  };
 
+DEFINE_TYPES(mips_cpu_types)
+
  static void mips_cpu_cpudef_class_init(ObjectClass *oc, void *data)
  {
      MIPSCPUClass *mcc = MIPS_CPU_CLASS(oc);

target/mips/cpu.c static void mips_cpu_register_types(void) · line 641

Quoted 7 lines
target/mips/cpu.c +0 -1
@@ -632,7 +636,6 @@ static void mips_cpu_register_types(void)
  {
      int i;
 
-    type_register_static(&mips_cpu_type_info);
      for (i = 0; i < mips_defs_number; i++) {
          mips_register_cpudef_type(&mips_defs[i]);
      }
#4 lore raw

PATCH 3/8 target/mips: Make MIPS_CPU common to new MIPS32_CPU / MIPS64_CPU types

In reply to Philippe Mathieu-Daudé

"target/foo/cpu-qom.h" can not use any target specific definitions.

Currently "target/mips/cpu-qom.h" defines TYPE_MIPS_CPU depending on the mips(32)/mips64 build type. This doesn't scale in a heterogeneous context where we need to access both types concurrently.

In order to do that, introduce the new MIPS32_CPU / MIPS64_CPU types, both inheriting a common TYPE_MIPS_CPU base type.

Keep the current CPU types registered in mips_register_cpudef_type() as 32 or 64-bit, but instead of depending on the binary built being targeting 32/64-bit, check whether the CPU is 64-bit by looking at the CPU_MIPS64 bit.

---
 target/mips/cpu-qom.h | 12 ++++++------
 target/mips/cpu.c     | 11 ++++++++++-
 2 files changed, 16 insertions(+), 7 deletions(-)
target/mips/cpu-qom.h +6 -6
index 0eea2a2598e..9acf647420c 100644
@@ -1,5 +1,5 @@
 /*
- * QEMU MIPS CPU
+ * QEMU MIPS CPU QOM header (target agnostic)
  *
  * Copyright (c) 2012 SUSE LINUX Products GmbH
  *
@@ -22,12 +22,12 @@
 
 #include "hw/core/cpu.h"
 
-#ifdef TARGET_MIPS64
-#define TYPE_MIPS_CPU "mips64-cpu"
-#else
-#define TYPE_MIPS_CPU "mips-cpu"
-#endif
+#define TYPE_MIPS32_CPU "mips32-cpu"
+#define TYPE_MIPS64_CPU "mips64-cpu"
+#define TYPE_MIPS_CPU   "mips-cpu"
 
+OBJECT_DECLARE_CPU_TYPE(MIPS32CPU, MIPSCPUClass, MIPS32_CPU)
+OBJECT_DECLARE_CPU_TYPE(MIPS64CPU, MIPSCPUClass, MIPS64_CPU)
 OBJECT_DECLARE_CPU_TYPE(MIPSCPU, MIPSCPUClass, MIPS_CPU)
 
 #define MIPS_CPU_TYPE_SUFFIX "-" TYPE_MIPS_CPU
target/mips/cpu.c +10 -1
index 097554fd8ae..5ed6b3402d3 100644
@@ -607,6 +607,14 @@ static const TypeInfo mips_cpu_types[] = {
         .abstract       = true,
         .class_size     = sizeof(MIPSCPUClass),
         .class_init     = mips_cpu_class_init,
+    }, {
+        .name           = TYPE_MIPS32_CPU,
+        .parent         = TYPE_MIPS_CPU,
+        .abstract       = true,
+    }, {
+        .name           = TYPE_MIPS64_CPU,
+        .parent         = TYPE_MIPS_CPU,
+        .abstract       = true,
     }
 };
 
@@ -623,7 +631,8 @@ static void mips_register_cpudef_type(const struct mips_def_t *def)
     char *typename = mips_cpu_type_name(def->name);
     TypeInfo ti = {
         .name = typename,
-        .parent = TYPE_MIPS_CPU,
+        .parent = def->insn_flags & CPU_MIPS64
+                  ? TYPE_MIPS64_CPU : TYPE_MIPS32_CPU,
         .class_init = mips_cpu_cpudef_class_init,
         .class_data = (void *)def,
     };
Signature · 1 line
2.47.1
Reviewed-by on v1 3/8
#5 lore raw

Re: [PATCH-for-10.1 3/8] target/mips: Make MIPS_CPU common to new MIPS32_CPU / MIPS64_CPU types

Quoted 69 lines · Philippe Mathieu-Daudé

"target/foo/cpu-qom.h" can not use any target specific definitions.

Currently "target/mips/cpu-qom.h" defines TYPE_MIPS_CPU depending on the mips(32)/mips64 build type. This doesn't scale in a heterogeneous context where we need to access both types concurrently.

In order to do that, introduce the new MIPS32_CPU / MIPS64_CPU types, both inheriting a common TYPE_MIPS_CPU base type.

Keep the current CPU types registered in mips_register_cpudef_type() as 32 or 64-bit, but instead of depending on the binary built being targeting 32/64-bit, check whether the CPU is 64-bit by looking at the CPU_MIPS64 bit.

---
  target/mips/cpu-qom.h | 12 ++++++------
  target/mips/cpu.c     | 11 ++++++++++-
  2 files changed, 16 insertions(+), 7 deletions(-)
target/mips/cpu-qom.h +6 -6
index 0eea2a2598e..9acf647420c 100644
@@ -1,5 +1,5 @@
  /*
- * QEMU MIPS CPU
+ * QEMU MIPS CPU QOM header (target agnostic)
   *
   * Copyright (c) 2012 SUSE LINUX Products GmbH
   *
@@ -22,12 +22,12 @@
 
  #include "hw/core/cpu.h"
 
-#ifdef TARGET_MIPS64
-#define TYPE_MIPS_CPU "mips64-cpu"
-#else
-#define TYPE_MIPS_CPU "mips-cpu"
-#endif
+#define TYPE_MIPS32_CPU "mips32-cpu"
+#define TYPE_MIPS64_CPU "mips64-cpu"
+#define TYPE_MIPS_CPU   "mips-cpu"
 
+OBJECT_DECLARE_CPU_TYPE(MIPS32CPU, MIPSCPUClass, MIPS32_CPU)
+OBJECT_DECLARE_CPU_TYPE(MIPS64CPU, MIPSCPUClass, MIPS64_CPU)
  OBJECT_DECLARE_CPU_TYPE(MIPSCPU, MIPSCPUClass, MIPS_CPU)
 
  #define MIPS_CPU_TYPE_SUFFIX "-" TYPE_MIPS_CPU
target/mips/cpu.c +8 -0
index 097554fd8ae..5ed6b3402d3 100644
@@ -607,6 +607,14 @@ static const TypeInfo mips_cpu_types[] = {
          .abstract       = true,
          .class_size     = sizeof(MIPSCPUClass),
          .class_init     = mips_cpu_class_init,
+    }, {
+        .name           = TYPE_MIPS32_CPU,
+        .parent         = TYPE_MIPS_CPU,
+        .abstract       = true,
+    }, {
+        .name           = TYPE_MIPS64_CPU,
+        .parent         = TYPE_MIPS_CPU,
+        .abstract       = true,
      }
  };
 

target/mips/cpu.c static void mips_register_cpudef_type(const struct mips_def_t *def) · line 638

Quoted 9 lines
target/mips/cpu.c +2 -1
@@ -623,7 +631,8 @@ static void mips_register_cpudef_type(const struct mips_def_t *def)
      char *typename = mips_cpu_type_name(def->name);
      TypeInfo ti = {
          .name = typename,
-        .parent = TYPE_MIPS_CPU,
+        .parent = def->insn_flags & CPU_MIPS64
+                  ? TYPE_MIPS64_CPU : TYPE_MIPS32_CPU,
          .class_init = mips_cpu_cpudef_class_init,
          .class_data = (void *)def,
      };

I'm not sure we absolutely need to introduce a new common type TYPE_MIPS_CPU.

If types don't share any common data, or have specific method applying to them, I would just define 32/64 types without a common ancestor.

That said, if you prefer, or if needed, I'm ok with the patch as it is:

#6 lore raw

PATCH 4/8 target/mips: Prefix MMU API with 'mips_'

In reply to Philippe Mathieu-Daudé

MIPS MMU API declared in tcg-internal.h has public linkage. In order to avoid name clashing with other targets, prefix the API with 'mips_'.

---
 target/mips/tcg/tcg-internal.h      | 2 +-
 target/mips/cpu.c                   | 2 +-
 target/mips/tcg/system/tlb_helper.c | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
target/mips/tcg/tcg-internal.h +1 -1
index 74fc1309a71..a8bf2a5da40 100644
@@ -45,7 +45,7 @@ void do_raise_exception(CPUMIPSState *env,
 void mips_cpu_do_interrupt(CPUState *cpu);
 bool mips_cpu_exec_interrupt(CPUState *cpu, int int_req);
 
-void mmu_init(CPUMIPSState *env, const mips_def_t *def);
+void mips_mmu_init(CPUMIPSState *env, const mips_def_t *def);
 
 void update_pagemask(CPUMIPSState *env, target_ulong arg1, int32_t *pagemask);
 
target/mips/cpu.c +1 -1
index 5ed6b3402d3..d8930468b7d 100644
@@ -485,7 +485,7 @@ static void mips_cpu_realizefn(DeviceState *dev, Error **errp)
     env->exception_base = (int32_t)0xBFC00000;
 
 #if defined(CONFIG_TCG) && !defined(CONFIG_USER_ONLY)
-    mmu_init(env, env->cpu_model);
+    mips_mmu_init(env, env->cpu_model);
 #endif
     fpu_init(env, env->cpu_model);
     mvp_init(env);
target/mips/tcg/system/tlb_helper.c +1 -1
index ca4d6b27bc9..1ef2c32cfd4 100644
@@ -466,7 +466,7 @@ static void r4k_mmu_init(CPUMIPSState *env, const mips_def_t *def)
     env->tlb->helper_tlbinvf = r4k_helper_tlbinvf;
 }
 
-void mmu_init(CPUMIPSState *env, const mips_def_t *def)
+void mips_mmu_init(CPUMIPSState *env, const mips_def_t *def)
 {
     env->tlb = g_malloc0(sizeof(CPUMIPSTLBContext));
 
Signature · 1 line
2.47.1
Reviewed-by on v1 4/8
#7 lore raw

Re: [PATCH-for-10.1 4/8] target/mips: Prefix MMU API with 'mips_'

Quoted 42 lines · Philippe Mathieu-Daudé

MIPS MMU API declared in tcg-internal.h has public linkage. In order to avoid name clashing with other targets, prefix the API with 'mips_'.

---
  target/mips/tcg/tcg-internal.h      | 2 +-
  target/mips/cpu.c                   | 2 +-
  target/mips/tcg/system/tlb_helper.c | 2 +-
  3 files changed, 3 insertions(+), 3 deletions(-)
target/mips/tcg/tcg-internal.h +1 -1
index 74fc1309a71..a8bf2a5da40 100644
@@ -45,7 +45,7 @@ void do_raise_exception(CPUMIPSState *env,
  void mips_cpu_do_interrupt(CPUState *cpu);
  bool mips_cpu_exec_interrupt(CPUState *cpu, int int_req);
 
-void mmu_init(CPUMIPSState *env, const mips_def_t *def);
+void mips_mmu_init(CPUMIPSState *env, const mips_def_t *def);
 
  void update_pagemask(CPUMIPSState *env, target_ulong arg1, int32_t *pagemask);
 
target/mips/cpu.c +1 -1
index 5ed6b3402d3..d8930468b7d 100644
@@ -485,7 +485,7 @@ static void mips_cpu_realizefn(DeviceState *dev, Error **errp)
      env->exception_base = (int32_t)0xBFC00000;
 
  #if defined(CONFIG_TCG) && !defined(CONFIG_USER_ONLY)
-    mmu_init(env, env->cpu_model);
+    mips_mmu_init(env, env->cpu_model);
  #endif
      fpu_init(env, env->cpu_model);
      mvp_init(env);
target/mips/tcg/system/tlb_helper.c +0 -0
index ca4d6b27bc9..1ef2c32cfd4 100644

target/mips/tcg/system/tlb_helper.c static void r4k_mmu_init(CPUMIPSState *env, const mips_def_t *def) · line 472

Quoted 8 lines
target/mips/tcg/system/tlb_helper.c +1 -1
@@ -466,7 +466,7 @@ static void r4k_mmu_init(CPUMIPSState *env, const mips_def_t *def)
      env->tlb->helper_tlbinvf = r4k_helper_tlbinvf;
  }
 
-void mmu_init(CPUMIPSState *env, const mips_def_t *def)
+void mips_mmu_init(CPUMIPSState *env, const mips_def_t *def)
  {
      env->tlb = g_malloc0(sizeof(CPUMIPSTLBContext));
 
#8 lore raw

PATCH 5/8 target/mips: Replace ldtul_p() -> ldn_p(sizeof(target_ulong))

In reply to Philippe Mathieu-Daudé

Replace the single ldtul_p() call by a generic ldn_p() one. No logical change.

---
 target/mips/gdbstub.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
target/mips/gdbstub.c +5 -4
index 169d47416a6..b9fc667373e 100644
@@ -79,12 +79,13 @@ int mips_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n)
 {
     CPUMIPSState *env = cpu_env(cs);
     target_ulong tmp;
+    size_t regsize = sizeof(tmp);
 
-    tmp = ldtul_p(mem_buf);
+    tmp = ldn_p(mem_buf, regsize);
 
     if (n < 32) {
         env->active_tc.gpr[n] = tmp;
-        return sizeof(target_ulong);
+        return regsize;
     }
     if (env->CP0_Config1 & (1 << CP0C1_FP) && n >= 38 && n < 72) {
         switch (n) {
@@ -104,7 +105,7 @@ int mips_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n)
             }
             break;
         }
-        return sizeof(target_ulong);
+        return regsize;
     }
     switch (n) {
     case 32:
@@ -144,5 +145,5 @@ int mips_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n)
         break;
     }
 
-    return sizeof(target_ulong);
+    return regsize;
 }
Signature · 1 line
2.47.1
Reviewed-by on v1 5/8
#9 lore raw

Re: [PATCH-for-10.1 5/8] target/mips: Replace ldtul_p() -> ldn_p(sizeof(target_ulong))

Quoted 38 lines · Philippe Mathieu-Daudé

Replace the single ldtul_p() call by a generic ldn_p() one. No logical change.

---
  target/mips/gdbstub.c | 9 +++++----
  1 file changed, 5 insertions(+), 4 deletions(-)
target/mips/gdbstub.c +4 -3
index 169d47416a6..b9fc667373e 100644
@@ -79,12 +79,13 @@ int mips_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n)
  {
      CPUMIPSState *env = cpu_env(cs);
      target_ulong tmp;
+    size_t regsize = sizeof(tmp);
 
-    tmp = ldtul_p(mem_buf);
+    tmp = ldn_p(mem_buf, regsize);
 
      if (n < 32) {
          env->active_tc.gpr[n] = tmp;
-        return sizeof(target_ulong);
+        return regsize;
      }
      if (env->CP0_Config1 & (1 << CP0C1_FP) && n >= 38 && n < 72) {
          switch (n) {
@@ -104,7 +105,7 @@ int mips_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n)
              }
              break;
          }
-        return sizeof(target_ulong);
+        return regsize;
      }
      switch (n) {
      case 32:

target/mips/gdbstub.c int mips_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n) · line 149

Quoted 6 lines
target/mips/gdbstub.c +1 -1
@@ -144,5 +145,5 @@ int mips_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n)
          break;
      }
 
-    return sizeof(target_ulong);
+    return regsize;
  }
#10 lore raw

PATCH 6/8 target/mips: Introduce mips_cpu_is_64bit() helper

In reply to Philippe Mathieu-Daudé

mips_cpu_is_64bit() returns whether the CPU is a
32-bit or a 64-bit one.

---
 target/mips/cpu-qom.h | 2 ++
 target/mips/cpu.c     | 6 ++++++
 2 files changed, 8 insertions(+)
target/mips/cpu-qom.h +2 -0
index 9acf647420c..52996e7c354 100644
@@ -33,4 +33,6 @@ OBJECT_DECLARE_CPU_TYPE(MIPSCPU, MIPSCPUClass, MIPS_CPU)
 #define MIPS_CPU_TYPE_SUFFIX "-" TYPE_MIPS_CPU
 #define MIPS_CPU_TYPE_NAME(model) model MIPS_CPU_TYPE_SUFFIX
 
+bool mips_cpu_is_64bit(MIPSCPU *cpu);
+
 #endif
target/mips/cpu.c +6 -0
index d8930468b7d..05b3ce42af5 100644
@@ -516,6 +516,12 @@ static void mips_cpu_initfn(Object *obj)
 #endif
 }
 
+bool mips_cpu_is_64bit(MIPSCPU *cpu)
+{
+    return !!object_class_dynamic_cast(OBJECT_CLASS(CPU(cpu)->cc),
+                                       TYPE_MIPS64_CPU);
+}
+
 static char *mips_cpu_type_name(const char *cpu_model)
 {
     return g_strdup_printf(MIPS_CPU_TYPE_NAME("%s"), cpu_model);
Signature · 1 line
2.47.1
Reviewed-by on v1 6/8
#11 lore raw

Re: [PATCH-for-10.1 6/8] target/mips: Introduce mips_cpu_is_64bit() helper

Quoted 27 lines · Philippe Mathieu-Daudé

mips_cpu_is_64bit() returns whether the CPU is a
32-bit or a 64-bit one.

---
  target/mips/cpu-qom.h | 2 ++
  target/mips/cpu.c     | 6 ++++++
  2 files changed, 8 insertions(+)
target/mips/cpu-qom.h +2 -0
index 9acf647420c..52996e7c354 100644
@@ -33,4 +33,6 @@ OBJECT_DECLARE_CPU_TYPE(MIPSCPU, MIPSCPUClass, MIPS_CPU)
  #define MIPS_CPU_TYPE_SUFFIX "-" TYPE_MIPS_CPU
  #define MIPS_CPU_TYPE_NAME(model) model MIPS_CPU_TYPE_SUFFIX
 
+bool mips_cpu_is_64bit(MIPSCPU *cpu);
+
  #endif
target/mips/cpu.c +0 -0
index d8930468b7d..05b3ce42af5 100644
@@ -516,6 +516,12 @@ static void mips_cpu_initfn(Object *obj)
  #endif
  }

target/mips/cpu.c static void mips_cpu_initfn(Object *obj) · line 527

Quoted 10 lines
target/mips/cpu.c +6 -0
 
+bool mips_cpu_is_64bit(MIPSCPU *cpu)
+{
+    return !!object_class_dynamic_cast(OBJECT_CLASS(CPU(cpu)->cc),
+                                       TYPE_MIPS64_CPU);
+}
+
  static char *mips_cpu_type_name(const char *cpu_model)
  {
      return g_strdup_printf(MIPS_CPU_TYPE_NAME("%s"), cpu_model);

On the long term, when having the TargetInfo, this is probably an information we want to be able to retrieve in the same way for every target. That said, we can always replace that later, so this patch is ok for now.

#12 lore raw

Re: [PATCH-for-10.1 6/8] target/mips: Introduce mips_cpu_is_64bit() helper

In reply to Philippe Mathieu-Daudé
Quoted 25 lines · Philippe Mathieu-Daudé

mips_cpu_is_64bit() returns whether the CPU is a
32-bit or a 64-bit one.

---
  target/mips/cpu-qom.h | 2 ++
  target/mips/cpu.c     | 6 ++++++
  2 files changed, 8 insertions(+)
target/mips/cpu-qom.h +2 -0
index 9acf647420c..52996e7c354 100644
@@ -33,4 +33,6 @@ OBJECT_DECLARE_CPU_TYPE(MIPSCPU, MIPSCPUClass, MIPS_CPU)
  #define MIPS_CPU_TYPE_SUFFIX "-" TYPE_MIPS_CPU
  #define MIPS_CPU_TYPE_NAME(model) model MIPS_CPU_TYPE_SUFFIX
 
+bool mips_cpu_is_64bit(MIPSCPU *cpu);
+
  #endif
target/mips/cpu.c +0 -0
index d8930468b7d..05b3ce42af5 100644

target/mips/cpu.c static void mips_cpu_initfn(Object *obj) · line 523

Quoted 8 lines
target/mips/cpu.c +5 -0
@@ -516,6 +516,12 @@ static void mips_cpu_initfn(Object *obj)
  #endif
  }
 
+bool mips_cpu_is_64bit(MIPSCPU *cpu)
+{
+    return !!object_class_dynamic_cast(OBJECT_CLASS(CPU(cpu)->cc),
+                                       TYPE_MIPS64_CPU);
+}

This is very nearly object_dynamic_cast. So why not just

  return object_dynamic_cast(OBJECT(cpu), TYPE_MIPS64_CPU);

?

r~

#13 lore raw

Re: [PATCH-for-10.1 6/8] target/mips: Introduce mips_cpu_is_64bit() helper

Quoted 36 lines · Richard Henderson
Quoted 25 lines · Philippe Mathieu-Daudé

mips_cpu_is_64bit() returns whether the CPU is a
32-bit or a 64-bit one.

---
  target/mips/cpu-qom.h | 2 ++
  target/mips/cpu.c     | 6 ++++++

  2 files changed, 8 insertions(+)
target/mips/cpu-qom.h +2 -0
index 9acf647420c..52996e7c354 100644
@@ -33,4 +33,6 @@ OBJECT_DECLARE_CPU_TYPE(MIPSCPU, MIPSCPUClass, 
 MIPS_CPU)
   #define MIPS_CPU_TYPE_SUFFIX "-" TYPE_MIPS_CPU
   #define MIPS_CPU_TYPE_NAME(model) model MIPS_CPU_TYPE_SUFFIX
+bool mips_cpu_is_64bit(MIPSCPU *cpu);
+
   #endif
target/mips/cpu.c +0 -0
index d8930468b7d..05b3ce42af5 100644

target/mips/cpu.c static void mips_cpu_initfn(Object *obj) · line 522

Quoted 7 lines
target/mips/cpu.c +5 -0
@@ -516,6 +516,12 @@ static void mips_cpu_initfn(Object *obj)
   #endif
   }
+bool mips_cpu_is_64bit(MIPSCPU *cpu)
+{
+    return !!object_class_dynamic_cast(OBJECT_CLASS(CPU(cpu)->cc),
+                                       TYPE_MIPS64_CPU);
+}

This is very nearly object_dynamic_cast.  So why not just

Quoted 3 lines

  return object_dynamic_cast(OBJECT(cpu), TYPE_MIPS64_CPU);

?

Good point!

#14 lore raw

PATCH 7/8 target/mips: Get CPU register size using mips_cpu_is_64bit()

In reply to Philippe Mathieu-Daudé

CPU registers size is static and depends on the type of CPU.

---
 target/mips/gdbstub.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
target/mips/gdbstub.c +6 -1
index b9fc667373e..84fd3de4137 100644
@@ -75,11 +75,16 @@ int mips_cpu_gdb_read_register(CPUState *cs, GByteArray *mem_buf, int n)
     return 0;
 }
 
+static size_t mips_regsize(MIPSCPU *cpu)
+{
+    return mips_cpu_is_64bit(cpu) ? sizeof(uint64_t) : sizeof(uint32_t);
+}
+
 int mips_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n)
 {
     CPUMIPSState *env = cpu_env(cs);
     target_ulong tmp;
-    size_t regsize = sizeof(tmp);
+    size_t regsize = mips_regsize(MIPS_CPU(cs));
 
     tmp = ldn_p(mem_buf, regsize);
 
Signature · 1 line
2.47.1
Reviewed-by on v1 7/8
#15 lore raw

Re: [PATCH-for-10.1 7/8] target/mips: Get CPU register size using mips_cpu_is_64bit()

Quoted 19 lines · Philippe Mathieu-Daudé

CPU registers size is static and depends on the type of CPU.

---
  target/mips/gdbstub.c | 7 ++++++-
  1 file changed, 6 insertions(+), 1 deletion(-)
target/mips/gdbstub.c +4 -0
index b9fc667373e..84fd3de4137 100644
@@ -75,11 +75,16 @@ int mips_cpu_gdb_read_register(CPUState *cs, GByteArray *mem_buf, int n)
      return 0;
  }
 
+static size_t mips_regsize(MIPSCPU *cpu)
+{
+    return mips_cpu_is_64bit(cpu) ? sizeof(uint64_t) : sizeof(uint32_t);
+}

target/mips/gdbstub.c int mips_cpu_gdb_read_register(CPUState *cs, GByteArray *mem_buf, int n) · line 90

Quoted 10 lines
target/mips/gdbstub.c +2 -1
+
  int mips_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n)
  {
      CPUMIPSState *env = cpu_env(cs);
      target_ulong tmp;
-    size_t regsize = sizeof(tmp);
+    size_t regsize = mips_regsize(MIPS_CPU(cs));
 
      tmp = ldn_p(mem_buf, regsize);
 
#16 lore raw

PATCH 8/8 target/mips: Introduce mips_env_64bit_enabled() helper

In reply to Philippe Mathieu-Daudé

mips_env_64bit_enabled() returns whether the CPU is running in 32-bit or 64-bit (behavior which might change at runtime).

---
 target/mips/internal.h | 5 +++++
 1 file changed, 5 insertions(+)
target/mips/internal.h +5 -0
index 28eb28936ba..8107a59b908 100644
@@ -225,6 +225,11 @@ static inline void mips_env_set_pc(CPUMIPSState *env, target_ulong value)
     }
 }
 
+static inline bool mips_env_64bit_enabled(CPUMIPSState *env)
+{
+    return env->hflags & MIPS_HFLAG_64;
+}
+
 static inline bool mips_env_is_bigendian(CPUMIPSState *env)
 {
     return extract32(env->CP0_Config0, CP0C0_BE, 1);
Signature · 1 line
2.47.1
Reviewed-by on v1 8/8
#17 lore raw

Re: [PATCH-for-10.1 8/8] target/mips: Introduce mips_env_64bit_enabled() helper

Quoted 14 lines · Philippe Mathieu-Daudé

mips_env_64bit_enabled() returns whether the CPU is running in 32-bit or 64-bit (behavior which might change at runtime).

---
  target/mips/internal.h | 5 +++++
  1 file changed, 5 insertions(+)
target/mips/internal.h +0 -0
index 28eb28936ba..8107a59b908 100644
@@ -225,6 +225,11 @@ static inline void mips_env_set_pc(CPUMIPSState *env, target_ulong value)
      }

target/mips/internal.h static inline void mips_env_set_pc(CPUMIPSState *env, target_ulong value) · line 235

Quoted 10 lines
target/mips/internal.h +5 -0
  }
 
+static inline bool mips_env_64bit_enabled(CPUMIPSState *env)
+{
+    return env->hflags & MIPS_HFLAG_64;
+}
+
  static inline bool mips_env_is_bigendian(CPUMIPSState *env)
  {
      return extract32(env->CP0_Config0, CP0C0_BE, 1);
#18 lore raw

Re: [PATCH-for-10.1 8/8] target/mips: Introduce mips_env_64bit_enabled() helper

In reply to Philippe Mathieu-Daudé
Quoted 14 lines · Philippe Mathieu-Daudé

mips_env_64bit_enabled() returns whether the CPU is running in 32-bit or 64-bit (behavior which might change at runtime).

---
  target/mips/internal.h | 5 +++++
  1 file changed, 5 insertions(+)
target/mips/internal.h +0 -0
index 28eb28936ba..8107a59b908 100644
@@ -225,6 +225,11 @@ static inline void mips_env_set_pc(CPUMIPSState *env, target_ulong value)
      }

target/mips/internal.h static inline void mips_env_set_pc(CPUMIPSState *env, target_ulong value) · line 235

Quoted 10 lines
target/mips/internal.h +5 -0
  }
 
+static inline bool mips_env_64bit_enabled(CPUMIPSState *env)
+{
+    return env->hflags & MIPS_HFLAG_64;
+}
+
  static inline bool mips_env_is_bigendian(CPUMIPSState *env)
  {
      return extract32(env->CP0_Config0, CP0C0_BE, 1);

Maybe delay this until you need it?

r~

Reviewed-by Anton Johansson on the v1 series
#19 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 17 lines · Philippe Mathieu-Daudé

- Remove the TARGET_MIPS64 use in 'cpu-qom.h' to make it target agnostic
- Introduce mips_cpu_is_64bit() as a runtime check for compile time TARGET_MIPS64 definition
- Replace the ldtul_p() gdbstub call by ldn_p()
- Rename few symbols to avoid future linkage clash

Philippe Mathieu-Daudé (8):
cpus: Open code OBJECT_DECLARE_TYPE() in OBJECT_DECLARE_CPU_TYPE() target/mips: Declare CPU QOM types using DEFINE_TYPES() macro target/mips: Make MIPS_CPU common to new MIPS32_CPU / MIPS64_CPU types target/mips: Prefix MMU API with 'mips_'
target/mips: Replace ldtul_p() -> ldn_p(sizeof(target_ulong)) target/mips: Introduce mips_cpu_is_64bit() helper target/mips: Get CPU register size using mips_cpu_is_64bit() target/mips: Introduce mips_env_64bit_enabled() helper

Quoted 12 lines
 include/hw/core/cpu.h               |  7 ++++-
 target/mips/cpu-qom.h               | 14 +++++-----
 target/mips/internal.h              |  5 ++++
 target/mips/tcg/tcg-internal.h      |  2 +-
 target/mips/cpu.c                   | 42 ++++++++++++++++++++---------
 target/mips/gdbstub.c               | 14 +++++++---
 target/mips/tcg/system/tlb_helper.c |  2 +-
 7 files changed, 61 insertions(+), 25 deletions(-)
Signature · 1 line
2.47.1