Sprint 3 — Interrupts & Exceptions

Handle CPU exceptions and hardware interrupts safely.

🔲 Planned

Table of contents

Overview #

Sprint 3 enables the kernel to respond to CPU exceptions (page faults, general protection faults) and hardware interrupts (timer, keyboard). This is a prerequisite for the scheduler (Sprint 4) and for safely switching to our own page tables.


GDT (Global Descriptor Table) #

🔲 Not Yet Implemented

The GDT defines memory segments for Ring 0 (kernel) and Ring 3 (user) code. On x86_64, segments are mostly legacy, but the GDT is still required for:

Planned Design


IDT (Interrupt Descriptor Table) #

🔲 Not Yet Implemented

The IDT maps interrupt/exception vectors (0–255) to handler functions.

Exception Handlers (Vectors 0–31)

VectorExceptionHandler Plan
0Divide ErrorPrint diagnostics, kill process
6Invalid OpcodePrint diagnostics, kill process
8Double FaultIST1 stack, print registers, halt
13General Protection FaultPrint error code + RIP, kill process
14Page FaultDetailed handler (see below)

Page Fault Handler (Vector 14)

The most important exception handler. On page fault, the CPU provides:

Planned behavior:

  1. Kernel fault → panic with full register dump (bug in the kernel)
  2. User fault on valid VMA → map the page (demand paging, copy-on-write)
  3. User fault on invalid address → deliver signal / kill process

IRQ Handlers (Vectors 32+)

After the LAPIC and I/O APIC are configured:


LAPIC (Local APIC) #

🔲 Not Yet Implemented

Each CPU core has its own Local APIC for:

Timer Calibration

  1. Program the PIT (Programmable Interval Timer) for a known interval
  2. Start the LAPIC timer in free-running mode
  3. Wait for the PIT interval to elapse
  4. Read the LAPIC timer count → compute ticks per millisecond
  5. Use one-shot mode: set the deadline, get exactly one interrupt, reprogram

MMIO Access

The LAPIC is accessed via memory-mapped registers at a fixed physical address (typically 0xFEE00000), accessed through the HHDM.


I/O APIC #

🔲 Not Yet Implemented

The I/O APIC routes external hardware interrupts to CPU cores.

MADT Parsing

The ACPI MADT (Multiple APIC Description Table) contains:

Redirection Table

Each I/O APIC pin has a Redirection Table Entry (RTE) that specifies:

Legacy IRQ Routing

IRQDeviceI/O APIC Pin
0PIT TimerGSI 2 (typically remapped)
1KeyboardGSI 1
3COM2GSI 3
4COM1GSI 4
8RTCGSI 8
12PS/2 MouseGSI 12

Dependencies #


Deferred from Sprint 2 #

Two VMM features were deferred to Sprint 3 because they require exception handling:

  1. Kernel higher-half remap — Create our own PML4, map the kernel with proper W^X permissions, switch from Limine's page tables
  2. W^X enforcement — Set .text as Execute-only, .rodata as Read-only, .data/.bss as NX (No Execute)

These will be the first tasks in Sprint 3, immediately after IDT setup enables page fault debugging.