Topic 02: IDT, 8259 PIC & Interrupt Handling

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.


1. High-Level Summary

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.

2. Spoken Interview Answer (Simple & Direct)

"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."


3. Architecture & Mechanics

3.1 Why Remap the 8259 PIC?

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.

3.2 IDT Setup

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:

3.3 End of Interrupt (EOI)

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.

3.4 Preventing VGA Console Deadlocks

To avoid deadlock between tasks and interrupt handlers, printing is implemented in src/vga_buffer.rs:211 using without_interrupts:

  1. Checks if interrupts are currently enabled.
  2. Disables interrupts (cli instruction).
  3. Acquires the console lock and writes text to VGA memory at physical address 0xb8000.
  4. Releases the lock.
  5. Re-enables interrupts (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.


4. Edge Cases & Debugging

⚠️ Bug Faced: System Freezing After First Keystroke
Symptom: Pressing the first key printed the character to the screen, but then the whole kernel froze—no more typing worked, and timer ticks stopped.
Cause: The keyboard handler read the scancode from port 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.
Fix: Ensured every interrupt handler always calls notify_end_of_interrupt() (src/interrupts.rs:224) before returning.

5. Quick Interview Q&A

Q: Why do we remap the 8259 PIC?
"Because by default, the PIC uses interrupt vectors 0 to 15, which overlaps with x86 CPU exception vectors like Double Fault (vector 8). Remapping moves hardware interrupts to vectors 32–47 so hardware signals don't collide with CPU exceptions."
Q: What is an EOI and what happens if you forget it?
"EOI stands for End of Interrupt. It tells the PIC that the current interrupt handler has finished. If you forget to send it, the PIC thinks the handler is still running and stops forwarding all subsequent interrupts on that line, freezing that device."
Q: How do you prevent deadlocks when printing inside interrupt handlers?
"We wrap lock acquisition in 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."