cpu: lower SGDT, SIDT, SLDT, SMSW, and STR under opt-in host-CPU profile #2057

Merged
Ammmae0x merged 2 commits from upstream-pr/2064 into main 2026-11-25 18:39:29 +00:00
Ammmae0x commented 2026-11-24 23:51:46 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-24T23:51:46Z.


Change

Add an opt-in --host-cpu (HOST_CPU) bounded Windows x64 AOT lifter (tools/cpu_sysdesc_aot.py), source-built MASM fixture (tests/cpu/sysdesc.asm, tests/cpu/sysdesc.def), differential verification harness (tests/cpu/sysdesc_check.py), and CTest registration (cpu_aot_sysdesc in CMakeLists.txt) covering the five system-descriptor / machine-status store mnemonics in docs/coverage/x64_zen2.tsv (Integer):

  • sgdt m16&64 (0F 01 /0): stores 10-byte canonical Windows x64 kernel GDTR (limit + base) without modifying buffer guard bytes [10..15].
  • sidt m16&64 (0F 01 /1): stores 10-byte live host IDTR via MSVC __sidt() without modifying guard bytes [10..15].
  • sldt r/m16, r32, r64 (0F 00 /0): stores 16-bit LDTR selector (0x0000 on Windows x64).
  • str r/m16, r32, r64 (0F 00 /1): stores 16-bit TR selector (0x0040, KGDT64_SYS_TSS).
  • smsw r/m16, r32, r64 (0F 01 /4): stores CR0[15:0] (m16/r16) or CR0[31:0] zero-extended (r32/r64).

Key semantics and safety guarantees:

  • All five instructions are gated behind --host-cpu; the default profile rejects every one with ValueError.
  • SLDT/STR/SMSW preserve [63:16] on r16, zero-extend to 64 bits on r32/r64, store exactly 2 bytes on m16, and leave RFLAGS untouched.
  • Memory stores use std::atomic_signal_fence(std::memory_order_seq_cst) + volatile writes so /O2 never eliminates or reorders faulting stores.
  • Rejects VEX/EVEX/XOP encodings, segment/LOCK/REP/REPNE prefixes, privileged write counterparts (LGDT/LIDT/LLDT/LMSW/LTR), RIP-relative and SIB-indexed addressing, clobbered pointer bases, nonvolatile and high-byte GPRs, non-leaf branches/calls, memory destinations for XOR/ADD, and undefined GPR/flag reads.
  • tests/cpu/sysdesc_check.py queries CPUID.07H:ECX[2] (UMIP), pins thread affinity per logical CPU when comparing live SIDT, and exits cleanly with 77 (SKIP_RETURN_CODE) if native execution traps (0xC0000096/0xC000001D) or runs on a non-standard host state.

Verification

Tested on commit 7fa4a686 (rebased onto upstream/main 127f5f80, Windows x64, MSVC 14.50, dev Debug and release /O2):

  1. cmake --build --preset dev --target cpu_sysdesc_original cpu_sysdesc_original_lifted cpu_sysdesc_altered cpu_sysdesc_altered_lifted
    • Result: succeeded with 0 errors, 0 warnings.
  2. cmake --build --preset release --target cpu_sysdesc_original cpu_sysdesc_original_lifted cpu_sysdesc_altered cpu_sysdesc_altered_lifted
    • Result: succeeded with 0 errors, 0 warnings.
  3. ctest --preset dev -R "^cpu_aot_sysdesc$" --output-on-failure -V
    • Result: 1/1 Test #636: cpu_aot_sysdesc .................. Passed (rejections passed: 5 default-profile PE + 5 default IR + 6 raw + 26 IR + 8 PE-patched + 3 CLI; sysdesc passed (UMIP_CPUID=1, cpus=[0, 1, 2, 3]): 212 lifted + 212 /O2 cases across 22 exports; ALTER_IMM diverged on ['op_sgdt', 'op_sidt', 'op_sldt_r16', 'op_sldt_m16', 'op_str_r16', 'op_smsw_r16', 'op_sysdesc_flags_preserve']). Capability skips: none (exits 77 if native traps or host state is non-standard).
  4. ctest --preset release -R "^cpu_aot_sysdesc$" --output-on-failure
    • Result: 1/1 Test #636: cpu_aot_sysdesc .................. Passed.
  5. FORM_COVERAGE_RECORD (out/build/dev/form_coverage/cpu_aot_sysdesc.txt and release):
    • Emits: add, mov, ret, setb, sete, seto, setp, sets, sgdt, sidt, sldt, smsw, str.
  6. .\out\pe-env\Scripts\python.exe -B tests/form_coverage_check.py
    • Result: form coverage checks passed (exit 0).
  7. .\out\pe-env\Scripts\python.exe -B tests/docs_links_check.py
    • Result: Self-test passed. Checked 18 Markdown files and 391 links. (exit 0).
  8. .\out\pe-env\Scripts\python.exe -B tests/bidi_controls_check.py
    • Result: Self-test passed. Checked 1411 tracked source, script and build/CI files: no bidirectional controls. (exit 0).

