vmm: Rename patch_cpuid() to generate_common_cpuid()

This reflects that it generates CPUID state used across all vCPUs.
Further ensure that errors from this function get correctly propagated.

Signed-off-by: Rob Bradford <robert.bradford@intel.com>
This commit is contained in:
Rob Bradford 2021-02-09 14:55:19 +00:00
parent ccdea0274c
commit 50a995b63d
2 changed files with 23 additions and 12 deletions

View File

@ -173,9 +173,6 @@ pub enum Error {
/// Missing SGX_LC CPU feature /// Missing SGX_LC CPU feature
MissingSgxLaunchControlFeature, MissingSgxLaunchControlFeature,
// Error populating Cpuid
PopulatingCpuid,
} }
impl From<Error> for super::Error { impl From<Error> for super::Error {
@ -337,6 +334,7 @@ pub fn configure_vcpu(
cpuid: CpuId, cpuid: CpuId,
kvm_hyperv: bool, kvm_hyperv: bool,
) -> super::Result<()> { ) -> super::Result<()> {
// Per vCPU CPUID changes; common are handled via CpuManager::generate_common_cpuid()
let mut cpuid = cpuid; let mut cpuid = cpuid;
CpuidPatch::set_cpuid_reg(&mut cpuid, 0xb, None, CpuidReg::EDX, u32::from(id)); CpuidPatch::set_cpuid_reg(&mut cpuid, 0xb, None, CpuidReg::EDX, u32::from(id));
CpuidPatch::set_cpuid_reg(&mut cpuid, 0x1f, None, CpuidReg::EDX, u32::from(id)); CpuidPatch::set_cpuid_reg(&mut cpuid, 0x1f, None, CpuidReg::EDX, u32::from(id));

View File

@ -174,6 +174,18 @@ pub enum Error {
/// Failed to allocate MMIO address /// Failed to allocate MMIO address
AllocateMMIOAddress, AllocateMMIOAddress,
/// Error populating CPUID with KVM HyperV emulation details
#[cfg(target_arch = "x86_64")]
CpuidKVMHyperV(vmm_sys_util::fam::Error),
/// Error populating CPUID with KVM HyperV emulation details
#[cfg(target_arch = "x86_64")]
CpuidSGX(arch::x86_64::Error),
/// Error populating CPUID with CPU identification
#[cfg(target_arch = "x86_64")]
CpuidIdentification(vmm_sys_util::fam::Error),
} }
pub type Result<T> = result::Result<T, Error>; pub type Result<T> = result::Result<T, Error>;
@ -554,7 +566,7 @@ impl CpuManager {
#[cfg(target_arch = "x86_64")] #[cfg(target_arch = "x86_64")]
let cpuid = { let cpuid = {
let phys_bits = physical_bits(config.max_phys_bits); let phys_bits = physical_bits(config.max_phys_bits);
CpuManager::patch_cpuid( CpuManager::generate_common_cpuid(
hypervisor, hypervisor,
&config.topology, &config.topology,
sgx_epc_sections, sgx_epc_sections,
@ -605,7 +617,7 @@ impl CpuManager {
} }
#[cfg(target_arch = "x86_64")] #[cfg(target_arch = "x86_64")]
fn patch_cpuid( fn generate_common_cpuid(
hypervisor: Arc<dyn hypervisor::Hypervisor>, hypervisor: Arc<dyn hypervisor::Hypervisor>,
topology: &Option<CpuTopology>, topology: &Option<CpuTopology>,
sgx_epc_sections: Option<Vec<SgxEpcSection>>, sgx_epc_sections: Option<Vec<SgxEpcSection>>,
@ -664,7 +676,8 @@ impl CpuManager {
} }
if let Some(sgx_epc_sections) = sgx_epc_sections { if let Some(sgx_epc_sections) = sgx_epc_sections {
arch::x86_64::update_cpuid_sgx(&mut cpuid, sgx_epc_sections).unwrap(); arch::x86_64::update_cpuid_sgx(&mut cpuid, sgx_epc_sections)
.map_err(Error::CpuidSGX)?;
} }
// Set CPU physical bits // Set CPU physical bits
@ -687,7 +700,7 @@ impl CpuManager {
edx: leaf.edx, edx: leaf.edx,
..Default::default() ..Default::default()
}) })
.unwrap(); .map_err(Error::CpuidIdentification)?;
} }
if kvm_hyperv { if kvm_hyperv {
@ -705,14 +718,14 @@ impl CpuManager {
edx: 0x7648204d, // "M Hv" edx: 0x7648204d, // "M Hv"
..Default::default() ..Default::default()
}) })
.unwrap(); .map_err(Error::CpuidKVMHyperV)?;
cpuid cpuid
.push(CpuIdEntry { .push(CpuIdEntry {
function: 0x40000001, function: 0x40000001,
eax: 0x31237648, // "Hv#1" eax: 0x31237648, // "Hv#1"
..Default::default() ..Default::default()
}) })
.unwrap(); .map_err(Error::CpuidKVMHyperV)?;
cpuid cpuid
.push(CpuIdEntry { .push(CpuIdEntry {
function: 0x40000002, function: 0x40000002,
@ -720,7 +733,7 @@ impl CpuManager {
ebx: 0xa0000, // "Version" ebx: 0xa0000, // "Version"
..Default::default() ..Default::default()
}) })
.unwrap(); .map_err(Error::CpuidKVMHyperV)?;
cpuid cpuid
.push(CpuIdEntry { .push(CpuIdEntry {
function: 0x4000_0003, function: 0x4000_0003,
@ -730,14 +743,14 @@ impl CpuManager {
| 1 << 9, // AccessPartitionReferenceTsc | 1 << 9, // AccessPartitionReferenceTsc
..Default::default() ..Default::default()
}) })
.unwrap(); .map_err(Error::CpuidKVMHyperV)?;
for i in 0x4000_0004..=0x4000_000a { for i in 0x4000_0004..=0x4000_000a {
cpuid cpuid
.push(CpuIdEntry { .push(CpuIdEntry {
function: i, function: i,
..Default::default() ..Default::default()
}) })
.unwrap(); .map_err(Error::CpuidKVMHyperV)?;
} }
} }