feat(libkernel): implement four pthread_attr forms #9156

Closed
DavidSamir wants to merge 7 commits from upstream-pr/9156 into main
DavidSamir commented 2026-10-21 14:00:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-21T14:00:41Z.


What

Four pthread attribute exports in libkernel_sys were discovery stubs that threw NotImplemented. They are now implemented in libkernel, in libkernel/Pthread/Posix/AttrNp.cpp, and the libkernel_sys stubs are removed. They wrap libkernel's existing scePthreadAttr* calls, map the result to an errno the way PosixThread::ToErrno does (& 0xFFFF), and follow FreeBSD 11 lib/libthr/thread/thr_attr.c:

Export Over Errors
pthread_attr_getsolosched_np(attr, solosched) scePthreadAttrGetsolosched EINVAL for a NULL attr, *attr or out pointer, as the existing pthread_attr_getstackaddr does
pthread_attr_setstackaddr(attr, addr) scePthreadAttrSetstackaddr EINVAL for a NULL attr or *attr (_pthread_attr_setstackaddr)
pthread_attr_getaffinity_np(attr, size, set) scePthreadAttrGetaffinity EINVAL for a NULL attr or *attr; ERANGE for size below the 32-byte kernel set (_pthread_attr_getaffinity_np); the bytes past the 64-bit mask are zeroed; EFAULT for a NULL set, as the existing pthread_getaffinity_np does
pthread_attr_setaffinity_np(attr, size, set) scePthreadAttrSetaffinity EINVAL for a NULL attr or *attr, and for nonzero bytes past the 64-bit mask, which scePthreadAttrSetaffinity can't hold

Two inputs throw and are recorded in docs/dev/debt/libkernel.md:

  • pthread_attr_setstackaddr with a NULL address. FreeBSD stores it, but scePthreadAttrSetstackaddr refuses it.
  • pthread_attr_setaffinity_np with a zero size or a NULL set. FreeBSD takes that as clearing the attribute's set, and no scePthreadAttr* call clears it.

Moved to libkernel so libkernel imports reach it on Windows. The PS5 3.20 export lists have these four in libkernel as well as libkernel_sys, and on Windows a libkernel import never searches libkernel_sys (WindowsEntryStubBuilder.cpp:175). With the libkernel exports and no libkernel_sys stubs, both imports reach them, and the duplicate-export rule holds. The code is unchanged by the move.

