feat(libkernel): deliver SIGEV_SIGNAL and SIGEV_THREAD_ID ktimer expiries #19463

Merged
SHOnnay merged 6 commits from upstream-pr/19463 into main 2026-11-21 20:06:14 +00:00
SHOnnay commented 2026-11-13 11:07:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by SHOnnay on 2026-11-13T11:07:47Z.


What

ktimer_create with SIGEV_SIGNAL, with SIGEV_THREAD_ID or with a null sigevent no longer throws, and its expiries are delivered through libkernel's existing guest signal queue. This follows FreeBSD stable/11 at 5ed7eb0d97ba:

  • kern_time.c: kern_ktimer_create L1182-1282, kern_ktimer_delete L1323-1352, itimer_accept L1577-1592, realtimer_expire L1606-1652, itimer_fire L1654-1682
  • kern_sig.c: sigev_findtd, tdsendsignal, sigqueue_take, and the itimer_accept calls in kern_sigtimedwait and postsig

Ktimer.cpp

  • Creation. The sigevent is copied in, and a bad pointer gives EFAULT. A null event means SIGEV_SIGNAL with SIGALRM and sival equal to the timer id, as FreeBSD defaults it.
  • Expiry. One host timer thread runs realtimer_expire: it fires due timers and counts missed intervals as overruns.
  • SIGEV_NONE keeps its lazy expiry. A signal timer's ktimer_gettime reports {0, 1} until the thread fires, as realtimer_gettime does.
  • ktimer_getoverrun returns it_overrun_last.
  • Unknown thread. A SIGEV_THREAD_ID timer whose thread does not exist is disarmed at expiry, as itimer_fire does when sigev_findtd returns ESRCH. FreeBSD does not check the id at create, so neither does this.

Signals.cpp: the existing pending queue is reused rather than duplicated.

  • Queue entries now carry the code, timer, value, overrun and errno.
  • SendTimerSignal follows itimer_fire and tdsendsignal:
    • a still-queued entry counts an overrun instead of queueing again
    • an ignored signal is dropped
    • a signal the target thread blocks is queued with SI_TIMER
  • Taking a pending entry applies itimer_accept, so sigtimedwait, sigwaitinfo, sigwait and delivery on unblock all set it_overrun_last. ktimer_delete removes a pending entry, as sigqueue_take does.
  • siginfo gets si_errno, si_value, si_timerid and si_overrun at offsets 4, 32, 40 and 44.
  • Thread masks are recorded in pthread_sigmask and sigsuspend, so an expiry can check the target thread's mask.
  • New internal header System/include/TimerSignals.hpp.

Still throwing, as setitimer already does:

  • An expiry for a thread that does not block the signal throws, because guest handlers never run on the timer thread. SIGALRM with the default disposition exits with status 142.
  • SA_SIGINFO handlers are still not delivered, so the timer fields can be read through sigtimedwait/sigwaitinfo, or a plain handler runs on unblock.

The debt entry is rewritten to state this first, and is shorter: 899 to 835 characters. The pthread_sigmask entry gets one clause.

Tested

macOS 27 on Apple M1. libkernel does not build on macOS, so the registered guest_kernel_sys_ktimer test was not run; a Linux or Windows run is needed.

Harness. A temporary harness, not committed, built the unchanged Signals.cpp and Ktimer.cpp with the extended GuestKernelSysKtimer.cpp. Its stand-ins fake errno, guest copies, the clock, thread ids, pthread create and join, and the guest-throw and terminate hooks.

Run Result
Branch, x86-64 under Rosetta guest_kernel_sys_ktimer passed, 41 of 41 runs
Branch, arm64 ASan + UBSan 5 of 5
Branch, arm64 TSan 3 of 3, no reports
Base code with the new test NotImplemented: ktimer_create delivering a signal, exit 3

GuestSigtimedwait, GuestSignals, GuestItimer and the old ktimer test pass on both base and branch in the same harness. GuestSignals' pointer-fault subtest was skipped, because the stand-ins cannot fake read-only pages.

