loader/
cpuid.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Provides common CPUID values and functions.
5
6use x86defs::cpuid::CpuidFunction;
7use zerocopy::FromBytes;
8use zerocopy::FromZeros;
9use zerocopy::Immutable;
10use zerocopy::IntoBytes;
11use zerocopy::KnownLayout;
12
13/// CPUID information used to build the CPUID page for SNP.
14#[repr(C)]
15#[derive(Debug, IntoBytes, Immutable, KnownLayout, FromBytes)]
16pub struct SNP_REQUIRED_CPUID_LEAF {
17    pub eax: u32,
18    pub ecx: u32,
19}
20
21macro_rules! cpuid_leaf {
22    ($eax: expr, $ecx: expr) => {
23        SNP_REQUIRED_CPUID_LEAF {
24            eax: $eax.0,
25            ecx: $ecx,
26        }
27    };
28}
29
30/// The list of static CPUID leaves stored in the CPUID page for SNP without a paravisor.
31/// The extended SEV leaf must be first in the list of required CPUID leaves.
32pub const SNP_REQUIRED_CPUID_LEAF_LIST_UEFI: [SNP_REQUIRED_CPUID_LEAF; 20] = [
33    cpuid_leaf!(CpuidFunction::ExtendedSevFeatures, 0),
34    cpuid_leaf!(CpuidFunction::VersionAndFeatures, 0),
35    cpuid_leaf!(CpuidFunction::CacheAndTlbInformation, 0),
36    cpuid_leaf!(CpuidFunction::MonitorMwait, 0),
37    cpuid_leaf!(CpuidFunction::PowerManagement, 0),
38    cpuid_leaf!(CpuidFunction::DirectCacheAccessParameters, 0),
39    cpuid_leaf!(CpuidFunction::PerformanceMonitoring, 0),
40    cpuid_leaf!(CpuidFunction::ExtendedFeatures, 0),
41    cpuid_leaf!(CpuidFunction::CacheParameters, 0),
42    cpuid_leaf!(CpuidFunction::CacheParameters, 1),
43    cpuid_leaf!(CpuidFunction::CacheParameters, 2),
44    cpuid_leaf!(CpuidFunction::ExtendedTopologyEnumeration, 0),
45    cpuid_leaf!(CpuidFunction::ExtendedTopologyEnumeration, 1),
46    cpuid_leaf!(CpuidFunction::ExtendedVersionAndFeatures, 0),
47    cpuid_leaf!(CpuidFunction::ExtendedL1CacheParameters, 0),
48    cpuid_leaf!(CpuidFunction::ExtendedL2CacheParameters, 0),
49    cpuid_leaf!(CpuidFunction::ExtendedPowerManagement, 0),
50    cpuid_leaf!(CpuidFunction::ExtendedAddressSpaceSizes, 0),
51    cpuid_leaf!(CpuidFunction::ExtendedSvmVersionAndFeatures, 0),
52    cpuid_leaf!(CpuidFunction::ProcessorTopologyDefinition, 0),
53];
54
55/// The list of static CPUID leaves stored in the CPUID page for SNP with an underhill based
56/// paravisor. The extended SEV leaf must be first in the list of required CPUID leaves.
57pub const SNP_REQUIRED_CPUID_LEAF_LIST_PARAVISOR: [SNP_REQUIRED_CPUID_LEAF; 35] = [
58    cpuid_leaf!(CpuidFunction::ExtendedSevFeatures, 0),
59    cpuid_leaf!(CpuidFunction::VendorAndMaxFunction, 0),
60    cpuid_leaf!(CpuidFunction::VersionAndFeatures, 0),
61    cpuid_leaf!(CpuidFunction::ExtendedMaxFunction, 0),
62    cpuid_leaf!(CpuidFunction::MonitorMwait, 0),
63    cpuid_leaf!(CpuidFunction::DirectCacheAccessParameters, 0),
64    cpuid_leaf!(CpuidFunction::PerformanceMonitoring, 0),
65    cpuid_leaf!(CpuidFunction::ExtendedFeatures, 0),
66    cpuid_leaf!(CpuidFunction::ExtendedTopologyEnumeration, 0),
67    cpuid_leaf!(CpuidFunction::ExtendedTopologyEnumeration, 1),
68    cpuid_leaf!(CpuidFunction::ExtendedVersionAndFeatures, 0),
69    cpuid_leaf!(CpuidFunction::ExtendedL1CacheParameters, 0),
70    cpuid_leaf!(CpuidFunction::ExtendedL2CacheParameters, 0),
71    cpuid_leaf!(CpuidFunction::ExtendedPowerManagement, 0),
72    cpuid_leaf!(CpuidFunction::ExtendedAddressSpaceSizes, 0),
73    cpuid_leaf!(CpuidFunction::ExtendedSvmVersionAndFeatures, 0),
74    cpuid_leaf!(CpuidFunction::ProcessorTopologyDefinition, 0),
75    cpuid_leaf!(CpuidFunction::ExtendedStateEnumeration, 0),
76    cpuid_leaf!(CpuidFunction::ExtendedStateEnumeration, 1),
77    cpuid_leaf!(CpuidFunction::ExtendedStateEnumeration, 2),
78    cpuid_leaf!(CpuidFunction::ExtendedStateEnumeration, 3),
79    cpuid_leaf!(CpuidFunction::ExtendedStateEnumeration, 4),
80    cpuid_leaf!(CpuidFunction::ExtendedStateEnumeration, 5),
81    cpuid_leaf!(CpuidFunction::ExtendedStateEnumeration, 6),
82    cpuid_leaf!(CpuidFunction::ExtendedStateEnumeration, 7),
83    cpuid_leaf!(CpuidFunction::ExtendedStateEnumeration, 8),
84    cpuid_leaf!(CpuidFunction::ExtendedBrandingString1, 0),
85    cpuid_leaf!(CpuidFunction::ExtendedBrandingString2, 0),
86    cpuid_leaf!(CpuidFunction::ExtendedBrandingString3, 0),
87    cpuid_leaf!(CpuidFunction::ExtendedTlb1GBIdentifiers, 0),
88    cpuid_leaf!(CpuidFunction::ExtendedOptimizationIdentifiers, 0),
89    cpuid_leaf!(CpuidFunction::CacheTopologyDefinition, 0),
90    cpuid_leaf!(CpuidFunction::CacheTopologyDefinition, 1),
91    cpuid_leaf!(CpuidFunction::CacheTopologyDefinition, 2),
92    cpuid_leaf!(CpuidFunction::CacheTopologyDefinition, 3),
93];
94
95#[repr(C)]
96#[derive(Debug, IntoBytes, Immutable, KnownLayout, FromBytes, Clone, Copy)]
97pub struct HV_PSP_CPUID_LEAF {
98    pub eax_in: u32,
99    pub ecx_in: u32,
100    pub xfem_in: u64,
101    pub xss_in: u64,
102    pub eax_out: u32,
103    pub ebx_out: u32,
104    pub ecx_out: u32,
105    pub edx_out: u32,
106    pub reserved_z: u64,
107}
108
109pub const HV_PSP_CPUID_LEAF_COUNT_MAX: usize = 64;
110
111#[repr(C)]
112#[derive(Debug, IntoBytes, Immutable, KnownLayout, FromBytes)]
113pub struct HV_PSP_CPUID_PAGE {
114    pub count: u32,
115    pub reserved_z1: u32,
116    pub reserved_z2: u64,
117    pub cpuid_leaf_info: [HV_PSP_CPUID_LEAF; HV_PSP_CPUID_LEAF_COUNT_MAX],
118}
119
120impl Default for HV_PSP_CPUID_PAGE {
121    fn default() -> Self {
122        HV_PSP_CPUID_PAGE {
123            count: 0,
124            reserved_z1: 0,
125            reserved_z2: 0,
126            cpuid_leaf_info: [HV_PSP_CPUID_LEAF::new_zeroed(); HV_PSP_CPUID_LEAF_COUNT_MAX],
127        }
128    }
129}