Repository navigation
Conversation
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 <noreply@anthropic.com>
|
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! |
| 0 | ||
| }; | ||
| context.Rax = cycles & 0xffff_ffff; | ||
| context.Rdx = cycles >> 32; |
There was a problem hiding this comment.
Writes to RAX but leaves the flags as they are. See my comment
|
Tested
AI-assisted (DeepSeek V4.1 Flash coding agent). |
|
Closing in favour of #214, which rebases this over #45 and improves it. Two things from there worth repeating here:
Thanks to the reviewers and to the author of #214. |
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.
…sed, with a fix) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
…visak#34 rebased, with a fix)
…visak#34 rebased, with a fix)
…visak#34 rebased, with a fix)
Loader: clzero loops in one exception; rdpru (#34 rebased, with a fix)
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