Skip to contentSkip to conversation

target/riscv: Initial support for native debug feature via M-mode CSRs

PATCH Superseded Bin Meng 2 participants 10 messages Oct 29, 2021

Subsystems (2)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Bin Meng

author
#0 lore raw

cover target/riscv: Initial support for native debug feature via M-mode CSRs

This adds initial support for the native debug via the Trigger Module, as defined in the RISC-V Debug Specification [1].

Only "Address / Data Match" trigger (type 2) is implemented as of now, which is mainly used for hardware breakpoint and watchpoint. The number of type 2 triggers implemented is 2, which is the number that we can find in the SiFive U54/U74 cores.

[1] https://github.com/riscv/riscv-debug-spec/raw/master/riscv-debug-stable.pdf

Bin Meng (5):
target/riscv: Add initial support for native debug target/riscv: debug: Implement debug related TCGCPUOps target/riscv: Add a config option for native debug target/riscv: csr: Hook debug CSR read/write
hw/core: tcg-cpu-ops.h: Update comments of debug_check_watchpoint()

 include/hw/core/tcg-cpu-ops.h |   1 +
 target/riscv/cpu.h            |   7 +
 target/riscv/debug.h          | 114 +++++++++
 target/riscv/cpu.c            |  14 ++
 target/riscv/csr.c            |  57 +++++
 target/riscv/debug.c          | 439 ++++++++++++++++++++++++++++++++++
 target/riscv/meson.build      |   1 +
 7 files changed, 633 insertions(+)
 create mode 100644 target/riscv/debug.h
 create mode 100644 target/riscv/debug.c
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH 1/5 target/riscv: Add initial support for native debug

This adds initial support for the native debug via the Trigger Module, as defined in the RISC-V Debug Specification [1].

Only "Address / Data Match" trigger (type 2) is implemented as of now, which is mainly used for hardware breakpoint and watchpoint. The number of type 2 triggers implemented is 2, which is the number that we can find in the SiFive U54/U74 cores.

[1] https://github.com/riscv/riscv-debug-spec/raw/master/riscv-debug-stable.pdf

---

 target/riscv/cpu.h       |   5 +
 target/riscv/debug.h     | 110 ++++++++++++
 target/riscv/debug.c     | 364 +++++++++++++++++++++++++++++++++++++++
 target/riscv/meson.build |   1 +
 4 files changed, 480 insertions(+)
 create mode 100644 target/riscv/debug.h
 create mode 100644 target/riscv/debug.c
target/riscv/cpu.h +5 -0
index a33dc30be8..457adde952 100644
@@ -97,6 +97,7 @@ typedef struct CPURISCVState CPURISCVState;
 
 #if !defined(CONFIG_USER_ONLY)
 #include "pmp.h"
+#include "debug.h"
 #endif
 
 #define RV_VLEN_MAX 256
@@ -231,6 +232,10 @@ struct CPURISCVState {
     pmp_table_t pmp_state;
     target_ulong mseccfg;
 
+    /* trigger module */
+    target_ulong trigger_cur;
+    trigger_type2_t trigger_type2[TRIGGER_TYPE2_NUM];
+
     /* machine specific rdtime callback */
     uint64_t (*rdtime_fn)(uint32_t);
     uint32_t rdtime_fn_arg;
target/riscv/debug.h new file +110 -0

110 lines not on this page · raw message

target/riscv/debug.c new file +364 -0

364 lines not on this page · raw message

target/riscv/meson.build +1 -0
index d5e0bc93ea..966d97237a 100644
@@ -24,6 +24,7 @@ riscv_softmmu_ss = ss.source_set()
 riscv_softmmu_ss.add(files(
   'arch_dump.c',
   'pmp.c',
+  'debug.c',
   'monitor.c',
   'machine.c'
 ))
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH 1/5] target/riscv: Add initial support for native debug

