Skip to contentSkip to conversation

target/riscv: Fix RVF-only GDB FPR writes

PATCH New Zephyr Li 1 participant 1 message Dec 2 RISC-V

lore
Share & tools
lore

Zephyr Li

author
#0 lore raw

PATCH 1/1 target/riscv: Fix RVF-only GDB FPR writes

The RVF-only GDB register feature advertises 32-bit FPRs, but the write callback loads and reports eight bytes for every FPR.

Register accessors alongside their matching XML feature so their width also stays consistent if a guest changes misa after registration. Load and report four bytes for the 32-bit feature, and NaN-box the value in the internal FPR. Keep the full eight-byte write for the 64-bit feature.

Fixes 5371f5cd7170 ("RISC-V: Add hooks to use the gdb xml files.")

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4427

---
 target/riscv/gdbstub.c | 40 +++++++++++++++++++++++++++++-----------
 1 file changed, 29 insertions(+), 11 deletions(-)
target/riscv/gdbstub.c +29 -11
index 50e834e367..3138bf474a 100644
@@ -109,29 +109,47 @@ int riscv_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n)
     return regsize;
 }
 
-static int riscv_gdb_get_fpu(CPUState *cs, GByteArray *buf, int n)
+static int riscv_gdb_get_fpu32(CPUState *cs, GByteArray *buf, int n)
 {
     RISCVCPU *cpu = RISCV_CPU(cs);
     CPURISCVState *env = &cpu->env;
 
     if (n < 32) {
-        if (env->misa_ext & RVD) {
-            return gdb_get_reg64(buf, env->fpr[n]);
-        }
-        if (env->misa_ext & RVF) {
-            return gdb_get_reg32(buf, env->fpr[n]);
-        }
+        return gdb_get_reg32(buf, env->fpr[n]);
+    }
+    return 0;
+}
+
+static int riscv_gdb_set_fpu32(CPUState *cs, uint8_t *mem_buf, int n)
+{
+    RISCVCPU *cpu = RISCV_CPU(cs);
+    CPURISCVState *env = &cpu->env;
+
+    if (n < 32) {
+        env->fpr[n] = nanbox_s(env, ldn(env, mem_buf, 4));
+        return sizeof(uint32_t);
+    }
+    return 0;
+}
+
+static int riscv_gdb_get_fpu64(CPUState *cs, GByteArray *buf, int n)
+{
+    RISCVCPU *cpu = RISCV_CPU(cs);
+    CPURISCVState *env = &cpu->env;
+
+    if (n < 32) {
+        return gdb_get_reg64(buf, env->fpr[n]);
     }
     return 0;
 }
 
-static int riscv_gdb_set_fpu(CPUState *cs, uint8_t *mem_buf, int n)
+static int riscv_gdb_set_fpu64(CPUState *cs, uint8_t *mem_buf, int n)
 {
     RISCVCPU *cpu = RISCV_CPU(cs);
     CPURISCVState *env = &cpu->env;
 
     if (n < 32) {
-        env->fpr[n] = ldn(env, mem_buf, 8); /* always 64-bit */
+        env->fpr[n] = ldn(env, mem_buf, 8);
         return sizeof(uint64_t);
     }
     return 0;
@@ -348,10 +366,10 @@ void riscv_cpu_register_gdb_regs_for_features(CPUState *cs)
     RISCVCPU *cpu = RISCV_CPU(cs);
     CPURISCVState *env = &cpu->env;
     if (env->misa_ext & RVD) {
-        gdb_register_coprocessor(cs, riscv_gdb_get_fpu, riscv_gdb_set_fpu,
+        gdb_register_coprocessor(cs, riscv_gdb_get_fpu64, riscv_gdb_set_fpu64,
                                  gdb_find_static_feature("riscv-64bit-fpu.xml"));
     } else if (env->misa_ext & RVF) {
-        gdb_register_coprocessor(cs, riscv_gdb_get_fpu, riscv_gdb_set_fpu,
+        gdb_register_coprocessor(cs, riscv_gdb_get_fpu32, riscv_gdb_set_fpu32,
                                  gdb_find_static_feature("riscv-32bit-fpu.xml"));
     }
     if (cpu->cfg.ext_zve32x) {
Signature · 1 line
2.43.0