Something went wrong. Try again.
An experimental port of NetBSD, FreeBSD, OpenBSD, and XNU to unkernel unkernel.dev
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283#!/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 -eroot=$(cd "$(dirname "$0")/.." && pwd)vsrc=$root/vendor/srcsrc=$vsrc/xnuout=$root/vendor/build/xnuMEM=${MEM:-2048}XNU_TAG=${XNU_TAG:-xnu-11417.140.69} # macOS 15.6XNU_BASE=43a90889846e00bfb5cf1d255cdc0a701a1e05a4 # $XNU_TAGPTHREAD_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-distributionsLLVM_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" <<EOF#!/bin/shout=\$("$vsrc/AvailabilityVersions/availability" "\$@")case "\$1" in--macosx) echo "\$out 14.4 14.5 14.6 15.0 15.1 15.2 15.3 15.4 15.5 15.6" ;;--ios) echo "\$out 17.4 17.5 17.6 18.0 18.1 18.2 18.3 18.4 18.5 18.6" ;;*) echo "\$out" ;;esacEOF chmod +x "$a" echo "$a"}
# KW_SMP=1 (kernel-smp): the SMP kernel, built apart ($obj, $kwemu, the# kexts' objects), on shared memory with wasm atomics and kwemu's SMP build.obj=$out/obj${KW_SMP:+-smp}kwemu=$root/zig-out/lib/libkwemu.a[ -n "${KW_SMP:-}" ] && kwemu=$root/vendor/build/kwemu-smp/lib/libkwemu.akwkexts=$out/libkwkexts${KW_SMP:+-smp}.akernel_wasm=$obj/RELEASE_ARM64_WASM/kernel.wasm
xnu_make() { (cd "$src" && env RC_DARWIN_KERNEL_VERSION=24.6.0 AVAILABILITY_PL="$(availability)" \ make SDKROOT=macosx ARCH_CONFIGS=ARM64 KERNEL_CONFIGS=RELEASE MACHINE_CONFIGS=WASM \ OBJROOT="$obj" SYMROOT="$out/sym" DSTROOT="$out/dst" \ CC="$LLVM_BIN/clang" CXX="$LLVM_BIN/clang++" MIGCC="$(xcrun -find clang)" \ WASM_LD="$WASM_LD" KWEMU="$kwemu" KWKEXTS="$kwkexts" \ KWBRIDGE="$out/kwbridge.dylib" KW_SMP="${KW_SMP:-}" \ BUILD_WERROR=0 -j"$(sysctl -n hw.ncpu)" "$@")}
# The kext bridge's compiler plugin (kernel/tools/xnu-kwbridge.cpp), for# every kernel and built-in kext object.do_kwbridge() { mkdir -p "$out" if [ ! -f "$out/kwbridge.dylib" ] || [ "$tools/xnu-kwbridge.cpp" -nt "$out/kwbridge.dylib" ]; then # shellcheck disable=SC2046 "$LLVM_BIN/clang++" -shared -fPIC -O2 $("$LLVM_BIN/llvm-config" --cxxflags) \ "$tools/xnu-kwbridge.cpp" -o "$out/kwbridge.dylib" \ -L"$("$LLVM_BIN/llvm-config" --libdir)" -lLLVM fi}
# The kexts XNU's kernelcache would hold, built against the installed# Kernel.framework headers as kexts are, into one library the kernel# links; bsd/dev/wasm/kw_kexts.c starts them.do_kexts() { kf=$out/dst/System/Library/Frameworks/Kernel.framework/Versions/A o=$out/kexts rm -rf "$o" mkdir -p "$o" cc="$LLVM_BIN/clang --target=wasm64-unknown-unknown -O2 -g -nostdlibinc -ffreestanding \ -fpass-plugin=$out/kwbridge.dylib \ -fno-builtin -fno-common -fblocks -fno-stack-protector -ffunction-sections -fdata-sections \ -mbulk-memory -mnontrapping-fptoint -msign-ext -mmutable-globals ${KW_SMP:+-matomics -DKW_SMP=1} \ -include $src/osfmk/wasm/kw_wasm_prefix.h \ -D__arm64__=1 -D__arm64=1 -D__AARCH64EL__=1 -D__ARM_64BIT_STATE=1 -D__APPLE__=1 -D__MACH__=1 \ -D__MACHO__=1 -DLP64 -DPAGE_SIZE_FIXED -D__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__=150600 \ -D__ENVIRONMENT_OS_VERSION_MIN_REQUIRED__=150600 -DKERNEL -DKERNEL_PRIVATE -DPRIVATE -DAPPLE \ -I$kf/PrivateHeaders -I$kf/Headers -I$src/EXTERNAL_HEADERS -I$tools/xnu-kext -w \ -Werror=implicit-function-declaration -Werror=incompatible-function-pointer-types \ -Werror=return-type -Wno-error=int-conversion" objs= # libpthread's kern/ (pthread.kext), as its kext.xcconfig builds it. p=$vsrc/libpthread for f in kern_init kern_support kern_synch; do # shellcheck disable=SC2086 $cc -I$p -I$p/private -I$p/include -DXNU_KERNEL_PRIVATE -DMACH_KERNEL_PRIVATE \ -DABSOLUTETIME_SCALAR_TYPE -DNEEDS_SCHED_CALL_T -D__PTHREAD_EXPOSE_INTERNALS__ \ -Dpthread_kern=kw_pthread_kern -Dcurrent_uthread=kw_pthread_current_uthread \ -Dport_name_to_thread=kw_kext_port_name_to_thread \ -c "$p/kern/$f.c" -o "$o/pthread_$f.o" objs="$objs $o/pthread_$f.o" done # HFS.kext and HFSEncodings.kext (hfs core/ and hfs_encodings/). h=$vsrc/hfs hf="-I$h/core -I$h/hfs_encodings -include $h/core/kext-config.h -DXNU_KERNEL_PRIVATE=0" for f in "$h"/core/*.c "$h"/hfs_encodings/hfs_encodings.c \ "$h"/hfs_encodings/hfs_encodinghint.c "$tools/xnu-kext/kw_hfs.c"; do b=$(basename "$f" .c) ef= case $f in */hfs_encodings/*) ef=-Dhfs_allocated=hfs_encodings_allocated ;; esac # shellcheck disable=SC2086 $cc $hf $ef -c "$f" -o "$o/hfs_$b.o" objs="$objs $o/hfs_$b.o" done # shellcheck disable=SC2086 $cc $hf -x c++ -std=gnu++17 -fno-exceptions -fno-rtti -fno-c++-static-destructors \ -c "$h/core/hfs_iokit.cpp" -o "$o/hfs_hfs_iokit.o" objs="$objs $o/hfs_hfs_iokit.o" # shellcheck disable=SC2086 python3 -I "$tools/xnu-kpi-thunks.py" "$src/config" "$o/kpi-thunks.s" $objs "$LLVM_BIN/clang" --target=wasm64-unknown-unknown -c "$o/kpi-thunks.s" -o "$o/kpi-thunks.o" rm -f "$kwkexts" # shellcheck disable=SC2086 "$LLVM_BIN/llvm-ar" rcs "$kwkexts" $objs "$o/kpi-thunks.o"}
do_kernel() { if [ -n "${KW_SMP:-}" ]; then (cd "$root" && zig build -Dkwemu-jit=true -Dkwemu-atomics=true --prefix vendor/build/kwemu-smp) else (cd "$root" && zig build -Dkwemu-jit=true) fi cp "$root/include/kw_emu.h" "$src/osfmk/wasm/kw_emu.h" mkdir -p "$out" xnu_make installhdrs > "$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}" insrc) 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