diff --git a/hypervisor/src/kvm/mod.rs b/hypervisor/src/kvm/mod.rs index effa63e8c..112b49b18 100644 --- a/hypervisor/src/kvm/mod.rs +++ b/hypervisor/src/kvm/mod.rs @@ -22,6 +22,8 @@ use crate::vm::{self, VmmOps}; use crate::{arm64_core_reg_id, offset__of}; use kvm_ioctls::{NoDatamatch, VcpuFd, VmFd}; use serde_derive::{Deserialize, Serialize}; +#[cfg(target_arch = "aarch64")] +use std::convert::TryInto; use std::os::unix::io::{AsRawFd, RawFd}; use std::result; #[cfg(target_arch = "x86_64")] @@ -558,7 +560,18 @@ impl hypervisor::Hypervisor for KvmHypervisor { /// let vm = hypervisor.create_vm().unwrap() /// fn create_vm(&self) -> hypervisor::Result> { - self.create_vm_with_type(0) // Create with default platform type + #[allow(unused_mut)] + let mut vm_type: u64 = 0; // Create with default platform type + + // When KVM supports Cap::ArmVmIPASize, it is better to get the IPA + // size from the host and use that when creating the VM, which may + // avoid unnecessary VM creation failures. + #[cfg(target_arch = "aarch64")] + if self.kvm.check_extension(Cap::ArmVmIPASize) { + vm_type = self.kvm.get_host_ipa_limit().try_into().unwrap(); + } + + self.create_vm_with_type(vm_type) } fn check_required_extensions(&self) -> hypervisor::Result<()> { @@ -575,6 +588,7 @@ impl hypervisor::Hypervisor for KvmHypervisor { .get_supported_cpuid(kvm_bindings::KVM_MAX_CPUID_ENTRIES) .map_err(|e| hypervisor::HypervisorError::GetCpuId(e.into())) } + #[cfg(target_arch = "x86_64")] /// /// Retrieve the list of MSRs supported by KVM.