diff options
| author | aslan2142 <jan.trencansky@protonmail.com> | 2026-11-25 02:43:51 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-25 02:43:51 +0100 |
| commit | 0aecc5efab56a77eddf00ad6cefa046d42aaae6c (patch) | |
| tree | 110af0af7587dd3f58954594684e198ad56c4b4b /runtime/shims/xmem.cpp | |
| parent | 18a22c1aefa227fd2eda047bab3385147d569f9d (diff) | |
| parent | c5510733e9696cac6f3839b472e517725607c1d5 (diff) | |
Merge pull request #968 from CLHelper/runtime/queue-slot31
d3d12_xs: queue slot 31 is CopyPageMappingsBatchX, mapped through xmem (RT-QUEUE31)
Diffstat (limited to 'runtime/shims/xmem.cpp')
| -rw-r--r-- | runtime/shims/xmem.cpp | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/runtime/shims/xmem.cpp b/runtime/shims/xmem.cpp index 82d8142d0..c8cbbd8da 100644 --- a/runtime/shims/xmem.cpp +++ b/runtime/shims/xmem.cpp | |||
| @@ -983,6 +983,62 @@ BOOL WINAPI XMemMapPhysicalPagesScatter(void* const* addresses, uint32_t page_co | |||
| 983 | return TRUE; | 983 | return TRUE; |
| 984 | } | 984 | } |
| 985 | 985 | ||
| 986 | // rt::xmem::copy_page_mappings_fn (xmem.h), for the console queue's CopyPageMappingsBatchX: each destination page gets | ||
| 987 | // a view of the physical page mapped at its source page, so both addresses are one memory, as two XMemMapPhysicalPages | ||
| 988 | // calls with the same page make them. Every source page is looked up before any destination page changes, so a source | ||
| 989 | // range that overlaps the destination maps what was there before the call. A destination in a GPU-only range is mapped | ||
| 990 | // at its paired CPU range too (title_gpu_views), and a source page missing at a GPU-only address is looked up there. | ||
| 991 | bool copy_page_mappings(void* destination, const void* source, uint64_t pages, bool repeat_first) { | ||
| 992 | const uintptr_t to = reinterpret_cast<uintptr_t>(destination); | ||
| 993 | const uintptr_t from = reinterpret_cast<uintptr_t>(source); | ||
| 994 | constexpr uintptr_t kMax = (std::numeric_limits<uintptr_t>::max)(); | ||
| 995 | if (!aligned_page_address(to) || !aligned_page_address(from) || pages > kMax / kPhysicalPageSize || | ||
| 996 | to > kMax - pages * kPhysicalPageSize || (!repeat_first && from > kMax - pages * kPhysicalPageSize)) { | ||
| 997 | SetLastError(ERROR_INVALID_PARAMETER); | ||
| 998 | return false; | ||
| 999 | } | ||
| 1000 | if (!placeholder_api().available()) { | ||
| 1001 | SetLastError(ERROR_NOT_SUPPORTED); | ||
| 1002 | return false; | ||
| 1003 | } | ||
| 1004 | const std::lock_guard lock(g_phys_mutex); | ||
| 1005 | // The destination is usually a GPU-only reservation, which the D3D12 layer reads at its paired CPU view | ||
| 1006 | // (title_gpu_views::to_cpu); that view gets the same pages, or it would stay reserved and never readable. | ||
| 1007 | const uintptr_t cpu_to = leonida::title_gpu_views::to_cpu(to, pages * kPhysicalPageSize); | ||
| 1008 | const Window* const cpu_window = cpu_to != to ? find_window(cpu_to, pages * kPhysicalPageSize) : nullptr; | ||
| 1009 | if (!source) { | ||
| 1010 | for (uint64_t i = 0; i < pages; ++i) { | ||
| 1011 | if (!unmap_page(to + i * kPhysicalPageSize)) return false; | ||
| 1012 | if (cpu_window && !unmap_page(cpu_to + i * kPhysicalPageSize)) return false; | ||
| 1013 | } | ||
| 1014 | return true; | ||
| 1015 | } | ||
| 1016 | const Window* const window = find_window(to, pages * kPhysicalPageSize); | ||
| 1017 | if (!window) { | ||
| 1018 | SetLastError(ERROR_INVALID_ADDRESS); | ||
| 1019 | return false; | ||
| 1020 | } | ||
| 1021 | std::vector<uint64_t> found(static_cast<size_t>(pages), 0); | ||
| 1022 | for (uint64_t i = 0; i < pages; ++i) { | ||
| 1023 | const uintptr_t src_va = from + (repeat_first ? 0 : i) * kPhysicalPageSize; | ||
| 1024 | auto it = g_mapped.find(src_va); | ||
| 1025 | if (it == g_mapped.end()) { // a GPU-only source whose pages were mapped at its CPU view | ||
| 1026 | const uintptr_t cpu_src = leonida::title_gpu_views::to_cpu(src_va, kPhysicalPageSize); | ||
| 1027 | if (cpu_src != src_va) it = g_mapped.find(cpu_src); | ||
| 1028 | } | ||
| 1029 | if (it != g_mapped.end()) found[static_cast<size_t>(i)] = it->second; | ||
| 1030 | } | ||
| 1031 | const DWORD protect = view_protect(*window); | ||
| 1032 | const DWORD cpu_protect = cpu_window ? view_protect(*cpu_window) : 0; | ||
| 1033 | for (uint64_t i = 0; i < pages; ++i) { | ||
| 1034 | const uint64_t page = found[static_cast<size_t>(i)]; | ||
| 1035 | if (!map_page(to + i * kPhysicalPageSize, page, protect)) return false; | ||
| 1036 | if (cpu_window && !map_page(cpu_to + i * kPhysicalPageSize, page, cpu_protect)) return false; | ||
| 1037 | } | ||
| 1038 | return true; | ||
| 1039 | } | ||
| 1040 | [[maybe_unused]] const bool g_copy_page_mappings_set =(rt::xmem::copy_page_mappings_fn = ©_page_mappings, true); | ||
| 1041 | |||
| 986 | // The title's write watching, on reservations it made with MEM_WRITE_WATCH. One xmem write-watched for the shadows | 1042 | // The title's write watching, on reservations it made with MEM_WRITE_WATCH. One xmem write-watched for the shadows |
| 987 | // (reserve_watched) is refused as the host refuses a region that is not write-watched ((UINT)-1, | 1043 | // (reserve_watched) is refused as the host refuses a region that is not write-watched ((UINT)-1, |
| 988 | // ERROR_INVALID_PARAMETER), as the console would: it isn't write-watched there, and a reset here would clear the bits | 1044 | // ERROR_INVALID_PARAMETER), as the console would: it isn't write-watched there, and a reset here would clear the bits |