From e56789a345aebd0e99ff1f3b4c0fe0e3956b5b5a Mon Sep 17 00:00:00 2001 From: DadaCap <167002618+DadaCap@users.noreply.github.com> Date: Tue, 24 Nov 2026 17:02:37 +0100 Subject: [PATCH] dstorage: requests into physical pages write the title's pages (RT-DSTORAGE-DEST-PAGES) The page array is read when the request runs: the retail title passes a pointer into its own live page table. Co-Authored-By: Claude Opus 5.5 --- docs/directstorage.md | 17 ++++++++-- runtime/shims/dstorage.cpp | 37 +++++++++++++++++++-- runtime/shims/xmem.cpp | 28 ++++++++++++++++ runtime/shims/xmem.h | 8 +++++ runtime/test/dstorage_test.cpp | 61 +++++++++++++++++++++++++++++++++- runtime/test/xmem_test.cpp | 11 ++++++ 6 files changed, 155 insertions(+), 7 deletions(-) diff --git a/docs/directstorage.md b/docs/directstorage.md index dc2987a559..ac655adb40 100644 --- a/docs/directstorage.md +++ b/docs/directstorage.md @@ -93,7 +93,8 @@ The first boot with the executable settles most of this: the dry-run report show 3. reserved option bits (`E_DSTORAGE_RESERVED_FIELDS`), then the source type against the queue's (`E_DSTORAGE_INVALID_SOURCE_TYPE`); 4. BCPack mode above 7, swizzle mode above 4 (`_INVALID_BCPACK_MODE`, `_INVALID_SWIZZLE_MODE`), then BCPack or - swizzle, with or without zlib, then physical pages (`E_NOTIMPL`, logged once: later PRs); + swizzle, with or without zlib, then physical-page sources (`E_NOTIMPL`, logged once: later PRs); a request into + physical pages goes on as the same request into a staging buffer (below); 5. an `IntermediateSize` (`_INVALID_INTERMEDIATE_SIZE`: it is the size between the inflate and the BCPack decode, so only a zlib + BCPack request has one), a `DestinationSize` other than `SourceSize` without zlib, or not larger than `SourceSize` with it (`_INVALID_DESTINATION_SIZE`: the rule `dstorageerr.h` gives for that code, @@ -152,6 +153,17 @@ The first boot with the executable settles most of this: the dry-run report show worker; later enqueues are dropped and recorded as `E_DSTORAGE_QUEUE_CLOSED`. The last `Release` closes the same way, so a queue released with requests in flight finishes them first. +## Physical-page destinations + +`DestinationIsPhysicalPages` names an array of 64 KiB page numbers from `XMemAllocatePhysicalPages` and a byte offset +into the first page. The request runs into a staging buffer and its bytes then go to the memory behind those pages +(`rt::xmem::write_physical_pages`), so every address they are mapped at sees them. The shim reads the array **when the +request runs**, not at enqueue: the retail title (1.0.2.1) passes a pointer into its own page table, indexed by the +destination address, and the entries are still 0 when it enqueues and filled by the time the request runs (measured: +with a copy taken at enqueue nearly every such request named page 0; read late, none failed over 240 s). A page number +that is not live is `E_DSTORAGE_ACCESS_VIOLATION`, and so is an array that cannot be read; without the xmem shim in the +process the request is `E_NOTIMPL`. + ## Not done yet (later PRs) - **BCPack** (`BcpackMode`; `tools/bcpack.py` is the reference decoder), then **swizzle** (`SwizzleMode`). The @@ -159,8 +171,7 @@ The first boot with the executable settles most of this: the dry-run report show (the inflate's output, the BCPack decoder's input), and `FileOffset` must be 16-byte aligned. Each becomes a stage after `inflate_request`'s inflate and before its destination write, so the request checks above stay where they are. -- **Physical pages** (`SourceIsPhysicalPages`, `DestinationIsPhysicalPages`): arrays of 64 KiB page numbers from - `XMemAllocatePhysicalPages`; needs the `xmem` shim to translate a page number to the address it mapped. +- **Physical-page sources** (`SourceIsPhysicalPages`): no request of the retail title has been seen with one. - **GPU memory**: the console API has no GPU destinations of its own (memory is unified, so a destination is an address). An address inside memory the Vulkan layer backs will need its write routed to that memory. The destination is written in two places, both in `execute_request` (`read_file_range` for a file source, `guarded_move` for a diff --git a/runtime/shims/dstorage.cpp b/runtime/shims/dstorage.cpp index 0f83456894..f9ec10b83c 100644 --- a/runtime/shims/dstorage.cpp +++ b/runtime/shims/dstorage.cpp @@ -11,7 +11,10 @@ // - A zlib request (ZlibDecompress alone) reads its source into a staging buffer, inflates it on the worker with // the runtime's RFC 1950 inflater (leonida::zlib_inflate, src/shader_library.h) and copies the result to the // destination only when it is exactly DestinationSize bytes; a stream that fails leaves the destination as it was. -// - BCPack, swizzle and physical-page requests fail with E_NOTIMPL for now (later PRs: they become stages between +// - DestinationIsPhysicalPages: the request runs into a staging buffer, then its bytes go to the pages xmem allocated +// (rt::xmem::write_physical_pages). The page array is read when the request runs: the retail title passes a pointer +// into its own live page table, whose entries are still 0 when it enqueues. +// - BCPack, swizzle and physical-page sources fail with E_NOTIMPL for now (later PRs: they become stages between // the read and the destination write, BCPack after the inflate). // Nothing here goes online or touches the GPU. #include @@ -35,6 +38,7 @@ #include "../src/shader_library.h" // leonida::zlib_inflate #include "dstorage_xs_abi.h" #include "shims.h" +#include "xmem.h" using namespace dsx; @@ -800,10 +804,11 @@ class Queue final : public IDStorageQueueX2 { log_once("decompress", "BCPack and swizzle requests are not implemented yet (E_NOTIMPL)"); return E_NOTIMPL; } - if (o & (options::kDestinationPages | options::kSourcePages)) { - log_once("pages", "physical-page requests are not implemented yet (E_NOTIMPL)"); + if (o & options::kSourcePages) { + log_once("pages", "physical-page sources are not implemented yet (E_NOTIMPL)"); return E_NOTIMPL; } + if (o & options::kDestinationPages) return paged_request(e); const bool zlib = options::zlib(o); // IntermediateSize is the BCPack data's size between the inflate and the BCPack decode (DSTORAGE_REQUEST), so // it is 0 for every request that gets here (BCPack is refused above). @@ -834,6 +839,32 @@ class Queue final : public IDStorageQueueX2 { return read_file_range(e.file->handle(), r.FileOffset, r.Destination, r.SourceSize); } + // A request into physical pages: the same request into a staging buffer, then that to the pages. + HRESULT paged_request(const Entry& e) { + const DSTORAGE_REQUEST& r = e.request; + if (!rt::xmem::write_physical_fn) return E_NOTIMPL; // a process without the xmem shim + if (r.DestinationSize > kMax4kPageDestinationSize) return E_DSTORAGE_REQUEST_TOO_LARGE; + if (!r.DestinationPageArray) return E_INVALIDARG; + std::vector staging; + std::vector pages; + try { + staging.resize((std::max)(r.DestinationSize, 1u)); // never empty: the plain request checks its destination + pages.resize(static_cast((uint64_t{r.DestinationPageOffset} + r.DestinationSize + 0xffff) >> 16)); + } catch (const std::bad_alloc&) { + return E_OUTOFMEMORY; + } + Entry plain = e; + plain.request.Options.AsUINT64 &= ~options::kDestinationPages; + plain.request.Destination = staging.data(); + if (const HRESULT hr = execute_request(plain); FAILED(hr) || r.DestinationSize == 0) return hr; + static_assert(sizeof(ULONG_PTR) == sizeof(uint64_t)); + if (!guarded_move(pages.data(), r.DestinationPageArray, pages.size() * sizeof(uint64_t))) + return E_DSTORAGE_ACCESS_VIOLATION; + return rt::xmem::write_physical_fn(pages.data(), r.DestinationPageOffset, staging.data(), r.DestinationSize) + ? S_OK + : E_DSTORAGE_ACCESS_VIOLATION; + } + // A zlib request whose checks have passed: the source (SourceSize bytes of the file or of memory) goes into a // staging buffer, inflates there, and only a result of exactly DestinationSize bytes is copied to the destination. // So a failed stream leaves the destination as it was, and a memory source that overlaps its destination is safe. diff --git a/runtime/shims/xmem.cpp b/runtime/shims/xmem.cpp index a0335a2376..9a8872ec68 100644 --- a/runtime/shims/xmem.cpp +++ b/runtime/shims/xmem.cpp @@ -823,6 +823,34 @@ bool commit_physical_range(const void* address, size_t size) { return true; } +bool write_physical_pages(const uint64_t* page_array, size_t offset, const void* bytes, size_t size) { + if (size == 0) return true; + if (!page_array || !bytes) { + SetLastError(ERROR_INVALID_PARAMETER); + return false; + } + const std::lock_guard lock(g_phys_mutex); + page_array += offset / kPhysicalPageSize; + offset %= kPhysicalPageSize; + for (size_t done = 0; done < size; ++page_array, offset = 0) { + const auto it = g_pages.find(*page_array); + if (it == g_pages.end() || !it->second.section->scratch) { + SetLastError(ERROR_INVALID_PARAMETER); + return false; + } + auto* const backing = static_cast(it->second.section->scratch) + it->second.offset; + if (!VirtualAlloc(backing, kPhysicalPageSize, MEM_COMMIT, PAGE_READWRITE)) return false; + const size_t part = (std::min)(size - done, static_cast(kPhysicalPageSize) - offset); + std::memcpy(backing + offset, static_cast(bytes) + done, part); + done += part; + } + return true; +} + +namespace { +const bool g_write_physical_set = (write_physical_fn = &write_physical_pages, true); +} + bool commit_physical_pages(const uint64_t* page_array, size_t count) { if (count == 0) return true; if (!page_array) { diff --git a/runtime/shims/xmem.h b/runtime/shims/xmem.h index beef935e65..93aa23f3f2 100644 --- a/runtime/shims/xmem.h +++ b/runtime/shims/xmem.h @@ -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); +// Writes `size` bytes into the physical pages `page_array` names (64 KiB each), from byte `offset` of the first, +// committing them. False for a page that is not live; the pages before it are written. The array must be readable. +bool write_physical_pages(const std::uint64_t* page_array, std::size_t offset, const void* bytes, std::size_t size); + +// The same, for a caller that does not link the shim; null in a process without it. +using WritePhysicalFn = bool (*)(const std::uint64_t* page_array, std::size_t offset, const void* bytes, std::size_t size); +inline WritePhysicalFn write_physical_fn = nullptr; + } diff --git a/runtime/test/dstorage_test.cpp b/runtime/test/dstorage_test.cpp index 1e8ea73183..a462a3910f 100644 --- a/runtime/test/dstorage_test.cpp +++ b/runtime/test/dstorage_test.cpp @@ -15,6 +15,7 @@ #include #include +#include "xmem.h" #include "dstorage_xs_abi.h" #include "file_system.h" #include "shims.h" @@ -419,7 +420,10 @@ void test_reads_and_errors(Env& env) { cases.push_back({r, E_DSTORAGE_RESERVED_FIELDS, "a reserved option bit"}); r = file_request(b, 0, few.data(), 8); r.Options.DestinationIsPhysicalPages = 1; - cases.push_back({r, E_NOTIMPL, "physical pages are not implemented yet"}); + cases.push_back({r, E_NOTIMPL, "physical-page destinations without the xmem shim"}); + r = file_request(b, 0, few.data(), 8); + r.Options.SourceIsPhysicalPages = 1; + cases.push_back({r, E_NOTIMPL, "physical-page sources are not implemented yet"}); r = file_request(b, 0, few.data(), 8); r.IntermediateSize = 4; cases.push_back({r, E_DSTORAGE_INVALID_INTERMEDIATE_SIZE, "IntermediateSize without zlib and BCPack"}); @@ -501,6 +505,60 @@ void test_reads_and_errors(Env& env) { big->Release(); } +// DestinationIsPhysicalPages, with a stand-in for the xmem shim: which pages, at what offset, with what bytes. +std::vector g_written_pages; +size_t g_written_offset = 0; +std::vector g_written; +bool g_refuse_pages = false; +bool fake_write_physical(const uint64_t* pages, size_t offset, const void* bytes, size_t size) { + if (g_refuse_pages) return false; + g_written_pages.assign(pages, pages + (offset + size + 0xffff) / 0x10000); + g_written_offset = offset; + g_written.assign(static_cast(bytes), static_cast(bytes) + size); + return true; +} + +void test_physical_page_destinations(Env& env) { + rt::xmem::write_physical_fn = &fake_write_physical; + IDStorageFileX* big = open(env, L"GAME:\\data\\big.bin"); + IDStorageQueueX2* q = make_queue(env, 0x80, false); + IDStorageStatusArrayX* s = make_status(env, 4); + ULONG_PTR table[3] = {}; // still 0 when the request is enqueued, as in the title's page table + DSTORAGE_REQUEST r = file_request(big, 4096, nullptr, 0x18000); + r.Options.DestinationIsPhysicalPages = 1; + r.DestinationPageArray = table; + r.DestinationPageOffset = 0xC000; + q->EnqueueRequest(&r); + q->EnqueueStatus(s, 0); + table[0] = 0x10, table[1] = 0x50, table[2] = 0x20; + q->Submit(); + check(wait_complete(s, 0), "a request into physical pages completes"); + check_hr(s->GetHResult(0), S_OK, "a request into physical pages"); + check(g_written_pages == std::vector({0x10, 0x50, 0x20}) && g_written_offset == 0xC000, + "the page table is read when the request runs, for as many pages as offset and size reach"); + check(g_written.size() == 0x18000 && std::memcmp(g_written.data(), env.big.data() + 4096, g_written.size()) == 0, + "the pages get the file's bytes"); + g_refuse_pages = true; + q->EnqueueRequest(&r); + q->EnqueueStatus(s, 1); + r.DestinationPageArray = nullptr; + q->EnqueueRequest(&r); + q->EnqueueStatus(s, 2); + r.DestinationPageArray = reinterpret_cast(0x10); + q->EnqueueRequest(&r); + q->EnqueueStatus(s, 3); + q->Submit(); + check(wait_complete(s, 3), "the bad ones complete"); + check_hr(s->GetHResult(1), E_DSTORAGE_ACCESS_VIOLATION, "a page that is not live"); + check_hr(s->GetHResult(2), E_INVALIDARG, "a null page array"); + check_hr(s->GetHResult(3), E_DSTORAGE_ACCESS_VIOLATION, "a page array that cannot be read"); + g_refuse_pages = false; + rt::xmem::write_physical_fn = nullptr; + big->Release(); + s->Release(); + q->Release(); +} + void test_fence_order_and_events(Env& env) { IDStorageFileX* b = open(env, L"GAME:\\data\\b.bin"); IDStorageQueueX2* q = make_queue(env, 0x200); @@ -1243,6 +1301,7 @@ int main() { test_factory_checks(env); test_open_paths(env); test_reads_and_errors(env); + test_physical_page_destinations(env); test_fence_order_and_events(env); test_cancel(env); test_auto_submit_and_full_queue(env); diff --git a/runtime/test/xmem_test.cpp b/runtime/test/xmem_test.cpp index bf5bd36389..12665bfb56 100644 --- a/runtime/test/xmem_test.cpp +++ b/runtime/test/xmem_test.cpp @@ -598,6 +598,17 @@ void test_commit_physical_pages(const PhysicalApi& api) { "the two committed by number are committed in the window and the third is not yet"); check(read_u32(window, 0) == 0 && read_u32(window + kPage64k, 0) == 0, "pages committed by number read as zero"); + // Across the end of one page into the next, the third page named first. + const uint64_t order[2] = {pages[2], pages[0]}; + const uint8_t bytes[8] = {1, 2, 3, 4, 5, 6, 7, 8}; + check(rt::xmem::write_physical_pages(order, kPage64k - 4, bytes, 8) && + std::memcmp(window + 3 * kPage64k - 4, bytes, 4) == 0 && std::memcmp(window, bytes + 4, 4) == 0, + "write_physical_pages writes the pages in the array's order, uncommitted ones too"); + check(rt::xmem::write_physical_pages(order, kPage64k + 16, bytes, 8) && std::memcmp(window + 16, bytes, 8) == 0, + "an offset past the first page starts in a later one"); + check(!rt::xmem::write_physical_pages(&unknown, 0, bytes, 8) && !rt::xmem::write_physical_pages(nullptr, 0, bytes, 8) && + rt::xmem::write_physical_pages(nullptr, 0, bytes, 0), + "a page that is not live or a null array fails, no bytes succeed"); api.map(window, kPages, nullptr); } check(api.free_pages(kPages, pages) == TRUE, "XMemFreePhysicalPages frees them");