Skip to main content

Module cpuid

Module cpuid 

Source
Expand description

CPU feature detection via CPUID instruction.

Provides a CpuInfo struct populated at boot time with vendor, model, feature flags, and XSAVE geometry. All subsequent queries go through host() which returns the cached result.

Re-exportsΒ§

use crate::sync::SpinLock; πŸ”’
use alloc::string::String; πŸ”’
use bitflags::bitflags; πŸ”’
use core::sync::atomic::AtomicBool; πŸ”’
use core::sync::atomic::AtomicU64; πŸ”’
use core::sync::atomic::Ordering; πŸ”’

StructsΒ§

CpuFeatures
Logical internal bitmap.
CpuInfo
Cached CPU identification and feature information.
XsaveProfile
Cached extended-state save profile, frozen once at init().

EnumsΒ§

CpuVendor

ConstantsΒ§

XCR0_AVX
XCR0_HI16_ZMM
XCR0_OPMASK
XCR0_SSE
XCR0_X87
XCR0 component bits.
XCR0_ZMM_HI256

StaticsΒ§

HOST_CPU πŸ”’
HOST_DEFAULT_XCR0_CACHE πŸ”’
Lock-free cache of the host’s default XCR0 mask, written once during init(). Used by normalized_xcr0() in the context-switch hot path to avoid acquiring the HOST_CPU spinlock with interrupts disabled.
INITIALIZED πŸ”’
PROFILE_AREA_SIZE πŸ”’
PROFILE_XCR0 πŸ”’
USES_XSAVE πŸ”’
Lock-free copy of CpuInfo::features.contains(XSAVE), published by init() before INITIALIZED becomes true and never cleared.

FunctionsΒ§

boot_xsave_profile
Return the frozen XSAVE profile. Before init() (or without XSAVE), returns the conservative FXSAVE baseline (mask=x87|SSE, 512 bytes).
default_xcr0_for πŸ”’
Pure helper: the XCR0 mask this kernel would enable for info (x87 + SSE always, plus AVX / AVX-512 states when the CPU announces them AND the hardware supports the required XCR0 bits). Shared by init() (pre-lock computation) and host().
detect πŸ”’
Detect CPU features by interrogating CPUID leaves.
features_to_flags_string
Build a Linux-style flags string from CPU features.
host
Return a clone of the cached host CPU info. Panics if init() not called.
host_default_xcr0
Return the host’s default XCR0 mask (all supported features). Safe to call before init() : returns XCR0_X87 | XCR0_SSE if not yet initialized.
host_default_xcr0_fast
Return the host’s default XCR0 mask from the lock-free cache, falling back to the locked query before init() is complete.
host_uses_xsave
Whether the boot CPU supports XSAVE.
init
Detect and cache CPU information. Must be called once at BSP boot.
tsc_frequency_khz
Read the TSC frequency from CPUID leaf 0x15 (Time Stamp Counter and Core Crystal Clock Information). Returns kHz, or None if the leaf is absent or the core-crystal ratio is not enumerated.
xcr0_for_features
Compute the XCR0 mask for a given set of allowed features, clamped to what the host actually supports.
xsave_area_size πŸ”’
Walk CPUID leaf 0x0D for the components in xcr0 (skipping supervisor states managed via IA32_XSS) and return the save-area size, clamped to max_bytes and floored at 576 (legacy area + XSAVE header).
xsave_size_for_info πŸ”’
Compute the XSAVE area size needed for a given XCR0 mask directly from a CpuInfo, without consulting the global cache (host_uses_xsave/host). This is what init() must use, because the cache is not yet published when init() runs : otherwise AVX/AVX-512 would be given a 512-byte area.
xsave_size_for_xcr0
Compute the XSAVE area size needed for a given XCR0 mask. Falls back to 512 (FXSAVE) if XSAVE is not supported.