feat(libkernel): implement pthread_getaffinity_np #1922

Merged
rmz-24 merged 1 commit from upstream-pr/1922 into main 2026-10-11 08:32:43 +00:00
rmz-24 commented 2026-10-09 00:06:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rmz-24 on 2026-10-09T00:06:11Z.


What

Implements pthread_getaffinity_np (libkernel, NID Jb2uGFMr688), claimed in #1578. EVO-PLAYER PPSA99039 v0.11.0 imports it, and it is the only one of the title's absent imports with no open pull request. The title calls it once, at 0xE1DB90: pthread_getaffinity_np(pthread_self(), 0x20, &set), then counts the bits of the 32-byte set. On failure it logs and falls back.

int pthread_getaffinity_np(pthread_t td, size_t cpusetsize, cpuset_t* cpusetp);

Semantics follow FreeBSD 11 lib/libthr/thread/thr_affinity.c and kern_cpuset_getaffinity in sys/kern/kern_cpuset.c:

  • The error number is returned, not set in errno.
  • cpuset_t is CPU_MAXSIZE (256) bits, 32 bytes. The kernel accepts only sizeof(cpuset_t) <= cpusetsize <= CPU_MAXSIZE / 8, so any size other than 32 returns ERANGE.
  • A null thread returns ESRCH, and a null set returns EFAULT.
  • The set receives the thread's affinity mask, the same value scePthreadGetaffinity returns, in its low 64 bits. The other 24 bytes are zeroed, as the kernel copies out the whole zero-filled set.