Tested

Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, Release. These are CPU-side exports, so no GPU is involved.

  • New guest_kernel_sys_pthread_attr (core/libs/tests/GuestKernelSysPthreadAttr.cpp):
    • the solosched round trip (0, then 16, the value libkernel's setter accepts, then a NULL out and a NULL attribute);
    • the stack address round trip through pthread_attr_getstackaddr;
    • the affinity get (0x5, a 40-byte set zero-filled past the mask, ERANGE at 16 bytes, EFAULT for a NULL set) and set (32- and 8-byte sets, EINVAL for a nonzero byte 9, the stored mask unchanged after it, read back through scePthreadAttrGetaffinity), each with a NULL attribute as well.
  • On main the test aborts with pthread_attr_getsolosched_np_nid_postfix not implemented. On this branch it passes. guest_kernel_sys still passes.
  • tools/check_conventions.py passes, including the duplicate-export rule.
  • Link control: with AttrNp.cpp removed from libkernel's sources, the test fails to link (undefined reference to pthread_attr_getsolosched_np_nid_postfix), so it reaches libkernel's exports, the only ones.
  • Overlap: tools/pr_overlap.py crashes on this machine's partial clone, so it was replaced by a check against an index of every open PR's diff, plus git merge-tree against the 8 open PRs that touch these files (#9141, #8373, #8987, #9103, #9019, #9008, #9040, #9097). All 8 merge cleanly.

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 (#9573 goes second and rebases, agreed with its author on this PR)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4952845911 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9156). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-21T14:00:41Z. --- ### What Four pthread attribute exports in `libkernel_sys` were discovery stubs that threw `NotImplemented`. They are now implemented in libkernel, in `libkernel/Pthread/Posix/AttrNp.cpp`, and the libkernel_sys stubs are removed. They wrap libkernel's existing `scePthreadAttr*` calls, map the result to an `errno` the way `PosixThread::ToErrno` does (`& 0xFFFF`), and follow FreeBSD 11 `lib/libthr/thread/thr_attr.c`: | Export | Over | Errors | |---|---|---| | `pthread_attr_getsolosched_np(attr, solosched)` | `scePthreadAttrGetsolosched` | `EINVAL` for a NULL `attr`, `*attr` or out pointer, as the existing `pthread_attr_getstackaddr` does | | `pthread_attr_setstackaddr(attr, addr)` | `scePthreadAttrSetstackaddr` | `EINVAL` for a NULL `attr` or `*attr` (`_pthread_attr_setstackaddr`) | | `pthread_attr_getaffinity_np(attr, size, set)` | `scePthreadAttrGetaffinity` | `EINVAL` for a NULL `attr` or `*attr`; `ERANGE` for `size` below the 32-byte kernel set (`_pthread_attr_getaffinity_np`); the bytes past the 64-bit mask are zeroed; `EFAULT` for a NULL set, as the existing `pthread_getaffinity_np` does | | `pthread_attr_setaffinity_np(attr, size, set)` | `scePthreadAttrSetaffinity` | `EINVAL` for a NULL `attr` or `*attr`, and for nonzero bytes past the 64-bit mask, which `scePthreadAttrSetaffinity` can't hold | Two inputs throw and are recorded in `docs/dev/debt/libkernel.md`: - `pthread_attr_setstackaddr` with a NULL address. FreeBSD stores it, but `scePthreadAttrSetstackaddr` refuses it. - `pthread_attr_setaffinity_np` with a zero `size` or a NULL set. FreeBSD takes that as clearing the attribute's set, and no `scePthreadAttr*` call clears it. **Moved to libkernel so libkernel imports reach it on Windows.** The PS5 3.20 export lists have these four in libkernel as well as libkernel_sys, and on Windows a libkernel import never searches libkernel_sys (`WindowsEntryStubBuilder.cpp:175`). With the libkernel exports and no libkernel_sys stubs, both imports reach them, and the duplicate-export rule holds. The code is unchanged by the move. ### Tested Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, Release. These are CPU-side exports, so no GPU is involved. - New `guest_kernel_sys_pthread_attr` (`core/libs/tests/GuestKernelSysPthreadAttr.cpp`): - the solosched round trip (0, then 16, the value libkernel's setter accepts, then a NULL out and a NULL attribute); - the stack address round trip through `pthread_attr_getstackaddr`; - the affinity get (`0x5`, a 40-byte set zero-filled past the mask, `ERANGE` at 16 bytes, `EFAULT` for a NULL set) and set (32- and 8-byte sets, `EINVAL` for a nonzero byte 9, the stored mask unchanged after it, read back through `scePthreadAttrGetaffinity`), each with a NULL attribute as well. - On `main` the test aborts with `pthread_attr_getsolosched_np_nid_postfix not implemented`. On this branch it passes. `guest_kernel_sys` still passes. - `tools/check_conventions.py` passes, including the duplicate-export rule. - Link control: with `AttrNp.cpp` removed from libkernel's sources, the test fails to link (`undefined reference to pthread_attr_getsolosched_np_nid_postfix`), so it reaches libkernel's exports, the only ones. - Overlap: `tools/pr_overlap.py` crashes on this machine's partial clone, so it was replaced by a check against an index of every open PR's diff, plus `git merge-tree` against the 8 open PRs that touch these files (#9141, #8373, #8987, #9103, #9019, #9008, #9040, #9097). All 8 merge cleanly. ### 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 (#9573 goes second and rebases, agreed with its author on this PR) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
DavidSamir commented 2026-10-21 15:01:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-21T15:01:13Z.


Overlap note: #9160 (opened 4 minutes after this PR) also implements pthread_attr_getsolosched_np, pthread_attr_setstackaddr, pthread_attr_getaffinity_np and pthread_attr_setaffinity_np, together with pthread_main_np. This PR covers only the four attribute forms, and it merges cleanly with current main.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6319207063 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9156#issuecomment-6319207063). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-21T15:01:13Z. --- Overlap note: #9160 (opened 4 minutes after this PR) also implements `pthread_attr_getsolosched_np`, `pthread_attr_setstackaddr`, `pthread_attr_getaffinity_np` and `pthread_attr_setaffinity_np`, together with `pthread_main_np`. This PR covers only the four attribute forms, and it merges cleanly with current main.
DavidSamir commented 2026-10-22 02:06:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-22T02:06:14Z.


Linux: Ubuntu 24.04.1 LTS (WSL2), GCC 13.3.0, Release; head 44e997ec4 merged onto main 8c26eac3c.

  • guest_kernel_sys_pthread_attr passes 3/3.
  • Link control: with pthread_attr_getsolosched_np_nid_postfix renamed in AttrNp.cpp (instead of removing the file from the sources), the test fails to link: undefined reference to pthread_attr_getsolosched_np_nid_postfix, as listed.

Run from the PC (Windows host, Linux in WSL2), not from the PR author's machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6330941433 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9156#issuecomment-6330941433). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-22T02:06:14Z. --- Linux: Ubuntu 24.04.1 LTS (WSL2), GCC 13.3.0, Release; head `44e997ec4` merged onto `main` `8c26eac3c`. - `guest_kernel_sys_pthread_attr` passes 3/3. - Link control: with `pthread_attr_getsolosched_np_nid_postfix` renamed in `AttrNp.cpp` (instead of removing the file from the sources), the test fails to link: `undefined reference to pthread_attr_getsolosched_np_nid_postfix`, as listed. Run from the PC (Windows host, Linux in WSL2), not from the PR author's machine.
zaryabzip commented 2026-10-22 08:04:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by zaryabzip on 2026-10-22T08:04:26Z.


Hi @DavidSamir, this PR and #9573 (threads created suspended) meet in core/libs/prx/libkernel_sys/Export.Discovery.cpp. This PR deletes the pthread_attr_getaffinity_np, pthread_attr_getsolosched_np and pthread_attr_setaffinity_np stubs (L361-375 on main), and #9573 changes the pthread_attr_setcreatesuspend_np signature on the very next line (L376). Git counts edits on adjacent lines as one conflict, and that signature can't move, so one of the two has to go first.

I'd suggest this PR goes first, which needs nothing from you: #9573 now says Depends on: #9156, and I'll rebase it onto main and resolve the conflict once this one is merged. If you'd rather have the other order, let me know and I'll switch it.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6335875190 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9156#issuecomment-6335875190). Originally posted by [zaryabzip](https://github.com/zaryabzip) on 2026-10-22T08:04:26Z. --- Hi @DavidSamir, this PR and #9573 (threads created suspended) meet in `core/libs/prx/libkernel_sys/Export.Discovery.cpp`. This PR deletes the `pthread_attr_getaffinity_np`, `pthread_attr_getsolosched_np` and `pthread_attr_setaffinity_np` stubs (L361-375 on main), and #9573 changes the `pthread_attr_setcreatesuspend_np` signature on the very next line (L376). Git counts edits on adjacent lines as one conflict, and that signature can't move, so one of the two has to go first. I'd suggest this PR goes first, which needs nothing from you: #9573 now says `Depends on: #9156`, and I'll rebase it onto main and resolve the conflict once this one is merged. If you'd rather have the other order, let me know and I'll switch it.
DavidSamir commented 2026-10-22 09:20:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-22T09:20:33Z.


Thanks @zaryabzip, that order works for me: #9156 first, and #9573 rebases onto main after it. I've noted it in this PR's description.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6337293594 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9156#issuecomment-6337293594). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-22T09:20:33Z. --- Thanks @zaryabzip, that order works for me: #9156 first, and #9573 rebases onto `main` after it. I've noted it in this PR's description. AI-assisted: yes
harisibrar1 commented 2026-10-24 12:55:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T12:55:45Z.

Review: COMMENTED


Checked against main 99feaf0eb8 and FreeBSD 11.4 lib/libthr/thread/thr_attr.c:

  • pthread_attr_setstackaddr with a NULL address: FreeBSD 11.4 returns EINVAL (_pthread_attr_setstackaddr checks stackaddr == NULL together with attr/*attr, L545); it doesn't store it. So the throw in AttrNp.cpp:52 and its debt bullet aren't needed. scePthreadAttrSetstackaddr on main already returns SCE_KERNEL_ERROR_EINVAL for a NULL address, which PthreadAttrErrno maps to 22.
  • pthread_attr_setaffinity_np has no lower bound on cpusetsize, so (attr, 1, "\x03") stores mask 0x3 and returns 0. FreeBSD returns ERANGE below the kernel set size (L609-611), and the getter here already uses 32 for ERANGE. Could the setter use the same bound? The test at GuestKernelSysPthreadAttr.cpp:85 (size 8 → 0) would change with it. Main's cpuset_getaffinity/cpuset_setaffinity use 8 as the minimum, so it's worth deciding which bound both should use.
  • Small note: on an attribute that never had a set, FreeBSD's getter returns an all-ones set, while this returns main's 0x1FFF default. That seems reasonable for the sce layer; only mentioning it in case it matters.
  • docs/dev/debt/libkernel.md now conflicts with main, because the neighbouring sceKernelAioSetParam bullet was rewritten, so it needs a rebase.
  • Optional: Posix/Attr.cpp uses PthreadAttr* and PosixThread::ToErrno/GUEST_EINVAL from Common.hpp. Using those here instead of redeclaring them with void** and a local PthreadAttrErrno would keep the two files consistent.

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

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638316329 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9156#pullrequestreview-5638316329). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-24T12:55:45Z. **Review:** COMMENTED --- Checked against main 99feaf0eb8 and FreeBSD 11.4 lib/libthr/thread/thr_attr.c: - `pthread_attr_setstackaddr` with a NULL address: FreeBSD 11.4 returns `EINVAL` (`_pthread_attr_setstackaddr` checks `stackaddr == NULL` together with `attr`/`*attr`, L545); it doesn't store it. So the throw in `AttrNp.cpp:52` and its debt bullet aren't needed. `scePthreadAttrSetstackaddr` on main already returns `SCE_KERNEL_ERROR_EINVAL` for a NULL address, which `PthreadAttrErrno` maps to 22. - `pthread_attr_setaffinity_np` has no lower bound on `cpusetsize`, so `(attr, 1, "\x03")` stores mask 0x3 and returns 0. FreeBSD returns `ERANGE` below the kernel set size (L609-611), and the getter here already uses 32 for `ERANGE`. Could the setter use the same bound? The test at `GuestKernelSysPthreadAttr.cpp:85` (size 8 → 0) would change with it. Main's `cpuset_getaffinity`/`cpuset_setaffinity` use 8 as the minimum, so it's worth deciding which bound both should use. - Small note: on an attribute that never had a set, FreeBSD's getter returns an all-ones set, while this returns main's 0x1FFF default. That seems reasonable for the sce layer; only mentioning it in case it matters. - `docs/dev/debt/libkernel.md` now conflicts with main, because the neighbouring `sceKernelAioSetParam` bullet was rewritten, so it needs a rebase. - Optional: `Posix/Attr.cpp` uses `PthreadAttr*` and `PosixThread::ToErrno`/`GUEST_EINVAL` from `Common.hpp`. Using those here instead of redeclaring them with `void**` and a local `PthreadAttrErrno` would keep the two files consistent. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
MarcZX10 commented 2026-11-23 02:32:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-23T02:32:05Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the targeted code review found the four wrappers consistent with the documented behavior and existing attribute APIs.

What I ran

Reviewed head 550810d74a304089683fd7f6994315974df0aaa1 using the prepared diff and repository reader. No commands or tests were run.

Results

Check Result Note
Targeted source review passed Wrapper logic and callers inspected
Automated checks not run Author reports Windows tests and convention check passed

Findings

Blocking

None.

Non-blocking

None.

Not verified

No automated checks were run. Reviewed all six changed files listed in the manifest; no files were excluded. The described Windows test runs and overlap checks are author-reported, not independently verified.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6015939994 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9156#pullrequestreview-6015939994). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-23T02:32:05Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the targeted code review found the four wrappers consistent with the documented behavior and existing attribute APIs. ## What I ran Reviewed head `550810d74a304089683fd7f6994315974df0aaa1` using the prepared diff and repository reader. No commands or tests were run. ## Results | Check | Result | Note | |---|---|---| | Targeted source review | passed | Wrapper logic and callers inspected | | Automated checks | not run | Author reports Windows tests and convention check passed | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified No automated checks were run. Reviewed all six changed files listed in the manifest; no files were excluded. The described Windows test runs and overlap checks are author-reported, not independently verified. ## To get this to a good state Nothing is required.

Pull request closed

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!9156
No description provided.