The Nature of Interrupts: How CPU Hardware Responds to Events
This article explains interrupts as an event-driven hardware pathway built from logic gates, not as software `if` checks. It walks through the full interrupt lifecycle from event generation and request aggregation to sampling at instruction boundaries, context saving, vector lookup, and return. It also covers masking, priority, nesting, the difference between interrupts and exceptions, correct top-half and bottom-half software design, common misconceptions with a debugging order, and how the model maps to cloud and distributed systems.
1. Start Here: An Interrupt Is a Hardware Switch, Not an if Check
Many people first model interrupts as a CPU repeatedly running if (interrupt pending) { handle it } inside a loop. That mental model is wrong, and the mistake matters.
A more accurate description: an interrupt is an event-driven pathway built out of gate circuits. A peripheral does not need to be polled. It only needs to drive a level or a pulse into an input pin on the controller; the controller forwards the result to the CPU’s interrupt sampling logic; and the CPU samples that line at an instruction boundary. Once the condition holds, the instruction fetch path is switched onto another track to run a pre-arranged handler.
Two points deserve emphasis:
- Sampling consumes no extra instruction cycles. It is not the execution of a branch statement; it is combinational logic producing a result directly on a clock edge. The input is on one side, the output on the other, with only gate delay in between. This is the fundamental source of the speed difference between hardware and software implementations.
- The switch happens at instruction boundaries. A CPU does not jump away halfway through an instruction (except in special cases such as atomic instructions and interrupt-disabled windows); it waits for the current instruction to retire before switching tracks. This preserves the integrity of program state.
Early processor pin definitions stated this plainly: one pin receives the non-maskable interrupt request, another receives the maskable interrupt request; when the processor decides to respond, it asserts an acknowledge signal on a further pin to tell the outside world that it is about to enter the interrupt sequence. Very few pins, but the mechanism was already complete.
2. The Full Path: What an Interrupt Goes Through from Generation to Return
Broken apart, the lifecycle of an interrupt has six stages. A fault in any one of them shows up as “a lost interrupt” or “a hung system.”
- Event generation: a peripheral completing a transfer, a timer counter overflowing, or a key being pressed sets that unit’s internal request flag.
- Aggregation and arbitration: requests from multiple sources are merged onto a single request line through “OR” logic, while the controller performs priority ordering and vector lookup.
- Sampling and decision: at an instruction boundary the CPU samples the request line and decides whether to respond, combining that with the global interrupt enable bit and the current priority threshold.
- Context saving: the return address and status word are pushed onto the stack; the interrupt enable bit in the status word is normally cleared automatically by hardware so the handler cannot be interrupted again immediately after entry.
- Vector jump: the interrupt number indexes the vector table, the handler entry address is fetched and jumped to, so software never has to “ask around” to find out who raised the request.
- Return and restore: when the handler finishes it executes the interrupt return instruction, the saved context is restored from the stack, and the fetch path switches back to the original track.
The original text describes aggregation as “an OR gate wiring a series of interrupts onto one request pin,” and this is crucial: an OR gate answers only “is there one,” never “which one.” Identifying the source requires an extra mechanism, and two approaches are common — vectoring (the controller supplies the interrupt number directly) and daisy-chain acknowledge (the acknowledge signal travels along a chain, and only the first device that both receives it and actually has a request is allowed to put its vector on the bus).
The daisy-chain logic can be expressed as pseudocode. Note that the “upstream acknowledged” control line comes from the previous stage, not directly from the CPU:
// Acknowledge decision logic at each device node (high-level description of hardware combinational logic)bool upstream_ack = ...; // acknowledge propagation signal from the previous devicebool has_request = dev->irq_pending;bool cpu_ack = cpu_is_asserting_ack;if (cpu_ack && upstream_ack && has_request) {dev->vector_out = dev->vector; // drive this device's vector onto the busdev->ack_out = true; // propagate the acknowledge to the next stage} else {dev->vector_out = 0; // high-impedance/zero, does not drive the busdev->ack_out = upstream_ack; // acknowledge keeps propagating downstream}
This logic answers the question the original text left open: why the AND gate after B takes one control input from A2–E2 rather than directly from the CPU. The acknowledge has to ask devices one by one in priority order; only after a preceding stage declines does the next one get a chance, otherwise several devices would drive the bus at the same time.
3. Masking, Priority, and Nesting: Why Real-Time Systems Need Them
- Masking has two levels: the global enable bit (switched with a single instruction) and per-source mask bits (configured by source in the controller). They are combined with AND, so if either is “off” there is no response.
- Priority determines service order when requests arrive simultaneously, and also who may interrupt whom. Only a request with higher priority (or a lower threshold) can trigger nesting while a handler is running.
- Nesting costs stack depth and worsens worst-case latency, so in practice a “short top half plus deferred bottom half” is often used to compress interrupt-disabled time.
An interrupt-disabled region should contain only the few statements that must be atomic. Too long a region directly amplifies interrupt latency and breaks real-time behavior; too short a region fails to protect shared data. The test is simple: does this code read or write data that a handler can modify?
4. Interrupts and Exceptions: One Mechanism, Two Trigger Sources
Divide-by-zero, illegal instructions, page faults, and system calls all use the same “switch tracks” mechanism; the difference is that the trigger source is inside the processor rather than on an external pin. In engineering practice, external sources are called interrupts (asynchronous, unrelated to the current instruction) and internal sources are called exceptions (synchronous, caused by the current instruction). Once this is clear, page-fault handling, single-step debugging, and trapping into the kernel on a system call stop being scattered facts and become instances of one model.
5. The Right Approach on the Software Side
The top half should only “confirm the source, clear the flag, move the minimum amount of data”; the heavy work goes to the bottom half, which avoids long interrupt-disabled periods:
// Top half: extremely short; no blocking, no sleeping, no long loopsvoid irq_handler_top(void) {uint32_t src = irq_controller_get_pending();if (src & SRC_UART_RX) {uart_rx_buf_push(uart_read_byte()); // enqueue onlyirq_controller_clear_pending(SRC_UART_RX);softirq_raise(SOFTRIG_RX);}}// Bottom half: sleepable context, handles time-consuming work such as protocol parsingvoid softirq_bottom(void) {while (uart_rx_buf_pop(&b)) {protocol_feed(b);}}
When protecting shared data, pick the primitive by context: data shared with a hardware interrupt needs interrupt disabling or a take/release semaphore; between tasks use a mutex; lock-free structures need correct memory barriers, not assignments that merely “look atomic.”
6. Common Misconceptions and a Debugging Order
- Misconception 1: that interrupts require polling. Polling is software actively checking; an interrupt is hardware actively switching the track.
- Misconception 2: that one request pin can serve only one device. Aggregation relies on OR logic; identification relies on vectors or daisy-chain acknowledge.
- Misconception 3: doing time-consuming work inside the handler. This lengthens interrupt-disabled time and causes losses.
- Misconception 4: forgetting to clear the request flag, so the same interrupt is entered repeatedly, appearing as “an infinite loop the moment the interrupt fires.”
When debugging, follow the path in order: confirm the peripheral sets its flag → the controller lets it through (masking and priority) → the request reaches the sampling pin → the vector is correct → the context and stack are intact. Locating the fault stage by stage is far more efficient than changing code blindly.
7. Extending the Model to Cloud and Distributed Environments
The single-machine interrupt model maps smoothly onto cloud architectures: event notifications in object storage or a message queue act as the “request line,” with the platform handling aggregation and delivery; probes and monitoring alerts on a container platform take on “sampling and decision”; log services and distributed tracing are used to reconstruct the “context.” Carrying over the interrupt principle of a “short top half, slow bottom half” means consumers acknowledge and persist quickly, then hand heavy processing to asynchronous workflows, balancing throughput against latency.
