Skip to contentSkip to conversation

gdbstub: Remove target_long_bits() from GDB register reads

PATCH In review Philippe Mathieu-Daudé 4 participants 17 messages Oct 7 – Oct 9 RISC-V

lore
Share & tools
lore
#0 lore raw

cover gdbstub: Remove target_long_bits() from GDB register reads

Prefer getting target register word size at runtime (via CPU state) instead of build time.

This allow to remove gdb_get_regl() and various target_long_bits() calls, and to eliminate the TARGET_LONG_SIZE definition.

Philippe Mathieu-Daudé (10):
target/sparc/gdbstub: Replace gdb_get_regl() by gdb_get_reg64() target/mips/gdbstub: Reduce to a single gdb_get_regl() call target/mips/gdbstub: Replace target_long_bits() by mips_env_is_64bit() target/ppc: Factor ppc_env_is_64bit() out target/ppc/gdbstub: Replace target_long_bits() by ppc_env_is_64bit() target/ppc/gdbstub: Replace gdb_get_regl() by ppc_env_is_64bit() target/riscv: Factor riscv_gdb_get_reg_mxl() out target/riscv: Replace gdb_get_regl() by riscv_gdb_get_reg_mxl()
gdbstub: Remove unused gdb_get_regl()
include: Remove TARGET_LONG_SIZE definition

 include/exec/target_long.h  |  8 ++--
 include/gdbstub/helpers.h   | 18 --------
 include/user/tswap-target.h |  2 +-
 target/mips/internal.h      |  5 +++
 target/ppc/internal.h       | 11 +++++
 target/mips/gdbstub.c       | 85 ++++++++++++++++++++++---------------
 target/ppc/cpu_init.c       |  2 +-
 target/ppc/gdbstub.c        | 38 +++++++++++------
 target/riscv/gdbstub.c      | 35 +++++++++------
 target/sparc/gdbstub.c      | 13 +++---
 10 files changed, 125 insertions(+), 92 deletions(-)
Signature · 1 line
2.55.0
#1 lore raw

PATCH 1/10 target/sparc/gdbstub: Replace gdb_get_regl() by gdb_get_reg64()

The gdb_get_regl() calls in sparc_cp0_gdb_read_register() are guarded within:

  #if defined(TARGET_ABI32) || !defined(TARGET_SPARC64)
  /* 32-bit */
  #else
  /* 64-bit */
  #endif

Since the #else branch is only compiled for 64-bit targets where gdb_get_regl() forwards to gdb_get_reg64(), we can call the latter directly, dropping the use of the deprecated helper. Assert the invariant at build time to be safe.

---
 target/sparc/gdbstub.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
target/sparc/gdbstub.c +7 -6
index 2874ce1b350..d3ccf533839 100644
@@ -106,22 +106,23 @@ static int sparc_cp0_gdb_read_register(CPUState *cs, GByteArray *mem_buf, int n)
         return gdb_get_rega(mem_buf, 0); /* csr */
     }
 #else
