A data disk records the architecture of the image that uses it. One written by another architecture (a riscv64 disk on the aarch64 guest) loses its installed packages and system changes once, with a notice on the console and the page; root's home is kept.
11 KiB
AGENTS.md
unkernel runs Linux 7.3, compiled to a single wasm module (arch/wasm with an
MMU), in Node 24 or a browser with JSPI. User space is unmodified aarch64
Linux ELF (ARMv8.0-A with FP, AdvSIMD, AES, PMULL, SHA1, SHA2, CRC32, LSE,
SHA512, SHA3), run
by kwemu: a Zig interpreter with a wasm JIT tier, linked into the kernel.
README.md is the overview, and kernel/README.md covers the kernel side,
the devices and the guest image.
kwemu's guest CPU is AArch64 (src/a64, .agents/a64.md: a decoded-op tier
over a reference interpreter, and a JIT that compiles hot blocks to
wasm), on an ISA-neutral MMU, block cache and JIT core (src/emu,
.agents/kwemu.md); its contract with the kernel is include/kw_emu.h
(ABI 2). The kernel presents the arm64 user ABI (kernel.md, arm64 user
ABI), and the guest image is Alpine's aarch64 minirootfs
(kernel/README.md, alpine).
.agents/ has one file per area. Read the one for the area you change:
| File | Covers |
|---|---|
| .agents/kernel.md | kernel tree and patch workflow, kw_emu.h contract, memory model, JSPI, host imports, IRQs, arm64 user ABI, config |
| .agents/a64.md | the AArch64 CPU (src/a64): layout, adding instructions, shared helpers, the decoded-op tier and its op set, coherence on SMP, tests, coverage tool, numbers, the JIT (family API, flags contract, tests) |
| .agents/kwemu.md | the ISA-neutral core: MMU and TLBs, block cache, JIT core (regions, invariants, SMP, host contract), softfloat, measured numbers, rejected designs |
| .agents/js.md | the SDK (js/): rules, build checks, disks, network stack and Wisp |
| .agents/web.md | the browser page, releasing its kernel, engine quirks |
| .agents/data-disk.md | the persistent data disk, overlay root, safe mode, apk handling |
| .agents/guest-packages.md | busybox, zlib, openssl, apk-tools rebuilt for kwemu (alpine-opt.sh): build, numbers, traps |
| .agents/toolchain.md | patched wasm-ld, Binaryen, Zig 0.16, host tools, pinned downloads |
| .agents/testing.md | the full test matrix, for releases or when asked |
| .agents/status.md | roadmap, open issues, work not yet in the image |
Host safety rules (read first)
A binfmt_misc handler with a truncated magic can match every 64-bit ELF and stop exec on the whole host until a reboot. So:
- Never register binfmt_misc handlers. Never write under
/procor/sys. Do not usemount,chroot,unshareorsysctl, and do not change services. - No
sudoand no package installs. - Run binaries of another architecture (the aarch64 guests and test
programs, or any other) on the host only through an explicit emulator
(
qemu-aarch64 ./bin). Never run the guest's/etc/kw-binfmt.sh, or any part of it, on the host. - Do not start anything on port 8080: that is
pnpm dev's port. - Repeat these rules in every subagent brief.
Layout
The parts that ls does not make obvious:
src/is kwemu, with its contract ininclude/kw_emu.h.guest/holds its test guests.kernel/build.shruns every kernel-side step (toolchain binaryen kernel smp lower alpine disk datadisk bundle all, plusubuntuand the profiling variantstats). One tree gives two kernels, UP (kernel) and SMP (smp); the bundle carries the UP kernel and a patch to the SMP one (kernel.md, Two kernels).KSRC,KOUT,KOUT_SMPandKW_BUILDpoint it at another tree and output root. Linux patches are inkernel/patches/linux/, wasm-ld patches inkernel/patches/llvm/.vendor/(gitignored) holds the kernel treevendor/src/linux-wasm-kernel(branchkw-arm64-user),vendor/src/llvm22, the toolchain and all build outputs. Those branches exist only there, sokernel/patches/is their only durable record.- The page's kernel is the gitignored
web/kernel/unkernel-alpine.wasm.kernel/build.sh bundlecopies its output there, andpnpm devruns that file as it is (its copy inweb/kernel/page/, without an SMP patch section). A page build (pnpm build:web, a deploy) replaces it with the newest GitHub release's bundle (web/scripts/kernel.ts, web.md).
Building
./setup.sh # check prerequisites, then zig build + kernel/build.sh all
zig build test # A64 unit tests (native, wasm64 + JIT), A64 guests (native, wasm64, + JIT) against qemu-aarch64
kernel/build.sh kernel # the UP kernel: kwemu with the JIT, vmlinux, lower, refresh the bundle
kernel/build.sh smp # the SMP kernel (kwemu with atomics); the bundle then gets the SMP patch
pnpm install # SDK and page dev dependencies
node js/cli.ts # boot Alpine from the bundle; --help lists options
CLI exit codes: 0 = power off, 1 = halt or boot failure, 2 = restart,
124 = --timeout, 253 = panic, 254 = wasm trap, 255 = fatal.
Easy to break
Details are in the area files.
include/kw_emu.his copied into the kernel by build.sh. BumpKW_ABI_VERSIONon any layout or semantic change (kernel.md).- Only
wasm_switch_to,poll_oneoff,sched_yieldandproc_exitmay suspend under JSPI (kernel.md). - Every kernel thread needs a
kw_park_point()in its loop, or parking (and so snapshots) fails with EBUSY naming it (kernel.md, Parking). - JIT output must match the interpreter exactly (kwemu.md, Invariants; a64.md, JIT).
- SMP kernels: a task moves to another CPU only where its JSPI stack (that CPU thread's) can be dropped and it can start on a fresh one: a user task on its way to user mode, a kernel thread at a restart point. Host imports other than clocks, randomness, console output and task switching answer on CPU 0 alone, and a kernel thread that is bound to another CPU after it ran needs a restart point (kernel.md, SMP).
kw_defconfigis maintained by hand. Kconfig drops options silently, so check$KOUT/.configafter a change.js/corestays universal: nonode:imports, no unguarded DOM APIs (js.md).- The image's busybox, zlib, openssl and apk-tools are rebuilt from
pinned aports commits (
kernel/guest/alpine-opt.sh;KW_ALPINE_OPT=0keeps Alpine's builds), without FP or AdvSIMD code in hot paths and with libcrypto on its integer code. An Alpine update needs new pins there;alpine.shstops until they match (guest-packages.md). - Guest C programs in the image (
kernel/guest/*.c) are built for the generic ARMv8.0 CPU. Check new code with the coverage classifier (a64.md, Coverage tool): it lists what kwemu rejects (later architecture features: SVE, SM3, SM4, ...). - Overlay root and data disk rules are in data-disk.md. Never use
switch_root, and never clear the IndexedDB storekwdisk:kwdata-v1. The one sanctioned discard is the guest's reset of a disk written by another guest architecture, which deletes all but root's home (data-disk.md, Guest architecture).
Changing things
- Kernel: commit on
kw-arm64-userinvendor/src/linux-wasm-kernel, then export so thatkernel/patches/linux/equalsformat-patch -N 7595a62d45e..kw-arm64-user(kernel.md). - wasm-ld: commit in
vendor/src/llvm22, runkernel/build.sh toolchain, and re-export (toolchain.md). - Guest image:
kernel/build.sh alpineorkernel/build.sh disk. - Page: a kernel or guest change reaches the deployed page with the
next release (
.github/workflows/release.yml, run by hand).pnpm devruns it as soon askernel/build.shhas rebuilt the bundle.
Tests
Run the quick check for what you changed. The full matrix in .agents/testing.md is for releases, or when the user asks for it.
| Change | Quick check |
|---|---|
Emulator (src/, guest/, include/) |
zig build test |
Kernel, kw_emu.h, JIT |
kernel/build.sh kernel; node js/cli.ts --append "console=hvc0 rdinit=/usr/bin/kwtest" (kwtest: 0 failure(s), exit 0); for JIT changes, again with --no-jit |
| Guest image, anything user space sees | pnpm test:api; optional: node js/cli.ts --timeout 120 --input '{8}uname -a; apk --version\n{2}poweroff\n' |
| Display, input, sound (kwfb, kwinput, kwsnd) | pnpm test:api; optional: node test/glimmer-check.mjs --run |
| Block, ext4, data disk | pnpm test:block |
SDK (js/) |
pnpm typecheck; pnpm test:api |
Host file systems (9p, kw9p) |
pnpm test:9p; pnpm test:api (after a kw9p change, kernel/build.sh alpine first) |
Inbound to the guest (vm.connect, vm.fetch, kwtty CONNECT, --forward) |
pnpm test:http; pnpm test:tty; pnpm test:api (after a kwtty change, kernel/build.sh alpine first) |
| Network | pnpm test:net |
SMP (CONFIG_SMP, js/core/cpu.ts; kernel.md, SMP), or any kernel change that SMP code sees |
kernel/build.sh smp (after kernel, so that the bundle carries the patch); kwtest with --smp 2 (the SMP kernel from the bundle) and once with --kernel vendor/build/kernel-smp/vmlinux32.wasm --initrd alpine-test (one CPU); zig build test -Dkwemu-atomics=true after a kwemu change |
Bundle and kernel patch (bundle.mjs, kwdiff.mjs, kpatch.ts, kremap.ts) |
KPATCH_UP=vendor/build/kernel/vmlinux32.wasm KPATCH_SMP=vendor/build/kernel-smp/vmlinux32.wasm pnpm test:kpatch |
Page (web/) |
pnpm typecheck:web; pnpm build:web |
The optional checks boot the same bundle as pnpm test:api, which already
runs shell commands and glimmer --test inline and in worker mode. Run them
when the change touches what only they cover. The Alpine boot adds apk and
the CLI's --input. glimmer-check compares the whole frame pixel for
pixel and the sound sample for sample, and checks the mouse and tablet
events and the CLI's --display, --sound-wav and --worker. Both are
still part of the full matrix.
Do not run the browser tests (web/test/) unless asked. The page is
tested by hand with pnpm dev.
To add a test: for the emulator, a unit test in src/a64/tests.zig or a
guest in guest/a64/ plus entries in build.zig (a64_guests) and
test/a64-test.mjs; for the kernel, a CHECK(...) in
kernel/guest/kwtest.c.
Conventions
- Commit messages are imperative sentences, optionally prefixed with an
area (
web:,CLI:). Kernel patches use kernel style (wasm: ...); a fix is folded into the patch it belongs to, a new piece is a new patch (kernel.md). Do not addSigned-off-bylines; only a human author can certify one. - Every new file gets an SPDX header and a copyright line. Project code is
GPL-2.0-or-later, the JavaScript (
js/,web/, SDK tests) is MIT, and kernel patches are GPL-2.0-only. Never copy code from linux-wasm's GPL runtime intojs/. - When a change makes something here or in
.agents/wrong, fix it in the same commit. The docs describe the current state: no change history and no commit hashes. Date measurements (absolute dates).