Skip to content

cpu: handle reserved XMem backing during CLZERO emulation - #826

Open
moffington wants to merge 2 commits into
ProjectLeonida:mainfrom
moffington:submit/xmem-clzero-first-touch
Open

moffington wants to merge 2 commits into
ProjectLeonida:mainfrom
moffington:submit/xmem-clzero-first-touch

Conversation

@moffington

@moffington moffington commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: RT-CLZERO-XMEM

What and why

Emulated CLZERO raised a false write access violation on XMem pages that were untouched or that Wine still reported as reserved after a successful CPU write. The handler now checks the exact faulting subpage, commits its XMem backing when eligible, then rechecks live protection before zeroing the cache line. Initially read-only and unrelated mappings stay reserved. Read-only, no-access, and one-shot guard protections still apply.

Tested

  • The original synthetic regression failed on d0da7dade and passed with this change, including 20 repeat runs. It covers concurrent first touches and partially committed XMem pages.
  • d3d12_xmem_placed passed on an NVIDIA GeForce RTX 3060 with driver 615.78.08. Stock xmem reproduced the same five Wine failures before and after: two metadata checks, one commit-accounting check, and two protection-setup checks.
  • Omarchy, Wine 11.20, clang-cl 23.1.3, Windows SDK 10.0.26100. Native Windows and MSVC remain untested.
  • The batched CLZERO regression starts in reserved XMem and verifies that the emulation zeroes the writable prefix, reports a write fault at the exact read-only boundary, and leaves every byte from that boundary through the end of the requested span untouched.
  • The test target links the real XMem shim. The fixture runs in separate processes for SEC_RESERVE and Wine’s default SEC_COMMIT mode. Both CPU test cases passed three consecutive runs each on the rebased branch.
  • As a negative control, the new batch assertions failed as expected in SEC_RESERVE mode when built against the CPU-emulation header from either main or runtime: step try_clzero_loop by VirtualQuery regions and guard wraparound in cpu_emulation #2035.

AI-assisted with OpenAI Codex.

Known limitation

The protection check and the zeroing write are separate operations, so there is no atomicity guarantee against a concurrent remap or protection change.

Overlap with other open PRs

CI

CI pending on the rebased commit

@abnalstlll-byte abnalstlll-byte left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Clean-Room Peer Review for PR #826 (cpu: handle reserved XMem backing during CLZERO emulation)

High-impact fix for emulated Zen 2 memory semantics on Intel/non-AMD host CPUs:

  • First-Touch Backing Commitment:
    • Implements commit_physical_page_for_write(address) in
      untime/shims/xmem.cpp to dynamically commit reserved XMem scratch backing when targeted by emulated CLZERO.
    • Respects page protection boundaries: safely verifies AllocationProtect when State == MEM_RESERVE and delivers PAGE_GUARD violations accurately before testing writability.
  • Defensive Error Handling:
    • Rejects read-only views, unmapped placeholders, and inaccessible subpages with STATUS_ACCESS_VIOLATION.
    • Accurately reports STATUS_GUARD_PAGE_VIOLATION on first touch of guard pages before resetting to standard write mode.
  • Concurrency & Unit Test Coverage:

untime/test/cpu_emulation_test.cpp adds est_xmem_clzero testing concurrent multi-threaded first touches on one XMem page via std::latch(2).

  • Tests exact 64-byte line clearing invariance (std::memcmp against zero array).
  • Clean-room verification: Zero game data, strict C++20 compliance, builds and passes on MSVC with /OPT:NOREF.

LGTM! Outstanding systems-level fix. Approved.

@abnalstlll-byte abnalstlll-byte left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Clean-room verified (zero game data). Crucial fix for Zen 2 CLZERO instruction emulation on Intel hosts touching lazily committed XMem physical memory: introduces commit_physical_page_for_write while strictly honoring PAGE_GUARD/PAGE_READONLY. Exhaustive concurrency and subpage tests in cpu_emulation_test.cpp.

@moffington

Copy link
Copy Markdown
Contributor Author

Investigated both failed CI checks and confirmed identical failures at the PR's base (d0da7dade):