+    QEMU_BUILD_BUG_ON(TARGET_LONG_BITS != 64);
     switch (n) {
     case 0:
-        return gdb_get_regl(mem_buf, env->pc);
+        return gdb_get_reg64(mem_buf, env->pc);
     case 1:
-        return gdb_get_regl(mem_buf, env->npc);
+        return gdb_get_reg64(mem_buf, env->npc);
     case 2:
-        return gdb_get_regl(mem_buf, (cpu_get_ccr(env) << 32) |
+        return gdb_get_reg64(mem_buf, (cpu_get_ccr(env) << 32) |
                                      ((env->asi & 0xff) << 24) |
                                      ((env->pstate & 0xfff) << 8) |
                                      cpu_get_cwp64(env));
     case 3:
-        return gdb_get_regl(mem_buf, cpu_get_fsr(env));
+        return gdb_get_reg64(mem_buf, cpu_get_fsr(env));
     case 4:
-        return gdb_get_regl(mem_buf, env->fprs);
+        return gdb_get_reg64(mem_buf, env->fprs);
     case 5:
-        return gdb_get_regl(mem_buf, env->y);
+        return gdb_get_reg64(mem_buf, env->y);
     }
 #endif
     return 0;
Signature · 1 line
2.55.0
#2 lore raw

PATCH 2/10 target/mips/gdbstub: Reduce to a single gdb_get_regl() call

In reply to Philippe Mathieu-Daudé

Store the register value in a local @val in each switch case, then append it once at the end of mips_cpu_gdb_read_register().

No functional change.

---
 target/mips/gdbstub.c | 76 ++++++++++++++++++++++++-------------------
 1 file changed, 43 insertions(+), 33 deletions(-)
target/mips/gdbstub.c +43 -33
index 8e0b08a096c..7598aab6158 100644
@@ -27,6 +27,7 @@
 int mips_cpu_gdb_read_register(CPUState *cs, GByteArray *mem_buf, int n)
 {
     CPUMIPSState *env = cpu_env(cs);
+    uint64_t val;
 
     if (n < 32) {
         return gdb_get_regl(mem_buf, env->active_tc.gpr[n]);
@@ -34,46 +35,55 @@ int mips_cpu_gdb_read_register(CPUState *cs, GByteArray *mem_buf, int n)
     if (env->CP0_Config1 & (1 << CP0C1_FP) && n >= 38 && n < 72) {
         switch (n) {
         case 70:
-            return gdb_get_regl(mem_buf, (int32_t)env->active_fpu.fcr31);
+            val = (int32_t)env->active_fpu.fcr31;
+            break;
         case 71:
-            return gdb_get_regl(mem_buf, (int32_t)env->active_fpu.fcr0);
+            val = (int32_t)env->active_fpu.fcr0;
+            break;
         default:
             if (env->CP0_Status & (1 << CP0St_FR)) {
-                return gdb_get_regl(mem_buf,
-                    env->active_fpu.fpr[n - 38].d);
+                val = env->active_fpu.fpr[n - 38].d;
             } else {
-                return gdb_get_regl(mem_buf,
-                    env->active_fpu.fpr[n - 38].w[FP_ENDIAN_IDX]);
+                val = env->active_fpu.fpr[n - 38].w[FP_ENDIAN_IDX];
             }
+            break;
+        }
+    } else {
+        switch (n) {
+        case 32:
+            val = (int32_t)env->CP0_Status;
+            break;
+        case 33:
+            val = env->active_tc.LO[0];
+            break;
+        case 34:
+            val = env->active_tc.HI[0];
+            break;
+        case 35:
+            val = env->CP0_BadVAddr;
+            break;
+        case 36:
+            val = (int32_t)env->CP0_Cause;
+            break;
+        case 37:
+            val = env->active_tc.PC | !!(env->hflags & MIPS_HFLAG_M16);
+            break;
+        case 72:
+            val = 0; /* fp */
+            break;
+        case 89:
+            val = (int32_t)env->CP0_PRid;
+            break;
+        default:
+            if (n > 89) {
+                return 0;
+            }
+            /* 16 embedded regs.  */
+            val = 0;
+            break;
         }
     }
-    switch (n) {
-    case 32:
-        return gdb_get_regl(mem_buf, (int32_t)env->CP0_Status);
-    case 33:
-        return gdb_get_regl(mem_buf, env->active_tc.LO[0]);
-    case 34:
-        return gdb_get_regl(mem_buf, env->active_tc.HI[0]);
-    case 35:
-        return gdb_get_regl(mem_buf, env->CP0_BadVAddr);
-    case 36:
-        return gdb_get_regl(mem_buf, (int32_t)env->CP0_Cause);
-    case 37:
-        return gdb_get_regl(mem_buf, env->active_tc.PC |
-                                     !!(env->hflags & MIPS_HFLAG_M16));
-    case 72:
-        return gdb_get_regl(mem_buf, 0); /* fp */
-    case 89:
-        return gdb_get_regl(mem_buf, (int32_t)env->CP0_PRid);
-    default:
-        if (n > 89) {
-            return 0;
-        }
-        /* 16 embedded regs.  */
-        return gdb_get_regl(mem_buf, 0);
-    }
-
-    return 0;
+    return gdb_get_regl(mem_buf, val);
 }
 
 int mips_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n)
Signature · 1 line
2.55.0
#3 lore raw

PATCH 3/10 target/mips/gdbstub: Replace target_long_bits() by mips_env_is_64bit()

In reply to Philippe Mathieu-Daudé

Add a mips_env_is_64bit() helper and inline gdb_get_regl() as mips_gdb_get_regl(), so that the GDB register size follows the emulated CPU model rather than the target build's TARGET_LONG_BITS.

---
 target/mips/internal.h |  5 +++++
 target/mips/gdbstub.c  | 11 +++++++++--
 2 files changed, 14 insertions(+), 2 deletions(-)
target/mips/internal.h +5 -0
index 7dba22300b1..0c9b1335ebb 100644
@@ -228,6 +228,11 @@ static inline void mips_env_set_pc(CPUMIPSState *env, target_ulong value)
     }
 }
 
+static inline bool mips_env_is_64bit(const CPUMIPSState *env)
+{
+    return env->cpu_model->insn_flags & CPU_MIPS64;
+}
+
 static inline bool mips_env_is_bigendian(CPUMIPSState *env)
 {
     return extract32(env->CP0_Config0, CP0C0_BE, 1);
target/mips/gdbstub.c +9 -2
index 7598aab6158..78d3e9a5b9b 100644
@@ -24,13 +24,20 @@
 #include "gdbstub/helpers.h"
 #include "fpu_helper.h"
 
+static int mips_gdb_get_regl(const CPUMIPSState *env,
+                             GByteArray *buf, uint64_t val)
+{
+    return mips_env_is_64bit(env) ? gdb_get_reg64(buf, val)
+                                  : gdb_get_reg32(buf, val);
+}
+
 int mips_cpu_gdb_read_register(CPUState *cs, GByteArray *mem_buf, int n)
 {
     CPUMIPSState *env = cpu_env(cs);
     uint64_t val;
 
     if (n < 32) {
-        return gdb_get_regl(mem_buf, env->active_tc.gpr[n]);
+        return mips_gdb_get_regl(env, mem_buf, env->active_tc.gpr[n]);
     }
     if (env->CP0_Config1 & (1 << CP0C1_FP) && n >= 38 && n < 72) {
         switch (n) {
@@ -83,7 +90,7 @@ int mips_cpu_gdb_read_register(CPUState *cs, GByteArray *mem_buf, int n)
             break;
         }
     }
-    return gdb_get_regl(mem_buf, val);
+    return mips_gdb_get_regl(env, mem_buf, val);
 }
 
 int mips_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n)
