Interrupts at the Core: The Enable Bits
A request has reached the processor, and nothing happens. Two bits inside the processor decide whether it is taken. A third bit shows that it is waiting.
You Will Be Able To
- Tell an interrupt from an exception, and say what machine mode is.
- Name the two gates a pending request must pass, and the bit behind each.
- Use the pending bits to find out why an interrupt is not taken.
- Lesson
- S6.4
- Time
- 22 min
- Runs on
- Your browser
- Check
- 6 questions
Your timer driver is finished. The timer runs out, its request wire goes to 1, and the handler never runs. The wire is fine, and so is the handler. Something between them is closed.
Before You Start
You need S0.8: the pending bit, the forced jump, the handler and the saved PC. You need The Interrupt Path: external and core-local interrupts, and handler entry.
You also need Registers: software controls hardware by writing bits.
| Question | |
|---|---|
| 1 | How does an exception differ from an interrupt? |
| 2 | Which two gates must a pending request pass before it is taken? |
| 3 | What can software still see while a gate is closed? |
| 4 | What happens to a request that arrives while a gate is closed? |
| 5 | Why are there two gates and not one? |
The Idea: Two Switches and a Bit That Remembers
S0.8 showed one way into a handler: a device asks, and the processor makes a forced jump. A processor makes the same kind of jump for a second reason. An instruction itself can fail: it reads an address that nothing owns, or its number is no instruction at all. That is an exception. It comes from inside, at one exact instruction. An interrupt comes from outside, at any moment. RISC-V has one word for both: a trap.
This page is about interrupts only, and about one question: when is a request taken? A program cannot let a handler break in just anywhere. While it updates a list that the handler also reads, it needs a moment of peace. So the processor gives software two switches.
The first is one enable bit for each kind of interrupt. Software sets the bit of a kind once it has a handler for that kind. The second is one global enable bit for all kinds together. Software clears it for the few instructions that must not be disturbed, and sets it again.
A request that meets a closed switch is not lost. Its pending bit stays 1, and software can read it. The request is taken as soon as both switches are open. Without the pending bit, a closed switch would turn a late answer into no answer.
New Words
| Word | Meaning |
|---|---|
| Exception | A forced jump caused by the instruction in progress, such as a read of an address that nothing owns. |
| Processor trap | Any forced jump of a processor: an exception or an interrupt. RISC-V says trap for short. |
| Machine mode | The mode in which a RISC-V processor may do everything. It starts in this mode after a reset. Every lesson of this module stays in it. |
| CSR | Control and status register: a register inside the processor. Special instructions reach it. It has no address on the bus. |
| Enable bit of a kind | One bit for each kind of interrupt. While it is 0, that kind is not taken. |
| Global enable | One bit for all kinds together. While it is 0, no interrupt is taken. |
| Masked request | A request that is pending behind a closed gate. |
| Name | What it is |
|---|---|
| mip | The CSR that holds the pending bits. Software reads it. |
| mie | The CSR that holds the enable bits, one for each kind. |
| mstatus | A CSR of status bits. Its bit named MIE is the global enable. |
| MEIP and MEIE | Pending and enable of the external interrupt of machine mode, bit 11 of mip and of mie. In ap_soc the request comes from ap_plic. |
| MTIP and MTIE | Pending and enable of the timer interrupt of machine mode, bit 7. The request comes from ap_clint. |
| MSIP and MSIE | Pending and enable of the software interrupt of machine mode, bit 3. The request comes from ap_clint. |
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.
Two names are easy to mix up. The register mie holds the enable bits of the kinds. The bit MIE, in capitals, is the global enable inside mstatus.
One Picture: Request, Two Gates, Jump
- Peripheral
- Memory
- Control / FSM
- Processor
- Bus
- A request arrives
Three wires reach the processor, one for each kind of interrupt. A wire at 1 sets the pending bit of its kind. Nothing else happens yet.
- Gate 1: the enable bit of the kind
Each kind has an enable bit of its own. A pending request passes gate 1 only if the enable bit of its kind is 1.
- Gate 2: the global enable
One more bit stands in front of all kinds together. While it is 0, no interrupt of any kind is taken.
- Taken
With both gates open, the processor makes the forced jump of S0.8 at the end of the instruction in progress.
- Software owns the gates, and can always look
Software writes the enable bits and the global enable. It can read the pending bits at any time, gates open or closed.
Read the diagram as text
- Request wires: external, timer, software
- Pending bits: one for each kind: the register mip
- Gate 1: one enable bit for each kind: the register mie
- Gate 2: one global enable bit, in mstatus
- Forced jump: the interrupt is taken
- Software: reads and writes these registers
- Request wires → Pending bits: a kind asks
- Pending bits → Gate 1: pending
- Gate 1 → Gate 2: pending and enabled
- Gate 2 → Forced jump: taken
- Pending bits → Software: read at any time
- Software → Gate 1: opens or closes
- Software → Gate 2: opens or closes
The gates are inside the processor. The devices and the interrupt controller sit to the left of this picture.
The Smallest Example, Worked by Hand
Take one kind, the timer. Its request wire is at 1 in every row below. Only the two bits that software owns change. Decide for each row whether the interrupt is taken.
| MTIE | MIE | Pending bit MTIP | Passes gate 1? | Taken? |
|---|---|---|---|---|
| 0 | 0 | 1 | no | no |
| 0 | 1 | 1 | no | no |
| 1 | 0 | 1 | yes | no: gate 2 is closed |
| 1 | 1 | 1 | yes | yes |
The pending bit is 1 in all four rows. Only the last row leads to a handler. So a handler that never runs has three possible causes, and the pending bit rules out none of them. It rules out a fourth: a request that never arrived.
Now a second kind. The software request is pending, MSIE is 0, MTIE is 1 and MIE is 1. Nothing is taken. The open enable bit belongs to the timer, and the timer is not asking.
The Rules
Rule 1: A request sets its pending bit, whatever the gates hold.
The bit stays 1 for as long as the request lasts. Software can read it at any time.
Rule 2: Each kind of interrupt has an enable bit of its own.
A pending request passes the first gate only when the enable bit of its kind is 1.
Rule 3: One global enable stands in front of all kinds.
While it is 0, the processor takes no interrupt.
Rule 4: An interrupt is taken only when a kind is pending, enabled, and the global enable is 1.
All three, at the end of the same instruction.
Rule 5: A closed gate delays a request and does not delete it.
The request is taken once both gates are open, if it is still pending then.
Rule 6: After a reset the global enable is 0.
Software opens it only when the handlers are in place.
Sources for These Rules
The textbook "Computer Organization and Design" by Patterson and Hennessy, section 4.9. An event from outside the processor is an interrupt, and one from inside is an exception. Both use the saved address.
The six rules, the three kinds 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.
The routes of the three requests, from ap_plic and from ap_clint, are choices of ap_soc: docs/soc/ap_soc-facts.md, sections 1.1 and 1.2.
Now Run It
The lab at the end of this page is the two gates and nothing else. Test code stands in for the processor: it holds the bits and works out one wire, taken. Each row prints the three kinds in the order external, timer, software.
after reset pending=000 enable=000 MIE=0 taken=0
the timer asks pending=010 enable=000 MIE=0 taken=0
MIE alone pending=010 enable=000 MIE=1 taken=0
MTIE alone pending=010 enable=010 MIE=0 taken=0
both gates open pending=010 enable=010 MIE=1 taken=1
software asks pending=001 enable=010 MIE=1 taken=0Rule 1 at work: in the row "the timer asks", pending is 010 while every gate is closed. Rule 6 at work: the first row has MIE=0.
Rules 2 and 3 at work: "MIE alone" and "MTIE alone" each open one gate, and both end with taken=0. Rule 4 at work: only "both gates open" has taken=1.
The last row is the second kind. The software request is pending, but the open enable bit is the timer bit, so taken=0.
This lab leaves things out. It has no instructions and no clock, so it does not show when the jump is made. Interrupt Entry and Return does.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| A global enable beside the enable bits | Only one enable bit for each kind | One write closes every kind for a few instructions, and one write opens them. Software has two places to get wrong. |
| An enable bit for each kind | Only a global enable | A kind with no handler yet stays off while the others work. Each kind costs one more bit. |
| The pending bit shows a masked request | Pending is hidden while a gate is closed | Software can poll a kind it does not want as an interrupt, and can debug a silent handler. The bit must be kept apart from the gates. |
| The global enable is 0 after a reset | It is 1 after a reset | No interrupt is taken before software has said where the handlers are. Software must remember to set it. |
The Trap: The Interrupt That Never Came
Back to the timer driver. The code set MTIE and started the timer. The handler never ran. The first instinct is to probe the timer, and the timer is innocent.
Read mip instead. Bit 7 is 1, so the request arrived and rule 1 held. Then one of the gates is closed. The driver did set MTIE. Nobody set MIE, and rule 6 says it was 0 from the reset on. That is the row "MTIE alone" of the lab.
The same picture has a second face. A handler is already running, and a new timer request arrives:
in a handler pending=000 enable=010 MIE=0 taken=0
the timer asks pending=010 enable=010 MIE=0 taken=0
the handler ends pending=010 enable=010 MIE=1 taken=1The request waits with taken=0, and it is taken when the handler ends. Rule 5 held: nothing was lost. But the answer came late, and how late depends on the length of that handler.
At Scale
This section looks ahead. A larger processor has more modes and more kinds, and the two gates stay.
Several requests pass at once
The processor then takes one, in a fixed order of the kinds. In RISC-V the order is external, then software, then timer. This is a RISC-V fact, not checked against the specification.
More modes
A processor with a mode for the operating system has a second set of these bits for that mode.
More kinds
Some controllers give each device a kind of its own inside the processor. Other Interrupt Controllers compares them.
What the processor does next
Once a request is taken, the processor saves the PC and jumps. Interrupt Entry and Return follows that.
Try It
Run the lab. Check the rows "MIE alone", "MTIE alone" and "both gates open" against your hand table.
Add msie = 1; before the last show. Predict the last row, then run it.
Remove global_ie = 1; from the row "both gates open". Which rule does the new row show?
after reset pending=000 enable=000 MIE=0 taken=0 the timer asks pending=010 enable=000 MIE=0 taken=0 MIE alone pending=010 enable=000 MIE=1 taken=0 MTIE alone pending=010 enable=010 MIE=0 taken=0 both gates open pending=010 enable=010 MIE=1 taken=1 software asks pending=001 enable=010 MIE=1 taken=0Check Yourself
Common Mistakes
Wrong Set the enable bit of a kind and expect the handler to run.
The global enable is still 0 after a reset.
Fix Set the enable bit of the kind, then the global enable.
Wrong Conclude from a silent handler that the device never asked.
A request behind a closed gate is pending and not taken.
Fix Read the pending bits. A 1 there means that the request arrived.
Wrong Treat the register mie and the bit MIE as the same thing.
One holds the enable bits of the kinds. The other is the global enable in mstatus.
Fix Check both: the bit of the kind in mie, and MIE in mstatus.
Remember
- An exception comes from the instruction in progress. An interrupt comes from outside. RISC-V calls both a trap.
- A pending request is taken only when the enable bit of its kind and the global enable are both 1.
- The pending bit shows a request whether the gates are open or not.
- A closed gate delays a request. It does not delete it.