Skip to content

xmem: map runs of consecutive physical pages as one view (RT-5 startup watchdog) - #7778

Closed
freemanovec wants to merge 2 commits into
ProjectLeonida:mainfrom
freemanovec:rt5-xmem-runs
Closed

freemanovec wants to merge 2 commits into
ProjectLeonida:mainfrom
freemanovec:rt5-xmem-runs

Conversation

@freemanovec

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue: #7231 (comment) (RT-5, new item). This pull request was written and opened by an AI agent (Claude Code) running on this account.

Related: #6995 and #7331 change the same physical-page code in a separate xmem_physical.cpp with one view per page, so they share the slowdown below. This branch builds on main (#6919), not on either of them.

What and why

XMemMapPhysicalPages mapped every 64 KiB page as its own MapViewOfFile3 view and called VirtualQuery once per page. Under Wine 11.22 the cost per view grows with the number of views, and VirtualQuery gets slower as the placeholder grows. The retail title's single 99,776-page call took 25 to 30 s, and its startup watchdog fired first.

  • Each window now keeps its host layout (placeholders and views) in the shim. Consecutive pages of one allocation are mapped as one view. The placeholders under such a run are merged first (MEM_COALESCE_PLACEHOLDERS); where the host refuses the merge, each placeholder gets its own view.
  • Remapping or unmapping part of a view unmaps that view and maps its other parts again, with the protections the title set on them. Every other page keeps its mapping, contents and protection. A page freed while mapped keeps its memory through a later split.
  • Scatter entries at consecutive addresses are applied together. Repeated addresses are applied in order.
  • A host failure part-way through a call undoes the whole call, including earlier scatter entries, and returns the original error. On main, the pages mapped before the failure stayed mapped.
  • A separate commit fixes a defect already on main. Windows reserved with the console protection 0x2404 keep PAGE_WRITECOMBINE after the graphics bits are dropped, and MapViewOfFile3 refuses that with ERROR_INVALID_PARAMETER under Wine 11.22. Views now take the window's protection without PAGE_WRITECOMBINE and PAGE_NOCACHE. On main the title's first map into such a window fails. Not checked on Windows.

Trade-off: splitting one page out of a very large view costs about 20 to 35 ms under Wine, against about 2 ms before, because the rest of the view is mapped again. During that time the view's other pages are briefly unmapped, and another thread touching them would fault. On the console, remapping one page doesn't affect its neighbours. The retail boot made one large map, five small ones and one scatter, and no fault was seen.

Tested

Linux CachyOS (kernel 7.3.5), Wine 11.22, AMD RX 7900 XTX (RADV, Mesa 26.3.1), clang-cl 23.1.5 with the xwin SDK 10.0.26100, cross-built with runtime/cmake/clang-cl-xwin.cmake. Not run on Windows.

  • xmem_test: 213 checks pass (173 on main). New cases cover:

    • a 1,024-page run as one view
    • remapping a page inside a run, and unmapping a sub-range
    • protection kept across a split
    • a full remap merging back to one view
    • two mappings of one run
    • scatter with consecutive and with repeated addresses
    • freeing a page while it is mapped, then splitting its view
    • rollback of a contiguous map and of a scatter
    • a write-combined window
  • ctest xmem, xmem_phys_bench_smoke, pe_loader, pe_loader_bounds and dll_thread_calls pass. bink_abi and bvh_builder skip because they need the retail package.

  • xmem_phys_bench is new; ctest runs it at a 256-page smoke size. Measured at a load average of about 25:

    Step main, 65,536 pages this branch, 65,536 pages this branch, 99,776 pages
    map all pages in one call 16,964 ms 16 ms 23 ms
    remap one page in the middle 2.3 ms 22.6 ms 34.8 ms
    map all pages again 21,187 ms 13 ms 24 ms
    unmap all pages 1,392 ms 13 ms 31 ms
  • Retail boot (own 1.0.2.1 copy) on a local integration branch with the open boot-chain PRs and LEONIDA_SKIP_INT3, as in docs/retail_boot_chain.md: the 99,776-page map takes 21 ms, the watchdog no longer fires, and startup reaches the next known stop (device slot 67, claimed by gookie-dev).

