Riscv Privileged BCN.v7 2
Riscv Privileged BCN.v7 2
Riscv Privileged BCN.v7 2
Allen Baum
Esperanto Technologies.
[email protected]
Hardware
Provides clean split between layers of the software stack
Layer Communicates with via
Application Application Execution Application Binary
Environment (AEE) Interface (ABI)
Operating Supervisor Execution System Binary
System Environment (SEE) Interface (SBI)
Hypervisor Hypervisor Execution Hypervisor Binary
Environment (HEE) Interface (HBI)
ECALL instruction used for the communication
All ISA levels designed to support virtualization
4
Introduction to RISC-V Privileged Architecture
Why a Privileged Architecture?
Profiles
Privileges and Modes
Privileged Features
- CSRs
- Instructions Profiles
Memory Addressing
- Translation
- Protection
Trap Handling
- Exceptions
- Interrupts
Counters
- Time
- Performance
5
Platform Profile Concept: Some basic profiles
Mem
Profile Modes Trust Other
Protect
Embedded
all Low cost:16B each of
without M None
Trusted arch. State/timers/counters
Protection
Embedded
M+ Apps Phys Mem Optional N-extension for
with
U untrusted Protect user int. handling
Protection
Unix-like OS M+ OS Vmem Vaddr size options:
capable S+U Trusted + RWX 32,39,48b
M+
Cloud OS Hypervisor 2-level Vmem Unix+ (Supports >1 OS)
[V](S+
capable Trusted + RWX +new/background CSRs
U)
RiscV has rich set of architectural modes & optional features
A profile is restricted combination of all possible options
See https://github.com/riscv/riscv-platform-specs
6
Why a Privileged Architecture?
Profiles
Privileges and Modes
Privileged Features
- CSRs
Privileges and Modes
- Instructions
Memory Addressing
- Translation
- Protection
Trap Handling
- Exceptions
- Interrupts
Counters
- Time
- Performance
7
RISC-V Privilege Modes
User & 2+ privileged modes (hierarchical)
- User (U-mode), normal and virtualized* (lowest privileges)
- Supervisor (S-mode), normal and virtualized*
- Machine (M-mode) (highest privileges)**
Supported combinations of modes:
- M (simple embedded systems)
- M, U (embedded systems with protection)
- M, S, U (systems running Unix-like operating systems)
- M, [V]S, [V]U (systems running multiple Oses)
Each privileged mode add a few ops, and
Control/Status Registers (CSRs) that control operations
- CSRs accessible only by code running at a specific privilege mode or higher
- There are (often) multiple CSR copies/views for each mode
*Virtualized modes for hypervisor support- not covered here
** A higher (Debug) mode exists: only entered if debug port connected & enabled,
has separate state saving CSRs, but otherwise much like M-mode
8
Why a Privileged Architecture?
Profiles
Privileges and Modes
Privileged Features
- CSRs
Privileged
- Instructions
Memory Addressing
Features:
- Translation
- Protection
Instructions and CSRs
Trap Handling
- Exceptions
- Interrupts
Counters
- Time
- Performance
9
Mode Specific Instructions
Privileged Machine (M-mode) & Supervisor (S-mode) add
instructions to base U-mode ops
- Priv Insts can only be executed from appropriate mode (or higher)
All modes
- ECALL: generates <curr_mode>environment_call exception
- EBREAK: generates breakpoint exception
- FENCE[.I]: synchronizes updates to memory
- <x>RET: returns from a trap *from* the specified mode
- SRET provided only if S-mode is implemented
- URET provided only if U-mode traps supported (N-extension)
S-mode (+M-mode): adds
- SFENCE.VMA: synchs updates to implicitly accessed memory
M-mode: adds
- WFI: stall the current hart until an interrupt needs service
- Is a hint only (could be noop, 10
Mode Specific CSRs
Control/Status Regs (CSRs) have their own address space
– Direct address mode only
Each hart has its own set of 4K CSRs (1K/mode)
CSRs are accessed by dedicated ops
- that can implement atomic swap or bit set/clear
CSRs are mode sensitive
- Can only be accessed by code in appropriate or higher privileged
mode; accesses by lower privilege modes will trap
Many CSRs optional/ have optional fields/mode dependent
- Accesses to non-existent CSRs will trap
- Writes to Read_Only CSRs will trap
- But writes to read_only fields in read/write CSRs are ignored
- Accesses to optional CSRs read zeroes, & (if RW) ignore writes
- Note that optional vs. non-existent
can depend on architecture!
11
CSR address space
CSR Machine- Supervisor- User-
Address Mode Mode Mode Binary Encoding
[11:10] [7:6] [9:8]=11 [9:8]=01 [9:8]=00
00 XX 0b00MM xxxx xxxx
01 0X 0b01MM 0xxx xxxx
01 10 0b01MM 100x xxxx
01 10 Debug only 0b01MM 101x xxxx
01 11 NonStandard 0b01MM 11xx xxxx
10 ~11 0b10MM ~11xx xxxx
10 11 nonStandard 0b10MM 11xx xxxx
11 ~11 RdOnly 0b11MM ~11xx xxxx
(RO) 11 NonStandard Rd Only 0b11MM 11xx xxxx
13
Why a Privileged Architecture?
Profiles
Privileges and Modes
Privileged Features
- CSRs
Memory
- Instructions
Memory Addressing
Addressing:
- Translation
- Protection
Translation
Trap Handling
- Exceptions
- Interrupts
Counters
- Time
- Performance
14
Memory Address Translation:
Virtual Memory
S-mode adds virtual memory page mapping
- Smallest unit of protection/mapping is 4 KB pages
Supports multiple User mode processes w/ separate
address spaces (using Addr_Space_ID field in SATP CSR)
Page tables have multiple levels that are walked:
- 2 levels for RV32 (Sv32)
- 3,4 levels for RV64 (Sv39, Sv48)
- 5,6 levels RSVD (Sv57, Sv64)
Page Table walk can stop at any level to create Superpages
- e.g. for Sv39 2 MB if stopped at 2 levels
- or 1 GB if stopped at 1 levels
HW Page Table walk semantics specified in Priv Mode spec
- But could trap to M-mode for software TLB refill
15
RISC-V Page Table Entries
17
Why a Privileged Architecture?
Profiles
Privileges and Modes
Privileged Features
- CSRs
Memory
- Instructions
Memory Addressing
Addressing:
- Translation
- Protection
Protection
Trap Handling
- Exceptions
- Interrupts
Counters
- Time
- Performance
18
RISC-V Virtual Memory Protections
20
Physical Memory Attributes
RISC-V systems have the concept of Physical Memory
Attributes: platform and implementation specific
- Maps access to a bus transaction type, or an error
PMA is dedicated HW that maps specific address
ranges to certain access attributes, e.g.
- Access widths allowed (e.g. 1/2/4/8/16/64B)
- Alignment restrictions (e.g. can’t cross 2^x byte boundary)
- Idempotency (enabling speculation)
- Ordering (Strong/Weak per Channel)
- Cacheability (incl Wt Thru, Wt Combining, etc)
- Priority (e.g. high/low if conflicting)
- Atomicity allowed (none, swap, logical, arithmetic)
- Allowed access modes (M/S/U/debug)
Some attributes could be configurable
21
Why a Privileged Architecture?
Profiles
Privileges and Modes
Trap Handling:
Privileged Features
- CSRs
Exceptions and Interrupts
- Instructions
Memory Addressing
- Translation
- Protection
Trap Handling
- Exceptions
- Interrupts
Counters
- Time
- Performance
22
Interrupts vs Exceptions
Exceptions: Synchronous events
- Synchronous: caused by a specific instruction execution
Interrupts: Asynchronous events
- not caused by an inst: I/O, timer, SW (from another hart)
Both handled (almost) identically by trapping:
Where - xTVEC CSR holds handler address
to trap - Interrupts optionally vector to xTVEC+4*xCause Trap setup
Mode to
- xI/EDELEG CSRs: select mode to trap into (next slide)
trap into
- xCause CSR (x=new mode) saves cause ID Trap handling
Reason - MSB: interrupt vs. exception, LSBs: interrupt /exception ID code
for trap - xTVAL CSR saves additional information about cause
- This could be an illegal address, or illegal opcode (not for int)
- xEPC CSR saves return Program Counter
How to
- could be next instruction (interrupts) or same inst (enabling retry)
return
from trap
- xSTATUS CSR saves curr Mode/IntEn bits
- xSTATUS[IntEn] cleared
23
Trap Setup
Interrupt/Exception Handler Delegation
Traps always sent to M-mode, but…
Can be delegated to lower priv level, reducing overhead
- Never to a less privileged mode than the one that trapped!
Bits in delegation CSR send traps to next lower priv level
- m[i/e]deleg: MS (or MU if no S-mode & N_extension)
- s[i/e]deleg: SU (if delegated to S-mode & N_extension)
24
Trap Handling
Interrupt/Exception Causes
• <x>cause CSR indicates which interrupt/exception occurred
• Corresponding bit is set in <x>E/IP CSR Interrupts > Exceptions
Trap Priority for simultaneous interrupts/exceptions: M-mode > S-mode > U-mode
Pending[N] > Pending[M] if N>M
Trap
code[62:0]
Exception (Cause[MSB]=0) Interrupt (Cause[MSB]==1)
Special case: Timer & SW
0 Instruction addr misaligned User Software Interrupt interrupt priorities swapped!
1 Instruction access fault Supervisor Software Interrupt
2 Illegal instruction Reserved
Local
3 Breakpoint Machine Software Interrupt
4 Load address misaligned User Timer Interrupt
5 Load access fault Supervisor Timer Interrupt
6 Store/AMO addr misaligned Reserved
7 Store/AMO access fault Machine Timer Interrupt
External
8 Environment call User External Interrupt
9 Supervisor External Interrupt
10 Reserved Reserved
11 Machine External Interrupt
12 Instruction page fault
13 Load page fault
Reserved
14 Reserved
15 Store/AMO page fault
>=16 Reserved Reserved
25
Why a Privileged Architecture?
Profiles
Privileges and Modes
Privileged Features
- CSRs
- Instructions Interrupts
Memory Addressing
- Translation
- Protection
Trap Handling
- Exceptions
- Interrupts
Counters
- Time
- Performance
26
Platform-Level Interrupt Overview
Global Interrupts Local Interrupts
External Hart 0
External External External Timer
Interrupt 1 Interrupt 2 Interrupt N U Software
Timer
S Software
Timer
M Software
PLIC Hart 1
Timer
U Software
Timer
S Software
Timer
M Software
27
RISC-V Interrupt Source Categories
Global (External) Interrupts
- Routed to harts via Platform-Level Interrupt Controller (PLIC)
- Actual source determined by read of PLIC MMIO CSR
Local Interrupts
- Directly connected to one hart, independent of other harts
- Cause determined directly from <x>cause CSR
- Only two standard local interrupts (software, timer)
Any interrupt can target any M/S/U mode
- Except for priority during simultaneous interrupts,
handling is identical
28
External Interrupts
Inputs from a Platform-Level Interrupt Controller (PLIC)
- PLIC targets hart based on hart interrupt threshold & enable,
and interrupt priority
Interrupts can target multiple harts simultaneously
- Harts must arbitrate to determine which services it
- E.g. by racing to read MMIO mapped interrupt source CSR
PLIC labels each output with a privilege mode
- Which can be handled differently using delegation
Interrupts cleared via MMIO mapped LD/ST to PLIC
Software can inject SEIP and UEIP interrupts to support
virtualizing the PLIC by writing CSR directly
29
Software Interrupts
Software interrupt are how harts interrupt each other
- Mechanism for inter-hart interrupts (IPIs)
30
Timer Interrupts
Single M-mode 64b real-time HW timer per system
Single M-mode 64b Time comparator per hart (logically)
NOT CSRs, but MMIO addressed
- Must count at a fixed rate, regardless of core clock or power
- mtime >= mtimecmp causes hart’s MTIP bit to be set
M-mode responsible for virtualizing the single HW timer
and hart comparator for lower-privilege modes
- CSR reads by U-mode will trap & be handled by M-mode
STIP and UTIP CSR bits are handled by M-mode
- ABI/SBI calls to set up timer
- M-mode software writes/clears STIP,UTIP
31
Why a Privileged Architecture?
Profiles
Privileges and Modes
Privileged Features
- CSRs
- Instructions
Memory Addressing
Counters:
- Translation Time and Performance
- Protection
Trap Handling
- Exceptions
- Interrupts
Counters
- Time
- Performance
32
Timers and Counters
RISC-V has several architected Timers and Counters
implemented (mostly) as CSRs
All are 64 bits (split into 2 CSRs for RV32 only)
- Real Time Clock Time, described in Timer Interrupt slide
- U/S-mode read of CSR traps to M-mode, which does MMIO read
- Instructions_Retired InstRet Counter
- M-mode RW, U-mode RO, used for RDINSTRET pseudo-instructions
- Cycles Cycle Counter
- M-mode RW, U-mode RO, used for RDCYCLE pseudo-instructions
- 0..29 HW Performance Monitors mhpmcounters
- each w/corresponding HPMEvent to select what to count
33
Timer/Counter protections
Easily accessible timers have issues
- Lack of reproducibility
- Side channel security attacks (Meltdown, Spectre…)
<x>CounterEn CSRs enables access to the counters
- 1 bit per counter (Time/Cycle/InstRet/HPMCounter[])
- Accessing <x>timer/counter in a mode<x will trap if
corresponding bit in <x>CounterEn is clear for x<y
- Any bit may be optionally hardwired to zero
34
Wrap Up
35
Privileged Architecture is Stable
Latest version is v1.11 draft
36
Implementation Status
Spike and UCB Rocket-Chip conform to v1.11
Linux port is upstreamed and conforms to v1.11
- works with Spike/Rocket
QEMU port is upstreamed and conforms to v1.11
Upstream GCC and binutils ports are compatible
37
Questions?
Specs available at
https://github.com/riscv/riscv-isa-manual
38
Backup
39
<x>Status CSR
In S,U-mode In U-mode <x>PP, <x>PIE, <x>IE
(SD moves to read as zero if curr_mode<x
read as zero read as zero
SV64 only bit 31 for SV32)
63 35 34 33 32 22 21 20 19 18 1716:15 14:13 12 8 7 4 3 0
S SXL UXL S T T T M S M XS FS M – S M – S U M – S U
D D S WV X U P PP PIE IE
R MR M R
V
Previous Current
S-mode XLEN
Previous Per Mode
U-mode XLEN
Privilege Int Enable
Extension state is dirty
Modes
TSR Trap if SRET is executed in S-mode M: MSU
TW Trap if WFI in S-mode exceeds timeout S: SU
TVM Trap on S-mode execution of SFENCE.VMA
FP
0= Off
MXR Allow Ld from pages with RX=01 1= Initial
SUM Allow SMode accessest to U-mode pages 2=Clean
MPRV Use priv mode in MPP for Ld/St 3=Dirty
Summary (other)
0= All Off
1= No Dirty/Clean, some on
2=No dirty, Some clean
3=Some dirty
40
CSRs, Privileged Modes, & Options
CSR
Category CSR Name Comments M-mode S-mode Umode
Accrued Exceptions X(DF)
Floating-Point CRs Dynamic Rounding Mode X(DF)
Ctl & Status Reg (frm + fflags) X(DF)
Vendor ID (o) Encoded JEDEC ID X
(DF) Optional unless D/F extension implemented
Architecture ID (o) MSB==Commercial X
Information (UN) Exists only in Umode & N-extension implemented
Implementation ID (o) X
Hardware_thread_ID Hart 0 must exist (U32)Exists only in Umode & RV32I architecture
Status (S) X HBX (H) X (n) Exact number is implementation dependent
ISAs and Extensions X (I) exist only if RV23I architecture
Exception_Delegation X (SN) BX (H,N) (R) Single register with restricted view
Trap Setup Interrupt_ Delegation X (SN) BX (H,N) (P) Only bits corresponding to <= curr mode are visible
Interrupt Enable (R) X BX (H) X (UN) (H) Swapped with background CSR on VM entry/exit
Trap_Vector_Base_Address (z) X BX (H) X (UN) (o) Optional (Read 0 if unimplemented)
Counter Enable (Z) X X (SN)Exists only if Smode | N-extension implemented
Scratch Register For Trap handlers X BX (H) X (UN) (z) Some bits may be hardwired RdOnly
Exception_Program_Counter X BX (H) X (UN)
(Z) May be hardwired RdOnly zero
Trap_Cause (P,Z) X BX (H) X (UN)
Trap Handling
Trap_Value (Z) X HBX X (UN)
Interrupt pending (R) X BX (H) X (UN)
Addr Translation & Protection(RZ) BX (H)
Protection/Translation Phys Mem Prot Config[3:0] (on) X
Phys Mem Prot Addr[15:0] (on) X * = optional (read as zero)
Cycle_counter For RDCYCLE inst X X Types of CSR fields:
Time For RTIME inst X X - WIRI (Reserved) Writes ignored, Reads ignored
Instr-retired_counter For RDINSTRET inst X X - WPRI (Reserved) Writes preserved, Reads ignored
Perfmon counters[31:3] (Zn) X X - WLRL Write Legal, Read Legal
Counter/Timers
Upper_32b_of_cycle X (I) X (U32)
- WARL Write Any, Read Legal
Upper_32b_of_time X (I) X (U32)
Upper_32b_of_instret X (I) X (U32)
Upper_32b_of_perfmon[31:3] (Z) X (I,n) X(U32n) H= new CSR version added for Hypervisor extension
Counter Setup Perfmon Event selector[31:3] (Z) X(n) B = background CSR added for Hypervisor extension
Debug/Trace Trigger Reg Select X
Debug/Trace trigger data Reg[3:1] X
Debug/Trace Debug Control/Status X
Debug PC X
Debug Scratch Reg X
41
Interrupt/Exception Handler Delegation
Global Enable M = currMode<M + currMode==M & Mstatus.MIE
Global Enable S = currMode<S + currMode== S & Mstatus.SIE
Global Enable U = currMode<U + currMode== U & Mstatus.UIE
Treat as 0 if
Global
no S-mode or Enables
Interrupts > Exceptions
no U-mode or M > S > U-mode
Int
Deleg no N_extension Enable Pending[N] > Pending[M]
M if N>M
PEND M Exception: Timer
Trap & SW interrupt
Exception M
Priority Logic
0
or 1
S New priorities swapped!
Interrupt Mode
S
0
Deleg 1 U Cause
S U
Int Enab }
Int Src
Int Pend
Int Enab
Int Src
Int Pend