From b5270e0b451196a18267df251017be8af08a8048 Mon Sep 17 00:00:00 2001 From: Wei Liu Date: Wed, 20 Jul 2022 12:52:06 +0000 Subject: [PATCH] hypervisor: allow downcasting to hypervisor VM types Signed-off-by: Wei Liu --- hypervisor/src/kvm/mod.rs | 4 ++++ hypervisor/src/mshv/mod.rs | 4 ++++ hypervisor/src/vm.rs | 5 ++++- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/hypervisor/src/kvm/mod.rs b/hypervisor/src/kvm/mod.rs index aa04251d5..a386ed010 100644 --- a/hypervisor/src/kvm/mod.rs +++ b/hypervisor/src/kvm/mod.rs @@ -837,6 +837,10 @@ impl vm::Vm for KvmVm { ) .map_err(vm::HypervisorVmError::InitMemRegionTdx) } + /// Downcast to the underlying KvmVm type + fn as_any(&self) -> &dyn Any { + self + } } #[cfg(feature = "tdx")] diff --git a/hypervisor/src/mshv/mod.rs b/hypervisor/src/mshv/mod.rs index 5a45bb213..dc65340c8 100644 --- a/hypervisor/src/mshv/mod.rs +++ b/hypervisor/src/mshv/mod.rs @@ -1212,4 +1212,8 @@ impl vm::Vm for MshvVm { fn set_clock(&self, _data: &ClockData) -> vm::Result<()> { Ok(()) } + /// Downcast to the underlying MshvVm type + fn as_any(&self) -> &dyn Any { + self + } } diff --git a/hypervisor/src/vm.rs b/hypervisor/src/vm.rs index ae2954a44..8e364d638 100644 --- a/hypervisor/src/vm.rs +++ b/hypervisor/src/vm.rs @@ -23,6 +23,7 @@ use crate::UserMemoryRegion; use crate::{IoEventAddress, IrqRoutingEntry}; #[cfg(feature = "kvm")] use kvm_ioctls::Cap; +use std::any::Any; #[cfg(target_arch = "x86_64")] use std::fs::File; use std::sync::Arc; @@ -260,7 +261,7 @@ pub enum InterruptSourceConfig { /// /// This crate provides a hypervisor-agnostic interfaces for Vm /// -pub trait Vm: Send + Sync { +pub trait Vm: Send + Sync + Any { #[cfg(target_arch = "x86_64")] /// Sets the address of the one-page region in the VM's address space. fn set_identity_map_address(&self, address: u64) -> Result<()>; @@ -355,6 +356,8 @@ pub trait Vm: Send + Sync { size: u64, measure: bool, ) -> Result<()>; + /// Downcast to the underlying hypervisor VM type + fn as_any(&self) -> &dyn Any; } pub trait VmOps: Send + Sync {