[Baseline CI run](https://github.com/ubiquitarianism/leonida/actions/runs/45406612415)

Neither failure appears to be introduced by this PR.

@moffington
moffington force-pushed the submit/xmem-clzero-first-touch branch from 2aeecd3 to ed514a5 Compare November 25, 2026 02:27
@github-actions github-actions Bot added the needs rebase Conflicts with the base branch: rebase it on main label Nov 25, 2026
@moffington
moffington force-pushed the submit/xmem-clzero-first-touch branch from ed514a5 to ffdb444 Compare November 25, 2026 13:57
@moffington

Copy link
Copy Markdown
Contributor Author

Rebased #826 onto 77baf9062 and published ffdb44446. git merge-tree also passed against refreshed main at 71a769d7d. The rebase preserves upstream’s Wine SEC_COMMIT fast path and keeps the CLZERO fallback for subpages that still report MEM_RESERVE.

On Wine 11.20, 8/8 focused tests passed, including d3d12_xmem_placed on an RTX 3060. The CLZERO regression passed 20/20 runs. The same fixture reproduced four CLZERO failures on upstream main at 77baf9062.

#1499 overlaps in cpu_emulation_test.cpp and routes MSVC single-line CLZERO through an SEH-protected store that bypasses #826’s writable() check. The combined MSVC path remains unverified.

@moffington

Copy link
Copy Markdown
Contributor Author

Rechecked mergeability against upstream main (db1a22d) with current HEAD ffdb444. The merge succeeds cleanly, so the needs rebase label appears stale. CI is currently queued.

I also checked the overlap with #1499. While its MSVC SEH approach addresses first-touch backing, #826 also covers the non-MSVC path. A trial merge of both PRs produces a conflict in runtime/test/cpu_emulation_test.cpp.

Flagging this for merge-order coordination so we preserve the correctness guarantees and regression coverage of both changes.

@moffington

Copy link
Copy Markdown
Contributor Author

Investigated the Runtime workflow #1655 failures on ffdb444 (run 45628868378). None of the failures are introduced by this PR:

  • windows (job): 420/421 tests passed (the earlier XMA2 unresolved imports from build: link synchronization for the Rust XMA2 decoder (BUILD-XMA2-SYNCH-LIB) #877 are gone). Only 419 - vk_xdxr_postbuild_readback failed because runtime/test/vk_xdxr_postbuild_readback_test.cpp returns 77 when no bufferDeviceAddress Vulkan device is present, but runtime/CMakeLists.txt registers the test without SKIP_RETURN_CODE 77 (upstream dfadf4ce5; also failing on main run #1288).
  • linux-gpu-tools (job): Build fails in runtime/test/vk_null_descriptor_test.cpp:97 (‘LEONIDA_TEST_NULL_DESCRIPTORS_SPV’ was not declared in this scope). linux-gpu-tools does not install glslangValidator, so CMake omits -DLEONIDA_TEST_NULL_DESCRIPTORS_SPV=...; main() is guarded on #ifndef LEONIDA_TEST_NULL_DESCRIPTORS_SPV, but run() at line 97 is not (upstream ba626820e, reproduced locally).
  • macos (job): Runtime tests on MoltenVK passed; GPU tests on MoltenVK failed on cs_imgatomic_ext (6 of 64 sint words differ; first word 32: got 0x0badf00d, expected 0x00000101), matching main run #1288 (upstream 367d91ef2).
  • wine (job): Skipped by .github/workflows/runtime.yml (github.event_name != 'pull_request'), not due to the windows failure.

Mergeability check: GitHub reports mergeable: true / rebaseable: true and git merge-tree against main (7f3d0095a) is clean, so the needs rebase label remains stale.

A CLZERO emulation can encounter an XMem view whose backing is still reserved. Commit only the backing of a view that permits the attempted access, then check the faulting subpage again before writing. Keep existing XMem callers on their current commitment path.
@moffington
moffington force-pushed the submit/xmem-clzero-first-touch branch from ffdb444 to d62aa5b Compare November 26, 2026 04:44
@github-actions github-actions Bot removed the needs rebase Conflicts with the base branch: rebase it on main label Nov 26, 2026
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.

2 participants