TinyARM

TinyARM is a lightweight AArch64 emulator in Zig with virtio, 9p, networking, image-converter, benchmark, and browser demo tooling for the current AArch64 path.

Build

make

This writes:

  • zig-out/bin/tinyarm
  • zig-out/bin/splitimg-zig
  • zig-out/bin/build_filelist-zig

Embedding

zig build lib produces zig-out/lib/libtinyarm.a and zig-out/include/tinyarm.h. The library exposes the CLI as one C function, tinyarm_main(argc, argv, envp), which runs a VM to completion and returns the exit status instead of exiting the host process. Cross-compile it with -Dtarget, e.g. -Dtarget=aarch64-ios.

Multi-core

By default tinyarm emulates one core on one host thread. -smp N emulates a chip with N cores (up to 64), each on its own host thread, sharing guest RAM; the guest sees one socket with N cores. Embedders pass the same flag in tinyarm_main's argv.

tinyarm -smp 10 -config pack/config -- nproc

A multi-core machine gets a GICv3 (GICv2 addresses at most 8 cores), PSCI CPU_ON/CPU_OFF (CPU hotplug works) and per-core timers. Guest atomics, both LSE and exclusive load/store loops, run as host atomics, and barriers as host barriers. Broadcast TLB invalidations reach the other cores, and code that one core writes is seen by the others after an ISB, as on hardware. Snapshots need a single-core machine.

  • -host-qos interactive|initiated|default|utility|background sets the Darwin QoS class of the core threads. iOS and macOS offer no CPU affinity, so this is the only control over performance and efficiency cores: background keeps threads on the efficiency cores, and the higher classes prefer the performance cores.
  • TINYARM_SMP_STATS=1 prints per-core statistics at exit: instructions, busy time, interrupts, TLB shootdowns and waits, and which host CPUs ran each core's thread; a -Druntime-profile=true build adds the decoded-block cache refills and how many of them evicted another block.

guest-probes/smp-torture.c (built by guest-probes/build-smp-torture.sh) checks the guest-visible guarantees: atomics, memory ordering, TLB shootdown, cross-modifying code and fork under load. smp-stress.sh runs stress-ng with --verify, and smp-bench.sh runs thread-scaling benchmarks. TINYARM_SMP=N runs the acceptance gates multi-core.

Image Packs

Build the AArch64 microterm image pack:

make microterm-pack-aarch64

The default source image is docker.direct/aarch64/alpine/microterm. The default kernel is stock Linux 7.3, built from the kernel.org release tarball with layers-converter/assets/config/tinyarm/linux_arm64_config (patches, if any, in layers-converter/assets/patches/linux/).

The pack includes a TinyARM config at pack/config:

machine: "aarch64"
kernel: "bzImage"
drive0: { file: "rootfs.bin" }

Relative paths resolve against the config file's directory, so a pack can be used in place (tinyarm -config path/to/pack/config) or mounted at /pack.

Consoles and ports

