Skip to content

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

C

GPU bridge: Metal on Venus in the hosted macOS works

Heyang Zhou424b3a2dc97e
diff --git a/gpu/venus/patches/0002-venus-virtgpu-over-xnudrm-on-macos.patch b/gpu/venus/patches/0002-venus-virtgpu-over-xnudrm-on-macos.patch
index 7a397f3..a3cecc9 100644
--- a/gpu/venus/patches/0002-venus-virtgpu-over-xnudrm-on-macos.patch
+++ b/gpu/venus/patches/0002-venus-virtgpu-over-xnudrm-on-macos.patch
@@ -6,7 +6,7 @@ Subject: venus: the virtgpu renderer on macOS over the xnulinux DRM bridge
In the hosted macOS of xnulinux, the GPU is the Linux host's virtio-gpu render node, reached through kexts/XnuDrm.
gpu/xnu-drm/darwin (libxnudrm: xf86drm.h, the render node's open/mmap/poll) is copied to src/virtio/vulkan/xnudrm by the
build (gpu/venus/build-darwin.sh) and compiled into the driver; syncobjs are simulated (vn_renderer_sim_syncobj).
-The vtest renderer (a socket to a local virglrenderer) is left out on macOS.
+The vtest renderer (a socket to a local virglrenderer) and VK_EXT_map_memory_placed are left out on macOS.
--- a/src/virtio/vulkan/meson.build
+++ b/src/virtio/vulkan/meson.build
@@ -38,6 +38,18 @@ The vtest renderer (a socket to a local virglrenderer) is left out on macOS.
endif
if with_platform_wayland or with_platform_x11 or \
+--- a/src/virtio/vulkan/vn_physical_device.c
++++ b/src/virtio/vulkan/vn_physical_device.c
+@@ -1219,7 +1219,8 @@
+ exts->KHR_deferred_host_operations =
+ physical_dev->ray_tracing && renderer_exts->KHR_acceleration_structure;
+ exts->KHR_map_memory2 = true;
+-#if !DETECT_OS_WINDOWS
++ /* not over the xnulinux DRM bridge: its mappings are made by the hosted kernel where it chooses */
++#if !DETECT_OS_WINDOWS && !defined(VN_XNUDRM)
+ exts->EXT_map_memory_placed = true;
+ exts->EXT_physical_device_drm = true;
+ #endif
--- a/src/virtio/vulkan/vn_renderer.h
+++ b/src/virtio/vulkan/vn_renderer.h
@@ -213,7 +213,7 @@
diff --git a/gpu/xnu-drm/darwin/xnudrm.c b/gpu/xnu-drm/darwin/xnudrm.c
index 98640b6..aa4fddf 100644
--- a/gpu/xnu-drm/darwin/xnudrm.c
+++ b/gpu/xnu-drm/darwin/xnudrm.c
@@ -37,27 +37,34 @@
#define NODE_PRIMARY "/dev/dri/card0"
static pthread_mutex_t lk = PTHREAD_MUTEX_INITIALIZER;
-static io_connect_t conn; /* one user client per process */
-static int node_fd = -1; /* the descriptor handed out for the render node (a /dev/null) */
-static int node_refs;
-static struct xmap { void *va; size_t len; uint64_t id; } *maps;
+static io_connect_t conn; /* the user client of the call in progress (under lk) */
+static struct xnode { int fd; io_connect_t conn; } nodes[64]; /* open render nodes: a /dev/null fd each */
+static unsigned nnodes;
+static struct xmap { void *va; size_t len; uint64_t id; io_connect_t conn; } *maps;
static unsigned nmaps;
-static struct xfence { dev_t dev; ino_t ino; int wfd; uint64_t id; } *fences;
+static struct xfence { dev_t dev; ino_t ino; int wfd; uint64_t id; io_connect_t conn; } *fences;
static unsigned nfences;
static uint8_t *buf; /* message, then reply */
static int debug(void) { static int d = -1; if (d < 0) d = getenv("XNUDRM_DEBUG") != NULL; return d; }
-static int connect_kext(void)
+static int open_conn(io_connect_t *c)
{
- if (conn) return 0;
- io_service_t s = IOServiceGetMatchingService(kIOMainPortDefault, IOServiceMatching("XnuDrm"));
- if (!s) { if (debug()) fprintf(stderr, "xnudrm: no XnuDrm service\n"); return -ENODEV; }
- kern_return_t kr = IOServiceOpen(s, mach_task_self(), 0, &conn);
- IOObjectRelease(s);
- if (kr != KERN_SUCCESS) { if (debug()) fprintf(stderr, "xnudrm: IOServiceOpen %#x\n", kr); conn = 0; return -EIO; }
+ io_service_t sv = IOServiceGetMatchingService(kIOMainPortDefault, IOServiceMatching("XnuDrm"));
+ if (!sv) { if (debug()) fprintf(stderr, "xnudrm: no XnuDrm service\n"); return -ENODEV; }
+ kern_return_t kr = IOServiceOpen(sv, mach_task_self(), 0, c);
+ IOObjectRelease(sv);
+ if (kr != KERN_SUCCESS) { if (debug()) fprintf(stderr, "xnudrm: IOServiceOpen %#x\n", kr); return -EIO; }
if (!buf) buf = malloc(sizeof(struct xnudrm_hdr) + XNUDRM_MAX_MSG);
- return buf ? 0 : -ENOMEM;
+ if (!buf) { IOServiceClose(*c); return -ENOMEM; }
+ return 0;
+}
+
+/* The user client behind a render node descriptor (0: not one). Called with lk held. */
+static io_connect_t node_conn(int fd)
+{
+ for (unsigned i = 0; i < nnodes; i++) if (nodes[i].fd == fd) return nodes[i].conn;
+ return 0;
}
/* buf holds a header and plen bytes of payload; the reply replaces it. Returns the reply's status or -errno. */
@@ -83,12 +90,14 @@ static struct xnudrm_hdr *hdr(uint32_t op, uint64_t a0, uint64_t a1)
int xnudrm_open(const char *path, int flags, ...)
{
if (path && !strcmp(path, NODE_RENDER)) {
+ io_connect_t c = 0;
pthread_mutex_lock(&lk);
- int r = connect_kext();
- if (!r && node_fd < 0) node_fd = open("/dev/null", O_RDWR | O_CLOEXEC);
- if (!r && node_fd >= 0) node_refs++;
- int fd = r ? (errno = -r, -1) : node_fd;
+ int r = nnodes < sizeof(nodes) / sizeof(nodes[0]) ? open_conn(&c) : -EMFILE;
+ int fd = r ? -1 : open("/dev/null", O_RDWR | O_CLOEXEC);
+ if (!r && fd < 0) { IOServiceClose(c); r = -errno; }
+ if (!r) nodes[nnodes++] = (struct xnode){ fd, c };
pthread_mutex_unlock(&lk);
+ if (r) { errno = -r; return -1; }
return fd;
}
if (flags & O_CREAT) {
@@ -101,12 +110,16 @@ int xnudrm_open(const char *path, int flags, ...)
int xnudrm_close(int fd)
{
pthread_mutex_lock(&lk);
- if (fd >= 0 && fd == node_fd) {
- if (--node_refs == 0) {
- /* the user client stays open: closing it drops every mapping the process still has */
+ for (unsigned i = 0; i < nnodes; i++) {
+ if (nodes[i].fd == fd) {
+ /* closing the user client drops what is still mapped from it and its DRM file on Linux */
+ io_connect_t c = nodes[i].conn;
+ nodes[i] = nodes[--nnodes];
+ for (unsigned j = 0; j < nmaps; ) { if (maps[j].conn == c) maps[j] = maps[--nmaps]; else j++; }
+ for (unsigned j = 0; j < nfences; j++) if (fences[j].conn == c && fences[j].id) fences[j].id = 0;
+ IOServiceClose(c);
+ break;
}
- pthread_mutex_unlock(&lk);
- return 0;
}
pthread_mutex_unlock(&lk);
return close(fd);
@@ -138,16 +151,26 @@ static int put(uint32_t *plen, uint64_t *field, const void *p, uint64_t len)
return 0;
}
-static int new_fence(uint64_t id)
+static int new_fence(uint64_t id) /* for conn */
{
int p[2];
if (pipe(p)) return -1;
fcntl(p[0], F_SETFD, FD_CLOEXEC); fcntl(p[1], F_SETFD, FD_CLOEXEC);
struct stat st; fstat(p[0], &st);
+ /* a pipe's inode number is reused once every descriptor of an earlier pipe is closed: an entry for that dead pipe
+ * would answer for this one */
+ for (unsigned i = 0; i < nfences; ) {
+ if (fences[i].ino == st.st_ino && fences[i].dev == st.st_dev) {
+ /* (an unsignalled one's fence stays with xnudrmd until the node closes: no host call here, buf holds the
+ * reply being copied out) */
+ if (fences[i].wfd >= 0) close(fences[i].wfd);
+ fences[i] = fences[--nfences];
+ } else i++;
+ }
struct xfence *nf = realloc(fences, (nfences + 1) * sizeof(*fences));
if (!nf) { close(p[0]); close(p[1]); return -1; }
fences = nf;
- fences[nfences++] = (struct xfence){ st.st_dev, st.st_ino, p[1], id };
+ fences[nfences++] = (struct xfence){ st.st_dev, st.st_ino, p[1], id, conn };
return p[0];
}
@@ -242,9 +265,9 @@ static int do_ioctl(unsigned long request, void *arg)
int drmIoctl(int fd, unsigned long request, void *arg)
{
- if (fd != node_fd || fd < 0) { errno = EBADF; return -1; }
pthread_mutex_lock(&lk);
- int r = do_ioctl(request, arg);
+ conn = node_conn(fd);
+ int r = conn ? do_ioctl(request, arg) : -EBADF;
pthread_mutex_unlock(&lk);
if (r) { errno = -r; return -1; }
return 0;
@@ -258,14 +281,15 @@ int drmIoctl(int fd, unsigned long request, void *arg)
*/
void *xnudrm_mmap(void *addr, size_t len, int prot, int flags, int fd, off_t off)
{
- if (fd < 0 || fd != node_fd) return mmap(addr, len, prot, flags, fd, off);
+ pthread_mutex_lock(&lk);
+ io_connect_t c = fd >= 0 ? node_conn(fd) : 0;
+ if (!c) { pthread_mutex_unlock(&lk); return mmap(addr, len, prot, flags, fd, off); }
uint64_t in[2] = { (uint64_t)off >> 12, len }, out[2] = { 0, 0 };
uint32_t nout = 2;
- pthread_mutex_lock(&lk);
- kern_return_t kr = IOConnectCallScalarMethod(conn, 1, in, 2, out, &nout);
+ kern_return_t kr = IOConnectCallScalarMethod(c, 1, in, 2, out, &nout);
if (kr == KERN_SUCCESS) {
struct xmap *nm = realloc(maps, (nmaps + 1) * sizeof(*maps));
- if (nm) { maps = nm; maps[nmaps++] = (struct xmap){ (void *)(uintptr_t)out[0], len, out[1] }; }
+ if (nm) { maps = nm; maps[nmaps++] = (struct xmap){ (void *)(uintptr_t)out[0], len, out[1], c }; }
}
pthread_mutex_unlock(&lk);
if (kr != KERN_SUCCESS) { if (debug()) fprintf(stderr, "xnudrm: map handle %llu: %#x\n", (unsigned long long)in[0], kr); errno = ENOMEM; return MAP_FAILED; }
@@ -284,8 +308,9 @@ int xnudrm_munmap(void *addr, size_t len)
for (unsigned i = 0; i < nmaps; i++) {
if (maps[i].va == addr) {
uint64_t id = maps[i].id;
+ io_connect_t c = maps[i].conn;
maps[i] = maps[--nmaps];
- IOConnectCallScalarMethod(conn, 2, &id, 1, NULL, NULL);
+ IOConnectCallScalarMethod(c, 2, &id, 1, NULL, NULL);
pthread_mutex_unlock(&lk);
return 0;
}
@@ -309,9 +334,11 @@ static struct xfence *fence_of(int fd)
static int fence_check(struct xfence *f)
{
if (!f->id) return 1;
+ conn = f->conn;
hdr(XNUDRM_FENCE_WAIT, f->id, 0);
int r = call(0);
if (r == -XNUDRM_ETIME) return 0;
+ if (debug() && r) fprintf(stderr, "xnudrm: fence %llu wait: %d\n", (unsigned long long)f->id, r);
hdr(XNUDRM_FENCE_CLOSE, f->id, 0);
call(0);
f->id = 0;
@@ -333,7 +360,11 @@ int xnudrm_poll(struct pollfd *fds, nfds_t n, int timeout)
if (f && f->id && fence_check(f) == 0) pending = 1;
}
pthread_mutex_unlock(&lk);
- if (debug()) fprintf(stderr, "xnudrm: poll %u fds, timeout %d: %s\n", (unsigned)n, timeout, pending ? "pending" : "ready");
+ if (debug()) {
+ fprintf(stderr, "xnudrm: poll %u fds, timeout %d: %s;", (unsigned)n, timeout, pending ? "pending" : "ready");
+ for (nfds_t i = 0; i < n; i++) { struct xfence *f = nfences ? fence_of(fds[i].fd) : NULL; fprintf(stderr, " fd %d -> %s id %llu", fds[i].fd, f ? "fence" : "other", f ? (unsigned long long)f->id : 0ull); }
+ fprintf(stderr, "\n");
+ }
if (!pending) return poll(fds, n, timeout); /* signalled fences are readable pipes now */
clock_gettime(CLOCK_MONOTONIC, &now);
long ms = (now.tv_sec - t0.tv_sec) * 1000 + (now.tv_nsec - t0.tv_nsec) / 1000000;
@@ -372,7 +403,10 @@ void drmFreeDevices(drmDevicePtr devices[], int count)
drmVersionPtr drmGetVersion(int fd)
{
- if (fd != node_fd) return NULL;
+ pthread_mutex_lock(&lk);
+ io_connect_t c = node_conn(fd);
+ pthread_mutex_unlock(&lk);
+ if (!c) return NULL;
drmVersionPtr v = calloc(1, sizeof(*v));
if (!v) return NULL;
v->name = "virtio_gpu";
diff --git a/kexts/XnuDrm/XnuDrm.cpp b/kexts/XnuDrm/XnuDrm.cpp
index 41118f3..a4b140e 100644
--- a/kexts/XnuDrm/XnuDrm.cpp
+++ b/kexts/XnuDrm/XnuDrm.cpp
@@ -4,6 +4,10 @@
// fki) answered by xnudrmd, the Linux service that owns the DRM render node. Buffer mappings come back as the physical
// pages Linux mapped, which this kext maps into the calling task: the process and the GPU share the memory, no copies.
//
+// The user client's class is named AGXDeviceUserClient, the name Apple GPUs' device user client has: the sandbox profiles
+// of the processes that draw (apps, WebKit's GPU process, agents) let them open user clients of that class and of few
+// others, and refuse any other (MACF: "IOUC ... failed MACF"). No AGX kext is in the hosted collection, so the name is free.
+//
// Methods (IOConnectCallScalarMethod):
// 0 call(in_ptr, in_len, out_ptr, out_max) -> reply_len a message for xnudrmd (xnudrm_proto.h), client id filled in
// 1 map(gem_handle, size) -> (user address, map id) XNUDRM_MAP, then the pages mapped into the task
@@ -37,8 +41,8 @@ public:
bool start(IOService *provider) override;
};
-class XnuDrmClient : public IOUserClient {
- OSDeclareDefaultStructors(XnuDrmClient)
+class AGXDeviceUserClient : public IOUserClient {
+ OSDeclareDefaultStructors(AGXDeviceUserClient)
task_t fTask = nullptr;
uint32_t fClient = 0; // xnudrmd's client id (0: not open)
struct Slot { uint64_t id; IOMemoryMap *map; IOMemoryDescriptor *md; };
@@ -57,7 +61,7 @@ public:
};
OSDefineMetaClassAndStructors(XnuDrm, IOService)
-OSDefineMetaClassAndStructors(XnuDrmClient, IOUserClient)
+OSDefineMetaClassAndStructors(AGXDeviceUserClient, IOUserClient)
bool XnuDrm::start(IOService *provider)
{
@@ -68,13 +72,13 @@ bool XnuDrm::start(IOService *provider)
gOut = (uint8_t *)IOMallocData(sizeof(xnudrm_hdr) + XNUDRM_MAX_MSG);
if (!gCallLock || !gIn || !gOut) return false;
}
- setProperty("IOUserClientClass", "XnuDrmClient");
+ setProperty("IOUserClientClass", "AGXDeviceUserClient");
registerService();
IOLog("XnuDrm: GPU bridge ready\n");
return true;
}
-bool XnuDrmClient::initWithTask(task_t owningTask, void *securityID, UInt32 type)
+bool AGXDeviceUserClient::initWithTask(task_t owningTask, void *securityID, UInt32 type)
{
if (!IOUserClient::initWithTask(owningTask, securityID, type)) return false;
fTask = owningTask;
@@ -89,7 +93,7 @@ bool XnuDrmClient::initWithTask(task_t owningTask, void *securityID, UInt32 type
}
// A message without payload; the reply's header into *reply.
-long XnuDrmClient::simple(uint32_t op, uint64_t a0, uint64_t a1, xnudrm_hdr *reply)
+long AGXDeviceUserClient::simple(uint32_t op, uint64_t a0, uint64_t a1, xnudrm_hdr *reply)
{
IOLockLock(gCallLock);
xnudrm_hdr *h = (xnudrm_hdr *)gIn;
@@ -100,7 +104,7 @@ long XnuDrmClient::simple(uint32_t op, uint64_t a0, uint64_t a1, xnudrm_hdr *rep
return n;
}
-IOReturn XnuDrmClient::doCall(IOExternalMethodArguments *a)
+IOReturn AGXDeviceUserClient::doCall(IOExternalMethodArguments *a)
{
user_addr_t in = a->scalarInput[0], out = a->scalarInput[2];
uint64_t inlen = a->scalarInput[1], outmax = a->scalarInput[3];
@@ -124,7 +128,7 @@ out:
return ret;
}
-IOReturn XnuDrmClient::doMap(IOExternalMethodArguments *a)
+IOReturn AGXDeviceUserClient::doMap(IOExternalMethodArguments *a)
{
uint64_t handle = a->scalarInput[0], size = a->scalarInput[1];
IOLockLock(gCallLock);
@@ -174,7 +178,7 @@ IOReturn XnuDrmClient::doMap(IOExternalMethodArguments *a)
return kIOReturnSuccess;
}
-IOReturn XnuDrmClient::doUnmap(IOExternalMethodArguments *a)
+IOReturn AGXDeviceUserClient::doUnmap(IOExternalMethodArguments *a)
{
uint64_t id = a->scalarInput[0];
for (unsigned i = 0; i < fNSlots; i++) {
@@ -190,7 +194,7 @@ IOReturn XnuDrmClient::doUnmap(IOExternalMethodArguments *a)
return kIOReturnBadArgument;
}
-IOReturn XnuDrmClient::externalMethod(uint32_t selector, IOExternalMethodArguments *a, IOExternalMethodDispatch *,
+IOReturn AGXDeviceUserClient::externalMethod(uint32_t selector, IOExternalMethodArguments *a, IOExternalMethodDispatch *,
OSObject *, void *)
{
switch (selector) {
@@ -201,7 +205,7 @@ IOReturn XnuDrmClient::externalMethod(uint32_t selector, IOExternalMethodArgumen
return kIOReturnUnsupported;
}
-IOReturn XnuDrmClient::clientClose()
+IOReturn AGXDeviceUserClient::clientClose()
{
for (unsigned i = 0; i < fNSlots; i++) {
if (fSlots[i].map) { fSlots[i].map->release(); fSlots[i].md->release(); fSlots[i] = (Slot){}; }
@@ -215,7 +219,7 @@ IOReturn XnuDrmClient::clientClose()
return kIOReturnSuccess;
}
-void XnuDrmClient::free()
+void AGXDeviceUserClient::free()
{
if (fSlots) IOFreeData(fSlots, fNSlots * sizeof(Slot));
IOUserClient::free();