feat(relinker): lower SHA-256 instructions for --to-intel #200
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/200"
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 boykopovar/AnyPS5. Originally posted by ljkx on 2026-09-30T22:25:29Z.
What
Addresses part of the SHA-NI item in
docs/dev/TechnicalDebt.md.--to-intelpassed SHA-NI instructions through unchanged, so a relinked title that executes them fails with an illegal instruction on Intel CPUs without SHA-NI (Haswell to Comet Lake, e.g. the i5-7500 and i5-7200 from the compatibility table).DecodedInstruction::IsShaNiexisted but was not used, and its opcode list did not include SHA256RNDS2 (0F 38 CB).No title is currently known to use SHA-NI; this removes the silent pass-through for SHA-256. CPUID is not intercepted, so code that checks for SHA-NI already takes its own path on the host; this matters for code that uses it unconditionally on the fixed PS5 CPU, as Demon's Souls does with INSERTQ. SHA-1 is left for a follow-up PR.
Sha256Lowering. Only legacy SSE2 instructions are used, so the upper YMM halves, RFLAGS and the general-purpose registers are untouched. Rotations are shift pairs; each lane-0 scalar of the two rounds of SHA256RNDS2 lives in its own scratch register (8 spilled scratch registers), and SHA256MSG2 computes W16/W17 before W18/W19.MatchSequencenow accepts SHA-256 next to EXTRQ/INSERTQ, so consecutive short SSE4a and SHA-256 sites share one stub; other following instructions are copied after the body as before.Sse4aLowering.cpptoStubBodyBuilderso both lowerings use it. It gainsSseImm(PSHUFD) andShiftDwordImm(PSRLD/PSLLD). The SSE4a golden bodies are byte-identical.IsShaNinow includes0F 38 CB(it is still unused); the newIsSha256selects the three instructions.TechnicalDebt.md: the item now names the remaining SHA-1 instructions and the SHA-256 memory forms.Tested
amd64_only_windows: every destination/source pair (all 256) for each of the three instructions, and 256 random sequences of 2–3 instructions throughMatchSequence, on random register images. Results are checked against a reference transcribed from the SDM, plus the existing clobber / RFLAGS / rsp / red-zone checks.amd64_only_converter: operand decoding (REX, memory form rejected, prefixed0F 38 CBand SHA-1 not taken as SHA-256), matcher names, SHA-1 still unmatched, a short SHA256RNDS2 absorbing the following SHA256MSG1, a short SHA256MSG2 absorbing anop, the failure before aret, and the file offset of the memory-form failure. On x86-64 Linux the stubs are executed with distinct and equal operands.ctest._mm_sha256rnds2/msg1/msg2_epu32on 1,000,000 random inputs each. The lowered stubs match the native instructions on all 128 bits of all 16 XMM registers, the status flags and rsp: 307,200 single-instruction runs (3 instructions × 256 register pairs × 400) and 20,000 random 2–4 instruction sequences. The same six mutations each produce mismatches there.test_guest_intel_trampolines.pyfixture with a SHA256RNDS2 +nopsite, run throughrelinker --to-intel(Linux ELF and Windows PE output, main executable and guest module): the site jumps to a stub without the original instruction, and the stub returns after the absorbednop.Checklist
main; no other open PR implements the same functions.mdfiles or images added to the repository