Skip to content

file_system: the title's ReadFile commits first-touch physical pages before the kernel writes them - #9166

Open
Etr0p wants to merge 7 commits into
ItsNotRexed:mainfrom
Etr0p:readfile-commit-first-touch
Open

Etr0p wants to merge 7 commits into
ItsNotRexed:mainfrom
Etr0p:readfile-commit-first-touch

Conversation

@Etr0p

@Etr0p Etr0p commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue: #8868 (comment). The measurement it comes from: #7556 (comment)

Depends on: #7556. This branch is #7556's head e85ebec7d plus one commit, 44b7276f4. @Cattn, if you'd rather fold this into #7556, say so and I'll close this one.

What and why

#7556 commits an xmem physical page on the title's first touch: the touch faults, and a vectored handler commits the page. A write by the kernel raises no user-mode fault. So when the title ReadFiles into a physical page it hasn't touched yet, the read fails with ERROR_NOACCESS and the handler never runs. On my retail boot, almost every ReadFile of the title failed this way, and every failing destination was a mapped view still in MEM_RESERVE.

With this change, ReadFile, ReadFileEx and ReadFileScatter (kernel32, kernelbase, api-ms-win-core-file-l1-1-0) go through file shims. Each shim prepares the destination first, then calls the host function:

  • The hook: the shims call rt::kernel_write::before(destination, size). xmem sets that hook to commit_physical_range when it installs its first-touch handler. Memory xmem doesn't own is left alone, as commit_physical_range already does.
  • Why a hook: the hook is header-only (runtime/shims/kernel_write_hook.h) because file_system.cpp is built into several executables that don't link xmem.cpp.
  • Overlapped reads are covered too: the pages stay committed after the call returns.
  • Unchanged: the read itself, its result and its error codes.

Note for #9045 (slot-133 shadows): it adds ReadFile shims to the same table for another reason (opening pages it write-protects). Whichever lands second calls both helpers from one shim. That merge is mechanical.

Tested

Windows 11 26200, MSVC 14.44, Ninja RelWithDebInfo. CPU only, synthetic data.

  • xmem_test: OK, 220 checks. It now links file_system.cpp and resolves the shims through the registry, as the title's imports do. On five mapped physical pages nothing has touched:
    • Control: the host's ReadFile fails with ERROR_NOACCESS, and the page stays uncommitted.
    • Through the shims: four reads all return the file's bytes. A synchronous read into an untouched page. A read through the api-ms-win-core-file-l1-1-0 entry that straddles two untouched pages. An overlapped read on an asynchronous handle. A ReadFileEx.
  • file_system_test: 446 checks, 0 failures.

Retail measurement (counts only)

RTX 5070 Ti Laptop, NVIDIA 621.24, my own copy of Xbox 1.0.2.1. I used a local boot stack that includes #7556 (and #9045), with and without this change. 120 s per run, and the ReadFile failures were counted by a local pass-through shim.

Before After
Title ReadFile calls in 120 s 53 248+ fewer than 4 096
Title ReadFile failed 52 566 (all ERROR_NOACCESS, on uncommitted physical pages) 1 (ERROR_INVALID_PARAMETER, not a physical page; present before too)
DirectStorage entries failed 0 0
PresentX in 120 s / per second 19 000 / 193 11 000 / 115
Device or queue slots reached that weren't before — queue slot 31, 2 calls
Non-zero pixels in the presented image (read back at presents 2, 300, 3 000, 10 000) 0 0
  • What changed: the title no longer loops on failing reads. Its file reads succeed, and it goes on to call a queue slot it never reached before.
  • What didn't: the frame it presents is still black.
  • PresentX rate: the drop wasn't analysed. It is the same stack doing more work after its reads succeed.

Not tested: Wine, clang-cl, and DirectStorage's own reads. #7180's dstorage.cpp ReadFiles straight into a request's destination, so it should call rt::kernel_write::before too.

AI-assisted: yes (Claude Code).

🤖 Generated with Claude Code

Cattn and others added 6 commits November 23, 2026 12:01
XMemAllocatePhysicalPages backed each call with one SEC_COMMIT section, so
the title's ~6.1 GiB page-pool request after boot stop 17 was charged in
full at once and fails with ERROR_COMMITMENT_LIMIT on a 16 GB machine.
Mapping the pages right away (as the title does) would charge most of it
within seconds too, so the commit now follows first access.

