Console CPU instructions: monitorx / mwaitx are rewritten in place at their first fault #2608
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2618"
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 ivall/xcl. Originally posted by lukastaro on 2026-11-26T02:27:07Z.
A title that paces its frames with
monitorx/mwaitxspins on them. On a CPU without them (Intel) every one raises an illegal-instruction exception thatehemulates, the same instruction again and again.clwbis 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 anymwaitx).Tested
cargo test --workspace --no-fail-fast -- --test-threads=4on19aa5c2: 1426 passed, 11 failed, 47 ignored. Onmainb49be18the same run: 1425 passed, 10 failed, 47 ignored, the same 10 failures (fuzz_validate_and_gpu_0–_3, the twolink_tests,vram::tests::d32s8_targets_take_their_own_size_of_video_memoryand the threetest_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.monitorx_and_mwaitx_are_rewritten_at_their_first_fault(both rewritten, rip stepped over them),code_across_an_aligned_word_is_not_rewritten.Regression run
main:
PR:
The 60 s stay in the title's menus; the two sites are rewritten in the first seconds.
Not tested
monitorx/mwaitxnatively and never reach this path.AI use
🤖 Generated with Claude Code
Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-26T02:35:12Z.
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
Imported from ivall/xcl. Originally posted by luckyteagirl on 2026-11-26T03:15:35Z.
Review: COMMENTED
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.
Imported from ivall/xcl. Originally posted by luckyteagirl on 2026-11-26T03:15:35Z.
Code review location:
crates/xcl/src/eh.rs; line 274A failed
compare_exchangeis currently ignored, but another thread may have rewritten a differentmonitorx/mwaitxsite 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 thoughrewrite_codereturnstrue, 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.Imported from ivall/xcl. Originally posted by luckyteagirl on 2026-11-26T03:15:35Z.
Code review location:
crates/xcl/src/eh.rs; line 266VirtualProtect(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.SEENis 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.Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-26T08:29:21Z.
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
Imported from ivall/xcl. Originally posted by kole07139 on 2026-11-26T16:31:40Z.
The case in your "Not tested" list (an instruction across an aligned 8-byte word in a real title) happens on another build of the retail title (
title: 16b5e5d4), and it is themonitorxsite: onlymwaitxis rewritten, somonitorxstays emulated every time it runs. Two runs per arm show no change in the world's present rate, but that does not test the PR's idea while half of it is inactive.What the log shows
I added a local log line (not part of the PR) in the two new match arms, printing what
rewrite_codereturned:In the title's
.text,+0x…bd7holds0F 01 FA:monitorx, 3 bytes, no prefix, atbd7–bd9. Sooff = 7andoff + len > 8.rewrite_coderefuses it by design, which is the casecode_across_an_aligned_word_is_not_rewrittentests. The image base is 64 KiB-aligned, so this happens on every run of this build. Your runs usedtitle: eccf25ce, where both sites were rewritten, so that build'smonitorxmust lie inside one word.A possible way to cover it (not tried):
bd7–bd9lie inside the aligned 16-byte blockbd0–bdf. Alock cmpxchg16bon that block would give the same guarantee as the 8-byte exchange: a thread runs either the old instruction or the new one.Numbers
Same tree, same cache snapshot copied for each run, 300 s each, runs alternated (3d, 3e, 3d, 3e). Presents are counted in fixed windows after the title's first
ListX #101, which is where it enters the world:#101From 30 s after entry, all four runs present about 0.23 times a second with or without the PR. At that rate this tree is bound by something other than the title's CPU pacing loop. So these runs do not show whether the rewrite helps:
monitorxis still emulated, and the frame rate is held down elsewhere.Tested
monitorx/mwaitx), 32 GB RAM, Windows 11 26200, release builds.main6d12f58+ #2404, #2097, #684, #751, #1803, #2229, #2384, #1975, then + #2590 + #2753. With and without this PR at19aa5c2, which applies cleanly.XCL_DIRECT_DXIL=1 XCL_HEAP_WINDOWS=1 XCL_TEXTURE_LEVEL_PAGES=1 XCL_PSO_WAIT_MS=50, A pressed every 5 s, warm DXIL and driver-blob cache. The NVIDIA shader heap was 1 GiB in all four runs (xcl nvprofile install --mb 1024, #2590).cargo test -p xcl --lib eh::on the tree with the PR: 8 passed, 0 failed, 0 ignored.Not tested
title: eccf25ce; a version that also rewrites amonitorxacross a word; more than two runs per arm; AMD (not relevant: AMD CPUs run these natively).AI use
Imported from ivall/xcl. Originally posted by throny on 2026-11-26T18:28:32Z.
Test report: in the world on an Intel CPU with an RTX 4080 16 GB. No gain here, because this machine waits for the GPU; one figure worth a look in the intro.
Tested
git apply, as it is) onnvidia-devaf2cecc4+ twelve other open PRs (#684, #2697, #2750, #2749, #2729, #2794, #2817 stack, #2768, #2782, #2725, #2581, #2422),XCL_WATCH_HOT=0. The retail title of #505, Series X profile. Two world runs with it, three of the same tree without it just before.PresentX, see #2729), so the CPU time this PR gives back cannot show as frames here. That fits your numbers coming from a setup where the CPU was the limit.pauseloop where the title expected to sleep could show up exactly like that.Not tested
AI use: Claude Code (Claude Fable 5.2) ran this on the owner's machine and wrote the report.
Imported from ivall/xcl. Originally posted by voiceovarian1-max on 2026-11-27T11:39:14Z.
A second build with the case @kole07139 found, and what we use for it.
The site: in the retail title's 1.0.2.1 build (
game_scarlett_master.exe),monitorxat +0x4fb0bd7 crosses the aligned word at +0x4fb0bd8;mwaitxat +0x4fb0c00 does not (checked in the executable). So with this PR only themwaitxis rewritten there, and themonitorxkeeps faulting at every pass of the wait loop. Under Wine the pair faulted ~6,000 times a second before any rewrite: a core's worth of exception handling.A fallback for that case: change only the byte after
0F:0F 01 FA/FBbecomes0F 1F FA/FB, a multi-byte NOP of the same length. A one-byte store is atomic at any alignment, so another thread still sees either the old instruction or the new one. A thread whose fault is handled after the rewrite finds0F 1F FA/FBand is resumed as is. On our fork it runs whenrewrite_codereturns false, on top of this PR: e9f4c457, with a test for a site across a word. An earlier version of ours used only this one-byte form for every site, and the title's world ran with it through several 200 s runs.Tested: Linux, Wine 11 (Proton Experimental), i7-14700K (no
monitorx/mwaitx), this PR's head19aa5c28carried on nvidia-dev plus the fallback.cargo test -p xcl --lib eh::under Wine: 13 passed, 0 failed (this PR's two tests among them). Not tested: Windows, and a retail run with this exact combination.Written with Claude Code (Claude Opus 5.5).
Imported from ivall/xcl. Originally posted by VolenNakov on 2026-11-27T12:11:01Z.
@voiceovarian1-max, a Windows retail run with your fallback (
e9f4c457) on top of this PR, the combination your "Not tested" names. On an i7-8700 the world's present rate went from 3.30 to 4.60 presents/s, and the GPU went from 75 % to 88 % busy. One run per arm, so take it as a first data point.Setup
monitorx/mwaitx; 6 cores / 12 threads); 32 GB RAM; Windows 11 26200; release. Retail titleea3b5e4dof #505, Series X profile. Itsmonitorxis at+0x4fb0bd7and itsmwaitxat+0x4fb0c00in the log, the same sites you name.main08db802+ #2526, #2097, #2404, #2590, #2615, #751, #2753, #2759, #759 / #1826, #2844 (fc007a9), #2934 (7692dd87,c230aa1b), and this PR at its head (19aa5c28, identical byrange-diff). The "with" arm addse9f4c457(git am, clean).XCL_NETWORK=0 XCL_DIRECT_DXIL=all XCL_TEXTURE_LEVEL_PAGES=1 XCL_SPLIT_INDIRECT_ROUNDS=2 XCL_DIRECT_SPLIT_INSTRUCTIONS=0 XCL_WATCH_TRAP=1, 2 GiB shader heap (#2590), warm caches, 900 s.e9f4c457nvidia-smievery 5 s, 200–880 s)sparse windows timing: write-watchshare of the runmonitorxfaults, but I have not shown the fault count directly.Not tested: more than one run per arm; a fault count with and without;
cargo test -p xcl --lib eh::on this tree, whose test build fails on an unrelated conflict between two of the other PRs; the intro's 30 fps figure that @throny asked about.AI use: Claude Code (Claude Opus 5.5) ran the runs, read the logs and wrote this.
Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T08:29:17Z.
Triage (#1607) – head
19aa5c2Main commit
d634138(merged with #2959) is adapted from this PR. Ineh.rs,rewrite_code(at, expected, new)turns monitorx into nops and mwaitx intopause+ nops at the first fault, with an atomic aligned-8-byte replacement, as here.Main also checks the expected bytes, serializes the rewrite under
CODE_REWRITE, and records rewritten sites so a fault already in progress at a just-rewritten site is recognized. #1829 only adds tests for the emulation.Recommendation: close (superseded by main commit
d634138).AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.
Imported from ivall/xcl. Originally posted by throny on 2026-12-01T12:55:32Z.
Closing as superseded: this change is already on
main(d634138c). main eh.rs rewrite_code (atomic aligned-word replace, serialized, with expected bytes) rewrites monitorx to nops and mwaitx to pause+nops at first fault; superset of PR with testsTwo independent checks found this: Foniass's close list on #1607 and ours against current
main. If something here is still missing onmain, say so and reopen it, or ask and I will.AI use: Claude Code (Claude Opus 5.5); the per-PR comparison was done by Claude Sonnet 5.5 sub-agents.
Pull request closed