diff options
| author | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-26 09:32:25 -0500 |
|---|---|---|
| committer | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-26 09:32:25 -0500 |
| commit | 63c4978d2aa89cf896f8e42f450665a0d25aab74 (patch) | |
| tree | 1f36cc0bd35c1c058ae2bf6ac693532b06ed9e02 /runtime/shims/xmem.cpp | |
| parent | 071ec8449caf234f3255cdc083afd9db2eda399b (diff) | |
| parent | 28fc0262d2957412cecfbfa8dd679b8e9e7dbf53 (diff) | |
Merge #1972 from kurrutako: a slot-31 copy leaves the title's own mappings at the paired CPU range (RT-SLOT31-CPU-TWIN-TITLE-PAGES)
Head 28fc0262d. Closed by its author as incomplete; merged because world run 41 had it.
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 a81adc0a8..cbd857fce 100644 --- a/runtime/shims/xmem.cpp +++ b/runtime/shims/xmem.cpp | |||
| @@ -2024,6 +2024,7 @@ BOOL WINAPI XMemMapPhysicalPagesScatter(void* const* addresses, uint32_t page_co | |||
| 2024 | return scatter_refused(ScatterFailure::unaligned, address, ERROR_INVALID_PARAMETER); | 2024 | return scatter_refused(ScatterFailure::unaligned, address, ERROR_INVALID_PARAMETER); |
| 2025 | } | 2025 | } |
| 2026 | const uint64_t page = page_array ? page_array[i] : 0; | 2026 | const uint64_t page = page_array ? page_array[i] : 0; |
| 2027 | const UnmapCause cause(page ? "XMemMapPhysicalPagesScatter (remap)" : "XMemMapPhysicalPagesScatter (unmap)"); | ||
| 2027 | if (page == 0) { | 2028 | if (page == 0) { |
| 2028 | if (!unmap_page(address)) return scatter_refused(ScatterFailure::unmap, address, GetLastError()); | 2029 | if (!unmap_page(address)) return scatter_refused(ScatterFailure::unmap, address, GetLastError()); |
| 2029 | continue; | 2030 | continue; |
| @@ -2046,6 +2047,34 @@ BOOL WINAPI XMemMapPhysicalPagesScatter(void* const* addresses, uint32_t page_co | |||
| 2046 | // calls with the same page make them. Every source page is looked up before any destination page changes, so a source | 2047 | // calls with the same page make them. Every source page is looked up before any destination page changes, so a source |
| 2047 | // range that overlaps the destination maps what was there before the call. A destination in a GPU-only range is mapped | 2048 | // range that overlaps the destination maps what was there before the call. A destination in a GPU-only range is mapped |
| 2048 | // at its paired CPU range too (title_gpu_views), and a source page missing at a GPU-only address is looked up there. | 2049 | // at its paired CPU range too (title_gpu_views), and a source page missing at a GPU-only address is looked up there. |
| 2050 | // A page of that CPU range the title mapped itself is the title's (title_mapped_twin): the copy leaves it as it is. | ||
| 2051 | |||
| 2052 | // With g_phys_mutex held. True when the paired CPU page at `address` holds a mapping of the title's own: its | ||
| 2053 | // XMemMapPhysicalPages or XMemMapPhysicalPagesScatter call, or a slot-31 copy onto that address. The console's | ||
| 2054 | // CopyPageMappingsBatchX changes only the GPU-only range's page table, so such a page keeps what the title put | ||
| 2055 | // there; only a page the copy mirrored there itself (MapOwner::mirror), a zero plant, or a page with no mapping | ||
| 2056 | // follows the GPU-only range. Each page kept is counted, and the first ones logged. | ||
| 2057 | bool title_mapped_twin(uintptr_t address, uintptr_t gpu, bool unmap) { | ||
| 2058 | MapOwner owner; | ||
| 2059 | { | ||
| 2060 | const FaultLockShared faults; | ||
| 2061 | uintptr_t base = 0; | ||
| 2062 | const View* const view = covering_view(address, base); | ||
| 2063 | if (!view || view->zero || view->owner == MapOwner::mirror) return false; | ||
| 2064 | owner = view->owner; | ||
| 2065 | } | ||
| 2066 | static std::atomic<uint64_t> kept{0}; | ||
| 2067 | const uint64_t n = kept.fetch_add(1, std::memory_order_relaxed) + 1; | ||
| 2068 | if (n <= 8 || (n & (n - 1)) == 0) | ||
| 2069 | std::fprintf(stderr, | ||
| 2070 | "[rt] xmem: slot-31 %s at 0x%llx left the title's %s mapping at its paired CPU page 0x%llx " | ||
| 2071 | "(%llu such pages so far)\n", | ||
| 2072 | unmap ? "unmap" : "copy", static_cast<unsigned long long>(gpu), | ||
| 2073 | owner == MapOwner::scatter ? "scatter" : owner == MapOwner::slot31 ? "slot-31" : "physical", | ||
| 2074 | static_cast<unsigned long long>(address), static_cast<unsigned long long>(n)); | ||
| 2075 | return true; | ||
| 2076 | } | ||
| 2077 | |||
| 2049 | bool copy_page_mappings(void* destination, const void* source, uint64_t pages, bool repeat_first) { | 2078 | bool copy_page_mappings(void* destination, const void* source, uint64_t pages, bool repeat_first) { |
| 2050 | const uintptr_t to = reinterpret_cast<uintptr_t>(destination); | 2079 | const uintptr_t to = reinterpret_cast<uintptr_t>(destination); |
| 2051 | const uintptr_t from = reinterpret_cast<uintptr_t>(source); | 2080 | const uintptr_t from = reinterpret_cast<uintptr_t>(source); |
| @@ -2067,8 +2096,14 @@ bool copy_page_mappings(void* destination, const void* source, uint64_t pages, b | |||
| 2067 | const Window* const cpu_window = cpu_to != to ? find_window(cpu_to, pages * kPhysicalPageSize) : nullptr; | 2096 | const Window* const cpu_window = cpu_to != to ? find_window(cpu_to, pages * kPhysicalPageSize) : nullptr; |
| 2068 | if (!source) { | 2097 | if (!source) { |
| 2069 | for (uint64_t i = 0; i < pages; ++i) { | 2098 | for (uint64_t i = 0; i < pages; ++i) { |
| 2070 | if (!unmap_page(to + i * kPhysicalPageSize)) return false; | 2099 | const uintptr_t gpu = to + i * kPhysicalPageSize, cpu = cpu_to + i * kPhysicalPageSize; |
| 2071 | if (cpu_window && !unmap_page(cpu_to + i * kPhysicalPageSize)) return false; | 2100 | { |
| 2101 | const UnmapCause cause("CopyPageMappingsBatchX (unmap)"); | ||
| 2102 | if (!unmap_page(gpu)) return false; | ||
| 2103 | } | ||
| 2104 | if (!cpu_window || title_mapped_twin(cpu, gpu, true)) continue; | ||
| 2105 | const UnmapCause cause("CopyPageMappingsBatchX (unmap paired CPU page)"); | ||
| 2106 | if (!unmap_page(cpu)) return false; | ||
| 2072 | } | 2107 | } |
| 2073 | return true; | 2108 | return true; |
| 2074 | } | 2109 | } |
| @@ -2091,8 +2126,11 @@ bool copy_page_mappings(void* destination, const void* source, uint64_t pages, b | |||
| 2091 | const DWORD cpu_protect = cpu_window ? view_protect(*cpu_window) : 0; | 2126 | const DWORD cpu_protect = cpu_window ? view_protect(*cpu_window) : 0; |
| 2092 | for (uint64_t i = 0; i < pages; ++i) { | 2127 | for (uint64_t i = 0; i < pages; ++i) { |
| 2093 | const uint64_t page = found[static_cast<size_t>(i)]; | 2128 | const uint64_t page = found[static_cast<size_t>(i)]; |
| 2094 | if (!map_page(to + i * kPhysicalPageSize, page, protect, MapOwner::slot31)) return false; | 2129 | const uintptr_t gpu = to + i * kPhysicalPageSize, cpu = cpu_to + i * kPhysicalPageSize; |
| 2095 | if (cpu_window && !map_page(cpu_to + i * kPhysicalPageSize, page, cpu_protect, MapOwner::mirror)) return false; | 2130 | const UnmapCause cause(page ? "CopyPageMappingsBatchX (remap)" : "CopyPageMappingsBatchX (hole)"); |
| 2131 | if (!map_page(gpu, page, protect, MapOwner::slot31)) return false; | ||
| 2132 | if (cpu_window && !title_mapped_twin(cpu, gpu, false) && !map_page(cpu, page, cpu_protect, MapOwner::mirror)) | ||
| 2133 | return false; | ||
| 2096 | } | 2134 | } |
| 2097 | return true; | 2135 | return true; |
| 2098 | } | 2136 | } |