Loader: clzero loops in one exception; rdpru #34
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/34"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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
maindoes 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
clzeroa 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:
main(one line per exception)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 frommainitself.The handler recognises the two loops compilers build around
clzeroby the instructions beside it and carries the whole loop out, leaving registers and flags as the last pass would:Anything else around a
clzerois 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-loaderon an i5-13600KF, where the instructions run through the handler: both loop shapes (end registers, zeroed lines, and flags compared with a nativecmp), andrdpru.🤖 Generated with Claude Code
Imported from Marvisak/VIctory. Originally posted by raffclar on 2026-11-23T23:49:03Z.
Review: COMMENTED
No description provided.
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.
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!
Imported from Marvisak/VIctory. Originally posted by DaviTostes on 2026-11-24T00:08:32Z.
Tested
faa0a9astacked 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.clzero/rdpruwork and its tests live in the Windows exception handler (victory-loader/src/crash.rsand the loader's Windows-gated tests), so the Linux run exercises the portable crates around it, not the emulation itself.AI-assisted (DeepSeek V4.1 Flash coding agent).
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:
cmp rax, raxfor the loop and left ZF wrong; #214 rejects that shape. That was a bug in this PR.rdprureview 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.
Imported from Marvisak/VIctory. Originally posted by raffclar on 2026-11-23T23:49:03Z.
Code review location:
crates/victory-loader/src/crash.rs; line 166Writes to RAX but leaves the flags as they are. See my comment
Pull request closed