#!/bin/sh # SPDX-License-Identifier: GPL-2.0-or-later # Copyright (C) 2026 Tsiry Sandratraina # # Build the NetBSD kernel for wasm (.agents/netbsd.md): the NetBSD source # tree (sparse) with kernel/patches/netbsd/ applied, NetBSD's host tools # (nbmake, nbconfig, nbmakefs), the GENERIC kernel as a wasm64 module with # kwemu linked in, and a root file system from NetBSD's rescue set. # # kernel/netbsd.sh src clone NetBSD (sparse) into vendor/src/netbsd, apply the patches # kernel/netbsd.sh tools build nbmake, nbconfig, nbmakefs, ... into vendor/build/netbsd-tools # kernel/netbsd.sh kernel zig build (kwemu), config + build GENERIC and GENERIC_MP (wasm64), # lowered to vendor/build/netbsd/netbsd32.wasm and netbsd-mp32.wasm # kernel/netbsd.sh rootfs vendor/build/netbsd/root.fs: FFS image of the evbarm64 rescue set # kernel/netbsd.sh image vendor/build/netbsd/netbsd.img: FFS disk of the base and etc sets # kernel/netbsd.sh all src, tools, kernel, rootfs, image # kernel/netbsd.sh run [ARGS] boot single-user from root.fs on the SDK host # kernel/netbsd.sh boot [ARGS] boot multi-user from netbsd.img (--disk) to a login prompt # CPUS=N kernel/netbsd.sh boot|run the same on N CPUs (the GENERIC_MP kernel, --cpus N) # kernel/netbsd.sh export re-export kernel/patches/netbsd from branch kw-wasm # # Needs clang and lld 22 from Homebrew (LLVM_BIN, WASM_LD override them), # Zig 0.16 and Node 24 with JSPI (--experimental-wasm-jspi). set -e root=$(cd "$(dirname "$0")/.." && pwd) src=$root/vendor/src/netbsd tools=$root/vendor/build/netbsd-tools obj=$root/vendor/build/netbsd-obj out=$root/vendor/build/netbsd compile=$src/sys/arch/wasm/compile/GENERIC compile_mp=$src/sys/arch/wasm/compile/GENERIC_MP CPUS=${CPUS:-1} NETBSD_REPO=${NETBSD_REPO:-https://github.com/NetBSD/src} NETBSD_COMMIT=${NETBSD_COMMIT:-02172b2d} # trunk, 2026-11-28 LLVM_BIN=${LLVM_BIN:-/opt/homebrew/opt/llvm/bin} WASM_LD=${WASM_LD:-/opt/homebrew/bin/wasm-ld} WASM_OPT=${WASM_OPT:-wasm-opt} # Binaryen >= 133 (--table64-lowering) # The root file system: NetBSD 11.1's rescue set (one static aarch64 binary # under 154 names, sh and init among them). RESCUE_URL=${RESCUE_URL:-https://cdn.netbsd.org/pub/NetBSD/NetBSD-11.1/evbarm-aarch64/binary/sets/rescue.tar.xz} RESCUE_SHA512=6480a08c405c69db275e52f8f3a49362c05e7b4dafaede34b3d0350d86b0b9205c31a7702b274f915f710cb6ef62c3ddc3fc70b03e79c3681d4629bdf2eed6c0 # The disk image: the same release's base and etc sets. SETS_URL=${SETS_URL:-https://cdn.netbsd.org/pub/NetBSD/NetBSD-11.1/evbarm-aarch64/binary/sets} # 11.1's binary packages: for pkg_add (which bootstraps pkgin in the image) # over FTP, which libfetch can list (pkg_add resolves names and dependencies # from the directory listing; over HTTP it cannot); for pkgin over HTTP (it # needs only pkg_summary.gz; cdn.netbsd.org redirects the release name to # the quarterly branch, which a file fetch follows). PKGS_URL=${PKGS_URL:-ftp://ftp.netbsd.org/pub/pkgsrc/packages/NetBSD/aarch64/11.1/All} PKGIN_URL=${PKGIN_URL:-http://cdn.netbsd.org/pub/pkgsrc/packages/NetBSD/aarch64/11.1/All} BASE_SHA512=e39dd71119b207c8dedd03875ec2eac6b11674acfba1ba33312ac98ed0aa77726a24b16e19db1bd1013dad64c399f6a68aaccedc8c3a2aa1d5ac2e2bcac391fe ETC_SHA512=d2254b0d1e7f9cbf643959afb03e7145228af57eab355888c2c0328bc2f2edecf8dcdf47ced8594bf43d111f625dbf79da2994b79409d8c75c871c65939de194 # What the kernel and the host tools (makefs among them) need from the tree. sparse="sys tools usr.bin usr.sbin share/mk include common etc bin lib/libc lib/libutil external/bsd/byacc external/bsd/flex external/bsd/zstd external/public-domain/xz external/historical/nawk sbin/mknod sbin/fsck sbin/fsck_ffs sbin/newfs sbin/newfs_msdos sbin/newfs_udf sbin/newfs_v7fs" do_src() { if [ ! -d "$src/.git" ]; then mkdir -p "$root/vendor/src" git clone --filter=blob:none --no-checkout "$NETBSD_REPO" "$src" git -C "$src" sparse-checkout init --cone # shellcheck disable=SC2086 git -C "$src" sparse-checkout set $sparse git -C "$src" checkout -q "$NETBSD_COMMIT" fi if ! git -C "$src" rev-parse -q --verify kw-wasm >/dev/null; then git -C "$src" checkout -q -b kw-wasm "$NETBSD_COMMIT" git -C "$src" am "$root"/kernel/patches/netbsd/*.patch fi git -C "$src" checkout -q kw-wasm } do_tools() { mkdir -p "$tools" "$obj" # TOOLS_BUILDRUMP: only the tools a kernel build needs (no compiler). (cd "$src" && ./build.sh -U -u -m evbarm -a aarch64 -T "$tools" -O "$obj" \ -V TOOLS_BUILDRUMP=yes -V MKLLVM=no -V MKGCC=no -V MKMAN=no -V MKDOC=no \ -V MKHTML=no -V MKCTF=no -N1 -j8 tools) # The make wrapper for MACHINE=wasm: config copies Makefile.wasm, so it # needs the machine name, not the toolchain (there is none in-tree). sed -e 's/^MACHINE=evbarm;/MACHINE=wasm;/' -e 's/^MAKEWRAPPERMACHINE=evbarm;/MAKEWRAPPERMACHINE=wasm;/' \ -e 's#destdir.evbarm#destdir.wasm#' "$tools/bin/nbmake-evbarm" > "$tools/bin/nbmake-wasm" chmod +x "$tools/bin/nbmake-wasm" # makefs (the root image) and pax, which it needs to build: not in the # TOOLS_BUILDRUMP set. for t in pax makefs; do (cd "$src/tools/$t" && unset CPPFLAGS CFLAGS LDFLAGS && "$tools/bin/nbmake-wasm" obj >/dev/null && "$tools/bin/nbmake-wasm" -j8 dependall && "$tools/bin/nbmake-wasm" install) done } # build_kernel CONF DIR KWEMU OUT: config, make, lower to OUT. build_kernel() { mkdir -p "$2" (cd "$2" && unset CPPFLAGS CFLAGS LDFLAGS && "$tools/bin/nbconfig" -s ../../../.. -b . "../../conf/$1" && "$tools/bin/nbmake-wasm" -j8 LLVM_BIN="$LLVM_BIN" WASM_LD="$WASM_LD" KWEMU="$3") # The 32-bit-memory build, as kernel/build.sh lower does for Linux: no # memory64 needed, faster in JavaScriptCore. "$WASM_OPT" -g --memory64-lowering --table64-lowering -O2 --enable-bulk-memory \ --enable-sign-ext --enable-nontrapping-float-to-int --enable-mutable-globals \ --enable-threads "$2/netbsd" -o "$4" ls -l "$2/netbsd" "$4" } do_kernel() { # kwemu with its JIT (unkernel.kw_host_jit_*, which the SDK host serves), # and with atomics for the MULTIPROCESSOR kernel's shared memory. (cd "$root" && zig build -Dkwemu-jit=true) (cd "$root" && zig build -Dkwemu-jit=true -Dkwemu-atomics=true --prefix vendor/build/kwemu-smp) # The kernel build has no .depend (only a changed .c rebuilds its object), # and machine/proc.h, pmap.h and trap.h include kw_emu.h (struct kw_cpu # sits in the LWP's machine-dependent state), so a new kw_emu.h means a # full rebuild: with the old objects the kernel kept the old ABI ("kwemu # ABI 5, kernel expects 4") or its old struct lwp layout (an assertion # "l->l_proc == p" at boot). if ! cmp -s "$root/include/kw_emu.h" "$src/sys/arch/wasm/include/kw_emu.h"; then echo "kw_emu.h changed: rebuilding the kernels from scratch" rm -rf "$compile" "$compile_mp" cp "$root/include/kw_emu.h" "$src/sys/arch/wasm/include/kw_emu.h" fi mkdir -p "$out" build_kernel GENERIC "$compile" "$root/zig-out/lib/libkwemu.a" "$out/netbsd32.wasm" build_kernel GENERIC_MP "$compile_mp" "$root/vendor/build/kwemu-smp/lib/libkwemu.a" "$out/netbsd-mp32.wasm" } # The kernel and CLI flags for $CPUS: the MP kernel and --cpus N above one. kernel_args() { if [ "$CPUS" -gt 1 ]; then echo "--kernel $out/netbsd-mp32.wasm --cpus $CPUS" else echo "--kernel $out/netbsd32.wasm" fi } # root.fs: /rescue, bin and sbin linking to it, uname(1) with ld.elf_so and # libc from the base set (the one dynamic program, for checks), # /dev/{console,tty,null} (conf/majors.std: cons 240, ctty 241, mem 242), # /tmp, /etc. The kernel reads it as the host's "initrd" into md0, the root # (MEMORY_DISK_DYNAMIC). do_rootfs() { mkdir -p "$out" set=$out/rescue.tar.xz if [ ! -f "$set" ] || [ "$(shasum -a 512 "$set" | cut -d' ' -f1)" != "$RESCUE_SHA512" ]; then curl -fL -o "$set.tmp" "$RESCUE_URL" [ "$(shasum -a 512 "$set.tmp" | cut -d' ' -f1)" = "$RESCUE_SHA512" ] || { echo "rescue.tar.xz: checksum mismatch" >&2; exit 1; } mv "$set.tmp" "$set" fi stage=$out/root rm -rf "$stage" mkdir -p "$stage" tar xJf "$set" -C "$stage" rm -rf "$stage/etc" mkdir -p "$stage/dev" "$stage/tmp" "$stage/etc" ln -s rescue "$stage/bin" ln -s rescue "$stage/sbin" fetch "$SETS_URL/base.tar.xz" "$out/base.tar.xz" "$BASE_SHA512" tar xJf "$out/base.tar.xz" -C "$stage" ./usr/bin/uname ./libexec/ld.elf_so \ ./usr/libexec/ld.elf_so ./lib/libc.so.12 ./usr/lib/libc.so.12 \ "$(tar tJf "$out/base.tar.xz" | grep '^\./lib/libc\.so\.12\.[0-9]*$' | head -1)" python3 -I - "$stage" > "$out/root.spec" <<'PY' import os, stat, sys root = sys.argv[1] print("/set uid=0 gid=0") print(". type=dir mode=0755") for dp, dns, fns in os.walk(root): rel = os.path.relpath(dp, root) for n in sorted(dns + fns): p = os.path.join(dp, n) r = "./" + (n if rel == "." else os.path.join(rel, n)) st = os.lstat(p) if stat.S_ISLNK(st.st_mode): print(f"{r} type=link mode=0755 link={os.readlink(p)}") elif stat.S_ISDIR(st.st_mode): print(f"{r} type=dir mode={'1777' if r == './tmp' else '0755'}") else: print(f"{r} type=file mode=0555") print("./dev/console type=char mode=0600 device=netbsd,240,0") print("./dev/tty type=char mode=0666 device=netbsd,241,0") print("./dev/null type=char mode=0666 device=netbsd,242,2") PY rm -f "$out/root.fs" "$tools/bin/nbmakefs" -t ffs -B le -o version=1 -s 24m -F "$out/root.spec" "$out/root.fs" "$stage" ls -l "$out/root.fs" } # fetch URL FILE SHA512: download unless FILE is there with that checksum. fetch() { if [ ! -f "$2" ] || [ "$(shasum -a 512 "$2" | cut -d' ' -f1)" != "$3" ]; then curl -fL -o "$2.tmp" "$1" [ "$(shasum -a 512 "$2.tmp" | cut -d' ' -f1)" = "$3" ] || { echo "$2: checksum mismatch" >&2; exit 1; } mv "$2.tmp" "$2" fi } # netbsd.img: the base and etc sets on FFSv2, configured for this machine # (root on ld0a, no swap, no network services), with /dev for this # kernel's majors and modes from the sets' mtree lists # (kernel/tools/netbsd-image-spec.py), and dhcpcd for the host's network. # Boots multi-user to a login prompt on the console; root has no password. do_image() { mkdir -p "$out" fetch "$SETS_URL/base.tar.xz" "$out/base.tar.xz" "$BASE_SHA512" fetch "$SETS_URL/etc.tar.xz" "$out/etc.tar.xz" "$ETC_SHA512" stage=$out/image rm -rf "$stage" mkdir -p "$stage" tar xpJf "$out/base.tar.xz" -C "$stage" tar xpJf "$out/etc.tar.xz" -C "$stage" # makefs runs as us: let it read everything (the spec has the real modes). chmod -R u+rwX "$stage" mkdir -p "$stage/dev/fd" "$stage/dev/pts" "$stage/kern" "$stage/proc" cat >> "$stage/etc/rc.conf" <<'RC' # unkernel (kernel/netbsd.sh image) rc_configured=YES certctl_init=YES # populate OpenSSL trust anchors on first boot hostname=netbsd no_swap=YES savecore=NO postfix=NO inetd=NO cron=NO makemandb=NO virecover=NO dhcpcd=YES # kwnet0 on the host's stack (10.0.2.15, gateway 10.0.2.2) RC cat > "$stage/etc/fstab" <<'FSTAB' /dev/ld0a / ffs rw,log 0 0 ptyfs /dev/pts ptyfs rw 0 0 kernfs /kern kernfs rw 0 0 procfs /proc procfs rw 0 0 tmpfs /tmp tmpfs rw,-m1777 0 0 FSTAB # pkg_add's repository ($PKGS_URL), CHECK_OSABI=no for the 11.99 kernel # on 11.1's packages; pkgin's repository (the package keeps an existing # repositories.conf); then neofetch at login. cat > "$stage/etc/pkg_install.conf" < "$stage/usr/pkg/etc/pkgin/repositories.conf" cat >> "$stage/root/.profile" <<'PROFILE' # unkernel (kernel/netbsd.sh image): neofetch at login [ -x /usr/pkg/bin/neofetch ] && neofetch PROFILE python3 -I "$root/kernel/tools/netbsd-image-spec.py" "$stage" > "$out/image.spec" rm -f "$out/netbsd.img" # density: an inode per 8 KiB (makefs otherwise makes just enough inodes # for the staged files, and pkgin's installs then fail with "no inodes # free"). "$tools/bin/nbmakefs" -t ffs -B le -o version=2,density=8192 -s 1g -N "$stage/etc" \ -F "$out/image.spec" "$out/netbsd.img" "$stage" ls -l "$out/netbsd.img" install_pkgs } # The packages the image ships (IMAGE_PKGS; empty: none), installed in one # boot of the fresh image, which writes the disk in place (a network is # needed): pkg_add bootstraps pkgin, which installs them. Root's .profile # runs neofetch at login. IMAGE_PKGS=${IMAGE_PKGS-neofetch} install_pkgs() { [ -n "$IMAGE_PKGS" ] || return 0 local log=$out/pkgs.log echo "image: installing pkgin with pkg_add, then $IMAGE_PKGS with pkgin, in a boot of netbsd.img ($log)" (cd "$root" && node --experimental-wasm-jspi --stack-trace-limit=40 js/cli.ts \ $(kernel_args) --initrd none --disk "$out/netbsd.img" \ --append "root=ld0a" --distro none --mem 512 \ --timeout 1500 --input "root pkg_add pkgin && pkgin -y update && pkgin -y install $IMAGE_PKGS && echo KW-PKGS-\"OK\"; shutdown -p now " --input-delay 90 < /dev/null > "$log" 2>&1) || true # The marker is quoted in the typed (echoed) command, whole in the output. grep -q '^KW-PKGS-OK' "$log" || { echo "image: installing $IMAGE_PKGS failed (see $log)" >&2; exit 1; } } # Multi-user from netbsd.img: rc, then getty and login on the console. # The SDK writes the image in place; --snapshot keeps the writes in memory. do_boot() { cd "$root" exec node --experimental-wasm-jspi --stack-trace-limit=40 js/cli.ts \ $(kernel_args) --initrd none --disk "$out/netbsd.img" \ --append "root=ld0a" --distro none --mem 256 "$@" } # Single user (-s): init asks for a shell; RETURN takes /rescue/sh. do_run() { cd "$root" exec node --experimental-wasm-jspi --stack-trace-limit=40 js/cli.ts \ $(kernel_args) --initrd "$out/root.fs" --append "-s" \ --distro none --no-net --mem 128 "$@" } do_export() { rm -f "$root"/kernel/patches/netbsd/*.patch git -C "$src" format-patch -N -o "$root/kernel/patches/netbsd" "$NETBSD_COMMIT..kw-wasm" } case "${1:-all}" in src) do_src ;; tools) do_tools ;; kernel) do_kernel ;; rootfs) do_rootfs ;; image) do_image ;; all) do_src; do_tools; do_kernel; do_rootfs; do_image ;; run) shift; do_run "$@" ;; boot) shift; do_boot "$@" ;; export) do_export ;; *) echo "usage: $0 src|tools|kernel|rootfs|image|all|run [ARGS]|boot [ARGS]|export" >&2; exit 2 ;; esac