cpu: handle SHA, RDPID, RDPRU and non-temporal scalar stores the host processor lacks #1305

Merged
JohnConnorNPC merged 10 commits from upstream-pr/1312 into main 2026-11-24 10:08:53 +00:00
JohnConnorNPC commented 2026-11-24 07:35:41 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by JohnConnorNPC on 2026-11-24T07:35:41Z.


Refs #870. Stacked on #1274, which must land first: until it does, this PR's diff also shows #1274's commits. The new work is the last commit.

Problem

#1274 rewrites CLWB, CLZERO, MONITORX/MWAITX and the immediate INSERTQ/EXTRQ for hosts that lack them. A Zen 2 executable can use four more groups that many hosts lack: the SHA extensions, RDPID, RDPRU and the SSE4a scalar non-temporal stores. On such a host each still ends a startup run with an illegal-instruction exception.

Change

Same switch (X2W_HOST_CPU=1, run.ps1 -HostCpu), same module (tools/startup/host_cpu.py).

Instruction Handling
MOVNTSS m32, xmm, MOVNTSD m64, xmm Rewritten once to MOVSS / MOVSD stores: one opcode byte (2B → 11). The same bytes reach memory; the non-temporal hint is dropped.
RDPRU Rewritten once to RDTSC; NOP. The time-stamp counter stands in for both MPERF and APERF, so their ratio reads 1.
RDPID r64 Replaced once by a jump to a stub that runs RDTSCP and moves its ecx (the same IA32_TSC_AUX value) to the target. rax, rcx, rdx and the flags are kept unless one is the target. rsp as target is refused.
SHA1RNDS4, SHA1NEXTE, SHA1MSG1, SHA1MSG2, SHA256RNDS2, SHA256MSG1, SHA256MSG2 Not rewritten. sha_apply decodes the instruction with iced-x86, reads the operands (register, or memory with base, index, scale, displacement or rip-relative) through the debugger and writes the result into the thread's context, at every hit.

SHA is the exception to "rewrite once" on purpose: a machine-code stub for seven instructions is a lot of hand-written code to trust, and the operations in Python are short and checkable against hashlib. The cost is one debugger round trip per SHA instruction, which is fine for an occasional digest and too slow for bulk hashing. The summary counts them as sha (per hit) so that cost is visible.

Refused: SHA operands with an fs:/gs: override, unreadable memory operands, and anything that is not one of the forms above.

Evidence