Signature · 1 line
2.55.0
#4 lore raw

PATCH 4/10 target/ppc: Factor ppc_env_is_64bit() out

In reply to Philippe Mathieu-Daudé

Replace the open-coded PPC_64B test in register_BookE206_sprs() by a ppc_env_is_64bit() helper for re-use in the next commit.

---
 target/ppc/internal.h | 11 +++++++++++
 target/ppc/cpu_init.c |  2 +-
 2 files changed, 12 insertions(+), 1 deletion(-)
target/ppc/internal.h +11 -0
index a20a0f9f7e0..11476692616 100644
@@ -23,6 +23,17 @@
 #include "hw/core/registerfields.h"
 #include "exec/page-protection.h"
 
+/**
+ * ppc_env_is_64bit:
+ * @env: the cpu context
+ *
+ * Return whether the CPU is a 64-bit implementation.
+ */
+static inline bool ppc_env_is_64bit(const CPUPPCState *env)
+{
+    return env->insns_flags & PPC_64B;
+}
+
 static inline bool ppc_env_is_little_endian(const CPUPPCState *env)
 {
     return FIELD_EX64(env->msr, MSR, LE);
target/ppc/cpu_init.c +1 -1
index b711f9c0a85..a43ef47acd2 100644
@@ -869,7 +869,7 @@ static void register_BookE206_sprs(CPUPPCState *env, uint32_t mas_mask,
             spr_register(env, mas_sprn[i], mas_names[i],
                          SPR_NOACCESS, SPR_NOACCESS,
                          &spr_read_generic,
-                         (i == 2 && (env->insns_flags & PPC_64B))
+                         (i == 2 && ppc_env_is_64bit(env))
                          ? &spr_write_generic : &spr_write_generic32,
                          0x00000000);
         }
Signature · 1 line
2.55.0
Reviewed-by on v1 4/10
#5 lore raw

Re: [PATCH 04/10] target/ppc: Factor ppc_env_is_64bit() out

Thanks,

Glenn

Quoted 44 lines · Philippe Mathieu-Daudé

Replace the open-coded PPC_64B test in register_BookE206_sprs() by a ppc_env_is_64bit() helper for re-use in the next commit.

---
 target/ppc/internal.h | 11 +++++++++++
 target/ppc/cpu_init.c |  2 +-
 2 files changed, 12 insertions(+), 1 deletion(-)
target/ppc/internal.h +11 -0
index a20a0f9f7e0..11476692616 100644
@@ -23,6 +23,17 @@
 #include "hw/core/registerfields.h"
 #include "exec/page-protection.h"
 
+/**
+ * ppc_env_is_64bit:
+ * @env: the cpu context
+ *
+ * Return whether the CPU is a 64-bit implementation.
+ */
+static inline bool ppc_env_is_64bit(const CPUPPCState *env)
+{
+    return env->insns_flags & PPC_64B;
+}
+
 static inline bool ppc_env_is_little_endian(const CPUPPCState *env)
 {
     return FIELD_EX64(env->msr, MSR, LE);
target/ppc/cpu_init.c +1 -1
index b711f9c0a85..a43ef47acd2 100644
@@ -869,7 +869,7 @@ static void register_BookE206_sprs(CPUPPCState *env, uint32_t mas_mask,
             spr_register(env, mas_sprn[i], mas_names[i],
                          SPR_NOACCESS, SPR_NOACCESS,
                          &spr_read_generic,
-                         (i == 2 && (env->insns_flags & PPC_64B))
+                         (i == 2 && ppc_env_is_64bit(env))
                          ? &spr_write_generic : &spr_write_generic32,
                          0x00000000);
         }
#6 lore raw

PATCH 5/10 target/ppc/gdbstub: Replace target_long_bits() by ppc_env_is_64bit()

In reply to Philippe Mathieu-Daudé

Add a gdb_get_reg_bits() helper deriving the register width from ppc_env_is_64bit(), and use it instead of target_long_bits() and TARGET_LONG_SIZE.

---
 target/ppc/gdbstub.c | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)
target/ppc/gdbstub.c +12 -7
index 4d622c5cad5..ee02be63ce8 100644
@@ -23,12 +23,17 @@
 #include "gdbstub/helpers.h"
 #include "internal.h"
 
-static unsigned ppc_gdb_register_len(int n)
+static int gdb_get_reg_bits(const CPUPPCState *env)
+{
+    return ppc_env_is_64bit(env) ? 64 : 32;
+}
+
+static unsigned ppc_gdb_register_len(const CPUPPCState *env, int n)
 {
     switch (n) {
     case 0 ... 31:
         /* gprs */
-        return target_long_bits() / 8;
+        return gdb_get_reg_bits(env) / 8;
     case 66:
         /* cr */
     case 69:
@@ -42,7 +47,7 @@ static unsigned ppc_gdb_register_len(int n)
         /* lr */
     case 68:
         /* ctr */
-        return target_long_bits() / 8;
+        return gdb_get_reg_bits(env) / 8;
     default:
         return 0;
     }
@@ -85,7 +90,7 @@ int ppc_cpu_gdb_read_register(CPUState *cs, GByteArray *buf, int n)
 {
     CPUPPCState *env = cpu_env(cs);
     uint8_t *mem_buf;
-    unsigned r = ppc_gdb_register_len(n);
+    unsigned r = ppc_gdb_register_len(env, n);
 
     if (!r) {
         return r;
@@ -127,7 +132,7 @@ int ppc_cpu_gdb_read_register(CPUState *cs, GByteArray *buf, int n)
 int ppc_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n)
 {
     CPUPPCState *env = cpu_env(cs);
-    unsigned r = ppc_gdb_register_len(n);
+    unsigned r = ppc_gdb_register_len(env, n);
 
     if (!r) {
         return r;
@@ -251,7 +256,7 @@ static int gdb_get_spr_reg(CPUState *cs, GByteArray *buf, int n)
         return 0;
     }
 
-    len = TARGET_LONG_SIZE;
+    len = gdb_get_reg_bits(env) / 8;
 
     /* Handle those SPRs that are not part of the env->spr[] array */
     target_ulong val;
@@ -294,7 +299,7 @@ static int gdb_set_spr_reg(CPUState *cs, uint8_t *mem_buf, int n)
         return 0;
     }
 
-    len = TARGET_LONG_SIZE;
+    len = gdb_get_reg_bits(env) / 8;
     ppc_maybe_bswap_register(env, mem_buf, len);
 
     /* Handle those SPRs that are not part of the env->spr[] array */
Signature · 1 line
2.55.0
Reviewed-by on v1 5/10
#7 lore raw

Re: [PATCH 05/10] target/ppc/gdbstub: Replace target_long_bits() by ppc_env_is_64bit()

Thanks,

Glenn

Quoted 78 lines · Philippe Mathieu-Daudé

Add a gdb_get_reg_bits() helper deriving the register width from ppc_env_is_64bit(), and use it instead of target_long_bits() and TARGET_LONG_SIZE.

---
 target/ppc/gdbstub.c | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)
target/ppc/gdbstub.c +12 -7
index 4d622c5cad5..ee02be63ce8 100644
@@ -23,12 +23,17 @@
 #include "gdbstub/helpers.h"
 #include "internal.h"
 
-static unsigned ppc_gdb_register_len(int n)
+static int gdb_get_reg_bits(const CPUPPCState *env)
+{
+    return ppc_env_is_64bit(env) ? 64 : 32;
+}
+
+static unsigned ppc_gdb_register_len(const CPUPPCState *env, int n)
 {
     switch (n) {
     case 0 ... 31:
         /* gprs */
-        return target_long_bits() / 8;
+        return gdb_get_reg_bits(env) / 8;
     case 66:
         /* cr */
     case 69:
@@ -42,7 +47,7 @@ static unsigned ppc_gdb_register_len(int n)
         /* lr */
     case 68:
         /* ctr */
-        return target_long_bits() / 8;
+        return gdb_get_reg_bits(env) / 8;
     default:
         return 0;
     }
@@ -85,7 +90,7 @@ int ppc_cpu_gdb_read_register(CPUState *cs, GByteArray *buf, int n)
 {
     CPUPPCState *env = cpu_env(cs);
     uint8_t *mem_buf;
-    unsigned r = ppc_gdb_register_len(n);
+    unsigned r = ppc_gdb_register_len(env, n);
 
     if (!r) {
         return r;
@@ -127,7 +132,7 @@ int ppc_cpu_gdb_read_register(CPUState *cs, GByteArray *buf, int n)
 int ppc_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n)
 {
     CPUPPCState *env = cpu_env(cs);
-    unsigned r = ppc_gdb_register_len(n);
+    unsigned r = ppc_gdb_register_len(env, n);
 
     if (!r) {
         return r;
@@ -251,7 +256,7 @@ static int gdb_get_spr_reg(CPUState *cs, GByteArray *buf, int n)
         return 0;
     }
 
-    len = TARGET_LONG_SIZE;
+    len = gdb_get_reg_bits(env) / 8;
 
     /* Handle those SPRs that are not part of the env->spr[] array */
     target_ulong val;
@@ -294,7 +299,7 @@ static int gdb_set_spr_reg(CPUState *cs, uint8_t *mem_buf, int n)
         return 0;
     }
 
-    len = TARGET_LONG_SIZE;
+    len = gdb_get_reg_bits(env) / 8;
     ppc_maybe_bswap_register(env, mem_buf, len);
 
     /* Handle those SPRs that are not part of the env->spr[] array */
#8 lore raw

PATCH 6/10 target/ppc/gdbstub: Replace gdb_get_regl() by ppc_env_is_64bit()

In reply to Philippe Mathieu-Daudé

Inline the deprecated gdb_get_regl() as ppc_gdb_get_regl(), replacing the target_long_bits() check by a ppc_env_is_64bit() one.

---
 target/ppc/gdbstub.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)
target/ppc/gdbstub.c +13 -6
index ee02be63ce8..0071c2b3bab 100644
@@ -53,6 +53,13 @@ static unsigned ppc_gdb_register_len(const CPUPPCState *env, int n)
     }
 }
 
+static inline int ppc_gdb_get_regl(const CPUPPCState *env,
+                                   GByteArray *buf, uint64_t val)
+{
+    return ppc_env_is_64bit(env) ? gdb_get_reg64(buf, val)
+                                 : gdb_get_reg32(buf, val);
+}
+
 /*
  * We need to present the registers to gdb in the "current" memory
  * ordering.  For user-only mode we get this for free;
@@ -98,14 +105,14 @@ int ppc_cpu_gdb_read_register(CPUState *cs, GByteArray *buf, int n)
 
     if (n < 32) {
         /* gprs */
-        gdb_get_regl(buf, env->gpr[n]);
+        ppc_gdb_get_regl(env, buf, env->gpr[n]);
     } else {
         switch (n) {
         case 64:
-            gdb_get_regl(buf, env->nip);
+            ppc_gdb_get_regl(env, buf, env->nip);
             break;
         case 65:
-            gdb_get_regl(buf, env->msr);
+            ppc_gdb_get_regl(env, buf, env->msr);
             break;
         case 66:
             {
@@ -114,10 +121,10 @@ int ppc_cpu_gdb_read_register(CPUState *cs, GByteArray *buf, int n)
                 break;
             }
         case 67:
-            gdb_get_regl(buf, env->lr);
+            ppc_gdb_get_regl(env, buf, env->lr);
             break;
         case 68:
-            gdb_get_regl(buf, env->ctr);
+            ppc_gdb_get_regl(env, buf, env->ctr);
             break;
         case 69:
             gdb_get_reg32(buf, cpu_read_xer(env));
@@ -281,7 +288,7 @@ static int gdb_get_spr_reg(CPUState *cs, GByteArray *buf, int n)
     default:
         val = env->spr[reg];
     }
-    gdb_get_regl(buf, val);
+    ppc_gdb_get_regl(env, buf, val);
 
     ppc_maybe_bswap_register(env, gdb_get_reg_ptr(buf, len), len);
     return len;
Signature · 1 line
2.55.0
Reviewed-by on v1 6/10
#9 lore raw

Re: [PATCH 06/10] target/ppc/gdbstub: Replace gdb_get_regl() by ppc_env_is_64bit()

Thanks,

Glenn

Quoted 67 lines · Philippe Mathieu-Daudé

Inline the deprecated gdb_get_regl() as ppc_gdb_get_regl(), replacing the target_long_bits() check by a ppc_env_is_64bit() one.

---
 target/ppc/gdbstub.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)
