target/riscv: Initial support for native debug feature via M-mode CSRs
Subsystems (2)
v3 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
Changes in v3:
- drop riscv_trigger_init(), which will be moved to patch #5
- add riscv_trigger_init(), moved from patch #1 to this patch
- enable debug feature by default for all CPUs
Changes in v2:
- new patch: add debug state description
- use 0 instead of GETPC()
- change the config option to 'disabled' by default
Bin Meng (7):
target/riscv: Add initial support for native debug target/riscv: machine: Add debug state description target/riscv: debug: Implement debug related TCGCPUOps target/riscv: cpu: Add a config option for native debug target/riscv: csr: Hook debug CSR read/write target/riscv: cpu: Enable native debug feature
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 | 441 ++++++++++++++++++++++++++++++++++ target/riscv/machine.c | 33 +++ target/riscv/meson.build | 1 + 8 files changed, 668 insertions(+) create mode 100644 target/riscv/debug.h create mode 100644 target/riscv/debug.c
Signature · 1 line
2.25.1
PATCH v3 1/7 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
---
Changes in v3:
- drop riscv_trigger_init(), which will be moved to patch #5
target/riscv/cpu.h | 5 + target/riscv/debug.h | 108 +++++++++++++ target/riscv/debug.c | 339 +++++++++++++++++++++++++++++++++++++++ target/riscv/meson.build | 1 + 4 files changed, 453 insertions(+) create mode 100644 target/riscv/debug.h create mode 100644 target/riscv/debug.c
target/riscv/cpu.h +5 -0
@@ -98,6 +98,7 @@ typedef struct CPURISCVState CPURISCVState;#if !defined(CONFIG_USER_ONLY )#include "pmp.h" +#include "debug.h" #endif #define RV_VLEN_MAX 1024
@@ -234,6 +235,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 +108 -0
108 lines not on this page · raw message
target/riscv/debug.c new file +339 -0
339 lines not on this page · raw message
target/riscv/meson.build +1 -0
@@ -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
PATCH v3 2/7 target/riscv: machine: Add debug state description
In reply to Bin MengAdd a subsection to machine.c to migrate debug CSR state.
---
(no changes since v2)
Changes in v2:
- new patch: add debug state description
target/riscv/machine.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+)
target/riscv/machine.c +33 -0
@@ -164,6 +164,38 @@ static const VMStateDescription vmstate_pointermasking = { } }; +static bool debug_needed (void *opaque) +{ +RISCVCPU *cpu = opaque; +CPURISCVState *env = &cpu->env; + +return riscv_feature (env,RISCV_FEATURE_DEBUG ); +} + +static const VMStateDescription vmstate_debug_type2 = { + .name ="cpu/debug/type2" , + .version_id =1 , + .minimum_version_id =1 , + .fields = (VMStateField []) { +VMSTATE_UINTTL (mcontrol, trigger_type2_t), +VMSTATE_UINTTL (maddress, trigger_type2_t), +VMSTATE_END_OF_LIST () + } +}; + +static const VMStateDescription vmstate_debug = { + .name ="cpu/debug" , + .version_id =1 , + .minimum_version_id =1 , + .needed = debug_needed, + .fields = (VMStateField []) { +VMSTATE_UINTTL (env.trigger_cur,RISCVCPU ), +VMSTATE_STRUCT_ARRAY (env.trigger_type2,RISCVCPU ,TRIGGER_TYPE2_NUM , +0 , vmstate_debug_type2, trigger_type2_t), +VMSTATE_END_OF_LIST () + } +}; +const VMStateDescription vmstate_riscv_cpu = { .name ="cpu" , .version_id =3 ,
@@ -218,6 +250,7 @@ const VMStateDescription vmstate_riscv_cpu = { &vmstate_hyper, &vmstate_vector, &vmstate_pointermasking, + &vmstate_debug, NULL } };
Signature · 1 line
2.25.1
PATCH v3 3/7 target/riscv: debug: Implement debug related TCGCPUOps
In reply to Bin MengImplement .debug_excp_handler, .debug_check_{breakpoint, watchpoint} TCGCPUOps and hook them into riscv_tcg_ops.
---
(no changes since v2)
Changes in v2:
- use 0 instead of GETPC()
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
@@ -105,4 +105,8 @@ void tselect_csr_write(CPURISCVState *env, target_ulong val);target_ulong tdata_csr_read (CPURISCVState *env,int tdata_index);void tdata_csr_write (CPURISCVState *env,int tdata_index,target_ulong val); +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
@@ -705,6 +705,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
PATCH v3 4/7 target/riscv: cpu: Add a config option for native debug
In reply to Bin MengAdd a config option to enable support for native M-mode debug. This is disabled by default and can be enabled with 'debug=true'.
---
(no changes since v2)
Changes in v2:
- change the config option to 'disabled' by default
target/riscv/cpu.h | 2 ++ target/riscv/cpu.c | 5 +++++ 2 files changed, 7 insertions(+)
target/riscv/cpu.h +2 -0
@@ -75,6 +75,7 @@ enum {RISCV_FEATURE_MMU ,RISCV_FEATURE_PMP ,RISCV_FEATURE_EPMP , +RISCV_FEATURE_DEBUG , RISCV_FEATURE_MISA };
@@ -332,6 +333,7 @@ struct RISCVCPU {bool mmu;bool pmp;bool epmp; +bool debug;uint64_t resetvec; } cfg; };
target/riscv/cpu.c +5 -0
@@ -448,6 +448,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. */
@@ -634,6 +638,7 @@ static Property riscv_cpu_properties[] = {DEFINE_PROP_BOOL ("Zfhmin" ,RISCVCPU , cfg.ext_zfhmin, false),DEFINE_PROP_BOOL ("mmu" ,RISCVCPU , cfg.mmu, true),DEFINE_PROP_BOOL ("pmp" ,RISCVCPU , cfg.pmp, true), +DEFINE_PROP_BOOL ("debug" ,RISCVCPU , cfg.debug, false),DEFINE_PROP_STRING ("priv_spec" ,RISCVCPU , cfg.priv_spec), DEFINE_PROP_STRING("vext_spec", RISCVCPU, cfg.vext_spec),
Signature · 1 line
2.25.1
PATCH v3 5/7 target/riscv: csr: Hook debug CSR read/write
In reply to Bin MengThis adds debug CSR read/write support to the RISC-V CSR RW table.
---
Changes in v3:
- add riscv_trigger_init(), moved from patch #1 to this patch
target/riscv/debug.h | 2 ++ target/riscv/cpu.c | 6 +++++ target/riscv/csr.c | 57 ++++++++++++++++++++++++++++++++++++++++++++ target/riscv/debug.c | 27 +++++++++++++++++++++ 4 files changed, 92 insertions(+)
target/riscv/debug.h +2 -0
@@ -109,4 +109,6 @@ 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); +void riscv_trigger_init (CPURISCVState *env); +#endif /* RISCV_DEBUG_H */
target/riscv/cpu.c +6 -0
@@ -575,6 +575,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
target/riscv/debug.c +27 -0
@@ -412,3 +412,30 @@ bool riscv_cpu_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp)return false; } + +void riscv_trigger_init (CPURISCVState *env) +{ +target_ulong type2 =trigger_type (env,TRIGGER_TYPE_AD_MATCH ); +int i; + +/* type 2 triggers */ +for (i =0 ; i <TRIGGER_TYPE2_NUM ; i++) { +/* +* type = TRIGGER_TYPE_AD_MATCH +* dmode = 0 (both debug and M-mode can write tdata) +* maskmax = 0 (unimplemented, always 0) +* sizehi = 0 (match against any size, RV64 only) +* hit = 0 (unimplemented, always 0) +* select = 0 (always 0, perform match on address) +* timing = 0 (always 0, trigger before instruction) +* sizelo = 0 (match against any size) +* action = 0 (always 0, raise a breakpoint exception) +* chain = 0 (unimplemented, always 0) +* match = 0 (always 0, when any compare value equals tdata2) +*/ + env->trigger_type2[i].mcontrol = type2; + env->trigger_type2[i].maddress =0 ; + env->trigger_type2[i].bp =NULL ; + env->trigger_type2[i].wp =NULL ; + } +}
Signature · 1 line
2.25.1
PATCH v3 6/7 target/riscv: cpu: Enable native debug feature
In reply to Bin MengTurn on native debug feature by default for all CPUs.
---
Changes in v3:
- enable debug feature by default for all CPUs
target/riscv/cpu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
target/riscv/cpu.c +1 -1
@@ -644,7 +644,7 @@ static Property riscv_cpu_properties[] = {DEFINE_PROP_BOOL ("Zfhmin" ,RISCVCPU , cfg.ext_zfhmin, false),DEFINE_PROP_BOOL ("mmu" ,RISCVCPU , cfg.mmu, true),DEFINE_PROP_BOOL ("pmp" ,RISCVCPU , cfg.pmp, true), -DEFINE_PROP_BOOL ("debug" ,RISCVCPU , cfg.debug, false), +DEFINE_PROP_BOOL ("debug" ,RISCVCPU , cfg.debug, true),DEFINE_PROP_STRING ("priv_spec" ,RISCVCPU , cfg.priv_spec), DEFINE_PROP_STRING("vext_spec", RISCVCPU, cfg.vext_spec),
Signature · 1 line
2.25.1
PATCH v3 7/7 hw/core: tcg-cpu-ops.h: Update comments of debug_check_watchpoint()
In reply to Bin MengThis is now used by RISC-V as well. Update the comments.
---
(no changes since v1)
include/hw/core/tcg-cpu-ops.h | 1 + 1 file changed, 1 insertion(+)
include/hw/core/tcg-cpu-ops.h +1 -0
@@ -90,6 +90,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