Skip to content
Open
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
contents. Callers must keep blobs immutable. Use the new
`Snapshot::checked_save` to recompute hashes, verify existing blobs, and
atomically repair mismatched contents.
* **Breaking:** Snapshots use ABI 12. Guests must be rebuilt and existing
snapshots regenerated. Capture and restore provide full H2G bootstrap
capacity before guest entry, independent of retained buffers. Snapshot
capture runs on the host while the guest is stopped.
The checkpoint request, `prepare_snapshot`, and
`VirtqProducer::reclaim_slots` APIs are removed.
* `Snapshot::save` no longer synchronously flushes files to stable storage.
Callers requiring power-loss durability must sync the snapshot files and
directory hierarchy themselves after saving.
Expand Down
2 changes: 1 addition & 1 deletion docs/snapshot-oci-format.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Four blob kinds per tag:
binary image of the canonical G2H and H2G rings. Loading checks ring
sizes and canonical state against the config.

The runtime queue protocol and canonical checkpoint are described in
The runtime queue protocol and snapshot bootstrap state are described in
[Virtqueue host and guest communication](./virtio-host-guest-communication.md).

Manifest layers are ordered data, then transport.
Expand Down
6 changes: 6 additions & 0 deletions docs/snapshot-versioning.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ host page size, and log level. Earlier snapshots must be regenerated.
ABI 11 encodes control data with postcard and carries its length in a 16-byte
message header. Earlier snapshots must be regenerated.

ABI 12 stores deterministic queue bootstrap images. Capture and restore install
empty G2H and fully prefilled H2G rings. Guest entry reconstructs ownership
without changing host-written data. The mailbox reports initialization readiness.
Snapshot capture runs on the host while the guest is stopped. Earlier snapshots
must be regenerated.

The `OCI_LAYOUT_VERSION` constant is pinned by the OCI image-layout
spec at `1.0.0`.

Expand Down
145 changes: 67 additions & 78 deletions docs/virtio-host-guest-communication.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,16 @@ On the first VM entry, the guest:
2. Reconstructs `TransportArena`.
3. Converts each transport GPA into its scratch GVA.
4. Constructs a `GuestContext` and installs it with `set_global_context`.
H2G receives one writable descriptor per available slot, bounded by queue size.
Both rings are initialized empty. H2G receives one writable descriptor per
available slot, bounded by queue size.

Dispatch starts with `transport::maybe_refresh`, before logging or tracing.
Runtime operations borrow the context through `transport::with_ctx`.

After user initialization and trace flushing, the guest calls
`transport::prepare_snapshot`, then halts. The host resets its consumers and
requires `MailboxValue::CheckpointComplete`. Initial snapshots include values
`transport::complete_initialization`, then halts. The host requires
`MailboxValue::InitComplete`. Both sides keep their queue cursors so traffic
during initialization remains valid. Initial snapshots include values
retained during initialization.

## Wire format
Expand All @@ -201,16 +203,14 @@ Every logical message has this byte layout:
`payload_len` covers the control data and all external bytes. `control_len`
covers the control data and never exceeds `payload_len`. The host reads it
first so it can copy exactly the control data out of guest memory in one read
before parsing. RPC correlation
IDs are nonzero. Responses echo the request ID. Logs and snapshot checkpoints
use ID zero.
before parsing. Request IDs are nonzero. Responses echo the request ID.
Logs use ID zero.

The active message kinds are:

* `Request`
* `Response`
* `Log`
* `SnapshotCheckpoint`

