Sub topic 5
What Happens When an Interrupt Occurs
This chapter assumes that you know the CPU instruction cycle, registers, memory, buses, stacks, and subroutine call/return. You should also understand that a device communicates through an I/O interface with data, status, and control registers.
An interrupt is a request that asks the CPU to pause its current program and run a special routine. This routine is called an interrupt service routine (ISR). The device raises the request, but the CPU decides when and whether to accept it.
The interrupt cycle
Suppose a keyboard interface receives a character. The interface stores the character in its data register and sets a bit in its status register. It then sends an interrupt request to the CPU or to an interrupt controller.
The CPU usually follows this sequence:
- The CPU completes the instruction currently being executed.
- It checks whether interrupts are enabled.
- It checks whether the request is unmasked and has sufficient priority.
- It accepts the interrupt.
- It saves the return address and necessary status information.
- It identifies the correct ISR.
- It transfers control to the ISR.
- The ISR saves any additional registers that it will change.
- The ISR services the device and clears the interrupt request.
- The ISR restores the saved registers.
- A special return-from-interrupt instruction restores the saved status and program counter.
- The interrupted program continues.
The return address is normally the address of the next instruction that has not yet executed. Therefore, the interrupted program resumes after the instruction boundary at which the interrupt was accepted.
Saving state and entering the ISR
The CPU must preserve enough state to continue the interrupted program correctly. The saved state commonly includes:
- The program counter, which holds the return address.
- The status or flag register, which records conditions such as zero, carry, and interrupt-enable state.
- Sometimes general-purpose registers, depending on the processor design.
Some processors save the program counter and status automatically. The ISR must save other registers in software. The exact division is architecture-dependent.
An interrupt entry is similar to a subroutine call because both operations transfer control to another routine. They are not identical. A normal subroutine returns to a known caller using a normal return instruction. An ISR returns by restoring the interrupted processor state with a return-from-interrupt instruction.
A typical ISR for a receive device reads the data register, places the byte in a buffer, and clears the request. On some devices, reading the data register clears the request automatically. On others, the ISR must write an explicit acknowledge bit.
Masking, priority, and nesting
The interrupt-enable bit provides a broad control over interrupt acceptance. A maskable interrupt can be disabled temporarily. This is useful while the CPU updates shared data that must not be interrupted halfway through.
An interrupt controller may provide separate mask bits for different devices. It may also assign priorities. If several requests arrive together, the highest-priority eligible request is selected.
A higher-priority interrupt may interrupt a lower-priority ISR. This is called nested interrupt handling. Nesting increases responsiveness for urgent devices, but it also increases the amount of saved state and makes shared-data bugs more likely.
A non-maskable interrupt cannot normally be disabled by the ordinary interrupt-enable bit. It is reserved for events that require immediate attention, such as a serious hardware failure. The processor still follows its defined acceptance rules for that interrupt.
| Interrupt property | Meaning |
|---|---|
| Maskable | Software or hardware can temporarily prevent acceptance |
| Non-maskable | Ordinary interrupt masking cannot prevent acceptance |
| Vectored | The request provides, or selects, the ISR address |
| Non-vectored | The CPU enters a common location and software identifies the source |
With a vectored interrupt, the device or interrupt controller supplies a vector number. The CPU uses that number to obtain the ISR address from an interrupt vector table. With a non-vectored interrupt, all requests may enter one common ISR, which reads status registers to determine the source.
Common errors
A frequent mistake is assuming that an interrupt is handled in the middle of an instruction. In ordinary processors, acceptance occurs at a defined instruction boundary.
Another mistake is treating an interrupt request as an unconditional command. A masked request may remain pending until it becomes enabled. Priority logic can also delay a lower-priority request.
Finally, an ISR should be short. Long processing delays other interrupts and increases the time before the interrupted program resumes. A common design is to copy device data into a buffer in the ISR and process that data later in the main program.
Exercise
A CPU receives an interrupt request while its interrupt-enable bit is clear. What happens immediately, and what can happen later?
Show answer
The CPU does not accept the request immediately. If the request remains pending, the CPU can accept it after interrupts are enabled, provided its priority and other acceptance conditions allow it.