Skip to contentSkip to conversation

target/riscv: Expose "virt" register for GDB for reads

PATCH In review Jim Shu 3 participants 9 messages Mar 5, 2023 – Mar 10, 2023 RISC-V

lore
Share & tools
lore

Jim Shu

author
#0 lore raw

PATCH 1/2 target/riscv: Expose "virt" register for GDB for reads

This patch enables a debugger to read current virtualization mode via virtual "virt" register. After it, we could get full current privilege mode via both "priv" and "virt" register.

Extend previous commit ab9056ff9bdb3f95db6e7a666d10522d289f14ec to support H-extension.

---
 gdb-xml/riscv-32bit-virtual.xml |  1 +
 gdb-xml/riscv-64bit-virtual.xml |  1 +
 target/riscv/gdbstub.c          | 12 ++++++++----
 3 files changed, 10 insertions(+), 4 deletions(-)
gdb-xml/riscv-32bit-virtual.xml +1 -0
index 905f1c555d..d44b6ca2dc 100644
@@ -8,4 +8,5 @@
 <!DOCTYPE feature SYSTEM "gdb-target.dtd">
 <feature name="org.gnu.gdb.riscv.virtual">
   <reg name="priv" bitsize="32"/>
+  <reg name="virt" bitsize="32"/>
 </feature>
gdb-xml/riscv-64bit-virtual.xml +1 -0
index 62d86c237b..7c9b63d5b6 100644
@@ -8,4 +8,5 @@
 <!DOCTYPE feature SYSTEM "gdb-target.dtd">
 <feature name="org.gnu.gdb.riscv.virtual">
   <reg name="priv" bitsize="64"/>
+  <reg name="virt" bitsize="64"/>
 </feature>
target/riscv/gdbstub.c +8 -4
index 6048541606..1755fd9d51 100644
@@ -187,13 +187,17 @@ static int riscv_gdb_set_csr(CPURISCVState *env, uint8_t *mem_buf, int n)
 
 static int riscv_gdb_get_virtual(CPURISCVState *cs, GByteArray *buf, int n)
 {
-    if (n == 0) {
 #ifdef CONFIG_USER_ONLY
+    if (n >= 0 && n <= 1) {
         return gdb_get_regl(buf, 0);
+    }
 #else
+    if (n == 0) {
         return gdb_get_regl(buf, cs->priv);
-#endif
+    } else if (n == 1) {
+        return gdb_get_regl(buf, riscv_cpu_virt_enabled(cs));
     }
+#endif
     return 0;
 }
 
@@ -328,13 +332,13 @@ void riscv_cpu_register_gdb_regs_for_features(CPUState *cs)
     case MXL_RV32:
         gdb_register_coprocessor(cs, riscv_gdb_get_virtual,
                                  riscv_gdb_set_virtual,
-                                 1, "riscv-32bit-virtual.xml", 0);
+                                 2, "riscv-32bit-virtual.xml", 0);
         break;
     case MXL_RV64:
     case MXL_RV128:
         gdb_register_coprocessor(cs, riscv_gdb_get_virtual,
                                  riscv_gdb_set_virtual,
-                                 1, "riscv-64bit-virtual.xml", 0);
+                                 2, "riscv-64bit-virtual.xml", 0);
         break;
     default:
         g_assert_not_reached();
Signature · 1 line
2.17.1
author
#1 lore raw

PATCH 2/2 target/riscv: Make the "virt" register writable by GDB

This patch also enables debugger to set current privilege mode to VU/VS-mode.

Extend previous commit 81d2929c41d32af138f3562f5a7b309f6eac7ca7 to support H-extension.

---
 target/riscv/gdbstub.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
