C825 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

C8 · Lesson 25 · Interrupts and exceptions

Stopping the program to deal with something else

A microcontroller has to notice things: a byte arriving on a serial line, a button press, a timer running out. It could check each one over and over in a loop (polling), but that wastes time and responds late. Instead, devices raise an interrupt. The processor finishes the instruction it's on, remembers where it was, and jumps to a handler; the handler deals with the event and returns, and the main program carries on as if nothing had happened. Here the main program just counts in s1, while a timer interrupts it every 40 or so instructions to count a tick in s0 and flip LED 0.

Play

  • Press Run and watch the PC jump into the handler and back. The control registers show where it came from (mepc) and why (mcause).
  • Step slowly around a tick. What's in mepc while the handler runs?
  • The handler saves t0 and t1 on the stack before using them. Why, when the main program never called it?

Predict

The timer interrupt arrives just before li t2, 6 runs. After the handler's mret, which instruction runs next?

Name it

Traps: interrupts and exceptions

A trap moves control to a handler. An interrupt comes from outside the instruction stream (a timer, a pin, a peripheral). An exception is caused by the instruction itself: an illegal instruction, a bad memory access, or ecall, a deliberate request for the operating system or firmware to do something.

What the processor does (RISC-V machine mode):

  1. Finish the current instruction (for an exception, abandon the faulting one).
  2. Save the PC in mepc: the next instruction to run for an interrupt, the faulting instruction itself for an exception.
  3. Record why in mcause. The top bit is 1 for an interrupt; the rest is a cause number. The machine timer interrupt is 0x80000007; ecall from machine mode is 11; an illegal instruction is 2.
  4. Turn interrupts off (bit MIE of mstatus is copied to MPIE, then cleared), so the handler isn't interrupted itself.
  5. Jump to the handler's address, in mtvec.

mret reverses it: the PC comes back from mepc and MIE from MPIE.

Enabling. An interrupt is taken only when its own enable bit is set (bit 7 of mie for the timer) and the global enable, MIE in mstatus, is on.

The timer. This board's TIMER counts instructions (a real chip's counts clock ticks), and the interrupt is pending whenever TIMER ≥ TIMECMP. The handler must move TIMECMP forward, or the interrupt fires again the moment it returns.

Handlers save everything they touch. An ordinary function is called at a point the caller chose, and the calling convention says which registers it may change. An interrupt can arrive between any two instructions, when every register might be in use, so a handler must save and restore every register it uses.

Worked example

A timer interrupt is taken just after the instruction at 0x0040 finishes. What are mepc and mcause?

Show the solution
  1. mepc. mepc = 0x0044, the next instruction to run, so nothing is skipped or repeated.
  2. mcause. mcause = 0x80000007: the top bit marks an interrupt, and 7 is the machine timer.

Answer mepc = 0x0044 and mcause = 0x80000007.

Worked example

An ecall at 0x0100 traps. What is mepc, and what must the handler do before mret?

Show the solution
  1. mepc. mepc = 0x0100, the ecall itself, because it's an exception.
  2. Before mret. The handler must add 4 to mepc, or mret returns to the ecall and traps again forever.

Answer mepc = 0x0100, and the handler must add 4 to it before mret.

Worked example

Entering a handler takes 3 cycles and mret 3 more. The handler adjusts sp twice, saves and restores 6 registers, and does 20 cycles of work. How many interrupts a second can a 16 MHz core take, and what does a 50 kHz rate cost it?

Show the solution
  1. Cycles per interrupt. n=3+1+6+20+6+1+3=40cycles
  2. The most interrupts a second. At 16 MHz that's at most 400,000 interrupts a second, with nothing left for the main program.
  3. The cost at 50 kHz. 50,000×40=2×106 cycles a second, 12.5% of the core.
  4. Cutting it. Saving fewer registers (only the ones the handler touches) is the cheapest way to cut it.

Answer at most 400,000 interrupts a second, and a 50 kHz rate costs 12.5% of the core.

Check

Put the steps of taking a timer interrupt in order.

  1. Record the cause in mcause
  2. Jump to the handler at mtvec
  3. Turn interrupts off
  4. Finish the instruction that's running
  5. Save the address of the next instruction in mepc
  6. Run the handler, then mret

Check

An ecall at address 0x060C traps to a handler. Before its mret, what should the handler put in mepc so the program continues after the ecall?

Check

Finish the handler so it adds 100 to a0 and returns to the instruction after the ecall.

RISC-V RV32IM (base integer plus multiply and divide). End your program with ebreak. Tab indents; press Esc and then Tab to move on.

Check

A timer counts at 8 MHz. For an interrupt every 1 ms, how much should the handler add to TIMECMP each time?

Check

A timer interrupt fires every 100 instructions, and its handler runs 17 of them. What percentage of the processor's time goes to the handler?

Check

Why must an interrupt handler save every register it uses, when an ordinary function only has to save the s registers and ra?

Check

Entry takes 3 cycles and mret 3. A handler adjusts sp twice, saves and restores 4 registers and does 50 cycles of work, at 48 MHz.

(a)

How many cycles does each interrupt cost?

(b)

What share of the core does a 10 kHz interrupt rate use?

Check

The cause code is 7.

(a)

What's mcause for a machine-mode interrupt with that code?

(b)

And for an exception with the same code?

Explain why

Why is responding to a button with an interrupt better than checking the button in a polling loop?

Practice

Keep practicing

9 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.