feat(libkernel): deliver sceKernelRaiseException on Linux #1431
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1431"
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 Zaid-Talib on 2026-10-08T05:51:22Z.
What
sceKernelRaiseExceptionthrewNotImplementedon Linux, so a title whose garbage collector stops threads with it (Unity's PS5Util module: installs a SIGUSR1 handler withsceKernelInstallExceptionHandler, raises it on each thread and readsmc_rspfrom the context) could not run there. Two commits:feat(libkernel): deliver sceKernelRaiseException on Linux: the raise sends the host SIGUSR1 to the target thread withpthread_kill(model: shadPS4exception.cpp, asigactionwrapper withSA_SIGINFO | SA_RESTART). The host handler builds the FreeBSD-layoutucontextthe Windows path already uses (general registers, rip, rsp, rflags, cs, ss, 512-byte FXSAVE area), calls the guest handler on the target's own stack and writes the registers back. Only the 416 bytes up to XMM15 are written back: the rest of the FXSAVE area holds the kernel's XSAVE frame marker. A raise on another thread blocks the host signal, takes the target's join lock and returnsESRCHonce the thread has finished. The handler table is lock-free because the host handler reads it.fix(libkernel): keep raises off unregistered threads and clamp the handler's MXCSR:scePthreadCreateblocks all signals aroundpthread_createandRunThreadrestores the creator's mask once the thread is registered, so a raise right after creation cannot reach a thread whosescePthreadSelf()would adopt a second handle. The MXCSR a handler leaves is masked withMXCSR_MASK(0xffbf when it reads 0), as FreeBSD'sfpusetregsdoes, on both platforms; without it a reserved bit madert_sigreturnfail with SIGSEGV.guest_raise_exceptionis now registered on every platform. TechnicalDebt: the "throws on Linux" line is replaced by what still differs from the console (the handler runs wherever the thread was interrupted, host waits resume, calls Linux does not restart return early orEINTR).Tested
Rebased onto
mainac2c5c08c(the reviewer resolved the conflicts with thepthread_createthread start and the Windows-only test). Linux, GCC, libkernel scope:guest_raise_exceptionpasses 20/20 in a row,guest_signals,guest_exception_handlerand theguest_pthread_*tests pass; the new test fails without the source changes. Covers: self, busy, waiting insceKernelWaitSema, blocked on a host mutex, leaving a wait; register round trip (r12, r13, xmm8, MXCSR including a reserved bit); 50 raises right afterscePthreadCreatecheckingscePthreadSelf()in the handler; a finished thread returnsESRCH.Third parties: Hollow Knight: Silksong (PPSA12544, Arch, RX 9070 XT, @hugodearaujo) gets past
sceKernelRaiseException not implemented; God of War Sons of Sparta (PPSA28997, Arch, RTX 3050, @GustavoGamarra95) boots and plays several levels with this branch and #1482. Windows: not run here; @popcoder565 ran the pre-split branch on Windows 10.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)Cloud credits project contribution
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-08T06:37:32Z.
Please split this into separate PRs: the per-thread
sigprocmaskwith FreeBSD's bit layout,sceKernelRaiseExceptionon Linux, and thesleepUsEINTR fix in AudioOut. One topic per PR; at +773 lines across those it can't be reviewed or gated as one. For the Linux raise, the title ID and the log line where it stops on main would show the need.Imported from boykopovar/AnyPS5. Originally posted by popcoder565 on 2026-10-08T07:09:04Z.
Since Windows wasn't run, I ran it there before the split, in case it helps the Windows half: Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs), Release,
mainbc1fbe81merged with 24f89cf0.guest_signalspasses 10 of 10 runs andguest_exception_handler5 of 5.guest_raise_exceptionhangs in 10 of 10 runs (ctest timeout). In 9 runs it hangs in the new toggling phase. The 10th hangs in the busy phase, which is theNtContinuerace #1421 fixes, independent of this PR.In the toggling hang, main waits in
scePthreadJoin, and both toggling threads are stuck in the same place:MinGW's
thread_localgoes through emutls and winpthreads'pthread_getspecific, which takes a per-thread spinlock (spin_keysin mingw-w64'spthread.c)._sigprocmask_nid_postfixcallsscePthreadSelf()before it claims SIGUSR1's bit, so a raise can land while the thread holds that spinlock. The delivery then ends inDeliverUnblocked, which self-raises the SIGUSR1 that went pending, andRaiseOnchecksthread == scePthreadSelf()on that same thread. It spins on the lock the interrupted frame holds. Any emutlsthread_localread on the delivery path can do the same, for examplecurrentThreadand theTimedWaitwait state.A way out could be to identify the current thread on the delivery path without emutls: compare
GetCurrentThreadId()with the target's native thread id, or pass thePthreadPrivate&thatDeliveralready has to a self-delivery helper instead of going throughsceKernelRaiseException.Imported from boykopovar/AnyPS5. Originally posted by popcoder565 on 2026-10-08T07:45:16Z.
Follow-up: the same deadlock reproduces on
mainwithout this PR, when a handler reads athread_localwhile the target is interrupted insidescePthreadSelf. #1421 now retries any raise that finds its target insidelibwinpthread-1.dll(384078c9), so the toggling phase should stop hanging once this is rebased on it.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-09T09:22:08Z.
The branch now has only the Linux
sceKernelRaiseExceptiondelivery and the MXCSR clamp, but the title and description still describe the five-commit version (per-threadsigprocmask, AudioOut). Please update them to match the diff.Imported from boykopovar/AnyPS5. Originally posted by hugodearaujo on 2026-10-09T12:41:07Z.
Also gets Hollow Knight: Silksong (PPSA12544) further on Linux (Arch, GCC, RX 9070 XT). On current
main(d70b89989) with #1836 applied (without it, the title stops earlier at shader registration), it stops at:With this branch merged on top as well, that raise is delivered. The title loads
globalgamemanagers,unity default resourcesandsharedassets0, and openslevel0. It then stops on an uncaughtIl2CppExceptionWrapper, which looks unrelated to signal delivery. It is most likely a Unity plugin that cannot start on Linux yet (#846).Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-09T21:27:40Z.
Review: CHANGES_REQUESTED
Thanks for splitting this. Two things before it can be reviewed and merged:
mainincore/libs/tests/GuestRaiseException.cpp(#1478 changed it). Please rebase and rerunguest_raise_exceptionandguest_signals.sigprocmask, AudioOutsleepUs, 409 ctest results, stress runs, mutation checks for code that is no longer here), as @oneandonlydean asked on Oct 9. Please rewrite them for the current diff: LinuxsceKernelRaiseExceptiondelivery, the startup signal block inscePthreadCreate, and the MXCSR clamp. Update "Tested" with what you ran on this head, and the list of overlapping PRs.Useful for the description: the Silksong (PPSA12544) log @hugodearaujo posted, where
mainstops atsceKernelRaiseException not implemented.Imported from boykopovar/AnyPS5. Originally posted by GustavoGamarra95 on 2026-10-11T02:48:09Z.
Another title this gets further on Linux: God of War Sons of Sparta (PPSA28997, Unity IL2CPP). Its GC stops threads with
sceKernelRaiseException, so onmainit stops withsceKernelRaiseException not implementedright after creating its job workers. With this branch (head77143010) the raise is delivered, and the title boots and plays through several levels on Arch Linux (RTX 3050), together with #1482.To test it I had to rebase it onto
main942a174emyself. The conflicts includedPthread.hpp,Exception.cpp,Thread.cppandGuestRaiseException.cpp:Thread.cpp:mainnow creates guest threads withpthread_createinstead ofstd::thread. I blocked all signals aroundpthread_create, restored the creator's mask on both the success and the failure path, and set the saved mask inRunThreadas the branch does.GuestRaiseException.cpp: I did not merge it, becausemainadded Windows checks in the same places as the branch's Linux ones.I can share the resolved diff if it helps.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T09:20:10Z.
Review: APPROVED
Rebased onto current
main(c356112f3) and fixed the two points from the earlier review ourselves:pthread_create, so the startup signal block wraps that call andRunThreadrestores the creator's mask;GuestRaiseException.cppnow runs on every platform, with the Windows-only parts under#ifdef _WIN32; removed the duplicatedhandlerSelf.Checked:
guest_raise_exception20/20 in a row,guest_signals,guest_exception_handlerandguest_pthread_*green on Linux; the test fails without the source changes. Windows not run here.Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-11T09:21:44Z.
Progress report (
e272b92→c356112)📈 System libraries: 85.48% (+0.03%, +1 implemented functions)
✅ 1 implemented
sceKernelRaiseException