feat(libkernel): implement _umtx_op SEM2_WAIT, SEM2_WAKE and MUTEX_WAKE2 #19450

Merged
SHOnnay merged 5 commits from upstream-pr/19450 into main 2026-11-16 16:07:11 +00:00
SHOnnay commented 2026-11-13 10:49:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by SHOnnay on 2026-11-13T10:49:52Z.


What

_umtx_op operations MUTEX_WAKE2 (22), SEM2_WAIT (23) and SEM2_WAKE (24) no longer throw. They follow FreeBSD stable/11 sys/kern/kern_umtx.c at 5ed7eb0d97ba: do_wake2_umutex (L1287-1377), do_sem2_wait (L3195-3273) and do_sem2_wake (L3276-3313). The _usem2 layout {_count, _flags}, USEM_HAS_WAITERS (0x80000000) and USEM_MAX_COUNT come from sys/sys/_umtx.h and sys/sys/umtx.h.

All three use the file's existing wait queues. The bucket mutex plays the role of FreeBSD's umtxq busy lock.

SEM2_WAIT

  • The timeout is read through UmtxDeadline, with the same size-dependent forms as the other operations.
  • Null gives EFAULT; a misaligned object throws, as SEM_WAIT does.
  • It returns 0 at once if USEM_COUNT is non-zero. Otherwise it sets USEM_HAS_WAITERS with a compare-and-swap from 0, or sees it already set, and sleeps.
  • Woken gives 0, timeout gives ETIMEDOUT, an unreadable count gives EFAULT. The count is never decremented here; userland does that, as on FreeBSD.

SEM2_WAKE

  • Null or an unreadable _flags gives EFAULT, as FreeBSD's fueword32 of _flags.
  • With exactly one waiter queued it clears USEM_HAS_WAITERS, then wakes one waiter.

MUTEX_WAKE2

  • The flags come from val; priority inherit and protect together give EINVAL.
  • With no waiters the mutex is not touched, so a null object returns 0.
  • With more than one waiter it sets UMUTEX_CONTESTED. With one, it sets it only while the mutex is owned. It then wakes one waiter if the mutex has no owner.

Still throwing: the legacy lock, SHM, robust lists and robust umutexes, including MUTEX_WAKE2 with UMUTEX_ROBUST, which FreeBSD treats as a normal mutex here. The debt entry is updated.

Tested

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

