XNU as a loadable kernel module inside Linux (macOS personality for arm64 Linux)
C
Review fixes: fence ids, stopping, pins
Heyang Zhou307160746c5a
diff --git a/fki-ko/fki_hostcall.c b/fki-ko/fki_hostcall.cindex 9ef57a2..ee9e4d2 100644--- a/fki-ko/fki_hostcall.c+++ b/fki-ko/fki_hostcall.c@@ -9,7 +9,8 @@* a call with no service to answer it fails at once (-ENODEV), not after HC_TIMEOUT: the other CPUs would time out* waiting on that one (an XNU panic). The service also asks fki to translate its own mappings into physical addresses* (XNU_HOSTCALL_XLATE), so the foreign kernel can map the same pages. Pages nothing else holds in place (shared memory,- * not a driver's buffer) it pins first (XNU_HOSTCALL_PIN), until XNU_HOSTCALL_UNPIN or its close.+ * not a driver's buffer) it pins first (XNU_HOSTCALL_PIN), until XNU_HOSTCALL_UNPIN or its close; a device mapping+ * (VM_IO, VM_PFNMAP) has no pages to pin and is refused with -EOPNOTSUPP.*/#include "fki.h"#include "fki_internal.h"@@ -224,11 +225,34 @@ static long hc_xlate(struct xnu_hostcall_xlate __user *ua)static void hc_pin_free(struct hc_pin *p){- unpin_user_pages(p->pages, p->n);+ unpin_user_pages_dirty_lock(p->pages, p->n, true); /* the foreign kernel wrote them through its own mappings */kvfree(p->pages);kfree(p);}+/* 1 when any part of the range is a device mapping or not mapped at all (the pin then fails by itself) */+static int hc_range_is_device(unsigned long va, unsigned long len)+{+ struct mm_struct *mm = current->mm;+ struct vm_area_struct *vma;+ unsigned long at = va;+ int dev = 0;++ mmap_read_lock(mm);+ while (at < va + len) {+ vma = vma_lookup(mm, at);+ if (!vma)+ break;+ if (vma->vm_flags & (VM_IO | VM_PFNMAP)) {+ dev = 1;+ break;+ }+ at = vma->vm_end;+ }+ mmap_read_unlock(mm);+ return dev;+}+static long hc_pin(struct hc_file *hf, unsigned int cmd, struct xnu_hostcall_range __user *ua){struct xnu_hostcall_range a;@@ -253,6 +277,10 @@ static long hc_pin(struct hc_file *hf, unsigned int cmd, struct xnu_hostcall_ran}if (((a.va | a.len) & (PAGE_SIZE - 1)) || !a.len || (a.len >> PAGE_SHIFT) > HC_PIN_MAX)return -EINVAL;+ if (a.va + a.len < a.va)+ return -EINVAL;+ if (hc_range_is_device(a.va, a.len))+ return -EOPNOTSUPP;p = kzalloc(sizeof(*p), GFP_KERNEL);if (!p)return -ENOMEM;diff --git a/gpu/xnu-drm/test/virgl_smoke.c b/gpu/xnu-drm/test/virgl_smoke.cindex f0a262e..02f3270 100644--- a/gpu/xnu-drm/test/virgl_smoke.c+++ b/gpu/xnu-drm/test/virgl_smoke.c@@ -53,6 +53,11 @@ int main(void)memcpy(in, &eb, sizeof(eb));if (call(XNUDRM_IOCTL, c, DRM_COMMAND_BASE + DRM_VIRTGPU_EXECBUFFER, 0, sizeof(eb))) return 1;int f = ((struct drm_virtgpu_execbuffer *)out)->fence_fd, i;+ /* a fence closed without a wait, then the next one in its slot */+ call(XNUDRM_FENCE_CLOSE, c, f, 0, 0);+ memcpy(in, &eb, sizeof(eb));+ if (call(XNUDRM_IOCTL, c, DRM_COMMAND_BASE + DRM_VIRTGPU_EXECBUFFER, 0, sizeof(eb))) return 1;+ f = ((struct drm_virtgpu_execbuffer *)out)->fence_fd;for (i = 0; i < 20 && call(XNUDRM_FENCE_WAIT, c, f, 50000000, 0); i++) ;call(XNUDRM_FENCE_CLOSE, c, f, 0, 0);struct drm_gem_close gcl = { .handle = h };diff --git a/gpu/xnu-drm/xnudrmd.h b/gpu/xnu-drm/xnudrmd.hindex 87ffd87..a22aa96 100644--- a/gpu/xnu-drm/xnudrmd.h+++ b/gpu/xnu-drm/xnudrmd.h@@ -11,7 +11,7 @@ extern int verbose;/* Physical ranges of a mapping of this process. */int xnudrmd_ranges(void *va, size_t len, struct xnudrm_range *rg, uint32_t *nr);-/* Pin a mapping's pages (shared memory only). */+/* Pin a mapping's pages (shared memory only: -EOPNOTSUPP for a device mapping, which has none to pin). */int xnudrmd_pin(void *va, size_t len);void xnudrmd_unpin(void *va);diff --git a/gpu/xnu-drm/xnudrmd_virgl.c b/gpu/xnu-drm/xnudrmd_virgl.cindex ffce93f..eff5235 100644--- a/gpu/xnu-drm/xnudrmd_virgl.c+++ b/gpu/xnu-drm/xnudrmd_virgl.c@@ -24,15 +24,23 @@static uint8_t clients[VG_CLIENTS]; /* 0 free, 1 open, 2 with a context (its id is the client's) */static struct res { uint32_t client; uint32_t blob_mem; uint64_t size; void *va; uint64_t maplen; int pinned; } res[VG_RES];-static struct fence { uint32_t client; uint8_t state; } fences[VG_FENCES]; /* 0 free, 1 pending, 2 signalled */+/*+ * A client names a fence by its slot. The renderer's id is the slot and a serial number: a fence closed before it+ * signalled must not signal the slot's next one.+ */+static struct fence { uint32_t client; uint8_t state; uint64_t serial; } fences[VG_FENCES]; /* 0 free, 1 pending, 2 signalled */+static uint64_t fence_serial;+#define FENCE_ID(slot, serial) (((serial) << 16) | (slot))static void *cmdbuf;static int vgerr(int r) { return r < 0 ? r : r > 0 ? -r : 0; }static void write_context_fence(void *cookie, uint32_t ctx, uint32_t ring, uint64_t id){- if (id && id < VG_FENCES && fences[id].state == 1 && fences[id].client == ctx)- fences[id].state = 2;+ struct fence *f = &fences[id & (VG_FENCES - 1)];++ if (f->state == 1 && f->client == ctx && FENCE_ID(id & (VG_FENCES - 1), f->serial) == id)+ f->state = 2;}static void write_fence(void *cookie, uint32_t fence) { }@@ -187,10 +195,11 @@ static int do_ioctl(uint32_t c, uint32_t nr, uint8_t *p, uint32_t plen)if ((r = virgl_renderer_submit_cmd(cmdbuf, c, a->size / 4))) return vgerr(r);}if (id) {- fences[id] = (struct fence){ .client = c, .state = 1 };- r = virgl_renderer_context_create_fence(c, 0, a->flags & VIRTGPU_EXECBUF_RING_IDX ? a->ring_idx : 0, id);+ fences[id] = (struct fence){ .client = c, .state = 1, .serial = ++fence_serial };+ r = virgl_renderer_context_create_fence(c, 0, a->flags & VIRTGPU_EXECBUF_RING_IDX ? a->ring_idx : 0,+ FENCE_ID(id, fences[id].serial));if (r) { fences[id] = (struct fence){ 0 }; return vgerr(r); }- a->fence_fd = id; /* the fence's id */+ a->fence_fd = id; /* the fence's slot */}return 0;}@@ -211,9 +220,16 @@ static int do_map(uint32_t c, struct xnudrm_hdr *h, uint8_t *out, uint32_t *outlvoid *va = NULL;uint64_t maplen = 0;if ((r = virgl_renderer_resource_map(id, &va, &maplen))) return vgerr(r);+ /* shared memory is pinned: unpinned, Linux could move or reclaim pages the hosted kernel has mapped */+ r = xnudrmd_pin(va, maplen);+ if (r && r != -EOPNOTSUPP) { /* -EOPNOTSUPP: a device mapping, nothing to pin */+ fprintf(stderr, "xnudrmd: resource %u: its %llu bytes could not be pinned (%d)\n", id, (unsigned long long)maplen, r);+ virgl_renderer_resource_unmap(id);+ return r;+ }rs->va = va;rs->maplen = maplen;- rs->pinned = !xnudrmd_pin(va, maplen); /* shm pages only; device memory cannot be pinned */+ rs->pinned = !r;}if (len > ((rs->maplen + 4095) & ~(uint64_t)4095)) return -EINVAL;if ((r = xnudrmd_ranges(rs->va, len, (struct xnudrm_range *)out, &nr))) return r;diff --git a/scripts/xnulinux-boot.sh b/scripts/xnulinux-boot.shindex bc7c141..fd92939 100755--- a/scripts/xnulinux-boot.sh+++ b/scripts/xnulinux-boot.sh@@ -113,6 +113,7 @@ fi# Chip name for System Information (override: SOC_NAME=)XCPU0=$(cat /sys/module/fki/parameters/cpu_list 2>/dev/null | tr -d '\n' | cut -d, -f1); XCPU0=${XCPU0:-0}SOC_NAME=${SOC_NAME:-$(lscpu -e=CPU,MODELNAME 2>/dev/null | awk -v c="$XCPU0" '$1 == c { $1 = ""; sub(/^ +/, ""); print }' || true)}+[ "$SOC_NAME" = - ] && SOC_NAME= # lscpu's "-": the CPU has no model name (a guest)NAMES=()[ -n "${SOC_NAME:-}" ] && NAMES+=("soc_name=\"$SOC_NAME\"")[ -n "${GPU_MODEL:-}" ] && NAMES+=("gpu_model=\"$GPU_MODEL\"")diff --git a/xnu-ko/xnu_main.c b/xnu-ko/xnu_main.cindex 64d6ff3..a66152d 100644--- a/xnu-ko/xnu_main.c+++ b/xnu-ko/xnu_main.c@@ -447,7 +447,8 @@ static inline void acct_add(unsigned long call, u64_t ticks)/** Stopping the run: the CPUs leave XNU together. A CPU still inside XNU could otherwise wait for good, interrupts masked,- * for one that had already left. Each parks briefly at its host calls; the run is abandoned once all are parked at once.+ * for one that had already left. Each parks briefly at its host calls and kicks the others, so that one idle or in user+ * space comes to a host call too; the run is abandoned once all are parked at once.* stop_state: the number of parked CPUs, and STOP_COMMIT once the run is abandoned.*/#define STOP_COMMIT 0x10000u@@ -483,6 +484,9 @@ static int stop_park(void) /* 1: the run is abandoned, do not return to XNU */s = stop_state;if (s & STOP_COMMIT) return 1;} while (!stop_cas(s, s + 1));+ for (int c = 0, me = linux_cpu_of_slot[this_slot()]; c < cpus; c++)+ if (c != me && (c == 0 || cpu_running[c]))+ fki_kick_cpu(c);end = fki_counter() + fki_counter_frequency() / 500; /* 2 ms */for (;;) {s = stop_state;