Skip to content

Commit 9ff7e89

Browse files
Copilotsimongdavies
andcommitted
Remove OutBHandlerCaller trait and OutBHandlerFunction type, rename handle_outb_impl to handle_outb, fix duplicate imports in hyperv_windows.rs
Co-authored-by: simongdavies <[email protected]>
1 parent af0707b commit 9ff7e89

File tree

5 files changed

+30
-30
lines changed

5 files changed

+30
-30
lines changed

src/hyperlight_host/src/hypervisor/handlers.rs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,39 +20,40 @@ use tracing::{Span, instrument};
2020

2121
use crate::{Result, new_error};
2222

23-
/// The trait representing custom logic to handle the case when
24-
/// a Hypervisor's virtual CPU (vCPU) informs Hyperlight the guest
25-
/// has initiated an outb operation.
26-
pub trait OutBHandlerCaller: Sync + Send {
27-
/// Function that gets called when an outb operation has occurred.
28-
fn call(&mut self, port: u16, payload: u32) -> Result<()>;
29-
}
23+
/// Type alias for the function that handles outb operations
24+
type OutBHandlerFunction = Box<dyn FnMut(u16, u32) -> Result<()> + Send>;
3025

31-
pub(crate) type OutBHandlerFunction = Box<dyn FnMut(u16, u32) -> Result<()> + Send>;
32-
33-
/// A `OutBHandler` implementation using a `OutBHandlerFunction`
26+
/// A `OutBHandler` implementation using a function closure
3427
///
3528
/// Note: This handler must live no longer than the `Sandbox` to which it belongs
36-
pub(crate) struct OutBHandler(Arc<Mutex<OutBHandlerFunction>>);
29+
pub(crate) struct OutBHandler {
30+
func: Arc<Mutex<OutBHandlerFunction>>,
31+
}
3732