Harness. A temporary harness, not committed, built the unchanged SyncOnAddress.cpp and TimedWait.cpp with the extended GuestUmtxOp.cpp and minimal stand-ins, for x86-64 under Rosetta (arm64 cannot build the file's x86 inline assembly).

Run Result
Branch exit 0
Branch, ASan + UBSan exit 0
Branch, TSan no reports in the new tests
Base code with the new test not implemented: _umtx_op operation 23, exit 134

TSan also reports a race in the existing ConditionWaitReleasesTheMutexAndWakes (a non-atomic read of hasWaiters), on base as on this branch.

New test cases, all with real threads:

  • the count fast path, also with the waiters bit set
  • the waiters bit set on timeout: zero, relative timespec, relative _umtx_time, absolute and past UMTX_ABSTIME, and EINVAL
  • two waiters woken one at a time with count 0: the bit survives the first wake and clears on the last
  • EFAULT for null and for address 0x10
  • MUTEX_WAKE2 with no waiters, EINVAL, the robust flag throwing, and contested repair with a single wake, with two LOCK waiters and one MUTEX_WAIT waiter

The old assertion that SEM2_WAIT throws now uses SHM.

Mutation check: all 7 hand mutations failed the harness:

  • wait never sets the bit
  • wake clears the bit with more than one waiter
  • MUTEX_WAKE2 wakes while owned
  • MUTEX_WAKE2 ignores the waiter count
  • the fast path tests the raw count
  • wake skips the fault probe
  • MUTEX_WAKE2 sets contested with one waiter on an unowned mutex
$ python3 tools/check_conventions.py --base upstream/main
All conventions checks passed.

Overlap. No open PR implements these operations.

  • #17887 and #16873 edit SyncOnAddress.cpp; each merges with this branch without conflict, and each merged tree passes the harness.
  • #17887 also edits the _umtx_op debt line, so the second to merge needs that line merged by hand.

Not verified:

  • the console
  • a registered Linux or Windows run
  • USYNC_PROCESS_SHARED, which is ignored, as for the existing operations

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:5294169669 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/19450). Originally posted by [SHOnnay](https://github.com/SHOnnay) on 2026-11-13T10:49:52Z. --- ### What `_umtx_op` operations `MUTEX_WAKE2` (22), `SEM2_WAIT` (23) and `SEM2_WAKE` (24) no longer throw. They follow FreeBSD stable/11 [`sys/kern/kern_umtx.c`](https://github.com/freebsd/freebsd-src/blob/5ed7eb0d97ba4436218e810f61bd059acba984c2/sys/kern/kern_umtx.c) at `5ed7eb0d97ba`: `do_wake2_umutex` (L1287-1377), `do_sem2_wait` (L3195-3273) and `do_sem2_wake` (L3276-3313). The `_usem2` layout `{_count, _flags}`, `USEM_HAS_WAITERS` (0x80000000) and `USEM_MAX_COUNT` come from `sys/sys/_umtx.h` and `sys/sys/umtx.h`. All three use the file's existing wait queues. The bucket mutex plays the role of FreeBSD's umtxq busy lock. **`SEM2_WAIT`** - The timeout is read through `UmtxDeadline`, with the same size-dependent forms as the other operations. - Null gives `EFAULT`; a misaligned object throws, as `SEM_WAIT` does. - It returns 0 at once if `USEM_COUNT` is non-zero. Otherwise it sets `USEM_HAS_WAITERS` with a compare-and-swap from 0, or sees it already set, and sleeps. - Woken gives 0, timeout gives `ETIMEDOUT`, an unreadable count gives `EFAULT`. The count is never decremented here; userland does that, as on FreeBSD. **`SEM2_WAKE`** - Null or an unreadable `_flags` gives `EFAULT`, as FreeBSD's `fueword32` of `_flags`. - With exactly one waiter queued it clears `USEM_HAS_WAITERS`, then wakes one waiter. **`MUTEX_WAKE2`** - The flags come from `val`; priority inherit and protect together give `EINVAL`. - With no waiters the mutex is not touched, so a null object returns 0. - With more than one waiter it sets `UMUTEX_CONTESTED`. With one, it sets it only while the mutex is owned. It then wakes one waiter if the mutex has no owner. **Still throwing:** the legacy lock, `SHM`, robust lists and robust umutexes, including `MUTEX_WAKE2` with `UMUTEX_ROBUST`, which FreeBSD treats as a normal mutex here. The debt entry is updated. ### Tested macOS 27 on Apple M1. libkernel does not build on macOS, so the registered `guest_umtx_op` test was **not run**; a Linux or Windows run is needed. **Harness.** A temporary harness, not committed, built the unchanged `SyncOnAddress.cpp` and `TimedWait.cpp` with the extended `GuestUmtxOp.cpp` and minimal stand-ins, for x86-64 under Rosetta (arm64 cannot build the file's x86 inline assembly). | Run | Result | |---|---| | Branch | exit 0 | | Branch, ASan + UBSan | exit 0 | | Branch, TSan | no reports in the new tests | | Base code with the new test | `not implemented: _umtx_op operation 23`, exit 134 | TSan also reports a race in the existing `ConditionWaitReleasesTheMutexAndWakes` (a non-atomic read of `hasWaiters`), on base as on this branch. **New test cases**, all with real threads: - the count fast path, also with the waiters bit set - the waiters bit set on timeout: zero, relative `timespec`, relative `_umtx_time`, absolute and past `UMTX_ABSTIME`, and `EINVAL` - two waiters woken one at a time with count 0: the bit survives the first wake and clears on the last - `EFAULT` for null and for address 0x10 - `MUTEX_WAKE2` with no waiters, `EINVAL`, the robust flag throwing, and contested repair with a single wake, with two `LOCK` waiters and one `MUTEX_WAIT` waiter The old assertion that `SEM2_WAIT` throws now uses `SHM`. **Mutation check:** all 7 hand mutations failed the harness: - wait never sets the bit - wake clears the bit with more than one waiter - `MUTEX_WAKE2` wakes while owned - `MUTEX_WAKE2` ignores the waiter count - the fast path tests the raw count - wake skips the fault probe - `MUTEX_WAKE2` sets contested with one waiter on an unowned mutex ```text $ python3 tools/check_conventions.py --base upstream/main All conventions checks passed. ``` **Overlap.** No open PR implements these operations. - #17887 and #16873 edit `SyncOnAddress.cpp`; each merges with this branch without conflict, and each merged tree passes the harness. - #17887 also edits the `_umtx_op` debt line, so the second to merge needs that line merged by hand. **Not verified:** - the console - a registered Linux or Windows run - `USYNC_PROCESS_SHARED`, which is ignored, as for the existing operations ### 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-13 18:34:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-13T18:34:02Z.

Review: CHANGES_REQUESTED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 7429e6b166:

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_umtx_op_tests built cleanly.
  • CTest (guest_umtx_op):
    1/1 Test #4717: guest_umtx_op ....................   Passed    1.96 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libkernel/SyncOnAddress/src/SyncOnAddress.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #4717: guest_umtx_op ....................Subprocess aborted***Exception:   1.43 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified FreeBSD 11 kern_umtx.c do_sem2_wait, do_sem2_wake and do_wake2_umutex semantics against Sem2Wait, Sem2Wake and MutexWake2
    • Audited error handling and return codes (GUEST_EFAULT, GUEST_EINVAL, GUEST_ETIMEDOUT, APS5_INVALID_ARG_EX) across all three operations
    • Checked memory safety, alignment checks and pointer validation order in SyncOnAddress.cpp
    • Verified waiter queue handling, bucket mutex synchronization, and contested bit manipulation in MutexWake2 and Sem2Wake
    • Checked unit test coverage in GuestUmtxOp.cpp for fast path, timeouts, multi-thread waiter wakes, and invalid operations
    • Audited AGENTS.md compliance regarding comments in code, debt documentation, and third-party code rules
  • Concerns:
    • core/libs/prx/libkernel/SyncOnAddress/src/SyncOnAddress.cpp:1-31: The added 31-line FreeBSD BSD-3-Clause copyright and derivation comment block violates AGENTS.md ('No comments in code, except #endif and namespace ends'). Existing libkernel files including SyncOnAddress.cpp contain no top-of-file comment headers.
  • Minor: please drop the Generated with footer from the description; the AI-assisted checkbox can stay.

Requesting changes for the points above.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5896159735 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/19450#pullrequestreview-5896159735). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-13T18:34:02Z. **Review:** CHANGES_REQUESTED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `7429e6b166`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `guest_umtx_op_tests` built cleanly. - **CTest** (`guest_umtx_op`): ``` 1/1 Test #4717: guest_umtx_op .................... Passed 1.96 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libkernel/SyncOnAddress/src/SyncOnAddress.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #4717: guest_umtx_op ....................Subprocess aborted***Exception: 1.43 sec 0% tests passed, 1 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - Verified FreeBSD 11 kern_umtx.c do_sem2_wait, do_sem2_wake and do_wake2_umutex semantics against Sem2Wait, Sem2Wake and MutexWake2 - Audited error handling and return codes (GUEST_EFAULT, GUEST_EINVAL, GUEST_ETIMEDOUT, APS5_INVALID_ARG_EX) across all three operations - Checked memory safety, alignment checks and pointer validation order in SyncOnAddress.cpp - Verified waiter queue handling, bucket mutex synchronization, and contested bit manipulation in MutexWake2 and Sem2Wake - Checked unit test coverage in GuestUmtxOp.cpp for fast path, timeouts, multi-thread waiter wakes, and invalid operations - Audited AGENTS.md compliance regarding comments in code, debt documentation, and third-party code rules - **Concerns**: - core/libs/prx/libkernel/SyncOnAddress/src/SyncOnAddress.cpp:1-31: The added 31-line FreeBSD BSD-3-Clause copyright and derivation comment block violates AGENTS.md ('No comments in code, except #endif and namespace ends'). Existing libkernel files including SyncOnAddress.cpp contain no top-of-file comment headers. - Minor: please drop the `Generated with` footer from the description; the AI-assisted checkbox can stay. Requesting changes for the points above.
PrivacyOnion commented 2026-11-15 05:14:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-15T05:14:24Z.

Review: APPROVED


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

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_umtx_op_tests built cleanly.
  • CTest (guest_umtx_op):
    1/1 Test #4989: guest_umtx_op ....................   Passed    1.97 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libkernel/SyncOnAddress/src/SyncOnAddress.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #4989: guest_umtx_op ....................Subprocess aborted***Exception:   1.21 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified core/libs/prx/libkernel/SyncOnAddress/src/SyncOnAddress.cpp Sem2Wait, Sem2Wake, and MutexWake2 implementations against FreeBSD kern_umtx.c and AnyPS5 sync-on-address architecture
    • Verified pointer validation, alignment checks, and error return mappings (GUEST_EFAULT, GUEST_EINVAL, GUEST_ETIMEDOUT, APS5_INVALID_ARG_EX)
    • Checked atomic memory operations and concurrency invariants under WaiterBucket mutex locks for race conditions or TOCTOU issues
    • Confirmed compliance with AGENTS.md rules including comment restrictions, TechnicalDebt fragment formatting, and Conventional Commits
    • Audited core/libs/tests/GuestUmtxOp.cpp unit tests for comprehensive coverage of fast paths, waiter bit manipulation, timeouts, and contested bit repairs
  • Minor: please drop the Generated with footer from the description; the AI-assisted checkbox can stay.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5909232377 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/19450#pullrequestreview-5909232377). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-15T05:14:24Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `b3ccff0da7`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `guest_umtx_op_tests` built cleanly. - **CTest** (`guest_umtx_op`): ``` 1/1 Test #4989: guest_umtx_op .................... Passed 1.97 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libkernel/SyncOnAddress/src/SyncOnAddress.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #4989: guest_umtx_op ....................Subprocess aborted***Exception: 1.21 sec 0% tests passed, 1 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - Verified core/libs/prx/libkernel/SyncOnAddress/src/SyncOnAddress.cpp Sem2Wait, Sem2Wake, and MutexWake2 implementations against FreeBSD kern_umtx.c and AnyPS5 sync-on-address architecture - Verified pointer validation, alignment checks, and error return mappings (GUEST_EFAULT, GUEST_EINVAL, GUEST_ETIMEDOUT, APS5_INVALID_ARG_EX) - Checked atomic memory operations and concurrency invariants under WaiterBucket mutex locks for race conditions or TOCTOU issues - Confirmed compliance with AGENTS.md rules including comment restrictions, TechnicalDebt fragment formatting, and Conventional Commits - Audited core/libs/tests/GuestUmtxOp.cpp unit tests for comprehensive coverage of fast paths, waiter bit manipulation, timeouts, and contested bit repairs - Minor: please drop the `Generated with` footer from the description; the AI-assisted checkbox can stay. Looks Good To Me
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!19450
No description provided.