fix(libkernel): follow the thread priority ranges and POSIX attribute errors measured on a PS5 #10133

Merged
orezraey merged 16 commits from upstream-pr/10133 into main 2026-10-27 01:58:57 +00:00
orezraey commented 2026-10-23 10:35:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by orezraey on 2026-10-23T10:35:31Z.


What

Thread priority ranges and POSIX attribute errors measured on a retail PS5 (firmware 13.60) with four homebrew probes calling libkernel_sys.sprx from a payload process. The sources and console logs are in this gist (pposix, pprio, pprio2, pprio3).

scePthreadSetprio and pthread_setprio accepted any value before. A sweep of every value from -4096 to 70000 on the console accepted exactly 256–767, 896–959, 65792–66303 and 66432–66495. More values outside the sweep agree with the same rule:

  • the priority is accepted when its low 16 bits are 256–767 or 896–959;
  • 131328, 196864, -65280, 0x7FFF0100 and 0x80000100 are accepted, and 131840 (low bits 768) is refused;
  • an accepted value is stored and read back exactly as given (scePthreadGetprio returns 65792 and -65280);
  • anything else returns EINVAL (0x80020016, or 22 from pthread_setprio); both set errno to 22 and leave the priority alone. A null thread returns EINVAL without touching errno.

A joined or finished thread returns ESRCH for any priority, before the range is checked, and leaves errno alone. pthread_setprio returned ESRCH for a joined thread on the console; the order and scePthreadSetprio setting errno follow firmware 13.42's libkernel, which @BunnyFl ran in an emulator below. Before, the value was stored in the ended thread's object. scePthreadAttrSetschedparam only takes 256–767, which AnyPS5 already did.

pthread_getprio(NULL) returns -1 with EINVAL. Before, it set ESRCH. A joined thread keeps ESRCH.

pthread_attr_getinheritsched with a null output returns EINVAL, as pthread_attr_getscope and pthread_attr_getstackaddr do. Before, it threw.

pthread_mutexattr_setprioceiling takes ceilings from 256 to 767 under PTHREAD_PRIO_PROTECT. Values 0, 255, 768 and 1000 now return EINVAL; before, any value was stored.

Already matching, now recorded as measured:

  • the default scope 2 and inheritance 4;
  • setscope taking 0 and 2 and returning EINVAL for 1, 3 and -1;
  • setinheritsched taking 0 and 4 and returning ENOTSUP (45) otherwise;
  • pthread_attr_setsolosched_np taking 0 and returning ENOTSUP for 1, 2, -1 and 100;
  • EINVAL for null, uninitialized and destroyed attributes;
  • mutex protocols 0 to 2 (3 and -1 give EINVAL), the ceiling reset to 767 when protocol 2 is set, and EINVAL for the ceiling under any other protocol.

A null output of pthread_mutexattr_getprotocol crashed the probe and is left as it is.

