loader: enable the AMD CPU instruction patcher on macOS #1014
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
GPU
GUI
help wanted
question
Shaders
Video
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/KytyPS5--KytyPS5!1014
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/1014/head"
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?
@wuboo on GitHub · 2026-10-03 17:50 UTC · original
wuboo:pr/sse4a-macos→main· +244 −43 · 5 files · 5 commits · merged 2026-10-06 17:55 UTCWhat
The guest instruction patcher from #839 and the INSERTQ/RDPID/CLWB follow-up (
19a9464b) is compiled out onmacOS (
#if !defined(__APPLE__)around xbyak,PatchModule,Register/UnregisterGuestInstructionPatchModule),and
RuntimeLinker::LoadProgramToMemoryforcesemulate_amd = falsethere.This PR:
guestInstructionPatcher.cppon all hosts (xbyak is already linked everywhere) and drops themacOS stubs;
Config::AmdCpuEnabled()(--amd-cpu) on macOS exactly like on Windows and Linux, so the switch andits default are unchanged;
macos_sse4ato run the patcher on macOS.No new flag and no change for other platforms.
Why
Rosetta 2 does not implement SSE4a, so every
EXTRQraises SIGILL and goes through the signal handler andX64InstructionEmulator. On Apple Silicon (M3 Max, Rosetta 2) in Astro Bot one SIGILL costs about 17.9 us and thegame executes
EXTRQ(register and immediate forms) at about 97 sites of the eboot, hundreds of thousands ofhits each. In a level the process took about 41k SIGILL/s, 95% of them
EXTRQ.Numbers from an earlier, independent version of this change on our fork (it rewrote
EXTRQ/INSERTQinto SSE2trampolines in the loader, commit 41d54224 on our bench branch; same idea as this patcher, not this code):
Same binary,
KYTY_OPT_OFF=sse4a_patchtoggled, one run each; intro chase scene went from 4.42 to 9.86 frames/s.These are results of that earlier version, not yet of this PR. In-game validation of this branch is pending.
How it was tested
macos_sse4a(x86-64 macOS, runs under Rosetta): maps an RWX code page with a trampoline page behind it,registers the module, runs
PatchGuestInstructionswith the host features Rosetta reports(no SSE4a/RDPID/CLWB) and compares against the SIGILL emulator on the same inputs:
EXTRQimmediate and REX.B (xmm10) become native trampolines, the patched copy raises no SIGILL andreturns the same bytes as the emulator;
INSERTQ(immediate, indexed REX form, register form) is marked as a trap by the patcher, as on otherhosts, and keeps going through the emulator with identical results;
emulate_amd = falsenothing is patched and SIGILL is still raised.The new test fails without the change (patcher stubbed out on macOS: "EXTRQ immediate found by the patcher").
ctest(50 tests) on macOS x86-64 compared withmainat19a9464b: identical results, 34 pass and 16 GPU tests skipped (no Vulkan loader on the test machine) in both trees; no regression.Notes
INSERTQstays a trap in the patcher (unchanged from main), so it still costs one SIGILL per execution onmacOS; it was the minor part (about 5%) of the measured signals. A native
INSERTQtrampoline can follow ifwanted.
#984 is now rebased on main and starts its split trampolines behind the bytes this patcher has used
(
GetGuestInstructionTrampolineBytesUsed), so with--amd-cpuand the split both active on macOS they nolonger overwrite each other. I have a test that loads one module with an
EXTRQand a page-crossing 256-bitstore and checks both results and that the trampoline ranges are disjoint; it needs both changes, so I will add
it to whichever of the two lands second.
@coderabbitai[bot] on GitHub · 2026-10-03 17:59 UTC · original · edited 2026-10-06 17:54 UTC
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
c7511823-cb51-4d2d-af21-16bed2a3275d📥 Commits
Reviewing files that changed from the base of the PR and between
91e2f2b7ecande2afc57499.📒 Files selected for processing (3)
src/loader/guestInstructionPatcher.cppsrc/loader/x64InstructionEmulator.cpptests/macosSse4aTests.cpp💤 Files with no reviewable changes (2)
🚧 Files skipped from review as they are similar to previous changes (1)
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
📝 Walkthrough
Walkthrough
macOS now uses the shared guest-instruction patching and module registry implementations. The runtime linker reads the AMD emulation setting on macOS. Apple instruction dispatch now attempts reciprocal-square-root and MONITORX/MWAITX emulation. The macOS SSE4a test target includes the patcher and Xbyak. New tests cover VRSQRTPS, MWAITX, and EXTRQ and INSERTQ patching.
Priority: ➖ Normal
Change: Feature
Merge Risk: ⚪ Minimal · up to
e2afcThis change enables the AMD guest-instruction patcher on macOS and adds emulation paths for trapped instructions. No actionable merge-blocking risk was identified in the supplied context. In-game validation was reported as pending by the author.
Security Architecture Review
Security architecture risk: 🔵 Low · up to
6218cThe change remains optional and disabled by default, and it reuses existing execution permissions rather than adding privileges. No introduced security vulnerability was established, but reliable code publication and interrupted or concurrent lifecycle behavior remain incompletely validated.
Retained concerns
No architecture-level concerns identified.
Security review details
Security Blast Radius
Trust Boundaries and Controls
Resilience and Maintainability Implications
Hardening Proposals
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share
Comment
@coderabbitai helpto get the list of available commands.@wuboo on GitHub · 2026-10-06 06:19 UTC · original
I rebased this branch on current main and pushed a follow-up commit:
loader: emulate the patcher's VRSQRTPS trap and MWAITX on macOS
With the AMD CPU patcher enabled on macOS, a VRSQRTPS that the patcher cannot relocate is marked as a trap
by clearing a reserved VEX.vvvv bit. That raises #UD on every host, Rosetta included. On macOS
TryEmulateReciprocalSquareRootandTryEmulateMonitorxMwaitxwere still under#if !defined(__APPLE__), so thefirst such site ended in an unhandled SIGILL when running with
--amd-cpu.Both emulators are plain SSE2/libc, so they are now built on macOS and dispatched from
TryEmulate. The DarwinContextgainsClearUpperYmmso the VEX.128 result zeroes YMM[255:128] when the thread has the AVX state(
uc_mcsizecheck).macos_sse4anow covers a trapped VRSQRTPS (including +0, negative, +inf inputs and the upperhalf of YMM) and MWAITX.
Verification: full
ctest(50 tests) matches the main baseline (34 pass, 16 skipped only because the Vulkan loader isnot available in the test run, 0 failures).
macos_sse4afails with "unexpected emulator refusal" when theemulator change is reverted and passes with it.
@coderabbitai[bot] on GitHub · 2026-10-06 06:34 UTC · original · reviewed
🧹 Nitpick comments (1)
ℹ️ Review info
⚙️ Run configuration
65948759-ad27-4711-9b03-9591e84925ea📥 Commits
Reviewing files that changed from the base of the PR and between 6218c9064ed46aad1fbc9aa0c1048a87069d4b2e and
b452d04e4b.📒 Files selected for processing (4)
src/loader/guestInstructionPatcher.cppsrc/loader/runtimeLinker.cppsrc/loader/x64InstructionEmulator.cpptests/macosSse4aTests.cpp💤 Files with no reviewable changes (2)
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
@wuboo on GitHub · 2026-10-06 08:04 UTC · original
Good catch. The old
VRSQRTPScase started from an instruction that was already marked as a trap, so it never went through the patcher.91e2f2b7adds cases that feed the ordinary VEX.128 encoding (c5 f8 52 c8) toPatchGuestInstructionsand compare the result with the emulated reference, in both ways the patcher can handle it: a native trampoline (no SIGILL), and, when the instruction cannot be relocated, the in-place trap mark (c5 f8->c5 f0) plus the signal emulator. Disabling the trap mark makes the test fail.