From fbc96feef8fcd92d6b11a8ba98854b21e25995ed Mon Sep 17 00:00:00 2001 From: QemmHD <290605352+QemmHD@users.noreply.github.com> Date: Tue, 24 Nov 2026 19:43:50 -0500 Subject: [PATCH] d3d12_xs: console device slot 74 registers a page pool and writes its handle (RT-SLOT74-POOL-HANDLE) Since #385 the title's call of device slot 74, (pointer, u32, pointer), returns S_OK and writes nothing, so the title keeps whatever its handle word held. VIctory (Marvisak/VIctory, GPL-3, crates/victory-gpu/src/resource.rs, register_page_pool, device method 0x250) reads the method as RegisterPagePool(address, page count, handle out) for a pool of 64 KiB pages, and writes a handle (index + 1) after refusing a null handle pointer, a page count of 0 or an address that is not 64 KiB aligned with E_INVALIDARG. This is written anew from that reading. For a console caller, proxy_slot74 now records {address, page count} in a mutex-guarded registry and writes a non-zero 64-bit handle from a counter (never reused) through the out pointer. It answers E_INVALIDARG and writes nothing for VIctory's three cases and also for an address of 0 and a handle pointer that VirtualQuery does not show as committed, writable memory (a write there would fault, so the console method cannot have succeeded on it either). The first call is logged once. console_page_pool (d3d12_xs_device.h) reads a record back; nothing consumes a handle yet, as queue slot 31 (CopyPageMappingsBatchX, #968) takes the pool's address directly, so the registry stays in d3d12_xs rather than xmem. A host caller still gets the PC slot 74 (ID3D12Device9::ShaderCacheControl). VIctory reads slot 75 (0x258) as UnregisterPagePool(handle); that is left as a TODO, since slot 75 is the PC CreateCommandQueue1 here. Tested: xs_proxy_mock_test check_slot74 (two calls give distinct non-zero handles and their records; a null, read-only, 0-page, 0-address and misaligned call answer E_INVALIDARG, write nothing and take no handle; a host caller still reaches ShaderCacheControl at level 9 and E_NOTIMPL at level 0), plain and with LEONIDA_VK_VALIDATION=1. With main's stand-in put back the test fails at the handle check. Full ctest 309/310 (only d3d12_depth_resolve fails, as on main); the docs/cmake/make_game consistency set passes (578 passed, 5 skipped). RTX 4070 SUPER, Windows 11. No retail run. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SR1Fv7eQNgZjM6miWPKjHf --- docs/d3d12_vulkan.md | 2 +- runtime/shims/d3d12_xs_device.cpp | 69 ++++++++++++++++++++++++++--- runtime/shims/d3d12_xs_device.h | 10 ++++- runtime/test/xs_proxy_mock_test.cpp | 40 ++++++++++++----- 4 files changed, 102 insertions(+), 19 deletions(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index be932f1e9c..13f4498fff 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -3615,7 +3615,7 @@ to be recovered from call sites (or the NDA GDK headers, if a developer has acce | Device slots 48/73/78/79/80 | Public HLE `Freminet`/`ScarlettX` (third-party, not Microsoft documentation) | `d3d12_xs_device.cpp` routes `CreateCommandQueueX` (48), `CreateCommandListX` (73), `SetFrameIntervalX` (78), `ScheduleFrameEventX` (79) and `WaitFrameEventX` (80) before it indexes the PC vtable. Queue and list creation use the existing create paths. The frame-event calls are a per-device token counter: `WaitFrameEventX` returns at once with a fresh non-zero token, because there is no vblank source before a presenter exists. The five indices can be moved with `LEONIDA_XS_QUEUE_X_SLOT`, `LEONIDA_XS_LIST_X_SLOT`, `LEONIDA_XS_FRAME_INTERVAL_SLOT`, `LEONIDA_XS_SCHEDULE_EVENT_SLOT` and `LEONIDA_XS_WAIT_EVENT_SLOT` (3..127; a value that names a slot the proxy already handles keeps that slot's handler); a trace that disagrees wins. | | Indirect argument type 105 | The retail title's startup creates five one-argument command signatures (device slot 41) with no root signature; one has type 105 and a 12-byte stride. Its two `ExecuteIndirect` sites, read statically, set the compute state first, as the `DISPATCH` sites do, and pass one command from the same argument buffer, on a second command list | `d3d12_xs_device.cpp` creates it as `DISPATCH` (`D3D12_DISPATCH_ARGUMENTS`). What the console does differently for it is **Open**. | | Device slot 61 | The retail title's boot under `gta6rt --run` (stop 20 of the RT-5 measurements in #6910): called as `(desc, dest handle)` once per sampler | `d3d12_xs_device.cpp` routes it to the PC `CreateSampler` (slot 22) with the description's PC `D3D12_SAMPLER_DESC` prefix, for a caller in an image the guest loader mapped; the PC slot 61 is `CreateMetaCommand`, which any other caller (a host test driving the device through the proxy) still gets. The layout of the description beyond the PC fields is **Open**. | -| Device slot 74 | The retail title's boot under `gta6rt --run`: called as `(pointer, u32, pointer)` | **Stand-in.** `d3d12_xs_device.cpp` answers `S_OK` and does nothing, for a caller in an image the guest loader mapped; the PC slot 74 is `ShaderCacheControl`, which the call does not fit and which any other caller still gets. What the console method is, and what it should do, is **Open**. | +| Device slot 74 | The retail title's boot under `gta6rt --run`: called as `(pointer, u32, pointer)`; VIctory (Marvisak/VIctory, GPL-3, `crates/victory-gpu/src/resource.rs`, `register_page_pool`, device method 0x250) reads it as `RegisterPagePool(address, page count, handle out)` | **Implemented (registration only).** For a caller in an image the guest loader mapped, `d3d12_xs_device.cpp` records the pool's address and page count and writes a non-zero 64-bit handle, unique to the process, through the out pointer (`console_page_pool` reads the record back). As in VIctory, a null out pointer, a page count of 0 or an address that is not 64 KiB aligned answers `E_INVALIDARG` and writes nothing; so do an address of 0 and an out pointer that is not committed, writable memory. Any other caller still gets the PC slot 74 (`ShaderCacheControl`). Nothing reads a handle yet: queue slot 31 (`CopyPageMappingsBatchX`) takes the pool's address directly. **Open:** VIctory reads the title's slot 75 (0x258) as `UnregisterPagePool(handle)`; here slot 75 is still the PC `CreateCommandQueue1` for every caller (`xs_proxy_mock_test`, `check_slot74`). | | Command-allocator slot 9 | The retail title's boot under `gta6rt --run`: called past the 9-slot PC allocator, as `(function pointer, context, ...)`; `SetAllocatorCallbackX` is the name the commit that added it uses | **Stand-in.** The allocator proxy records the callback and its context (`allocator_proxy_callback`, `allocator_proxy_callback_context`) and answers `S_OK`. **Nothing calls the callback**: the Vulkan layer backs command buffers itself. Whether the title depends on being called back is **Open**. | | Device slot 81, queue `PresentX`/`SuspendX`/`ResumeX` | XCL (GPL-2.0-only, third-party): `GetFrameStatisticsX` at device slot 81, `PresentX` at queue slot 30 (older runtime layout) or 32 (newer); `SuspendX`/`ResumeX` slots unmeasured | `set_presentation_handler` forwards 78-81 and the queue routes to a presenter (`attach_xbox_presentation`; `gta6rt` creates no `XboxPresentation` yet). Without one, slot 81 answers `S_FALSE` with a zero count (no statistics yet) and the queue slots stay refused. Moved with `LEONIDA_XS_FRAME_STATISTICS_SLOT` (3..127) and `LEONIDA_XS_PRESENT_X_SLOT` (default 30); `SuspendX` and `ResumeX` are routed only when `LEONIDA_XS_SUSPEND_X_SLOT` / `LEONIDA_XS_RESUME_X_SLOT` are set (queue slots 19..127; two routes on one slot are refused with a log). | | VRS | Public `D3D12_SHADING_RATE` / `D3D12_AXIS_SHADING_RATE` | `VK_KHR_fragment_shading_rate`; `RSSetShadingRate`/image-based rate map to `vkCmdSetFragmentShadingRateKHR`. | diff --git a/runtime/shims/d3d12_xs_device.cpp b/runtime/shims/d3d12_xs_device.cpp index 88593951a0..0e5b7135be 100644 --- a/runtime/shims/d3d12_xs_device.cpp +++ b/runtime/shims/d3d12_xs_device.cpp @@ -263,7 +263,7 @@ const char* slot_name(unsigned slot) { case 67: return "Xbox DeserializeComputePipelineStateX"; case 70: return "Xbox shader-resource view from raw words"; case 73: return "Xbox CreateCommandListX -> PC CreateCommandList"; - case 74: return "Xbox (pointer, u32, pointer) stand-in"; + case 74: return "Xbox RegisterPagePool (address, pages, handle out)"; case 75: return "CreateCommandQueue1 -> queue proxy"; case 76: return "Xbox resource creation"; case 127: return "Xbox CreateCommittedResource3"; @@ -2604,16 +2604,62 @@ bool route_xbox_device_slot(DeviceProxy* self, unsigned slot, std::uint64_t a1, // Slot 74: the retail title calls it once with (pointer, u32, pointer) (kapitche, issue 7231). The PC slot 74 is // ID3D12Device9::ShaderCacheControl(kinds, control), which reads the first word as a flags value and returns -// E_INVALIDARG, and the title then stops on an int3. The words are not measured: nothing is written through the -// pointers, the PC device is not called, and the call succeeds. Only for the title's calls (set_console_caller, as -// slot 61): a host caller of ID3D12Device9::ShaderCacheControl through the proxy gets the PC method. +// E_INVALIDARG, and the title then stops on an int3. Only for the title's calls (set_console_caller, as slot 61): a host +// caller of ID3D12Device9::ShaderCacheControl through the proxy gets the PC method. +// +// For the title it is RegisterPagePool(address, page count, handle out), as VIctory reads it (Marvisak/VIctory, +// GPL-3, crates/victory-gpu/src/resource.rs, register_page_pool, device method 0x250 = slot 74): a pool of 64 KiB +// pages the title later maps into resources by page index. This is written anew from that reading. As VIctory does, +// a null handle pointer, a page count of 0 or an address that is not 64 KiB aligned is refused with E_INVALIDARG and +// nothing is written; an address of 0 and a handle pointer this process cannot write are refused the same way, since +// the console method could not succeed on either and writing would fault. Otherwise the pool's {address, page count} +// is recorded and a non-zero handle unique to this process is written as a 64-bit word (VIctory writes a usize). +// Queue slot 31 (CopyPageMappingsBatchX) takes the pool's address and page count directly, so nothing looks a handle +// up yet; console_page_pool (d3d12_xs_device.h) reads the record. +// TODO(RT-SLOT74-POOL-HANDLE): VIctory reads the title's device method 0x258 (slot 75) as UnregisterPagePool(handle), +// which frees the record; here slot 75 is still the PC ID3D12Device9::CreateCommandQueue1 for every caller. +constexpr std::uint64_t kConsolePoolPageSize = 64 * 1024; + +struct ConsolePagePool { + std::uint64_t handle = 0; + std::uint64_t address = 0; + std::uint32_t pages = 0; +}; + +std::mutex g_page_pools_mutex; +std::vector g_page_pools; // under g_page_pools_mutex +std::uint64_t g_next_page_pool_handle = 1; // under g_page_pools_mutex; 0 is never a handle + +// True when the 8 bytes at `address` are committed memory this process can write, without touching them. +bool handle_word_writable(std::uint64_t address) { + if (address == 0 || (address & 7) != 0) return false; // a handle word is 8-byte aligned, so it sits in one page + MEMORY_BASIC_INFORMATION mbi{}; + if (!VirtualQuery(reinterpret_cast(address), &mbi, sizeof(mbi)) || mbi.State != MEM_COMMIT) + return false; + if (mbi.Protect & (PAGE_GUARD | PAGE_NOACCESS)) return false; + const DWORD protect = mbi.Protect & 0xffu; + return protect == PAGE_READWRITE || protect == PAGE_WRITECOPY || protect == PAGE_EXECUTE_READWRITE || + protect == PAGE_EXECUTE_WRITECOPY; +} + std::uint64_t proxy_slot74(std::uint64_t a1, std::uint64_t a2, std::uint64_t a3) { + const std::uint64_t address = a1; + const auto pages = static_cast(a2); static std::atomic logged{false}; bool expected = false; if (logged.compare_exchange_strong(expected, true, std::memory_order_relaxed)) - std::fprintf(stderr, "[rt] d3d12_xs: slot 74 (pointer, u32, pointer) returns S_OK; words 0x%llx 0x%llx 0x%llx\n", - static_cast(a1), static_cast(a2), + std::fprintf(stderr, "[rt] d3d12_xs: slot 74 (RegisterPagePool) first call: pool 0x%llx of %u pages, handle " + "out 0x%llx\n", static_cast(address), pages, static_cast(a3)); + if (address == 0 || address % kConsolePoolPageSize != 0 || pages == 0 || !handle_word_writable(a3)) + return hr_to_u64(E_INVALIDARG); + std::uint64_t handle = 0; + { + std::lock_guard lock(g_page_pools_mutex); + handle = g_next_page_pool_handle++; + g_page_pools.push_back(ConsolePagePool{handle, address, pages}); + } + *reinterpret_cast(a3) = handle; return hr_to_u64(S_OK); } @@ -3329,6 +3375,17 @@ bool heap_slot_overridden(unsigned slot) { return slot <= 2 || slot == 7 || slot == 9 || slot == 10; } +bool console_page_pool(std::uint64_t handle, std::uint64_t* address, std::uint32_t* pages) { + std::lock_guard lock(g_page_pools_mutex); + for (const ConsolePagePool& pool : g_page_pools) { + if (pool.handle != handle) continue; + if (address) *address = pool.address; + if (pages) *pages = pool.pages; + return true; + } + return false; +} + void set_console_caller(ConsoleCaller classify) { g_console_caller.store(classify, std::memory_order_release); } diff --git a/runtime/shims/d3d12_xs_device.h b/runtime/shims/d3d12_xs_device.h index c9b62b3a92..3899fddaf7 100644 --- a/runtime/shims/d3d12_xs_device.h +++ b/runtime/shims/d3d12_xs_device.h @@ -19,8 +19,10 @@ // and a GPU address, with no resource (retail boot chain stop 14). The PC slot 70 is CreatePlacedResource1. // The words are not measured, so the destination gets the PC null view (PC CreateShaderResourceView // with no resource) and the call returns. -// slot 74 (pointer, u32, pointer): not measured, returns S_OK. The PC slot 74 is ShaderCacheControl(kinds, control), -// which reads the first word as flags and returns E_INVALIDARG. Nothing is written, the PC device is not called. +// slot 74 RegisterPagePool(address, pages, handle out), as VIctory reads it, for a console caller: records the pool +// and writes a non-zero handle (console_page_pool); E_INVALIDARG and no write for a null or unwritable +// handle pointer, 0 pages or an address that is 0 or not 64 KiB aligned. The PC slot 74 is +// ShaderCacheControl(kinds, control), which any other caller still gets. // slot 76 resource creation -> PC CreateCommittedResource (PC slot 27), with the Xbox layout word ignored. // slot 127 CreateCommittedResource3 (heap, flags, desc1, layout, clear, session, castable count/list, riid, out) -> // PC ID3D12Device10::CreateCommittedResource3 (PC slot 76), with the Xbox layout word replaced and the @@ -206,6 +208,10 @@ bool device_slot_overridden(unsigned slot); using ConsoleCaller = bool (*)(std::uint64_t address); void set_console_caller(ConsoleCaller classify); +// The page pools the title registered through device slot 74 (RegisterPagePool, for a console caller): true, with the +// pool's first address and page count, for a handle slot 74 wrote; false for any other word, 0 included. +bool console_page_pool(std::uint64_t handle, std::uint64_t* address, std::uint32_t* pages); + // Queue and fence proxies created by device slots 8/75 and 36. The real_* accessors return the wrapped object // (no reference added), or null when the pointer is not such a proxy. bool is_queue_proxy(const void* queue); diff --git a/runtime/test/xs_proxy_mock_test.cpp b/runtime/test/xs_proxy_mock_test.cpp index 1062d02ffd..47cd2cd024 100644 --- a/runtime/test/xs_proxy_mock_test.cpp +++ b/runtime/test/xs_proxy_mock_test.cpp @@ -1632,27 +1632,46 @@ int check_slot70() { return 0; } -// ---- 8b. Device slot 74: (pointer, u32, pointer) succeeds and touches nothing, never the PC ShaderCacheControl ---- +// ---- 8b. Device slot 74: the title's RegisterPagePool writes a handle; never the PC ShaderCacheControl ---- int check_slot74() { // Level 9 has the PC slot 74 (ShaderCacheControl); level 0 does not (44 PC slots). Neither may reach it. + constexpr std::uint64_t kPool = 0x7ff600000000ull; // 64 KiB aligned; the pool itself is never read or written for (const int level : {9, 0}) { Mock* mock = nullptr; void* device = make_device(mock, level, true); EXPECT(device && xs::device_slot_overridden(74)); xs::reset_device_proxy_stats(); - std::uint32_t first[4] = {0x11111111, 0x22222222, 0x33333333, 0x44444444}; - std::uint32_t second[4] = {0x55555555, 0x66666666, 0x77777777, 0x88888888}; - const std::uint32_t first_before[4] = {first[0], first[1], first[2], first[3]}; - const std::uint32_t second_before[4] = {second[0], second[1], second[2], second[3]}; - // The title's call (a caller the classifier takes for a guest image): the stand-in. + // The title's call (a caller the classifier takes for a guest image): RegisterPagePool(address, pages, out). xs::set_console_caller([](std::uint64_t) { return true; }); - EXPECT(hr_of(raw(device, 74, word(first), 7, word(second))) == S_OK); - EXPECT(mock->calls[74] == 0 && xs::device_slot_hits(74) == 1); - EXPECT(std::memcmp(first, first_before, sizeof(first)) == 0 && std::memcmp(second, second_before, sizeof(second)) == 0); + std::uint64_t first = 0, second = 0; + EXPECT(hr_of(raw(device, 74, kPool, 16, word(&first))) == S_OK); + EXPECT(hr_of(raw(device, 74, kPool + 0x10000, 1, word(&second))) == S_OK); + EXPECT(first != 0 && second != 0 && first != second); + std::uint64_t address = 0; + std::uint32_t pages = 0; + EXPECT(xs::console_page_pool(first, &address, &pages) && address == kPool && pages == 16); + EXPECT(xs::console_page_pool(second, &address, &pages) && address == kPool + 0x10000 && pages == 1); + EXPECT(!xs::console_page_pool(0, nullptr, nullptr)); + EXPECT(mock->calls[74] == 0 && xs::device_slot_hits(74) == 2); + // Refused with E_INVALIDARG, nothing written and nothing recorded: no handle pointer, a handle pointer this + // process cannot write (a read-only page), no pages, an address of 0 or not 64 KiB aligned. + void* const read_only = VirtualAlloc(nullptr, 4096, MEM_COMMIT | MEM_RESERVE, PAGE_READONLY); + EXPECT(read_only); + constexpr std::uint64_t kUntouched = 0xA5A5A5A5A5A5A5A5ull; + std::uint64_t out = kUntouched; + EXPECT(hr_of(raw(device, 74, kPool, 16, 0)) == E_INVALIDARG); + EXPECT(hr_of(raw(device, 74, kPool, 16, word(read_only))) == E_INVALIDARG); + EXPECT(*static_cast(read_only) == 0); + EXPECT(hr_of(raw(device, 74, kPool, 0, word(&out))) == E_INVALIDARG && out == kUntouched); + EXPECT(hr_of(raw(device, 74, 0, 16, word(&out))) == E_INVALIDARG && out == kUntouched); + EXPECT(hr_of(raw(device, 74, kPool + 0x1000, 16, word(&out))) == E_INVALIDARG && out == kUntouched); + EXPECT(VirtualFree(read_only, 0, MEM_RELEASE) != FALSE); + EXPECT(!xs::console_page_pool(second + 1, nullptr, nullptr)); // the refusals took no handle + EXPECT(mock->calls[74] == 0); // A host caller, under a classifier and with none installed: the PC slot 74 (ShaderCacheControl) with the // same words where the PC device has it, and the refusal past its table where it does not (found on Windows - // in the review of this change: the stand-in used to take a host's ShaderCacheControl too). + // in the review of #385: the stand-in used to take a host's ShaderCacheControl too). Nothing is registered. for (const bool installed : {true, false}) { if (installed) xs::set_console_caller([](std::uint64_t) { return false; }); @@ -1667,6 +1686,7 @@ int check_slot74() { EXPECT(hr == E_NOTIMPL && mock->calls[74] == before); } } + EXPECT(!xs::console_page_pool(second + 1, nullptr, nullptr)); xs::set_console_caller(nullptr); EXPECT(release(device) == 0 && mock->refs == 0); }