The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.
boxlite-hypervisor
Host backends for BoxLite's VMM: HVF on macOS arm64 and KVM on Linux x86_64
and arm64, with WHP on Windows x86_64 reserved for M10. The Vm, Vcpu and
VcpuHandle traits, with VcpuExit, MemoryRegion and Error, are the
shared contract. Each architecture extends it with the register access boot
needs: X86Vm and X86Vcpu on x86_64, Arm64Vcpu on arm64, where
VcpuExit::CpuOn also carries the PSCI CPU_ON that HVF leaves to the VMM.
The rules each backend keeps are in the
VMM design.
This crate owns host VM/vCPU handles, memory registration, interrupt
injection, and decoding host exits. It is a leaf among the BoxLite crates;
machine layout and device emulation belong to boxlite-vmm.
| Module | Responsibility |
|---|---|
vm |
Vm: memory mapping, vCPU creation, and interrupt lines |
vcpu |
Vcpu and VcpuHandle: running a vCPU, completing pending I/O before stop, and kicking it from another thread |
exit |
VcpuExit: decoded exits and the I/O completion contract |
memory |
MemoryRegion: host memory mapped into the guest |
error |
Error: the failed operation, its resource, and the host cause |
arm64 |
Arm64Vcpu, Arm64BootRegisters and CpuOnResult: the arm64 boot registers and the VMM's answer to CPU_ON |
hvf |
macOS arm64: the VM and its in-kernel GICv3, memory, vCPU execution and kicks, with syndrome decoding exceptions and emulating trapped system registers, and psci serving PSCI 0.2 |
x86 |
Boot configuration and Linux x86_64 VM/vCPU state images with Serde encoding |
kvm |
x86_64 VM creation, memory slots, registers, CPUID/MSRs, execution, kicks, pending-I/O completion and paused-state transfer; arm64 follows |
whp / whp::emulator |
WHP operations and x86 instruction decoding for memory-access exits (reserved for M10) |
Three boundaries decide placement when a case is ambiguous: KVM memory-slot
indices stay inside kvm, because HVF has no such concept; ARM syndrome
decoding stays inside hvf, so boxlite-vmm never sees a raw ESR_EL2; and
x86 instruction decoding stays inside whp, so boxlite-vmm never sees raw
instruction bytes.
Backend status
The backend modules are selected by host OS and architecture, and a host with
no backend fails the build rather than producing a library that exposes no VM
operations. The traits are exported from the crate root. Linux x86_64 also
exports KvmVm and thread-bound KvmVcpu, with creation, memory registration,
run and complete_pending_io. KvmVm and KvmVcpu implement the shared Vm
and Vcpu traits, including IRQ forwarding and cross-thread kick handles.
run blocks on an idle guest until interrupted.
KvmVcpu::set_boot_registers installs X86BootRegisters: general entry registers,
segments, descriptor tables, control registers, and the x87 control word (FCW is
0x037f). A fresh vCPU's reset SSE state has MXCSR 0x1f80. Hardware tests
read MXCSR from the legacy SSE area returned by KVM_GET_XSAVE;
KVM_GET_FPU does not return it.
Values and guest addresses come from the VMM; no Linux memory layout lives in
KVM.
Call it before first entry and discard the vCPU after any configuration error,
because multiple host writes cannot be rolled back atomically. Secondary vCPUs
retain reset state until INIT/SIPI.
KvmVm::supported_cpuid exposes host-supported CPU features. A VMM can remove
features or adjust topology, then pass the result to KvmVcpu::set_cpu_features
before set_boot_registers and first entry.
The initialization order is vCPU creation → CPU features → boot registers → guest entry. KVM validates register state against the guest CPUID, including long-mode support when EFER.LME/LMA are set.
X86CpuidEntry preserves subleaf matching without leaking the
KVM ABI. Legacy stateful CPUID entries are omitted because this type cannot
preserve their read sequence; other unsupported KVM flags are rejected.
MSRs are explicit index/value writes. A short KVM write is an error identifying
the first rejected MSR, never a successful partially configured CPU. Host feature
discovery is a ceiling, not permission to add unsupported instructions.
These boot methods form the x86_64 extension traits X86Vm
(supported_cpuid) and X86Vcpu (set_cpu_features, set_boot_registers),
which KvmVm and KvmVcpu implement, so the VMM's vCPU workers and its mock
backend use one interface. On arm64, Arm64Vcpu sets MPIDR_EL1 (set_mpidr) and
the entry PC, X0 and PSTATE (set_boot_registers).
Linux x86_64 paused state
X86Vm::save_state and X86Vcpu::save_state capture owned images on Linux
x86_64 KVM. Restore requires the same host and KVM environment, with compatible
guest CPU policy and VM configuration. The images have a non-stable ABI and
implement Serde for Postcard encoding. The VMM owns RAM, devices, machine-state
assembly, and persistence.
| Image | Captured state |
|---|---|
X86VmState |
PIC master and slave, IOAPIC, PIT, and KVM clock |
X86VcpuState |
General and special registers, FPU, CPUID, filtered MSRs, host-sized XSAVE, XCRs, debug registers, events, MP state, LAPIC, and TSC frequency |
Save
Keep all vCPUs out of the guest and stop IRQ producers. On each vCPU's owning
thread, call Vcpu::complete_pending_io until it returns Ok(IoCompletion::Done).
Dispatch each Ok(IoCompletion::Exit(exit)) and release its borrow before the
next call. Split MMIO can produce multiple exits for one instruction. A
terminal exit or completion error aborts capture. Both VcpuExit and
IoCompletion are #[must_use]: discarding either after unwrapping its
Result triggers a lint.
Begin the round with X86Vm::save_state, then call X86Vcpu::save_state once
per vCPU on its owning thread. A failed vCPU save can be retried in the same
round. Finish all vCPU saves before starting another round. KVM timers can
advance during capture, and PIT restore can lose sub-tick timing.
Restore
Restore into an identically configured VM with fresh, never-entered vCPUs.
- Register restored guest RAM with
Vm::map_memory. - Call
X86Vm::restore_before_vcpusto set the guest clock in nanoseconds. KVM receivesflags: 0for kernel compatibility and to exclude snapshot downtime. - Create the vCPUs and call
X86Vcpu::restore_stateon each owning thread. The restored clock anchors the pvclock epoch that KVM writes into guest RAM. - Call
X86Vm::restore_after_vcpusafter all vCPUs are restored. It validates IRQ chip IDs, restores the chips, then restores the PIT if present. - Call
Vcpu::notify_guest_stoppedon each owning thread beforeVcpu::run. It succeeds even if KVM pvclock is inactive.
A vCPU restore preflight error permits a retry on the same vCPU. Any failure
from the KVM_SET_CPUID2 attempt onward requires discarding the destination VM.
VM snapshot operations return Error::VmState on failure, and vCPU snapshot
operations return Error::ConfigureVcpu. Both preserve the underlying io::Error,
including its kind and any ioctl errno.
Scope
Nested virtualization, Hyper-V SynIC, MCE bank setup, guest-debug configuration, IRQ routing, and device or irqfd configuration are outside the snapshot images. Capture and restore reject paged 32-bit PAE and CET shadow stack (SHSTK). Keep host XSTATE permissions fixed after vCPU creation.
Capture gates and restore validates CPUID-gated MSRs against guest CPUID. SGX launch key hashes require SGX_LC. IA32_PERF_CAPABILITIES requires PDCM and a nonzero architectural PMU version.
make test:unit:vmmruns unit tests without hardware.make test:integration:vmm:kvmruns restoration tests on Linux x86_64 with/dev/kvm.
Host execution
macOS arm64 exports HvfVm and thread-bound HvfVcpu, which implement Vm,
Vcpu and Arm64Vcpu on Hypervisor.framework with the in-kernel GICv3 of
macOS 15. Design and research: #2103.
- One VM per process.
HvfVm::newcreates the VM, then the GIC at the distributor and redistributor bases the VMM picks, after checking them against the alignment and region sizes HVF reports. A second VM fails withResourceBusy; a missing hypervisor entitlement withPermissionDenied; macOS 12 to 14, which lack the in-kernel GIC, withUnsupported, a path no tested host exercises. The GIC calls are looked up at run time, so a binary that links the backend imports none of them; a unit test checks its own binary withnm -u. - Exits.
rundecodes the exception syndrome itself:- data aborts with a valid syndrome become
MmioReadandMmioWrite. The nextrunorcomplete_pending_iowrites a read's value, sign- or zero-extended by the syndrome, and moves PC past the access. PC advancement wraps at 64 bits: an instruction at0xffff_ffff_ffff_fffccompletes at PC zero even with overflow checks enabled. Register 31 is XZR, never HVF's PC. A cache-maintenance abort is skipped, as in QEMU; - HVC is PSCI 0.2. The backend answers
VERSIONandMIGRATE_INFO_TYPEitself, and any other call it does not act on withNOT_SUPPORTED, without an exit.SYSTEM_OFFandSYSTEM_RESETend the VM, andCPU_ONbecomesVcpuExit::CpuOn, whoseresultthe guest reads back; - trapped system registers are emulated as QEMU does: the OS lock that Linux clears at boot, the other debug registers and the reserved ID space as zero;
- SMC, a data abort without a usable syndrome or from a stage-1 table walk,
and any other exception are
UnhandledExit, with the guest PC.
- data aborts with a valid syndrome become
- Waiting and kicks. HVF waits out WFI itself and wakes the vCPU for its
timer and for SPIs set from any thread, so no host thread parks.
HvfVcpuHandle::kickcallshv_vcpus_exit, which HVF holds until the nextrun. Dropping the vCPU clears the ID a handle kicks before HVF can reuse it.
KvmVcpu::handle() can kick a worker before or during guest entry. Cloned handles
become inert when the vCPU is dropped or its worker exits, even if safe code
forgets the vCPU. KvmVm::new() reserves SIGRTMIN + 1 on each worker, or the
application can select a realtime signal with with_kick_signal. Keep that signal unblocked
and at its default disposition before vCPU creation; do not reuse it while the
vCPU lives. No process-wide handler is installed. KVM temporarily unmasks the
signal during entry, and vCPU drop drains pending kicks before restoring its bit.
Signal validation and reservation run before KVM allocates a vCPU ID, so a caller
can fix a worker-mask error and retry with the same ID. KVM signal-mask setup is
applied after the vCPU fd is created.
Hardware qualification covers kicks queued before entry and kicks sent after
guest code publishes an atomic marker, plus inert handles after vCPU destruction.
make test:unit:vmm checks memory-slot validation and rollback without KVM.
On Linux x86_64, make test:integration:vmm:kvm requires read/write access to
/dev/kvm. It checks VM/interrupt-controller creation, executes instructions
from registered RAM, and replaces an unmapped region. Execution currently uses
the public facade; hardware tests also read back the public register configuration
through KVM to verify long-mode segments, entry addresses, and floating-point state.
Linux x64 CI runs these tests with make coverage:vmm:kvm, adding their coverage
to the unit profiles before upload; missing KVM access is a failure.
On macOS arm64, make test:integration:vmm:hvf runs the HVF hardware tests:
tiny machine-code guests that check MMIO widths and extension, PSCI, the OS
lock, MPIDR, WFI woken by an SPI from another thread, and kicks. Its runner
signs the test binary with com.apple.security.hypervisor, which
Hypervisor.framework requires, and the tests run one at a time because a
process holds one VM. Hosted macOS runners cannot nest virtualization, so these
tests run on a developer Mac.
M1 proceeds in boot order: create the VM and map RAM, create/run vCPUs, load
the kernel and boot metadata, attach legacy devices, then reach a test /init
and return the guest's reset to the caller. Linux x86_64 boots that way; for
macOS arm64 the backend is in place and the arm64 machine and first boot
follow (#2103). Linux
arm64 comes later.
References for this step:
- kvm-ioctls
src/lib.rs:39–56demonstrates VM creation, memory registration and a tiny machine-code guest. - Firecracker
arch/x86_64/vm.rs:170–183creates the in-kernel irqchip and PIT before any vCPU. - libkrun
linux/vstate.rs:446–476checks the KVM API version and capabilities before constructing a VM.
From the repository root, run make vmm, make clippy:vmm or
make test:unit:vmm. See the VMM README for cross-target
build commands.