diff --git a/src/hyperlight_host/src/hypervisor/handlers.rs b/src/hyperlight_host/src/hypervisor/handlers.rs index 9dbb948af..ac8106869 100644 --- a/src/hyperlight_host/src/hypervisor/handlers.rs +++ b/src/hyperlight_host/src/hypervisor/handlers.rs @@ -28,14 +28,6 @@ pub trait OutBHandlerCaller: Sync + Send { fn call(&mut self, port: u16, payload: u32) -> Result<()>; } -/// A convenient type representing a common way `OutBHandler` implementations -/// are passed as parameters to functions -/// -/// Note: This needs to be wrapped in a Mutex to be able to grab a mutable -/// reference to the underlying data (i.e., handle_outb in `Sandbox` takes -/// a &mut self). -pub type OutBHandlerWrapper = Arc>; - pub(crate) type OutBHandlerFunction = Box Result<()> + Send>; /// A `OutBHandler` implementation using a `OutBHandlerFunction` diff --git a/src/hyperlight_host/src/hypervisor/hyperv_linux.rs b/src/hyperlight_host/src/hypervisor/hyperv_linux.rs index 85dc514b5..506753f49 100644 --- a/src/hyperlight_host/src/hypervisor/hyperv_linux.rs +++ b/src/hyperlight_host/src/hypervisor/hyperv_linux.rs @@ -25,6 +25,7 @@ extern crate mshv_bindings3 as mshv_bindings; extern crate mshv_ioctls3 as mshv_ioctls; use std::fmt::{Debug, Formatter}; +use std::sync::{Arc, Mutex}; use log::{error, LevelFilter}; #[cfg(mshv2)] @@ -54,7 +55,7 @@ use super::fpu::{FP_CONTROL_WORD_DEFAULT, FP_TAG_WORD_DEFAULT, MXCSR_DEFAULT}; use super::gdb::{DebugCommChannel, DebugMsg, DebugResponse, GuestDebug, MshvDebug}; #[cfg(gdb)] use super::handlers::DbgMemAccessHandlerWrapper; -use super::handlers::{MemAccessHandlerWrapper, OutBHandlerWrapper}; +use super::handlers::{MemAccessHandlerWrapper, OutBHandler, OutBHandlerCaller}; use super::{ Hypervisor, VirtualCPU, CR0_AM, CR0_ET, CR0_MP, CR0_NE, CR0_PE, CR0_PG, CR0_WP, CR4_OSFXSR, CR4_OSXMMEXCPT, CR4_PAE, EFER_LMA, EFER_LME, EFER_NX, EFER_SCE, @@ -459,7 +460,7 @@ impl Hypervisor for HypervLinuxDriver { peb_addr: RawPtr, seed: u64, page_size: u32, - outb_hdl: OutBHandlerWrapper, + outb_hdl: Arc>, mem_access_hdl: MemAccessHandlerWrapper, hv_handler: Option, max_guest_log_level: Option, @@ -501,7 +502,7 @@ impl Hypervisor for HypervLinuxDriver { fn dispatch_call_from_host( &mut self, dispatch_func_addr: RawPtr, - outb_handle_fn: OutBHandlerWrapper, + outb_handle_fn: Arc>, mem_access_fn: MemAccessHandlerWrapper, hv_handler: Option, #[cfg(gdb)] dbg_mem_access_fn: DbgMemAccessHandlerWrapper, @@ -544,7 +545,7 @@ impl Hypervisor for HypervLinuxDriver { data: Vec, rip: u64, instruction_length: u64, - outb_handle_fn: OutBHandlerWrapper, + outb_handle_fn: Arc>, ) -> Result<()> { let mut padded = [0u8; 4]; let copy_len = data.len().min(4); diff --git a/src/hyperlight_host/src/hypervisor/hyperv_windows.rs b/src/hyperlight_host/src/hypervisor/hyperv_windows.rs index abc2b77cf..799ef115c 100644 --- a/src/hyperlight_host/src/hypervisor/hyperv_windows.rs +++ b/src/hyperlight_host/src/hypervisor/hyperv_windows.rs @@ -18,6 +18,7 @@ use core::ffi::c_void; use std::fmt; use std::fmt::{Debug, Formatter}; use std::string::String; +use std::sync::{Arc, Mutex}; use hyperlight_common::mem::PAGE_SIZE_USIZE; use log::LevelFilter; @@ -31,7 +32,7 @@ use windows::Win32::System::Hypervisor::{ use super::fpu::{FP_TAG_WORD_DEFAULT, MXCSR_DEFAULT}; #[cfg(gdb)] use super::handlers::DbgMemAccessHandlerWrapper; -use super::handlers::{MemAccessHandlerWrapper, OutBHandlerWrapper}; +use super::handlers::{MemAccessHandlerWrapper, OutBHandler, OutBHandlerCaller}; use super::surrogate_process::SurrogateProcess; use super::surrogate_process_manager::*; use super::windows_hypervisor_platform::{VMPartition, VMProcessor}; @@ -305,7 +306,7 @@ impl Hypervisor for HypervWindowsDriver { peb_address: RawPtr, seed: u64, page_size: u32, - outb_hdl: OutBHandlerWrapper, + outb_hdl: Arc>, mem_access_hdl: MemAccessHandlerWrapper, hv_handler: Option, max_guest_log_level: Option, @@ -347,7 +348,7 @@ impl Hypervisor for HypervWindowsDriver { fn dispatch_call_from_host( &mut self, dispatch_func_addr: RawPtr, - outb_hdl: OutBHandlerWrapper, + outb_hdl: Arc>, mem_access_hdl: MemAccessHandlerWrapper, hv_handler: Option, #[cfg(gdb)] dbg_mem_access_hdl: DbgMemAccessHandlerWrapper, @@ -388,7 +389,7 @@ impl Hypervisor for HypervWindowsDriver { data: Vec, rip: u64, instruction_length: u64, - outb_handle_fn: OutBHandlerWrapper, + outb_handle_fn: Arc>, ) -> Result<()> { let mut padded = [0u8; 4]; let copy_len = data.len().min(4); diff --git a/src/hyperlight_host/src/hypervisor/hypervisor_handler.rs b/src/hyperlight_host/src/hypervisor/hypervisor_handler.rs index 8e351708c..7f318e46d 100644 --- a/src/hyperlight_host/src/hypervisor/hypervisor_handler.rs +++ b/src/hyperlight_host/src/hypervisor/hypervisor_handler.rs @@ -39,7 +39,7 @@ use windows::Win32::System::Hypervisor::{WHvCancelRunVirtualProcessor, WHV_PARTI use super::gdb::create_gdb_thread; #[cfg(gdb)] use crate::hypervisor::handlers::DbgMemAccessHandlerWrapper; -use crate::hypervisor::handlers::{MemAccessHandlerWrapper, OutBHandlerWrapper}; +use crate::hypervisor::handlers::{MemAccessHandlerWrapper, OutBHandler}; #[cfg(target_os = "windows")] use crate::hypervisor::wrappers::HandleWrapper; use crate::hypervisor::Hypervisor; @@ -184,7 +184,7 @@ pub(crate) struct HvHandlerConfig { pub(crate) dispatch_function_addr: Arc>>, pub(crate) max_init_time: Duration, pub(crate) max_exec_time: Duration, - pub(crate) outb_handler: OutBHandlerWrapper, + pub(crate) outb_handler: Arc>, pub(crate) mem_access_handler: MemAccessHandlerWrapper, pub(crate) max_wait_for_cancellation: Duration, pub(crate) max_guest_log_level: Option, diff --git a/src/hyperlight_host/src/hypervisor/kvm.rs b/src/hyperlight_host/src/hypervisor/kvm.rs index 3dd1cb1fc..469de6177 100644 --- a/src/hyperlight_host/src/hypervisor/kvm.rs +++ b/src/hyperlight_host/src/hypervisor/kvm.rs @@ -16,7 +16,6 @@ limitations under the License. use std::convert::TryFrom; use std::fmt::Debug; -#[cfg(gdb)] use std::sync::{Arc, Mutex}; use kvm_bindings::{kvm_fpu, kvm_regs, kvm_userspace_memory_region, KVM_MEM_READONLY}; @@ -30,7 +29,7 @@ use super::fpu::{FP_CONTROL_WORD_DEFAULT, FP_TAG_WORD_DEFAULT, MXCSR_DEFAULT}; use super::gdb::{DebugCommChannel, DebugMsg, DebugResponse, GuestDebug, KvmDebug, VcpuStopReason}; #[cfg(gdb)] use super::handlers::DbgMemAccessHandlerWrapper; -use super::handlers::{MemAccessHandlerWrapper, OutBHandlerWrapper}; +use super::handlers::{MemAccessHandlerWrapper, OutBHandler, OutBHandlerCaller}; use super::{ HyperlightExit, Hypervisor, VirtualCPU, CR0_AM, CR0_ET, CR0_MP, CR0_NE, CR0_PE, CR0_PG, CR0_WP, CR4_OSFXSR, CR4_OSXMMEXCPT, CR4_PAE, EFER_LMA, EFER_LME, EFER_NX, EFER_SCE, @@ -404,7 +403,7 @@ impl Hypervisor for KVMDriver { peb_addr: RawPtr, seed: u64, page_size: u32, - outb_hdl: OutBHandlerWrapper, + outb_hdl: Arc>, mem_access_hdl: MemAccessHandlerWrapper, hv_handler: Option, max_guest_log_level: Option, @@ -445,7 +444,7 @@ impl Hypervisor for KVMDriver { fn dispatch_call_from_host( &mut self, dispatch_func_addr: RawPtr, - outb_handle_fn: OutBHandlerWrapper, + outb_handle_fn: Arc>, mem_access_fn: MemAccessHandlerWrapper, hv_handler: Option, #[cfg(gdb)] dbg_mem_access_fn: DbgMemAccessHandlerWrapper, @@ -487,7 +486,7 @@ impl Hypervisor for KVMDriver { data: Vec, _rip: u64, _instruction_length: u64, - outb_handle_fn: OutBHandlerWrapper, + outb_handle_fn: Arc>, ) -> Result<()> { // KVM does not need RIP or instruction length, as it automatically sets the RIP diff --git a/src/hyperlight_host/src/hypervisor/mod.rs b/src/hyperlight_host/src/hypervisor/mod.rs index 62cebe829..e37969ef9 100644 --- a/src/hyperlight_host/src/hypervisor/mod.rs +++ b/src/hyperlight_host/src/hypervisor/mod.rs @@ -67,9 +67,7 @@ use gdb::VcpuStopReason; #[cfg(gdb)] use self::handlers::{DbgMemAccessHandlerCaller, DbgMemAccessHandlerWrapper}; -use self::handlers::{ - MemAccessHandlerCaller, MemAccessHandlerWrapper, OutBHandlerCaller, OutBHandlerWrapper, -}; +use self::handlers::{MemAccessHandlerCaller, MemAccessHandlerWrapper, OutBHandler}; use crate::hypervisor::hypervisor_handler::HypervisorHandler; use crate::mem::ptr::RawPtr; @@ -124,7 +122,7 @@ pub(crate) trait Hypervisor: Debug + Sync + Send { peb_addr: RawPtr, seed: u64, page_size: u32, - outb_handle_fn: OutBHandlerWrapper, + outb_handle_fn: Arc>, mem_access_fn: MemAccessHandlerWrapper, hv_handler: Option, guest_max_log_level: Option, @@ -141,7 +139,7 @@ pub(crate) trait Hypervisor: Debug + Sync + Send { fn dispatch_call_from_host( &mut self, dispatch_func_addr: RawPtr, - outb_handle_fn: OutBHandlerWrapper, + outb_handle_fn: Arc>, mem_access_fn: MemAccessHandlerWrapper, hv_handler: Option, #[cfg(gdb)] dbg_mem_access_fn: DbgMemAccessHandlerWrapper, @@ -154,7 +152,7 @@ pub(crate) trait Hypervisor: Debug + Sync + Send { data: Vec, rip: u64, instruction_length: u64, - outb_handle_fn: OutBHandlerWrapper, + outb_handle_fn: Arc>, ) -> Result<()>; /// Run the vCPU @@ -254,7 +252,7 @@ impl VirtualCPU { pub fn run( hv: &mut dyn Hypervisor, hv_handler: Option, - outb_handle_fn: Arc>, + outb_handle_fn: Arc>, mem_access_fn: Arc>, #[cfg(gdb)] dbg_mem_access_fn: Arc>, ) -> Result<()> { @@ -341,7 +339,7 @@ pub(crate) mod tests { #[cfg(gdb)] use super::handlers::DbgMemAccessHandlerWrapper; - use super::handlers::{MemAccessHandlerWrapper, OutBHandlerWrapper}; + use super::handlers::{MemAccessHandlerWrapper, OutBHandler}; use crate::hypervisor::hypervisor_handler::{ HvHandlerConfig, HypervisorHandler, HypervisorHandlerAction, }; @@ -351,7 +349,7 @@ pub(crate) mod tests { use crate::{new_error, Result}; pub(crate) fn test_initialise( - outb_hdl: OutBHandlerWrapper, + outb_hdl: Arc>, mem_access_hdl: MemAccessHandlerWrapper, #[cfg(gdb)] dbg_mem_access_fn: DbgMemAccessHandlerWrapper, ) -> Result<()> { diff --git a/src/hyperlight_host/src/sandbox/outb.rs b/src/hyperlight_host/src/sandbox/outb.rs index 4dc91207f..d0032f2a4 100644 --- a/src/hyperlight_host/src/sandbox/outb.rs +++ b/src/hyperlight_host/src/sandbox/outb.rs @@ -26,7 +26,7 @@ use tracing_log::format_trace; use super::host_funcs::FunctionRegistry; use super::mem_mgr::MemMgrWrapper; -use crate::hypervisor::handlers::{OutBHandler, OutBHandlerFunction, OutBHandlerWrapper}; +use crate::hypervisor::handlers::{OutBHandler, OutBHandlerFunction}; use crate::mem::mgr::SandboxMemoryManager; use crate::mem::shared_mem::HostSharedMemory; use crate::{new_error, HyperlightError, Result}; @@ -184,14 +184,14 @@ fn handle_outb_impl( } /// Given a `MemMgrWrapper` and ` HostFuncsWrapper` -- both passed by _value_ -/// -- return an `OutBHandlerWrapper` wrapping the core OUTB handler logic. +/// -- return an Arc> wrapping the core OUTB handler logic. /// /// TODO: pass at least the `host_funcs_wrapper` param by reference. #[instrument(skip_all, parent = Span::current(), level= "Trace")] pub(crate) fn outb_handler_wrapper( mut mem_mgr_wrapper: MemMgrWrapper, host_funcs_wrapper: Arc>, -) -> OutBHandlerWrapper { +) -> Arc> { let outb_func: OutBHandlerFunction = Box::new(move |port, payload| { handle_outb_impl( &mut mem_mgr_wrapper,