Console CPU instructions: monitorx / mwaitx are rewritten in place at their first fault #2618

Open
github-bot wants to merge 1 commit from github-bot/gh-2618 into main AGit
Owner

@⁠lukastaro on GitHub · 2026-11-26 02:27 UTC · original
lukastaro:pr-patch-waits → main · +86 −1 · 1 files · 1 commits · open

A title that paces its frames with monitorx / mwaitx spins on them. On a CPU without them (Intel) every one raises an illegal-instruction exception that eh emulates, the same instruction again and again. clwb is already rewritten to nops at its first fault; this does the same for these two:

  • monitorx → nops, mwaitx → pause + nops (a wait that ended at once; the title checks its own condition again, as it must after any mwaitx).
  • The bytes are written with one atomic 8-byte compare-exchange when the instruction lies inside an aligned 8-byte word, so a thread at the same instruction runs either the old one (and is emulated) or the new one, never a mix. Otherwise the instruction stays emulated as before.

Tested

  • NVIDIA GeForce RTX 5060 Ti (driver 621.40), Intel Core i7-14700K, Windows 10 22H2 (19045), release builds.
  • cargo test --workspace --no-fail-fast -- --test-threads=4 on 19aa5c2: 1426 passed, 11 failed, 47 ignored. On main b49be18 the same run: 1425 passed, 10 failed, 47 ignored, the same 10 failures (fuzz_validate_and_gpu_0–_3, the two link_tests, vram::tests::d32s8_targets_take_their_own_size_of_video_memory and the three test_title_* integration tests). The 11th, list_x::tests::a_reset_while_the_last_submit_runs_records_on_another_pc_list, passed 3 of 3 times alone.
  • New tests: monitorx_and_mwaitx_are_rewritten_at_their_first_fault (both rewritten, rip stepped over them), code_across_an_aligned_word_is_not_rewritten.
  • A retail title on the machine above (a lab tree with this change; same build and settings with and without it, one run each): two sites rewritten at 3.9 s; in its world 1.24 → 2–3.5 presents per second.

Regression run

main:

xcl-smoke v1
commit: b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7
gpu: NVIDIA GeForce RTX 5060 Ti
title: eccf25ce
seconds: 60
exit: 0x0
frames shown: 1489 (PresentX 1489)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15106 MB

PR:

xcl-smoke v1
commit: 19aa5c28b3ae4ed912c36c7a2fd18dae59d46c13
gpu: NVIDIA GeForce RTX 5060 Ti
title: eccf25ce
seconds: 60
exit: 0x0
frames shown: 1588 (PresentX 1588)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15252 MB

The 60 s stay in the title's menus; the two sites are rewritten in the first seconds.

Not tested

  • AMD CPUs: they run monitorx / mwaitx natively and never reach this path.
  • An instruction that crosses an aligned 8-byte word in a real title: it stays emulated, as before (covered by the second test only).
  • Other titles than the one above.

AI use

  • Claude Code (Claude Opus 5.5) wrote the change, the tests and this description; the runs above were made on the hardware named.

🤖 Generated with Claude Code

