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:
authorcynbun <74268930+cynbun@users.noreply.github.com>2026-11-23 15:06:21 +0100
committerGitHub <noreply@github.com>2026-11-23 08:06:21 -0600
commit085fe1efd46d63ee4f4da9d700d23a40484ea6a2 (patch)
tree4ae5da04b3853dd08c0d0b60583da57e316e200a /runtime/shims/xmem.cpp
parent7022367cbd4f7ef30ec3168189c79eb68a6dd1d3 (diff)
xmem: serve XMemTransferMemory (RT-449) (#6351)
Squash-merged after review of PR #6351 - xmem: serve XMemTransferMemory (RT-449)
Diffstat (limited to 'runtime/shims/xmem.cpp')
-rw-r--r--runtime/shims/xmem.cpp48
1 files changed, 48 insertions, 0 deletions
diff --git a/runtime/shims/xmem.cpp b/runtime/shims/xmem.cpp
index c7cfb76c0..e6ab2d43b 100644
--- a/runtime/shims/xmem.cpp
+++ b/runtime/shims/xmem.cpp
@@ -21,6 +21,7 @@
21#include <limits> 21#include <limits>
22#include <mutex> 22#include <mutex>
23#include <set> 23#include <set>
24#include <string>
24#include <utility> 25#include <utility>
25 26
26#include "shims.h" 27#include "shims.h"
@@ -219,6 +220,52 @@ UINT WINAPI XMemResetWriteWatch(void* base_address, SIZE_T region_size) {
219 return ResetWriteWatch(base_address, region_size); 220 return ResetWriteWatch(base_address, region_size);
220} 221}
221 222
223// BOOL XMemTransferMemory(XMEM_TRANSFER_TYPE type, SIZE_T size). Its reference page and the XMEM_TRANSFER_TYPE enum
224// page (gaming/gdk/docs/reference/system/xmem/{functions/xmemtransfermemory,enums/xmem_transfer_type}) are NDA
225// topics on Microsoft Learn, so the shape comes from public code built against the GDK's xmem.h: Microsoft's
226// XBOX-MonoGame-NetRumble sample (console/NetRumble.Bootstrap/Main.cpp) and FNA-XNA/NativeAOT-Xbox (Bootstrap/Main.cpp)
227// both call `if (!XMemTransferMemory(XMemTransferTitleToSystem, additionalKernelPoolMemory))` with a `const size_t`,
228// commented "Transfer additional memory to kernel pool" and "Must be multiples of 2MB sizes". So: two arguments, the
229// enum (32 bits) in ECX and the size in RDX, and a zero return is failure. The public XLaunchNewGame page adds that
230// "Memory transferred to the system using XMemTransferMemory (NDA topic) is persisted", and the game-save debugging
231// page suggests it for E_OUTOFMEMORY "to transfer some memory to the system partition".
232//
233// On the console it moves budget between the title's partition and the system's. A PC has one pool and no budget
234// split, so there is nothing to move: every direction and size returns TRUE and nothing is allocated or reserved. Not
235// known (the pages are NDA): the numeric XMEM_TRANSFER_TYPE values, and whether the console refuses or rounds a size
236// that is not a multiple of 2 MiB. Refusing would stop a title at startup (both samples return -1), while succeeding
237// changes nothing here. TRUE is 1 in EAX, so it also reads as true if the real return type is a one-byte bool.
238//
239// gta6rt --trace FILE: the first call for each distinct (type, size) pair goes to the startup trace as an
240// "xmem_transfer" event with the caller as module+RVA, so a run with the executable records the values it passes.
241BOOL WINAPI XMemTransferMemory(uint32_t type, SIZE_T size) {
242 static std::atomic<bool> warned{false};
243 if (!warned.exchange(true, std::memory_order_relaxed)) {
244 std::fprintf(stderr,
245 "[rt] xmem: XMemTransferMemory(type %u, %zu bytes): a PC has no title/system memory partition; "
246 "returning TRUE without moving anything (logged once)\n",
247 type, static_cast<size_t>(size));
248 }
249 if (rt::startup_trace::enabled()) {
250 static std::mutex mutex;
251 static std::set<std::pair<uint32_t, uint64_t>> seen;
252 bool first = false;
253 {
254 const std::lock_guard lock(mutex);
255 first = seen.insert({type, static_cast<uint64_t>(size)}).second;
256 }
257 if (first) {
258 rt::startup_trace::event(
259 "xmem_transfer",
260 "\"api\":\"XMemTransferMemory\",\"type\":" + std::to_string(type) +
261 ",\"size\":" + std::to_string(static_cast<uint64_t>(size)) + ",\"caller\":" +
262 rt::startup_trace::quote(
263 rt::startup_trace::where(reinterpret_cast<uint64_t>(_ReturnAddress()))));
264 }
265 }
266 return TRUE;
267}
268
222// kernel32!VirtualAlloc as a title calls it. The console's allocation type has two bits of its own, MEM_GRAPHICS 269// kernel32!VirtualAlloc as a title calls it. The console's allocation type has two bits of its own, MEM_GRAPHICS
223// (0x10000000, memory the GPU reads) and MEM_TITLE (0x40000000, the title's own heap). On a PC the host rejects both 270// (0x10000000, memory the GPU reads) and MEM_TITLE (0x40000000, the title's own heap). On a PC the host rejects both
224// with ERROR_INVALID_PARAMETER (measured on Windows 11 26200: VirtualAlloc(NULL, 64 KiB, MEM_COMMIT | MEM_RESERVE | 271// with ERROR_INVALID_PARAMETER (measured on Windows 11 26200: VirtualAlloc(NULL, 64 KiB, MEM_COMMIT | MEM_RESERVE |
@@ -258,4 +305,5 @@ SHIM_TABLE(xmem,
258 {"xmem.dll", "XMemVirtualAlloc", reinterpret_cast<void*>(&XMemVirtualAlloc)}, 305 {"xmem.dll", "XMemVirtualAlloc", reinterpret_cast<void*>(&XMemVirtualAlloc)},
259 {"xmem.dll", "XMemGetWriteWatch", reinterpret_cast<void*>(&XMemGetWriteWatch)}, 306 {"xmem.dll", "XMemGetWriteWatch", reinterpret_cast<void*>(&XMemGetWriteWatch)},
260 {"xmem.dll", "XMemResetWriteWatch", reinterpret_cast<void*>(&XMemResetWriteWatch)}, 307 {"xmem.dll", "XMemResetWriteWatch", reinterpret_cast<void*>(&XMemResetWriteWatch)},
308 {"xmem.dll", "XMemTransferMemory", reinterpret_cast<void*>(&XMemTransferMemory)},
261); 309);