The guest's byte streams to the host are channels (include/tinyarm.h):

  • consoles: N (default 8, at most 16): single-port virtio consoles, /dev/hvc0.. in the guest, channels 0..N-1. hvc0 is the kernel console. Stock Linux has 8 hvc lines, so more than 8 needs a patched kernel.
  • ports: N (default 0, at most 64): a multiport virtio console whose ports the host adds and removes while the guest runs (tinyarm_port_open, tinyarm_port_close, or O and C MTMUX frames on stdin), channels 64 + n. In the guest a port is /dev/vportXpN, found by name in /sys/class/virtio-ports/*/name; a terminal runs a shell on a pty and copies its bytes to and from the port. Ports appear in the VM's devtmpfs, so they need the runc-free direct path (or a devtmpfs /dev).
consoles: 3,
ports: 64,

9p shares

The guest mounts three 9p shares from the VM's working directory: wasi0 (the host root), wasi1 (./pack) and wasi2 (./workspace, at /workspace when the directory exists). fsN in the config sets how share wasiN presents ownership and permissions:

fs2: { uid: 1000, gid: 1000, security_model: "mapped" }
  • attach (the default): every file shows as owned by whichever guest user asks. The guest kernel caches one owner per file, so after root has looked at a file an unprivileged user sees it as root's (and gets EACCES). Modes set on the workspace are kept in .tinyarm-9p-meta/.
  • mapped: files are owned by uid:gid, except those the guest made someone else's: created by another user, or chowned. That, and modes the host permissions cannot carry (bits the host user must keep: read and write, plus search on directories; setuid, setgid, sticky), live in the host file's user.tinyarm.attr extended attribute (16 bytes; like QEMU's security_model=mapped-xattr). Host files stay readable and writable by the host user, so the host can read and change what the guest makes. On a host without extended attributes the default owner and the host permissions show.
  • none: files are always owned by uid:gid; chown is accepted and has no effect. Modes are the host permissions (with the host user's read and write bits).

Giving only uid/gid selects mapped. Under mapped and none the attach model's mode file is folded into the host files on first use and removed. Share options are host policy, not machine state: they are left out of a snapshot's pack identity, so a snapshot resumes under different ones.

WASI Web Demo

Build a deployable browser bundle:

make web

This rebuilds:

  • zig-out/layers/aarch64/pack from docker.direct/aarch64/alpine/microterm
  • zig-out/web/tinyarm.wasm from this repo

It also copies the pack into zig-out/web/pack with a static manifest, so zig-out/web can be served as a standalone demo directory. Start the localhost server with COOP/COEP headers for live terminal input:

make web-serve

Then open http://127.0.0.1:8000/. make wasm-serve is an alias for the full web demo serve path. Use HOST=0.0.0.0 PORT=8080 make web-serve to bind a different address, or override MICROTERM_AARCH64_IMAGE for an alternate image. DOCKER_BUILDX_NETWORK defaults to host and can be overridden if your Docker setup needs a different Buildx network mode.

This demo intentionally does not enable wsproxy or guest networking.

Build only the Wasmtime-oriented WASI artifact:

make wasmtime-wasi

This writes zig-out/wasmtime/tinyarm.wasm. The WASI build targets wasm32-wasi with WebAssembly simd128 and ReleaseFast enabled by default. The Wasmtime gates require a wasmtime CLI on PATH, or WASMTIME=/path/to/wasmtime.

WASI networking fd layout

TinyARM has two network adapter families:

  • Native TinyARM uses wsproxy/layers-net directly, for example -net wsproxy=127.0.0.1:8888. The socket=listenfd=<fd> backend is WASI only and is not available in the native binary.
  • Browser-style WASI hosts should keep the preopened pack filesystem at fd 3 and pass the network listener as fd 4, then run TinyARM with --net=socket=listenfd=4.
  • Wasmtime's legacy WASI listen-fd support (-S preview2=false -S listenfd=y) follows the systemd convention and exposes the inherited listener at fd 3. In that mode, run TinyARM with --net=socket=listenfd=3; TinyARM shifts its WASI cwd/preopen access to fd 4 so /pack/config, /pack/bzImage, and /pack/rootfs.bin still resolve through the --dir ...::. preopen.

This fd split is intentional: it preserves browser compatibility while making the same WASI socket backend usable under Wasmtime.

Acceptance Gates

AArch64 through TinyARM:

make acceptance-aarch64

AArch64 through the TinyARM WASI artifact under Wasmtime:

make acceptance-aarch64-wasmtime

The npm/curl userland regression gate needs wsproxy run listening on localhost:8888 unless TINYARM_NET is overridden. It is also part of the aggregate make acceptance target:

make acceptance-aarch64-userland-tools

The Bun host-binary regression gate downloads Bun's installer metadata on the host, fetches the AArch64 musl release artifact, mounts the extracted binary as guest /workspace/bun, copies it to /usr/bin/bun with sudo, and runs it. It is also part of the aggregate make acceptance target:

make acceptance-aarch64-bun-host-binary

A workspace share owned by uid 1000 (mapped), used as that user after root (mkdir, create, chmod, rename, rm, symlinks, git init and commit), with the results checked on the host:

make acceptance-aarch64-workspace

Node benchmark:

make microterm-bench-tinyarm

Native Bun benchmark:

make microterm-bench-tinyarm-bun

Node benchmark through Wasmtime:

make microterm-bench-tinyarm-wasmtime

Performance

docs/PERFORMANCE.md records the optimization work: how it was measured on an iPhone, what worked (dense AOT regions, preserve_none handlers, superinstructions, page-cache changes), what did not, and where to continue.

Current CPU Status

src/tinyarm/aarch64_cpu.zig implements the A64 decoder coverage needed by the current microterm Linux path, including integer immediates/register ops, branches, bitfield/extract aliases, conditional select/compare, indexed and exclusive load/store forms, selected EL1 system registers, ERET, permissive stage-1 4 KiB translation, and PSCI HVC calls.

The current AArch64 pack boots Linux 7.0.3 through the TinyARM acceptance gate. Broader ARMv8 instruction coverage, GIC behavior, and device support are still intentionally scoped to the current microterm workload rather than a general-purpose virt machine.

License

Apache License 2.0. See LICENSE.