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:
authorJan Trencansky <jan.trencansky@protonmail.com>2026-11-24 21:09:27 -0500
committerJan Trencansky <jan.trencansky@protonmail.com>2026-11-24 21:09:27 -0500
commit7ea226e65b87491b0977f23d6ff693145d64d02b (patch)
tree39d3d45cd393db1a896d5e24a559e12257c5ed94 /runtime/shims/xmem.cpp
parent52ef7df72f856e4be441bc2cb274838a17cfce28 (diff)
parent0aecc5efab56a77eddf00ad6cefa046d42aaae6c (diff)
Merge main (0aecc5efa: #984 and #968) into the #939 merge
main now holds the #984 merge (18a22c1ae) and #968, queue slot 31 as CopyPageMappingsBatchX. Two conflicts: the include list of d3d12_xs_device.cpp (both includes kept) and the call order in xmem_test.cpp (the two page-mapping tests of #968 run after test_physical_pages, after the early return of #939's file-pools mode). The slot 31 dispatch of #968 stays ahead of the presentation routes, so #939's rule for queue slots 30 to 32 on a device that had a presentation handler does not reach it where xmem is linked. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Diffstat (limited to 'runtime/shims/xmem.cpp')
-rw-r--r--runtime/shims/xmem.cpp56
1 files changed, 56 insertions, 0 deletions
diff --git a/runtime/shims/xmem.cpp b/runtime/shims/xmem.cpp
index c0bf90503..3ccdabfe9 100644
--- a/runtime/shims/xmem.cpp
+++ b/runtime/shims/xmem.cpp
@@ -1026,6 +1026,62 @@ BOOL WINAPI XMemMapPhysicalPagesScatter(void* const* addresses, uint32_t page_co
1026 return TRUE; 1026 return TRUE;
1027} 1027}
1028 1028
1029// rt::xmem::copy_page_mappings_fn (xmem.h), for the console queue's CopyPageMappingsBatchX: each destination page gets
1030// a view of the physical page mapped at its source page, so both addresses are one memory, as two XMemMapPhysicalPages
1031// calls with the same page make them. Every source page is looked up before any destination page changes, so a source
1032// range that overlaps the destination maps what was there before the call. A destination in a GPU-only range is mapped
1033// at its paired CPU range too (title_gpu_views), and a source page missing at a GPU-only address is looked up there.
1034bool copy_page_mappings(void* destination, const void* source, uint64_t pages, bool repeat_first) {
1035 const uintptr_t to = reinterpret_cast<uintptr_t>(destination);
1036 const uintptr_t from = reinterpret_cast<uintptr_t>(source);
1037 constexpr uintptr_t kMax = (std::numeric_limits<uintptr_t>::max)();
1038 if (!aligned_page_address(to) || !aligned_page_address(from) || pages > kMax / kPhysicalPageSize ||
1039 to > kMax - pages * kPhysicalPageSize || (!repeat_first && from > kMax - pages * kPhysicalPageSize)) {
1040 SetLastError(ERROR_INVALID_PARAMETER);
1041 return false;
1042 }
1043 if (!placeholder_api().available()) {
1044 SetLastError(ERROR_NOT_SUPPORTED);
1045 return false;
1046 }
1047 const std::lock_guard lock(g_phys_mutex);
1048 // The destination is usually a GPU-only reservation, which the D3D12 layer reads at its paired CPU view
1049 // (title_gpu_views::to_cpu); that view gets the same pages, or it would stay reserved and never readable.
1050 const uintptr_t cpu_to = leonida::title_gpu_views::to_cpu(to, pages * kPhysicalPageSize);
1051 const Window* const cpu_window = cpu_to != to ? find_window(cpu_to, pages * kPhysicalPageSize) : nullptr;
1052 if (!source) {
1053 for (uint64_t i = 0; i < pages; ++i) {
1054 if (!unmap_page(to + i * kPhysicalPageSize)) return false;
1055 if (cpu_window && !unmap_page(cpu_to + i * kPhysicalPageSize)) return false;
1056 }
1057 return true;
1058 }
1059 const Window* const window = find_window(to, pages * kPhysicalPageSize);
1060 if (!window) {
1061 SetLastError(ERROR_INVALID_ADDRESS);
1062 return false;
1063 }
1064 std::vector<uint64_t> found(static_cast<size_t>(pages), 0);
1065 for (uint64_t i = 0; i < pages; ++i) {
1066 const uintptr_t src_va = from + (repeat_first ? 0 : i) * kPhysicalPageSize;
1067 auto it = g_mapped.find(src_va);
1068 if (it == g_mapped.end()) { // a GPU-only source whose pages were mapped at its CPU view
1069 const uintptr_t cpu_src = leonida::title_gpu_views::to_cpu(src_va, kPhysicalPageSize);
1070 if (cpu_src != src_va) it = g_mapped.find(cpu_src);
1071 }
1072 if (it != g_mapped.end()) found[static_cast<size_t>(i)] = it->second;
1073 }
1074 const DWORD protect = view_protect(*window);
1075 const DWORD cpu_protect = cpu_window ? view_protect(*cpu_window) : 0;
1076 for (uint64_t i = 0; i < pages; ++i) {
1077 const uint64_t page = found[static_cast<size_t>(i)];
1078 if (!map_page(to + i * kPhysicalPageSize, page, protect)) return false;
1079 if (cpu_window && !map_page(cpu_to + i * kPhysicalPageSize, page, cpu_protect)) return false;
1080 }
1081 return true;
1082}
1083[[maybe_unused]] const bool g_copy_page_mappings_set =(rt::xmem::copy_page_mappings_fn = &copy_page_mappings, true);
1084
1029// The title's write watching, on reservations it made with MEM_WRITE_WATCH. One xmem write-watched for the shadows 1085// The title's write watching, on reservations it made with MEM_WRITE_WATCH. One xmem write-watched for the shadows
1030// (reserve_watched) is refused as the host refuses a region that is not write-watched ((UINT)-1, 1086// (reserve_watched) is refused as the host refuses a region that is not write-watched ((UINT)-1,
1031// ERROR_INVALID_PARAMETER), as the console would: it isn't write-watched there, and a reset here would clear the bits 1087// ERROR_INVALID_PARAMETER), as the console would: it isn't write-watched there, and a reset here would clear the bits