Overview

An interrupt is a signal that breaks the normal execution flow to handle an event. When an interrupt occurs, the CPU pauses its current task, jumps to an interrupt service routine (ISR), and after the ISR completes it resumes the original task. In other words, interrupts let hardware or software requests “call” the CPU’s attention immediately, then let the program continue “as if nothing happened” after handling it.

Why are interrupts needed?

  • Avoid Polling: More efficient than continuously checking device status (polling), reducing CPU overhead and increasing system throughput
  • Real-Time Responsiveness: Essential for systems requiring quick reactions to events
    • Automotive airbag systems detecting collisions
    • Network Interface Cards (NICs) processing incoming packets

Interrupt Types

  • Hardware Interrupts: Triggered by devices (e.g., keyboard, NIC). Managed by the Programmable Interrupt Controller (PIC) or APIC.
  • Software Interrupts: Generated by software (e.g., int 0x80 for syscalls).
  • Exceptions: CPU-generated (e.g., divide-by-zero, page faults).

How the Kernel Registers Interrupts

  1. Interrupt Descriptor Table (IDT) Initialization:
    • At boot, the kernel populates the IDT with default handlers (e.g., for exceptions).
    • Hardware interrupts are mapped to a generic entry (e.g., common_interrupt on x86).
  2. Device Drivers:
    • Drivers request a specific IRQ (Interrupt Request Line) using request_irq().
    • Example:
    int request_irq(unsigned int irq, irq_handler_t handler, unsigned long flags,
    				const char *name, void *dev);
    
    • irq: The interrupt number (e.g., IRQ 1 for keyboard).
    • handler: The ISR function.
    • flags: Options like IRQF_SHARED for shared interrupts.
    • dev: A cookie passed to the ISR (used for shared IRQs).

What happens when an interrupt is occurred?

See Interrupt Handling Flow

References

Prompt

I want you to explain me what happens in the kernel when the interrupt is called. Start from where the kernel/cpu comes to know that what interrupts even exist, then what interrupt descriptor table is and how the cpu knows about it, how is this table used and managed. Also what the programmer would do to add their own interrupt in any device driver. I know little about top half and bottom half, so your job is to teach me the concepts necessary for actually calling interrupts or isr and routing back to the original cpu state. Give all the kernel apis used in this process. Give relevant example.