target/riscv/gdbstub.c +16 -2
index 1755fd9d51..a7f234beaf 100644
@@ -203,15 +203,29 @@ static int riscv_gdb_get_virtual(CPURISCVState *cs, GByteArray *buf, int n)
 
 static int riscv_gdb_set_virtual(CPURISCVState *cs, uint8_t *mem_buf, int n)
 {
+#ifdef CONFIG_USER_ONLY
+    if (n >= 0 && n <= 1) {
+        return sizeof(target_ulong);
+    }
+#else
+    bool virt;
+
     if (n == 0) {
-#ifndef CONFIG_USER_ONLY
         cs->priv = ldtul_p(mem_buf) & 0x3;
         if (cs->priv == PRV_H) {
             cs->priv = PRV_S;
         }
-#endif
+        return sizeof(target_ulong);
+    } else if (n == 1) {
+        virt = ldtul_p(mem_buf) & 0x1;
+        if ((cs->priv == PRV_M) && (virt == true)) {
+            /* M-mode only supports V=0. */
+            virt = false;
+        }
+        riscv_cpu_set_virt_enabled(cs, virt);
         return sizeof(target_ulong);
     }
+#endif
     return 0;
 }
 
Signature · 1 line
2.17.1
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] target/riscv: Make the "virt" register writable by GDB

Quoted 25 lines · Jim Shu

This patch also enables debugger to set current privilege mode to VU/VS-mode.

Extend previous commit 81d2929c41d32af138f3562f5a7b309f6eac7ca7 to support H-extension.

---
  target/riscv/gdbstub.c | 18 ++++++++++++++++--
  1 file changed, 16 insertions(+), 2 deletions(-)
target/riscv/gdbstub.c +5 -0
index 1755fd9d51..a7f234beaf 100644
@@ -203,15 +203,29 @@ static int riscv_gdb_get_virtual(CPURISCVState *cs, GByteArray *buf, int n)
 
  static int riscv_gdb_set_virtual(CPURISCVState *cs, uint8_t *mem_buf, int n)
  {
+#ifdef CONFIG_USER_ONLY
+    if (n >= 0 && n <= 1) {
+        return sizeof(target_ulong);
+    }
+#else

target/riscv/gdbstub.c static int riscv_gdb_get_virtual(CPURISCVState *cs, GByteArray *buf, int n) · line 218

Quoted 10 lines
target/riscv/gdbstub.c +3 -2
+    bool virt;
+
      if (n == 0) {
-#ifndef CONFIG_USER_ONLY
          cs->priv = ldtul_p(mem_buf) & 0x3;
          if (cs->priv == PRV_H) {
              cs->priv = PRV_S;
          }
-#endif
+        return sizeof(target_ulong);

We should return according to the misa_mxl_max. And this is a bug before your commit.

target/riscv/gdbstub.c static int riscv_gdb_get_virtual(CPURISCVState *cs, GByteArray *buf, int n) · line 226

Quoted 8 lines
target/riscv/gdbstub.c +7 -0
+    } else if (n == 1) {
+        virt = ldtul_p(mem_buf) & 0x1;
+        if ((cs->priv == PRV_M) && (virt == true)) {
+            /* M-mode only supports V=0. */
+            virt = false;
+        }
+        riscv_cpu_set_virt_enabled(cs, virt);
          return sizeof(target_ulong);

Same error here. Otherwise,

Zhiwei

Quoted 5 lines
target/riscv/gdbstub.c +1 -0
      }
+#endif
      return 0;
  }
 
author
#3 lore raw

Re: [PATCH 2/2] target/riscv: Make the "virt" register writable by GDB

Thanks for reviewing.
I'll fix this issue.

Quoted 59 lines · LIU Zhiwei
Quoted 25 lines · Jim Shu

This patch also enables debugger to set current privilege mode to VU/VS-mode.

Extend previous commit 81d2929c41d32af138f3562f5a7b309f6eac7ca7 to support H-extension.

---
  target/riscv/gdbstub.c | 18 ++++++++++++++++--
  1 file changed, 16 insertions(+), 2 deletions(-)
