diff options
Diffstat (limited to 'runtime/shims/xmem.cpp')
| -rw-r--r-- | runtime/shims/xmem.cpp | 55 |
1 files changed, 48 insertions, 7 deletions
diff --git a/runtime/shims/xmem.cpp b/runtime/shims/xmem.cpp index 44c6f140b..fca6f6f47 100644 --- a/runtime/shims/xmem.cpp +++ b/runtime/shims/xmem.cpp | |||
| @@ -1033,6 +1033,7 @@ constexpr size_t kUnmapRecords = 1024; | |||
| 1033 | UnmapRecord g_unmaps[kUnmapRecords]; | 1033 | UnmapRecord g_unmaps[kUnmapRecords]; |
| 1034 | uint64_t g_unmap_count = 0; | 1034 | uint64_t g_unmap_count = 0; |
| 1035 | thread_local const char* t_unmap_cause = "unmap"; | 1035 | thread_local const char* t_unmap_cause = "unmap"; |
| 1036 | |||
| 1036 | struct UnmapCause { | 1037 | struct UnmapCause { |
| 1037 | const char* was; | 1038 | const char* was; |
| 1038 | explicit UnmapCause(const char* cause) : was(std::exchange(t_unmap_cause, cause)) {} | 1039 | explicit UnmapCause(const char* cause) : was(std::exchange(t_unmap_cause, cause)) {} |
| @@ -1576,7 +1577,7 @@ LONG CALLBACK physical_page_fault(EXCEPTION_POINTERS* info) { | |||
| 1576 | MEMORY_BASIC_INFORMATION seen{}; // the page as the decision saw it | 1577 | MEMORY_BASIC_INFORMATION seen{}; // the page as the decision saw it |
| 1577 | char read_note[1024]; | 1578 | char read_note[1024]; |
| 1578 | DWORD read_note_size = 0; | 1579 | DWORD read_note_size = 0; |
| 1579 | bool plant_read = false, promote = false; | 1580 | bool plant_read = false, promote = false, from_title = false; |
| 1580 | bool queried = false; | 1581 | bool queried = false; |
| 1581 | { | 1582 | { |
| 1582 | const FaultLockShared lock; | 1583 | const FaultLockShared lock; |
| @@ -1595,7 +1596,8 @@ LONG CALLBACK physical_page_fault(EXCEPTION_POINTERS* info) { | |||
| 1595 | if (where != kNoReservation || access == 1) | 1596 | if (where != kNoReservation || access == 1) |
| 1596 | queried = VirtualQuery(reinterpret_cast<const void*>(accessed), &seen, sizeof(seen)) != 0; | 1597 | queried = VirtualQuery(reinterpret_cast<const void*>(accessed), &seen, sizeof(seen)) != 0; |
| 1597 | const auto pc = reinterpret_cast<uintptr_t>(record->ExceptionAddress); | 1598 | const auto pc = reinterpret_cast<uintptr_t>(record->ExceptionAddress); |
| 1598 | if (access == 0 && result == PageCommit::not_ours && title_code(pc)) { | 1599 | from_title = access == 0 && result == PageCommit::not_ours && title_code(pc); |
| 1600 | if (from_title) { | ||
| 1599 | if (!queried) queried = VirtualQuery(reinterpret_cast<const void*>(accessed), &seen, sizeof(seen)) != 0; | 1601 | if (!queried) queried = VirtualQuery(reinterpret_cast<const void*>(accessed), &seen, sizeof(seen)) != 0; |
| 1600 | read_note_size = describe_read(read_note, pc, accessed, seen); | 1602 | read_note_size = describe_read(read_note, pc, accessed, seen); |
| 1601 | plant_read = where == kInWindow && g_pool_zero_pages; | 1603 | plant_read = where == kInWindow && g_pool_zero_pages; |
| @@ -1671,7 +1673,8 @@ LONG CALLBACK physical_page_fault(EXCEPTION_POINTERS* info) { | |||
| 1671 | exit = FaultExit::committed; | 1673 | exit = FaultExit::committed; |
| 1672 | } else if (answered) { | 1674 | } else if (answered) { |
| 1673 | exit = FaultExit::answered; | 1675 | exit = FaultExit::answered; |
| 1674 | } else if (result == PageCommit::committed_before && !escalated) { | 1676 | } else if ((result == PageCommit::committed_before && !escalated) || |
| 1677 | (title_page && access == 0 && result == PageCommit::not_ours && !from_title)) { | ||
| 1675 | exit = FaultExit::passed_guarded; // a guard's (the D3D12 layer's), routine: not noted | 1678 | exit = FaultExit::passed_guarded; // a guard's (the D3D12 layer's), routine: not noted |
| 1676 | } else if (title_page || reserved_write) { | 1679 | } else if (title_page || reserved_write) { |
| 1677 | note_fault(kRefused, accessed, access, result, where, seen); | 1680 | note_fault(kRefused, accessed, access, result, where, seen); |
| @@ -1970,6 +1973,7 @@ BOOL WINAPI XMemMapPhysicalPagesScatter(void* const* addresses, uint32_t page_co | |||
| 1970 | return scatter_refused(ScatterFailure::unaligned, address, ERROR_INVALID_PARAMETER); | 1973 | return scatter_refused(ScatterFailure::unaligned, address, ERROR_INVALID_PARAMETER); |
| 1971 | } | 1974 | } |
| 1972 | const uint64_t page = page_array ? page_array[i] : 0; | 1975 | const uint64_t page = page_array ? page_array[i] : 0; |
| 1976 | const UnmapCause cause(page ? "XMemMapPhysicalPagesScatter (remap)" : "XMemMapPhysicalPagesScatter (unmap)"); | ||
| 1973 | if (page == 0) { | 1977 | if (page == 0) { |
| 1974 | if (!unmap_page(address)) return scatter_refused(ScatterFailure::unmap, address, GetLastError()); | 1978 | if (!unmap_page(address)) return scatter_refused(ScatterFailure::unmap, address, GetLastError()); |
| 1975 | continue; | 1979 | continue; |
| @@ -1992,6 +1996,34 @@ BOOL WINAPI XMemMapPhysicalPagesScatter(void* const* addresses, uint32_t page_co | |||
| 1992 | // calls with the same page make them. Every source page is looked up before any destination page changes, so a source | 1996 | // calls with the same page make them. Every source page is looked up before any destination page changes, so a source |
| 1993 | // range that overlaps the destination maps what was there before the call. A destination in a GPU-only range is mapped | 1997 | // range that overlaps the destination maps what was there before the call. A destination in a GPU-only range is mapped |
| 1994 | // at its paired CPU range too (title_gpu_views), and a source page missing at a GPU-only address is looked up there. | 1998 | // at its paired CPU range too (title_gpu_views), and a source page missing at a GPU-only address is looked up there. |
| 1999 | // A page of that CPU range the title mapped itself is the title's (title_mapped_twin): the copy leaves it as it is. | ||
| 2000 | |||
| 2001 | // With g_phys_mutex held. True when the paired CPU page at `address` holds a mapping of the title's own: its | ||
| 2002 | // XMemMapPhysicalPages or XMemMapPhysicalPagesScatter call, or a slot-31 copy onto that address. The console's | ||
| 2003 | // CopyPageMappingsBatchX changes only the GPU-only range's page table, so such a page keeps what the title put | ||
| 2004 | // there; only a page the copy mirrored there itself (MapOwner::mirror), a zero plant, or a page with no mapping | ||
| 2005 | // follows the GPU-only range. Each page kept is counted, and the first ones logged. | ||
| 2006 | bool title_mapped_twin(uintptr_t address, uintptr_t gpu, bool unmap) { | ||
| 2007 | MapOwner owner; | ||
| 2008 | { | ||
| 2009 | const FaultLockShared faults; | ||
| 2010 | uintptr_t base = 0; | ||
| 2011 | const View* const view = covering_view(address, base); | ||
| 2012 | if (!view || view->zero || view->owner == MapOwner::mirror) return false; | ||
| 2013 | owner = view->owner; | ||
| 2014 | } | ||
| 2015 | static std::atomic<uint64_t> kept{0}; | ||
| 2016 | const uint64_t n = kept.fetch_add(1, std::memory_order_relaxed) + 1; | ||
| 2017 | if (n <= 8 || (n & (n - 1)) == 0) | ||
| 2018 | std::fprintf(stderr, | ||
| 2019 | "[rt] xmem: slot-31 %s at 0x%llx left the title's %s mapping at its paired CPU page 0x%llx " | ||
| 2020 | "(%llu such pages so far)\n", | ||
| 2021 | unmap ? "unmap" : "copy", static_cast<unsigned long long>(gpu), | ||
| 2022 | owner == MapOwner::scatter ? "scatter" : owner == MapOwner::slot31 ? "slot-31" : "physical", | ||
| 2023 | static_cast<unsigned long long>(address), static_cast<unsigned long long>(n)); | ||
| 2024 | return true; | ||
| 2025 | } | ||
| 2026 | |||
| 1995 | bool copy_page_mappings(void* destination, const void* source, uint64_t pages, bool repeat_first) { | 2027 | bool copy_page_mappings(void* destination, const void* source, uint64_t pages, bool repeat_first) { |
| 1996 | const uintptr_t to = reinterpret_cast<uintptr_t>(destination); | 2028 | const uintptr_t to = reinterpret_cast<uintptr_t>(destination); |
| 1997 | const uintptr_t from = reinterpret_cast<uintptr_t>(source); | 2029 | const uintptr_t from = reinterpret_cast<uintptr_t>(source); |
| @@ -2012,8 +2044,14 @@ bool copy_page_mappings(void* destination, const void* source, uint64_t pages, b | |||
| 2012 | const Window* const cpu_window = cpu_to != to ? find_window(cpu_to, pages * kPhysicalPageSize) : nullptr; | 2044 | const Window* const cpu_window = cpu_to != to ? find_window(cpu_to, pages * kPhysicalPageSize) : nullptr; |
| 2013 | if (!source) { | 2045 | if (!source) { |
| 2014 | for (uint64_t i = 0; i < pages; ++i) { | 2046 | for (uint64_t i = 0; i < pages; ++i) { |
| 2015 | if (!unmap_page(to + i * kPhysicalPageSize)) return false; | 2047 | const uintptr_t gpu = to + i * kPhysicalPageSize, cpu = cpu_to + i * kPhysicalPageSize; |
| 2016 | if (cpu_window && !unmap_page(cpu_to + i * kPhysicalPageSize)) return false; | 2048 | { |
| 2049 | const UnmapCause cause("CopyPageMappingsBatchX (unmap)"); | ||
| 2050 | if (!unmap_page(gpu)) return false; | ||
| 2051 | } | ||
| 2052 | if (!cpu_window || title_mapped_twin(cpu, gpu, true)) continue; | ||
| 2053 | const UnmapCause cause("CopyPageMappingsBatchX (unmap paired CPU page)"); | ||
| 2054 | if (!unmap_page(cpu)) return false; | ||
| 2017 | } | 2055 | } |
| 2018 | return true; | 2056 | return true; |
| 2019 | } | 2057 | } |
| @@ -2036,8 +2074,11 @@ bool copy_page_mappings(void* destination, const void* source, uint64_t pages, b | |||
| 2036 | const DWORD cpu_protect = cpu_window ? view_protect(*cpu_window) : 0; | 2074 | const DWORD cpu_protect = cpu_window ? view_protect(*cpu_window) : 0; |
| 2037 | for (uint64_t i = 0; i < pages; ++i) { | 2075 | for (uint64_t i = 0; i < pages; ++i) { |
| 2038 | const uint64_t page = found[static_cast<size_t>(i)]; | 2076 | const uint64_t page = found[static_cast<size_t>(i)]; |
| 2039 | if (!map_page(to + i * kPhysicalPageSize, page, protect, MapOwner::slot31)) return false; | 2077 | const uintptr_t gpu = to + i * kPhysicalPageSize, cpu = cpu_to + i * kPhysicalPageSize; |
| 2040 | if (cpu_window && !map_page(cpu_to + i * kPhysicalPageSize, page, cpu_protect, MapOwner::mirror)) return false; | 2078 | const UnmapCause cause(page ? "CopyPageMappingsBatchX (remap)" : "CopyPageMappingsBatchX (hole)"); |
| 2079 | if (!map_page(gpu, page, protect, MapOwner::slot31)) return false; | ||
| 2080 | if (cpu_window && !title_mapped_twin(cpu, gpu, false) && !map_page(cpu, page, cpu_protect, MapOwner::mirror)) | ||
| 2081 | return false; | ||
| 2041 | } | 2082 | } |
| 2042 | return true; | 2083 | return true; |
| 2043 | } | 2084 | } |