roadmap BUILT

316. Which core booted: making NIFE_SMP=2 mean something on x86_64

Built by a lane on milestone/316-x86-smp-two-cores. (Number provisional until the merge queue lands it.)

arch::x86_64::ap_boot's BUGS #3 is fixed at its root, both of its symptoms are gone, and the two-core suite was run repeatedly rather than once. The NIFE_SMP default stays at 1, and the reason it stays is not the reason it stayed before, which is the finding worth carrying out of this milestone.

The bug, and why it looked correct for a month

arch::x86_64::boot_cpu_id read CPUID leaf 1's initial local APIC id on every call:

let leaf1 = isa::cpuid(1);
((leaf1.ebx >> 24) & 0xff) as usize

That is a correct answer to "which core am I". Every caller in this tree wants "which core booted", and those are the same number only while there is one core. Milestone 161 (kernel) changed this function from a hardcoded 0 to the CPUID read for a real reason. The roster seats the boot core at the slot its own local APIC id names, so the two have to agree. It got the mechanism wrong while getting the requirement right.

The other two architectures never had the option of being wrong this way, and the asymmetry is instructive:

Architecture boot_cpu_id Why that shape
aarch64 the constant 0 the boot core is architecturally affinity 0 on virt; nothing to record
riscv64 reads BOOT_HARTID OpenSBI hands the id in a0 and it is gone after _start, so boot.s must catch it
x86_64 (before) recomputes from CPUID CPUID stays readable forever, so nothing ever forced the record

RISC-V got this right by accident of its boot protocol. Its id would have been lost if boot.s had not stashed it, so the record existed for a reason unrelated to correctness-under-SMP, and then turned out to be the correct shape anyway. x86 had no such forcing function: the value is always available, so recomputing it read as the simpler option right up until a second core existed.

Both symptoms, one cause

DECISIONS §28's placement can migrate a test body onto a secondary. When it did, that body asked boot_cpu_id and got the secondary's id:

One was a hang-to-timeout and the other a false alarm about stack exhaustion, and they looked like two bugs in two subsystems. They are one line.

The fix

boot.s's _start_high stamps the id once, after it zeroes .bss and before it calls kernel_main:

    mov eax, 1
    cpuid
    shr ebx, 24
    movzx ebx, bl
    movabs rax, offset BOOT_CPU_ID
    mov [rax], rbx

and boot_cpu_id reads the record. That is riscv64's BOOT_HARTID move, deliberately, rather than a third shape: code that runs exactly once, on exactly the boot processor, cannot tell a later caller that the later caller is it. aarch64 is untouched and keeps its constant, because a record of a value the architecture fixes would be machinery with nothing to hold.

The ordering that makes this safe is worth stating, because the function is called from the kernel's very first Rust statement (cpu::init_this_cpu(arch::boot_cpu_id()), before the console, the GDT or ACPI). The stamp precedes call kernel_main, so there is no window in which the accessor can read the AtomicUsize's zero default. The secondary entry path (ap_long_mode_entry) never writes it.

BOOT_CPU_ID is a provisional name. It mirrors BOOT_HARTID on the architecture that already had this shape, which is the answer to "what does this tree already do in the analogous case", but names are an architect's.

The number

Measured with NIFE_SMP=2 script/repeat-under-load -n 12 -s 0 -- --arch x86_64 on patagonia (Mac15,3, 8 cores), no induced load, logs under target/acceptance/. Each run is a full script/test --arch x86_64, which is three boots: SeaBIOS plus two OVMF.

5 of 12 runs green, load average 2.0 to 8.9, one QEMU at every sample (no neighboring lane). The interesting number is not that one. It is that all seven failures are the same assertion in a_revoked_holder_faults_on_its_next_port_write in system_tests/src/user/x86_port_tests.rs, and none of them is #3 or #1:

