Skip to content

Commit 9bde4de

Browse files
committed
feat(virtq): remove input output regions from ABI
Signed-off-by: Tomasz Andrzejak <andreiltd@gmail.com>
1 parent 392bfa6 commit 9bde4de

17 files changed

Lines changed: 105 additions & 409 deletions

File tree

fuzz/fuzz_targets/host_call.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ static SANDBOX: OnceLock<Mutex<MultiUseSandbox>> = OnceLock::new();
3333
fuzz_target!(
3434
init: {
3535
let mut cfg = SandboxConfiguration::default();
36-
cfg.set_output_data_size(64 * 1024); // 64 KB output buffer
37-
cfg.set_input_data_size(64 * 1024); // 64 KB input buffer
38-
cfg.set_scratch_size(512 * 1024); // large scratch region to contain those buffers, any data copies, etc.
36+
cfg.set_g2h_pool_pages(16); // 64 KB / 4096 = 16 pages
37+
cfg.set_h2g_pool_pages(16); // 64 KB / 4096 = 16 pages
38+
cfg.set_scratch_size(512 * 1024); // large scratch region
3939
let u_sbox = UninitializedSandbox::new(
4040
GuestBinary::FilePath(simple_guest_for_fuzzing_as_string().expect("Guest Binary Missing")),
4141
Some(cfg)

src/hyperlight_common/src/arch/aarch64/layout.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ pub const SNAPSHOT_PT_GVA_MIN: usize = 0xffff_8000_0000_0000;
2020
pub const SNAPSHOT_PT_GVA_MAX: usize = 0xffff_80ff_ffff_ffff;
2121
pub const MAX_GPA: usize = 0x0000_000f_ffff_ffff;
2222

23-
pub fn min_scratch_size(
24-
_input_data_size: usize,
25-
_output_data_size: usize,
26-
_g2h_num_descs: usize,
27-
_h2g_num_descs: usize,
28-
) -> usize {
23+
pub fn min_scratch_size(_g2h_num_descs: usize, _h2g_num_descs: usize) -> usize {
2924
unimplemented!("min_scratch_size")
3025
}

src/hyperlight_common/src/arch/amd64/layout.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,11 @@ pub const MAX_GPA: usize = 0x0000_000f_ffff_ffff;
3737
/// - A page for the smallest possible non-exception stack
3838
/// - (up to) 3 pages for mapping that
3939
/// - Two pages for the exception stack and metadata
40-
/// - A page-aligned amount of memory for I/O buffers and virtqueue rings
41-
pub fn min_scratch_size(
42-
input_data_size: usize,
43-
output_data_size: usize,
44-
g2h_num_descs: usize,
45-
h2g_num_descs: usize,
46-
) -> usize {
40+
/// - A page-aligned amount of memory for virtqueue rings
41+
pub fn min_scratch_size(g2h_num_descs: usize, h2g_num_descs: usize) -> usize {
4742
let g2h_ring_size = crate::virtq::Layout::query_size(g2h_num_descs);
4843
let h2g_ring_size = crate::virtq::Layout::query_size(h2g_num_descs);
4944

50-
(input_data_size + output_data_size + g2h_ring_size + h2g_ring_size)
51-
.next_multiple_of(crate::vmem::PAGE_SIZE)
45+
(g2h_ring_size + h2g_ring_size).next_multiple_of(crate::vmem::PAGE_SIZE)
5246
+ 12 * crate::vmem::PAGE_SIZE
5347
}

src/hyperlight_common/src/arch/i686/layout.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ limitations under the License.
2020
pub const MAX_GVA: usize = 0xffff_ffff;
2121
pub const MAX_GPA: usize = 0xffff_ffff;
2222

23-
pub fn min_scratch_size(
24-
_input_data_size: usize,
25-
_output_data_size: usize,
26-
_g2h_num_descs: usize,
27-
_h2g_num_descs: usize,
28-
) -> usize {
23+
pub fn min_scratch_size(_g2h_num_descs: usize, _h2g_num_descs: usize) -> usize {
2924
crate::vmem::PAGE_SIZE
3025
}

src/hyperlight_common/src/layout.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,27 +83,20 @@ pub const fn scratch_top_ptr<T>(offset: u64) -> *mut T {
8383

8484
/// Compute the byte offset from the scratch base to the G2H ring.
8585
///
86-
/// TODO(virtq): Remove input/output
87-
pub const fn g2h_ring_scratch_offset(input_data_size: usize, output_data_size: usize) -> usize {
88-
let io_off = input_data_size + output_data_size;
89-
let align = crate::virtq::Descriptor::ALIGN;
90-
91-
(io_off + align - 1) & !(align - 1)
86+
/// The G2H ring starts at offset 0, aligned to descriptor alignment.
87+
pub const fn g2h_ring_scratch_offset() -> usize {
88+
0
9289
}
9390

9491
/// Compute the byte offset from the scratch base to the H2G ring.
9592
///
96-
/// TODO(ring): Remove input/output
97-
pub const fn h2g_ring_scratch_offset(
98-
input_data_size: usize,
99-
output_data_size: usize,
100-
g2h_num_descs: usize,
101-
) -> usize {
102-
let g2h_offset = g2h_ring_scratch_offset(input_data_size, output_data_size);
93+
/// The H2G ring follows immediately after the G2H ring, aligned to
94+
/// descriptor alignment.
95+
pub const fn h2g_ring_scratch_offset(g2h_num_descs: usize) -> usize {
10396
let g2h_size = crate::virtq::Layout::query_size(g2h_num_descs);
10497
let align = crate::virtq::Descriptor::ALIGN;
10598

106-
(g2h_offset + g2h_size + align - 1) & !(align - 1)
99+
(g2h_size + align - 1) & !(align - 1)
107100
}
108101

109102
/// Compute the minimum scratch region size needed for a sandbox.

src/hyperlight_common/src/mem.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ impl Default for FileMappingInfo {
6868
#[derive(Debug, Clone, Copy)]
6969
#[repr(C)]
7070
pub struct HyperlightPEB {
71-
pub input_stack: GuestMemoryRegion,
72-
pub output_stack: GuestMemoryRegion,
7371
pub init_data: GuestMemoryRegion,
7472
pub guest_heap: GuestMemoryRegion,
7573
/// File mappings array descriptor.

src/hyperlight_guest_capi/src/dispatch.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,22 @@ pub extern "C" fn hl_call_host_function(function_call: &FfiFunctionCall) {
109109
let return_type = unsafe { function_call.copy_return_type() };
110110

111111
virtq::with_context(|ctx| {
112-
let result: ReturnValue = ctx
113-
.call_host_function(&func_name, Some(parameters), return_type)
114-
.expect("Failed to call host function");
115-
ctx.stash_host_return(result);
112+
match ctx.call_host_function::<ReturnValue>(&func_name, Some(parameters), return_type) {
113+
Ok(result) => ctx.stash_host_return(result),
114+
Err(e) => {
115+
// Host function returned an error. Abort with the error
116+
// message so the host can capture it via the abort buffer.
117+
let msg = alloc::ffi::CString::new(e.message)
118+
.unwrap_or_else(|_| alloc::ffi::CString::new("host error").unwrap());
119+
120+
unsafe {
121+
hyperlight_guest::exit::abort_with_code_and_message(
122+
&[e.kind as u8],
123+
msg.as_ptr(),
124+
);
125+
}
126+
}
127+
}
116128
});
117129
}
118130

src/hyperlight_host/benches/benchmarks.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,9 +385,9 @@ fn guest_call_benchmark_large_param(c: &mut Criterion) {
385385
let large_string = String::from_utf8(large_vec.clone()).unwrap();
386386

387387
let mut config = SandboxConfiguration::default();
388-
config.set_input_data_size(2 * SIZE + (1024 * 1024)); // 2 * SIZE + 1 MB, to allow 1MB for the rest of the serialized function call
388+
config.set_h2g_pool_pages((2 * SIZE + (1024 * 1024)) / 4096); // pool pages for the large input
389389
config.set_heap_size(SIZE as u64 * 15);
390-
config.set_scratch_size(6 * SIZE + 4 * (1024 * 1024)); // Big enough for the IO data regions and enough of the heap to be used
390+
config.set_scratch_size(6 * SIZE + 4 * (1024 * 1024)); // Big enough for any data copies, etc.
391391

392392
let sandbox = UninitializedSandbox::new(
393393
GuestBinary::FilePath(simple_guest_as_string().unwrap()),
@@ -470,7 +470,7 @@ fn sample_workloads_benchmark(c: &mut Criterion) {
470470

471471
fn bench_24k_in_8k_out(b: &mut criterion::Bencher, guest_path: String) {
472472
let mut cfg = SandboxConfiguration::default();
473-
cfg.set_input_data_size(25 * 1024);
473+
cfg.set_h2g_pool_pages(7); // 25 * 1024 / 4096 ~= 7 pages
474474

475475
let mut sandbox = UninitializedSandbox::new(GuestBinary::FilePath(guest_path), Some(cfg))
476476
.unwrap()

src/hyperlight_host/src/hypervisor/hyperlight_vm/x86_64.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2125,17 +2125,18 @@ mod tests {
21252125
}
21262126

21272127
/// Creates VM with guest code that: dirtys FPU (if flag==0), does FXSAVE to buffer, sets flag=1.
2128-
/// Uses output_data region for FXSAVE buffer (like regular guest output), scratch for stack.
2128+
/// Uses scratch region after rings for FXSAVE buffer.
21292129
fn hyperlight_vm_with_mem_mgr_fxsave() -> FxsaveTestContext {
21302130
use iced_x86::code_asm::*;
21312131

21322132
// Compute fixed addresses for FXSAVE buffer and flag.
2133-
// These are in the output_data region which starts at a known offset.
2134-
// We use a default SandboxConfiguration to get the same layout as create_test_vm_context.
2133+
// We use the page-table area in scratch after rings as a
2134+
// convenient 512-byte aligned buffer for FXSAVE.
21352135
let config: SandboxConfiguration = Default::default();
21362136
let layout = SandboxMemoryLayout::new(config, 512, 4096, None).unwrap();
2137-
let fxsave_offset = layout.get_output_data_buffer_scratch_host_offset();
2138-
let fxsave_gva = layout.get_output_data_buffer_gva();
2137+
let fxsave_offset = layout.get_pt_base_scratch_offset();
2138+
let fxsave_gva = hyperlight_common::layout::scratch_base_gva(config.get_scratch_size())
2139+
+ fxsave_offset as u64;
21392140
let flag_gva = fxsave_gva + 512;
21402141

21412142
let mut a = CodeAssembler::new(64).unwrap();

0 commit comments

Comments
 (0)