target/riscv/gdbstub.c +5 -0
index 1755fd9d51..a7f234beaf 100644
@@ -203,15 +203,29 @@ static int riscv_gdb_get_virtual(CPURISCVState *cs, GByteArray *buf, int n)
 
  static int riscv_gdb_set_virtual(CPURISCVState *cs, uint8_t *mem_buf, int n)
  {
+#ifdef CONFIG_USER_ONLY
+    if (n >= 0 && n <= 1) {
+        return sizeof(target_ulong);
+    }
+#else

target/riscv/gdbstub.c static int riscv_gdb_get_virtual(CPURISCVState *cs, GByteArray *buf, int n) · line 218

Quoted 10 lines
target/riscv/gdbstub.c +3 -2
+    bool virt;
+
      if (n == 0) {
-#ifndef CONFIG_USER_ONLY
          cs->priv = ldtul_p(mem_buf) & 0x3;
          if (cs->priv == PRV_H) {
              cs->priv = PRV_S;
          }
-#endif
+        return sizeof(target_ulong);

We should return according to the misa_mxl_max. And this is a bug before your commit.

target/riscv/gdbstub.c static int riscv_gdb_get_virtual(CPURISCVState *cs, GByteArray *buf, int n) · line 226

Quoted 8 lines
target/riscv/gdbstub.c +7 -0
+    } else if (n == 1) {
+        virt = ldtul_p(mem_buf) & 0x1;
+        if ((cs->priv == PRV_M) && (virt == true)) {
+            /* M-mode only supports V=0. */
+            virt = false;
+        }
+        riscv_cpu_set_virt_enabled(cs, virt);
          return sizeof(target_ulong);

Same error here. Otherwise,

Zhiwei

Quoted 5 lines
target/riscv/gdbstub.c +1 -0
      }
+#endif
      return 0;
  }
 
author
#4 lore raw

Re: [PATCH 2/2] target/riscv: Make the "virt" register writable by GDB

In reply to LIU Zhiwei
Quoted 32 lines · LIU Zhiwei
Quoted 29 lines · Jim Shu

This patch also enables debugger to set current privilege mode to VU/VS-mode.

Extend previous commit 81d2929c41d32af138f3562f5a7b309f6eac7ca7 to support H-extension.

---
  target/riscv/gdbstub.c | 18 ++++++++++++++++--
  1 file changed, 16 insertions(+), 2 deletions(-)
target/riscv/gdbstub.c +7 -1
index 1755fd9d51..a7f234beaf 100644
@@ -203,15 +203,29 @@ static int riscv_gdb_get_virtual(CPURISCVState *cs, GByteArray *buf, int n)
 
  static int riscv_gdb_set_virtual(CPURISCVState *cs, uint8_t *mem_buf, int n)
  {
+#ifdef CONFIG_USER_ONLY
+    if (n >= 0 && n <= 1) {
+        return sizeof(target_ulong);
+    }
+#else
+    bool virt;
+
      if (n == 0) {
-#ifndef CONFIG_USER_ONLY
Quoted 8 lines

target/riscv/gdbstub.c static int riscv_gdb_get_virtual(CPURISCVState *cs, GByteArray *buf, int n) · line 218

Quoted 6 lines
target/riscv/gdbstub.c +1 -1
          cs->priv = ldtul_p(mem_buf) & 0x3;
          if (cs->priv == PRV_H) {
              cs->priv = PRV_S;
          }
-#endif
+        return sizeof(target_ulong);

We should return according to the misa_mxl_max. And this is a bug before your commit.

Hi Zhiwei,

After reading other gdbstub.c code, I think it is OK to use 'sizeof(target_ulong)' as virtual register length.
Its length is 32-bit in RV32 and is 64-bit in RV64/RV128. We don't need to handle RV128 specially.
Virtual register length is same as CSR length and 'riscv_gdb_set_csr()' also use 'sizeof(target_ulong)'.

Jim Shu

Quoted 19 lines

target/riscv/gdbstub.c static int riscv_gdb_get_virtual(CPURISCVState *cs, GByteArray *buf, int n) · line 226

Quoted 8 lines
target/riscv/gdbstub.c +7 -0
+    } else if (n == 1) {
+        virt = ldtul_p(mem_buf) & 0x1;
+        if ((cs->priv == PRV_M) && (virt == true)) {
+            /* M-mode only supports V=0. */
+            virt = false;
+        }
+        riscv_cpu_set_virt_enabled(cs, virt);
          return sizeof(target_ulong);

Same error here. Otherwise,

Zhiwei

Quoted 5 lines
target/riscv/gdbstub.c +1 -0
      }
+#endif
      return 0;
  }
 
