diff --git a/crates/stela-boot-core/src/parking.rs b/crates/stela-boot-core/src/parking.rs index a323e3074f..d26c4d8b07 100644 --- a/crates/stela-boot-core/src/parking.rs +++ b/crates/stela-boot-core/src/parking.rs @@ -163,11 +163,97 @@ impl fmt::Display for Census { } } +/// A processor that neither parked before the firmware's AML nor took INIT +/// then: it may still read the startup page. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Holder { + pub apic: u32, + pub firmware_cpu: u32, +} + +/// The startup page when the PEs' processors start. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Page { + /// No processor may still read it. + Free, + /// Its holder took INIT at the second try, and reads nothing. + Released(Holder), + /// Its holder did not take INIT twice: no processor starts from the page, + /// and the boot goes on with the boot processor alone. + Held(Holder), +} + +/// The startup's answer to a held page: INIT its holder once more (`init`, +/// true when delivered). The boot never stops for it. +pub fn release(holder: Option, init: impl FnOnce(u32) -> bool) -> Page { + match holder { + None => Page::Free, + Some(holder) if init(holder.apic) => Page::Released(holder), + Some(holder) => Page::Held(holder), + } +} + +impl Page { + /// Whether processors may start from the page. + pub fn usable(self) -> bool { + !matches!(self, Self::Held(_)) + } +} + +impl fmt::Display for Page { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Free => f.write_str("NATIVE STARTUP PAGE free"), + Self::Released(Holder { apic, firmware_cpu }) => write!( + f, + "NATIVE STARTUP PAGE free: processor apic={apic:#x} firmware_cpu={firmware_cpu}, \ + which neither parked before the firmware's AML nor took INIT then, took INIT \ + now" + ), + Self::Held(Holder { apic, firmware_cpu }) => write!( + f, + "NATIVE STARTUP PAGE held: processor apic={apic:#x} firmware_cpu={firmware_cpu} \ + neither parked before the firmware's AML nor took INIT, twice, and may still \ + read the page: no other processor starts from it, and macOS runs on the boot \ + processor alone" + ), + } + } +} + #[cfg(test)] mod tests { use super::*; use alloc::format; + #[test] + fn a_held_startup_page_leaves_the_boot_processor_alone_and_never_stops_the_boot() { + let holder = Holder { + apic: 0x0c, + firmware_cpu: 6, + }; + let mut sent = None; + let page = release(Some(holder), |apic| { + sent = Some(apic); + false + }); + assert_eq!((page, sent), (Page::Held(holder), Some(0x0c))); + assert!(!page.usable()); + assert_eq!( + format!("{page}"), + "NATIVE STARTUP PAGE held: processor apic=0xc firmware_cpu=6 neither parked before \ + the firmware's AML nor took INIT, twice, and may still read the page: no other \ + processor starts from it, and macOS runs on the boot processor alone" + ); + // INIT delivered at the second try: the page is the startup's again. + let page = release(Some(holder), |_| true); + assert_eq!(page, Page::Released(holder)); + assert!(page.usable()); + assert!(format!("{page}").contains("apic=0xc firmware_cpu=6")); + // No holder: no INIT. + assert_eq!(release(None, |_| unreachable!()), Page::Free); + } + fn intel(leaf5_ecx: u32, leaf5_edx: u32) -> IdleCpuid { IdleCpuid { intel: true, diff --git a/crates/stela-boot/src/native_smp/executor.rs b/crates/stela-boot/src/native_smp/executor.rs index 206881d66e..12310b1dba 100644 --- a/crates/stela-boot/src/native_smp/executor.rs +++ b/crates/stela-boot/src/native_smp/executor.rs @@ -64,6 +64,11 @@ pub(crate) enum StartupFailure { slot: usize, reason: Refusal, }, + /// A processor that neither parked before the firmware's AML nor took + /// INIT, twice, may still read the startup page (`park::release_page`). + PageHeld { + apic: u32, + }, } /// This object and its descriptors remain pinned for the whole boot, even @@ -517,12 +522,6 @@ pub(crate) unsafe fn start( _fp_qualification: bool, ) -> Result { let cores = handoff.native_resources.cores as usize; - if cores > 1 && park::page_held() { - return Err(StartupFailure::Resources( - "a processor that did not park before the firmware's AML may still read the \ - startup page", - )); - } if cores == 0 || cores > capture.core_count as usize { return Err(StartupFailure::Resources( "the reserved cores are none or more than the processor capture selects", @@ -537,6 +536,9 @@ pub(crate) unsafe fn start( handoff.apic_base, layout::physical(handoff.apic_base & apic::BASE_ADDRESS), ); + // SAFETY: CPL0 on the BSP, which alone sends, with interrupts masked and + // no processor started from the page yet. + let page = unsafe { park::release_page(&sender, handoff.tsc_hz) }; let mut processors = Vec::with_capacity(cores - 1); let mut started: Vec = Vec::with_capacity(cores - 1); // SAFETY: CPL0, BSP; PAT exists under the validated native floor. @@ -613,6 +615,11 @@ pub(crate) unsafe fn start( // An AP that does not start ends the startup of the ones after it, and // the guest runs on the cores before it (`Executors::failure`). let mut failed: Vec<(usize, StartupFailure)> = Vec::new(); + if let stela_boot_core::parking::Page::Held(holder) = page + && cores > 1 + { + failed.push((1, StartupFailure::PageHeld { apic: holder.apic })); + } for slot in 1..cores { if !failed.is_empty() { break; diff --git a/crates/stela-boot/src/native_smp/executor/park.rs b/crates/stela-boot/src/native_smp/executor/park.rs index f2495900f9..1d51b17a53 100644 --- a/crates/stela-boot/src/native_smp/executor/park.rs +++ b/crates/stela-boot/src/native_smp/executor/park.rs @@ -21,7 +21,10 @@ //! firmware's own idle loop or online, never in wait-for-SIPI. A PE's //! processor parked there is started from the page again later, by INIT //! and SIPI, as the startup always does; one that does not park is sent -//! INIT again, so it reads nothing, and stays in wait-for-SIPI. +//! INIT again, so it reads nothing, and stays in wait-for-SIPI. One that +//! takes no INIT either holds the page: the startup sends it INIT once more +//! and, when that fails too, starts no processor from the page and boots on +//! the boot processor alone ([`release_page`]). use super::super::{bootstrap, firmware, memory_types::Image, topology}; use crate::{ apic::{self, Apic}, @@ -36,9 +39,9 @@ use core::{ x86_64::{__cpuid, CpuidResult}, }, cell::UnsafeCell, - sync::atomic::{AtomicPtr, AtomicU8, AtomicU32, AtomicUsize, Ordering}, + sync::atomic::{AtomicPtr, AtomicU8, AtomicU32, AtomicU64, AtomicUsize, Ordering}, }; -use stela_boot_core::parking::{Census, Idle, IdleCpuid, Left, UNPUBLISHED}; +use stela_boot_core::parking::{Census, Holder, Idle, IdleCpuid, Left, Page, UNPUBLISHED}; use stela_engine::machine::mmu::placement::Placement; /// A parked processor's stack: its descriptor installation runs on it, then @@ -220,15 +223,43 @@ pub(super) unsafe fn park( pub(super) static OTHERS_PARKED: AtomicU32 = AtomicU32::new(0); pub(super) static OTHERS_WAITING: AtomicU32 = AtomicU32::new(0); -/// A processor that did not park before the firmware's AML did not take -/// INIT again either: it may still read the startup page, from which no -/// other processor then starts. -static PAGE_HELD: core::sync::atomic::AtomicBool = core::sync::atomic::AtomicBool::new(false); +/// A processor that did not park before the firmware's AML and did not take +/// INIT again either, which may still read the startup page: its firmware +/// number above its APIC ID, or [`NO_HOLDER`]. +static HOLDER: AtomicU64 = AtomicU64::new(NO_HOLDER); +const NO_HOLDER: u64 = u64::MAX; /// Whether the startup page may still be read by a processor that did not /// park before the firmware's AML. pub(super) fn page_held() -> bool { - PAGE_HELD.load(Ordering::Acquire) + HOLDER.load(Ordering::Acquire) != NO_HOLDER +} + +/// Send the processor that holds the startup page, if one does, INIT once +/// more, and log what came of it: the page is free when it took it. When it +/// did not, the page stays held, no processor starts from it, and the boot +/// goes on with the boot processor alone (`Page::Held`). +/// +/// # Safety +/// On the boot processor, which alone sends interprocessor interrupts, with +/// interrupts masked, before any processor starts from the startup page. +pub(super) unsafe fn release_page(sender: &Apic, tsc_hz: u64) -> Page { + let word = HOLDER.load(Ordering::Acquire); + let holder = (word != NO_HOLDER).then_some(Holder { + apic: word as u32, + firmware_cpu: (word >> 32) as u32, + }); + let page = stela_boot_core::parking::release(holder, |apic| { + // SAFETY: a destination validated before its parking; the caller's. + unsafe { sender.init_only(apic, tsc_hz) }.is_ok() + }); + if let Page::Released(_) = page { + HOLDER.store(NO_HOLDER, Ordering::Release); + } + if page != Page::Free { + crate::output::arguments(format_args!("{page}\n")); + } + page } /// Each processor's wait from [`park_before_firmware`] (`Idle::encode`), by @@ -320,7 +351,10 @@ pub(super) unsafe fn park_before_firmware( // no other processor starts from the page here. // SAFETY: a validated destination; the BSP alone sends. if unsafe { sender.init_only(destination, handoff.tsc_hz) }.is_err() { - PAGE_HELD.store(true, Ordering::Release); + HOLDER.store( + u64::from(processor.processor_number) << 32 | u64::from(destination), + Ordering::Release, + ); break; } }