Skip to content

Commit e0258db

Browse files
committed
Add file backed ReadonlySharedMemory
Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com>
1 parent 16a5e2f commit e0258db

6 files changed

Lines changed: 660 additions & 39 deletions

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1497,7 +1497,8 @@ mod tests {
14971497
[layout.get_guest_code_offset()..layout.get_guest_code_offset() + code.len()]
14981498
.copy_from_slice(code);
14991499
layout.write_peb(&mut snapshot_contents).unwrap();
1500-
let ro_mem = ReadonlySharedMemory::from_bytes(&snapshot_contents).unwrap();
1500+
let ro_mem =
1501+
ReadonlySharedMemory::from_bytes(&snapshot_contents, snapshot_pt_start).unwrap();
15011502

15021503
let scratch_mem = ExclusiveSharedMemory::new(config.get_scratch_size()).unwrap();
15031504
let mem_mgr = SandboxMemoryManager::new(

src/hyperlight_host/src/mem/layout.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,15 @@ pub(crate) struct SandboxMemoryLayout {
232232
pub(crate) init_data_permissions: Option<MemoryRegionFlags>,
233233
/// The size of the scratch region in physical memory.
234234
pub(crate) scratch_size: usize,
235-
/// The size of the snapshot region in physical memory.
235+
/// Size of the primary guest memory region at `BASE_ADDRESS`
236+
/// (code, PEB, heap, init data). For a snapshot-backed layout
237+
/// this is also the guest-visible prefix of the host snapshot
238+
/// mapping.
236239
pub(crate) snapshot_size: usize,
237-
/// The size of the page tables (None if not yet set).
240+
/// Size of the page-table region. Sits at the tail of the host
241+
/// snapshot mapping but is never mapped to the guest from there.
242+
/// On restore the host copies it into scratch, where the guest
243+
/// sees it at `SNAPSHOT_PT_GVA`. `None` until page tables are built.
238244
pub(crate) pt_size: Option<usize>,
239245
}
240246

@@ -497,7 +503,12 @@ impl SandboxMemoryLayout {
497503
}
498504
}
499505

500-
/// Sets the size of the memory region used for page tables
506+
/// Record the size of the page-table tail appended to the
507+
/// snapshot blob. The PT bytes live at the end of the blob and
508+
/// the host mapping, outside the guest mapping of the snapshot
509+
/// region, and are copied into the scratch region on restore.
510+
/// `snapshot_size` (the guest-visible prefix of the blob) is an
511+
/// independent field and must be set separately.
501512
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
502513
pub(crate) fn set_pt_size(&mut self, size: usize) -> Result<()> {
503514
let min_fixed_scratch = hyperlight_common::layout::min_scratch_size(
@@ -508,8 +519,6 @@ impl SandboxMemoryLayout {
508519
if self.scratch_size < min_scratch {
509520
return Err(MemoryRequestTooSmall(self.scratch_size, min_scratch));
510521
}
511-
let old_pt_size = self.pt_size.unwrap_or(0);
512-
self.snapshot_size = self.snapshot_size - old_pt_size + size;
513522
self.pt_size = Some(size);
514523
Ok(())
515524
}

src/hyperlight_host/src/mem/memory_region.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,14 @@ pub enum MemoryRegionType {
153153
impl MemoryRegionType {
154154
/// Derives the [`SurrogateMapping`] from this region type.
155155
///
156-
/// `MappedFile` regions use read-only file-backed mappings with no
157-
/// guard pages; all other region types use the standard sandbox
158-
/// shared memory mapping with guard pages.
156+
/// `MappedFile` and `Snapshot` regions use read-only file-backed
157+
/// mappings with no guard pages. All other region types use the
158+
/// standard sandbox shared memory mapping with guard pages.
159159
pub fn surrogate_mapping(&self) -> SurrogateMapping {
160160
match self {
161-
MemoryRegionType::MappedFile => SurrogateMapping::ReadOnlyFile,
161+
MemoryRegionType::MappedFile | MemoryRegionType::Snapshot => {
162+
SurrogateMapping::ReadOnlyFile
163+
}
162164
_ => SurrogateMapping::SandboxMemory,
163165
}
164166
}

src/hyperlight_host/src/mem/mgr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@ impl SandboxMemoryManager<HostSharedMemory> {
723723
use crate::mem::memory_region::HostGuestMemoryRegion;
724724

725725
let snapshot_base = SandboxMemoryLayout::BASE_ADDRESS;
726-
let snapshot_size = self.shared_mem.mem_size();
726+
let snapshot_size = self.layout.snapshot_size;
727727
let snapshot_host = self.shared_mem.base_addr();
728728

729729
let scratch_size = self.scratch_mem.mem_size();

0 commit comments

Comments
 (0)