New test cases

  • EINVAL for signal 0, -1 and 129, and notify -1, 3 and 5. EFAULT for a bad sigevent pointer.
  • Three periodic SIGUSR1 expiries with SI_TIMER, the sival and the timer id, and a handler run on unblock.
  • A null sigevent gives SIGALRM with sival equal to the id.
  • While blocked: one pending entry, an overrun of at least 5, and ktimer_getoverrun agreeing.
  • ktimer_delete removes the pending entry, and nothing arrives afterwards.
  • SIGEV_THREAD_ID delivers SIGUSR2 to a second thread. An unknown thread id disarms the timer.

Mutation check: 7 of 8 hand mutations failed the harness:

  • re-queueing instead of counting an overrun
  • accept not setting it_overrun_last
  • delete keeping the pending entry
  • SIGEV_THREAD_ID using the arming thread's mask
  • the null-event value not set to the id
  • the siginfo value not written
  • an unknown thread not disarming

The survivor applies the lazy expiry to signal timers, which matters only in a timing race.

$ python3 tools/check_conventions.py --base upstream/main
All conventions checks passed.

Overlap. No open PR delivers ktimer signals.

  • #13964 conflicts on the ktimer_create lines, because it rewraps the NotImplemented line this PR removes.
  • The test additions avoid the hunks of #14496 and #14695.
  • #12682, #12688, #12700 and #12965 already conflict with main in many files and now also in Signals.cpp.

Not verified:

  • the console
  • a registered Linux or Windows run
  • asynchronous delivery to unblocked threads, which still throws
  • the INT_MAX overrun saturation path

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 under Silent stubs in their library's technical debt file
  • 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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5294391728 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/19463). Originally posted by [SHOnnay](https://github.com/SHOnnay) on 2026-11-13T11:07:47Z. --- ### What `ktimer_create` with `SIGEV_SIGNAL`, with `SIGEV_THREAD_ID` or with a null `sigevent` no longer throws, and its expiries are delivered through libkernel's existing guest signal queue. This follows FreeBSD stable/11 at [`5ed7eb0d97ba`](https://github.com/freebsd/freebsd-src/blob/5ed7eb0d97ba4436218e810f61bd059acba984c2/sys/kern/kern_time.c): - `kern_time.c`: `kern_ktimer_create` L1182-1282, `kern_ktimer_delete` L1323-1352, `itimer_accept` L1577-1592, `realtimer_expire` L1606-1652, `itimer_fire` L1654-1682 - `kern_sig.c`: `sigev_findtd`, `tdsendsignal`, `sigqueue_take`, and the `itimer_accept` calls in `kern_sigtimedwait` and `postsig` **`Ktimer.cpp`** - **Creation.** The `sigevent` is copied in, and a bad pointer gives `EFAULT`. A null event means `SIGEV_SIGNAL` with `SIGALRM` and `sival` equal to the timer id, as FreeBSD defaults it. - **Expiry.** One host timer thread runs `realtimer_expire`: it fires due timers and counts missed intervals as overruns. - **`SIGEV_NONE`** keeps its lazy expiry. A signal timer's `ktimer_gettime` reports `{0, 1}` until the thread fires, as `realtimer_gettime` does. - **`ktimer_getoverrun`** returns `it_overrun_last`. - **Unknown thread.** A `SIGEV_THREAD_ID` timer whose thread does not exist is disarmed at expiry, as `itimer_fire` does when `sigev_findtd` returns `ESRCH`. FreeBSD does not check the id at create, so neither does this. **`Signals.cpp`**: the existing pending queue is reused rather than duplicated. - Queue entries now carry the code, timer, value, overrun and errno. - `SendTimerSignal` follows `itimer_fire` and `tdsendsignal`: - a still-queued entry counts an overrun instead of queueing again - an ignored signal is dropped - a signal the target thread blocks is queued with `SI_TIMER` - Taking a pending entry applies `itimer_accept`, so `sigtimedwait`, `sigwaitinfo`, `sigwait` and delivery on unblock all set `it_overrun_last`. `ktimer_delete` removes a pending entry, as `sigqueue_take` does. - `siginfo` gets `si_errno`, `si_value`, `si_timerid` and `si_overrun` at offsets 4, 32, 40 and 44. - Thread masks are recorded in `pthread_sigmask` and `sigsuspend`, so an expiry can check the target thread's mask. - New internal header `System/include/TimerSignals.hpp`. **Still throwing**, as `setitimer` already does: - An expiry for a thread that does not block the signal throws, because guest handlers never run on the timer thread. `SIGALRM` with the default disposition exits with status 142. - `SA_SIGINFO` handlers are still not delivered, so the timer fields can be read through `sigtimedwait`/`sigwaitinfo`, or a plain handler runs on unblock. The debt entry is rewritten to state this first, and is shorter: 899 to 835 characters. The `pthread_sigmask` entry gets one clause. ### Tested macOS 27 on Apple M1. libkernel does not build on macOS, so the registered `guest_kernel_sys_ktimer` test was **not run**; a Linux or Windows run is needed. **Harness.** A temporary harness, not committed, built the unchanged `Signals.cpp` and `Ktimer.cpp` with the extended `GuestKernelSysKtimer.cpp`. Its stand-ins fake errno, guest copies, the clock, thread ids, pthread create and join, and the guest-throw and terminate hooks. | Run | Result | |---|---| | Branch, x86-64 under Rosetta | `guest_kernel_sys_ktimer passed`, 41 of 41 runs | | Branch, arm64 ASan + UBSan | 5 of 5 | | Branch, arm64 TSan | 3 of 3, no reports | | Base code with the new test | `NotImplemented: ktimer_create delivering a signal`, exit 3 | `GuestSigtimedwait`, `GuestSignals`, `GuestItimer` and the old ktimer test pass on both base and branch in the same harness. `GuestSignals`' pointer-fault subtest was skipped, because the stand-ins cannot fake read-only pages. **New test cases** - `EINVAL` for signal 0, -1 and 129, and notify -1, 3 and 5. `EFAULT` for a bad `sigevent` pointer. - Three periodic `SIGUSR1` expiries with `SI_TIMER`, the `sival` and the timer id, and a handler run on unblock. - A null `sigevent` gives `SIGALRM` with `sival` equal to the id. - While blocked: one pending entry, an overrun of at least 5, and `ktimer_getoverrun` agreeing. - `ktimer_delete` removes the pending entry, and nothing arrives afterwards. - `SIGEV_THREAD_ID` delivers `SIGUSR2` to a second thread. An unknown thread id disarms the timer. **Mutation check:** 7 of 8 hand mutations failed the harness: - re-queueing instead of counting an overrun - accept not setting `it_overrun_last` - delete keeping the pending entry - `SIGEV_THREAD_ID` using the arming thread's mask - the null-event value not set to the id - the `siginfo` value not written - an unknown thread not disarming The survivor applies the lazy expiry to signal timers, which matters only in a timing race. ```text $ python3 tools/check_conventions.py --base upstream/main All conventions checks passed. ``` **Overlap.** No open PR delivers ktimer signals. - #13964 conflicts on the `ktimer_create` lines, because it rewraps the `NotImplemented` line this PR removes. - The test additions avoid the hunks of #14496 and #14695. - #12682, #12688, #12700 and #12965 already conflict with `main` in many files and now also in `Signals.cpp`. **Not verified:** - the console - a registered Linux or Windows run - asynchronous delivery to unblocked threads, which still throws - the `INT_MAX` overrun saturation path ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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 under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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: <!-- #PR, or none --> none - [x] Fixes: <!-- issue it closes as Fixes #N, or none --> none - [x] Unblocks: <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> none - [x] GPUs tested: <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> n/a - [x] AI-assisted: <!-- yes / no --> yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
PrivacyOnion commented 2026-11-16 01:07:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-16T01:07:32Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 17547dc88c:

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_kernel_sys_ktimer_exit_tests, guest_kernel_sys_ktimer_tests built cleanly.
  • CTest (guest_kernel_sys_ktimer, guest_kernel_sys_ktimer_exit_armed, guest_kernel_sys_ktimer_exit_deleted):
    1/3 Test #928: guest_kernel_sys_ktimer_exit_armed .....   Passed    0.04 sec
    2/3 Test #929: guest_kernel_sys_ktimer_exit_deleted ...   Passed    0.10 sec
    3/3 Test #927: guest_kernel_sys_ktimer ................   Passed    0.65 sec
    100% tests passed, 0 tests failed out of 3
    
  • Negative control: with core/libs/prx/libkernel/System/src/Signals.cpp, core/libs/prx/libkernel/Time/Ktimer.cpp reverted to main and the new tests kept, the suite fails:
    1/3 Test #929: guest_kernel_sys_ktimer_exit_deleted ...Subprocess aborted***Exception:   2.21 sec
    2/3 Test #927: guest_kernel_sys_ktimer ................***Exception: SegFault  2.35 sec
    3/3 Test #928: guest_kernel_sys_ktimer_exit_armed .....Subprocess aborted***Exception:   0.46 sec
    0% tests passed, 3 tests failed out of 3
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified GuestSiginfo layout and static_asserts in core/libs/prx/libkernel/System/src/Signals.cpp against FreeBSD 11 siginfo_t layout (size 80, value at offset 32, timerid at 40, overrun at 44)
    • Verified ktimer_create, ktimer_delete, ktimer_settime, ktimer_gettime, and ktimer_getoverrun in core/libs/prx/libkernel/Time/Ktimer.cpp use leaked heap allocation behind Ktimers() to avoid static destructor deadlock on exit
    • Verified SendTimerSignal, AcceptTimerLocked, ForgetTimerSignal, and TimerSignalOverrun in core/libs/prx/libkernel/System/src/Signals.cpp properly track overrun saturation, ERANGE, and ESRCH thread lookup disarm semantics
    • Verified guest pointer copy-in/copy-out checks and error codes (EFAULT=14, EINVAL=22, EAGAIN=35) in Signals.cpp and Ktimer.cpp
    • Verified Linux and Windows test execution and TSan/ASan verification results for guest_kernel_sys_ktimer, guest_kernel_sys_ktimer_exit_armed, and guest_kernel_sys_ktimer_exit_deleted

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5913954034 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/19463#pullrequestreview-5913954034). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-16T01:07:32Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `17547dc88c`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `guest_kernel_sys_ktimer_exit_tests`, `guest_kernel_sys_ktimer_tests` built cleanly. - **CTest** (`guest_kernel_sys_ktimer`, `guest_kernel_sys_ktimer_exit_armed`, `guest_kernel_sys_ktimer_exit_deleted`): ``` 1/3 Test #928: guest_kernel_sys_ktimer_exit_armed ..... Passed 0.04 sec 2/3 Test #929: guest_kernel_sys_ktimer_exit_deleted ... Passed 0.10 sec 3/3 Test #927: guest_kernel_sys_ktimer ................ Passed 0.65 sec 100% tests passed, 0 tests failed out of 3 ``` - **Negative control**: with `core/libs/prx/libkernel/System/src/Signals.cpp`, `core/libs/prx/libkernel/Time/Ktimer.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/3 Test #929: guest_kernel_sys_ktimer_exit_deleted ...Subprocess aborted***Exception: 2.21 sec 2/3 Test #927: guest_kernel_sys_ktimer ................***Exception: SegFault 2.35 sec 3/3 Test #928: guest_kernel_sys_ktimer_exit_armed .....Subprocess aborted***Exception: 0.46 sec 0% tests passed, 3 tests failed out of 3 ``` Restoring the change makes it pass again. - **Code reading**: - Verified GuestSiginfo layout and static_asserts in core/libs/prx/libkernel/System/src/Signals.cpp against FreeBSD 11 siginfo_t layout (size 80, value at offset 32, timerid at 40, overrun at 44) - Verified ktimer_create, ktimer_delete, ktimer_settime, ktimer_gettime, and ktimer_getoverrun in core/libs/prx/libkernel/Time/Ktimer.cpp use leaked heap allocation behind Ktimers() to avoid static destructor deadlock on exit - Verified SendTimerSignal, AcceptTimerLocked, ForgetTimerSignal, and TimerSignalOverrun in core/libs/prx/libkernel/System/src/Signals.cpp properly track overrun saturation, ERANGE, and ESRCH thread lookup disarm semantics - Verified guest pointer copy-in/copy-out checks and error codes (EFAULT=14, EINVAL=22, EAGAIN=35) in Signals.cpp and Ktimer.cpp - Verified Linux and Windows test execution and TSan/ASan verification results for guest_kernel_sys_ktimer, guest_kernel_sys_ktimer_exit_armed, and guest_kernel_sys_ktimer_exit_deleted Looks Good To Me
MarcZX10 commented 2026-11-21 04:21:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T04:21:01Z.

Review: COMMENTED


Reviewed — comments

Material uncertainty remains around per-thread signal routing: the recipient mask map is populated only when a thread changes its mask, and registrations are never removed or refreshed after thread ID reuse.

What I ran

Reviewed head 17547dc88cdd3e402271117a2c108a2b34843a86. No commands or tests were run in this review; the author reports convention checks and harness runs in the PR description.

Results

Check Result Note
Source review passed All seven changed files read
Build and tests not run Registered ktimer test not run by author on Linux or Windows
Thread routing not verified Lifecycle and registration cases are not covered by the reported harness

Findings

Blocking

None.

Non-blocking

  • core/libs/prx/libkernel/System/src/Signals.cpp:142 — RegisterSignalThread caches each thread’s ThreadSignals* by guest ID once, but there is no unregister path. If a guest thread exits and its ID is reused, the map can retain the old mask pointer; if a live thread changes its guest ID, the cache is not refreshed. SendTimerSignal then may check the wrong mask or treat the target as unregistered and take the unsupported unblocked-delivery path. Please tie registration to the guest thread lifecycle or validate/update the entry before using it.

Not verified

The registered Linux or Windows build and test, console behavior, actual asynchronous handler delivery, INT_MAX overrun saturation, and thread-ID reuse/lifecycle behavior were not verified. The PR description reports macOS harness runs with stand-ins; those are author-reported, not runs performed here. All changed files listed in the manifest were read: core/libs/CMakeLists.txt, core/libs/prx/libkernel/System/include/TimerSignals.hpp, core/libs/prx/libkernel/System/src/Signals.cpp, core/libs/prx/libkernel/Time/Ktimer.cpp, core/libs/tests/GuestKernelSysKtimer.cpp, core/libs/tests/GuestKernelSysKtimerExit.cpp, and docs/dev/debt/changes/feat-libkernel-ktimer-signal.md. No changed files were excluded.

To get this to a good state

Nothing is required. Optional: address thread registration lifecycle and cover thread-ID reuse in a test.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6002788427 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/19463#pullrequestreview-6002788427). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-21T04:21:01Z. **Review:** COMMENTED --- # Reviewed — comments Material uncertainty remains around per-thread signal routing: the recipient mask map is populated only when a thread changes its mask, and registrations are never removed or refreshed after thread ID reuse. ## What I ran Reviewed head `17547dc88cdd3e402271117a2c108a2b34843a86`. No commands or tests were run in this review; the author reports convention checks and harness runs in the PR description. ## Results | Check | Result | Note | |---|---|---| | Source review | passed | All seven changed files read | | Build and tests | not run | Registered ktimer test not run by author on Linux or Windows | | Thread routing | not verified | Lifecycle and registration cases are not covered by the reported harness | ## Findings ### Blocking None. ### Non-blocking - `core/libs/prx/libkernel/System/src/Signals.cpp:142` — `RegisterSignalThread` caches each thread’s `ThreadSignals*` by guest ID once, but there is no unregister path. If a guest thread exits and its ID is reused, the map can retain the old mask pointer; if a live thread changes its guest ID, the cache is not refreshed. `SendTimerSignal` then may check the wrong mask or treat the target as unregistered and take the unsupported unblocked-delivery path. Please tie registration to the guest thread lifecycle or validate/update the entry before using it. ## Not verified The registered Linux or Windows build and test, console behavior, actual asynchronous handler delivery, `INT_MAX` overrun saturation, and thread-ID reuse/lifecycle behavior were not verified. The PR description reports macOS harness runs with stand-ins; those are author-reported, not runs performed here. All changed files listed in the manifest were read: `core/libs/CMakeLists.txt`, `core/libs/prx/libkernel/System/include/TimerSignals.hpp`, `core/libs/prx/libkernel/System/src/Signals.cpp`, `core/libs/prx/libkernel/Time/Ktimer.cpp`, `core/libs/tests/GuestKernelSysKtimer.cpp`, `core/libs/tests/GuestKernelSysKtimerExit.cpp`, and `docs/dev/debt/changes/feat-libkernel-ktimer-signal.md`. No changed files were excluded. ## To get this to a good state Nothing is required. Optional: address thread registration lifecycle and cover thread-ID reuse in a test.
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!19463
No description provided.