- OCaml 62%
- C 28%
- Shell 10%
- Raku <1%
ocaml-solo5 #
Build, inspect and run Solo5 hvt
unikernels from OCaml, on macOS as well as Linux.
A unikernel is a single application linked with the operating-system library
code it needs into one image. The image runs directly on a hypervisor, in one
address space, with no processes and no system calls. Solo5's hvt
(hardware-virtualised tender) target runs such a guest in a hardware virtual
machine. A host process called the tender owns that machine and services the
guest's few hypercalls. Upstream Solo5 ships a tender written in C for
Linux/KVM, FreeBSD/vmm and OpenBSD/vmm, and none for macOS.
This repository is the OCaml side of hvt. It has the ABI and the on-disk note
formats as wire codecs, a tender
core that does not depend on the hypervisor, and a CLI that generates and
inspects unikernel manifests without a C toolchain. It also has two tenders
that boot the same aarch64 hvt unikernels. solo5-hvf runs on macOS on
Apple silicon through Hypervisor.framework, which upstream Solo5 does not
support. solo5-kvm runs on Linux through KVM and does in OCaml what the
upstream C tender does.
The two tenders share all the OCaml code above the boundary with the hypervisor and guest memory: ELF loading, manifest and ABI decoding, aarch64 page tables and the loop that dispatches hypercalls. Each backend adds only a few hundred lines of C that call the host hypervisor.
On aarch64 the guest makes hypercall nr with a 32-bit store to the
guest-physical address 0x100000000 + 8 x nr (Solo5_abi.Abi.hypercall_address).
That window is unmapped in the second-stage page tables, so the store faults
out of the virtual machine. The tender then services the call (walltime, puts,
poll, block and net read and write, halt) and reads its arguments from the
guest memory that the stored value points to.
The unikernel binary lists the devices it needs in the MFT1 manifest, an ELF note of up to 64 entries of 104 bytes each. An entry holds a device name of at most 67 characters and a type tag, 1 for a block device and 2 for a network device. At boot the tender binds each entry to a host resource, and it attaches nothing the manifest does not name.
Contents #
Packages #
| Package | What it is |
|---|---|
solo5-abi |
The Solo5 hvt ABI and on-disk note formats as wire codecs, no I/O beyond ELF parsing: hypercall numbers, boot info and argument structs, the MFT1 application manifest, and the Solo5 ABI note. Defined once, shared by the tenders and the tooling. |
solo5-tender |
The backend-independent core shared by every tender: guest-memory access, the ELF loader, the manifest attach, the net/block/virtio device models, and the aarch64 hvt arch glue (page tables, CPU setup, the hypercall run loop) as a functor over a hypervisor backend. A backend supplies the machine boundary and drives this core. |
solo5-tool |
The solo5 CLI: generate the MFT1 manifest C source the loader reads, emit the OCaml/Solo5 startup glue, set up the cross toolchain, and inspect a built unikernel's ABI and manifest notes. None of these needs a C toolchain or runs the unikernel. |
solo5-hvf |
A Solo5 hvt tender for macOS on Apple silicon. The Hypervisor.framework boundary (vCPU, guest memory, exits) is a small C stub; the page tables, register setup, and hypercall loop come from solo5-tender. |
solo5-kvm |
A Solo5 hvt tender for Linux (aarch64) on KVM. The /dev/kvm boundary is a small C stub of KVM ioctls; everything above it is the same solo5-tender core solo5-hvf uses. |
solo5-slirp |
The unprivileged user-mode network gateway behind --net=slirp, run as a separate process so the tender itself never opens a host socket. It answers the guest's ARP, leases it a DHCP address and terminates its outbound TCP, UDP and ICMP on ordinary host sockets. |
solo5-bindings |
The freestanding aarch64 Solo5 platform binding for Xen/ARM (solo5_xen.o), which upstream Solo5 provides for x86_64 only. |
Installation #
Install with opam:
$ opam install solo5-tool
The package may not be in the public opam-repository yet. If opam cannot
find it, add the overlay repository first:
$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install solo5-tool
The libraries and the solo5 CLI are plain OCaml and build on any host with
dune build solo5-abi solo5-tender solo5-tool. Each tender builds only on its
own host, dune build solo5-hvf on macOS on Apple silicon and
dune build solo5-kvm on Linux on aarch64. solo5-hvf calls hv_vm_create,
which requires the com.apple.security.hypervisor entitlement. The build signs
it ad hoc with that entitlement, so the binary runs straight after the build.
solo5-kvm needs read and write access to /dev/kvm and nothing more.
Usage #
To build a unikernel, first run solo5 up, which installs the freestanding
cross toolchain for your host. Dune then cross-builds the unikernel in the
solo5 context, into _build/default.solo5/. A plain dune build only builds
for the host, so pass -x solo5:
solo5 up
dune build -x solo5 path/to/unikernel.hvt
The solo5 CLI reads the ABI and manifest notes out of the ELF file, without a
C toolchain and without running the guest:
solo5 query path/to/unikernel.hvt # the Solo5 ABI note and the device manifest
solo5 size path/to/unikernel.hvt # ELF section sizes
solo5 manifest manifest.json out.c writes the C source of the MFT1 manifest
that the Solo5 loader reads, and solo5 startup writes the C entry point that
starts OCaml on Solo5. mirage dune generates the dune rules that call them
for each unikernel.
The two tenders take the same flags, and a guest boots on either without change:
# Boot a networked unikernel and forward host port 8080 to its port 80.
_build/default/bin/main.exe \
--mem=64 --net=slirp -p 8080:80 path/to/unikernel.hvt
# The same guest on Linux, through /dev/kvm.
_build/default/bin/main_kvm.exe --mem=64 path/to/unikernel.hvt
--net=slirp starts the solo5-slirp gateway next to the tender. The gateway
answers the guest's ARP, gives it an address over DHCP and carries its TCP, UDP
and ICMP on ordinary host sockets. It needs no root and leaves the host's
routing alone. -p HOST:GUEST forwards a host port to the guest.
--watchdog=SECS powers off a guest that runs for SECS without one VM exit.
A VM exit is the hardware passing control from the guest back to the tender,
on a hypercall fault, an interrupt or a trap, and a guest that loops without
one never lets the tender act. A guest blocked in a long POLL is waiting on the
host side, so the watchdog leaves it alone. --disk=IMAGE attaches a raw image
as a block device, and --help lists the other flags.
A Linux VM under Virtualization.framework on Apple silicon can open /dev/kvm
through nested virtualisation. That VM runs solo5-kvm while solo5-hvf runs
on the macOS host.
Security #
The guest ELF and the hypercall data may come from an attacker. The tender
bounds-checks every offset and length the guest supplies (Guest.region and
the other Guest accessors), the loader validates the ELF segments, and the
lengths of puts and net calls are capped. A hostile or broken guest is
stopped with a diagnostic and exit code 2. The tender does not crash with an
uncaught backtrace, and it writes no host memory outside the guest. Pages of
the loaded image are write-xor-execute: code is read-only and executable, data
is writable and not executable.
Before it enters the guest, the tender locks itself into a sandbox that denies
by default. On Linux, solo5-kvm installs a seccomp allowlist with the same
system calls as upstream Solo5's hvt_seccomp_linux.c, and narrows four of
them by argument: ioctl to KVM_RUN, tgkill to the watchdog's SIGURG,
and mmap and mprotect to calls without PROT_EXEC. On macOS, solo5-hvf
applies a Seatbelt profile that allows no Mach service lookup and no IOKit
user client, since the VM is created before it and a running guest needs
neither. A guest that took over the tender could not start a process, create
a file, open a socket or reach a host service; on Linux, it could not make
memory executable either. With --net=slirp, the host sockets
belong to the separate solo5-slirp process, so the tender's sandbox does not
need to allow them.
The tender assumes a single tenant. --disk opens any host path read-write,
and with --net=slirp the guest can reach any host the gateway can. Untrusted
tenants would need disk paths limited to images they own and an allowlist of
destinations.
Status #
solo5-hvf works on Apple silicon and has tests with hostile guests. It boots
real aarch64 hvt unikernels that use puts, walltime, poll and halt.
It enforces W^X, attaches block devices and slirp networking, and runs in its
sandbox. It powers off a runaway guest when --watchdog is set, and it stops
on SIGINT or SIGTERM as upstream Solo5 does. solo5-kvm drives the same core
through /dev/kvm. It passes the same boot harness on any machine where KVM is
available, which means a Linux host on aarch64 or a Virtualization.framework
guest on Apple silicon.
These are not implemented yet:
- A
vmnetnetworking backend for bridged layer-2 networking. Slirp works without root.vmnetneeds either root or the restrictedcom.apple.vm.networkingentitlement, and an ad-hoc signature cannot carry that entitlement, so it can only be built where one of the two is available. - Multi-tenant authority and resource policy (see Security).
- Tickless idle through the virtual timer. A guest with nothing to do would arm the ARM generic virtual timer for its next deadline and wait for the interrupt, so that it causes no VM exit before then.
dune test runs the unit tests. Most of them do not touch a hypervisor, so
they run on macOS and on Linux. The boot tests are in test/boot/.
run.sh boots through Hypervisor.framework and run_kvm.sh through KVM.
run_kvm_vz.sh runs the KVM test from a macOS host, inside a Linux VM with
nested virtualisation. Each script checks its platform first and reports SKIP
elsewhere, so dune build @boot and @boot_kvm can run on any machine.
The benchmarks in bench/ time the hypercall, device-model and
virtio-ring code without a hypervisor (dune exec bench/bench.exe).
bench/capture.sh records VM-exit, probe and memtrace
traces from a running solo5-hvf.
Related work #
- Solo5 has the C
hvttender for Linux/KVM, FreeBSD/vmm and other hosts.solo5-kvmdoes in OCaml what its KVM backend does. Upstream has no Hypervisor.framework backend;solo5-hvfis one. - ocaml-solo5 is the freestanding OCaml cross-compiler that builds the unikernels these tenders run.
A verbatim copy of the Solo5 headers is kept in reference/, with
its origin in reference/PROVENANCE. lib/abi
rewrites them by hand as wire codecs and is compared with them whenever
upstream changes. The same structs are also written out as
EverParse 3D specs, one .3d file per struct. EverParse, from Microsoft,
reads a 3D (Dependent Data Description) file that describes a binary format
and generates C parsing code. That code is proven in F*, a proof-oriented
programming language, to accept exactly the bytes the description allows and
never to read out of bounds.
License #
ISC. See LICENSE.md. Vendored Solo5 ABI definitions are ISC
(Solo5 contributors).