diff --git a/crates/stela-boot/src/engine/host.rs b/crates/stela-boot/src/engine/host.rs index 9361a4bec5..e11812044f 100644 --- a/crates/stela-boot/src/engine/host.rs +++ b/crates/stela-boot/src/engine/host.rs @@ -75,6 +75,7 @@ pub(super) mod local; use stela_boot_core::native_profile as profile; use stela_boot_core::ram; mod translated; +pub(crate) use local::activated_base; pub(crate) use local::context::{RootContext, RootExecution, RootIdentity, RootProcessScope}; #[cfg(feature = "nvk")] pub(crate) use local::processor; diff --git a/crates/stela-boot/src/engine/host/local.rs b/crates/stela-boot/src/engine/host/local.rs index eacec5585f..e58d0aa935 100644 --- a/crates/stela-boot/src/engine/host/local.rs +++ b/crates/stela-boot/src/engine/host/local.rs @@ -345,6 +345,13 @@ fn current() -> *mut Local { pointer } +/// This processor's GS base, read through its record's self pointer rather +/// than with RDMSR. Once [`activate`] has run on it: from then on GS is the +/// record, whose `this` names it. +pub(crate) fn activated_base() -> u64 { + current() as u64 +} + /// This processor's index, dense from zero. Once [`activate`] has run on it. /// The BSP installs its record before any AP starts. pub(crate) fn processor() -> u32 { diff --git a/crates/stela-boot/src/engine/host/local/context.rs b/crates/stela-boot/src/engine/host/local/context.rs index 30dc1ede3a..6053ab3d8f 100644 --- a/crates/stela-boot/src/engine/host/local/context.rs +++ b/crates/stela-boot/src/engine/host/local/context.rs @@ -18,9 +18,13 @@ use stela_drivers::{bus::mmio::owned::PhysicalSpan, platform::work::WorkError}; const IF: u64 = 1 << 9; const ADDRESS: u64 = 0x000f_ffff_ffff_f000; +/// This processor's GS base, as its record's self pointer names it. Every +/// owner and identity here belongs to an activated processor, whose GS base +/// is its own record from activation on, so a record of another processor +/// names that processor's base. The clock and IRQ checks run this many times +/// per guest clock read, where RDMSR would cost far more than the read. fn gs_base() -> u64 { - // SAFETY: CPL0 architectural observation at this native entry. - unsafe { crate::cpu::msr::GS_BASE.read() } + current() as u64 } fn root() -> u64 { let value; @@ -171,12 +175,8 @@ impl RootIdentity { return Err(WorkError::WrongCpu); } // SAFETY: immutable architectural observation; original owner kept. - let observed = unsafe { interrupts::observe() }; - if observed.gs != self.gs - || observed.gdt != self.gdt - || observed.idt != self.idt - || observed.task != self.task - { + let observed = unsafe { interrupts::observe_descriptors() }; + if observed.gdt != self.gdt || observed.idt != self.idt || observed.task != self.task { return Err(WorkError::CorruptContext); } Ok(()) diff --git a/crates/stela-boot/src/engine/interrupts.rs b/crates/stela-boot/src/engine/interrupts.rs index 7d1bab21f1..a01a43dc0a 100644 --- a/crates/stela-boot/src/engine/interrupts.rs +++ b/crates/stela-boot/src/engine/interrupts.rs @@ -536,24 +536,51 @@ pub(crate) struct Observation { /// CPL0 with this processor's permanently pinned descriptor/GS storage active. /// The private tables cannot be changed concurrently with these observations. pub(crate) unsafe fn observe() -> Observation { - let mut gdt = Pointer { limit: 0, base: 0 }; - let mut idt = Pointer { limit: 0, base: 0 }; - let (rsp, task): (u64, u16); + // SAFETY: the caller's contract. + let descriptors = unsafe { observe_descriptors() }; + let rsp: u64; let gs: u64; - // SAFETY: caller is the owning CPL0 processor with stable private tables. + // SAFETY: caller is the owning CPL0 processor. unsafe { - asm!("sgdt [{}]", in(reg) &mut gdt, options(nostack, preserves_flags)); - asm!("sidt [{}]", in(reg) &mut idt, options(nostack, preserves_flags)); - asm!("str {0:x}", out(reg) task, options(nostack, preserves_flags)); asm!("mov {}, rsp", out(reg) rsp, options(nostack, preserves_flags)); gs = crate::cpu::msr::GS_BASE.read(); } Observation { rsp, + gdt: descriptors.gdt, + idt: descriptors.idt, + task: descriptors.task, + gs, + } +} + +/// The descriptor-table bases and task register of [`Observation`]. +#[derive(Debug, Clone, Copy)] +pub(crate) struct Descriptors { + pub gdt: u64, + pub idt: u64, + pub task: u16, +} + +/// [`observe`] without its stack pointer and GS base, for checks that run +/// per guest clock read: the GS read is an RDMSR. +/// +/// # Safety +/// As for [`observe`]. +pub(crate) unsafe fn observe_descriptors() -> Descriptors { + let mut gdt = Pointer { limit: 0, base: 0 }; + let mut idt = Pointer { limit: 0, base: 0 }; + let task: u16; + // SAFETY: caller is the owning CPL0 processor with stable private tables. + unsafe { + asm!("sgdt [{}]", in(reg) &mut gdt, options(nostack, preserves_flags)); + asm!("sidt [{}]", in(reg) &mut idt, options(nostack, preserves_flags)); + asm!("str {0:x}", out(reg) task, options(nostack, preserves_flags)); + } + Descriptors { gdt: gdt.base, idt: idt.base, task, - gs, } } diff --git a/crates/stela-boot/src/engine/storage.rs b/crates/stela-boot/src/engine/storage.rs index 4e658a3aa2..467f5479de 100644 --- a/crates/stela-boot/src/engine/storage.rs +++ b/crates/stela-boot/src/engine/storage.rs @@ -118,12 +118,13 @@ pub(super) fn entered_loader_root(owner: &crate::paging::loader::LoaderTables) - if !HOST_DRIVERS.load(Ordering::Acquire) { return false; } - let (root, gs): (u64, u64); + let root: u64; // SAFETY: this internal query follows RootIdentity's CPL0/CPU admission. unsafe { asm!("mov {}, cr3", out(reg) root, options(nomem, nostack, preserves_flags)); - gs = crate::cpu::msr::GS_BASE.read(); } + // RootIdentity's admission is of an activated processor. + let gs = super::host::activated_base(); // The context is 0 only without PCIDs, whose CR3 holds none. let context = root & CONTEXT; gs != 0