cpu: lift RDFSBASE, RDGSBASE, and RDPID in cpu_switch_aot #1442

Merged
Ammmae0x merged 3 commits from upstream-pr/1449 into main 2026-11-24 13:06:32 +00:00
Ammmae0x commented 2026-11-24 11:45:40 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-24T11:45:40Z.


Change

Add opt-in --host-cpu (host_cpu=True, CMake HOST_CPU) lifting in tools/cpu_switch_aot.py and CMakeLists.txt (cpu_translation), along with a source-built MASM fixture (tests/cpu/switch_fsgsbase_rdpid.asm, tests/cpu/switch_fsgsbase_rdpid.def), differential/control harness (tests/cpu/switch_fsgsbase_rdpid_check.py), and experiment documentation (docs/experiments.md) covering three read-only / processor-ID x64 mnemonics from docs/coverage/x64_zen2.tsv group Integer:

  • rdfsbase (32- and 64-bit register forms via _readfsbase_u32 / _readfsbase_u64)
  • rdgsbase (32- and 64-bit register forms via _readgsbase_u32 / _readgsbase_u64)
  • rdpid (64-bit register form via _rdpid_u32)

All three instructions are registered in FLAG_NEUTRAL, preserve unreferenced reads under /O2 via const volatile, and are rejected when host_cpu is omitted, when rsp/esp is addressed, when a lock/rep/repne/segment prefix is present, or when rdpid uses its 32-bit encoding. Host segment-base writes (wrfsbase, wrgsbase) are explicitly excluded from this profile and rejected both with and without --host-cpu.

Verification

Verified at commit 3f445536:

  • & .\tools\dev.ps1; ctest --preset dev -N and ctest --preset release -N -> Total Tests: 918 on both presets (0 dropped tests).
  • cmake --build --preset dev --target cpu_switch_fsgsbase_rdpid cpu_switch_fsgsbase_rdpid_lifted cpu_register_probe cpu_switch_bswap cpu_switch_bswap_lifted -> exited 0 (0 errors, 0 warnings).
  • cmake --build --preset release --target cpu_switch_fsgsbase_rdpid cpu_switch_fsgsbase_rdpid_lifted -> exited 0 (0 errors, 0 warnings).
  • ctest --preset dev --no-tests=error -R '^(cpu_aot_switch_fsgsbase_rdpid|cpu_aot_switch_bswap)$' -V -> 2/2 passed: cpu_aot_switch_bswap (27357 differential comparisons passed) and cpu_aot_switch_fsgsbase_rdpid (232 CMake-lifted + 232 /O2-lifted checks across 4 pinned CPU(s), including canonical FS_BASE sweeps via the native-only native_with_fsbase helper, RDTSCP IA32_TSC_AUX cross-checks, 5 /O2 mutated-C++ controls keep_high32/truncate_64/swap_fs_gs/constant_rdpid/clobber_flags, 27 unit/MASM rejections including wrfsbase/wrgsbase 32/64-bit, and 9 PE-patched DLL rejections).
  • ctest --preset release --no-tests=error -R '^cpu_aot_switch_fsgsbase_rdpid$' -V -> 1/1 passed with identical 232 CMake-lifted + 232 /O2-lifted checks, 5 mutated-C++ controls, and all rejections passing.
  • out/build/dev/form_coverage/cpu_aot_switch_fsgsbase_rdpid.txt recorded: add, mov, pop, push, rdfsbase, rdgsbase, rdpid, ret, setb, sete, seto, setp, sets, xor.
  • .\out\pe-env\Scripts\python.exe -B tests/form_coverage_check.py -> exited 0 (form coverage checks passed).
  • .\out\pe-env\Scripts\python.exe -B tests/docs_links_check.py -> exited 0 (Checked 16 Markdown files and 332 links.).
  • .\out\pe-env\Scripts\python.exe -B tests/bidi_controls_check.py -> exited 0 (Checked 1201 tracked source, script and build/CI files: no bidirectional controls.).

