cpu: handle SHA, RDPID, RDPRU and non-temporal scalar stores the host processor lacks #1305
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1312"
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 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).MOVNTSS m32, xmm,MOVNTSD m64, xmmMOVSS/MOVSDstores: one opcode byte (2B→11). The same bytes reach memory; the non-temporal hint is dropped.RDPRURDTSC; NOP. The time-stamp counter stands in for both MPERF and APERF, so their ratio reads 1.RDPID r64RDTSCPand moves itsecx(the same IA32_TSC_AUX value) to the target.rax,rcx,rdxand the flags are kept unless one is the target.rspas target is refused.SHA1RNDS4,SHA1NEXTE,SHA1MSG1,SHA1MSG2,SHA256RNDS2,SHA256MSG1,SHA256MSG2sha_applydecodes 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 assha (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):New in it:
Ethrough NEXTE — and compared withhashlib. All seven operations are on the path of one digest or the other. Negative controls: aSHA256RNDS2that takes its two round words from the wrong half, and aSHA1RNDS4with the wrong round function, each change the digest.sha_applyoperand forms on a made-up register file and memory: register-register, REX registers with an immediate, base + index×4 + displacement with the implicitxmm0, and rip-relative. Refusals: unreadable operand,fs:override, AESENC, UD2.rax,rdxand CF survive.Debugger check (new
tests/startup_host_cpu_debug_check.py, registered asstartup_host_cpu_debug, with the new source-builttests/startup_host_cpu_probe.cpp, which computes SHA-256 of "abc" with the SHA intrinsics and reads RDPID):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) andgit diff --check(clean).Not measured
<immintrin.h>intrinsics with a clangtargetattribute under__clang__; MSVC has not compiled it. CI is the first Windows run.hashlibcomparison alone.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
#UDhandler that emulates every hit. The instruction list and the choice of RDTSC for RDPRU and RDTSCP'secxfor RDPID follow that approach; the code here is written separately, against the vendor manuals' pseudocode, for the debugger's rewrite-once model.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.