**@⁠lukastaro** on GitHub · 2026-11-26 02:27 UTC · [original](https://github.com/xcl-dev/xcl/pull/2618) `lukastaro:pr-patch-waits` → `main` · +86 −1 · 1 files · 1 commits · open A title that paces its frames with `monitorx` / `mwaitx` spins on them. On a CPU without them (Intel) every one raises an illegal-instruction exception that `eh` emulates, the same instruction again and again. `clwb` is already rewritten to nops at its first fault; this does the same for these two: - `monitorx` → nops, `mwaitx` → `pause` + nops (a wait that ended at once; the title checks its own condition again, as it must after any `mwaitx`). - The bytes are written with one atomic 8-byte compare-exchange when the instruction lies inside an aligned 8-byte word, so a thread at the same instruction runs either the old one (and is emulated) or the new one, never a mix. Otherwise the instruction stays emulated as before. **Tested** - NVIDIA GeForce RTX 5060 Ti (driver 621.40), Intel Core i7-14700K, Windows 10 22H2 (19045), release builds. - `cargo test --workspace --no-fail-fast -- --test-threads=4` on `19aa5c2`: 1426 passed, 11 failed, 47 ignored. On `main` `b49be18` the same run: 1425 passed, 10 failed, 47 ignored, the same 10 failures (`fuzz_validate_and_gpu_0`–`_3`, the two `link_tests`, `vram::tests::d32s8_targets_take_their_own_size_of_video_memory` and the three `test_title_*` integration tests). The 11th, `list_x::tests::a_reset_while_the_last_submit_runs_records_on_another_pc_list`, passed 3 of 3 times alone. - New tests: `monitorx_and_mwaitx_are_rewritten_at_their_first_fault` (both rewritten, rip stepped over them), `code_across_an_aligned_word_is_not_rewritten`. - A retail title on the machine above (a lab tree with this change; same build and settings with and without it, one run each): two sites rewritten at 3.9 s; in its world 1.24 → 2–3.5 presents per second. **Regression run** main: ``` xcl-smoke v1 commit: b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7 gpu: NVIDIA GeForce RTX 5060 Ti title: eccf25ce seconds: 60 exit: 0x0 frames shown: 1489 (PresentX 1489) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 15106 MB ``` PR: ``` xcl-smoke v1 commit: 19aa5c28b3ae4ed912c36c7a2fd18dae59d46c13 gpu: NVIDIA GeForce RTX 5060 Ti title: eccf25ce seconds: 60 exit: 0x0 frames shown: 1588 (PresentX 1588) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 15252 MB ``` The 60 s stay in the title's menus; the two sites are rewritten in the first seconds. **Not tested** - AMD CPUs: they run `monitorx` / `mwaitx` natively and never reach this path. - An instruction that crosses an aligned 8-byte word in a real title: it stays emulated, as before (covered by the second test only). - Other titles than the one above. **AI use** - Claude Code (Claude Opus 5.5) wrote the change, the tests and this description; the runs above were made on the hardware named. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
A title that paces its frames with monitorx / mwaitx spins on them; on a CPU
without them every one was an emulated illegal-instruction exception. Like
clwb, each site is now rewritten at its first fault: monitorx to nops, mwaitx
to pause + nops. The bytes go in with one atomic 8-byte compare-exchange when
the instruction lies inside an aligned 8-byte word; otherwise it stays emulated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016SP4yAAy7ztGPWPR7JHKKa
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:34:59 +00:00
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-26 02:35 UTC · original

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, 26 ignored.

Thanks!

— Devin

**@⁠DavidM1911** on GitHub · 2026-11-26 02:35 UTC · [original](https://github.com/xcl-dev/xcl/pull/2618#issuecomment-7180938707) Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, 26 ignored. Thanks! — Devin
@ -248,0 +271,4 @@
let mut bytes = was.to_le_bytes();
bytes[off..off + new.len()].copy_from_slice(new);
// another thread may have rewritten it first: then it holds the same bytes already
let _ = cell.compare_exchange(was, u64::from_le_bytes(bytes), Ordering::AcqRel, Ordering::Acquire);
Author
Owner

@⁠luckyteagirl on GitHub · 2026-11-26 03:15 UTC · original

A failed compare_exchange is currently ignored, but another thread may have rewritten a different monitorx/mwaitx site in this same aligned 8-byte word. If both load the original word, one CAS wins and the other loses; the losing site remains unchanged even though rewrite_code returns true, so later visits fault again. Could this retry after a lost CAS by reloading and merging only the requested bytes? A deterministic test with two adjacent sites would cover it. This is a source-derived interleaving, not a measured title failure.

**@⁠luckyteagirl** on GitHub · 2026-11-26 03:15 UTC · [original](https://github.com/xcl-dev/xcl/pull/2618#discussion_r4703493224) A failed `compare_exchange` is currently ignored, but another thread may have rewritten a *different* `monitorx`/`mwaitx` site in this same aligned 8-byte word. If both load the original word, one CAS wins and the other loses; the losing site remains unchanged even though `rewrite_code` returns `true`, so later visits fault again. Could this retry after a lost CAS by reloading and merging only the requested bytes? A deterministic test with two adjacent sites would cover it. This is a source-derived interleaving, not a measured title failure.
@ -248,0 +263,4 @@
// SAFETY: `word` is the aligned word around code the processor just fetched; it is made
// writable for the one store and given its protection back
unsafe {
if VirtualProtect(word as *const c_void, 8, PAGE_EXECUTE_READWRITE, &mut old) == 0 {
Author
Owner

@⁠luckyteagirl on GitHub · 2026-11-26 03:15 UTC · original

VirtualProtect(word, 8, ...) changes the whole containing page. Two concurrent calls on that page can save RX/RWX respectively; if the first restores RX before the second restores its saved RWX, the page stays writable and executable. SEEN is unlocked before this span. Could the protect/CAS/restore span be serialized for calls touching the same page, with a concurrent-patch test asserting the original protection afterwards? This is a source-derived interleaving, not a measured title failure.

**@⁠luckyteagirl** on GitHub · 2026-11-26 03:15 UTC · [original](https://github.com/xcl-dev/xcl/pull/2618#discussion_r4703493236) `VirtualProtect(word, 8, ...)` changes the [whole containing page](https://learn.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-virtualprotect). Two concurrent calls on that page can save RX/RWX respectively; if the first restores RX before the second restores its saved RWX, the page stays writable and executable. `SEEN` is unlocked before this span. Could the protect/CAS/restore span be serialized for calls touching the same page, with a concurrent-patch test asserting the original protection afterwards? This is a source-derived interleaving, not a measured title failure.
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-26 08:29 UTC · original

Re-verification on RTX 3060 Ti (Windows 11), new head merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, 27 ignored. The earlier merge conflict is resolved.

Thanks!

— Devin

**@⁠DavidM1911** on GitHub · 2026-11-26 08:29 UTC · [original](https://github.com/xcl-dev/xcl/pull/2618#issuecomment-7186654799) Re-verification on RTX 3060 Ti (Windows 11), new head merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, 27 ignored. The earlier merge conflict is resolved. Thanks! — Devin
Author
Owner

@⁠luckyteagirl on GitHub · 2026-11-26 03:15 UTC · original · reviewed

Two source-derived concurrency points on the current head. No local build or title run. AI-assisted source review: OpenAI Codex (GPT-6; exact serving variant unavailable) and a Luna subagent.

**@⁠luckyteagirl** on GitHub · 2026-11-26 03:15 UTC · [original](https://github.com/xcl-dev/xcl/pull/2618#pullrequestreview-6068075238) · **reviewed** Two source-derived concurrency points on the current head. No local build or title run. AI-assisted source review: OpenAI Codex (GPT-6; exact serving variant unavailable) and a Luna subagent.
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2618/head:github-bot/gh-2618'
git switch 'github-bot/gh-2618'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2618'
git switch 'github-bot/gh-2618'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2618'
git switch 'github-bot/gh-2618'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2618'
git switch 'main'
git merge --squash 'github-bot/gh-2618'
git switch 'main'
git merge --ff-only 'github-bot/gh-2618'
git switch 'main'
git merge 'github-bot/gh-2618'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.