Skip to content

refactor(abi): System V AMD64 for internal calls, Win64 only at the firmware boundary - #35

Open
emilf wants to merge 1 commit into
mainfrom
feat/sysv64-internal-abi
Open

emilf wants to merge 1 commit into
mainfrom
feat/sysv64-internal-abi

Conversation

@emilf

@emilf emilf commented Nov 17, 2026

Copy link
Copy Markdown
Owner

Summary

Uses the System V AMD64 ABI for internal calls and keeps the Microsoft x64 ABI only where firmware or raw assembly is the other side. Branched from main (76b9021) so it merges independently of #31 / #32 / #33.

Why the kernel was using MS ABI

The kernel builds under the UEFI target, where rustc forces is-like-windows because os is "uefi". So plain extern "C" means:

  • first argument in RCX
  • args in RCX/RDX/R8/R9

That is correct at the firmware boundary, but it had leaked into purely internal boundaries — so assembly calling into Rust had to use Microsoft registers, while every reference document and external snippet assumes SysV. PR #29 had to be fixed for exactly this ("Correct calling convention (RCX not RDI)").

The target spec cannot be edited to opt out: rustc rejects is_like_windows unless os is windows/uefi/cygwin, and rejects linker_flavor unless it matches is_like_msvc. Per-function ABI is the available mechanism.

Changes

Converted to extern "sysv64":

Deliberately left extern "C", each annotated in-code with the reason:

  • kernel_entry — called by the bootloader as extern "C" fn(u64); entry-abi: win64 in the target spec
  • after_high_half_entry / continue_after_stack_switch + the memory.rs extern block — reached by a raw jump, so declarations must pair with definitions
  • IRQ stub symbol declarations and IRQ_STUB_TABLE — addresses only, never called through Rust

Verification

theseus-qemu --headless, serial relay attached before boot:

  • 0 page/GP faults, no panic
  • LAPIC timer interrupt received successfully — the timer ISR dispatched through the converted irq_dispatch_common. This is the path the change actually exercises; a wrong ABI here faults immediately.
  • xHCI MSI handler invoked (first delivery) — MSI vector path intact
  • 1429 bytes on COM1; boot reaches monitor and idle loop

Docs

  • New axiom A1.1 — both conventions, the permitted-extern "C" set, the asm-register rule (irq_stub_common → RDI), and why disable-redzone: true must stay (SysV's 128-byte red zone is clobberable by an interrupt).
  • plans/interrupts-and-platform.md — implemented invariant for the SysV dispatch path.
  • AGENTS.md — new "Calling conventions" block under Gotchas so a fresh session learns this before hitting a compiler error.

Not done here (deliberately)

This does not split the bootloader and kernel. A real split (kernel as its own ELF on x86_64-unknown-none) is still worth doing eventually — but its motivating benefit is the ELF loader, which is the same work Phase 6 needs for user processes. Because internal boundaries are now explicitly sysv64, that future split becomes a spec-and-loader change with no ABI rewrite; with plain extern "C" it would silently change meaning the day the target changed.

…irmware boundary

Under the UEFI target rustc forces `is-like-windows` (os = "uefi"), so plain
`extern "C"` means the Microsoft x64 ABI: first argument in RCX, args in
RCX/RDX/R8/R9. That is correct where firmware is the other side, but it leaked
into purely internal boundaries, so assembly calling into Rust had to use
Microsoft registers while every reference and external doc assumes SysV.

Convert the internal boundaries to `extern "sysv64"`:

- `interrupts/handlers.rs::irq_dispatch_common` — called from `irq_stub_common`.
  The stub now pops the vector into RDI instead of RCX. This retires the
  Microsoft-ABI workaround added in PR #29 ("correct calling convention (RCX not
  RDI)"); the stub uses the register the SysV documentation assumes.
- `interrupts/mod.rs::noop_for_test`
- `monitor/commands/control.rs` — the monitor's `call` trampoline

Deliberately left `extern "C"`, each annotated with why:

- `lib.rs::kernel_entry` — called by the bootloader as `extern "C" fn(u64)` and
  `entry-abi: win64` in the target spec.
- `environment.rs::after_high_half_entry` / `continue_after_stack_switch` and the
  matching `memory.rs` extern block — reached by a raw jump, so the declaration
  must pair with the definition, not with a Rust caller.
- `handlers.rs` stub symbol declarations and `IRQ_STUB_TABLE` — addresses only,
  never called through Rust; the ABI string is declarative.

Verification (theseus-qemu --headless, serial relay attached before boot):
- 0 page/GP faults, no panic.
- `LAPIC timer interrupt received successfully` — the timer ISR dispatched
  through the converted `irq_dispatch_common`, which is the path this change
  actually exercises.
- `xHCI MSI handler invoked (first delivery)` — MSI vector path intact.
- 1429 bytes on COM1; boot reaches monitor and idle loop.

Docs:
- New axiom A1.1 recording both conventions, the permitted-`extern "C"` set, the
  asm-register rule, and the red-zone requirement (`disable-redzone: true` stays).
- interrupts-and-platform plan: implemented invariant for the SysV dispatch path.
- AGENTS.md: new "Calling conventions" gotcha block, so a fresh dev session sees
  it without inferring from a compiler error.

No behaviour change intended beyond ABI selection; the retained Win64 boundary is
unchanged and was re-verified by the boot.
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