target/ppc/gdbstub.c +13 -6
index ee02be63ce8..0071c2b3bab 100644
@@ -53,6 +53,13 @@ static unsigned ppc_gdb_register_len(const CPUPPCState *env, int n)
     }
 }
 
+static inline int ppc_gdb_get_regl(const CPUPPCState *env,
+                                   GByteArray *buf, uint64_t val)
+{
+    return ppc_env_is_64bit(env) ? gdb_get_reg64(buf, val)
+                                 : gdb_get_reg32(buf, val);
+}
+
 /*
  * We need to present the registers to gdb in the "current" memory
  * ordering.  For user-only mode we get this for free;
@@ -98,14 +105,14 @@ int ppc_cpu_gdb_read_register(CPUState *cs, GByteArray *buf, int n)
 
     if (n < 32) {
         /* gprs */
-        gdb_get_regl(buf, env->gpr[n]);
+        ppc_gdb_get_regl(env, buf, env->gpr[n]);
     } else {
         switch (n) {
         case 64:
-            gdb_get_regl(buf, env->nip);
+            ppc_gdb_get_regl(env, buf, env->nip);
             break;
         case 65:
-            gdb_get_regl(buf, env->msr);
+            ppc_gdb_get_regl(env, buf, env->msr);
             break;
         case 66:
             {
@@ -114,10 +121,10 @@ int ppc_cpu_gdb_read_register(CPUState *cs, GByteArray *buf, int n)
                 break;
             }
         case 67:
-            gdb_get_regl(buf, env->lr);
+            ppc_gdb_get_regl(env, buf, env->lr);
             break;
         case 68:
-            gdb_get_regl(buf, env->ctr);
+            ppc_gdb_get_regl(env, buf, env->ctr);
             break;
         case 69:
             gdb_get_reg32(buf, cpu_read_xer(env));
@@ -281,7 +288,7 @@ static int gdb_get_spr_reg(CPUState *cs, GByteArray *buf, int n)
     default:
         val = env->spr[reg];
     }
-    gdb_get_regl(buf, val);
+    ppc_gdb_get_regl(env, buf, val);
 
     ppc_maybe_bswap_register(env, gdb_get_reg_ptr(buf, len), len);
     return len;
#10 lore raw

PATCH 7/10 target/riscv: Factor riscv_gdb_get_reg_mxl() out

In reply to Philippe Mathieu-Daudé

Move the MXL-based selection of the register width out of riscv_cpu_gdb_read_register() into a helper, so it can be reused by the following patch.

---
 target/riscv/gdbstub.c | 29 ++++++++++++++++++-----------
 1 file changed, 18 insertions(+), 11 deletions(-)
target/riscv/gdbstub.c +18 -11
index 9abbf5bcdf0..b3c11db5397 100644
@@ -54,9 +54,25 @@ static uint64_t ldn(CPURISCVState *env, uint8_t *mem_buf, size_t regsz)
     return (mo_endian_env(env) == MO_LE ? ldn_le_p : ldn_be_p)(mem_buf, regsz);
 }
 