Evidence and follow-up

  • tests/cpu/switch_fsgsbase_rdpid.asm and tests/cpu/switch_fsgsbase_rdpid.def are source-built in-repo MASM fixtures gated at runtime by CPUID.07H (FSGSBASE EBX[0] and RDPID ECX[22], returning exit code 77 on hosts without support).
  • Assumptions/open scope: host segment-base writes (wrfsbase/wrgsbase) require a separate write profile with an explicit isolation/restoration contract; cross-thread FS_BASE/GS_BASE migration and title CPU code execution are not covered.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6581024828 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1449). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-24T11:45:40Z. --- ## Change Add opt-in `--host-cpu` (`host_cpu=True`, CMake `HOST_CPU`) lifting in `tools/cpu_switch_aot.py` and `CMakeLists.txt` (`cpu_translation`), along with a source-built MASM fixture (`tests/cpu/switch_fsgsbase_rdpid.asm`, `tests/cpu/switch_fsgsbase_rdpid.def`), differential/control harness (`tests/cpu/switch_fsgsbase_rdpid_check.py`), and experiment documentation (`docs/experiments.md`) covering three read-only / processor-ID x64 mnemonics from `docs/coverage/x64_zen2.tsv` group `Integer`: - `rdfsbase` (32- and 64-bit register forms via `_readfsbase_u32` / `_readfsbase_u64`) - `rdgsbase` (32- and 64-bit register forms via `_readgsbase_u32` / `_readgsbase_u64`) - `rdpid` (64-bit register form via `_rdpid_u32`) All three instructions are registered in `FLAG_NEUTRAL`, preserve unreferenced reads under `/O2` via `const volatile`, and are rejected when `host_cpu` is omitted, when `rsp`/`esp` is addressed, when a `lock`/`rep`/`repne`/segment prefix is present, or when `rdpid` uses its 32-bit encoding. Host segment-base writes (`wrfsbase`, `wrgsbase`) are explicitly excluded from this profile and rejected both with and without `--host-cpu`. ## Verification Verified at commit `3f445536`: - `& .\tools\dev.ps1; ctest --preset dev -N` and `ctest --preset release -N` -> `Total Tests: 918` on both presets (0 dropped tests). - `cmake --build --preset dev --target cpu_switch_fsgsbase_rdpid cpu_switch_fsgsbase_rdpid_lifted cpu_register_probe cpu_switch_bswap cpu_switch_bswap_lifted` -> exited 0 (0 errors, 0 warnings). - `cmake --build --preset release --target cpu_switch_fsgsbase_rdpid cpu_switch_fsgsbase_rdpid_lifted` -> exited 0 (0 errors, 0 warnings). - `ctest --preset dev --no-tests=error -R '^(cpu_aot_switch_fsgsbase_rdpid|cpu_aot_switch_bswap)$' -V` -> `2/2` passed: `cpu_aot_switch_bswap` (`27357 differential comparisons passed`) and `cpu_aot_switch_fsgsbase_rdpid` (`232 CMake-lifted + 232 /O2-lifted checks across 4 pinned CPU(s)`, including canonical `FS_BASE` sweeps via the native-only `native_with_fsbase` helper, `RDTSCP` `IA32_TSC_AUX` cross-checks, 5 `/O2` mutated-C++ controls `keep_high32`/`truncate_64`/`swap_fs_gs`/`constant_rdpid`/`clobber_flags`, `27` unit/MASM rejections including `wrfsbase`/`wrgsbase` 32/64-bit, and `9` PE-patched DLL rejections). - `ctest --preset release --no-tests=error -R '^cpu_aot_switch_fsgsbase_rdpid$' -V` -> `1/1` passed with identical `232 CMake-lifted + 232 /O2-lifted checks`, 5 mutated-C++ controls, and all rejections passing. - `out/build/dev/form_coverage/cpu_aot_switch_fsgsbase_rdpid.txt` recorded: `add`, `mov`, `pop`, `push`, `rdfsbase`, `rdgsbase`, `rdpid`, `ret`, `setb`, `sete`, `seto`, `setp`, `sets`, `xor`. - `.\out\pe-env\Scripts\python.exe -B tests/form_coverage_check.py` -> exited 0 (`form coverage checks passed`). - `.\out\pe-env\Scripts\python.exe -B tests/docs_links_check.py` -> exited 0 (`Checked 16 Markdown files and 332 links.`). - `.\out\pe-env\Scripts\python.exe -B tests/bidi_controls_check.py` -> exited 0 (`Checked 1201 tracked source, script and build/CI files: no bidirectional controls.`). ## Evidence and follow-up - `tests/cpu/switch_fsgsbase_rdpid.asm` and `tests/cpu/switch_fsgsbase_rdpid.def` are source-built in-repo MASM fixtures gated at runtime by `CPUID.07H` (`FSGSBASE` `EBX[0]` and `RDPID` `ECX[22]`, returning exit code `77` on hosts without support). - Assumptions/open scope: host segment-base writes (`wrfsbase`/`wrgsbase`) require a separate write profile with an explicit isolation/restoration contract; cross-thread `FS_BASE`/`GS_BASE` migration and title CPU code execution are not covered. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. - [x] I did not include game files, dumps, distribution links or secrets. AI-assisted.
