Exceptions and interrupts
Exception/interrupt/trap, U/S/M modes, trap CSRs, the entry sequence and mret, direct/vectored, ecall as a syscall, precise exceptions in a pipeline.
Exception, interrupt, trap
Five instructions may be in flight when something goes wrong: a bad page, an illegal word, a kernel request, a timer. RISC-V names: an exception is tied to an instruction of this hart; an interrupt is asynchronous from outside; a trap is the control transfer either causes.
Modes: U (0) < S (1) < M (3). Traps only go up; mret/sret go down. Linux: firmware M, kernel S, programs U.
Worked example. Before the ecall, mstatus has MIE = 1, MPIE = 1, MPP = U: the value 23+27=136=2^3 + 2^7 = 136 =23+27=136= 0x88 in its low bits. On trap entry MIE becomes 0 and MPP stays 00 for U, so mstatus reads 0x80, and the hart jumps to 0x80000100. Step 5 matters: by step 2, mepc points at the ecall itself, not after it (RISC-V Privileged ISA, section 3.3.1). Without the + 4, mret would return to the ecall, which would trap again, forever. For a page fault the handler does the opposite and leaves mepc alone, so the load runs again once the page is mapped. The Code section runs this sequence in a model of the hart.
Vectored mode saves those instructions on every interrupt, which matters most where interrupts are frequent and must be served fast, as in microcontrollers that react to sensors. How much do a few instructions matter? A timer that fires 1000 times a second with a handler of 2000 cycles costs 2×1062 \times 10^62×106 cycles per second, 0.1 % of a 2 GHz core; skipping ten dispatch instructions per interrupt changes the total cost by less than 1 % of that. The point of vectoring is the latency to the first useful instruction, not the throughput.
Trap CSRs
Machine trap CSRs: mtvec (BASE + MODE), mepc, mcause (bit 31 = interrupt), mtval, mstatus (MIE/MPIE/MPP), mie/mip. mscratch is a software spare (often the kernel stack pointer). U-mode ecall is cause 8.
Worked example. With mtvec = 0x80000101, BASE is 0x80000100 and MODE is 1. A machine timer interrupt (code 7) jumps to 0x80000100 + 4 × 7 = 0x8000011c, a machine external interrupt (code 11) to 0x8000012c, and a load page fault (an exception) to 0x80000100 itself. In direct mode all three would land at 0x80000100, and the handler would spend a few instructions reading mcause, testing the interrupt bit and indexing a table of handler addresses before it reached the timer code.
Entry and mret
On trap into M the hardware, atomically: save PC/cause → MPIE←MIE, MIE←0 → MPP←old, mode←M → pc←BASE (or BASE+ if vectored interrupt). GPRs untouched — handler swaps via mscratch.
mret restores mode and MIE, pc←mepc. After ecall, add 4 to mepc or you trap forever; after a page fault, leave mepc so the load retries.
Worked example. A program runs in U-mode with mie.MTIE = 1 when the timer sets mip.MTIP. The third condition holds because the mode is below M, so the hart traps: mcause = 0x80000007 (bit 31 for interrupt, code 7), mepc the next instruction, and with vectored mtvec = 0x80000101 the PC becomes 0x8000011c. In the handler, MIE = 0, so the same pending bit no longer causes a trap; the handler must clear the cause at the source (here by writing a new compare value to the timer), or the interrupt would be taken again right after mret.
Precise exceptions
A precise exception: older instructions finished, younger ones left no architectural effects, mepc names the instruction to restart or skip. Pipelines flush younger stages and complete older ones before taking the trap. Baseline without traps. Checking every load and every device in software would dominate the instruction stream. Traps invert the default: the common path runs unchecked; the rare path enters the kernel through one atomic hardware sequence. Modes nest: U cannot write satp or touch MMIO the kernel left unmapped. Linux system calls are ecall from U with cause 8: libc loads the number into a7, args into a0–a5, executes ecall, expects the result in a0 after sret/mret. Forgetting to add 4 to mepc after ecall loops forever; changing mepc after a page fault skips the faulting instruction. Vectored mode (mtvec.MODE=1) jumps to BASE+4×cause; direct mode always jumps to BASE and reads mcause in software. Handlers often swap sp via mscratch before touching memory. Precise traps flush younger pipeline stages, complete older ones, then redirect — the same kill path as a mispredict, with mepc naming the victim. IRQs arriving while MIE=0 stay pending in mip until re-enabled. Cause codes for timer, software and external interrupts differ in the high bit of mcause (interrupt=1) and the low cause ID. Walk a timer interrupt: mip.MTIP sets when the mtime comparator fires; if mie.MTIE and mstatus.MIE are set, the hart traps. The handler clears the comparator or sets the next deadline, optionally calls a scheduler, then mret. Software interrupts (MSIP) are how one hart pokes another for TLB shootdowns or cross-calls. Delegating traps to S-mode (medeleg/mideleg) lets Linux run mostly without M-mode in the hot path: user page faults and syscalls land in the kernel’s S-mode vectors. Machine-mode firmware still owns early boot, the clock driver glue and any unhandled faults bubbling up. When debugging “stuck in trap”, dump mepc, mcause, mtval and the privilege stack first — those four usually name the bug. Next: Input and output — devices that raise those interrupts. Worked example. The illegal instruction was detected first, in cycle 4, but it is younger than the lw. Had the pipeline trapped in cycle 4, the handler would see mepc = 0x00400108
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Answers are saved on the server and tied to this browser until you sign in.