Tested

  • Head after merging main (5bdda3661e, with the firmware 13.60 debt relabel from #10179): 8bef063196. The only conflict was libkernel.md: it keeps this PR's measured pthread_mutexattr_* entry and main's 13.60 sceCoredumpSetUserDataType entry. The 9 guest_pthread_* tests named here pass three runs in a row on Windows 11 (WinLibs GCC 15.2) and on Linux (WSL2, GCC); check_conventions passes.
  • Head after merging main again (eeb7760717, with #10130 and #10105; no conflicts): da762ebf4b. guest_pthread_attr, guest_pthread_attr_errors, guest_pthread_mutexattr, guest_pthread_schedparam, guest_pthread_start, guest_pthread_join_detach, guest_pthread_mutex_errors and guest_pthread_destroyed_objects pass three runs in a row on Windows 11 (WinLibs GCC 15.2) and on Linux (WSL2, GCC); check_conventions passes.
  • Head after merging main (99b8446cbc, with the pthread_rwlockattr_settype_np and sceCoredumpSetUserDataType debt updates): 60b9e7f43f. The only conflict was libkernel.md: it keeps this PR's measured pthread_mutexattr_* entry and main's new pthread_rwlockattr_settype_np entry and PPSA02225 sentence. The 9 guest_pthread_* tests named here pass three runs in a row on Windows 11 (WinLibs GCC 15.2) and on Linux (WSL2, GCC); check_conventions passes.
  • Head after merging main again (with #9573, through #10130's refreshed branch): 496d29622c. libkernel.md keeps this PR's measured pthread_getprio, scePthreadSetprio and pthread_setprio entry; scePthreadAttrGet and pthread_attr_get_np keep #9573's createSuspended next to the inheritance. guest_pthread_attr, guest_pthread_attr_errors, guest_pthread_mutexattr, guest_pthread_schedparam, guest_pthread_start, guest_pthread_join_detach, guest_pthread_mutex_errors, guest_pthread_destroyed_objects and guest_pthread_create_suspended pass three runs in a row on Windows 11 (WinLibs GCC 15.2) and on Linux (WSL2, GCC); check_conventions passes.
  • Head after merging main (f26434afa3, with #10706): d677bc6353. The tests take their declarations from the library headers, and Thread.cpp gets __error from libc/include/Api.hpp instead of declaring it again. The 8 guest_pthread_* tests named here pass three runs in a row on Windows (MinGW) and on Linux (WSL Ubuntu, GCC); public_headers audit and check_conventions pass.
    Windows 11, MinGW (winlibs).
  • guest_pthread_attr checks rejected priorities (-1, 0, 255, 768, 895, 960, 1023, 131840) and accepted ones read back as given (256, 900, 959, 65792, -65280), and EINVAL for a null getinheritsched output.
  • guest_pthread_mutexattr checks the ceiling range.
  • guest_pthread_schedparam checks EINVAL for pthread_getprio(NULL) and the errno rules of both setters (out of range, null, and a joined thread with an out-of-range priority).
  • Nine thread and attribute tests pass three runs in a row, including guest_pthread_attr_errors, guest_pthread_start, guest_pthread_join_detach, guest_pthread_mutex_errors and guest_pthread_destroyed_objects.
  • At 293bc5dd2b (main merged; libkernel.md keeps main's new SCE mutex-attribute entry and this PR's measured POSIX mutex-attribute entry): guest_pthread_attr, guest_pthread_attr_errors, guest_pthread_mutexattr, guest_pthread_schedparam, guest_pthread_start, guest_pthread_join_detach, guest_pthread_mutex_errors and guest_pthread_destroyed_objects pass three runs in a row on Windows 11 (WinLibs GCC 15.2); the first four also pass three runs in a row on Linux (WSL2 6.18, GCC 16.2).
  • At 898736710e (main merged again after #7299; libkernel.md keeps this PR's rewritten pthread_attr_getinheritsched entry plus main's Shared memory and Windows shm_open, shm_unlink entries): guest_pthread_attr, guest_pthread_attr_errors, guest_pthread_mutexattr and guest_pthread_schedparam pass three runs in a row on Windows 11 (WinLibs GCC 15.2) and on Linux (WSL2, GCC 16.2).
  • tools/check_conventions.py passes.

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: #11085 (the pthread_mutexattr protocol debt entry), #10130, #8373, #10089, #10179 (both edit the thread-attribute debt entries; this branch carries its commits until it merges); #8373, #10089, #10179 edit the same lines too (merge order only); #10706 removes the test declarations this PR adds to GuestPthreadAttr.cpp, GuestPthreadMutexattr.cpp and GuestPthreadSchedparam.cpp (merge order only)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4981443027 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10133). Originally posted by [orezraey](https://github.com/orezraey) on 2026-10-23T10:35:31Z. --- ### What Thread priority ranges and POSIX attribute errors measured on a retail PS5 (firmware 13.60) with four homebrew probes calling `libkernel_sys.sprx` from a payload process. The sources and console logs are in [this gist](https://gist.github.com/orezraey/86d00297e19367c6bc277693e3640958) (`pposix`, `pprio`, `pprio2`, `pprio3`). **`scePthreadSetprio` and `pthread_setprio`** accepted any value before. A sweep of every value from -4096 to 70000 on the console accepted exactly 256–767, 896–959, 65792–66303 and 66432–66495. More values outside the sweep agree with the same rule: - the priority is accepted when its **low 16 bits** are 256–767 or 896–959; - 131328, 196864, -65280, 0x7FFF0100 and 0x80000100 are accepted, and 131840 (low bits 768) is refused; - an accepted value is stored and read back exactly as given (`scePthreadGetprio` returns 65792 and -65280); - anything else returns `EINVAL` (`0x80020016`, or 22 from `pthread_setprio`); both set `errno` to 22 and leave the priority alone. A null thread returns `EINVAL` without touching `errno`. A joined or finished thread returns `ESRCH` for any priority, before the range is checked, and leaves `errno` alone. `pthread_setprio` returned `ESRCH` for a joined thread on the console; the order and `scePthreadSetprio` setting `errno` follow firmware 13.42's `libkernel`, which @BunnyFl ran in an emulator below. Before, the value was stored in the ended thread's object. `scePthreadAttrSetschedparam` only takes 256–767, which AnyPS5 already did. **`pthread_getprio(NULL)`** returns -1 with `EINVAL`. Before, it set `ESRCH`. A joined thread keeps `ESRCH`. **`pthread_attr_getinheritsched` with a null output** returns `EINVAL`, as `pthread_attr_getscope` and `pthread_attr_getstackaddr` do. Before, it threw. **`pthread_mutexattr_setprioceiling`** takes ceilings from 256 to 767 under `PTHREAD_PRIO_PROTECT`. Values 0, 255, 768 and 1000 now return `EINVAL`; before, any value was stored. **Already matching, now recorded as measured:** - the default scope 2 and inheritance 4; - `setscope` taking 0 and 2 and returning `EINVAL` for 1, 3 and -1; - `setinheritsched` taking 0 and 4 and returning `ENOTSUP` (45) otherwise; - `pthread_attr_setsolosched_np` taking 0 and returning `ENOTSUP` for 1, 2, -1 and 100; - `EINVAL` for null, uninitialized and destroyed attributes; - mutex protocols 0 to 2 (3 and -1 give `EINVAL`), the ceiling reset to 767 when protocol 2 is set, and `EINVAL` for the ceiling under any other protocol. A null output of `pthread_mutexattr_getprotocol` crashed the probe and is left as it is. ### 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. --> - Head after merging `main` (`5bdda3661e`, with the firmware 13.60 debt relabel from #10179): `8bef063196`. The only conflict was `libkernel.md`: it keeps this PR's measured `pthread_mutexattr_*` entry and `main`'s 13.60 `sceCoredumpSetUserDataType` entry. The 9 `guest_pthread_*` tests named here pass three runs in a row on Windows 11 (WinLibs GCC 15.2) and on Linux (WSL2, GCC); `check_conventions` passes. - Head after merging `main` again (`eeb7760717`, with #10130 and #10105; no conflicts): `da762ebf4b`. `guest_pthread_attr`, `guest_pthread_attr_errors`, `guest_pthread_mutexattr`, `guest_pthread_schedparam`, `guest_pthread_start`, `guest_pthread_join_detach`, `guest_pthread_mutex_errors` and `guest_pthread_destroyed_objects` pass three runs in a row on Windows 11 (WinLibs GCC 15.2) and on Linux (WSL2, GCC); `check_conventions` passes. - Head after merging `main` (`99b8446cbc`, with the `pthread_rwlockattr_settype_np` and `sceCoredumpSetUserDataType` debt updates): `60b9e7f43f`. The only conflict was `libkernel.md`: it keeps this PR's measured `pthread_mutexattr_*` entry and `main`'s new `pthread_rwlockattr_settype_np` entry and PPSA02225 sentence. The 9 `guest_pthread_*` tests named here pass three runs in a row on Windows 11 (WinLibs GCC 15.2) and on Linux (WSL2, GCC); `check_conventions` passes. - Head after merging `main` again (with #9573, through #10130's refreshed branch): `496d29622c`. `libkernel.md` keeps this PR's measured `pthread_getprio, scePthreadSetprio and pthread_setprio` entry; `scePthreadAttrGet` and `pthread_attr_get_np` keep #9573's `createSuspended` next to the inheritance. `guest_pthread_attr`, `guest_pthread_attr_errors`, `guest_pthread_mutexattr`, `guest_pthread_schedparam`, `guest_pthread_start`, `guest_pthread_join_detach`, `guest_pthread_mutex_errors`, `guest_pthread_destroyed_objects` and `guest_pthread_create_suspended` pass three runs in a row on Windows 11 (WinLibs GCC 15.2) and on Linux (WSL2, GCC); `check_conventions` passes. - Head after merging `main` (`f26434afa3`, with #10706): `d677bc6353`. The tests take their declarations from the library headers, and `Thread.cpp` gets `__error` from `libc/include/Api.hpp` instead of declaring it again. The 8 `guest_pthread_*` tests named here pass three runs in a row on Windows (MinGW) and on Linux (WSL Ubuntu, GCC); `public_headers` audit and `check_conventions` pass. Windows 11, MinGW (winlibs). - `guest_pthread_attr` checks rejected priorities (-1, 0, 255, 768, 895, 960, 1023, 131840) and accepted ones read back as given (256, 900, 959, 65792, -65280), and `EINVAL` for a null `getinheritsched` output. - `guest_pthread_mutexattr` checks the ceiling range. - `guest_pthread_schedparam` checks `EINVAL` for `pthread_getprio(NULL)` and the `errno` rules of both setters (out of range, null, and a joined thread with an out-of-range priority). - Nine thread and attribute tests pass three runs in a row, including `guest_pthread_attr_errors`, `guest_pthread_start`, `guest_pthread_join_detach`, `guest_pthread_mutex_errors` and `guest_pthread_destroyed_objects`. - At `293bc5dd2b` (main merged; `libkernel.md` keeps main's new SCE mutex-attribute entry and this PR's measured POSIX mutex-attribute entry): `guest_pthread_attr`, `guest_pthread_attr_errors`, `guest_pthread_mutexattr`, `guest_pthread_schedparam`, `guest_pthread_start`, `guest_pthread_join_detach`, `guest_pthread_mutex_errors` and `guest_pthread_destroyed_objects` pass three runs in a row on Windows 11 (WinLibs GCC 15.2); the first four also pass three runs in a row on Linux (WSL2 6.18, GCC 16.2). - At `898736710e` (main merged again after #7299; `libkernel.md` keeps this PR's rewritten `pthread_attr_getinheritsched` entry plus main's `Shared memory` and Windows `shm_open, shm_unlink` entries): `guest_pthread_attr`, `guest_pthread_attr_errors`, `guest_pthread_mutexattr` and `guest_pthread_schedparam` pass three runs in a row on Windows 11 (WinLibs GCC 15.2) and on Linux (WSL2, GCC 16.2). - `tools/check_conventions.py` passes. ### 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: #11085 (the `pthread_mutexattr` protocol debt entry), #10130, #8373, #10089, #10179 (both edit the thread-attribute debt entries; this branch carries its commits until it merges); #8373, #10089, #10179 edit the same lines too (merge order only); #10706 removes the test declarations this PR adds to `GuestPthreadAttr.cpp`, `GuestPthreadMutexattr.cpp` and `GuestPthreadSchedparam.cpp` (merge order only) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-23 10:50:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T10:50:31Z.

Review: CHANGES_REQUESTED


The firmware 13.60 probe supports the accepted low-16-bit priority ranges, exact stored priorities, errno differences, mutex ceiling range, and null-output behavior. The regression additions cover both accepted/rejected values and live-thread cases. GCC 12.2 syntax checks passed for the changed implementation and tests; conventions, diff-check, and main-sync pass (5 ahead/29 behind; merge clean). The author reports nine related Windows thread/attribute tests passing three times. Integration is blocked by open PR #10130: the current heads conflict in docs/dev/debt/libkernel.md on the thread-attribute entry, and the entries need one combined update. Please coordinate the wording and landing order with #10130, preserve both measured priority and inherited-attribute findings, then refresh and rerun the overlap/convention checks. Add Depends on if one must land first. Hosted policy checks pass; product jobs are skipped by the workflow.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5627214324 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10133#pullrequestreview-5627214324). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T10:50:31Z. **Review:** CHANGES_REQUESTED --- The firmware 13.60 probe supports the accepted low-16-bit priority ranges, exact stored priorities, errno differences, mutex ceiling range, and null-output behavior. The regression additions cover both accepted/rejected values and live-thread cases. GCC 12.2 syntax checks passed for the changed implementation and tests; conventions, diff-check, and main-sync pass (5 ahead/29 behind; merge clean). The author reports nine related Windows thread/attribute tests passing three times. Integration is blocked by open PR #10130: the current heads conflict in `docs/dev/debt/libkernel.md` on the thread-attribute entry, and the entries need one combined update. Please coordinate the wording and landing order with #10130, preserve both measured priority and inherited-attribute findings, then refresh and rerun the overlap/convention checks. Add `Depends on` if one must land first. Hosted policy checks pass; product jobs are skipped by the workflow.
MarcZX10 commented 2026-10-23 11:12:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T11:12:54Z.

Review: APPROVED


Reviewed the exact head 8c2c979aec7a0259e1819759f111b3ad7ff55c0b. The low-16-bit priority validation matches the measured accepted ranges and preserves the input value when storing; live-thread locking prevents writes to retired thread state. The POSIX wrapper distinguishes null, invalid-priority and joined-thread outcomes as documented, and the attribute changes now return measured POSIX errors instead of throwing. The probe sources/logs are linked in the description. Exact conventions/diff checks pass and merge preview is clean. Hosted conventions, eligibility, intake, labels and report pass; product jobs were skipped by path policy. The author reports nine focused Windows thread/attribute tests passing three runs each; I did not independently run them. No blocking issue found.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5627438779 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10133#pullrequestreview-5627438779). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T11:12:54Z. **Review:** APPROVED --- Reviewed the exact head `8c2c979aec7a0259e1819759f111b3ad7ff55c0b`. The low-16-bit priority validation matches the measured accepted ranges and preserves the input value when storing; live-thread locking prevents writes to retired thread state. The POSIX wrapper distinguishes null, invalid-priority and joined-thread outcomes as documented, and the attribute changes now return measured POSIX errors instead of throwing. The probe sources/logs are linked in the description. Exact conventions/diff checks pass and merge preview is clean. Hosted conventions, eligibility, intake, labels and report pass; product jobs were skipped by path policy. The author reports nine focused Windows thread/attribute tests passing three runs each; I did not independently run them. No blocking issue found.
MarcZX10 commented 2026-10-23 12:15:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T12:15:37Z.

Review: APPROVED


Reviewed the current head a99a56fe83d7fef6428e8af2fa3acf361a417426, including the post-review updates. Priority validation uses the measured low-16-bit ranges and preserves the original accepted value; the live-thread lookup preserves the measured ESRCH-before-range behavior, while null and invalid-priority errno handling remain distinct. pthread_getprio(NULL), null getinheritsched output, and the mutex ceiling range now have matching regressions. The inheritance field is captured at thread creation and reported by both attribute query paths. The PR body lists its dependency on #10130 and carries the relevant combined thread-attribute changes. Current eligibility, conventions, labels and report checks pass; product jobs are skipped by repository policy. The author reports nine focused Windows thread/attribute tests passing three runs each; I did not independently run that suite locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5628067819 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10133#pullrequestreview-5628067819). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T12:15:37Z. **Review:** APPROVED --- Reviewed the current head `a99a56fe83d7fef6428e8af2fa3acf361a417426`, including the post-review updates. Priority validation uses the measured low-16-bit ranges and preserves the original accepted value; the live-thread lookup preserves the measured ESRCH-before-range behavior, while null and invalid-priority errno handling remain distinct. `pthread_getprio(NULL)`, null `getinheritsched` output, and the mutex ceiling range now have matching regressions. The inheritance field is captured at thread creation and reported by both attribute query paths. The PR body lists its dependency on #10130 and carries the relevant combined thread-attribute changes. Current eligibility, conventions, labels and report checks pass; product jobs are skipped by repository policy. The author reports nine focused Windows thread/attribute tests passing three runs each; I did not independently run that suite locally.
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!10133
No description provided.