1. Control and Status Registers (CSRs)
The SYSTEM major opcode is used to encode all privileged instructions in the RISC-V ISA. These can be divided into two main classes: those that atomically read-modify-write control and status registers (CSRs), which are defined in the Zicsr extension, and all other privileged instructions. The privileged architecture requires the Zicsr extension; which other privileged instructions are required depends on the privileged-architecture feature set.
In addition to the unprivileged state described in Volume I of this manual, an implementation may contain additional CSRs, accessible by some subset of the privilege levels using the CSR instructions described in Volume I. In this chapter, we map out the CSR address space. The following chapters describe the function of each of the CSRs according to privilege level, as well as the other privileged instructions which are generally closely associated with a particular privilege level. Note that although CSRs and instructions are associated with one privilege level, they are also accessible at all higher privilege levels.
Standard CSRs do not have side effects on reads but may have side effects on writes.
1.1. CSR Address Mapping Conventions
The standard RISC-V ISA sets aside a 12-bit encoding space (csr[11:0])
for up to 4,096 CSRs. By convention, the upper 4 bits of the CSR address
(csr[11:8]) are used to encode the read and write accessibility of the
CSRs according to privilege level as shown in Table 1. The top two bits (csr[11:10]) indicate whether the register is read/write (00
,01
, or 10
) or read-only (11
). The next two bits (csr[9:8]) encode the lowest privilege level that can access the CSR.
The CSR address convention uses the upper bits of the CSR address to encode default access privileges. This simplifies error checking in the hardware and provides a larger CSR space, but does constrain the mapping of CSRs into the address space. Implementations might allow a more-privileged level to trap otherwise permitted CSR accesses by a less-privileged level to allow these accesses to be intercepted. This change should be transparent to the less-privileged software. |
Attempts to access a non-existent CSR raise an illegal-instruction exception. Attempts to access a CSR without appropriate privilege level or to write a read-only register also raise illegal-instruction exceptions. A read/write register might also contain some bits that are read-only, in which case writes to the read-only bits are ignored.
Table 1 also indicates the convention to allocate CSR addresses between standard and custom uses. The CSR addresses designated for custom uses will not be redefined by future standard extensions.
Machine-mode standard read-write CSRs 0x7A0
-0x7BF
are reserved for
use by the debug system. Of these CSRs, 0x7A0
-0x7AF
are accessible
to machine mode, whereas 0x7B0
-0x7BF
are only visible to debug mode.
Implementations should raise illegal-instruction exceptions on
machine-mode access to the latter set of registers.
Effective virtualization requires that as many instructions run natively as possible inside a virtualized environment, while any privileged accesses trap to the virtual machine monitor. (Goldberg, 1974) CSRs that are read-only at some lower privilege level are shadowed into separate CSR addresses if they are made read-write at a higher privilege level. This avoids trapping permitted lower-privilege accesses while still causing traps on illegal accesses. Currently, the counters are the only shadowed CSRs. |
1.2. CSR Listing
Table 2-Table 6 list the CSRs that have currently been allocated CSR addresses. The timers, counters, and floating-point CSRs are standard unprivileged CSRs. The other registers are used by privileged code, as described in the following chapters. Note that not all registers are required on all implementations.
CSR Address |
Hex |
Use and Accessibility |
||
[11:10] |
[9:8] |
[7:4] |
||
Unprivileged and User-Level CSRs |
||||
|
|
|
|
Standard read/write |
Supervisor-Level CSRs |
||||
|
|
|
|
Standard read/write |
Hypervisor and VS CSRs |
||||
|
|
|
|
Standard read/write |
Machine-Level CSRs |
||||
|
|
|
|
Standard read/write |
Number | Privilege | Name | Description |
---|---|---|---|
Unprivileged Floating-Point CSRs |
|||
|
URW |
|
Floating-Point Accrued Exceptions. |
Unprivileged Counter/Timers |
|||
|
URO |
|
Cycle counter for RDCYCLE instruction. |
Number | Privilege | Name | Description |
---|---|---|---|
Supervisor Trap Setup |
|||
|
SRW |
|
Supervisor status register. |
Supervisor Configuration |
|||
|
SRW |
|
Supervisor environment configuration register. |
Supervisor Trap Handling |
|||
|
SRW |
|
Scratch register for supervisor trap handlers. |
Supervisor Protection and Translation |
|||
|
SRW |
|
Supervisor address translation and protection. |
Debug/Trace Registers |
|||
|
SRW |
|
Supervisor-mode context register. |
Number | Privilege | Name | Description |
---|---|---|---|
Hypervisor Trap Setup |
|||
|
HRW |
|
Hypervisor status register. |
Hypervisor Trap Handling |
|||
|
HRW |
|
Hypervisor bad guest physical address. |
Hypervisor Configuration |
|||
|
HRW |
|
Hypervisor environment configuration register. |
Hypervisor Protection and Translation |
|||
|
HRW |
|
Hypervisor guest address translation and protection. |
Debug/Trace Registers |
|||
|
HRW |
|
Hypervisor-mode context register. |
Hypervisor Counter/Timer Virtualization Registers |
|||
|
HRW |
|
Delta for VS/VU-mode timer. |
Virtual Supervisor Registers |
|||
|
HRW |
|
Virtual supervisor status register. |
Number | Privilege | Name | Description |
---|---|---|---|
Machine Information Registers |
|||
|
MRO |
|
Vendor ID. |
Machine Trap Setup |
|||
|
MRW |
|
Machine status register. |
Machine Trap Handling |
|||
|
MRW |
|
Scratch register for machine trap handlers. |
Machine Configuration |
|||
|
MRW |
|
Machine environment configuration register. |
Machine Memory Protection |
|||
|
MRW |
|
Physical memory protection configuration. |
Number | Privilege | Name | Description |
---|---|---|---|
Machine Non-Maskable Interrupt Handling |
|||
|
MRW |
|
Resumable NMI scratch register. |
Machine Counter/Timers |
|||
|
MRW |
|
Machine cycle counter. |
Machine Counter Setup |
|||
|
MRW |
|
Machine counter-inhibit register. |
Debug/Trace Registers (shared with Debug Mode) |
|||
|
MRW |
|
Debug/Trace trigger register select. |
Debug Mode Registers |
|||
|
DRW |
|
Debug control and status register. |
1.3. CSR Field Specifications
The following definitions and abbreviations are used in specifying the behavior of fields within the CSRs.
1.3.1. Reserved Writes Preserve Values, Reads Ignore Values (WPRI)
Some whole read/write fields are reserved for future use. Software should ignore the values read from these fields, and should preserve the values held in these fields when writing values to other fields of the same register. For forward compatibility, implementations that do not furnish these fields must make them read-only zero. These fields are labeled WPRI in the register descriptions.
To simplify the software model, any backward-compatible future definition of previously reserved fields within a CSR must cope with the possibility that a non-atomic read/modify/write sequence is used to update other fields in the CSR. Alternatively, the original CSR definition must specify that subfields can only be updated atomically, which may require a two-instruction clear bit/set bit sequence in general that can be problematic if intermediate values are not legal. |
1.3.2. Write/Read Only Legal Values (WLRL)
Some read/write CSR fields specify behavior for only a subset of possible bit encodings, with other bit encodings reserved. Software should not write anything other than legal values to such a field, and should not assume a read will return a legal value unless the last write was of a legal value, or the register has not been written since another operation (e.g., reset) set the register to a legal value. These fields are labeled WLRL in the register descriptions.
Hardware implementations need only implement enough state bits to differentiate between the supported values, but must always return the complete specified bit-encoding of any supported value when read. |
Implementations are permitted but not required to raise an illegal-instruction exception if an instruction attempts to write a non-supported value to a WLRL field. Implementations can return arbitrary bit patterns on the read of a WLRL field when the last write was of an illegal value, but the value returned should deterministically depend on the illegal written value and the value of the field prior to the write.
1.3.3. Write Any Values, Reads Legal Values (WARL)
Some read/write CSR fields are only defined for a subset of bit encodings, but allow any value to be written while guaranteeing to return a legal value whenever read. Assuming that writing the CSR has no other side effects, the range of supported values can be determined by attempting to write a desired setting then reading to see if the value was retained. These fields are labeled WARL in the register descriptions.
Implementations will not raise an exception on writes of unsupported values to a WARL field. Implementations can return any legal value on the read of a WARL field when the last write was of an illegal value, but the legal value returned should deterministically depend on the illegal written value and the architectural state of the hart.
1.4. CSR Field Modulation
If a write to one CSR changes the set of legal values allowed for a
field of a second CSR, then unless specified otherwise, the second CSR’s
field immediately gets an UNSPECIFIED
value from among its new legal values. This
is true even if the field’s value before the write remains legal after
the write; the value of the field may be changed in consequence of the
write to the controlling CSR.
As a special case of this rule, the value written to one CSR may control
whether a field of a second CSR is writable (with multiple legal values)
or is read-only. When a write to the controlling CSR causes the second
CSR’s field to change from previously read-only to now writable, that
field immediately gets an Some CSR fields are, when writable, defined as aliases of other CSR
fields. Let x be such a CSR field, and let y be the CSR field it aliases when writable. If a write to a controlling CSR causes field x to change from previously read-only to now writable, the new value of x is not |
A change to the value of a CSR for this reason is not a write to the affected CSR and thus does not trigger any side effects specified for that CSR.
1.5. Implicit Reads of CSRs
Implementations sometimes perform implicit reads of CSRs. (For
example, all S-mode instruction fetches implicitly read the satp
CSR.)
Unless otherwise specified, the value returned by an implicit read of a
CSR is the same value that would have been returned by an explicit read
of the CSR, using a CSR-access instruction in a sufficient privilege
mode.
1.6. CSR Width Modulation
If the width of a CSR is changed (for example, by changing MXLEN or UXLEN, as described in [xlen-control]), the values of the writable fields and bits of the new-width CSR are, unless specified otherwise, determined from the previous-width CSR as though by this algorithm:
-
The value of the previous-width CSR is copied to a temporary register of the same width.
-
For the read-only bits of the previous-width CSR, the bits at the same positions in the temporary register are set to zeros.
-
The width of the temporary register is changed to the new width. If the new width W is narrower than the previous width, the least-significant W bits of the temporary register are retained and the more-significant bits are discarded. If the new width is wider than the previous width, the temporary register is zero-extended to the wider width.
-
Each writable field of the new-width CSR takes the value of the bits at the same positions in the temporary register.
Changing the width of a CSR is not a read or write of the CSR and thus does not trigger any side effects.
1.7. Explicit Accesses to CSRs Wider than XLEN
If a standard CSR is wider than XLEN bits, then an explicit read of the CSR returns the register’s least-significant XLEN bits, and an explicit write to the CSR modifies only the register’s least-significant XLEN bits, leaving the upper bits unchanged.
Some standard CSRs, such as the counter CSRs of extension
Zicntr, are always 64 bits, even when XLEN=32 (RV32).
For each such 64-bit CSR (for example, counter time
),
a corresponding 32-bit high-half CSR is usually defined with
the same name but with the letter ‘h’ appended at the end (timeh
).
The high-half CSR aliases bits 63:32 of its namesake
64-bit CSR, thus providing a way for RV32 software
to read and modify the otherwise-unreachable 32 bits.
Standard high-half CSRs are accessible only when the base RISC-V instruction set is RV32 (XLEN=32). For RV64 (when XLEN=64), the addresses of all standard high-half CSRs are reserved, so an attempt to access a high-half CSR typically raises an illegal-instruction exception.