RISC-V Sspesa: QEMU model support
v2 cover RISC-V Sspesa: QEMU model support
This QEMU series adds support for the Sspesa (Precise Event Sample Attribution) extension.
The first patch adds the 'sspesa' and 'ssplcofi' CPU properties, the ISA string/DT entry, and the shpmspc/shpmsdata CSRs. It also adds dependency of Sspesa on Sscofpmf and of Ssplcofi on Sspesa.
The second patch captures the overflowing counter ID, priv mode and pc for the first counter overflow.
Note:
QEMU does not currently identify any event as supporting precise attribution. The QEMU setup is intended to validate the end-to-end Sspesa plumbing only. Similarly, the pesa=1 raw-event format bit in Linux is used to simulate an event that supports precise attribution. Such events are expected to be defined in vendor/platform specific JSON event files, but this series does not add such an event file for the 'Virt' machine.
Reproducing the full setup:
Sspesa support requires Linux, QEMU and OpenSBI. The matching v2 branches are:
Linux: https://github.com/mdchitale/linux/tree/riscv_sspesa_rfc_v2
QEMU: https://github.com/mdchitale/qemu/tree/riscv_sspesa_rfc_v2
OpenSBI must be built with the following fix so that PMU overflow interrupts work correctly:
https://lists.infradead.org/pipermail/opensbi/2026-October/010957.html
Build Linux and QEMU from the branches above, and build OpenSBI with this fix applied. By default QEMU doesn't define a raw event that can be mapped to programmable counters and hence a special test DTB is required which includes the raw-event-to-counter mapping.
Build the test DTB:
- qemu-system-riscv64 -machine virt,dumpdtb=/tmp/virt-sspesa.dtb \
-cpu rv64,sspesa=true,ssplcofi=true,ssccfg=true,sscofpmf=true
- dtc -I dtb -O dts -o /tmp/virt-sspesa.dts /tmp/virt-sspesa.dtb
- Edit /tmp/virt-sspesa.dts and add the following property to the
`/pmu` node:
riscv,raw-event-to-mhpmcounters = <0x0 0x2 0x0 0xffffffffffff 0x7fff8>; - dtc -I dts -O dtb -o /tmp/virt-sspesa-test.dtb /tmp/virt-sspesa.dts
Boot Linux with the test DTB:
- qemu-system-riscv64 -nographic -machine virt \
-cpu rv64,sspesa=true,ssplcofi=true,ssccfg=true,sscofpmf=true \
-bios /path/to/fw_dynamic.bin -kernel /path/to/Image \
-drive file=/path/to/rootfs.img,id=hd0,format=raw,if=none \
-device virtio-blk-device,drive=hd0 \
-append "root=/dev/vda rw console=ttyS0 earlycon=sbi" \
-dtb /tmp/virt-sspesa-test.dtbOnce Linux boots, use the PESA event modifier with a precise request:
- perf record -o /tmp/sspesa-perf.data -e 'cpu/event=2,pesa=1/ppp' <workload>
- perf record -o /tmp/non-sspesa-perf.data -e 'cpu/event=2/' <workload>
Check the resulting data with:
- perf report -D -i /tmp/sspesa-perf.data \
| grep 'PERF_RECORD_SAMPLE' | head
- perf report -D -i /tmp/non-sspesa-perf.data \
| grep 'PERF_RECORD_SAMPLE' | head
Samples in sspesa-perf.data should have the PERF_RECORD_MISC_EXACT_IP bit (0x4000) set in the misc field, while samples in non-sspesa-perf.data should not have that bit set. This verifies plumbing only regardless of whether QEMU supports precise attribution.
Changes since v1:
- Record the effective privilege mode in the new shpmsdata.PRIV field:
https://github.com/riscv/riscv-performance-event-sampling/commit/3eff81a6161ffc7a74a57436132d0c15ab549e74
- Capture shpmspc at counter overflow, instead of during interrupt delivery.
- Preserve the first sample as long as LCOFIP remains pending.
- Update sample CSRs only for delegated counters when menvcfg.CDE=1.
- Fix extension dependencies i.e. Sspesa implies Sscofpmf and Ssplcofi implies Sspesa.
Mayuresh Chitale (2):
target/riscv: Add Sspesa extension and sample CSRs target/riscv: Capture Sspesa sample PC and counter ID on overflow
target/riscv/cpu.c | 24 ++++++++++++++ target/riscv/cpu.h | 4 +++ target/riscv/cpu_bits.h | 11 +++++++ target/riscv/cpu_cfg_fields.h.inc | 2 ++ target/riscv/machine.c | 20 +++++++++++ target/riscv/tcg/csr.c | 55 +++++++++++++++++++++++++++++++ target/riscv/tcg/pmu.c | 39 ++++++++++++++++++++++ 7 files changed, 155 insertions(+)
Signature · 1 line
2.43.0
RFC PATCH v2 1/2 target/riscv: Add Sspesa extension and sample CSRs
Add initial support for the Sspesa (Precise Event Sample Attribution) extension from the RISC-V Performance Event Sampling specification.
Added the 'sspesa' and ssplcofi CPU properties, and the shpmspc and shpmsdata CSRs.
--- target/riscv/cpu.c | 24 ++++++++++++++ target/riscv/cpu.h | 4 +++ target/riscv/cpu_bits.h | 12 +++++++ target/riscv/cpu_cfg_fields.h.inc | 2 ++ target/riscv/machine.c | 20 +++++++++++ target/riscv/tcg/csr.c | 55 +++++++++++++++++++++++++++++++ 6 files changed, 117 insertions(+)
target/riscv/cpu.c +24 -0
@@ -282,6 +282,8 @@ const RISCVIsaExtData isa_edata_arr[] = {ISA_EXT_DATA_ENTRY (ssctr,PRIV_VERSION_1_12_0 , ext_ssctr),ISA_EXT_DATA_ENTRY (ssdbltrp,PRIV_VERSION_1_13_0 , ext_ssdbltrp),ISA_EXT_DATA_ENTRY (ssnpm,PRIV_VERSION_1_13_0 , ext_ssnpm), +ISA_EXT_DATA_ENTRY (sspesa,PRIV_VERSION_1_13_0 , ext_sspesa), +ISA_EXT_DATA_ENTRY (ssplcofi,PRIV_VERSION_1_13_0 , ext_ssplcofi),ISA_EXT_DATA_ENTRY (sspm,PRIV_VERSION_1_13_0 , ext_sspm),ISA_INTERNAL_EXT_DATA_ENTRY (ssstateen,PRIV_VERSION_1_12_0 , ext_ssstateen), ISA_INTERNAL_EXT_DATA_ENTRY(ssstrict, PRIV_VERSION_1_12_0, has_priv_1_12),
@@ -1070,6 +1072,8 @@ static void riscv_cpu_reset_hold(Object *obj, ResetType type) }pmp_unlock_entries (env); + env->shpmspc =0 ; + env->shpmsdata =0 ;#endif /* ifdef CONFIG_TCG */ #else env->priv =PRV_U ;
@@ -2933,6 +2937,24 @@ static RISCVCPUImpliedExtsRule SSSTATEEN_IMPLIED = { }, }; +static RISCVCPUImpliedExtsRule SSPLCOFI_IMPLIED = { + .ext =CPU_CFG_OFFSET (ext_ssplcofi), + .implied_multi_exts = { +CPU_CFG_OFFSET (ext_sspesa), + +RISCV_IMPLIED_EXTS_RULE_END + }, +}; + +static RISCVCPUImpliedExtsRule SSPESA_IMPLIED = { + .ext =CPU_CFG_OFFSET (ext_sspesa), + .implied_multi_exts = { +CPU_CFG_OFFSET (ext_sscofpmf), + +RISCV_IMPLIED_EXTS_RULE_END + }, +}; +static RISCVCPUImpliedExtsRule ZVFBFA_IMPLIED = { .ext =CPU_CFG_OFFSET (ext_zvfbfa), .implied_multi_exts = {
@@ -3013,6 +3035,8 @@ RISCVCPUImpliedExtsRule *riscv_multi_ext_implied_rules[] = { &SSSTATEEN_IMPLIED , &XSFVQMACCDOD_IMPLIED , &XSFVQMACCQOQ_IMPLIED , + &SSPESA_IMPLIED , + &SSPLCOFI_IMPLIED , NULL };
target/riscv/cpu.h +4 -0
@@ -470,6 +470,10 @@ struct CPUArchState {uint64_t sscratch;uint64_t mscratch; +/* Sspesa CSRs */ +target_ulong shpmspc; +target_ulong shpmsdata; +/* Sstc CSRs */ uint64_t stimecmp;
target/riscv/cpu_bits.h +12 -0
@@ -634,6 +634,18 @@#define CSR_SCOUNTOVF 0xda0 +/* Temporary addresses for Sspesa (Precise Event Sample Attribution) CSRs. */ +#define CSR_SHPMSPC 0x5d0 +#define CSR_SHPMSDATA 0x5d1 + +/* shpmsdata fields */ +#define SHPMSDATA_CNTRID 0x1f +#define SHPMSDATA_MODE 0x60 +#define SHPMSDATA_V 0x80 +#define SHPMSDATA_MODE_U 0x00 +#define SHPMSDATA_MODE_S 0x20 +#define SHPMSDATA_VALID_MASK (SHPMSDATA_CNTRID | SHPMSDATA_MODE | SHPMSDATA_V) +/* Crypto Extension */ #define CSR_SEED 0x015
target/riscv/cpu_cfg_fields.h.inc +2 -0
@@ -112,6 +112,8 @@ BOOL_FIELD(ext_ssaia) BOOL_FIELD(ext_smctr) BOOL_FIELD(ext_ssctr) BOOL_FIELD(ext_sscofpmf) +BOOL_FIELD(ext_sspesa) +BOOL_FIELD(ext_ssplcofi) BOOL_FIELD(ext_smepmp) BOOL_FIELD(ext_smrnmi) BOOL_FIELD(ext_ssnpm)
target/riscv/machine.c +20 -0
@@ -475,6 +475,25 @@ static const VMStateDescription vmstate_pmu_ctr_state = { } }; +static bool sspesa_needed (void *opaque) +{ +RISCVCPU *cpu = opaque; + +return cpu->cfg.ext_sspesa; +} + +static const VMStateDescription vmstate_sspesa = { + .name ="cpu/sspesa" , + .version_id =1 , + .minimum_version_id =1 , + .needed = sspesa_needed, + .fields = (const VMStateField []) { +VMSTATE_UINTTL (env.shpmspc,RISCVCPU ), +VMSTATE_UINTTL (env.shpmsdata,RISCVCPU ), +VMSTATE_END_OF_LIST () + } +}; +static bool jvt_needed (void *opaque) {RISCVCPU *cpu = opaque;
@@ -653,6 +672,7 @@ const VMStateDescription vmstate_riscv_cpu = { &vmstate_elp, &vmstate_ssp, &vmstate_ctr, + &vmstate_sspesa, &vmstate_sstc, &vmstate_mseccfg, &vmstate_sdext,
target/riscv/tcg/csr.c +55 -0
73 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [RFC PATCH v2 1/2] target/riscv: Add Sspesa extension and sample CSRs
Quoted 8 lines · Mayuresh Chitale
Add initial support for the Sspesa (Precise Event Sample Attribution) extension from the RISC-V Performance Event Sampling specification.
Added the 'sspesa' and ssplcofi CPU properties, and the shpmspc and shpmsdata CSRs.
Signed-off-by Mayuresh Chitale <[email protected]>---
Quoted 233 lines
target/riscv/cpu.c | 24 ++++++++++++++ target/riscv/cpu.h | 4 +++ target/riscv/cpu_bits.h | 12 +++++++ target/riscv/cpu_cfg_fields.h.inc | 2 ++ target/riscv/machine.c | 20 +++++++++++ target/riscv/tcg/csr.c | 55 +++++++++++++++++++++++++++++++ 6 files changed, 117 insertions(+)target/riscv/cpu.c +24
-0@@ -282,6 +282,8 @@ const RISCVIsaExtData isa_edata_arr[] = {ISA_EXT_DATA_ENTRY (ssctr,PRIV_VERSION_1_12_0 , ext_ssctr),ISA_EXT_DATA_ENTRY (ssdbltrp,PRIV_VERSION_1_13_0 , ext_ssdbltrp),ISA_EXT_DATA_ENTRY (ssnpm,PRIV_VERSION_1_13_0 , ext_ssnpm), +ISA_EXT_DATA_ENTRY (sspesa,PRIV_VERSION_1_13_0 , ext_sspesa), +ISA_EXT_DATA_ENTRY (ssplcofi,PRIV_VERSION_1_13_0 , ext_ssplcofi),ISA_EXT_DATA_ENTRY (sspm,PRIV_VERSION_1_13_0 , ext_sspm),ISA_INTERNAL_EXT_DATA_ENTRY (ssstateen,PRIV_VERSION_1_12_0 , ext_ssstateen), ISA_INTERNAL_EXT_DATA_ENTRY(ssstrict, PRIV_VERSION_1_12_0, has_priv_1_12),@@ -1070,6 +1072,8 @@ static void riscv_cpu_reset_hold(Object *obj, ResetType type) }pmp_unlock_entries (env); + env->shpmspc =0 ; + env->shpmsdata =0 ;#endif /* ifdef CONFIG_TCG */ #else env->priv =PRV_U ;@@ -2933,6 +2937,24 @@ static RISCVCPUImpliedExtsRule SSSTATEEN_IMPLIED = { }, }; +static RISCVCPUImpliedExtsRule SSPLCOFI_IMPLIED = { + .ext =CPU_CFG_OFFSET (ext_ssplcofi), + .implied_multi_exts = { +CPU_CFG_OFFSET (ext_sspesa), + +RISCV_IMPLIED_EXTS_RULE_END + }, +}; + +static RISCVCPUImpliedExtsRule SSPESA_IMPLIED = { + .ext =CPU_CFG_OFFSET (ext_sspesa), + .implied_multi_exts = { +CPU_CFG_OFFSET (ext_sscofpmf), + +RISCV_IMPLIED_EXTS_RULE_END + }, +}; +static RISCVCPUImpliedExtsRule ZVFBFA_IMPLIED = { .ext =CPU_CFG_OFFSET (ext_zvfbfa), .implied_multi_exts = {@@ -3013,6 +3035,8 @@ RISCVCPUImpliedExtsRule *riscv_multi_ext_implied_rules[] = { &SSSTATEEN_IMPLIED , &XSFVQMACCDOD_IMPLIED , &XSFVQMACCQOQ_IMPLIED , + &SSPESA_IMPLIED , + &SSPLCOFI_IMPLIED , NULL };target/riscv/cpu.h +4
-0@@ -470,6 +470,10 @@ struct CPUArchState {uint64_t sscratch;uint64_t mscratch; +/* Sspesa CSRs */ +target_ulong shpmspc; +target_ulong shpmsdata; +/* Sstc CSRs */ uint64_t stimecmp;target/riscv/cpu_bits.h +12
-0@@ -634,6 +634,18 @@#define CSR_SCOUNTOVF 0xda0 +/* Temporary addresses for Sspesa (Precise Event Sample Attribution) CSRs. */ +#define CSR_SHPMSPC 0x5d0 +#define CSR_SHPMSDATA 0x5d1 + +/* shpmsdata fields */ +#define SHPMSDATA_CNTRID 0x1f +#define SHPMSDATA_MODE 0x60 +#define SHPMSDATA_V 0x80 +#define SHPMSDATA_MODE_U 0x00 +#define SHPMSDATA_MODE_S 0x20 +#define SHPMSDATA_VALID_MASK (SHPMSDATA_CNTRID | SHPMSDATA_MODE | SHPMSDATA_V) +/* Crypto Extension */ #define CSR_SEED 0x015target/riscv/cpu_cfg_fields.h.inc +2
-0@@ -112,6 +112,8 @@ BOOL_FIELD(ext_ssaia) BOOL_FIELD(ext_smctr) BOOL_FIELD(ext_ssctr) BOOL_FIELD(ext_sscofpmf) +BOOL_FIELD(ext_sspesa) +BOOL_FIELD(ext_ssplcofi) BOOL_FIELD(ext_smepmp) BOOL_FIELD(ext_smrnmi) BOOL_FIELD(ext_ssnpm)target/riscv/machine.c +20
-0@@ -475,6 +475,25 @@ static const VMStateDescription vmstate_pmu_ctr_state = { } }; +static bool sspesa_needed (void *opaque) +{ +RISCVCPU *cpu = opaque; + +return cpu->cfg.ext_sspesa; +} + +static const VMStateDescription vmstate_sspesa = { + .name ="cpu/sspesa" , + .version_id =1 , + .minimum_version_id =1 , + .needed = sspesa_needed, + .fields = (const VMStateField []) { +VMSTATE_UINTTL (env.shpmspc,RISCVCPU ), +VMSTATE_UINTTL (env.shpmsdata,RISCVCPU ), +VMSTATE_END_OF_LIST () + } +}; +static bool jvt_needed (void *opaque) {RISCVCPU *cpu = opaque;@@ -653,6 +672,7 @@ const VMStateDescription vmstate_riscv_cpu = { &vmstate_elp, &vmstate_ssp, &vmstate_ctr, + &vmstate_sspesa, &vmstate_sstc, &vmstate_mseccfg, &vmstate_sdext,target/riscv/tcg/csr.c +55
-0@@ -279,6 +279,28 @@ static RISCVException smcntrpmf(CPURISCVState *env, int csrno)return RISCV_EXCP_NONE ; } +static RISCVException sspesa (CPURISCVState *env,int csrno) +{ +bool menvcfg_cde; + +if (!riscv_cpu_cfg (env)->ext_sspesa) { +return RISCV_EXCP_ILLEGAL_INST ; + } + +if (env->priv ==PRV_M ) { +return RISCV_EXCP_NONE ; + } + + menvcfg_cde =get_field (env->menvcfg,MENVCFG_CDE ); +/* Access to shpmspc/shpmsdata from VS-mode always causes a fault */ +if (env->virt_enabled) { +return menvcfg_cde ?RISCV_EXCP_VIRT_INSTRUCTION_FAULT : +RISCV_EXCP_ILLEGAL_INST ; + } + +return menvcfg_cde ?RISCV_EXCP_NONE :RISCV_EXCP_ILLEGAL_INST ; +} +static RISCVException smcntrpmf_32 (CPURISCVState *env,int csrno) {if (riscv_cpu_mxl (env) !=MXL_RV32 ) {@@ -1677,6 +1699,34 @@ static RISCVException read_scountovf(CPURISCVState *env, int csrno,return RISCV_EXCP_NONE ; } +static RISCVException read_shpmspc (CPURISCVState *env,int csrno, +target_ulong *val) +{ + *val = env->shpmspc; +return RISCV_EXCP_NONE ; +} + +static RISCVException write_shpmspc (CPURISCVState *env,int csrno, +target_ulong val,uintptr_t ra) +{ + env->shpmspc = val; +return RISCV_EXCP_NONE ; +} + +static RISCVException read_shpmsdata (CPURISCVState *env,int csrno, +target_ulong *val) +{ + *val = env->shpmsdata; +return RISCV_EXCP_NONE ; +} + +static RISCVException write_shpmsdata (CPURISCVState *env,int csrno, +target_ulong val,uintptr_t ra) +{ + env->shpmsdata = val &SHPMSDATA_VALID_MASK ; +return RISCV_EXCP_NONE ; +} +static RISCVException read_time (CPURISCVState *env,int csrno,target_ulong *val) {@@ -6978,6 +7028,11 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { write_mhpmcounterh }, [CSR_SCOUNTOVF] = { "scountovf", sscofpmf, read_scountovf, .min_priv_ver = PRIV_VERSION_1_12_0 }, + [CSR_SHPMSPC] = { "shpmspc", sspesa, read_shpmspc, write_shpmspc, + .min_priv_ver = PRIV_VERSION_1_13_0 }, + [CSR_SHPMSDATA] = { "shpmsdata", sspesa, read_shpmsdata, + write_shpmsdata, + .min_priv_ver = PRIV_VERSION_1_13_0 }, #endif /* !CONFIG_USER_ONLY */ };
RFC PATCH v2 2/2 target/riscv: Capture Sspesa sample PC and counter ID on overflow
In reply to Mayuresh ChitaleCapture shpmspc and shpmsdata just before updating mip.LCOFIP. As required by the spec, record the current PC, counter ID and effective privilege mode. When supervisor counter delegation is enabled, only overflows of delegated counters update the sample CSRs.
Preserve shpmspc and shpmsdata while LCOFIP remains pending so later overflows do not replace the pending sample. Currently, cntrid is not updated if two counters overflow simultaneously.
The captured PC is QEMU's current env->pc, so it provides best-effort attribution rather than guaranteed precise attribution.
--- target/riscv/cpu_bits.h | 3 +-- target/riscv/tcg/pmu.c | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-)
target/riscv/cpu_bits.h +1 -2
@@ -642,9 +642,8 @@#define SHPMSDATA_CNTRID 0x1f#define SHPMSDATA_MODE 0x60#define SHPMSDATA_V 0x80 -#define SHPMSDATA_MODE_U 0x00 -#define SHPMSDATA_MODE_S 0x20#define SHPMSDATA_VALID_MASK (SHPMSDATA_CNTRID | SHPMSDATA_MODE | SHPMSDATA_V) +#define SHPMSDATA_MODE_SHIFT 5/* Crypto Extension */ #define CSR_SEED 0x015
target/riscv/tcg/pmu.c +39 -0
@@ -178,6 +178,43 @@ void riscv_pmu_decr_instret(CPURISCVState *env) env->pmu_ctrs[2 ].mhpmcounter_prev++; } +static void riscv_pmu_sspesa_capture (CPURISCVState *env,uint32_t ctr_idx) +{ +RISCVCPU *cpu =env_archcpu (env); +target_ulong sdata; +bool cde, ctr_enabled; + +if (!cpu->cfg.ext_sspesa) { +return ; + } + +/* +* When supervisor counter delegation is enabled only overflows of +* delegated counters update the sample CSRs. +*/ + cde =get_field (env->menvcfg,MENVCFG_CDE ); + ctr_enabled =get_field (env->mcounteren,BIT (ctr_idx)); +if (cde && !ctr_enabled) { +return ; + } + + sdata = ctr_idx; + sdata |= env->priv <<SHPMSDATA_MODE_SHIFT ; +if (env->virt_enabled) { + sdata |=SHPMSDATA_V ; + } + +/* +* Only the first overflow that raises LCOFIP updates the sample PC and +* metadata. Currently updating CNTRID on simultaneous overflows is +* not supported. +*/ +if (!(env->mip &MIP_LCOFIP )) { + env->shpmspc = env->pc; + env->shpmsdata = sdata; + } +} +int riscv_pmu_incr_ctr (RISCVCPU *cpu,enum riscv_pmu_event_idx event_idx) {uint32_t ctr_idx;
@@ -211,6 +248,7 @@ int riscv_pmu_incr_ctr(RISCVCPU *cpu, enum riscv_pmu_event_idx event_idx)/* Generate interrupt only if OF bit is clear */ if (!(env->mhpmevent_val[ctr_idx] &MHPMEVENT_BIT_OF )) { env->mhpmevent_val[ctr_idx] |=MHPMEVENT_BIT_OF ; +riscv_pmu_sspesa_capture (env, ctr_idx);riscv_cpu_update_mip (env,MIP_LCOFIP ,BOOL_TO_MASK (1 )); } }else {
@@ -403,6 +441,7 @@ static void pmu_timer_trigger_irq(RISCVCPU *cpu,if (cpu->pmu_avail_ctrs &BIT (ctr_idx)) {if (pmu_hpmevent_set_of_if_clear (env, ctr_idx)) { +riscv_pmu_sspesa_capture (env, ctr_idx);riscv_cpu_update_mip (env,MIP_LCOFIP ,BOOL_TO_MASK (1 )); } }
Signature · 1 line
2.43.0
Re: [RFC PATCH v2 2/2] target/riscv: Capture Sspesa sample PC and counter ID on overflow
Quoted 23 lines · Mayuresh Chitale
Capture shpmspc and shpmsdata just before updating mip.LCOFIP. As required by the spec, record the current PC, counter ID and effective privilege mode. When supervisor counter delegation is enabled, only overflows of delegated counters update the sample CSRs.
Preserve shpmspc and shpmsdata while LCOFIP remains pending so later overflows do not replace the pending sample. Currently, cntrid is not updated if two counters overflow simultaneously.
The captured PC is QEMU's current
env->pc, so it provides best-effort attribution rather than guaranteed precise attribution.Signed-off-by Mayuresh Chitale <[email protected]>--- target/riscv/cpu_bits.h | 3 +-- target/riscv/tcg/pmu.c | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-)target/riscv/cpu_bits.h +0
-0
target/riscv/cpu_bits.h line 646
Quoted 5 lines
target/riscv/cpu_bits.h +0
-2@@ -642,9 +642,8 @@#define SHPMSDATA_CNTRID 0x1f#define SHPMSDATA_MODE 0x60#define SHPMSDATA_V 0x80 -#define SHPMSDATA_MODE_U 0x00 -#define SHPMSDATA_MODE_S 0x20
These 2 macros were added in patch 1, never used there, and are now being removed here. Let's remove them in patch 1 instead (i.e. never add them in the first place).
Quoted 10 lines
target/riscv/cpu_bits.h +1
-0#define SHPMSDATA_VALID_MASK (SHPMSDATA_CNTRID | SHPMSDATA_MODE | SHPMSDATA_V) +#define SHPMSDATA_MODE_SHIFT 5/* Crypto Extension */ #define CSR_SEED 0x015target/riscv/tcg/pmu.c +0
-0
target/riscv/tcg/pmu.c void riscv_pmu_decr_instret(CPURISCVState *env) · line 183
Quoted 6 lines
target/riscv/tcg/pmu.c +3
-0@@ -178,6 +178,43 @@ void riscv_pmu_decr_instret(CPURISCVState *env) env->pmu_ctrs[2 ].mhpmcounter_prev++; } +static void riscv_pmu_sspesa_capture (CPURISCVState *env,uint32_t ctr_idx) +{ +RISCVCPU *cpu =env_archcpu (env);
I believe this is something we should document somewhere, but env_archcpu() is a not the nicest call to make and we would like to avoid it every time we can. One of its main usages back in 2023 was to access the cpu->cfg pointer to check if a given extension is available. We ended up creating an inline called riscv_cpu_cfg() to allow access to the CFG struct without having to call env_archcpu().
So, if the code doesn't do anything else with the RISCVCPU pointer aside from reading cpu->cfg, we recommend using riscv_cpu_cfg() instead.
For this code I suggest not using env_archcpu() and:
target/riscv/tcg/pmu.c void riscv_pmu_decr_instret(CPURISCVState *env) · line 187
Quoted 4 lines
target/riscv/tcg/pmu.c +4
-0+target_ulong sdata; +bool cde, ctr_enabled; + +if (!cpu->cfg.ext_sspesa) {
if (!riscv_cpu_cfg(env)->ext_sspesa) {
return;
}Thanks,
Daniel
Quoted 49 lines
target/riscv/tcg/pmu.c +32
-0+return ; + } + +/* +* When supervisor counter delegation is enabled only overflows of +* delegated counters update the sample CSRs. +*/ + cde =get_field (env->menvcfg,MENVCFG_CDE ); + ctr_enabled =get_field (env->mcounteren,BIT (ctr_idx)); +if (cde && !ctr_enabled) { +return ; + } + + sdata = ctr_idx; + sdata |= env->priv <<SHPMSDATA_MODE_SHIFT ; +if (env->virt_enabled) { + sdata |=SHPMSDATA_V ; + } + +/* +* Only the first overflow that raises LCOFIP updates the sample PC and +* metadata. Currently updating CNTRID on simultaneous overflows is +* not supported. +*/ +if (!(env->mip &MIP_LCOFIP )) { + env->shpmspc = env->pc; + env->shpmsdata = sdata; + } +} +int riscv_pmu_incr_ctr (RISCVCPU *cpu,enum riscv_pmu_event_idx event_idx) {uint32_t ctr_idx;@@ -211,6 +248,7 @@ int riscv_pmu_incr_ctr(RISCVCPU *cpu, enum riscv_pmu_event_idx event_idx)/* Generate interrupt only if OF bit is clear */ if (!(env->mhpmevent_val[ctr_idx] &MHPMEVENT_BIT_OF )) { env->mhpmevent_val[ctr_idx] |=MHPMEVENT_BIT_OF ; +riscv_pmu_sspesa_capture (env, ctr_idx);riscv_cpu_update_mip (env,MIP_LCOFIP ,BOOL_TO_MASK (1 )); } }else {@@ -403,6 +441,7 @@ static void pmu_timer_trigger_irq(RISCVCPU *cpu,if (cpu->pmu_avail_ctrs &BIT (ctr_idx)) {if (pmu_hpmevent_set_of_if_clear (env, ctr_idx)) { +riscv_pmu_sspesa_capture (env, ctr_idx);riscv_cpu_update_mip (env,MIP_LCOFIP ,BOOL_TO_MASK (1 )); } }
Re: [RFC PATCH v2 2/2] target/riscv: Capture Sspesa sample PC and counter ID on overflow
Quoted 67 lines · Daniel Henrique Barboza
Quoted 23 lines · Mayuresh Chitale
Capture shpmspc and shpmsdata just before updating mip.LCOFIP. As required by the spec, record the current PC, counter ID and effective privilege mode. When supervisor counter delegation is enabled, only overflows of delegated counters update the sample CSRs.
Preserve shpmspc and shpmsdata while LCOFIP remains pending so later overflows do not replace the pending sample. Currently, cntrid is not updated if two counters overflow simultaneously.
The captured PC is QEMU's current
env->pc, so it provides best-effort attribution rather than guaranteed precise attribution.Signed-off-by Mayuresh Chitale <[email protected]>--- target/riscv/cpu_bits.h | 3 +-- target/riscv/tcg/pmu.c | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-)target/riscv/cpu_bits.h +0
-0target/riscv/cpu_bits.h line 646
Quoted 5 lines
target/riscv/cpu_bits.h +0
-2@@ -642,9 +642,8 @@#define SHPMSDATA_CNTRID 0x1f#define SHPMSDATA_MODE 0x60#define SHPMSDATA_V 0x80 -#define SHPMSDATA_MODE_U 0x00 -#define SHPMSDATA_MODE_S 0x20These 2 macros were added in patch 1, never used there, and are now being removed here. Let's remove them in patch 1 instead (i.e. never add them in the first place).
Quoted 10 lines
target/riscv/cpu_bits.h +1
-0#define SHPMSDATA_VALID_MASK (SHPMSDATA_CNTRID | SHPMSDATA_MODE | SHPMSDATA_V) +#define SHPMSDATA_MODE_SHIFT 5/* Crypto Extension */ #define CSR_SEED 0x015target/riscv/tcg/pmu.c +0
-0target/riscv/tcg/pmu.c void riscv_pmu_decr_instret(CPURISCVState *env) · line 183
Quoted 6 lines
target/riscv/tcg/pmu.c +3
-0@@ -178,6 +178,43 @@ void riscv_pmu_decr_instret(CPURISCVState *env) env->pmu_ctrs[2 ].mhpmcounter_prev++; } +static void riscv_pmu_sspesa_capture (CPURISCVState *env,uint32_t ctr_idx) +{ +RISCVCPU *cpu =env_archcpu (env);I believe this is something we should document somewhere, but
env_archcpu()is a not the nicest call to make and we would like to avoid it every time we can. One of its main usages back in 2023 was to access thecpu->cfgpointer to check if a given extension is available. We ended up creating an inline calledriscv_cpu_cfg()to allow access to the CFG struct without having to callenv_archcpu().So, if the code doesn't do anything else with the RISCVCPU pointer aside from reading
cpu->cfg, we recommend usingriscv_cpu_cfg()instead.For this code I suggest not using
env_archcpu()and:
Quoted 9 lines
target/riscv/tcg/pmu.c void riscv_pmu_decr_instret(CPURISCVState *env) · line 187
Quoted 4 lines
target/riscv/tcg/pmu.c +4
-0+target_ulong sdata; +bool cde, ctr_enabled; + +if (!cpu->cfg.ext_sspesa) {if (!riscv_cpu_cfg(env)->ext_sspesa) { return; }
Thanks Daniel, I will fix these in the next version.
Quoted 54 lines
Thanks,
DanielQuoted 49 lines
target/riscv/tcg/pmu.c +32
-0+return ; + } + +/* +* When supervisor counter delegation is enabled only overflows of +* delegated counters update the sample CSRs. +*/ + cde =get_field (env->menvcfg,MENVCFG_CDE ); + ctr_enabled =get_field (env->mcounteren,BIT (ctr_idx)); +if (cde && !ctr_enabled) { +return ; + } + + sdata = ctr_idx; + sdata |= env->priv <<SHPMSDATA_MODE_SHIFT ; +if (env->virt_enabled) { + sdata |=SHPMSDATA_V ; + } + +/* +* Only the first overflow that raises LCOFIP updates the sample PC and +* metadata. Currently updating CNTRID on simultaneous overflows is +* not supported. +*/ +if (!(env->mip &MIP_LCOFIP )) { + env->shpmspc = env->pc; + env->shpmsdata = sdata; + } +} +int riscv_pmu_incr_ctr (RISCVCPU *cpu,enum riscv_pmu_event_idx event_idx) {uint32_t ctr_idx;@@ -211,6 +248,7 @@ int riscv_pmu_incr_ctr(RISCVCPU *cpu, enum riscv_pmu_event_idx event_idx)/* Generate interrupt only if OF bit is clear */ if (!(env->mhpmevent_val[ctr_idx] &MHPMEVENT_BIT_OF )) { env->mhpmevent_val[ctr_idx] |=MHPMEVENT_BIT_OF ; +riscv_pmu_sspesa_capture (env, ctr_idx);riscv_cpu_update_mip (env,MIP_LCOFIP ,BOOL_TO_MASK (1 )); } }else {@@ -403,6 +441,7 @@ static void pmu_timer_trigger_irq(RISCVCPU *cpu,if (cpu->pmu_avail_ctrs &BIT (ctr_idx)) {if (pmu_hpmevent_set_of_if_clear (env, ctr_idx)) { +riscv_pmu_sspesa_capture (env, ctr_idx);riscv_cpu_update_mip (env,MIP_LCOFIP ,BOOL_TO_MASK (1 )); } }