#5 lore raw

Re: [PATCH 2/2] target/riscv: Make the "virt" register writable by GDB

Quoted 40 lines · Jim Shu
Quoted 31 lines · LIU Zhiwei
Quoted 29 lines · Jim Shu

This patch also enables debugger to set current privilege mode to VU/VS-mode.

Extend previous commit 81d2929c41d32af138f3562f5a7b309f6eac7ca7 to support H-extension.

---
   target/riscv/gdbstub.c | 18 ++++++++++++++++--
   1 file changed, 16 insertions(+), 2 deletions(-)
target/riscv/gdbstub.c +7 -1
index 1755fd9d51..a7f234beaf 100644
@@ -203,15 +203,29 @@ static int riscv_gdb_get_virtual(CPURISCVState *cs, GByteArray *buf, int n)
 
   static int riscv_gdb_set_virtual(CPURISCVState *cs, uint8_t *mem_buf, int n)
   {
+#ifdef CONFIG_USER_ONLY
+    if (n >= 0 && n <= 1) {
+        return sizeof(target_ulong);
+    }
+#else
+    bool virt;
+
       if (n == 0) {
-#ifndef CONFIG_USER_ONLY
Quoted 8 lines

target/riscv/gdbstub.c static int riscv_gdb_get_virtual(CPURISCVState *cs, GByteArray *buf, int n) · line 218

Quoted 6 lines
target/riscv/gdbstub.c +1 -1
           cs->priv = ldtul_p(mem_buf) & 0x3;
           if (cs->priv == PRV_H) {
               cs->priv = PRV_S;
           }
-#endif
+        return sizeof(target_ulong);

We should return according to the misa_mxl_max. And this is a bug before your commit.

Quoted 4 lines

Hi Zhiwei,

After reading other gdbstub.c code, I think it is OK to use 'sizeof(target_ulong)' as virtual register length.

No, you should refer to the riscv_cpu_gdb_read(write)_register.

Quoted 1 line

Its length is 32-bit in RV32 and is 64-bit in RV64/RV128.

Although we don't support MXLEN 32bit  currently on qemu-system-riscv64, we should not introduce more code to make dynamic xlen support complex.

Zhiwei

Quoted 26 lines

We don't
need to handle RV128 specially.
Virtual register length is same as CSR length and 'riscv_gdb_set_csr()' also use 'sizeof(target_ulong)'.

Jim Shu

Quoted 19 lines

target/riscv/gdbstub.c static int riscv_gdb_get_virtual(CPURISCVState *cs, GByteArray *buf, int n) · line 226

Quoted 8 lines
target/riscv/gdbstub.c +7 -0
+    } else if (n == 1) {
+        virt = ldtul_p(mem_buf) & 0x1;
+        if ((cs->priv == PRV_M) && (virt == true)) {
+            /* M-mode only supports V=0. */
+            virt = false;
+        }
+        riscv_cpu_set_virt_enabled(cs, virt);
           return sizeof(target_ulong);

Same error here. Otherwise,

Zhiwei

Quoted 5 lines
target/riscv/gdbstub.c +1 -0
       }
+#endif
       return 0;
   }
 
#6 lore raw

Re: [PATCH 2/2] target/riscv: Make the "virt" register writable by GDB

In reply to Jim Shu
Quoted 6 lines · Jim Shu

This patch also enables debugger to set current privilege mode to VU/VS-mode.

Extend previous commit 81d2929c41d32af138f3562f5a7b309f6eac7ca7 to support H-extension.