-int riscv_cpu_gdb_read_register(CPUState *cs, GByteArray *mem_buf, int n)
+static int riscv_gdb_get_reg_mxl(CPUState *cs,
+                                 GByteArray *buf, uint64_t val)
 {
     RISCVCPUClass *mcc = RISCV_CPU_GET_CLASS(cs);
+
+    switch (mcc->def->misa_mxl_max) {
+    case MXL_RV32:
+        return gdb_get_reg32(buf, val);
+    case MXL_RV64:
+    case MXL_RV128:
+        return gdb_get_reg64(buf, val);
+    default:
+        g_assert_not_reached();
+    }
+    return 0;
+}
+
+int riscv_cpu_gdb_read_register(CPUState *cs, GByteArray *mem_buf, int n)
+{
     RISCVCPU *cpu = RISCV_CPU(cs);
     CPURISCVState *env = &cpu->env;
     uint64_t tmp;
@@ -69,16 +85,7 @@ int riscv_cpu_gdb_read_register(CPUState *cs, GByteArray *mem_buf, int n)
         return 0;
     }
 
-    switch (mcc->def->misa_mxl_max) {
-    case MXL_RV32:
-        return gdb_get_reg32(mem_buf, tmp);
-    case MXL_RV64:
-    case MXL_RV128:
-        return gdb_get_reg64(mem_buf, tmp);
-    default:
-        g_assert_not_reached();
-    }
-    return 0;
+    return riscv_gdb_get_reg_mxl(cs, mem_buf, tmp);
 }
 
 int riscv_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n)
