Loader: clzero loops in one exception; rdpru #34

Closed
github-bot wants to merge 1 commit from gh-pr/34/head into gh-pr/34/base
Owner

@⁠ka1qux on GitHub · 2026-11-23 23:28 UTC · original
ka1qux:clzero-loops-rdpru → main · +207 −3 · 1 files · 1 commits · closed without merging

Follow-up to #10 (and to my closed #18): two things the handler on main does not do yet.

clzero loops in one exception

Each emulated instruction is one exception, about a microsecond on a Core i5-13600KF. The title clears its larger objects with clzero a line per pass, so the cost is per 64 bytes.

Measured in the loader's test binary on that CPU, for one 28 KiB clear:

exceptions time
main (one line per exception) 448 0.40 ms
this branch (loop done whole) 1 4 µs

In Grand Theft Auto VI's menus I counted about 130 such clears a second (one 29 KB object, five loops each), so this is roughly 50 ms of exception handling per second of one thread on main. That count comes from a run of my earlier handler, not from main itself.

The handler recognises the two loops compilers build around clzero by the instructions beside it and carries the whole loop out, leaving registers and flags as the last pass would:

top: clzero                 top: mov rax, a
     add rax, 0x40               add a, 0x40
     cmp rax, b                  clzero
     jb  top                     cmp a, b
                                 jb  top

Anything else around a clzero is still one line and the next instruction. The neighbouring bytes are read only inside the faulting instruction's page.

rdpru

rdpru (0F 01 FD) appears once in the executable. It now returns the time-stamp counter for both MPERF and APERF. No run of mine has reached it, so this is untested against the title.

Testing

  • cargo test -p victory-loader on an i5-13600KF, where the instructions run through the handler: both loop shapes (end registers, zeroed lines, and flags compared with a native cmp), and rdpru.
  • Not run against the title on this branch, and not tested on an AMD CPU, where the handler is never entered.

🤖 Generated with Claude Code

