XNU as a loadable kernel module inside Linux (macOS personality for arm64 Linux)
C
The hosted kernel's DRAM: a carve-out Linux never manages
Heyang Zhou69674d06d217
diff --git a/README.md b/README.mdindex 9f21cf5..2b7d2da 100644--- a/README.md+++ b/README.md@@ -37,8 +37,8 @@ build time; it is never committed.## Running-The host is an arm64 Linux VM with PAC on an Apple Silicon Mac (`scripts/krun-vm.sh`; tart works without the GPU), with-`cma=10G@0x100000000-0x600000000` on its kernel command line.+The host is an arm64 Linux VM with PAC on an Apple Silicon Mac (`scripts/krun-vm.sh`; tart works without the GPU), 24 GB:+`scripts/vm-install-fki-service.sh` gives Linux 15 GB (`mem=15G`) and the 8 GB above to the hosted kernel (fki's carve-out).1. `scripts/vm-xnu-build.sh` (once) sets up the macOS-DroidVM Linux toolchain and builds XNU natively on arm64.2. `scripts/vm-xnu-rebuild.sh` applies `xnu-patches/`, rebuilds the kernel, builds `kexts/` and links the kernel collection.diff --git a/fki-ko/fki_mod.c b/fki-ko/fki_mod.cindex cb6ea91..1020439 100644--- a/fki-ko/fki_mod.c+++ b/fki-ko/fki_mod.c@@ -12,6 +12,7 @@#include <linux/gfp.h>#include <linux/irqflags.h>#include <linux/mm.h>+#include <linux/io.h>#include <linux/module.h>#include <linux/preempt.h>#include <linux/printk.h>@@ -375,20 +376,28 @@ EXPORT_SYMBOL(fki_phys_peek);/* ---- contiguous memory and the top foreign window ---- *//*- * reserve_mb: take that much from the CMA area when fki loads (early in the host's boot, while the area is clean) and- * keep it for the foreign kernel's DRAM. Later, a single page the host pinned in the middle of the area (a buffer-head- * page of a file system, say) is enough to make a multi-GB cma_alloc fail for good.+ * The foreign kernel's DRAM, one physically contiguous block of GBs, from the first that works of:+ * - carve_base/carve_mb: RAM the host kernel does not manage at all (its command line limits it with mem=, the rest is+ * left for us), mapped here with memremap. Nothing of Linux's ever lands in it: the block is always there.+ * - reserve_mb: taken from the CMA area when fki loads early in the host's boot. A single page pinned in the area+ * (a file system's buffer-head page of the block device, allocated movable during the boot) can already be there+ * and split the area, so this is best effort.+ * - a CMA or alloc_contig_pages allocation at the time of the call.*/-static unsigned long reserve_mb;+static unsigned long carve_base, carve_mb, reserve_mb;+module_param(carve_base, ulong, 0444);+module_param(carve_mb, ulong, 0444);module_param(reserve_mb, ulong, 0444);static struct page *resv_pg;static unsigned long resv_cnt;-static bool resv_used;+static bool resv_used, carve_used;static void fki_reserve(void){unsigned long cnt = ALIGN(reserve_mb << 20, SZ_2M) >> PAGE_SHIFT;+ if (carve_mb)+ pr_info("fki: %lu MiB at %#lx for the foreign kernel's DRAM (outside the host's memory)\n", carve_mb, carve_base);if (!reserve_mb || fki_resolve() || !p_cma_area || !*p_cma_area || !p_cma_alloc)return;resv_pg = p_cma_alloc(*p_cma_area, cnt, get_order(SZ_2M), true);@@ -405,6 +414,19 @@ int fki_contig_alloc(unsigned long size, struct fki_region *r)if (err)return err;+ if (carve_mb && !carve_used && ALIGN(size, SZ_2M) <= carve_mb << 20 && !(carve_base & (SZ_2M - 1))) {+ void *va = memremap(carve_base, ALIGN(size, SZ_2M), MEMREMAP_WB);+ if (va) {+ carve_used = true;+ r->va = va;+ r->size = ALIGN(size, SZ_2M);+ memset(r->va, 0, r->size);+ r->pa = carve_base;+ r->handle = (void *)4UL; /* bit 2: the carve-out */+ return 0;+ }+ pr_warn("fki: carve-out at %#lx not mappable\n", carve_base);+ }if (resv_pg && !resv_used && ALIGN(size, SZ_2M) >> PAGE_SHIFT <= resv_cnt) {resv_used = true;r->va = page_address(resv_pg);@@ -449,6 +471,11 @@ EXPORT_SYMBOL(fki_contig_alloc);void fki_contig_free(struct fki_region *r){+ if ((unsigned long)r->handle == 4) {+ memunmap(r->va);+ carve_used = false;+ return;+ }if ((unsigned long)r->handle & 2) {resv_used = false; /* back to the reservation, for the next run */return;diff --git a/scripts/vm-install-fki-service.sh b/scripts/vm-install-fki-service.shindex bf33420..5ec2d0c 100755--- a/scripts/vm-install-fki-service.sh+++ b/scripts/vm-install-fki-service.sh@@ -1,9 +1,14 @@#!/bin/bash-# In the VM (once): load fki.ko early in every boot, reserving the hosted kernel's DRAM while the CMA area is still clean-# (fki reserve_mb). xnulinux-boot.sh then finds fki loaded with the reservation and uses it.+# 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.set -euo pipefailP=$(cd "$(dirname "$0")/.." && pwd)-RESERVE_MB=${RESERVE_MB:-8156}+CARVE_BASE=${CARVE_BASE:-0x400000000}; CARVE_MB=${CARVE_MB:-8156}+G=/etc/default/grub.d/99-xnulinux.cfg+echo 'GRUB_CMDLINE_LINUX="$GRUB_CMDLINE_LINUX mem=15G log_buf_len=128M"' | sudo tee $G >/dev/null+sudo update-grub >/dev/null 2>&1sudo tee /etc/systemd/system/xnulinux-fki.service >/dev/null <<UNIT[Unit]Description=xnulinux: fki.ko with the hosted kernel's DRAM reserved@@ -14,11 +19,12 @@ Before=sysinit.target[Service]Type=oneshotRemainAfterExit=yes-ExecStart=/usr/sbin/insmod $P/fki-ko/fki.ko reserve_mb=$RESERVE_MB+ExecStart=/usr/sbin/insmod $P/fki-ko/fki.ko carve_base=$CARVE_BASE carve_mb=$CARVE_MB[Install]WantedBy=sysinit.targetUNITsudo systemctl daemon-reloadsudo systemctl enable xnulinux-fki.service-echo "fki.ko loads at boot with reserve_mb=$RESERVE_MB (rebuild fki-ko before a reboot when it changes)"+sync+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)"diff --git a/scripts/xnulinux-boot.sh b/scripts/xnulinux-boot.shindex 60caa70..d77adbf 100755--- a/scripts/xnulinux-boot.sh+++ b/scripts/xnulinux-boot.sh@@ -25,15 +25,16 @@ build() { # a module; its compiler errors on failurebuild fki-kobuild xnu-ko "KBUILD_EXTRA_SYMBOLS=$PWD/fki-ko/Module.symvers"sudo dmesg -n "${CONLEVEL:-8}"-# fki keeps the hosted kernel's DRAM reserved from when it first loaded (reserve_mb): reload it only when it changed-RESERVE_MB=${RESERVE_MB:-8156} # the same as scripts/vm-install-fki-service.sh+# 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)"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- sudo insmod fki-ko/fki.ko reserve_mb="$RESERVE_MB"+ sudo insmod fki-ko/fki.ko $CARVEfi-grep -q . /sys/module/fki/parameters/reserve_mb 2>/dev/null && [ "$(cat /sys/module/fki/parameters/reserve_mb)" != 0 ] ||- echo "fki loaded without a DRAM reservation: the allocation may fail on a long-running host" >&2+[ "$(cat /sys/module/fki/parameters/carve_mb 2>/dev/null)" != 0 ] ||+ echo "fki has no carve-out (scripts/vm-install-fki-service.sh): the allocation from CMA may fail" >&2bash scripts/vm-net-up.sh# the hosted system's files on the host: console and input queues, the framebuffer xnu.ko writes, the keystore