diff --git a/IOGPUMetalDeviceShim.h b/IOGPUMetalDeviceShim.h new file mode 100644 index 0000000..50b721e --- /dev/null +++ b/IOGPUMetalDeviceShim.h @@ -0,0 +1,241 @@ +// SPDX-License-Identifier: BSD-2-Clause-Patent +// macOS: what +[IOGPUMetalDevice registerAcceleratorService:] and its clients take from an IOGPUMetalDevice, which the native frontend's +// device (an _MTLDevice) is not. From macOS-DroidVM's macOS Metal plugin (overlay/metal/NativeAdrenoMetalPlugin.mm), on the same frontend +// classes (NativeBridgeMetalFrontend.mm: NBBuffer, NBQueue, ...): +// - registration hands the device the accelerator's service object (-_setAcceleratorService:, kept like IOGPU's ivar); Metal's wrappers +// set themselves (-_setDeviceWrapper:, weak; -_deviceWrapper is the device itself without one: _MTLDevice's nil made +// MTLCopyDeviceForRegistryID answer nil); +// - -acceleratorPort replaces _MTLDevice's 0: SkyLight composites with Metal only when the first device has a port +// (WSGetDefaultMetalAccelerator), else in software (CompositorSW); +// - IOGPU's answers about features this device has not got, its resource-pool housekeeping, queue priorities, per-command-buffer +// userDictionary (MPS), gpuCoreCount (MPS), bitCodeHash (stitched libraries), and the Metal protocol conformances clients check; +// - any other IOGPU selector a client sends gets a stub answering 0, named on stderr once per class (a survey, not an abort). +#pragma once +#if TARGET_OS_OSX +#import +#include +#include +#include +#include +#include +#include + +namespace iogpu { + +inline io_service_t gAcceleratorPort; +inline id gAcceleratorService; +inline __weak id gDeviceWrapper; + +static void setAcceleratorService(id, SEL, id service) { gAcceleratorService = service; } +static io_service_t acceleratorPort(id, SEL) { return gAcceleratorPort; } +static void setDeviceWrapper(id, SEL, id wrapper) { gDeviceWrapper = wrapper; } +static id deviceWrapper(id self, SEL) { id wrapper = gDeviceWrapper; return wrapper ? wrapper : self; } +static BOOL isBuiltIn(id, SEL) { return YES; } +static BOOL no(id, SEL) { return NO; } +static uint64_t zero(id, SEL) { return 0; } +static uint32_t one(id, SEL) { return 1; } +static void nothing(id, SEL) {} +static void nothingWith(id, SEL, uint64_t) {} +static BOOL amplificationCount(id, SEL, NSUInteger count) { return count == 1; } +static uint64_t commandBufferArgumentsSize(id, SEL) { return 48; } // IOGPU's +// Unified memory: two thirds of RAM, as Metal recommends on Apple GPUs. +static uint64_t workingSetSize(id, SEL) { return NSProcessInfo.processInfo.physicalMemory / 3 * 2; } +static uint64_t stub(id, SEL) { return 0; } + +// The frontend's classes and the IOGPU class each stands in for; the device is any other class. +static Class referenceClass(Class cls) +{ + static const struct { const char *frontend, *iogpu; } classes[] = { + {"NBBuffer", "IOGPUMetalBuffer"}, {"NBTexture", "IOGPUMetalTexture"}, {"NBHeap", "IOGPUMetalHeap"}, + {"NBQueue", "IOGPUMetalCommandQueue"}, {"NBCommandBuffer", "IOGPUMetalCommandBuffer"}, + {"NBComputeEncoder", "IOGPUMetalComputeCommandEncoder"}, {"NBRenderEncoder", "IOGPUMetalRenderCommandEncoder"}, + {"NBBlitEncoder", "IOGPUMetalBlitCommandEncoder"}, {"NBEncoder", "IOGPUMetalCommandEncoder"}, {"NBFence", "IOGPUMetalFence"}, + {"NBSharedEvent", "IOGPUMetalSharedEvent"}, {"NBEvent", "IOGPUMetalEvent"}, {"NBObject", nullptr}, + }; + for (Class c = cls; c; c = class_getSuperclass(c)) + for (const auto &entry : classes) + if (!strcmp(class_getName(c), entry.frontend)) + return entry.iogpu ? objc_getClass(entry.iogpu) : nil; + return objc_getClass("IOGPUMetalDevice"); +} + +// Only the selectors of the IOGPU class the object stands in for: -respondsToSelector: stays NO for everything a real IOGPU object does +// not answer either. Silent when called: WindowServer sends some for every texture of every frame. +static BOOL resolveStub(id cls, SEL, SEL selector) +{ + Class reference = referenceClass((Class)cls); + Method method = reference ? class_getInstanceMethod(reference, selector) : nullptr; + if (!method) + return NO; + fprintf(stderr, "NativeAdrenoMetal: %s: stubbed %s %s\n", getprogname(), class_getName((Class)cls), sel_getName(selector)); + return class_addMethod((Class)cls, selector, (IMP)stub, method_getTypeEncoding(method)); +} + +// IOGPUMetalCommandQueue's GPU priorities (MTLGPUPriority 0-5; IOGPU starts a queue at 1, its background priority at 2), kept on the queue. +static char kPriorityKey, kBackgroundPriorityKey; +static NSUInteger priorityOf(id queue, const void *key, NSUInteger initial) +{ + NSNumber *value = objc_getAssociatedObject(queue, key); + return value ? value.unsignedIntegerValue : initial; +} +static NSUInteger getGPUPriority(id self, SEL) { return priorityOf(self, &kPriorityKey, 1); } +static NSUInteger getBackgroundGPUPriority(id self, SEL) { return priorityOf(self, &kBackgroundPriorityKey, 2); } +static void setPriorities(id self, NSUInteger priority, NSUInteger background) +{ + objc_setAssociatedObject(self, &kPriorityKey, @(priority), OBJC_ASSOCIATION_RETAIN_NONATOMIC); + objc_setAssociatedObject(self, &kBackgroundPriorityKey, @(background), OBJC_ASSOCIATION_RETAIN_NONATOMIC); +} +static BOOL setGPUPriority(id self, SEL, NSUInteger priority) +{ + setPriorities(self, priority, getBackgroundGPUPriority(self, nullptr)); + return YES; +} +static BOOL setGPUPriorityOffset(id self, SEL, NSUInteger priority, NSUInteger) { return setGPUPriority(self, nullptr, priority); } +static BOOL setBothGPUPriorities(id self, SEL, NSUInteger priority, NSUInteger background) +{ + setPriorities(self, priority, background); + return YES; +} + +// IOGPUMetalCommandBuffer's userDictionary: MPS keeps its per-buffer state in it (a nil one lost it and crashed WindowServer's effects). +static char kUserDictionaryKey; +static id userDictionary(id self, SEL) +{ + @synchronized(self) { + NSMutableDictionary *dictionary = objc_getAssociatedObject(self, &kUserDictionaryKey); + if (!dictionary) { + dictionary = [NSMutableDictionary dictionary]; + objc_setAssociatedObject(self, &kUserDictionaryKey, dictionary, OBJC_ASSOCIATION_RETAIN_NONATOMIC); + } + return dictionary; + } +} + +// MPSCore's MPSDevice::GetMPSDevice reads the core count for every Apple GPU family; the Adreno's shader processors count as eight. +static NSUInteger gpuCoreCount(id, SEL) { return 8; } + +// _MTLFunction's bitCodeHash, a 32-byte digest the stitched-library path hashes: the function's name and a per-process serial. +static char kBitCodeHashKey; +static const void *bitCodeHash(id self, SEL) +{ + @synchronized(self) { + NSData *hash = objc_getAssociatedObject(self, &kBitCodeHashKey); + if (!hash) { + static std::atomic serial; + NSString *text = [NSString stringWithFormat:@"%@#%llu", [self valueForKey:@"name"], (unsigned long long)++serial]; + NSData *bytes = [text dataUsingEncoding:NSUTF8StringEncoding]; + NSMutableData *digest = [NSMutableData dataWithLength:CC_SHA256_DIGEST_LENGTH]; + CC_SHA256(bytes.bytes, (CC_LONG)bytes.length, (unsigned char *)digest.mutableBytes); + hash = digest; + objc_setAssociatedObject(self, &kBitCodeHashKey, hash, OBJC_ASSOCIATION_RETAIN_NONATOMIC); + } + return hash.bytes; + } +} + +struct MethodSpec { + const char *name; + IMP imp; + const char *types; + bool override; // also over a base class's answer +}; + +static void addMethods(Class cls, const MethodSpec *methods, size_t count) +{ + for (size_t i = 0; i < count; i++) { + SEL selector = sel_registerName(methods[i].name); + if (methods[i].override || ![cls instancesRespondToSelector:selector]) + class_addMethod(cls, selector, methods[i].imp, methods[i].types); + } +} + +// The device the frontend made for this process, made an IOGPUMetalDevice for its clients, on the accelerator port. +static void install(id device, io_service_t port) +{ + IOObjectRetain(port); + gAcceleratorPort = port; + static const MethodSpec deviceMethods[] = { + {"_setAcceleratorService:", (IMP)setAcceleratorService, "v24@0:8@16", false}, + {"acceleratorPort", (IMP)acceleratorPort, "I16@0:8", true}, + {"_setDeviceWrapper:", (IMP)setDeviceWrapper, "v24@0:8@16", true}, + {"_deviceWrapper", (IMP)deviceWrapper, "@16@0:8", true}, + {"isBuiltIn", (IMP)isBuiltIn, "B16@0:8", false}, + {"supportPriorityBand", (IMP)no, "B16@0:8", false}, + {"supportsBackgroundAppRole", (IMP)no, "B16@0:8", false}, + {"isSlotted", (IMP)no, "B16@0:8", false}, + {"supportsResourceDetachBacking", (IMP)no, "B16@0:8", false}, + {"supportsVertexAmplification", (IMP)no, "B16@0:8", false}, + {"supportsVertexAmplificationCount:", (IMP)amplificationCount, "B24@0:8Q16", false}, + {"maxTransferRate", (IMP)zero, "Q16@0:8", false}, + {"location", (IMP)zero, "Q16@0:8", false}, + {"locationNumber", (IMP)zero, "Q16@0:8", false}, + {"peerGroupID", (IMP)zero, "Q16@0:8", false}, + {"peerIndex", (IMP)zero, "I16@0:8", false}, + {"peerCount", (IMP)one, "I16@0:8", false}, + {"dedicatedMemorySize", (IMP)zero, "Q16@0:8", false}, + {"hwResourcePoolCount", (IMP)zero, "I16@0:8", false}, + {"cmdBufArgsSize", (IMP)commandBufferArgumentsSize, "Q16@0:8", false}, + {"kickCleanupQueue", (IMP)nothing, "v16@0:8", false}, + {"updateGPUSelectionProperties", (IMP)nothing, "v16@0:8", false}, + {"updateResourcePoolPurgeability", (IMP)nothing, "v16@0:8", false}, + {"periodicUpdateResourcePoolPurgeability", (IMP)nothing, "v16@0:8", false}, + {"cancelPeriodicUpdateResourcePoolPurgeability", (IMP)nothing, "v16@0:8", false}, + {"recommendedMaxWorkingSetSize", (IMP)workingSetSize, "Q16@0:8", false}, + {"sharedMemorySize", (IMP)workingSetSize, "Q16@0:8", false}, + {"currentAllocatedSize", (IMP)zero, "Q16@0:8", false}, + {"gpuCoreCount", (IMP)gpuCoreCount, "Q16@0:8", false}, + // _MTLGetMTLCompilerLLVMVersionForDevice reads it for a stitched library: below 8 it takes the default compiler version. + {"featureProfile", (IMP)zero, "Q16@0:8", false}, + }; + Class cls = object_getClass(device); + addMethods(cls, deviceMethods, sizeof deviceMethods / sizeof *deviceMethods); + class_addMethod(object_getClass(cls), @selector(resolveInstanceMethod:), (IMP)resolveStub, "B24@0:8:16"); + static const MethodSpec queueMethods[] = { + {"getGPUPriority", (IMP)getGPUPriority, "Q16@0:8", false}, + {"getBackgroundGPUPriority", (IMP)getBackgroundGPUPriority, "Q16@0:8", false}, + {"setGPUPriority:", (IMP)setGPUPriority, "B24@0:8Q16", false}, + {"setGPUPriority:offset:", (IMP)setGPUPriorityOffset, "B32@0:8Q16Q24", false}, + {"_setGPUPriority:backgroundPriority:", (IMP)setBothGPUPriorities, "B32@0:8Q16Q24", false}, + }; + if (Class queue = objc_getClass("NBQueue")) + addMethods(queue, queueMethods, sizeof queueMethods / sizeof *queueMethods); + // IOGPUMetalResource's: the process charged for a resource's memory (one pool, charged to nobody) and aliasing (a heap's placement + // never reuses a live resource's range). + static const MethodSpec resourceMethods[] = { + {"setResponsibleProcess:", (IMP)nothingWith, "v20@0:8i16", false}, + {"makeAliasable", (IMP)nothing, "v16@0:8", false}, + }; + for (const char *name : {"NBBuffer", "NBTexture"}) + if (Class resource = objc_getClass(name)) + addMethods(resource, resourceMethods, sizeof resourceMethods / sizeof *resourceMethods); + static const MethodSpec commandBufferMethods[] = {{"userDictionary", (IMP)userDictionary, "@16@0:8", true}}; + static const MethodSpec functionMethods[] = {{"bitCodeHash", (IMP)bitCodeHash, "^v16@0:8", false}}; + if (Class function = objc_getClass("NBFunction")) + addMethods(function, functionMethods, 1); + if (Class commandBuffer = objc_getClass("NBCommandBuffer")) + addMethods(commandBuffer, commandBufferMethods, 1); + // The frontend's classes declare no Metal protocols, and clients check them (MPSCommandBufferImageCache returns nil for a command + // buffer that does not conform to MTLCommandBuffer). + static const char *const conformances[][2] = { + {"NBBuffer", "MTLBuffer"}, {"NBTexture", "MTLTexture"}, {"NBLibrary", "MTLLibrary"}, {"NBFunction", "MTLFunction"}, + {"NBComputePipeline", "MTLComputePipelineState"}, {"NBRenderPipeline", "MTLRenderPipelineState"}, + {"NBSampler", "MTLSamplerState"}, {"NBDepthStencil", "MTLDepthStencilState"}, {"NBEvent", "MTLEvent"}, + {"NBSharedEvent", "MTLSharedEvent"}, {"NBFence", "MTLFence"}, {"NBQueue", "MTLCommandQueue"}, + {"NBCommandBuffer", "MTLCommandBuffer"}, {"NBEncoder", "MTLCommandEncoder"}, {"NBComputeEncoder", "MTLComputeCommandEncoder"}, + {"NBRenderEncoder", "MTLRenderCommandEncoder"}, {"NBBlitEncoder", "MTLBlitCommandEncoder"}, {"NBHeap", "MTLHeap"}, + {"NBBinaryArchive", "MTLBinaryArchive"}, + }; + for (const auto &conformance : conformances) { + Class frontend = objc_getClass(conformance[0]); + Protocol *protocol = objc_getProtocol(conformance[1]); + if (frontend && protocol) + class_addProtocol(frontend, protocol); + else + fprintf(stderr, "NativeAdrenoMetal: %s: no %s for %s\n", getprogname(), conformance[1], conformance[0]); + } + if (Class object = objc_getClass("NBObject")) + class_addMethod(object_getClass(object), @selector(resolveInstanceMethod:), (IMP)resolveStub, "B24@0:8:16"); +} + +} // namespace iogpu +#endif // TARGET_OS_OSX diff --git a/NativeAdrenoMetalPlugin.mm b/NativeAdrenoMetalPlugin.mm index 0387e31..47d9348 100644 --- a/NativeAdrenoMetalPlugin.mm +++ b/NativeAdrenoMetalPlugin.mm @@ -8,6 +8,12 @@ // gets its own native device too (its own driver connection, GPU address space and shader cache), as every process does with Apple's // own GPU plugin. #import +#include +#include +#include +#include +#include +#include "NativeRenderServer.h" #include "SharedShaderCache.h" #include #include @@ -30,13 +36,24 @@ #include "PrimeShaders.h" #include "ExceptionDiag.h" +#if TARGET_OS_OSX +#include "IOGPUMetalDeviceShim.h" +// macOS: +[IOGPUMetalDevice registerAcceleratorService:] takes the bundle's MetalPluginClassName only if it subclasses IOGPUMetalDevice. +@interface IOGPUMetalDevice : NSObject +@end +#else @interface _MTLDevice : NSObject @end +#endif @interface NativeAdrenoMetalDeviceBase : NSObject // only a lock object for the published mode's one-time setup @end @implementation NativeAdrenoMetalDeviceBase @end +#if TARGET_OS_OSX +@interface NativeAdrenoMetalDevice : IOGPUMetalDevice +#else @interface NativeAdrenoMetalDevice : _MTLDevice +#endif - (id)initWithAcceleratorPort:(unsigned)port; @end @@ -80,7 +97,7 @@ static bool readTask(uintptr_t address, void *out, size_t size) { vm_size_t got = 0; return vm_read_overwrite(mach_task_self(), address, size, (vm_address_t) out, &got) == KERN_SUCCESS && got == size; } -static bool writeTask(uintptr_t address, const void *data, size_t size) { +[[maybe_unused]] static bool writeTask(uintptr_t address, const void *data, size_t size) { // iPadOS only (24A446 QuartzCore) return vm_write(mach_task_self(), address, (vm_offset_t) data, (mach_msg_type_number_t) size) == KERN_SUCCESS; } // EXPERIMENT (second report onward): this display path takes plain 8-bit BGRA only, so tell the render server's display that it has neither the deep @@ -106,7 +123,7 @@ static bool bootArgSet(const char *key) { const size_t length = strlen(key); return at && (at[length] == 0 || at[length] == ' '); } -static unsigned patchFramebufferBranch(uintptr_t slide) { +[[maybe_unused]] static unsigned patchFramebufferBranch(uintptr_t slide) { const uintptr_t site = 0x184461928 + slide; uint32_t word = 0; if (!readTask(site, &word, sizeof word)) return 0x6E40; @@ -216,6 +233,42 @@ static id clientDevice(unsigned port) { } return device; } +#if TARGET_OS_OSX +// The compositor's QuartzCore asserts metallib_url != nil (CA::OGL::new_metal_context: [[NSBundle bundleForClass:[CALayer class]] +// URLForResource:@"default" withExtension:@"metallib"]) on this layout (macOS run32), though both the root and the SystemOS volume hold +// QuartzCore.framework/Versions/A/Resources/default.metallib. Measured here, in the render server, with the same calls, before QuartzCore +// makes its Metal context: what the bundle resolves to, and what readdir and NSFileManager list at the bundle's root and in Versions. +static void quartzCoreBundleReport() { + @autoreleasepool { + Class layer = NSClassFromString(@"CALayer"); + NSBundle *bundle = layer ? [NSBundle bundleForClass:layer] : nil; + NSURL *url = [bundle URLForResource:@"default" withExtension:@"metallib"]; + os_log(OS_LOG_DEFAULT, "NativeAdrenoMetal qc: bundle %{public}s resources %{public}s exec %{public}s url %{public}s", + bundle.bundlePath.UTF8String ?: "-", bundle.resourcePath.UTF8String ?: "-", bundle.executablePath.UTF8String ?: "-", + url.path.UTF8String ?: "nil"); + NSString *root = bundle.bundlePath ?: @"/System/Library/Frameworks/QuartzCore.framework"; + for (NSString *sub in @[@"", @"/Versions", @"/Versions/A", @"/Versions/A/Resources", @"/Resources"]) { + NSString *path = [root stringByAppendingString:sub]; + std::string listing; + if (DIR *d = opendir(path.fileSystemRepresentation)) { + while (struct dirent *e = readdir(d)) {listing += e->d_name; listing += ' ';} + closedir(d); + } else listing = "(opendir " + std::to_string(errno) + ")"; + NSArray *items = [NSFileManager.defaultManager contentsOfDirectoryAtPath:path error:nil]; + struct stat st = {}; + const int sr = lstat(path.fileSystemRepresentation, &st); + // opendir reads a whole union directory (both layers) only when fstatfs reports MNT_UNION (0x20) for it. + struct statfs sf = {}; + const int fr = statfs(path.fileSystemRepresentation, &sf); + os_log(OS_LOG_DEFAULT, "NativeAdrenoMetal qc: %{public}s statfs %d flags 0x%x %{public}s on %{public}s", + path.UTF8String, fr, sf.f_flags, sf.f_fstypename, sf.f_mntonname); + os_log(OS_LOG_DEFAULT, "NativeAdrenoMetal qc: %{public}s lstat %d mode %o dev %d readdir [%{public}s] fm [%{public}s]", + path.UTF8String, sr, st.st_mode, st.st_dev, listing.c_str(), + [[items componentsJoinedByString:@" "] UTF8String] ?: "nil"); + } + } +} +#endif static id deviceForPort(unsigned port) { static id device; static bool tried; @@ -264,7 +317,23 @@ static id deviceForPort(unsigned port) { // log-gpu=1: the GPU stack's log lines (NSLog, the pipeline journal); off without it. if (bootArgSet("log-gpu=1")) setenv("SHENG_LOG_GPU", "1", 1); else unsetenv("SHENG_LOG_GPU"); - if (strcmp(getprogname(), "backboardd") != 0) return device = clientDevice(port); +#if TARGET_OS_OSX + // macOS: Turnip opens the accelerator by class (AGXAccelerator here); the display takes plain rows (no Apple framebuffer or IOSurface + // compression). The runtime keeps its default block size: the virtual-machine board shrank it for a 512 MiB GPU pool, which this + // GPU's window does not have. The render server (WindowServer) takes the same path as backboardd on iPadOS below (display hand-off + // client, shader cache directory, pipeline pack, priming), without the iPadOS QuartzCore addresses; every device gets IOGPU's + // answers (IOGPUMetalDeviceShim.h). + setenv("TU_DARWIN_SERVICE", "AGXAccelerator", 1); + setenv("CA_DISABLE_FRAMEBUFFER_COMPRESSION", "1", 1); + CFPreferencesSetAppValue(CFSTR("CAEnableFramebufferCompression"), kCFBooleanFalse, kCFPreferencesCurrentApplication); + if (!nativeIsRenderServer()) { + device = clientDevice(port); + if (device) iogpu::install(device, port); + return device; + } +#else + if (!nativeIsRenderServer()) return device = clientDevice(port); +#endif // The discovery releases the accelerator port when it is done; the monitor and the scanout client keep using it. if (void *iokit = dlopen("/System/Library/Frameworks/IOKit.framework/IOKit", RTLD_NOW | RTLD_LOCAL)) { if (auto retain = (int (*)(unsigned))dlsym(iokit, "IOObjectRetain")) retain(port); @@ -322,6 +391,8 @@ static id deviceForPort(unsigned port) { setenv("CA_DISPLAY_EDR_HEADROOM", "1", 1); setenv("CA_FP16_MIN_HEADROOM", "1e30", 1); setenv("CA_ALLOW_EDR_MIRRORING", "0", 1); +#if !TARGET_OS_OSX + // The stored option values and the framebuffer branch are addresses of iPadOS 27's QuartzCore (24A446). if (void *start = dlsym(RTLD_DEFAULT, "CARenderServerStart")) { const uintptr_t slide = (uintptr_t)ptrauth_strip(start, ptrauth_key_function_pointer) - 0x1846dce88; const float one = 1.0f, never = 1e30f; @@ -362,6 +433,7 @@ static id deviceForPort(unsigned port) { }, gpu) == 0) pthread_detach(reader); } +#endif // !TARGET_OS_OSX #endif // The runtime's creation log reports each VkPipeline making's time to the device's sink, which the telemetry's V row sums // (diag=1). @@ -377,7 +449,7 @@ static id deviceForPort(unsigned port) { // so with a persistent Data store the pipelines were still compiled again each boot (V row hit0). Try the user's Library/Caches first // (marker 0x6814); the earlier candidates keep their markers. if (const passwd *user = getpwuid(getuid()); user && user->pw_dir && user->pw_dir[0]) { - snprintf(candidates[4], sizeof candidates[4] - 16, "%s/Library/Caches/com.apple.backboardd", user->pw_dir); + snprintf(candidates[4], sizeof candidates[4] - 16, "%s/Library/Caches/com.apple." NATIVE_RENDER_SERVER, user->pw_dir); mkdir(candidates[4], 0700); strlcat(candidates[4], "/", sizeof candidates[4]); } @@ -456,7 +528,7 @@ static id deviceForPort(unsigned port) { pthread_attr_destroy(&attributes); // The GMEM colour order test (NativeTurnipSwapTest.h), once, 20 s after the device is published: each result line goes to the // kernel's GPU trace as a problem record (event 65). - if (bootArgSet("swaptest=1") && !strcmp(getprogname(), "backboardd")) { + if (bootArgSet("swaptest=1") && nativeIsRenderServer()) { pthread_t tester; pthread_create(&tester, nullptr, [](void *) -> void * { sleep(20); @@ -477,6 +549,10 @@ static id deviceForPort(unsigned port) { pthread_detach(tester); } gpu->mark(0x6803); +#if TARGET_OS_OSX + iogpu::install(device, port); + quartzCoreBundleReport(); +#endif return device; } } @@ -484,8 +560,18 @@ static id deviceForPort(unsigned port) { @implementation NativeAdrenoMetalDevice - (id)initWithAcceleratorPort:(unsigned)port { +#if TARGET_OS_OSX + // The registered device is the frontend's object, not this one: IOGPUMetalDevice's own initializer would open an IOGPU user client. + // This object is kept, because -[IOGPUMetalDevice dealloc] tears down state its initializer never made. + static NSMutableArray *kept; + static dispatch_once_t once; + dispatch_once(&once, ^{ kept = [NSMutableArray array]; }); + @synchronized(kept) {[kept addObject:self];} + if (!port) return nil; +#else self = [super init]; if (!self || !port) return nil; +#endif if (publish::requested()) return publish::deviceForPort(port); static bool attempted; if (attempted) return nil; @@ -512,7 +598,11 @@ static id deviceForPort(unsigned port) { auto matching = (void *(*)(const char *))dlsym(iokit, "IOServiceMatching"); auto getService = (unsigned (*)(unsigned, void *))dlsym(iokit, "IOServiceGetMatchingService"); if (matching && getService) { +#if TARGET_OS_OSX + const unsigned own = getService(0, matching("AGXAccelerator")); +#else const unsigned own = getService(0, matching("IOAcceleratorES")); +#endif if (own) port = own; } } @@ -525,3 +615,80 @@ static id deviceForPort(unsigned port) { return nil; } @end + +#if TARGET_OS_OSX +#include +#include +#include +// WindowServer's frame pacing, as macOS-DroidVM's plugin (24af265, kc253-kc292), each behind its boot argument. +// +// ca-triple-buffer=1 sets QuartzCore's debug option 198, CA_FORCE_TRIPLE_BUFFERED_LATENCY (read by IOMFBDisplay::can_update_): +// CoreAnimation renders a frame ahead instead of starting each frame only after the last one's buffer came back, which it otherwise +// does whenever a frame took longer than a refresh (DroidVM: a heavy window drag from 56-60 to 61-67 frames a second). +__attribute__((constructor)) static void tripleBufferFromBootArgs() { + if (strcmp(getprogname(), "WindowServer") || !publish::bootArgSet("ca-triple-buffer=1")) return; + auto setOption = reinterpret_cast(dlsym(RTLD_DEFAULT, "CASetDebugOption")); + if (setOption) setOption(198, true); + os_log(OS_LOG_DEFAULT, "NativeAdrenoMetal: CA_FORCE_TRIPLE_BUFFERED_LATENCY %{public}s", setOption ? "set" : "not found"); +} + +// ca-free-wakeup=1: SkyLight starts each WindowServer update at -[CAWindowServerDisplay nextWakeupTime], which QuartzCore puts on the +// refresh grid (the last VBL plus a frame), so an update that took 13 ms is followed by one at 16.7 ms. This answers the earlier of that +// and a refresh after the last update's start (-beginExternalUpdate:usingSoftwareRenderer:): the display shows a swap when it comes, +// not at a VBL. SkyLight adds its updater delay (0.5 ms) and updates at once only when the last update is more than 1.25 frames before +// the answer, so before the due time the answer is the due time less that delay, after it the least time 1.25 frames past the last +// start. Without an update in the last second the grid's answer stands. The refresh is vsync='s (120 by default). +// DroidVM: a heavy window drag from 60 to 96 frames a second. +static std::atomic gUpdateBegan{0}, gLastAsk{0}; +static double hostSeconds(uint64_t t) { + static mach_timebase_info_data_t base; + if (!base.denom) mach_timebase_info(&base); + return (double)t * base.numer / base.denom / 1e9; +} +static uint64_t hostTicks(double seconds) { + static mach_timebase_info_data_t base; + if (!base.denom) mach_timebase_info(&base); + return (uint64_t)(seconds * 1e9 * base.denom / base.numer); +} +__attribute__((constructor)) static void freeWakeupFromBootArgs() { + if (strcmp(getprogname(), "WindowServer") || !publish::bootArgSet("ca-free-wakeup=1")) return; + char args[1100] = {}; + size_t size = sizeof args - 1; + unsigned hz = 120; + if (sysctlbyname("kern.bootargs", args, &size, nullptr, 0) == 0) + if (const char *at = publish::bootArgToken(args, "vsync=")) { + const int value = atoi(at + 6); + if (value >= 30 && value <= 240) hz = (unsigned)value; + } + const double refresh = 1.0 / hz; + Class display = objc_getClass("CAWindowServerDisplay"); + static SEL wakeSel = sel_registerName("nextWakeupTime"), beginSel = sel_registerName("beginExternalUpdate:usingSoftwareRenderer:"); + Method wake = display ? class_getInstanceMethod(display, wakeSel) : nullptr; + Method begin = display ? class_getInstanceMethod(display, beginSel) : nullptr; + if (!wake || !begin || method_getTypeEncoding(wake)[0] != 'd' || method_getNumberOfArguments(begin) != 4) { + os_log(OS_LOG_DEFAULT, "NativeAdrenoMetal: free wakeup: CAWindowServerDisplay lacks %{public}s", + !wake ? "nextWakeupTime" : "beginExternalUpdate:usingSoftwareRenderer:"); + return; + } + static auto wakeGrid = reinterpret_cast(method_getImplementation(wake)); + static auto beginOriginal = reinterpret_cast(method_getImplementation(begin)); + method_setImplementation(begin, imp_implementationWithBlock(^uint64_t(id self, void *update, uint64_t software) { + // The update's start is SkyLight's last ask before it, where its update callback began. + const uint64_t now = mach_absolute_time(), asked = gLastAsk.load(std::memory_order_relaxed); + gUpdateBegan.store(asked && now - asked < hostTicks(0.003) ? asked : now, std::memory_order_relaxed); + return beginOriginal(self, beginSel, update, software); + })); + method_setImplementation(wake, imp_implementationWithBlock(^double(id self) { + const double grid = wakeGrid(self, wakeSel); + gLastAsk.store(mach_absolute_time(), std::memory_order_relaxed); + const uint64_t began = gUpdateBegan.load(std::memory_order_relaxed); + if (grid <= 0 || !began) return grid; + const double now = hostSeconds(mach_absolute_time()), start = hostSeconds(began); + if (now - start > 1.0) return grid; + const double due = start + refresh - 0.0005, ahead = start + 1.25 * refresh + 0.001; + if (now < due) return due < grid ? due : grid; + return now > ahead ? now : ahead; + })); + os_log(OS_LOG_DEFAULT, "NativeAdrenoMetal: free wakeup on (%.2f ms)", refresh * 1e3); +} +#endif diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 5c78424..d2084ed 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -11,6 +11,7 @@ #import #import #import +#include "NativeRenderServer.h" #include #include #include @@ -222,7 +223,7 @@ static uint32_t threadCpuUs() { return (uint32_t)((uint64_t)(info.user_time.seconds+info.system_time.seconds)*1000000ull+(uint64_t)(info.user_time.microseconds+info.system_time.microseconds)); } static uint32_t threadIdLow() {uint64_t id=0;pthread_threadid_np(NULL,&id);return (uint32_t)id;} -static bool traceWanted() {static const bool on=diagOn() && !strcmp(getprogname(),"backboardd");return on;} +static bool traceWanted() {static const bool on=diagOn() && nativeIsRenderServer();return on;} static void tracePush(const TraceRec &rec) { if(!traceWanted())return; if(rec.type==12 && (rec.a>=40 || !native_gpu_latency_stage(rec.a)))return; @@ -342,7 +343,7 @@ static void traceStartWriter() { // The file is the render server's: SpringBoard, which runs as the same user, opened the same path with O_TRUNC and the two processes' // records overwrote each other's at their own offsets (untagged-3f2bd680: SpringBoard's header, two interleaved stage sequences). // Only the render server writes the file; no other process reads its Turnip rings. - static const bool render=!strcmp(getprogname(),"backboardd"); + static const bool render=nativeIsRenderServer(); if(!diagOn() || !render)return; bool expected=false; if(!gTraceStarted.compare_exchange_strong(expected,true))return; @@ -559,7 +560,7 @@ static bool logGpuOn() {static const bool on=getenv("SHENG_LOG_GPU")!=nullptr;re // the display shows the surfaces it composed last until backboardd restarts (the hang's evidence is in the host's KGSL log // and the context's loss mask, not here). End it once its completed handlers have seen the error; launchd starts it // again, SpringBoard with it, and the screen comes back at the lock screen. Other clients get AccessRevoked and decide. -static bool isRenderServer() {static const bool renderServer=!strcmp(getprogname(),"backboardd");return renderServer;} +static bool isRenderServer() {return nativeIsRenderServer();} static void endRenderServerAfterGpuLoss() { static std::atomic ending{false}; if(!isRenderServer() || ending.exchange(true))return; @@ -927,7 +928,7 @@ static void waitForResource(id resource); - (void)countUse; @end // The render server counts the surfaces it textures in use while the textures live (countUse). -static const bool gCountsSurfaceUse=!strcmp(getprogname(),"backboardd"); +static const bool gCountsSurfaceUse=nativeIsRenderServer(); // A no-copy import keeps its pages until the runtime frees it, which can be after the buffer object goes (a command buffer still using // it). The owner's release (Metal's deallocator, an IOSurface's mapping) waits for that, as Metal's does: run earlier, the owner could // unmap or reuse pages the GPU still reads or writes. @@ -4234,8 +4235,25 @@ static bool pipelineArchives(NSArray *list,std::vector &arch NBLibrary *library=[NBLibrary new];library.handle=handle;library.storage=mapped;library.bytes=(const uint8_t *)bytes;library.length=size; if(error)*error=nil;return (id)library; } +// A library file that cannot be read (macOS run36: QuartzCore's default.metallib failed with NSCocoaErrorDomain 260 on a fresh Data +// volume and WindowServer exited): the path, the read's errno and each component's lstat (mode, device, inode) and link target. +static void reportUnreadableLibrary(NSString *path) { + const char *full=path.fileSystemRepresentation;if(!full)return; + const int fd=open(full,O_RDONLY);const int openErrno=fd<0?errno:0;if(fd>=0)close(fd); + std::string walk;std::string prefix; + for(const char *at=full;*at;) { + const char *next=strchr(at+1,'/');const std::string part(at,next?next-at:strlen(at));prefix+=part; + struct stat st={};const int r=lstat(prefix.c_str(),&st);char link[256]={}; + if(r==0 && S_ISLNK(st.st_mode))readlink(prefix.c_str(),link,sizeof link-1); + char item[400];snprintf(item,sizeof item," [%s %d %o %d/%llu%s%s]",part.c_str(),r==0?0:errno,st.st_mode,st.st_dev,(unsigned long long)st.st_ino, + link[0]?" ->":"",link); + walk+=item;if(!next)break;at=next; + } + os_log(OS_LOG_DEFAULT,"NativeAdrenoMetal library unreadable: %{public}s open errno %d:%{public}s",full,openErrno,walk.c_str()); +} - (id)newLibraryWithFile:(NSString *)path error:(NSError **)error { - NSData *bytes=path?[NSData dataWithContentsOfFile:path options:NSDataReadingMappedIfSafe error:error]:nil;if(!bytes)return nil; + NSData *bytes=path?[NSData dataWithContentsOfFile:path options:NSDataReadingMappedIfSafe error:error]:nil; + if(!bytes){if(path)reportUnreadableLibrary(path);return nil;} const uint64_t libraryBegan=nowUs(); NmbLibrary *handle=nmb_library_new(gDevice,bytes.bytes,bytes.length,false);gTimeLibrary.add(libraryBegan); if(!handle)return failWith>(error,@"library",nil); diff --git a/NativeRenderServer.h b/NativeRenderServer.h new file mode 100644 index 0000000..8aa9c31 --- /dev/null +++ b/NativeRenderServer.h @@ -0,0 +1,19 @@ +// SPDX-License-Identifier: MIT +// The system's render server, the process that composes the display and gets the render-server path of the plugin (display hand-off client, +// shader cache directory, pipeline priming), the IOSurface use counts and the frame trace: backboardd on iPadOS, WindowServer on macOS. The +// kernel's NativeAdrenoPublish.cpp names the same process (row 0, its mailbox page). +#pragma once +#include +#include +#include + +#if TARGET_OS_OSX +#define NATIVE_RENDER_SERVER "WindowServer" +#else +#define NATIVE_RENDER_SERVER "backboardd" +#endif + +inline bool nativeIsRenderServer() { + static const bool renderServer = !strcmp(getprogname(), NATIVE_RENDER_SERVER); + return renderServer; +} diff --git a/PrimeShaders.h b/PrimeShaders.h index 7b35a3d..56dbf1e 100644 --- a/PrimeShaders.h +++ b/PrimeShaders.h @@ -13,6 +13,7 @@ #pragma once #import #import +#include #include #include #include @@ -42,7 +43,12 @@ extern "C" {volatile uint32_t native_prime_compiling, native_prime_libraries_don extern "C" void native_bridge_metal_quiet_problems(bool); namespace prime { +#if TARGET_OS_OSX +// macOS: the system apps are in /System/Applications; the OS cryptex is reached through /System/Cryptexes/OS as on iPadOS. +static const char *const kRoots[] = {"/System/Library", "/System/Applications", "/Applications", "/System/Cryptexes/OS/System/Library"}; +#else static const char *const kRoots[] = {"/System/Library", "/Applications", "/System/Cryptexes/OS/System/Library"}; +#endif static std::set readLines(const std::string &path) { std::set lines;