Loader: clzero loops in one exception; rdpru #34

Closed
ka1qux wants to merge 1 commit from upstream-pr/34 into main
ka1qux commented 2026-11-23 23:28:36 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by ka1qux on 2026-11-23T23:28:36Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:issue:6571003607 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/34). Originally posted by [ka1qux](https://github.com/ka1qux) on 2026-11-23T23:28:36Z. --- 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)
upstream-sync closed this pull request 2026-11-24 06:50:59 +00:00
raffclar commented 2026-11-24 06:51:00 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by raffclar on 2026-11-23T23:49:03Z.

Review: COMMENTED


No description provided.

<!-- greyfinch-sync:Marvisak/VIctory:review:6031373846 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/34#pullrequestreview-6031373846). Originally posted by [raffclar](https://github.com/raffclar) on 2026-11-23T23:49:03Z. **Review:** COMMENTED --- _No description provided._
raffclar commented 2026-11-24 06:54:32 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by raffclar on 2026-11-23T23:47:51Z.


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!

<!-- greyfinch-sync:Marvisak/VIctory:comment:7117373858 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/34#issuecomment-7117373858). Originally posted by [raffclar](https://github.com/raffclar) on 2026-11-23T23:47:51Z. --- 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!
DaviTostes commented 2026-11-24 06:54:35 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DaviTostes on 2026-11-24T00:08:32Z.


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).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7117748080 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/34#issuecomment-7117748080). Originally posted by [DaviTostes](https://github.com/DaviTostes) on 2026-11-24T00:08:32Z. --- 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).
ka1qux commented 2026-11-24 06:55:02 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by ka1qux on 2026-11-24T01:39:14Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7119314876 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/34#issuecomment-7119314876). Originally posted by [ka1qux](https://github.com/ka1qux) on 2026-11-24T01:39:14Z. --- 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.
raffclar commented 2026-11-24 06:56:30 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by raffclar on 2026-11-23T23:49:03Z.

Code review location: crates/victory-loader/src/crash.rs; line 166

@@ -149,9 +153,18 @@ unsafe fn emulate_amd_only(info: *mut EXCEPTION_POINTERS) -> bool {
         }
         let code = context.Rip as *const u8;
         match code.cast::<[u8; 3]>().read_unaligned() {
-            [0x0f, 0x01, 0xfc] => ((context.Rax & !63) as *mut u8).write_bytes(0, 64),
+            [0x0f, 0x01, 0xfc] => return clzero(context),
             [0x0f, 0x01, 0xfa] => {}
             [0x0f, 0x01, 0xfb] => std::thread::yield_now(),
+            [0x0f, 0x01, 0xfd] => {
+                let cycles = if context.Rcx as u32 <= 1 {
+                    std::arch::x86_64::_rdtsc()
+                } else {
+                    0
+                };
+                context.Rax = cycles & 0xffff_ffff;
+                context.Rdx = cycles >> 32;

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

<!-- greyfinch-sync:Marvisak/VIctory:review-comment:4673588003 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/34#discussion_r4673588003). Originally posted by [raffclar](https://github.com/raffclar) on 2026-11-23T23:49:03Z. **Code review location:** `crates/victory-loader/src/crash.rs`; line 166 ~~~~diff @@ -149,9 +153,18 @@ unsafe fn emulate_amd_only(info: *mut EXCEPTION_POINTERS) -> bool { } let code = context.Rip as *const u8; match code.cast::<[u8; 3]>().read_unaligned() { - [0x0f, 0x01, 0xfc] => ((context.Rax & !63) as *mut u8).write_bytes(0, 64), + [0x0f, 0x01, 0xfc] => return clzero(context), [0x0f, 0x01, 0xfa] => {} [0x0f, 0x01, 0xfb] => std::thread::yield_now(), + [0x0f, 0x01, 0xfd] => { + let cycles = if context.Rcx as u32 <= 1 { + std::arch::x86_64::_rdtsc() + } else { + 0 + }; + context.Rax = cycles & 0xffff_ffff; + context.Rdx = cycles >> 32; ~~~~ --- Writes to RAX but leaves the flags as they are. See my comment

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!34
No description provided.