d3d12_xs: queue slot 31 is CopyPageMappingsBatchX, mapped through xmem (RT-QUEUE31)

The retail title calls its command queue's slot 31 (game_scarlett_master.exe+0xf2cc7f) with a batch count, 24-byte
batches {destination, range count, ranges}, the base and page count of its physical-page pool, and 0. Each 12-byte
range {kind, start, count} maps pool pages at the destination: 0 a run, 1 one page repeated, 2 unmapped, 3 unchanged.
main refused the slot with E_NOTIMPL; the title does not test the result and goes on to read the destinations.

Each range now reaches rt::xmem::copy_page_mappings_fn, which maps the physical page behind every source page at its
destination page (a second view of the same section, so both addresses are one memory) and unmaps where the source has
nothing or the kind is 2. The hook is set by xmem.cpp; tests that compile the proxy alone keep the old refusal.

Tests: xmem_test (a run, one page repeated, an unmapped source page, a null source, an overlapping range, bad
addresses) and xs_proxy_mock_test (the four kinds, a range past the pool, an unknown kind, xmem refusing, no xmem).

AI-assisted.
This commit is contained in:
CLHelper
2026-11-24 15:56:24 +00:00
parent a28927849a
commit f2268abead
5 changed files with 280 additions and 1 deletions

View File