upstream-sync changed title from [PR #1449] cpu: lift RDFSBASE, RDGSBASE, WRFSBASE, WRGSBASE, and RDPID in cpu_switch_aot to [PR #1449] cpu: lift RDFSBASE, RDGSBASE, and RDPID in cpu_switch_aot 2026-11-24 13:00:30 +00:00
R0Y00 commented 2026-11-24 13:00:30 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T12:17:36Z.

Review: CHANGES_REQUESTED


The source-built checks show that these particular functions restore their host state. The public --host-cpu switch also permits an arbitrary accepted PE function to write the host thread's FS/GS bases from a defined guest register. The profile does not enforce the fixture's restoration or protect host runtime accesses during the call. Please split the read-only and processor-ID work from host segment-base writes. Any later write profile needs a defined isolation/restoration contract and rejection outside it.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6039044864 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1449#pullrequestreview-6039044864). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-24T12:17:36Z. **Review:** CHANGES_REQUESTED --- The source-built checks show that these particular functions restore their host state. The public --host-cpu switch also permits an arbitrary accepted PE function to write the host thread's FS/GS bases from a defined guest register. The profile does not enforce the fixture's restoration or protect host runtime accesses during the call. Please split the read-only and processor-ID work from host segment-base writes. Any later write profile needs a defined isolation/restoration contract and rejection outside it.
Ammmae0x commented 2026-11-24 13:00:53 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-24T12:32:03Z.


Split WRFSBASE / WRGSBASE out of tools/cpu_switch_aot.py (--host-cpu) in commit 3f445536 so this PR only lifts read-only RDFSBASE, RDGSBASE, and RDPID and rejects host segment-base writes both with and without --host-cpu:

  • Removed WRFSBASE / WRGSBASE from FLAG_NEUTRAL, HOST_CPU_READ_BASE_CODES, Lifter.lower, and PRELUDE (<atomic>).
  • Replaced the translated write round-trips in tests/cpu/switch_fsgsbase_rdpid.asm with read-only nonvolatile reads (read_host_nonvolatile) plus a native-only native_with_fsbase helper for canonical FS_BASE differential coverage, and added explicit wrfsbase/wrgsbase 32/64-bit rejection controls in MASM negative exports, Lifter.lower, and PE-patched DLL checks.
  • Re-verified cpu_aot_switch_fsgsbase_rdpid across dev and release builds (232 CMake-lifted + 232 /O2-lifted checks, 5 mutated-C++ controls, 27 unit/MASM rejections, 9 PE-patched rejections), cpu_aot_switch_bswap, form_coverage_check.py, docs_links_check.py, and bidi_controls_check.py.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7131571869 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1449#issuecomment-7131571869). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-24T12:32:03Z. --- Split `WRFSBASE` / `WRGSBASE` out of `tools/cpu_switch_aot.py` (`--host-cpu`) in commit `3f445536` so this PR only lifts read-only `RDFSBASE`, `RDGSBASE`, and `RDPID` and rejects host segment-base writes both with and without `--host-cpu`: - Removed `WRFSBASE` / `WRGSBASE` from `FLAG_NEUTRAL`, `HOST_CPU_READ_BASE_CODES`, `Lifter.lower`, and `PRELUDE` (`<atomic>`). - Replaced the translated write round-trips in `tests/cpu/switch_fsgsbase_rdpid.asm` with read-only nonvolatile reads (`read_host_nonvolatile`) plus a native-only `native_with_fsbase` helper for canonical `FS_BASE` differential coverage, and added explicit `wrfsbase`/`wrgsbase` 32/64-bit rejection controls in MASM negative exports, `Lifter.lower`, and PE-patched DLL checks. - Re-verified `cpu_aot_switch_fsgsbase_rdpid` across `dev` and `release` builds (232 CMake-lifted + 232 `/O2`-lifted checks, 5 mutated-C++ controls, 27 unit/MASM rejections, 9 PE-patched rejections), `cpu_aot_switch_bswap`, `form_coverage_check.py`, `docs_links_check.py`, and `bidi_controls_check.py`. AI-assisted.
upstream-sync closed this pull request 2026-11-24 14:02:45 +00:00
R0Y00 commented 2026-11-24 14:02:46 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T13:06:18Z.

Review: APPROVED


Windows Release native and lifted comparison passed. The opt-in now permits the read-only forms. The previous FS/GS base writes are excluded, and the unsupported mode and flag-preservation controls still execute.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6039710154 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1449#pullrequestreview-6039710154). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-24T13:06:18Z. **Review:** APPROVED --- Windows Release native and lifted comparison passed. The opt-in now permits the read-only forms. The previous FS/GS base writes are excluded, and the unsupported mode and flag-preservation controls still execute.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!1442
No description provided.