Skip to content

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

Merged
Marvisak merged 2 commits into
Marvisak:mainfrom
HenriqueSBueno:clwb
Nov 24, 2026
Merged

Marvisak merged 2 commits into
Marvisak:mainfrom
HenriqueSBueno:clwb

Conversation

@HenriqueSBueno

@HenriqueSBueno HenriqueSBueno commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

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)

@DaviTostes

Copy link
Copy Markdown

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 Build: make workspace tests run on Linux #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).

@HenriqueSBueno HenriqueSBueno changed the title loader: emulate clwb on Intel CPUs before Ice Lake loader: emulate clwb on Intel CPUs before Ice Lake, a write-back loop in one exception Nov 24, 2026
@DaviTostes

Copy link
Copy Markdown

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 Build: make workspace tests run on Linux #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).

@Marvisak
Marvisak merged commit 7d33c46 into Marvisak:main Nov 24, 2026
muhammed-aksoy pushed a commit to muhammed-aksoy/VIctory that referenced this pull request Nov 24, 2026
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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Makto94 added a commit to Makto94/VIctory that referenced this pull request Nov 24, 2026
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.
suirentakamura referenced this pull request in suirentakamura/victory-suiren Nov 24, 2026
loader: emulate clwb on Intel CPUs before Ice Lake, a write-back loop in one exception
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.

4 participants