From 5108a02e3a85fd1102219516b54a996c975675c4 Mon Sep 17 00:00:00 2001 From: lixonik <71736299+lixonik@users.noreply.github.com> Date: Wed, 25 Nov 2026 16:49:09 +0300 Subject: [PATCH] Resolve batch page-copy sources through their CPU aliases --- runtime/shims/xmem.cpp | 15 +++++++++------ runtime/test/xmem_test.cpp | 8 ++++++++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/runtime/shims/xmem.cpp b/runtime/shims/xmem.cpp index ff2f9b4094..dedf3c4162 100644 --- a/runtime/shims/xmem.cpp +++ b/runtime/shims/xmem.cpp @@ -2023,6 +2023,13 @@ bool title_mapped_twin(uintptr_t address, uintptr_t gpu, bool unmap) { return true; } +auto find_mapped_source_page(uintptr_t source) { + const auto mapped = g_mapped.find(source); + if (mapped != g_mapped.end()) return mapped; + const uintptr_t cpu_source = leonida::title_gpu_views::to_cpu(source, kPhysicalPageSize); + return cpu_source == source ? mapped : g_mapped.find(cpu_source); +} + 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); @@ -2062,11 +2069,7 @@ bool copy_page_mappings(void* destination, const void* source, uint64_t pages, b std::vector found(static_cast(pages), 0); for (uint64_t i = 0; i < pages; ++i) { const uintptr_t src_va = from + (repeat_first ? 0 : i) * kPhysicalPageSize; - auto it = g_mapped.find(src_va); - if (it == g_mapped.end()) { // a GPU-only source whose pages were mapped at its CPU view - const uintptr_t cpu_src = leonida::title_gpu_views::to_cpu(src_va, kPhysicalPageSize); - if (cpu_src != src_va) it = g_mapped.find(cpu_src); - } + const auto it = find_mapped_source_page(src_va); if (it != g_mapped.end()) found[static_cast(i)] = it->second; } const DWORD protect = view_protect(*window); @@ -2479,7 +2482,7 @@ bool copy_page_mappings(std::uint32_t count, const PageBatch* batches, std::uint const auto source = pool + uint64_t(r.start) * kPhysicalPageSize; for (uint32_t j = 0; j < n; ++j) { const auto src = source + uint64_t(j) * kPhysicalPageSize; - const auto it = g_mapped.find(src); + const auto it = find_mapped_source_page(src); if (it == g_mapped.end()) { // A hole in a valid source window unmaps the destination (CLHelper, RT-QUEUE31). if (!find_window(src, kPhysicalPageSize)) diff --git a/runtime/test/xmem_test.cpp b/runtime/test/xmem_test.cpp index 79f6554128..716354b443 100644 --- a/runtime/test/xmem_test.cpp +++ b/runtime/test/xmem_test.cpp @@ -741,6 +741,14 @@ 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"); + api.map(other, 1, nullptr); + const rt::xmem::PageRange cpu_source{0, 2, 1}; + const rt::xmem::PageBatch batch{reinterpret_cast(other), 1, &cpu_source}; + check(rt::xmem::copy_page_mappings(1, &batch, reinterpret_cast(gpu), kPair, 0), + "a batch copies from a GPU-only range whose source page is mapped at its CPU view"); + check(readable(other) && page_holds(other, 0x7300u), + "a batch source at the paired CPU view leaves its destination mapped with the source bytes"); + // 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");