target/riscv: parametrize debug trigger number
v3 cover target/riscv: parametrize debug trigger number
Hi,
In this new version we're adding Phil's comments in v2, in particular the need for an unrealize() callback to free the stuff we're now allocating.
Patches based on alistair/riscv-to-apply.next.
Changes from v2:
- patch 1 (new):
- add RISC-V CPU unrealize callback
- patch 2 (former 1):
- free allocated debug trigger arrays during unrealize()
- change version_id and minimum_version from vmstate_debug
- patch 3 (former 2):
- do not touch vmstate_debug version_id/minimum_version_id
- added Phil's R-b
- v2 link: https://lore.kernel.org/qemu-devel/[email protected]/
Daniel Henrique Barboza (3):
target/riscv/cpu: add CPU unrealize callback target/riscv: dynamic alloc of debug trigger arrays target/riscv: add 'num-triggers' debug property
target/riscv/cpu.c | 26 ++++++++++++++++++++++++ target/riscv/cpu.h | 39 ++++++++++++++++++++++++++++------- target/riscv/csr.c | 2 +- target/riscv/debug.c | 46 ++++++++++++++++++++++++++++++++++-------- target/riscv/debug.h | 1 + target/riscv/machine.c | 16 ++++++++++----- 6 files changed, 109 insertions(+), 21 deletions(-)
Signature · 1 line
2.43.0
PATCH v3 1/3 target/riscv/cpu: add CPU unrealize callback
Next patch is going to dynamically allocate debug trigger arrays during realize() time. We need a way of freeing them during unrealize(), which doesn't exist at this moment.
There's a lot going on in that patch already so we're adding the callback infrastructure beforehand.
--- target/riscv/cpu.c | 9 +++++++++ target/riscv/cpu.h | 2 ++ 2 files changed, 11 insertions(+)
target/riscv/cpu.c +9 -0
@@ -1012,6 +1012,13 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp) mcc->parent_realize (dev, errp); } +static void riscv_cpu_unrealize (DeviceState *dev) +{ +RISCVCPUClass *mcc =RISCV_CPU_GET_CLASS (dev); + + mcc->parent_unrealize (dev); +} +bool riscv_cpu_accelerator_compatible (RISCVCPU *cpu) {if (tcg_enabled ()) {
@@ -2667,6 +2674,8 @@ static void riscv_cpu_common_class_init(ObjectClass *c, const void *data)device_class_set_parent_realize (dc, riscv_cpu_realize, &mcc->parent_realize); +device_class_set_parent_unrealize (dc, riscv_cpu_unrealize, + &mcc->parent_unrealize);resettable_class_set_parent_phases (rc,NULL , riscv_cpu_reset_hold,NULL , &mcc->parent_phases);
target/riscv/cpu.h +2 -0
@@ -575,6 +575,7 @@ typedef struct RISCVCPUDef {/** * RISCVCPUClass: * @parent_realize: The parent class' realize handler. +* @parent_unrealize: The parent class' unrealize handler. * @parent_phases: The parent class' reset phase handlers. * * A RISCV CPU model.
@@ -583,6 +584,7 @@ struct RISCVCPUClass {CPUClass parent_class;DeviceRealize parent_realize; +DeviceUnrealize parent_unrealize;ResettablePhases parent_phases;RISCVCPUDef *def; };
Signature · 1 line
2.43.0
Re: [PATCH v3 1/3] target/riscv/cpu: add CPU unrealize callback
Quoted 7 lines · Daniel Henrique Barboza
Next patch is going to dynamically allocate debug trigger arrays during
realize()time. We need a way of freeing them duringunrealize(), which doesn't exist at this moment.There's a lot going on in that patch already so we're adding the callback infrastructure beforehand.
Quoted 6 lines
Suggested-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Daniel Henrique Barboza <[email protected]>--- target/riscv/cpu.c | 9 +++++++++ target/riscv/cpu.h | 2 ++ 2 files changed, 11 insertions(+)
Re: [PATCH v3 1/3] target/riscv/cpu: add CPU unrealize callback
In reply to Daniel Henrique BarbozaQuoted 10 lines · Daniel Henrique Barboza
Next patch is going to dynamically allocate debug trigger arrays during
realize()time. We need a way of freeing them duringunrealize(), which doesn't exist at this moment.There's a lot going on in that patch already so we're adding the callback infrastructure beforehand.
Suggested-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Daniel Henrique Barboza <[email protected]>
Alistair
Quoted 56 lines
--- target/riscv/cpu.c | 9 +++++++++ target/riscv/cpu.h | 2 ++ 2 files changed, 11 insertions(+)target/riscv/cpu.c +9
-0@@ -1012,6 +1012,13 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp) mcc->parent_realize (dev, errp); } +static void riscv_cpu_unrealize (DeviceState *dev) +{ +RISCVCPUClass *mcc =RISCV_CPU_GET_CLASS (dev); + + mcc->parent_unrealize (dev); +} +bool riscv_cpu_accelerator_compatible (RISCVCPU *cpu) {if (tcg_enabled ()) {@@ -2667,6 +2674,8 @@ static void riscv_cpu_common_class_init(ObjectClass *c, const void *data)device_class_set_parent_realize (dc, riscv_cpu_realize, &mcc->parent_realize); +device_class_set_parent_unrealize (dc, riscv_cpu_unrealize, + &mcc->parent_unrealize);resettable_class_set_parent_phases (rc,NULL , riscv_cpu_reset_hold,NULL , &mcc->parent_phases);target/riscv/cpu.h +2
-1@@ -575,6 +575,7 @@ typedef struct RISCVCPUDef {/** * RISCVCPUClass: * @parent_realize: The parent class' realize handler. +* @parent_unrealize: The parent class' unrealize handler. * @parent_phases: The parent class' reset phase handlers. * * A RISCV CPU model. @@ -583,6 +584,7 @@ struct RISCVCPUClass {CPUClass parent_class;DeviceRealize parent_realize; +DeviceUnrealize parent_unrealize;ResettablePhases parent_phases;RISCVCPUDef *def; }; --2.43.0
PATCH v3 2/3 target/riscv: dynamic alloc of debug trigger arrays
In reply to Daniel Henrique BarbozaThe debug trigger facility consists of a set of arrays: tdata1-3, cpu_breakpoint, cpu_watchpoint and itrigger_timer. All of them are static allocated with RV_MAX_TRIGGERS (2). This means that all RISC-V cpus will have 2 triggers per hart.
The RISC-V Server Ref demands at least 11 triggers per hart, and several CPUs in the wild works with 4+ triggers. We need more flexibility, ergo we need to parametrize the amount of triggers and make it configurable.
Before doing that we need to handle a situation faced in a previous attempt [1]. We were unable to set the tdataN array length in vmstate_debug, meaning that we would always migrate RV_MAX_TRIGGERS regardless of the actual amount of triggers in play. To fix that we need to change the tdata arrays from static to dynamic, allowing us to use VMSTATE_VARRAY_UINT32(). This also means that, in contrast with [1], we have the opportunity to turn all trigger arrays into dynamic allocation and reduce the amount of stuff being carried by CPURISCVState, or in other words, we can carry just what we're using instead of a static max value.
All the forementioned trigger facility arrays are now dynamic. They are allocated and freed during realize/unrealize, and their size is expressed by env->num_triggers. All relevant code is changed to use env->num_triggers instead of the RV_MAX_TRIGGERS to loop through each array.
This will make it easier for the next patch to parametrize env->num_triggers.
[1] https://lore.kernel.org/qemu-devel/[email protected]/
--- target/riscv/cpu.c | 6 ++++++ target/riscv/cpu.h | 18 +++++++++++------ target/riscv/csr.c | 2 +- target/riscv/debug.c | 46 ++++++++++++++++++++++++++++++++++-------- target/riscv/debug.h | 1 + target/riscv/machine.c | 16 ++++++++++----- 6 files changed, 69 insertions(+), 20 deletions(-)
target/riscv/cpu.c +6 -0
@@ -1015,7 +1015,13 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp)static void riscv_cpu_unrealize (DeviceState *dev) {RISCVCPUClass *mcc =RISCV_CPU_GET_CLASS (dev); +#ifndef CONFIG_USER_ONLY +RISCVCPU *cpu =RISCV_CPU (dev); +if (cpu->cfg.debug) { +riscv_trigger_unrealize (&cpu->env); + } +#endif mcc->parent_unrealize (dev); }
target/riscv/cpu.h +12 -6
@@ -467,12 +467,18 @@ struct CPUArchState {/* trigger module */ uint16_t mcontext;uint8_t trigger_cur; -uint64_t tdata1[RV_MAX_TRIGGERS ]; -uint64_t tdata2[RV_MAX_TRIGGERS ]; -uint64_t tdata3[RV_MAX_TRIGGERS ]; -struct CPUBreakpoint *cpu_breakpoint[RV_MAX_TRIGGERS ]; -struct CPUWatchpoint *cpu_watchpoint[RV_MAX_TRIGGERS ]; -QEMUTimer *itrigger_timer[RV_MAX_TRIGGERS ]; +/* +* num_triggers is the length of tdata1, tdata2, tdata3, +* cpu_breakpoint, cpu_watchpoint and itrigger_timer +* arrays. +*/ +uint32_t num_triggers; +uint64_t *tdata1; +uint64_t *tdata2; +uint64_t *tdata3; +struct CPUBreakpoint **cpu_breakpoint; +struct CPUWatchpoint **cpu_watchpoint; +QEMUTimer **itrigger_timer;int64_t last_icount;bool itrigger_enabled;
target/riscv/csr.c +1 -1
@@ -5436,7 +5436,7 @@ static RISCVException read_tdata(CPURISCVState *env, int csrno, target_ulong *val) {/* return 0 in tdata1 to end the trigger enumeration */ -if (env->trigger_cur >=RV_MAX_TRIGGERS && csrno ==CSR_TDATA1 ) { +if (env->trigger_cur >= env->num_triggers && csrno ==CSR_TDATA1 ) { *val =0 ;return RISCV_EXCP_NONE ; }
target/riscv/debug.c +38 -8
99 lines not on this page · raw message
target/riscv/debug.h +1 -0
@@ -148,6 +148,7 @@ bool riscv_cpu_debug_check_breakpoint(CPUState *cs);bool riscv_cpu_debug_check_watchpoint (CPUState *cs,CPUWatchpoint *wp);void riscv_trigger_realize (CPURISCVState *env); +void riscv_trigger_unrealize (CPURISCVState *env);void riscv_trigger_reset_hold (CPURISCVState *env);bool riscv_itrigger_enabled (CPURISCVState *env);
target/riscv/machine.c +11 -5
@@ -240,16 +240,22 @@ static int debug_post_load(void *opaque, int version_id)static const VMStateDescription vmstate_debug = { .name ="cpu/debug" , - .version_id =3 , - .minimum_version_id =3 , + .version_id =4 , + .minimum_version_id =4 , .needed = debug_needed, .post_load = debug_post_load, .fields = (const VMStateField []) {VMSTATE_UINT16 (env.mcontext,RISCVCPU ),VMSTATE_UINT8 (env.trigger_cur,RISCVCPU ), -VMSTATE_UINT64_ARRAY (env.tdata1,RISCVCPU ,RV_MAX_TRIGGERS ), -VMSTATE_UINT64_ARRAY (env.tdata2,RISCVCPU ,RV_MAX_TRIGGERS ), -VMSTATE_UINT64_ARRAY (env.tdata3,RISCVCPU ,RV_MAX_TRIGGERS ), +VMSTATE_VARRAY_UINT32 (env.tdata1,RISCVCPU , + env.num_triggers,0 , + vmstate_info_uint64, uint64_t), +VMSTATE_VARRAY_UINT32 (env.tdata2,RISCVCPU , + env.num_triggers,0 , + vmstate_info_uint64, uint64_t), +VMSTATE_VARRAY_UINT32 (env.tdata3,RISCVCPU , + env.num_triggers,0 , + vmstate_info_uint64, uint64_t),VMSTATE_END_OF_LIST () } };
Signature · 1 line
2.43.0
Re: [PATCH v3 2/3] target/riscv: dynamic alloc of debug trigger arrays
Quoted 32 lines · Daniel Henrique Barboza
The debug trigger facility consists of a set of arrays: tdata1-3,
cpu_breakpoint,cpu_watchpointanditrigger_timer. All of them are static allocated withRV_MAX_TRIGGERS(2). This means that all RISC-V cpus will have 2 triggers per hart.The RISC-V Server Ref demands at least 11 triggers per hart, and several CPUs in the wild works with 4+ triggers. We need more flexibility, ergo we need to parametrize the amount of triggers and make it configurable.
Before doing that we need to handle a situation faced in a previous attempt [1]. We were unable to set the tdataN array length in
vmstate_debug, meaning that we would always migrateRV_MAX_TRIGGERSregardless of the actual amount of triggers in play. To fix that we need to change the tdata arrays from static to dynamic, allowing us to useVMSTATE_VARRAY_UINT32(). This also means that, in contrast with [1], we have the opportunity to turn all trigger arrays into dynamic allocation and reduce the amount of stuff being carried by CPURISCVState, or in other words, we can carry just what we're using instead of a static max value.All the forementioned trigger facility arrays are now dynamic. They are allocated and freed during realize/unrealize, and their size is expressed by
env->num_triggers. All relevant code is changed to useenv->num_triggersinstead of theRV_MAX_TRIGGERSto loop through each array.This will make it easier for the next patch to parametrize
env->num_triggers.[1] https://lore.kernel.org/qemu-devel/[email protected]/
Signed-off-by Daniel Henrique Barboza <[email protected]>
Quoted 8 lines
--- target/riscv/cpu.c | 6 ++++++ target/riscv/cpu.h | 18 +++++++++++------ target/riscv/csr.c | 2 +- target/riscv/debug.c | 46 ++++++++++++++++++++++++++++++++++-------- target/riscv/debug.h | 1 + target/riscv/machine.c | 16 ++++++++++----- 6 files changed, 69 insertions(+), 20 deletions(-)
Re: [PATCH v3 2/3] target/riscv: dynamic alloc of debug trigger arrays
In reply to Daniel Henrique BarbozaQuoted 27 lines · Daniel Henrique Barboza
The debug trigger facility consists of a set of arrays: tdata1-3,
cpu_breakpoint,cpu_watchpointanditrigger_timer. All of them are static allocated withRV_MAX_TRIGGERS(2). This means that all RISC-V cpus will have 2 triggers per hart.The RISC-V Server Ref demands at least 11 triggers per hart, and several CPUs in the wild works with 4+ triggers. We need more flexibility, ergo we need to parametrize the amount of triggers and make it configurable.
Before doing that we need to handle a situation faced in a previous attempt [1]. We were unable to set the tdataN array length in
vmstate_debug, meaning that we would always migrateRV_MAX_TRIGGERSregardless of the actual amount of triggers in play. To fix that we need to change the tdata arrays from static to dynamic, allowing us to useVMSTATE_VARRAY_UINT32(). This also means that, in contrast with [1], we have the opportunity to turn all trigger arrays into dynamic allocation and reduce the amount of stuff being carried by CPURISCVState, or in other words, we can carry just what we're using instead of a static max value.All the forementioned trigger facility arrays are now dynamic. They are allocated and freed during realize/unrealize, and their size is expressed by
env->num_triggers. All relevant code is changed to useenv->num_triggersinstead of theRV_MAX_TRIGGERSto loop through each array.
Quoted 6 lines
This will make it easier for the next patch to parametrize
env->num_triggers.[1] https://lore.kernel.org/qemu-devel/[email protected]/
Signed-off-by Daniel Henrique Barboza <[email protected]>
Alistair
Quoted 227 lines
--- target/riscv/cpu.c | 6 ++++++ target/riscv/cpu.h | 18 +++++++++++------ target/riscv/csr.c | 2 +- target/riscv/debug.c | 46 ++++++++++++++++++++++++++++++++++-------- target/riscv/debug.h | 1 + target/riscv/machine.c | 16 ++++++++++----- 6 files changed, 69 insertions(+), 20 deletions(-)target/riscv/cpu.c +6
-0@@ -1015,7 +1015,13 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp)static void riscv_cpu_unrealize (DeviceState *dev) {RISCVCPUClass *mcc =RISCV_CPU_GET_CLASS (dev); +#ifndef CONFIG_USER_ONLY +RISCVCPU *cpu =RISCV_CPU (dev); +if (cpu->cfg.debug) { +riscv_trigger_unrealize (&cpu->env); + } +#endif mcc->parent_unrealize (dev); }target/riscv/cpu.h +12
-6@@ -467,12 +467,18 @@ struct CPUArchState {/* trigger module */ uint16_t mcontext;uint8_t trigger_cur; -uint64_t tdata1[RV_MAX_TRIGGERS ]; -uint64_t tdata2[RV_MAX_TRIGGERS ]; -uint64_t tdata3[RV_MAX_TRIGGERS ]; -struct CPUBreakpoint *cpu_breakpoint[RV_MAX_TRIGGERS ]; -struct CPUWatchpoint *cpu_watchpoint[RV_MAX_TRIGGERS ]; -QEMUTimer *itrigger_timer[RV_MAX_TRIGGERS ]; +/* +* num_triggers is the length of tdata1, tdata2, tdata3, +* cpu_breakpoint, cpu_watchpoint and itrigger_timer +* arrays. +*/ +uint32_t num_triggers; +uint64_t *tdata1; +uint64_t *tdata2; +uint64_t *tdata3; +struct CPUBreakpoint **cpu_breakpoint; +struct CPUWatchpoint **cpu_watchpoint; +QEMUTimer **itrigger_timer;int64_t last_icount;bool itrigger_enabled;target/riscv/csr.c +1
-1@@ -5436,7 +5436,7 @@ static RISCVException read_tdata(CPURISCVState *env, int csrno, target_ulong *val) {/* return 0 in tdata1 to end the trigger enumeration */ -if (env->trigger_cur >=RV_MAX_TRIGGERS && csrno ==CSR_TDATA1 ) { +if (env->trigger_cur >= env->num_triggers && csrno ==CSR_TDATA1 ) { *val =0 ;return RISCV_EXCP_NONE ; }target/riscv/debug.c +38
-8@@ -172,7 +172,7 @@ target_ulong tselect_csr_read(CPURISCVState *env)void tselect_csr_write (CPURISCVState *env,target_ulong val) { -if (val <RV_MAX_TRIGGERS ) { +if (val < env->num_triggers) { env->trigger_cur = val; } }@@ -701,7 +701,7 @@ static bool check_itrigger_priv(CPURISCVState *env, int index)bool riscv_itrigger_enabled (CPURISCVState *env) {int count; -for (int i =0 ; i <RV_MAX_TRIGGERS ; i++) { +for (int i =0 ; i < env->num_triggers; i++) {if (get_trigger_type (env, i) !=TRIGGER_TYPE_INST_CNT ) {continue ; }@@ -721,7 +721,7 @@ bool riscv_itrigger_enabled(CPURISCVState *env)void helper_itrigger_match (CPURISCVState *env) {int count; -for (int i =0 ; i <RV_MAX_TRIGGERS ; i++) { +for (int i =0 ; i < env->num_triggers; i++) {if (get_trigger_type (env, i) !=TRIGGER_TYPE_INST_CNT ) {continue ; }@@ -750,7 +750,7 @@ static void riscv_itrigger_update_count(CPURISCVState *env)int64_t last_icount = env->last_icount, current_icount; current_icount = env->last_icount =icount_get_raw (); -for (int i =0 ; i <RV_MAX_TRIGGERS ; i++) { +for (int i =0 ; i < env->num_triggers; i++) {if (get_trigger_type (env, i) !=TRIGGER_TYPE_INST_CNT ) {continue ; }@@ -950,7 +950,7 @@ bool riscv_cpu_debug_check_breakpoint(CPUState *cs)int i; QTAILQ_FOREACH(bp, &cs->breakpoints, entry) { -for (i =0 ; i <RV_MAX_TRIGGERS ; i++) { +for (i =0 ; i < env->num_triggers; i++) { trigger_type =get_trigger_type (env, i);if (!trigger_common_match (env, trigger_type, i)) {@@ -996,7 +996,7 @@ bool riscv_cpu_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp)int flags;int i; -for (i =0 ; i <RV_MAX_TRIGGERS ; i++) { +for (i =0 ; i < env->num_triggers; i++) { trigger_type =get_trigger_type (env, i);if (!trigger_common_match (env, trigger_type, i)) {@@ -1049,19 +1049,49 @@ void riscv_trigger_realize(CPURISCVState *env) {int i; -for (i =0 ; i <RV_MAX_TRIGGERS ; i++) { +/* +* Alloc env->tdata1/2/3, cpu_breakpoint, cpu_watchpoint and +* itrigger_timer dynamically. This is overkill now +* given that they could be static arrays with RV_MAX_TRIGGERS +* but we'll parametrize the trigger number later, i.e. the +* array length won't be static. +*/ + env->num_triggers =RV_MAX_TRIGGERS ; + env->tdata1 =g_new0 (uint64_t, env->num_triggers); + env->tdata2 =g_new0 (uint64_t, env->num_triggers); + env->tdata3 =g_new0 (uint64_t, env->num_triggers); + env->cpu_breakpoint = g_new0(struct CPUBreakpoint *, env->num_triggers); + env->cpu_watchpoint = g_new0(struct CPUWatchpoint *, env->num_triggers); + env->itrigger_timer = g_new0(QEMUTimer *, env->num_triggers); + +for (i =0 ; i < env->num_triggers; i++) { env->itrigger_timer[i] =timer_new_ns (QEMU_CLOCK_VIRTUAL , riscv_itrigger_timer_cb, env); } } +void riscv_trigger_unrealize (CPURISCVState *env) +{ +g_free (env->tdata1); +g_free (env->tdata2); +g_free (env->tdata3); + +g_free (env->cpu_breakpoint); +g_free (env->cpu_watchpoint); + +for (int i =0 ; i < env->num_triggers; i++) { +timer_del (env->itrigger_timer[i]); + } +g_free (env->itrigger_timer); +} +void riscv_trigger_reset_hold (CPURISCVState *env) {target_ulong tdata1 =build_tdata1 (env,TRIGGER_TYPE_AD_MATCH ,0 ,0 );int i;/* init to type 2 triggers */ -for (i =0 ; i <RV_MAX_TRIGGERS ; i++) { +for (i =0 ; i < env->num_triggers; i++) {/* * type = TRIGGER_TYPE_AD_MATCH * dmode = 0 (both debug and M-mode can write tdata) target/riscv/debug.h +1
-0@@ -148,6 +148,7 @@ bool riscv_cpu_debug_check_breakpoint(CPUState *cs);bool riscv_cpu_debug_check_watchpoint (CPUState *cs,CPUWatchpoint *wp);void riscv_trigger_realize (CPURISCVState *env); +void riscv_trigger_unrealize (CPURISCVState *env);void riscv_trigger_reset_hold (CPURISCVState *env);bool riscv_itrigger_enabled (CPURISCVState *env);target/riscv/machine.c +11
-6@@ -240,16 +240,22 @@ static int debug_post_load(void *opaque, int version_id)static const VMStateDescription vmstate_debug = { .name ="cpu/debug" , - .version_id =3 , - .minimum_version_id =3 , + .version_id =4 , + .minimum_version_id =4 , .needed = debug_needed, .post_load = debug_post_load, .fields = (const VMStateField []) {VMSTATE_UINT16 (env.mcontext,RISCVCPU ),VMSTATE_UINT8 (env.trigger_cur,RISCVCPU ), -VMSTATE_UINT64_ARRAY (env.tdata1,RISCVCPU ,RV_MAX_TRIGGERS ), -VMSTATE_UINT64_ARRAY (env.tdata2,RISCVCPU ,RV_MAX_TRIGGERS ), -VMSTATE_UINT64_ARRAY (env.tdata3,RISCVCPU ,RV_MAX_TRIGGERS ), +VMSTATE_VARRAY_UINT32 (env.tdata1,RISCVCPU , + env.num_triggers,0 , + vmstate_info_uint64, uint64_t), +VMSTATE_VARRAY_UINT32 (env.tdata2,RISCVCPU , + env.num_triggers,0 , + vmstate_info_uint64, uint64_t), +VMSTATE_VARRAY_UINT32 (env.tdata3,RISCVCPU , + env.num_triggers,0 , + vmstate_info_uint64, uint64_t),VMSTATE_END_OF_LIST () } }; --2.43.0
PATCH v3 3/3 target/riscv: add 'num-triggers' debug property
In reply to Daniel Henrique BarbozaAll CPUs have the same amount of triggers: 2 triggers per hart, set via RV_MAX_TRIGGERS. This is not enough anymore: we'll have at least one future CPU that will demand more triggers per hart when implementing the RISC-V Server Ref Platform, requiring at least 11 triggers per hart.
Parametrize the trigger amount using a new 'num_triggers' property. The default amount is kept at 2 for backwards compatibility. The new maximum is bumped to a generous 1024 triggers per hart, which hopefully will be enough for the foreseeable future.
The property can be set in two ways:
- a '.num_triggers' CPU definition flag, allowing CPUs to set a custom amount inside the CPU def in DEFINE_RISCV_CPU();
- a new 'num-triggers' user property. The user property has a higher priority than an existing '.num_triggers' CPU def setting.
Assuming a hypothetical case where a CPU 'X' is defined with '.num_triggers = 8':
- -cpu X,num-triggers=30 => num_triggers set to 30
- -cpu X (...) => num_triggers set to 8
For a CPU that doesn't set '.num_triggers':
- -cpu rv64,num-triggers=30 => num_triggers set to 30
- -cpu rv64 (...) => num_triggers set to 2
--- target/riscv/cpu.c | 11 +++++++++++ target/riscv/cpu.h | 19 ++++++++++++++++++- target/riscv/debug.c | 16 ++++++++-------- 3 files changed, 37 insertions(+), 9 deletions(-)
target/riscv/cpu.c +11 -0
@@ -1167,6 +1167,11 @@ static void riscv_cpu_init(Object *obj) IRQ_LOCAL_MAX + IRQ_LOCAL_GUEST_MAX);qdev_init_gpio_in_named (DEVICE (cpu), riscv_cpu_set_nmi,"riscv.cpu.rnmi" ,RNMI_MAX ); + +if (mcc->def->num_triggers) { + env->num_triggers = mcc->def->num_triggers; + } +#endif /* CONFIG_USER_ONLY */ general_user_opts =g_hash_table_new (g_str_hash, g_str_equal);
@@ -2619,6 +2624,8 @@ static const Property riscv_cpu_properties[] = { DEFAULT_RNMI_IRQVEC),DEFINE_PROP_UINT64 ("rnmi-exception-vector" ,RISCVCPU , env.rnmi_excpvec,DEFAULT_RNMI_EXCPVEC ), +DEFINE_PROP_UINT32 ("num-triggers" ,RISCVCPU , env.num_triggers, +RV_DEFAULT_NUM_TRIGGERS ), #endif DEFINE_PROP_BOOL("short-isa-string", RISCVCPU, cfg.short_isa_string, false),
@@ -2764,6 +2771,10 @@ static void riscv_cpu_class_base_init(ObjectClass *c, const void *data) !valid_vm_1_10_32[mcc->def->cfg.max_satp_mode]) { mcc->def->cfg.max_satp_mode =VM_1_10_SV32 ; } + +if (def->num_triggers) { + mcc->def->num_triggers = def->num_triggers; + }#endif }if (def->priv_spec !=RISCV_PROFILE_ATTR_UNUSED ) {
target/riscv/cpu.h +18 -1
@@ -189,7 +189,22 @@ extern RISCVCPUImpliedExtsRule *riscv_multi_ext_implied_rules[];#define RV_VLEN_MAX 1024#define RV_MAX_MHPMEVENTS 32#define RV_MAX_MHPMCOUNTERS 32 -#define RV_MAX_TRIGGERS 2 + +/* +* The Debug 1.0 spec allows a humongous amount of triggers. Section +* "Enumeration" says: "The above algorithm reads back tselect so that +* implementations which have 2^n triggers only need to implement n +* bits of tselect.". tselect can have up to XLEN bits, so the max +* theoretical RV_MAX_TRIGGERS value is 2^XLEN. +* +* Allowing 2^XLEN triggers per hart is silly so we'll set a max to a +* modest 1024 triggers, which is way more than what we see current +* hardware use (most chips uses 2-4 triggers per hart, RISC-V Server +* Ref requires at least 11). With a 1024 max per hart we'll be set +* for a long time ... hopefully. +*/ +#define RV_MAX_TRIGGERS 1024 +#define RV_DEFAULT_NUM_TRIGGERS 2 FIELD(VTYPE, VLMUL, 0, 3)FIELD (VTYPE ,VSEW ,3 ,3 )
@@ -576,6 +591,8 @@ typedef struct RISCVCPUDef {RISCVCPUConfig cfg;bool bare;const RISCVCSR *custom_csrs; +/* This is just a setter for env->num_triggers. */ +uint32_t num_triggers; } RISCVCPUDef;/**
target/riscv/debug.c +8 -8
@@ -26,6 +26,7 @@#include "qemu/osdep.h" #include "qemu/log.h" #include "qapi/error.h" +#include "qemu/error-report.h" #include "cpu.h" #include "target/riscv/debug.h" #include "trace.h"
@@ -1049,14 +1050,13 @@ void riscv_trigger_realize(CPURISCVState *env) {int i; -/* -* Alloc env->tdata1/2/3, cpu_breakpoint, cpu_watchpoint and -* itrigger_timer dynamically. This is overkill now -* given that they could be static arrays with RV_MAX_TRIGGERS -* but we'll parametrize the trigger number later, i.e. the -* array length won't be static. -*/ - env->num_triggers =RV_MAX_TRIGGERS ; +if (env->num_triggers >RV_MAX_TRIGGERS ) { +error_report ( +"Invalid configuration: 'num-triggers' must be less than %u" , +RV_MAX_TRIGGERS ); +exit (1 ); + } + env->tdata1 =g_new0 (uint64_t, env->num_triggers); env->tdata2 =g_new0 (uint64_t, env->num_triggers); env->tdata3 =g_new0 (uint64_t, env->num_triggers);
Signature · 1 line
2.43.0
Re: [PATCH v3 3/3] target/riscv: add 'num-triggers' debug property
Quoted 24 lines · Daniel Henrique Barboza
All CPUs have the same amount of triggers: 2 triggers per hart, set via
RV_MAX_TRIGGERS. This is not enough anymore: we'll have at least one future CPU that will demand more triggers per hart when implementing the RISC-V Server Ref Platform, requiring at least 11 triggers per hart.Parametrize the trigger amount using a new '
num_triggers' property. The default amount is kept at 2 for backwards compatibility. The new maximum is bumped to a generous 1024 triggers per hart, which hopefully will be enough for the foreseeable future.The property can be set in two ways:
- a '.num_triggers' CPU definition flag, allowing CPUs to set a custom amount inside the CPU def in
DEFINE_RISCV_CPU();
- a new 'num-triggers' user property. The user property has a higher priority than an existing '.num_triggers' CPU def setting.Assuming a hypothetical case where a CPU 'X' is defined with '.num_triggers = 8':
- -cpu X,num-triggers=30 =>
num_triggersset to 30
- -cpu X (...) =>num_triggersset to 8
Quoted 7 lines
For a CPU that doesn't set '.num_triggers':
- -cpu rv64,num-triggers=30 =>
num_triggersset to 30
- -cpu rv64 (...) =>num_triggersset to 2Signed-off-by Daniel Henrique Barboza <[email protected]>Reviewed-by Philippe Mathieu-Daudé <[email protected]>
Alistair
Quoted 117 lines
--- target/riscv/cpu.c | 11 +++++++++++ target/riscv/cpu.h | 19 ++++++++++++++++++- target/riscv/debug.c | 16 ++++++++-------- 3 files changed, 37 insertions(+), 9 deletions(-)target/riscv/cpu.c +11
-0@@ -1167,6 +1167,11 @@ static void riscv_cpu_init(Object *obj) IRQ_LOCAL_MAX + IRQ_LOCAL_GUEST_MAX);qdev_init_gpio_in_named (DEVICE (cpu), riscv_cpu_set_nmi,"riscv.cpu.rnmi" ,RNMI_MAX ); + +if (mcc->def->num_triggers) { + env->num_triggers = mcc->def->num_triggers; + } +#endif /* CONFIG_USER_ONLY */ general_user_opts =g_hash_table_new (g_str_hash, g_str_equal);@@ -2619,6 +2624,8 @@ static const Property riscv_cpu_properties[] = { DEFAULT_RNMI_IRQVEC),DEFINE_PROP_UINT64 ("rnmi-exception-vector" ,RISCVCPU , env.rnmi_excpvec,DEFAULT_RNMI_EXCPVEC ), +DEFINE_PROP_UINT32 ("num-triggers" ,RISCVCPU , env.num_triggers, +RV_DEFAULT_NUM_TRIGGERS ), #endif DEFINE_PROP_BOOL("short-isa-string", RISCVCPU, cfg.short_isa_string, false),@@ -2764,6 +2771,10 @@ static void riscv_cpu_class_base_init(ObjectClass *c, const void *data) !valid_vm_1_10_32[mcc->def->cfg.max_satp_mode]) { mcc->def->cfg.max_satp_mode =VM_1_10_SV32 ; } + +if (def->num_triggers) { + mcc->def->num_triggers = def->num_triggers; + }#endif }if (def->priv_spec !=RISCV_PROFILE_ATTR_UNUSED ) {target/riscv/cpu.h +18
-1@@ -189,7 +189,22 @@ extern RISCVCPUImpliedExtsRule *riscv_multi_ext_implied_rules[];#define RV_VLEN_MAX 1024#define RV_MAX_MHPMEVENTS 32#define RV_MAX_MHPMCOUNTERS 32 -#define RV_MAX_TRIGGERS 2 + +/* +* The Debug 1.0 spec allows a humongous amount of triggers. Section +* "Enumeration" says: "The above algorithm reads back tselect so that +* implementations which have 2^n triggers only need to implement n +* bits of tselect.". tselect can have up to XLEN bits, so the max +* theoretical RV_MAX_TRIGGERS value is 2^XLEN. +* +* Allowing 2^XLEN triggers per hart is silly so we'll set a max to a +* modest 1024 triggers, which is way more than what we see current +* hardware use (most chips uses 2-4 triggers per hart, RISC-V Server +* Ref requires at least 11). With a 1024 max per hart we'll be set +* for a long time ... hopefully. +*/ +#define RV_MAX_TRIGGERS 1024 +#define RV_DEFAULT_NUM_TRIGGERS 2 FIELD(VTYPE, VLMUL, 0, 3)FIELD (VTYPE ,VSEW ,3 ,3 )@@ -576,6 +591,8 @@ typedef struct RISCVCPUDef {RISCVCPUConfig cfg;bool bare;const RISCVCSR *custom_csrs; +/* This is just a setter for env->num_triggers. */ +uint32_t num_triggers; } RISCVCPUDef;/** target/riscv/debug.c +8
-9@@ -26,6 +26,7 @@#include "qemu/osdep.h" #include "qemu/log.h" #include "qapi/error.h" +#include "qemu/error-report.h" #include "cpu.h" #include "target/riscv/debug.h" #include "trace.h" @@ -1049,14 +1050,13 @@ void riscv_trigger_realize(CPURISCVState *env) {int i; -/* -* Alloc env->tdata1/2/3, cpu_breakpoint, cpu_watchpoint and -* itrigger_timer dynamically. This is overkill now -* given that they could be static arrays with RV_MAX_TRIGGERS -* but we'll parametrize the trigger number later, i.e. the -* array length won't be static. -*/ - env->num_triggers =RV_MAX_TRIGGERS ; +if (env->num_triggers >RV_MAX_TRIGGERS ) { +error_report ( +"Invalid configuration: 'num-triggers' must be less than %u" , +RV_MAX_TRIGGERS ); +exit (1 ); + } + env->tdata1 =g_new0 (uint64_t, env->num_triggers); env->tdata2 =g_new0 (uint64_t, env->num_triggers); env->tdata3 =g_new0 (uint64_t, env->num_triggers); --2.43.0
Quoted 8 lines · Daniel Henrique Barboza
Hi,
In this new version we're adding Phil's comments in v2, in particular the need for an
unrealize()callback to free the stuff we're now allocating.Patches based on alistair/riscv-to-apply.next.
Hmm... I can't get this to apply. Mind resending?
Alistair
Quoted 30 lines
Changes from v2:
- patch 1 (new):
- add RISC-V CPU unrealize callback
- patch 2 (former 1):
- free allocated debug trigger arrays duringunrealize()
- changeversion_idandminimum_versionfromvmstate_debug
- patch 3 (former 2):
- do not touchvmstate_debugversion_id/minimum_version_id
- added Phil's R-b
- v2 link: https://lore.kernel.org/qemu-devel/[email protected]/Daniel Henrique Barboza (3):
target/riscv/cpu: add CPU unrealize callback target/riscv: dynamic alloc of debug trigger arrays target/riscv: add 'num-triggers' debug propertytarget/riscv/cpu.c | 26 ++++++++++++++++++++++++ target/riscv/cpu.h | 39 ++++++++++++++++++++++++++++------- target/riscv/csr.c | 2 +- target/riscv/debug.c | 46 ++++++++++++++++++++++++++++++++++-------- target/riscv/debug.h | 1 + target/riscv/machine.c | 16 ++++++++++----- 6 files changed, 109 insertions(+), 21 deletions(-)--
2.43.0
Quoted 5 lines · Alistair Francis
Quoted 3 lines · Daniel Henrique Barboza
Hi,
Quoted 7 lines
Quoted 5 lines
In this new version we're adding Phil's comments in v2, in particular the need for an
unrealize()callback to free the stuff we're now allocating.Patches based on alistair/riscv-to-apply.next.
Hmm... I can't get this to apply. Mind resending?
Sure thing. I'll rebase and send a v4.
Cheers,
Daniel
Quoted 33 lines
Alistair
Quoted 30 lines
Changes from v2:
- patch 1 (new):
- add RISC-V CPU unrealize callback
- patch 2 (former 1):
- free allocated debug trigger arrays duringunrealize()
- changeversion_idandminimum_versionfromvmstate_debug
- patch 3 (former 2):
- do not touchvmstate_debugversion_id/minimum_version_id
- added Phil's R-b
- v2 link: https://lore.kernel.org/qemu-devel/[email protected]/Daniel Henrique Barboza (3):
target/riscv/cpu: add CPU unrealize callback target/riscv: dynamic alloc of debug trigger arrays target/riscv: add 'num-triggers' debug propertytarget/riscv/cpu.c | 26 ++++++++++++++++++++++++ target/riscv/cpu.h | 39 ++++++++++++++++++++++++++++------- target/riscv/csr.c | 2 +- target/riscv/debug.c | 46 ++++++++++++++++++++++++++++++++++-------- target/riscv/debug.h | 1 + target/riscv/machine.c | 16 ++++++++++----- 6 files changed, 109 insertions(+), 21 deletions(-)--
2.43.0