upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/shims/xmem.cpp
diff options
context:
space:
mode:
authorAslan2142 <jan.trencansky@protonmail.com>2026-11-26 15:41:00 +0100
committerGitHub <noreply@github.com>2026-11-26 15:41:00 +0100
commitfbf100b1b10e391625585fe6a8bf4e653662ca9a (patch)
tree497fe3bc8ef8b45d1ac8c68fa9f85196d08f11ad /runtime/shims/xmem.cpp
parent73516a2902a9872e4d21d1bdbbde4e26059497c5 (diff)
parent484b21262a21f9a99148b89ad2458b389faf7bd1 (diff)
Merge pull request #2350 from aslan2142/linux-amd-world
Self-merge by a maintainer, on purpose: current main does not get through the world on Linux/AMD, and this brings the regression fixes (the single-wave round-loop fallback of 9f98dcc61, the wave64 choice driven by llvmpipe) together with eleven PRs the world run needed. Tested on main 73516a290: translator suite 500 files / 6,365 tests, 0 failed; 513 standard-library test files pass; check_no_game_data clean on 80 files; Linux ctest 112/112; Wine suite (clang-cl + xwin, RDNA2 iGPU, RADV) with no test going from pass to fail. Not tested: native Windows, NVIDIA. The included PRs are closed in favour of this one with credit to their authors. AI-assisted (Claude Code, Claude Opus 5.5 and Claude Fable 5.2).
Diffstat (limited to 'runtime/shims/xmem.cpp')
-rw-r--r--runtime/shims/xmem.cpp109
1 files changed, 102 insertions, 7 deletions
diff --git a/runtime/shims/xmem.cpp b/runtime/shims/xmem.cpp
index 384f7bd27..cbd857fce 100644
--- a/runtime/shims/xmem.cpp
+++ b/runtime/shims/xmem.cpp
@@ -1032,12 +1032,62 @@ constexpr size_t kUnmapRecords = 1024;
1032UnmapRecord g_unmaps[kUnmapRecords]; 1032UnmapRecord g_unmaps[kUnmapRecords];
1033uint64_t g_unmap_count = 0; 1033uint64_t g_unmap_count = 0;
1034thread_local const char* t_unmap_cause = "unmap"; 1034thread_local const char* t_unmap_cause = "unmap";
1035
1036// A remap of physical pages (XMemMapPhysicalPages with pages, the scatter call, CopyPageMappingsBatchX): the pages
1037// whose frame it took away. unmap_range_locked records each page that held a frame while one is installed (under the
1038// fault lock: host_heap only, nothing else); when it goes, with g_phys_mutex still held and the fault lock not, every
1039// page now mapped to another frame than it had (or to none) is reported as released, in runs, so the geometry copies
1040// over it are retired as over freed or unmapped memory (geometry_writes::note_released, GeometryViews::drain_released):
1041// the title streams by remapping the frames of its pools, and the copies of what those pages held before were kept
1042// for the device's life. A page mapped again to the frame it had is no change. LEONIDA_GEOMETRY_REMAP_RELEASE=0: off.
1043bool remap_releases_enabled() {
1044 static const bool on = [] {
1045 const char* v = std::getenv("LEONIDA_GEOMETRY_REMAP_RELEASE");
1046 return !v || std::strcmp(v, "0") != 0;
1047 }();
1048 return on;
1049}
1050struct RemapReleases;
1051thread_local RemapReleases* t_remap_releases = nullptr;
1052struct RemapReleases {
1053 leonida::host_heap::vector<std::pair<uintptr_t, uint64_t>> pages; // address, the frame it had
1054 bool active = false;
1055 RemapReleases() {
1056 if (!t_remap_releases && remap_releases_enabled()) {
1057 t_remap_releases = this;
1058 active = true;
1059 }
1060 }
1061 RemapReleases(const RemapReleases&) = delete;
1062 RemapReleases& operator=(const RemapReleases&) = delete;
1063 ~RemapReleases(); // g_phys_mutex held, the fault lock not
1064};
1035struct UnmapCause { 1065struct UnmapCause {
1036 const char* was; 1066 const char* was;
1037 explicit UnmapCause(const char* cause) : was(std::exchange(t_unmap_cause, cause)) {} 1067 explicit UnmapCause(const char* cause) : was(std::exchange(t_unmap_cause, cause)) {}
1038 ~UnmapCause() { t_unmap_cause = was; } 1068 ~UnmapCause() { t_unmap_cause = was; }
1039}; 1069};
1040 1070
1071RemapReleases::~RemapReleases() {
1072 if (!active) return;
1073 t_remap_releases = nullptr;
1074 if (pages.empty()) return;
1075 std::sort(pages.begin(), pages.end());
1076 uintptr_t run_begin = 0, run_end = 0;
1077 for (const auto& [at, frame] : pages) {
1078 const auto now = g_mapped.find(at);
1079 if (now != g_mapped.end() && now->second == frame) continue; // the frame it had: no change
1080 if (run_end == at && run_end > run_begin) {
1081 run_end = at + kPhysicalPageSize;
1082 continue;
1083 }
1084 if (run_end > run_begin) leonida::geometry_writes::note_released(run_begin, run_end);
1085 run_begin = at;
1086 run_end = at + kPhysicalPageSize;
1087 }
1088 if (run_end > run_begin) leonida::geometry_writes::note_released(run_begin, run_end);
1089}
1090
1041// With g_fault_lock held exclusively. Unmaps every page in [start, end); the addresses become placeholders again. A 1091// With g_fault_lock held exclusively. Unmaps every page in [start, end); the addresses become placeholders again. A
1042// view that reaches outside the range is unmapped whole, and its pages outside the range are mapped again, so 1092// view that reaches outside the range is unmapped whole, and its pages outside the range are mapped again, so
1043// neighbours keep their pages. 1093// neighbours keep their pages.
@@ -1064,6 +1114,7 @@ bool unmap_range_locked(uintptr_t start, uintptr_t end) {
1064 // The pages of [start, end) this view held are unmapped now; the ones outside it are mapped again below. 1114 // The pages of [start, end) this view held are unmapped now; the ones outside it are mapped again below.
1065 for (uintptr_t at = (std::max)(base, start); at < (std::min)(view_end, end); at += kPhysicalPageSize) { 1115 for (uintptr_t at = (std::max)(base, start); at < (std::min)(view_end, end); at += kPhysicalPageSize) {
1066 if (const auto mapped = g_mapped.find(at); mapped != g_mapped.end()) { 1116 if (const auto mapped = g_mapped.find(at); mapped != g_mapped.end()) {
1117 if (t_remap_releases) t_remap_releases->pages.emplace_back(at, mapped->second);
1067 note_mapping(mapped->second, at, false); 1118 note_mapping(mapped->second, at, false);
1068 g_mapped.erase(mapped); 1119 g_mapped.erase(mapped);
1069 } 1120 }
@@ -1575,7 +1626,7 @@ LONG CALLBACK physical_page_fault(EXCEPTION_POINTERS* info) {
1575 MEMORY_BASIC_INFORMATION seen{}; // the page as the decision saw it 1626 MEMORY_BASIC_INFORMATION seen{}; // the page as the decision saw it
1576 char read_note[1024]; 1627 char read_note[1024];
1577 DWORD read_note_size = 0; 1628 DWORD read_note_size = 0;
1578 bool plant_read = false, promote = false; 1629 bool plant_read = false, promote = false, from_title = false;
1579 bool queried = false; 1630 bool queried = false;
1580 { 1631 {
1581 const FaultLockShared lock; 1632 const FaultLockShared lock;
@@ -1594,7 +1645,8 @@ LONG CALLBACK physical_page_fault(EXCEPTION_POINTERS* info) {
1594 if (where != kNoReservation || access == 1) 1645 if (where != kNoReservation || access == 1)
1595 queried = VirtualQuery(reinterpret_cast<const void*>(accessed), &seen, sizeof(seen)) != 0; 1646 queried = VirtualQuery(reinterpret_cast<const void*>(accessed), &seen, sizeof(seen)) != 0;
1596 const auto pc = reinterpret_cast<uintptr_t>(record->ExceptionAddress); 1647 const auto pc = reinterpret_cast<uintptr_t>(record->ExceptionAddress);
1597 if (access == 0 && result == PageCommit::not_ours && title_code(pc)) { 1648 from_title = access == 0 && result == PageCommit::not_ours && title_code(pc);
1649 if (from_title) {
1598 if (!queried) queried = VirtualQuery(reinterpret_cast<const void*>(accessed), &seen, sizeof(seen)) != 0; 1650 if (!queried) queried = VirtualQuery(reinterpret_cast<const void*>(accessed), &seen, sizeof(seen)) != 0;
1599 read_note_size = describe_read(read_note, pc, accessed, seen); 1651 read_note_size = describe_read(read_note, pc, accessed, seen);
1600 plant_read = where == kInWindow && g_pool_zero_pages; 1652 plant_read = where == kInWindow && g_pool_zero_pages;
@@ -1670,7 +1722,8 @@ LONG CALLBACK physical_page_fault(EXCEPTION_POINTERS* info) {
1670 exit = FaultExit::committed; 1722 exit = FaultExit::committed;
1671 } else if (answered) { 1723 } else if (answered) {
1672 exit = FaultExit::answered; 1724 exit = FaultExit::answered;
1673 } else if (result == PageCommit::committed_before && !escalated) { 1725 } else if ((result == PageCommit::committed_before && !escalated) ||
1726 (title_page && access == 0 && result == PageCommit::not_ours && !from_title)) {
1674 exit = FaultExit::passed_guarded; // a guard's (the D3D12 layer's), routine: not noted 1727 exit = FaultExit::passed_guarded; // a guard's (the D3D12 layer's), routine: not noted
1675 } else if (title_page || reserved_write) { 1728 } else if (title_page || reserved_write) {
1676 note_fault(kRefused, accessed, access, result, where, seen); 1729 note_fault(kRefused, accessed, access, result, where, seen);
@@ -1834,6 +1887,7 @@ void* WINAPI XMemMapPhysicalPages(void* address, uint64_t page_count, const uint
1834 } 1887 }
1835 drain_fault_notes(); 1888 drain_fault_notes();
1836 const std::lock_guard lock(g_phys_mutex); 1889 const std::lock_guard lock(g_phys_mutex);
1890 RemapReleases remap_releases; // reported as it goes: after the fault lock, with g_phys_mutex
1837 const uintptr_t end = start + page_count * kPhysicalPageSize; 1891 const uintptr_t end = start + page_count * kPhysicalPageSize;
1838 const UnmapCause cause(page_array ? "XMemMapPhysicalPages (remap)" : "XMemMapPhysicalPages (unmap)"); 1892 const UnmapCause cause(page_array ? "XMemMapPhysicalPages (remap)" : "XMemMapPhysicalPages (unmap)");
1839 if (!page_array) { 1893 if (!page_array) {
@@ -1957,6 +2011,7 @@ BOOL scatter_refused(ScatterFailure reason, uintptr_t address, DWORD error) {
1957BOOL WINAPI XMemMapPhysicalPagesScatter(void* const* addresses, uint32_t page_count, const uint64_t* page_array) { 2011BOOL WINAPI XMemMapPhysicalPagesScatter(void* const* addresses, uint32_t page_count, const uint64_t* page_array) {
1958 drain_fault_notes(); 2012 drain_fault_notes();
1959 const std::lock_guard lock(g_phys_mutex); 2013 const std::lock_guard lock(g_phys_mutex);
2014 RemapReleases remap_releases;
1960 if (page_count && !addresses) { 2015 if (page_count && !addresses) {
1961 return scatter_refused(ScatterFailure::missing_addresses, 0, ERROR_INVALID_PARAMETER); 2016 return scatter_refused(ScatterFailure::missing_addresses, 0, ERROR_INVALID_PARAMETER);
1962 } 2017 }
@@ -1969,6 +2024,7 @@ BOOL WINAPI XMemMapPhysicalPagesScatter(void* const* addresses, uint32_t page_co
1969 return scatter_refused(ScatterFailure::unaligned, address, ERROR_INVALID_PARAMETER); 2024 return scatter_refused(ScatterFailure::unaligned, address, ERROR_INVALID_PARAMETER);
1970 } 2025 }
1971 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)");
1972 if (page == 0) { 2028 if (page == 0) {
1973 if (!unmap_page(address)) return scatter_refused(ScatterFailure::unmap, address, GetLastError()); 2029 if (!unmap_page(address)) return scatter_refused(ScatterFailure::unmap, address, GetLastError());
1974 continue; 2030 continue;
@@ -1991,6 +2047,34 @@ BOOL WINAPI XMemMapPhysicalPagesScatter(void* const* addresses, uint32_t page_co
1991// 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
1992// 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
1993// 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.
2057bool 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
1994bool copy_page_mappings(void* destination, const void* source, uint64_t pages, bool repeat_first) { 2078bool copy_page_mappings(void* destination, const void* source, uint64_t pages, bool repeat_first) {
1995 const uintptr_t to = reinterpret_cast<uintptr_t>(destination); 2079 const uintptr_t to = reinterpret_cast<uintptr_t>(destination);
1996 const uintptr_t from = reinterpret_cast<uintptr_t>(source); 2080 const uintptr_t from = reinterpret_cast<uintptr_t>(source);
@@ -2005,14 +2089,21 @@ bool copy_page_mappings(void* destination, const void* source, uint64_t pages, b
2005 return false; 2089 return false;
2006 } 2090 }
2007 const std::lock_guard lock(g_phys_mutex); 2091 const std::lock_guard lock(g_phys_mutex);
2092 RemapReleases remap_releases;
2008 // The destination is usually a GPU-only reservation, which the D3D12 layer reads at its paired CPU view 2093 // The destination is usually a GPU-only reservation, which the D3D12 layer reads at its paired CPU view
2009 // (title_gpu_views::to_cpu); that view gets the same pages, or it would stay reserved and never readable. 2094 // (title_gpu_views::to_cpu); that view gets the same pages, or it would stay reserved and never readable.
2010 const uintptr_t cpu_to = leonida::title_gpu_views::to_cpu(to, pages * kPhysicalPageSize); 2095 const uintptr_t cpu_to = leonida::title_gpu_views::to_cpu(to, pages * kPhysicalPageSize);
2011 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;
2012 if (!source) { 2097 if (!source) {
2013 for (uint64_t i = 0; i < pages; ++i) { 2098 for (uint64_t i = 0; i < pages; ++i) {
2014 if (!unmap_page(to + i * kPhysicalPageSize)) return false; 2099 const uintptr_t gpu = to + i * kPhysicalPageSize, cpu = cpu_to + i * kPhysicalPageSize;
2015 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;
2016 } 2107 }
2017 return true; 2108 return true;
2018 } 2109 }
@@ -2035,8 +2126,11 @@ bool copy_page_mappings(void* destination, const void* source, uint64_t pages, b
2035 const DWORD cpu_protect = cpu_window ? view_protect(*cpu_window) : 0; 2126 const DWORD cpu_protect = cpu_window ? view_protect(*cpu_window) : 0;
2036 for (uint64_t i = 0; i < pages; ++i) { 2127 for (uint64_t i = 0; i < pages; ++i) {
2037 const uint64_t page = found[static_cast<size_t>(i)]; 2128 const uint64_t page = found[static_cast<size_t>(i)];
2038 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;
2039 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;
2040 } 2134 }
2041 return true; 2135 return true;
2042} 2136}
@@ -2399,6 +2493,7 @@ bool copy_page_mappings(std::uint32_t count, const PageBatch* batches, std::uint
2399 std::vector<uint64_t> frames; 2493 std::vector<uint64_t> frames;
2400 drain_fault_notes(); 2494 drain_fault_notes();
2401 const std::lock_guard lock(g_phys_mutex); 2495 const std::lock_guard lock(g_phys_mutex);
2496 RemapReleases remap_releases;
2402 try { 2497 try {
2403 for (uint32_t b = 0; b < count; ++b) { 2498 for (uint32_t b = 0; b < count; ++b) {
2404 const auto& batch = batches[b]; 2499 const auto& batch = batches[b];