XNU as a loadable kernel module inside Linux (macOS personality for arm64 Linux)
C
A 16 GB VM by default; fki refuses a carve-out that is not RAM
Heyang Zhou3aa9a7d79533
diff --git a/README.md b/README.mdindex b674256..7a15eca 100644--- a/README.md+++ b/README.md@@ -70,7 +70,7 @@ the system volume, AMFI is patched and SIP relaxed, and memory access is slower## Requirements-- An arm64 Linux host with PAC (Apple Silicon M1 or later in a VM: krunkit for GPU access, or tart), 4K pages, ~24 GB of+- An arm64 Linux host with PAC (Apple Silicon M1 or later in a VM: krunkit for GPU access, or tart), 4K pages, 16 GB ofRAM (8 GB of it reserved for the hosted kernel).- A Mac with Xcode to make the system disk and build the GPU parts.- A macOS 27 installation (an Apple Virtualization VM disk) of your own. **No Apple binaries are in this repository ordiff --git a/docs/production.md b/docs/production.mdindex 59f1eb1..09bf229 100644--- a/docs/production.md+++ b/docs/production.md@@ -28,7 +28,7 @@ What the PoC did to get each step running, and what replaces it. Ordered by what| `kc-amfi-platform.py` patches AMFI's expected platform version in the kernel collection | Build the collection from the same macOS build as the disk (one version source), no byte patching || `amfi_get_out_of_my_way=1`, `lp-sip0=0x802`, `kern.memorystatus_highwater_enabled=0` | Keep SIP/AMFI on; sign our plugin and kexts (ad-hoc trust cache of our own), only the unauthenticated-root bit stays || `/chosen` UUIDs passed by hand (`root_uuid=`, `preboot_uuid=`) | Read from the disk's APFS container at load (xnu.ko or a host tool), no manual parameters (step one done: `mk-macvm-disk.sh` writes them to `mac.conf`, `xnulinux-boot.sh` reads it) |-| Fixed `carve_base=0x400000000` with `nokaslr` so Linux's arm64 EFI stub does not place the kernel image inside the carve-out before `mem=15G` takes effect | Have `fki` choose the carve-out dynamically at load from the actual memory map (avoiding the kernel image and reserved regions), then re-enable KASLR |+| Fixed `carve_base` (`0x200000000` in a 16 GB VM) with `nokaslr` so Linux's arm64 EFI stub does not place the kernel image inside the carve-out before `mem=` takes effect | Have `fki` choose the carve-out dynamically at load from the actual memory map (avoiding the kernel image and reserved regions), then re-enable KASLR |## 4. Scripts and testsdiff --git a/fki-ko/fki_mod.c b/fki-ko/fki_mod.cindex a54d324..8d1ff02 100644--- a/fki-ko/fki_mod.c+++ b/fki-ko/fki_mod.c@@ -13,6 +13,7 @@#include <linux/irqflags.h>#include <linux/mm.h>#include <linux/io.h>+#include <linux/efi.h>#include <linux/ioport.h>#include <linux/module.h>#include <linux/preempt.h>@@ -76,6 +77,7 @@ static bool (*p_cma_release)(struct cma *, const struct page *, unsigned long);static set_memory_fn p_set_memory_x, p_set_memory_nx;static unsigned long p_stext, p_end;static u64 *p_kimage_voffset;+static int (*p_efi_mem_type)(unsigned long); /* the firmware's memory map: is the carve-out RAM at all */static int fki_resolve(void){@@ -102,6 +104,7 @@ static int fki_resolve(void)p_stext = lookup("_stext");p_end = lookup("_end");p_kimage_voffset = (u64 *)lookup("kimage_voffset");+ p_efi_mem_type = (void *)lookup("efi_mem_type");return p_set_memory_x && p_set_memory_nx ? 0 : -ENOENT;}@@ -467,6 +470,23 @@ static int fki_carve_check(unsigned long base, unsigned long len)pr_err("fki: carve-out [%#lx, %#lx) overflows physical address space\n", base, end);return -EINVAL;}+ /* RAM in the firmware's map, every 2 MiB of it: a carve-out past the end of the VM's memory (carve_base from a+ * bigger VM) would map addresses with nothing behind them */+ if (p_efi_mem_type && p_efi_mem_type(base) >= 0) { /* < 0: no EFI memory map (not an EFI boot) */+ unsigned long a;++ for (a = base; a < end; a += SZ_2M) {+ int t = p_efi_mem_type(a);++ /* free RAM once Linux runs: loader and boot-services code and data, conventional memory */+ if (t != EFI_LOADER_CODE && t != EFI_LOADER_DATA && t != EFI_BOOT_SERVICES_CODE &&+ t != EFI_BOOT_SERVICES_DATA && t != EFI_CONVENTIONAL_MEMORY) {+ pr_err("fki: carve-out [%#lx, %#lx) is not all RAM (EFI type %d at %#lx); refusing to map (check carve_base, carve_mb and the VM's memory size)\n",+ base, end, t, a);+ return -EINVAL;+ }+ }+ }if (p_stext && p_end && p_kimage_voffset) {unsigned long k_start = p_stext - *p_kimage_voffset;unsigned long k_end = p_end - *p_kimage_voffset;diff --git a/scripts/krun-vm.sh b/scripts/krun-vm.shindex 3bede5e..b2434a8 100644--- a/scripts/krun-vm.sh+++ b/scripts/krun-vm.sh@@ -11,7 +11,7 @@ set -euo pipefailKRUN=${KRUN:-$HOME/.local/krunkit}DISK=${DISK:-$HOME/.tart/vms/xnupoc/disk.img}MAC=${MAC:-$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1]))["macAddress"])' "$(dirname "$DISK")/config.json")}-CPUS=${CPUS:-6}; MEM=${MEM:-24576}; SSH_PORT=${SSH_PORT:-2223}+CPUS=${CPUS:-6}; MEM=${MEM:-16384}; SSH_PORT=${SSH_PORT:-2223}S=${S:-$HOME/.local/state/xnupoc-krun}; mkdir -p "$S"if pgrep -f "tart run xnupoc" >/dev/null; then echo "tart is running xnupoc: stop it first" >&2; exit 1; firm -f "$S"/gv.sock "$S"/gvapi.sockdiff --git a/scripts/vm-install-fki-service.sh b/scripts/vm-install-fki-service.shindex 80e9334..54b6289 100755--- a/scripts/vm-install-fki-service.sh+++ b/scripts/vm-install-fki-service.sh@@ -1,16 +1,18 @@#!/bin/bash# SPDX-License-Identifier: GPL-2.0-only# In the VM (once): the hosted kernel's DRAM as RAM Linux never manages, and fki.ko loaded with it early in every boot.-# The VM has 24 GB; Linux's command line says mem=15G (RAM from 0x40000000 to 0x400000000), and the 8 GB above, below-# the firmware's holes near the top, are fki's carve-out (carve_base/carve_mb). A CMA area was not enough: one page Linux-# pins in it during the boot (a buffer-head page of the block device) splits it, and the 8 GB block does not fit.+# The VM has 16 GB (RAM from 0x40000000 to 0x440000000); Linux's command line says mem=7G (RAM up to 0x200000000), and the+# 8 GB above it are fki's carve-out (carve_base/carve_mb), ending ~1 GB below the top of RAM, where the firmware keeps its+# regions and loads the Linux kernel image (nokaslr: see below). A CMA area was not enough: one page Linux pins in it+# during the boot (a buffer-head page of the block device) splits it, and the 8 GB block does not fit.+# A VM with more memory: LINUX_MEM=15G CARVE_BASE=0x400000000 for 24 GB.set -euo pipefailP=$(cd "$(dirname "$0")/.." && pwd)-CARVE_BASE=${CARVE_BASE:-0x400000000}; CARVE_MB=${CARVE_MB:-8156}+LINUX_MEM=${LINUX_MEM:-7G}; CARVE_BASE=${CARVE_BASE:-0x200000000}; CARVE_MB=${CARVE_MB:-8156}G=/etc/default/grub.d/99-xnulinux.cfg-# nokaslr: arm64's EFI stub randomises the Linux kernel's physical load address across all EFI memory before mem=15G-# takes effect, which otherwise places the kernel image inside the 0x400000000 carve-out on ~1/3 of boots.-echo 'GRUB_CMDLINE_LINUX="$GRUB_CMDLINE_LINUX mem=15G nokaslr log_buf_len=128M"' | sudo tee $G >/dev/null+# nokaslr: arm64's EFI stub randomises the Linux kernel's physical load address across all EFI memory before mem=+# takes effect, which otherwise places the kernel image inside the carve-out on some boots (fki then refuses to map it).+echo "GRUB_CMDLINE_LINUX=\"\$GRUB_CMDLINE_LINUX mem=$LINUX_MEM nokaslr log_buf_len=128M\"" | sudo tee $G >/dev/nullsudo update-grub >/dev/null 2>&1sudo tee /etc/systemd/system/xnulinux-fki.service >/dev/null <<UNIT[Unit]@@ -30,4 +32,4 @@ UNITsudo systemctl daemon-reloadsudo systemctl enable xnulinux-fki.servicesync-echo "after a reboot: Linux on 15 GB, fki.ko loaded with the $CARVE_MB MB carve-out at $CARVE_BASE (rebuild fki-ko before a reboot when it changes)"+echo "after a reboot: Linux on $LINUX_MEM, fki.ko loaded with the $CARVE_MB MB carve-out at $CARVE_BASE (rebuild fki-ko before a reboot when it changes)"diff --git a/scripts/xnulinux-boot.sh b/scripts/xnulinux-boot.shindex d1d9653..e4cf95b 100755--- a/scripts/xnulinux-boot.sh+++ b/scripts/xnulinux-boot.sh@@ -48,7 +48,7 @@ build xnu-ko "KBUILD_EXTRA_SYMBOLS=$PWD/fki-ko/Module.symvers"sudo dmesg -n "${CONLEVEL:-1}"# fki has the hosted kernel's DRAM from its load at boot (scripts/vm-install-fki-service.sh): reload it only when it changed,# with the same carve-out-CARVE="carve_base=$(cat /sys/module/fki/parameters/carve_base 2>/dev/null || echo 0x400000000) carve_mb=$(cat /sys/module/fki/parameters/carve_mb 2>/dev/null || echo 8156)"+CARVE="carve_base=$(cat /sys/module/fki/parameters/carve_base 2>/dev/null || echo 0x200000000) carve_mb=$(cat /sys/module/fki/parameters/carve_mb 2>/dev/null || echo 8156)"if [ "$(cat /sys/module/fki/srcversion 2>/dev/null)" != "$(modinfo -F srcversion fki-ko/fki.ko)" ]; thensudo systemctl stop xnudrmd 2>/dev/null || true # it holds fki's /dev/xnu-hostcallsudo rmmod fki 2>/dev/null || true@@ -83,8 +83,8 @@ for try in 1 2 3 4 5 6 7 8 9; doconsole_in="$CONSOLE_IN" input_in="$INPUT_IN" fb_dump="$FB_DUMP" keystore="$KEYSTORE" bootargs="\"$BOOTARGS\"" ${XNU_ARGS:-} \> /var/tmp/xnu-insmod.log 2>&1 < /dev/null &sleep 5- if sudo dmesg | grep -aq "fki: carve-out .* overlaps"; then- sudo dmesg | grep -a "fki: carve-out .* overlaps" >&2+ if sudo dmesg | grep -aqE "fki: carve-out .* (overlaps|is not all RAM)"; then # a configuration error: no retry+ sudo dmesg | grep -aE "fki: carve-out .* (overlaps|is not all RAM)" >&2exit 1fisudo dmesg | grep -aq "contiguous allocation failed" || break