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
UniXNU's Metal on Vulkan runs on the phone: m2v_test (PASS)
yehorkaa4eb6776f60c7
diff --git a/tools/m2v/adrenometal-ios16.patch b/tools/m2v/adrenometal-ios16.patchnew file mode 100644index 0000000..1b1ca43--- /dev/null+++ b/tools/m2v/adrenometal-ios16.patch@@ -0,0 +1,13 @@+diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm+index aec500d..3b42251 100644+--- a/NativeBridgeMetalFrontend.mm++++ b/NativeBridgeMetalFrontend.mm+@@ -4550,7 +4550,7 @@ extern "C" id native_bridge_metal_open(const char *loader,const char *cache,uint+ #if defined(__arm64e__)+ if(!base){NBLOG(@"native-metal: no _MTLDevice");return nil;}+ #else+- if(!base || class_getInstanceSize(base)!=536){NBLOG(@"native-metal: unexpected _MTLDevice ABI");return nil;}++ if(!base){NBLOG(@"native-metal: no _MTLDevice");return nil;} // (hakios: iOS 16.5, whose _MTLDevice is of another size)+ #endif+ #endif+ if(!base)base=[NSObject class];diff --git a/tools/m2v/ios16-compat.h b/tools/m2v/ios16-compat.hnew file mode 100644index 0000000..f355244--- /dev/null+++ b/tools/m2v/ios16-compat.h@@ -0,0 +1,12 @@+/*+ * What AdrenoMetal's frontend (written against the iPadOS 17 SDK) takes from Metal's headers that the iOS 16.5 SDK+ * has not got: forced into each of its files by tools/mk-m2v.sh (-include).+ */+#pragma once+#ifdef __OBJC__+#import <Metal/Metal.h>+#if !defined(__IPHONE_17_0) || __IPHONE_OS_VERSION_MAX_ALLOWED < 170000+typedef MTLArgumentAccess MTLBindingAccess; /* iOS 17's name for the same values */+#define MTLLanguageVersion3_1 ((MTLLanguageVersion)((3 << 16) + 1))+#endif+#endifdiff --git a/tools/mesa/hakios-drm/hakios.drm.c b/tools/mesa/hakios-drm/hakios.drm.cindex add92af..78bb5e8 100644--- a/tools/mesa/hakios-drm/hakios.drm.c+++ b/tools/mesa/hakios-drm/hakios.drm.c@@ -106,8 +106,7 @@ intdrmCommandWriteRead(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) {+ if (index == DRM_MSM_GEM_SUBMIT && size >= sizeof(struct drm_msm_gem_submit)) {return submit_with_syncobjs(data, size);}return -call((unsigned)(DRM_COMMAND_BASE + index), data, size);@@ -522,6 +521,8 @@ drmSyncobjTransfer(int fd, uint32_t dst, uint64_t dst_point, uint32_t src, uint6return 0;}+static uint32_t last_fence; /* the fence of the last submission the kernel took */+static intsubmit_with_syncobjs(struct drm_msm_gem_submit *submit, unsigned long size){@@ -545,8 +546,17 @@ submit_with_syncobjs(struct drm_msm_gem_submit *submit, unsigned long size)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;+ if (submit->nr_cmds == 0) {+ /*+ * Nothing for the GPU (Turnip's submissions that only signal): the kernel takes none, and what it stands for+ * is done when everything submitted before it is, the last fence -- there is one ring.+ */+ submit->fence = __atomic_load_n(&last_fence, __ATOMIC_ACQUIRE);+ } else {+ error = call((unsigned)(DRM_COMMAND_BASE + DRM_MSM_GEM_SUBMIT), submit, size);+ if (error != 0) return -error;+ __atomic_store_n(&last_fence, submit->fence, __ATOMIC_RELEASE);+ }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);diff --git a/tools/mesa/turnip-darwin.patch b/tools/mesa/turnip-darwin.patchindex 4b709a7..1aedc6e 100644--- a/tools/mesa/turnip-darwin.patch+++ b/tools/mesa/turnip-darwin.patch@@ -39,7 +39,7 @@ index d0a38c9..4f68424 100644install : true,)diff --git a/src/freedreno/vulkan/tu_device.cc b/src/freedreno/vulkan/tu_device.cc-index 62ebfd0..72aac15 100644+index 62ebfd0..f1bc5f6 100644--- a/src/freedreno/vulkan/tu_device.cc+++ b/src/freedreno/vulkan/tu_device.cc@@ -2709,6 +2709,7 @@ tu_CreateDevice(VkPhysicalDevice physicalDevice,@@ -58,6 +58,23 @@ index 62ebfd0..72aac15 100644if (pthread_cond_init(&device->timeline_cond, &condattr) != 0) {pthread_condattr_destroy(&condattr);result = vk_startup_errorf(physical_device->instance,+@@ -3015,6 +3017,16 @@ vk_icdGetInstanceProcAddr(VkInstance instance, const char *pName)+ return tu_GetInstanceProcAddr(instance, pName);+ }+++#ifdef __APPLE__++/* No Vulkan loader on Darwin: the driver is the loader too, for what opens it by the loader's entry point++ * (metal2vulkan, through ash::Entry::load_from). */++extern "C" PUBLIC VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL++vkGetInstanceProcAddr(VkInstance instance, const char *pName)++{++ return tu_GetInstanceProcAddr(instance, pName);++}++#endif+++ VKAPI_ATTR VkResult VKAPI_CALL+ tu_AllocateMemory(VkDevice _device,+ const VkMemoryAllocateInfo *pAllocateInfo,diff --git a/src/freedreno/vulkan/tu_knl.cc b/src/freedreno/vulkan/tu_knl.ccindex 80e3900..3f00525 100644--- a/src/freedreno/vulkan/tu_knl.ccdiff --git a/tools/mk-m2v.sh b/tools/mk-m2v.shnew file mode 100755index 0000000..b206622--- /dev/null+++ b/tools/mk-m2v.sh@@ -0,0 +1,77 @@+#!/bin/bash+# Build UniXNU's Metal on Vulkan for the iOS 16.5 userland: AdrenoMetal's frontend (the Metal objects, MIT) over its+# bridge to steelbrain's metal2vulkan runtime (Rust; LGPL-3.0+/GPL-2.0+), whose Vulkan is Turnip (tools/mk-turnip.sh).+# Both sources at the revisions UniXNU qualified, metal2vulkan with AdrenoMetal's 30 patches; on top, what iOS 16.5+# needs (tools/m2v/: two names of the iPadOS 17 SDK, and the size check of Metal's _MTLDevice).+#+# What comes out, in xnu-deps/build/m2v/out/: frontend.o and libadreno_metal_bridge.a, linked into a program or the+# Metal plug-in (with -framework Foundation Metal IOSurface CoreFoundation -lc++), whose native_bridge_metal_open()+# gives the MTLDevice.+#+# Needs on this machine: rustup's cargo with Rust 1.98 and its aarch64-apple-ios target, llvm-as of LLVM 21+# (apt: llvm-21), clang, and the theos iPhoneOS16.5 SDK.+set -e+ROOT=$(cd "$(dirname "$0")/.." && pwd)+B=$ROOT/xnu-deps/build/m2v+SDK=${THEOS:-$HOME/theos}/sdks/iPhoneOS16.5.sdk+M2V_PIN=624bab3918de5b978e1101517abca2b4fa426565 # steelbrain/metal2vulkan, branch development+AM_PIN=243d63cf43305a85c8119b66b49cb7df4b30b019 # UniXNU/AdrenoMetal+export PATH=$HOME/.cargo/bin:$PATH+command -v cargo >/dev/null || { echo "mk-m2v.sh: no cargo (rustup, Rust 1.98, target aarch64-apple-ios)" >&2; exit 1; }+LLVM_AS=$(command -v llvm-as-21 || command -v llvm-as) || { echo "mk-m2v.sh: no llvm-as (apt: llvm-21)" >&2; exit 1; }+mkdir -p $B/out $B/obj+cd $B+fetch() { # repository, revision, directory, patches...+ local url=$1 rev=$2 dir=$3; shift 3+ if [ ! -d $dir ]; then+ git clone -q --filter=blob:none $url $dir+ git -C $dir checkout -q $rev+ for p in "$@"; do git -C $dir apply "$p"; done+ fi+ [ "$(git -C $dir rev-parse HEAD)" = $rev ] || { echo "mk-m2v.sh: $dir is not at $rev" >&2; exit 1; }+}+fetch https://github.com/UniXNU/AdrenoMetal.git $AM_PIN AdrenoMetal $ROOT/tools/m2v/adrenometal-ios16.patch+A=$B/AdrenoMetal+fetch https://github.com/steelbrain/metal2vulkan.git $M2V_PIN metal2vulkan $A/bridge/patches/00*.patch+S=$B/metal2vulkan++# the bridge: the runtime behind a C interface, as UniXNU's scripts/build-adreno-metal-bridge.py builds it+$LLVM_AS -o obj/runtime_examples.air $S/crates/metal2vulkan-validation/fixtures/public/examples/runtime_examples.ll+$LLVM_AS -o obj/function_constant.air $S/crates/metal2vulkan-validation/fixtures/public/kernel_function_constant_gate_fold.ll+$LLVM_AS -o obj/scanout_convert.air $A/shaders/scanout_convert.ll+# (the manifest in a directory of its own: one in $B would take the trees below it into its workspace)+mkdir -p bridge+cat > bridge/Cargo.toml <<EOF+[package]+name = "adreno-metal-bridge"+version = "0.1.0"+edition = "2024"+rust-version = "1.98"+[workspace]+[lib]+crate-type = ["staticlib"]+path = "$A/bridge/NativeMetalBridge.rs"+[dependencies]+metal2vulkan = { path = "$S/crates/metal2vulkan", features = ["ash"] }+metal2vulkan-runtime = { path = "$S/crates/metal2vulkan-runtime" }+libc = "0.2"+sha2 = { version = "0.10", features = ["asm"] }+[profile.release]+panic = "unwind"+debug = 1+lto = "fat"+codegen-units = 1+EOF+CC_aarch64_apple_ios=clang CFLAGS_aarch64_apple_ios="-target arm64-apple-ios16.0 -isysroot $SDK" \+AR_aarch64_apple_ios=$(command -v llvm-ar-21 || command -v llvm-ar) IPHONEOS_DEPLOYMENT_TARGET=16.0 SDKROOT=$SDK \+NMB_RUNTIME_ID=$(echo $M2V_PIN | cut -c1-32) NMB_FIXTURE_AIR=$B/obj/runtime_examples.air \+NMB_FC_AIR=$B/obj/function_constant.air NMB_SCANOUT_AIR=$B/obj/scanout_convert.air \+ cargo build --release --manifest-path bridge/Cargo.toml --target aarch64-apple-ios --target-dir target > cargo.log 2>&1 ||+ { tail -30 cargo.log >&2; exit 1; }+cp target/aarch64-apple-ios/release/libadreno_metal_bridge.a out/++# the frontend: the Metal objects+clang++ -target arm64-apple-ios16.0 -isysroot $SDK -O2 -std=c++17 -fobjc-arc -Wno-deprecated-declarations \+ -include $ROOT/tools/m2v/ios16-compat.h -I $A -I $ROOT/xnu-deps/build/mesa/mesa-25.0.7/include \+ -x objective-c++ -c $A/NativeBridgeMetalFrontend.mm -o out/frontend.o+echo "mk-m2v.sh: $B/out: frontend.o ($(stat -c %s out/frontend.o) bytes), libadreno_metal_bridge.a ($(stat -c %s out/libadreno_metal_bridge.a) bytes)"diff --git a/userland/build.sh b/userland/build.shindex 6802708..6788222 100755--- a/userland/build.sh+++ b/userland/build.sh@@ -50,6 +50,9 @@# TURNIP=1 with Turnip, Mesa's Vulkan driver for the Adreno (tools/mk-turnip.sh), as# lib/libvulkan_freedreno.dylib (11 MB), and bin/vk_test, which runs it once round# (src/vk_test.c; RC_JOBS=rc.jobs.vktest): the first piece of a Metal on Vulkan+# M2V=1 (with TURNIP=1) and UniXNU's Metal on Vulkan (tools/mk-m2v.sh: AdrenoMetal's+# frontend over metal2vulkan), in bin/m2v_test, which opens it as a Metal device+# and draws (src/m2v_test.m; RC_JOBS=rc.jobs.m2vtest)# ADSP=1 with the audio DSP's firmware from ../fw-audio in fw/ (20 MB; for the# kernel's adsp_step=N, drivers/audio/hakios.adsp.c, which runs before the root is# mounted). Not beside GL=1: the two do not fit the 64 MB the loader takes with@@ -323,6 +326,12 @@ if [ -d $DARWIN/rootfs/usr/lib ]; then[ -f $M/libvulkan_freedreno.dylib ] || ../tools/mk-turnip.sh$DYN -I../xnu-deps/build/mesa/mesa-25.0.7/include -o obj/vk_test src/vk_test.ccp $M/libvulkan_freedreno.dylib obj/+ if [ -n "${M2V:-}" ]; then+ V=../xnu-deps/build/m2v/out+ [ -f $V/frontend.o ] || ../tools/mk-m2v.sh+ ${DYN/xnu-clang/xnu-clang++} -fobjc-arc -o obj/m2v_test src/m2v_test.m $V/frontend.o $V/libadreno_metal_bridge.a \+ -framework Foundation -framework Metal -framework IOSurface -framework CoreFoundation -lc++ -lobjc+ fifificp obj/hello_dyn obj/svc_test obj/surf_test obj/rwtouch obj/mg_test root/bin/@@ -368,6 +377,7 @@ if [ -d $DARWIN/rootfs/usr/lib ]; thenif [ -n "${TURNIP:-}" ]; thencp obj/libvulkan_freedreno.dylib root/lib/cp obj/vk_test root/bin/+ [ -f obj/m2v_test ] && cp obj/m2v_test root/bin/fiif [ -n "$GL" ]; thencp obj/libOSMesa.8.dylib root/lib/diff --git a/userland/rc.jobs.m2vtest b/userland/rc.jobs.m2vtestnew file mode 100644index 0000000..ef06834--- /dev/null+++ b/userland/rc.jobs.m2vtest@@ -0,0 +1,4 @@+# Commands run by the launchd job com.hakios.demo (see com.hakios.demo.plist): UniXNU's Metal on Vulkan once round+# (src/m2v_test.m), in an image built with TURNIP=1 M2V=1+/redwood/bin/m2v_test+/redwood/bin/cputop 10 &diff --git a/userland/src/m2v_test.m b/userland/src/m2v_test.mnew file mode 100644index 0000000..296309f--- /dev/null+++ b/userland/src/m2v_test.m@@ -0,0 +1,168 @@+/*+ * Metal over Vulkan on the phone, the smallest whole round: UniXNU's AdrenoMetal frontend and its metal2vulkan runtime+ * (tools/mk-m2v.sh) over Turnip (tools/mk-turnip.sh), linked into this program and opened as the plug-in would open it+ * (native_bridge_metal_open), with no window server in between. A buffer filled by a blit, a texture cleared by a render+ * pass and read back, each checked; then the clear a hundred times, timed. RC_JOBS=rc.jobs.m2vtest runs it (an image+ * built with TURNIP=1 M2V=1).+ */+#import <Foundation/Foundation.h>+#import <Metal/Metal.h>+#include <signal.h>+#include <stdio.h>+#include <dlfcn.h>+#include <sys/stat.h>+#include <sys/ucontext.h>+#include <time.h>+#include <unistd.h>++id native_bridge_metal_open(const char *loader, const char *cache, uint64_t registryID, void (*missing)(const char *, const char *));+size_t native_bridge_metal_status(char *out, size_t size);+uint64_t native_bridge_metal_first_problem(char *out, size_t size);++#define TURNIP "/redwood/lib/libvulkan_freedreno.dylib"+#define CACHE "/private/var/tmp/m2v"++static double+now_ms(void)+{+ struct timespec t;++ clock_gettime(CLOCK_MONOTONIC, &t);+ return t.tv_sec * 1e3 + t.tv_nsec / 1e6;+}++static void+missing(const char *class, const char *selector)+{+ printf("m2v_test: missing -[%s %s]\n", class, selector);+}++static void+where(const char *what, uintptr_t a)+{+ Dl_info info;++ if (dladdr((void *)a, &info) && info.dli_fname != NULL)+ printf("m2v_test: %s %#lx = %s+%#lx (%s)\n", what, (unsigned long)a, info.dli_fname,+ (unsigned long)(a - (uintptr_t)info.dli_fbase), info.dli_sname ? info.dli_sname : "?");+ else+ printf("m2v_test: %s %#lx\n", what, (unsigned long)a);+}++static void+crashed(int sig, siginfo_t *si, void *context)+{+ ucontext_t *uc = context;++ printf("m2v_test: signal %d at address %p\n", sig, si->si_addr);+ where("pc", (uintptr_t)uc->uc_mcontext->__ss.__pc);+ where("lr", (uintptr_t)uc->uc_mcontext->__ss.__lr);+ uintptr_t fp = (uintptr_t)uc->uc_mcontext->__ss.__fp;+ for (int i = 0; i < 16 && fp != 0 && (fp & 7) == 0; i++) {+ where("frame", ((uintptr_t *)fp)[1]);+ fp = ((uintptr_t *)fp)[0];+ }+ _exit(128 + sig);+}++static int+status(int failed)+{+ char line[512] = "", problem[256] = "";++ native_bridge_metal_status(line, sizeof line);+ printf("m2v_test: status %s\n", line);+ if (native_bridge_metal_first_problem(problem, sizeof problem))+ printf("m2v_test: first problem: %s\n", problem);+ printf("m2v_test: %s\n", failed ? "FAIL" : "PASS");+ return failed;+}++int+main(void)+{+ struct sigaction sa = { .sa_sigaction = crashed, .sa_flags = SA_SIGINFO };+ sigaction(SIGSEGV, &sa, NULL);+ sigaction(SIGBUS, &sa, NULL);+ sigaction(SIGILL, &sa, NULL);+ sigaction(SIGABRT, &sa, NULL);+ sigaction(SIGTRAP, &sa, NULL);+ setvbuf(stdout, NULL, _IONBF, 0);+ dup2(1, 2); /* the runtime's and Turnip's messages (stderr) beside ours */+ setenv("TU_DEBUG", "startup", 0);+ mkdir("/private/var/tmp", 0777);+ mkdir(CACHE, 0700);++ @autoreleasepool {+ double t0 = now_ms();+ id<MTLDevice> device = native_bridge_metal_open(TURNIP, CACHE, 0, missing);+ if (device == nil) {+ printf("m2v_test: no device\n");+ return status(1);+ }+ printf("m2v_test: device \"%s\" (class %s) in %.1f ms\n", device.name.UTF8String,+ object_getClassName(device), now_ms() - t0);++ id<MTLCommandQueue> queue = [device newCommandQueue];+ const NSUInteger words = 1u << 20;+ id<MTLBuffer> buffer = [device newBufferWithLength:words * 4 options:MTLResourceStorageModeShared];+ memset(buffer.contents, 0, words * 4);+ id<MTLCommandBuffer> cb = [queue commandBuffer];+ id<MTLBlitCommandEncoder> blit = [cb blitCommandEncoder];+ [blit fillBuffer:buffer range:NSMakeRange(0, words * 4) value:0x5a];+ [blit endEncoding];+ t0 = now_ms();+ [cb commit];+ [cb waitUntilCompleted];+ const uint32_t *w = buffer.contents;+ NSUInteger right = 0;+ for (NSUInteger i = 0; i < words; i++)+ right += w[i] == 0x5a5a5a5a;+ printf("m2v_test: blit fill: %lu of %lu words right in %.2f ms (status %ld)%s\n", (unsigned long)right,+ (unsigned long)words, now_ms() - t0, (long)cb.status, right == words ? "" : " -- WRONG");+ if (right != words)+ return status(1);++ const NSUInteger width = 1080, height = 2400;+ MTLTextureDescriptor *td = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm+ width:width height:height mipmapped:NO];+ td.usage = MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead;+ td.storageMode = MTLStorageModeShared;+ id<MTLTexture> texture = [device newTextureWithDescriptor:td];+ MTLRenderPassDescriptor *rp = [MTLRenderPassDescriptor renderPassDescriptor];+ rp.colorAttachments[0].texture = texture;+ rp.colorAttachments[0].loadAction = MTLLoadActionClear;+ rp.colorAttachments[0].storeAction = MTLStoreActionStore;+ rp.colorAttachments[0].clearColor = MTLClearColorMake(1.0, 0.5, 0.0, 1.0);+ cb = [queue commandBuffer];+ [[cb renderCommandEncoderWithDescriptor:rp] endEncoding];+ t0 = now_ms();+ [cb commit];+ [cb waitUntilCompleted];+ double first = now_ms() - t0;+ uint32_t *pixels = malloc(width * height * 4);+ [texture getBytes:pixels bytesPerRow:width * 4 fromRegion:MTLRegionMake2D(0, 0, width, height) mipmapLevel:0];+ NSUInteger good = 0;+ for (NSUInteger i = 0; i < width * height; i++)+ good += (pixels[i] & 0xffffff) == 0xff8000 || (pixels[i] & 0xffffff) == 0xff7f00;+ printf("m2v_test: clear of %lux%lu: %lu of %lu pixels orange in %.2f ms (first pixel %#x)%s\n", (unsigned long)width,+ (unsigned long)height, (unsigned long)good, (unsigned long)(width * height), first, pixels[0],+ good == width * height ? "" : " -- WRONG");++ double best = 1e9, sum = 0;+ for (int i = 0; i < 100; i++) {+ cb = [queue commandBuffer];+ [[cb renderCommandEncoderWithDescriptor:rp] endEncoding];+ double a = now_ms();+ [cb commit];+ [cb waitUntilCompleted];+ double d = now_ms() - a;+ sum += d;+ if (d < best)+ best = d;+ }+ printf("m2v_test: 100 full-screen clears, each committed and waited for: %.3f ms on average, %.3f at best\n",+ sum / 100, best);+ return status(good != width * height);+ }+}