Sub topic 6
Interrupt types, masking, vectoring and daisy-chain priority
This chapter assumes you know the instruction cycle, how a stack and subroutine call/return work, and how hexadecimal addresses are written. It also assumes you have read the previous chapter on the interrupt handling cycle: the CPU finishes the current instruction, saves the program counter (PC) and status, and jumps to an interrupt service routine (ISR). Here we look at what kinds of interrupts exist, how the CPU can ignore some of them, how it finds the right ISR, and how it chooses among several devices that interrupt at once.
Classifying interrupts
An interrupt is a signal that makes the CPU suspend its current program and run a handler. GATE questions classify interrupts along several independent axes. Keep the axes separate, because a single interrupt has one value on each axis.
| Axis | Values | Meaning |
|---|---|---|
| Source | Hardware, software | Hardware interrupts come from a device pin. Software interrupts come from an instruction such as `INT n` or a system call trap. |
| Timing | Asynchronous, synchronous | Asynchronous interrupts arrive at any time, unrelated to the instruction being run. Synchronous ones (exceptions) are caused by the instruction itself. |
| Maskability | Maskable, non-maskable | The CPU can be told to ignore maskable interrupts. It cannot ignore non-maskable ones. |
| Identification | Vectored, non-vectored | A vectored interrupt supplies the ISR address or an index to it. A non-vectored one jumps to a fixed address. |
Hardware, software and exceptions
A hardware interrupt is an electrical signal from a device, such as a keyboard controller or a disk controller. It is asynchronous: the CPU only notices it between instructions.
A software interrupt is requested by a program through a special instruction. It is synchronous, because it happens at a known point in the program. Operating systems use it to switch from user mode to kernel mode for system calls.
An exception (also called a trap or fault) is raised by the CPU itself when an instruction cannot complete normally. Examples are division by zero, an illegal opcode, and a page fault. Exceptions are synchronous. Some, such as a page fault, are restartable: the handler fixes the problem and the same instruction runs again, so the saved PC must point to the faulting instruction. Others, such as a system call trap, resume at the next instruction.
Maskable and non-maskable interrupts
A maskable interrupt can be disabled by the program. The CPU keeps an interrupt-enable flag (called IF on x86) in its status register. When the flag is 0, the CPU does not respond to maskable requests. The request stays pending on the line and is recognized after the flag is set again.
A non-maskable interrupt (NMI) ignores the flag. It is reserved for events that cannot wait or be ignored: a power failure warning, a memory parity error, or a hardware watchdog timer. The NMI has the highest priority and a fixed handler.
Why masking is needed
Masking protects critical sections. Suppose an ISR and the main program both update a shared counter. If an interrupt arrives halfway through the main program's update, the ISR sees half-updated data. The main program therefore disables interrupts around the update and enables them afterward.
The CPU also masks automatically. On most designs, accepting an interrupt clears the enable flag, so the ISR starts with maskable interrupts off. The ISR can set the flag again if it wants to allow higher-priority interrupts to preempt it (nested interrupts).
Masking with a mask register
Many systems have a mask register with one bit per interrupt line, in addition to the single global flag. A common convention is that a 1 bit allows the line and a 0 bit blocks it. Some devices use the opposite convention, so read the question carefully.
For example, with four lines I3 to I0 and mask 0101 (1 = enabled), only I2 and I0 can interrupt the CPU. Requests on I3 and I1 are held pending until the mask changes.
# Which pending requests reach the CPU, given a request register and a mask register
pending = 0b1111 # all four lines requesting
mask = 0b0101 # 1 = line enabled
effective = pending & mask
print(f"Effective requests: {effective:04b}")
# Highest-numbered line wins in this fixed-priority example
for line in range(3, -1, -1):
if effective & (1 << line):
print(f"Serviced first: I{line}")
breakThe program prints Effective requests: 0101 and then Serviced first: I2. The bitwise AND models the hardware: a request gets through only where both the request bit and the mask bit are 1.
Level-triggered and edge-triggered lines
A device can signal by holding a line at a level (level-triggered) or by changing the line's voltage (edge-triggered). A level-triggered request stays asserted until the ISR makes the device drop it, usually by reading the device's data or status register. A level-triggered request that is masked is not lost, because the line stays asserted. An edge-triggered request that arrives while masked can be lost unless the controller latches it.
Vectored and non-vectored interrupts
After accepting an interrupt, the CPU must find the correct ISR. There are two approaches.
- Non-vectored: every interrupt jumps to one fixed address. The ISR at that address must poll the devices to find which one requested service. This is simple but slow when there are many devices.
- Vectored: the requesting device supplies an interrupt vector, which identifies the ISR. The CPU uses it to find the handler without polling.
The vector table
A vector is usually a number, not a full address. The CPU multiplies it by the entry size and adds the base of the interrupt vector table, an array in memory whose entries hold ISR addresses.
Suppose the table starts at address 0 and each entry is 4 bytes. A device supplies vector 0x21. The entry is at 0x21 × 4 = 0x84, so the CPU loads the PC from memory at 0x84.
def vector_entry_address(vector, entry_size=4, table_base=0x0):
return table_base + vector * entry_size
print(hex(vector_entry_address(0x21))) # 0x84
print(hex(vector_entry_address(0x21, 4, 0x1000))) # 0x1084Some processors use a different scheme. The 8085, for example, has fixed restart addresses: RST 7.5 jumps to 003C, RST 6.5 to 0034, RST 5.5 to 002C, and TRAP to 0024. These are vectored in the sense that each line has its own address, but the addresses are hard-wired rather than read from a table.
Who supplies the vector
In the common bus-based design, the CPU raises an interrupt acknowledge (INTA) signal. The device that won the priority contest puts its vector on the data bus. The CPU reads it and looks up the ISR. This is why the acknowledge signal must reach the right device and no other one.
Priority when several devices interrupt
If two devices request service at the same time, or one requests while an ISR is running, something must decide the order. Priority can be set by software or hardware.
Software polling
The CPU jumps to a common ISR, which checks each device's status register in a fixed order. The device checked first has the highest priority. This needs no extra hardware, but the time to reach a low-priority device grows with the number of devices.
Daisy chain
The daisy chain is the standard hardware scheme. All devices share one interrupt request line to the CPU. The interrupt acknowledge signal is not shared. It enters the first device and passes from device to device along a chain.
- A device that needs service asserts the shared INT line.
- The CPU finishes its instruction and asserts INTA, which goes to device 1.
- A device that is not requesting passes INTA to the next device in the chain.
- A device that is requesting blocks INTA from going further and places its vector on the data bus.
The device closest to the CPU therefore has the highest priority, and priority falls with distance along the chain. Priority is fixed by physical wiring order.
The strength of the daisy chain is that it needs few wires and no priority logic in the CPU. Its weaknesses are fixed priority, since you must rewire to change it, and starvation, since a busy high-priority device can keep a low-priority device waiting indefinitely. A broken device in the middle of the chain can also cut off every device after it.
Parallel priority
In the parallel scheme, each device has its own request line into a priority encoder. A mask register can disable individual lines. The encoder outputs the vector of the highest-priority unmasked request. This is faster than a daisy chain, and priorities can be changed by changing the mask or the encoder logic, but it needs one line per device.
| Scheme | Wires to CPU side | Speed of selection | Priority change | Typical cost |
|---|---|---|---|---|
| Software polling | One shared line | Slowest, grows with devices | Edit poll order | Cheapest |
| Daisy chain | One shared line plus INTA chain | Delay grows along chain | Rewire | Low |
| Parallel priority | One line per device | Fast | Via mask or encoder | Highest |
Nested interrupts and priority levels
Priority matters in two moments. First, it chooses among simultaneous requests. Second, it decides whether a new request may interrupt a running ISR.
If an ISR for a low-priority device is running, and a higher-priority request arrives, the CPU may suspend the ISR and run the new one. This is a nested interrupt. It requires the ISR to have re-enabled interrupts, or the hardware to compare priorities, and it requires a stack that saves each interrupted context. A lower-priority or equal-priority request must wait until the running ISR finishes.
Each nesting level uses stack space for the saved PC and status. A design that allows unlimited nesting can overflow the stack, so real systems limit levels or keep ISRs short.
/* Sketch: a critical section protected by disabling interrupts.
disable_interrupts() and enable_interrupts() are platform specific. */
extern void disable_interrupts(void);
extern void enable_interrupts(void);
volatile unsigned int shared_count = 0;
void main_increment(void) {
disable_interrupts(); /* a maskable ISR cannot run now */
shared_count = shared_count + 1;
enable_interrupts(); /* pending requests are now recognized */
}
void timer_isr(void) {
shared_count = shared_count + 1;
}Without the disable and enable calls, the read-modify-write in main_increment could be split by timer_isr, and one increment would be lost.
Common errors and exam traps
- Confusing the two flags. Masking a line does not erase the request. It delays recognition. Only the device or the ISR can remove a level-triggered request.
- Forgetting that NMI ignores the mask. If a question says all maskable interrupts are disabled, the NMI can still occur.
- Treating exceptions as asynchronous. Division by zero and page faults come from the running instruction, so they are synchronous.
- Saying the CPU checks for interrupts at any time. It checks at the end of an instruction, normally. Long instructions may allow checks at defined points.
- Mixing up daisy-chain order. The device nearest the CPU on the INTA path has the highest priority, not the device farthest away.
- Assuming the vector is the ISR address. Usually the vector is an index, and the address is read from the table. Check whether the question gives the table entry size.
- Forgetting that the ISR must save any registers it changes, and restore them before returning, because the interrupted program does not know it was interrupted.
Summary
Interrupts are classified by source, timing, maskability and identification. Masking lets software and the CPU defer maskable requests, while the NMI cannot be deferred. Vectored interrupts let the device identify its handler, and the CPU finds the ISR through a vector table. When several devices compete, priority is resolved by software polling, a hardware daisy chain, or parallel priority logic, and priority also governs whether an ISR may be preempted by a nested interrupt.
Exercise
A system has four devices D1 to D4 on a daisy chain, with D1 closest to the CPU. D2 and D4 raise requests at the same time. The vector table starts at 0x0000 with 4-byte entries, and D4 supplies vector 0x30 while D2 supplies vector 0x12. Which device is serviced first, and at what address does the CPU read the ISR address?
Show answer
D2 is serviced first, because it is closer to the CPU on the INTA path and blocks INTA from reaching D4. The entry address is 0x12 × 4 = 18 × 4 = 72 = 0x48, so the CPU reads the ISR address from memory at 0x0048. D4 is serviced afterward, if D2's ISR finishes and the request is still pending.
Exercise
Four interrupt lines I3 to I0 use the convention 1 = enabled in the mask register. The mask is 0110, and lines I3, I2 and I0 are requesting. If a higher line number means higher priority, which line is served first, and which requests remain pending?
Show answer
The request register is 1101 and the mask is 0110. The AND gives 0100, so only I2 reaches the CPU, and it is served first. I3 and I0 remain pending, blocked by the mask, until the mask is changed to enable them.
Exercise
Why can a page fault not be treated as an ordinary asynchronous hardware interrupt, and what must the saved PC point to?
Show answer
A page fault is caused by the instruction that accessed an absent page, so it is synchronous. After the OS loads the page, the same instruction must run again. The saved PC therefore points to the faulting instruction, not to the next one.