Skip to content

Commit 0b8b212

Browse files
committed
Introduce HyperlightVm as a transparent wrapper
Signed-off-by: Ludvig Liljenberg <[email protected]>
1 parent 485f8e5 commit 0b8b212

File tree

4 files changed

+114
-9
lines changed

4 files changed

+114
-9
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
Copyright 2025 The Hyperlight Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
use crate::Result;
18+
19+
use crate::hypervisor::{Hypervisor, InterruptHandle};
20+
use crate::mem::memory_region::MemoryRegion;
21+
use crate::mem::mgr::SandboxMemoryManager;
22+
use crate::mem::ptr::RawPtr;
23+
use crate::mem::shared_mem::HostSharedMemory;
24+
use crate::sandbox::host_funcs::FunctionRegistry;
25+
26+
use log::LevelFilter;
27+
use std::sync::{Arc, Mutex};
28+
29+
#[derive(Debug)]
30+
pub(crate) struct HyperlightVm {
31+
pub(crate) vm: Box<dyn Hypervisor>,
32+
}
33+
34+
impl HyperlightVm {
35+
pub(crate) fn new(inner: Box<dyn Hypervisor>) -> Self {
36+
Self { vm: inner }
37+
}
38+
39+
#[allow(clippy::too_many_arguments)]
40+
pub(crate) fn initialise(
41+
&mut self,
42+
peb_addr: RawPtr,
43+
seed: u64,
44+
page_size: u32,
45+
mem_mgr: &mut SandboxMemoryManager<HostSharedMemory>,
46+
host_funcs: &Arc<Mutex<FunctionRegistry>>,
47+
guest_max_log_level: Option<LevelFilter>,
48+
#[cfg(gdb)] dbg_mem_access_fn: Arc<Mutex<SandboxMemoryManager<HostSharedMemory>>>,
49+
) -> Result<()> {
50+
self.vm.initialise(
51+
peb_addr,
52+
seed,
53+
page_size,
54+
mem_mgr,
55+
host_funcs,
56+
guest_max_log_level,
57+
#[cfg(gdb)]
58+
dbg_mem_access_fn,
59+
)
60+
}
61+
62+
pub(crate) unsafe fn map_region(&mut self, rgn: &MemoryRegion) -> Result<()> {
63+
unsafe { self.vm.map_region(rgn) }
64+
}
65+
66+
pub(crate) unsafe fn unmap_region(&mut self, rgn: &MemoryRegion) -> Result<()> {
67+
unsafe { self.vm.unmap_region(rgn) }
68+
}
69+
70+
pub(crate) fn get_mapped_regions(
71+
&self,
72+
) -> Box<dyn ExactSizeIterator<Item = &MemoryRegion> + '_> {
73+
self.vm.get_mapped_regions()
74+
}
75+
76+
pub(crate) fn dispatch_call_from_host(
77+
&mut self,
78+
dispatch_func_addr: RawPtr,
79+
mem_mgr: &mut SandboxMemoryManager<HostSharedMemory>,
80+
host_funcs: &Arc<Mutex<FunctionRegistry>>,
81+
#[cfg(gdb)] dbg_mem_access_fn: Arc<Mutex<SandboxMemoryManager<HostSharedMemory>>>,
82+
) -> Result<()> {
83+
self.vm.dispatch_call_from_host(
84+
dispatch_func_addr,
85+
mem_mgr,
86+
host_funcs,
87+
#[cfg(gdb)]
88+
dbg_mem_access_fn,
89+
)
90+
}
91+
92+
pub(crate) fn interrupt_handle(&self) -> Arc<dyn InterruptHandle> {
93+
self.vm.interrupt_handle()
94+
}
95+
96+
pub(crate) fn clear_cancel(&self) {
97+
self.vm.clear_cancel()
98+
}
99+
}

src/hyperlight_host/src/hypervisor/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ pub(crate) mod wrappers;
5959
#[cfg(crashdump)]
6060
pub(crate) mod crashdump;
6161

62+
pub(crate) mod hyperlight_vm;
63+
6264
use std::fmt::Debug;
6365
use std::str::FromStr;
6466
#[cfg(any(kvm, mshv3))]

