Repository navigation
loader: emulate clwb on Intel CPUs before Ice Lake, a write-back loop in one exception - #45
Conversation
|
Tested
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). |
|
Re-checked the new head
AI-assisted (DeepSeek V4.1 Flash coding agent). |
Based on Marvisak#34 by ka1qux (commit faa0a9a), rebased over Marvisak#45, which added the same flag and register helpers for clwb loops; the duplicates are merged into gpr/cmp_flags/CMP_FLAGS. clzero: the two loops compilers build around it are done whole in one exception, leaving registers and flags as the last pass would. Anything else around it is one line and the next instruction. rdpru: returns the time-stamp counter for MPERF and APERF, 0 for other counters, and leaves the flags as they are: a Ryzen 7 5800X3D running it natively in user mode changes none, for known counters or not. Added over Marvisak#34: - loop 1 rejects cmp rax, rax (it fell into the loop and left wrong flags); - tests that go through the emulation on any CPU (both loop shapes, the rax-rax and wrong-neighbour fallbacks, rdpru with both flag states), since on an AMD host the native tests never enter the handler.
| [0x0f, 0x01, 0xfb] => std::thread::yield_now(), | ||
| _ => { | ||
| // Only bytes inside the faulting instruction's page are read: the next page may | ||
| // not be mapped. |
There was a problem hiding this comment.
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.
Based on Marvisak#34 by ka1qux (commit faa0a9a), rebased over Marvisak#45, which added the same flag and register helpers for clwb loops; the duplicates are merged into gpr/cmp_flags/CMP_FLAGS. clzero: the two loops compilers build around it are done whole in one exception, leaving registers and flags as the last pass would. Anything else around it is one line and the next instruction. rdpru: returns the time-stamp counter for MPERF and APERF, 0 for other counters, and leaves the flags as they are: a Ryzen 7 5800X3D running it natively in user mode changes none, for known counters or not. Added over Marvisak#34: - loop 1 rejects cmp rax, rax (it fell into the loop and left wrong flags); - tests that go through the emulation on any CPU (both loop shapes, the rax-rax and wrong-neighbour fallbacks, rdpru with both flag states), since on an AMD host the native tests never enter the handler.
loader: emulate clwb on Intel CPUs before Ice Lake, a write-back loop in one exception
What
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)