Tested

  • Windows 11, WinLibs GCC 15.2.0 r7, Release.
  • guest_available_cpumask is extended:
    • The set of pthread_self() and of a created thread matches scePthreadGetaffinity, including after each scePthreadSetaffinity.
    • Bytes 8–31 are zeroed, and nothing past 32 bytes is written.
    • Sizes 0, 8, 31, 33 and 64 return ERANGE. A null thread returns ESRCH, and a null set returns EFAULT.
  • All 153 guest_* tests pass.
  • EVO-PLAYER v0.11.0 relinked with --windows: tools/import_audit.py no longer lists Jb2uGFMr688 as absent. The title can't reach the call yet, because 20 other imports are still absent, all with open pull requests (#1578).

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 in TechnicalDebt
  • 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
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4794270280 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1922). Originally posted by [rmz-24](https://github.com/rmz-24) on 2026-10-09T00:06:11Z. --- ### What Implements `pthread_getaffinity_np` (libkernel, NID `Jb2uGFMr688`), claimed in #1578. EVO-PLAYER PPSA99039 v0.11.0 imports it, and it is the only one of the title's absent imports with no open pull request. The title calls it once, at `0xE1DB90`: `pthread_getaffinity_np(pthread_self(), 0x20, &set)`, then counts the bits of the 32-byte set. On failure it logs and falls back. ```c int pthread_getaffinity_np(pthread_t td, size_t cpusetsize, cpuset_t* cpusetp); ``` Semantics follow FreeBSD 11 [`lib/libthr/thread/thr_affinity.c`](https://github.com/freebsd/freebsd-src/blob/stable/11/lib/libthr/thread/thr_affinity.c) and `kern_cpuset_getaffinity` in [`sys/kern/kern_cpuset.c`](https://github.com/freebsd/freebsd-src/blob/stable/11/sys/kern/kern_cpuset.c): - The error number is returned, not set in `errno`. - `cpuset_t` is `CPU_MAXSIZE` (256) bits, 32 bytes. The kernel accepts only `sizeof(cpuset_t) <= cpusetsize <= CPU_MAXSIZE / 8`, so any size other than 32 returns `ERANGE`. - A null thread returns `ESRCH`, and a null set returns `EFAULT`. - The set receives the thread's affinity mask, the same value `scePthreadGetaffinity` returns, in its low 64 bits. The other 24 bytes are zeroed, as the kernel copies out the whole zero-filled set. ### Tested - Windows 11, WinLibs GCC 15.2.0 r7, Release. - `guest_available_cpumask` is extended: - The set of `pthread_self()` and of a created thread matches `scePthreadGetaffinity`, including after each `scePthreadSetaffinity`. - Bytes 8–31 are zeroed, and nothing past 32 bytes is written. - Sizes 0, 8, 31, 33 and 64 return `ERANGE`. A null thread returns `ESRCH`, and a null set returns `EFAULT`. - All 153 `guest_*` tests pass. - EVO-PLAYER v0.11.0 relinked with `--windows`: `tools/import_audit.py` no longer lists `Jb2uGFMr688` as absent. The title can't reach the call yet, because 20 other imports are still absent, all with open pull requests (#1578). ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes
klosixgaming-arch commented 2026-10-09 00:37:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by klosixgaming-arch on 2026-10-09T00:37:41Z.

Review: APPROVED


Correct, general implementation of pthread_getaffinity_np. The error paths are handled properly (ERANGE for a mismatched cpusetsize, ESRCH for a null thread, EFAULT for a null buffer) and the buffer is zeroed to the full guest cpu_set_t before the KernelCpumask is copied in — so callers reading the whole structure don't see stale bytes. The test covers the happy path plus all three error codes and verifies the bytes past the mask are zeroed and the guard bytes after the buffer are untouched.

One ABI assumption worth a sanity check before merge: GUEST_CPUSET_BYTES is hard-fixed at 32 and any other size is rejected with ERANGE. That's correct only if the PS5's cpu_set_t is exactly 32 bytes. If a title ever passes a larger cpusetsize expecting zero-fill (glibc permits cpusetsize >= needed), this would spuriously fail. Worth confirming the 32-byte figure against the guest ABI / a title, or accepting >= sizeof(KernelCpumask) and zero-filling the remainder.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5464471991 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1922#pullrequestreview-5464471991). Originally posted by [klosixgaming-arch](https://github.com/klosixgaming-arch) on 2026-10-09T00:37:41Z. **Review:** APPROVED --- Correct, general implementation of `pthread_getaffinity_np`. The error paths are handled properly (ERANGE for a mismatched `cpusetsize`, ESRCH for a null thread, EFAULT for a null buffer) and the buffer is zeroed to the full guest `cpu_set_t` before the `KernelCpumask` is copied in — so callers reading the whole structure don't see stale bytes. The test covers the happy path plus all three error codes and verifies the bytes past the mask are zeroed and the guard bytes after the buffer are untouched. One ABI assumption worth a sanity check before merge: `GUEST_CPUSET_BYTES` is hard-fixed at 32 and any other size is rejected with ERANGE. That's correct only if the PS5's `cpu_set_t` is exactly 32 bytes. If a title ever passes a larger `cpusetsize` expecting zero-fill (glibc permits `cpusetsize >= needed`), this would spuriously fail. Worth confirming the 32-byte figure against the guest ABI / a title, or accepting `>= sizeof(KernelCpumask)` and zero-filling the remainder. Written with AI assistance.
oneandonlydean commented 2026-10-09 09:30:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-09T09:30:37Z.


Closing: EVO-PLAYER can't reach pthread_getaffinity_np yet; as the description says, 20 other imports stop it first. Reopen when it's the title's next stop on main, with the log line.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6078243757 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1922#issuecomment-6078243757). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-09T09:30:37Z. --- Closing: EVO-PLAYER can't reach `pthread_getaffinity_np` yet; as the description says, 20 other imports stop it first. Reopen when it's the title's next stop on main, with the log line.
babyhuey commented 2026-10-11 06:37:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by babyhuey on 2026-10-11T06:37:18Z.


@rmz-24 pthread_getaffinity_np is now the next stop for three catalog titles on main 52ce9b8a (Linux, catalog builds with sce_module/libc.prx unwrapped as #2605 requires), each with app.elf: symbol lookup error: app.elf: undefined symbol: Jb2uGFMr688:

  • EVO Player (PPSA99039), which goes on to setcontext with this PR
  • PS5VR (PPSA99177), which goes on to link
  • Jelly5 (PPSA99505), which goes on to getprogname (#1869)

The head (79e3daf5) conflicts with main only in GuestAvailableCpumask.cpp: a newer test there declares its own cpuset in the same function. Keeping both sides and renaming this PR's array to affinity makes guest_available_cpumask pass 5/5 (--repeat until-fail:5).

Intake needs 10 merged PRs to open a new one. If you'd like, I can open it as a carry that keeps your commit and authorship, with that rename on top. I'll wait for your yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6106308403 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1922#issuecomment-6106308403). Originally posted by [babyhuey](https://github.com/babyhuey) on 2026-10-11T06:37:18Z. --- @rmz-24 `pthread_getaffinity_np` is now the next stop for three catalog titles on `main` 52ce9b8a (Linux, catalog builds with `sce_module/libc.prx` unwrapped as #2605 requires), each with `app.elf: symbol lookup error: app.elf: undefined symbol: Jb2uGFMr688`: - EVO Player (PPSA99039), which goes on to `setcontext` with this PR - PS5VR (PPSA99177), which goes on to `link` - Jelly5 (PPSA99505), which goes on to `getprogname` (#1869) The head (79e3daf5) conflicts with `main` only in `GuestAvailableCpumask.cpp`: a newer test there declares its own `cpuset` in the same function. Keeping both sides and renaming this PR's array to `affinity` makes `guest_available_cpumask` pass 5/5 (`--repeat until-fail:5`). Intake needs 10 merged PRs to open a new one. If you'd like, I can open it as a carry that keeps your commit and authorship, with that rename on top. I'll wait for your yes.
oneandonlydean commented 2026-10-11 08:19:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-11T08:19:28Z.


Reopened: it's now the next stop for three titles (see above). Two things before it goes in:

  • a null thread returns EINVAL on FreeBSD 11, not ESRCH (_thr_find_thread in thr_list.c), and the thread is looked up before the size is checked, so a null thread with a wrong size is EINVAL too;
  • rebase onto main: GuestAvailableCpumask.cpp conflicts with a newer test's cpuset in the same function.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6107065083 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1922#issuecomment-6107065083). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-11T08:19:28Z. --- Reopened: it's now the next stop for three titles (see above). Two things before it goes in: - a null thread returns `EINVAL` on FreeBSD 11, not `ESRCH` (`_thr_find_thread` in `thr_list.c`), and the thread is looked up before the size is checked, so a null thread with a wrong size is `EINVAL` too; - rebase onto main: `GuestAvailableCpumask.cpp` conflicts with a newer test's `cpuset` in the same function.
github-actions[bot] commented 2026-10-11 08:19:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-11T08:19:47Z.


Progress report (0c617ba → 45eb492)

➖ System libraries: 85.54% (+0.0%, +1 implemented functions)

✅ 1 implemented
Library Name
libkernel pthread_getaffinity_np_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6107067293 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1922#issuecomment-6107067293). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-11T08:19:47Z. --- <!-- progress-report --> ### Progress report (0c617ba → 45eb492) ➖ **System libraries**: 85.54% (+0.0%, +1 implemented functions) <details> <summary>✅ 1 implemented</summary> | Library | Name | | - | - | | libkernel | `pthread_getaffinity_np_nid_postfix` | </details>
adriadam10 commented 2026-10-11 08:27:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T08:27:47Z.


Rebased onto current main and pushed the two changes @oneandonlydean asked for, so this can merge:

  • A null thread now returns EINVAL (_thr_find_thread in FreeBSD 11 thr_list.c), and the thread is looked up before the size check, so a null thread with a wrong size is EINVAL too.
  • Resolved the conflict in GuestAvailableCpumask.cpp (the new test's cpuset vs. ours), reused PosixThread::GUEST_EINVAL/GUEST_EFAULT instead of local copies, dropped a duplicate scePthreadGetaffinity declaration. The test now pins EINVAL for a null thread with sizes 32 and 0.

Checked: build + ctest on the rebased tree (guest_available_cpumask passes and fails to link without the change), no other open PR touches these exports. Merging once CI is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6107127810 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1922#issuecomment-6107127810). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T08:27:47Z. --- Rebased onto current `main` and pushed the two changes @oneandonlydean asked for, so this can merge: - A null thread now returns `EINVAL` (`_thr_find_thread` in FreeBSD 11 `thr_list.c`), and the thread is looked up before the size check, so a null thread with a wrong size is `EINVAL` too. - Resolved the conflict in `GuestAvailableCpumask.cpp` (the new test's `cpuset` vs. ours), reused `PosixThread::GUEST_EINVAL`/`GUEST_EFAULT` instead of local copies, dropped a duplicate `scePthreadGetaffinity` declaration. The test now pins `EINVAL` for a null thread with sizes 32 and 0. Checked: build + ctest on the rebased tree (`guest_available_cpumask` passes and fails to link without the change), no other open PR touches these exports. Merging once CI is green.
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!1922
No description provided.