Interrupt Entry and Return
A request has passed both gates. The processor now does four things that no instruction asked for, and one instruction at the end of the handler undoes them.
You Will Be Able To
- List what the processor saves and changes by itself when it takes an interrupt.
- Work out the entry address in direct mode and in vectored mode.
- Explain what a handler must save before it lets a second interrupt in.
- Lesson
- S6.14
- Time
- 26 min
- Runs on
- Your browser
- Check
- 6 questions
A handler worked for months. Then someone allowed a more urgent interrupt to break into it. Now the board hangs once a day, deep inside that handler. The new code looks correct, and yet one register was written twice.
Before You Start
You need S0.8: the forced jump, the saved PC and the return. You need the enable bits: the two gates, the global enable MIE, a masked request, and the word CSR.
You also need hexadecimal numbers, to add a small number to an address.
| Question | |
|---|---|
| 1 | What does the processor do by itself when it takes an interrupt? |
| 2 | Where does the PC go: in direct mode, and in vectored mode? |
| 3 | What does the return instruction undo? |
| 4 | Why does the processor close the global enable at entry? |
| 5 | What must a handler do before it lets a second interrupt in? |
The Idea: One Action in, One Instruction Out
S0.8 gave the outline on a teaching processor. Save the PC, force the PC to a fixed address, and return with one instruction. A real processor does the same, and adds three things that the teaching processor did not need.
First, it writes down why it jumped. A chip has many reasons for a forced jump, and the handler must be able to ask which one this is. So the processor records a cause code.
Second, it closes the global enable. The saved PC lives in one register. A second interrupt, taken a moment later, would write that register again, and the first way back would be gone. So the processor shuts the gate by itself, in the same action, and remembers how the gate stood before.
Third, software chooses where the jump goes. It writes an address into a register, once, at start-up. The processor can use that address in two ways. It can be the one entry for every cause. Or it can be the start of a table with a slot for each cause.
The return is one instruction. It puts the saved PC back and gives the global enable its old value. Everything else that the handler changed is the handler's duty to put back.
New Words
| Word | Meaning |
|---|---|
| Cause code | A small number that names the reason for a forced jump. Each kind of interrupt has one. |
| Entry address | The address that a forced jump puts into the PC. |
| Direct mode | Every forced jump goes to one address. The handler there reads the cause and decides what to do. |
| Vectored mode | Each kind of interrupt jumps to a place of its own in a table. |
| Handler table | A row of slots in memory, one for each cause code. Also called a vector table. |
| Slot of a handler table | Four bytes: room for one instruction, which is usually a jump to the real handler. |
| Nested interrupt | An interrupt taken while the handler of another one is still running. |
| Outer handler and inner handler | Of two nested interrupts: the handler that was broken into, and the one that broke in. |
| Name | What it is |
|---|---|
| mepc | The CSR that holds the saved PC. |
| mcause | The CSR that holds the cause. Bit 31 is 1 for an interrupt and 0 for an exception. The low bits hold the cause code. |
| mtvec | The CSR that software loads with the entry address. Its two lowest bits choose the mode: 0 is direct, 1 is vectored. |
| MPIE | A bit of mstatus. At entry it takes the old value of MIE. |
| mret | The return instruction of machine mode. |
| Cause codes 3, 7 and 11 | The machine software, timer and external interrupts. They are the bit numbers of the last page. |
RISC-V facts, from the privileged specification of RISC-V. Our reference library does not hold that document, so these names are not checked against it.
One Picture: Save, Record, Close, Jump, Return
- Processor
- Memory
- Control / FSM
- Bus
- Peripheral
- A request has passed both gates
The main program is about to run the instruction at 0x84. The processor takes the timer interrupt in place of that instruction.
- Save the PC
The processor copies 0x84 into mepc. That instruction has not run yet, so it is the right place to carry on.
- Record the cause
It writes the cause into mcause. The top bit is 1 for an interrupt. The low bits hold the code of the kind: 7 for the timer.
- Close the gate
It copies MIE into MPIE and then clears MIE. No second interrupt can be taken now, and the old value is kept.
- Jump
It forces the PC to the slot of the timer in the handler table. From here on, ordinary instructions run: the handler.
- The return
The last instruction of the handler is mret. It copies mepc into the PC and MPIE into MIE. The main program runs the instruction at 0x84.
Read the diagram as text
- PC: the address of the next instruction
- mepc: the saved PC
- mcause: why the jump was made
- MIE and MPIE: the global enable, and its saved value
- Handler table: starts at the address in mtvec
- Timer handler: ends with mret
- PC → mepc: 1: save the PC
- PC → mcause: 2: record the cause
- PC → MIE and MPIE: 3: MIE to MPIE, then MIE = 0
- PC → Handler table: 4: jump to a slot
- Handler table → Timer handler: the slot leads to the handler
- mepc → PC: mret: the saved PC
- MIE and MPIE → PC: mret: MPIE back to MIE
Steps 2 to 5 take no instruction. The processor does them as one action, between two instructions.
The Smallest Example, Worked by Hand
Software has written 0x100 into mtvec, with vectored mode. The main program is about to run the instruction at 0x84 when the timer interrupt is taken. The slot of the timer is 0x100 + 4 × 7 = 0x11c.
| Moment | PC | mepc | mcause | MIE | MPIE |
|---|---|---|---|---|---|
| Before | 0x84 | 1 | |||
| After entry | 0x11c | 0x84 | 0x80000007 | 0 | 1 |
| After one handler instruction | 0x120 | 0x84 | 0x80000007 | 0 | 1 |
| After mret | 0x84 | 0x84 | 0x80000007 | 1 | 1 |
In direct mode the second row would have PC = 0x100, and nothing else would differ.
Now the outer handler lets a second interrupt in. At 0x120 it sets MIE to 1. A software interrupt is pending and enabled, so it is taken at once. Its slot is 0x100 + 4 × 3 = 0x10c.
| Moment | PC | mepc | What it means |
|---|---|---|---|
| Outer handler opens the gate | 0x120 | 0x84 | the way back to the main program |
| Entry of the inner interrupt | 0x10c | 0x120 | the way back to the outer handler: 0x84 is gone from mepc |
| Inner mret | 0x120 | 0x120 | the outer handler carries on |
| Outer mret, with no copy kept | 0x120 | 0x120 | back into the outer handler, for ever |
| Outer mret, after mepc is put back from a copy | 0x84 | 0x84 | the main program carries on |
There is one mepc. The inner entry used it, as it must. Only a copy that the outer handler made beforehand can bring 0x84 back.
The Rules
Rule 1: At entry the processor saves the PC in mepc.
It is the address of the instruction that was about to run. For an interrupt, the return goes exactly there.
Rule 2: It records the cause in mcause.
The top bit says interrupt or exception. The low bits hold the cause code.
Rule 3: It copies MIE into MPIE and clears MIE.
So a handler starts with the global enable closed.
Rule 4: It jumps to the address in mtvec, or to a slot.
In direct mode every cause enters at that address. In vectored mode an interrupt enters at that address plus four times its cause code.
Rule 5: mret copies mepc into the PC and MPIE into MIE.
It undoes rules 1 and 3. It restores nothing else.
Rule 6: A handler that sets MIE must first save mepc, and restore it before mret.
A nested entry overwrites mepc, mcause and MPIE. The handler also closes MIE again while it restores them.
Sources for These Rules
The textbook "Computer Organization and Design" by Patterson and Hennessy, section 4.9. It covers the saved address, the recorded cause, and one entry address against a table of them.
The six rules and every name in the table of RISC-V names are RISC-V facts. They are stated from common knowledge of the privileged specification of RISC-V. Our reference library does not hold that document, so none of them is checked against it.
In vectored mode an exception still enters at the address in mtvec. That is also a RISC-V fact, not checked.
The addresses 0x84, 0x100 and 0x120 are choices of this page.
Now Run It
The lab at the end of this page is the second hand table as test code. A task named enter does what the processor does at entry. A task named mret does what that instruction does. No real processor is in the lab.
main pc=00000084 MIE=1
entry timer mepc=00000084 mcause=80000007 MIE=0 MPIE=1 pc=0000011c
entry software mepc=00000120 mcause=80000003 MIE=0 MPIE=1 pc=0000010c
mret software pc=00000120 MIE=1
mret timer pc=00000084 MIE=1
the main program carries on at 00000084Rules 1 to 4 at work, all in the row "entry timer". There mepc holds 0x84 and mcause holds 0x80000007. MIE is 0 with MPIE at 1, and the PC is 0x11c.
Rule 6 at work: in the row "entry software", mepc is 0x120. The value 0x84 is no longer in any CSR. Rule 5 at work: the inner mret returns to 0x120.
The last two rows are the proof. The outer mret returns to 0x84, because the handler put its copy back first.
This lab leaves things out. It runs no instructions, so it saves no general registers. S0.8 showed why a handler must: the processor saves only the PC.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| The processor closes the gate at entry | Software closes it with the first instruction | A second interrupt cannot slip in before that instruction. Every handler starts masked, wanted or not. |
| One saved PC, in a register | The processor pushes each saved PC into memory | Entry needs no memory access, so it is quick. Nesting is left to software. |
| A table of slots | One entry address | No instructions are spent on finding the cause. The table takes memory, and each slot holds only one instruction. |
| One entry address | A table of slots | One handler, easy to place and to debug. It must read the cause and branch, on every interrupt. |
The Trap: The Handler That Never Returned
Back to the board that hangs once a day. The timer handler was changed to set MIE early, so that a more urgent interrupt could break in. Nobody added the copy. Here is the same lab with that one line taken out:
main pc=00000084 MIE=1
entry timer mepc=00000084 mcause=80000007 MIE=0 MPIE=1 pc=0000011c
entry software mepc=00000120 mcause=80000003 MIE=0 MPIE=1 pc=0000010c
mret software pc=00000120 MIE=1
mret timer pc=00000120 MIE=1
LOST: pc=00000120 is inside the timer handlerThe first four rows are the same as before. The fifth is the bug: the outer mret goes to 0x120, an address inside the handler. From there the handler runs to its mret again, and again.
It fails only when the second interrupt arrives inside the first handler, which is why it took a day. On every other run, mepc still held 0x84 at the outer mret, and the missing copy did no harm.
A smaller trap is the slot. It holds an instruction, not an address. Put the address of the handler there, and the processor runs that number as if it were an instruction.
At Scale
This section looks ahead. Real firmware wraps these rules in a few lines that every handler shares.
A stack of copies
Each handler that may be broken into keeps its copies in memory, one set above the other. It takes them back in reverse order.
General registers
The same code saves every register that the handler will use, and restores them before mret.
Exceptions
An exception uses the same registers. Its handler often returns to the instruction after the one that failed, so it changes mepc first.
How long entry takes
Interrupt Latency counts the clock edges from the event to the first instruction of the handler.
Try It
Run the lab. Check each row against the two hand tables.
Set VECTORED to 0. Predict the PC in both entry rows, and the mepc of the inner entry.
Set SAVE_FIRST to 0. Find the first row that differs, and say which rule was broken.
main pc=00000084 MIE=1 entry timer mepc=00000084 mcause=80000007 MIE=0 MPIE=1 pc=0000011c entry software mepc=00000120 mcause=80000003 MIE=0 MPIE=1 pc=0000010c mret software pc=00000120 MIE=1 mret timer pc=00000084 MIE=1 the main program carries on at 00000084Check Yourself
Common Mistakes
Wrong Set MIE inside a handler without saving mepc first.
A nested entry overwrites the only saved PC.
Fix Save mepc, then open the gate. Close it and restore mepc before mret.
Wrong Put the address of a handler into a slot of the table.
The processor runs the content of a slot as an instruction.
Fix Put a jump instruction into the slot.
Wrong Add 4 to mepc before returning from an interrupt.
mepc already holds the instruction that has not run. Adding 4 skips it.
Fix Return to mepc as it was saved.
Remember
- At entry the processor saves the PC in mepc, records the cause in mcause, closes MIE and jumps.
- Direct mode: one entry address. Vectored mode: the address in mtvec plus four times the cause code.
- mret puts back the PC and the global enable, and nothing else.
- There is one mepc. Save it before a second interrupt may be taken.