target/riscv/cpu.h struct CPURISCVState { · line 237

Quoted 7 lines · Bin Meng
target/riscv/cpu.h +3 -0
@@ -231,6 +232,10 @@ struct CPURISCVState {
      pmp_table_t pmp_state;
      target_ulong mseccfg;
 
+    /* trigger module */
+    target_ulong trigger_cur;
+    trigger_type2_t trigger_type2[TRIGGER_TYPE2_NUM];

You need to add a subsection to machine.c to migrate this state.

r~

author
#3 lore raw

PATCH 2/5 target/riscv: debug: Implement debug related TCGCPUOps

In reply to Bin Meng

Implement .debug_excp_handler, .debug_check_{breakpoint, watchpoint} TCGCPUOps and hook them into riscv_tcg_ops.

---

 target/riscv/debug.h |  4 +++
 target/riscv/cpu.c   |  3 ++
 target/riscv/debug.c | 75 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 82 insertions(+)
target/riscv/debug.h +4 -0
index cb8a6e0024..fddc103650 100644
@@ -107,4 +107,8 @@ void tdata_csr_write(CPURISCVState *env, int tdata_index, target_ulong val);
 
 void riscv_trigger_init(CPURISCVState *env);
 
+void riscv_cpu_debug_excp_handler(CPUState *cs);
+bool riscv_cpu_debug_check_breakpoint(CPUState *cs);
+bool riscv_cpu_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp);
+
 #endif /* RISCV_DEBUG_H */
target/riscv/cpu.c +3 -0
index 788fa0b11c..eface73e7d 100644
@@ -688,6 +688,9 @@ static const struct TCGCPUOps riscv_tcg_ops = {
     .do_interrupt = riscv_cpu_do_interrupt,
     .do_transaction_failed = riscv_cpu_do_transaction_failed,
     .do_unaligned_access = riscv_cpu_do_unaligned_access,
+    .debug_excp_handler = riscv_cpu_debug_excp_handler,
+    .debug_check_breakpoint = riscv_cpu_debug_check_breakpoint,
+    .debug_check_watchpoint = riscv_cpu_debug_check_watchpoint,
 #endif /* !CONFIG_USER_ONLY */
 };
 
target/riscv/debug.c +75 -0

78 lines not on this page · raw message

Signature · 1 line
2.25.1
#4 lore raw

Re: [PATCH 2/5] target/riscv: debug: Implement debug related TCGCPUOps

Quoted 6 lines · Bin Meng
target/riscv/debug.c +6 -0
+void riscv_cpu_debug_excp_handler(CPUState *cs)
+{
+    RISCVCPU *cpu = RISCV_CPU(cs);
+    CPURISCVState *env = &cpu->env;
+
+    if (cs->watchpoint_hit) {

target/riscv/debug.c void riscv_trigger_init(CPURISCVState *env) · line 381

Quoted 10 lines
target/riscv/debug.c +10 -0
+        if (cs->watchpoint_hit->flags & BP_CPU) {
+            cs->watchpoint_hit = NULL;
+            riscv_raise_exception(env, RISCV_EXCP_BREAKPOINT, GETPC());
+        }
+    } else {
+        if (cpu_breakpoint_test(cs, env->pc, BP_CPU)) {
+            riscv_raise_exception(env, RISCV_EXCP_BREAKPOINT, GETPC());
+        }
+    }
+}

GETPC is only to be used by handlers called from TCG generated code.
You want 0 to indicate that unwinding is not needed, as it has been done for you already by generic code.

r~

author
#5 lore raw

PATCH 3/5 target/riscv: Add a config option for native debug

In reply to Bin Meng

Add a config option to enable support for native M-mode debug. This is enabled by default and can be disabled with 'debug=false'.

---

 target/riscv/cpu.h | 2 ++
 target/riscv/cpu.c | 5 +++++
 2 files changed, 7 insertions(+)
target/riscv/cpu.h +2 -0
index 457adde952..5787d1598c 100644
@@ -74,6 +74,7 @@ enum {
     RISCV_FEATURE_MMU,
     RISCV_FEATURE_PMP,
     RISCV_FEATURE_EPMP,
+    RISCV_FEATURE_DEBUG,
     RISCV_FEATURE_MISA
 };
 
@@ -314,6 +315,7 @@ struct RISCVCPU {
         bool mmu;
         bool pmp;
         bool epmp;
+        bool debug;
         uint64_t resetvec;
     } cfg;
 };
