cpu_emulation: rewrite bare MONITORX/MWAITX in place at first #UD (RT-MONITORX-PATCH) #1576
Closed
leonida/mandy
madhyn wants to merge 0 commits from
feat/monitorx-mwaitx-inplace-patch into main
pull from: feat/monitorx-mwaitx-inplace-patch
merge into: leonida:main
leonida:main
leonida:github-pr/3295
leonida:beta
leonida:docs/changelog-3483-3500
leonida:github-pr/3294
leonida:github-pr/2975
leonida:github-pr/2815
leonida:feat/gate-driver-start
leonida:feat/skip-pipelines-release
leonida:perf/lazy-compare
leonida:perf/mwaitx-wait
leonida:test/prio-ceiling-main
leonida:fix/ab-cpu-stdout
leonida:fix/gate-old-crashes
leonida:diag/ab-cpu
leonida:test/main-0312
leonida:github-pr/3291
leonida:diag/indirect-clamp-census
leonida:feat/barrier-enhanced
leonida:perf/texture-frame-ceiling
leonida:perf/dstorage-threads
leonida:fix/console-queue-dma-tables
leonida:fix/port-descriptors-record-parts
leonida:fix/frame-waits-fence
leonida:fix/console-sampler-words
leonida:fix/graphics-userdata-spill
leonida:fix/xg-reserved-layout-rotation
leonida:fix/barrier-console-flags
leonida:fix/switches-wait-test-child
leonida:test/list-value-waits
leonida:test/lot-gxdk-0412
leonida:fix/bvh-compacted-size
leonida:docs/i18n-quickstarts
leonida:feat/native-pipeline-slots
leonida:fix/clippy-leonida-gpu
leonida:fix/list-value-producers
leonida:fix/descriptor-page-straddle
leonida:fix/xmem-low-window
leonida:fix/ffx-jitter-never-zero
leonida:fix/run-stdout-closed
leonida:github-pr/2892
leonida:github-pr/2896
leonida:feat/warm-save-every
leonida:github-pr/2980
leonida:github-pr/2994
leonida:github-pr/3005
leonida:github-pr/3010
leonida:github-pr/3136
leonida:github-pr/3303
leonida:github-pr/3220
leonida:github-pr/3143
leonida:github-pr/3330
leonida:github-pr/3226
leonida:github-pr/3187
leonida:carry/3395
leonida:github-pr/2946
leonida:github-pr/2947
leonida:github-pr/2945
leonida:github-pr/2983
leonida:github-pr/3001
leonida:github-pr/3372
leonida:github-pr/3128
leonida:github-pr/3296
leonida:github-pr/2588
leonida:github-pr/3119
leonida:github-pr/3378
leonida:fix/crash-diagnostics
leonida:feat/dlss-mip-bias
leonida:fix/retired-out-of-order
leonida:fix/dxr-postbuild-barrier
leonida:fix/amd-relay-default
leonida:fix/removed-report-wait
leonida:feat/heap-twin
leonida:feat/vram-margin
leonida:github-pr/2472
leonida:github-pr/3123
leonida:github-pr/3088
leonida:assets/annonce-v1
leonida:github-pr/2794
leonida:github-pr/3300
leonida:github-pr/3304
leonida:github-pr/3308
leonida:github-pr/3367
leonida:github-pr/3369
leonida:fix/launcher-dlss-diagnostics
leonida:github-pr/3269
leonida:github-pr/2957
leonida:github-pr/3205
leonida:fix/crash-zero-guard
leonida:github-pr/3185
leonida:github-pr/3099
leonida:github-pr/3104
leonida:github-pr/3348
leonida:github-pr/3379
leonida:github-pr/3377
leonida:perf/darner-title-priority-ceiling
leonida:github-pr/3376
leonida:gh-readonly-queue/main/pr-3311-b5c45b663dbc5a9c3532bb3f307e5f81b32faefa
leonida:gh-readonly-queue/main/pr-3088-af22e7847ff5808f72d1a1881c24be2fafd20b54
leonida:gh-readonly-queue/main/pr-3123-7f3fa026a94c6646e89244f28e1ce22290361867
leonida:github-pr/3373
leonida:github-pr/3368
leonida:github-pr/3198
leonida:gh-readonly-queue/main/pr-3354-637baa0b77ba47222e7280b17c1a59c6a1781d57
leonida:gh-readonly-queue/main/pr-3365-d7a0e0196a43834813014b4bd3aa5502a8263b14
leonida:gh-readonly-queue/main/pr-3361-5dfe8bbd05ed12fb2f2e1718d9af77a7c5f46fd8
leonida:gh-readonly-queue/main/pr-3366-8da9148204e9d12c585efa9ac2b7cc1ab08c7682
leonida:github-pr/3346
leonida:github-pr/3375
leonida:github-pr/2958
leonida:github-pr/3151
leonida:github-pr/3307
leonida:github-pr/3278
leonida:github-pr/3374
leonida:fix/runtime-test-conservative-fixtures
leonida:github-pr/3366
leonida:ci/shard-semantic-linux
leonida:github-pr/3354
leonida:docs/fg-limit-fps-switch
leonida:github-pr/3365
leonida:fix/read-file-hard-link
leonida:github-pr/3364
leonida:fix/book-slot-heap-never-read
leonida:github-pr/3361
leonida:github-pr/3263
leonida:github-pr/3360
leonida:github-pr/3353
leonida:github-pr/3331
leonida:fix/alias-later-queued-submissions
leonida:github-pr/3352
leonida:github-pr/3228
leonida:github-pr/3349
leonida:claude/gallant-sagan-aamatr
leonida:github-pr/3200
leonida:github-pr/3132
leonida:github-pr/3323
leonida:github-pr/3332
leonida:github-pr/3340
leonida:github-pr/3311
leonida:github-pr/3334
leonida:github-pr/3297
leonida:github-pr/3256
leonida:github-pr/2567
leonida:github-pr/2851
leonida:d3d12/agility-sdk-621
leonida:github-pr/3164
leonida:github-pr/3114
leonida:github-pr/3050
leonida:github-pr/3113
leonida:github-pr/3326
leonida:github-pr/3144
leonida:github-pr/2642
leonida:github-pr/3072
leonida:github-pr/2789
leonida:github-pr/2643
leonida:github-pr/3009
leonida:github-pr/3035
leonida:github-pr/3031
leonida:github-pr/2686
leonida:github-pr/2834
leonida:github-pr/3321
leonida:github-pr/3325
leonida:github-pr/3290
leonida:github-pr/3324
leonida:github-pr/3130
leonida:claude/fervent-curie-m19lnr
leonida:github-pr/3309
leonida:github-pr/3305
leonida:github-pr/3306
leonida:github-pr/3302
leonida:github-pr/3301
leonida:github-pr/3345
leonida:github-pr/3211
leonida:github-pr/3027
leonida:github-pr/3286
leonida:github-pr/3235
leonida:github-pr/3265
leonida:github-pr/3275
leonida:codex/skip-no-hit-ray-passes
leonida:github-pr/2823
leonida:github-pr/2911
leonida:github-pr/3262
leonida:github-pr/3125
leonida:github-pr/3206
leonida:github-pr/3250
leonida:github-pr/3238
leonida:github-pr/3188
leonida:github-pr/2952
leonida:github-pr/3217
leonida:github-pr/3168
leonida:t3/preserve-fsr-sdk-restart-path
leonida:t3/propagate-host-failure-hresult
leonida:github-pr/3209
leonida:t3/launcher-custom-parameters
leonida:github-pr/3207
leonida:github-pr/3183
leonida:github-pr/3160
leonida:github-pr/3201
leonida:ci/less-to-run
leonida:github-pr/2930
leonida:github-pr/2797
leonida:github-pr/2999
leonida:perf/native-compute-shader-forms
leonida:github-pr/3191
leonida:github-pr/3011
leonida:github-pr/3019
leonida:github-pr/2996
leonida:gh-readonly-queue/main/pr-2789-45dc41f95c1cd0dcc29c2c50582e0e9af8a77259
leonida:claude/blissful-franklin-ukp7ag
leonida:github-pr/3141
leonida:github-pr/3156
leonida:github-pr/3150
leonida:github-pr/3153
leonida:github-pr/3163
leonida:github-pr/3142
leonida:github-pr/3139
leonida:github-pr/2841
leonida:github-pr/3069
leonida:github-pr/3098
leonida:github-pr/3107
leonida:github-pr/3071
leonida:github-pr/2774
leonida:github-pr/2889
leonida:github-pr/3075
leonida:github-pr/2661
leonida:github-pr/3064
leonida:github-pr/3054
leonida:github-pr/3049
leonida:github-pr/2855
leonida:github-pr/2943
leonida:github-pr/3016
leonida:github-pr/2887
leonida:github-pr/3006
leonida:github-pr/3003
leonida:github-pr/2993
leonida:notes/card-time-and-plant-flicker
leonida:github-pr/2989
leonida:feat/vulkan-rs
leonida:github-pr/2985
leonida:github-pr/2951
leonida:github-pr/2982
leonida:github-pr/752
leonida:github-pr/792
leonida:github-pr/2877
leonida:github-pr/2654
leonida:github-pr/2893
leonida:github-pr/2948
leonida:github-pr/2944
leonida:github-pr/2919
leonida:github-pr/2915
leonida:archive/ci/less-to-run/20261202T1121Z
leonida:github-pr/2800
leonida:github-pr/2822
leonida:github-pr/2833
leonida:github-pr/2840
leonida:github-pr/2831
leonida:github-pr/2832
leonida:github-pr/2801
leonida:github-pr/2890
leonida:github-pr/2891
leonida:github-pr/2664
leonida:github-pr/2807
leonida:rt-call-trace
leonida:github-pr/2755
leonida:perf/ahead-split
leonida:github-pr/2587
leonida:github-pr/2698
leonida:github-pr/2609
leonida:github-pr/3219
leonida:docs/readme-runtime-proposal
leonida:github-pr/2017
leonida:github-pr/1741
leonida:github-pr/1667
leonida:codex/coord-claim-check-20261124-7e48
No reviewers
Assignees
Clear assignees
No assignees
Labels
Clear labels
bug
Something isn't working
dependencies
Pull requests that update a dependency file
documentation
Improvements or additions to documentation
enhancement
New feature or request
missing claim
Needs a claim in #14
needs rebase
Conflicts with the base branch: rebase it on main
needs-triage
Needs maintainer evaluation before the next triage state
question
Further information is requested
ready-for-agent
Fully specified and ready for an AFK agent
submodules
Pull requests that update submodules code
No labels
bug
dependencies
documentation
enhancement
missing claim
needs rebase
needs-triage
question
ready-for-agent
submodules
Projects
Clear projects
No items
No project
Milestone
Clear milestone
No items
No milestone
Dependencies
No dependencies set
Notifications
1 participant
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".
No due date set.
Reference
leonida/mandy!1576
Loading…
Reference in a new issue
No description provided.
Delete branch "feat/monitorx-mwaitx-inplace-patch"
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?
Pull request closed
This pull request cannot be reopened because the branch was deleted.
Claimed in the coordination issue as:
RT-MONITORX-PATCH(new item; coordination issue #14 is at GitHub's 2,500-comment cap with no open successor at PR time — confirmed 0 open PRs touchMONITORX/MWAITXpatching; placed cleanly apart from #1472'sCLWBhelper so the two do not conflict).What and why
On Intel hosts (
CPUID.80000001H:ECX[29] == 0), AMD-onlyMONITORX(0F 01 FA) andMWAITX(0F 01 FB) instructions inside spin-wait loops currently raise#UDon every loop iteration, paying full Windows/Wine VEH exception dispatch overhead repeatedly.This change rewrites bare 3-byte
MONITORX/MWAITXsites in place on their first#UDinruntime/src/cpu_emulation.h:MONITORX(0F 01 FA) -> canonical 3-byteNOP(0F 1F 00)MWAITX(0F 01 FB) ->PAUSE; NOP(F3 90 90)Patching uses a page-bounded atomic 32-bit
InterlockedCompareExchangeunder anSRWLOCK(switching the target page temporarily toPAGE_EXECUTE_READWRITEand restoring its prior protection plusFlushInstructionCache). When a 3-byte site starts at page offset0xFFD, the 4-byte CAS shifts back 1 byte so the write never crosses a page boundary. Sites starting at0xFFE/0xFFFor prefixed forms (prefix_len > 0) fall back to existing per-fault emulation. SetLEONIDA_MONITORX_PATCH=0to disable in-place patching.Tested
runtime/test/cpu_emulation_test.cppwithclang-cl --target=x86_64-pc-windows-msvc /std:c++20 /O2 /W4 /WX(0 warnings) and ran under Wine 11.22 on Arch Linux (Intel Core i7-12700,MONITORX 0):MONITORX(0F 1F 00) andMWAITX(F3 90 90), 0 repeat#UDfaults on subsequent executions, prefixed fallback,0xFFDpage-end shift-back, and0xFFEpage-straddling fallback —cpu_emulation: all checks passed.LEONIDA_MONITORX_PATCH=0: verified bytes remain unpatched and per-fault emulation path stays intact —cpu_emulation: all checks passed.python3 scripts/check_no_game_data.py: passed.GPUs tested: n/a (CPU instruction emulation only; tested on Intel Core i7-12700, Arch Linux x86_64 / Wine 11.22).
AI-assisted: yes (Antigravity)
Checklist
mainand merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed. (n/a: no translator change)Rebased on
mainand fixed the Windows CI failure incpu_emulation_test: the twotwo_pagesboundary cases (0xFFDand0xFFE) previously entered directly withECX = lines != 0and uninitializedEAX/EDX, which triggers#GP(0)instead of#UDon AMD EPYC runners withMONITORX=0under Hyper-V. Added valid register prologues before the boundary instructions and flushed the instruction cache afterVirtualProtect. Re-verified locally under Wine withclang-cl /W4 /WX(default mode andLEONIDA_MONITORX_PATCH=0).Hi @madhyn, thanks for this pull request.
CONTRIBUTING.md asks contributors to claim their work in the coordination issue ("Coordinating who works on what", currently #1608) before opening a pull request: post
claiming: <ID>there, or a one-line description if the item is not listed yet, and put the ID in the pull request. I could not find a claim from you for this one (the IDRT-MONITORX-PATCHappears in the pull request, but there is noclaiming:line for it).The claim matters because "it is claimed in the coordination issue by the pull request's author" is the first item on the maintainers' checklist for a mergeable pull request, and because it lets others see that the work is taken before they write it a second time.
Could you post the claim in #1608 and add the ID to the pull request description? If you already claimed it in another form, a link to your comment is enough.
AI-assisted (Claude Code, Claude Opus 5.5).
Independent Verification Report —
44e437bda4+origin/main(a8465211fb)Environment: Windows 11 (
26200), AMD Ryzen 9 5950X, MSVC19.44.35228(x64,/std:c++20 /EHsc /W4 /permissive- /utf-8, Debug/Od /MDd& Release/O2 /MD).Verdict: C++ implementation & silicon probe PASS; 1 CI doc-check blocker (
tests/test_docs_env_vars.py) noted below — clean Git merge withorigin/main(a8bcfd49d2c2).1. C++ Build & Hardware Probe Verification
cpu_emulation_test(Debug/Od /MDd& Release/O2 /MD): PASS (0warnings under/W4 /permissive-).MONITORX/MWAITXnatively (host.monitorx == true), we also compiled and ran a dedicated directrt::cpu_emulation::patch_monitorx_mwaitx+ live-execution harness onPAGE_EXECUTE_READallocations:MONITORX(0F 01 FA->0F 1F 003-byte NOP) andMWAITX(0F 01 FB->F3 90 90PAUSE; NOP): PASS (rewritten atomically via 32-bitInterlockedCompareExchangeand executed cleanly).0xFFDshift-back 4-byte atomic window (window_addr = rip - 1 = 0xFFC): PASS (rewrites[0xFFD..0xFFF]while preserving[0xFFC] == 0x90and[0x1000] == 0xC3).0xFFE([0xFFE..0x1000]) and prefixedlength == 4(66 0F 01 FA): PASS (rejected cleanly without touching code bytes so per-fault emulation handles them).2. CI Blocker (
tests/test_docs_env_vars.py)python -I tests/test_docs_env_vars.pyon44e437bda4merged withorigin/mainfails (rc = 1):AssertionError: read by the code and named in no .md file: LEONIDA_MONITORX_PATCH (runtime/src/cpu_emulation.h:302)LEONIDA_MONITORX_PATCH(LEONIDA_MONITORX_PATCH=0disables in-placeMONITORX/MWAITXpatching) indocs/cpu_features.mdorruntime/README.mdalongsideLEONIDA_CLWB_PATCH.AI-assisted.
Verified by OreoFR
c1ecf04ed+ PR head44e437bda; green run in a merge package of 15 PRs, no new ctest failure vs mainctest -R ^cpu_emulation$AI-assisted (Claude Code, Claude Opus 5.5).
Fix proposal for a race in this PR's MONITORX/MWAITX rewrite (OreoFR)
@madhyn: the in-place rewrite here has the same race CLWB had before #2559. When several threads fault on the same MONITORX or MWAITX site at once, the ones whose #UD is handled after another thread rewrote the site decode
0F 1F 00/F3 90 90asOp::noneand returnEXCEPTION_CONTINUE_SEARCH, which ends the title.Fix (one commit on top of this PR's head
44e437bda, merged with current main):github.com/OreoFR/leonida@1844fc670d(branchOreoFR:fix/monitorx-patch-race),runtime/src/cpu_emulation.h+91/-61 andruntime/test/cpu_emulation_test.cpp+147:code_patch_lock(), shared withpatch_clwb: both rewrites unprotect, re-check, write and re-protect the same code pages; with a lock each, one could set a page back to execute-read while the other writes into it (a separate lock made the mixed test hang: ctest timeout, 5 runs out of 5);monitorx_sites()counts each site once, by the thread that wrote it.Tests:
test_monitorx_rewrite(first #UD, a second #UD on the rewritten site, 16 threads on 2048 fresh execute-read sites) andtest_mixed_rewrite(CLWB, MONITORX and MWAITX sites side by side on 6 pages, 16 threads). Before the fix 30,304 of 32,768 concurrent #UDs were passed on (29,599 of 49,152 in the mixed case); with it every check passes, 10 runs out of 10. Windows 11, RTX 4070 Laptop 8 GB, NVIDIA 621.40, MSVC RelWithDebInfo.Feel free to cherry-pick it into this PR (it is GPL like the rest). If there is no answer in 48 h, I will open it as a small PR of its own that depends on this one, with you as co-author.
AI-assisted (Claude Code, Claude Opus 5.5).
Ready for testing: merges cleanly with
main3535eb5c4. Not inmainyet:git cherryfinds 2 commit(s) not there, and 5 of the 102 lines it adds are already present. No duplicate found: its claimRT-MONITORX-PATCHappears in no other open pull request and in nomaincommit.AI-assisted (Claude Code).
RUN report for #1576 (RT-MONITORX-PATCH)
Heads: PR head
44e437bda, based on5e95fa2f4; origin/main91bab934d(merged locally withgit merge --no-edit --no-ff, clean, 2 files:runtime/src/cpu_emulation.h,runtime/test/cpu_emulation_test.cpp).Commands (VS 2022 Build Tools env from vcvars64, VS cmake + ninja,
-G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DLEONIDA_REQUIRE_VULKAN=ON, SDL3 static found via SDL3_DIR):cmake --build runtime/build -j 8 --target cpu_emulation_testthenctest -R "^cpu_emulation$" --output-on-failure(head and red)cmake --build runtime/build -j 8 --target gta6rt cpu_emulation_testthen the same ctest (main and merged)Results:
git checkout origin/main -- runtime/src/cpu_emulation.hat the PR head): the test builds and fails, 5 checks FAIL ("monitorx rewritten in place as 3-byte NOP", "mwaitx rewritten in place as pause; nop", "rewritten monitorx/mwaitx runs without faulting again", "page-end mwaitx at 0xFFD patched without touching adjacent bytes", "page-end rewritten mwaitx runs again without faulting"). The prefixed and page-straddling checks pass in that state, as they should (they expect emulation).cpu_emulationPassed. The host reports MONITORX 0 here, so the patch path runs (3 sites rewritten, including the 0xFFD shift-back case). WithLEONIDA_MONITORX_PATCH=0the exe also passes (opt-out branch of the test).cpu_emulationPassed;gta6rtbuilds and links.cpu_emulationPassed (so nothing pre-existing is failing).dumpbin /imports gta6rt.exe, main vs main + PR: 701 DLL!symbol entries in both, 0 new, 0 removed.Machine: Windows 11 Pro x64, RTX 3090 24 GB, NVIDIA 621.40, MSVC 14.44.
Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (
d3d12/) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools,d3d12/, or general docs), please feel free to reopen it.