Skip to content

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

C

Debug: on a repeating fault, print TTBR0/TCR and walk the user table (fki_phys_peek)

Heyang Zhou6a76f3d2e4c2
diff --git a/fki-ko/fki.h b/fki-ko/fki.h
index 97df2ca..d786e69 100644
--- a/fki-ko/fki.h
+++ b/fki-ko/fki.h
@@ -90,6 +90,8 @@ unsigned long fki_regs_lr(void *regs);
void fki_regs_set_pc(void *regs, unsigned long pc);
int fki_fault_register(unsigned long lo, unsigned long hi, int (*handler)(unsigned long far, unsigned long esr, void *regs));
void fki_fault_unregister(void);
+/* Read 8 bytes of RAM by physical address (debugging: walking a foreign page table). */
+fki_u64 fki_phys_peek(fki_u64 pa);
#ifdef __cplusplus
}
#endif
diff --git a/fki-ko/fki_mod.c b/fki-ko/fki_mod.c
index 49162c4..50d9e9f 100644
--- a/fki-ko/fki_mod.c
+++ b/fki-ko/fki_mod.c
@@ -313,6 +313,14 @@ void fki_fault_unregister(void)
}
EXPORT_SYMBOL(fki_fault_unregister);
+fki_u64 fki_phys_peek(fki_u64 pa)
+{
+ if (!pfn_valid(PHYS_PFN(pa)))
+ return ~0ULL;
+ return READ_ONCE(*(u64 *)phys_to_virt(pa & ~7ULL));
+}
+EXPORT_SYMBOL(fki_phys_peek);
+
/* ---- contiguous memory and the top foreign window ---- */
int fki_contig_alloc(unsigned long size, struct fki_region *r)
diff --git a/linux/include/linux/fki.h b/linux/include/linux/fki.h
index 97df2ca..d786e69 100644
--- a/linux/include/linux/fki.h
+++ b/linux/include/linux/fki.h
@@ -90,6 +90,8 @@ unsigned long fki_regs_lr(void *regs);
void fki_regs_set_pc(void *regs, unsigned long pc);
int fki_fault_register(unsigned long lo, unsigned long hi, int (*handler)(unsigned long far, unsigned long esr, void *regs));
void fki_fault_unregister(void);
+/* Read 8 bytes of RAM by physical address (debugging: walking a foreign page table). */
+fki_u64 fki_phys_peek(fki_u64 pa);
#ifdef __cplusplus
}
#endif
diff --git a/xnu-ko/xnu_main.c b/xnu-ko/xnu_main.c
index 158b772..b236759 100644
--- a/xnu-ko/xnu_main.c
+++ b/xnu-ko/xnu_main.c
@@ -149,8 +149,24 @@ static int xnu_fault(unsigned long far, unsigned long esr, void *regs)
/* the same address faulting over and over: vm_fault says yes, the hardware still says no */
if (repeats < 64 && xnu_kfault(far, esr) == 0)
return 0;
- if (repeats >= 64)
+ if (repeats >= 64) {
+ unsigned long long ttbr0, tcr;
+ __asm__ volatile("mrs %0, ttbr0_el1" : "=r"(ttbr0));
+ __asm__ volatile("mrs %0, tcr_el1" : "=r"(tcr));
logs("xnu: the same address keeps faulting after vm_fault resolved it");
+ logkv("xnu: TTBR0", ttbr0);
+ logkv("xnu: TCR ", tcr);
+ /* walk it as the hardware does with this TCR (4K granule, T0SZ from TCR) */
+ unsigned t0sz = tcr & 0x3f, level = t0sz > 24 ? 1 : 0;
+ u64_t table = ttbr0 & 0x0000fffffffff000ULL;
+ for (; level <= 3; level++) {
+ unsigned shift = 39 - 9 * level;
+ u64_t d = fki_phys_peek(table + ((far >> shift) & 511) * 8);
+ logkv(level == 0 ? "xnu: L0" : level == 1 ? "xnu: L1" : level == 2 ? "xnu: L2" : "xnu: L3", d);
+ if ((d & 3) != 3 || level == 3) break;
+ table = d & 0x0000fffffffff000ULL;
+ }
+ }
}
/*
* An XNU fault nothing resolves (a bug, or a NULL dereference): report it and end the run instead of letting Linux