Skip to content

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

C

Hosted macOS has a Metal device: kext XnuCpuMetal (kexts/, clean room) publishes an IOAccelerator naming the in-process plugin XnuCpuMetal.bundle (Metal -> metal2vulkan -> lavapipe on the CPU); IOGraphicsFamily kept in the collection; boot-arg lp-sip0 stands in for the device tree's boot policy (Metal trusts /Library/GPUBundles only with CSR_ALLOW_UNRESTRICTED_FS). The service must not carry Removable/SafeEjectRequested at all: Metal skips a service on which either exists. MTLCreateSystemDefaultDevice returns XnuCpuMetal (llvmpipe) and a blit fill reads back right. console-send-file.sh ships files to the console shell in short paced lines (one long typed line gets mangled)

Heyang Zhoufdac157a930d
diff --git a/kexts/XnuCpuMetal/XnuCpuMetal.cpp b/kexts/XnuCpuMetal/XnuCpuMetal.cpp
new file mode 100644
index 0000000..86d2773
--- /dev/null
+++ b/kexts/XnuCpuMetal/XnuCpuMetal.cpp
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: BSD-2-Clause
+// XnuCpuMetal: the hosted macOS's Metal device, as a kext of the kernel collection (written for xnulinux, clean room).
+// Metal.framework finds GPUs by watching IOServiceMatching("IOAccelerator") and loads the plugin bundle named by the service's
+// MetalPluginName from /Library/GPUBundles, taking MetalPluginClassName (an IOGPUMetalDevice subclass) as the device class.
+// XnuCpuMetalDevice runs its work in the calling process (Metal -> metal2vulkan -> Mesa lavapipe on the CPU), so the service
+// is only a registry entry: no user client, no hardware. It matches IOResources, so no device tree node is needed.
+#include <IOKit/IOService.h>
+#include <IOKit/IOLib.h>
+#include <mach/kmod.h>
+
+// IOGraphicsFamily's abstract accelerator class; kc-build fills the subclass's vtable from it (the JSON's "vtable").
+class IOAccelerator : public IOService {
+ OSDeclareAbstractStructors(IOAccelerator)
+};
+
+class XnuCpuMetalAccelerator : public IOAccelerator {
+ OSDeclareDefaultStructors(XnuCpuMetalAccelerator)
+public:
+ bool start(IOService *provider) override;
+};
+
+OSDefineMetaClassAndStructors(XnuCpuMetalAccelerator, IOAccelerator)
+
+bool XnuCpuMetalAccelerator::start(IOService *provider)
+{
+ if (!IOService::start(provider)) return false;
+ setProperty("MetalPluginName", "XnuCpuMetal");
+ setProperty("MetalPluginClassName", "XnuCpuMetalDevice");
+ // No "Removable" / "SafeEjectRequested": Metal (-[MTLIOAccelService initWithAcceleratorPort:deviceClass:]) skips
+ // a service on which either is present at all, whatever the value.
+ setProperty("model", "XnuCpuMetal (lavapipe)");
+ registerService();
+ IOLog("xnucpumetal: Metal accelerator published on %s\n", provider->getName());
+ return true;
+}
+
+extern "C" {
+kern_return_t native_kext_start(kmod_info_t *, void *) { return KERN_SUCCESS; }
+kern_return_t native_kext_stop(kmod_info_t *, void *) { return KERN_SUCCESS; }
+}
diff --git a/kexts/XnuCpuMetal/XnuCpuMetal.json b/kexts/XnuCpuMetal/XnuCpuMetal.json
new file mode 100644
index 0000000..23f9908
--- /dev/null
+++ b/kexts/XnuCpuMetal/XnuCpuMetal.json
@@ -0,0 +1,32 @@
+{
+ "name": "XnuCpuMetal",
+ "bundle": "org.xnulinux.driver.XnuCpuMetal",
+ "version": "1.0.0",
+ "objects": "out/x12377/native-kexts/XnuCpuMetal/objects.json",
+ "start": "_native_kext_start",
+ "stop": "_native_kext_stop",
+ "libraries": {
+ "com.apple.iokit.IOGraphicsFamily": "1.0.0b1",
+ "com.apple.kpi.iokit": "18.0.0",
+ "com.apple.kpi.libkern": "18.0.0",
+ "com.apple.kpi.mach": "18.0.0"
+ },
+ "personalities": {
+ "XnuCpuMetal": {
+ "CFBundleIdentifier": "org.xnulinux.driver.XnuCpuMetal",
+ "IOClass": "XnuCpuMetalAccelerator",
+ "IOProviderClass": "IOResources",
+ "IOResourceMatch": "IOKit",
+ "IOMatchCategory": "IOAccelerator",
+ "MetalPluginName": "XnuCpuMetal",
+ "MetalPluginClassName": "XnuCpuMetalDevice"
+ }
+ },
+ "vtable": {
+ "class": "XnuCpuMetalAccelerator",
+ "base": "IOAccelerator",
+ "base_kext": "com.apple.iokit.IOGraphicsFamily",
+ "slots": 272,
+ "ignore_slots": [16]
+ }
+}
diff --git a/scripts/console-send-file.sh b/scripts/console-send-file.sh
new file mode 100755
index 0000000..a41f1a0
--- /dev/null
+++ b/scripts/console-send-file.sh
@@ -0,0 +1,9 @@
+#!/bin/bash
+# console-send-file.sh LOCAL REMOTE: types LOCAL into the hosted macOS console shell as short base64 lines (one every
+# 0.3 s, so the tty input queue never overflows: one long typed line gets mangled by the line editor), decoded to REMOTE.
+cd "$(dirname "$0")/.."
+f=$1; r=$2
+{ echo "rm -f $r.b64"
+ base64 < "$f" | tr -d '\n' | fold -w 100 | while read -r l || [ -n "$l" ]; do echo "echo $l >> $r.b64"; done
+ echo "base64 -D -i $r.b64 -o $r"
+} | bash scripts/vm.sh 'while IFS= read -r l; do printf "%s\n" "$l" >> /var/tmp/console-in; sleep 0.3; done'
diff --git a/scripts/vm-gpu-prep.sh b/scripts/vm-gpu-prep.sh
new file mode 100644
index 0000000..0c658dc
--- /dev/null
+++ b/scripts/vm-gpu-prep.sh
@@ -0,0 +1,15 @@
+#!/bin/bash
+# in the VM: Metal plugin bundle onto the golden system disk, kernel collection with XnuCpuMetal + IOGraphicsFamily
+set -e -o pipefail
+IMG=/var/tmp/full.img
+sudo dmsetup remove xnusys 2>/dev/null || true; for l in $(losetup -j $IMG -O NAME -n); do sudo losetup -d $l; done
+sudo fsck.hfsplus -fy $IMG 2>&1 | tail -1
+sudo mkdir -p /mnt/sys; sudo mount -o loop,rw,force -t hfsplus $IMG /mnt/sys
+sudo mkdir -p /mnt/sys/Library/GPUBundles
+sudo rm -rf /mnt/sys/Library/GPUBundles/XnuCpuMetal.bundle
+sudo tar -C /mnt/sys/Library/GPUBundles --no-same-owner -xzf /var/tmp/XnuCpuMetal.bundle.tgz
+sudo chown -R 0:0 /mnt/sys/Library/GPUBundles
+sudo du -sh /mnt/sys/Library/GPUBundles/XnuCpuMetal.bundle
+df -h /mnt/sys | tail -1
+sudo umount /mnt/sys; sudo fsck.hfsplus -fy $IMG | tail -1
+[ -n "${SKIP_REBUILD:-}" ] || bash ~/w/poc/scripts/vm-xnu-rebuild.sh 2>&1 | tail -25
diff --git a/scripts/vm-xnu-rebuild.sh b/scripts/vm-xnu-rebuild.sh
index 2744450..96c35b5 100755
--- a/scripts/vm-xnu-rebuild.sh
+++ b/scripts/vm-xnu-rebuild.sh
@@ -20,7 +20,16 @@ if [ -e out/kc/kc27.bin ]; then
echo "kernel slots moved: linking the kernel again"
JOBS=12 bash k27/build-kernel27.sh 2>&1 | tail -3
fi
- K27_NO_BOOT=1 NATIVE= bash k27/try.sh 0 2>&1 | tail -3
+ # xnulinux's own kexts (kexts/*/NAME.json + NAME.cpp), built against the k27 kernel's objects and linked into the collection
+ NATIVE=
+ for j in ~/w/poc/kexts/*/*.json; do
+ [ -e "$j" ] || continue
+ n=$(basename $j .json)
+ python3 scripts/build-native-kext.py $n $(dirname $j)/$n.cpp --obj ${KEXT_OBJ:-k27-obj}
+ NATIVE="$NATIVE $j"
+ done
+ K27_NO_BOOT=1 NATIVE="$NATIVE" bash k27/try.sh 0 2>&1 | tail -3
+ grep -iE 'error|unresolved|XnuCpuMetal' out/k27/kc-build.log | tail -8 || true
sudo cp out/k27/kc-k27.bin /lib/firmware/xnu/kc.bin
fi
ls -la out/k27/kernel-k27.macho
diff --git a/tools/hosted/tests/b.m b/tools/hosted/tests/b.m
new file mode 100644
index 0000000..3f25305
--- /dev/null
+++ b/tools/hosted/tests/b.m
@@ -0,0 +1,28 @@
+#import <Foundation/Foundation.h>
+#import <Metal/Metal.h>
+#import <objc/runtime.h>
+@interface NSObject (X)
+- (id)initWithAcceleratorPort:(unsigned)port;
+@end
+int main(void) {
+ @autoreleasepool {
+ NSBundle *b = [NSBundle bundleWithPath:@"/Library/GPUBundles/XnuCpuMetal.bundle"];
+ NSError *e = nil;
+ printf("load: %d %s\n", [b loadAndReturnError:&e], e ? e.description.UTF8String : "");
+ Class c = [b principalClass];
+ printf("principal: %s\n", c ? class_getName(c) : "nil");
+ id<MTLDevice> d = [[c alloc] initWithAcceleratorPort:0];
+ printf("direct init: %s\n", d ? d.name.UTF8String : "nil");
+ if (d) {
+ id<MTLBuffer> buf = [d newBufferWithLength:4096 options:0];
+ id<MTLCommandQueue> q = [d newCommandQueue];
+ id<MTLCommandBuffer> cb = [q commandBuffer];
+ id<MTLBlitCommandEncoder> be = [cb blitCommandEncoder];
+ [be fillBuffer:buf range:NSMakeRange(0, 4096) value:7];
+ [be endEncoding]; [cb commit]; [cb waitUntilCompleted];
+ unsigned char *p = buf.contents;
+ printf("blit fill: %d %d status %ld\n", p[0], p[4095], (long)cb.status);
+ }
+ }
+ return 0;
+}
diff --git a/tools/hosted/tests/crash.sh b/tools/hosted/tests/crash.sh
new file mode 100644
index 0000000..75970e2
--- /dev/null
+++ b/tools/hosted/tests/crash.sh
@@ -0,0 +1,18 @@
+cd /tmp
+ls -t /Library/Logs/DiagnosticReports /var/root/Library/Logs/DiagnosticReports 2>/dev/null | head -8
+f=$(ls -t /Library/Logs/DiagnosticReports/*.ips /var/root/Library/Logs/DiagnosticReports/*.ips 2>/dev/null | head -1)
+echo "REPORT $f"
+python3 - "$f" <<'PY'
+import json,sys
+t=open(sys.argv[1]).read().split('\n',1)
+h=json.loads(t[0]); b=json.loads(t[1])
+print(h.get('app_name'), h.get('name'))
+print('exc', b.get('exception'), 'term', b.get('termination'))
+print('asi', b.get('asi'))
+imgs=b.get('usedImages',[])
+for th in b.get('threads',[]):
+ if th.get('triggered'):
+ for fr in th.get('frames',[])[:14]:
+ im=imgs[fr['imageIndex']] if fr.get('imageIndex') is not None and fr['imageIndex']<len(imgs) else {}
+ print(' ', im.get('name','?'), fr.get('symbol','?'), hex(fr.get('imageOffset',0)))
+PY
diff --git a/tools/hosted/tests/d.swift b/tools/hosted/tests/d.swift
new file mode 100644
index 0000000..d09d6ce
--- /dev/null
+++ b/tools/hosted/tests/d.swift
@@ -0,0 +1,12 @@
+import Foundation
+import Metal
+let p = "/Library/GPUBundles/XnuCpuMetal.bundle/Contents/MacOS/XnuCpuMetal"
+let h = dlopen(p, RTLD_NOW)
+print("dlopen:", h == nil ? "FAILED \(String(cString: dlerror()))" : "ok")
+if let b = Bundle(path: "/Library/GPUBundles/XnuCpuMetal.bundle") {
+ print("bundle principal load:", b.load(), "class:", NSClassFromString("XnuCpuMetalDevice") as Any)
+ if let c: AnyClass = NSClassFromString("XnuCpuMetalDevice"), let s: AnyClass = NSClassFromString("IOGPUMetalDevice") {
+ print("subclass of IOGPUMetalDevice:", c.isSubclass(of: s))
+ }
+}
+print("metal device after load:", MTLCreateSystemDefaultDevice()?.name ?? "nil")
diff --git a/tools/hosted/tests/m.swift b/tools/hosted/tests/m.swift
new file mode 100644
index 0000000..22ddb39
--- /dev/null
+++ b/tools/hosted/tests/m.swift
@@ -0,0 +1,14 @@
+import Metal
+let d = MTLCreateSystemDefaultDevice()
+print("metal device:", d?.name ?? "nil")
+print("all devices:", MTLCopyAllDevices().count)
+if let d = d {
+ let b = d.makeBuffer(length: 4096)!
+ let q = d.makeCommandQueue()!
+ let c = q.makeCommandBuffer()!
+ let e = c.makeBlitCommandEncoder()!
+ e.fill(buffer: b, range: 0..<4096, value: 7)
+ e.endEncoding(); c.commit(); c.waitUntilCompleted()
+ let p = b.contents().bindMemory(to: UInt8.self, capacity: 4096)
+ print("blit fill:", p[0], p[4095], "status", c.status.rawValue)
+}
diff --git a/tools/hosted/tests/s.m b/tools/hosted/tests/s.m
new file mode 100644
index 0000000..4bede5d
--- /dev/null
+++ b/tools/hosted/tests/s.m
@@ -0,0 +1,89 @@
+// Trace Metal's device discovery: wrap the methods on the path from IORegistry to the plugin's device and log each call.
+#import <Foundation/Foundation.h>
+#import <Metal/Metal.h>
+#import <objc/runtime.h>
+#include <dlfcn.h>
+
+static void wrapInst(const char *cls, const char *sel) {
+ Class c = objc_getClass(cls);
+ Method m = c ? class_getInstanceMethod(c, sel_registerName(sel)) : NULL;
+ printf("wrap -[%s %s]: %s\n", cls, sel, m ? "ok" : "missing");
+ if (!m) return;
+ IMP orig = method_getImplementation(m);
+ if (!strcmp(sel, "initWithAcceleratorPort:deviceClass:")) {
+ method_setImplementation(m, imp_implementationWithBlock(^id(id self, unsigned port, Class dc) {
+ printf("-> -[%s initWithAcceleratorPort:%u deviceClass:%s]\n", cls, port, dc ? class_getName(dc) : "nil");
+ id r = ((id (*)(id, SEL, unsigned, Class))orig)(self, sel_registerName(sel), port, dc);
+ printf("<- %p\n", (void *)r);
+ if (r) { unsigned n = 0; Ivar *iv = class_copyIvarList(object_getClass(r), &n);
+ for (unsigned i = 0; i < n; i++) { const char *t = ivar_getTypeEncoding(iv[i]); ptrdiff_t o = ivar_getOffset(iv[i]);
+ unsigned long long v = *(unsigned long long *)((char *)r + o);
+ printf(" ivar %s %s = 0x%llx%s%s\n", ivar_getName(iv[i]), t, v, t[0] == '@' && v ? " " : "", t[0] == '@' && v ? [[[(id)v description] substringToIndex:MIN((NSUInteger)80, [[(id)v description] length])] UTF8String] : ""); } }
+ return r; }));
+ } else if (!strcmp(sel, "processPendingCreateIOAccelServiceRequests")) {
+ method_setImplementation(m, imp_implementationWithBlock(^void(id self) {
+ printf("-> -[%s %s]\n", cls, sel); ((void (*)(id, SEL))orig)(self, sel_registerName(sel)); printf("<- done\n"); }));
+ } else if (!strcmp(sel, "deviceNotifyQueue")) {
+ method_setImplementation(m, imp_implementationWithBlock(^id(id self) {
+ id q = ((id (*)(id, SEL))orig)(self, sel_registerName(sel));
+ printf(" deviceNotifyQueue -> %s\n", q ? dispatch_queue_get_label((dispatch_queue_t)q) : "nil"); return q; }));
+ } else if (!strcmp(sel, "registerService:deviceClass:")) {
+ method_setImplementation(m, imp_implementationWithBlock(^id(id self, unsigned port, Class dc) {
+ printf("-> -[%s registerService:%u deviceClass:%s]\n", cls, port, dc ? class_getName(dc) : "nil");
+ id r = ((id (*)(id, SEL, unsigned, Class))orig)(self, sel_registerName(sel), port, dc);
+ printf("<- %p\n", (void *)r); return r; }));
+ } else if (!strcmp(sel, "initWithAcceleratorPort:")) {
+ method_setImplementation(m, imp_implementationWithBlock(^id(id self, unsigned port) {
+ printf("-> -[%s initWithAcceleratorPort:%u]\n", cls, port);
+ id r = ((id (*)(id, SEL, unsigned))orig)(self, sel_registerName(sel), port);
+ printf("<- %p\n", (void *)r); return r; }));
+ } else if (!strcmp(sel, "init")) {
+ method_setImplementation(m, imp_implementationWithBlock(^id(id self) {
+ printf("-> -[%s init]\n", cls);
+ id r = ((id (*)(id, SEL))orig)(self, sel_registerName(sel));
+ printf("<- %p\n", (void *)r); return r; }));
+ }
+}
+
+static void wrapClass(const char *cls, const char *sel) {
+ Class c = objc_getClass(cls);
+ Method m = c ? class_getClassMethod(c, sel_registerName(sel)) : NULL;
+ printf("wrap +[%s %s]: %s\n", cls, sel, m ? "ok" : "missing");
+ if (!m) return;
+ IMP orig = method_getImplementation(m);
+ method_setImplementation(m, imp_implementationWithBlock(^id(id self, unsigned port) {
+ printf("-> +[%s %s %u]\n", cls, sel, port);
+ id r = ((id (*)(id, SEL, unsigned))orig)(self, sel_registerName(sel), port);
+ printf("<- %p\n", (void *)r); return r; }));
+}
+
+int main(void) {
+ {
+ setvbuf(stdout, NULL, _IONBF, 0);
+ dlopen("/System/Library/PrivateFrameworks/IOGPU.framework/IOGPU", RTLD_NOW);
+ [[NSBundle bundleWithPath:@"/Library/GPUBundles/XnuCpuMetal.bundle"] load];
+ { Class meta = object_getClass(objc_getClass("XnuCpuMetalDevice"));
+ class_addMethod(meta, @selector(resolveClassMethod:), imp_implementationWithBlock(^BOOL(id self, SEL q) {
+ printf("?? +[XnuCpuMetalDevice %s] looked up, not implemented\n", sel_getName(q)); return NO; }), "c@::");
+ class_addMethod(objc_getClass("XnuCpuMetalDevice") ? meta : nil, @selector(resolveInstanceMethod:), imp_implementationWithBlock(^BOOL(id self, SEL q) {
+ printf("?? -[XnuCpuMetalDevice %s] looked up, not implemented\n", sel_getName(q)); return NO; }), "c@::"); }
+ wrapInst("MTLIOAccelServiceGlobalContext", "init");
+ wrapInst("MTLIOAccelService", "initWithAcceleratorPort:deviceClass:");
+ wrapInst("XnuCpuMetalDevice", "initWithAcceleratorPort:");
+ wrapClass("IOGPUMetalDevice", "registerAcceleratorService:");
+ wrapInst("MTLIOAccelServiceGlobalContext", "processPendingCreateIOAccelServiceRequests");
+ wrapInst("MTLIOAccelServiceGlobalContext", "registerService:deviceClass:");
+ wrapInst("MTLIOAccelServiceGlobalContext", "deviceNotifyQueue");
+ { unsigned n = 0; Method *ms = class_copyMethodList(objc_getClass("MTLIOAccelService"), &n);
+ for (unsigned i = 0; i < n; i++) printf(" -MTLIOAccelService %s\n", sel_getName(method_getName(ms[i]))); }
+ NSArray *all = MTLCopyAllDevices();
+ printf("MTLCopyAllDevices: %lu\n", (unsigned long)all.count);
+ id gc = nil; { Class c = objc_getClass("MTLIOAccelServiceGlobalContext"); (void)c; }
+ [[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:2]];
+ printf("after run loop: %lu\n", (unsigned long)MTLCopyAllDevices().count);
+ dispatch_sync(dispatch_get_global_queue(0, 0), ^{ printf("global queue alive\n"); });
+ (void)gc;
+ printf("after 2 s: %lu, default: %p\n", (unsigned long)MTLCopyAllDevices().count, (void *)MTLCreateSystemDefaultDevice());
+ }
+ return 0;
+}
diff --git a/tools/hosted/tests/t.sh b/tools/hosted/tests/t.sh
new file mode 100644
index 0000000..b5ab4fa
--- /dev/null
+++ b/tools/hosted/tests/t.sh
@@ -0,0 +1,10 @@
+cd /tmp
+xcrun swiftc -O m.swift -o m 2>&1 | tail -3
+./m 2>&1 | tail -5
+I="/Applications/Xcode.app/Contents/Applications/Icon Composer.app/Contents/Executables/ictool"
+rm -rf /tmp/t.icon /tmp/t.png; cp -R "/var/root/src/stats-master/Stats/Supporting Files/Stats.icon" /tmp/t.icon
+"$I" /tmp/t.icon --export-image --output-file /tmp/t.png --platform macOS --rendition Default --width 16 --height 16 --scale 2 > /tmp/ic.log 2>&1
+echo "ictool rc=$?"; grep -v "^$" /tmp/ic.log | tail -8; ls -la /tmp/t.png
+log show --last 2m --style compact 2>/dev/null | grep -i "metal\|gpubundle\|XnuCpu\|IOGPU" | tail -15
+grep -rln "8080" /var/root/src/Alamofire-5.10.2/Tests | head -3
+grep -rn "8080\|firewalk" /var/root/src/Alamofire-5.10.2/Tests/*.swift | head -4
diff --git a/tools/hosted/tests/t2.sh b/tools/hosted/tests/t2.sh
new file mode 100644
index 0000000..5238a0e
--- /dev/null
+++ b/tools/hosted/tests/t2.sh
@@ -0,0 +1,4 @@
+cd /tmp
+xcrun swiftc -O d.swift -o d 2>&1 | tail -3
+./d 2>&1 | tail -8
+log show --last 3m --style compact 2>/dev/null | grep -v "Sandbox:\|amfid\|AMFI\|kernel.development" | grep -i "metal\|gpubundle\|XnuCpu\|IOGPU\|accelerat" | cut -c1-300 | tail -20
diff --git a/tools/hosted/tests/t3.sh b/tools/hosted/tests/t3.sh
new file mode 100644
index 0000000..bafc1fd
--- /dev/null
+++ b/tools/hosted/tests/t3.sh
@@ -0,0 +1,5 @@
+cd /tmp
+xcrun clang -fobjc-arc b.m -framework Foundation -framework Metal -o b 2>&1 | tail -3
+XNU_CPU_METAL_TRACE=1 ./b 2>&1 | tail -12
+echo "--- discovery with os_log on stderr"
+OS_ACTIVITY_DT_MODE=YES ./m 2>&1 | grep -v "^$" | cut -c1-250 | tail -25
diff --git a/tools/hosted/tests/t4.sh b/tools/hosted/tests/t4.sh
new file mode 100644
index 0000000..81a1932
--- /dev/null
+++ b/tools/hosted/tests/t4.sh
@@ -0,0 +1,6 @@
+cd /tmp
+OS_ACTIVITY_DT_MODE=YES ./m 2>&1 | grep -v "^$\|Server root port\|bootstrap: unknown" | cut -c1-260 | head -40
+echo "--- swift: MTLCopyAllDevices first"
+printf '%s\n' 'import Metal' 'print("all:", MTLCopyAllDevices().map { $0.name })' > a.swift
+xcrun swiftc a.swift -o a 2>&1 | tail -2
+OS_ACTIVITY_DT_MODE=YES ./a 2>&1 | grep -v "^$\|Server root port\|bootstrap: unknown" | cut -c1-260 | head -30
diff --git a/tools/hosted/tests/t5.sh b/tools/hosted/tests/t5.sh
new file mode 100644
index 0000000..190d27c
--- /dev/null
+++ b/tools/hosted/tests/t5.sh
@@ -0,0 +1,3 @@
+cd /tmp
+xcrun clang s.m -framework Foundation -framework Metal -o s 2>&1 | grep -v warning | tail -4
+./s 2>&1 | cut -c1-200 | head -80
diff --git a/tools/hosted/tests/t6.sh b/tools/hosted/tests/t6.sh
new file mode 100644
index 0000000..fa0706d
--- /dev/null
+++ b/tools/hosted/tests/t6.sh
@@ -0,0 +1,5 @@
+cd /tmp
+rm -f /tmp/msgSends-*
+NSObjCMessageLoggingEnabled=YES ./a > /dev/null 2>&1
+f=$(ls /tmp/msgSends-* | head -1); echo "log $f $(wc -l < $f) lines"
+grep -n "XnuCpuMetal\|MTLIOAccel\|IOGPUMetalDevice\|_MTLDevice\|MTLDevice" $f | grep -v "NSString\|NSMutable" | cut -c1-200 | head -60
diff --git a/tools/hosted/tests/t7.sh b/tools/hosted/tests/t7.sh
new file mode 100644
index 0000000..50ed6de
--- /dev/null
+++ b/tools/hosted/tests/t7.sh
@@ -0,0 +1,3 @@
+cd /tmp
+f=$(ls /tmp/msgSends-* | head -1)
+sed -n '14530,14615p' $f | cut -c1-150
diff --git a/xnu-patches/0001-linux64-stage-hook.patch b/xnu-patches/0001-linux64-stage-hook.patch
index 9cbf541..1b161b8 100644
--- a/xnu-patches/0001-linux64-stage-hook.patch
+++ b/xnu-patches/0001-linux64-stage-hook.patch
@@ -278,6 +278,24 @@ index 5362b32..8028c48 100644
#endif /* ETHER */
#if NETWORKING
+diff --git a/bsd/kern/kern_csr.c b/bsd/kern/kern_csr.c
+index 604f16a..7cffe13 100644
+--- a/bsd/kern/kern_csr.c
++++ b/bsd/kern/kern_csr.c
+@@ -226,6 +226,13 @@ csr_bootstrap(void)
+ } else {
+ csr_config &= ~CSR_ALLOW_UNAUTHENTICATED_ROOT;
+ }
++
++ /* LINUX64 (xnulinux): hosted XNU has no boot policy in its device tree; the boot-arg lp-sip0 stands in for it (0x2 lets
++ * rootless_check_trusted_class accept /Library/GPUBundles, where Metal finds the hosted system's plugin). */
++ uint32_t hosted_sip;
++ if (PE_parse_boot_argn("lp-sip0", &hosted_sip, sizeof(hosted_sip))) {
++ csr_config |= hosted_sip & ~CSR_ALLOW_APPLE_INTERNAL;
++ }
+ }
+ STARTUP(TUNABLES, STARTUP_RANK_FIRST, csr_bootstrap);
+
diff --git a/bsd/kern/kern_descrip.c b/bsd/kern/kern_descrip.c
index fac307f..23e1d06 100644
--- a/bsd/kern/kern_descrip.c
diff --git a/xnu-patches/kc-keep-extra.txt b/xnu-patches/kc-keep-extra.txt
index fafe806..ae29ed6 100644
--- a/xnu-patches/kc-keep-extra.txt
+++ b/xnu-patches/kc-keep-extra.txt
@@ -1,2 +1,3 @@
# Apple kexts the hosted macOS needs beyond configs/kc-keep.txt (bundle IDs, one per line)
com.apple.iokit.IOSurface
+com.apple.iokit.IOGraphicsFamily