**@⁠ka1qux** on GitHub · 2026-11-23 23:28 UTC · [original](https://github.com/Marvisak/VIctory/pull/34) `ka1qux:clzero-loops-rdpru` → `main` · +207 −3 · 1 files · 1 commits · closed without merging Follow-up to #10 (and to my closed #18): two things the handler on `main` does not do yet. ## clzero loops in one exception Each emulated instruction is one exception, about a microsecond on a Core i5-13600KF. The title clears its larger objects with `clzero` a line per pass, so the cost is per 64 bytes. Measured in the loader's test binary on that CPU, for one 28 KiB clear: | | exceptions | time | |---|---|---| | `main` (one line per exception) | 448 | 0.40 ms | | this branch (loop done whole) | 1 | 4 µs | In Grand Theft Auto VI's menus I counted about 130 such clears a second (one 29 KB object, five loops each), so this is roughly 50 ms of exception handling per second of one thread on `main`. That count comes from a run of my earlier handler, not from `main` itself. The handler recognises the two loops compilers build around `clzero` by the instructions beside it and carries the whole loop out, leaving registers and flags as the last pass would: ``` top: clzero top: mov rax, a add rax, 0x40 add a, 0x40 cmp rax, b clzero jb top cmp a, b jb top ``` Anything else around a `clzero` is still one line and the next instruction. The neighbouring bytes are read only inside the faulting instruction's page. ## rdpru `rdpru` (`0F 01 FD`) appears once in the executable. It now returns the time-stamp counter for both MPERF and APERF. No run of mine has reached it, so this is untested against the title. ## Testing - `cargo test -p victory-loader` on an i5-13600KF, where the instructions run through the handler: both loop shapes (end registers, zeroed lines, and flags compared with a native `cmp`), and `rdpru`. - Not run against the title on this branch, and not tested on an AMD CPU, where the handler is never entered. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Every emulated instruction is an exception, about a microsecond each on
a Core i5-13600KF. The title clears its larger objects with clzero a
line per pass, so a 28 KiB clear took 448 exceptions: 0.40 ms, against
4 microseconds once the loop is done whole. Grand Theft Auto VI runs
such clears (one 29 KB object, five loops) about 130 times a second in
its menus.

The handler now recognises the two loops compilers build around clzero
by the instructions beside it and carries the whole loop out, leaving
registers and flags as the last pass would. Any other use is still one
line and the next instruction. The neighbouring bytes are read only
inside the faulting instruction's page.

rdpru (0F 01 FD), which the executable holds once, reports the
time-stamp counter for MPERF and APERF. No run has reached it yet.

Tested: victory-loader tests on an i5-13600KF (both loop shapes against
native flags, rdpru), where they run through the handler.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:47:21 +00:00
github-bot closed this pull request 2026-11-26 03:47:21 +00:00
Author
Owner

@⁠raffclar on GitHub · 2026-11-23 23:47 UTC · original

The rdpru instruction reads a CPU counter but on a real AMD CPU, it also sets a flag called CF to say whether the read worked.

  • 1 means it worked
  • 0 means the counter ID is not supported and It also clears the other status flags

The emulator writes the counter value into rax and rdx, but it does not touch the flags. CF keeps whatever an earlier instruction left there so we need to clear those flags.

Super easy fix you need to do, clear the status flags, then set CF to 1 when the counter ID in ecx is 0 or 1.

AMD's manual should be checked to confirm this exact behaviour first so check with your agent too!

**@⁠raffclar** on GitHub · 2026-11-23 23:47 UTC · [original](https://github.com/Marvisak/VIctory/pull/34#issuecomment-7117373858) The rdpru instruction reads a CPU counter but on a real AMD CPU, it also sets a flag called CF to say whether the read worked. * 1 means it worked * 0 means the counter ID is not supported and It also clears the other status flags The emulator writes the counter value into rax and rdx, but it does not touch the flags. CF keeps whatever an earlier instruction left there so we need to clear those flags. Super easy fix you need to do, clear the status flags, then set CF to 1 when the counter ID in ecx is 0 or 1. AMD's manual should be checked to confirm this exact behaviour first so check with your agent too!
Author
Owner

@⁠DaviTostes on GitHub · 2026-11-24 00:08 UTC · original

Tested faa0a9a 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.
  • Scope note: the clzero/rdpru work and its tests live in the Windows exception handler (victory-loader/src/crash.rs and the loader's Windows-gated tests), so the Linux run exercises the portable crates around it, not the emulation itself.
  • No game data used.

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

**@⁠DaviTostes** on GitHub · 2026-11-24 00:08 UTC · [original](https://github.com/Marvisak/VIctory/pull/34#issuecomment-7117748080) Tested `faa0a9a` 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. - Scope note: the `clzero`/`rdpru` work and its tests live in the Windows exception handler (`victory-loader/src/crash.rs` and the loader's Windows-gated tests), so the Linux run exercises the portable crates around it, not the emulation itself. - No game data used. AI-assisted (DeepSeek V4.1 Flash coding agent).
Author
Owner

@⁠ka1qux on GitHub · 2026-11-24 01:39 UTC · original

Closing in favour of #214, which rebases this over #45 and improves it. Two things from there worth repeating here:

  • Loop 1 took cmp rax, rax for the loop and left ZF wrong; #214 rejects that shape. That was a bug in this PR.
  • On the rdpru review comment: #214 measured the instruction natively on a Zen 3 and it changes no flags, so the emulation here (which leaves them alone) matched hardware on that point. I had not been able to check AMD's manual myself.

Thanks to the reviewers and to the author of #214.

**@⁠ka1qux** on GitHub · 2026-11-24 01:39 UTC · [original](https://github.com/Marvisak/VIctory/pull/34#issuecomment-7119314876) Closing in favour of #214, which rebases this over #45 and improves it. Two things from there worth repeating here: - Loop 1 took `cmp rax, rax` for the loop and left ZF wrong; #214 rejects that shape. That was a bug in this PR. - On the `rdpru` review comment: #214 measured the instruction natively on a Zen 3 and it changes no flags, so the emulation here (which leaves them alone) matched hardware on that point. I had not been able to check AMD's manual myself. Thanks to the reviewers and to the author of #214.
@ -155,0 +163,4 @@
0
};
context.Rax = cycles & 0xffff_ffff;
context.Rdx = cycles >> 32;
Author
Owner

@⁠raffclar on GitHub · 2026-11-23 23:49 UTC · original

Writes to RAX but leaves the flags as they are. See my comment

**@⁠raffclar** on GitHub · 2026-11-23 23:49 UTC · [original](https://github.com/Marvisak/VIctory/pull/34#discussion_r4673588003) Writes to RAX but leaves the flags as they are. See my comment

Pull request closed

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!34
No description provided.