From 1c54fc3ab79bc0a25c6920f310169b1dc511c387 Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Tue, 23 Feb 2021 14:19:32 +0000 Subject: [PATCH] hypervisor: Support creating a VM of a specified KVM type This is necessary to support creating a TD VM. Signed-off-by: Rob Bradford --- hypervisor/src/hypervisor.rs | 7 ++++++- hypervisor/src/kvm/mod.rs | 20 ++++++++++++++++---- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/hypervisor/src/hypervisor.rs b/hypervisor/src/hypervisor.rs index aac0a7f3d..7f1cc8a21 100644 --- a/hypervisor/src/hypervisor.rs +++ b/hypervisor/src/hypervisor.rs @@ -15,7 +15,6 @@ use crate::x86_64::MsrList; #[cfg(all(feature = "kvm", target_arch = "x86_64"))] use kvm_ioctls::Cap; use std::sync::Arc; - use thiserror::Error; #[derive(Error, Debug)] @@ -87,6 +86,12 @@ pub trait Hypervisor: Send + Sync { fn create_vm(&self) -> Result>; #[cfg(feature = "kvm")] /// + /// Create a Vm of a specific type using the underlying hypervisor + /// Return a hypervisor-agnostic Vm trait object + /// + fn create_vm_with_type(&self, vm_type: u64) -> Result>; + #[cfg(feature = "kvm")] + /// /// Returns the size of the memory mapping required to use the vcpu's structures /// fn get_vcpu_mmap_size(&self) -> Result; diff --git a/hypervisor/src/kvm/mod.rs b/hypervisor/src/kvm/mod.rs index 1750d0e29..42c934b03 100644 --- a/hypervisor/src/kvm/mod.rs +++ b/hypervisor/src/kvm/mod.rs @@ -521,18 +521,18 @@ impl KvmHypervisor { /// let vm = hypervisor.create_vm().expect("new VM fd creation failed"); /// impl hypervisor::Hypervisor for KvmHypervisor { - /// Create a KVM vm object and return the object as Vm trait object + /// Create a KVM vm object of a specific VM type and return the object as Vm trait object /// Example /// # extern crate hypervisor; /// # use hypervisor::KvmHypervisor; /// use hypervisor::KvmVm; /// let hypervisor = KvmHypervisor::new().unwrap(); - /// let vm = hypervisor.create_vm().unwrap() + /// let vm = hypervisor.create_vm_with_type(KvmVmType::LegacyVm).unwrap() /// - fn create_vm(&self) -> hypervisor::Result> { + fn create_vm_with_type(&self, vm_type: u64) -> hypervisor::Result> { let fd: VmFd; loop { - match self.kvm.create_vm() { + match self.kvm.create_vm_with_type(vm_type) { Ok(res) => fd = res, Err(e) => { if e.errno() == libc::EINTR { @@ -577,6 +577,18 @@ impl hypervisor::Hypervisor for KvmHypervisor { } } + /// Create a KVM vm object and return the object as Vm trait object + /// Example + /// # extern crate hypervisor; + /// # use hypervisor::KvmHypervisor; + /// use hypervisor::KvmVm; + /// let hypervisor = KvmHypervisor::new().unwrap(); + /// let vm = hypervisor.create_vm().unwrap() + /// + fn create_vm(&self) -> hypervisor::Result> { + self.create_vm_with_type(0) // Create with default platform type + } + fn check_required_extensions(&self) -> hypervisor::Result<()> { check_required_kvm_extensions(&self.kvm).expect("Missing KVM capabilities"); Ok(())