use crate::{
error::Result,
memory::{GuestMemoryRegion, GuestRange, Protection},
vcpu::Vcpu,
};
pub const MAX_SUPPORTED_VCPUS: u32 = 32;
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
#[non_exhaustive]
pub enum BackendKind {
Hvf,
Mock,
}
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
#[non_exhaustive]
#[allow(clippy::struct_excessive_bools)] pub struct BackendCapabilities {
pub kind: BackendKind,
pub max_vcpus: u32,
pub dirty_page_tracking: bool,
pub postcopy_restore: bool,
pub exposes_vcpu_exits: bool,
pub custom_mmio_devices: bool,
}
pub trait Vm: Send + Sync {
fn map_memory(&self, region: &GuestMemoryRegion) -> Result<()>;
fn unmap_memory(&self, region: &GuestMemoryRegion) -> Result<()>;
fn protect_memory(&self, range: GuestRange, prot: Protection) -> Result<()>;
fn create_vcpu(&self, index: u32) -> Result<Box<dyn Vcpu>>;
}
pub trait HypervisorBackend: Send + Sync {
type Vm: Vm;
fn capabilities(&self) -> BackendCapabilities;
fn create_vm(&self, vcpu_count: u32, mem_size_mib: u64) -> Result<Self::Vm>;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn capabilities_struct_constructs() {
let caps = BackendCapabilities {
kind: BackendKind::Mock,
max_vcpus: 4,
dirty_page_tracking: false,
postcopy_restore: false,
exposes_vcpu_exits: true,
custom_mmio_devices: true,
};
assert_eq!(caps.max_vcpus, 4);
}
}