Status and Trap Architecture
Two kinds of Traps:
- Exceptions
- Interrupts
- System call (
ecall
) - Program Error
- Illegal Instruction, Alignment Error
- Device Interrupts
- Happens in Supervisor or User mode
Trap Handling
stvec
is a CSR that contains the address of handler code.kernelvec
handles traps in supervisor modeuservec
handles traps in user mode
Supervisor Mode Trapping
sstatus
is the CSR that holds the status of interrupts in supervisor mode.sie
is a bit that holds whether or not interrupts are enabled/disabled.spie
is previous interrupts enabled.spp
is previous privilege level the interrupt happened in, with 0 = User and 1 = Supervisor.
The kernel sets sie
to prevent interrupts to the core.
When a trap occurs, if interrupts are disabled, the trap will wait until later for processing. If interrupts are enabled, then the handler will handle it right away.
The hardware then stores registers to use in the trap:
pc
is saved insepc
.stvec
is saved inpc
.scause
gets register info about the interrupt:- 1 = Timer interrupt
- 8 = System call
- 9 = External Device
- anything else = program exception
stval
stores other info about the fault- the address or instruction of the trap
- a faulting memory address during page faults
- if an illegal instruction, the failing instruction
sstatus.SPP
is assigned the previous mode (0 = user, 1 = supervisor)sstatus.SPIE
is assignedsstatus.SIE
(previous interrupts enabled)sstatus.SIE
is set to 0 to disable interruptsmode
is set to supervisor.
Then the trap executes, and returns to the handler. The handler then restores context back to the running code.
Once the supervisor is done, it calls sret
to do the following:
sstatus.SIE
is assignedsstatus.SPIE
mode
is assignedsstatus.SPP
pc
is assignedsepc
To undo the storage done.
Machine Mode Trapping
Similar to supervisor mode, except for mpp
mstatus
is the CSR that holds the status of interrupts in machine mode.mie
is a bit that holds whether or not interrupts are enabled/disabled.mpie
is previous interrupts enabled.mpp
is previous privilege level the interrupt happened in, with 00 = User and 01 = Supervisor, and 11 = machine mode
The only interrupt is for the timer. Every other interrupt goes to supervisor mode.
Interrupts are always enabled in machine mode
And on an timer interrupt:
- Force a software interrupt to supervisor mode
- re-enable interrupts
- returns to the interrupted code
mtvec
contains the address of the handler
The hardware also stores registers from the interrupted code:
pc
is saved inmepc
.mtvec
is saved inpc
.mcause
andmtval
are ignoredmstatus.SPP
is assigned the previous mode (00 = user, 01 = supervisor, 11 = machine)mstatus.SPIE
is assignedmstatus.SIE
(previous interrupts enabled)sstatus.SIE
is set to 0 to disable interruptsmode
is set to machine.
The handler will then cause an interrupt at the supervisor level, and
then mret
to restore registers
mstatus.SIE
is assignedmstatus.SPIE
mode
is assignedmstatus.SPP
pc
is assignedmepc
Status values
seie
indicates whether device interrupts are on in supervisor mode
stie
indicates whether time interrupts are on in supervisor mode
ssie
indicates whether software interrupts are on in supervisor mode
sip
indicates whether or not an interrupt is pending, 0 = no
interrupt.
Hardware Delegation Registers
medeleg
is for delegating exceptions to supervisor modemideleg
is for delegating interrupts to supervisor mode.
Medeleg
medeleg
has a bunch of bytes:
STORE/AMO
page faultLoad
page faultInstruction
page faultecall
m-modeecall
s-modeecall
u-modeSTORE/AMO
access faultSTORE/AMO
misalignedLoad
access faultLoad
misalignedbreakpoint
illegal instruction
Instr. access fault
Inst. Misaligned
medeleg
is set with csrw medeleg, $val
Mideleg
mideleg
stores interrupt info:
Device interrupt
in m-modeDevice interrupt
in s-modeTimer interrupt
in m-modeTimer interrupt
in s-modeSoftware interrupt
in m-modeSoftware interrupt
in s-mode