use std::any::Any;
use crate::{
config::VmConfig,
error::HypervisorError,
memory::GuestAddress,
types::{
DeviceSnapshot, DirtyPageInfo, PlatformCapabilities, Registers, VcpuExit, VcpuSnapshot,
VirtioDeviceConfig,
},
};
pub trait Hypervisor: Send + Sync + 'static {
type Vm: VirtualMachine;
fn capabilities(&self) -> &PlatformCapabilities;
fn create_vm(&self, config: VmConfig) -> Result<Self::Vm, HypervisorError>;
}
pub trait VirtualMachine: Send + Sync {
type Vcpu: Vcpu;
type Memory: GuestMemory;
fn is_managed_execution(&self) -> bool {
false }
fn memory(&self) -> &Self::Memory;
fn create_vcpu(&mut self, id: u32) -> Result<Self::Vcpu, HypervisorError>;
fn add_virtio_device(&mut self, device: VirtioDeviceConfig) -> Result<(), HypervisorError>;
fn start(&mut self) -> Result<(), HypervisorError>;
fn pause(&mut self) -> Result<(), HypervisorError>;
fn resume(&mut self) -> Result<(), HypervisorError>;
fn stop(&mut self) -> Result<(), HypervisorError>;
fn as_any(&self) -> &dyn Any;
fn as_any_mut(&mut self) -> &mut dyn Any;
fn vcpu_count(&self) -> u32;
fn snapshot_devices(&self) -> Result<Vec<DeviceSnapshot>, HypervisorError>;
fn restore_devices(&mut self, snapshots: &[DeviceSnapshot]) -> Result<(), HypervisorError>;
}
pub trait Vcpu: Send {
fn run(&mut self) -> Result<VcpuExit, HypervisorError>;
fn get_regs(&self) -> Result<Registers, HypervisorError>;
fn set_regs(&mut self, regs: &Registers) -> Result<(), HypervisorError>;
fn id(&self) -> u32;
fn set_io_result(&mut self, value: u64) -> Result<(), HypervisorError>;
fn set_mmio_result(&mut self, value: u64) -> Result<(), HypervisorError>;
fn snapshot(&self) -> Result<VcpuSnapshot, HypervisorError>;
fn restore(&mut self, snapshot: &VcpuSnapshot) -> Result<(), HypervisorError>;
}
pub trait GuestMemory: Send + Sync {
fn read(&self, addr: GuestAddress, buf: &mut [u8]) -> Result<(), HypervisorError>;
fn write(&self, addr: GuestAddress, buf: &[u8]) -> Result<(), HypervisorError>;
fn get_host_address(&self, addr: GuestAddress) -> Result<*mut u8, HypervisorError>;
fn size(&self) -> u64;
fn enable_dirty_tracking(&mut self) -> Result<(), HypervisorError>;
fn disable_dirty_tracking(&mut self) -> Result<(), HypervisorError>;
fn get_dirty_pages(&mut self) -> Result<Vec<DirtyPageInfo>, HypervisorError>;
fn dump_all(&self, buf: &mut [u8]) -> Result<(), HypervisorError>;
}