#!/bin/sh # SPDX-License-Identifier: GPL-2.0-or-later # Copyright (C) 2026 Tsiry Sandratraina # # Build XNU (Darwin, macOS 15.6's kernel) for wasm (.agents/xnu.md): the # XNU tree with kernel/patches/xnu/ applied (machine config ARM64^WASM, # osfmk/wasm), the open-source kexts it needs built in (libpthread, HFS), # and a root disk of this Mac's own macOS user space. Runs on macOS: the # user space is the host's (not redistributable), and so are hdiutil and # Apple's clang for MIG. # # kernel/xnu.sh src clone XNU, libpthread, HFS and AvailabilityVersions into # vendor/src, apply the patches (branch kw-wasm) # kernel/xnu.sh kernel zig build (kwemu), the Kernel.framework headers, the kexts # (vendor/build/xnu/libkwkexts.a), the kernel: a wasm64 module, # vendor/build/xnu/obj/RELEASE_ARM64_WASM/kernel.wasm (not # lowered: Binaryen's lowering fails on locore.s's table.get) # kernel/xnu.sh image vendor/build/xnu/root.img: HFS+ root of this Mac's macOS # (kernel/tools/xnu-rootfs.sh) # sudo kernel/xnu.sh image-apfs vendor/build/xnu/root-apfs.img: the same on APFS (a container # with the system and a Preboot volume), which ROOT=apfs boot # uses; needs root and the kexts (kc); does not boot yet # kernel/xnu.sh kc vendor/build/xnu/kexts.kwkc: closed kexts (APFS and what it # needs) from this Mac's kernelcache of the same XNU, for the kext # bridge (kernel/tools/xnu-kc.py); boot attaches it as a disk # kernel/xnu.sh all src, kernel, image # kernel/xnu.sh boot [ARGS] boot multi-user from root.img (--disk) to a console login, with # kexts.kwkc as the second disk when it exists (KEXTS=0 leaves it out) # MEM=MiB kernel/xnu.sh boot memory (default 2048: XNU sizes its kmem ranges from it, and the # closed kexts need the room) # kernel/xnu.sh priv print the one root command that copies sudo and visudo # (readable by root only) to vendor/build/xnu/priv for the image # kernel/xnu.sh export re-export kernel/patches/xnu from branch kw-wasm # # Needs clang and lld 22 from Homebrew (LLVM_BIN, WASM_LD override them), # Xcode's clang (MIG), Zig 0.16, Node 24 with JSPI and memory64. set -e root=$(cd "$(dirname "$0")/.." && pwd) vsrc=$root/vendor/src src=$vsrc/xnu out=$root/vendor/build/xnu MEM=${MEM:-2048} XNU_TAG=${XNU_TAG:-xnu-11417.140.69} # macOS 15.6 XNU_BASE=43a90889846e00bfb5cf1d255cdc0a701a1e05a4 # $XNU_TAG PTHREAD_TAG=${PTHREAD_TAG:-libpthread-536} HFS_TAG=${HFS_TAG:-hfs-683.120.3} AV_TAG=${AV_TAG:-AvailabilityVersions-157.2} OSS=https://github.com/apple-oss-distributions LLVM_BIN=${LLVM_BIN:-/opt/homebrew/opt/llvm/bin} WASM_LD=${WASM_LD:-/opt/homebrew/bin/wasm-ld} tools=$root/kernel/tools clone() { # repo tag dir [ -d "$3/.git" ] || git -c advice.detachedHead=false clone -q --depth 1 -b "$2" "$OSS/$1.git" "$3" } do_src() { mkdir -p "$vsrc" clone xnu "$XNU_TAG" "$src" clone libpthread "$PTHREAD_TAG" "$vsrc/libpthread" clone hfs "$HFS_TAG" "$vsrc/hfs" clone AvailabilityVersions "$AV_TAG" "$vsrc/AvailabilityVersions" if ! git -C "$src" rev-parse -q --verify kw-wasm >/dev/null; then git -C "$src" checkout -q -b kw-wasm "$XNU_BASE" git -C "$src" am "$root"/kernel/patches/xnu/*.patch fi git -C "$src" checkout -q kw-wasm } # AvailabilityVersions' newest tag stops at macOS 14.3 and iOS 17.3: XNU # 11417 needs the versions up to 15.6 and 18.6. availability() { a=$out/sdk/availability.pl mkdir -p "$out/sdk" cat > "$a" < "$out/installhdrs.log" 2>&1 || { tail -20 "$out/installhdrs.log"; exit 1; } do_kwbridge do_kexts rm -f "$kernel_wasm" xnu_make > "$out/build.log" 2>&1 || { grep -m20 "error:" "$out/build.log"; exit 1; } ls -l "$kernel_wasm" } # The kexts the bridge loads, in order (a kext after those it uses): what a # headless macOS needs for an APFS root, from the collection's own # dependency lists. KC_KEXTS=${KC_KEXTS:-"com.apple.driver.AppleEffaceableStorage com.apple.iokit.CoreAnalyticsFamily \ com.apple.iokit.IOStorageFamily com.apple.kec.corecrypto com.apple.iokit.IOCryptoAcceleratorFamily \ com.apple.kext.CoreTrust com.apple.security.AppleImage4 com.apple.filesystems.apfs \ com.apple.driver.AppleFileSystemDriver"} # This Mac's kernelcache of the XNU built here (its Preboot volume keeps # one per installed macOS). do_kc() { want=$(echo "$XNU_TAG" | sed 's/^xnu-//') for k in /System/Volumes/Preboot/*/boot/*/System/Library/Caches/com.apple.kernelcaches/kernelcache; do if python3 -I "$tools/xnu-kc.py" --version "$k" | grep -q "xnu-$want~"; then # shellcheck disable=SC2086 python3 -I "$tools/xnu-kc.py" "$k" "$src/config" "$out/kexts.kwkc" $KC_KEXTS return fi done echo "no kernelcache of $XNU_TAG on this Mac" >&2 exit 1 } do_image() { "$tools/xnu-rootfs.sh" "$out/root.img" "${SIZE:-10g}" } do_image_apfs() { "$tools/xnu-rootfs.sh" "$out/root-apfs.img" "${SIZE:-10g}" apfs chown "$(stat -f %u "$0"):$(stat -f %g "$0")" "$out/root-apfs.img" "$out/root-apfs.img.uuid" } # Multi-user from root.img: launchd, then getty and login on the console. # kexts.kwkc (kc) rides along as the second disk unless KEXTS=0. fips_mode=64 # is corecrypto's own boot-arg to skip its integrity self-test: the packer # rewrites its code (xnu-kc.py), so the stored MAC cannot match. do_boot() { cd "$root" kexts= rootimg=$out/root.img bootargs="fips_mode=64" if [ "${ROOT:-hfs}" = apfs ] && [ -f "$out/root-apfs.img.uuid" ]; then # The APFS root, by its volume's UUID (IOFindBSDRoot): the kexts find # it, and do not publish it yet (the authenticated-root check). rootimg=$out/root-apfs.img bootargs="$bootargs rd=uuid boot-uuid=$(cat "$out/root-apfs.img.uuid")" KEXTS=1 fi if [ "${KEXTS:-1}" != 0 ] && [ -f "$out/kexts.kwkc" ]; then kexts="--disk $out/kexts.kwkc" fi case " $* " in *" --append "*) ;; *) set -- --append "$bootargs" "$@" ;; esac cpus= if [ "${CPUS:-1}" -gt 1 ]; then # The SMP kernel (KW_SMP=1 kernel/xnu.sh kernel) on N CPUs. kernel_wasm=$out/obj-smp/RELEASE_ARM64_WASM/kernel.wasm cpus="--cpus $CPUS" # A secondary is a new instance of the module: longer than the 1 s default. bootargs="$bootargs cpu_boot_timeout_secs=60" fi # shellcheck disable=SC2086 exec node --experimental-wasm-jspi --stack-trace-limit=40 js/cli.ts \ --kernel "$kernel_wasm" --initrd none --disk "$rootimg" \ $kexts $cpus --distro none --mem "$MEM" "$@" } do_priv() { echo "sudo sh -c 'mkdir -p $out/priv/usr/bin $out/priv/usr/sbin &&" \ "cp /usr/bin/sudo $out/priv/usr/bin/ && cp /usr/sbin/visudo $out/priv/usr/sbin/ &&" \ "chmod 0444 $out/priv/usr/bin/sudo $out/priv/usr/sbin/visudo'" } do_export() { rm -f "$root"/kernel/patches/xnu/*.patch mkdir -p "$root/kernel/patches/xnu" git -C "$src" format-patch -N -o "$root/kernel/patches/xnu" "$XNU_BASE..kw-wasm" } case "${1:-all}" in src) do_src ;; kernel) do_kernel ;; image) do_image ;; image-apfs) do_image_apfs ;; kc) do_kc ;; all) do_src; do_kernel; do_image ;; boot) shift; do_boot "$@" ;; priv) do_priv ;; export) do_export ;; *) echo "usage: $0 src|kernel|kc|image|image-apfs|all|boot [ARGS]|priv|export" >&2; exit 2 ;; esac