Causes of a Trap and the Precise Trap in the Pipeline
A pipeline holds five instructions at once. One of them faults. Which ones still finish, which ones vanish, and how does the handler learn what went wrong?
You Will Be Able To
- Read the cause of an exception from mcause, and the detail from mtval.
- Say what ecall and ebreak are for, and where their handler returns to.
- Explain what makes a trap precise, and count what it costs in the five-stage pipeline.
- Lesson
- S2.29
- Time
- 35 min
- Runs on
- Your browser
- Check
- 6 questions
S2.13 showed one fault and one handler. A real program can fail in many ways, and it fails in the middle of a pipeline.
Before You Start
You need the trap from S2.13: the entry, mepc, mcause, mtval and mret.
You need the flush and the pipeline grid from S2.10.
| Question | |
|---|---|
| 1 | Which number does mcause hold for an illegal instruction, and which for an ecall? |
| 2 | What does mtval hold for a load at a bad address? |
| 3 | What are ecall and ebreak for? |
| 4 | In which stage does ap_rv32 take a trap, and what happens to the instructions behind it? |
| 5 | How many cycles does a trap cost on ap_rv32? |
The Idea: Many Causes, One Exact Place
An instruction can fail in several ways. The word may be no instruction. A load may use an address with no memory. A jump may go to half an instruction.
Each way has its own number in mcause. And mtval holds the detail that the handler will want: the bad word, or the bad address.
Two instructions fail on purpose. One is named ecall: it asks the handler for a service. The other is named ebreak: it stops the program at a chosen place, for a person who wants to look. Neither is an error.
Now the pipeline. When an instruction faults, older instructions are still finishing, and younger ones are already fetched.
The handler must find a clean state. Every instruction before the fault has run completely. The faulting one and all after it have changed nothing.
A trap that keeps this promise is a precise trap. To the handler, it looks as if instructions ran one at a time.
ap_rv32 keeps it with one rule. A trap is taken at one stage only, MEM. That stage is the commit point.
New Words
| Word | Meaning |
|---|---|
ecall | Environment call: an instruction that traps on purpose, to ask the handler for a service. |
| Debugger | A tool with which a person stops a program and looks at its registers and memory. |
| Breakpoint | A place where a program shall stop, so that a person can look at it. |
ebreak | An instruction that traps on purpose, to stop at this place. It is used for a breakpoint. |
| Misaligned | An address that is not a multiple of the size of the thing at it: 4 for a word or an instruction. |
| Bus access fault | A trap because the memory or the bus refused the address. Short: an access fault. |
| Precise trap | A trap after which every older instruction has finished, and no younger one has changed anything. |
| Commit point | The stage after which an instruction can no longer be thrown away. In ap_rv32: MEM. |
One Picture: The Fault Travels as a Mark
- Memory
- Control / FSM
- Processor
- Bus
- Cycle 8: found in ID
The word at 0c is in ID. The decoder says: no instruction. Nothing happens yet. The word gets a mark and moves on.
- Cycle 9: in EX
The marked word is in EX. Behind it, the instructions at 10 and 14 are in ID and IF. The instruction at 08 is ahead, in MEM.
- Cycle 10: in MEM, the trap is taken
The marked word is the oldest that has not finished. Now the trap is taken. The instruction at 08, ahead of it, is in WB and completes.
- Cycle 10, at the clock edge
The CSRs take their values. The instructions at 10, 14 and 18, in EX, ID and IF, become bubbles. The PC takes 40.
- Cycle 11: the handler is fetched
IF holds 40, the first instruction of the handler. ID, EX and MEM hold bubbles: three cycles are lost.
Read the diagram as text
- IF: fetch
- ID: decode: finds the illegal word
- EX: execute
- MEM: the commit point
- WB: write back
- Trap CSRs: mepc, mcause, mtval, mstatus
- PC: takes the handler address
- IF → ID: IF/ID
- ID → EX: ID/EX, with a mark: illegal
- EX → MEM: EX/MEM, the mark travels
- MEM → WB: MEM/WB
- MEM → Trap CSRs: mepc = 0c, mcause = 2
- MEM → ID: flush IF, ID and EX
- Trap CSRs → PC: the PC takes mtvec: 40
The fault is found early and acted on late. In between it is only a mark that travels with the instruction.
| mcause | Name | Raised by | mtval holds |
|---|---|---|---|
| 0 | Instruction address misaligned | A taken branch or a jump to an address with bit 1 set | The target address |
| 1 | Instruction access fault | A fetch that the bus refuses | The address of the instruction |
| 2 | Illegal instruction | A word that is no instruction, or a CSR instruction that is not allowed | The word |
| 3 | Breakpoint | ebreak | The address of the ebreak |
| 4 | Load address misaligned | A load of a word at an address that is not a multiple of 4 | The address of the data |
| 5 | Load access fault | A load that the bus refuses | The address of the data |
| 6 | Store address misaligned | A store, as for cause 4 | The address of the data |
| 7 | Store access fault | A store that the bus refuses | The address of the data |
| 11 | Environment call | ecall | 0 |
The numbers are those of RISC-V, not checked against its specification. Half words follow the same rule with a multiple of 2.
The Smallest Example, Worked by Hand
| Address, in hex | Instruction | What it is for |
|---|---|---|
| 000 | addi x5, x0, 128 | |
| 004 | csrw mtvec, x5 | |
| 008 | lui x20, 0x80000 | x20: the list, at 8000_0000 |
| 00c | lui x8, 0x80001 | x8: data, at 8000_1000 |
| 010 | lui x9, 0x40000 | x9: an address with no memory |
| 014 | ecall | a call for the handler |
| 018 | ebreak | a breakpoint |
| 01c | .word 0xffffffff | not an instruction |
| 020 | csrr x11, 0x123 | a CSR that does not exist |
| 024 | csrw mhartid, x11 | a write to a read-only CSR |
| 028 | lw x11, 2(x8) | a word at an address that is not a multiple of 4 |
| 02c | sw x11, 1(x8) | the same for a store |
| 030 | lw x11, 0(x9) | a load where no memory is |
| 034 | sw x11, 0(x9) | a store where no memory is |
| 038 | jalr x1, 2(x9) | a jump to an address that is not a multiple of 4 |
| 03c | jal x0, 0x3c | stop |
x8 points into data memory, at 8000_1000. x9 holds 4000_0000, where no memory is. The handler, at 80, is below.
Take the load at 28: lw x11, 2(x8). Its address is 8000_1000 plus 2. A word must start at a multiple of 4.
So it traps. Find the row in the table: load address misaligned, cause 4. mtval takes the address, 8000_1002.
Now the load at 30: lw x11, 0(x9). The address 4000_0000 is a multiple of 4. But no memory answers there.
The bus refuses the read. That is cause 5, a load access fault. mtval takes 4000_0000.
And the row at 20: csrr x11, 0x123. No CSR has this address. The word is treated as no instruction: cause 2, and mtval takes the word.
| Address | Instruction | mcause | mtval |
|---|---|---|---|
| 14 | ecall | 11 | 0 |
| 18 | ebreak | 3 | 0000_0018 |
| 28 | lw x11, 2(x8) | 4 | 8000_1002 |
| 30 | lw x11, 0(x9) | 5 | 4000_0000 |
| 38 | jalr x1, 2(x9) | 0 | 4000_0002 |
The jump at 38 traps itself, before anything is fetched from the bad target. mepc is 38.
The Rules
Rule 1: mcause names the kind of fault, and mtval gives its detail.
An address for a fault about an address. The word for an illegal instruction.
Rule 2: ecall and ebreak trap every time, and mepc points at them.
A handler that serves an ecall returns behind it: mepc plus 4.
Rule 3: A fault is found where it happens, and acted on in MEM.
An illegal word is found in ID, a bad data address in EX, a refused access in MEM. Until MEM it is a mark.
Rule 4: At the trap, every older instruction finishes.
The instruction in WB completes. It was before the fault in program order.
Rule 5: At the trap, the three younger instructions are flushed.
Those in EX, ID and IF become bubbles. The trapping instruction itself writes no register and no memory word.
Rule 6: The oldest fault wins.
Each fault waits for MEM, so the instruction that comes first in the program traps first.
Rule 7: A trap costs 3 cycles on ap_rv32.
Three fetched instructions are lost. An mret and a CSR instruction also leave from MEM, and cost 3 each.
Sources for These Rules
Checked in the library: Patterson and Hennessy, Computer Organization and Design (ARM edition), section 4.9. It treats an exception in a pipeline as a control hazard, flushes the instructions behind it, and lets the earliest instruction win. Its pipeline acts in another stage than ours. The cause numbers, the names ecall and ebreak and the contents of mtval come from the RISC-V privileged specification. That document is not in our library: they are not checked against it. For ap_rv32 they are in docs/soc/ap_rv32-blueprint.md, sections 7.1 and 7.2, and its mtval values are a choice of this core. Every pipeline row on this page is recorded from the real core.
Now Run It
The lab at the end of this page runs the program above. Its handler writes each cause to a list, and returns behind the instruction.
00000014 | ecall | TRAP mcause=11 mepc=00000014 mtval=00000000, next is 00000080
00000018 | ebreak | TRAP mcause=3 mepc=00000018 mtval=00000018, next is 00000080
0000001c | .word 0xffffffff | TRAP mcause=2 mepc=0000001c mtval=ffffffff, next is 00000080
00000020 | csrr x11, 0x123 | TRAP mcause=2 mepc=00000020 mtval=123025f3, next is 00000080
00000024 | csrw mhartid, x11 | TRAP mcause=2 mepc=00000024 mtval=f1459073, next is 00000080
00000028 | lw x11, 2(x8) | TRAP mcause=4 mepc=00000028 mtval=80001002, next is 00000080
0000002c | sw x11, 1(x8) | TRAP mcause=6 mepc=0000002c mtval=80001001, next is 00000080
00000030 | lw x11, 0(x9) | TRAP mcause=5 mepc=00000030 mtval=40000000, next is 00000080
00000034 | sw x11, 0(x9) | TRAP mcause=7 mepc=00000034 mtval=40000000, next is 00000080
00000038 | jalr x1, 2(x9) | TRAP mcause=0 mepc=00000038 mtval=40000002, next is 00000080
the list: 11 3 2 2 2 4 6 5 7 0
instructions=75 traps=10Rule 1 at work: compare the rows with your table. The store at 2c has cause 6 and its address in mtval. The word at 1c has cause 2 and the word in mtval.
The two rows at 20 and 24 are CSR instructions that are not allowed. One names a CSR that does not exist. The other writes a read-only one.
Rule 2 at work: the ecall and the ebreak trap like any fault. The last line shows 75 instructions and 10 traps.
Now the pipeline. These rows are not from the lab. They are the stages of the real core ap_rv32, for the program of S2.13.
cy | IF ID EX MEM WB | what happens
7 | 0c 08 -- -- -- |
8 | 10 0c 08 -- -- |
9 | 14 10 0c 08 -- |
10 | 18 14 10 0c 08 | x10=1 MEM: go 40
11 | 40 -- -- -- 0c | trap 2
12 | 44 40 -- -- -- |
13 | 48 44 40 -- -- |
14 | 4c 48 44 40 -- | MEM: go 44
15 | 44 -- -- -- 40 | x6=2Rule 3 at work: the word at 0c is in ID in row 8 and in EX in row 9. No note marks it. In row 10 it is in MEM: go 40.
Rule 4 at work: in row 10 the instruction at 08 is in WB. It writes x10 = 1. It was older than the fault.
Rule 5 at work: row 11 has three pairs of dashes. The instructions at 10, 14 and 18 are gone. IF holds 40.
Row 14 shows go 44. The instruction at 40 is a CSR read. On this core every CSR instruction leaves from MEM and fetches again behind itself.
Rule 7 at work: the whole run has 11 trace records and 6 such exits from MEM. It takes 33 cycles: 11 plus 4 plus 18.
Now two faults next to each other. A load at 14 uses an address with no memory. Right behind it, at 18, is a word that is no instruction.
cy | IF ID EX MEM WB | what happens
10 | 18 14 10 0c 08 | x20=80000000
11 | 1c 18 14 10 0c | x9=40000000
12 | 20 1c 18 14 10 | x10=1 wait
13 | 20 1c 18 14 10 | MEM: go 80
14 | 80 -- -- -- 14 | trap 5
15 | 84 80 -- -- -- |Rule 6 at work: in row 11 the illegal word is in ID, and found. In row 12 the load is in MEM, and the bus answers with an error. The note says wait.
In row 13 the load traps: cause 5. The illegal word, in EX, is flushed with the rest. It was found first, and it does not win.
The handler returns behind the load. Then the word at 18 is fetched again and traps for itself. The list of this run is 5, then 2.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Act on every fault in one stage | Act where the fault is found | Faults are served in program order with no sorting logic. A fault found in ID waits two cycles. |
| That stage is MEM | WB, one stage later | A store writes memory in MEM, so the decision must fall there. One instruction less is flushed. |
| Flush everything younger | Let fetched instructions finish | The handler sees a clean state. Three fetches are lost. |
| A number for each cause | One "fault" for all | The handler branches on mcause at once. The numbers are fixed for every RISC-V core. |
| mtval holds the address or the word | The handler decodes the instruction at mepc | No decoder in software. One more CSR to write at the entry. |
| ecall is a trap | A jump instruction into the handler | One mechanism for every way in. The cost of a trap for every service call. |
Why must a store wait for the decision? A register write can be held back until WB. A word written to memory, or to a device, cannot be taken back.
The real core shows it. A store right behind a faulty word had not happened when the handler looked. A store right before it had.
The Trap: Returning to an Ecall as If It Were an Interrupt
After an interrupt, a handler returns to mepc as it is. The interrupted instruction never ran, and now it must.
An ecall is different. It did its work by trapping. Returning to it runs it again.
00000000 | addi x5, x0, 128 | x5 = 00000080
00000004 | csrw mtvec, x5 | mtvec = 00000080
00000008 | addi x10, x0, 7 | x10 = 00000007
0000000c | ecall | TRAP mcause=11 mepc=0000000c mtval=00000000, next is 00000080
00000080 | addi x11, x11, 1 | x11 = 00000001
00000084 | mret | next is 0000000c, MIE=0
0000000c | ecall | TRAP mcause=11 mepc=0000000c mtval=00000000, next is 00000080
00000080 | addi x11, x11, 1 | x11 = 00000002
00000084 | mret | next is 0000000c, MIE=0
0000000c | ecall | TRAP mcause=11 mepc=0000000c mtval=00000000, next is 00000080
00000080 | addi x11, x11, 1 | x11 = 00000003
00000084 | mret | next is 0000000c, MIE=0
0000000c | ecall | TRAP mcause=11 mepc=0000000c mtval=00000000, next is 00000080
00000080 | addi x11, x11, 1 | x11 = 00000004
still running after 10 instructions and 4 traps
x10=7 x11=4 instructions=10 traps=4Read it. Each mret says "next is 0000000c", and the row after it is the ecall again. x11 counts 4 calls. The program made one.
So a handler must look at mcause first. Bit 31 tells it: an interrupt returns to mepc, an ecall to mepc plus 4.
At Scale
This section looks ahead. Three flushed instructions are a small price. Larger cores pay more, and need more machinery to stay precise.
Interrupts enter the same way
An interrupt is attached to an instruction in the pipeline and waits for MEM like a fault. S6.15 measures the delay.
Cores that reorder
Some cores execute instructions in another order than the program has. They need a buffer that puts results back in order. S2.32 builds one.
A debugger uses ebreak
To stop a program at a place, a debugger writes an ebreak over the instruction there. That is common practice.
A verification team needs this table
Each cause, with its mepc and mtval, is a row of a test plan. The real core reports all three for every trap, on a port that S2.16 shows.
Try It
Run the lab. Find the ten rows that are marked as a trap, and compare each with the table of causes.
Change the row at 28 to i(LW, 11, 8, 4). Which cause disappears from the list?
Change the row at 38 to i(JALR, 1, 9, 0). The jump is now legal. Which cause does the list end with, and what is mepc?
00000000 | addi x5, x0, 128 | x5 = 00000080 00000004 | csrw mtvec, x5 | mtvec = 00000080 00000008 | lui x20, 0x80000 | x20 = 80000000 0000000c | lui x8, 0x80001 | x8 = 80001000 00000010 | lui x9, 0x40000 | x9 = 40000000 00000014 | ecall | TRAP mcause=11 mepc=00000014 mtval=00000000, next is 00000080 00000080 | csrr x6, mcause | x6 = 0000000b 00000084 | sw x6, 0(x20) | mem[80000000] = 0000000b 00000088 | addi x20, x20, 4 | x20 = 80000004 0000008c | csrr x7, mepc | x7 = 00000014 00000090 | addi x7, x7, 4 | x7 = 00000018 00000094 | csrw mepc, x7 | mepc = 00000018 00000098 | mret | next is 00000018, MIE=0 00000018 | ebreak | TRAP mcause=3 mepc=00000018 mtval=00000018, next is 00000080 00000080 | csrr x6, mcause | x6 = 00000003 00000084 | sw x6, 0(x20) | mem[80000004] = 00000003 00000088 | addi x20, x20, 4 | x20 = 80000008 0000008c | csrr x7, mepc | x7 = 00000018 00000090 | addi x7, x7, 4 | x7 = 0000001c 00000094 | csrw mepc, x7 | mepc = 0000001c 00000098 | mret | next is 0000001c, MIE=0 0000001c | .word 0xffffffff | TRAP mcause=2 mepc=0000001c mtval=ffffffff, next is 00000080 00000080 | csrr x6, mcause | x6 = 00000002 00000084 | sw x6, 0(x20) | mem[80000008] = 00000002 00000088 | addi x20, x20, 4 | x20 = 8000000c 0000008c | csrr x7, mepc | x7 = 0000001c 00000090 | addi x7, x7, 4 | x7 = 00000020 00000094 | csrw mepc, x7 | mepc = 00000020 00000098 | mret | next is 00000020, MIE=0 00000020 | csrr x11, 0x123 | TRAP mcause=2 mepc=00000020 mtval=123025f3, next is 00000080 00000080 | csrr x6, mcause | x6 = 00000002 00000084 | sw x6, 0(x20) | mem[8000000c] = 00000002 00000088 | addi x20, x20, 4 | x20 = 80000010 0000008c | csrr x7, mepc | x7 = 00000020 00000090 | addi x7, x7, 4 | x7 = 00000024 00000094 | csrw mepc, x7 | mepc = 00000024 00000098 | mret | next is 00000024, MIE=0 00000024 | csrw mhartid, x11 | TRAP mcause=2 mepc=00000024 mtval=f1459073, next is 00000080 00000080 | csrr x6, mcause | x6 = 00000002 00000084 | sw x6, 0(x20) | mem[80000010] = 00000002 00000088 | addi x20, x20, 4 | x20 = 80000014 0000008c | csrr x7, mepc | x7 = 00000024 00000090 | addi x7, x7, 4 | x7 = 00000028 00000094 | csrw mepc, x7 | mepc = 00000028 00000098 | mret | next is 00000028, MIE=0 00000028 | lw x11, 2(x8) | TRAP mcause=4 mepc=00000028 mtval=80001002, next is 00000080 00000080 | csrr x6, mcause | x6 = 00000004 00000084 | sw x6, 0(x20) | mem[80000014] = 00000004 00000088 | addi x20, x20, 4 | x20 = 80000018 0000008c | csrr x7, mepc | x7 = 00000028 00000090 | addi x7, x7, 4 | x7 = 0000002c 00000094 | csrw mepc, x7 | mepc = 0000002c 00000098 | mret | next is 0000002c, MIE=0 0000002c | sw x11, 1(x8) | TRAP mcause=6 mepc=0000002c mtval=80001001, next is 00000080 00000080 | csrr x6, mcause | x6 = 00000006 00000084 | sw x6, 0(x20) | mem[80000018] = 00000006 00000088 | addi x20, x20, 4 | x20 = 8000001c 0000008c | csrr x7, mepc | x7 = 0000002c 00000090 | addi x7, x7, 4 | x7 = 00000030 00000094 | csrw mepc, x7 | mepc = 00000030 00000098 | mret | next is 00000030, MIE=0 00000030 | lw x11, 0(x9) | TRAP mcause=5 mepc=00000030 mtval=40000000, next is 00000080 00000080 | csrr x6, mcause | x6 = 00000005 00000084 | sw x6, 0(x20) | mem[8000001c] = 00000005 00000088 | addi x20, x20, 4 | x20 = 80000020 0000008c | csrr x7, mepc | x7 = 00000030 00000090 | addi x7, x7, 4 | x7 = 00000034 00000094 | csrw mepc, x7 | mepc = 00000034 00000098 | mret | next is 00000034, MIE=0 00000034 | sw x11, 0(x9) | TRAP mcause=7 mepc=00000034 mtval=40000000, next is 00000080 00000080 | csrr x6, mcause | x6 = 00000007 00000084 | sw x6, 0(x20) | mem[80000020] = 00000007 00000088 | addi x20, x20, 4 | x20 = 80000024 0000008c | csrr x7, mepc | x7 = 00000034 00000090 | addi x7, x7, 4 | x7 = 00000038 00000094 | csrw mepc, x7 | mepc = 00000038 00000098 | mret | next is 00000038, MIE=0 00000038 | jalr x1, 2(x9) | TRAP mcause=0 mepc=00000038 mtval=40000002, next is 00000080 00000080 | csrr x6, mcause | x6 = 00000000 00000084 | sw x6, 0(x20) | mem[80000024] = 00000000 00000088 | addi x20, x20, 4 | x20 = 80000028 0000008c | csrr x7, mepc | x7 = 00000038 00000090 | addi x7, x7, 4 | x7 = 0000003c 00000094 | csrw mepc, x7 | mepc = 0000003c 00000098 | mret | next is 0000003c, MIE=0 0000003c | jal x0, 0x3c | the program stops here the list: 11 3 2 2 2 4 6 5 7 0 instructions=75 traps=10Check Yourself
Common Mistakes
Wrong Return to mepc unchanged after an ecall.
mepc points at the ecall. It traps again, for ever.
Fix Add 4 to mepc for an ecall. Leave it for an interrupt.
Wrong Expect the fault that is found first to trap first.
An illegal word is found in ID, two cycles before an older load is refused in MEM.
Fix The oldest instruction traps first: faults are acted on in MEM, in program order.
Wrong Read mtval as an address for every cause.
For an illegal instruction it is the word, and for an ecall it is 0.
Fix Read mcause first. It says what mtval means.
Remember
- mcause names the fault. mtval holds its detail: an address, or the illegal word.
- ecall and ebreak trap on purpose. An ecall handler returns to mepc plus 4.
- A fault travels as a mark and is acted on in MEM. Older instructions finish, and three younger ones are flushed.
- The oldest fault wins. A trap, an mret and a CSR instruction each cost 3 cycles on ap_rv32.