@@ -20,39 +20,40 @@ use tracing::{Span, instrument};
2020
2121use 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.
0 commit comments