XNU as a loadable kernel module inside Linux (macOS personality for arm64 Linux)
C
M2b: XNU's arm_vm_init hands its kernel L1 entries to the host, which installs them in Linux's top L1
Heyang Zhoua6fab139cfe5
diff --git a/xnu-ko/xnu_main.c b/xnu-ko/xnu_main.cindex 09cbab8..343271c 100644--- a/xnu-ko/xnu_main.c+++ b/xnu-ko/xnu_main.c@@ -25,6 +25,11 @@ static int ram_mb = 256;module_param(ram_mb, int, 0444);static struct xnu_image img;+static struct fki_region l2page;+static unsigned long long *window_slot, *window_l1;+static unsigned char window_owned[512];+#define XNU_L1_FIRST 384 /* VM_MIN_KERNEL_ADDRESS 0xffffffe000000000 */+#define XNU_L1_LAST 495 /* VM_MAX_KERNEL_ADDRESS 0xfffffffbffffffff */static void *blob;struct xnu_world xnu_W;@@ -91,6 +96,17 @@ long xnu_hook(unsigned long stage, unsigned long a1, unsigned long a2)logs(m);return 0;}+ if (stage == 200 && window_l1) { /* install XNU's kernel L1 entries (the kernel VA range only) */+ const unsigned long long *tte = (const unsigned long long *)a1;+ int n = 0, last = -1;+ for (int i = XNU_L1_FIRST; i <= XNU_L1_LAST; i++) {+ if (!(tte[i] & 1)) continue;+ if (window_l1[i] && &window_l1[i] != window_slot) { logkv("xnu: L1 slot in use by Linux, refusing:", i); return 1; }+ window_l1[i] = tte[i]; window_owned[i] = 1; n++; last = i;+ }+ if (last >= 0) fki_window_set(&window_l1[last], tte[last]); /* barrier + TLB flush */+ logkv("xnu: XNU L1 entries installed", n);+ }if (stage == 999) {char m[200]; const char *f = (const char *)a1; unsigned i = 0;while (f && f[i] && i < sizeof(m) - 1) { m[i] = f[i]; i++; }@@ -106,6 +122,9 @@ long xnu_hook(unsigned long stage, unsigned long a1, unsigned long a2){ 140, "timer_call_init" }, { 150, "cpu_init" }, { 160, "processor_bootstrap" },{ 170, "boot-arg parsing" }, { 175, "nanoseconds_to_absolutetime" }, { 180, "/chosen dram-base/size" },{ 199, "arm_init reached arm_vm_init" },+ { 200, "arm_vm_init: XNU's kernel L1 entries installed in Linux's table" },+ { 210, "arm_vm_init: running on XNU's own page tables" }, { 220, "pmap_bootstrap returned" },+ { 230, "arm_vm_init returned" }, { 300, "kprintf initialized" },};const char *what = "?";for (unsigned i = 0; i < sizeof(names) / sizeof(names[0]); i++)@@ -221,8 +240,6 @@ static int run_m2a(void)* 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 */-static struct fki_region l2page;-static unsigned long long *window_slot;static int run_m2b(void){@@ -252,6 +269,8 @@ static int run_m2b(void)logkv("xnu: L1 slot", idx);if (l1[idx]) { logkv("xnu: L1 slot already used by Linux:", l1[idx]); return -16; }window_slot = &l1[idx];+ window_l1 = l1;+ window_owned[idx] = 1;fki_window_set(window_slot, l2page.pa | 3);logs("xnu: bootstrap window mapped");@@ -289,7 +308,11 @@ static int __init xnu_init(void)static void __exit xnu_exit(void){- if (window_slot) fki_window_set(window_slot, 0);+ if (window_l1) {+ int last = -1;+ for (int i = 0; i < 512; i++) if (window_owned[i]) { window_l1[i] = 0; last = i; }+ if (last >= 0) fki_window_set(&window_l1[last], 0);+ }if (mode == 3) {if (img.region.va) fki_contig_free(&img.region);if (l2page.va) fki_contig_free(&l2page);diff --git a/xnu-patches/0001-linux64-stage-hook.patch b/xnu-patches/0001-linux64-stage-hook.patchindex 4b338e4..5d3f803 100644--- a/xnu-patches/0001-linux64-stage-hook.patch+++ b/xnu-patches/0001-linux64-stage-hook.patch@@ -1,5 +1,5 @@diff --git a/osfmk/arm/arm_init.c b/osfmk/arm/arm_init.c-index ac1713e..f0a504d 100644+index ac1713e..9d38023 100644--- a/osfmk/arm/arm_init.c+++ b/osfmk/arm/arm_init.c@@ -228,6 +228,13 @@ extern vm_offset_t segLOWESTKC, segHIGHESTKC, segLOWESTROKC, segHIGHESTROKC;@@ -32,7 +32,7 @@ index ac1713e..f0a504d 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 +352,34 @@ arm_auxkc_init(void *mh, void *base)+@@ -341,6 +352,50 @@ arm_auxkc_init(void *mh, void *base)#endif /* defined(HAS_APPLE_PAC) */}@@ -63,11 +63,27 @@ index ac1713e..f0a504d 100644+{+ linux64_stage(stage);+}++++/* A stage that hands the host one argument (x1). */++void linux64_stage_arg(unsigned long stage, unsigned long arg);++void++linux64_stage_arg(unsigned long stage, unsigned long arg)++{++ if (linux64_hook) {++ register unsigned long x0 __asm__("x0") = stage;++ register unsigned long x1 __asm__("x1") = arg;++ __asm__ volatile ("blr %2"++ : "+r"(x0), "+r"(x1)++ : "r"(linux64_hook)++ : "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10", "x11",++ "x12", "x13", "x14", "x15", "x16", "x17", "x30", "cc", "memory");++ }++}+/** Routine: arm_init* Function: Runs on the boot CPU, once, on entry from iBoot.-@@ -361,6 +400,7 @@ arm_init(+@@ -361,6 +416,7 @@ arm_init(port_crumb = 100;arm_slide_rebase_and_sign_image();port_crumb = 101;@@ -75,7 +91,7 @@ index ac1713e..f0a504d 100644arm_static_if_init(args);port_crumb = 102;-@@ -382,6 +422,7 @@ arm_init(+@@ -382,6 +438,7 @@ arm_init(PE_init_platform(FALSE, args); /* Get platform expert set up */boot_progress_init();@@ -83,7 +99,7 @@ index ac1713e..f0a504d 100644#if __arm64__configure_timer_apple_regs();-@@ -475,6 +516,7 @@ arm_init(+@@ -475,6 +532,7 @@ arm_init(#endifml_parse_cpu_topology();@@ -91,7 +107,7 @@ index ac1713e..f0a504d 100644boot_cpu_id = ml_get_boot_cpu_number();-@@ -511,15 +553,19 @@ arm_init(+@@ -511,15 +569,19 @@ arm_init(kernel_debug_string_early("kernel_startup_bootstrap");kernel_startup_bootstrap();@@ -111,7 +127,7 @@ index ac1713e..f0a504d 100644if (PE_parse_boot_argn("maxmem", &maxmem, sizeof(maxmem))) {xmaxmem = (uint64_t) maxmem * (1024 * 1024);-@@ -544,7 +590,9 @@ arm_init(+@@ -544,7 +606,9 @@ arm_init(}#endif /* SCHED_HYGIENE_DEBUG */@@ -121,7 +137,7 @@ index ac1713e..f0a504d 100644#if HAS_BP_RETPE_parse_boot_argn("bpret", &bp_ret, sizeof(bp_ret));-@@ -581,10 +629,12 @@ arm_init(+@@ -581,10 +645,12 @@ arm_init(panic("%s: Unable to find 'dram-size' entry in the 'chosen' DT node", __FUNCTION__);}@@ -134,6 +150,14 @@ index ac1713e..f0a504d 100644boot_progress_stage("arm_vm_init enter");port_crumb = 1;arm_vm_init(xmaxmem, args);+@@ -607,6 +673,7 @@ arm_init(+ port_crumb = 5;+ kernel_startup_initialize_upto(STARTUP_SUB_KPRINTF);+ kprintf("kprintf initialized\n");++ linux64_stage(300);++ serialmode = 0;+ if (PE_parse_boot_argn("serial", &serialmode, sizeof(serialmode))) {diff --git a/osfmk/arm/cpu_data.h b/osfmk/arm/cpu_data.hindex 743117b..9dc298b 100644--- a/osfmk/arm/cpu_data.h@@ -179,6 +203,63 @@ index 743117b..9dc298b 100644__asm__ volatile ("msr SPSel, #1 \n""mov %0, sp \n"+diff --git a/osfmk/arm64/arm_vm_init.c b/osfmk/arm64/arm_vm_init.c+index f7f387e..8cd4c20 100644+--- a/osfmk/arm64/arm_vm_init.c++++ b/osfmk/arm64/arm_vm_init.c+@@ -2194,14 +2194,31 @@ arm_vm_init(uint64_t memory_size, boot_args * args)++ arm_vm_physmap_init(args);+ port_crumb = 38;+- set_mmu_ttb_alternate(cpu_ttep & TTBR_BADDR_MASK);++ {++ extern uintptr_t linux64_hook;++ extern void linux64_stage_arg(unsigned long, unsigned long);++ if (linux64_hook) {++ /*++ * LINUX64 (xnulinux): TTBR1 is Linux's. The host installs XNU's kernel L1 entries++ * (the kernel VA range only) into Linux's top-level L1 table; TTBR0 stays Linux's.++ */++ linux64_stage_arg(200, (unsigned long)cpu_tte);++ } else {++ set_mmu_ttb_alternate(cpu_ttep & TTBR_BADDR_MASK);++ }++ }+ port_crumb = 39;++ #if XNU_MONITOR+ ml_enable_monitor();+ #endif++- set_mmu_ttb(invalid_ttep & TTBR_BADDR_MASK);++ {++ extern uintptr_t linux64_hook;++ if (!linux64_hook) {++ set_mmu_ttb(invalid_ttep & TTBR_BADDR_MASK);++ }++ }++ flush_mmu_tlb();+ port_crumb = 40;+@@ -2301,8 +2318,10 @@ arm_vm_init(uint64_t memory_size, boot_args * args)+ }+ #endif+ #endif++ { extern void linux64_stage_ext(unsigned long); linux64_stage_ext(210); }+ pmap_bootstrap(dynamic_memory_begin);+ port_crumb = 41;++ { extern void linux64_stage_ext(unsigned long); linux64_stage_ext(220); }++ disable_preemption();++@@ -2431,6 +2450,7 @@ arm_vm_init(uint64_t memory_size, boot_args * args)+ patch_low_glo_static_region(args->topOfKernelData, avail_start - args->topOfKernelData);+ enable_preemption();+ port_crumb = 43;++ { extern void linux64_stage_ext(unsigned long); linux64_stage_ext(230); }+ }++ /*diff --git a/osfmk/arm64/asm.h b/osfmk/arm64/asm.hindex 84deb3f..767b8f8 100644--- a/osfmk/arm64/asm.h