From 77e901a60274fa9fcf03416040f0e5c23ff97c4a Mon Sep 17 00:00:00 2001 From: Muminul Islam Date: Fri, 21 Aug 2020 13:00:37 -0700 Subject: [PATCH] hypervisor: Introduce VM state to Vm hypervisor trait We may need to store hypervisor speciific data to the VM. This support is needed for Microsoft hyperv implementations. This patch introduces two new definitions to Vm trait and implements for KVM. Signed-off-by: Muminul Islam --- hypervisor/src/kvm/mod.rs | 43 ++++++++++++++++++++++++++++++--------- hypervisor/src/lib.rs | 1 + hypervisor/src/vm.rs | 5 +++++ 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/hypervisor/src/kvm/mod.rs b/hypervisor/src/kvm/mod.rs index de0e7d053..0461c19f0 100644 --- a/hypervisor/src/kvm/mod.rs +++ b/hypervisor/src/kvm/mod.rs @@ -8,20 +8,20 @@ // // -use kvm_ioctls::{NoDatamatch, VcpuFd, VmFd}; -use std::os::unix::io::{AsRawFd, RawFd}; -use std::result; -use std::sync::Arc; -#[cfg(target_arch = "x86_64")] -use vm_memory::Address; -use vmm_sys_util::eventfd::EventFd; - #[cfg(target_arch = "aarch64")] pub use crate::aarch64::{check_required_kvm_extensions, VcpuInit, VcpuKvmState as CpuState}; use crate::cpu; use crate::device; use crate::hypervisor; use crate::vm; +use kvm_ioctls::{NoDatamatch, VcpuFd, VmFd}; +use serde_derive::{Deserialize, Serialize}; +use std::os::unix::io::{AsRawFd, RawFd}; +use std::result; +use std::sync::Arc; +#[cfg(target_arch = "x86_64")] +use vm_memory::Address; +use vmm_sys_util::eventfd::EventFd; // x86_64 dependencies #[cfg(target_arch = "x86_64")] pub mod x86_64; @@ -66,12 +66,16 @@ pub use { kvm_bindings::kvm_vcpu_events as VcpuEvents, kvm_ioctls::DeviceFd, kvm_ioctls::IoEventAddress, kvm_ioctls::VcpuExit, }; +#[derive(Clone, Copy, Debug, PartialEq, Deserialize, Serialize)] +pub struct KvmVmState {} +use KvmVmState as VmState; /// Wrapper over KVM VM ioctls. pub struct KvmVm { fd: Arc, #[cfg(target_arch = "x86_64")] msrs: MsrEntries, + state: KvmVmState, } // Returns a `Vec` with a size in bytes at least as large as `size_in_bytes`. @@ -314,6 +318,18 @@ impl vm::Vm for KvmVm { self.create_device(&mut vfio_dev) .map_err(|e| vm::HypervisorVmError::CreatePassthroughDevice(e.into())) } + /// + /// Get the Vm state. Return VM specific data + /// + fn state(&self) -> vm::Result { + Ok(self.state) + } + /// + /// Set the VM state + /// + fn set_state(&self, _state: &VmState) -> vm::Result<()> { + Ok(()) + } } /// Wrapper over KVM system ioctls. pub struct KvmHypervisor { @@ -381,12 +397,19 @@ impl hypervisor::Hypervisor for KvmHypervisor { msr_entries[pos].index = *index; } - Ok(Arc::new(KvmVm { fd: vm_fd, msrs })) + Ok(Arc::new(KvmVm { + fd: vm_fd, + msrs, + state: VmState {}, + })) } #[cfg(any(target_arch = "arm", target_arch = "aarch64"))] { - Ok(Arc::new(KvmVm { fd: vm_fd })) + Ok(Arc::new(KvmVm { + fd: vm_fd, + state: VmState {}, + })) } } diff --git a/hypervisor/src/lib.rs b/hypervisor/src/lib.rs index ad65d2a5c..7de42ed5c 100644 --- a/hypervisor/src/lib.rs +++ b/hypervisor/src/lib.rs @@ -52,6 +52,7 @@ pub use kvm::*; pub use vm::{DataMatch, HypervisorVmError, Vm}; use std::sync::Arc; + pub fn new() -> std::result::Result, HypervisorError> { #[cfg(feature = "kvm")] let hv = kvm::KvmHypervisor::new()?; diff --git a/hypervisor/src/vm.rs b/hypervisor/src/vm.rs index 70a8be3cb..ab5412006 100644 --- a/hypervisor/src/vm.rs +++ b/hypervisor/src/vm.rs @@ -14,6 +14,7 @@ use crate::cpu::Vcpu; use crate::device::Device; #[cfg(target_arch = "x86_64")] use crate::ClockData; +use crate::KvmVmState as VmState; use crate::{CreateDevice, IoEventAddress, IrqRoutingEntry, MemoryRegion}; use kvm_ioctls::Cap; use std::sync::Arc; @@ -179,4 +180,8 @@ pub trait Vm: Send + Sync { fn check_extension(&self, c: Cap) -> bool; /// Create a device that is used for passthrough fn create_passthrough_device(&self) -> Result>; + /// Get the Vm state. Return VM specific data + fn state(&self) -> Result; + /// Set the VM state + fn set_state(&self, state: &VmState) -> Result<()>; }