src/hyperlight_host/src/sandbox/initialized_multi_use.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use super::host_funcs::FunctionRegistry;
3737
use super::snapshot::Snapshot;
3838
use crate::HyperlightError::{self, SnapshotSandboxMismatch};
3939
use crate::func::{ParameterTuple, SupportedReturnType};
40+
use crate::hypervisor::hyperlight_vm::HyperlightVm;
4041
use crate::hypervisor::{Hypervisor, InterruptHandle};
4142
#[cfg(unix)]
4243
use crate::mem::memory_region::MemoryRegionType;
@@ -97,7 +98,7 @@ pub struct MultiUseSandbox {
9798
poisoned: bool,
9899
pub(super) host_funcs: Arc<Mutex<FunctionRegistry>>,
99100
pub(crate) mem_mgr: SandboxMemoryManager<HostSharedMemory>,
100-
vm: Box<dyn Hypervisor>,
101+
vm: HyperlightVm,
101102
dispatch_ptr: RawPtr,
102103
#[cfg(gdb)]
103104
dbg_mem_access_fn: Arc<Mutex<SandboxMemoryManager<HostSharedMemory>>>,
@@ -116,7 +117,7 @@ impl MultiUseSandbox {
116117
pub(super) fn from_uninit(
117118
host_funcs: Arc<Mutex<FunctionRegistry>>,
118119
mgr: SandboxMemoryManager<HostSharedMemory>,
119-
vm: Box<dyn Hypervisor>,
120+
vm: HyperlightVm,
120121
dispatch_ptr: RawPtr,
121122
#[cfg(gdb)] dbg_mem_access_fn: Arc<Mutex<SandboxMemoryManager<HostSharedMemory>>>,
122123
) -> MultiUseSandbox {
@@ -711,7 +712,7 @@ impl MultiUseSandbox {
711712
#[cfg(crashdump)]
712713
#[instrument(err(Debug), skip_all, parent = Span::current())]
713714
pub fn generate_crashdump(&self) -> Result<()> {
714-
crate::hypervisor::crashdump::generate_crashdump(self.vm.as_ref())
715+
crate::hypervisor::crashdump::generate_crashdump(self.vm.vm.as_ref() as &dyn Hypervisor)
715716
}
716717

717718
/// Returns whether the sandbox is currently poisoned.

src/hyperlight_host/src/sandbox/uninitialized_evolve.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use super::hypervisor::{HypervisorType, get_available_hypervisor};
2626
use super::uninitialized::SandboxRuntimeConfig;
2727
use crate::HyperlightError::NoHypervisorFound;
2828
use crate::hypervisor::Hypervisor;
29+
use crate::hypervisor::hyperlight_vm::HyperlightVm;
2930
use crate::mem::exe::LoadInfo;
3031
use crate::mem::layout::SandboxMemoryLayout;
3132
use crate::mem::mgr::SandboxMemoryManager;
@@ -106,7 +107,7 @@ pub(crate) fn set_up_hypervisor_partition(
106107
#[cfg_attr(target_os = "windows", allow(unused_variables))] config: &SandboxConfiguration,
107108
#[cfg(any(crashdump, gdb))] rt_cfg: &SandboxRuntimeConfig,
108109
_load_info: LoadInfo,
109-
) -> Result<Box<dyn Hypervisor>> {
110+
) -> Result<HyperlightVm> {
110111
#[cfg(feature = "init-paging")]
111112
let rsp_ptr = {
112113
let mut regions = mgr.layout.get_memory_regions(&mgr.shared_mem)?;
@@ -167,7 +168,7 @@ pub(crate) fn set_up_hypervisor_partition(
167168
#[cfg(feature = "mem_profile")]
168169
let trace_info = MemTraceInfo::new(_load_info)?;
169170

170-
match *get_available_hypervisor() {
171+
let vm: Box<dyn Hypervisor> = match *get_available_hypervisor() {
171172
#[cfg(mshv3)]
172173
Some(HypervisorType::Mshv) => {
173174
let hv = crate::hypervisor::hyperv_linux::HypervLinuxDriver::new(
@@ -183,7 +184,7 @@ pub(crate) fn set_up_hypervisor_partition(
183184
#[cfg(feature = "mem_profile")]
184185
trace_info,
185186
)?;
186-
Ok(Box::new(hv))
187+
Box::new(hv)
187188
}
188189

189190
#[cfg(kvm)]
@@ -201,7 +202,7 @@ pub(crate) fn set_up_hypervisor_partition(
201202
#[cfg(feature = "mem_profile")]
202203
trace_info,
203204
)?;
204-
Ok(Box::new(hv))
205+
Box::new(hv)
205206
}
206207

207208
#[cfg(target_os = "windows")]
@@ -225,13 +226,15 @@ pub(crate) fn set_up_hypervisor_partition(
225226
#[cfg(feature = "mem_profile")]
226227
trace_info,
227228
)?;
228-
Ok(Box::new(hv))
229+
Box::new(hv)
229230
}
230231

231232
_ => {
232233
log_then_return!(NoHypervisorFound());
233234
}
234-
}
235+
};
236+
237+
Ok(HyperlightVm::new(vm))
235238
}
236239

237240
#[cfg(test)]

0 commit comments

Comments
 (0)