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
stvecis a CSR that contains the address of handler code.kernelvechandles traps in supervisor modeuservechandles traps in user mode
Supervisor Mode Trapping
sstatusis the CSR that holds the status of interrupts in supervisor mode.sieis a bit that holds whether or not interrupts are enabled/disabled.spieis previous interrupts enabled.sppis 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:
pcis saved insepc.stvecis saved inpc.scausegets register info about the interrupt:- 1 = Timer interrupt
- 8 = System call
- 9 = External Device
- anything else = program exception
stvalstores 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.SPPis assigned the previous mode (0 = user, 1 = supervisor)sstatus.SPIEis assignedsstatus.SIE(previous interrupts enabled)sstatus.SIEis set to 0 to disable interruptsmodeis 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.SIEis assignedsstatus.SPIEmodeis assignedsstatus.SPPpcis assignedsepc
To undo the storage done.
Machine Mode Trapping
Similar to supervisor mode, except for mpp
mstatusis the CSR that holds the status of interrupts in machine mode.mieis a bit that holds whether or not interrupts are enabled/disabled.mpieis previous interrupts enabled.mppis 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:
pcis saved inmepc.mtvecis saved inpc.mcauseandmtvalare ignoredmstatus.SPPis assigned the previous mode (00 = user, 01 = supervisor, 11 = machine)mstatus.SPIEis assignedmstatus.SIE(previous interrupts enabled)sstatus.SIEis set to 0 to disable interruptsmodeis set to machine.
The handler will then cause an interrupt at the supervisor level, and
then mret to restore registers
mstatus.SIEis assignedmstatus.SPIEmodeis assignedmstatus.SPPpcis 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
medelegis for delegating exceptions to supervisor modemidelegis for delegating interrupts to supervisor mode.
Medeleg
medeleg has a bunch of bytes:
STORE/AMOpage faultLoadpage faultInstructionpage faultecallm-modeecalls-modeecallu-modeSTORE/AMOaccess faultSTORE/AMOmisalignedLoadaccess faultLoadmisalignedbreakpointillegal instructionInstr. access faultInst. Misaligned
medeleg is set with csrw medeleg, $val
Mideleg
mideleg stores interrupt info:
Device interruptin m-modeDevice interruptin s-modeTimer interruptin m-modeTimer interruptin s-modeSoftware interruptin m-modeSoftware interruptin s-mode