target/riscv/cpu.c +5 -0
index eface73e7d..3a2fa97098 100644
@@ -439,6 +439,10 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp)
         }
     }
 
+    if (cpu->cfg.debug) {
+        set_feature(env, RISCV_FEATURE_DEBUG);
+    }
+
     set_resetvec(env, cpu->cfg.resetvec);
 
     /* Validate that MISA_MXL is set properly. */
@@ -619,6 +623,7 @@ static Property riscv_cpu_properties[] = {
     DEFINE_PROP_BOOL("Zicsr", RISCVCPU, cfg.ext_icsr, true),
     DEFINE_PROP_BOOL("mmu", RISCVCPU, cfg.mmu, true),
     DEFINE_PROP_BOOL("pmp", RISCVCPU, cfg.pmp, true),
+    DEFINE_PROP_BOOL("debug", RISCVCPU, cfg.debug, true),
 
     DEFINE_PROP_STRING("priv_spec", RISCVCPU, cfg.priv_spec),
 
Signature · 1 line
2.25.1
#6 lore raw

Re: [PATCH 3/5] target/riscv: Add a config option for native debug

Quoted 16 lines · Bin Meng
target/riscv/cpu.c +4 -0
index eface73e7d..3a2fa97098 100644
@@ -439,6 +439,10 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp)
          }
      }
 
+    if (cpu->cfg.debug) {
+        set_feature(env, RISCV_FEATURE_DEBUG);
+    }
+
      set_resetvec(env, cpu->cfg.resetvec);
 
      /* Validate that MISA_MXL is set properly. */

target/riscv/cpu.c static Property riscv_cpu_properties[] = { · line 628

Quoted 6 lines
target/riscv/cpu.c +1 -0
@@ -619,6 +623,7 @@ static Property riscv_cpu_properties[] = {
      DEFINE_PROP_BOOL("Zicsr", RISCVCPU, cfg.ext_icsr, true),
      DEFINE_PROP_BOOL("mmu", RISCVCPU, cfg.mmu, true),
      DEFINE_PROP_BOOL("pmp", RISCVCPU, cfg.pmp, true),
+    DEFINE_PROP_BOOL("debug", RISCVCPU, cfg.debug, true),
 
      DEFINE_PROP_STRING("priv_spec", RISCVCPU, cfg.priv_spec),

This half of the patch needs to come as the last patch, after you've finished implementation. The first two hunks might as well fold into the first patch.

r~

author
#7 lore raw

PATCH 4/5 target/riscv: csr: Hook debug CSR read/write

In reply to Bin Meng

This adds debug CSR read/write support to the RISC-V CSR RW table.

---

 target/riscv/cpu.c |  6 +++++
 target/riscv/csr.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 63 insertions(+)
target/riscv/cpu.c +6 -0
index 3a2fa97098..45b054b294 100644
@@ -563,6 +563,12 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp)
 
     riscv_cpu_register_gdb_regs_for_features(cs);
 
+#ifndef CONFIG_USER_ONLY
+    if (riscv_feature(env, RISCV_FEATURE_DEBUG)) {
+        riscv_trigger_init(env);
+    }
+#endif
+
     qemu_init_vcpu(cs);
     cpu_reset(cs);
 
target/riscv/csr.c +57 -0

75 lines not on this page · raw message

Signature · 1 line
2.25.1
author
#8 lore raw

PATCH 5/5 hw/core: tcg-cpu-ops.h: Update comments of debug_check_watchpoint()

In reply to Bin Meng

This is now used by RISC-V as well. Update the comments.

---

 include/hw/core/tcg-cpu-ops.h | 1 +
 1 file changed, 1 insertion(+)
include/hw/core/tcg-cpu-ops.h +1 -0
index 6cbe17f2e6..532c148a80 100644
@@ -92,6 +92,7 @@ struct TCGCPUOps {
     /**
      * @debug_check_watchpoint: return true if the architectural
      * watchpoint whose address has matched should really fire, used by ARM
+     * and RISC-V
      */
     bool (*debug_check_watchpoint)(CPUState *cpu, CPUWatchpoint *wp);
 
Signature · 1 line
2.25.1