An experimental port of NetBSD, FreeBSD, OpenBSD, and XNU to unkernel unkernel.dev
unkernel native README.md

Native kernel host #

The optional unkernel-native CLI runs single-CPU and SMP kernels with Wasmtime on a 64-bit Unix host. Build it with Rust and Cargo. Preparing kernels also requires Binaryen's wasm-opt on PATH. Node and JSPI are only needed by the JavaScript host.

From the repository root, build once and boot any project root disk by giving the kernel Wasm and the root filesystem image:

cargo build --release --locked
runner=target/release/unkernel-native

# Alpine Linux
"$runner" boot vendor/build/bundle/unkernel.wasm vendor/build/distro/alpine.erofs
# Debian Linux
"$runner" boot vendor/build/bundle/unkernel.wasm vendor/build/distro/debian.erofs
# FreeBSD
"$runner" boot vendor/build/freebsd/freebsd32.wasm vendor/build/freebsd/freebsd.img
# NetBSD
"$runner" boot vendor/build/netbsd/netbsd32.wasm vendor/build/netbsd/netbsd.img
# OpenBSD
"$runner" boot vendor/build/openbsd/openbsd32.wasm vendor/build/openbsd/openbsd.img
# XNU (Darwin)
"$runner" boot vendor/build/xnu/obj/RELEASE_ARM64_WASM/kernel.wasm vendor/build/xnu/root-apfs.img

boot recognizes the kernel, prepares and caches raw Wasm on first use, and selects the root arguments and RAM size. The default is one vCPU. Use --cpus 2 for SMP, --mem 1024 for 1024 MiB of RAM and --jit for guest AArch64 compilation. With the standard project paths, the runner finds the sibling SMP build. Otherwise supply the SMP kernel directly. Linux uses the embedded initramfs or the project's initramfs/base.cpio and mounts a kwroot distro image read-only. A standalone Linux root filesystem boots directly from /dev/wda read-write. BSD and XNU attach writable root disks. For XNU APFS, keep the volume UUID file (IMAGE.uuid) and kexts.kwkc beside the root image.

Networking starts automatically using gvproxy on PATH. Use --net qemu to select qemu-system-aarch64 or --net none to boot offline. Wasmtime compiles the kernel before starting the network helper and caches the native code for later boots. Kernel loading reports progress only if it takes ten seconds. To do the first compilation ahead of time, run "$runner" precompile vendor/build/freebsd/freebsd32.wasm on the boot host. Pass the SMP kernel to precompile when using multiple CPUs. Each session owns its backend and private socket directory. A supervisor reaps that backend and removes the directory when the runner exits, including after a crash or SIGKILL.

For custom kernels or disk layouts, prepare INPUT -o OUTPUT and run OUTPUT expose the initrd, kernel command line and individual disks through --initrd FILE, --append 'ARGS' and repeatable --disk PATH[:ro]. run accepts the same managed backends and defaults to offline.

Architecture #

kwemu emits a hot AArch64regionInstall compiled function in thisCPU’s tablewasm_switch_to andret_from_forkWASI preview 1 and unkernelimportsInput events and idle deadlinesKernel WasmBinaryen Asyncify andexception conversionWasmtime instance pervCPU on its own hostthreadGuest RAM shared acrossvCPUsWasmtime guest JIT(optional)Rust schedulerSeparate unwind memoryand saved task stacksConsole, clocks, initrd, diskand EthernetTokio runtime

The guest scheduler chooses the next task. A switch unwinds the current Wasm call into a second linear memory private to that CPU. The host saves those bytes with the task's entry point and arguments. It calls ret_from_fork for a fresh task or rewinds the saved invocation for a previously parked task. All tasks see the same guest RAM. Only the live frames move on a switch.

Wasmtime executes on a blocking thread. Console and network readers feed a bounded channel. On a single CPU, WASI poll_oneoff waits for input or a clock deadline on Tokio. On SMP it waits on the primary CPU’s shared IRQ word. Both host input and guest interprocessor interrupts notify that word. Raw disks use synchronous positional I/O through the kwblk protocol. The Ethernet option --net-socket PATH connects to a Unix stream carrying QEMU frames (a four-byte big-endian length followed by the Ethernet payload). This option connects to an externally managed backend.

Multiple CPUs #

Prepare the SMP build of the selected kernel and pass the desired CPU count to run. For example, from the repository root:

"$runner" prepare vendor/build/netbsd/netbsd-mp32.wasm -o vendor/build/native/netbsd-mp.wasm
"$runner" run vendor/build/native/netbsd-mp.wasm --cpus 2 --disk vendor/build/netbsd/netbsd.img --append root=ld0a

Linux uses vendor/build/kernel-smp/vmlinux32.wasm and an explicit --initrd vendor/build/initramfs/base.cpio with --append console=hvc0. FreeBSD and OpenBSD use their *-mp32.wasm artifacts. XNU uses vendor/build/xnu/obj-smp/RELEASE_ARM64_WASM/kernel.wasm. Each port caps the number of CPUs it brings online.

The kernel starts each secondary through wasm_cpu_start. All instances reuse the compiled module. CPU 0 verifies the linker's shared-memory initialization flag before starting a secondary at kw_secondary_entry. Device state belongs to CPU 0. Secondary CPUs share the monotonic clock origin and have their own globals, tables, and parked tasks.

Pass --jit to run to enable kwemu's guest AArch64 JIT on each CPU. Wasmtime compiles hot regions emitted by kwemu and installs them in that CPU's kernel function table. Omitting the flag or passing --no-jit uses the interpreter. Each CPU reuses identical compiled regions. Because Wasmtime retains instances for the lifetime of the store, each CPU admits up to 4096 distinct regions or 16 MiB of module input. Once either limit is reached, uncached regions use the interpreter.

Run cargo test --locked with Binaryen installed for the scheduler regression test.