Repository navigation
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
Conversation
… 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>
|
Tested Retail boot. Base:
So on Raptor Lake the cache-line write-back site runs natively and the trap path isn't entered before slot 41; no summary is printed, as the description says for a run with nothing emulated.
One nit in On Zen 2+ and on Intel from Ice Lake/Alder Lake on, CLWB is native, so the "emulation costs" figure is the native CLWB there, not the Not run: a boot past slot 41, so the SSE4a vector sites stay unmeasured here too. AI-assisted (Claude Code). |
|
Tested Built with The emulation itself, on a host that has the instructions. exit 0. So on this Zen 4 desktop part every one of the four user-mode instructions the page flags is present, nothing goes through the handler, and the checks still pass on that path. That is the case the page can only infer from the ISA ("On an AMD Zen 2 or later host every flagged user-mode instruction runs as on the console"), and here it is the path the code actually took. The counts reproduce on a second copy. Every row of the table in A retail boot here, with the counters on. One wording problem in
Docs and unrelated files. One thing to save the next reviewer some time: |
|
Tested
Pass. AI-assisted (Claude Code). |
…fix stay #UD An x86 instruction is at most 15 bytes, so decode() now reads no further than that: a CLZERO behind 13 prefixes or a 17-byte EXTRQ is refused even when more bytes are readable (the handler already passed at most 15). MONITORX and MWAITX are only taken without 66/F2/F3; CLZERO keeps the 66/F2/F3 forms measured on Zen 3. The title's MONITORX/MWAITX sites carry no prefix. New decoder checks cover both; they fail without the header change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
VirtualQuery sizes the run of pages with the same attributes after the address, so its cost grows with the rest of the region: in a mapped image or a large heap each emulated instruction took 0.4-1.5 ms (LastThylacine's measurement on ItsNotRexed#7111; 1.5 ms per CLZERO and 0.5 ms per INSERTQ on an i7-10700K). - The handler reads the instruction bytes in RIP's page without a query (the CPU just fetched them there) and queries only when the 15-byte window reaches into the next page. - writable() asks QueryWorkingSetEx about the line's page first; a resident, plainly writable page returns at once. Anything else (not resident, read-only, guard) takes the VirtualQuery path as before. Now about 2 us per emulated instruction. New live checks: CLZERO on a resident read-only page still raises an access violation, on a resident guard page a guard-page violation, and zeroes the line once the guard is gone. Suggested by LastThylacine in the ItsNotRexed#7111 review. 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>
… docs: three hosts and the frame loop --bench timed CLWB on every host and called the native instruction "the emulation cost" where CLWB is native (towade, fiestorra, SpaghettiMaster545 on ItsNotRexed#8537). It now times each of CLWB, CLZERO and EXTRQ that the host lacks, against a NOP of the same length, and says there is nothing to time on a host that has all three. cpu_features.md: the Raptor Lake and Zen 4/5 rows from the reviews, the measured scope (up to the slot 41 assert), the CLZERO sites a boot that reaches the frame loop adds, and ItsNotRexed#8783 for the hot CLZERO loop. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
7f464ba to
293fb2f
Compare
|
Thanks @towade, @fiestorra and @SpaghettiMaster545 for the three runs. All three of you caught the same
AI-assisted (Claude Code). |
|
Re-tested
Pass. AI-assisted (Claude Code). |
|
checked: #8537 (diff includes dep #7111's head) | with: applied the diff on main 5d74c27, built and ran cpu_emulation_test (MSVC 14.44, Ninja RelWithDebInfo) | on: Windows 11, RX 9060 XT host (Zen-based, all emulated instructions native: SSE4a/MONITORX/CLZERO/CLWB present) | result: PASS — cpu_emulation: all checks passed Note: on this host every emulated site is native, so the #UD emulation paths themselves are not exercised here (the test validates the counter table, snapshot ordering and live-site reporting structurally). The per-site retail measurement in the docs came from an Intel host, which this machine is not. |
|
Tested
No retail boot (I have no game files), so nothing for the boot table. AI-assisted (Claude Code) |
Claimed in the coordination issue as: new RT item, #7496 (comment)
Depends on: #7111 (this branch is on its head
c5f025fac; it only adds toruntime/src/cpu_emulation.hand the files below)What and why
docs/cpu_features.mdin #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.#UDhandler (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.runtime/src/main.cpp): acpu_emulatedstartup-trace event at each new site (instruction,atas module+RVA); a per-site summary on stderr at a normal exit; and withLEONIDA_CPU_EMU_STATS=Nthe 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_testchecks the table (first hit reported once, hits and ticks per address, snapshot order, and the live CLWB sites on a host without CLWB). With--benchit times each of CLWB, CLZERO and EXTRQ the host lacks (the whole#UDround trip) against a NOP of the same length, and says there is nothing to time on a host that has all three; it checks nothing there, so a slow machine cannot fail it.docs/cpu_features.mdgets the measurement below,runtime/README.mdthe 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,
mainwith the open boot-chain PRs merged locally (stops 1-20, #7111, #7516 for the BVH builder):--bench, 200,000 iterations)c5f025fac(1.5 to 1.9 µs before its hot-path change)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=5and 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
CreateComputePipelineStatehit 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_testandcpu_emulation_test --bench: all checks pass on the i9-9900K (every form through the handler).gta6rt --runon the retail executable as above, with--traceandLEONIDA_CPU_EMU_STATS=2/3: thecpu_emulatedevent and the summary appear, and the counts match between the trace and the summary.python -I tests/test_docs_*.pyandtests/test_boot_trace.py: pass, excepttest_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 indocs/dev-tools.md); not touched here.LEONIDA_D3D12=vulkan.AI-assisted: yes (Claude Code)
Checklist
docs/design.md, with how it was confirmed. (Not applicable: no translator change.)🤖 Generated with Claude Code