feat(libkernel): implement pthread_mutex_getprioceiling and setprioceiling #9326

Merged
erickerxhalli merged 7 commits from upstream-pr/9326 into main 2026-11-04 19:19:17 +00:00
erickerxhalli commented 2026-10-21 18:31:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by erickerxhalli on 2026-10-21T18:31:22Z.


What

Implements pthread_mutex_getprioceiling, pthread_mutex_setprioceiling and their SCE twins scePthreadMutexGetprioceiling and scePthreadMutexSetprioceiling in libkernel; all four threw NotImplemented_nid_no_patch from libkernel_sys. They are declared once in libkernel/include/Api.hpp with their real signatures, and the four (void) discovery declarations are removed from libkernel_sys/include/Api.hpp.

scePthreadMutexInit now copies the attribute's protocol and ceiling into the mutex, so a mutex made through the SCE calls or through pthread_mutex_init (which calls it) gets them; scePthreadMutexattrSetprotocol stores the protocol since #8373. The getters return EINVAL (0x80020016 from the SCE twin) for a static, adaptive or destroyed handle or a mutex without PTHREAD_PRIO_PROTECT, before the output pointer is used. This, and the SCE path passing the protocol and ceiling on, follow firmware 13.42's libkernel_sys as BunnyFl reported in this PR; the checks match FreeBSD 11.4's _pthread_mutex_getprioceiling. A null mutex, or a null output for a priority-protected mutex, throws: the console dereferences it.

The set does the same checks, then, as FreeBSD's do_set_ceiling does in the kernel, takes the mutex unless the caller holds it, so it waits for another holder, and writes the previous ceiling when old_ceiling is given. On the console that part runs in the kernel (_umtx_op UMTX_OP_SET_CEILING), which the 13.42 dump does not contain, so it is recorded as unverified. FreeBSD rejects a ceiling above RTP_PRIO_MAX (31), a bound that does not fit the console's 256-767 SCHED_RR priorities, so a ceiling outside sched_get_priority_min(SCHED_RR) to sched_get_priority_max(SCHED_RR) throws. The SCE twins' signatures follow Orbital's db_types.json. Priority protection itself is still not applied. The gaps are under Functional in docs/dev/debt/libkernel.md.

The new guest_pthread_mutex_prioceiling test checks EINVAL and 0x80020016, with and without an output pointer, for a static, adaptive, default and destroyed mutex; the ceiling taken from a POSIX attribute; the set with and without the old-ceiling output; that 255 and 768 throw and leave the ceiling; the set while the caller holds the mutex and while another thread does. It also covers a mutex made entirely through the SCE calls (protocol 2 with ceiling 300, and protocols 0 and 1, which give EINVAL), a POSIX attribute passed to scePthreadMutexInit, and an SCE attribute passed to pthread_mutex_init, each read and written through the other API.

Tested

  • Linux (Ubuntu 24.04 container, GCC 13.3, Release, full build of the head commit addd914): ctest ran 2287 tests; 2275 passed, including guest_pthread_mutex_prioceiling, guest_pthread_mutexattr, guest_pthread_mutex_errors, guest_pthread_timed_locks, guest_pthread_destroyed_objects, guest_kernel_sys, patched_libraries and public_headers. The 12 failures come from the container, not this change: guest_net_protocol, guest_net_buffer_validation, guest_net_nodelay_readback, guest_sce_net_keepalive, guest_socket_options, guest_socket_stream_options, guest_socket_address_length and guest_tcp open AF_INET6 sockets and the container has no IPv6; agc_submit_progress, agc_submit_progress_no_timeline, agc_driver_pipelined_metadata_pass and agc_driver_pipelined_metadata_pass_draw_pipeline stop at vkCreateInstance (VK_ERROR_INCOMPATIBLE_DRIVER), as it has no Vulkan driver.
  • Mutation check on the head commit: removing the protocol and ceiling copy from scePthreadMutexInit, copying only the protocol, checking the output pointer before the protocol, returning 0 for a null output on a priority-protected mutex, or returning POSIX numbers from the SCE twins each makes guest_pthread_mutex_prioceiling fail. On the first commit, dropping the protocol check, the old-ceiling write, the owner check, the unlock or the range check each did too.
  • Windows (MinGW-w64 GCC 15.2.0 WinLibs, full build of addd914): build.yml with full=true, linux=false on my fork passed (run); the only skips were tests that need Vulkan or a display.

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

🤖 Generated with Claude Code

