loader: emulate clwb on Intel CPUs before Ice Lake, a write-back loop in one exception #45

Manually merged
github merged 2 commits from gh-pr/45/head into gh-pr/45/base 2026-11-26 03:47:58 +00:00
Owner

@⁠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 UTC

What

The illegal-instruction handler from #10 now also covers clwb (66 [rex] 0f ae /6 with a memory operand). It skips the instruction: clwb only writes a cache line back, and the PC's memory is coherent, so there is nothing to do.

Why

clwb is 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 with 0xc000001d at a 66 41 0f ae 31 (clwb [r9]). #10 was tested on an i7-14700KF, which has clwb, so it didn't show there.

Changes

  • clwb_len decodes the instruction's length: an optional REX, then ModRM with SIB and disp8 / disp32, RIP-relative included. Other 0f ae forms return None: /7 (clflushopt), no 66 (xsaveopt), and the register form.
  • clwb_lengths unit-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.
  • Before: the title stops 8 s in with the illegal instruction above.
  • After: it runs through the first-boot screens to the account prompt ("account picker (Y) / PLAYER (A)") at a steady 30 fps. Checked with VICTORY_CAPTURE on 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:

top: clwb [base]
     add  base, 0x40
     cmp  base, limit
     jbe  top

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: base ends just past limit, the flags are what the last cmp leaves, and execution resumes after the jbe. The same approach as #34 takes for clzero; 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 (rcx and r9) and reject other strides, other jump targets and a cut-off loop.

AI-assisted (Claude Code)

**@⁠HenriqueSBueno** on GitHub · 2026-11-23 23:45 UTC · [original](https://github.com/Marvisak/VIctory/pull/45) `HenriqueSBueno:clwb` → `main` · +208 −1 · 1 files · 2 commits · merged 2026-11-24 00:30 UTC ## What The illegal-instruction handler from #10 now also covers `clwb` (`66 [rex] 0f ae /6` with a memory operand). It skips the instruction: `clwb` only writes a cache line back, and the PC's memory is coherent, so there is nothing to do. ## Why `clwb` is 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 with `0xc000001d` at a `66 41 0f ae 31` (`clwb [r9]`). #10 was tested on an i7-14700KF, which has `clwb`, so it didn't show there. ## Changes - `clwb_len` decodes the instruction's length: an optional REX, then ModRM with SIB and disp8 / disp32, RIP-relative included. Other `0f ae` forms return `None`: `/7` (`clflushopt`), no `66` (`xsaveopt`), and the register form. - `clwb_lengths` unit-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. - **Before:** the title stops 8 s in with the illegal instruction above. - **After:** it runs through the first-boot screens to the account prompt ("account picker (Y) / PLAYER (A)") at a steady 30 fps. Checked with `VICTORY_CAPTURE` on 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: ``` top: clwb [base] add base, 0x40 cmp base, limit jbe top ``` 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: `base` ends just past `limit`, the flags are what the last `cmp` leaves, and execution resumes after the `jbe`. The same approach as #34 takes for `clzero`; 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 (`rcx` and `r9`) and reject other strides, other jump targets and a cut-off loop. AI-assisted (Claude Code)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:47:57 +00:00
github manually merged commit c4e915f95e into gh-pr/45/base 2026-11-26 03:47:58 +00:00
Author
Owner

@⁠DaviTostes on GitHub · 2026-11-23 23:51 UTC · original

Tested 71cfe84 stacked on #39 on Arch Linux x86_64, rustc 1.98.1: pass.

  • main alone does not build on Linux (E0425 in victory-gpu), so the check uses #39 as the base; the merge is clean.
  • cargo test --workspace: 156 passed, 0 failed, 3 ignored.
  • The new clwb_lengths test 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-23 23:51 UTC · [original](https://github.com/Marvisak/VIctory/pull/45#issuecomment-7117442497) Tested `71cfe84` stacked on **#39** on **Arch Linux x86_64, rustc 1.98.1**: **pass**. - `main` alone does not build on Linux (E0425 in `victory-gpu`), so the check uses #39 as the base; the merge is clean. - `cargo test --workspace`: **156 passed, 0 failed, 3 ignored**. - The new `clwb_lengths` test 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).
Author
Owner

@⁠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.
  • The loop-in-one-exception path is Windows-only, so the portable loader suite is what runs here.
  • No game data used.

AI-assisted (DeepSeek V4.1 Flash coding agent).

**@⁠DaviTostes** on GitHub · 2026-11-24 00:17 UTC · [original](https://github.com/Marvisak/VIctory/pull/45#issuecomment-7117918446) 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. - The loop-in-one-exception path is Windows-only, so the portable loader suite is what runs here. - No game data used. 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.
Author
Owner

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

**@⁠JayFoxRox** on GitHub · 2026-11-24 17:57 UTC · [original](https://github.com/Marvisak/VIctory/pull/45#discussion_r4684301102) 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.
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!45
No description provided.