Skip to content

XNU as a loadable kernel module inside Linux (macOS personality for arm64 Linux)

C

xnu.ko: load a fileset kernel collection (kernel symbols from its com.apple.kernel entry; XNU applies the chained fixups); bootstrap window follows the image's vmlow; kbisect IMAGE=

Heyang Zhou2a0ec4f2a374
diff --git a/scripts/vm-m2-kbisect.sh b/scripts/vm-m2-kbisect.sh
index 2a3bfe5..c3b86fb 100755
--- a/scripts/vm-m2-kbisect.sh
+++ b/scripts/vm-m2-kbisect.sh
@@ -9,7 +9,7 @@ sudo rmmod xnu 2>/dev/null; sudo rmmod fki 2>/dev/null; sudo insmod fki-ko/fki.k
for K in "$@"; do
sudo dmesg -C
echo ">>> kmax=$K"; sync
- sudo insmod xnu-ko/xnu.ko mode=3 stop=899 kmax=$K idle_max=${IDLE_MAX:-200} ram_mb=${RAM_MB:-128}
+ sudo insmod xnu-ko/xnu.ko mode=3 stop=899 kmax=$K idle_max=${IDLE_MAX:-200} ram_mb=${RAM_MB:-128} ${IMAGE:+image=$IMAGE}
echo "lines: $(sudo dmesg | grep -ac 'xnu\]')"
sudo dmesg | grep -a "xnu\]\|XNU stage 4\|budget\|panic\|run ended" | tail -${SHOW:-4}
sudo rmmod xnu
diff --git a/xnu-ko/macho.c b/xnu-ko/macho.c
index a1b5c17..64afd39 100644
--- a/xnu-ko/macho.c
+++ b/xnu-ko/macho.c
@@ -41,6 +41,27 @@ int macho_load(struct xnu_image *img, const void *file, unsigned long size, unsi
}
if (img->vmhigh <= img->vmlow)
return -ENOEXEC;
+ img->filetype = mh->filetype;
+ if (mh->filetype == MH_FILESET) {
+ /*
+ * A kernel collection: a flat image (file offset = vmaddr - base) whose pointers are chained fixups that XNU's
+ * arm_init applies itself (kernel_collection_slide). The kernel's symbols are in its own entry's LC_SYMTAB.
+ */
+ img->symtab = 0;
+ FOR_EACH_LC(img, l) {
+ if (l->cmd != LC_FILESET_ENTRY) continue;
+ const struct fileset_entry_lc *fe = (const void *)l;
+ if (fe->entry_id >= fe->cmdsize || x_strcmp((const char *)fe + fe->entry_id, "com.apple.kernel")) continue;
+ if (fe->fileoff + sizeof(struct mh64) > size) return -ENOEXEC;
+ img->kernel_mh = fe->vmaddr;
+ const struct mh64 *kh = (const void *)((const char *)file + fe->fileoff);
+ const struct lc *kl = (const void *)(kh + 1);
+ for (u32_t i = 0; i < kh->ncmds; i++, kl = (const void *)((const char *)kl + kl->cmdsize))
+ if (kl->cmd == LC_SYMTAB) img->symtab = (const void *)kl;
+ }
+ if (!img->kernel_mh || !img->symtab)
+ return -ENOEXEC;
+ }
if (extra == ~0UL) /* parse only: the caller places the image (macho_copy) */
return 0;
err = fki_region_alloc(img->vmhigh - img->vmlow + extra, &img->region);
diff --git a/xnu-ko/macho.h b/xnu-ko/macho.h
index 53e5b18..6ddfcca 100644
--- a/xnu-ko/macho.h
+++ b/xnu-ko/macho.h
@@ -8,6 +8,8 @@ typedef unsigned long long u64_t;
#define LC_SEGMENT_64 0x19
#define LC_SYMTAB 0x2
#define LC_UNIXTHREAD 0x5
+#define LC_FILESET_ENTRY 0x80000035
+#define MH_FILESET 12
struct mh64 { u32_t magic, cputype, cpusubtype, filetype, ncmds, sizeofcmds, flags, reserved; };
struct lc { u32_t cmd, cmdsize; };
@@ -24,7 +26,10 @@ struct xnu_image {
const struct symtab_lc *symtab;
u64_t thread_starts, thread_starts_size; /* unslid vmaddr */
u64_t entry; /* unslid */
+ u32_t filetype; /* MH_EXECUTE kernel or MH_FILESET kernel collection */
+ u64_t kernel_mh; /* MH_FILESET: the com.apple.kernel entry's header (unslid vmaddr) */
};
+struct fileset_entry_lc { u32_t cmd, cmdsize; u64_t vmaddr, fileoff; u32_t entry_id, reserved; };
int macho_load(struct xnu_image *img, const void *file, unsigned long size, unsigned long extra);
int macho_copy(struct xnu_image *img, void *dst);
diff --git a/xnu-ko/xnu_main.c b/xnu-ko/xnu_main.c
index 033d27f..934f90f 100644
--- a/xnu-ko/xnu_main.c
+++ b/xnu-ko/xnu_main.c
@@ -340,15 +340,21 @@ static int monitor_fn(void *arg)
static int enter_arm_init(struct boot_args_v2 *ba)
{
- struct fki_pac_keys saved; unsigned long c = fki_enter(&xnu_W.xnu_keys, &saved);
- int n = macho_rebase(&img);
- fki_leave(c, &saved);
- logkv("xnu: threaded fixups applied", (u64_t)n);
- if (n < 0) return n;
-
- u64_t mh_unslid = 0;
- int segs = macho_slide_header(&img, &mh_unslid);
- logkv("xnu: load commands slid (segments)", (u64_t)segs);
+ if (img.filetype == MH_FILESET) {
+ /* kernel collection at its link address: XNU applies the chained fixups itself; nothing slides */
+ if (img.slide) { logs("xnu: a kernel collection must not slide"); return -22; }
+ logs("xnu: kernel collection: XNU applies its chained fixups (kernel_collection_slide)");
+ } else {
+ struct fki_pac_keys saved; unsigned long c = fki_enter(&xnu_W.xnu_keys, &saved);
+ int n = macho_rebase(&img);
+ fki_leave(c, &saved);
+ logkv("xnu: threaded fixups applied", (u64_t)n);
+ if (n < 0) return n;
+
+ u64_t mh_unslid = 0;
+ int segs = macho_slide_header(&img, &mh_unslid);
+ logkv("xnu: load commands slid (segments)", (u64_t)segs);
+ }
u64_t a_init = macho_sym(&img, "_arm_init"), a_hook = macho_sym(&img, "_linux64_hook"),
a_slide = macho_sym(&img, "_linux64_slide");
logkv("xnu: arm_init at", a_init);
@@ -383,12 +389,12 @@ static int run_m2a(void)
* M2b: XNU at its own link address in the top foreign window, on physically contiguous memory with real
* physical addresses, so that the page tables XNU builds can be walked by the hardware.
*/
-#define XNU_WINDOW_BASE 0xfffffff007000000ULL /* 2 MiB aligned, at or below the image's vmlow */
+#define XNU_WINDOW_BASE (img.vmlow & ~0x1fffffULL) /* 2 MiB aligned, at or below the image's vmlow */
static int run_m2b(void)
{
u64_t image_size = img.vmhigh - img.vmlow;
- if (img.vmlow < XNU_WINDOW_BASE || img.vmlow - XNU_WINDOW_BASE >= 0x200000) { logs("xnu: unexpected link address"); return -22; }
+ if (img.vmlow < 0xffffffe000000000ULL) { logs("xnu: unexpected link address"); return -22; }
u64_t image_off = img.vmlow - XNU_WINDOW_BASE;
u64_t want = image_off + image_size + 0x20000 + ((u64_t)ram_mb << 20);
int err = fki_contig_alloc(want, &img.region);
@@ -401,6 +407,8 @@ static int run_m2b(void)
if (err) return err;
unsigned long long *l2 = l2page.va;
unsigned first = (XNU_WINDOW_BASE >> 21) & 511;
+ if (first + (img.region.size >> 21) > 512) { logs("xnu: image and DRAM cross a 1 GiB L1 slot (one bootstrap L2 table only)"); return -22; }
+ logkv("xnu: bootstrap window base", XNU_WINDOW_BASE);
for (u64_t o = 0; o < img.region.size; o += 0x200000) {
/* 2 MiB block: normal WB (AttrIdx 0 in Linux's MAIR), inner shareable, AF, EL1 RWX, UXN */
l2[first + (o >> 21)] = (img.region.pa + o) | (1ULL << 54) | (1ULL << 10) | (3ULL << 8) | 1;