Skip to content

feat: Per-vector interrupt stubs (global_asm!, replaces ISR scanning) - #29

Merged
emilf merged 6 commits into
mainfrom
feat/phase1-hardening
Mar 28, 2026
Merged

emilf merged 6 commits into
mainfrom
feat/phase1-hardening

Conversation

@emilf

@emilf emilf commented Mar 28, 2026 •

Copy link
Copy Markdown
Owner

Replaces the bad APIC-ISR-scanning approach with per-vector assembly stubs — the canonical solution used by Linux, FreeBSD, and standard OS references.

Problem with ISR scanning (now reverted)

  • O(8 MMIO reads) on every hardware interrupt — unacceptable hot-path cost
  • Fragile reasoning under SMP: each CPU has its own LAPIC/ISR; the scan gives the right answer per-core but the design adds unnecessary complexity
  • Not how any production OS does it

Solution: per-vector assembly stubs

CPU takes interrupt on vector N
  → IDT[N]          → irq_stub_N        push N; jmp irq_stub_common
  → irq_stub_common → irq_dispatch_common (Rust, vector in RDI)
  → IRQ registry    → registered driver handler, or EOI + warn
  • 222 stubs generated by global_asm! macro loop in handlers.rs
  • Each stub is ~6 bytes: push $imm8 + jmp rel32 — fits in a cache line
  • IRQ_STUB_TABLE[256] maps vector → stub address for the IDT setup loop
  • IDT setup uses set_handler_addr to install each stub

SMP correctness

  • Stubs are pure read-only code, safely shared across all CPUs
  • irq_dispatch_common accesses IRQ_REGISTRY via Mutex — already SMP-safe
  • Per-CPU interrupt stacks (IST for NMI/DF/MCE) are a GDT/TSS concern, orthogonal to this

Documentation

  • docs/axioms/arch-x86_64.md — new axiom A3.1 documents the stub design as a binding invariant
  • docs/plans/interrupts-and-platform.md — new "IRQ Dispatch Architecture" section explains the design and SMP rationale

References

  • Linux: arch/x86/entry/entry_64.S irq_entries_start, idtentry.h
  • OSDev: Interrupt Service Routines — per-vector stubs pushing vector before common handler
  • Intel SDM Vol. 3 Ch. 6: interrupt gate mechanics (CPU pushes RFLAGS/CS/RIP, not vector)

Rowan (OpenClaw) added 2 commits March 28, 2026 13:58
- Add read_isr_vector() to read interrupt vector from APIC In-Service Register
- Update handler_general to use ISR reading to determine interrupt vector
- Support both xAPIC and x2APIC modes
- Fixes the critical issue where general interrupt handler couldn't identify vectors
Replace APIC-ISR scanning with the canonical per-vector stub approach.

Design (matches Linux irq_entries_start / FreeBSD IDTVEC):
  CPU takes interrupt on vector N
  → IDT[N] → irq_stub_N     (push N; jmp irq_stub_common)
  → irq_stub_common          (pop rdi; call irq_dispatch_common; iretq)
  → irq_dispatch_common      (Rust: IRQ registry lookup, EOI)

Why stubs over APIC ISR:
- ISR scan is 8 MMIO reads per interrupt — unacceptable on hot paths
- ISR approach is fragile for SMP (per-CPU LAPIC; confusing to reason about)
- Stubs bake the vector into code; no shared state needed at dispatch time
- SMP-safe: stubs are pure read-only code; registry access is Mutex-guarded

Also:
- Remove stale ISR-reading code from apic.rs
- Remove stale handler_general; update mod.rs IDT loop to use IRQ_STUB_TABLE
- Document design in docs/plans/interrupts-and-platform.md
- Add axiom A3.1 in docs/axioms/arch-x86_64.md
@emilf emilf changed the title feat: Implement interrupt vector extraction via APIC ISR reading feat: Per-vector interrupt stubs (global_asm!, replaces ISR scanning) Mar 28, 2026
Rowan (OpenClaw) added 4 commits March 28, 2026 17:20
Previously timer (0x40), serial (0x41), and xHCI (0x50) had separate
dedicated extern "x86-interrupt" handlers installed directly in the IDT.
All other IRQs went through the new per-vector stubs.

This inconsistency meant those three vectors bypassed the IRQ registry
entirely. The fix:

- Add stubs for 0x40 and 0x50 to the global_asm! table and IRQ_STUB_TABLE
- Convert handler_timer/handler_serial_rx/handler_usb_xhci from
  extern "x86-interrupt" fn to plain fn() (irq_timer/irq_serial_rx/irq_usb_xhci)
- Register them in the IRQ registry during init_idt()
- Remove install_timer_vector_runtime() — manual IDT patching no longer
  needed since IDT is populated correctly from init_idt() via IRQ_STUB_TABLE

Remaining special cases (intentional):
- 0xFE (APIC error) and 0xFF (spurious): Intel SDM requires spurious
  interrupts must NOT receive EOI; these keep dedicated Rust handlers.
- interrupts-and-platform.md: rewrite IRQ Dispatch Architecture section;
  documents that timer/serial/xHCI now go through the registry like all
  other IRQs, explains the registration pattern, why ISR scanning was
  rejected, and the SMP note about the registry Mutex
- arch-x86_64.md A3.1: rewrite to state the correct invariant — ALL
  hardware IRQs 0x20-0xFD use stubs; only 0xFE/0xFF are excepted and why
- outstanding-todos.md: mark interrupt vector extraction as done
init_idt() no longer hardcodes registrations for timer/serial/xHCI.
Each subsystem now owns its registration:

- lapic_timer_configure() registers irq_timer (0x40)
- init_serial()           registers irq_serial_rx (0x41)
- usb::init()             registers irq_usb_xhci (0x50)

init_idt() only sets up the IDT stub table. No driver knowledge lives there.

Also: make handlers module pub(crate) and irq_{timer,serial_rx,usb_xhci}
pub(crate) so drivers can reference them without going through interrupts::mod.

Docs updated: interrupts-and-platform.md and arch-x86_64.md A3.1.
Two bugs found during QEMU testing:

1. Wrong calling convention in irq_stub_common
   The kernel binary uses Microsoft x64 ABI (UEFI PE target), not SysV AMD64.
   First integer arg is in RCX, not RDI. Changed 'pop rdi' to 'pop rcx' in
   irq_stub_common. Without this, every dispatch received vector=0 and logged
   'Unhandled IRQ vector 0x00' on every interrupt.

2. Redundant serial IRQ registration
   SerialDriver::init() already registers its own handler (serial_interrupt_handler)
   in the IRQ registry. The extra register call added to init_serial() caused an
   'already registered' error. Removed the duplicate; SerialDriver::init() owns
   its registration.

   Also removed irq_serial_rx() from handlers.rs — unused after above fix.

Tested in QEMU: timer fires, xHCI MSI fires, USB keyboard detected, serial
driver binds, no Unhandled IRQ warnings, no double-registration errors.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant