loader: emulate clwb on Intel CPUs before Ice Lake, a write-back loop in one exception #45
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!45
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/45/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@HenriqueSBueno on GitHub · 2026-11-23 23:45 UTC · original
HenriqueSBueno:clwb→main· +208 −1 · 1 files · 2 commits · merged 2026-11-24 00:30 UTCWhat
The illegal-instruction handler from #10 now also covers
clwb(66 [rex] 0f ae /6with a memory operand). It skips the instruction:clwbonly writes a cache line back, and the PC's memory is coherent, so there is nothing to do.Why
clwbis on Intel only from Ice Lake (desktop: Rocket Lake / Alder Lake). Desktop Comet Lake and older lack it, and the title runs it during startup. On an i5-10400F the title died about 8 s in with0xc000001dat a66 41 0f ae 31(clwb [r9]). #10 was tested on an i7-14700KF, which hasclwb, so it didn't show there.Changes
clwb_lendecodes the instruction's length: an optional REX, then ModRM with SIB and disp8 / disp32, RIP-relative included. Other0f aeforms returnNone:/7(clflushopt), no66(xsaveopt), and the register form.clwb_lengthsunit-tests the decoder on those forms.Tested
Windows 11, i5-10400F (Comet Lake, no
clwb), RTX 3080 (NVIDIA 616.92):cargo test -p victory-loader: all pass.cargo clippy -p victory-loader --all-targets: clean.cargo xtask policy: ok.VICTORY_CAPTUREon a diagnostics build, and still running at 240 s.Update: the write-back loop in one exception
With the first commit alone the title loads very slowly on these CPUs. It writes back whole buffers a cache line at a time:
That is one exception per 64 bytes. Sampled during loading on the i5-10400F, the busy threads spent about a third of their time in
KiUserExceptionDispatcher, and the hottest title address was this loop. Loading read at about 5 MB/s.The second commit,
clwb_loop, recognises that loop and finishes it in one exception:baseends just pastlimit, the flags are what the lastcmpleaves, and execution resumes after thejbe. The same approach as #34 takes forclzero; its helpers are kept local (gpr,cmp_flags) so the two PRs don't conflict. Only bytes inside the faulting instruction's page are read. Tests cover both register forms the title uses (rcxandr9) and reject other strides, other jump targets and a cut-off loop.AI-assisted (Claude Code)
@DaviTostes on GitHub · 2026-11-23 23:51 UTC · original
Tested
71cfe84stacked on #39 on Arch Linux x86_64, rustc 1.98.1: pass.mainalone does not build on Linux (E0425 invictory-gpu), so the check uses #39 as the base; the merge is clean.cargo test --workspace: 156 passed, 0 failed, 3 ignored.clwb_lengthstest runs and passes, and the surrounding loader tests stay green. No game data used.One scope note: the emulation path itself selects on the host CPU, so on this AMD machine only the decode/length helper and the suite can be exercised; the Intel pre-Ice Lake path is not reachable here.
AI-assisted (DeepSeek V4.1 Flash coding agent).
@DaviTostes on GitHub · 2026-11-24 00:17 UTC · original
Re-checked the new head
c4e915f("a clwb write-back loop runs in one exception") stacked on #39 on Arch Linux x86_64, rustc 1.98.1: pass (no regression only).cargo test --workspace: 155 passed, 0 failed, 3 ignored; merge with #39 clean.AI-assisted (DeepSeek V4.1 Flash coding agent).
@ -154,2 +154,4 @@[0x0f, 0x01, 0xfb] => std::thread::yield_now(),_ => {// Only bytes inside the faulting instruction's page are read: the next page may// not be mapped.@JayFoxRox on GitHub · 2026-11-24 17:57 UTC · original
This sounds a bit weird; maybe I'm missing something.
Instructions can go across page boundaries.
However, if the instruction starts at 0x...FFF then this code would only check 1 byte and it wouldn't be detected as
clwb?The code is read at RIP (not some random pointer from stack), so unless there's an always-taken-branch here, the real CPU will cross the page boundary anyway. Hence, the decoder should be able to fetch bytes (from next page) on its own if necessary. As far as I can tell, no such behavior exists here and it would instead misrecognize a partial instruction.
It would probably be even safer to just fetch into the next page because it sounds more likely to get an instruction crossing pages, rather than getting extremly unlucky where you get an unsupported instruction at the very end of the last page of a code section (example: a multi-megabyte .text section which is otherwise contiguously mapped by a PE loader).
While this probably doesn't affect (the currently available build of) GTA VI (where the instruction might not cross the page boundary) it might affect other versions of the game (or forks / copies of this code) in the future.