XNU as a loadable kernel module inside Linux (macOS personality for arm64 Linux)
C
Hosted XNU on a pure EL0 host: more than about 8 GB of memory (the physical aperture goes below the kernel) (#35)
CommaBot33cf50bcccfa
diff --git a/fki-ko/fki.h b/fki-ko/fki.hindex 6fb84f9..ecc318c 100644--- a/fki-ko/fki.h+++ b/fki-ko/fki.h@@ -1,6 +1,6 @@/* SPDX-License-Identifier: GPL-2.0-only WITH Linux-syscall-note *//*- * Foreign kernel interface (FKI), version 2.+ * Foreign kernel interface (FKI), version 3.** The only interface between Linux and a foreign-kernel module. It is* semantic and shaped like a hypervisor interface: plain integer types and@@ -10,7 +10,7 @@#ifndef _LINUX_FKI_H#define _LINUX_FKI_H-#define FKI_VERSION 2+#define FKI_VERSION 3#ifndef __KERNEL__#include <stdint.h>@@ -106,8 +106,12 @@ void fki_window_set(unsigned long long *slot, fki_u64 desc);* fki_high_map/unmap/flush mirror additional kernel-VA mappings backed by xpa in [pa_base, pa_base + size).* fki_high_map only supports new mappings or in-place same-PFN re-protection; cross-PFN replacement* returns -EBUSY and requires the caller to fki_high_unmap while quiescent first.+ * fki_kmem_alias maps the first size bytes of the memory a second time, at va, in the same frames and as RWX and+ * as permanent as at high_va (2 MiB aligned, apart from the first mapping, nothing else mapped there). A later+ * call with the same va and another size grows or shrinks it. Only before the foreign kernel runs.*/int fki_kmem_alloc(unsigned long size, unsigned long high_va, fki_u64 pa_base, struct fki_region *r);+int fki_kmem_alias(struct fki_region *r, unsigned long va, unsigned long size);void fki_kmem_free(struct fki_region *r);int fki_high_map(unsigned long va, fki_u64 xpa, unsigned long size, int prot);void fki_high_unmap(unsigned long va, unsigned long size);diff --git a/fki-ko/fki_arm64.c b/fki-ko/fki_arm64.cindex 773a15b..5bc3722 100644--- a/fki-ko/fki_arm64.c+++ b/fki-ko/fki_arm64.c@@ -539,6 +539,8 @@ EXPORT_SYMBOL(fki_cpu_mpidr);int fki_kmem_alloc(unsigned long size, unsigned long high_va, fki_u64 pa_base, struct fki_region *r) { return -EOPNOTSUPP; }EXPORT_SYMBOL(fki_kmem_alloc);+int fki_kmem_alias(struct fki_region *r, unsigned long va, unsigned long size) { return -EOPNOTSUPP; }+EXPORT_SYMBOL(fki_kmem_alias);void fki_kmem_free(struct fki_region *r) {}EXPORT_SYMBOL(fki_kmem_free);int fki_high_map(unsigned long va, fki_u64 xpa, unsigned long size, int prot) { return -EOPNOTSUPP; }diff --git a/fki-ko/fki_l4h.c b/fki-ko/fki_l4h.cindex 2ff724f..69db247 100644--- a/fki-ko/fki_l4h.c+++ b/fki-ko/fki_l4h.c@@ -261,6 +261,8 @@ struct fki_kmem_chunk {unsigned long large_slot;struct page **small_pgs; /* 512 entries when !is_large */unsigned long *small_slots; /* 512 entries when !is_large */+ unsigned long alias_large_slot; /* the chunk's second mapping (fki_kmem_alias), when is_large */+ unsigned long *alias_small_slots; /* the same, 512 entries when !is_large */};#define FKI_AS_SLOT_BITS 30@@ -318,6 +320,8 @@ struct fki_kmem {unsigned long high_va;fki_u64 pa_base;unsigned long size;+ unsigned long alias_va; /* fki_kmem_alias: the first alias_size bytes are mapped here as well */+ unsigned long alias_size;unsigned long nchunks;struct fki_kmem_chunk *chunks;struct list_head pts;@@ -436,6 +440,24 @@ static int xpa_to_gpa(struct fki_kmem *km, fki_u64 xpa, unsigned long *gpa_out)return 0;}+static void kmem_unalias_chunk(struct fki_kmem_chunk *ch)+{+ unsigned long j;++ if (ch->alias_large_slot) {+ l4h_slot_delete(ch->alias_large_slot);+ ch->alias_large_slot = 0;+ }+ if (ch->alias_small_slots) {+ for (j = 0; j < 512; j++) {+ if (ch->alias_small_slots[j])+ l4h_slot_delete(ch->alias_small_slots[j]);+ }+ kfree(ch->alias_small_slots);+ ch->alias_small_slots = NULL;+ }+}+static void kmem_cleanup(struct fki_kmem *km){struct fki_pt_node *node, *tmp;@@ -458,6 +480,7 @@ static void kmem_cleanup(struct fki_kmem *km)for (i = 0; i < km->nchunks; i++) {struct fki_kmem_chunk *ch = &km->chunks[i];+ kmem_unalias_chunk(ch);if (ch->is_large) {if (ch->large_slot)l4h_slot_delete(ch->large_slot);@@ -665,6 +688,98 @@ fail:}EXPORT_SYMBOL(fki_kmem_alloc);+/* The second mapping of one 2 MiB chunk, at cva: a large frame where the first is one. */+static int kmem_alias_chunk(struct fki_kmem *km, struct fki_kmem_chunk *ch, unsigned long cva)+{+ const int prot = L4H_PROT_READ | L4H_PROT_WRITE | L4H_PROT_EXEC;+ unsigned long flags, j;+ int err = 0;++ if (ch->is_large) {+ unsigned long lf = l4h_large_frame(ch->large_gpa);+ unsigned long slot = lf ? l4h_cap_copy(lf, L4H_RIGHTS_ALL) : 0;++ if (!slot)+ return -ENOMEM;+ spin_lock_irqsave(&km->lock, flags);+ err = map_high_frame(km, slot, cva, prot | L4H_PROT_LARGE);+ spin_unlock_irqrestore(&km->lock, flags);+ if (err) {+ l4h_slot_delete(slot);+ return err;+ }+ ch->alias_large_slot = slot;+ return 0;+ }+ ch->alias_small_slots = kcalloc(512, sizeof(*ch->alias_small_slots), GFP_KERNEL);+ if (!ch->alias_small_slots)+ return -ENOMEM;+ for (j = 0; j < 512 && !err; j++) {+ unsigned long slot = l4h_cap_copy(L4H_FRAME_CPTR(page_to_phys(ch->small_pgs[j])),+ L4H_RIGHTS_ALL);++ if (!slot) {+ err = -ENOMEM;+ break;+ }+ spin_lock_irqsave(&km->lock, flags);+ err = map_high_frame(km, slot, cva + (j << PAGE_SHIFT), prot);+ spin_unlock_irqrestore(&km->lock, flags);+ if (err)+ l4h_slot_delete(slot);+ else+ ch->alias_small_slots[j] = slot;+ }+ if (err)+ kmem_unalias_chunk(ch);+ return err;+}++int fki_kmem_alias(struct fki_region *r, unsigned long va, unsigned long size)+{+ struct fki_kmem *km;+ unsigned long va_end, km_va_end, old_n, n, i, flags;+ int err = 0;++ if (!r || ((va | size) & (SZ_2M - 1)) || check_add_overflow(va, size, &va_end))+ return -EINVAL;+ mutex_lock(&fki_kmem_mutex);+ km = r->handle;+ if (!km || rcu_access_pointer(active_kmem) != km) {+ mutex_unlock(&fki_kmem_mutex);+ return -ENODEV;+ }+ n = size >> 21;+ old_n = km->alias_size >> 21;+ km_va_end = km->high_va + km->size; /* fki_kmem_alloc checked the sum */+ if (n > km->nchunks || (va < km_va_end && va_end > km->high_va) ||+ (old_n && va != km->alias_va)) {+ mutex_unlock(&fki_kmem_mutex);+ return -EINVAL;+ }+ for (i = old_n; i < n && !err; i++)+ err = kmem_alias_chunk(km, &km->chunks[i], va + (i << 21));+ if (err) {+ /* back to what it was: i - 1 is the chunk that failed, and it has undone itself */+ n = old_n;+ i--;+ } else {+ i = old_n;+ }+ spin_lock_irqsave(&km->lock, flags);+ km->alias_va = va;+ km->alias_size = n << 21;+ spin_unlock_irqrestore(&km->lock, flags);+ if (i > n) {+ while (i-- > n)+ kmem_unalias_chunk(&km->chunks[i]);+ l4h_vspace_flush(L4H_CPTR(L4H_SLOT_HIGH_VSPACE));+ }+ mutex_unlock(&fki_kmem_mutex);+ return err;+}+EXPORT_SYMBOL(fki_kmem_alias);+static void fki_as_drain_list(struct fki_kmem *km);void fki_kmem_free(struct fki_region *r)@@ -739,12 +854,25 @@ static int prot_to_l4h(int prot)return p;}+/* The region's second mapping (fki_kmem_alias), which is as permanent as the aperture. */+static bool high_overlaps_alias(struct fki_kmem *km, unsigned long va, unsigned long va_end)+{+ return km->alias_size && va < km->alias_va + km->alias_size && va_end > km->alias_va;+}++static bool high_in_alias(struct fki_kmem *km, unsigned long va, unsigned long va_end)+{+ return km->alias_size && va >= km->alias_va && va_end <= km->alias_va + km->alias_size;+}+static bool high_overlaps_aperture(struct fki_kmem *km, unsigned long va, unsigned long va_end){unsigned long km_va_end, ap_start, ap_end;if (check_add_overflow(km->high_va, km->size, &km_va_end))return true;+ if (high_overlaps_alias(km, va, va_end))+ return true;ap_start = round_down(km->high_va, SZ_2M);ap_end = round_up(km_va_end, SZ_2M);if (ap_end < km_va_end)@@ -868,6 +996,12 @@ int fki_high_map(unsigned long va, fki_u64 xpa, unsigned long size, int prot)fki_kmem_put(km);return 0;}+ /* So is its second mapping (fki_kmem_alias). */+ if (high_in_alias(km, va, va_end) &&+ xpa >= km->pa_base && (va - km->alias_va) == (unsigned long)(xpa - km->pa_base)) {+ fki_kmem_put(km);+ return 0;+ }/* Reject any non-identity or cross-boundary overlap with the permanent 2 MiB aperture. */if (high_overlaps_aperture(km, va, va_end)) {fki_kmem_put(km);@@ -1109,7 +1243,11 @@ static void fki_high_unmap_km(struct fki_kmem *km, unsigned long va, unsigned lofor (cur_va = lo[i]; cur_va < hi[i]; cur_va += PAGE_SIZE) {unsigned long vpn = cur_va >> PAGE_SHIFT;unsigned long idx = 0;- struct fki_high_txn *txn = fki_high_txn_find_locked(km, cur_va, &idx);+ struct fki_high_txn *txn;++ if (high_overlaps_alias(km, cur_va, cur_va + PAGE_SIZE))+ continue;+ txn = fki_high_txn_find_locked(km, cur_va, &idx);if (txn) {if (txn->pages[idx].stashed) {@@ -1210,6 +1348,8 @@ int fki_high_txn_begin(unsigned long va, unsigned long size, void **handle_out)if (cur_va >= ap_lo && cur_va < ap_hi)continue;+ if (high_overlaps_alias(km, cur_va, cur_va + PAGE_SIZE))+ continue;old = xa_load(&km->dyn_slots, vpn);if (!old)continue;@@ -1346,10 +1486,13 @@ static bool high_range_readable_locked(struct fki_kmem *km, unsigned long va, unreturn false;if (va >= km->high_va && end <= km_end)return true;+ if (high_in_alias(km, va, end))+ return true;page_va = va & PAGE_MASK;last_page = (end - 1) & PAGE_MASK;for (;; page_va += PAGE_SIZE) {if (!(page_va >= km->high_va && page_va < km_end) &&+ !high_overlaps_alias(km, page_va, page_va + PAGE_SIZE) &&!xa_load(&km->dyn_slots, page_va >> PAGE_SHIFT))return false;if (page_va == last_page)diff --git a/linux/drivers/fki/fki.c b/linux/drivers/fki/fki.cindex 6d61b80..86fe9b6 100644--- a/linux/drivers/fki/fki.c+++ b/linux/drivers/fki/fki.c@@ -199,6 +199,8 @@ EXPORT_SYMBOL(fki_log_alert);int fki_kmem_alloc(unsigned long size, unsigned long high_va, fki_u64 pa_base, struct fki_region *r) { return -EOPNOTSUPP; }EXPORT_SYMBOL(fki_kmem_alloc);+int fki_kmem_alias(struct fki_region *r, unsigned long va, unsigned long size) { return -EOPNOTSUPP; }+EXPORT_SYMBOL(fki_kmem_alias);void fki_kmem_free(struct fki_region *r) {}EXPORT_SYMBOL(fki_kmem_free);int fki_high_map(unsigned long va, fki_u64 xpa, unsigned long size, int prot) { return -EOPNOTSUPP; }diff --git a/linux/include/linux/fki.h b/linux/include/linux/fki.hindex 6fb84f9..ecc318c 100644--- a/linux/include/linux/fki.h+++ b/linux/include/linux/fki.h@@ -1,6 +1,6 @@/* SPDX-License-Identifier: GPL-2.0-only WITH Linux-syscall-note *//*- * Foreign kernel interface (FKI), version 2.+ * Foreign kernel interface (FKI), version 3.** The only interface between Linux and a foreign-kernel module. It is* semantic and shaped like a hypervisor interface: plain integer types and@@ -10,7 +10,7 @@#ifndef _LINUX_FKI_H#define _LINUX_FKI_H-#define FKI_VERSION 2+#define FKI_VERSION 3#ifndef __KERNEL__#include <stdint.h>@@ -106,8 +106,12 @@ void fki_window_set(unsigned long long *slot, fki_u64 desc);* fki_high_map/unmap/flush mirror additional kernel-VA mappings backed by xpa in [pa_base, pa_base + size).* fki_high_map only supports new mappings or in-place same-PFN re-protection; cross-PFN replacement* returns -EBUSY and requires the caller to fki_high_unmap while quiescent first.+ * fki_kmem_alias maps the first size bytes of the memory a second time, at va, in the same frames and as RWX and+ * as permanent as at high_va (2 MiB aligned, apart from the first mapping, nothing else mapped there). A later+ * call with the same va and another size grows or shrinks it. Only before the foreign kernel runs.*/int fki_kmem_alloc(unsigned long size, unsigned long high_va, fki_u64 pa_base, struct fki_region *r);+int fki_kmem_alias(struct fki_region *r, unsigned long va, unsigned long size);void fki_kmem_free(struct fki_region *r);int fki_high_map(unsigned long va, fki_u64 xpa, unsigned long size, int prot);void fki_high_unmap(unsigned long va, unsigned long size);diff --git a/xnu-ko/xnu_main.c b/xnu-ko/xnu_main.cindex a1596e7..d15511d 100644--- a/xnu-ko/xnu_main.c+++ b/xnu-ko/xnu_main.c@@ -1040,7 +1040,7 @@ static long xnu_hook_body(unsigned long call, unsigned long a1, unsigned long a2{ /* the frame-pointer chain from here through XNU's frames (the hook runs on XNU's stack); return addresses* with their pointer-authentication bits replaced by the kernel's all-ones top */unsigned long *fp = el0_mode ? (unsigned long *)xnu_worlds[this_slot()]->xnu_regs[10] : __builtin_frame_address(0);- unsigned long fp_lo = el0_mode ? (unsigned long)(img.vmlow & ~0x1fffffULL) : 0xffffff8000000000UL;+ unsigned long fp_lo = el0_mode ? (unsigned long)XNU_VA_LO : 0xffffff8000000000UL; /* EL0: a stack can be in the aperture, below the image */unsigned long fp_hi = 0xfffffffc00000000UL;for (i = 0; i < 24 && fp && !((unsigned long)fp & 15) &&(unsigned long)fp >= fp_lo && (unsigned long)fp + 16 <= fp_hi; i++) {@@ -2284,18 +2284,40 @@ static int run_m2b_el0(void)u64_t overhead = image_off + image_size + 0x60000 + BOOT_EXTRA;if (overhead < image_off || ram_bytes > (~0ULL) - overhead) { logs("xnu: ram_mb overflow"); return -22; }u64_t want = overhead + ram_bytes;- int err = fki_kmem_alloc((unsigned long)want, (unsigned long)XNU_WINDOW_BASE, XNU_FAKE_PHYS, &img.region);+ /*+ * All of DRAM goes below the kernel (LINUX64_EL0_APERTURE_VA: XNU's physical aperture, the host's permanent+ * linear mapping in 2 MiB frames), so that it takes none of the room after the kernel's link address, which+ * the kernel map's ranges need whole: at the kernel's base it left them too little above about 8 GiB. The+ * image and the boot data are mapped a second time at the kernel's base, where they are linked to run+ * (fki_kmem_alias: the same frames, 2 MiB ones where the aperture has them, and as permanent).+ */+ if (XNU_WINDOW_BASE < LINUX64_EL0_APERTURE_END || want > LINUX64_EL0_APERTURE_END - LINUX64_EL0_APERTURE_VA - 0x200000) {+ logs("xnu: ram_mb does not fit below the kernel (or an unexpected link address)"); return -22;+ }+ int err = fki_kmem_alloc((unsigned long)want, (unsigned long)LINUX64_EL0_APERTURE_VA, XNU_FAKE_PHYS, &img.region);if (err) { logkv("xnu: EL0 kmem allocation failed, err", (u64_t)err); return err; }- logkv("xnu: EL0 high window base", (u64_t)img.region.va);- logkv("xnu: EL0 high window size", img.region.size);+ logkv("xnu: EL0 aperture base", (u64_t)img.region.va);+ logkv("xnu: EL0 aperture size", img.region.size);logkv("xnu: EL0 synthetic phys base", img.region.pa);unsigned char *base = (unsigned char *)XNU_WINDOW_BASE;+ u64_t head = (overhead + 0x1fffff) & ~0x1fffffULL; /* at least what build_boot fills; cut to topOfKernelData below */+ err = fki_kmem_alias(&img.region, (unsigned long)XNU_WINDOW_BASE, (unsigned long)head);+ if (err) { logkv("xnu: EL0 mapping at the kernel's base failed, err", (u64_t)err); return err; }err = macho_copy(&img, base + image_off);if (err) { logkv("xnu: macho_copy failed, err", (u64_t)err); return err; }img.slide = 0;struct boot_args_v2 *ba = build_boot(base, img.region.pa, img.region.size, image_off, image_size);if (!ba) return -22;+ /* the kernel's static range ends at topOfKernelData rounded up to 2 MiB (arm_vm_init); its heap starts there */+ u64_t top = (ba->topOfKernelData - img.region.pa + 0x1fffff) & ~0x1fffffULL;+ if (top > head) { logs("xnu: boot data beyond the mapping at the kernel's base"); return -22; }+ if (top < head) { /* what follows is the kernel map's */+ err = fki_kmem_alias(&img.region, (unsigned long)XNU_WINDOW_BASE, (unsigned long)top);+ if (err) { logkv("xnu: EL0 mapping at the kernel's base could not be cut, err", (u64_t)err); return err; }+ }+ logkv("xnu: EL0 kernel static range base", XNU_WINDOW_BASE);+ logkv("xnu: EL0 kernel static range end", XNU_WINDOW_BASE + top);return enter_arm_init(ba);}diff --git a/xnu-patches/0011-linux64-el0-physmap-below-the-kernel.patch b/xnu-patches/0011-linux64-el0-physmap-below-the-kernel.patchnew file mode 100644index 0000000..95c4107--- /dev/null+++ b/xnu-patches/0011-linux64-el0-physmap-below-the-kernel.patch@@ -0,0 +1,241 @@+From: xnulinux+Subject: [PATCH] linux64: on a pure EL0 host too, the physical aperture goes below the kernel++0009 moved the physical aperture below the kernel for a host that runs XNU at EL1, and left a pure EL0 host as it+was: there the aperture is not XNU's to place, it is the host's permanent linear mapping of all of DRAM, and xnu.ko+put that at the kernel's base, so it took its room from the 48 GiB after the kernel's link address and the kernel+map's ranges did not fit above about 8 GiB of DRAM.++xnu.ko now puts that mapping at LINUX64_EL0_APERTURE_VA (linux64_abi.h, version 5), which is VM_MIN_KERNEL_ADDRESS, and+maps the image and the boot data (up to topOfKernelData, rounded up to 2 MiB) a second time at the kernel's base,+where the kernel is linked to run. Here:++- physmap_base is that address and physmap_end is real_phys_size above it; the kernel's static range ends with the+ boot data again (rounded up to a twig), and the heap follows it.+- The table of the aperture has what the static range does not, as on every other system: the memory before the image+ and everything after the boot data. Its second entry is set before the first page table page is allocated, because+ that memory is no longer mapped at the kernel's base.+- The addresses the host maps for good, which XNU's tables only describe and never mirror to the host, are now two+ ranges, the aperture and the kernel's static range: linux64_el0_va_permanent.+- ml_static_mfree keeps freeing the static range on this host, as it always did there.+---+--- a/osfmk/arm/pmap/pmap.c++++ b/osfmk/arm/pmap/pmap.c+@@ -662,6 +662,18 @@ pte_is_xs(const pt_attr_t *pt_attr, pt_entry_t pte)+ arm64_sync_tlb(strong); \+ }+++/*++ * Pure EL0 host: the kernel addresses the host maps for good, and XNU's tables only describe: the physical aperture++ * (all of DRAM, in 2 MiB frames) and the kernel's static range at its base (the image and the boot data). The two++ * were one range while the aperture began at the kernel's base.++ */++static inline bool++linux64_el0_va_permanent(vm_map_address_t va)++{++ return (physmap_end > physmap_base && va >= physmap_base && va < physmap_end) ||++ (va >= gVirtBase && va < static_memory_end);++}+++ static inline void+ linux64_el0_mirror_pte(pmap_t pmap, vm_map_address_t va, pt_entry_t pte, uint64_t size)+ {+@@ -670,7 +682,7 @@ linux64_el0_mirror_pte(pmap_t pmap, vm_map_address_t va, pt_entry_t pte, uint64_+ }+ __builtin_arm_dmb(DMB_ISH);+ if (pmap == kernel_pmap) {+- if (physmap_end > physmap_base && va >= physmap_base && va < physmap_end) {++ if (linux64_el0_va_permanent(va)) {+ return;+ }+ struct linux64_map_req req = { .va = va, .size = size };+@@ -741,7 +753,7 @@ pmap_el0_kmap_range_supported(vm_map_address_t va, pmap_paddr_t pa, vm_map_size_+ for (vm_map_size_t off = 0; off < size; off += PAGE_SIZE) {+ vm_map_address_t cva = va + off;+ pmap_paddr_t cpa = pa + off;+- if (physmap_end > physmap_base && cva >= physmap_base && cva < physmap_end) {++ if (linux64_el0_va_permanent(cva)) {+ continue;+ }+ if (!pa_valid(cpa)) {+@@ -759,7 +771,7 @@ pmap_el0_kmap_pte_supported(pmap_t pmap, vm_map_address_t va)+ if (!linux64_el0 || pmap != kernel_pmap) {+ return true;+ }+- if (physmap_end > physmap_base && va >= physmap_base && va < physmap_end) {++ if (linux64_el0_va_permanent(va)) {+ return true;+ }+ pn = pmap_find_phys(pmap, va);+@@ -786,7 +798,7 @@ pmap_el0_kmap_pte_ready(pmap_t pmap, vm_map_address_t va, vm_prot_t req_prot)+ if (!pte_is_valid(pte)) {+ return false;+ }+- if (!(physmap_end > physmap_base && va >= physmap_base && va < physmap_end) &&++ if (!(linux64_el0_va_permanent(va)) &&+ !pa_valid(pte_to_pa(pte))) {+ return false;+ }+@@ -7784,7 +7796,7 @@ static bool+ linux64_el0_kpage_check_locked(vm_offset_t addr, bool require_write, pmap_paddr_t *pa_out)+ {+ vm_map_address_t va = (vm_map_address_t)(addr & ~0xfffULL);+- bool in_physmap = (physmap_end > physmap_base && va >= physmap_base && va < physmap_end);++ bool in_physmap = (linux64_el0_va_permanent(va));+ tt_entry_t *ttep = pmap_tte(kernel_pmap, va);+ if (!ttep) {+ return false;+--- a/osfmk/arm64/arm_vm_init.c++++ b/osfmk/arm64/arm_vm_init.c+@@ -414,7 +414,12 @@ linux64_el0_ptov_vtop(vm_offset_t va)+ return (pmap_paddr_t)(va - ptov_table[i].va) + ptov_table[i].pa;+ }+ }+- if ((va >= gVirtBase) && (va < (gVirtBase + real_phys_size))) {++ /*++ * The kernel's static range (the image and the boot data, which the host maps at the kernel's base) is linear++ * from gVirtBase; the rest of that range is the heap's. Before arm_vm_init has set static_memory_end nothing++ * else is mapped there.++ */++ if ((va >= gVirtBase) && (va < (static_memory_end ? static_memory_end : (gVirtBase + real_phys_size)))) {+ return (pmap_paddr_t)(va - gVirtBase) + gPhysBase;+ }+ return 0;+@@ -1658,19 +1663,35 @@ arm_vm_physmap_init(boot_args *args)++ if (linux64_el0) {+ /*+- * Pure EL0 host: reuse the permanent 2 MiB linear aperture+- * [gVirtBase, gVirtBase + real_phys_size) directly as physmap so+- * large-RAM boots (>= 6 GiB) do not duplicate physmap VA space.++ * Pure EL0 host: the physical aperture is the host's permanent linear mapping of all of DRAM at++ * LINUX64_EL0_APERTURE_VA (physmap_base), below the kernel, so that it takes none of the heap's room++ * whatever the DRAM size (it was at the kernel's base, and above about 8 GiB the kernel map's ranges no++ * longer fit after it). The table has what the kernel's static range does not: the memory before the++ * image and everything after the boot data. The image and the boot data keep their addresses at the++ * kernel's base, where the host maps them a second time.+ */+- if (segLOWEST > gVirtBase) {+- arm_vm_page_granular_RWNX(gVirtBase, segLOWEST - gVirtBase,+- ARM64_GRANULE_ALLOW_BLOCK | ARM64_GRANULE_ALLOW_HINT);++ const vm_offset_t top_of_kernel_data = gVirtBase + (args->topOfKernelData - gPhysBase);++ const pmap_paddr_t el0_pa[2] = { gPhysBase, args->topOfKernelData };++ const vm_size_t el0_len[2] = { segLOWEST - gVirtBase, real_avail_end - args->topOfKernelData };++ unsigned int n = 0;++++ arm_vm_page_granular_RWNX(end_kern, top_of_kernel_data - end_kern,++ ARM64_GRANULE_ALLOW_BLOCK | ARM64_GRANULE_ALLOW_HINT); /* Device Tree, RAM Disk (if present), bootArgs */++ for (unsigned int i = 0; i < 2; i++) {++ if (el0_len[i] == 0) {++ continue;++ }++ temp_ptov_table[n].pa = el0_pa[i];++ temp_ptov_table[n].va = physmap_base + (el0_pa[i] - gPhysBase);++ temp_ptov_table[n].len = round_page(el0_len[i]);++ /* pa_offset as arm_vm_physmap_slide has it: the aperture's address less the one at the kernel's base */++ arm_vm_page_granular_prot(temp_ptov_table[n].va, temp_ptov_table[n].len,++ temp_ptov_table[n].va - (el0_pa[i] - gPhysBase + gVirtBase), 1, AP_RWNA, 1,++ ARM64_GRANULE_ALLOW_BLOCK | ARM64_GRANULE_ALLOW_HINT, ARM64_PAGE_UNGUARDED);++ assert((temp_ptov_table[n].va + temp_ptov_table[n].len) <= physmap_end);++ n++;+ }+- arm_vm_page_granular_RWNX(end_kern, (gVirtBase + real_phys_size) - end_kern,+- ARM64_GRANULE_ALLOW_BLOCK | ARM64_GRANULE_ALLOW_HINT);+- temp_ptov_table[0].pa = gPhysBase;+- temp_ptov_table[0].va = gVirtBase;+- temp_ptov_table[0].len = real_phys_size;++ qsort(temp_ptov_table, PTOV_TABLE_SIZE, sizeof(temp_ptov_table[0]), cmp_ptov_entries);+ memcpy(ptov_table, temp_ptov_table, sizeof(ptov_table));+ return;+ }+@@ -1922,10 +1943,10 @@ arm_vm_init(uint64_t memory_size, boot_args * args)+ * pmap_virtual_region already keeps out of the heap). After the kernel it took as much of the 48 GiB between the+ * kernel's link address and VM_MAX_KERNEL_ADDRESS as there is DRAM, and the rest had to hold the zone map's 24 GiB+ * and the compressor's range: with more than about 8 GiB of DRAM the boot ended in kmem_range_init+- * ("vm_map_store_lookup_hole() failing for claim ..."). Not on a pure EL0 host, whose aperture is the linear+- * mapping at the kernel's base (below).++ * ("vm_map_store_lookup_hole() failing for claim ..."). On a pure EL0 host too: there the aperture is the++ * host's own linear mapping of DRAM, which xnu.ko puts at the same address (below).+ */+- if (linux64_hook && !linux64_el0) {++ if (linux64_hook) {+ physmap_base = VM_MIN_KERNEL_ADDRESS;+ }+ #endif+@@ -1939,10 +1960,11 @@ arm_vm_init(uint64_t memory_size, boot_args * args)+ physmap_base += physmap_slide;+ if (linux64_el0) {+ /*+- * Pure EL0 host: reuse the already-mapped permanent 2 MiB linear+- * aperture [gVirtBase, gVirtBase + real_phys_size) as physmap.++ * Pure EL0 host: the physmap is the host's permanent linear mapping of DRAM (2 MiB frames), which++ * xnu.ko puts at LINUX64_EL0_APERTURE_VA: no slide, the host chose the address.+ */+- physmap_base = gVirtBase;++ _Static_assert(LINUX64_EL0_APERTURE_VA == VM_MIN_KERNEL_ADDRESS, "the EL0 host's aperture starts the kernel's window");++ physmap_base = LINUX64_EL0_APERTURE_VA;+ }++ #if XNU_MONITOR+@@ -1965,8 +1987,22 @@ arm_vm_init(uint64_t memory_size, boot_args * args)+ physmap_end = physmap_base + real_phys_size;+ #else+ if (linux64_el0) {+- physmap_end = gVirtBase + real_phys_size;+- static_memory_end = ROUND_TWIG(gVirtBase + real_phys_size);++ /*++ * The host maps the image and the boot data at the kernel's base as well, up to the next 2 MiB boundary++ * (xnu.ko): the kernel's static range ends there, or at the next twig boundary where that is further, and++ * the heap follows. Until arm_vm_physmap_init has++ * the whole table, memory after the boot data (the page tables allocated below) is reached through the++ * aperture already: at the kernel's base the host does not map it.++ */++ physmap_end = physmap_base + real_phys_size;++ static_memory_end = ROUND_TWIG(gVirtBase + (args->topOfKernelData - gPhysBase));++ if (physmap_end > LOW_GLOBAL_BASE_ADDRESS) {++ panic("LINUX64: no room below the kernel for a physical aperture of %llu MiB",++ (unsigned long long)(real_phys_size >> 20));++ }++ ptov_table[0].pa = args->topOfKernelData;++ ptov_table[0].va = physmap_base + (args->topOfKernelData - gPhysBase);++ ptov_table[0].len = gPhysBase + real_phys_size - args->topOfKernelData;+ } else {+ static_memory_end = physmap_base + mem_size + (PTOV_TABLE_SIZE * ARM_TT_TWIG_SIZE); // worst possible case for block alignment+ physmap_end = physmap_base + real_phys_size + (PTOV_TABLE_SIZE * ARM_TT_TWIG_SIZE);+@@ -2077,7 +2113,7 @@ arm_vm_init(uint64_t memory_size, boot_args * args)+ init_ptpages(cpu_tte, physmap_base, ROUND_L1(physmap_end), TRUE, ARM_DYNAMIC_TABLE_XN | ARM_TTE_TABLE_AP(ARM_TTE_TABLE_AP_USER_NA));+ port_crumb = 33;+ #else+- if (linux64_hook && !linux64_el0) {++ if (linux64_hook) {+ /* LINUX64: the tables of the physical aperture below the kernel (it is no longer inside the range above) */+ init_ptpages(cpu_tte, physmap_base, ROUND_L1(physmap_end), TRUE,+ ARM_DYNAMIC_TABLE_XN | ARM_TTE_TABLE_AP(ARM_TTE_TABLE_AP_USER_NA));+@@ -2280,11 +2316,11 @@ arm_vm_init(uint64_t memory_size, boot_args * args)+ } else {+ /*+ * Pure EL0 host: mirror any ptov_table[] entry outside the already-mapped+- * permanent linear aperture [gVirtBase, gVirtBase + real_phys_size).++ * permanent linear aperture [physmap_base, physmap_end).+ */+ for (size_t i = 0; (i < PTOV_TABLE_SIZE) && (ptov_table[i].len != 0); i++) {+- if (ptov_table[i].va >= gVirtBase &&+- ptov_table[i].va + ptov_table[i].len <= gVirtBase + real_phys_size) {++ if (ptov_table[i].va >= physmap_base &&++ ptov_table[i].va + ptov_table[i].len <= physmap_end) {+ continue;+ }+ struct linux64_map_req req = {+--- a/osfmk/arm64/machine_routines.c++++ b/osfmk/arm64/machine_routines.c+@@ -2247,7 +2247,9 @@ ml_static_mfree(+ /*+ * LINUX64: with the physical aperture below the kernel, the kernel's own static range is above physmap_base.+ * It was never freed while the aperture came after it (the test above), and it is not freed now. On a pure EL0+- * host the aperture is the linear mapping from gVirtBase and static_memory_end is its end: nothing to keep.++ * host it is freed, as it always was there (the aperture began at gVirtBase): the host keeps its own mapping of++ * the static range for good, and that is where phystokv finds these pages, which the table of the aperture++ * leaves out.+ */+ if (linux64_hook && !linux64_el0 && vaddr >= gVirtBase && vaddr < static_memory_end) {+ return;diff --git a/xnu-patches/include/linux64_abi.h b/xnu-patches/include/linux64_abi.hindex a864c83..ec75513 100644--- a/xnu-patches/include/linux64_abi.h+++ b/xnu-patches/include/linux64_abi.h@@ -23,7 +23,7 @@#ifndef _LINUX64_ABI_H_#define _LINUX64_ABI_H_-#define LINUX64_ABI_VERSION 4+#define LINUX64_ABI_VERSION 5enum linux64_call {LINUX64_CALL_KERNEL_TABLES = 210, /* boot: a1 = XNU's kernel L1 table; the host installs its kernel-range entries */@@ -73,6 +73,15 @@ enum linux64_call {#define LINUX64_PROT_WRITE (1 << 1)#define LINUX64_PROT_EXEC (1 << 2)+/*+ * Pure EL0 host: where the host maps all of the hosted system's DRAM, linearly, for good (the kernel's physical+ * aperture): the start of the kernel's window (VM_MIN_KERNEL_ADDRESS), below the kernel, so that it takes none of the+ * 48 GiB after the kernel's link address, which are the kernel map's. The image and the boot data (up to+ * topOfKernelData, rounded up to 2 MiB) are mapped a second time at the kernel's base, virtBase.+ */+#define LINUX64_EL0_APERTURE_VA 0xffffffe000000000ULL+#define LINUX64_EL0_APERTURE_END 0xfffffff000000000ULL /* LOW_GLOBAL_BASE_ADDRESS: the aperture ends at or below it */+/* EL0 host capability bits exported to XNU via linux64_el0_caps. */#define LINUX64_EL0_CAP_EXEC_ONLY (1U << 0) /* host supports execute-only (--x) user mappings */#define LINUX64_EL0_CAP_XO_COMPAT (1U << 1) /* opt-in: relax --x to r-x when host lacks EXEC_ONLY */