Signature · 1 line
2.55.0
Reviewed-by on v1 7/10
#11 lore raw

Re: [PATCH 07/10] target/riscv: Factor riscv_gdb_get_reg_mxl() out

Quoted 6 lines · Philippe Mathieu-Daudé

Move the MXL-based selection of the register width out of riscv_cpu_gdb_read_register() into a helper, so it can be reused by the following patch.

---

Quoted 52 lines
  target/riscv/gdbstub.c | 29 ++++++++++++++++++-----------
  1 file changed, 18 insertions(+), 11 deletions(-)
target/riscv/gdbstub.c +18 -11
index 9abbf5bcdf0..b3c11db5397 100644
@@ -54,9 +54,25 @@ static uint64_t ldn(CPURISCVState *env, uint8_t *mem_buf, size_t regsz)
      return (mo_endian_env(env) == MO_LE ? ldn_le_p : ldn_be_p)(mem_buf, regsz);
  }
 
-int riscv_cpu_gdb_read_register(CPUState *cs, GByteArray *mem_buf, int n)
+static int riscv_gdb_get_reg_mxl(CPUState *cs,
+                                 GByteArray *buf, uint64_t val)
  {
      RISCVCPUClass *mcc = RISCV_CPU_GET_CLASS(cs);
+
+    switch (mcc->def->misa_mxl_max) {
+    case MXL_RV32:
+        return gdb_get_reg32(buf, val);
+    case MXL_RV64:
+    case MXL_RV128:
+        return gdb_get_reg64(buf, val);
+    default:
+        g_assert_not_reached();
+    }
+    return 0;
+}
+
+int riscv_cpu_gdb_read_register(CPUState *cs, GByteArray *mem_buf, int n)
+{
      RISCVCPU *cpu = RISCV_CPU(cs);
      CPURISCVState *env = &cpu->env;
      uint64_t tmp;
@@ -69,16 +85,7 @@ int riscv_cpu_gdb_read_register(CPUState *cs, GByteArray *mem_buf, int n)
          return 0;
      }
 
