Skip to content

Commit 14ee7bc

Browse files
committed
feat: add snapshot generation counter to scratch bookkeeping
Signed-off-by: Tomasz Andrzejak <andreiltd@gmail.com>
1 parent 09f3dd7 commit 14ee7bc

3 files changed

Lines changed: 18 additions & 1 deletion

File tree

src/hyperlight_common/src/layout.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ pub use arch::{SNAPSHOT_PT_GVA_MAX, SNAPSHOT_PT_GVA_MIN};
3737
pub const SCRATCH_TOP_SIZE_OFFSET: u64 = 0x08;
3838
pub const SCRATCH_TOP_ALLOCATOR_OFFSET: u64 = 0x10;
3939
pub const SCRATCH_TOP_SNAPSHOT_PT_GPA_BASE_OFFSET: u64 = 0x18;
40-
pub const SCRATCH_TOP_EXN_STACK_OFFSET: u64 = 0x20;
40+
pub const SCRATCH_TOP_SNAPSHOT_GENERATION_OFFSET: u64 = 0x20;
41+
pub const SCRATCH_TOP_EXN_STACK_OFFSET: u64 = 0x30;
4142

4243
/// Offset from the top of scratch memory for a shared host-guest u64 counter.
4344
///

src/hyperlight_guest/src/layout.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ pub fn snapshot_pt_gpa_base_gva() -> *mut u64 {
3232
use hyperlight_common::layout::{MAX_GVA, SCRATCH_TOP_SNAPSHOT_PT_GPA_BASE_OFFSET};
3333
(MAX_GVA as u64 - SCRATCH_TOP_SNAPSHOT_PT_GPA_BASE_OFFSET + 1) as *mut u64
3434
}
35+
pub fn snapshot_generation_gva() -> *mut u64 {
36+
use hyperlight_common::layout::{MAX_GVA, SCRATCH_TOP_SNAPSHOT_GENERATION_OFFSET};
37+
(MAX_GVA as u64 - SCRATCH_TOP_SNAPSHOT_GENERATION_OFFSET + 1) as *mut u64
38+
}
3539
pub use arch::{scratch_base_gpa, scratch_base_gva};
3640

3741
/// Returns a pointer to the guest counter u64 in scratch memory.

src/hyperlight_host/src/mem/mgr.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ pub(crate) struct SandboxMemoryManager<S: SharedMemory> {
148148
pub(crate) mapped_rgns: u64,
149149
/// Buffer for accumulating guest abort messages
150150
pub(crate) abort_buffer: Vec<u8>,
151+
/// Snapshot restore generation counter. 0 means no restore
152+
/// and is incremented on each `restore_snapshot` call.
153+
pub(crate) restore_count: u64,
151154
}
152155

153156
/// Buffer for building guest page tables during snapshot creation.
@@ -274,6 +277,7 @@ where
274277
entrypoint,
275278
mapped_rgns: 0,
276279
abort_buffer: Vec::new(),
280+
restore_count: 0,
277281
}
278282
}
279283

@@ -341,6 +345,7 @@ impl SandboxMemoryManager<ExclusiveSharedMemory> {
341345
entrypoint: self.entrypoint,
342346
mapped_rgns: self.mapped_rgns,
343347
abort_buffer: self.abort_buffer,
348+
restore_count: self.restore_count,
344349
};
345350
let guest_mgr = SandboxMemoryManager {
346351
shared_mem: gshm,
@@ -349,6 +354,7 @@ impl SandboxMemoryManager<ExclusiveSharedMemory> {
349354
entrypoint: self.entrypoint,
350355
mapped_rgns: self.mapped_rgns,
351356
abort_buffer: Vec::new(), // Guest doesn't need abort buffer
357+
restore_count: self.restore_count,
352358
};
353359
host_mgr.update_scratch_bookkeeping()?;
354360
host_mgr.copy_pt_to_scratch()?;
@@ -541,6 +547,8 @@ impl SandboxMemoryManager<HostSharedMemory> {
541547
Some(gscratch)
542548
};
543549
self.layout = *snapshot.layout();
550+
self.restore_count += 1;
551+
544552
self.update_scratch_bookkeeping()?;
545553
self.copy_pt_to_scratch()?;
546554
Ok((gsnapshot, gscratch))
@@ -565,6 +573,10 @@ impl SandboxMemoryManager<HostSharedMemory> {
565573
SCRATCH_TOP_SNAPSHOT_PT_GPA_BASE_OFFSET,
566574
self.layout.get_pt_base_gpa(),
567575
)?;
576+
self.update_scratch_bookkeeping_item(
577+
SCRATCH_TOP_SNAPSHOT_GENERATION_OFFSET,
578+
self.restore_count,
579+
)?;
568580

569581
// Initialise the guest input and output data buffers in
570582
// scratch memory. TODO: remove the need for this.

0 commit comments

Comments
 (0)