@ -25,6 +25,7 @@
// behind the device before a presenter exists, so WaitFrameEventX returns at once with a fresh non-zero token.
#include "d3d12_xs_device.h"
#include "d3d12_trace.h"
#include "xmem.h" // rt::xmem::copy_page_mappings_fn for queue slot 31
#include "../src/shader_library.h" // leonida::fspx_payload_from_parts for slot 67
#include <windows.h>
@ -712,6 +713,8 @@ static_assert(kQueueSlots * sizeof(void*) <= 4096 && kFenceSlots * sizeof(void*)
"a table must fit in the first page of its reservation");
constexpr unsigned kQueueSignal = 14, kQueueWait = 15; // (ID3D12Fence*, UINT64)
// CopyPageMappingsBatchX(count, batches, pool, pool pages, flags), past the public chain (queue_copy_page_mappings).
constexpr unsigned kQueueCopyPageMappingsBatchX = 31;
struct ChildProxy {
void** vtable; // offset 0: the caller's ID3D12CommandQueue* / ID3D12Fence* points here
@ -1228,6 +1231,82 @@ LONG CALLBACK table_overrun_handler(EXCEPTION_POINTERS* info) {
return EXCEPTION_CONTINUE_SEARCH; // diagnostics only: the fault goes on to the title's handlers or the crash
}
// ---- Queue slot 31: CopyPageMappingsBatchX ----
//
// Measured on the retail executable (static): game_scarlett_master.exe+0xf2cc7f calls the queue's slot 31
// (`call [vtable + 0xf8]`) with EDX the batch count, R8 the batches, R9 the base of its physical-page pool, the pool's
// size in 64 KiB pages as the fifth argument and 0 as the sixth; the result is not tested. It fills a batch at +0xf2ca74
// as {UINT64 destination; UINT32 range count; const range* ranges} (24 bytes) and a range as {UINT32 kind, start,
// count} (12 bytes). The kinds: 0 maps pool pages start, start + 1, ... at the destination; 1 maps pool page `start` at
// every destination page; 2 leaves the destination unmapped; 3 leaves it as it is. Each range moves the destination on
// by count pages, whatever its kind. The same call shape is in VIctory's queue (copy_page_mappings, "newer" layout,
// where every slot from 14 on is two later than in older titles). In a boot the pool size matched the title's own
// XMemAllocatePhysicalPages count exactly. The console maps GPU page tables; here each range becomes xmem's
// copy_page_mappings_fn, which maps the physical page behind each pool page at its destination, the aliasing two
// XMemMapPhysicalPages calls with one page give.
//
// Without xmem (tests that compile this proxy alone) the slot is refused as before. A range outside the pool, of an
// unknown kind, or that xmem refuses is skipped and logged once; the call then returns E_INVALIDARG.
struct ConsolePageRange {
std::uint32_t kind, start, count;
};
struct ConsolePageBatch {
std::uint64_t destination;
std::uint32_t count;
const ConsolePageRange* ranges;
};
static_assert(sizeof(ConsolePageRange) == 12 && sizeof(ConsolePageBatch) == 24, "the title's page batch layout");
constexpr std::uint64_t kConsolePageSize = 0x10000;
bool copy_console_page_range(const ConsolePageRange& range, std::uint64_t destination, std::uint64_t pool,
std::uint32_t pool_pages) {
void* const at = reinterpret_cast<void*>(destination);
const void* const from = reinterpret_cast<const void*>(pool + std::uint64_t{range.start} * kConsolePageSize);
switch (range.kind) {
case 0:
return std::uint64_t{range.start} + range.count <= pool_pages &&
rt::xmem::copy_page_mappings_fn(at, from, range.count, false);
case 1:
return range.start < pool_pages && rt::xmem::copy_page_mappings_fn(at, from, range.count, true);
case 2:
return rt::xmem::copy_page_mappings_fn(at, nullptr, range.count, false);
case 3:
return true;
default:
return false;
}
}
HRESULT queue_copy_page_mappings(std::uint64_t count_word, std::uint64_t batches_word, std::uint64_t pool,
std::uint64_t pool_pages_word) {
const auto count = static_cast<std::uint32_t>(count_word);
const auto pool_pages = static_cast<std::uint32_t>(pool_pages_word);
const auto* const batches = reinterpret_cast<const ConsolePageBatch*>(batches_word);
if (count && !batches) return E_INVALIDARG;
static std::atomic<bool> first_call{true}, first_refusal{true};
if (first_call.exchange(false, std::memory_order_relaxed))
std::fprintf(stderr, "[rt] d3d12_xs: queue slot 31 (CopyPageMappingsBatchX) first call: %u batch(es), pool "
"0x%llx of %u pages\n", count, static_cast<unsigned long long>(pool), pool_pages);
bool all = true;
for (std::uint32_t b = 0; b < count; ++b) {
if (!batches[b].ranges) continue;
std::uint64_t at = batches[b].destination;
for (std::uint32_t r = 0; r < batches[b].count; ++r) {
const ConsolePageRange& range = batches[b].ranges[r];
if (!copy_console_page_range(range, at, pool, pool_pages)) {
all = false;
if (first_refusal.exchange(false, std::memory_order_relaxed))
std::fprintf(stderr, "[rt] d3d12_xs: queue slot 31: range kind %u, pool pages %u+%u at 0x%llx not "
"mapped (pool of %u pages, error %lu); logged once\n", range.kind, range.start,
range.count, static_cast<unsigned long long>(at), pool_pages, GetLastError());
}
at += std::uint64_t{range.count} * kConsolePageSize;
}
}
return all ? S_OK : E_INVALIDARG;
}
// PresentX is at queue slot 30 in an older runtime layout and 32 in a newer one (public corroboration, XCL; see
// docs/d3d12_vulkan.md). 30 is the default and LEONIDA_XS_PRESENT_X_SLOT sets the other. SuspendX and ResumeX have no
// default: 0 means not routed, so they answer only when their variable is set.
@ -1282,6 +1361,12 @@ std::uint64_t WINAPI child_slot(ChildProxy* self, std::uint64_t a1, std::uint64_
} else {
note_child_slot<K>(Slot);
if (Slot >= self->slot_count) {
if constexpr (K == ChildKind::queue && Slot == kQueueCopyPageMappingsBatchX) {
if (rt::xmem::copy_page_mappings_fn) {
d3d12_trace::record(child_trace_name<K>(Slot));
return hr_to_u64(queue_copy_page_mappings(a1, a2, a3, a4));
}
}
if constexpr (K == ChildKind::queue) {
const XboxQueueSlotRoutes& qroutes = xbox_queue_slot_routes();
HRESULT hr = S_OK;

View File

@ -731,6 +731,48 @@ BOOL WINAPI XMemMapPhysicalPagesScatter(void* const* addresses, uint32_t page_co
return TRUE;
}
// rt::xmem::copy_page_mappings_fn (xmem.h), for the console queue's CopyPageMappingsBatchX: each destination page gets
// a view of the physical page mapped at its source page, so both addresses are one memory, as two XMemMapPhysicalPages
// calls with the same page make them. Every source page is looked up before any destination page changes, so a source
// range that overlaps the destination maps what was there before the call.
bool copy_page_mappings(void* destination, const void* source, uint64_t pages, bool repeat_first) {
const uintptr_t to = reinterpret_cast<uintptr_t>(destination);
const uintptr_t from = reinterpret_cast<uintptr_t>(source);
constexpr uintptr_t kMax = (std::numeric_limits<uintptr_t>::max)();
if (!aligned_page_address(to) || !aligned_page_address(from) || pages > kMax / kPhysicalPageSize ||
to > kMax - pages * kPhysicalPageSize || (!repeat_first && from > kMax - pages * kPhysicalPageSize)) {
SetLastError(ERROR_INVALID_PARAMETER);
return false;
}
if (!placeholder_api().available()) {
SetLastError(ERROR_NOT_SUPPORTED);
return false;
}
const std::lock_guard lock(g_phys_mutex);
if (!source) {
for (uint64_t i = 0; i < pages; ++i) {
if (!unmap_page(to + i * kPhysicalPageSize)) return false;
}
return true;
}
const Window* const window = find_window(to, pages * kPhysicalPageSize);
if (!window) {
SetLastError(ERROR_INVALID_ADDRESS);
return false;
}
std::vector<uint64_t> found(static_cast<size_t>(pages), 0);
for (uint64_t i = 0; i < pages; ++i) {
const auto it = g_mapped.find(from + (repeat_first ? 0 : i) * kPhysicalPageSize);
if (it != g_mapped.end()) found[static_cast<size_t>(i)] = it->second.page;
}
const DWORD protect = view_protect(*window);
for (uint64_t i = 0; i < pages; ++i) {
if (!map_page(to + i * kPhysicalPageSize, found[static_cast<size_t>(i)], protect)) return false;
}
return true;
}
[[maybe_unused]] const bool g_copy_page_mappings_set =(rt::xmem::copy_page_mappings_fn = &copy_page_mappings, true);
UINT WINAPI XMemGetWriteWatch(DWORD flags, void* base_address, SIZE_T region_size, void** addresses, ULONG_PTR* count,
ULONG* granularity) {
return GetWriteWatch(flags, base_address, region_size, addresses, count, granularity);

View File

@ -8,4 +8,12 @@ namespace rt::xmem {
bool commit_physical_range(const void* address, std::size_t size);
bool commit_physical_pages(const std::uint64_t* page_array, std::size_t count);
// Console queue slot 31, CopyPageMappingsBatchX (d3d12_xs_device.cpp): maps at `destination` the physical pages that
// are mapped at `source` now, `pages` 64 KiB pages one after another, or the one page at `source` every time with
// `repeat_first`. A source page with nothing mapped leaves its destination page unmapped; a null `source` unmaps them
// all. False, with the last error set, for an unaligned address or a destination outside a physical-page window.
// Set by xmem.cpp when it is linked: the command-object proxy is also compiled into tests without it.
using CopyPageMappingsFn = bool (*)(void* destination, const void* source, std::uint64_t pages, bool repeat_first);
inline CopyPageMappingsFn copy_page_mappings_fn = nullptr;
}

View File

@ -444,6 +444,66 @@ void test_physical_pages(const PhysicalApi& api) {
// The windows were split into one placeholder per page; the process exit releases them.
}
// rt::xmem::copy_page_mappings_fn, behind console queue slot 31 (CopyPageMappingsBatchX): a destination page maps the
// physical page that is mapped at the source page, so both addresses are one memory, as two XMemMapPhysicalPages calls
// with the same page would make them.
void test_copy_page_mappings(const PhysicalApi& api) {
const auto copy = rt::xmem::copy_page_mappings_fn;
check(copy != nullptr, "xmem.cpp sets the copy_page_mappings hook");
if (!copy) return;
constexpr uint64_t kPool = 4, kHeap = 8;
auto* const pool = static_cast<uint8_t*>(api.virtual_alloc(
nullptr, kPool * kPage64k, MEM_RESERVE | kXmem64kPages, 0x23, PAGE_READWRITE | PAGE_GRAPHICS_READWRITE));
auto* const heap = static_cast<uint8_t*>(api.virtual_alloc(
nullptr, kHeap * kPage64k, MEM_RESERVE | kXmem64kPages, 0x23, PAGE_READWRITE | PAGE_GRAPHICS_READWRITE));
check(pool && heap, "XMemVirtualAlloc reserves a pool window and a heap window");
if (!pool || !heap) return;
uint64_t pages[kPool]{};
uint64_t count = kPool;
check(api.allocate(0x280, &count, pages) == TRUE && count == kPool, "pages for the pool");
// Pool pages 0..2 mapped, pool page 3 left unmapped.
check(api.map(pool, 3, pages) == pool, "the pool maps three pages");
for (uint64_t i = 0; i < 3; ++i) fill_page(pool + i * kPage64k, 0x7100u + static_cast<uint32_t>(i));
// A run: heap pages 0 and 1 show pool pages 1 and 2, and are the same memory.
check(copy(heap, pool + kPage64k, 2, false), "a run of pool pages maps into the heap");
check(page_holds(heap, 0x7101u) && page_holds(heap + kPage64k, 0x7102u), "the heap pages show pool pages 1 and 2");
static_cast<volatile uint32_t*>(static_cast<void*>(heap + kPage64k))[1] = 0xc0ffee31;
check(read_u32(pool + 2 * kPage64k, 4) == 0xc0ffee31, "a write through the heap is seen in the pool: one memory");
// One page repeated.
check(copy(heap + 2 * kPage64k, pool, 3, true), "one pool page maps at three heap pages");
check(page_holds(heap + 2 * kPage64k, 0x7100u) && page_holds(heap + 4 * kPage64k, 0x7100u),
"every repeated page shows pool page 0");
// A pool page with nothing mapped leaves its destination unmapped; so does the null source.
check(copy(heap + 5 * kPage64k, pool + 2 * kPage64k, 2, false), "a run that ends in an unmapped pool page maps");
check(page_holds(heap + 5 * kPage64k, 0x7102u) && reserved_only(heap + 6 * kPage64k),
"the unmapped pool page leaves its heap page unmapped");
check(copy(heap, nullptr, 2, false), "a null source unmaps");
check(reserved_only(heap) && reserved_only(heap + kPage64k), "the heap pages are reserved again");
check(page_holds(pool + kPage64k, 0x7101u), "unmapping the heap leaves the pool's own mapping");
// Overlapping source and destination: every source page is read before any destination page changes.
check(copy(pool + kPage64k, pool, 2, false), "a run maps over an overlapping range");
check(page_holds(pool + kPage64k, 0x7100u) && page_holds(pool + 2 * kPage64k, 0x7101u),
"the overlapping run shows the pages from before it");
uint8_t outside = 0;
const auto outside_page =
reinterpret_cast<uint8_t*>((reinterpret_cast<uintptr_t>(&outside) + 0xffff) & ~uintptr_t{0xffff});
check(!copy(outside_page, pool, 1, false), "a destination outside any window fails");
SetLastError(ERROR_SUCCESS);
check(!copy(heap + 1, pool, 1, false) && GetLastError() == ERROR_INVALID_PARAMETER,
"a destination that is not 64 KiB-aligned fails");
check(!copy(heap, pool + 1, 1, false) && GetLastError() == ERROR_INVALID_PARAMETER,
"a source that is not 64 KiB-aligned fails");
api.map(heap, kHeap, nullptr);
api.map(pool, kPool, nullptr);
check(api.free_pages(kPool, pages) == TRUE, "the pool's pages are freed");
}
bool access_faults(volatile uint32_t* address, bool write) {
__try {
if (write) {
@ -968,6 +1028,7 @@ int main() {
if (tools_mem) check(tools_mem() == FALSE, "XMemIsToolsMemAvailable returns FALSE (no devkit tools memory)");
if (virtual_alloc && physical.allocate && physical.free_pages && physical.map && physical.scatter) {
test_physical_pages(physical);
test_copy_page_mappings(physical);
test_physical_pages_commit_on_touch(physical);
test_commit_physical_pages(physical);
test_first_touch_during_remap(physical);

View File

@ -12,6 +12,7 @@
// patching this executable's own import table before the first proxy table is built (one process each).
// Every device, queue, fence and resource here is a mock that records the calls it gets. No game data, no Xbox DLLs.
#include "../shims/d3d12_xs_device.h"
#include "../shims/xmem.h"
#include <d3d12.h>
#include <windows.h>
@ -1210,6 +1211,88 @@ int check_tail(bool want_guarded) {
return 0;
}
// ---- 9b. Queue slot 31: CopyPageMappingsBatchX(count, batches, pool, pool pages, flags) ----
//
// The batches as the retail title fills them (game_scarlett_master.exe+0xf2ca74): a 24-byte batch {destination, range
// count, ranges}, 12-byte ranges {kind, start, count}. Each range reaches xmem's hook once; xmem is not linked here, so
// a recording stand-in takes its place.
struct PageRange31 {
std::uint32_t kind, start, count;
};
struct PageBatch31 {
std::uint64_t destination;
std::uint32_t count;
const PageRange31* ranges;
};
static_assert(sizeof(PageRange31) == 12 && sizeof(PageBatch31) == 24, "the title's batch layout");
struct CopyCall {
std::uint64_t destination, source, pages;
bool repeat;
};
CopyCall g_copy_calls[8];
unsigned g_copy_count = 0;
bool g_copy_result = true;
bool record_copy(void* destination, const void* source, std::uint64_t pages, bool repeat_first) {
if (g_copy_count < std::size(g_copy_calls))
g_copy_calls[g_copy_count] = {word(destination), word(source), pages, repeat_first};
++g_copy_count;
return g_copy_result;
}
int check_queue_copy_page_mappings() {
Mock* mock = nullptr;
void* device = make_device(mock, 9, true);
EXPECT(device);
void* queue = make_queue(device);
EXPECT(queue);
Mock* const queue_mock = reinterpret_cast<Mock*>(xs::queue_proxy_real_queue(queue));
constexpr std::uint64_t kPage = 0x10000, kPool = 0x7000000000, kHeap = 0x9000000000;
const PageRange31 first[3] = {{0, 4, 2}, {3, 0, 5}, {1, 7, 3}}; // a run, left as is, one page repeated
const PageRange31 second[1] = {{2, 0, 6}}; // unmapped
const PageBatch31 batches[3] = {{kHeap, 3, first}, {kHeap + 0x100 * kPage, 1, second}, {kHeap, 0, nullptr}};
// Without xmem the slot stays refused, as before.
rt::xmem::copy_page_mappings_fn = nullptr;
EXPECT(hr_of(raw(queue, 31, 3, word(batches), kPool, 16, 0)) == E_NOTIMPL);
rt::xmem::copy_page_mappings_fn = &record_copy;
g_copy_count = 0;
EXPECT(hr_of(raw(queue, 31, 3, word(batches), kPool, 16, 0)) == S_OK && g_copy_count == 3);
EXPECT(g_copy_calls[0].destination == kHeap && g_copy_calls[0].source == kPool + 4 * kPage &&
g_copy_calls[0].pages == 2 && !g_copy_calls[0].repeat);
EXPECT(g_copy_calls[1].destination == kHeap + 7 * kPage && g_copy_calls[1].source == kPool + 7 * kPage &&
g_copy_calls[1].pages == 3 && g_copy_calls[1].repeat);
// Kind 3 maps nothing but still moves the destination on (5 pages, so the repeat lands at page 7); kind 2 unmaps.
EXPECT(g_copy_calls[2].destination == kHeap + 0x100 * kPage && g_copy_calls[2].source == 0 &&
g_copy_calls[2].pages == 6);
EXPECT(queue_mock->calls[31] == 0); // never forwarded to the PC queue
// A range past the pool, or of an unknown kind, is refused on its own; the others still map.
const PageRange31 bad[3] = {{0, 15, 2}, {7, 0, 1}, {1, 15, 4}}; // 15 + 2 > 16 pages; kind 7; page 15 is inside
const PageBatch31 bad_batch[1] = {{kHeap, 3, bad}};
g_copy_count = 0;
EXPECT(hr_of(raw(queue, 31, 1, word(bad_batch), kPool, 16, 0)) == E_INVALIDARG && g_copy_count == 1);
EXPECT(g_copy_calls[0].destination == kHeap + 3 * kPage && g_copy_calls[0].source == kPool + 15 * kPage &&
g_copy_calls[0].repeat);
// xmem refusing a range (no window there) makes the call fail too; no batches at all is fine.
g_copy_result = false;
EXPECT(hr_of(raw(queue, 31, 1, word(batches), kPool, 16, 0)) == E_INVALIDARG);
g_copy_result = true;
EXPECT(hr_of(raw(queue, 31, 0, 0, kPool, 16, 0)) == S_OK);
EXPECT(hr_of(raw(queue, 31, 2, 0, kPool, 16, 0)) == E_INVALIDARG);
rt::xmem::copy_page_mappings_fn = nullptr;
EXPECT(release(queue) == 0 && release(device) == 0 && mock->refs == 0);
std::printf("xs_proxy_mock: queue slot 31 (CopyPageMappingsBatchX) sends each range to xmem's hook (a run, one page "
"repeated, unmapped, left as is), refuses a range past the pool or of an unknown kind, and stays refused "
"without xmem\n");
return 0;
}
// ---- Guard-region setup failures: this executable's import table, patched before the first proxy table ----
std::atomic<int> g_injected{0};
@ -1718,7 +1801,7 @@ int main(int argc, char** argv) {
check_alloc_failures() || check_allocators() || check_xbox_slots() || check_console_command_list() ||
check_custom_heap_properties() ||
check_resource_allocation_info() || check_slot70() || check_committed_resource3_x() ||
check_presentation_wiring())
check_presentation_wiring() || check_queue_copy_page_mappings())
return 1;
std::printf("xs_proxy_mock_test: PASS (%d allocations refused on purpose)\n", g_failed_news);
return 0;