-    switch (mcc->def->misa_mxl_max) {
-    case MXL_RV32:
-        return gdb_get_reg32(mem_buf, tmp);
-    case MXL_RV64:
-    case MXL_RV128:
-        return gdb_get_reg64(mem_buf, tmp);
-    default:
-        g_assert_not_reached();
-    }
-    return 0;
+    return riscv_gdb_get_reg_mxl(cs, mem_buf, tmp);
  }
 
  int riscv_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n)
#12 lore raw

PATCH 8/10 target/riscv: Replace gdb_get_regl() by riscv_gdb_get_reg_mxl()

In reply to Philippe Mathieu-Daudé

riscv_gdb_get_reg_mxl() picks the register width from the CPU's misa_mxl_max instead of target_long_bits(); use it in place of the deprecated gdb_get_regl().

---
 target/riscv/gdbstub.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
target/riscv/gdbstub.c +3 -3
index b3c11db5397..50e834e367c 100644
@@ -183,7 +183,7 @@ static int riscv_gdb_get_csr(CPUState *cs, GByteArray *buf, int n)
 
         result = riscv_csrrw_debug(env, n, &val, 0, 0);
         if (result == RISCV_EXCP_NONE) {
-            return gdb_get_regl(buf, val);
+            return riscv_gdb_get_reg_mxl(cs, buf, val);
         }
     }
     return 0;
@@ -211,7 +211,7 @@ static int riscv_gdb_get_virtual(CPUState *cs, GByteArray *buf, int n)
 {
     if (n == 0) {
 #ifdef CONFIG_USER_ONLY
-        return gdb_get_regl(buf, 0);
+        return riscv_gdb_get_reg_mxl(cs, buf, 0);
 #else
         RISCVCPU *cpu = RISCV_CPU(cs);
         CPURISCVState *env = &cpu->env;
@@ -219,7 +219,7 @@ static int riscv_gdb_get_virtual(CPUState *cs, GByteArray *buf, int n)
         /* Per RiscV debug spec v1.0.0 rc4 */
         uint32_t vbit = (env->virt_enabled) ? BIT(2) : 0;
 
-        return gdb_get_regl(buf, env->priv | vbit);
+        return riscv_gdb_get_reg_mxl(cs, buf, env->priv | vbit);
 #endif
     }
     return 0;
Signature · 1 line
2.55.0
Reviewed-by on v1 8/10
#13 lore raw

Re: [PATCH 08/10] target/riscv: Replace gdb_get_regl() by riscv_gdb_get_reg_mxl()

Quoted 6 lines · Philippe Mathieu-Daudé

riscv_gdb_get_reg_mxl() picks the register width from the CPU's misa_mxl_max instead of target_long_bits(); use it in place of the deprecated gdb_get_regl().

---

Quoted 34 lines
  target/riscv/gdbstub.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
target/riscv/gdbstub.c +3 -3
index b3c11db5397..50e834e367c 100644
@@ -183,7 +183,7 @@ static int riscv_gdb_get_csr(CPUState *cs, GByteArray *buf, int n)
 
          result = riscv_csrrw_debug(env, n, &val, 0, 0);
          if (result == RISCV_EXCP_NONE) {
-            return gdb_get_regl(buf, val);
+            return riscv_gdb_get_reg_mxl(cs, buf, val);
          }
      }
      return 0;
