From f9f0a60dcf89f431bdc72f62e6d0f157aac05ec4 Mon Sep 17 00:00:00 2001 From: Wei Liu Date: Thu, 14 Jul 2022 13:06:26 +0000 Subject: [PATCH] hypervisor: provide a generic IoEventAddress structure Signed-off-by: Wei Liu --- hypervisor/src/kvm/mod.rs | 27 +++++++++++++++++++++++---- hypervisor/src/lib.rs | 12 ++++++++---- hypervisor/src/mshv/mod.rs | 23 +++++++++++++++++++++-- 3 files changed, 52 insertions(+), 10 deletions(-) diff --git a/hypervisor/src/kvm/mod.rs b/hypervisor/src/kvm/mod.rs index f4364e4ca..3923af3f8 100644 --- a/hypervisor/src/kvm/mod.rs +++ b/hypervisor/src/kvm/mod.rs @@ -49,8 +49,8 @@ pub mod x86_64; #[cfg(target_arch = "x86_64")] use crate::arch::x86::NUM_IOAPIC_PINS; use crate::{ - MpState, UserMemoryRegion, USER_MEMORY_REGION_LOG_DIRTY, USER_MEMORY_REGION_READ, - USER_MEMORY_REGION_WRITE, + IoEventAddress, MpState, UserMemoryRegion, USER_MEMORY_REGION_LOG_DIRTY, + USER_MEMORY_REGION_READ, USER_MEMORY_REGION_WRITE, }; #[cfg(target_arch = "aarch64")] use aarch64::{RegList, Register, StandardRegisters}; @@ -96,8 +96,7 @@ pub use { kvm_bindings::kvm_clock_data as ClockData, kvm_bindings::kvm_create_device as CreateDevice, kvm_bindings::kvm_device_attr as DeviceAttr, kvm_bindings::kvm_irq_routing_entry as IrqRoutingEntry, kvm_bindings::kvm_run, - kvm_bindings::kvm_vcpu_events as VcpuEvents, kvm_ioctls::DeviceFd, kvm_ioctls::IoEventAddress, - kvm_ioctls::VcpuExit, + kvm_bindings::kvm_vcpu_events as VcpuEvents, kvm_ioctls::DeviceFd, kvm_ioctls::VcpuExit, }; #[cfg(target_arch = "x86_64")] @@ -229,6 +228,24 @@ impl From for kvm_mp_state { } } +impl From for IoEventAddress { + fn from(a: kvm_ioctls::IoEventAddress) -> Self { + match a { + kvm_ioctls::IoEventAddress::Pio(x) => Self::Pio(x), + kvm_ioctls::IoEventAddress::Mmio(x) => Self::Mmio(x), + } + } +} + +impl From for kvm_ioctls::IoEventAddress { + fn from(a: IoEventAddress) -> Self { + match a { + IoEventAddress::Pio(x) => Self::Pio(x), + IoEventAddress::Mmio(x) => Self::Mmio(x), + } + } +} + #[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize, Serialize)] pub struct KvmVmState {} @@ -359,6 +376,7 @@ impl vm::Vm for KvmVm { addr: &IoEventAddress, datamatch: Option, ) -> vm::Result<()> { + let addr = &kvm_ioctls::IoEventAddress::from(*addr); if let Some(dm) = datamatch { match dm { vm::DataMatch::DataMatch32(kvm_dm32) => self @@ -380,6 +398,7 @@ impl vm::Vm for KvmVm { /// Unregisters an event from a certain address it has been previously registered to. /// fn unregister_ioevent(&self, fd: &EventFd, addr: &IoEventAddress) -> vm::Result<()> { + let addr = &kvm_ioctls::IoEventAddress::from(*addr); self.fd .unregister_ioevent(fd, addr, NoDatamatch) .map_err(|e| vm::HypervisorVmError::UnregisterIoEvent(e.into())) diff --git a/hypervisor/src/lib.rs b/hypervisor/src/lib.rs index 534abcc87..76c6b13e3 100644 --- a/hypervisor/src/lib.rs +++ b/hypervisor/src/lib.rs @@ -60,16 +60,14 @@ pub use kvm::{aarch64, GicState}; // Aliased types exposed from both hypervisors #[cfg(feature = "kvm")] pub use kvm::{ - ClockData, CpuState, CreateDevice, DeviceAttr, DeviceFd, IoEventAddress, IrqRoutingEntry, - VcpuEvents, VmState, + ClockData, CpuState, CreateDevice, DeviceAttr, DeviceFd, IrqRoutingEntry, VcpuEvents, VmState, }; #[cfg(all(feature = "mshv", target_arch = "x86_64"))] pub use mshv::x86_64; // Aliased types exposed from both hypervisors #[cfg(all(feature = "mshv", target_arch = "x86_64"))] pub use mshv::{ - CpuState, CreateDevice, DeviceAttr, DeviceFd, IoEventAddress, IrqRoutingEntry, VcpuEvents, - VmState, + CpuState, CreateDevice, DeviceAttr, DeviceFd, IrqRoutingEntry, VcpuEvents, VmState, }; use std::sync::Arc; pub use vm::{ @@ -144,3 +142,9 @@ pub enum MpState { #[cfg(all(feature = "mshv", target_arch = "x86_64"))] Mshv, /* MSHV does not supprt MpState yet */ } + +#[derive(Debug, Clone, Copy)] +pub enum IoEventAddress { + Pio(u64), + Mmio(u64), +} diff --git a/hypervisor/src/mshv/mod.rs b/hypervisor/src/mshv/mod.rs index 9e403a8e1..8571a9813 100644 --- a/hypervisor/src/mshv/mod.rs +++ b/hypervisor/src/mshv/mod.rs @@ -13,7 +13,6 @@ use crate::hypervisor; use crate::vec_with_array_field; use crate::vm::{self, InterruptSourceConfig, VmOps}; pub use mshv_bindings::*; -pub use mshv_ioctls::IoEventAddress; use mshv_ioctls::{set_registers_64, Mshv, NoDatamatch, VcpuFd, VmFd}; use serde::{Deserialize, Serialize}; use std::any::Any; @@ -25,7 +24,7 @@ use vm::DataMatch; pub mod x86_64; use crate::device; use crate::{ - MpState, UserMemoryRegion, USER_MEMORY_REGION_EXECUTE, USER_MEMORY_REGION_READ, + IoEventAddress, MpState, UserMemoryRegion, USER_MEMORY_REGION_EXECUTE, USER_MEMORY_REGION_READ, USER_MEMORY_REGION_WRITE, }; use vmm_sys_util::eventfd::EventFd; @@ -98,6 +97,24 @@ impl From for mshv_user_mem_region { } } +impl From for IoEventAddress { + fn from(a: mshv_ioctls::IoEventAddress) -> Self { + match a { + mshv_ioctls::IoEventAddress::Pio(x) => Self::Pio(x), + mshv_ioctls::IoEventAddress::Mmio(x) => Self::Mmio(x), + } + } +} + +impl From for mshv_ioctls::IoEventAddress { + fn from(a: IoEventAddress) -> Self { + match a { + IoEventAddress::Pio(x) => Self::Pio(x), + IoEventAddress::Mmio(x) => Self::Mmio(x), + } + } +} + #[derive(Debug, Default, Copy, Clone, Serialize, Deserialize)] pub struct HvState { hypercall_page: u64, @@ -937,6 +954,7 @@ impl vm::Vm for MshvVm { addr: &IoEventAddress, datamatch: Option, ) -> vm::Result<()> { + let addr = &mshv_ioctls::IoEventAddress::from(*addr); debug!( "register_ioevent fd {} addr {:x?} datamatch {:?}", fd.as_raw_fd(), @@ -962,6 +980,7 @@ impl vm::Vm for MshvVm { } /// Unregister an event from a certain address it has been previously registered to. fn unregister_ioevent(&self, fd: &EventFd, addr: &IoEventAddress) -> vm::Result<()> { + let addr = &mshv_ioctls::IoEventAddress::from(*addr); debug!("unregister_ioevent fd {} addr {:x?}", fd.as_raw_fd(), addr); self.fd