Skip to content

Console CPU instructions: the CLZERO loop is decoded and run as the CPU does it (#278) - #295

Draft
TG0000 wants to merge 1 commit into
ivall:mainfrom
TG0000:fix/clzero-loop-decode
Draft

TG0000 wants to merge 1 commit into
ivall:mainfrom
TG0000:fix/clzero-loop-decode

Conversation

@TG0000

@TG0000 TG0000 commented Nov 23, 2026 •

Copy link
Copy Markdown

Part of #278 (items 1 and 2). This fixes how the CLZERO loop (clzero; add rax, 0x40; cmp rax, <reg>; jb -12) is decoded and emulated on CPUs without CLZERO. is_writable (item 3) and the CLWB hot-patch race (item 4) will come in separate PRs.

What is in it (one commit)

  • crates/xcl/src/insn.rs, decoder: the loop is now recognised only when the ModRM is mod 3 with rax in r/m (modrm & 0xC7 == 0xC0, no REX.B). Before, cmp rdx, rcx, cmp [rax], rdx and similar were taken for the loop. cmp rax, rax never loops, so it is left to the plain Clzero.
  • crates/xcl/src/insn.rs, new clzero_loop(rax, end, max_lines) and cmp_flags: they compute what the CPU does. That means:
    • whole 64-byte lines, starting from the line rax is in;
    • rax moves by whole lines and keeps its offset in the line;
    • the body always runs at least once, so an end at or below rax still zeroes one line;
    • the loop ends at the first rax not below end (unsigned);
    • the flags are those of the last cmp.
    • It returns None when rax would wrap past the top of the address space.
  • crates/xcl/src/eh.rs, ClzeroLoop emulation: it now uses clzero_loop, so the old bugs are gone:
    • it no longer zeroes a byte-exact [rax, end) and sets rax = end;
    • it no longer skips the first line when end <= rax;
    • it no longer leaves EFLAGS unchanged.
  • How a fault is handled now:
    • Each fault zeroes at most 256 MiB. If the loop isn't finished, it stays at the clzero and the next fault continues. This replaces the 2 GiB cap, which skipped the loop entirely.
    • If the span is not one writable region, it zeroes one line per fault. The loop then stops at the same line where the CPU would fault, and the earlier lines stay zeroed.

Tests (new)

  • insn::tests::clzero_loop_needs_cmp_rax_with_a_register: covers every cmp rax, reg for regs 1–15 and r8, plus the encodings that must not be read as the loop: cmp rax, rax, swapped registers, memory compares with and without displacement, and REX.B.
  • insn::tests::cmp_flags_match_the_cpu: CF/PF/AF/ZF/SF/OF on hand-checked cases.
  • insn::tests::clzero_loop_runs_like_the_cpu: covers an unaligned start, an end that is not a whole number of lines away, an end at or below rax, a run cut by max_lines, wraparound, and a sweep checked against the loop stepped one iteration at a time.
  • eh::tests::clzero_loop_zeroes_whole_lines_as_the_cpu: calls emulate_console_only on real memory. It checks the zeroed span, the final rax, rip and EFLAGS. It also covers a span across two committed regions (one line per fault until the rest fits in one region) and a span running into decommitted memory (the lines before stay zeroed, then the fault goes to the title).

Tested

  • AMD Ryzen 9 5900X, AMD Radeon RX 6800, Windows 11 build 26300, Rust 1.100.0, stable-x86_64-pc-windows-gnu (llvm-mingw clang), debug.
  • cargo test --workspace --exclude testgame-mw --no-fail-fast -- --test-threads=4, run in this order:
    • xcl lib: 614 passed, 1 failed, 20 ignored
    • xcl integration tests: e2e 2 passed / 1 failed; everything else passed
    • 87 SKIPPED ...: no DXC (no DXC in this environment, so GPU tests did not run)
  • Both failures also happen on main (9ab7d76) in the same environment, and neither comes from this change:
    • pe::tests::parses_own_test_binary_tls ("rust binaries have a TLS directory") fails because GNU-target binaries have no TLS directory.
    • test_title_runs_and_game_folder_is_untouched fails because testgame_mw.dll is not built when testgame-mw is excluded.
  • cargo build -p xcl --lib: no new warnings. The only warning is the existing fetch_update deprecation, which main has too.

Regression run
Windows 11, Ryzen 9 5900X, AMD Radeon RX 6800, Rust stable x86_64-pc-windows-msvc (VS 2022 Build Tools, link.exe), cargo build --release -p xcl, scripts/smoke.ps1 -Seconds 45, one run at a time under a RAM watchdog.

main (9ab7d76, this PR's merge base):

xcl-smoke v1
commit: 9ab7d763197c7f420bff10cd9bf1310f07e2a9aa
gpu: AMD Radeon RX 6800
title: retail-title-1.0.2.1
seconds: 45
exit: 0x0
frames shown: 843 (PresentX 844)
presents per 2 s (last 3): 60 60 59
device removals: 0
crashes: 0
peak commit: 15251 MB

PR (head commit, no local changes):

xcl-smoke v1
commit: 3ad409465b2c05819ed32a90d5305d972aad7f93
gpu: AMD Radeon RX 6800
title: retail-title-1.0.2.1
seconds: 45
exit: 0x0
frames shown: 826 (PresentX 827)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15256 MB

No regression: 826 frames against 843 on its base, no device removals or crashes. The 5900X has CLZERO natively, so the emulation is not reached here. This branch conflicts with current main in eh.rs and insn.rs and needs a rebase. Current main (2b4bb43) on the same title, for reference: 709 frames in 45 s, 0 device removals, 0 crashes, peak commit 15183 MB.

Not tested

  • An Intel CPU (the only kind where this path runs in a real title), GPU tests (no DXC).

AI use

  • Claude Code with Claude Opus 5.5 wrote the code, the tests and this description. I reviewed the diff, and the test results above come from a real run.

🤖 Generated with Claude Code

…PU does it (ivall#278)

The loop decoder took any `[48|4C] 39 modrm` as `cmp rax, <reg>`: `cmp rdx, rcx` or a memory
compare (`cmp [rax], rdx`) was emulated as a loop to the wrong register. It now needs ModRM mod 3
with rax in r/m, and leaves `cmp rax, rax` (which never loops) to the plain CLZERO.

The emulation zeroed [rax, end) byte-exact and set rax = end: an unaligned rax or an end that is
not a whole number of lines away zeroed a partial line, an end at or below rax zeroed nothing
though the body runs once, and the cmp's flags were left as they were. insn::clzero_loop now gives
what the CPU does: whole lines from the one rax is in, rax moved by whole lines, at least one
iteration, the flags of the last cmp. A fault zeroes at most 256 MiB and stays at the clzero when
the loop goes on (instead of the 2 GiB cap that skipped the loop), and a span that is not one
writable region goes a line a fault, so it stops at the line the CPU would fault on.

Part of ivall#278 (items 1 and 2; is_writable and the CLWB patch are separate PRs).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ethandepstein-del

Copy link
Copy Markdown

Independent static code & x86-64 ISA audit of 3ad409465 (code inspection only — cargo test not run on macOS; no game files):

Summary

(Note: overlaps with #290, which also addresses #278.)

  • cmp_flags (insn.rs:68-77): Verified all 6 arithmetic flags (CF, PF, AF, ZF, SF, OF) against x86-64 cmp a, b (a - b), including AF via (a ^ b ^ r) & 0x10 != 0 and OF via ((a ^ b) & (a ^ r)) >> 63 != 0.
  • ModRM & loop semantics (insn.rs:161-172, eh.rs:327-345): code[i + 9] & 0xC7 == 0xC0 with (code[i + 7], code[i + 9]) != (0x48, 0xC0) accurately matches cmp rax, <reg != rax> across rcx..r15. Chunking at CLZERO_LOOP_LINES and dropping to 1-line-per-fault across non-uniform regions preserves exact hardware fault boundaries.

One subtle edge case

  • Prefixes before clzero (insn.rs:163, eh.rs:343): decode only excludes 0x67 from code[..i], not other prefixes (i > 0). Because jb -12 (72 F4) jumps back to rip + i (0F 01 FC), returning -1 on !run.done leaves ctx.Rip at rip (before the prefix) instead of rip + i. Requiring i == 0 for ClzeroLoop (falling back to single Clzero when prefixed) eliminates that mismatch.

(AI-assisted)

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.

2 participants