https://claude.ai/code/session_01WYjMwvLdPR8iX8EP8tQYWa

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4956463809 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9326). Originally posted by [erickerxhalli](https://github.com/erickerxhalli) on 2026-10-21T18:31:22Z. --- ### What Implements `pthread_mutex_getprioceiling`, `pthread_mutex_setprioceiling` and their SCE twins `scePthreadMutexGetprioceiling` and `scePthreadMutexSetprioceiling` in libkernel; all four threw `NotImplemented_nid_no_patch` from libkernel_sys. They are declared once in `libkernel/include/Api.hpp` with their real signatures, and the four `(void)` discovery declarations are removed from `libkernel_sys/include/Api.hpp`. `scePthreadMutexInit` now copies the attribute's protocol and ceiling into the mutex, so a mutex made through the SCE calls or through `pthread_mutex_init` (which calls it) gets them; `scePthreadMutexattrSetprotocol` stores the protocol since #8373. The getters return `EINVAL` (`0x80020016` from the SCE twin) for a static, adaptive or destroyed handle or a mutex without `PTHREAD_PRIO_PROTECT`, before the output pointer is used. This, and the SCE path passing the protocol and ceiling on, follow firmware 13.42's libkernel_sys as BunnyFl reported in this PR; the checks match FreeBSD 11.4's [`_pthread_mutex_getprioceiling`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/lib/libthr/thread/thr_mutex.c#L1001-L1043). A null mutex, or a null output for a priority-protected mutex, throws: the console dereferences it. The set does the same checks, then, as FreeBSD's [`do_set_ceiling`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/kern_umtx.c#L2330-L2420) does in the kernel, takes the mutex unless the caller holds it, so it waits for another holder, and writes the previous ceiling when `old_ceiling` is given. On the console that part runs in the kernel (`_umtx_op` `UMTX_OP_SET_CEILING`), which the 13.42 dump does not contain, so it is recorded as unverified. FreeBSD rejects a ceiling above `RTP_PRIO_MAX` (31), a bound that does not fit the console's 256-767 `SCHED_RR` priorities, so a ceiling outside `sched_get_priority_min(SCHED_RR)` to `sched_get_priority_max(SCHED_RR)` throws. The SCE twins' signatures follow [Orbital's `db_types.json`](https://github.com/AlexAltea/orbital/blob/cc408367da6a6ebbf5ac1ac559fefeadc73b2831/tools/ida/db_types.json). Priority protection itself is still not applied. The gaps are under Functional in `docs/dev/debt/libkernel.md`. The new `guest_pthread_mutex_prioceiling` test checks `EINVAL` and `0x80020016`, with and without an output pointer, for a static, adaptive, default and destroyed mutex; the ceiling taken from a POSIX attribute; the set with and without the old-ceiling output; that 255 and 768 throw and leave the ceiling; the set while the caller holds the mutex and while another thread does. It also covers a mutex made entirely through the SCE calls (protocol 2 with ceiling 300, and protocols 0 and 1, which give `EINVAL`), a POSIX attribute passed to `scePthreadMutexInit`, and an SCE attribute passed to `pthread_mutex_init`, each read and written through the other API. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> - Linux (Ubuntu 24.04 container, GCC 13.3, Release, full build of the head commit addd914): `ctest` ran 2287 tests; 2275 passed, including `guest_pthread_mutex_prioceiling`, `guest_pthread_mutexattr`, `guest_pthread_mutex_errors`, `guest_pthread_timed_locks`, `guest_pthread_destroyed_objects`, `guest_kernel_sys`, `patched_libraries` and `public_headers`. The 12 failures come from the container, not this change: `guest_net_protocol`, `guest_net_buffer_validation`, `guest_net_nodelay_readback`, `guest_sce_net_keepalive`, `guest_socket_options`, `guest_socket_stream_options`, `guest_socket_address_length` and `guest_tcp` open `AF_INET6` sockets and the container has no IPv6; `agc_submit_progress`, `agc_submit_progress_no_timeline`, `agc_driver_pipelined_metadata_pass` and `agc_driver_pipelined_metadata_pass_draw_pipeline` stop at `vkCreateInstance` (`VK_ERROR_INCOMPATIBLE_DRIVER`), as it has no Vulkan driver. - Mutation check on the head commit: removing the protocol and ceiling copy from `scePthreadMutexInit`, copying only the protocol, checking the output pointer before the protocol, returning 0 for a null output on a priority-protected mutex, or returning POSIX numbers from the SCE twins each makes `guest_pthread_mutex_prioceiling` fail. On the first commit, dropping the protocol check, the old-ceiling write, the owner check, the unlock or the range check each did too. - Windows (MinGW-w64 GCC 15.2.0 WinLibs, full build of addd914): `build.yml` with `full=true`, `linux=false` on my fork passed ([run](https://github.com/erickerxhalli/AnyPS5-fork/actions/runs/40239088313)); the only skips were tests that need Vulkan or a display. ### 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: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01WYjMwvLdPR8iX8EP8tQYWa
harisibrar1 commented 2026-10-22 05:47:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-22T05:47:48Z.

Review: COMMENTED


Checked against main 4a17c7175 and FreeBSD libthr thr_mutex.c (_pthread_mutex_getprioceiling/_setprioceiling) and kern_umtx.c do_set_ceiling:

  • The POSIX checks match FreeBSD as far as I can see. Handles <= 2 and non-PRIO_PROTECT mutexes return EINVAL, the set takes the lock unless the caller already owns it, and old_ceiling is written only when non-null. Throwing outside 256..767 instead of applying RTP_PRIO_MAX seems reasonable, and it is recorded in the debt file.
  • The SCE twins can't succeed for a mutex made through the SCE API. scePthreadMutexattrSetprotocol (src/Mutex.cpp:149) validates the protocol and discards it, and scePthreadMutexInit (src/Mutex.cpp:159-166) copies only the type. The protocol and ceiling are copied only in pthread_mutex_init (Posix/Mutex.cpp:68-71). So scePthreadMutexattrSetprotocol(&a, 2) + scePthreadMutexInit(&m, &a, "m") + scePthreadMutexGetprioceiling(&m, &c) returns 0x80020016 for a mutex the title asked to be PRIO_PROTECT. The same happens with pthread_mutexattr_setprotocol + scePthreadMutexInit.
  • Suggested fix: store the protocol in scePthreadMutexattrSetprotocol, resetting the ceiling as pthread_mutexattr_setprotocol does, and move the protocol/ceiling copy into scePthreadMutexInit next to SetType. Then both init paths agree, and the "scePthreadMutexInit copies none" debt sentence can go. A test that creates the mutex through the SCE calls would cover it.
  • The PR text says the mutex takes its attribute's protocol and ceiling "in scePthreadMutexInit". The code does it only in pthread_mutex_init.

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5609775520 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9326#pullrequestreview-5609775520). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-22T05:47:48Z. **Review:** COMMENTED --- Checked against main 4a17c7175 and FreeBSD libthr thr_mutex.c (`_pthread_mutex_getprioceiling`/`_setprioceiling`) and kern_umtx.c `do_set_ceiling`: - The POSIX checks match FreeBSD as far as I can see. Handles <= 2 and non-PRIO_PROTECT mutexes return EINVAL, the set takes the lock unless the caller already owns it, and `old_ceiling` is written only when non-null. Throwing outside 256..767 instead of applying RTP_PRIO_MAX seems reasonable, and it is recorded in the debt file. - The SCE twins can't succeed for a mutex made through the SCE API. `scePthreadMutexattrSetprotocol` (src/Mutex.cpp:149) validates the protocol and discards it, and `scePthreadMutexInit` (src/Mutex.cpp:159-166) copies only the type. The protocol and ceiling are copied only in `pthread_mutex_init` (Posix/Mutex.cpp:68-71). So `scePthreadMutexattrSetprotocol(&a, 2)` + `scePthreadMutexInit(&m, &a, "m")` + `scePthreadMutexGetprioceiling(&m, &c)` returns `0x80020016` for a mutex the title asked to be PRIO_PROTECT. The same happens with `pthread_mutexattr_setprotocol` + `scePthreadMutexInit`. - Suggested fix: store the protocol in `scePthreadMutexattrSetprotocol`, resetting the ceiling as `pthread_mutexattr_setprotocol` does, and move the protocol/ceiling copy into `scePthreadMutexInit` next to `SetType`. Then both init paths agree, and the "scePthreadMutexInit copies none" debt sentence can go. A test that creates the mutex through the SCE calls would cover it. - The PR text says the mutex takes its attribute's protocol and ceiling "in `scePthreadMutexInit`". The code does it only in `pthread_mutex_init`. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
CnRJay commented 2026-11-01 20:01:21 +00:00 (Migrated from github.com)

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

Review: COMMENTED


Correction (2026-11-01): this review's claim that maintainers close PRs without a title ID is wrong. CONTRIBUTING allows Unblocks: none; a title is only required when the PR fixes a title. The technical points below are unaffected.

No title evidence. The template says Unblocks: none and the body names no title ID and no log line showing a title calling pthread_mutex_getprioceiling or setprioceiling (or the SCE twins) on main. Please add the title ID and the log line (for example the NotImplemented hit these four exports produce today in libkernel_sys/Export.Discovery.cpp), otherwise this is the export-only shape that gets closed.

One question tied to the code: pthread_mutex_setprioceiling_nid_postfix throws for a ceiling outside the SCHED_RR range, while the getter and the other invalid-handle cases return EINVAL. If the console returns an error code for an out-of-range ceiling rather than faulting, the throw turns a valid-input case into a host exception. What does the firmware 13.42 disassembly show for that check?

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5729961756 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9326#pullrequestreview-5729961756). Originally posted by [CnRJay](https://github.com/CnRJay) on 2026-11-01T20:01:21Z. **Review:** COMMENTED --- **Correction (2026-11-01):** this review's claim that maintainers close PRs without a title ID is wrong. CONTRIBUTING allows `Unblocks: none`; a title is only required when the PR fixes a title. The technical points below are unaffected. **No title evidence.** The template says `Unblocks: none` and the body names no title ID and no log line showing a title calling `pthread_mutex_getprioceiling` or `setprioceiling` (or the SCE twins) on main. Please add the title ID and the log line (for example the `NotImplemented` hit these four exports produce today in `libkernel_sys/Export.Discovery.cpp`), otherwise this is the export-only shape that gets closed. One question tied to the code: `pthread_mutex_setprioceiling_nid_postfix` throws for a ceiling outside the SCHED_RR range, while the getter and the other invalid-handle cases return `EINVAL`. If the console returns an error code for an out-of-range ceiling rather than faulting, the throw turns a valid-input case into a host exception. What does the firmware 13.42 disassembly show for that check?
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!9326
No description provided.