Commit under test: the head of this branch (on #1274 merged with main).

Host check (tests/startup_host_cpu_check.py, already registered by #1274):

startup_host_cpu: PASS (288 insertq/extrq results, clzero with two moved-instruction forms, 5 clwb and 2 wait rewrites, 4 non-temporal stores, rdpru, rdpid into 7 registers, sha-1 and sha-256 of 4 messages through the seven sha operations with 0 native results; native comparison: none)

New in it:

  • SHA. SHA-1 and SHA-256 of four messages (0, 3, 200 and 768 bytes) are computed the way SHA-extension code computes them — every schedule word past the first sixteen through the MSG1/MSG2 instructions, every round through RNDS4/RNDS2, SHA-1's E through NEXTE — and compared with hashlib. All seven operations are on the path of one digest or the other. Negative controls: a SHA256RNDS2 that takes its two round words from the wrong half, and a SHA1RNDS4 with the wrong round function, each change the digest.
  • sha_apply operand forms on a made-up register file and memory: register-register, REX registers with an immediate, base + index×4 + displacement with the implicit xmm0, and rip-relative. Refusals: unreadable operand, fs: override, AESENC, UD2.
  • Non-temporal stores. Four encodings are decoded before and after with iced-x86 (same length, register and memory operand); MOVNTPS, MOVNTPD, the register form and MOVSS itself are refused; the rewritten 4- and 8-byte stores are executed and must leave the neighbouring bytes alone.
  • RDPRU. The replacement decodes as RDTSC + NOP, and two executions give rising values.
  • RDPID. The stub is executed for seven target registers and must equal what RDTSCP reports just before or just after (a thread can move between processors); a separate function checks rax, rdx and CF survive.
  • On a host that has SHA or RDPID (CPUID leaf 7), the real instructions run too and must agree: 16 random operand sets for each of the seven SHA instructions. That did not run here.

Debugger check (new tests/startup_host_cpu_debug_check.py, registered as startup_host_cpu_debug, with the new source-built tests/startup_host_cpu_probe.cpp, which computes SHA-256 of "abc" with the SHA intrinsics and reads RDPID):

startup_host_cpu_debug: PASS (performed by the debugger: rdpid x1, sha (per hit) x56)

On this host (Intel Core i7-10700KF: no SHA, no RDPID) the probe prints the correct digest and "rdpid agrees with rdtscp" with the switch, the debugger reports exactly 56 SHA instructions (32 RNDS2, 12 MSG1, 12 MSG2 for one block), and the same run without the switch stops at an illegal instruction. On a host that has the instructions the check passes with "nothing rewritten" and says so.

Run on: Linux x86-64 with CPython 3.13 and iced-x86 1.21.0 (host check), and CPython 3.12 for Windows under Wine 11.0 (Proton Experimental) with the probe built by clang-cl against the Windows SDK (both checks). Translation-layer results, not Windows.

Also ran python -B tests/docs_links_check.py (pass) and git diff --check (clean).

Not measured

  • Windows, and MSVC. The probe uses <immintrin.h> intrinsics with a clang target attribute under __clang__; MSVC has not compiled it. CI is the first Windows run.
  • A host with SHA-NI. The native comparison of the seven SHA operations has not run. CI runners that have SHA will run it; I would treat that as the stronger evidence for the Python operations than my hashlib comparison alone.
  • In a startup run. None of these instructions was reached in the console executable I have run; a static sweep of it found none of them. This PR is for other executables and hosts, and for completeness of the switch.
  • RDPRU's stand-in ignores ecx (MPERF or APERF) and leaves the flags alone; I have not checked the flag behaviour of the real instruction on hardware.

Prior work

An independent fork of this project handles the same set in an in-process #UD handler that emulates every hit. The instruction list and the choice of RDTSC for RDPRU and RDTSCP's ecx for RDPID follow that approach; the code here is written separately, against the vendor manuals' pseudocode, for the debugger's rewrite-once model.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6576949895 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1312). Originally posted by [JohnConnorNPC](https://github.com/JohnConnorNPC) on 2026-11-24T07:35:41Z. --- Refs #870. **Stacked on #1274**, which must land first: until it does, this PR's diff also shows #1274's commits. The new work is the last commit. ## Problem #1274 rewrites CLWB, CLZERO, MONITORX/MWAITX and the immediate INSERTQ/EXTRQ for hosts that lack them. A Zen 2 executable can use four more groups that many hosts lack: the SHA extensions, RDPID, RDPRU and the SSE4a scalar non-temporal stores. On such a host each still ends a startup run with an illegal-instruction exception. ## Change Same switch (`X2W_HOST_CPU=1`, `run.ps1 -HostCpu`), same module (`tools/startup/host_cpu.py`). | Instruction | Handling | |---|---| | `MOVNTSS m32, xmm`, `MOVNTSD m64, xmm` | Rewritten once to `MOVSS` / `MOVSD` stores: one opcode byte (`2B` → `11`). The same bytes reach memory; the non-temporal hint is dropped. | | `RDPRU` | Rewritten once to `RDTSC; NOP`. The time-stamp counter stands in for both MPERF and APERF, so their ratio reads 1. | | `RDPID r64` | Replaced once by a jump to a stub that runs `RDTSCP` and moves its `ecx` (the same IA32_TSC_AUX value) to the target. `rax`, `rcx`, `rdx` and the flags are kept unless one is the target. `rsp` as target is refused. | | `SHA1RNDS4`, `SHA1NEXTE`, `SHA1MSG1`, `SHA1MSG2`, `SHA256RNDS2`, `SHA256MSG1`, `SHA256MSG2` | **Not rewritten.** `sha_apply` decodes the instruction with iced-x86, reads the operands (register, or memory with base, index, scale, displacement or rip-relative) through the debugger and writes the result into the thread's context, at every hit. | SHA is the exception to "rewrite once" on purpose: a machine-code stub for seven instructions is a lot of hand-written code to trust, and the operations in Python are short and checkable against `hashlib`. The cost is one debugger round trip per SHA instruction, which is fine for an occasional digest and too slow for bulk hashing. The summary counts them as `sha (per hit)` so that cost is visible. Refused: SHA operands with an `fs:`/`gs:` override, unreadable memory operands, and anything that is not one of the forms above. ## Evidence Commit under test: the head of this branch (on #1274 merged with `main`). **Host check** (`tests/startup_host_cpu_check.py`, already registered by #1274): ``` startup_host_cpu: PASS (288 insertq/extrq results, clzero with two moved-instruction forms, 5 clwb and 2 wait rewrites, 4 non-temporal stores, rdpru, rdpid into 7 registers, sha-1 and sha-256 of 4 messages through the seven sha operations with 0 native results; native comparison: none) ``` New in it: - **SHA.** SHA-1 and SHA-256 of four messages (0, 3, 200 and 768 bytes) are computed the way SHA-extension code computes them — every schedule word past the first sixteen through the MSG1/MSG2 instructions, every round through RNDS4/RNDS2, SHA-1's `E` through NEXTE — and compared with `hashlib`. All seven operations are on the path of one digest or the other. Negative controls: a `SHA256RNDS2` that takes its two round words from the wrong half, and a `SHA1RNDS4` with the wrong round function, each change the digest. - **`sha_apply` operand forms** on a made-up register file and memory: register-register, REX registers with an immediate, base + index×4 + displacement with the implicit `xmm0`, and rip-relative. Refusals: unreadable operand, `fs:` override, AESENC, UD2. - **Non-temporal stores.** Four encodings are decoded before and after with iced-x86 (same length, register and memory operand); MOVNTPS, MOVNTPD, the register form and MOVSS itself are refused; the rewritten 4- and 8-byte stores are executed and must leave the neighbouring bytes alone. - **RDPRU.** The replacement decodes as RDTSC + NOP, and two executions give rising values. - **RDPID.** The stub is executed for seven target registers and must equal what RDTSCP reports just before or just after (a thread can move between processors); a separate function checks `rax`, `rdx` and CF survive. - On a host that has SHA or RDPID (CPUID leaf 7), the real instructions run too and must agree: 16 random operand sets for each of the seven SHA instructions. That did not run here. **Debugger check** (new `tests/startup_host_cpu_debug_check.py`, registered as `startup_host_cpu_debug`, with the new source-built `tests/startup_host_cpu_probe.cpp`, which computes SHA-256 of "abc" with the SHA intrinsics and reads RDPID): ``` startup_host_cpu_debug: PASS (performed by the debugger: rdpid x1, sha (per hit) x56) ``` On this host (Intel Core i7-10700KF: no SHA, no RDPID) the probe prints the correct digest and "rdpid agrees with rdtscp" with the switch, the debugger reports exactly 56 SHA instructions (32 RNDS2, 12 MSG1, 12 MSG2 for one block), and the same run without the switch stops at an illegal instruction. On a host that has the instructions the check passes with "nothing rewritten" and says so. Run on: Linux x86-64 with CPython 3.13 and iced-x86 1.21.0 (host check), and CPython 3.12 for Windows under Wine 11.0 (Proton Experimental) with the probe built by clang-cl against the Windows SDK (both checks). Translation-layer results, not Windows. Also ran `python -B tests/docs_links_check.py` (pass) and `git diff --check` (clean). ## Not measured - **Windows, and MSVC.** The probe uses `<immintrin.h>` intrinsics with a clang `target` attribute under `__clang__`; MSVC has not compiled it. CI is the first Windows run. - **A host with SHA-NI.** The native comparison of the seven SHA operations has not run. CI runners that have SHA will run it; I would treat that as the stronger evidence for the Python operations than my `hashlib` comparison alone. - **In a startup run.** None of these instructions was reached in the console executable I have run; a static sweep of it found none of them. This PR is for other executables and hosts, and for completeness of the switch. - RDPRU's stand-in ignores `ecx` (MPERF or APERF) and leaves the flags alone; I have not checked the flag behaviour of the real instruction on hardware. ## Prior work An independent fork of this project handles the same set in an in-process `#UD` handler that emulates every hit. The instruction list and the choice of RDTSC for RDPRU and RDTSCP's `ecx` for RDPID follow that approach; the code here is written separately, against the vendor manuals' pseudocode, for the debugger's rewrite-once model.
upstream-sync closed this pull request 2026-11-24 11:02:36 +00:00
R0Y00 commented 2026-11-24 11:02:36 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T10:08:49Z.

Review: APPROVED


Verified the Windows x64 host fixture on this head, including 112 native SHA results and the RDPID checks. The CLZERO controls from the prerequisite still pass.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6037272043 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1312#pullrequestreview-6037272043). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-24T10:08:49Z. **Review:** APPROVED --- Verified the Windows x64 host fixture on this head, including 112 native SHA results and the RDPID checks. The CLZERO controls from the prerequisite still pass.
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!1305
No description provided.