XNU as a loadable kernel module inside Linux (macOS personality for arm64 Linux)
C
gpu/xnu-cpu-metal: sync host memory on newBufferWithBytesNoCopy copy fallback
Heyang Zhouffdbdbc2598a
diff --git a/gpu/xnu-cpu-metal/REPORT.md b/gpu/xnu-cpu-metal/REPORT.mdindex a2d3d43..fd79da6 100644--- a/gpu/xnu-cpu-metal/REPORT.md+++ b/gpu/xnu-cpu-metal/REPORT.md@@ -569,3 +569,25 @@ dist/XnuCpuMetal.bundle/4. 设恶意 `VK_ICD_FILENAMES=lvp` + `XNU_CPU_METAL_BACKEND=venus`(宿主机无 `XnuDrm`)→ 干净返回 `nil` (`VkResult(-3)`),`libvulkan_lvp` `loaded=0`。5. 模拟 secure-mode 双 ICD bundle(`Resources/vulkan/icd.d/` 同时放 `lvp` + `MoltenVK` 且 `unset VK_ICD_FILENAMES VK_DRIVER_FILES`)→ 默认选中 `llvmpipe`(`libMoltenVK` `loaded=0`),强选 `venus` 返回 `nil`(`libvulkan_lvp` `loaded=0`)。- **全套回归全通过**:`lavapipe (arm64e)` 与 `MoltenVK (arm64)` 的 `test-bundle.sh`、`test-render-ci.sh`、`test_round10`、`test_carenderer`、`test_round13`、`test_round14_vfx` 全部 PASS。++---++## Round 18: 修复 `WindowServer` 在 Venus 下黑屏(无 `VK_EXT_external_memory_host` 时的 `newBufferWithBytesNoCopy` 回退同步)++### 根因定位(托管 macOS `WindowServer` 实时排查)+1. 在托管 macOS 中运行独立 `test_carenderer`(`Virtio-GPU Venus (Apple M5 Max)`),`color`/`image`/`two_layers`/`rounded_only`/`rounded_shadow` 5 项全部像素精确匹配原生 Apple M5 Max;12 种常见像素格式 × 3 种 usage 在 Venus 上全部创建成功。+2. 在 `WindowServer` 中增加 `newTexture` / `newRenderPSO` / `newComputePSO` / `syncToIOSurface` 诊断日志,确认:+ - 0 个 `newTexture` / `newRenderPSO` / `newComputePSO` 失败;+ - `syncToIOSurface` 回写 `1280×800 &BGA` (`0x26424741`) 与 `1280×30 BGRA` 全部 `ok=1`,但内容全为 `BGRA(0,0,0,255)`(`nz=1,024,000/4,096,000` 与 `38,400/153,600`);+ - 唯一错误日志是 `newBufferWithBytesNoCopy` 在 Venus 上因缺少 `VK_EXT_external_memory_host` 触发拷贝回退(`Unsupported(HostMemory, needs: ExtExternalMemoryHost)`,涵盖 4 KB – 98 KB 的图元/文字/图层缓冲及 33 MB 壁纸缓冲)。+3. **直接根因**:`xcmDev_newBufferNoCopy` 回退到 `xm2v_buffer_new` 拷贝路径时,此前只在有 `deallocator` 时记录 `_fallbackPtr`(仅用于 `dealloc` 释放),**从未在后续提交 GPU 前将调用方写入 `_fallbackPtr` 的新数据同步回 Vulkan 缓冲**,导致 `WindowServer` / `QuartzCore` 通过 `newBufferWithBytesNoCopy:` + 后续写入宿主指针或 `newLinearTextureWithDescriptor:` 提交的所有画面内容均为全零/旧数据。++### 修复内容(`src/xnu-cpu-metal-bundle/XnuCpuMetalPlugin.mm`)+- `XcmBuffer` 在 `newBufferWithBytesNoCopy:` 回退时始终保存 `_fallbackPtr` 与 `_fallbackLen`(无论是否传 `deallocator`),并跟踪 `_contentsAccessed` 避免覆盖通过 `.contents` 写入的数据。+- 新增 `-[XcmBuffer syncFromFallbackHostMemory]`:当 `!_contentsAccessed` 时将 `_fallbackPtr` 同步到 `xm2v_buffer_contents(_handle)`(`MTLStorageModeManaged` 时同步调 `didModifyRange:`)。+- 在 `-[XcmCommandBuffer commit]` 提交前遍历 `_retainedResources` 同步所有回退 `XcmBuffer`(含 `XcmTexture->_buffer`),并在 `newTextureWithDescriptor:offset:bytesPerRow:`、`newLinearTextureWithDescriptor:...` 与 `didModifyRange:` 中同步调用。+- 保留 `newTexture` / `newTextureIOSurface` / `newRenderPSO` / `newComputePSO` / `syncToIOSurface` 的限流失败 `os_log_error`;将 `newBufferWithBytesNoCopy` 回退提示降级为每进程仅记一次 `os_log_info`。++### 验证结果+- **托管 macOS `WindowServer` on Venus**:替换新 bundle 并重启 `WindowServer` 后,`vmmap` 确认仅加载 `libvulkan_virtio.dylib`(无 `libvulkan_lvp.dylib`),帧缓冲 `/var/tmp/xnu-fb.raw` 点亮 **`1,023,982 / 1,024,000`** 像素,完整渲染出 macOS 27 登录界面(金门大桥日落壁纸、`Sun 22 Nov`、`xnu` 头像、`Enter Password` 输入框、右上角 Wi-Fi 图标)。+- **宿主机全套回归**:`lavapipe (arm64e)` 与 `MoltenVK (arm64)` 的 `test_round17_direct_driver`、`test-bundle.sh`、`test-render-ci.sh`、`test_round10`、`test_carenderer`、`test_round13`、`test_round14_vfx` 全部 PASS。diff --git a/gpu/xnu-cpu-metal/src/xnu-cpu-metal-bundle/XnuCpuMetalPlugin.mm b/gpu/xnu-cpu-metal/src/xnu-cpu-metal-bundle/XnuCpuMetalPlugin.mmindex 478eb7e..4a3926c 100644--- a/gpu/xnu-cpu-metal/src/xnu-cpu-metal-bundle/XnuCpuMetalPlugin.mm+++ b/gpu/xnu-cpu-metal/src/xnu-cpu-metal-bundle/XnuCpuMetalPlugin.mm@@ -214,8 +214,11 @@ static NSData *xcmCodeGenRequest(uint32_t type, const std::vector<uint8_t> &requMTLPurgeableState _purgeable;void (^_fallbackDeallocator)(void *, NSUInteger);void *_fallbackPtr;+ NSUInteger _fallbackLen;+ BOOL _contentsAccessed;}- (instancetype)initWithHandle:(Xm2vBuffer *)h device:(id)dev options:(MTLResourceOptions)opts;+- (void)syncFromFallbackHostMemory;@end@implementation XcmBuffer@@ -237,11 +240,25 @@ static NSData *xcmCodeGenRequest(uint32_t type, const std::vector<uint8_t> &requd(_fallbackPtr, (NSUInteger)_length);}}+- (void)syncFromFallbackHostMemory {+ if (!_fallbackPtr || !_fallbackLen || !_handle || _contentsAccessed) return;+ void *dst = xm2v_buffer_contents(_handle);+ if (dst && dst != _fallbackPtr) {+ memcpy(dst, _fallbackPtr, _fallbackLen);+ if (self.storageMode == MTLStorageModeManaged) {+ xm2v_buffer_did_modify_range(_handle, 0, _fallbackLen);+ }+ }+}- (id<MTLDevice>)device { return _device; }-- (void *)contents { return _handle ? xm2v_buffer_contents(_handle) : nullptr; }+- (void *)contents {+ _contentsAccessed = YES;+ return _handle ? xm2v_buffer_contents(_handle) : nullptr;+}- (NSUInteger)length { return (NSUInteger)_length; }- (uint64_t)gpuAddress { return _handle ? xm2v_buffer_gpu_address(_handle) : 0; }- (void)didModifyRange:(NSRange)range {+ [self syncFromFallbackHostMemory];if (_handle) xm2v_buffer_did_modify_range(_handle, range.location, range.length);}- (MTLResourceOptions)resourceOptions { return _options; }@@ -435,6 +452,17 @@ static bool xcmIOSurfacePlaneLayout(IOSurfaceRef surf, NSUInteger plane, NSUIntesize_t bpr = 0;if (xcmIOSurfacePlaneLayout(_iosurface, _iosurfacePlane, _width, _height, _pixelFormat, &base, &bpr)) {bool ok = xm2v_texture_get_bytes(_handle, base, bpr, 0, 0, 0, 0, _width, _height, 1, 0, 0);+ if (!ok) {+ static int sSyncFailLogs = 0;+ if (sSyncFailLogs < 10) {+ sSyncFailLogs++;+ os_log_error(OS_LOG_DEFAULT,+ "XnuCpuMetal: syncToIOSurface failed (sid=0x%x surfFmt=0x%x fmt=%lu %lux%lu bpr=%zu): %{public}s",+ IOSurfaceGetID(_iosurface), IOSurfaceGetPixelFormat(_iosurface),+ (unsigned long)_pixelFormat, (unsigned long)_width, (unsigned long)_height,+ bpr, xm2v_last_error() ?: "?");+ }+ }if (xcmTraceOn() || !ok) {const uint8_t *p = (const uint8_t *)base;size_t nz = 0;@@ -590,6 +618,7 @@ static void xcmFillTextureDesc(MTLTextureDescriptor *desc, Xm2vTextureDesc &td)@implementation XcmBuffer (LinearTextures)- (id<MTLTexture>)newTextureWithDescriptor:(MTLTextureDescriptor *)desc offset:(NSUInteger)offset bytesPerRow:(NSUInteger)bytesPerRow {if (!_handle || !desc) return nil;+ [self syncFromFallbackHostMemory];Xm2vTextureDesc td;xcmFillTextureDesc(desc, td);Xm2vTexture *t = xm2v_buffer_new_texture(_handle, &td, offset, bytesPerRow);@@ -610,6 +639,7 @@ static void xcmFillTextureDesc(MTLTextureDescriptor *desc, Xm2vTextureDesc &td)}- (id<MTLTexture>)newLinearTextureWithDescriptor:(MTLTextureDescriptor *)desc offset:(NSUInteger)offset bytesPerRow:(NSUInteger)bytesPerRow bytesPerImage:(NSUInteger)bytesPerImage {if (!_handle || !desc) return nil;+ [self syncFromFallbackHostMemory];Xm2vTextureDesc td;xcmFillTextureDesc(desc, td);Xm2vTexture *t = xm2v_buffer_new_linear_texture(_handle, &td, offset, bytesPerRow, bytesPerImage);@@ -1992,6 +2022,7 @@ static void xcmSharedEventNotified(void *context, uint64_t value) {- (void)commit {BOOL needsWait = NO;NSUInteger nSched = 0, nComp = 0, nDirty = 0, nRet = 0;+ NSArray *retSnapshot = nil;@synchronized(self) {if (_committed) return;_committed = YES;@@ -1999,8 +2030,17 @@ static void xcmSharedEventNotified(void *context, uint64_t value) {nComp = _completedHandlers.count;nDirty = _dirtyIOSurfaceTextures.count;nRet = _retainedResources.count;+ if (nRet) retSnapshot = [_retainedResources copy];needsWait = (nSched || nComp || nDirty || nRet);}+ for (id res in retSnapshot) {+ if ([res isKindOfClass:[XcmBuffer class]]) {+ [(XcmBuffer *)res syncFromFallbackHostMemory];+ } else if ([res isKindOfClass:[XcmTexture class]]) {+ XcmBuffer *b = ((XcmTexture *)res)->_buffer;+ if (b) [b syncFromFallbackHostMemory];+ }+ }if (xcmTraceOn()) fprintf(stderr, "XCM commit cb=%p sched=%lu comp=%lu dirty=%lu ret=%lu\n",(__bridge void *)self, (unsigned long)nSched,(unsigned long)nComp, (unsigned long)nDirty, (unsigned long)nRet);@@ -2294,14 +2334,18 @@ static id xcmDev_newBufferNoCopy(id self, SEL, void *pointer, NSUInteger length,ctx ? xcmBufferDeallocCallback : nullptr, ctx);if (!b) {if (ctx) (void)(__bridge_transfer id)ctx;- os_log_error(OS_LOG_DEFAULT, "XnuCpuMetal: newBufferWithBytesNoCopy failed (ptr=%p len=%lu opts=0x%lx), falling back to copy: %{public}s",- pointer, (unsigned long)length, (unsigned long)opts, xm2v_last_error() ?: "?");+ static dispatch_once_t sNoCopyLogOnce;+ dispatch_once(&sNoCopyLogOnce, ^{+ os_log_info(OS_LOG_DEFAULT, "XnuCpuMetal: newBufferWithBytesNoCopy unsupported (%{public}s), using host-memory copy+sync fallback",+ xm2v_last_error() ?: "?");+ });b = xm2v_buffer_new(gXm2vDevice, length, opts, pointer);if (!b) return nil;XcmBuffer *buf = [[XcmBuffer alloc] initWithHandle:b device:self options:opts];- if (buf && deallocator) {- buf->_fallbackDeallocator = [deallocator copy];+ if (buf) {buf->_fallbackPtr = pointer;+ buf->_fallbackLen = length;+ if (deallocator) buf->_fallbackDeallocator = [deallocator copy];}return buf;}@@ -2321,12 +2365,28 @@ static id xcmDev_newTexture(id self, SEL, MTLTextureDescriptor *desc) {td.resource_options = (uint64_t)desc.resourceOptions;td.usage = (uint64_t)desc.usage;Xm2vTexture *t = xm2v_texture_new(gXm2vDevice, &td);- if (!t || xcmTraceOn()) {+ if (!t) {+ const char *err = xm2v_last_error() ?: "?";+ static int sNewTexFailLogs = 0;+ if (sNewTexFailLogs < 20) {+ sNewTexFailLogs++;+ os_log_error(OS_LOG_DEFAULT,+ "XnuCpuMetal: newTexture failed (fmt=%lu type=%lu %lux%lux%lu mip=%lu samp=%lu arr=%lu usage=0x%lx opts=0x%lx): %{public}s",+ (unsigned long)desc.pixelFormat, (unsigned long)desc.textureType,+ (unsigned long)desc.width, (unsigned long)desc.height, (unsigned long)desc.depth,+ (unsigned long)desc.mipmapLevelCount, (unsigned long)desc.sampleCount,+ (unsigned long)desc.arrayLength, (unsigned long)desc.usage,+ (unsigned long)desc.resourceOptions, err);+ }fprintf(stderr, "XCM newTexture -> %p fmt=%lu type=%lu %lux%lux%lu mip=%lu usage=0x%lx opts=0x%lx err=%s\n",(void *)t, (unsigned long)desc.pixelFormat, (unsigned long)desc.textureType,(unsigned long)desc.width, (unsigned long)desc.height, (unsigned long)desc.depth,- (unsigned long)desc.mipmapLevelCount, (unsigned long)desc.usage, (unsigned long)desc.resourceOptions,- t ? "ok" : xm2v_last_error());+ (unsigned long)desc.mipmapLevelCount, (unsigned long)desc.usage, (unsigned long)desc.resourceOptions, err);+ } else if (xcmTraceOn()) {+ fprintf(stderr, "XCM newTexture -> %p fmt=%lu type=%lu %lux%lux%lu mip=%lu usage=0x%lx opts=0x%lx err=ok\n",+ (void *)t, (unsigned long)desc.pixelFormat, (unsigned long)desc.textureType,+ (unsigned long)desc.width, (unsigned long)desc.height, (unsigned long)desc.depth,+ (unsigned long)desc.mipmapLevelCount, (unsigned long)desc.usage, (unsigned long)desc.resourceOptions);}return t ? [[XcmTexture alloc] initWithHandle:t device:self descriptor:desc] : nil;}@@ -2337,6 +2397,19 @@ static id xcmDev_newTextureIOSurface(id self, SEL, MTLTextureDescriptor *desc, IMTLTextureDescriptor *copy = [desc copy];copy.storageMode = MTLStorageModeShared;XcmTexture *tex = xcmDev_newTexture(self, nullptr, copy);+ if (!tex) {+ static int sIosFailLogs = 0;+ if (sIosFailLogs < 20) {+ sIosFailLogs++;+ uint32_t sid = surface ? IOSurfaceGetID(surface) : 0;+ uint32_t pf = surface ? IOSurfaceGetPixelFormat(surface) : 0;+ os_log_error(OS_LOG_DEFAULT,+ "XnuCpuMetal: newTextureIOSurface failed (sid=0x%x surfFmt=0x%x plane=%lu descFmt=%lu %lux%lu usage=0x%lx origOpts=0x%lx)",+ sid, pf, (unsigned long)plane, (unsigned long)desc.pixelFormat,+ (unsigned long)desc.width, (unsigned long)desc.height,+ (unsigned long)desc.usage, (unsigned long)desc.resourceOptions);+ }+ }if (xcmTraceOn()) fprintf(stderr, "XCM newTextureIOSurface -> %p surf=%p plane=%lu\n", (__bridge void *)tex, surface, (unsigned long)plane);if (tex && surface) {CFRetain(surface);@@ -2562,6 +2635,14 @@ static id xcmDev_newComputePSO(id self, SEL, id<MTLFunction> fn, NSError **errorreturn nil;}Xm2vComputePipeline *pso = xm2v_compute_pipeline_new(gXm2vDevice, ((XcmFunction *)fn)->_handle);+ if (!pso) {+ static int sCpsoFailLogs = 0;+ if (sCpsoFailLogs < 20) {+ sCpsoFailLogs++;+ os_log_error(OS_LOG_DEFAULT, "XnuCpuMetal: newComputePSO failed (fn=%{public}s): %{public}s",+ fn.name.UTF8String ?: "?", xm2v_last_error() ?: "?");+ }+ }if (xcmTraceOn() || !pso) {fprintf(stderr, "XCM newComputePSO(%s) -> %p err=%s\n",fn.name.UTF8String ?: "?", (void *)pso, pso ? "ok" : xm2v_last_error());@@ -2606,6 +2687,14 @@ static id xcmDev_newComputePSODesc(id self, SEL, MTLComputePipelineDescriptor *dgXm2vDevice, ((XcmFunction *)desc.computeFunction)->_handle,pubLinked.empty() ? nullptr : pubLinked.data(), (uint32_t)pubLinked.size(),privLinked.empty() ? nullptr : privLinked.data(), (uint32_t)privLinked.size());+ if (!pso) {+ static int sCpsoDescFailLogs = 0;+ if (sCpsoDescFailLogs < 20) {+ sCpsoDescFailLogs++;+ os_log_error(OS_LOG_DEFAULT, "XnuCpuMetal: newComputePSODesc failed (fn=%{public}s): %{public}s",+ desc.computeFunction.name.UTF8String ?: "?", xm2v_last_error() ?: "?");+ }+ }if (xcmTraceOn() || !pso) {fprintf(stderr, "XCM newComputePSODesc(%s, linked=%zu+%zu) -> %p err=%s\n",desc.computeFunction.name.UTF8String ?: "?", pubLinked.size(), privLinked.size(),@@ -2770,6 +2859,18 @@ static id xcmDev_newRenderPSO(id self, SEL, MTLRenderPipelineDescriptor *desc, N}break;}+ if (!pso) {+ static int sPsoFailLogs = 0;+ if (sPsoFailLogs < 20) {+ sPsoFailLogs++;+ os_log_error(OS_LOG_DEFAULT,+ "XnuCpuMetal: newRenderPSO failed (v=%{public}s f=%{public}s fmt0=%llu): %{public}s",+ desc.vertexFunction.name.UTF8String ?: "?",+ desc.fragmentFunction.name.UTF8String ?: "-",+ (unsigned long long)rd.color_attachments[0].pixel_format,+ xm2v_last_error() ?: "?");+ }+ }if (xcmTraceOn() || !pso) {const auto &c0 = rd.color_attachments[0];fprintf(stderr, "XCM newRenderPSO(v=%s,f=%s, fmt0=%llu blend=%d[op=%llu/%llu rgb=%llu,%llu a=%llu,%llu] mask=0x%llx, vl=%zu+%zu, fl=%zu+%zu) -> %p err=%s\n",