The control data holds the typed function call or result and the lengths of
external byte values. It is one [postcard](https://docs.rs/postcard) value.
Expand Down Expand Up @@ -389,23 +389,18 @@ external `ByteChunks`:
* The final owner unmaps its alias and returns the virtual range for reuse.
Its lease releases the slot in its original pool.

Producer reset releases allocations still owned by queue bookkeeping. After
both producers reset and before H2G prefill, every live pool slot belongs to
guest retained `Bytes`. H2G prefills free slots up to the queue size.

Checkpoint preparation requires stopped host consumers with no live chain
handles. The host resets both consumers before processing more queue traffic.

### Retained virtual addresses

Both pools share one guest-global alias allocator. Its state is captured with
the mappings and keeps retained ranges reserved across pool swaps.
the mappings and keeps retained ranges reserved across transport generations.
Each completed `GuestMapping` owns a separate page-aligned range. Mapping occurs
when the guest receives the buffer. Capture, restore, and cloning preserve its
pointer and contents.

Retained aliases reach captured memory after restore. The first transport
entry swaps allocators that hold retained slots, preserving posted reservations.
Capture installs independent backing for retained aliases in the source sandbox
and in restored sandboxes. The first transport entry in each generation
refreshes both producers in place. Private bookkeeping storage is reused with
fresh pools.
Retained leases keep their original pools, so their release cannot free slots
in the new pool.
Retained values keep their aliases until their final owner drops. Reusing
Expand All @@ -423,93 +418,89 @@ lengths as untrusted.
size before the host writes to them.
* G2H control and external values are copied into host owned storage before
host code receives them.
* Capture and load require the canonical transport state described below.
* Loaded ring images must match the deterministic bootstrap state described below.

## Snapshot checkpoint
## Snapshot capture

The transport arena lives in scratch and is not captured as ordinary guest
memory. Guest producer and pool bookkeeping is normal guest state, while ring
and pool bytes live in scratch. Snapshot capture needs a canonical transport
state. Retained aliases map live payload pages outside the scratch map, so
capture copies them as ordinary memory.
and pool bytes live in scratch. Capture prepares deterministic bootstrap images
without reading the live rings. Retained aliases map live payload pages outside
the scratch map, so capture copies them as ordinary memory.

`Sandbox` tracks whether queue traffic occurred after the last
canonical boundary. A cached or clean snapshot needs no VM entry. A dirty
snapshot uses this flow:
Capture requires a stopped VM with no unfinished guest calls or live host
chain handles. It runs entirely on the host. An unchanged sandbox can reuse
its cached snapshot. Building a fresh snapshot follows this flow:

```text
Host Guest
Host Guest (stopped)
| |
| mailbox = CheckpointPending |
| H2G SnapshotCheckpoint ------------>|
| enter VM |
| | reclaim completed G2H work
| | reset G2H producer
| | reset H2G producer
| | prefill free H2G slots
| | mailbox = CheckpointComplete
|<------------------------------------| halt
| reset both consumers |
| require CheckpointComplete |
| read and validate ring images |
| capture memory |
| prepare bootstrap images |
| capture memory and retained data |
| install captured memory |
| install bootstrap rings |
| attach fresh host consumers |
```

Checkpoint preparation keeps retained leases and aliases intact without
payload copying. Capture leaves the source queues and allocator ready for
continued use, including when memory capture fails.
Bootstrap images stay separate from live scratch until memory
capture succeeds. A failed memory capture leaves the source transport usable.

The canonical state is:
The bootstrap state is:

* G2H is empty at cursor zero.
* H2G starts at cursor zero with one writable descriptor per free slot,
bounded by queue size. Each descriptor names a distinct, configured-size
slot aligned relative to the pool start. Available descriptors form a prefix
followed by zeroed descriptors.
* Guest producer and pool bookkeeping matches the rings and current leases.
* Driver and device event suppression is normalized.
* H2G starts at cursor zero with one writable descriptor per configured pool
slot, bounded by queue size. Descriptor IDs and buffer addresses follow
fresh allocation order. Unposted descriptors are zero.
* Driver and device notifications are enabled at offset zero.
* Host consumers start at cursor zero.

The snapshot stores normal guest memory plus the two canonical ring images.
Construction and loading validate the ring images against the finalized
layout. The layout and copied ring images remain immutable.
The snapshot stores normal guest memory plus the two generated ring images.
Loading requires an exact match to the shared initializer for the finalized
layout. Partial prefill and reordered IDs or buffers are rejected.
The layout and ring images remain immutable.
The OCI representation places ring images in the
[transport layer](./snapshot-oci-format.md). Retained payloads use the ordinary
memory layer through their aliases. The mailbox and host consumer cursors are
not stored.

### Restore

Capture and load validate ring lengths and canonical transport state against
the layout. Load also checks the scratch size the transport blob records.
Load validates ring lengths and bootstrap state against the layout. It also
checks the scratch size the transport blob records.
Admitted images and their layout remain immutable.

Transport admission precedes changes to sandbox status, the cached snapshot,
and memory mappings.

Restore writes the arena GPA metadata, a `CheckpointComplete` mailbox value,
Restore writes the arena GPA metadata, an `InitComplete` mailbox value,
and both ring images into fresh scratch. It attaches new host consumers at
cursor zero. Normal guest memory restores the matching producers, pools,
leases, and aliases.
cursor zero. Normal guest memory restores the captured producers, pools,
leases, and aliases. The first entry in the new generation refreshes producer
bookkeeping in place with fresh pools.
Initialized `restore` and `from_snapshot` are ready for the first H2G request
before guest entry. Pre-initialization snapshots use normal guest startup.

The first request after restore must fit the H2G capacity posted at checkpoint.
Retained buffers can reduce this prefill if their leases still belong to the
current pool generation. Buffers from retired generations do not consume current
pool slots. Framing, slot rounding, ring size, and the external-byte control
reserve still apply.
The first request can use the full configured H2G bootstrap capacity, regardless
of retained buffers. Framing, slot rounding, ring size, and the external-byte
control reserve still apply.

The first transport entry after restore recycles slots held by captured
leases. Posted chains keep their reservations, preserving the submitted
request. Descriptors and cursors stay unchanged. Retained aliases keep their
captured mappings without payload copying.
Guest entry uses `VirtqProducer::refresh` for both queues before
logging or receiving. `BootstrapInfo` holds a `PoolLayout` and an initial posted count.
`SlotPool::layout()` supplies geometry independent of live allocations.
Reconstruction uses bootstrap capacities, not consumer-written lengths.
Host completions and payloads stay untouched. Producers prepare fresh pool
reservations and private table capacity before releasing old queue-owned records.
Retained aliases keep their captured mappings and original leases.
Guest execution cannot resume if either producer refresh fails.

Source continuation keeps the pool instances and live scratch leases intact.
Checkpointing alone does not detach retained payloads from scratch.
Successful capture installs the same bootstrap in the source sandbox, including
when scratch size stays the same. Source continuation uses the same generation
transition as restore and cloning. Ordinary entries within a generation keep
their active queues.

Result completion prefills free H2G slots. This requires no preparatory guest
entry or application warmup for the checkpoint-posted capacity.
Result completion prefills free H2G slots. Retention within a generation still
occupies pool slots. Full bootstrap capacity requires no preparatory guest entry
or application warmup after capture or restore.

## Transport mailbox

Expand All @@ -518,16 +509,14 @@ alignment gap. It is outside both rings and pools. Both sides derive its
address from trusted arena geometry. The host accesses it before VM entry and
after guest halt.

The mailbox avoids a G2H checkpoint response. G2H can remain empty in the
canonical image even when retained G2H slots reduce available capacity.
The mailbox reports initialization readiness without a G2H response.

Before a dirty checkpoint, the host writes `CheckpointPending`.
The guest writes `CheckpointComplete` after producer reset and free-slot H2G prefill.
The host reads the value after a successful guest halt and after resetting
both consumers.
Before initialization, the host writes `InitPending`. The guest writes
`InitComplete` after user initialization and trace flushing. The host reads
the value after a successful guest halt. Queue cursors remain unchanged.

* `CheckpointPending` (`0`) is a fatal incomplete checkpoint.
* `CheckpointComplete` (`1`) permits snapshot capture.
* `InitPending` (`0`) is a fatal incomplete initialization.
* `InitComplete` (`1`) permits normal guest calls.
* Every other value is a fatal invalid status.

## Placement and relocation limitations
Expand Down
8 changes: 4 additions & 4 deletions src/hyperlight_common/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ impl QueueDims {
}
}

