interrupt_init, the kernel installs 256 IDT gate descriptors, remaps both PIC chips to avoid conflicts with CPU exception vectors, programs the Programmable Interval Timer (PIT) to fire at 100 Hz, and unmasks the interrupt lines that the kernel uses. Interrupt and exception handling are then fully operational for the remainder of the boot session.
Architecture Overview
Exception vectors
0–31 are reserved by the x86_64 architecture for CPU-generated faults, traps, and aborts (e.g., #DE Divide Error at vector 0, #PF Page Fault at vector 14). The kernel installs stub handlers for all 32 exception vectors; each stub calls exception_dispatch, which prints diagnostic information and halts the processor. Hardware interrupts from PIC-attached devices use vectors 0x20 and above.
Functions
interrupt_init
Initialize the IDT, reprogram the 8259 PIC, configure the PIT at 100 Hz, and install all exception and hardware interrupt handlers. Call this once during early kernel initialization, before enabling interrupts with interrupt_enable.
interrupt_enable.
interrupt_enable
Enable hardware interrupts on the current CPU by executing the STI (Set Interrupt Flag) instruction.
interrupt_enable only after interrupt_init has completed. Once enabled, the processor begins accepting and dispatching hardware interrupts through the IDT.
interrupt_disable
Disable hardware interrupts on the current CPU by executing the CLI (Clear Interrupt Flag) instruction.
interrupt_disable to create short critical sections that must not be preempted by interrupt handlers. Keep the disabled window as brief as possible to avoid missing time-critical hardware events.
interrupt_disable masks hardware IRQs but does not prevent NMIs (Non-Maskable Interrupts). NMIs bypass the interrupt flag and always reach the CPU.exception_dispatch
The kernel’s unified exception handler. Called automatically by the IDT exception stubs for all CPU exception vectors (0–31). This function prints fault information and halts the processor; it never returns.
void *
Pointer to the CPU exception frame pushed onto the kernel stack by the IDT stub. The exact layout depends on the exception vector; some exceptions push an error code.
exception_dispatch interprets the frame internally.Do not call
exception_dispatch directly from your code. It is invoked automatically by the IDT exception stubs whenever the CPU raises an unhandled fault, trap, or abort. Triggering it manually will halt the system unconditionally.interrupt_count
Return the number of times a specific IDT vector has fired since interrupt_init was called.
uint8_t
required
IDT vector number to query (
0–255). PIC hardware IRQs start at vector 0x20 (IRQ0 = PIT, IRQ1 = PS/2 keyboard, etc.).uint64_t
Cumulative interrupt count for the given vector since system initialization. Returns
0 for vectors that have never fired.interrupt_total
Return the total number of interrupts that have fired across all 256 IDT vectors.
uint64_t
Sum of per-vector counters across all 256 vectors. This value monotonically increases for the lifetime of the system.
interrupt_spurious_count
Return the number of spurious interrupts that the PIC has generated since interrupt_init was called.
uint64_t
Cumulative count of spurious interrupts detected and silently discarded by the kernel. A small, slowly growing number is normal. A rapidly increasing count may indicate a hardware problem or an IRQ line that is incorrectly left unmasked.
Querying Interrupt Statistics
You can read per-vector counters at any time from kernel code. Theirqstat built-in shell command displays these counters in a formatted table.
Example: Initialization Sequence
The following shows the recommended interrupt initialization order during kernel startup.If the CPU raises an exception (e.g., a page fault or divide-by-zero) after
interrupt_init returns, the corresponding IDT stub calls exception_dispatch automatically. The kernel prints fault details and halts — there is no attempt to recover from unhandled exceptions.