fix(libkernel): keep ymm upper halves across a redirected raise #1478
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1478"
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 awake-devel on 2026-10-08T08:49:17Z.
What
On Windows,
sceKernelRaiseExceptionredirects a thread that is not in an alertableTimedWaitwait: it reads the thread's context, points it atAps5RedirectedEntryStub, runs the handler and resumes the thread withRtlRestoreContext. That context has noCONTEXT_XSTATE, so only the low 128 bits of each vector register are restored. The upper halves ofymm0-ymm15come back as the handler and the delivery path left them, and anything there that runsvzeroupperor a VEX instruction (Zen 2 guest code compiled for AVX, the CRT'smemcpywhen it copies theCONTEXT) clears them. A guest thread interrupted in the middle of AVX code, for example a 32-byte copy loop, then resumes with zeroed upper lanes and writes wrong data. TechnicalDebt listed this as a known gap.ymm0-ymm15in the delivery it already fills with the live registers, when the CPU and the OS enable AVX (__builtin_cpu_supports("avx"), which checksXCR0).RtlRestoreContextitself: a new exit routine loads the savedymm0-ymm15and then jumps toRtlRestoreContext, which restores the low halves (and any change the handler made to them throughuc_mcontext) without touching the upper halves.This touches lines that #1421 (open) and #1431 (open) also change in
Exception.cpp,GuestRaiseException.cppandTechnicalDebt.md; the conflicts are textual. The change does not depend on the restore path #1421 selects (NtContinuetoday, the user-modeiretqrestore with #1421): neither touches the upper halves, which the exit loads right before it. It also applies on top of #1421; the conflicts are textual.This PR was investigated and written by Claude Code (AI), and posted by me after review.
Tested
Not built or run on Windows. I have no Windows machine, so neither the fix nor the new test phase has been compiled with MinGW or executed. What was checked:
CONTEXTis 1232 bytes withRaxat 0x78, so inDeliverythe context starts at 16,vectorsat 1248 andsaveVectorsat 1760; the stub runs 40 bytes below the delivery, which gives the 176/1288/1800 displacements it uses. Thestatic_asserts encode these and will fail the Windows build if any is wrong.-DBUILD_TESTING=ON,APS5_WRITE_WATCH_IMPORTS=watch): the changed code is under_WIN32and the test is registered on Windows only, so Linux is unaffected. Fullctest -j4: 423/423 passed, 0 failed, 0 skipped, same asmain(bc1fbe81). UnderVK_LAYER_KHRONOS_validation(ctest -j2 -V): 423/423 passed, 27Validation Errorlines, the same VUIDs and counts asmain.What needs to be run on Windows:
ctest -R ^guest_raise_exception$ --repeat until-fail:20: the new phase loads a pattern intoymm0-ymm15, spins, gets 100 raises whose handler runsvzeroall, and compares the registers afterwards. It is skipped on a CPU without AVX.Exception.cppreverted tomain(test kept), the new phase should fail (the upper lanes come back zeroed); with the fix it should pass.main, and a Unity title that stops threads for GC (for example the one in discussion #726).Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
https://claude.ai/code/session_01HeC9bA3n74TzJRYv9AgBE8
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-08T09:00:06Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)core/libs/prx/libkernel/System/src/Exception.cppL15-18, L83-112, L159-163, L186-190, L193-234, L247-266Imported from boykopovar/AnyPS5. Originally posted by popcoder565 on 2026-10-08T09:19:04Z.
I tested this on Windows 10 (19045) with MinGW-w64 GCC 15.2.0 (WinLibs r7), a Release build, on an Intel Core i7-12700H (AVX2, no AVX-512). It builds, the
static_asserts hold, and the new phase does what it should. Below is the run on its own onmain, then together with #1421.This PR merged onto
maine82e03f1ctest -R ^guest_raise_exception$ --repeat until-fail:20: 3 passes, then a timeout in the busy-thread phase (only the first thread had started). This is the lost-delivery hang that #1421 fixes, and it happens onmainwithout this PR too. The vector phase passed in the 3 runs that reached it. Those runs return throughNtContinue, so that path keeps the reloaded upper halves as well.agc_driver_srgb8_color_targetfailed (#1434, an NVIDIA import flake), andguest_raise_exceptiontimed out, as above.Exception.cppback tomain, test kept): 1 of 4 runs aborted, and 3 of 4 timed out. Withmain's context flags each handler returns throughNtContinue. This phase raises again as soon ascallsmoves, so the next raise often finds the thread insideNtContinue, and that delivery is lost (#1421).ExpectDelivery's 5000 ×sleep_for(1 ms)takes 15.6 ms per step on Windows (#1471), so a lost delivery outlasts the 30 s timeout instead of failing theRequire. Onmainalone the mutation check is therefore noisy.Together with #1421 (merged locally; the conflicts were textual):
RaiseOn, I kept #1421's retry loop with this PR'sDeliveryinitializers:{handler, signum, {}, {}, 0}for the APC copy andSaveVectors ? 1u : 0ufor the redirect.ExpectDeliverybase addsContinuingRounds + SelfingRounds.Results:
ctest -R ^guest_raise_exception$ --repeat until-fail:20: 20 of 20 pass, 10.1 to 10.6 s each.Exception.cppwithout this PR's changes, merged test kept): 5 of 5 runs abort at the vector comparison. With a temporary print before thememcmp, 256 of the 512 bytes differ, all of them in the upper halves; the low halves come back throughRtlRestoreContext. With this PR the phase passes. So the user-modeiretqrestore that #1421 selects also keeps the upper halves thatAps5RedirectedExitloads.agc_driver_srgb8_color_target(#1434).Not tested: a Unity title that stops threads for GC. I don't have one.
Tested with AI assistance (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by hikazey on 2026-10-08T10:24:27Z.
A second Windows machine, AMD this time: Windows 11 Pro (26200), MinGW-w64 GCC 15.2.0 (WinLibs r7, the CI build), Release, configured like CI, Ryzen 9 5900X (Zen 3, AVX2, no AVX-512). This commit merged onto
main73ac9ef7on its own, without #1421.guest_raise_exceptionstatic_asserts on theCONTEXT/Deliveryoffsets holdException.cppfrommain, new test phase keptmainalone: 429/429)In the reverted runs the abort comes after the fifth thread starts and before the
finishedphase, which puts it in the new vector phase. So the ymm upper halves are lost onmainon this CPU, and this change keeps them. No busy-thread hang (#1421) occurred in these 6 runs.AI-assisted: yes (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-09T09:56:06Z.
Could you rebase onto current main? The full build checks out this branch with main's workflow, and it stops at
tools/check_test_registration.pynot being on the branch yet, so nothing gets compiled. After a rebase it can get a full build and go in.