feat(libkernel): record exception handlers #724

Merged
merwie merged 2 commits from upstream-pr/724 into main 2026-10-06 21:02:53 +00:00
merwie commented 2026-10-05 18:01:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by merwie on 2026-10-05T18:01:19Z.


What

sceKernelInstallExceptionHandler and sceKernelRemoveExceptionHandler threw not implemented. They now keep one handler per allowed signal, with the checks of shadPS4's implementation:

  • allowed signals: SIGHUP (1), SIGILL (4), SIGFPE (8), SIGBUS (10), SIGSEGV (11), SIGUSR1 (30); any other signal, or a null handler, returns SCE_KERNEL_ERROR_EINVAL;
  • a second handler for a signal returns SCE_KERNEL_ERROR_EAGAIN;
  • removing clears the handler (also when none was installed).

Nothing delivers to the recorded handler yet: a host fault still ends the process with the crash report, and sceKernelRaiseException still throws. This is recorded under Functional in TechnicalDebt.

  • New test guest_exception_handler: rejected signals and null handler, install / duplicate / remove / reinstall for each allowed signal.

Why registration alone is useful, and what delivery needs: PPSA12544's Media/Modules/PS5Util.prx (Unity) installs a SIGUSR1 handler in its GC stop init (gc_stop_init failure 1 error:%x on failure). Its handler checks signum == 30 and calls the stored callback with the context and *(context + 0xf8), i.e. uc_mcontext.mc_rsp of the PS5 ucontext (16-byte uc_sigmask, 48 bytes, then uc_mcontext). To stop a thread for garbage collection it calls sceKernelRaiseException(thread, 30). Delivery therefore has to run the handler on the target thread with the interrupted state and resume it afterwards; on Windows the targets are typically blocked in host waits (seen: AssetGarbageCollectorHelper threads in ntdll waits). That design is a separate change, proposed in #727.

Tested

Windows 11, WinLibs GCC 15.2.0 r7, Ryzen 7 9800X3D, Radeon RX 9070 XT.

  • ctest: 289/291 passed, including guest_exception_handler. agc_driver_lds_atomics64 and agc_driver_vopc_compare_table fail on this machine on main as well (Vulkan device tests).
  • PPSA12544 with #698, #699, #700, #702, #705, #709, #710, #716, #717, #721, #648: the handler is recorded and the title continues to its first sceKernelRaiseException(thread, 30), which throws.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4748942571 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/724). Originally posted by [merwie](https://github.com/merwie) on 2026-10-05T18:01:19Z. --- ### What `sceKernelInstallExceptionHandler` and `sceKernelRemoveExceptionHandler` threw `not implemented`. They now keep one handler per allowed signal, with the checks of shadPS4's implementation: - allowed signals: SIGHUP (1), SIGILL (4), SIGFPE (8), SIGBUS (10), SIGSEGV (11), SIGUSR1 (30); any other signal, or a null handler, returns `SCE_KERNEL_ERROR_EINVAL`; - a second handler for a signal returns `SCE_KERNEL_ERROR_EAGAIN`; - removing clears the handler (also when none was installed). Nothing delivers to the recorded handler yet: a host fault still ends the process with the crash report, and `sceKernelRaiseException` still throws. This is recorded under Functional in TechnicalDebt. - New test `guest_exception_handler`: rejected signals and null handler, install / duplicate / remove / reinstall for each allowed signal. Why registration alone is useful, and what delivery needs: PPSA12544's `Media/Modules/PS5Util.prx` (Unity) installs a SIGUSR1 handler in its GC stop init (`gc_stop_init failure 1 error:%x` on failure). Its handler checks `signum == 30` and calls the stored callback with the context and `*(context + 0xf8)`, i.e. `uc_mcontext.mc_rsp` of the PS5 `ucontext` (16-byte `uc_sigmask`, 48 bytes, then `uc_mcontext`). To stop a thread for garbage collection it calls `sceKernelRaiseException(thread, 30)`. Delivery therefore has to run the handler on the target thread with the interrupted state and resume it afterwards; on Windows the targets are typically blocked in host waits (seen: `AssetGarbageCollectorHelper` threads in `ntdll` waits). That design is a separate change, proposed in #727. ### Tested Windows 11, WinLibs GCC 15.2.0 r7, Ryzen 7 9800X3D, Radeon RX 9070 XT. - `ctest`: 289/291 passed, including `guest_exception_handler`. `agc_driver_lds_atomics64` and `agc_driver_vopc_compare_table` fail on this machine on `main` as well (Vulkan device tests). - PPSA12544 with #698, #699, #700, #702, #705, #709, #710, #716, #717, #721, #648: the handler is recorded and the title continues to its first `sceKernelRaiseException(thread, 30)`, which throws. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in TechnicalDebt - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
oneandonlydean commented 2026-10-06 05:34:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-06T05:34:21Z.


Thanks for this. It needs a one-line rebase: #796 added its Additional content (libSceAppContent) line to docs/dev/TechnicalDebt.md at the same place in the Functional list as your sceKernelInstallExceptionHandler line. Keeping both lines resolves it, and nothing else conflicts. #760 carries this commit, so it will need the same resolution.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6010066002 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/724#issuecomment-6010066002). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-06T05:34:21Z. --- Thanks for this. It needs a one-line rebase: #796 added its `Additional content` (libSceAppContent) line to `docs/dev/TechnicalDebt.md` at the same place in the Functional list as your `sceKernelInstallExceptionHandler` line. Keeping both lines resolves it, and nothing else conflicts. #760 carries this commit, so it will need the same resolution.
github-actions[bot] commented 2026-10-06 07:55:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-06T07:55:09Z.


Progress report (481c85e → deddb10)

📈 System libraries: 82.37% (+0.06%, +2 implemented functions)

✅ 2 implemented
Library Name
libkernel sceKernelInstallExceptionHandler
libkernel sceKernelRemoveExceptionHandler
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6011918094 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/724#issuecomment-6011918094). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-06T07:55:09Z. --- <!-- progress-report --> ### Progress report (481c85e → deddb10) 📈 **System libraries**: 82.37% (+0.06%, +2 implemented functions) <details> <summary>✅ 2 implemented</summary> | Library | Name | | - | - | | libkernel | `sceKernelInstallExceptionHandler` | | libkernel | `sceKernelRemoveExceptionHandler` | </details>
merwie commented 2026-10-06 07:59:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by merwie on 2026-10-06T07:59:49Z.


Merged main (a merge, not a rebase, so the review history stays) and kept both TechnicalDebt.md lines in the order you suggested. Nothing else changed.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6011984931 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/724#issuecomment-6011984931). Originally posted by [merwie](https://github.com/merwie) on 2026-10-06T07:59:49Z. --- Merged `main` (a merge, not a rebase, so the review history stays) and kept both `TechnicalDebt.md` lines in the order you suggested. Nothing else changed.
github-actions[bot] commented 2026-10-06 08:58:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-06T08:58:50Z.


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.

Pull request Conflicting files (lines on main) Same exports Same new files
#835 docs/dev/TechnicalDebt.md L138
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6012892320 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/724#issuecomment-6012892320). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-06T08:58:50Z. --- <!-- pr-overlap --> 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**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #835 | `docs/dev/TechnicalDebt.md` [L138](https://github.com/boykopovar/AnyPS5/blob/5b359092d850a496ae50e4e60b8c674cb4f921c1/docs/dev/TechnicalDebt.md#L138) | | |
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!724
No description provided.