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:
Diffstat (limited to 'runtime/shims/xmem.cpp')
-rw-r--r--runtime/shims/xmem.cpp36
1 files changed, 31 insertions, 5 deletions
diff --git a/runtime/shims/xmem.cpp b/runtime/shims/xmem.cpp
index 37f29459f..cccb4002f 100644
--- a/runtime/shims/xmem.cpp
+++ b/runtime/shims/xmem.cpp
@@ -35,6 +35,7 @@
35 35
36#include "shims.h" 36#include "shims.h"
37#include "startup_trace.h" 37#include "startup_trace.h"
38#include "../src/title_va_alias.h"
38 39
39namespace { 40namespace {
40 41
@@ -66,14 +67,27 @@ void note_attributes(const char* api, uint64_t attributes, size_t size, const vo
66 67
67void* allocate_block(size_t size) { return _aligned_malloc(size ? size : 1, kAlign); } 68void* allocate_block(size_t size) { return _aligned_malloc(size ? size : 1, kAlign); }
68 69
70// Every block and range the title gets here is recorded as the title's memory (src/title_va_alias.h): a shader
71// address into it can be given a GPU copy, and an address anywhere else cannot.
72void* title_block(size_t size) {
73 void* p = allocate_block(size);
74 leonida::title_va_aliases().add_title_memory(reinterpret_cast<uintptr_t>(p), size ? size : 1);
75 return p;
76}
77
78void free_title_block(void* p) {
79 if (p) leonida::title_va_aliases().remove_title_memory(reinterpret_cast<uintptr_t>(p));
80 _aligned_free(p);
81}
82
69void* __stdcall XMemAlloc(size_t size, uint64_t attributes) { 83void* __stdcall XMemAlloc(size_t size, uint64_t attributes) {
70 note_attributes("XMemAlloc", attributes, size, _ReturnAddress()); 84 note_attributes("XMemAlloc", attributes, size, _ReturnAddress());
71 return allocate_block(size); 85 return title_block(size);
72} 86}
73 87
74void __stdcall XMemFree(void* p, uint64_t attributes) { 88void __stdcall XMemFree(void* p, uint64_t attributes) {
75 note_attributes("XMemFree", attributes, kNoSize, _ReturnAddress()); 89 note_attributes("XMemFree", attributes, kNoSize, _ReturnAddress());
76 _aligned_free(p); 90 free_title_block(p);
77} 91}
78 92
79// XMemAllocDefault / XMemFreeDefault: the GDK's own allocator behind XMemAlloc and XMemFree, which a title that hooks 93// XMemAllocDefault / XMemFreeDefault: the GDK's own allocator behind XMemAlloc and XMemFree, which a title that hooks
@@ -82,12 +96,12 @@ void __stdcall XMemFree(void* p, uint64_t attributes) {
82// released by either free. 96// released by either free.
83void* __stdcall XMemAllocDefault(size_t size, uint64_t attributes) { 97void* __stdcall XMemAllocDefault(size_t size, uint64_t attributes) {
84 note_attributes("XMemAllocDefault", attributes, size, _ReturnAddress()); 98 note_attributes("XMemAllocDefault", attributes, size, _ReturnAddress());
85 return allocate_block(size); 99 return title_block(size);
86} 100}
87 101
88void __stdcall XMemFreeDefault(void* p, uint64_t attributes) { 102void __stdcall XMemFreeDefault(void* p, uint64_t attributes) {
89 note_attributes("XMemFreeDefault", attributes, kNoSize, _ReturnAddress()); 103 note_attributes("XMemFreeDefault", attributes, kNoSize, _ReturnAddress());
90 _aligned_free(p); 104 free_title_block(p);
91} 105}
92 106
93// BOOL XMemIsToolsMemAvailable(void). The measured title calls it from one helper, which six out-of-memory paths reach 107// BOOL XMemIsToolsMemAvailable(void). The measured title calls it from one helper, which six out-of-memory paths reach
@@ -320,8 +334,18 @@ PVOID reserve_placeholder(void* address, SIZE_T size, SIZE_T alignment) {
320// XMemMapPhysicalPages. The guest page size sets the alignment and rounding of the region; the host's own pages stay 334// XMemMapPhysicalPages. The guest page size sets the alignment and rounding of the region; the host's own pages stay
321// 4 KiB (host large pages need SeLockMemoryPrivilege). A 2 MiB region the host places is reserved 2 MiB-aligned through 335// 4 KiB (host large pages need SeLockMemoryPrivilege). A 2 MiB region the host places is reserved 2 MiB-aligned through
322// VirtualAlloc2. 336// VirtualAlloc2.
337void* xmem_virtual_alloc(void* address, SIZE_T size, DWORD allocation_type, ULONGLONG attributes, DWORD protect);
338
323void* WINAPI XMemVirtualAlloc(void* address, SIZE_T size, DWORD allocation_type, ULONGLONG attributes, DWORD protect) { 339void* WINAPI XMemVirtualAlloc(void* address, SIZE_T size, DWORD allocation_type, ULONGLONG attributes, DWORD protect) {
324 note_attributes("XMemVirtualAlloc", attributes, size, _ReturnAddress()); 340 note_attributes("XMemVirtualAlloc", attributes, size, _ReturnAddress());
341 void* result = xmem_virtual_alloc(address, size, allocation_type, attributes, protect);
342 // The title's memory (src/title_va_alias.h). Never forgotten: a released range is no longer committed, which
343 // the copy also checks.
344 if (result) leonida::title_va_aliases().add_title_memory(reinterpret_cast<uintptr_t>(result), size);
345 return result;
346}
347
348void* xmem_virtual_alloc(void* address, SIZE_T size, DWORD allocation_type, ULONGLONG attributes, DWORD protect) {
325 if (size == 0) { 349 if (size == 0) {
326 SetLastError(ERROR_INVALID_PARAMETER); 350 SetLastError(ERROR_INVALID_PARAMETER);
327 return nullptr; 351 return nullptr;
@@ -689,7 +713,9 @@ constexpr DWORD kMemGraphics = 0x10000000;
689constexpr DWORD kMemTitle = 0x40000000; 713constexpr DWORD kMemTitle = 0x40000000;
690 714
691LPVOID WINAPI TitleVirtualAlloc(LPVOID address, SIZE_T size, DWORD allocation_type, DWORD protect) { 715LPVOID WINAPI TitleVirtualAlloc(LPVOID address, SIZE_T size, DWORD allocation_type, DWORD protect) {
692 return VirtualAlloc(address, size, allocation_type & ~(kMemGraphics | kMemTitle), host_protect(protect)); 716 LPVOID result = VirtualAlloc(address, size, allocation_type & ~(kMemGraphics | kMemTitle), host_protect(protect));
717 if (result) leonida::title_va_aliases().add_title_memory(reinterpret_cast<uintptr_t>(result), size);
718 return result;
693} 719}
694 720
695} // namespace 721} // namespace