Skip to main content

ostd/specs/
context.rs

1//! Global context of OSTD verification.
2//!
3//! On x86_64, CPUID reads expose the platform-wide capabilities in `cpu_feature_model()`.
4use vstd::prelude::*;
5
6use core::arch::x86_64::CpuidResult;
7use x86::cpuid::CpuIdResult;
8
9verus! {
10
11/// The CPU capabilities and nominal TSC calibration data enumerated by CPUID.
12/// Each feature flag is false when its basic leaf is unavailable.
13pub ghost struct CpuFeatureModel {
14    /// The maximum basic leaf reported by CPUID.(EAX=0):EAX.
15    pub max_basic_leaf: u32,
16    pub supports_avx: bool,
17    /// AVX-512 Foundation.
18    pub supports_avx512f: bool,
19    pub supports_xapic: bool,
20    pub supports_x2apic: bool,
21    /// CPUID.(0x15, 0):EAX, the denominator of the TSC/core crystal clock ratio.
22    pub tsc_denominator: u32,
23    /// CPUID.(0x15, 0):EBX, the numerator of that ratio.
24    pub tsc_numerator: u32,
25    /// CPUID.(0x15, 0):ECX, the nominal core crystal clock frequency in Hz.
26    pub crystal_hz: u32,
27}
28
29impl CpuFeatureModel {
30    /// Constructs the shared model from the reference CPU's readings.
31    pub open spec fn from_cpuid(reference: spec_fn((u32, u32)) -> CpuidResult) -> Self {
32        let max_basic_leaf = reference((0, 0)).eax;
33        let features = reference((1, 0));
34        let tsc = reference((0x15, 0));
35        Self {
36            max_basic_leaf,
37            supports_avx: max_basic_leaf >= 1 && features.ecx & (1u32 << 28) != 0,
38            supports_avx512f: max_basic_leaf >= 7 && reference((7, 0)).ebx & (1u32 << 16) != 0,
39            supports_xapic: max_basic_leaf >= 1 && features.edx & (1u32 << 9) != 0,
40            supports_x2apic: max_basic_leaf >= 1 && features.ecx & (1u32 << 21) != 0,
41            tsc_denominator: tsc.eax,
42            tsc_numerator: tsc.ebx,
43            crystal_hz: tsc.ecx,
44        }
45    }
46}
47
48/// A reference CPU's register values indexed by CPUID leaf and subleaf.
49pub uninterp spec fn reference_cpuid() -> spec_fn((u32, u32)) -> CpuidResult;
50
51/// The CPUID data shared by every logical CPU on which OSTD executes.
52///
53/// The CPUID contracts assume that the maximum basic leaf agrees across those CPUs
54/// and remains stable throughout execution.
55pub open spec fn cpu_feature_model() -> CpuFeatureModel {
56    CpuFeatureModel::from_cpuid(reference_cpuid())
57}
58
59/// Whether `result`, read at (`leaf`, `subleaf`), agrees with `model`.
60///
61/// Fixes the maximum basic leaf, the modeled feature bits and the TSC calibration
62/// fields of leaf 0x15. The base frequency in leaf 0x16 is scoped to a logical
63/// processor (Intel SDM Vol. 1, Table 21-54).
64pub open spec fn cpuid_consistent(
65    model: CpuFeatureModel,
66    leaf: u32,
67    subleaf: u32,
68    result: CpuidResult,
69) -> bool {
70    subleaf == 0 && leaf <= model.max_basic_leaf ==> match leaf {
71        0 => result.eax == model.max_basic_leaf,
72        1 => {
73            &&& model.supports_avx == (result.ecx & (1u32 << 28) != 0)
74            &&& model.supports_xapic == (result.edx & (1u32 << 9) != 0)
75            &&& model.supports_x2apic == (result.ecx & (1u32 << 21) != 0)
76        },
77        7 => model.supports_avx512f == (result.ebx & (1u32 << 16) != 0),
78        0x15 => (result.eax, result.ebx, result.ecx) == (
79            model.tsc_denominator,
80            model.tsc_numerator,
81            model.crystal_hz,
82        ),
83        _ => true,
84    }
85}
86
87/// OSTD trusts this intrinsic to read subleaf zero under the platform assumptions
88/// of [`cpu_feature_model`].
89#[cfg(not(target_env = "sgx"))]
90pub assume_specification[ core::arch::x86_64::__cpuid ](leaf: u32) -> (result: CpuidResult)
91    ensures
92        cpuid_consistent(cpu_feature_model(), leaf, 0, result),
93    opens_invariants none
94    no_unwind
95;
96
97/// OSTD trusts this intrinsic to read the requested subleaf under the same
98/// platform assumptions.
99#[cfg(not(target_env = "sgx"))]
100pub assume_specification[ core::arch::x86_64::__cpuid_count ](leaf: u32, subleaf: u32) -> (result:
101    CpuidResult)
102    ensures
103        cpuid_consistent(cpu_feature_model(), leaf, subleaf, result),
104    opens_invariants none
105    no_unwind
106;
107
108/// `raw-cpuid` implements the native read behind `cpuid!` by copying the four
109/// registers returned by `__cpuid_count(leaf, subleaf)`.
110#[cfg(not(target_env = "sgx"))]
111pub assume_specification[ x86::cpuid::native_cpuid::cpuid_count ](
112    leaf: u32,
113    subleaf: u32,
114) -> (result: CpuIdResult)
115    ensures
116        cpuid_consistent(
117            cpu_feature_model(),
118            leaf,
119            subleaf,
120            CpuidResult { eax: result.eax, ebx: result.ebx, ecx: result.ecx, edx: result.edx },
121        ),
122    opens_invariants none
123    no_unwind
124;
125
126} // verus!