"Sscofpmf": Count Overflow & Mode-Based Filtering

Part II Linux boot: recommended Vol. II (Privileged) pp. 147–148 · ~3 min read

The base spec left every bit of mhpmevent to the implementation — so perf record had nothing standard to program. Sscofpmf (“count overflow + privilege mode filtering”) claims six top bits of the existing event CSRs and adds exactly one new register, turning the counters into a samplable, mode-filterable PMU with upstream-Linux support.

mhpmeventN, bits 63:58 (Sscofpmf) — the rest stays event-select

OF63MINH62SINH61UINH60VSINH59VUINH58(event select / WPRI)570
WPRI/RO WLRL WARL RW
Click a field for semantics, reset state, and the minimal-implementation note.

The overflow protocol

A counter overflowing with OF=0 sets OF and raises a count-overflow interrupt request; the requests from all counters merge into LCOFIP — local interrupt 13 in mip/sip (set eventually, not instantly), enabled by LCOFIE, delegated to S via mideleg like any other interrupt. The handler clears LCOFIP itself before servicing. Re-arming a sample period = write the counter to −period and clear OF in one setup.

In the handler, scountovf answers “who fired?”:

scountovf (0xDA0), 32-bit read-only — bit X shadows mhpmeventX.OF (X = 3…31)
Bit X readable when
M-modeAlways.
S/HS-modemcounteren[X] = 1, else reads zero.
VS-modemcounteren[X] AND hcounteren[X], else zero — the same masks that gate the counters themselves.
Dotted-underlined cells have explanations — click one.

Hardware Designer Notes

The filter inputs (current privilege + V bit) and the interrupt plumbing (bit 13 path through mip/mideleg/sip) already exist in any S-mode core; Sscofpmf is almost entirely wiring. The one subtlety is the “eventually sets LCOFIP” clause — a pipelined OF-to-LCOFIP path is legal, but don’t let it reorder past a read of sip that software uses to decide the handler is done.

Minimal Linux-boot hart MUST

  • Per counter: one sticky OF flop, an overflow-carry detect on the implemented width, and the five INH AND-gates on count-enable keyed by effective privilege (incl. V)
  • OR-reduce the per-counter requests into LCOFIP as local interrupt 13, with the eventually-consistent timing the spec allows
  • scountovf as a read mux over the OF bits, masked per mode by mcounteren/hcounteren

MAY simplify / trap-and-emulate

  • Implement only some hpmcounters with overflow capability — but Linux perf sampling expects OF on any counter it can program
  • Defer Sscofpmf on a first boot (perf degrades to non-sampling mode) — RVA23 mandates it, and the gate count is trivial

Check yourself — Sscofpmf

1.Why is there no separate 'overflow interrupt enable' bit — how does OF do double duty?

2.How does a hypervisor keep a guest from profiling the host?

3.In the LCOFI handler, how does S-mode find which of 29 counters overflowed?

3 questions