I'm not sure we want this. What is the use case for this?

Changing the virt mode on the fly like this is likely to break lots of software. Should we really allow users to do this?

Alistair

Quoted 47 lines
---
 target/riscv/gdbstub.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
target/riscv/gdbstub.c +16 -3
index 1755fd9d51..a7f234beaf 100644
@@ -203,15 +203,29 @@ static int riscv_gdb_get_virtual(CPURISCVState *cs, GByteArray *buf, int n)
 
 static int riscv_gdb_set_virtual(CPURISCVState *cs, uint8_t *mem_buf, int n)
 {
+#ifdef CONFIG_USER_ONLY
+    if (n >= 0 && n <= 1) {
+        return sizeof(target_ulong);
+    }
+#else
+    bool virt;
+
     if (n == 0) {
-#ifndef CONFIG_USER_ONLY
         cs->priv = ldtul_p(mem_buf) & 0x3;
         if (cs->priv == PRV_H) {
             cs->priv = PRV_S;
         }
-#endif
+        return sizeof(target_ulong);
+    } else if (n == 1) {
+        virt = ldtul_p(mem_buf) & 0x1;
+        if ((cs->priv == PRV_M) && (virt == true)) {
+            /* M-mode only supports V=0. */
+            virt = false;
+        }
+        riscv_cpu_set_virt_enabled(cs, virt);
         return sizeof(target_ulong);
     }
+#endif
     return 0;
 }
 
--

2.17.1

Reviewed-by on v1 1/2
#7 lore raw
In reply to Jim Shu
Quoted 62 lines · Jim Shu

This patch enables a debugger to read current virtualization mode via virtual "virt" register. After it, we could get full current privilege mode via both "priv" and "virt" register.

Extend previous commit ab9056ff9bdb3f95db6e7a666d10522d289f14ec to support H-extension.

---
  gdb-xml/riscv-32bit-virtual.xml |  1 +
  gdb-xml/riscv-64bit-virtual.xml |  1 +
  target/riscv/gdbstub.c          | 12 ++++++++----
  3 files changed, 10 insertions(+), 4 deletions(-)
gdb-xml/riscv-32bit-virtual.xml +1 -0
index 905f1c555d..d44b6ca2dc 100644
@@ -8,4 +8,5 @@
  <!DOCTYPE feature SYSTEM "gdb-target.dtd">
  <feature name="org.gnu.gdb.riscv.virtual">
    <reg name="priv" bitsize="32"/>
+  <reg name="virt" bitsize="32"/>
  </feature>
gdb-xml/riscv-64bit-virtual.xml +1 -0
index 62d86c237b..7c9b63d5b6 100644
@@ -8,4 +8,5 @@
  <!DOCTYPE feature SYSTEM "gdb-target.dtd">
  <feature name="org.gnu.gdb.riscv.virtual">
    <reg name="priv" bitsize="64"/>
+  <reg name="virt" bitsize="64"/>
  </feature>
target/riscv/gdbstub.c +6 -2
index 6048541606..1755fd9d51 100644
@@ -187,13 +187,17 @@ static int riscv_gdb_set_csr(CPURISCVState *env, uint8_t *mem_buf, int n)
 
  static int riscv_gdb_get_virtual(CPURISCVState *cs, GByteArray *buf, int n)
  {
-    if (n == 0) {
  #ifdef CONFIG_USER_ONLY
+    if (n >= 0 && n <= 1) {
          return gdb_get_regl(buf, 0);
+    }
  #else
+    if (n == 0) {
          return gdb_get_regl(buf, cs->priv);
-#endif
+    } else if (n == 1) {
+        return gdb_get_regl(buf, riscv_cpu_virt_enabled(cs));
      }
+#endif
      return 0;
  }
 
@@ -328,13 +332,13 @@ void riscv_cpu_register_gdb_regs_for_features(CPUState *cs)
      case MXL_RV32:
          gdb_register_coprocessor(cs, riscv_gdb_get_virtual,

target/riscv/gdbstub.c void riscv_cpu_register_gdb_regs_for_features(CPUState *cs) · line 341

Quoted 10 lines
target/riscv/gdbstub.c +2 -2
                                   riscv_gdb_set_virtual,
-                                 1, "riscv-32bit-virtual.xml", 0);
+                                 2, "riscv-32bit-virtual.xml", 0);
          break;
      case MXL_RV64:
      case MXL_RV128:
          gdb_register_coprocessor(cs, riscv_gdb_get_virtual,
                                   riscv_gdb_set_virtual,
-                                 1, "riscv-64bit-virtual.xml", 0);
+                                 2, "riscv-64bit-virtual.xml", 0);

