Part 3 of 8 · 3 min read
The interrupt path
Think of a secretary who is interrupted by the telephone. The secretary marks the page, answers the call, writes the note and returns to the exact word where the work stopped. The processor does the same with the saved program counter. The analogy stops in one place: the secretary chooses where to mark the page, and the hardware saves the state by fixed rules.
In plain words: The processor has an interrupt request line, a wire that it checks after every instruction. When a device controller raises its signal on that line, the processor does the following.
- It finishes the instruction it is running. It never stops in the middle of one.
- It reads the interrupt number supplied with the request.
- It uses the number as an index into the interrupt vector, a table of addresses of handler routines (usually kept in a fixed area of memory).
- It saves the minimum state, such as the program counter, so the interrupted work can resume, and it switches to kernel mode (page 2).
- It jumps to the handler. The handler saves the registers it will change, finds the cause, does the work, and restores the registers.
- It executes a special return-from-interrupt instruction, and the processor resumes the interrupted program as if nothing had happened.
A handler is reached by direct lookup in the table, not by asking a general routine to search for the source. That makes the dispatch fast, which matters since interrupts happen very often. Windows and Unix both dispatch this way.
MOS describes the lowest level of the OS during an interrupt in eight steps:
| Step | Who | What |
|---|---|---|
| 1 | Hardware | Pushes the program counter and other state on the stack |
| 2 | Hardware | Loads a new program counter from the interrupt vector |
| 3 | Assembly routine | Saves the registers |
| 4 | Assembly routine | Sets up a new stack for the handler |
| 5 | C routine | The interrupt service: for a keyboard, reads and buffers the input |
| 6 | Scheduler | Decides which process runs next |
| 7 | C routine | Returns to the assembly code |
| 8 | Assembly routine | Starts the chosen process |
Steps 3 and 4 cannot be written in C, since a high-level language has no way to name a register or set the stack pointer. A small assembly routine does them.
A worked example.
A worked cost (made up to show the idea). A handler takes 4 microseconds, plus 1 microsecond to save and restore state: 5 microseconds. The system receives 2,000 interrupts a second. The cost is 2,000 × 5 µs = 10,000 µs = 10 ms every second, which is 1 per cent of the processor.
Interrupt vector
The interrupt vector is a table in memory, indexed by interrupt number, whose entries hold the addresses of the handler routines. The processor uses the number to look up the right entry and jump to that handler.
Example: entry 33 might hold the address of the keyboard handler, so an interrupt numbered 33 jumps straight to it.
Check yourself: 1. At which point of an instruction can an interrupt be taken? 2. What does the processor use to find the right handler? 3. Why are the first steps of an interrupt written in assembly?