Traps: Exceptions, Interrupts and the Machine CSRs
A word in the program is no instruction. The processor cannot run it, and it must not go on as if nothing happened. It needs a safe way out, and a way back.
You Will Be Able To
- Tell an exception from an interrupt, and say what both have in common.
- Name the five machine CSRs of a trap, and say what each holds after one.
- Follow a trap from its entry to mret, and say who does each step: hardware or software.
- Lesson
- S2.13
- Time
- 35 min
- Runs on
- Your browser
- Check
- 6 questions
Until now every instruction did its work, and the next one followed. Now one cannot. What the processor does next is fixed in five registers.
Before You Start
You need the illegal instruction from S2.22: the decoder knows when a word is no instruction.
It helps to know interrupts from S0.8. The entry of an interrupt is taught later, on S6.14. This page does not repeat it.
| Question | |
|---|---|
| 1 | What is the difference between an exception and an interrupt? |
| 2 | Which register holds the address of the instruction that failed? |
| 3 | Which register tells the handler why it was called? |
| 4 | What does the processor do by itself when a trap is taken? |
| 5 | What does mret do, and who decides where it returns to? |
The Idea: Stop, Write Down What Happened, Jump
Sometimes the processor must leave the program. It jumps to other code that deals with the event. This forced jump is a processor trap, or trap for short.
A trap has one of two origins. The first is the instruction itself: it cannot be executed. That is an exception.
The second is a signal from outside, such as a timer. It has nothing to do with the instruction. That is an interrupt.
An exception is synchronous: run the program again, and it happens at the same instruction. An interrupt is asynchronous: it can arrive at any instruction.
Both use the same mechanism. The processor writes down where it was, and why. Then it jumps to a fixed address.
The code at that address is the handler. It is ordinary software. It looks at what was written down, and acts.
The notes are kept in special registers beside x0 to x31. Such a register is a CSR: a control and status register.
The handler ends with an instruction named mret. It jumps back to the address that was written down.
New Words
| Word | Meaning |
|---|---|
| Processor trap | A forced jump to the handler, made by the processor and not by a jump instruction. Here: a trap. |
| Exception | A trap caused by the instruction that is being executed. It is synchronous. |
| Interrupt | A trap caused by a signal from outside the program. It is asynchronous. |
| Synchronous, asynchronous | Tied to one instruction of the program, or arriving at any time. |
| Handler | The software that the processor jumps to when it takes a trap. |
| CSR | Control and status register: a register of the processor that is not one of x0 to x31. |
mtvec | The CSR with the address of the handler. Software writes it once, early. |
mepc | The CSR with the address of the instruction that trapped. |
mcause | The CSR with the reason, as a number. Bit 31 is 1 for an interrupt. |
mtval | The CSR with one more detail of the fault: here, the word that was no instruction. |
| MIE | Machine interrupt enable: one bit. Interrupts are taken only while it is 1. |
| MPIE | Machine previous interrupt enable: one bit. It keeps what MIE held before the trap. |
mstatus | The CSR that holds MIE, at bit 3, and MPIE, at bit 7. |
csrr, csrw | The instructions that copy a CSR into a register, and a register into a CSR. |
mret | The instruction that ends a handler: the PC takes mepc, and MIE takes MPIE. |
The letter m in each name stands for machine. S2.14 explains why the names carry it.
One Picture: What the Processor Writes Down
- Processor
- Memory
- Control / FSM
- Bus
- Peripheral
- The fault
The PC holds 0c. The word there is 0000_0000. The decoder says: this is no instruction.
- Three registers record it
mepc takes the address 0c. mcause takes the number 2. mtval takes the word itself.
- The interrupt enable closes
MPIE keeps the old value of MIE. Then MIE becomes 0. No interrupt can disturb the first steps of the handler.
- The jump
The PC takes the address in mtvec, 40. All five steps happen in one clock edge.
- The handler, then mret
The handler reads mcause and decides. Its last instruction, mret, puts mepc into the PC. MIE takes MPIE back.
Read the diagram as text
- PC: holds 0c: the word that cannot run
- mepc: where it happened
- mcause: why: a number
- mtval: one more detail of the fault
- mstatus: the bits MIE and MPIE
- mtvec: holds 40: the address of the handler
- Handler: software at 40, ends with mret
- PC → mepc: 1: mepc takes 0c
- PC → mcause: 2: mcause takes 2
- PC → mtval: 3: mtval takes the word
- PC → mstatus: 4: MPIE takes MIE, MIE = 0
- mtvec → PC: 5: the PC takes 40
- mtvec → Handler: the handler runs
- mepc → PC: mret: the PC takes mepc
The entry is done by hardware, in one step. Everything after it is software, until mret.
| Event | Kind | Why |
|---|---|---|
| A word that is no instruction | Exception | The instruction itself is the cause. |
| A load from an address with no memory | Exception | The same instruction fails every time. |
| The instruction ecall | Exception | The program asks for the handler on purpose. |
| The timer reaches its compare value | Interrupt | The program did nothing to cause it at this instruction. |
| A byte arrives at the UART | Interrupt | It comes from outside, at any time. |
Some books use the word exception for both kinds. RISC-V calls the jump a trap and keeps the two words apart.
The Smallest Example, Worked by Hand
| Address, in hex | Instruction | What it is for |
|---|---|---|
| 000 | addi x5, x0, 64 | the address of the handler |
| 004 | csrw mtvec, x5 | tell the processor where it is |
| 008 | addi x10, x0, 1 | |
| 00c | .word 0x00000000 | not an instruction |
| 010 | addi x10, x10, 1 | |
| 014 | jal x0, 0x14 | stop |
| 040 | csrr x6, mcause | the handler: why did we come here? |
| 044 | csrr x7, mepc | where did it happen? |
| 048 | addi x7, x7, 4 | the instruction behind it |
| 04c | csrw mepc, x7 | |
| 050 | mret | go back |
The first two instructions install the handler. The word at 0c is the fault. The handler is at 40.
Follow it. The first two instructions put 40 into mtvec. The third puts 1 into x10.
At 0c the processor finds a word that is no instruction. It takes a trap.
| CSR | Before | After | Who wrote it |
|---|---|---|---|
mtvec | 0000_0040 | 0000_0040 | The program, at address 04 |
mepc | 0000_0000 | 0000_000c | The processor: the address of the fault |
mcause | 0000_0000 | 0000_0002 | The processor: 2 means illegal instruction |
mtval | 0000_0000 | 0000_0000 | The processor: the word itself, which is 0 here |
mstatus | 0000_1800 | 0000_1800 | The processor: MPIE takes MIE, which was 0 |
The PC takes 40. Nothing else changes: no register x1 to x31, and no memory word.
Now the handler runs. It reads mcause into x6 and mepc into x7. It adds 4 to x7 and writes it back to mepc.
Why add 4? Because mepc points at the faulty word. A return to 0c would meet the same fault again.
Then mret puts mepc, now 10, into the PC. The program goes on behind the fault.
The Rules
Rule 1: An exception belongs to one instruction. An interrupt belongs to none.
So mepc of an exception is the instruction that failed. For an interrupt it is the one that was about to run.
Rule 2: At the entry, mepc takes the address, mcause the reason and mtval the detail.
All three in one clock edge. The instruction that trapped changes nothing else.
Rule 3: At the entry, MPIE takes MIE, and MIE becomes 0.
Interrupts are closed until software opens them again, or until mret.
Rule 4: At the entry, the PC takes the address in mtvec.
An exception always goes to this one address. Interrupts can also use a handler table, with one address for each cause: see S6.14.
Rule 5: mret puts mepc into the PC. MIE takes MPIE, and MPIE becomes 1.
It undoes the entry. It does not look at mcause.
Rule 6: The handler decides where mret returns to.
Behind an instruction that cannot run, it adds 4 to mepc. After an interrupt it changes nothing.
Sources for These Rules
Checked in the library: Patterson and Hennessy, Computer Organization and Design (ARM edition), section 4.9. It has the two kinds of event, the saved address, the cause register and the handler at a fixed address. Its processor uses other register names. Harris and Harris, Digital Design and Computer Architecture (ARM edition), section 6.6.3, has exceptions for another processor. The RISC-V names, bit positions and cause numbers come from the RISC-V privileged specification. That document is not in our library: they are not checked against it. They are checked against the real core: docs/soc/ap_rv32-blueprint.md, sections 5 and 7. Every row on this page was also given by the real core ap_rv32.
Now Run It
The lab at the end of this page is a small model of a processor with traps. It runs the program above.
00000000 | addi x5, x0, 64 | x5 = 00000040
00000004 | csrw mtvec, x5 | mtvec = 00000040
00000008 | addi x10, x0, 1 | x10 = 00000001
0000000c | .word 0x00000000 | TRAP mcause=2 mepc=0000000c mtval=00000000, next is 00000040
00000040 | csrr x6, mcause | x6 = 00000002
00000044 | csrr x7, mepc | x7 = 0000000c
00000048 | addi x7, x7, 4 | x7 = 00000010
0000004c | csrw mepc, x7 | mepc = 00000010
00000050 | mret | next is 00000010, MIE=0
00000010 | addi x10, x10, 1 | x10 = 00000002
00000014 | jal x0, 0x14 | the program stops here
x6=2 x7=00000010 x10=2 instructions=9 traps=1Rule 2 at work: read the row at 0000000c. It is marked as a trap, with mcause 2 and mepc 0000000c. The word was 0, so mtval is 0.
Rule 4 at work: the same row ends with "next is 00000040". The next row is the first instruction of the handler.
The handler: the row at 40 reads mcause, and x6 becomes 2. The rows at 44 to 4c move mepc from 0c to 10.
Rule 5 at work: the row at 50 is mret. It says "next is 00000010". The row after it is the instruction behind the fault.
The last line counts 9 instructions and 1 trap. The faulty word is not counted as an instruction: it never ran.
Now the two bits of mstatus. The next program opens MIE first, then calls the handler with ecall.
00000000 | addi x5, x0, 64 | x5 = 00000040
00000004 | csrw mtvec, x5 | mtvec = 00000040
00000008 | addi x5, x0, 8 | x5 = 00000008
0000000c | csrs mstatus, x5 | mstatus = 00001808
00000010 | ecall | TRAP mcause=11 mepc=00000010 mtval=00000000, next is 00000040
00000040 | csrr x8, mstatus | x8 = 00001880
00000044 | csrr x6, mcause | x6 = 0000000b
00000048 | csrr x7, mepc | x7 = 00000010
0000004c | addi x7, x7, 4 | x7 = 00000014
00000050 | csrw mepc, x7 | mepc = 00000014
00000054 | mret | next is 00000014, MIE=1
00000014 | csrr x11, mstatus | x11 = 00001888
00000018 | jal x0, 0x18 | the program stops here
x8=00001880 x11=00001888 x6=11 instructions=11 traps=1Before the trap, mstatus is 0000_1808: bit 3, MIE, is 1. Inside the handler x8 reads 0000_1880.
Rule 3 at work: bit 3 went to 0, and bit 7, MPIE, took the 1. After mret, x11 reads 0000_1888: MIE is back.
The 18 in front is a field of two bits that is always 11 on this core. S2.14 and S2.30 explain it.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Save the PC in a CSR | Save it on the stack, as some processors do | No memory access at the entry, so the entry cannot fail. One level only: S6.14 shows the cost. |
| One handler address for every exception | An address for each cause | One register. The handler must read mcause and branch. |
| The cause as a number in mcause | The handler works it out from the instruction | A read of one CSR. The numbers must be agreed by everybody. |
| The handler changes mepc | The processor always returns behind the instruction | Software can also repeat the instruction, after it repaired the cause. |
| Close MIE at the entry | Leave interrupts open | The first instructions of the handler cannot be interrupted before mepc is safe. |
Why does software get so much to do? Because the right answer differs. One fault ends a program, and another is repaired and repeated.
The Trap: No Handler Was Installed
After reset mtvec holds 0. Suppose the program forgets to write it, and then meets a fault.
The processor does what the rules say. It jumps to the address in mtvec. That is address 0: the start of the program.
00000000 | addi x10, x10, 1 | x10 = 00000001
00000004 | .word 0x00000000 | TRAP mcause=2 mepc=00000004 mtval=00000000, next is 00000000
00000000 | addi x10, x10, 1 | x10 = 00000002
00000004 | .word 0x00000000 | TRAP mcause=2 mepc=00000004 mtval=00000000, next is 00000000
00000000 | addi x10, x10, 1 | x10 = 00000003
00000004 | .word 0x00000000 | TRAP mcause=2 mepc=00000004 mtval=00000000, next is 00000000
00000000 | addi x10, x10, 1 | x10 = 00000004
00000004 | .word 0x00000000 | TRAP mcause=2 mepc=00000004 mtval=00000000, next is 00000000
00000000 | addi x10, x10, 1 | x10 = 00000005
00000004 | .word 0x00000000 | TRAP mcause=2 mepc=00000004 mtval=00000000, next is 00000000
00000000 | addi x10, x10, 1 | x10 = 00000006
00000004 | .word 0x00000000 | TRAP mcause=2 mepc=00000004 mtval=00000000, next is 00000000
still running after 6 instructions and 6 traps
x10=6 mepc=00000004 mcause=2 instructions=6 traps=6Read the rows. The program runs its first instruction, traps at 04, and starts again at 00. And again.
Nothing looks broken. x10 counts up, and the fault is never reported. The row at 08 is never reached.
So the first work of start-up code is to write mtvec. A fault before that write restarts the program without a word.
At Scale
This section looks ahead. One fault and five registers are the whole mechanism. Larger systems build much on it.
Interrupts use the same five registers
S6.4 has the enable bits and S6.14 the entry with a handler table. S6.15 measures the delay.
The other causes
S2.29 goes through the causes of ap_rv32 one by one, with ecall and ebreak. It also shows the trap inside the pipeline.
A handler must save what it uses
The handler here overwrites x6 and x7. A real one saves every register it touches, as a called function does on its stack frame.
An operating system lives here
In a large system the handler belongs to the operating system. A program that faults is ended, and the others go on. That is common practice.
Try It
Run the lab. Find the row that is marked as a trap, and the row with mret.
Change the faulty row to i(WORD, 0, 0, 32'hffffffff). Which value on the row of the trap changes?
Remove the row with ADDI 7, 7, 4 from the handler. What does the lab print now, and why does it never stop by itself?
00000000 | addi x5, x0, 64 | x5 = 00000040 00000004 | csrw mtvec, x5 | mtvec = 00000040 00000008 | addi x10, x0, 1 | x10 = 00000001 0000000c | .word 0x00000000 | TRAP mcause=2 mepc=0000000c mtval=00000000, next is 00000040 00000040 | csrr x6, mcause | x6 = 00000002 00000044 | csrr x7, mepc | x7 = 0000000c 00000048 | addi x7, x7, 4 | x7 = 00000010 0000004c | csrw mepc, x7 | mepc = 00000010 00000050 | mret | next is 00000010, MIE=0 00000010 | addi x10, x10, 1 | x10 = 00000002 00000014 | jal x0, 0x14 | the program stops here x6=2 x7=00000010 x10=2 instructions=9 traps=1Check Yourself
Common Mistakes
Wrong Return from an exception without changing mepc.
mepc points at the instruction that failed. It fails again, for ever.
Fix Add 4 to mepc, or repair the cause first.
Wrong Expect the processor to save x1 to x31 at a trap.
It saves the PC and one status bit, and nothing more.
Fix The handler saves every register that it uses.
Wrong Write mtvec late in the start-up code.
A fault before the write jumps to address 0 and restarts the program.
Fix Write mtvec in the first instructions.
Remember
- A trap is a forced jump. An exception comes from the instruction, an interrupt from outside.
- At the entry the processor writes mepc, mcause and mtval, moves MIE to MPIE, and jumps to mtvec.
- mret jumps to mepc and gives MIE back. The handler decides whether mepc is changed first.
- mtvec is 0 after reset. Write it before anything can fault.