Bit 60 of menvcfg
(bit 28 of menvcfgh
) is Counter Delegation Enable
(CDE). When CDE=0, the Smcdeleg and Ssccfg extensions appear to be not
implemented. When CDE=1, the behavior described in the sections below is
enabled. If Smcdeleg is not implemented, CDE is read-only zero.
The mcounteren
register allows M-mode to provide the next-lower
privilege mode with read access to select counters. When the Smcdeleg
extension is enabled, it further allows M-mode to delegate select
counters to S-mode.
The siselect
(and vsiselect
) index range 0x40-0x5F is reserved for
delegated counter access. When a counter i is delegated
(mcounteren
[i]=1 and menvcfg
.CDE=1), the register state associated
with counter i can be read or written via sireg*
, while siselect
holds
0x40+i. The counter state accessible via alias CSRs is shown in
Table 1 below.
siselect value |
sireg |
sireg4 |
sireg2 |
sireg5 |
---|---|---|---|---|
0x40 |
|
|
|
|
0x41 |
See below |
|||
0x42 |
|
|
|
|
0x43 |
|
|
|
|
… |
… |
… |
… |
… |
0x5F |
|
|
|
|
1 Depends on Zicntr support
2 Depends on Zihpm support
3 Depends on Sscofpmf support
4 Depends on Smcntrpmf support
Note
|
|
If extension Smstateen is implemented, refer to extension
Smcsrind/Sscsrind
(upon which this extension depends) for how setting bit 60 of CSR
mstateen0 to zero prevents access to registers siselect
, sireg*
,
vsiselect
, and vsireg*
from privileged modes less privileged than
M-mode, and likewise how setting bit 60 of hstateen0 to zero prevents
access to siselect
and sireg*
(really vsiselect
and vsireg*
) from
VS-mode.
The remaining rules of this section apply only when access to a CSR is not blocked by mstateen0[60] = 0 or hstateen0[60] = 0.
While the privilege mode is M or S and siselect
holds a value in the
range 0x40-0x5F, illegal instruction exceptions are raised for the
following cases:
-
attempts to access any
sireg*
whenmenvcfg
.CDE = 0; -
attempts to access
sireg3
orsireg6
; -
attempts to access
sireg4
orsireg5
when XLEN = 64; -
attempts to access
sireg*
whensiselect
= 0x41, or when the counter selected bysiselect
is not delegated to S-mode (the corresponding bit inmcounteren
= 0).
Note
|
The memory-mapped mtime register is not a performance monitoring
counter to be managed by supervisor software, hence the special
treatment of siselect value 0x41 described above.
|
For each siselect
and sireg*
combination defined in Table 1, the table
further indicates the extensions upon which the underlying counter state
depends. If any extension upon which the underlying state depends is not
implemented, an attempt from M or S mode to access the given state
through sireg*
raises an illegal instruction exception.
If the hypervisor (H) extension is also implemented, then as specified
by extension Smcsrind/Sscsrind, a virtual instruction exception is
raised for attempts from VS-mode or VU-mode to directly access vsiselect
or vsireg*
, or attempts from VU-mode to access siselect
or sireg*
. Furthermore, while vsiselect
holds a value in the range 0x40-0x5F:
-
An attempt to access any
vsireg*
from M or S mode raises an illegal instruction exception. -
An attempt from VS-mode to access any
sireg*
(reallyvsireg*
) raises either an illegal instruction exception ifmenvcfg
.CDE = 0, or a virtual instruction exception ifmenvcfg
.CDE = 1.
If Sscofpmf is implemented, sireg2
and sireg5
provide access only to a
subset of the event selector registers. Specifically, event selector bit
62 (MINH) is read-only 0 when accessed through sireg*
. Similarly, if
Smcntrpmf is implemented, sireg2
and sireg5
provide access only to a
subset of the counter configuration registers. Counter configuration
register bit 62 (MINH) is read-only 0 when accessed through sireg*
.
Smcdeleg/Ssccfg defines a new scountinhibit
register, a masked alias of
mcountinhibit
. For counters delegated to S-mode, the associated
mcountinhibit
bits can be accessed via scountinhibit
. For counters not
delegated to S-mode, the associated bits in scountinhibit
are read-only
zero.
When menvcfg
.CDE=0, attempts to access scountinhibit
raise an illegal
instruction exception. When the Supervisor Counter Delegation extension
is enabled, attempts to access scountinhibit
from VS-mode or VU-mode
raise a virtual instruction exception.
The CSR number for scountinhibit
is 0x120.
For implementations that support Smcdeleg/Ssccfg, Sscofpmf, and the H
extension, when menvcfg
.CDE=1, attempts to access scountovf
from VS-mode
or VU-mode raise a virtual instruction exception.
For implementations that support Smcdeleg, Smcofpmf, and Smaia, the
local counter overflow interrupt (LCOFI) bit (bit 13) in each of CSRs
mvip
and mvien
is implemented and writable.
For implementations that support Smcdeleg/Ssccfg, Smcofpmf/Sscofpmf,
Smaia/Ssaia, and the H extension, the LCOFI bit (bit 13) in each of hvip
and hvien
is implemented and writable.
Note
|
By virtue of implementing Requiring support for the LCOFI bits listed above ensures that virtual
LCOFIs can be delivered to an OS running in S-mode, and to a guest OS
running in VS-mode. The behavior of these bits is described in sections
5.3 (for |
Note
|
In a production “Rich OS” environment, it is expected that M-mode
firmware will delegate counters to supervisor by setting bits in
If M-mode firmware opts to reserve some counters for its own use (by not delegating them), and intends to use them for interrupt-based sampling, it will not delegate LCOFIs to S-mode. Instead, it will need to leverage Smaia in order to deliver virtual LCOFIs to S-mode when an LCOFI is the result of an overflow of a delegated counter (selective delegation).
The operating system (running in S-mode) can determine which counters
have been delegated by writing all ones to Should the OS prefer to count events per context, it can swap the counter, event selector, and counter configuration CSRs, for each counter in use, during context switch. For sampling usages, the OS will initialize a counter with a large
positive value suitably close to overflow via
A hypervisor may use the counters as described above, and can utilize
the xINH bits in the event selectors (via A guest OS or nested hypervisor running in VS-mode may attempt to
access performance counter resources. This extension supports a “trap
and emulate” approach to allowing guest use of counters. Guest access to
counters, event selectors, and counter configuration registers (via
More likely, a hypervisor will not indicate support for Supervisor Counter Delegation to a guest. The hypervisor thereby requires the guest to use the SBI PMU interface. Because the SBI allows multiple CSRs to be written per call, this approach should reduce the number of traps to HS-mode, and thus reduce the virtualization overhead associated with Zicntr and Zihpm use. Virtualization overhead can be further reduced when counters are delegated to HS-mode, which allows hypervisors to directly access delegated counters on guest SBI calls, rather than requiring a nested SBI call from HS-mode to M-mode. When a guest counter overflows and pends an LCOFI, the hypervisor has two options for delivering that interrupt to the guest:
In either case, when the LCOFI or virtual LCOFI traps to VS-mode, the
handler will acknowledge the interrupt by clearing |