Repository navigation
feat(libkernel): deliver sceKernelRaiseException on Windows - #760
Conversation
sceKernelInstallExceptionHandler and sceKernelRemoveExceptionHandler threw. They now keep one handler per allowed signal (SIGHUP, SIGILL, SIGFPE, SIGBUS, SIGSEGV, SIGUSR1, as in shadPS4): another signal or a null handler returns EINVAL, a second handler for a signal returns EAGAIN, and removing clears it. Delivery is not implemented and is recorded in the technical debt; sceKernelRaiseException still throws. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Run the SIGUSR1 handler recorded by sceKernelInstallExceptionHandler on the target thread with a PS5 ucontext (uc_mcontext at 0x40, FreeBSD amd64 layout) of its interrupted state: - on the calling thread, directly; - on a thread in a libkernel wait, from an APC: the TimedWait waits are alertable and count themselves in a per-thread wait state, so the handler runs inside the wait, which then continues; - on a running thread, by suspending it and redirecting it to an entry that calls the handler and restores the possibly modified context. Other signals return EINVAL, as in shadPS4; raising without a handler throws. Linux still throws. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks for this. One thing in the redirect path for a running thread:
Related, and smaller: The rest looks right to me: the |
…/kernel-raise-exception-windows # Conflicts: # core/libs/prx/libkernel/Pthread/src/Thread.cpp # docs/dev/TechnicalDebt.md
The redirect entry's Windows x64 home area started inside the guest's 128-byte red zone, and the interrupted thread freed the delivery on the heap, which it may have been suspended inside. The delivery is now copied onto the target stack below the red zone, with the home area below it, and the copy is refused when those pages are not committed. AI-assisted: yes Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks, both fixed in b550545. The delivery is now copied onto the target stack at |
…sters A thread suspended inside a system call returns through sysret, which overwrites rcx and r11, so the delivery address passed in rcx was garbage when the redirected thread resumed. PPSA12544 crashed that way on its SaveDataProcessQueue thread, blocked in a host wait when Unity's GC raised SIGUSR1. The redirected rip is now an entry stub that takes the delivery from its fixed place above rsp. It also stores the live general registers into the delivery's context first: the snapshot taken while the thread was in the system call still holds the call number in rax instead of its result, which the restore would have handed back to the wait. The guest test raises at a thread blocked on a host mutex in 20 rounds and expects each delivery once the wait returns; it fails without this change. AI-assisted: yes Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Found and fixed a real bug in the redirect path while testing PPSA12544 past its intro (3cd2ff9). A thread suspended inside a Windows system call (here Unity's The redirected |
…/kernel-raise-exception-windows # Conflicts: # core/libs/prx/libkernel/CMakeLists.txt # core/libs/prx/libkernel/System/src/Exception.cpp
|
Merged |
adriadam10
left a comment
There was a problem hiding this comment.
Thanks, the redirect path and the asm stub look right to me (field offsets of Delivery/CONTEXT match the static_asserts, entry stack is 8 mod 16). A few things before this can go in:
Exception.cpp:198:new Deliveryruns while the target is suspended. If the target holds the allocator/CRT lock when suspended, the raising thread deadlocks. Allocate beforeSuspendThread(and don't allocate in any path between suspend and resume, including thethrowsites, which should resume first, as the others do).Exception.cpp:197,Thread.cpp:177/411:waitStatepoints into the target'sthread_local. Raising on a thread that has already exited but isn't joined (or an adopted thread that exited) reads freed TLS. Please clear the pointer at thread exit (or keep the state inPthreadPrivate) and say what happens when the target already finished (ESRCH?).Exception.cpp:208: onlyCONTEXT_FULL | CONTEXT_FLOATING_POINTis saved, so the upper halves of YMM are not preserved if the handler or the entry uses AVX. Either capture the XState or document it as a known limit.- The limits listed in the description (waits outside
TimedWaitandAPS5_COARSE_TIMED_WAITS, signal masks not consulted,ucontextlayout taken from FreeBSD, Linux still throwing) need lines indocs/dev/TechnicalDebt.md(criterion: every unverified choice gets a line, under the right###section and not at the end of it). - The description doesn't mention the third test case (
HostBlocked, 20 rounds with a host mutex); please describe what it pins, since it relies on the handler running only after the thread returns to guest code.
Windows runtime behaviour can't be run on our side; the Windows CI job is green, so these are code-review points.
…wait count The APC delivery was allocated after SuspendThread, so a target holding the allocator lock deadlocked the raising thread; it is now allocated before the suspend. The wait count a raise checks lived in the target's thread_local storage and dangled once the thread exited; it is now a member of PthreadPrivate, which TimedWait counts into through a thread-local pointer bound when the thread starts, and a raise on a thread that already finished returns ESRCH. TechnicalDebt gets one line per unverified choice: Windows only, the FreeBSD ucontext layout, the signal mask, the YMM upper halves, and delivery to threads outside TimedWait. AI-assisted: yes Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks, all addressed in 51b6079:
The exception tests pass 3 runs in a row, and the non-GPU tests pass (232). |
…/kernel-raise-exception-windows # Conflicts: # docs/dev/TechnicalDebt.md
|
Data point: PPSA03306 (Silt, Unity) needs this too. On |
adriadam10
left a comment
There was a problem hiding this comment.
Thanks, everything from the last round is addressed (allocation before suspend, waitCount in PthreadPrivate, ESRCH on a finished thread, the TechnicalDebt lines). Build and ctest are green on our side against current main, and the Windows job runs guest_raise_exception 3/3. Two new things in the wait path, plus a minor one:
TimedWait.cpp:26-31(AlertableWait, same inAlertableSleep): there is a window afterWaitForMultipleObjectsExreturns and beforefetch_sub. IfRaiseOnsuspends the target there, it seeswaitCount > 0and queues an APC (Exception.cpp:199). The thread then leaves the wait and returns to guest code, so the APC only runs at its next alertableTimedWaitwait, which may never come. A stop-the-world collector waiting for that handler would hang. One way to close it: after the decrement, drain pending APCs before returning (SleepEx(0, TRUE), orNtTestAlertif yielding is a concern). A raise that suspends the thread after the decrement takes the redirect path anyway.TimedWait.cpp:172:Condition::waitSignalUntilcallsEventSet(waiter->event, 0)while holdingqueueLock, andEventSetis now alertable. A pending APC would run the guest handler with the condition'squeueLockheld. If the handler notifies that condition, it deadlocks on the non-recursive mutex. That poll is 0 ms anyway, so a non-alertable wait there avoids the problem.- Minor,
Exception.cpp:276/198: a target that exits between the_finishedcheck andSuspendThread/GetThreadContext/QueueUserAPCmakes the raise throw instead of returningESRCH. CheckingWaitForSingleObject(native, 0) == WAIT_OBJECT_0after the suspend (then resume and returnESRCH) would cover it.
A raise that suspends the thread after its wait returned but before the wait count drops queued an APC that only ran at the next alertable wait. Pending APCs are now run right after the count drops. The 0 ms poll in Condition::waitSignalUntil runs under queueLock and is no longer alertable, and a target that exits during the raise returns ESRCH. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks. All three are addressed in 2654e3a:
Merged |
adriadam10
left a comment
There was a problem hiding this comment.
Thanks, all three points are addressed in 2654e3a (APC drain after the count drops, with the Leaving test; non-alertable 0 ms poll under queueLock; ESRCH when the target exits after the suspend). Build and ctest green against current main; the Windows job covers guest_raise_exception. Merging once CI is green.
What
sceKernelRaiseException(thread, signum)threw. On Windows it now runs the handler recorded bysceKernelInstallExceptionHandler(#724) on the target thread, with a PS5ucontextof the interrupted state (16-byteuc_sigmask, 48 bytes, FreeBSD amd64mcontextat 0x40,mc_rspat 0xf8; checked withstatic_assert), as proposed in #727:TimedWaitwaits (Conditionsignal waits, timer waits, sleeps) are alertable and count themselves in a per-thread wait state (PthreadPrivate::waitState). If the suspended target is in such a wait, the delivery is queued withQueueUserAPC; the handler runs on the target inside its wait, with a context captured there, and the wait continues afterwards (an APC returnsWAIT_IO_COMPLETION, which the waits retry).SuspendThread+GetThreadContext, copy the delivery onto the target's own stack below its 128-byte red zone, with the entry's 32-byte home area below that copy (refused if those pages are not committed), and redirectripto an entry that builds theucontext, calls the handler, writes back the possibly modified registers and returns withRtlRestoreContext.Only SIGUSR1 (30) can be raised, as in shadPS4; other signals return
SCE_KERNEL_ERROR_EINVAL, a null threadSCE_KERNEL_ERROR_ESRCH, and raising without an installed handler throws. Linux still throws (the host signal choice is open in #727). Known limits, also in #727: a thread blocked in a host wait outsideTimedWait(or withAPS5_COARSE_TIMED_WAITS) does not run the handler until it returns to guest code or enters such a wait; signal masks are not consulted.guest_raise_exception, which checks:sceKernelWaitSema. Each checks that the handler ran on that worker, thatmc_rspis set and lies above the handler's frame, and that the worker continues (the blocked one returns from its wait once the semaphore is signaled).HostBlocked, 20 rounds: a worker blocked on a hoststd::mutex, a non-alertable wait in a system call. That wait takes the redirect path, so the handler runs only once the wait returns to guest code. The thread then resumes throughsysret, which overwritesrcx/r11. Each round raises while the worker is blocked, releases the mutex, and expects exactly one delivery on that worker. This pins that the redirect entry takes the delivery fromrspand the live registers (the wait gets its real result inrax), not fromrcxor the stale snapshot.ESRCH.Tested
Windows 11, WinLibs GCC 15.2.0 r7, Ryzen 7 9800X3D, Radeon RX 9070 XT.
ctest: 317/319 passed, includingguest_raise_exception(also 10 consecutive runs, 2000 deliveries).agc_driver_lds_atomics64andagc_driver_vopc_compare_tablefail on this machine onmainas well (Vulkan device tests).level0.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