ProjectScarlett: clzero, monitorx and mwaitx crash on Intel CPUs (AMD-only instructions) #145
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/brcly--TuxBox#145
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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?
@JeffreyBytes on GitHub · 2026-11-25 05:22 UTC · original
Summary
On an Intel Core i5-12600K (Alder Lake), ProjectScarlett exits about 14 seconds after launch with
EXCEPTION_ILLEGAL_INSTRUCTION. Exit code 29. The faulting instruction isclzero. The Xbox Series CPU (Zen 2) supports it, but no Intel CPU does. This is a different instruction from #71: this CPU hasclwb, so the #80 handler returns straight away.clzerozeroes the 64-byte cache line containing the address in RAX. Here it clears a range of an object one line at a time.With
clzerohandled, the game ran for 70 s and then crashed the same way onmonitorx, AMD's user-mode wait instruction:Affected sites
A full disassembly of the executable finds 14 AMD-only instructions in two functions (RVAs). The scan also matched
rdprubytes at 0x4761A33, but those are inside acalloperand, not an instruction.0x4e8aa70,0x4e8aa90,0x4e8aab0,0x4e8aad0,0x4e8aaf0clzeroclzero; add rax,0x40; cmp rax,rN; jb0x4e8ab2dclzero0x4e8ab53,0x4e8ab72,0x4e8ab91,0x4e8abb4clzerocmp rN,rM; jbback to an earlieradd(other loop shape)0x4fb0bd7,0x4fb0d21monitorx[rdi]0x4fb0c00,0x4fb0d52mwaitxecx=2)Workaround (verified)
clzerocan't be rewritten in place likeclwb→clflushopt, because no other 3-byte instruction zeroes a line. The waits can, with a single byte. I handled both in the existing vectored exception handler:clzero: zero the 64-byte line atRAX & ~63and resume after the instruction. When the exactclzero; add rax,0x40; cmp rax,rN; jbloop follows the fault, clear every line the loop would clear, set RAX to its exit value and resume at thecmp, so one trap covers the whole loop. Writability is checked first. If the range isn't writable, it falls back to one line, and a line real hardware would fault on is left to fault.monitorx/mwaitx: turn them into no-ops. Neither changes registers, flags or memory, andmwaitxmay return at any time, so the caller already re-checks[rdi]in a loop. Stepping over each one in the handler works, but the wait loop runs continuously: in a 66-minute run, one thread trapped about 6,000 times a second, Wine'swarn:sehlines grew the log to 53 GB, and presents fell from ~80 to ~5 per second. So on the first fault the handler rewrites the second opcode byte01→1F.0F 1F FAand0F 1F FBare the same-length reserved-NOP form0F 1F /r, the change is a single atomic byte store like theclwbrewrite, and later iterations run natively. A thread that faulted on the original bytes before the store resumes at the same address and runs the NOP. If the page can't be made writable, it falls back to stepping over the instruction.Both only act when CPUID reports the instruction missing (
0x8000_0008EBX bit 0 for CLZERO,0x8000_0001ECX bit 29 for MONITORX) and the fault is inside the title image, like theclwbhandler. Unit tests cover the game's exact bytes, the loop arithmetic against a line-by-line model, unwritable memory, the in-place rewrite (including restoring page protection, and a second thread that faulted before the store), and decoder rejections. One test executes the rewritten0F 1F FA/0F 1F FBnatively on this CPU. The d3d12 suite passes under Wine (86/86).Result on this machine with
clzerohandled and the waits stepped over (before the in-place rewrite): no unhandled fault in 66 minutes. I stopped the run by hand. All five first-groupclzeroloops and both wait sites were handled. The window stayed black throughout: the game presented frames and translated 6,350 shaders (1,897 refused), but I saw no image and never reached gameplay. So this fixes the CPU crashes only. It doesn't change rendering.With the in-place rewrite (the final version): each wait site trapped once and was rewritten, and after that the wait loop ran natively. No illegal-instruction fault went unhandled in 32 minutes, and no new AMD-only site turned up. I stopped the run by hand. That isn't a stability result: the game never got past a black screen, so it reached less far than the NVIDIA runs that later crashed in Game Running. What remains is about 250 traps a second from the
clzeroloops, one per loop, and the log grew about 150 MB an hour instead of 50 GB. The window is still black and presents run at about 6 per second. Profiling puts that on the GPU side, not the CPU: one vkd3d queue thread spends a full core inside RADV's command-submission ioctl with the GPU 2–4% busy, while the game holds 12,000–17,000 GPU buffers. That's a separate issue, and I'll file it on its own.The four
clzerosites with the other loop shape haven't been hit yet. They would trap once per line with the current handler, which is correct but slower.Suggested fix
Add the two handlers next to
clwb.rs(clzero.rs,mwaitx.rs) and call them from the vectored exception handler insurvey.rs. I have this onJeffreyBytes:fix/amd-only-cpu-instructions(one commit on97b8ad3) and will open a PR that links here.Environment
RockstarGamesInc.28872443D20A71.0.2.1,game_scarlett_master.exeSHA-256e95020b2fa4a721db3df59fdecf75621730ac559d8835252f6c3b658d5a08993clwbandclflushopt; noclzeroormonitorxin/proc/cpuinfo)97b8ad3(crash),97b8ad3+e1262ac(fix)unshare --user --map-current-user --net, as in the docsFull logs are available on request. They contain local paths and the console ID, so I've quoted the relevant lines above instead.