what was measured across the 12 runs
smp::tests::every_secondary_runs_scheduled_work 19 of 19 ok (it runs once per boot that reaches it)
stack::report_high_water's secondary line 12 of 12 read core1 9760/65536 bytes (14%)
smp: N core(s) online 26 of 26 boots read 2 core(s) online
failures 7, all a_revoked_holder_faults_on_its_next_port_write, left: 2, right: 1

Before the fix, every_secondary_runs_scheduled_work failed about half of runs at two cores (ap_boot.rs's own measurement) and the high-water report printed 65536/65536. Nineteen clean runs of the one and twelve of the other is not a proof. A flake that fires one run in six needs more than twelve to be called gone. It is enough to say the half-the-time failure is not half-the-time any more.

Neither BUGS #1 nor the UEFI two-core start flake fired once. Milestone 412 (design/roadmap/0412-the-uefi-boot-gate-asserts-two-cores-that-do-not-always-start.md) records smp: cpu 1 did not start (firmware returned -1) at one run in three from a three-run sample; this campaign saw 26 two-core boots and zero such lines, 8 of them under OVMF. That is incidental evidence rather than the dedicated cargo xtask uefi-boot measurement the proposal asks for. It is a different tree than the one that proposal was written against. One-in-three and zero-in-twenty-six do not sit together comfortably, and somebody should reconcile them.

Why the default stays at 1, and what changed about the reason

The remaining blocker is not an SMP bring-up bug. It is a confinement defect that the second core makes visible:

user::x86_port_tests::a_revoked_holder_faults_on_its_next_port_write goes red intermittently at two cores, with left: 2, right: 1. The revoked holder's out succeeded and the word arrived, where the test demands a fault. That is exactly the window sched::delete_port_range_caps_impl already documents at itself:

This resets the revoker's core only. ... On a multi-core x86 a holder that is running on another core keeps its installed bitmap until that core's next context switch, so its in/out succeed for up to one tick after this returns.

Milestone 313's audit found that window, reasoned it to "at most one tick", and accepted it. Milestone 315 is the lane that closes it by broadcasting the reset over the existing shootdown IPI.

This is a better outcome than a green run, and it bears directly on design/decisions/0153-two-core-x86-test-sequencing.md, which is PROPOSED and was gated on this milestone:

None of which is this lane's call to make: §153 (how a two-core x86_64 test earns its place) is an architect's, and the above is evidence for it rather than an answer to it.

BUGS

Follow-on

Index row

arch::x86_64::boot_cpu_id recomputed CPUID leaf 1's initial local APIC id on every call, which answers "which core am I" where every caller wants "which core booted"; the two agree only while there is one core. So a test body DECISIONS §28 (SMP placement: two random choices at spawn)'s placement had migrated onto a secondary took that secondary for the boot core. Two symptoms in two subsystems turned out to be one line. every_secondary_runs_scheduled_work inverted its own "is a secondary" predicate and waited out its bound for a mark the real boot core never sets. stack::report_high_water scanned an unpainted slot and reported a secondary stack at 65536/65536. boot.s now stamps the id into BOOT_CPU_ID (provisional) before calling kernel_main and the accessor reads the record: riscv64's BOOT_HARTID shape, because code that runs once on exactly the boot processor cannot misinform a later caller. Twelve two-core runs came back 5 green, and the useful numbers are the others. 19 of 19 on the migrated-boot-core test, 12 of 12 correct high-water lines, 26 of 26 boots with both cores online, and all seven failures one assertion at one line. The NIFE_SMP default stays at 1, for a new reason: what keeps the two-core suite from being clean is a_revoked_holder_faults_on_its_next_port_write, red because PortRange::REVOKE resets the TSS I/O bitmap on the revoker's core only. That is milestone 313's accepted window and milestone 315's target. It is a red for exactly the reason the test exists rather than substrate flakiness, which is evidence DECISIONS §153 was written without. The two-core observer 315 planned to write already exists in the suite. BUGS #1 remains open and did not fire at two cores.