diff options
| author | Mumogg <104529023+Mumogg@users.noreply.github.com> | 2026-11-23 15:21:22 -0600 |
|---|---|---|
| committer | Mumogg <104529023+Mumogg@users.noreply.github.com> | 2026-11-23 15:21:22 -0600 |
| commit | f9896e0fe580d8d8933eaa1ae59bd000b0ed7940 (patch) | |
| tree | e7395fb9a0e4947826a4193c03def282886c028c /runtime/shims/xmem.cpp | |
| parent | 237f9e0ba83c8b8e740dd7bcc0e10d311fddb897 (diff) | |
| parent | 6d1f046da6083c623bf78f2438f37c988df6b3f8 (diff) | |
local: merge #9137 (slot 133 kept as on ladder-frame)
Diffstat (limited to 'runtime/shims/xmem.cpp')
| -rw-r--r-- | runtime/shims/xmem.cpp | 36 |
1 files changed, 31 insertions, 5 deletions
diff --git a/runtime/shims/xmem.cpp b/runtime/shims/xmem.cpp index e25bf4d4e..e5db2ce6d 100644 --- a/runtime/shims/xmem.cpp +++ b/runtime/shims/xmem.cpp | |||
| @@ -36,6 +36,7 @@ | |||
| 36 | 36 | ||
| 37 | #include "shims.h" | 37 | #include "shims.h" |
| 38 | #include "startup_trace.h" | 38 | #include "startup_trace.h" |
| 39 | #include "../src/title_va_alias.h" | ||
| 39 | 40 | ||
| 40 | namespace { | 41 | namespace { |
| 41 | 42 | ||
| @@ -67,14 +68,27 @@ void note_attributes(const char* api, uint64_t attributes, size_t size, const vo | |||
| 67 | 68 | ||
| 68 | void* allocate_block(size_t size) { return _aligned_malloc(size ? size : 1, kAlign); } | 69 | void* allocate_block(size_t size) { return _aligned_malloc(size ? size : 1, kAlign); } |
| 69 | 70 | ||
| 71 | // Every block and range the title gets here is recorded as the title's memory (src/title_va_alias.h): a shader | ||
| 72 | // address into it can be given a GPU copy, and an address anywhere else cannot. | ||
| 73 | void* title_block(size_t size) { | ||
| 74 | void* p = allocate_block(size); | ||
| 75 | leonida::title_va_aliases().add_title_memory(reinterpret_cast<uintptr_t>(p), size ? size : 1); | ||
| 76 | return p; | ||
| 77 | } | ||
| 78 | |||
| 79 | void free_title_block(void* p) { | ||
| 80 | if (p) leonida::title_va_aliases().remove_title_memory(reinterpret_cast<uintptr_t>(p)); | ||
| 81 | _aligned_free(p); | ||
| 82 | } | ||
| 83 | |||
| 70 | void* __stdcall XMemAlloc(size_t size, uint64_t attributes) { | 84 | void* __stdcall XMemAlloc(size_t size, uint64_t attributes) { |
| 71 | note_attributes("XMemAlloc", attributes, size, _ReturnAddress()); | 85 | note_attributes("XMemAlloc", attributes, size, _ReturnAddress()); |
| 72 | return allocate_block(size); | 86 | return title_block(size); |
| 73 | } | 87 | } |
| 74 | 88 | ||
| 75 | void __stdcall XMemFree(void* p, uint64_t attributes) { | 89 | void __stdcall XMemFree(void* p, uint64_t attributes) { |
| 76 | note_attributes("XMemFree", attributes, kNoSize, _ReturnAddress()); | 90 | note_attributes("XMemFree", attributes, kNoSize, _ReturnAddress()); |
| 77 | _aligned_free(p); | 91 | free_title_block(p); |
| 78 | } | 92 | } |
| 79 | 93 | ||
| 80 | // XMemAllocDefault / XMemFreeDefault: the GDK's own allocator behind XMemAlloc and XMemFree, which a title that hooks | 94 | // XMemAllocDefault / XMemFreeDefault: the GDK's own allocator behind XMemAlloc and XMemFree, which a title that hooks |
| @@ -83,12 +97,12 @@ void __stdcall XMemFree(void* p, uint64_t attributes) { | |||
| 83 | // released by either free. | 97 | // released by either free. |
| 84 | void* __stdcall XMemAllocDefault(size_t size, uint64_t attributes) { | 98 | void* __stdcall XMemAllocDefault(size_t size, uint64_t attributes) { |
| 85 | note_attributes("XMemAllocDefault", attributes, size, _ReturnAddress()); | 99 | note_attributes("XMemAllocDefault", attributes, size, _ReturnAddress()); |
| 86 | return allocate_block(size); | 100 | return title_block(size); |
| 87 | } | 101 | } |
| 88 | 102 | ||
| 89 | void __stdcall XMemFreeDefault(void* p, uint64_t attributes) { | 103 | void __stdcall XMemFreeDefault(void* p, uint64_t attributes) { |
| 90 | note_attributes("XMemFreeDefault", attributes, kNoSize, _ReturnAddress()); | 104 | note_attributes("XMemFreeDefault", attributes, kNoSize, _ReturnAddress()); |
| 91 | _aligned_free(p); | 105 | free_title_block(p); |
| 92 | } | 106 | } |
| 93 | 107 | ||
| 94 | // BOOL XMemIsToolsMemAvailable(void). The measured title calls it from one helper, which six out-of-memory paths reach | 108 | // BOOL XMemIsToolsMemAvailable(void). The measured title calls it from one helper, which six out-of-memory paths reach |
| @@ -330,8 +344,18 @@ PVOID reserve_placeholder(void* address, SIZE_T size, SIZE_T alignment) { | |||
| 330 | // XMemMapPhysicalPages. The guest page size sets the alignment and rounding of the region; the host's own pages stay | 344 | // XMemMapPhysicalPages. The guest page size sets the alignment and rounding of the region; the host's own pages stay |
| 331 | // 4 KiB (host large pages need SeLockMemoryPrivilege). A 2 MiB region the host places is reserved 2 MiB-aligned through | 345 | // 4 KiB (host large pages need SeLockMemoryPrivilege). A 2 MiB region the host places is reserved 2 MiB-aligned through |
| 332 | // VirtualAlloc2. | 346 | // VirtualAlloc2. |
| 347 | void* xmem_virtual_alloc(void* address, SIZE_T size, DWORD allocation_type, ULONGLONG attributes, DWORD protect); | ||
| 348 | |||
| 333 | void* WINAPI XMemVirtualAlloc(void* address, SIZE_T size, DWORD allocation_type, ULONGLONG attributes, DWORD protect) { | 349 | void* WINAPI XMemVirtualAlloc(void* address, SIZE_T size, DWORD allocation_type, ULONGLONG attributes, DWORD protect) { |
| 334 | note_attributes("XMemVirtualAlloc", attributes, size, _ReturnAddress()); | 350 | note_attributes("XMemVirtualAlloc", attributes, size, _ReturnAddress()); |
| 351 | void* result = xmem_virtual_alloc(address, size, allocation_type, attributes, protect); | ||
| 352 | // The title's memory (src/title_va_alias.h). Never forgotten: a released range is no longer committed, which | ||
| 353 | // the copy also checks. | ||
| 354 | if (result) leonida::title_va_aliases().add_title_memory(reinterpret_cast<uintptr_t>(result), size); | ||
| 355 | return result; | ||
| 356 | } | ||
| 357 | |||
| 358 | void* xmem_virtual_alloc(void* address, SIZE_T size, DWORD allocation_type, ULONGLONG attributes, DWORD protect) { | ||
| 335 | if (size == 0) { | 359 | if (size == 0) { |
| 336 | SetLastError(ERROR_INVALID_PARAMETER); | 360 | SetLastError(ERROR_INVALID_PARAMETER); |
| 337 | return nullptr; | 361 | return nullptr; |
| @@ -770,7 +794,9 @@ constexpr DWORD kMemGraphics = 0x10000000; | |||
| 770 | constexpr DWORD kMemTitle = 0x40000000; | 794 | constexpr DWORD kMemTitle = 0x40000000; |
| 771 | 795 | ||
| 772 | LPVOID WINAPI TitleVirtualAlloc(LPVOID address, SIZE_T size, DWORD allocation_type, DWORD protect) { | 796 | LPVOID WINAPI TitleVirtualAlloc(LPVOID address, SIZE_T size, DWORD allocation_type, DWORD protect) { |
| 773 | return VirtualAlloc(address, size, allocation_type & ~(kMemGraphics | kMemTitle), host_protect(protect)); | 797 | LPVOID result = VirtualAlloc(address, size, allocation_type & ~(kMemGraphics | kMemTitle), host_protect(protect)); |
| 798 | if (result) leonida::title_va_aliases().add_title_memory(reinterpret_cast<uintptr_t>(result), size); | ||
| 799 | return result; | ||
| 774 | } | 800 | } |
| 775 | 801 | ||
| 776 | } // namespace | 802 | } // namespace |