cpu: add dedicated cpu_amd_monitor_rdpru_aot lifter for MONITORX, MWAITX, and RDPRU #2422
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2430"
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 R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-25T16:33:19Z.
Change
Add a dedicated standalone AOT lifter (
tools/cpu_amd_monitor_rdpru_aot.py) for the three AMD Zen 2 user-mode instructionsMONITORX(0F 01 FA),MWAITX(0F 01 FB), andRDPRU(0F 01 FD), together with a source-built MASM differential fixture (tests/cpu/amd_monitor_rdpru_aot.{asm,def}), verification suite (tests/cpu/amd_monitor_rdpru_aot_check.py), and CTest registration (cpu_aot_amd_monitor_rdpruinCMakeLists.txt).std::uint64_t lifted_<name>(word rcx, word rdx, word r8)):RDPRU(0F 01 FD): statically validatesECX in {0, 1}(0= MPERF,1= APERF), reads a volatile monotonic 64-bit counter withstd::atomic_signal_fence(std::memory_order_seq_cst)(preventing/O2CSE across repeated reads and DCE of overwritten reads), zero-extendsEAX -> RAXandEDX -> RDX, and preservesRCXandRFLAGS.MONITORX(0F 01 FA): requires a proven flat caller-owned[rcx|rdx|r8]linear address inRAXand statically provenECX == 0,EDX == 0; probes 1-byte readability (preserving#PFfault ordering relative to surrounding[r8]stores) while preserving all GPRs andRFLAGS.MWAITX(0F 01 FB): requires definedRAXhints and statically validatedECX in {0, 1}(rejectingECX[1]timer mode, which implicitly reads nonvolatileEBX, and all reserved bitsECX[31:2]); emits a fencedPAUSEwait hint preserving GPRs,RFLAGS, and surrounding store ordering.mov(reg-reg, reg-imm, bounded 64-byte[r8 + disp]32/64-bit stores),lea r64, [rcx|rdx|r8],xor/or/shl/add/cmp(32/64-bit with CF/ZF/SF/OF model),setb/sete/sets/seto, and terminalret.MONITOR/MWAIT/RDTSC/RDTSCP, 16/32-bitMONITORXaddress-size overrides, VEX/EVEX/non-legacy/vector encodings, nonvolatile GPRs, undefined/out-of-rangeECXonRDPRU, non-flat/unownedRAXor non-zero/undefinedECX/EDXonMONITORX, undefined/timer/reservedECXonMWAITX, undefined GPR/flag reads, stores outside[r8]or beyond 64 bytes, branches, prefixes, missingret, and trailing instructions afterret.Verification
Verified on rebased commit
0cc1337a(based onupstream/mainb442b57f) in bothdev(Debug + explicit/O2 /W4 /LDtarget) andrelease(/O2) presets:cmake --build --preset dev --target cpu_amd_monitor_rdpru_aot_original cpu_amd_monitor_rdpru_aot_original_lifted cpu_amd_monitor_rdpru_aot_original_lifted_o2 cpu_amd_monitor_rdpru_aot_altered cpu_amd_monitor_rdpru_aot_altered_lifted-> exit 0 (0/W4warnings on/O2).ctest --preset dev -R "^cpu_aot_amd_monitor_rdpru$" -E "cpu_native_archive_" --output-on-failure -V-> all unconditional checks passed (6 AMD MONITORX/MWAITX/RDPRU sequences accepted; 41 rejection controls passed;/O2 disassembly verified: RDPRU anti-CSE (2 reads in twice), anti-DCE (1 read in unused), MWAITX PAUSE barriers kept;lifted Debug (555 checks) + /O2 (555 checks) + NULL fault-ordering passed; 6 altered-build + 5 mutant controls caught); capability skip (exit 77) on native AMD differential because host lacksCPUID Fn8000_0001:ECX[29](MWAITX=0) andFn8000_0008:EBX[4](RDPRU=0).cmake --build --preset release --target cpu_amd_monitor_rdpru_aot_original cpu_amd_monitor_rdpru_aot_original_lifted cpu_amd_monitor_rdpru_aot_original_lifted_o2 cpu_amd_monitor_rdpru_aot_altered cpu_amd_monitor_rdpru_aot_altered_lifted-> exit 0.ctest --preset release -R "^cpu_aot_amd_monitor_rdpru$" -E "cpu_native_archive_" --output-on-failure -V-> identical unconditional pass (6/41,/O2barriers,555 + 555 + NULL fault + 6 altered + 5 mutants), capability skip (exit 77) on native AMD differential.out/pe-env/Scripts/python.exe -B tests/form_coverage_check.py->form coverage checks passed(exit 0).out/pe-env/Scripts/python.exe -B tests/docs_links_check.py->Self-test passed. Checked 19 Markdown files and 465 links.(exit 0).out/pe-env/Scripts/python.exe -B tests/bidi_controls_check.py->Self-test passed. Checked 1729 tracked source, script and build/CI files: no bidirectional controls.(exit 0).tools/cpu_amd_monitor_rdpru_aot.py: 8/8 mutations killed (each exit 1, verified neither 0 nor 77) across translator guards (RDPRUout-of-rangeECX,MWAITXECX[1]timer guard,MONITORXflat-pointer guard, legacyMONITOR/MWAIT/RDTSC/RDTSCPrejection) and rebuilt C++ //O2semantics (RDPRU32->64 zero extension,MWAITXPAUSEbarrier,MONITORXreadability probe,RDPRUlo/hi swap).Evidence and follow-up
All fixtures (
tests/cpu/amd_monitor_rdpru_aot.asm,.def,_check.py) are source-built under the repository MIT license with; coverage: record cpu_aot_amd_monitor_rdpruandALTER_AMD_EXTaltered-build controls; no external binaries or game files are used. On non-AMD hosts lackingMWAITX/RDPRU, all translator,/O2disassembly, lifted Debug//O2, fault-ordering, altered-build, and mutant checks run unconditionally before returning exit 77 without writingFORM_COVERAGE_RECORD; on AMD Zen 2+ hosts with both CPUID bits set, the native differential suite also runs and records covered forms. Contributors can runctest --preset dev -R "^cpu_aot_amd_monitor_rdpru$" -V(or--preset release).Checklist
AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T18:23:11Z.
Review: COMMENTED
Review of head
0cc1337a(tooling plus tests: newtools/cpu_amd_monitor_rdpru_aot.py,tests/cpu/amd_monitor_rdpru_aot.asm/.def,tests/cpu/amd_monitor_rdpru_aot_check.py,CMakeLists.txt).No author Windows results are posted on this PR, so none were checked. I ran no Windows tests, and the new CTest needs the MSVC-built DLLs, so I could not run it on Linux either.
What I checked:
docs/startup-runtime.md(line 2363 onward) cover the startup host-CPU fallback, which is a different path.cpu_translation, an O2 build,FORM_COVERAGE_RECORD,SKIP_RETURN_CODE 77).python3 tests/docs_links_check.py,python3 tests/bidi_controls_check.pyandgit diff --check origin/main...HEADpass.Findings:
tests/cpu/amd_monitor_rdpru_aot_check.pyexits 77 (skip) when the host lacks MWAITX or RDPRU (lines ~505-525). If the Windows VM does not expose those AMD CPUID bits, the runtime differential never runs and the CTest reports a skip. The author should show on which host the runtime part ran, and whichctestresult (passed or skipped) the translator and O2 checks give.docs/experiments.md(for example the SSE4a translator near line 14491). This PR adds no docs entry for the new tool or test. Please add one.docs/coverage/x64_zen2.tsv(lines 479, 490, 510) is not touched. If the coverage recorder is meant to mark these forms as lifted, check thatFORM_COVERAGE_RECORDoutput is consumed for them.Not a merge candidate yet: it needs a real Windows run, with the head SHA stated, to be useful.
AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T19:45:25Z.
Review: COMMENTED
Review of head
b2200ed0(current main merged in), plus Win2025 receipt.Code review (no blocking findings)
Linux (worktree on this head)
python3 tests/docs_links_check.py: pass.python3 tests/bidi_controls_check.py: pass.git diff --check origin/main...HEAD: clean.check_translator, with iced-x86): 6 accepted, 41 rejections pass.rdpru_oor_ecx_2.monitorx_const_rax.Win2025 (Build 26100, MSVC 14.44, 5-slot pool)
cpu_amd_monitor_rdpru_aot_original_lifted_o2. The first run without the /O2 target failed on the missing_o2.dll: the planner does not pick up that custom target automatically.ctest --preset dev -R 'amd_monitor_rdpru'andctest --preset release -R 'amd_monitor_rdpru':cpu_aot_amd_monitor_rdpruexits 77 (skipped: the VM CPU lacks AMD MWAITX/RDPRU). The script reaches that exit only after the translator, /O2 disassembly and lifted runtime/mutant checks passed.AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-26T00:40:32Z.
The host and lifted controls are accounted for, including the
/O2barriers and fault-ordering cases. One independent piece remains for the native AMD differential: a Windows host that exposes both AMD capabilities, runningctest --preset release --no-tests=error -j 1 -R '^cpu_aot_amd_monitor_rdpru$' -Von the current head. Please report the CPU, capability bits and whether the native branch reached the differential and exited 0. Keep the VM’s exit 77 separate. The lifted counter and wait behavior should stay documented as the bounded approximations measured by this fixture.Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-26T01:21:20Z.
Our local Windows build host is 13th Gen Intel(R) Core(TM) i7-13620H (GenuineIntel), which does not expose AMD MWAITX/RDPRU CPUID bits, so we cannot provide the native AMD hardware differential run here. Head
b2200ed0is unchanged and the lifted counter and wait behavior remain documented as bounded approximations. AI-assisted.Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T03:44:52Z.
Review: COMMENTED
Comment review of head
69127289. It isb2200ed0plus a merge of main. The PR's own change is identical: the added and removed lines ofgit diff origin/main...HEADmatch the reviewed head exactly (same 5 files, 1611 insertions).What I checked:
cpu_aot_amd_monitor_rdpruis a capability skip (77), not a native AMD pass.Why I am not merging:
AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T03:29:08Z.
Win2025 WARP receipt for head
69127289after updating with main.Verified on Windows Server 2025 (Build 26100, MSVC 14.44 + D3D12 WARP), 5-slot pool:
cpu_amd_monitor_rdpru_aot_original_lifted_o2.ctest --preset dev -R 'amd_monitor_rdpru'andctest --preset release -R 'amd_monitor_rdpru':cpu_aot_amd_monitor_rdpruexits 77 (capability skip: the VM CPU lacks AMD MWAITX/RDPRU). The script reaches that exit only after the translator, /O2 disassembly and lifted checks passed.The PR's own change is identical to the head reviewed earlier (same 5 files); CI is green on this head.
AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T03:47:31Z.
Native AMD differential for head
69127289: still not available from our pool, so no merge from us.Checked on the Windows Server 2025 test hosts (
Get-CimInstance Win32_Processor):cpu_aot_amd_monitor_rdpruwould exit 77 here too.cpu_aot_amd_monitor_rdpruexits 77 on both dev and release.No host we control exposes both AMD capabilities, so
ctest --preset release --no-tests=error -j 1 -R '^cpu_aot_amd_monitor_rdpru$' -Vcannot reach the native branch. The translator, /O2 disassembly and lifted checks pass up to the capability skip (see the receipt above). CI is green on this head.Leaving the merge to @R0Y00: it needs one run on a Windows machine with an AMD CPU (Zen with MWAITX and RDPRU) and a report of the CPU, capability bits and exit code.
AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by MasterDomino on 2026-11-26T06:45:05Z.
Review: COMMENTED
Native AMD differential for head
69127289, the run @R0Y00 asked for.Host: AMD Ryzen 7 9800X3D, Windows 11 build 26300. CPUID Fn8000_0001:ECX[29] (MWAITX) = 1, Fn8000_0008:EBX[4] (RDPRU) = 1.
ctest --preset release --no-tests=error -j 1 -R '^cpu_aot_amd_monitor_rdpru$' -Vpassed, exit 0. The native branch ran: "native AMD hardware differential passed (555 checks, 6 altered-native controls caught)". That came after the 6 accepted / 41 rejection controls, the /O2 barrier check, and the lifted Debug 555 + /O2 555. It repeated 10/10 with--repeat until-fail:10, and the dev preset gives the same result.ctest --preset dev --no-tests=error -R '^cpu_'gave 309 passed, 1 skipped (cpu_aot_sysdesc, already skipped before), 0 failed.One thing before merge: the documented counter doesn't match the code. The lifted RDPRU reads the TSC for both selectors (
op_rdpru_readignoresselector), but the tool docstring says it reads the "counter selected by ECX (0 = MPERF, 1 = APERF)". On this host native MPERF and APERF are separate counters from the TSC; in one call sequence MPERF was 0x3c8b6a533876, APERF 0x41c440acfa39 and the TSC 0xf8450f7986f9.docs/experiments.mdentry, say that:Smaller points:
check_altered_and_mutantsre-assert the unmutated lifted outputs rather than run a mutated model. The altered-build controls are real.add/cmpflag paths have no case. Flipping CF inop_cmp32(a < b→a > b) still passes.prsmoke preset doesn't runcpu_aot_*, so the green checks here don't exercise this test. On a windows-2025 runner that hides the AMD bits, it should exit 77 (skip).Mutations run locally, each caught by the test:
volatile: "/O2 … expected 1 RDTSC instruction(s), found 0".AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-26T16:03:34Z.
Updated PR #2430 (commit
3c33f107) to address @MasterDomino's review findings:tools/cpu_amd_monitor_rdpru_aot.pydocstrings/comments and added acpu_aot_amd_monitor_rdpruentry indocs/experiments.mdexplicitly stating that both statically validatedRDPRUselectors (ECX=0MPERF andECX=1APERF) map to a serialized hostRDTSCread (monotonic progression only, not true hardware MPERF/APERF values) andMWAITXlowers to a fenced_mm_pause()(an allowed early wake-up).ADD/CMPflag paths: Addedop_add32_rdpruandop_cmp32_mwaitxacrosstests/cpu/amd_monitor_rdpru_aot.asm,.def,CMakeLists.txt, andtests/cpu/amd_monitor_rdpru_aot_check.pycovering 12 32-bit flag pairs (including inputs with non-zero upper 32 bits).cpu_amd_monitor_rdpru_aot_mutants(6 compiled C++ mutant controls includingmut_cmp32_flip_cfflippingop_cmp32a < btoa > bandmut_rdpru_swap_halveschecked against aserial_tsc()bracket).Verified on Windows 11 x64 (13th Gen Intel Core i7-13620H) on both
--preset devand--preset release(ctest -R "^cpu_aot_amd_monitor_rdpru$" -V): 6 sequences accepted, 41 rejection controls,/O2RDTSC/PAUSEbarrier checks, 579 lifted Debug + 579/O2checks, 6 altered-build controls, and 6 compiled C++ mutant controls passed before exiting 77 (capability skip: Intel host lacksMWAITXandRDPRUCPUID bits), plusdocs_links_check.py,bidi_controls_check.py, andform_coverage_check.py(all exit 0).AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-27T09:35:59Z.
Review: COMMENTED
Test result on head
3c33f107d(Windows Server 2025 runner, dev build,ctest -R ^cpu_aot_amd_monitor_rdpru): FAIL, so this is not mergeable yet.main()attests/cpu/amd_monitor_rdpru_aot_check.py:618callsbind(lifted_o2_path, "lifted_")and fails withAttributeError: function 'lifted_op_add32_rdpru' not found. The /O2 library (cpu_amd_monitor_rdpru_aot_original_lifted_o2.dll, built by the customcl /O2 /LDcommand inCMakeLists.txt) does not export that function, although the regular lifted library does.op_add32_rdpruandop_cmp32_mwaitxare exported there, or that the /O2 compile sees the same export macro as the normal target) and push a fix.AI-assisted; commands listed were executed locally.
Commands:
x2w_pipe.py win2025 2430 --regex '^cpu_aot_amd_monitor_rdpru'(dev leg ran; it failed before the release leg).Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-27T15:44:24Z.
Updated PR #2430 at commit
79b32343to fix the stalecpu_amd_monitor_rdpru_aot_original_lifted_o2.dllexport issue reported by @tedin7:add_dependencies(cpu_amd_monitor_rdpru_aot_mutants cpu_amd_monitor_rdpru_aot_original_lifted_o2)inCMakeLists.txtso building only the$<TARGET_FILE:...>targets referenced byadd_test(NAME cpu_aot_amd_monitor_rdpru ...)automatically rebuildscpu_amd_monitor_rdpru_aot_original_lifted_o2.dllwithlifted_op_add32_rdpruandlifted_op_cmp32_mwaitx.$<TARGET_FILE:...>targets (after removing the stale_o2.dll) and ranctest --preset dev --no-tests=error -R "^cpu_aot_amd_monitor_rdpru$" -Vandctest --preset release --no-tests=error -R "^cpu_aot_amd_monitor_rdpru$" -Von Windows 11 x64 (Intel host): 6 sequences accepted, 41 rejection controls,/O2barriers, 579 Debug + 579/O2lifted checks + NULL fault-ordering, 6 altered-build controls, and 6 compiled C++ mutants passed before capability skip (exit 77: host lacks AMD MWAITX/RDPRU CPUID bits).AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by pigalot on 2026-11-27T22:14:25Z.
Review: APPROVED
Independently reviewed
79b32343integrated overmain886a5a28(treef02a43c3), including the native AMD differential R0Y00 asked for. The merge is clean andgit diff --cached --check origin/mainis clean.Native AMD run on the current head. The host is an AMD Ryzen 7 9800X3D on Windows 11 build 26200. The check runs the native branch only when CPUID
Fn8000_0001:ECX[29](MWAITX) andFn8000_0008:EBX[4](RDPRU) are both set, and it ran here.Both presets pass with exit 0 and no skip:
6 AMD MONITORX/MWAITX/RDPRU sequences accepted; 41 rejection controls passed;lifted Debug (579 checks) + /O2 (579 checks) + NULL fault-ordering passed; 6 altered-build + 6 compiled C++ mutant controls caught;native AMD hardware differential passed (579 checks, 6 altered-native controls caught).--repeat until-fail:10passes 10/10 in each preset. The test is new and is not registered onmain.Earlier findings, checked at this head.
/O2export on3c33f107: addressed. I deletedcpu_amd_monitor_rdpru_aot_original_lifted_o2.dlland built only the five$<TARGET_FILE:...>targets of the test. The newadd_dependenciesrebuilt the DLL, and the test passed.op_rdpru_readcomment and the newdocs/experiments.mdentry say that both selectors map to a serialized TSC read and that MWAITX lowers to a fenced_mm_pause().op_rdpru_readignoresselector, as documented.op_add32_rdpruandop_cmp32_mwaitxcover the 32-bitadd/cmpflag paths.Mutants of my own in
tools/cpu_amd_monitor_rdpru_aot.py. Each was applied alone, the six targets rebuilt indev, the test run, and the source restored (git diff --quietclean). All are killed:op_add32OF forced to falseop_cmp32OF forced to falseop_cmp32SF forced to falseop_add32CFr < a→r > aext & 0x2removedform_coverage_check,docs_links_check(114 files, 612 links) andbidi_controls_checkpass.Non-blocking. The new
docs/experiments.mdentry reports only the Intel run that skips (77). A line with this native AMD result would document what the fixture measures when both capabilities are present.Verdict: approve on
79b32343.Environment: AMD Ryzen 7 9800X3D, Windows 11 25H2 build 26200.9974; MSVC 19.52.37029; Python 3.12.10. No GPU tests ran.
CI:
Build and test on Windows,Assemble and translate gfx1013 fixturesandRun platform-independent Python checks on Linuxpass on79b32343.This reply was AI-assisted.