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.

Five questions this page answers
Question
1How does an exception differ from an interrupt?
2Which two gates must a pending request pass before it is taken?
3What can software still see while a gate is closed?
4What happens to a request that arrives while a gate is closed?
5Why 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

New words on this page
WordMeaning
ExceptionA forced jump caused by the instruction in progress, such as a read of an address that nothing owns.
Processor trapAny forced jump of a processor: an exception or an interrupt. RISC-V says trap for short.
Machine modeThe 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.
CSRControl and status register: a register inside the processor. Special instructions reach it. It has no address on the bus.
Enable bit of a kindOne bit for each kind of interrupt. While it is 0, that kind is not taken.
Global enableOne bit for all kinds together. While it is 0, no interrupt is taken.
Masked requestA request that is pending behind a closed gate.
The RISC-V names this page uses
NameWhat it is
mipThe CSR that holds the pending bits. Software reads it.
mieThe CSR that holds the enable bits, one for each kind.
mstatusA CSR of status bits. Its bit named MIE is the global enable.
MEIP and MEIEPending 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 MTIEPending and enable of the timer interrupt of machine mode, bit 7. The request comes from ap_clint.
MSIP and MSIEPending 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

Two Gates Between a Request and the Forced JumpStep through the picture. It follows one request from its wire to the forced jump.
  • Peripheral
  • Memory
  • Control / FSM
  • Processor
  • Bus
Two Gates Between a Request and the Forced JumpStep through the picture. It follows one request from its wire to the forced jump. component Request wires external, timer,software component Pending bits one for each kind: theregister mip component Gate 1 one enable bit foreach kind: theregister mie component Gate 2 one global enable bit,in mstatus component Forced jump the interrupt istaken component Software reads and writesthese registers a kind askspendingpending and enabledtakenread at any timeopens or closesopens or closes
  1. component Request wiresexternal, timer, software
    • a kind asks → Pending bits
  2. component Pending bitsone for each kind: the register mip
    • pending → Gate 1
    • read at any time → Software
  3. component Gate 1one enable bit for each kind: the register mie
    • pending and enabled → Gate 2
  4. component Gate 2one global enable bit, in mstatus
    • taken → Forced jump
  5. component Forced jumpthe interrupt is taken
  6. component Softwarereads and writes these registers
    • opens or closes → Gate 1
    • opens or closes → Gate 2
  1. 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.

  2. 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.

  3. 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.

  4. Taken

    With both gates open, the processor makes the forced jump of S0.8 at the end of the instruction in progress.

  5. 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.

One timer request and four settings of the gates
MTIEMIEPending bit MTIPPasses gate 1?Taken?
001nono
011nono
101yesno: gate 2 is closed
111yesyes

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

  1. 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.

  2. 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.

  3. Rule 3: One global enable stands in front of all kinds.

    While it is 0, the processor takes no interrupt.

  4. 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.

  5. 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.

  6. 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.

Printed by the lab on Icarus. In pending and enable, the left bit is external, the middle bit is the timer and the right bit is 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=0

Rule 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 choiceThe alternativeWhat the choice buys, and costs
A global enable beside the enable bitsOnly one enable bit for each kindOne 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 kindOnly a global enableA kind with no handler yet stays off while the others work. Each kind costs one more bit.
The pending bit shows a masked requestPending is hidden while a gate is closedSoftware 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 resetIt is 1 after a resetNo 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:

The same lab with other events. The global enable is 0 while a handler runs: the next page shows that the processor closes it by itself.
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=1

The 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 page3 kinds, 1 mode
A processor that runs an operating systemseveral modes, dozens of kinds

This section looks ahead. A larger processor has more modes and more kinds, and the two gates stay.

  1. 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.

  2. More modes

    A processor with a mode for the operating system has a second set of these bits for that mode.

  3. More kinds

    Some controllers give each device a kind of its own inside the processor. Other Interrupt Controllers compares them.

  4. 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?

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: 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=0

Check Yourself

Predict the outputIn this run MEIE and MIE are 1 and MTIE is 0. The timer asks, then a device asks through ap_plic. What does the last row say?
// Two gates in front of the forced jump. Test code stands in for the processor.
module tb;
  // Three request wires reach the processor. A 1 means that this kind asks for an interrupt.
  logic external_req = 0, timer_req = 0, software_req = 0;

  // The pending bits. The register mip shows each request, whether a gate is open or not.
  wire meip = external_req, mtip = timer_req, msip = software_req;

  // What software has written. One enable bit for each kind, in the register mie.
  logic meie = 0, mtie = 0, msie = 0;
  // The global enable: the bit named MIE in the register mstatus. It is 0 after a reset.
  logic global_ie = 0;

  // Gate 1, once for each kind: pending and enabled.
  wire external_passes = meip && meie;
  wire timer_passes    = mtip && mtie;
  wire software_passes = msip && msie;
  // Gate 2, one for all kinds: the global enable.
  wire taken = (external_passes || timer_passes || software_passes) && global_ie;

  // Each row shows the bits in the order external, timer, software.
  task show(input string what);
    #1 $display("%-18s pending=%b%b%b enable=%b%b%b MIE=%b taken=%b", what, meip, mtip, msip, meie, mtie, msie, global_ie, taken);
  endtask

  initial begin
    meie = 1;  global_ie = 1;      show("set up");
    timer_req = 1;                 show("the timer asks");
    external_req = 1;              show("a device asks");
  end
endmodule
ConceptHow does an exception differ from an interrupt?
ConceptWhich two bits must be 1 before a pending timer request is taken?
ConceptThe global enable is 0 and the timer asks. What does a read of mip show in bit 7?
ConceptA request arrives while the global enable is 0. What happens to it?
ConceptWhy does a processor have a global enable as well as the enable bits?

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.

Practice

Report a problem with this page