Skip to content

Loader: clzero loops in one exception; rdpru - #34

Closed
ka1qux wants to merge 1 commit into
Marvisak:mainfrom
ka1qux:clzero-loops-rdpru
Closed

ka1qux wants to merge 1 commit into
Marvisak:mainfrom
ka1qux:clzero-loops-rdpru

Conversation

@ka1qux

@ka1qux ka1qux commented Nov 23, 2026

Copy link
Copy Markdown

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

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>
@raffclar

Copy link
Copy Markdown
Contributor

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!

0
};
context.Rax = cycles & 0xffff_ffff;
context.Rdx = cycles >> 32;

@raffclar raffclar Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@DaviTostes

Copy link
Copy Markdown

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 Build: make workspace tests run on Linux #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

ka1qux commented Nov 24, 2026

Copy link
Copy Markdown
Author

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.

@ka1qux ka1qux closed this Nov 24, 2026
muhammed-aksoy pushed a commit to muhammed-aksoy/VIctory that referenced this pull request Nov 24, 2026
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.
edsuuu referenced this pull request in edsuuu/VI-BR-PORT Nov 24, 2026
…sed, with a fix)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Makto94 added a commit to Makto94/VIctory that referenced this pull request Nov 24, 2026
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.
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
Marvisak added a commit that referenced this pull request Nov 25, 2026
Loader: clzero loops in one exception; rdpru (#34 rebased, with a fix)
zebraso pushed a commit to zebraso/VIctory that referenced this pull request Nov 25, 2026
…n narrow-wave GPUs (Marvisak#34)

Restricts automatic divergent barrier sync omission to single-console-wave groups (group_threads <= console_wave), preserves memory fences and explicit VICTORY_GROUP_SYNC overrides, and adds unit and live narrow-wave GPU tests.
H4itz pushed a commit to H4itz/victory that referenced this pull request Nov 26, 2026
…h is looked up by a quick hash and its record words are compared, instead of a SipHash over them (a collision now costs a copy, not wrong descriptors)

Read line by line before merging; its helpers and Marvisak#33's stand side by side.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
iLucasRT pushed a commit to iLucasRT/VInAMD that referenced this pull request Nov 26, 2026
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.

(cherry picked from commit 0de9efc)
Callimann8 pushed a commit to Callimann8/VIctory-fork that referenced this pull request Nov 26, 2026
- VICTORY_PIPELINE_ONCE=1 (PR Marvisak#25): a pipeline a list binds again is not
  kept, cast and named again
- VICTORY_TABLE_QUICK_HASH=1 (PR Marvisak#34): fresh tables found by a quick hash,
  records compared (a collision costs a copy, not wrong descriptors)
- VICTORY_GPU_WRITE_ONCE=1 (PR Marvisak#29): a range shaders write is declared once
  per list until a title copy or run
- VICTORY_UPLOAD_POOLED=1 (PR Marvisak#30): buffer-to-texture staging from the
  list's scratch chunks instead of a committed buffer per copy
- VICTORY_FILL_ONCE=1 (PR Marvisak#24): one scratch fill per source per list until
  a title copy or run (unaligned root constant buffers)
- root constants on the stack instead of a Vec per draw (PR Marvisak#33, no
  behaviour change)
PR Marvisak#27 (heap without pending lock) was already in.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01YZ2PrBCXiMRWPWfcXFvb59
justr4mzi pushed a commit to justr4mzi/VIctory that referenced this pull request Nov 26, 2026
Title memory has guard pages: stack guards, and guards the title sets up
itself. A copy of title memory (safecopy: victory_safe_copy, _put and
_merge, under the write-catch handler in watch.rs) that touched one got
STATUS_GUARD_PAGE_VIOLATION (0x80000001), and the OS cleared the page's
guard flag. on_write sent every fault inside a copy to the copy's failing
exit, whatever the code, and nothing set the flag again. The page stayed an
ordinary page, so the title's next touch of it no longer raised the guard
fault it was set up for. Under Proton the process then exits silently 1-3
minutes into a world; a traced run logged two 0x80000001 among three
million access violations. That the lost guard causes the exit is the
reading of that trace, not something reproduced here.

A guard fault inside a copy now sets the guard flag again on its page, with
the protection the page has, and fails the copy as before. A guard fault
outside a copy still goes on to the next handler (CONTINUE_SEARCH).

The direct copy (VICTORY_DIRECT_COPY) had the same bug; klbf-lab PR Marvisak#34
(commit 478c6ef) fixed it there. This is the same fix for safecopy.

Test: safecopy::tests::a_guard_page_fails_the_copy_and_stays_a_guard_page.
Under Proton Experimental it fails without this change ("the guard flag was
consumed") and passes with it.
justr4mzi pushed a commit to justr4mzi/VIctory that referenced this pull request Nov 26, 2026
…ir defaults; xtask checks every VICTORY_* name in the Markdown is read

Checked every VICTORY_* name in the Markdown files against the source.
One was dead: VICTORY_MERGE_RACE (its counter was taken out after
8dd30ab); the line keeps the finding and drops the switch.
VICTORY_LATE_WAIT (host.rs late_stage: 1, 2, 3) and VICTORY_WIDE_REFRESH
(host.rs wide_refresh: 0, 1, else on at four or more CPUs) are read, as
documented; VICTORY_LATE_WAITS in frame.rs is a different switch.

What the docs had wrong, by the code:
- VICTORY_RELAY (stage 2), VICTORY_LATE_WAIT (stage 3 with the relay)
  and VICTORY_WIDE_BACK are on by default, not off; the frame-rate line
  says these are now the defaults (Marvisak#34 copied it and saw no change).
- VICTORY_CARD_ROOM_FOR_BUFFERS is on by default (=0 as before).
- VICTORY_PAGE_WRITES opens the guards by default (=0 counts only).
- VICTORY_CARD_BUFFERS_MIN_KB, when above 0, wins over _MIN_MB: said
  in both places that name them.

xtask: a test that every VICTORY_* name in a tracked Markdown file is
quoted in at least one Rust file outside xtask, as every switch is read.

Fixes Marvisak#49

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
XFlawless1 pushed a commit to XFlawless1/VIctory that referenced this pull request Nov 26, 2026
Title memory has guard pages: stack guards, and guards the title sets up
itself. A copy of title memory (safecopy: victory_safe_copy, _put and
_merge, under the write-catch handler in watch.rs) that touched one got
STATUS_GUARD_PAGE_VIOLATION (0x80000001), and the OS cleared the page's
guard flag. on_write sent every fault inside a copy to the copy's failing
exit, whatever the code, and nothing set the flag again. The page stayed an
ordinary page, so the title's next touch of it no longer raised the guard
fault it was set up for. Under Proton the process then exits silently 1-3
minutes into a world; a traced run logged two 0x80000001 among three
million access violations. That the lost guard causes the exit is the
reading of that trace, not something reproduced here.

A guard fault inside a copy now sets the guard flag again on its page, with
the protection the page has, and fails the copy as before. A guard fault
outside a copy still goes on to the next handler (CONTINUE_SEARCH).

The direct copy (VICTORY_DIRECT_COPY) had the same bug; klbf-lab PR Marvisak#34
(commit 478c6ef) fixed it there. This is the same fix for safecopy.

Test: safecopy::tests::a_guard_page_fails_the_copy_and_stays_a_guard_page.
Under Proton Experimental it fails without this change ("the guard flag was
consumed") and passes with it.

(cherry picked from commit fa11650)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
arnoudstorm added a commit to arnoudstorm/VIctory that referenced this pull request Nov 26, 2026
…arvisak#34-Marvisak#35

VICTORY_PIPELINE_WORKERS (1-8) sets how many pipeline workers translate
shaders at once (default: half the logical processors, 2 to 8), to lower
the RAM peak of a cold shader cache. Outside the translator fingerprint,
so the cache stays warm.

Diagnostics counters say why each texture refresh hashed: no guard,
aliased, the guard saw a write, the eighth-frame turn, partly mapped.

Docs: Test Marvisak#34 (600 s, 2.1 fps, hair streaks and black skin seen) and
Test Marvisak#35 (loop limit default, stopped by the emergency watchdog during
cold translation).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

3 participants