Skip to content

cpu_emulation: count each emulated site and time the #UD trap; first retail boot measurement on Intel (RT-500 follow-up, depends on #7111) - #8537

Open
AlexLopezDevelop wants to merge 8 commits into
ItsNotRexed:mainfrom
AlexLopezDevelop:rt-cpu-trap-stats
Open

AlexLopezDevelop wants to merge 8 commits into
ItsNotRexed:mainfrom
AlexLopezDevelop:rt-cpu-trap-stats

Conversation

@AlexLopezDevelop

Copy link
Copy Markdown

Claimed in the coordination issue as: new RT item, #7496 (comment)

Depends on: #7111 (this branch is on its head 39b757a6d; it only adds to runtime/src/cpu_emulation.h and the files below)

What and why

docs/cpu_features.md in #7111 ends with "Not measured: which of these sites a boot reaches, and in which order." This adds the counters to answer that, and the first answer.

  • Per-site counts in the #UD handler (runtime/src/cpu_emulation.h): every emulated instruction is counted against its address, with the handler's own TSC ticks, in a fixed 256-entry open-addressing table filled with compare-and-swap, so the handler never allocates or takes a lock. A site that does not fit is still emulated and counted as dropped.
  • gta6rt (runtime/src/main.cpp): a cpu_emulated startup-trace event at each new site (instruction, at as module+RVA); a per-site summary on stderr at a normal exit; and with LEONIDA_CPU_EMU_STATS=N the same summary every N seconds while the counts change, so a run that ends in a crash or an assert keeps its counts. Nothing is printed when nothing is emulated (any Zen 2 or later host).
  • cpu_emulation_test checks the table (first hit reported once, hits and ticks per address, snapshot order, and the live CLWB sites on a host without CLWB). With --bench it prints the cost of one emulated instruction, the whole #UD round trip, against the same loop with a NOP; it checks nothing there, so a slow machine cannot fail it.
  • Docs: docs/cpu_features.md gets the measurement below, runtime/README.md the switch and the trace event.

Measurement (counts only, nothing from the executable)

Intel Core i9-9900K (Coffee Lake: no SSE4a, CLZERO, MONITORX or CLWB), Windows 11, own 1.0.2.1 copy, main with the open boot-chain PRs merged locally (stops 1-20, #7111, #7516 for the BVH builder):

What Result
One emulated instruction (--bench, CLWB, 200,000 iterations) 1.5 to 1.9 µs, against under 1 ns for the NOP loop
Sites the boot reached 1 of the 41: a CLWB in a cache-line write-back loop
Hits at that site 16 at the first compute pipeline (stop 18); 18 by the next stop
Time in the emulation, boot so far about 30 to 40 µs in all
INSERTQ/EXTRQ, CLZERO, MONITORX/MWAITX not reached yet

The boot stops about 4 s in, at a fatal assert right after the first device slot 41 command signature. So far the trap path costs nothing that matters, and the load-time patching from my claim is not needed for the sites a boot reaches today. The vector-code SSE4a sites are the ones to watch: at about 2 µs a trap, around 8,000 hits a frame would cost a 60 Hz frame. Anyone whose boot gets further on an Intel CPU can re-run with LEONIDA_CPU_EMU_STATS=5 and update the table.

Also seen in passing, for whoever owns stop 18: on this RTX 2080 Ti (driver 621.24) the console BVH builder's first CreateComputePipelineState hit the title's 30 s watchdog on both a cold and a warm driver cache (two runs, about 33 s each); with #7516 the boot goes past it.

Tested

  • cpu_emulation_test and cpu_emulation_test --bench: all checks pass on the i9-9900K (every form through the handler).
  • gta6rt --run on the retail executable as above, with --trace and LEONIDA_CPU_EMU_STATS=2/3: the cpu_emulated event and the summary appear, and the counts match between the trace and the summary.
  • python -I tests/test_docs_*.py and tests/test_boot_trace.py: pass, except test_docs_links.py, which fails on RT-500: gta6rt emulates the Zen 2 instructions an Intel CPU lacks (INSERTQ/EXTRQ, CLZERO, MONITORX/MWAITX, CLWB) #7111's head too (an anchor in docs/dev-tools.md); not touched here.
  • Build: MSVC 14.44 (VS 2022 Build Tools), RelWithDebInfo, Windows SDK 10.0.26100. GPU: NVIDIA RTX 2080 Ti, driver 621.24, LEONIDA_D3D12=vulkan.

AI-assisted: yes (Claude Code)

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (Not applicable: no translator change.)

🤖 Generated with Claude Code

Krashgamer and others added 8 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>
…low-up)

Every instruction the #UD handler emulates is counted against its address,
with the handler's own TSC ticks, in a fixed lock-free table. gta6rt writes a
`cpu_emulated` startup-trace event per new site, prints a per-site summary at a
normal exit, and every N seconds with LEONIDA_CPU_EMU_STATS=N so a run that
crashes still leaves its counts. cpu_emulation_test checks the table and, with
--bench, prints the cost of the whole #UD round trip against a NOP loop.

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

cpu_features.md records the first measurement on an Intel i9-9900K: one CLWB
site, 16-18 hits up to boot stop 19, about 2 us per #UD round trip. The runtime
README documents the per-site summary, LEONIDA_CPU_EMU_STATS and the
cpu_emulated trace event.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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