diff options
| author | kurrutako <kurrutako@users.noreply.github.com> | 2026-11-25 18:02:47 +0100 |
|---|---|---|
| committer | kurrutako <kurrutako@users.noreply.github.com> | 2026-11-25 18:02:47 +0100 |
| commit | ca00cc0e0338d27ff018f94d9a7e6e3da58abe7d (patch) | |
| tree | 9d2d0b2b0e75d9e165826efc89df805ed9e4cb87 /runtime/shims/xmem.cpp | |
| parent | 9e7720c9e764211e0021aa23907d2d3ecd58963f (diff) | |
xmem: a slot-31 copy leaves the title's own mappings at the paired CPU range
copy_page_mappings (queue slot 31, CopyPageMappingsBatchX) mirrors every page
it maps or unmaps at a GPU-only destination onto the paired CPU range
(title_gpu_views::to_cpu). It did so even where the title had mapped that CPU
page itself with XMemMapPhysicalPages or XMemMapPhysicalPagesScatter: a copy
replaced the title's page, and an unmap (kind 2) left the title's CPU page
reserved. The title's next read of it faulted, and the run ended.
The console's CopyPageMappingsBatchX edits only the GPU-only range's page
table, so a CPU page holding a mapping of the title's (physical, scatter or a
slot-31 copy onto that address) is now left as it is. Pages the mirror made
itself, zero plants and pages with no mapping still follow the GPU-only range,
so the D3D12 layer's reads at the CPU view keep working. Each page kept is
counted and the first ones are logged.
The unmaps these paths make are now labelled in the read-fault report
(CopyPageMappingsBatchX unmap / paired CPU page / remap / hole,
XMemMapPhysicalPagesScatter unmap / remap) instead of the default "unmap".
xmem_test: test_copy_page_mappings_cpu_view now checks that a copy onto, and
an unmap of, a GPU-only page whose paired CPU page the title mapped keep the
title's bytes at the CPU page. Both checks fail on the old code.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/shims/xmem.cpp')
| -rw-r--r-- | runtime/shims/xmem.cpp | 46 |
1 files changed, 42 insertions, 4 deletions
diff --git a/runtime/shims/xmem.cpp b/runtime/shims/xmem.cpp index 40bc28f2a..330eeddbb 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 | |||
| 1950 | return scatter_refused(ScatterFailure::unaligned, address, ERROR_INVALID_PARAMETER); | 1950 | return scatter_refused(ScatterFailure::unaligned, address, ERROR_INVALID_PARAMETER); |
| 1951 | } | 1951 | } |
| 1952 | const uint64_t page = page_array ? page_array[i] : 0; | 1952 | const uint64_t page = page_array ? page_array[i] : 0; |
| 1953 | const UnmapCause cause(page ? "XMemMapPhysicalPagesScatter (remap)" : "XMemMapPhysicalPagesScatter (unmap)"); | ||
| 1953 | if (page == 0) { | 1954 | if (page == 0) { |
| 1954 | if (!unmap_page(address)) return scatter_refused(ScatterFailure::unmap, address, GetLastError()); | 1955 | if (!unmap_page(address)) return scatter_refused(ScatterFailure::unmap, address, GetLastError()); |
| 1955 | continue; | 1956 | continue; |
| @@ -1972,6 +1973,34 @@ BOOL WINAPI XMemMapPhysicalPagesScatter(void* const* addresses, uint32_t page_co | |||
| 1972 | // calls with the same page make them. Every source page is looked up before any destination page changes, so a source | 1973 | // calls with the same page make them. Every source page is looked up before any destination page changes, so a source |
| 1973 | // range that overlaps the destination maps what was there before the call. A destination in a GPU-only range is mapped | 1974 | // range that overlaps the destination maps what was there before the call. A destination in a GPU-only range is mapped |
| 1974 | // at its paired CPU range too (title_gpu_views), and a source page missing at a GPU-only address is looked up there. | 1975 | // at its paired CPU range too (title_gpu_views), and a source page missing at a GPU-only address is looked up there. |
| 1976 | // A page of that CPU range the title mapped itself is the title's (title_mapped_twin): the copy leaves it as it is. | ||
| 1977 | |||
| 1978 | // With g_phys_mutex held. True when the paired CPU page at `address` holds a mapping of the title's own: its | ||
| 1979 | // XMemMapPhysicalPages or XMemMapPhysicalPagesScatter call, or a slot-31 copy onto that address. The console's | ||
| 1980 | // CopyPageMappingsBatchX changes only the GPU-only range's page table, so such a page keeps what the title put | ||
| 1981 | // there; only a page the copy mirrored there itself (MapOwner::mirror), a zero plant, or a page with no mapping | ||
| 1982 | // follows the GPU-only range. Each page kept is counted, and the first ones logged. | ||
| 1983 | bool title_mapped_twin(uintptr_t address, uintptr_t gpu, bool unmap) { | ||
| 1984 | MapOwner owner; | ||
| 1985 | { | ||
| 1986 | const FaultLockShared faults; | ||
| 1987 | uintptr_t base = 0; | ||
| 1988 | const View* const view = covering_view(address, base); | ||
| 1989 | if (!view || view->zero || view->owner == MapOwner::mirror) return false; | ||
| 1990 | owner = view->owner; | ||
| 1991 | } | ||
| 1992 | static std::atomic<uint64_t> kept{0}; | ||
| 1993 | const uint64_t n = kept.fetch_add(1, std::memory_order_relaxed) + 1; | ||
| 1994 | if (n <= 8 || (n & (n - 1)) == 0) | ||
| 1995 | std::fprintf(stderr, | ||
| 1996 | "[rt] xmem: slot-31 %s at 0x%llx left the title's %s mapping at its paired CPU page 0x%llx " | ||
| 1997 | "(%llu such pages so far)\n", | ||
| 1998 | unmap ? "unmap" : "copy", static_cast<unsigned long long>(gpu), | ||
| 1999 | owner == MapOwner::scatter ? "scatter" : owner == MapOwner::slot31 ? "slot-31" : "physical", | ||
| 2000 | static_cast<unsigned long long>(address), static_cast<unsigned long long>(n)); | ||
| 2001 | return true; | ||
| 2002 | } | ||
| 2003 | |||
| 1975 | bool copy_page_mappings(void* destination, const void* source, uint64_t pages, bool repeat_first) { | 2004 | bool copy_page_mappings(void* destination, const void* source, uint64_t pages, bool repeat_first) { |
| 1976 | const uintptr_t to = reinterpret_cast<uintptr_t>(destination); | 2005 | const uintptr_t to = reinterpret_cast<uintptr_t>(destination); |
| 1977 | const uintptr_t from = reinterpret_cast<uintptr_t>(source); | 2006 | const uintptr_t from = reinterpret_cast<uintptr_t>(source); |
| @@ -1992,8 +2021,14 @@ bool copy_page_mappings(void* destination, const void* source, uint64_t pages, b | |||
| 1992 | const Window* const cpu_window = cpu_to != to ? find_window(cpu_to, pages * kPhysicalPageSize) : nullptr; | 2021 | const Window* const cpu_window = cpu_to != to ? find_window(cpu_to, pages * kPhysicalPageSize) : nullptr; |
| 1993 | if (!source) { | 2022 | if (!source) { |
| 1994 | for (uint64_t i = 0; i < pages; ++i) { | 2023 | for (uint64_t i = 0; i < pages; ++i) { |
| 1995 | if (!unmap_page(to + i * kPhysicalPageSize)) return false; | 2024 | const uintptr_t gpu = to + i * kPhysicalPageSize, cpu = cpu_to + i * kPhysicalPageSize; |
| 1996 | if (cpu_window && !unmap_page(cpu_to + i * kPhysicalPageSize)) return false; | 2025 | { |
| 2026 | const UnmapCause cause("CopyPageMappingsBatchX (unmap)"); | ||
| 2027 | if (!unmap_page(gpu)) return false; | ||
| 2028 | } | ||
| 2029 | if (!cpu_window || title_mapped_twin(cpu, gpu, true)) continue; | ||
| 2030 | const UnmapCause cause("CopyPageMappingsBatchX (unmap paired CPU page)"); | ||
| 2031 | if (!unmap_page(cpu)) return false; | ||
| 1997 | } | 2032 | } |
| 1998 | return true; | 2033 | return true; |
| 1999 | } | 2034 | } |
| @@ -2016,8 +2051,11 @@ bool copy_page_mappings(void* destination, const void* source, uint64_t pages, b | |||
| 2016 | const DWORD cpu_protect = cpu_window ? view_protect(*cpu_window) : 0; | 2051 | const DWORD cpu_protect = cpu_window ? view_protect(*cpu_window) : 0; |
| 2017 | for (uint64_t i = 0; i < pages; ++i) { | 2052 | for (uint64_t i = 0; i < pages; ++i) { |
| 2018 | const uint64_t page = found[static_cast<size_t>(i)]; | 2053 | const uint64_t page = found[static_cast<size_t>(i)]; |
| 2019 | if (!map_page(to + i * kPhysicalPageSize, page, protect, MapOwner::slot31)) return false; | 2054 | const uintptr_t gpu = to + i * kPhysicalPageSize, cpu = cpu_to + i * kPhysicalPageSize; |
| 2020 | if (cpu_window && !map_page(cpu_to + i * kPhysicalPageSize, page, cpu_protect, MapOwner::mirror)) return false; | 2055 | const UnmapCause cause(page ? "CopyPageMappingsBatchX (remap)" : "CopyPageMappingsBatchX (hole)"); |
| 2056 | if (!map_page(gpu, page, protect, MapOwner::slot31)) return false; | ||
| 2057 | if (cpu_window && !title_mapped_twin(cpu, gpu, false) && !map_page(cpu, page, cpu_protect, MapOwner::mirror)) | ||
| 2058 | return false; | ||
| 2021 | } | 2059 | } |
| 2022 | return true; | 2060 | return true; |
| 2023 | } | 2061 | } |