/// Addresses of both rings, the checkpoint mailbox, and pools in one fixed arena.
/// Addresses of both rings, the initialization mailbox, and pools in one fixed arena.
///
/// The G2H ring begins at the arena base. The H2G ring is descriptor aligned.
/// The mailbox is `u64` aligned. Both pools are page aligned.
Expand All @@ -225,7 +225,7 @@ pub struct TransportArena {
g2h_ring_addr: u64,
/// Address of the H2G ring.
h2g_ring_addr: u64,
/// Address of the snapshot checkpoint mailbox.
/// Address of the initialization mailbox.
mbx_addr: u64,
/// Address of the G2H pool.
g2h_pool_addr: u64,
Expand Down Expand Up @@ -291,12 +291,12 @@ impl TransportArena {
self.h2g_ring_addr
}

/// Address of the snapshot checkpoint mailbox.
/// Address of the initialization mailbox.
pub const fn mbx_addr(&self) -> u64 {
self.mbx_addr
}

/// Byte offset of the snapshot checkpoint mailbox from the arena base.
/// Byte offset of the initialization mailbox from the arena base.
pub const fn mbx_offset(&self) -> usize {
// `new` proves this difference is nonnegative and fits in `usize`.
(self.mbx_addr - self.g2h_ring_addr) as usize
Expand Down
32 changes: 8 additions & 24 deletions src/hyperlight_common/src/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ pub enum MsgKind {
Cancel = 0x05,
/// A guest log message (GuestLogData payload follows).
Log = 0x06,
/// Internal request to prepare canonical transport state for snapshotting.
SnapshotCheckpoint = 0x07,
}

impl TryFrom<u8> for MsgKind {
Expand All @@ -46,7 +44,6 @@ impl TryFrom<u8> for MsgKind {
0x04 => Ok(Self::StreamEnd),
0x05 => Ok(Self::Cancel),
0x06 => Ok(Self::Log),
0x07 => Ok(Self::SnapshotCheckpoint),
other => Err(other),
}
}
Expand All @@ -56,10 +53,10 @@ impl TryFrom<u8> for MsgKind {
#[repr(u64)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, bytemuck::CheckedBitPattern, bytemuck::NoUninit)]
pub enum MailboxValue {
/// Guest preparation is outstanding.
CheckpointPending = 0,
/// Both queues are reset and free H2G slots are prefilled.
CheckpointComplete = 1,
/// Guest initialization is outstanding.
InitPending = 0,
/// Guest initialization has completed.
InitComplete = 1,
}

impl MailboxValue {
Expand All @@ -79,8 +76,8 @@ impl TryFrom<u64> for MailboxValue {

fn try_from(value: u64) -> Result<Self, Self::Error> {
match value {
0 => Ok(Self::CheckpointPending),
1 => Ok(Self::CheckpointComplete),
0 => Ok(Self::InitPending),
1 => Ok(Self::InitComplete),
other => Err(other),
}
}
Expand Down Expand Up @@ -159,19 +156,6 @@ impl<'a> EncodedMessage<'a> {
})
}

// Build a snapshot checkpoint message with no payload.
pub fn new_snapshot_cp() -> Self {
let total_len = MsgHeader::SIZE;
let externals = ExternalValues::new();

Self {
header: MsgHeader::new(MsgKind::SnapshotCheckpoint, 0, 0, 0),
control: &[],
externals,
total_len,
}
}

/// Borrow the complete wire message as a zero-copy byte cursor.
pub fn as_buf(&self) -> impl Buf + '_ {
EncodedMessageBuf::new(
Expand Down Expand Up @@ -460,8 +444,8 @@ mod tests {
#[test]
fn mailbox_value_wire_contract() {
for (value, raw) in [
(MailboxValue::CheckpointPending, 0u64),
(MailboxValue::CheckpointComplete, 1),
(MailboxValue::InitPending, 0u64),
(MailboxValue::InitComplete, 1),
] {
assert_eq!(value.raw(), raw);
assert_eq!(value.as_bytes(), raw.to_ne_bytes());
Expand Down
Loading
Loading