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 <muislam@microsoft.com>
This commit is contained in:
Muminul Islam 2020-08-21 13:00:37 -07:00 committed by Sebastien Boeuf
parent 8962e25268
commit 77e901a602
3 changed files with 39 additions and 10 deletions

View File

@ -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<VmFd>,
#[cfg(target_arch = "x86_64")]
msrs: MsrEntries,
state: KvmVmState,
}
// Returns a `Vec<T>` 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<VmState> {
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 {},
}))
}
}

View File

@ -52,6 +52,7 @@ pub use kvm::*;
pub use vm::{DataMatch, HypervisorVmError, Vm};
use std::sync::Arc;
pub fn new() -> std::result::Result<Arc<dyn Hypervisor>, HypervisorError> {
#[cfg(feature = "kvm")]
let hv = kvm::KvmHypervisor::new()?;

View File

@ -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<Arc<dyn Device>>;
/// Get the Vm state. Return VM specific data
fn state(&self) -> Result<VmState>;
/// Set the VM state
fn set_state(&self, state: &VmState) -> Result<()>;
}