Skip to content

Apple's open-source XNU kernel on Android phones: its own loader and drivers, Metal on the Adreno. Runs from RAM; a reboot gives the phone back to Android.

C

Turnip for the Darwin userland: tools/mk-turnip.sh, turnip-darwin.patch, syncobjs in hakios-drm

yehorkaa95bc3b2c577e
diff --git a/tools/mesa/hakios-drm/hakios.drm.c b/tools/mesa/hakios-drm/hakios.drm.c
index 2fb23b1..2672652 100644
--- a/tools/mesa/hakios-drm/hakios.drm.c
+++ b/tools/mesa/hakios-drm/hakios.drm.c
@@ -8,6 +8,10 @@
* there is one GPU and one connection to it in a process.
*/
#include <errno.h>
+#include <pthread.h>
+#include <stdbool.h>
+#include <sys/time.h>
+#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
@@ -15,6 +19,7 @@
#include <IOKit/IOKitLib.h>
#include "xf86drm.h"
+#include "drm-uapi/msm_drm.h"
#define HAKIOS_GPU_SERVICE "HakiosGPU"
#define HAKIOS_GPU_SURFACE 0x7e /* the request that is not Linux's */
@@ -95,10 +100,16 @@ drmCommandWrite(int fd, unsigned long index, const void *data, unsigned long siz
return -call((unsigned)(DRM_COMMAND_BASE + index), (void *)(uintptr_t)data, size);
}
+static int submit_with_syncobjs(struct drm_msm_gem_submit *submit, unsigned long size);
+
int
drmCommandWriteRead(int fd, unsigned long index, void *data, unsigned long size)
{
(void)fd;
+ if (index == DRM_MSM_GEM_SUBMIT && size >= sizeof(struct drm_msm_gem_submit) &&
+ (((struct drm_msm_gem_submit *)data)->flags & (MSM_SUBMIT_SYNCOBJ_IN | MSM_SUBMIT_SYNCOBJ_OUT)) != 0) {
+ return submit_with_syncobjs(data, size);
+ }
return -call((unsigned)(DRM_COMMAND_BASE + index), data, size);
}
@@ -211,11 +222,6 @@ none(void)
#define NONE(name, ...) int name(__VA_ARGS__) { return none(); }
NONE(drmPrimeHandleToFD, __attribute__((unused)) int fd, __attribute__((unused)) uint32_t handle,
__attribute__((unused)) uint32_t flags, __attribute__((unused)) int *prime_fd)
-NONE(drmSyncobjCreate, __attribute__((unused)) int fd, __attribute__((unused)) uint32_t flags,
- __attribute__((unused)) uint32_t *handle)
-NONE(drmSyncobjDestroy, __attribute__((unused)) int fd, __attribute__((unused)) uint32_t handle)
-NONE(drmSyncobjReset, __attribute__((unused)) int fd, __attribute__((unused)) const uint32_t *handles,
- __attribute__((unused)) uint32_t count)
NONE(drmSyncobjFDToHandle, __attribute__((unused)) int fd, __attribute__((unused)) int obj_fd,
__attribute__((unused)) uint32_t *handle)
NONE(drmSyncobjHandleToFD, __attribute__((unused)) int fd, __attribute__((unused)) uint32_t handle,
@@ -224,3 +230,335 @@ NONE(drmSyncobjImportSyncFile, __attribute__((unused)) int fd, __attribute__((un
__attribute__((unused)) int sync_file_fd)
NONE(drmSyncobjExportSyncFile, __attribute__((unused)) int fd, __attribute__((unused)) uint32_t handle,
__attribute__((unused)) int *sync_file_fd)
+
+/*
+ * DRM's synchronisation objects (syncobj), which Turnip's msm backend makes its VkFences and VkSemaphores of, kept in
+ * this process: there is one GPU and one connection to it here, so what a syncobj waits for is always one of this
+ * process's submissions, a fence number of the kernel's (MSM_WAIT_FENCE tells when it is done). A syncobj is a timeline:
+ * the last point known reached, and the points still on their way, each with the submission that reaches it. A binary
+ * one is the timeline of point 1 (signalled: 1, reset: 0). A submission's MSM_SUBMIT_SYNCOBJ_IN syncobjs are waited for
+ * here, before it goes to the kernel, and its MSM_SUBMIT_SYNCOBJ_OUT ones are given its fence (submit_with_syncobjs).
+ * Not here: syncobjs and sync files as file descriptors (nothing outside this process waits for them).
+ */
+#define SYNCOBJS 4096
+#define PENDING 16
+
+struct syncobj {
+ bool used;
+ uint64_t reached; /* the last point known reached */
+ unsigned pending;
+ struct { uint64_t point; uint32_t fence; } on_the_way[PENDING];
+};
+static struct syncobj syncobjs[SYNCOBJS];
+static pthread_mutex_t syncobj_lock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_cond_t syncobj_submitted = PTHREAD_COND_INITIALIZER;
+
+static struct syncobj *
+syncobj_of(uint32_t handle)
+{
+ return handle != 0 && handle < SYNCOBJS && syncobjs[handle].used ? &syncobjs[handle] : NULL;
+}
+
+/* whether the kernel is done with submission FENCE: waited for until DEADLINE (absolute CLOCK_MONOTONIC ns; 0: not at all) */
+static bool
+fence_done(uint32_t fence, int64_t deadline)
+{
+ struct drm_msm_wait_fence wait = { .fence = fence, .queueid = 0 };
+
+ wait.timeout.tv_sec = deadline / 1000000000;
+ wait.timeout.tv_nsec = deadline % 1000000000;
+ return call(DRM_COMMAND_BASE + DRM_MSM_WAIT_FENCE, &wait, sizeof(wait)) == 0;
+}
+
+static int64_t
+now_ns(void)
+{
+ struct timespec t;
+
+ clock_gettime(CLOCK_MONOTONIC, &t);
+ return (int64_t)t.tv_sec * 1000000000 + t.tv_nsec;
+}
+
+/* the points on their way that their submissions have reached since: into REACHED (lock held) */
+static void
+syncobj_poll(struct syncobj *s)
+{
+ unsigned kept = 0;
+
+ for (unsigned i = 0; i < s->pending; i++) {
+ if (fence_done(s->on_the_way[i].fence, 0)) {
+ if (s->on_the_way[i].point > s->reached) s->reached = s->on_the_way[i].point;
+ } else {
+ s->on_the_way[kept++] = s->on_the_way[i];
+ }
+ }
+ s->pending = kept;
+}
+
+static void
+syncobj_add(struct syncobj *s, uint64_t point, uint32_t fence)
+{
+ if (s->pending == PENDING) {
+ syncobj_poll(s);
+ }
+ if (s->pending == PENDING) {
+ /* (full: the oldest is waited for, its point taken as reached) */
+ fence_done(s->on_the_way[0].fence, now_ns() + 10000000000LL);
+ if (s->on_the_way[0].point > s->reached) s->reached = s->on_the_way[0].point;
+ memmove(&s->on_the_way[0], &s->on_the_way[1], (PENDING - 1) * sizeof(s->on_the_way[0]));
+ s->pending--;
+ }
+ s->on_the_way[s->pending].point = point;
+ s->on_the_way[s->pending].fence = fence;
+ s->pending++;
+}
+
+int
+drmSyncobjCreate(int fd, uint32_t flags, uint32_t *handle)
+{
+ (void)fd;
+ pthread_mutex_lock(&syncobj_lock);
+ for (uint32_t h = 1; h < SYNCOBJS; h++) {
+ if (!syncobjs[h].used) {
+ memset(&syncobjs[h], 0, sizeof(syncobjs[h]));
+ syncobjs[h].used = true;
+ syncobjs[h].reached = (flags & DRM_SYNCOBJ_CREATE_SIGNALED) ? 1 : 0;
+ *handle = h;
+ pthread_mutex_unlock(&syncobj_lock);
+ return 0;
+ }
+ }
+ pthread_mutex_unlock(&syncobj_lock);
+ errno = ENOMEM;
+ return -ENOMEM;
+}
+
+int
+drmSyncobjDestroy(int fd, uint32_t handle)
+{
+ struct syncobj *s;
+
+ (void)fd;
+ pthread_mutex_lock(&syncobj_lock);
+ s = syncobj_of(handle);
+ if (s != NULL) s->used = false;
+ pthread_mutex_unlock(&syncobj_lock);
+ return s != NULL ? 0 : -EINVAL;
+}
+
+int
+drmSyncobjReset(int fd, const uint32_t *handles, uint32_t count)
+{
+ (void)fd;
+ pthread_mutex_lock(&syncobj_lock);
+ for (uint32_t i = 0; i < count; i++) {
+ struct syncobj *s = syncobj_of(handles[i]);
+ if (s != NULL) s->reached = 0, s->pending = 0;
+ }
+ pthread_mutex_unlock(&syncobj_lock);
+ return 0;
+}
+
+int
+drmSyncobjTimelineSignal(int fd, const uint32_t *handles, uint64_t *points, uint32_t count)
+{
+ (void)fd;
+ pthread_mutex_lock(&syncobj_lock);
+ for (uint32_t i = 0; i < count; i++) {
+ struct syncobj *s = syncobj_of(handles[i]);
+ uint64_t point = points != NULL && points[i] != 0 ? points[i] : 1;
+ if (s != NULL && point > s->reached) s->reached = point;
+ }
+ pthread_cond_broadcast(&syncobj_submitted);
+ pthread_mutex_unlock(&syncobj_lock);
+ return 0;
+}
+
+int
+drmSyncobjSignal(int fd, const uint32_t *handles, uint32_t count)
+{
+ return drmSyncobjTimelineSignal(fd, handles, NULL, count);
+}
+
+int
+drmSyncobjQuery2(int fd, uint32_t *handles, uint64_t *points, uint32_t count, uint32_t flags)
+{
+ (void)fd;
+ (void)flags;
+ pthread_mutex_lock(&syncobj_lock);
+ for (uint32_t i = 0; i < count; i++) {
+ struct syncobj *s = syncobj_of(handles[i]);
+ if (s != NULL) syncobj_poll(s);
+ points[i] = s != NULL ? s->reached : 0;
+ }
+ pthread_mutex_unlock(&syncobj_lock);
+ return 0;
+}
+
+int
+drmSyncobjQuery(int fd, uint32_t *handles, uint64_t *points, uint32_t count)
+{
+ return drmSyncobjQuery2(fd, handles, points, count, 0);
+}
+
+/*
+ * Wait for each syncobj's point (0 or NULL: 1, a binary one's) -- all of them, or the first (DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL
+ * clear) -- until DEADLINE (absolute CLOCK_MONOTONIC ns). A point no submission is on its way to yet is waited for to be
+ * submitted with DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT, an error without (as Linux answers); WAIT_AVAILABLE: submitted
+ * is enough.
+ */
+int
+drmSyncobjTimelineWait(int fd, uint32_t *handles, uint64_t *points, unsigned count, int64_t deadline, unsigned flags,
+ uint32_t *first)
+{
+ const bool all = (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL) != 0;
+ int result = -ETIME;
+
+ (void)fd;
+ pthread_mutex_lock(&syncobj_lock);
+ for (;;) {
+ unsigned reached = 0, unknown = 0;
+ uint32_t wait_fence = 0;
+ bool have_fence = false;
+
+ for (unsigned i = 0; i < count; i++) {
+ struct syncobj *s = syncobj_of(handles[i]);
+ uint64_t point = points != NULL && points[i] != 0 ? points[i] : 1;
+ bool on_the_way = false;
+
+ if (s == NULL) {
+ pthread_mutex_unlock(&syncobj_lock);
+ return -EINVAL;
+ }
+ syncobj_poll(s);
+ for (unsigned k = 0; k < s->pending; k++) {
+ if (s->on_the_way[k].point >= point) {
+ on_the_way = true;
+ if (!have_fence || (int32_t)(s->on_the_way[k].fence - wait_fence) < 0) wait_fence = s->on_the_way[k].fence;
+ have_fence = true;
+ break;
+ }
+ }
+ if (s->reached >= point || (on_the_way && (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE))) {
+ reached++;
+ if (!all) {
+ if (first != NULL) *first = i;
+ pthread_mutex_unlock(&syncobj_lock);
+ return 0;
+ }
+ } else if (!on_the_way) {
+ unknown++;
+ }
+ }
+ if (reached == count) {
+ if (first != NULL) *first = 0;
+ pthread_mutex_unlock(&syncobj_lock);
+ return 0;
+ }
+ if (unknown != 0 && !(flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT)) {
+ pthread_mutex_unlock(&syncobj_lock);
+ return -EINVAL;
+ }
+ if (now_ns() >= deadline) {
+ break;
+ }
+ if (have_fence && unknown == 0) {
+ /* every point is on its way: the earliest submission among them waited for, without the lock */
+ pthread_mutex_unlock(&syncobj_lock);
+ fence_done(wait_fence, deadline);
+ pthread_mutex_lock(&syncobj_lock);
+ } else {
+ /* some point not submitted yet: until a submission or a signal, in steps of a millisecond at most */
+ struct timespec until;
+ int64_t step = now_ns() + 1000000;
+ struct timeval tv;
+ gettimeofday(&tv, NULL);
+ int64_t real = (int64_t)tv.tv_sec * 1000000000 + (int64_t)tv.tv_usec * 1000 + (step < deadline ? 1000000 : deadline - now_ns());
+ until.tv_sec = real / 1000000000;
+ until.tv_nsec = real % 1000000000;
+ pthread_cond_timedwait(&syncobj_submitted, &syncobj_lock, &until);
+ }
+ }
+ pthread_mutex_unlock(&syncobj_lock);
+ return result;
+}
+
+int
+drmSyncobjWait(int fd, uint32_t *handles, unsigned count, int64_t deadline, unsigned flags, uint32_t *first)
+{
+ return drmSyncobjTimelineWait(fd, handles, NULL, count, deadline, flags, first);
+}
+
+/* DST at DST_POINT (0: binary) gets what SRC at SRC_POINT (0: binary) is or will be */
+int
+drmSyncobjTransfer(int fd, uint32_t dst, uint64_t dst_point, uint32_t src, uint64_t src_point, uint32_t flags)
+{
+ struct syncobj *d, *s;
+ uint64_t want = src_point != 0 ? src_point : 1, to = dst_point != 0 ? dst_point : 1;
+
+ (void)fd;
+ (void)flags;
+ pthread_mutex_lock(&syncobj_lock);
+ d = syncobj_of(dst);
+ s = syncobj_of(src);
+ if (d == NULL || s == NULL) {
+ pthread_mutex_unlock(&syncobj_lock);
+ return -EINVAL;
+ }
+ syncobj_poll(s);
+ if (dst_point == 0) d->reached = 0, d->pending = 0;
+ if (s->reached >= want) {
+ if (to > d->reached) d->reached = to;
+ } else {
+ for (unsigned k = 0; k < s->pending; k++) {
+ if (s->on_the_way[k].point >= want) {
+ syncobj_add(d, to, s->on_the_way[k].fence);
+ break;
+ }
+ }
+ }
+ pthread_cond_broadcast(&syncobj_submitted);
+ pthread_mutex_unlock(&syncobj_lock);
+ return 0;
+}
+
+static int
+submit_with_syncobjs(struct drm_msm_gem_submit *submit, unsigned long size)
+{
+ const struct drm_msm_gem_submit_syncobj *in = (const void *)(uintptr_t)submit->in_syncobjs;
+ const struct drm_msm_gem_submit_syncobj *out = (const void *)(uintptr_t)submit->out_syncobjs;
+ const uint32_t stride = submit->syncobj_stride ? submit->syncobj_stride : sizeof(*in);
+ const uint32_t nin = (submit->flags & MSM_SUBMIT_SYNCOBJ_IN) ? submit->nr_in_syncobjs : 0;
+ const uint32_t nout = (submit->flags & MSM_SUBMIT_SYNCOBJ_OUT) ? submit->nr_out_syncobjs : 0;
+ int error;
+
+ /* what it waits for first (this queue's own submissions are in order on the one ring anyway) */
+ for (uint32_t i = 0; i < nin; i++) {
+ const struct drm_msm_gem_submit_syncobj *x = (const void *)((const uint8_t *)in + i * stride);
+ uint32_t h = x->handle;
+ uint64_t p = x->point;
+ int r = drmSyncobjTimelineWait(0, &h, &p, 1, now_ns() + 10000000000LL,
+ DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL | DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT, NULL);
+ if (r != 0) return r;
+ if (x->flags & MSM_SUBMIT_SYNCOBJ_RESET) drmSyncobjReset(0, &h, 1);
+ }
+ submit->flags &= ~(uint32_t)(MSM_SUBMIT_SYNCOBJ_IN | MSM_SUBMIT_SYNCOBJ_OUT);
+ submit->in_syncobjs = submit->out_syncobjs = 0;
+ submit->nr_in_syncobjs = submit->nr_out_syncobjs = 0;
+ error = call((unsigned)(DRM_COMMAND_BASE + DRM_MSM_GEM_SUBMIT), submit, size);
+ if (error != 0) return -error;
+ pthread_mutex_lock(&syncobj_lock);
+ for (uint32_t i = 0; i < nout; i++) {
+ const struct drm_msm_gem_submit_syncobj *x = (const void *)((const uint8_t *)out + i * stride);
+ struct syncobj *s = syncobj_of(x->handle);
+ if (s == NULL) continue;
+ if (x->point == 0) s->reached = 0, s->pending = 0; /* a binary one: what it holds now is this submission */
+ syncobj_add(s, x->point != 0 ? x->point : 1, submit->fence);
+ }
+ pthread_cond_broadcast(&syncobj_submitted);
+ pthread_mutex_unlock(&syncobj_lock);
+ return 0;
+}
+
+/* Linux's device nodes, which Turnip's lists of devices ask about: one GPU, a render node 128 */
+int drmGetNodeTypeFromFd(int fd) { (void)fd; return DRM_NODE_RENDER; }
diff --git a/tools/mesa/hakios-drm/xf86drm.h b/tools/mesa/hakios-drm/xf86drm.h
index c3b3d8b..6feb777 100644
--- a/tools/mesa/hakios-drm/xf86drm.h
+++ b/tools/mesa/hakios-drm/xf86drm.h
@@ -15,6 +15,10 @@
#include "drm-uapi/drm.h"
+#ifdef __cplusplus
+extern "C" {
+#endif
+
#define DRM_NODE_PRIMARY 0
#define DRM_NODE_RENDER 2
#define DRM_CLOEXEC O_CLOEXEC
@@ -48,6 +52,16 @@ int drmPrimeFDToHandle(int fd, int surface, uint32_t *handle);
int drmSyncobjCreate(int fd, uint32_t flags, uint32_t *handle);
int drmSyncobjDestroy(int fd, uint32_t handle);
int drmSyncobjReset(int fd, const uint32_t *handles, uint32_t count);
+/* (syncobjs kept in this process, over the kernel's fences: hakios.drm.c) */
+int drmSyncobjSignal(int fd, const uint32_t *handles, uint32_t count);
+int drmSyncobjTimelineSignal(int fd, const uint32_t *handles, uint64_t *points, uint32_t count);
+int drmSyncobjWait(int fd, uint32_t *handles, unsigned count, int64_t timeout_nsec, unsigned flags, uint32_t *first_signaled);
+int drmSyncobjTimelineWait(int fd, uint32_t *handles, uint64_t *points, unsigned count, int64_t timeout_nsec, unsigned flags,
+ uint32_t *first_signaled);
+int drmSyncobjQuery(int fd, uint32_t *handles, uint64_t *points, uint32_t count);
+int drmSyncobjQuery2(int fd, uint32_t *handles, uint64_t *points, uint32_t count, uint32_t flags);
+int drmSyncobjTransfer(int fd, uint32_t dst_handle, uint64_t dst_point, uint32_t src_handle, uint64_t src_point, uint32_t flags);
+int drmGetNodeTypeFromFd(int fd);
int drmSyncobjFDToHandle(int fd, int obj_fd, uint32_t *handle);
int drmSyncobjHandleToFD(int fd, uint32_t handle, int *obj_fd);
int drmSyncobjImportSyncFile(int fd, uint32_t handle, int sync_file_fd);
@@ -73,4 +87,8 @@ struct hakios_drm_surface {
};
int hakios_drm_surface(int fd, struct hakios_drm_surface *surface);
+#ifdef __cplusplus
+}
+#endif
+
#endif
diff --git a/tools/mesa/turnip-darwin.patch b/tools/mesa/turnip-darwin.patch
new file mode 100644
index 0000000..7209b0c
--- /dev/null
+++ b/tools/mesa/turnip-darwin.patch
@@ -0,0 +1,101 @@
+diff --git a/meson.build b/meson.build
+index f055df1..457fe06 100644
+--- a/meson.build
++++ b/meson.build
+@@ -1716,7 +1716,7 @@ else
+ # dependencies (such as vk-runtime) linking to it
+ dep_libdrm = null_dep
+ endif
+-if host_machine.system() == 'darwin' and with_gallium_freedreno
++if host_machine.system() == 'darwin' and (with_gallium_freedreno or with_freedreno_vk)
+ # No DRM on this system: freedreno's requests go to the kernel's driver through a stand-in
+ # for libdrm (src/hakios-drm, put there by Darwin on redwood's tools/mk-mesa.sh; its one
+ # source file is built with freedreno's drm layer, src/freedreno/drm).
+diff --git a/src/freedreno/vulkan/meson.build b/src/freedreno/vulkan/meson.build
+index d0a38c9..4f68424 100644
+--- a/src/freedreno/vulkan/meson.build
++++ b/src/freedreno/vulkan/meson.build
+@@ -101,6 +101,10 @@ if freedreno_kmds.contains('msm')
+ tu_flags += '-DTU_HAS_MSM'
+ libtu_files += files('tu_knl_drm_msm.cc', 'tu_knl_drm.cc')
+ tu_deps += dep_libdrm
++ if host_machine.system() == 'darwin'
++ # the stand-in for libdrm (src/hakios-drm): msm's requests as calls of the kernel's IOKit user client
++ libtu_files += files('../../hakios-drm/hakios.drm.c')
++ endif
+ endif
+
+ if freedreno_kmds.contains('virtio')
+@@ -181,7 +185,10 @@ libvulkan_freedreno = shared_library(
+ c_args : [no_override_init_args, tu_flags],
+ cpp_args : [tu_cpp_args, tu_flags],
+ gnu_symbol_visibility : 'hidden',
+- link_args : [vulkan_icd_link_args, ld_args_bsymbolic, ld_args_gc_sections, ld_args_build_id],
++ # (Darwin: the entrypoint tables' weak references to what Turnip leaves to the common runtime are allowed
++ # undefined, as ELF lets them be, and come out NULL; UniXNU's Turnip does the same)
++ link_args : [vulkan_icd_link_args, ld_args_bsymbolic, ld_args_gc_sections, ld_args_build_id,
++ host_machine.system() == 'darwin' ? ['-Wl,-undefined,dynamic_lookup'] : []],
+ link_depends : vulkan_icd_link_depends,
+ install : true,
+ )
+diff --git a/src/freedreno/vulkan/tu_device.cc b/src/freedreno/vulkan/tu_device.cc
+index 62ebfd0..72aac15 100644
+--- a/src/freedreno/vulkan/tu_device.cc
++++ b/src/freedreno/vulkan/tu_device.cc
+@@ -2709,6 +2709,7 @@ tu_CreateDevice(VkPhysicalDevice physicalDevice,
+ "pthread condattr init");
+ goto fail_timeline_cond;
+ }
++#ifndef __APPLE__ /* (Darwin's condition variables have no clock to choose: timed waits go by the realtime one) */
+ if (pthread_condattr_setclock(&condattr, CLOCK_MONOTONIC) != 0) {
+ pthread_condattr_destroy(&condattr);
+ result = vk_startup_errorf(physical_device->instance,
+@@ -2716,6 +2717,7 @@ tu_CreateDevice(VkPhysicalDevice physicalDevice,
+ "pthread condattr clock setup");
+ goto fail_timeline_cond;
+ }
++#endif
+ if (pthread_cond_init(&device->timeline_cond, &condattr) != 0) {
+ pthread_condattr_destroy(&condattr);
+ result = vk_startup_errorf(physical_device->instance,
+diff --git a/src/freedreno/vulkan/tu_wsi.cc b/src/freedreno/vulkan/tu_wsi.cc
+index 57cf904..3e0a1b1 100644
+--- a/src/freedreno/vulkan/tu_wsi.cc
++++ b/src/freedreno/vulkan/tu_wsi.cc
+@@ -9,7 +9,9 @@
+ #include "tu_wsi.h"
+
+ #include "vk_util.h"
++#ifndef __APPLE__ /* (no DRM devices on Darwin: src/hakios-drm) */
+ #include "wsi_common_drm.h"
++#endif
+ #include "drm-uapi/drm_fourcc.h"
+
+ #include "tu_device.h"
+@@ -26,7 +28,13 @@ tu_wsi_can_present_on_device(VkPhysicalDevice physicalDevice, int fd)
+ {
+ #ifdef HAVE_LIBDRM
+ VK_FROM_HANDLE(tu_physical_device, pdevice, physicalDevice);
++#ifdef __APPLE__
++ (void)fd;
++ (void)pdevice;
++ return true; /* one GPU */
++#else
+ return wsi_common_drm_devices_equal(fd, pdevice->local_fd);
++#endif
+ #else
+ return true;
+ #endif
+diff --git a/src/vulkan/wsi/meson.build b/src/vulkan/wsi/meson.build
+index 6933eb7..b7ea38d 100644
+--- a/src/vulkan/wsi/meson.build
++++ b/src/vulkan/wsi/meson.build
+@@ -5,7 +5,7 @@ files_vulkan_wsi = files('wsi_common.c')
+ links_vulkan_wsi = []
+ platform_deps = []
+
+-if dep_libdrm.found()
++if dep_libdrm.found() and host_machine.system() != 'darwin'
+ files_vulkan_wsi += files('wsi_common_drm.c')
+ endif
+
diff --git a/tools/mk-turnip.sh b/tools/mk-turnip.sh
new file mode 100755
index 0000000..637efc6
--- /dev/null
+++ b/tools/mk-turnip.sh
@@ -0,0 +1,34 @@
+#!/bin/bash
+# Build Turnip, Mesa's Vulkan driver for the Adreno, for the Darwin userland: the first piece of a Metal that runs on
+# Vulkan (UniXNU's AdrenoMetal: metal2vulkan over Turnip). The tree is tools/mk-mesa.sh's (the pinned release with
+# tools/mesa/*.patch on, turnip-darwin.patch among them); run that first. Turnip's msm backend talks to the kernel's
+# GPU driver through tools/mesa/hakios-drm, the stand-in for libdrm, whose syncobjs (Turnip's fences and semaphores)
+# are kept in the process over the kernel's fences.
+#
+# What comes out: xnu-deps/build/mesa/out/libvulkan_freedreno.dylib (install name /redwood/lib/libvulkan_freedreno.dylib),
+# loaded by its vk_icdGetInstanceProcAddr like any Vulkan driver.
+#
+# Needs on this machine what mk-mesa.sh needs, and glslangValidator (apt: glslang-tools) for Turnip's own shaders.
+set -e
+ROOT=$(cd "$(dirname "$0")/.." && pwd)
+VERSION=25.0.7
+B=$ROOT/xnu-deps/build/mesa
+BUILD=build-turnip
+cd "$B"
+[ -d mesa-$VERSION ] && [ -f cross.ini ] || { echo "mk-turnip.sh: no Mesa tree in $B: tools/mk-mesa.sh first" >&2; exit 1; }
+grep -q with_freedreno_vk mesa-$VERSION/meson.build ||
+ { echo "mk-turnip.sh: the tree has not got turnip-darwin.patch: tools/mk-mesa.sh again" >&2; exit 1; }
+command -v glslangValidator >/dev/null || { echo "mk-turnip.sh: no glslangValidator (apt: glslang-tools)" >&2; exit 1; }
+export PATH=$B/venv/bin:$PATH
+[ -f $BUILD/build.ninja ] || meson setup $BUILD mesa-$VERSION --cross-file cross.ini --wrap-mode=nofallback \
+ --prefix=/redwood --libdir=lib --buildtype=release -Db_ndebug=true \
+ -Dplatforms= -Dgallium-drivers= -Dvulkan-drivers=freedreno -Dfreedreno-kmds=msm -Dopengl=false -Dgles1=disabled \
+ -Dgles2=disabled -Dglx=disabled -Degl=disabled -Dgbm=disabled -Dllvm=disabled -Dzstd=disabled -Dxmlconfig=disabled \
+ -Dexpat=disabled -Dshader-cache=disabled -Dvalgrind=disabled -Dlibunwind=disabled -Dglvnd=disabled -Dvideo-codecs= \
+ -Dtools= -Dbuild-tests=false > turnip-setup.log
+ninja -C $BUILD src/freedreno/vulkan/libvulkan_freedreno.dylib > turnip-ninja.log
+mkdir -p out
+cp $BUILD/src/freedreno/vulkan/libvulkan_freedreno.dylib out/
+install_name_tool -id /redwood/lib/libvulkan_freedreno.dylib out/libvulkan_freedreno.dylib 2>/dev/null ||
+ "$ROOT/xnu-deps/host/bin/install_name_tool" -id /redwood/lib/libvulkan_freedreno.dylib out/libvulkan_freedreno.dylib
+echo "mk-turnip.sh: Turnip $VERSION: $B/out/libvulkan_freedreno.dylib ($(stat -c %s out/libvulkan_freedreno.dylib) bytes)"