XNU as a loadable kernel module inside Linux (macOS personality for arm64 Linux)
C
M2a: slide Mach-O load commands like iBoot; run XNU on the task stack; pass slide to XNU
Heyang Zhou389b4a10f189
diff --git a/xnu-ko/macho.c b/xnu-ko/macho.cindex 37f2d35..22645f1 100644--- a/xnu-ko/macho.c+++ b/xnu-ko/macho.c@@ -117,3 +117,31 @@ u64_t macho_sym(struct xnu_image *img, const char *name)}return 0;}++/* As iBoot does for a static kernelcache: slide the load commands of the in-memory Mach-O header. */+int macho_slide_header(struct xnu_image *img, u64_t *mh_unslid)+{+ u64_t mh_vm = 0;+ FOR_EACH_LC(img, l) {+ if (l->cmd == LC_SEGMENT_64 && !x_strncmp(((const struct seg64 *)l)->segname, "__TEXT", 16) &&+ ((const struct seg64 *)l)->fileoff == 0)+ mh_vm = ((const struct seg64 *)l)->vmaddr;+ }+ if (!mh_vm)+ return -ENOEXEC;+ *mh_unslid = mh_vm;+ unsigned char *mh = (unsigned char *)(mh_vm + img->slide);+ const struct mh64 *h = (const void *)mh;+ struct lc *l = (void *)(mh + sizeof(struct mh64)), *e = (void *)(mh + sizeof(struct mh64) + h->sizeofcmds);+ int n = 0;+ for (; l < e; l = (void *)((char *)l + l->cmdsize)) {+ if (l->cmd != LC_SEGMENT_64) continue;+ struct seg64 *sg = (void *)l;+ if (!sg->vmsize) continue;+ sg->vmaddr += img->slide;+ struct sect64 *sc = (void *)(sg + 1);+ for (u32_t i = 0; i < sg->nsects; i++) sc[i].addr += img->slide;+ n++;+ }+ return n;+}diff --git a/xnu-ko/macho.h b/xnu-ko/macho.hindex e4777be..dd29902 100644--- a/xnu-ko/macho.h+++ b/xnu-ko/macho.h@@ -28,4 +28,5 @@ struct xnu_image {int macho_load(struct xnu_image *img, const void *file, unsigned long size, unsigned long extra);int macho_rebase(struct xnu_image *img);+int macho_slide_header(struct xnu_image *img, u64_t *mh_unslid);u64_t macho_sym(struct xnu_image *img, const char *name); /* runtime address or 0 */diff --git a/xnu-ko/world.S b/xnu-ko/world.Sindex 5972e61..6cd7268 100644--- a/xnu-ko/world.S+++ b/xnu-ko/world.S@@ -23,8 +23,7 @@ xnu_world_call:bl fki_enter /* no preemption, no IRQs */W(x9)str x0, [x9, #104]- ldr x10, [x9, #232]- mov sp, x10+ /* XNU runs on the Linux task stack: Linux's entry code treats any other stack as overflow. */mov x0, x20blr x19/* fn returned: XNU keys and thread pointer are live */diff --git a/xnu-ko/xnu_main.c b/xnu-ko/xnu_main.cindex 9afd20d..361e521 100644--- a/xnu-ko/xnu_main.c+++ b/xnu-ko/xnu_main.c@@ -166,13 +166,16 @@ static int run_m2a(void)logkv("xnu: threaded fixups applied", (u64_t)n);if (n < 0) return n;- u64_t a_init = macho_sym(&img, "_arm_init"), a_hook = macho_sym(&img, "_linux64_hook");+ 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);- if (!a_init || !a_hook) { logs("xnu: arm_init or linux64_hook missing (kernel without the LINUX64 patch?)"); return -2; }+ if (!a_init || !a_hook || !a_slide) { logs("xnu: arm_init or linux64_hook missing (kernel without the LINUX64 patch?)"); return -2; }*(u64_t *)a_hook = (u64_t)xnu_world_upcall;+ *(u64_t *)a_slide = img.slide;- /* XNU's boot stack: the last 64 KiB of the region */- xnu_W.xnu_stack_top = (u64_t)(base + region - 64);xnu_W.xnu_tpidr = 0;logs("xnu: entering arm_init");long r = xnu_world_call(a_init, (unsigned long)ba);diff --git a/xnu-patches/0001-linux64-stage-hook.patch b/xnu-patches/0001-linux64-stage-hook.patchindex 7a07f14..77338d2 100644--- a/xnu-patches/0001-linux64-stage-hook.patch+++ b/xnu-patches/0001-linux64-stage-hook.patch@@ -1,8 +1,8 @@diff --git a/osfmk/arm/arm_init.c b/osfmk/arm/arm_init.c-index ac1713e..2aac81e 100644+index ac1713e..bd92789 100644--- a/osfmk/arm/arm_init.c+++ b/osfmk/arm/arm_init.c-@@ -228,6 +228,11 @@ extern vm_offset_t segLOWESTKC, segHIGHESTKC, segLOWESTROKC, segHIGHESTROKC;+@@ -228,6 +228,13 @@ extern vm_offset_t segLOWESTKC, segHIGHESTKC, segLOWESTROKC, segHIGHESTROKC;extern vm_offset_t segLOWESTAuxKC, segHIGHESTAuxKC, segLOWESTROAuxKC, segHIGHESTROAuxKC;extern vm_offset_t segLOWESTRXAuxKC, segHIGHESTRXAuxKC, segHIGHESTNLEAuxKC;@@ -10,18 +10,19 @@ index ac1713e..2aac81e 100644+uintptr_t linux64_hook;+/* LINUX64: the current thread pointer (TPIDR_EL1 on Apple/BCM2837 boards). Single CPU for now. */+unsigned long linux64_tpidr;++/* LINUX64: the slide xnu.ko applied. */++uintptr_t linux64_slide;+static voidarm_slide_rebase_and_sign_image(void){-@@ -285,10 +290,15 @@ arm_slide_rebase_and_sign_image(void)+@@ -285,10 +292,14 @@ arm_slide_rebase_and_sign_image(void)* using hardcoded kernel link address*/slide = (uintptr_t)k_mh - VM_KERNEL_LINK_ADDRESS;+ if (linux64_hook) {-+ /* LINUX64: xnu.ko loaded, slid and signed the image already. */-+ seg = (kernel_segment_command_t *)((uintptr_t)k_mh + sizeof(*k_mh));-+ slide = (uintptr_t)k_mh - seg->vmaddr;++ /* LINUX64: xnu.ko loaded, slid (load commands too) and signed the image already. */++ slide = linux64_slide;+ }/* rebase and sign jops */@@ -31,7 +32,7 @@ index ac1713e..2aac81e 100644rebase_threaded_starts( &__thread_starts_sect_start[0],&__thread_starts_sect_end[0],(uintptr_t)k_mh, (uintptr_t)k_mh - slide, slide);-@@ -341,6 +351,27 @@ arm_auxkc_init(void *mh, void *base)+@@ -341,6 +352,27 @@ arm_auxkc_init(void *mh, void *base)#endif /* defined(HAS_APPLE_PAC) */}@@ -59,7 +60,7 @@ index ac1713e..2aac81e 100644/** Routine: arm_init* Function: Runs on the boot CPU, once, on entry from iBoot.-@@ -361,6 +392,7 @@ arm_init(+@@ -361,6 +393,7 @@ arm_init(port_crumb = 100;arm_slide_rebase_and_sign_image();port_crumb = 101;@@ -67,7 +68,7 @@ index ac1713e..2aac81e 100644arm_static_if_init(args);port_crumb = 102;-@@ -382,6 +414,7 @@ arm_init(+@@ -382,6 +415,7 @@ arm_init(PE_init_platform(FALSE, args); /* Get platform expert set up */boot_progress_init();@@ -75,7 +76,7 @@ index ac1713e..2aac81e 100644#if __arm64__configure_timer_apple_regs();-@@ -475,6 +508,7 @@ arm_init(+@@ -475,6 +509,7 @@ arm_init(#endifml_parse_cpu_topology();@@ -83,7 +84,7 @@ index ac1713e..2aac81e 100644boot_cpu_id = ml_get_boot_cpu_number();-@@ -511,6 +545,7 @@ arm_init(+@@ -511,6 +546,7 @@ arm_init(kernel_debug_string_early("kernel_startup_bootstrap");kernel_startup_bootstrap();@@ -91,7 +92,7 @@ index ac1713e..2aac81e 100644/** Initialize the timer callout world-@@ -585,6 +620,7 @@ arm_init(+@@ -585,6 +621,7 @@ arm_init(gDramSize = *dram_size;pmap_first_pnum = (ppnum_t)atop(gDramBase);