@@ -211,7 +211,7 @@ static int riscv_gdb_get_virtual(CPUState *cs, GByteArray *buf, int n)
  {
      if (n == 0) {
  #ifdef CONFIG_USER_ONLY
-        return gdb_get_regl(buf, 0);
+        return riscv_gdb_get_reg_mxl(cs, buf, 0);
  #else
          RISCVCPU *cpu = RISCV_CPU(cs);
          CPURISCVState *env = &cpu->env;
@@ -219,7 +219,7 @@ static int riscv_gdb_get_virtual(CPUState *cs, GByteArray *buf, int n)
          /* Per RiscV debug spec v1.0.0 rc4 */
          uint32_t vbit = (env->virt_enabled) ? BIT(2) : 0;
 
-        return gdb_get_regl(buf, env->priv | vbit);
+        return riscv_gdb_get_reg_mxl(cs, buf, env->priv | vbit);
  #endif
      }
      return 0;
#14 lore raw

PATCH 9/10 gdbstub: Remove unused gdb_get_regl()

In reply to Philippe Mathieu-Daudé

All callers have been converted to target-specific helpers, so gdb_get_regl() is unused now. Drop it, together with the "cpu-param.h" include that only provided TARGET_LONG_BITS for it.

---
 include/gdbstub/helpers.h | 18 ------------------
 1 file changed, 18 deletions(-)
include/gdbstub/helpers.h +0 -18
index b2f41d6d280..9833725b611 100644
@@ -15,10 +15,6 @@
 #include "qemu/bswap.h"
 #include "qemu/target-info.h"
 
-#ifdef COMPILING_PER_TARGET
-#include "cpu-param.h"
-#endif
-
 /*
  * The GDB remote protocol transfers values in target byte order. As
  * the gdbstub may be batching up several register values we always
@@ -88,20 +84,6 @@ static inline int gdb_get_zeroes(GByteArray *array, size_t len)
     return len;
 }
 
-/**
- * gdb_get_regl: append @val in @buf using 32 or 64-bit, depending on target
- *
- * This function is legacy and deprecated, thus should not be used in new code.
- */
-static inline int gdb_get_regl(GByteArray *buf, uint64_t val)
-{
-    if (target_long_bits() == 64) {
-        return gdb_get_reg64(buf, val);
-    } else {
-        return gdb_get_reg32(buf, val);
-    }
-}
-
 /**
  * gdb_get_reg_ptr: get pointer to start of last element
  * @len: length of element
Signature · 1 line
2.55.0
#15 lore raw

PATCH 10/10 include: Remove TARGET_LONG_SIZE definition

In reply to Philippe Mathieu-Daudé

All the code base uses TARGET_LONG_BITS,
remove TARGET_LONG_SIZE as unnecessary.

---
 include/exec/target_long.h  | 8 +++-----
 include/user/tswap-target.h | 2 +-
 2 files changed, 4 insertions(+), 6 deletions(-)
include/exec/target_long.h +3 -5
index 3cd8e26a23f..5d403c60402 100644
@@ -20,17 +20,15 @@
 #error TARGET_LONG_BITS not defined
 #endif
 
-#define TARGET_LONG_SIZE (TARGET_LONG_BITS / 8)
-
 /* target_ulong is the type of a virtual address */
-#if TARGET_LONG_SIZE == 4
+#if TARGET_LONG_BITS == 32
 typedef int32_t target_long;
 typedef uint32_t target_ulong;
 #define TARGET_FMT_lx "%08x"
 #define TARGET_FMT_ld "%d"
 #define TARGET_FMT_lu "%u"
 #define MO_TL MO_32
-#elif TARGET_LONG_SIZE == 8
+#elif TARGET_LONG_BITS == 64
 typedef int64_t target_long;
 typedef uint64_t target_ulong;
 #define TARGET_FMT_lx "%016" PRIx64
@@ -38,7 +36,7 @@ typedef uint64_t target_ulong;
 #define TARGET_FMT_lu "%" PRIu64
 #define MO_TL MO_64
 #else
-#error TARGET_LONG_SIZE undefined
+#error TARGET_LONG_BITS invalid
 #endif
 
 #endif /* _TARGET_LONG_H_ */
include/user/tswap-target.h +1 -1
index 4719330dbbc..1c66a5b9344 100644
@@ -11,7 +11,7 @@
 #include "exec/cpu-defs.h"
 #include "exec/tswap.h"
 
-#if TARGET_LONG_SIZE == 4
+#if TARGET_LONG_BITS == 32
 #define tswapl(s) tswap32(s)
 #define bswaptls(s) bswap32s(s)
 #else
Signature · 1 line
2.55.0
Reviewed-by on the v1 series
#16 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 18 lines · Philippe Mathieu-Daudé

Prefer getting target register word size at runtime (via CPU state) instead of build time.

This allow to remove gdb_get_regl() and various target_long_bits() calls, and to eliminate the TARGET_LONG_SIZE definition.

Philippe Mathieu-Daudé (10):
target/sparc/gdbstub: Replace gdb_get_regl() by gdb_get_reg64() target/mips/gdbstub: Reduce to a single gdb_get_regl() call target/mips/gdbstub: Replace target_long_bits() by mips_env_is_64bit() target/ppc: Factor ppc_env_is_64bit() out target/ppc/gdbstub: Replace target_long_bits() by ppc_env_is_64bit() target/ppc/gdbstub: Replace gdb_get_regl() by ppc_env_is_64bit() target/riscv: Factor riscv_gdb_get_reg_mxl() out target/riscv: Replace gdb_get_regl() by riscv_gdb_get_reg_mxl()
gdbstub: Remove unused gdb_get_regl()
include: Remove TARGET_LONG_SIZE definition

Quoted 12 lines
 include/exec/target_long.h  |  8 ++--
 include/gdbstub/helpers.h   | 18 --------
 include/user/tswap-target.h |  2 +-
 target/mips/internal.h      |  5 +++
 target/ppc/internal.h       | 11 +++++
 target/mips/gdbstub.c       | 85 ++++++++++++++++++++++---------------
 target/ppc/cpu_init.c       |  2 +-
 target/ppc/gdbstub.c        | 38 +++++++++++------
 target/riscv/gdbstub.c      | 35 +++++++++------
 target/sparc/gdbstub.c      | 13 +++---
 10 files changed, 125 insertions(+), 92 deletions(-)

For the series:

Regards,
Pierrick