The section is SEC_RESERVE. A vectored handler commits the faulting 64 KiB
page through a reserve-only view of the whole section when the address
lies in a live mapping, so every view of that page sees it. Faults on
committed pages whose protection forbids the access, and faults outside
the mapped pages, go on to the next handler, and a failed commit is not
swallowed. rt::xmem::commit_physical_range commits a range up front for
shims that hand guest memory to the kernel.

Reported by controldebils and rafaelpssz in ItsNotRexed#7333.
A writer that copies into physical pages through its own view of their
section (a DirectStorage destination given as page numbers, ItsNotRexed#7652) sees
reserve-only memory for pages the title has not touched yet, and no
fault handler can resolve that address. commit_physical_pages commits
the given pages through each section's own view, one VirtualAlloc per
contiguous run, so such a writer can commit before it copies. Raised by
nurtrino in ItsNotRexed#7599.
…record

unmap_page moved the record out of g_mapped before taking the exclusive
lock, so a first-touch fault on that page could read a moved-from
section, and map_page made the view live before recording it, so a
fault in between was not recognised. unmap_page now holds the exclusive
lock from the lookup to the erase, and map_page holds it across mapping
the view and recording it. xmem_test adds four threads writing into a
window while 256 slices of pages are mapped and unmapped under them.
Reported by tupacfan04 in the review of ItsNotRexed#7556.
…ting address

The handler queried the protection at the start of the 64 KiB page, so
a write to a 4 KiB part the title had made read-only looked allowed and
was resumed, faulting again forever instead of reaching the title's own
handler. It now looks the record up by the page and the protection up
by the faulting address. xmem_test writes to a read-only 4 KiB part of
a read-write page and expects the fault.
The commit-on-touch checks expect hardware faults from plain loads and
stores inside __try. MSVC catches those as it is; clang-cl only with
asynchronous exceptions, so without /EHa the expected fault escaped and
xmem_test crashed. Same option as tiny_dll. Reported by AirKyzzZ in ItsNotRexed#7556.
…before the kernel writes them

ItsNotRexed#7556 commits an xmem physical page on the title's first touch, from a vectored handler. A kernel write raises no
user-mode fault, so the title's ReadFile into a physical page not committed yet failed with ERROR_NOACCESS: on a
retail boot, 52,566 of 53,248 ReadFile calls in 120 s, every destination a MEM_RESERVE mapped view.

ReadFile, ReadFileEx and ReadFileScatter now go through file shims that call rt::kernel_write::before on the
destination first. That hook (kernel_write_hook.h, header-only, since file_system.cpp is built into executables
without xmem.cpp) is set by xmem when it installs its first-touch handler, to commit_physical_range.

xmem_test now links file_system.cpp and checks, on five mapped physical pages nothing touched: the host's ReadFile
fails with ERROR_NOACCESS (control); through the shims, a synchronous read, a read through the file-l1-1-0 entry
that straddles two pages, an overlapped read on an asynchronous handle and a ReadFileEx all succeed with the file's
bytes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@catminou4

Copy link
Copy Markdown

Not exercised here: xmem_test is a Windows-shim test, not in the macOS portable runtime build. No game files. AI-assisted (Devin).

@smowlyy

smowlyy commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Verified head 44b7276f4 merged onto main (867d922f0) on native Windows 11 (10.0.26300.9974, MSVC 19.44, RelWithDebInfo):

  • python -I scripts/check_no_game_data.py on all 6 changed files: clean (exit 0)
  • cmake --build runtime/build --target file_system_test xmem_test --parallel: built cleanly
  • ctest --test-dir runtime/build -R '^(file_system|xmem)$' --output-on-failure -V: 2/2 passed (xmem, file_system — 446 checks, 0 failures)

AI-assisted.

@Grimkeh Grimkeh left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Independent Linux CPU review

What changed: file_system shims ReadFile/ReadFileEx/ReadFileScatter to call kernel_write::before so xmem can commit physical pages before the kernel writes the destination.

Checked: diff + new kernel_write_hook.h; xmem_test now links file_system.cpp (Windows). Logic matches the ERROR_NOACCESS first-touch failure mode described.

No game files. Portable CPU execution of the Windows shim not available on this Linux host.

@GeremC

GeremC commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Tested 44b7276 merged with main 867d922 under Wine: xmem_test fails (215 pass, 5 fail). 4 of the 5 come from #7556 (its head e85ebec alone gives the same 4 under Wine; details there). The fifth is this PR's control:

  • control: the host's ReadFile into an untouched physical page fails with ERROR_NOACCESS. With a temporary print added locally, Wine's ::ReadFile returns TRUE, 65,536 bytes, error 0, and committed(window) is still false afterwards. Wine's ReadFile writes the buffer from user mode, so the first-touch handler runs (or the view is already writable) and the kernel-write case this PR fixes doesn't happen under Wine.
  • Every read through the shims passes under Wine: synchronous, the api-ms-win-core-file-l1-1-0 entry across two pages, overlapped, and ReadFileEx (the test has no ReadFileScatter case). The fix does no harm there.

So the control is a Windows-only expectation. Under Wine it could be reported as a skip (e.g. with host_wine.h) instead of a failure.

On: Linux, Ubuntu 24.04, cross-built with clang-cl 19.1.1 + xwin SDK (RelWithDebInfo), run through Wine 9.0; CPU only, no game files.

AI-assisted (Claude Code).

Wine's ReadFile writes the buffer from user mode, so the first-touch
handler runs and the read succeeds: the kernel write the control
guards against doesn't happen there. The control is reported as a skip
under Wine (same wine_get_version check as the other tests) and still
checked on Windows; every read through the shims stays active.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Etr0p

Etr0p commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

@GeremC thanks for running it under Wine. Pushed 88be3d046: the control (the host's ReadFile into an untouched physical page fails with ERROR_NOACCESS) is now reported as a SKIP under Wine, detected with the same wine_get_version check the other tests use. Under Wine ::ReadFile isn't called at all, so the page is still untouched when the shim reads into it. On Windows the control runs as before, and every read through the shims stays active everywhere.

Tested on native Windows 11 (26200, MSVC 14.44, RelWithDebInfo): xmem_test OK (220 checks, control checked and passing), ctest -R '^(file_system|xmem)$' 2/2 passed, git diff --check and python -I scripts/check_no_game_data.py clean. I haven't run it under Wine; that path is untested here. CPU only, no game files.

AI-assisted (Claude Code).

@oxoo8344-sudo

Copy link
Copy Markdown

checked: #916688be3d0 | base: PR head (includes #7556e85ebec) | on: native Windows 11 26300 x64, MSVC 19.44, SDK 10.0.26100, RelWithDebInfo | result: stock ctest -C RelWithDebInfo -R "^xmem$" passed (1/1); an extra synthetic ReadFileScatter check passed (233/233 assertions including stock tests).

The extra check opens an 8 KiB synthetic file with FILE_FLAG_OVERLAPPED | FILE_FLAG_NO_BUFFERING and targets two noncontiguous 4 KiB buffers in distinct, untouched XMem physical pages, leaving a third XMem page in between. The direct Win32 call fails with ERROR_NOACCESS (998), transfers 0 bytes and leaves both target pages uncommitted. The registered kernelbase shim completes with 8,192 bytes, each buffer matches its expected data, and the intervening physical page stays uncommitted.

The stock test checks registration of ReadFileScatter. This exercises the registered call with an actual kernel write. Consider adding this case to the existing fixture. Wine and retail boot were not part of this check. Synthetic inputs only. AI-assisted (Codex).

agent: yes

@Jadiac5

Jadiac5 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

AI-assisted (Claude Code). A retail measurement for the "not tested: DirectStorage's own reads" line above.

Local test copy: main plus this PR, #7180's dstorage.cpp and other open PRs; Windows 11, RX 9070 XT, retail package 1.0.2.1.

DirectStorage entries failed with E_DSTORAGE_ACCESS_VIOLATION Title
this PR as it is (1 run) at least 1 (logged once per code) stops with a breakpoint exception about 19 s in, after 4 presents
plus rt::kernel_write::before(out + done, size - done) before the ReadFile in read_file_range (3 runs) 0 still presenting when the run is ended (90 s, 240 s, 230 s)

So the guess is right: read_file_range in dstorage.cpp reads straight into the request's destination and needs the same call. That one line (plus the include) was the whole change.

Not covered: Wine, clang-cl, zlib requests (they go through guarded_move, which faults in user mode and did not fail here), and the picture — the frame is still empty in all runs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants