feat(libkernel): sceKernelRaiseException on macOS hosts #4630

Merged
mugurc merged 2 commits from upstream-pr/4630 into main 2026-11-08 18:07:50 +00:00
mugurc commented 2026-10-12 17:03:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mugurc on 2026-10-12T17:03:23Z.


What

Delivers sceKernelRaiseException on macOS hosts the way Linux does: the host SIGUSR1 interrupts the target thread and the handler runs on it, with the interrupted registers and float state in the guest context; changes the handler makes to the context are applied when it returns.

Darwin hands the signal handler an mcontext_t that points to a general register structure and a float state structure, instead of holding the registers (gregs, fpregs) as glibc does. The registers are copied one by one. The float state of x86-64 Darwin starts with an FXSAVE image; a static_assert checks the offsets of the control word, MXCSR and the first and last XMM register against the FXSAVE layout, so the 512 bytes of the guest context are copied from and to it. Rosetta 2 applies a modified context on return.

The test used constexpr int SIGUSR1 = 30, which is a macro on macOS; the constant is renamed, and the spin function of the test uses APS5_ASM_FUNCTION. The debt entry says Linux and macOS. Part of the macOS series announced on #1 (https://github.com/boykopovar/AnyPS5/issues/1#issuecomment-6124344298).

Tested

  • macOS 26.5.1, Apple M5 Max, Apple clang 21, x86-64 build under Rosetta 2. main 7e2ba73e4 with the rest of the macOS series (ci/macos-series-2 on my fork, which now includes #7937) and the libSceSsl change of #7332. The libraries do not link on macOS without the series. ninja, ninja libs and ctest -j4: 1798 tests, 1123 passed, 25 failed, the rest skipped (no Vulkan loader in the environment); guest_raise_exception passes.
  • Rebased on main 7e2ba73e4. The conflicts were the test's constants, where #8390 lowered Repeats to 50 (kept, with the constant still named GuestSigUsr1), and the debt entry, where main added the Windows entry after the Linux one (both kept, the Linux one saying Linux and macOS).
  • Earlier: guest_raise_exception passed 25 runs in a row on macOS (ctest --repeat until-fail:25); the test checks the registers, XMM8 and MXCSR the handler sees, nested waits and wake-ups.
  • Linux and Windows: not run for this version. The Linux path of Exception.cpp is unchanged (the Darwin code is under #ifdef __APPLE__). An earlier version ran on my fork's CI with the rest of the series: 960 tests on Linux and 966 on Windows pass (https://github.com/mugurc/AnyPS5/actions/runs/38256280627).

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 (none added)
  • Generic behaviour, not specific to one title
  • Depends on: none (#4628, whose macros the test uses, is merged)
  • Unblocks: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4832577205 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4630). Originally posted by [mugurc](https://github.com/mugurc) on 2026-10-12T17:03:23Z. --- ### What Delivers `sceKernelRaiseException` on macOS hosts the way Linux does: the host SIGUSR1 interrupts the target thread and the handler runs on it, with the interrupted registers and float state in the guest context; changes the handler makes to the context are applied when it returns. Darwin hands the signal handler an `mcontext_t` that points to a general register structure and a float state structure, instead of holding the registers (`gregs`, `fpregs`) as glibc does. The registers are copied one by one. The float state of x86-64 Darwin starts with an FXSAVE image; a `static_assert` checks the offsets of the control word, MXCSR and the first and last XMM register against the FXSAVE layout, so the 512 bytes of the guest context are copied from and to it. Rosetta 2 applies a modified context on return. The test used `constexpr int SIGUSR1 = 30`, which is a macro on macOS; the constant is renamed, and the spin function of the test uses `APS5_ASM_FUNCTION`. The debt entry says Linux and macOS. Part of the macOS series announced on #1 (https://github.com/boykopovar/AnyPS5/issues/1#issuecomment-6124344298). ### Tested - macOS 26.5.1, Apple M5 Max, Apple clang 21, x86-64 build under Rosetta 2. `main` 7e2ba73e4 with the rest of the macOS series (`ci/macos-series-2` on my fork, which now includes #7937) and the libSceSsl change of #7332. The libraries do not link on macOS without the series. `ninja`, `ninja libs` and `ctest -j4`: 1798 tests, 1123 passed, 25 failed, the rest skipped (no Vulkan loader in the environment); `guest_raise_exception` passes. - Rebased on `main` 7e2ba73e4. The conflicts were the test's constants, where #8390 lowered `Repeats` to 50 (kept, with the constant still named `GuestSigUsr1`), and the debt entry, where `main` added the Windows entry after the Linux one (both kept, the Linux one saying Linux and macOS). - Earlier: `guest_raise_exception` passed 25 runs in a row on macOS (`ctest --repeat until-fail:25`); the test checks the registers, XMM8 and MXCSR the handler sees, nested waits and wake-ups. - Linux and Windows: not run for this version. The Linux path of `Exception.cpp` is unchanged (the Darwin code is under `#ifdef __APPLE__`). An earlier version ran on my fork's CI with the rest of the series: 960 tests on Linux and 966 on Windows pass (https://github.com/mugurc/AnyPS5/actions/runs/38256280627). ### 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](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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 (none added) - [x] Generic behaviour, not specific to one title - [x] Depends on: none (#4628, whose macros the test uses, is merged) - [x] Unblocks: none - [x] AI-assisted: yes
jotitadev commented 2026-10-14 21:07:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-14T21:07:29Z.

Review: COMMENTED


Checked on Apple M2 Max, macOS 27.2, Xcode 27.0 (Apple clang 21.0.0), x86-64 full build under Rosetta. Head cc064ec78 on main 41cc655b8, together with #4627, #4629 and #5735.

  • The merge conflicts with main in core/libs/tests/GuestRaiseException.cpp only, against the Windows address-lock block from #5756. Taking main's block and renaming SIGUSR1 to GuestSigUsr1 resolves it. That includes the two new uses inside #ifdef _WIN32, at lines 417 and 469 of the merged file. Exception.cpp merges cleanly with the Windows change.
  • With that resolution, System/src/Exception.cpp and the test compile on macOS. On main they fail with the ucontext.h _XOPEN_SOURCE error, gregs/REG_* and the SIGUSR1 macro.
  • Not run: guest_raise_exception. libc and libkernel do not link on current main yet (GuestAllocations.cpp link.h, and the libkernel host APIs), so the test cannot link. Details are in #1.

Written with AI assistance (Claude Code). I ran the builds above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5524671685 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4630#pullrequestreview-5524671685). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-14T21:07:29Z. **Review:** COMMENTED --- Checked on Apple M2 Max, macOS 27.2, Xcode 27.0 (Apple clang 21.0.0), x86-64 full build under Rosetta. Head cc064ec78 on `main` 41cc655b8, together with #4627, #4629 and #5735. - The merge conflicts with `main` in `core/libs/tests/GuestRaiseException.cpp` only, against the Windows address-lock block from #5756. Taking `main`'s block and renaming `SIGUSR1` to `GuestSigUsr1` resolves it. That includes the two new uses inside `#ifdef _WIN32`, at lines 417 and 469 of the merged file. `Exception.cpp` merges cleanly with the Windows change. - With that resolution, `System/src/Exception.cpp` and the test compile on macOS. On `main` they fail with the `ucontext.h` `_XOPEN_SOURCE` error, `gregs`/`REG_*` and the `SIGUSR1` macro. - Not run: `guest_raise_exception`. `libc` and `libkernel` do not link on current `main` yet (`GuestAllocations.cpp` `link.h`, and the libkernel host APIs), so the test cannot link. Details are in #1. Written with AI assistance (Claude Code). I ran the builds above on my machine.
jotitadev commented 2026-10-18 15:45:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-18T15:45:24Z.


Follow-up to my earlier review, where guest_raise_exception could not link yet. Head f17e3d3e4.

Machine: Apple M2 Max, macOS 27.2, Apple clang 21, full x86-64 build under Rosetta 2. Base: main e5f18ea99 with your ci/macos-series-2 (fa6d8ae0b) and the open macOS fixes it uses (#6928, #6667, #6668, #6969, #6853, #6836, #6620, #7524). Everything merges without conflicts.

  • guest_raise_exception passes 25 runs in a row (ctest --repeat until-fail:25), so it holds on a second chip and on macOS 27.2.
  • The 13 commits main gained after a62c724c3 do not break anything in the series: guest_app_info, guest_thread_self, guest_kernel_fcntl, guest_file_vector, guest_file_timestamps, param_sfo_parser, the download_data_mount_* tests and sce_module_code_extent pass.

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6251856542 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4630#issuecomment-6251856542). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-18T15:45:24Z. --- Follow-up to my earlier review, where `guest_raise_exception` could not link yet. Head `f17e3d3e4`. Machine: Apple M2 Max, macOS 27.2, Apple clang 21, full x86-64 build under Rosetta 2. Base: `main` e5f18ea99 with your `ci/macos-series-2` (fa6d8ae0b) and the open macOS fixes it uses (#6928, #6667, #6668, #6969, #6853, #6836, #6620, #7524). Everything merges without conflicts. - `guest_raise_exception` passes 25 runs in a row (`ctest --repeat until-fail:25`), so it holds on a second chip and on macOS 27.2. - The 13 commits `main` gained after a62c724c3 do not break anything in the series: `guest_app_info`, `guest_thread_self`, `guest_kernel_fcntl`, `guest_file_vector`, `guest_file_timestamps`, `param_sfo_parser`, the `download_data_mount_*` tests and `sce_module_code_extent` pass. Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
imellag commented 2026-10-24 16:16:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by imellag on 2026-10-24T16:16:15Z.

Review: COMMENTED


Tested head 50dafe9523 on Apple M4 Pro, macOS 27.0.1, Apple clang, x86-64 Release under Rosetta 2.

Alone on main 3fcb5e4d76: System/src/Exception.cpp now compiles. GuestRaiseException.cpp conflicts with main. #9074 removed the #ifdef _WIN32 around the holding-handler block and added HandlerRunsInUnboundedAddressWait(), and both still use SIGUSR1, which this PR renames. To resolve it locally I kept main's block and switched its two SIGUSR1 uses, and the one in HandlerRunsInUnboundedAddressWait (line 505), to GuestSigUsr1.

With that and the other open macOS build PRs (the same stack as in my review of #7631), guest_raise_exception passes 20 runs in a row (ctest --repeat until-fail:20).

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638920885 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4630#pullrequestreview-5638920885). Originally posted by [imellag](https://github.com/imellag) on 2026-10-24T16:16:15Z. **Review:** COMMENTED --- Tested head `50dafe9523` on Apple M4 Pro, macOS 27.0.1, Apple clang, x86-64 Release under Rosetta 2. **Alone on `main` `3fcb5e4d76`:** `System/src/Exception.cpp` now compiles. `GuestRaiseException.cpp` conflicts with `main`. #9074 removed the `#ifdef _WIN32` around the holding-handler block and added `HandlerRunsInUnboundedAddressWait()`, and both still use `SIGUSR1`, which this PR renames. To resolve it locally I kept `main`'s block and switched its two `SIGUSR1` uses, and the one in `HandlerRunsInUnboundedAddressWait` (line 505), to `GuestSigUsr1`. **With that and the other open macOS build PRs** (the same stack as in my review of #7631), `guest_raise_exception` passes 20 runs in a row (`ctest --repeat until-fail:20`). AI-assisted: yes
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!4630
No description provided.