Skip to content

[WIP] Remove OutBHandlerWrapper type #519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 2 additions & 41 deletions src/hyperlight_host/src/hypervisor/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,49 +18,10 @@ use std::sync::{Arc, Mutex};

use tracing::{instrument, Span};

/// Re-export OutBHandler from the outb module where it naturally belongs
pub(crate) use crate::sandbox::outb::OutBHandler;
use crate::{new_error, Result};

/// The trait representing custom logic to handle the case when
/// a Hypervisor's virtual CPU (vCPU) informs Hyperlight the guest
/// has initiated an outb operation.
pub trait OutBHandlerCaller: Sync + Send {
/// Function that gets called when an outb operation has occurred.
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<Mutex<dyn OutBHandlerCaller>>;

pub(crate) type OutBHandlerFunction = Box<dyn FnMut(u16, u32) -> Result<()> + Send>;

/// A `OutBHandler` implementation using a `OutBHandlerFunction`
///
/// Note: This handler must live no longer than the `Sandbox` to which it belongs
pub(crate) struct OutBHandler(Arc<Mutex<OutBHandlerFunction>>);

impl From<OutBHandlerFunction> for OutBHandler {
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
fn from(func: OutBHandlerFunction) -> Self {
Self(Arc::new(Mutex::new(func)))
}
}

impl OutBHandlerCaller for OutBHandler {
#[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
fn call(&mut self, port: u16, payload: u32) -> Result<()> {
let mut func = self
.0
.try_lock()
.map_err(|e| new_error!("Error locking at {}:{}: {}", file!(), line!(), e))?;
func(port, payload)
}
}

/// The trait representing custom logic to handle the case when
/// a Hypervisor's virtual CPU (vCPU) informs Hyperlight a memory access
/// outside the designated address space has occurred.
Expand Down
9 changes: 5 additions & 4 deletions src/hyperlight_host/src/hypervisor/hyperv_linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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};
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,
Expand Down Expand Up @@ -459,7 +460,7 @@ impl Hypervisor for HypervLinuxDriver {
peb_addr: RawPtr,
seed: u64,
page_size: u32,
outb_hdl: OutBHandlerWrapper,
outb_hdl: Arc<Mutex<OutBHandler>>,
mem_access_hdl: MemAccessHandlerWrapper,
hv_handler: Option<HypervisorHandler>,
max_guest_log_level: Option<LevelFilter>,
Expand Down Expand Up @@ -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<Mutex<OutBHandler>>,
mem_access_fn: MemAccessHandlerWrapper,
hv_handler: Option<HypervisorHandler>,
#[cfg(gdb)] dbg_mem_access_fn: DbgMemAccessHandlerWrapper,
Expand Down Expand Up @@ -544,7 +545,7 @@ impl Hypervisor for HypervLinuxDriver {
data: Vec<u8>,
rip: u64,
instruction_length: u64,
outb_handle_fn: OutBHandlerWrapper,
outb_handle_fn: Arc<Mutex<OutBHandler>>,
) -> Result<()> {
let mut padded = [0u8; 4];
let copy_len = data.len().min(4);
Expand Down
23 changes: 14 additions & 9 deletions src/hyperlight_host/src/hypervisor/hyperv_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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};
use super::surrogate_process::SurrogateProcess;
use super::surrogate_process_manager::*;
use super::windows_hypervisor_platform::{VMPartition, VMProcessor};
Expand Down Expand Up @@ -305,7 +306,7 @@ impl Hypervisor for HypervWindowsDriver {
peb_address: RawPtr,
seed: u64,
page_size: u32,
outb_hdl: OutBHandlerWrapper,
outb_hdl: Arc<Mutex<OutBHandler>>,
mem_access_hdl: MemAccessHandlerWrapper,
hv_handler: Option<HypervisorHandler>,
max_guest_log_level: Option<LevelFilter>,
Expand Down Expand Up @@ -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<Mutex<OutBHandler>>,
mem_access_hdl: MemAccessHandlerWrapper,
hv_handler: Option<HypervisorHandler>,
#[cfg(gdb)] dbg_mem_access_hdl: DbgMemAccessHandlerWrapper,
Expand Down Expand Up @@ -388,7 +389,7 @@ impl Hypervisor for HypervWindowsDriver {
data: Vec<u8>,
rip: u64,
instruction_length: u64,
outb_handle_fn: OutBHandlerWrapper,
outb_handle_fn: Arc<Mutex<OutBHandler>>,
) -> Result<()> {
let mut padded = [0u8; 4];
let copy_len = data.len().min(4);
Expand Down Expand Up @@ -500,20 +501,24 @@ impl Hypervisor for HypervWindowsDriver {
pub mod tests {
use std::sync::{Arc, Mutex};

use hyperlight_testing::dummy_guest_as_string;
use serial_test::serial;

use crate::hypervisor::handlers::{MemAccessHandler, OutBHandler};
use crate::hypervisor::tests::test_initialise;
use crate::sandbox::uninitialized::{GuestBinary, UninitializedSandbox};
use crate::Result;

#[test]
#[serial]
fn test_init() {
let outb_handler = {
let func: Box<dyn FnMut(u16, u32) -> Result<()> + Send> =
Box::new(|_, _| -> Result<()> { Ok(()) });
Arc::new(Mutex::new(OutBHandler::from(func)))
};
let filename = dummy_guest_as_string().expect("Guest Binary Missing");
let sandbox = UninitializedSandbox::new(GuestBinary::FilePath(filename), None).unwrap();
let (hshm, gshm) = sandbox.mgr.build();
drop(gshm);
let host_funcs = sandbox.host_funcs.clone();

let outb_handler = { crate::sandbox::outb::outb_handler_wrapper(hshm.clone(), host_funcs) };
let mem_access_handler = {
let func: Box<dyn FnMut() -> Result<()> + Send> = Box::new(|| -> Result<()> { Ok(()) });
Arc::new(Mutex::new(MemAccessHandler::from(func)))
Expand Down
4 changes: 2 additions & 2 deletions src/hyperlight_host/src/hypervisor/hypervisor_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -184,7 +184,7 @@ pub(crate) struct HvHandlerConfig {
pub(crate) dispatch_function_addr: Arc<Mutex<Option<RawPtr>>>,
pub(crate) max_init_time: Duration,
pub(crate) max_exec_time: Duration,
pub(crate) outb_handler: OutBHandlerWrapper,
pub(crate) outb_handler: Arc<Mutex<OutBHandler>>,
pub(crate) mem_access_handler: MemAccessHandlerWrapper,
pub(crate) max_wait_for_cancellation: Duration,
pub(crate) max_guest_log_level: Option<LevelFilter>,
Expand Down
25 changes: 15 additions & 10 deletions src/hyperlight_host/src/hypervisor/kvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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};
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,
Expand Down Expand Up @@ -404,7 +403,7 @@ impl Hypervisor for KVMDriver {
peb_addr: RawPtr,
seed: u64,
page_size: u32,
outb_hdl: OutBHandlerWrapper,
outb_hdl: Arc<Mutex<OutBHandler>>,
mem_access_hdl: MemAccessHandlerWrapper,
hv_handler: Option<HypervisorHandler>,
max_guest_log_level: Option<LevelFilter>,
Expand Down Expand Up @@ -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<Mutex<OutBHandler>>,
mem_access_fn: MemAccessHandlerWrapper,
hv_handler: Option<HypervisorHandler>,
#[cfg(gdb)] dbg_mem_access_fn: DbgMemAccessHandlerWrapper,
Expand Down Expand Up @@ -487,7 +486,7 @@ impl Hypervisor for KVMDriver {
data: Vec<u8>,
_rip: u64,
_instruction_length: u64,
outb_handle_fn: OutBHandlerWrapper,
outb_handle_fn: Arc<Mutex<OutBHandler>>,
) -> Result<()> {
// KVM does not need RIP or instruction length, as it automatically sets the RIP

Expand Down Expand Up @@ -638,10 +637,13 @@ impl Hypervisor for KVMDriver {
mod tests {
use std::sync::{Arc, Mutex};

use hyperlight_testing::dummy_guest_as_string;

#[cfg(gdb)]
use crate::hypervisor::handlers::DbgMemAccessHandlerCaller;
use crate::hypervisor::handlers::{MemAccessHandler, OutBHandler};
use crate::hypervisor::tests::test_initialise;
use crate::sandbox::uninitialized::{GuestBinary, UninitializedSandbox};
use crate::Result;

#[cfg(gdb)]
Expand All @@ -668,11 +670,14 @@ mod tests {
return;
}

let outb_handler: Arc<Mutex<OutBHandler>> = {
let func: Box<dyn FnMut(u16, u32) -> Result<()> + Send> =
Box::new(|_, _| -> Result<()> { Ok(()) });
Arc::new(Mutex::new(OutBHandler::from(func)))
};
let filename = dummy_guest_as_string().expect("Guest Binary Missing");
let sandbox = UninitializedSandbox::new(GuestBinary::FilePath(filename), None).unwrap();
let (hshm, gshm) = sandbox.mgr.build();
drop(gshm);
let host_funcs = sandbox.host_funcs.clone();

let outb_handler: Arc<Mutex<OutBHandler>> =
{ crate::sandbox::outb::outb_handler_wrapper(hshm.clone(), host_funcs) };
let mem_access_handler = {
let func: Box<dyn FnMut() -> Result<()> + Send> = Box::new(|| -> Result<()> { Ok(()) });
Arc::new(Mutex::new(MemAccessHandler::from(func)))
Expand Down
16 changes: 7 additions & 9 deletions src/hyperlight_host/src/hypervisor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<Mutex<OutBHandler>>,
mem_access_fn: MemAccessHandlerWrapper,
hv_handler: Option<HypervisorHandler>,
guest_max_log_level: Option<LevelFilter>,
Expand All @@ -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<Mutex<OutBHandler>>,
mem_access_fn: MemAccessHandlerWrapper,
hv_handler: Option<HypervisorHandler>,
#[cfg(gdb)] dbg_mem_access_fn: DbgMemAccessHandlerWrapper,
Expand All @@ -154,7 +152,7 @@ pub(crate) trait Hypervisor: Debug + Sync + Send {
data: Vec<u8>,
rip: u64,
instruction_length: u64,
outb_handle_fn: OutBHandlerWrapper,
outb_handle_fn: Arc<Mutex<OutBHandler>>,
) -> Result<()>;

/// Run the vCPU
Expand Down Expand Up @@ -254,7 +252,7 @@ impl VirtualCPU {
pub fn run(
hv: &mut dyn Hypervisor,
hv_handler: Option<HypervisorHandler>,
outb_handle_fn: Arc<Mutex<dyn OutBHandlerCaller>>,
outb_handle_fn: Arc<Mutex<OutBHandler>>,
mem_access_fn: Arc<Mutex<dyn MemAccessHandlerCaller>>,
#[cfg(gdb)] dbg_mem_access_fn: Arc<Mutex<dyn DbgMemAccessHandlerCaller>>,
) -> Result<()> {
Expand Down Expand Up @@ -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,
};
Expand All @@ -351,7 +349,7 @@ pub(crate) mod tests {
use crate::{new_error, Result};

pub(crate) fn test_initialise(
outb_hdl: OutBHandlerWrapper,
outb_hdl: Arc<Mutex<OutBHandler>>,
mem_access_hdl: MemAccessHandlerWrapper,
#[cfg(gdb)] dbg_mem_access_fn: DbgMemAccessHandlerWrapper,
) -> Result<()> {
Expand Down
Loading