diff --git a/runtime/shims/d3d12_xs_device.cpp b/runtime/shims/d3d12_xs_device.cpp index 90dcc61136..06b8501393 100644 --- a/runtime/shims/d3d12_xs_device.cpp +++ b/runtime/shims/d3d12_xs_device.cpp @@ -2083,6 +2083,12 @@ HRESULT queue_copy_page_mappings(ChildProxy* self, UINT count, const xmem::PageB if (!map && rt::xmem::copy_page_mappings_fn) map = ©_page_mappings_default; if (!map || !count) return S_OK; if (!batches || flags) return E_INVALIDARG; + // Which implementation remaps, once: a boot's read-fault reports belong to that route's tests. + static std::atomic route_logged{false}; + if (!route_logged.exchange(true, std::memory_order_relaxed)) + std::fprintf(stderr, "[rt] d3d12_xs: CopyPageMappingsBatchX remaps through %s\n", + map == ©_page_mappings_default ? "xmem's range callback (copy_page_mappings_fn)" + : "the installed batch hook (set_copy_page_mappings_hook)"); auto* queue = reinterpret_cast(self->real); if (list_values::enabled() && !self->values) self->values = list_values::QueueValues::create(self->device->real, queue); diff --git a/runtime/shims/xmem.cpp b/runtime/shims/xmem.cpp index 40bc28f2a9..330eeddbb2 100644 --- a/runtime/shims/xmem.cpp +++ b/runtime/shims/xmem.cpp @@ -1950,6 +1950,7 @@ BOOL WINAPI XMemMapPhysicalPagesScatter(void* const* addresses, uint32_t page_co return scatter_refused(ScatterFailure::unaligned, address, ERROR_INVALID_PARAMETER); } const uint64_t page = page_array ? page_array[i] : 0; + const UnmapCause cause(page ? "XMemMapPhysicalPagesScatter (remap)" : "XMemMapPhysicalPagesScatter (unmap)"); if (page == 0) { if (!unmap_page(address)) return scatter_refused(ScatterFailure::unmap, address, GetLastError()); continue; @@ -1972,6 +1973,34 @@ BOOL WINAPI XMemMapPhysicalPagesScatter(void* const* addresses, uint32_t page_co // calls with the same page make them. Every source page is looked up before any destination page changes, so a source // range that overlaps the destination maps what was there before the call. A destination in a GPU-only range is mapped // at its paired CPU range too (title_gpu_views), and a source page missing at a GPU-only address is looked up there. +// A page of that CPU range the title mapped itself is the title's (title_mapped_twin): the copy leaves it as it is. + +// With g_phys_mutex held. True when the paired CPU page at `address` holds a mapping of the title's own: its +// XMemMapPhysicalPages or XMemMapPhysicalPagesScatter call, or a slot-31 copy onto that address. The console's +// CopyPageMappingsBatchX changes only the GPU-only range's page table, so such a page keeps what the title put +// there; only a page the copy mirrored there itself (MapOwner::mirror), a zero plant, or a page with no mapping +// follows the GPU-only range. Each page kept is counted, and the first ones logged. +bool title_mapped_twin(uintptr_t address, uintptr_t gpu, bool unmap) { + MapOwner owner; + { + const FaultLockShared faults; + uintptr_t base = 0; + const View* const view = covering_view(address, base); + if (!view || view->zero || view->owner == MapOwner::mirror) return false; + owner = view->owner; + } + static std::atomic kept{0}; + const uint64_t n = kept.fetch_add(1, std::memory_order_relaxed) + 1; + if (n <= 8 || (n & (n - 1)) == 0) + std::fprintf(stderr, + "[rt] xmem: slot-31 %s at 0x%llx left the title's %s mapping at its paired CPU page 0x%llx " + "(%llu such pages so far)\n", + unmap ? "unmap" : "copy", static_cast(gpu), + owner == MapOwner::scatter ? "scatter" : owner == MapOwner::slot31 ? "slot-31" : "physical", + static_cast(address), static_cast(n)); + return true; +} + bool copy_page_mappings(void* destination, const void* source, uint64_t pages, bool repeat_first) { const uintptr_t to = reinterpret_cast(destination); const uintptr_t from = reinterpret_cast(source); @@ -1992,8 +2021,14 @@ bool copy_page_mappings(void* destination, const void* source, uint64_t pages, b const Window* const cpu_window = cpu_to != to ? find_window(cpu_to, pages * kPhysicalPageSize) : nullptr; if (!source) { for (uint64_t i = 0; i < pages; ++i) { - if (!unmap_page(to + i * kPhysicalPageSize)) return false; - if (cpu_window && !unmap_page(cpu_to + i * kPhysicalPageSize)) return false; + const uintptr_t gpu = to + i * kPhysicalPageSize, cpu = cpu_to + i * kPhysicalPageSize; + { + const UnmapCause cause("CopyPageMappingsBatchX (unmap)"); + if (!unmap_page(gpu)) return false; + } + if (!cpu_window || title_mapped_twin(cpu, gpu, true)) continue; + const UnmapCause cause("CopyPageMappingsBatchX (unmap paired CPU page)"); + if (!unmap_page(cpu)) return false; } return true; } @@ -2016,8 +2051,11 @@ bool copy_page_mappings(void* destination, const void* source, uint64_t pages, b const DWORD cpu_protect = cpu_window ? view_protect(*cpu_window) : 0; for (uint64_t i = 0; i < pages; ++i) { const uint64_t page = found[static_cast(i)]; - if (!map_page(to + i * kPhysicalPageSize, page, protect, MapOwner::slot31)) return false; - if (cpu_window && !map_page(cpu_to + i * kPhysicalPageSize, page, cpu_protect, MapOwner::mirror)) return false; + const uintptr_t gpu = to + i * kPhysicalPageSize, cpu = cpu_to + i * kPhysicalPageSize; + const UnmapCause cause(page ? "CopyPageMappingsBatchX (remap)" : "CopyPageMappingsBatchX (hole)"); + if (!map_page(gpu, page, protect, MapOwner::slot31)) return false; + if (cpu_window && !title_mapped_twin(cpu, gpu, false) && !map_page(cpu, page, cpu_protect, MapOwner::mirror)) + return false; } return true; } diff --git a/runtime/test/d3d12_xmem_placed_test.cpp b/runtime/test/d3d12_xmem_placed_test.cpp index 7c64a23cf6..33b5d462ad 100644 --- a/runtime/test/d3d12_xmem_placed_test.cpp +++ b/runtime/test/d3d12_xmem_placed_test.cpp @@ -174,6 +174,82 @@ int queue_remap(ID3D12Device* real, ID3D12Device* proxy) { return 0; } +// Queue slot 31 as a retail run takes it: no batch hook, so the queue's default adapter sends each range to xmem's +// range callback (copy_page_mappings_fn), which mirrors a legacy-paired GPU-only destination at its CPU view. The +// source is a GPU-only pool backed only through its CPU alias, and the title maps a page of its own at one CPU twin +// while the remap waits behind queue work (deferred with list values, waited for without them). When the remap is +// applied, the GPU-only pages change and the title's CPU page keeps its bytes, through the copy and the unmap after it. +int queue_remap_default_route(ID3D12Device* real, ID3D12Device* proxy) { + using rt::xmem::PageBatch; + using rt::xmem::PageRange; + auto reserve = reinterpret_cast(rt::shims::find("xmem.dll", "XMemVirtualAlloc")); + auto allocate = reinterpret_cast(rt::shims::find("xmem.dll", "XMemAllocatePhysicalPages")); + auto map = reinterpret_cast(rt::shims::find("xmem.dll", "XMemMapPhysicalPages")); + auto free_pages = reinterpret_cast(rt::shims::find("xmem.dll", "XMemFreePhysicalPages")); + constexpr unsigned n = 2; + auto window = [&](DWORD protect) { + return static_cast(reserve(nullptr, n * kPage64k, MEM_RESERVE | kXmem64kPages, 0, protect)); + }; + // Two pairs, each a CPU range and then the GPU-only range of its size: the pool's and the destination's. + auto* pool_cpu = window(PAGE_READWRITE); + auto* pool = window(0x801); + auto* cpu = window(PAGE_READWRITE); + auto* dst = window(0x801); + EXPECT(pool_cpu && pool && cpu && dst); + EXPECT(leonida::title_gpu_views::to_cpu(uintptr_t(pool)) == uintptr_t(pool_cpu)); + EXPECT(leonida::title_gpu_views::to_cpu(uintptr_t(dst)) == uintptr_t(cpu)); + std::uint64_t pages[n + 1]{}, count = n + 1; + EXPECT(allocate(0x280, &count, pages) && count == n + 1); + EXPECT(map(pool_cpu, n, pages)); // the pool's pages are mapped at its CPU alias only + for (unsigned i = 0; i < n; ++i) pool_cpu[i * kPage64k] = static_cast(40 + i); + + D3D12_COMMAND_QUEUE_DESC desc{D3D12_COMMAND_LIST_TYPE_COPY}; + Ref queue; + Ref gate; + EXPECT(SUCCEEDED(proxy->CreateCommandQueue(&desc, IID_PPV_ARGS(&queue.p)))); + EXPECT(SUCCEEDED(real->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&gate.p)))); + auto* real_queue = rt::d3d12_xs::queue_proxy_real_queue(queue.p); + EXPECT(real_queue && SUCCEEDED(real_queue->Wait(gate.p, 1))); + rt::d3d12_xs::set_copy_page_mappings_hook(nullptr); // the retail route: the default adapter + using Copy = HRESULT(WINAPI*)(void*, UINT, const PageBatch*, std::uintptr_t, UINT, UINT); + auto call = reinterpret_cast((*reinterpret_cast(queue.p))[31]); + const PageRange copy_run[] = {{0, 0, n}}; + const PageBatch copy_batch{uintptr_t(dst), 1, copy_run}; + auto done = std::async(std::launch::async, [&] { return call(queue.p, 1, ©_batch, uintptr_t(pool), n, 0); }); + // While the remap waits behind the held work, the title maps a page of its own at the second CPU twin. + EXPECT(map(cpu + kPage64k, 1, &pages[n])); + cpu[kPage64k] = 77; + EXPECT(SUCCEEDED(gate->Signal(1))); + EXPECT(SUCCEEDED(done.get())); + EXPECT(SUCCEEDED(real_queue->Signal(gate.p, 2))); + HANDLE remapped = CreateEventW(nullptr, FALSE, FALSE, nullptr); + EXPECT(remapped && SUCCEEDED(gate->SetEventOnCompletion(2, remapped))); + const auto completion = WaitForSingleObject(remapped, 10000); + CloseHandle(remapped); + EXPECT(completion == WAIT_OBJECT_0); + EXPECT(dst[0] == 40 && dst[kPage64k] == 41); // the GPU-only pages, found through the pool's CPU alias + EXPECT(cpu[0] == 40); // a CPU twin with no mapping of the title's follows the copy + EXPECT(cpu[kPage64k] == 77); // the title's own page is kept + + // The unmap: the GPU-only pages and the mirrored twin are unmapped, the title's page stays. + const PageRange hole[] = {{2, 0, n}}; + const PageBatch unmap_batch{uintptr_t(dst), 1, hole}; + EXPECT(SUCCEEDED(call(queue.p, 1, &unmap_batch, uintptr_t(pool), n, 0))); + EXPECT(SUCCEEDED(real_queue->Signal(gate.p, 3))); + remapped = CreateEventW(nullptr, FALSE, FALSE, nullptr); + EXPECT(remapped && SUCCEEDED(gate->SetEventOnCompletion(3, remapped))); + EXPECT(WaitForSingleObject(remapped, 10000) == WAIT_OBJECT_0); + CloseHandle(remapped); + EXPECT(state_is(dst, MEM_RESERVE) && state_is(dst + kPage64k, MEM_RESERVE) && state_is(cpu, MEM_RESERVE)); + EXPECT(cpu[kPage64k] == 77); + + EXPECT(map(cpu + kPage64k, 1, nullptr) && map(pool_cpu, n, nullptr)); + EXPECT(free_pages(n + 1, pages)); + std::puts("queue slot 31 default route: a source through its CPU alias, a deferred copy and an unmap keep the " + "title's own CPU twin page"); + return 0; +} + // What a GPU copy of `buffer` reads now (a submission: the shadows are refreshed first). int read_back(ID3D12Device* real, ID3D12Resource* buffer, UINT64 size, std::vector& out) { D3D12_RESOURCE_DESC desc = buffer->GetDesc(); @@ -547,6 +623,7 @@ int main(int argc, char** argv) { failures += run(real.p, device.p, eager); failures += exposure_map_ranges(device.p); failures += queue_remap(real.p, device.p); + failures += queue_remap_default_route(real.p, device.p); failures += paired_views(real.p); } } diff --git a/runtime/test/xmem_test.cpp b/runtime/test/xmem_test.cpp index 991388c7f5..58143ae49c 100644 --- a/runtime/test/xmem_test.cpp +++ b/runtime/test/xmem_test.cpp @@ -741,7 +741,19 @@ void test_copy_page_mappings_cpu_view(const PhysicalApi& api) { check(readable(other) && page_holds(other, 0x7300u), "a GPU-only source page unmapped there is looked up at its CPU range"); - // A null source unmaps both ranges. + // A CPU page the title mapped itself is the title's: a copy onto its GPU-only page, or an unmap of it, changes the + // GPU-only page alone (the console's CopyPageMappingsBatchX edits only that range's page table). + check(copy(gpu + 2 * kPage64k, pool + kPage64k, 1, false), "a copy onto a GPU-only page whose CPU page is the title's"); + check(readable(gpu + 2 * kPage64k) && page_holds(gpu + 2 * kPage64k, 0x7301u), + "the GPU-only page shows the copied pool page"); + check(readable(cpu + 2 * kPage64k) && page_holds(cpu + 2 * kPage64k, 0x7300u), + "the title's own mapping at the paired CPU page is kept, not replaced by the copy"); + check(copy(gpu + 2 * kPage64k, nullptr, 1, false), "a null source unmaps a GPU-only page whose CPU page is the title's"); + check(!readable(gpu + 2 * kPage64k), "the GPU-only page is unmapped"); + check(readable(cpu + 2 * kPage64k) && page_holds(cpu + 2 * kPage64k, 0x7300u), + "the title's own mapping at the paired CPU page survives the unmap"); + + // A null source unmaps both ranges where the CPU pages are the copy's own. check(copy(gpu, nullptr, kPool, false), "a null source unmaps the GPU-only range"); check(!readable(gpu) && !readable(cpu) && !readable(cpu + kPage64k), "the paired CPU range is unmapped with it");