Skip to contentSkip to conversation

RISC-V Sspesa: QEMU model support

RFC PATCH Superseded Mayuresh Chitale 2 participants 5 messages Aug 17 – Aug 24 RISC-V

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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' CPU property, the ISA string/DT entry, and the shpmspc/shpmsdata CSRs. It also allows menvcfg.CDE to be set when Sspesa is enabled, and adds 'ssplcofi' which is implied by Sspesa so that it is advertised automatically. The second patch captures the overflowing counter ID in shpmsdata at overflow time and latches shpmspc with the interrupted instruction address at LCOFI delivery

Note: this series applies on top of the previously posted fix "target/riscv: Fix LCOFI masking in sie/sip/mvip", which is required for counter-overflow interrupts to be delivered and cleared correctly.

Reproducing the full setup:

Sspesa support requires three projects: OpenSBI, Linux and QEMU. The matching branches are:

OpenSBI: https://github.com/mdchitale/opensbi/tree/riscv_sspesa_rfc_v1
Linux: https://github.com/mdchitale/linux/tree/riscv_sspesa_rfc_v1
QEMU: https://github.com/mdchitale/qemu/tree/riscv_sspesa_rfc_v1

To test Sspesa, build OpenSBI, Linux and QEMU from the branches above, then boot Linux with:

  qemu-system-riscv64 -nographic -machine virt -smp 2 -m 2G \
    -cpu rv64,sscofpmf=on,sspesa=on \
    -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"

With all three in place, a counter overflow (e.g. 'perf record -e cycles -c 1000000 -- <workload>') is attributed using the hardware-provided sample PC (shpmspc) and counter ID (shpmsdata) instead of falling back to epc.

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                | 13 ++++-
 target/riscv/cpu.h                |  6 +++
 target/riscv/cpu_bits.h           |  7 +++
 target/riscv/cpu_cfg_fields.h.inc |  2 +
 target/riscv/tcg/cpu_helper.c     |  6 +++
 target/riscv/tcg/csr.c            | 84 ++++++++++++++++++++++++++++++-
 target/riscv/tcg/pmu.c            | 27 ++++++++++
 7 files changed, 142 insertions(+), 3 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

RFC PATCH 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                | 13 ++++++-
 target/riscv/cpu.h                |  4 +++
 target/riscv/cpu_bits.h           |  7 ++++
 target/riscv/cpu_cfg_fields.h.inc |  2 ++
 target/riscv/tcg/csr.c            | 59 +++++++++++++++++++++++++++++--
 5 files changed, 82 insertions(+), 3 deletions(-)
target/riscv/cpu.c +12 -1
index 5a82e6563b..f936cdeb68 100644
@@ -278,6 +278,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_12_0, ext_sspesa),
+    ISA_EXT_DATA_ENTRY(ssplcofi, PRIV_VERSION_1_12_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),
@@ -2800,6 +2802,15 @@ static RISCVCPUImpliedExtsRule SSSTATEEN_IMPLIED = {
     },
 };
 
