hypervisor: drop a level of indirection for MSHV's DeviceFd

Signed-off-by: Wei Liu <liuwe@microsoft.com>
This commit is contained in:
Wei Liu 2022-07-09 16:01:14 +00:00 committed by Rob Bradford
parent 3710932248
commit 47b5581c50

View File

@ -35,7 +35,7 @@ pub use x86_64::*;
#[cfg(target_arch = "x86_64")] #[cfg(target_arch = "x86_64")]
use std::fs::File; use std::fs::File;
use std::os::unix::io::{AsRawFd, RawFd}; use std::os::unix::io::AsRawFd;
const DIRTY_BITMAP_CLEAR_DIRTY: u64 = 0x4; const DIRTY_BITMAP_CLEAR_DIRTY: u64 = 0x4;
const DIRTY_BITMAP_SET_DIRTY: u64 = 0x8; const DIRTY_BITMAP_SET_DIRTY: u64 = 0x8;
@ -667,25 +667,21 @@ impl cpu::Vcpu for MshvVcpu {
} }
/// Device struct for MSHV /// Device struct for MSHV
pub struct MshvDevice { pub type MshvDevice = DeviceFd;
fd: DeviceFd,
}
impl device::Device for MshvDevice { impl device::Device for MshvDevice {
/// ///
/// Set device attribute /// Set device attribute
/// ///
fn set_device_attr(&self, attr: &DeviceAttr) -> device::Result<()> { fn set_device_attr(&self, attr: &DeviceAttr) -> device::Result<()> {
self.fd self.set_device_attr(attr)
.set_device_attr(attr)
.map_err(|e| device::HypervisorDeviceError::SetDeviceAttribute(e.into())) .map_err(|e| device::HypervisorDeviceError::SetDeviceAttribute(e.into()))
} }
/// ///
/// Get device attribute /// Get device attribute
/// ///
fn get_device_attr(&self, attr: &mut DeviceAttr) -> device::Result<()> { fn get_device_attr(&self, attr: &mut DeviceAttr) -> device::Result<()> {
self.fd self.get_device_attr(attr)
.get_device_attr(attr)
.map_err(|e| device::HypervisorDeviceError::GetDeviceAttribute(e.into())) .map_err(|e| device::HypervisorDeviceError::GetDeviceAttribute(e.into()))
} }
/// ///
@ -696,12 +692,6 @@ impl device::Device for MshvDevice {
} }
} }
impl AsRawFd for MshvDevice {
fn as_raw_fd(&self) -> RawFd {
self.fd.as_raw_fd()
}
}
struct MshvEmulatorContext<'a> { struct MshvEmulatorContext<'a> {
vcpu: &'a MshvVcpu, vcpu: &'a MshvVcpu,
map: (u64, u64), // Initial GVA to GPA mapping provided by the hypervisor map: (u64, u64), // Initial GVA to GPA mapping provided by the hypervisor
@ -1029,12 +1019,11 @@ impl vm::Vm for MshvVm {
/// ///
/// See the documentation for `MSHV_CREATE_DEVICE`. /// See the documentation for `MSHV_CREATE_DEVICE`.
fn create_device(&self, device: &mut CreateDevice) -> vm::Result<Arc<dyn device::Device>> { fn create_device(&self, device: &mut CreateDevice) -> vm::Result<Arc<dyn device::Device>> {
let fd = self let device_fd = self
.fd .fd
.create_device(device) .create_device(device)
.map_err(|e| vm::HypervisorVmError::CreateDevice(e.into()))?; .map_err(|e| vm::HypervisorVmError::CreateDevice(e.into()))?;
let device = MshvDevice { fd }; Ok(Arc::new(device_fd))
Ok(Arc::new(device))
} }
fn create_passthrough_device(&self) -> vm::Result<Arc<dyn device::Device>> { fn create_passthrough_device(&self) -> vm::Result<Arc<dyn device::Device>> {