Evidence and follow-up

  • Fixture tests/cpu/sysdesc.asm and tests/cpu/sysdesc.def are original, source-built synthetic MASM test fixtures under the repository MIT license; no external binaries or game files are used.
  • Negative controls cover 5 default-profile PE rejections + 5 default-profile IR rejections (without --host-cpu), 6 raw excluded-encoding checks, 26 IR rejections (including privileged LGDT/LIDT/LLDT/LMSW/LTR), 8 PE-patched rejections, 3 CLI non-traceback / zero-leftover-file checks, 7 ALTER_IMM mutant exports verifying divergence, and NULL-pointer store-fault checks under /O2.
  • Assumption: SGDT, SLDT, STR, and SMSW model standard Windows x64 user-mode host state while SIDT queries live __sidt(); hosts where UMIP traps native user-mode execution or host state differs skip cleanly with exit code 77.

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:6593830028 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2064). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-24T23:51:46Z. --- ## Change Add an opt-in `--host-cpu` (`HOST_CPU`) bounded Windows x64 AOT lifter (`tools/cpu_sysdesc_aot.py`), source-built MASM fixture (`tests/cpu/sysdesc.asm`, `tests/cpu/sysdesc.def`), differential verification harness (`tests/cpu/sysdesc_check.py`), and CTest registration (`cpu_aot_sysdesc` in `CMakeLists.txt`) covering the five system-descriptor / machine-status store mnemonics in `docs/coverage/x64_zen2.tsv` (`Integer`): - `sgdt m16&64` (`0F 01 /0`): stores 10-byte canonical Windows x64 kernel GDTR (`limit` + `base`) without modifying buffer guard bytes `[10..15]`. - `sidt m16&64` (`0F 01 /1`): stores 10-byte live host IDTR via MSVC `__sidt()` without modifying guard bytes `[10..15]`. - `sldt r/m16, r32, r64` (`0F 00 /0`): stores 16-bit LDTR selector (`0x0000` on Windows x64). - `str r/m16, r32, r64` (`0F 00 /1`): stores 16-bit TR selector (`0x0040`, `KGDT64_SYS_TSS`). - `smsw r/m16, r32, r64` (`0F 01 /4`): stores `CR0[15:0]` (`m16`/`r16`) or `CR0[31:0]` zero-extended (`r32`/`r64`). Key semantics and safety guarantees: - All five instructions are gated behind `--host-cpu`; the default profile rejects every one with `ValueError`. - `SLDT`/`STR`/`SMSW` preserve `[63:16]` on `r16`, zero-extend to 64 bits on `r32`/`r64`, store exactly 2 bytes on `m16`, and leave `RFLAGS` untouched. - Memory stores use `std::atomic_signal_fence(std::memory_order_seq_cst)` + `volatile` writes so `/O2` never eliminates or reorders faulting stores. - Rejects VEX/EVEX/XOP encodings, segment/LOCK/REP/REPNE prefixes, privileged write counterparts (`LGDT`/`LIDT`/`LLDT`/`LMSW`/`LTR`), RIP-relative and SIB-indexed addressing, clobbered pointer bases, nonvolatile and high-byte GPRs, non-leaf branches/calls, memory destinations for `XOR`/`ADD`, and undefined GPR/flag reads. - `tests/cpu/sysdesc_check.py` queries `CPUID.07H:ECX[2]` (UMIP), pins thread affinity per logical CPU when comparing live `SIDT`, and exits cleanly with `77` (`SKIP_RETURN_CODE`) if native execution traps (`0xC0000096`/`0xC000001D`) or runs on a non-standard host state. ## Verification Tested on commit `7fa4a686` (rebased onto `upstream/main` `127f5f80`, Windows x64, MSVC 14.50, `dev` Debug and `release` `/O2`): 1. `cmake --build --preset dev --target cpu_sysdesc_original cpu_sysdesc_original_lifted cpu_sysdesc_altered cpu_sysdesc_altered_lifted` - Result: succeeded with 0 errors, 0 warnings. 2. `cmake --build --preset release --target cpu_sysdesc_original cpu_sysdesc_original_lifted cpu_sysdesc_altered cpu_sysdesc_altered_lifted` - Result: succeeded with 0 errors, 0 warnings. 3. `ctest --preset dev -R "^cpu_aot_sysdesc$" --output-on-failure -V` - Result: `1/1 Test #636: cpu_aot_sysdesc .................. Passed` (`rejections passed: 5 default-profile PE + 5 default IR + 6 raw + 26 IR + 8 PE-patched + 3 CLI`; `sysdesc passed (UMIP_CPUID=1, cpus=[0, 1, 2, 3]): 212 lifted + 212 /O2 cases across 22 exports; ALTER_IMM diverged on ['op_sgdt', 'op_sidt', 'op_sldt_r16', 'op_sldt_m16', 'op_str_r16', 'op_smsw_r16', 'op_sysdesc_flags_preserve']`). Capability skips: none (exits `77` if native traps or host state is non-standard). 4. `ctest --preset release -R "^cpu_aot_sysdesc$" --output-on-failure` - Result: `1/1 Test #636: cpu_aot_sysdesc .................. Passed`. 5. `FORM_COVERAGE_RECORD` (`out/build/dev/form_coverage/cpu_aot_sysdesc.txt` and `release`): - Emits: `add, mov, ret, setb, sete, seto, setp, sets, sgdt, sidt, sldt, smsw, str`. 6. `.\out\pe-env\Scripts\python.exe -B tests/form_coverage_check.py` - Result: `form coverage checks passed` (exit 0). 7. `.\out\pe-env\Scripts\python.exe -B tests/docs_links_check.py` - Result: `Self-test passed. Checked 18 Markdown files and 391 links.` (exit 0). 8. `.\out\pe-env\Scripts\python.exe -B tests/bidi_controls_check.py` - Result: `Self-test passed. Checked 1411 tracked source, script and build/CI files: no bidirectional controls.` (exit 0). ## Evidence and follow-up - Fixture `tests/cpu/sysdesc.asm` and `tests/cpu/sysdesc.def` are original, source-built synthetic MASM test fixtures under the repository MIT license; no external binaries or game files are used. - Negative controls cover 5 default-profile PE rejections + 5 default-profile IR rejections (without `--host-cpu`), 6 raw excluded-encoding checks, 26 IR rejections (including privileged `LGDT`/`LIDT`/`LLDT`/`LMSW`/`LTR`), 8 PE-patched rejections, 3 CLI non-traceback / zero-leftover-file checks, 7 `ALTER_IMM` mutant exports verifying divergence, and `NULL`-pointer store-fault checks under `/O2`. - Assumption: `SGDT`, `SLDT`, `STR`, and `SMSW` model standard Windows x64 user-mode host state while `SIDT` queries live `__sidt()`; hosts where UMIP traps native user-mode execution or host state differs skip cleanly with exit code `77`. ## 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.
tedin7 commented 2026-11-25 01:01:25 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T00:49:23Z.