+static RISCVCPUImpliedExtsRule SSPESA_IMPLIED = {
+    .ext = CPU_CFG_OFFSET(ext_sspesa),
+    .implied_multi_exts = {
+        CPU_CFG_OFFSET(ext_ssplcofi),
+
+        RISCV_IMPLIED_EXTS_RULE_END
+    },
+};
+
 static RISCVCPUImpliedExtsRule ZVFBFA_IMPLIED = {
     .ext = CPU_CFG_OFFSET(ext_zvfbfa),
     .implied_multi_exts = {
@@ -2829,7 +2840,7 @@ RISCVCPUImpliedExtsRule *riscv_multi_ext_implied_rules[] = {
     &ZVKNC_IMPLIED, &ZVKNG_IMPLIED, &ZVKNHB_IMPLIED,
     &ZVKS_IMPLIED,  &ZVKSC_IMPLIED, &ZVKSG_IMPLIED, &SHA_IMPLIED,
     &SSCFG_IMPLIED, &SUPM_IMPLIED, &SSPM_IMPLIED, &SMCTR_IMPLIED,
-    &SSCTR_IMPLIED, &SSSTATEEN_IMPLIED,
+    &SSCTR_IMPLIED, &SSSTATEEN_IMPLIED, &SSPESA_IMPLIED,
     NULL
 };
 
target/riscv/cpu.h +4 -0
index c9dfa7daff..fe56a6afd1 100644
@@ -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 +7 -0
index 3f146a43fe..d3afc9cada 100644
@@ -601,6 +601,13 @@
 
 #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
+
 /* Crypto Extension */
 #define CSR_SEED            0x015
 
target/riscv/cpu_cfg_fields.h.inc +2 -0
index 9eb47af0a7..05e0162b76 100644
@@ -110,6 +110,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/tcg/csr.c +57 -2

89 lines not on this page · raw message

Signature · 1 line
2.43.0
Reviewed-by on v1 1/2
#2 lore raw

Re: [RFC PATCH 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.

---

Quoted 189 lines
  target/riscv/cpu.c                | 13 ++++++-
  target/riscv/cpu.h                |  4 +++
  target/riscv/cpu_bits.h           |  7 ++++
  target/riscv/cpu_cfg_fields.h.inc |  2 ++
  target/riscv/tcg/csr.c            | 59 +++++++++++++++++++++++++++++--
  5 files changed, 82 insertions(+), 3 deletions(-)
target/riscv/cpu.c +12 -1
index 5a82e6563b..f936cdeb68 100644
@@ -278,6 +278,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_12_0, ext_sspesa),
+    ISA_EXT_DATA_ENTRY(ssplcofi, PRIV_VERSION_1_12_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),
@@ -2800,6 +2802,15 @@ static RISCVCPUImpliedExtsRule SSSTATEEN_IMPLIED = {
      },
  };
 
+static RISCVCPUImpliedExtsRule SSPESA_IMPLIED = {
+    .ext = CPU_CFG_OFFSET(ext_sspesa),
+    .implied_multi_exts = {
+        CPU_CFG_OFFSET(ext_ssplcofi),
+
+        RISCV_IMPLIED_EXTS_RULE_END
+    },
+};
+
  static RISCVCPUImpliedExtsRule ZVFBFA_IMPLIED = {
      .ext = CPU_CFG_OFFSET(ext_zvfbfa),
      .implied_multi_exts = {
@@ -2829,7 +2840,7 @@ RISCVCPUImpliedExtsRule *riscv_multi_ext_implied_rules[] = {
      &ZVKNC_IMPLIED, &ZVKNG_IMPLIED, &ZVKNHB_IMPLIED,
      &ZVKS_IMPLIED,  &ZVKSC_IMPLIED, &ZVKSG_IMPLIED, &SHA_IMPLIED,
      &SSCFG_IMPLIED, &SUPM_IMPLIED, &SSPM_IMPLIED, &SMCTR_IMPLIED,
-    &SSCTR_IMPLIED, &SSSTATEEN_IMPLIED,
+    &SSCTR_IMPLIED, &SSSTATEEN_IMPLIED, &SSPESA_IMPLIED,
      NULL
  };
 
target/riscv/cpu.h +4 -0
index c9dfa7daff..fe56a6afd1 100644
@@ -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 +7 -0
index 3f146a43fe..d3afc9cada 100644
@@ -601,6 +601,13 @@
 
  #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
+
  /* Crypto Extension */
  #define CSR_SEED            0x015
 
target/riscv/cpu_cfg_fields.h.inc +2 -0
index 9eb47af0a7..05e0162b76 100644
@@ -110,6 +110,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/tcg/csr.c +57 -2
index eb823b16b5..ea49d28aae 100644
@@ -276,6 +276,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) {
@@ -1672,6 +1694,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;
+    return RISCV_EXCP_NONE;
+}
+
  static RISCVException read_time(CPURISCVState *env, int csrno,
                                  target_ulong *val)
  {
@@ -3220,7 +3270,7 @@ static RISCVException write_menvcfg(CPURISCVState *env, int csrno,
      if (riscv_cpu_mxl(env) == MXL_RV64) {
          mask |= (cfg->ext_svpbmt ? MENVCFG_PBMTE : 0) |
                  (cfg->ext_sstc ? MENVCFG_STCE : 0) |
-                (cfg->ext_smcdeleg ? MENVCFG_CDE : 0) |
+                ((cfg->ext_smcdeleg || cfg->ext_sspesa) ? MENVCFG_CDE : 0) |
                  (cfg->ext_svadu ? MENVCFG_ADUE : 0) |
                  (cfg->ext_ssdbltrp ? MENVCFG_DTE : 0);
 
@@ -3272,7 +3322,7 @@ static RISCVException write_menvcfgh(CPURISCVState *env, int csrno,
      uint64_t mask = (cfg->ext_svpbmt ? MENVCFG_PBMTE : 0) |
                      (cfg->ext_sstc ? MENVCFG_STCE : 0) |
                      (cfg->ext_svadu ? MENVCFG_ADUE : 0) |
-                    (cfg->ext_smcdeleg ? MENVCFG_CDE : 0) |
+                    ((cfg->ext_smcdeleg || cfg->ext_sspesa) ? MENVCFG_CDE : 0) |
                      (cfg->ext_ssdbltrp ? MENVCFG_DTE : 0);
      uint64_t valh = (uint64_t)val << 32;
      bool stce_changed = false;
@@ -6771,6 +6821,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_12_0 },
+    [CSR_SHPMSDATA]      = { "shpmsdata", sspesa, read_shpmsdata,
+                             write_shpmsdata,
+                             .min_priv_ver = PRIV_VERSION_1_12_0 },
 
  #endif /* !CONFIG_USER_ONLY */
  };
#3 lore raw

RFC PATCH 2/2 target/riscv: Capture Sspesa sample PC and counter ID on overflow

In reply to Mayuresh Chitale

On a Zihpm counter overflow and with LCOFIP asserted, record the overflowing counter ID in shpmsdata and arm a capture. When the LCOFI is actually delivered, the shpmspc register is populated with the address of the interrupted instruction.
Capturing shpmspc at LCOFI delivery (rather than at overflow time) ensures the sample PC reflects the correct interrupted instruction.

---
 target/riscv/cpu.h            |  2 ++
 target/riscv/tcg/cpu_helper.c |  6 ++++++
 target/riscv/tcg/csr.c        | 25 +++++++++++++++++++++++++
 target/riscv/tcg/pmu.c        | 27 +++++++++++++++++++++++++++
 4 files changed, 60 insertions(+)
target/riscv/cpu.h +2 -0
index fe56a6afd1..36425c3605 100644
@@ -473,6 +473,8 @@ struct CPUArchState {
     /* Sspesa CSRs */
     target_ulong shpmspc;
     target_ulong shpmsdata;
+    /* Capture shpmspc/shpmsdata only on the first overflow */
+    bool sspesa_capture_pending;
 
     /* Sstc CSRs */
     uint64_t stimecmp;
target/riscv/tcg/cpu_helper.c +6 -0
index 07d9222652..15af3262ed 100644
@@ -2255,6 +2255,12 @@ void riscv_cpu_do_interrupt(CPUState *cs)
         sxlen = 16 << riscv_cpu_sxl(env);
         env->scause = cause | ((target_ulong)async << (sxlen - 1));
         env->sepc = env->pc;
+        /* On LCOFI delivery, latch the sample PC to the interrupted PC. */
+        if (riscv_cpu_cfg(env)->ext_sspesa && env->sspesa_capture_pending) {
+            if (async && cause == IRQ_PMU_OVF) {
+                env->shpmspc = env->pc;
+            }
+        }
         env->stval = tval;
         env->htval = htval;
         env->htinst = tinst;
target/riscv/tcg/csr.c +25 -0
index ea49d28aae..e48c954359 100644
@@ -1228,6 +1228,15 @@ static RISCVException write_minstretcfgh(CPURISCVState *env, int csrno,
     return RISCV_EXCP_NONE;
 }
 
+static bool sspesa_is_captured_ctr(CPURISCVState *env, uint32_t evt_index)
+{
+    if (!riscv_cpu_cfg(env)->ext_sspesa || !env->sspesa_capture_pending) {
+        return false;
+    }
+
+    return (env->shpmsdata & SHPMSDATA_CNTRID) == evt_index;
+}
+
 static RISCVException read_mhpmevent(CPURISCVState *env, int csrno,
                                      target_ulong *val)
 {
@@ -1259,6 +1268,14 @@ static RISCVException write_mhpmevent(CPURISCVState *env, int csrno,
         mhpmevt_val = val & inh_avail_mask;
     }
 
+    /* Enable the sample capture when OF is cleared for the captured counter. */
+    if (sspesa_is_captured_ctr(env, evt_index)) {
+        if ((env->mhpmevent_val[evt_index] & MHPMEVENT_BIT_OF) &&
+            !(mhpmevt_val & MHPMEVENT_BIT_OF)) {
+            env->sspesa_capture_pending = false;
+        }
+    }
+
     env->mhpmevent_val[evt_index] = mhpmevt_val;
     riscv_pmu_update_event_map(env, mhpmevt_val, evt_index);
 
@@ -1289,6 +1306,14 @@ static RISCVException write_mhpmeventh(CPURISCVState *env, int csrno,
     inh_avail_mask |= (riscv_has_ext(env, RVH) &&
                        riscv_has_ext(env, RVS)) ? MHPMEVENTH_BIT_VSINH : 0;
 
+    /* Enable the sample capture when OF is cleared for the captured counter. */
+    if (sspesa_is_captured_ctr(env, evt_index)) {
+        if ((env->mhpmevent_val[evt_index] & MHPMEVENT_BIT_OF) &&
+            !((val & inh_avail_mask) & MHPMEVENTH_BIT_OF)) {
+            env->sspesa_capture_pending = false;
+        }
+    }
+
     env->mhpmevent_val[evt_index] = deposit64(env->mhpmevent_val[evt_index],
                                               32, 32, val & inh_avail_mask);
 
target/riscv/tcg/pmu.c +27 -0
index 38ad2737e1..337e255da4 100644
@@ -147,6 +147,31 @@ void riscv_pmu_update_fixed_ctrs(CPURISCVState *env,
     riscv_pmu_icount_update_priv(env, newpriv, new_virt);
 }
 
+static void riscv_pmu_sspesa_capture(CPURISCVState *env, uint32_t ctr_idx)
+{
+    RISCVCPU *cpu = env_archcpu(env);
+
+    if (!cpu->cfg.ext_sspesa) {
+        return;
+    }
+
+    /*
+     * Only the first overflow updates the sample CSRs. While a capture is
+     * pending, keep the lowest counter ID as required by the spec.
+     */
+    if (env->sspesa_capture_pending) {
+        if ((ctr_idx & SHPMSDATA_CNTRID) <
+            (env->shpmsdata & SHPMSDATA_CNTRID)) {
+            env->shpmsdata = ctr_idx & SHPMSDATA_CNTRID;
+        }
+        return;
+    }
+
+    /* shpmspc is latched later, on LCOFI delivery. */
+    env->shpmsdata = ctr_idx & SHPMSDATA_CNTRID;
+    env->sspesa_capture_pending = true;
+}
+
 int riscv_pmu_incr_ctr(RISCVCPU *cpu, enum riscv_pmu_event_idx event_idx)
 {
     uint32_t ctr_idx;
@@ -191,6 +216,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 {
@@ -383,6 +409,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
Reviewed-by on v1 2/2
#4 lore raw

Re: [RFC PATCH 2/2] target/riscv: Capture Sspesa sample PC and counter ID on overflow

Quoted 9 lines · Mayuresh Chitale

On a Zihpm counter overflow and with LCOFIP asserted, record the overflowing counter ID in shpmsdata and arm a capture. When the LCOFI is actually delivered, the shpmspc register is populated with the address of the interrupted instruction.
Capturing shpmspc at LCOFI delivery (rather than at overflow time) ensures the sample PC reflects the correct interrupted instruction.

---

Quoted 138 lines
  target/riscv/cpu.h            |  2 ++
  target/riscv/tcg/cpu_helper.c |  6 ++++++
  target/riscv/tcg/csr.c        | 25 +++++++++++++++++++++++++
  target/riscv/tcg/pmu.c        | 27 +++++++++++++++++++++++++++
  4 files changed, 60 insertions(+)
target/riscv/cpu.h +2 -0
index fe56a6afd1..36425c3605 100644
@@ -473,6 +473,8 @@ struct CPUArchState {
      /* Sspesa CSRs */
      target_ulong shpmspc;
      target_ulong shpmsdata;
+    /* Capture shpmspc/shpmsdata only on the first overflow */
+    bool sspesa_capture_pending;
 
      /* Sstc CSRs */
      uint64_t stimecmp;
target/riscv/tcg/cpu_helper.c +6 -0
index 07d9222652..15af3262ed 100644
@@ -2255,6 +2255,12 @@ void riscv_cpu_do_interrupt(CPUState *cs)
          sxlen = 16 << riscv_cpu_sxl(env);
          env->scause = cause | ((target_ulong)async << (sxlen - 1));
          env->sepc = env->pc;
+        /* On LCOFI delivery, latch the sample PC to the interrupted PC. */
+        if (riscv_cpu_cfg(env)->ext_sspesa && env->sspesa_capture_pending) {
+            if (async && cause == IRQ_PMU_OVF) {
+                env->shpmspc = env->pc;
+            }
+        }
          env->stval = tval;
          env->htval = htval;
          env->htinst = tinst;
target/riscv/tcg/csr.c +25 -0
index ea49d28aae..e48c954359 100644
@@ -1228,6 +1228,15 @@ static RISCVException write_minstretcfgh(CPURISCVState *env, int csrno,
      return RISCV_EXCP_NONE;
  }
 
+static bool sspesa_is_captured_ctr(CPURISCVState *env, uint32_t evt_index)
+{
+    if (!riscv_cpu_cfg(env)->ext_sspesa || !env->sspesa_capture_pending) {
+        return false;
+    }
+
+    return (env->shpmsdata & SHPMSDATA_CNTRID) == evt_index;
+}
+
  static RISCVException read_mhpmevent(CPURISCVState *env, int csrno,
                                       target_ulong *val)
  {
@@ -1259,6 +1268,14 @@ static RISCVException write_mhpmevent(CPURISCVState *env, int csrno,
          mhpmevt_val = val & inh_avail_mask;
      }
 
+    /* Enable the sample capture when OF is cleared for the captured counter. */
+    if (sspesa_is_captured_ctr(env, evt_index)) {
+        if ((env->mhpmevent_val[evt_index] & MHPMEVENT_BIT_OF) &&
+            !(mhpmevt_val & MHPMEVENT_BIT_OF)) {
+            env->sspesa_capture_pending = false;
+        }
+    }
+
      env->mhpmevent_val[evt_index] = mhpmevt_val;
      riscv_pmu_update_event_map(env, mhpmevt_val, evt_index);
 
@@ -1289,6 +1306,14 @@ static RISCVException write_mhpmeventh(CPURISCVState *env, int csrno,
      inh_avail_mask |= (riscv_has_ext(env, RVH) &&
                         riscv_has_ext(env, RVS)) ? MHPMEVENTH_BIT_VSINH : 0;
 
+    /* Enable the sample capture when OF is cleared for the captured counter. */
+    if (sspesa_is_captured_ctr(env, evt_index)) {
+        if ((env->mhpmevent_val[evt_index] & MHPMEVENT_BIT_OF) &&
+            !((val & inh_avail_mask) & MHPMEVENTH_BIT_OF)) {
+            env->sspesa_capture_pending = false;
+        }
+    }
+
      env->mhpmevent_val[evt_index] = deposit64(env->mhpmevent_val[evt_index],
                                                32, 32, val & inh_avail_mask);
 
target/riscv/tcg/pmu.c +27 -0
index 38ad2737e1..337e255da4 100644
@@ -147,6 +147,31 @@ void riscv_pmu_update_fixed_ctrs(CPURISCVState *env,
      riscv_pmu_icount_update_priv(env, newpriv, new_virt);
  }
 
+static void riscv_pmu_sspesa_capture(CPURISCVState *env, uint32_t ctr_idx)
+{
+    RISCVCPU *cpu = env_archcpu(env);
+
+    if (!cpu->cfg.ext_sspesa) {
+        return;
+    }
+
+    /*
+     * Only the first overflow updates the sample CSRs. While a capture is
+     * pending, keep the lowest counter ID as required by the spec.
+     */
+    if (env->sspesa_capture_pending) {
+        if ((ctr_idx & SHPMSDATA_CNTRID) <
+            (env->shpmsdata & SHPMSDATA_CNTRID)) {
+            env->shpmsdata = ctr_idx & SHPMSDATA_CNTRID;
+        }
+        return;
+    }
+
+    /* shpmspc is latched later, on LCOFI delivery. */
+    env->shpmsdata = ctr_idx & SHPMSDATA_CNTRID;
+    env->sspesa_capture_pending = true;
+}
+
  int riscv_pmu_incr_ctr(RISCVCPU *cpu, enum riscv_pmu_event_idx event_idx)
  {
      uint32_t ctr_idx;
@@ -191,6 +216,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 {
@@ -383,6 +409,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));
          }
      }