diff options
Diffstat (limited to 'runtime/shims/xmem.cpp')
| -rw-r--r-- | runtime/shims/xmem.cpp | 46 |
1 files changed, 29 insertions, 17 deletions
diff --git a/runtime/shims/xmem.cpp b/runtime/shims/xmem.cpp index e6ab2d43b..a1061af21 100644 --- a/runtime/shims/xmem.cpp +++ b/runtime/shims/xmem.cpp | |||
| @@ -30,34 +30,35 @@ | |||
| 30 | namespace { | 30 | namespace { |
| 31 | 31 | ||
| 32 | constexpr size_t kAlign = 64; | 32 | constexpr size_t kAlign = 64; |
| 33 | constexpr size_t kNoSize = static_cast<size_t>(-1); // XMemFree has no size field | ||
| 33 | 34 | ||
| 34 | // gta6rt --trace FILE: the first XMemAlloc and the first XMemFree for each distinct raw attribute word go to the | 35 | // gta6rt --trace FILE: the first XMemAlloc, XMemFree and XMemVirtualAlloc call for each distinct raw attribute word go to the |
| 35 | // startup trace as an "xmem_attributes" event (api, the word, the size for an allocation, the caller as module+RVA). The | 36 | // startup trace as an "xmem_attributes" event (api, the word, the size for an allocation, the caller as module+RVA). The |
| 36 | // bits of the attribute word are not known yet; this records them from a real run without logging every allocation, | 37 | // bits of the attribute word are not known yet; this records them from a real run without logging every allocation, |
| 37 | // which would fill the file. Off: one relaxed load per call. | 38 | // which would fill the file. Off: one relaxed load per call. |
| 38 | void note_attributes(bool is_alloc, uint64_t attributes, size_t size, const void* caller) { | 39 | void note_attributes(const char* api, uint64_t attributes, size_t size, const void* caller) { |
| 39 | if (!rt::startup_trace::enabled()) return; | 40 | if (!rt::startup_trace::enabled()) return; |
| 40 | static std::mutex mutex; | 41 | static std::mutex mutex; |
| 41 | static std::set<std::pair<bool, uint64_t>> seen; | 42 | static std::set<std::pair<std::string, uint64_t>> seen; |
| 42 | { | 43 | { |
| 43 | const std::lock_guard lock(mutex); | 44 | const std::lock_guard lock(mutex); |
| 44 | if (!seen.insert({is_alloc, attributes}).second) return; | 45 | if (!seen.insert({api, attributes}).second) return; |
| 45 | } | 46 | } |
| 46 | std::string fields = std::string("\"api\":\"") + (is_alloc ? "XMemAlloc" : "XMemFree") + | 47 | std::string fields = std::string("\"api\":\"") + api + |
| 47 | "\",\"attributes\":" + rt::startup_trace::quote(rt::startup_trace::hex(attributes)); | 48 | "\",\"attributes\":" + rt::startup_trace::quote(rt::startup_trace::hex(attributes)); |
| 48 | if (is_alloc) fields += ",\"size\":" + std::to_string(size); | 49 | if (size != kNoSize) fields += ",\"size\":" + std::to_string(size); |
| 49 | fields += ",\"caller\":" + | 50 | fields += ",\"caller\":" + |
| 50 | rt::startup_trace::quote(rt::startup_trace::where(reinterpret_cast<uint64_t>(caller))); | 51 | rt::startup_trace::quote(rt::startup_trace::where(reinterpret_cast<uint64_t>(caller))); |
| 51 | rt::startup_trace::event("xmem_attributes", fields); | 52 | rt::startup_trace::event("xmem_attributes", fields); |
| 52 | } | 53 | } |
| 53 | 54 | ||
| 54 | void* __stdcall XMemAlloc(size_t size, uint64_t attributes) { | 55 | void* __stdcall XMemAlloc(size_t size, uint64_t attributes) { |
| 55 | note_attributes(true, attributes, size, _ReturnAddress()); | 56 | note_attributes("XMemAlloc", attributes, size, _ReturnAddress()); |
| 56 | return _aligned_malloc(size ? size : 1, kAlign); | 57 | return _aligned_malloc(size ? size : 1, kAlign); |
| 57 | } | 58 | } |
| 58 | 59 | ||
| 59 | void __stdcall XMemFree(void* p, uint64_t attributes) { | 60 | void __stdcall XMemFree(void* p, uint64_t attributes) { |
| 60 | note_attributes(false, attributes, 0, _ReturnAddress()); | 61 | note_attributes("XMemFree", attributes, kNoSize, _ReturnAddress()); |
| 61 | _aligned_free(p); | 62 | _aligned_free(p); |
| 62 | } | 63 | } |
| 63 | 64 | ||
| @@ -141,18 +142,29 @@ bool round_up_size(SIZE_T value, SIZE_T alignment, SIZE_T& rounded) { | |||
| 141 | return true; | 142 | return true; |
| 142 | } | 143 | } |
| 143 | 144 | ||
| 145 | // The console's PAGE_GRAPHICS_* protection bits (winnt.h: PAGE_GRAPHICS_NOACCESS 0x800 .. PAGE_GRAPHICS_NOCACHE | ||
| 146 | // 0x40000). The host refuses them with ERROR_INVALID_PARAMETER, and the D3D12 layer reads guest memory through the | ||
| 147 | // CPU mapping, so only the CPU protection is applied. The retail executable passes 0x801 and 0x22004. | ||
| 148 | constexpr DWORD kPageGraphicsMask = 0x0007F800; | ||
| 149 | |||
| 150 | DWORD host_protect(DWORD p) { | ||
| 151 | const DWORD cpu = p & ~kPageGraphicsMask; | ||
| 152 | return cpu ? cpu : PAGE_NOACCESS; | ||
| 153 | } | ||
| 154 | |||
| 144 | void* WINAPI XMemVirtualAlloc(void* address, SIZE_T size, DWORD allocation_type, ULONGLONG attributes, DWORD protect) { | 155 | void* WINAPI XMemVirtualAlloc(void* address, SIZE_T size, DWORD allocation_type, ULONGLONG attributes, DWORD protect) { |
| 145 | // Host-backed support covers ordinary and 64 KiB guest reservations. The 2 MiB guest mode needs an aligned | 156 | // Host-backed support covers ordinary and 64 KiB guest reservations. The 2 MiB guest mode needs an aligned |
| 146 | // reservation strategy that this shim does not provide, so fail explicitly instead of returning a misaligned | 157 | // reservation strategy that this shim does not provide, so fail explicitly instead of returning a misaligned |
| 147 | // region. Nonzero attribute flags also fail until their host mapping is known. | 158 | // region. |
| 159 | // | ||
| 160 | // The attribute word is recorded, not applied, as for XMemAlloc: the host has no allocator ids or memory types. | ||
| 161 | // The retail executable passes 0x21, 0x3, 0x183, 0x283 and 0x181 and stops at an int3 when a reservation | ||
| 162 | // returns NULL, so refusing a nonzero word is not an option. | ||
| 163 | note_attributes("XMemVirtualAlloc", attributes, size, _ReturnAddress()); | ||
| 148 | if (size == 0) { | 164 | if (size == 0) { |
| 149 | SetLastError(ERROR_INVALID_PARAMETER); | 165 | SetLastError(ERROR_INVALID_PARAMETER); |
| 150 | return nullptr; | 166 | return nullptr; |
| 151 | } | 167 | } |
| 152 | if (attributes != 0) { | ||
| 153 | SetLastError(ERROR_NOT_SUPPORTED); | ||
| 154 | return nullptr; | ||
| 155 | } | ||
| 156 | 168 | ||
| 157 | const DWORD page_mode = allocation_type & kXmemPageModeMask; | 169 | const DWORD page_mode = allocation_type & kXmemPageModeMask; |
| 158 | if (page_mode == kXmemLargePages) { | 170 | if (page_mode == kXmemLargePages) { |
| @@ -208,7 +220,7 @@ void* WINAPI XMemVirtualAlloc(void* address, SIZE_T size, DWORD allocation_type, | |||
| 208 | return nullptr; | 220 | return nullptr; |
| 209 | } | 221 | } |
| 210 | 222 | ||
| 211 | return VirtualAlloc(address, size, host_type, protect); | 223 | return VirtualAlloc(address, size, host_type, host_protect(protect)); |
| 212 | } | 224 | } |
| 213 | 225 | ||
| 214 | UINT WINAPI XMemGetWriteWatch(DWORD flags, void* base_address, SIZE_T region_size, void** addresses, ULONG_PTR* count, | 226 | UINT WINAPI XMemGetWriteWatch(DWORD flags, void* base_address, SIZE_T region_size, void** addresses, ULONG_PTR* count, |
| @@ -271,13 +283,13 @@ BOOL WINAPI XMemTransferMemory(uint32_t type, SIZE_T size) { | |||
| 271 | // with ERROR_INVALID_PARAMETER (measured on Windows 11 26200: VirtualAlloc(NULL, 64 KiB, MEM_COMMIT | MEM_RESERVE | | 283 | // with ERROR_INVALID_PARAMETER (measured on Windows 11 26200: VirtualAlloc(NULL, 64 KiB, MEM_COMMIT | MEM_RESERVE | |
| 272 | // 0x10000000, PAGE_READWRITE) returns NULL; so does 0x40000000), so an engine allocating its heaps this way gets | 284 | // 0x10000000, PAGE_READWRITE) returns NULL; so does 0x40000000), so an engine allocating its heaps this way gets |
| 273 | // NULL at boot. Both are hints about where the memory lives; the host has no such split, so they are dropped and | 285 | // NULL at boot. Both are hints about where the memory lives; the host has no such split, so they are dropped and |
| 274 | // the rest is passed through. Not done: the console protection bits, and whether graphics memory has to come from | 286 | // the rest is passed through. The PAGE_GRAPHICS_* protection bits are dropped (host_protect). Not done: whether |
| 275 | // an arena the D3D12 layer can see (#313). | 287 | // graphics memory has to come from an arena the D3D12 layer can see (#313). |
| 276 | constexpr DWORD kMemGraphics = 0x10000000; | 288 | constexpr DWORD kMemGraphics = 0x10000000; |
| 277 | constexpr DWORD kMemTitle = 0x40000000; | 289 | constexpr DWORD kMemTitle = 0x40000000; |
| 278 | 290 | ||
| 279 | LPVOID WINAPI TitleVirtualAlloc(LPVOID address, SIZE_T size, DWORD allocation_type, DWORD protect) { | 291 | LPVOID WINAPI TitleVirtualAlloc(LPVOID address, SIZE_T size, DWORD allocation_type, DWORD protect) { |
| 280 | return VirtualAlloc(address, size, allocation_type & ~(kMemGraphics | kMemTitle), protect); | 292 | return VirtualAlloc(address, size, allocation_type & ~(kMemGraphics | kMemTitle), host_protect(protect)); |
| 281 | } | 293 | } |
| 282 | 294 | ||
| 283 | } // namespace | 295 | } // namespace |