XNU as a loadable kernel module inside Linux (macOS personality for arm64 Linux)
C
Pure EL0 host: user processes no longer die at random with more than one CPU (two races in the fast page-fault path) (#40)
CommaBot803cfe0b7dad
diff --git a/xnu-ko/xnu_main.c b/xnu-ko/xnu_main.cindex d15511d..e01a260 100644--- a/xnu-ko/xnu_main.c+++ b/xnu-ko/xnu_main.c@@ -725,6 +725,17 @@ static long xnu_hook_body(unsigned long call, unsigned long a1, unsigned long a2}return 0;}+ if (call == LINUX64_CALL_UTCB_RESUME) {+ /*+ * The registers of a resume are the ones the host's context holds, not q's (those are from the+ * thread's last full stop): when the context is no longer this thread's as it stopped, they are+ * gone, and running q's would take the thread back to that stop. XNU keeps the two together+ * (linux64_el0_utcb_entry, no preemption in between): say so instead.+ */+ logs_alert("xnu: UTCB_RESUME for a thread whose registers the CPU's user context no longer holds");+ q->result = -116; /* -ESTALE */+ return 0;+ }if (!utcb) {utcb = fki_utcb_create(as);if (!utcb) {diff --git a/xnu-patches/0013-linux64-el0-user-fault-with-nothing-to-install.patch b/xnu-patches/0013-linux64-el0-user-fault-with-nothing-to-install.patchnew file mode 100644index 0000000..d6a78b2--- /dev/null+++ b/xnu-patches/0013-linux64-el0-user-fault-with-nothing-to-install.patch@@ -0,0 +1,151 @@+From: xnulinux+Subject: [PATCH] linux64: a pure EL0 host's user fault with nothing to install runs the instruction again++On a pure EL0 host the user mappings the host has are a cache of XNU's entries: the host drops an address space's+whole cache when it needs the tables for another (32 host address spaces for every process by default), and each page+then comes back at its next fault. linux64_el0_sync_user_pte installs it without vm_fault when XNU's entry allows the+access, and linux64_el0_utcb_entry resumes the thread.++It took "the host changed nothing" for "not resolved" and gave the fault to vm_fault. That is the answer when another+CPU's thread of the same task faulted on the same page and installed it first, or when the host took the address+space's tables away between the fault and the call. vm_fault has nothing to resolve then, and for the commpage, which+no map entry describes, it fails: the thread got a SIGBUS for a fault that was the host's alone. With several CPUs+every boot lost daemons to it (logd three times in a row is a panic), more the more often address spaces were+dropped; with one CPU it cannot happen.++linux64_el0_sync_user_pte now says which it was (installed, nothing to install, not its fault to resolve), and the+instruction runs again in the first two cases. A fault that comes back on one page seventeen times in a row with+nothing to change goes to vm_fault after all.+---+--- a/osfmk/arm/pmap/pmap.c++++ b/osfmk/arm/pmap/pmap.c+@@ -7589,19 +7589,29 @@ linux64_el0_ensure_as(pmap_t pmap)+ return pmap->linux64_as;+ }++-bool++/*++ * A user page fault the host reported: when XNU's entry for the page already allows the access, the host's mapping is++ * what was missing (the host keeps a cache of XNU's entries, and drops an address space's whole cache when it needs++ * the tables for another), and it is installed here without vm_fault.++ *++ * Returns 1 when the host's mapping changed; 0 when XNU's entry allows the access and the host had nothing to change++ * (another CPU's thread of the task installed the same page between this thread's fault and now, or the host has taken++ * the address space's tables away in between and gives it new ones at the next run); -1 when the fault is not one++ * this function resolves (vm_fault's).++ */++int+ linux64_el0_sync_user_pte(pmap_t pmap, vm_offset_t far, uint64_t esr)+ {+ if (!linux64_el0 || !pmap || pmap == kernel_pmap) {+- return false;++ return -1;+ }+ uint64_t as = linux64_el0_ensure_as(pmap);+ if (!as) {+- return false;++ return -1;+ }+ uint64_t ufar = (uint64_t)((int64_t)(far << 8) >> 8);+ if (ufar >= 0x0001000000000000ULL) {+- return false;++ return -1;+ }+ vm_map_address_t va = (vm_map_address_t)(ufar & ~0xfffULL);+ uint32_t ec = (uint32_t)((esr >> 26) & 0x3f);+@@ -7612,7 +7622,7 @@ linux64_el0_sync_user_pte(pmap_t pmap, vm_offset_t far, uint64_t esr)+ pt_entry_t *ptep = pmap_pte(pmap, va);+ if (!ptep) {+ pmap_unlock(pmap, PMAP_LOCK_SHARED);+- return false;++ return -1;+ }+ pt_entry_t pte = ARM_PTE_EMPTY;+ pmap_paddr_t pa = 0;+@@ -7623,7 +7633,7 @@ linux64_el0_sync_user_pte(pmap_t pmap, vm_offset_t far, uint64_t esr)+ pte = *(volatile pt_entry_t *)ptep;+ if (!pte_is_valid(pte) || !(pte & ARM_PTE_AF)) {+ pmap_unlock(pmap, PMAP_LOCK_SHARED);+- return false;++ return -1;+ }+ uint64_t ap = pte & ARM_PTE_APMASK;+ user_read = (ap == ARM_PTE_AP(AP_RORO) || ap == ARM_PTE_AP(AP_RWRW));+@@ -7631,12 +7641,12 @@ linux64_el0_sync_user_pte(pmap_t pmap, vm_offset_t far, uint64_t esr)+ user_exec = !(pte & ARM_PTE_NX);+ if (is_exec ? !user_exec : (is_write ? !user_write : !user_read)) {+ pmap_unlock(pmap, PMAP_LOCK_SHARED);+- return false;++ return -1;+ }+ pa = pte_to_pa(pte);+ if (!pa_valid(pa)) {+ pmap_unlock(pmap, PMAP_LOCK_SHARED);+- return false;++ return -1;+ }+ pai = pa_index(pa);+ pvh_lock(pai);+@@ -7648,7 +7658,7 @@ linux64_el0_sync_user_pte(pmap_t pmap, vm_offset_t far, uint64_t esr)+ }+ if (!locked_pvh) {+ pmap_unlock(pmap, PMAP_LOCK_SHARED);+- return false;++ return -1;+ }++ struct linux64_map_req req = {+@@ -7663,7 +7673,7 @@ linux64_el0_sync_user_pte(pmap_t pmap, vm_offset_t far, uint64_t esr)+ linux64_call(LINUX64_CALL_AS_MAP, &req);+ pvh_unlock(pai);+ pmap_unlock(pmap, PMAP_LOCK_SHARED);+- return req.result > 0;++ return req.result > 0 ? 1 : (req.result == 0 ? 0 : -1);+ }++ bool+--- a/osfmk/arm/pmap/pmap.h++++ b/osfmk/arm/pmap/pmap.h+@@ -925,7 +925,7 @@ extern kern_return_t pmap_test_text_corruption(pmap_paddr_t);+ #endif /* __ARM_KERNEL_PROTECT__ */++ #if !defined(ASSEMBLER) && !defined(__ASSEMBLER__)+-extern bool linux64_el0_sync_user_pte(pmap_t pmap, vm_offset_t far, uint64_t esr);++extern int linux64_el0_sync_user_pte(pmap_t pmap, vm_offset_t far, uint64_t esr);+ extern bool linux64_el0_user_vtop(pmap_t pmap, vm_offset_t va, bool write, pmap_paddr_t *pa_out);+ extern bool linux64_el0_copyio_chunk(pmap_t pmap, vm_offset_t uaddr, bool write, void *bounce, size_t max_len, size_t *out_len);+ extern bool linux64_el0_atomic_op(pmap_t pmap, vm_offset_t uaddr, bool write, size_t width, uint64_t *val);+--- a/osfmk/arm64/sleh.c++++ b/osfmk/arm64/sleh.c+@@ -4084,7 +4084,9 @@ linux64_el0_utcb_entry(arm_saved_state_t *ss, unsigned long disable_jop)+ req.neon_ptr = (uint64_t)(uintptr_t)&ns->ns_64;+ req.fpu_dirty = 0;+++ uint64_t unchanged_page = ~0ULL; /* the page those faults were on */+ unsigned long run_call = LINUX64_CALL_UTCB_RUN;++ unsigned int unchanged = 0; /* faults in a row that found the host's mapping as XNU's entry has it */+ for (;;) {+ req.result = 0;+ linux64_call(run_call, &req);+@@ -4094,7 +4096,20 @@ linux64_el0_utcb_entry(arm_saved_state_t *ss, unsigned long disable_jop)+ (long long)req.result);+ }+ if (req.stop_kind == LINUX64_STOP_PAGEFAULT) {+- if (linux64_el0_sync_user_pte(pmap, (vm_offset_t)req.fault_addr, req.esr)) {++ /*++ * The instruction runs again when the host's mapping was installed, and also when there was++ * nothing to install (linux64_el0_sync_user_pte: another CPU was first, or the host dropped the++ * address space's tables meanwhile): XNU's entry allows the access, so vm_fault has nothing to++ * do for it, and for the commpage, which no map entry describes, vm_fault fails and the thread++ * gets a SIGBUS for a fault that was the host's alone. A fault that keeps coming back on one++ * page with nothing to change is given to vm_fault after all.++ */++ int synced = linux64_el0_sync_user_pte(pmap, (vm_offset_t)req.fault_addr, req.esr);++ if (synced > 0 || unchanged_page != (req.fault_addr & ~0xfffULL)) {++ unchanged = 0;++ unchanged_page = req.fault_addr & ~0xfffULL;++ }++ if (synced > 0 || (synced == 0 && ++unchanged <= 16)) {+ run_call = LINUX64_CALL_UTCB_RESUME;+ continue;+ }diff --git a/xnu-patches/0014-linux64-el0-no-preemption-while-the-host-holds-the-registers.patch b/xnu-patches/0014-linux64-el0-no-preemption-while-the-host-holds-the-registers.patchnew file mode 100644index 0000000..a9896c4--- /dev/null+++ b/xnu-patches/0014-linux64-el0-no-preemption-while-the-host-holds-the-registers.patch@@ -0,0 +1,44 @@+From: xnulinux+Subject: [PATCH] linux64: no preemption while a pure EL0 host holds a thread's registers++After a user page fault the host does not hand the thread's registers over: they stay in the host's user context,+and linux64_el0_utcb_entry resumes it as it is once linux64_el0_sync_user_pte has installed the page. That context+belongs to the CPU, not to the thread: every thread that runs user code on the CPU runs in it.++The window was preemptible. linux64_el0_sync_user_pte takes and drops the pmap's lock and the page's, and dropping+one with an urgent AST pending (another CPU made a thread runnable on this one) switched threads there. The next+thread's run replaced the registers in the host's context; this thread's resume, on whichever CPU it came back, found+a context that was not its own and went on from the registers XNU had: those of its last entry into the kernel. It ran+code a second time on a stack that had moved on, and its return addresses no longer authenticated (EXC_ARM_PAC_FAIL+at a retab or autibsp, in any process, several per boot with four CPUs and none with one).++linux64_el0_utcb_entry now runs with preemption disabled from before the first run until the registers are in its+request and fpu_tid says whose NEON state the host's context holds (a switch between the two lost that state the+same way).+---+--- a/osfmk/arm64/sleh.c++++ b/osfmk/arm64/sleh.c+@@ -4087,6 +4087,16 @@ linux64_el0_utcb_entry(arm_saved_state_t *ss, unsigned long disable_jop)+ uint64_t unchanged_page = ~0ULL; /* the page those faults were on */+ unsigned long run_call = LINUX64_CALL_UTCB_RUN;+ unsigned int unchanged = 0; /* faults in a row that found the host's mapping as XNU's entry has it */++ /*++ * No preemption from here until the thread's registers are in req: after a page fault they are only in the++ * host's user context, which belongs to this CPU and is every thread's that runs here. linux64_el0_sync_user_pte++ * takes and drops the pmap's and the page's locks, and dropping one with an urgent AST pending (another CPU made++ * a thread runnable here) switched threads: the next thread's run replaced the host's registers, and this thread's++ * resume, on whichever CPU, went on from the registers of its last entry into the kernel (code run a second time++ * on a stack that had moved on: its return addresses no longer authenticate, EXC_ARM_PAC_FAIL). The host's NEON++ * state is this thread's too until fpu_tid says so, below.++ */++ disable_preemption();+ for (;;) {+ req.result = 0;+ linux64_call(run_call, &req);+@@ -4129,5 +4139,6 @@ linux64_el0_utcb_entry(arm_saved_state_t *ss, unsigned long disable_jop)+ pcpu->fpu_tid = thread->thread_id;+ pcpu->fpu_live = 1;+ }++ enable_preemption();+ linux64_el0_dispatch(req.x, req.sp, req.pc, req.pstate, req.esr, req.fault_addr);+ }