From 2fe3586ebae2c3d78c5d7b6302f4619e63cb8c2f Mon Sep 17 00:00:00 2001 From: Henry Wang Date: Wed, 9 Jun 2021 17:42:37 +0800 Subject: [PATCH] hypervisor: support AArch64 `get_host_ipa_limit` Not all AArch64 platforms support IPAs up to 40 bits. Since the kvm-ioctl crate now supports `get_host_ipa_limit` for AArch64, when creating the VM, it is better to get the IPA size from the host and use that to create the VM. Signed-off-by: Henry Wang --- hypervisor/src/kvm/mod.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) 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.