Review: COMMENTED


Review of head 7fa4a686627b723506f100e1991c169870b2acde (SGDT/SIDT/SLDT/SMSW/STR under opt-in host-CPU profile).

Commands executed:

  • git merge-tree --write-tree --name-only origin/main HEAD: clean, no conflicts.
  • gh pr checks 2064: Linux Python, Windows build/test, gfx1013 fixtures pass.
  • git diff --check origin/main...HEAD: clean.
  • python3 tests/bidi_controls_check.py: pass. python3 tests/docs_links_check.py: pass.
  • python3 -B tests/cpu/sysdesc_check.py on Linux: cannot run, iced_x86 is not installed locally (not a PR defect).
  • python3 x2w_pipe.py win2025 2064 --wait 900 (Win2025, MSVC 14.44): Debug cpu_aot_sysdesc 1/1 passed; Release /O2 cpu_aot_sysdesc 1/1 passed.
  • Mutation: in tools/cpu_sysdesc_aot.py changed the SMSW helper constant 0x80050033u to 0x80050031u, ran on Win2025 via patch: cpu_aot_sysdesc FAILED (killed). Reverted with git checkout -- ..

Findings:

  • P1: none.
  • P2: none.
  • P3 tests/cpu/sysdesc_check.py:362-383: the native-vs-lifted comparison exits 77 (skip) when the host SLDT/STR/SMSW values differ from the hard-coded 0x0000/0x0040/0x80050033, or when UMIP traps. On such hosts the whole test silently skips, including the lifted-only checks. Consider running the lifted and rejection checks before the skip, and skipping only the native comparison.
  • P3 tools/cpu_sysdesc_aot.py:222-252: SGDT, SLDT, STR and SMSW return canned Windows x64 constants, while SIDT reads the live host IDTR. This is documented in the module docstring, but the mixed model (canned vs live) is worth a note in docs for whoever enables --host-cpu.

