@@ -26,11 +26,43 @@ use tracing_log::format_trace;
2626
2727use super :: host_funcs:: FunctionRegistry ;
2828use super :: mem_mgr:: MemMgrWrapper ;
29- use crate :: hypervisor:: handlers:: { OutBHandler , OutBHandlerFunction } ;
3029use crate :: mem:: mgr:: SandboxMemoryManager ;
3130use crate :: mem:: shared_mem:: HostSharedMemory ;
3231use crate :: { new_error, HyperlightError , Result } ;
3332
33+ /// A `OutBHandler` implementation that contains the required data directly
34+ ///
35+ /// Note: This handler must live no longer than the `Sandbox` to which it belongs
36+ pub ( crate ) struct OutBHandler {
37+ mem_mgr_wrapper : MemMgrWrapper < HostSharedMemory > ,
38+ host_funcs_wrapper : Arc < Mutex < FunctionRegistry > > ,
39+ }
40+
41+ impl OutBHandler {
42+ /// Create a new OutBHandler with the required dependencies
43+ #[ instrument( skip_all, parent = Span :: current( ) , level= "Trace" ) ]
44+ pub fn new (
45+ mem_mgr_wrapper : MemMgrWrapper < HostSharedMemory > ,
46+ host_funcs_wrapper : Arc < Mutex < FunctionRegistry > > ,
47+ ) -> Self {
48+ Self {
49+ mem_mgr_wrapper,
50+ host_funcs_wrapper,
51+ }
52+ }
53+
54+ /// Function that gets called when an outb operation has occurred.
55+ #[ instrument( err( Debug ) , skip_all, parent = Span :: current( ) , level= "Trace" ) ]
56+ pub fn call ( & mut self , port : u16 , payload : u32 ) -> Result < ( ) > {
57+ handle_outb_impl (
58+ & mut self . mem_mgr_wrapper ,
59+ self . host_funcs_wrapper . clone ( ) ,
60+ port,
61+ payload,
62+ )
63+ }
64+ }
65+
3466#[ instrument( err( Debug ) , skip_all, parent = Span :: current( ) , level="Trace" ) ]
3567pub ( super ) fn outb_log ( mgr : & mut SandboxMemoryManager < HostSharedMemory > ) -> Result < ( ) > {
3668 // This code will create either a logging record or a tracing record for the GuestLogData depending on if the host has set up a tracing subscriber.
@@ -146,7 +178,7 @@ fn outb_abort(mem_mgr: &mut MemMgrWrapper<HostSharedMemory>, data: u32) -> Resul
146178
147179/// Handles OutB operations from the guest.
148180#[ instrument( err( Debug ) , skip_all, parent = Span :: current( ) , level= "Trace" ) ]
149- fn handle_outb_impl (
181+ pub ( crate ) fn handle_outb_impl (
150182 mem_mgr : & mut MemMgrWrapper < HostSharedMemory > ,
151183 host_funcs : Arc < Mutex < FunctionRegistry > > ,
152184 port : u16 ,
@@ -189,18 +221,10 @@ fn handle_outb_impl(
189221/// TODO: pass at least the `host_funcs_wrapper` param by reference.
190222#[ instrument( skip_all, parent = Span :: current( ) , level= "Trace" ) ]
191223pub ( crate ) fn outb_handler_wrapper (
192- mut mem_mgr_wrapper : MemMgrWrapper < HostSharedMemory > ,
224+ mem_mgr_wrapper : MemMgrWrapper < HostSharedMemory > ,
193225 host_funcs_wrapper : Arc < Mutex < FunctionRegistry > > ,
194226) -> Arc < Mutex < OutBHandler > > {
195- let outb_func: OutBHandlerFunction = Box :: new ( move |port, payload| {
196- handle_outb_impl (
197- & mut mem_mgr_wrapper,
198- host_funcs_wrapper. clone ( ) ,
199- port,
200- payload,
201- )
202- } ) ;
203- let outb_hdl = OutBHandler :: from ( outb_func) ;
227+ let outb_hdl = OutBHandler :: new ( mem_mgr_wrapper, host_funcs_wrapper) ;
204228 Arc :: new ( Mutex :: new ( outb_hdl) )
205229}
206230
0 commit comments