vmm: arch: Make phys_bits functionality use CPU vendor API

Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
This commit is contained in:
Anatol Belski 2023-05-30 14:42:55 +02:00 committed by Anatol Belski
parent 7df80220ec
commit 7bf2a2c382
5 changed files with 18 additions and 18 deletions

View File

@ -198,11 +198,8 @@ pub fn initramfs_load_addr(
} }
} }
pub fn get_host_cpu_phys_bits() -> u8 { pub fn get_host_cpu_phys_bits(hypervisor: &Arc<dyn hypervisor::Hypervisor>) -> u8 {
// A dummy hypervisor created only for querying the host IPA size and will let host_cpu_phys_bits = hypervisor.get_host_ipa_limit().try_into().unwrap();
// be freed after the query.
let hv = hypervisor::new().unwrap();
let host_cpu_phys_bits = hv.get_host_ipa_limit().try_into().unwrap();
if host_cpu_phys_bits == 0 { if host_cpu_phys_bits == 0 {
// Host kernel does not support `get_host_ipa_limit`, // Host kernel does not support `get_host_ipa_limit`,
// we return the default value 40 here. // we return the default value 40 here.

View File

@ -16,7 +16,7 @@ use crate::GuestMemoryMmap;
use crate::InitramfsConfig; use crate::InitramfsConfig;
use crate::RegionType; use crate::RegionType;
use hypervisor::arch::x86::{CpuIdEntry, CPUID_FLAG_VALID_INDEX}; use hypervisor::arch::x86::{CpuIdEntry, CPUID_FLAG_VALID_INDEX};
use hypervisor::{HypervisorCpuError, HypervisorError}; use hypervisor::{CpuVendor, HypervisorCpuError, HypervisorError};
use linux_loader::loader::bootparam::boot_params; use linux_loader::loader::bootparam::boot_params;
use linux_loader::loader::elf::start_info::{ use linux_loader::loader::elf::start_info::{
hvm_memmap_table_entry, hvm_modlist_entry, hvm_start_info, hvm_memmap_table_entry, hvm_modlist_entry, hvm_start_info,
@ -1078,7 +1078,7 @@ pub fn initramfs_load_addr(
Ok(aligned_addr) Ok(aligned_addr)
} }
pub fn get_host_cpu_phys_bits() -> u8 { pub fn get_host_cpu_phys_bits(hypervisor: &Arc<dyn hypervisor::Hypervisor>) -> u8 {
// SAFETY: call cpuid with valid leaves // SAFETY: call cpuid with valid leaves
unsafe { unsafe {
let leaf = x86_64::__cpuid(0x8000_0000); let leaf = x86_64::__cpuid(0x8000_0000);
@ -1087,9 +1087,7 @@ pub fn get_host_cpu_phys_bits() -> u8 {
// Some physical address bits may become reserved when the feature is enabled. // Some physical address bits may become reserved when the feature is enabled.
// See AMD64 Architecture Programmer's Manual Volume 2, Section 7.10.1 // See AMD64 Architecture Programmer's Manual Volume 2, Section 7.10.1
let reduced = if leaf.eax >= 0x8000_001f let reduced = if leaf.eax >= 0x8000_001f
&& leaf.ebx == 0x6874_7541 // Vendor ID: AuthenticAMD && matches!(hypervisor.get_cpu_vendor(), CpuVendor::AMD)
&& leaf.ecx == 0x444d_4163
&& leaf.edx == 0x6974_6e65
&& x86_64::__cpuid(0x8000_001f).eax & 0x1 != 0 && x86_64::__cpuid(0x8000_001f).eax & 0x1 != 0
{ {
(x86_64::__cpuid(0x8000_001f).ebx >> 6) & 0x3f (x86_64::__cpuid(0x8000_001f).ebx >> 6) & 0x3f

View File

@ -716,7 +716,7 @@ impl CpuManager {
); );
self.cpuid = { self.cpuid = {
let phys_bits = physical_bits(self.config.max_phys_bits); let phys_bits = physical_bits(hypervisor, self.config.max_phys_bits);
arch::generate_common_cpuid( arch::generate_common_cpuid(
hypervisor, hypervisor,
topology, topology,

View File

@ -1219,7 +1219,8 @@ impl Vmm {
)) ))
})?; })?;
let phys_bits = vm::physical_bits(config.lock().unwrap().cpus.max_phys_bits); let phys_bits =
vm::physical_bits(&self.hypervisor, config.lock().unwrap().cpus.max_phys_bits);
let memory_manager = MemoryManager::new( let memory_manager = MemoryManager::new(
vm, vm,
@ -1535,7 +1536,8 @@ impl Vmm {
let vm_config = vm.get_config(); let vm_config = vm.get_config();
#[cfg(all(feature = "kvm", target_arch = "x86_64"))] #[cfg(all(feature = "kvm", target_arch = "x86_64"))]
let common_cpuid = { let common_cpuid = {
let phys_bits = vm::physical_bits(vm_config.lock().unwrap().cpus.max_phys_bits); let phys_bits =
vm::physical_bits(&hypervisor, vm_config.lock().unwrap().cpus.max_phys_bits);
arch::generate_common_cpuid( arch::generate_common_cpuid(
&hypervisor, &hypervisor,
None, None,
@ -1725,7 +1727,7 @@ impl Vmm {
let dest_cpuid = &{ let dest_cpuid = &{
let vm_config = &src_vm_config.lock().unwrap(); let vm_config = &src_vm_config.lock().unwrap();
let phys_bits = vm::physical_bits(vm_config.cpus.max_phys_bits); let phys_bits = vm::physical_bits(&self.hypervisor, vm_config.cpus.max_phys_bits);
arch::generate_common_cpuid( arch::generate_common_cpuid(
&self.hypervisor.clone(), &self.hypervisor.clone(),
None, None,

View File

@ -417,8 +417,8 @@ impl VmOps for VmOpsHandler {
} }
} }
pub fn physical_bits(max_phys_bits: u8) -> u8 { pub fn physical_bits(hypervisor: &Arc<dyn hypervisor::Hypervisor>, max_phys_bits: u8) -> u8 {
let host_phys_bits = get_host_cpu_phys_bits(); let host_phys_bits = get_host_cpu_phys_bits(hypervisor);
cmp::min(host_phys_bits, max_phys_bits) cmp::min(host_phys_bits, max_phys_bits)
} }
@ -762,7 +762,7 @@ impl Vm {
tdx_enabled, tdx_enabled,
)?; )?;
let phys_bits = physical_bits(vm_config.lock().unwrap().cpus.max_phys_bits); let phys_bits = physical_bits(&hypervisor, vm_config.lock().unwrap().cpus.max_phys_bits);
let memory_manager = if let Some(snapshot) = let memory_manager = if let Some(snapshot) =
snapshot_from_id(snapshot.as_ref(), MEMORY_MANAGER_SNAPSHOT_ID) snapshot_from_id(snapshot.as_ref(), MEMORY_MANAGER_SNAPSHOT_ID)
@ -2413,7 +2413,10 @@ impl Snapshottable for Vm {
#[cfg(all(feature = "kvm", target_arch = "x86_64"))] #[cfg(all(feature = "kvm", target_arch = "x86_64"))]
let common_cpuid = { let common_cpuid = {
let phys_bits = physical_bits(self.config.lock().unwrap().cpus.max_phys_bits); let phys_bits = physical_bits(
&self.hypervisor,
self.config.lock().unwrap().cpus.max_phys_bits,
);
arch::generate_common_cpuid( arch::generate_common_cpuid(
&self.hypervisor, &self.hypervisor,
None, None,