No blocking issues found.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6049840601 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2064#pullrequestreview-6049840601). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T00:49:23Z. **Review:** COMMENTED --- Review of head `7fa4a686627b723506f100e1991c169870b2acde` (SGDT/SIDT/SLDT/SMSW/STR under opt-in host-CPU profile). Commands executed: - `git merge-tree --write-tree --name-only origin/main HEAD`: clean, no conflicts. - `gh pr checks 2064`: Linux Python, Windows build/test, gfx1013 fixtures pass. - `git diff --check origin/main...HEAD`: clean. - `python3 tests/bidi_controls_check.py`: pass. `python3 tests/docs_links_check.py`: pass. - `python3 -B tests/cpu/sysdesc_check.py` on Linux: cannot run, `iced_x86` is not installed locally (not a PR defect). - `python3 x2w_pipe.py win2025 2064 --wait 900` (Win2025, MSVC 14.44): Debug `cpu_aot_sysdesc` 1/1 passed; Release /O2 `cpu_aot_sysdesc` 1/1 passed. - Mutation: in `tools/cpu_sysdesc_aot.py` changed the SMSW helper constant `0x80050033u` to `0x80050031u`, ran on Win2025 via patch: `cpu_aot_sysdesc` FAILED (killed). Reverted with `git checkout -- .`. Findings: - P1: none. - P2: none. - P3 `tests/cpu/sysdesc_check.py:362-383`: the native-vs-lifted comparison exits 77 (skip) when the host SLDT/STR/SMSW values differ from the hard-coded `0x0000`/`0x0040`/`0x80050033`, or when UMIP traps. On such hosts the whole test silently skips, including the lifted-only checks. Consider running the lifted and rejection checks before the skip, and skipping only the native comparison. - P3 `tools/cpu_sysdesc_aot.py:222-252`: SGDT, SLDT, STR and SMSW return canned Windows x64 constants, while SIDT reads the live host IDTR. This is documented in the module docstring, but the mixed model (canned vs live) is worth a note in docs for whoever enables `--host-cpu`. No blocking issues found. AI-assisted; commands listed were executed locally.
KelboLive commented 2026-11-25 17:01:56 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by KelboLive on 2026-11-25T16:11:43Z.


APPROVED. Opt-in host-CPU profile for SGDT/SIDT/SLDT/SMSW/STR AOT lifter. Solid system descriptor extension.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7167676379 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2064#issuecomment-7167676379). Originally posted by [KelboLive](https://github.com/KelboLive) on 2026-11-25T16:11:43Z. --- APPROVED. Opt-in host-CPU profile for SGDT/SIDT/SLDT/SMSW/STR AOT lifter. Solid system descriptor extension.
upstream-sync closed this pull request 2026-11-25 19:03:30 +00:00
tedin7 commented 2026-11-25 19:04:12 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T18:23:51Z.


Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit 6f79651886b34b52f1f398d5e0081efcf6c74c01:

  • Debug (--preset dev):
    • Build (cmake --build --preset dev --target cpu_sysdesc_altered cpu_sysdesc_altered_lifted cpu_sysdesc_original cpu_sysdesc_original_lifted -- -j 2): 3.17s (configure 1.65s)
    • Test (ctest --preset dev -R '^cpu_aot_sysdesc$' --parallel 6 --no-tests=error --output-on-failure): 1/1 passed (1.31s: cpu_aot_sysdesc 1.21s)
  • Release /O2 (--preset release):
    • Build (cmake --build --preset release --target cpu_sysdesc_altered cpu_sysdesc_altered_lifted cpu_sysdesc_original cpu_sysdesc_original_lifted -- -j 2): 1.32s (configure 1.64s)
    • Test (ctest --preset release -R '^cpu_aot_sysdesc$' --parallel 6 --no-tests=error --output-on-failure): 1/1 passed (1.23s: cpu_aot_sysdesc 1.14s)
  • sccache: 4/4 hits; wall 15.1s

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7170850845 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2064#issuecomment-7170850845). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T18:23:51Z. --- Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit `6f79651886b34b52f1f398d5e0081efcf6c74c01`: - **Debug (`--preset dev`)**: - Build (`cmake --build --preset dev --target cpu_sysdesc_altered cpu_sysdesc_altered_lifted cpu_sysdesc_original cpu_sysdesc_original_lifted -- -j 2`): `3.17s` (configure `1.65s`) - Test (`ctest --preset dev -R '^cpu_aot_sysdesc$' --parallel 6 --no-tests=error --output-on-failure`): **1/1 passed** (`1.31s`: `cpu_aot_sysdesc` `1.21s`) - **Release `/O2` (`--preset release`)**: - Build (`cmake --build --preset release --target cpu_sysdesc_altered cpu_sysdesc_altered_lifted cpu_sysdesc_original cpu_sysdesc_original_lifted -- -j 2`): `1.32s` (configure `1.64s`) - Test (`ctest --preset release -R '^cpu_aot_sysdesc$' --parallel 6 --no-tests=error --output-on-failure`): **1/1 passed** (`1.23s`: `cpu_aot_sysdesc` `1.14s`) - `sccache`: 4/4 hits; wall `15.1s` AI-assisted; commands listed were executed locally.
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!2057
No description provided.