XNU as a loadable kernel module inside Linux (macOS personality for arm64 Linux)
C
Hosted XNU: calendar from the host's wall clock (upcall 812, fki_realtime; no 30 s IORTC wait), user/system time accounting on EL0 entry and exit, kernel backtraces with Linux frames packed as out-of-reach. Lua, SQLite, git 2.47.1 and swift-argument-parser build and run inside the hosted macOS
Heyang Zhou7fa34ecd0db0
diff --git a/fki-ko/fki.h b/fki-ko/fki.hindex af6b5fe..4d5ebe8 100644--- a/fki-ko/fki.h+++ b/fki-ko/fki.h@@ -74,6 +74,8 @@ void fki_idle_until(fki_u64 counter_deadline, fki_u64 max_ns);* reschedule if it must (RCU, other tasks on this CPU), then return with IRQs and preemption off again.*/void fki_yield(void);+/* The host's wall clock (UTC): seconds and nanoseconds since 1970. */+void fki_realtime(fki_u64 *sec, fki_u64 *nsec);/* Keep the calling task on one CPU (the hosted kernel sees that CPU's MPIDR). */int fki_bind_cpu(int cpu);/*diff --git a/fki-ko/fki_mod.c b/fki-ko/fki_mod.cindex 1c46164..31cb9ee 100644--- a/fki-ko/fki_mod.c+++ b/fki-ko/fki_mod.c@@ -16,6 +16,7 @@#include <linux/printk.h>#include <linux/random.h>#include <linux/slab.h>+#include <linux/timekeeping.h>#include <linux/sched.h>#include <linux/cpumask.h>#include <linux/vmalloc.h>@@ -542,6 +543,16 @@ void fki_yield(void)}EXPORT_SYMBOL(fki_yield);+void fki_realtime(fki_u64 *sec, fki_u64 *nsec)+{+ struct timespec64 ts;++ ktime_get_real_ts64(&ts);+ *sec = ts.tv_sec;+ *nsec = ts.tv_nsec;+}+EXPORT_SYMBOL(fki_realtime);+MODULE_LICENSE("GPL");MODULE_DESCRIPTION("Foreign kernel interface v0 (out-of-tree form for stock kernels)");diff --git a/linux/include/linux/fki.h b/linux/include/linux/fki.hindex af6b5fe..4d5ebe8 100644--- a/linux/include/linux/fki.h+++ b/linux/include/linux/fki.h@@ -74,6 +74,8 @@ void fki_idle_until(fki_u64 counter_deadline, fki_u64 max_ns);* reschedule if it must (RCU, other tasks on this CPU), then return with IRQs and preemption off again.*/void fki_yield(void);+/* The host's wall clock (UTC): seconds and nanoseconds since 1970. */+void fki_realtime(fki_u64 *sec, fki_u64 *nsec);/* Keep the calling task on one CPU (the hosted kernel sees that CPU's MPIDR). */int fki_bind_cpu(int cpu);/*diff --git a/xnu-ko/xnu_main.c b/xnu-ko/xnu_main.cindex f889a3f..29bfa1a 100644--- a/xnu-ko/xnu_main.c+++ b/xnu-ko/xnu_main.c@@ -297,6 +297,10 @@ static long xnu_hook_body(unsigned long stage, unsigned long a1, unsigned long a}return 0;}+ if (stage == 812) { /* the host's wall clock for XNU's calendar: a1 -> u64[2] {seconds, nanoseconds} UTC */+ if (a1) fki_realtime(&((fki_u64 *)a1)[0], &((fki_u64 *)a1)[1]);+ return 0;+ }if (stage == 995) { /* host disk size in bytes into *a1, 0 when there is none */if (a1) *(u64_t *)a1 = hd_file ? hd_size : 0;return 0;diff --git a/xnu-patches/0001-linux64-stage-hook.patch b/xnu-patches/0001-linux64-stage-hook.patchindex c65626f..ee66d68 100644--- a/xnu-patches/0001-linux64-stage-hook.patch+++ b/xnu-patches/0001-linux64-stage-hook.patch@@ -496,10 +496,33 @@ index 0cc7f6e..c07d35c 100644DEBUG_INFO("region = %d\n", currentRegion->type);cnt = 0;diff --git a/iokit/Kernel/IOPlatformExpert.cpp b/iokit/Kernel/IOPlatformExpert.cpp-index 76555f6..b49cb93 100644+index 76555f6..280d150 100644--- a/iokit/Kernel/IOPlatformExpert.cpp+++ b/iokit/Kernel/IOPlatformExpert.cpp-@@ -1540,6 +1540,100 @@ IOPlatformExpert::registerNVRAMController(IONVRAMController * caller)+@@ -1403,15 +1403,18 @@ PESetGMTTimeOfDay(long secs)+ PESetUTCTimeOfDay(secs, 0);+ }+++extern "C" void linux64_stage_arg(unsigned long, unsigned long);+++ void+ PEGetUTCTimeOfDay(clock_sec_t * secs, clock_usec_t * usecs)+ {+ clock_nsec_t nsecs = 0;++- *secs = 0;+- if (gIOPlatform) {+- gIOPlatform->getUTCTimeOfDay(secs, &nsecs);+- }++ /* LINUX64: hosted, no RTC; the host's wall clock (UTC) through upcall 812 */++ uint64_t host[2] = { 0, 0 };++ linux64_stage_arg(812, (unsigned long)host);++ *secs = (clock_sec_t)host[0];++ nsecs = (clock_nsec_t)host[1];++ assert(nsecs < NSEC_PER_SEC);+ *usecs = nsecs / NSEC_PER_USEC;+@@ -1540,6 +1543,100 @@ IOPlatformExpert::registerNVRAMController(IONVRAMController * caller)publishResource("IONVRAM");}@@ -600,7 +623,7 @@ index 76555f6..b49cb93 100644IOReturnIOPlatformExpert::callPlatformFunction(const OSSymbol *functionName,bool waitForFunction,-@@ -1564,7 +1658,48 @@ IOPlatformExpert::callPlatformFunction(const OSSymbol *functionName,+@@ -1564,7 +1661,48 @@ IOPlatformExpert::callPlatformFunction(const OSSymbol *functionName,}if (waitForFunction) {@@ -650,7 +673,7 @@ index 76555f6..b49cb93 100644} else {_resources = getResourceService();}-@@ -2341,6 +2476,8 @@ IOPlatformDevice::getResources( void )+@@ -2341,6 +2479,8 @@ IOPlatformDevice::getResources( void )* the kernel with a suitable message.*********************************************************************/@@ -659,7 +682,7 @@ index 76555f6..b49cb93 100644class IOPanicPlatform : IOPlatformExpert {OSDeclareDefaultStructors(IOPanicPlatform);-@@ -2361,6 +2498,19 @@ IOPanicPlatform::start(IOService * provider)+@@ -2361,6 +2501,19 @@ IOPanicPlatform::start(IOService * provider)platform_name = provider->getName();}@@ -679,6 +702,19 @@ index 76555f6..b49cb93 100644panic("Unable to find driver for this platform: \"%s\".",platform_name);+diff --git a/iokit/Kernel/IOStartIOKit.cpp b/iokit/Kernel/IOStartIOKit.cpp+index f09a1dd..a819063 100644+--- a/iokit/Kernel/IOStartIOKit.cpp++++ b/iokit/Kernel/IOStartIOKit.cpp+@@ -78,7 +78,7 @@ IOKitInitializeTime( void )+ t.tv_nsec = 0;++ // RTC is not present on this target+-#ifndef BCM2837++#if 0 /* LINUX64: hosted, the calendar comes from the host's wall clock (PEGetUTCTimeOfDay); no IORTC to wait 30 s for */+ IOService::waitForService(+ IOService::resourceMatching("IORTC"), &t );+ #endifdiff --git a/iokit/bsddev/IOKitBSDInit.cpp b/iokit/bsddev/IOKitBSDInit.cppindex ae82fc8..3fd837c 100644--- a/iokit/bsddev/IOKitBSDInit.cpp@@ -2405,10 +2441,18 @@ index d211433..dc247da 100644// No need for atomic - These are per CPU and interrupts are disableddiff --git a/osfmk/arm64/sleh.c b/osfmk/arm64/sleh.c-index d8f1b19..8a617e2 100644+index d8f1b19..cc88f40 100644--- a/osfmk/arm64/sleh.c+++ b/osfmk/arm64/sleh.c-@@ -3646,3 +3646,223 @@ sleh_panic_lockdown_should_initiate_el1_sp0_sync(uint64_t esr, uint64_t elr,+@@ -78,6 +78,7 @@+ #if CONFIG_TELEMETRY+ #include <kern/telemetry.h>+ #include <kern/trap_telemetry.h>++#include <kern/recount.h>+ #endif++ #include <prng/entropy.h>+@@ -3646,3 +3647,229 @@ sleh_panic_lockdown_should_initiate_el1_sp0_sync(uint64_t esr, uint64_t elr,}__SECURITY_STACK_DISALLOWED_POP#endif /* CONFIG_SPTM */@@ -2540,6 +2584,9 @@ index d8f1b19..8a617e2 100644+ * IA/DA keys for it; hosted, user code runs with SCTLR_EL1.EnIA/EnDA cleared (linux64_el0_run puts them back on+ * the next exception), or a blraaz on one of its unsigned function pointers fails (EXC_ARM_PAC_FAIL).+ */++#if PRECISE_USER_KERNEL_TIME++ recount_enter_user(); /* user time vs system time, as locore's return to user space does */++#endif+ linux64_el0_run(thread->machine.upcb, (thread->machine.arm_machine_flags & ARM_MACHINE_THREAD_DISABLE_USER_JOP) != 0);+}+@@ -2562,6 +2609,9 @@ index d8f1b19..8a617e2 100644+ arm_saved_state_t *ss = thread->machine.upcb;+ extern unsigned long linux64_dbg[16];+++#if PRECISE_USER_KERNEL_TIME++ recount_leave_user(); /* interrupts are still off here, as on locore's exception entry */++#endif+ linux64_dbg[10]++; /* LINUX64 debug: EL0 exceptions, the last one's ESR, FAR, PC */+ linux64_dbg[11] = f->esr;+ linux64_dbg[12] = f->far;@@ -2768,6 +2818,37 @@ index 4224c9d..4a1fe94 100644#define assertf(ex, fmt, args...) ({ \if (mach_assert_enabled_expr(ex) && __builtin_expect(!(ex), 0L)) { \+diff --git a/osfmk/kern/backtrace.c b/osfmk/kern/backtrace.c+index 77044be..2c195af 100644+--- a/osfmk/kern/backtrace.c++++ b/osfmk/kern/backtrace.c+@@ -48,7 +48,7 @@+ #include <arm64/mte.h>+ #endif // HAS_MTE++-#if __x86_64__++#if __x86_64__ || __arm64__ /* LINUX64: hosted, a kernel stack also has frames in Linux's text, beyond 32 bits of stext */+ static void+ _backtrace_packed_out_of_reach(void)+ {+@@ -58,7 +58,7 @@ _backtrace_packed_out_of_reach(void)+ //+ // This is an Intel only issue.+ }+-#endif // __x86_64__++#endif // __x86_64__ || __arm64__++ // Pack an address according to a particular packing format.+ static size_t+@@ -74,7 +74,7 @@ _backtrace_pack_addr(backtrace_pack_t packing, uint8_t *dst, size_t dst_size,+ case BTP_KERN_OFFSET_32:;+ uintptr_t addr_delta = addr - vm_kernel_stext;+ int32_t addr_packed = (int32_t)addr_delta;+-#if __x86_64__++#if __x86_64__ || __arm64__+ if ((uintptr_t)(int32_t)addr_delta != addr_delta) {+ addr = (vm_offset_t)&_backtrace_packed_out_of_reach;+ addr_delta = addr - vm_kernel_stext;diff --git a/osfmk/kern/debug.c b/osfmk/kern/debug.cindex 0eae1f6..3253146 100644--- a/osfmk/kern/debug.c