Overview: How the CPU handles hardware interrupts (like timer and keyboard) and CPU exceptions (like page faults and double faults) without vector collisions or deadlocks.
| Component | Source Link | What It Does | Why It Is Needed |
|---|---|---|---|
| IDT Setup | src/interrupts.rs:45 |
Configures 256 interrupt descriptors | Tells the CPU which function to run when an interrupt or exception occurs. |
| PIC Remapping | src/interrupts.rs:27 |
Remaps hardware IRQs to vectors 32–47 | Prevents hardware interrupts from colliding with CPU exception vectors (0–31). |
| Deadlock-Free Print | src/vga_buffer.rs:211 |
Disables interrupts while holding console lock | Prevents the kernel from hanging if a timer interrupt tries to print while a task is already printing. |
| Page Fault Handler | src/interrupts.rs:161 |
Reads the CR2 register and error code |
Detects invalid memory accesses, unmapped pages, and protection violations. |
"Handling interrupts on bare-metal x86 requires solving two main problems: interrupt vector conflicts and console lock deadlocks.
First, by default on the PC architecture, the 8259 PIC sends hardware interrupts (like the timer and keyboard) using vectors 0 to 15. But Intel reserved vectors 0 to 31 for CPU exceptions like Divide-by-Zero, Double Faults, and Page Faults. If we don't remap the PIC, a simple timer tick will be treated by the CPU as a Double Fault! To fix this, I remapped the PIC so that hardware interrupts start at vector 32 and go up to 47.
Second, we have to prevent interrupt deadlocks when printing. The VGA text console is protected by a lock. If a running task is printing with println! and holds that lock, and a timer interrupt fires right in the middle, the timer handler might also try to call println!. The timer handler would spin forever waiting for the lock, but the task holding the lock can never run to release it.
I solved this by wrapping console lock acquisition in without_interrupts. This temporarily disables CPU interrupts while writing to the screen, releases the lock, and then restores interrupts. That way, an interrupt handler can never interrupt a thread while it holds the console lock."
The PC motherboard has two 8259 interrupt controller chips (Master and Slave). By default, they send hardware IRQs using vectors 0–15, which clashes with CPU exceptions:
Default Layout (Broken):
Vectors 0..31 --> Reserved by CPU for exceptions (Page Fault, Double Fault, etc.)
IRQs 0..15 --> Also mapped to Vectors 0..15 by default <-- COLLISION!
Remapped Layout in RustOS:
Vectors 0..31 --> CPU Exceptions
IRQs 0..7 --> Vectors 32..39 (Master PIC)
IRQs 8..15 --> Vectors 40..47 (Slave PIC)
In src/interrupts.rs:27, we set PIC_1_OFFSET = 32 and PIC_2_OFFSET = 40, cleanly separating hardware events from CPU faults.
The Interrupt Descriptor Table (IDT) is an array of 256 entries loaded into the CPU using the lidt instruction. In src/interrupts.rs:45, we register handlers for key events:
INT3 for debugging.src/interrupts.rs:117) to prevent triple-fault resets when kernel stacks overflow.CR2 register (src/interrupts.rs:161).src/interrupts.rs:104).0x60 (src/interrupts.rs:215).When the CPU finishes handling a hardware interrupt, it must tell the PIC that it is done by sending an End of Interrupt (EOI) signal (writing byte 0x20 to the PIC command port). If the interrupt came from the slave PIC (IRQs 8–15), both the slave and master must receive an EOI.
If you forget the EOI, the PIC assumes the CPU is still busy and will never send another interrupt on that line.
To avoid deadlock between tasks and interrupt handlers, printing is implemented in src/vga_buffer.rs:211 using without_interrupts:
cli instruction).0xb8000.sti instruction) if they were on before.This guarantees that a timer or keyboard interrupt can never interrupt a thread while it holds the screen lock.
0x60 but forgot to send the EOI signal to the PIC. The PIC held its in-service line high and refused to deliver any new interrupts.notify_end_of_interrupt() (src/interrupts.rs:224) before returning.
without_interrupts. This disables interrupts before acquiring the console lock and restores them after releasing it, ensuring an interrupt handler cannot interrupt a task while it holds the lock."