Zhiwei

Quoted 3 lines
target/riscv/gdbstub.c +0 -0
          break;
      default:
          g_assert_not_reached();
Reviewed-by on v1 1/2
#8 lore raw
In reply to Jim Shu
Quoted 10 lines · Jim Shu

This patch enables a debugger to read current virtualization mode via virtual "virt" register. After it, we could get full current privilege mode via both "priv" and "virt" register.

Extend previous commit ab9056ff9bdb3f95db6e7a666d10522d289f14ec to support H-extension.

Alistair

Quoted 70 lines
---
 gdb-xml/riscv-32bit-virtual.xml |  1 +
 gdb-xml/riscv-64bit-virtual.xml |  1 +
 target/riscv/gdbstub.c          | 12 ++++++++----
 3 files changed, 10 insertions(+), 4 deletions(-)
gdb-xml/riscv-32bit-virtual.xml +1 -0
index 905f1c555d..d44b6ca2dc 100644
@@ -8,4 +8,5 @@
 <!DOCTYPE feature SYSTEM "gdb-target.dtd">
 <feature name="org.gnu.gdb.riscv.virtual">
   <reg name="priv" bitsize="32"/>
+  <reg name="virt" bitsize="32"/>
 </feature>
gdb-xml/riscv-64bit-virtual.xml +1 -0
index 62d86c237b..7c9b63d5b6 100644
@@ -8,4 +8,5 @@
 <!DOCTYPE feature SYSTEM "gdb-target.dtd">
 <feature name="org.gnu.gdb.riscv.virtual">
   <reg name="priv" bitsize="64"/>
+  <reg name="virt" bitsize="64"/>
 </feature>
target/riscv/gdbstub.c +8 -5
index 6048541606..1755fd9d51 100644
@@ -187,13 +187,17 @@ static int riscv_gdb_set_csr(CPURISCVState *env, uint8_t *mem_buf, int n)
 
 static int riscv_gdb_get_virtual(CPURISCVState *cs, GByteArray *buf, int n)
 {
-    if (n == 0) {
 #ifdef CONFIG_USER_ONLY
+    if (n >= 0 && n <= 1) {
         return gdb_get_regl(buf, 0);
+    }
 #else
+    if (n == 0) {
         return gdb_get_regl(buf, cs->priv);
-#endif
+    } else if (n == 1) {
+        return gdb_get_regl(buf, riscv_cpu_virt_enabled(cs));
     }
+#endif
     return 0;
 }
 
@@ -328,13 +332,13 @@ void riscv_cpu_register_gdb_regs_for_features(CPUState *cs)
     case MXL_RV32:
         gdb_register_coprocessor(cs, riscv_gdb_get_virtual,
                                  riscv_gdb_set_virtual,
-                                 1, "riscv-32bit-virtual.xml", 0);
+                                 2, "riscv-32bit-virtual.xml", 0);
         break;
     case MXL_RV64:
     case MXL_RV128:
         gdb_register_coprocessor(cs, riscv_gdb_get_virtual,
                                  riscv_gdb_set_virtual,
-                                 1, "riscv-64bit-virtual.xml", 0);
+                                 2, "riscv-64bit-virtual.xml", 0);
         break;
     default:
         g_assert_not_reached();
--

2.17.1