38-
impl From<OutBHandlerFunction> for OutBHandler {
39-
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
40-
fn from(func: OutBHandlerFunction) -> Self {
41-
Self(Arc::new(Mutex::new(func)))
33+
impl OutBHandler {
34+
pub fn new(func: OutBHandlerFunction) -> Self {
35+
Self {
36+
func: Arc::new(Mutex::new(func)),
37+
}
4238
}
43-
}
4439

45-
impl OutBHandlerCaller for OutBHandler {
4640
#[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
47-
fn call(&mut self, port: u16, payload: u32) -> Result<()> {
41+
pub fn handle_outb(&self, port: u16, payload: u32) -> Result<()> {
4842
let mut func = self
49-
.0
43+
.func
5044
.try_lock()
5145
.map_err(|e| new_error!("Error locking at {}:{}: {}", file!(), line!(), e))?;
5246
func(port, payload)
5347
}
5448
}
5549

50+
impl From<OutBHandlerFunction> for OutBHandler {
51+
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
52+
fn from(func: OutBHandlerFunction) -> Self {
53+
Self::new(func)
54+
}
55+
}
56+
5657
/// The trait representing custom logic to handle the case when
5758
/// a Hypervisor's virtual CPU (vCPU) informs Hyperlight a memory access
5859
/// outside the designated address space has occurred.

src/hyperlight_host/src/hypervisor/hyperv_linux.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ use super::gdb::{
6060
};
6161
#[cfg(gdb)]
6262
use super::handlers::DbgMemAccessHandlerWrapper;
63-
use super::handlers::{MemAccessHandlerWrapper, OutBHandler, OutBHandlerCaller};
63+
use super::handlers::{MemAccessHandlerWrapper, OutBHandler};
6464
#[cfg(feature = "init-paging")]
6565
use super::{
6666
CR0_AM, CR0_ET, CR0_MP, CR0_NE, CR0_PE, CR0_PG, CR0_WP, CR4_OSFXSR, CR4_OSXMMEXCPT, CR4_PAE,
@@ -668,7 +668,7 @@ impl Hypervisor for HypervLinuxDriver {
668668
outb_handle_fn
669669
.try_lock()
670670
.map_err(|e| new_error!("Error locking at {}:{}: {}", file!(), line!(), e))?
671-
.call(port, val)?;
671+
.handle_outb(port, val)?;
672672

673673
// update rip
674674
self.vcpu_fd.set_reg(&[hv_register_assoc {

src/hyperlight_host/src/hypervisor/hyperv_windows.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,10 @@ use {
4141
super::handlers::DbgMemAccessHandlerWrapper,
4242
crate::HyperlightError,
4343
crate::hypervisor::handlers::DbgMemAccessHandlerCaller,
44-
std::sync::Mutex,
4544
};
4645

4746
use super::fpu::{FP_TAG_WORD_DEFAULT, MXCSR_DEFAULT};
48-
use super::handlers::{MemAccessHandlerWrapper, OutBHandler, OutBHandlerCaller};
47+
use super::handlers::{MemAccessHandlerWrapper, OutBHandler};
4948
use super::surrogate_process::SurrogateProcess;
5049
use super::surrogate_process_manager::*;
5150
use super::windows_hypervisor_platform::{VMPartition, VMProcessor};
@@ -683,7 +682,7 @@ impl Hypervisor for HypervWindowsDriver {
683682
outb_handle_fn
684683
.try_lock()
685684
.map_err(|e| new_error!("Error locking at {}:{}: {}", file!(), line!(), e))?
686-
.call(port, val)?;
685+
.handle_outb(port, val)?;
687686

688687
let mut regs = self.processor.get_regs()?;
689688
regs.rip = rip + instruction_length;

src/hyperlight_host/src/hypervisor/kvm.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use super::fpu::{FP_CONTROL_WORD_DEFAULT, FP_TAG_WORD_DEFAULT, MXCSR_DEFAULT};
3232
use super::gdb::{DebugCommChannel, DebugMsg, DebugResponse, GuestDebug, KvmDebug, VcpuStopReason};
3333
#[cfg(gdb)]
3434
use super::handlers::DbgMemAccessHandlerWrapper;
35-
use super::handlers::{MemAccessHandlerWrapper, OutBHandler, OutBHandlerCaller};
35+
use super::handlers::{MemAccessHandlerWrapper, OutBHandler};
3636
#[cfg(feature = "init-paging")]
3737
use super::{
3838
CR0_AM, CR0_ET, CR0_MP, CR0_NE, CR0_PE, CR0_PG, CR0_WP, CR4_OSFXSR, CR4_OSXMMEXCPT, CR4_PAE,
@@ -584,7 +584,7 @@ impl Hypervisor for KVMDriver {
584584
outb_handle_fn
585585
.try_lock()
586586
.map_err(|e| new_error!("Error locking at {}:{}: {}", file!(), line!(), e))?
587-
.call(port, value)?;
587+
.handle_outb(port, value)?;
588588
}
589589

590590
Ok(())

src/hyperlight_host/src/sandbox/outb.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use tracing_log::format_trace;
2626

2727
use super::host_funcs::FunctionRegistry;
2828
use super::mem_mgr::MemMgrWrapper;
29-
use crate::hypervisor::handlers::{OutBHandler, OutBHandlerFunction};
29+
use crate::hypervisor::handlers::OutBHandler;
3030
use crate::mem::mgr::SandboxMemoryManager;
3131
use crate::mem::shared_mem::HostSharedMemory;
3232
use crate::{HyperlightError, Result, new_error};
@@ -146,7 +146,7 @@ fn outb_abort(mem_mgr: &mut MemMgrWrapper<HostSharedMemory>, data: u32) -> Resul
146146

147147
/// Handles OutB operations from the guest.
148148
#[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
149-
fn handle_outb_impl(
149+
fn handle_outb(
150150
mem_mgr: &mut MemMgrWrapper<HostSharedMemory>,
151151
host_funcs: Arc<Mutex<FunctionRegistry>>,
152152
port: u16,
@@ -192,8 +192,8 @@ pub(crate) fn outb_handler_wrapper(
192192
mut mem_mgr_wrapper: MemMgrWrapper<HostSharedMemory>,
193193
host_funcs_wrapper: Arc<Mutex<FunctionRegistry>>,
194194
) -> Arc<Mutex<OutBHandler>> {
195-
let outb_func: OutBHandlerFunction = Box::new(move |port, payload| {
196-
handle_outb_impl(
195+
let outb_func: Box<dyn FnMut(u16, u32) -> Result<()> + Send> = Box::new(move |port, payload| {
196+
handle_outb(
197197
&mut mem_mgr_wrapper,
198198
host_funcs_wrapper.clone(),
199199
port,

0 commit comments

Comments
 (0)