Skip to content

cpu_emulation: the executable's CLZERO zeroing loop is finished in one trap (retail frame loop on Intel: about 2.4 to 46 frames a second; depends on #7111) - #8783

Open
joroyt wants to merge 7 commits into
ItsNotRexed:mainfrom
joroyt:clzero-loop

Conversation

@joroyt

@joroyt joroyt commented Nov 23, 2026 •

Copy link
Copy Markdown

Claimed in the coordination issue as: new RT item (RT-500 follow-up), #8683 (comment)

Depends on: #7111 (this branch is on its head 39b757a6d; review only the last commit, 58f7d1ca6)

What and why

#7111 emulates CLZERO from the #UD handler, one instruction per trap. The executable's 10 CLZERO sites are one routine that zeroes memory a line at a time with the same four instructions:

clzero
add  rax, 64
cmp  rax, <register>
jb   <the clzero>

so every 64 bytes cost one exception round trip, about 1.9 us here. A boot reaches this loop, and the title's frame loop runs it all the time: on a retail boot one title thread sat at 100 % of a core with its RIP on the CLZERO (4 of 24 stack samples), and the title presented about 2.4 frames a second.

finish_clzero_loop (runtime/src/cpu_emulation.h): after a CLZERO has been emulated, when the bytes at RIP are exactly that tail, the handler zeroes every further line the loop would, sets RAX, the arithmetic flags (those of the last cmp) and RIP as the loop leaves them, and continues after the branch. A line that is not committed, is guarded or cannot be written ends it early with RIP back at the CLZERO and RAX in that line, so that iteration traps again and raises its access violation as before (a page that is committed on first touch still gets its fault this way). Any other code after a CLZERO is emulated as before. Nothing is patched in the image.

Measured

Intel Core i9-14900KF (no SSE4a, CLZERO, MONITORX), NVIDIA RTX 5070 (driver 32.0.16.2124), Windows 11, MSVC 14.51 RelWithDebInfo, LEONIDA_D3D12=vulkan, own 1.0.2.1 copy.

Before After
1 MiB loop in cpu_emulation_test (16,376 lines) 31.8 ms 0.02 to 0.04 ms
Retail frame loop, frames presented a second about 2.4 (167 in 68 s) about 46 (6,000 in 129 s)

The retail numbers are from a local integration stack (tibzejoker's rt5-integration plus the open boot-chain pull requests up to a frame loop that presents, with #7111 and this change), not from this branch alone. The first loop the boot ran through it zeroed 229 lines in one trap. It does not change what the title draws: the frames were black before and after. Counts only, nothing from the executable.

For #8537's table: its boot stopped about 4 s in, before the loop is reached, so it saw no CLZERO. A boot that reaches the frame loop spends its time there.

Tested

  • cpu_emulation_test: all checks pass on the i9-14900KF (every form through the handler). New: test_clzero_loop (the lines zeroed, RAX/RIP/flags for a limit on and inside a line, cmp rax, rcx, nothing left to do, another instruction after the CLZERO, a branch that does not go back to the CLZERO, a read-only page) and, live, the whole loop over 1 MiB and into a read-only page (access violation, with the lines before it zeroed). The live cases run natively on a host with CLZERO, so there they check the instruction itself; I have no such host.
  • With the call in the handler removed, the same test binary takes 31.8 ms for the 1 MiB loop and still passes: the result is the same either way, only the time differs.
  • gta6rt builds; the retail run above.
  • python -I scripts/check_no_game_data.py on the four changed files: clean.
  • tests/test_docs_*.py: the same three fail as on RT-500: gta6rt emulates the Zen 2 instructions an Intel CPU lacks (INSERTQ/EXTRQ, CLZERO, MONITORX/MWAITX, CLWB) #7111's head (test_docs_cli_flags, test_docs_design_rows, test_docs_links); the others pass.

Not shown

  • Not run on an AMD host (the native path), under Wine, or with clang-cl.
  • Only this loop shape is recognised. The INSERTQ/EXTRQ sites in vector code are still one trap each; how often a boot reaches them is not measured here.

AI-assisted: yes (Claude, in OpenCode).

Krashgamer and others added 7 commits November 23, 2026 16:44
… desktop CPU may lack

The runtime runs the Xbox Series (Zen 2) executable natively, so an instruction the host CPU lacks raises #UD
where it is reached. tools/cpu_feature_scan.py streams dumpbin /disasm like inline_scan.py, maps each
instruction to its CPUID feature or privilege class (AMD-only, newer-Intel-only, not on Zen 2, privileged,
kernel call, host query, baseline), and splits the counts by .pdata range.

docs/cpu_features.md records the 1.0.2.1 result (counts only): 19 SSE4a, 10 CLZERO and 4 MONITORX/MWAITX
sites (AMD only) and 8 CLWB sites, all real code; the privileged hits are jump-table data.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… the first

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…Q/EXTRQ, CLZERO, MONITORX/MWAITX, CLWB)

