Repository navigation
feat: Per-vector interrupt stubs (global_asm!, replaces ISR scanning) - #29
Merged
Merged
Conversation
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
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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)
Solution: per-vector assembly stubs
global_asm!macro loop inhandlers.rspush $imm8+jmp rel32— fits in a cache lineIRQ_STUB_TABLE[256]maps vector → stub address for the IDT setup loopset_handler_addrto install each stubSMP correctness
irq_dispatch_commonaccessesIRQ_REGISTRYviaMutex— already SMP-safeDocumentation
docs/axioms/arch-x86_64.md— new axiom A3.1 documents the stub design as a binding invariantdocs/plans/interrupts-and-platform.md— new "IRQ Dispatch Architecture" section explains the design and SMP rationaleReferences
arch/x86/entry/entry_64.Sirq_entries_start,idtentry.h