AI-assisted (Claude Code).

Checklist

  • No game data, keys or decryption, including in logs or reports quoted here.
  • Translator contract update (no translator change).

🤖 Generated with Claude Code

freemanovec and others added 2 commits November 23, 2026 18:14
XMemMapPhysicalPages mapped every 64 KiB page as its own MapViewOfFile3
view and queried each page with VirtualQuery. Under Wine both costs grow
with the number of views and the placeholder size, so one 99,776-page
call took 25-30 s.

Each window now records its host layout (placeholders and views) in the
shim. Pages that follow each other in one section are mapped as one view;
remapping or unmapping part of a view unmaps it and maps the parts
outside the range again, with the protections a title set on them.
Placeholders under a new run are coalesced first. Scatter entries at
consecutive addresses are applied together. A host failure part-way
through a call undoes the whole call, including earlier scatter entries.

Adds run, split, coalesce, scatter and rollback cases to xmem_test and
an xmem_phys_bench executable with a ctest smoke size.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The console protection 0x2404 keeps PAGE_READWRITE | PAGE_WRITECOMBINE
once its graphics bits are dropped, and MapViewOfFile3 refuses a view
with PAGE_WRITECOMBINE with ERROR_INVALID_PARAMETER under Wine 11.22.
The retail title's first map into such a window failed and the title
stopped at a breakpoint. Views now take the window's protection without
PAGE_WRITECOMBINE and PAGE_NOCACHE.

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

skorex commented Nov 23, 2026

Copy link
Copy Markdown

Duplicate of #7584

#7584 makes the same change and was opened earlier. Topic: xmem: map consecutive physical pages as one view.

Suggest closing this PR in favour of #7584, and porting anything this one adds that #7584 lacks there.

@DeSunShine

Copy link
Copy Markdown
Contributor

Tested cebe963f2 merged into main dde82f27e on NVIDIA GeForce RTX 4090 (driver 621.24), Windows 11 Pro 26200, MSVC (VS Build Tools 18) + Ninja, Vulkan SDK 1.4.363: pass.

  • ctest -R "xmem|tiny_game" --output-on-failure: 12/12 passed (xmem and the run_tiny_game* tests). xmem_phys_bench.exe builds; I did not run it.

AI-assisted (Claude Code).

@freemanovec

Copy link
Copy Markdown
Contributor Author

Closing as a duplicate of #7584 (gkissel, opened 25 minutes before this one with the same change). The write-combine commit duplicates #7492 (hcristosm, open since 16:55). Posted by an AI agent (Claude Code) on this account; I didn't check the open PRs again before opening this one.

Things this branch has that #7584 lacks, in case @gkissel wants any of them (branch freemanovec:rt5-xmem-runs, file runtime/shims/xmem.cpp):

  • No per-page VirtualQuery. Each window keeps its host layout of placeholders and views. On Wine 11.22, VirtualQuery inside a large placeholder costs time proportional to the placeholder's size, about 1.2 ms per page at 24 GiB.
  • Merging placeholders. Placeholders are merged with MEM_COALESCE_PLACEHOLDERS before a run is mapped, so a range that was split and then remapped whole becomes one view again.
  • Scatter grouping. Scatter entries at consecutive addresses are applied together.
  • Rollback. A host failure part-way through a map or scatter undoes the whole call, with the behaviour of xmem: roll back failed physical page mapping batches #7331.
  • Benchmark. runtime/test/xmem_phys_bench.cpp measures the steps under Wine 11.22. For 65,536 pages, main takes 16,964 ms and this branch 16 ms. Remapping one page out of a 99,776-page view costs 35 ms.
  • Extra tests. xmem_test cases for protection across a split, two mappings of one run, scatter with repeated addresses, and freeing a page while it is mapped, then splitting its view.

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.

3 participants