The executable uses SSE4a INSERTQ, CLZERO and MONITORX/MWAITX (AMD only) and CLWB (Intel since Ice Lake)
without asking CPUID (docs/cpu_features.md). A host without one raises #UD there. src/cpu_emulation.h adds
a vectored handler that recognises those encodings at the faulting RIP, does the same work on the CONTEXT
and steps past the instruction; gta6rt installs it before any image code runs, after the startup trace's
handler so it runs first. One [rt] line names what the host lacks, one more per instruction on first use.

cpu_emulation_test checks the decoder on hand-written encodings, the bit-field helpers and apply() on a
synthetic CONTEXT, then runs every form for real from an executable buffer: natively on a Zen host,
through the handler on an Intel one, with the same expected values.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ult; accept 66 on 0F 01 FC/FA/FB

A line CLZERO cannot write now reaches the next handlers as the access violation (or guard-page
violation) the instruction raises, with the write flag and the line address, instead of the #UD.
The 66 prefix and segment overrides are accepted on CLZERO/MONITORX/MWAITX; F2/F3 (other
instructions there, F3 0F 01 FA is MCOMMIT) and LOCK stay #UD. Points raised by SirKingg on ItsNotRexed#7055.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… xmm1[127:64]; AMD prefix forms; no SEH

- INSERTQ xmm, xmm took the length and index from swapped fields. The APM (vol. 4) has the length in
  xmm2[69:64] and the index in xmm2[77:72]; a Ryzen 9800X3D and a Ryzen 5700X agree (TheHoracio,
  Mayzkl on ItsNotRexed#7111). The measured 0x0810 case is a unit check now.
- INSERTQ/EXTRQ zero xmm1[127:64], as the Zen 3 measured in the same review does in all four forms.
- Decode REX bytes among the legacy prefixes (a non-final REX is ignored) and accept F2/F3 on CLZERO,
  as Zen 3 runs them (ItsNotRexed#7055's reviews); F2/F3 stay #UD on MONITORX/MWAITX.
- Check the CLZERO line's page and the instruction bytes' readability with VirtualQuery instead of
  catching a fault in a local __try, which clang-cl without /EHa does not cover for inline stores
  (venetanji on ItsNotRexed#7111).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…on and xg_xs test targets)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…e trap (retail frame loop on Intel: about 2.4 to 46 frames a second)

@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.

Verified on Windows 11.

  • Massive performance optimization for Intel CPUs: recognizes the hot memory-zeroing loop tail (\clzero; add rax, 64; cmp rax, limit; jb) in the #UD exception filter and finishes remaining iterations in a single trap via \memset.
  • Accurately reconstructs CPU flags (CF, PF, AF, ZF, SF, OF) matching the final \cmp rax, limit\ instruction.
  • Safe page boundary protection: halts early and steps back to the \clzero\ instruction if encountering uncommitted, read-only, or guard pages so the kernel raises the authentic access/guard violation.
  • \ ools/cpu_feature_scan.py\ passes compileall and cleanly lists Zen 2 instructions.
  • Clean-room check and \git diff --check\ pass on all changed files.

LGTM!

@lohant27-netizen

Copy link
Copy Markdown

Tested 58f7d1ca6 on Windows 11 (10.0.26300), NVIDIA GeForce RTX 5070 (driver 32.0.16.2101, subgroup 32), MSVC with SDK 10.0.26100, Release, merged onto main 867d922f0 together with #8815, #8814, #8809, #8784, #8783 and #8813 (no conflicts): pass.

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.

4 participants