feat(libkernel): implement cpuset_getaffinity for any thread or the process and pthread_attr_setsolosched_np #3168

Closed
adriadam10 wants to merge 1 commit from upstream-pr/3168 into main
adriadam10 commented 2026-10-10 23:55:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T23:55:46Z.


What

Two libkernel exports that threw on main:

  • cpuset_getaffinity: before this PR, only the calling thread (CPU_LEVEL_WHICH, CPU_WHICH_TID, id -1) was handled; everything else hit NotImplemented. It now follows FreeBSD 11 sys_cpuset_getaffinity and cpuset_which (sys/kern/kern_cpuset.c), checking in the same order:

    • ERANGE for the size (unchanged).
    • EINVAL for an unknown which, then ESRCH for an unknown id.
    • EINVAL for an unknown level.
    • EFAULT for a null output.

    Supported cases:

    • CPU_WHICH_TID: id -1 or the id of any live thread of the process (scePthreadGetthreadid) → that thread's mask.
    • CPU_WHICH_PID: id -1 or getpid() → the union of the live threads' masks, as FreeBSD's FOREACH_THREAD_IN_PROC does. That is 0x1fff when the threads keep the default, matching what prospero-win measured on a console (patch 0881: a game's cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1) is 0x1fff).
    • Other ids: another process, or a thread that isn't a live thread of this one (including one that has exited but isn't joined yet), give ESRCH, as p_cansee does for a process outside the caller's jail.
    • Still throwing: CPU_LEVEL_ROOT/CPU_LEVEL_CPUSET and the cpuset, irq, jail and domain kinds, since the console's sets aren't known.
  • pthread_attr_setsolosched_np: it was declared without parameters. It now takes (pthread_attr_t*, int) as scePthreadAttrSetsolosched does (KytyPS5), returns EINVAL for a null or destroyed attribute like the other POSIX attribute setters, and stores the value for scePthreadAttrGetsolosched.

TechnicalDebt has a line for each under "Unknown function info": the ESRCH for ids outside the process isn't measured, and the solosched signature is taken from the sce twin.

Tested

Linux (CachyOS), GCC 16, Release.

  • guest_available_cpumask:
    • own tid and -1, getpid() and -1, all giving the default mask;
    • a second thread pinned to the highest CPU while the main thread is pinned to the lowest: its tid gives its mask, the PID gives the union;
    • after that thread exits (before join) and after the join, the PID gives only the main thread's CPU and its tid gives ESRCH;
    • ESRCH for tid 1 and getpid()+1;
    • EINVAL for which 0/7 and level 0/4;
    • lookup errors before level errors (ESRCH for level 4/ROOT with an unknown tid);
    • level errors before EFAULT.
  • guest_pthread_attr: solosched round-trips 0 → 1 → 0 through scePthreadAttrGetsolosched, with EINVAL for null and destroyed attributes.
  • On main both tests stop: pthread_attr_setsolosched_np and cpuset_getaffinity for an explicit tid both reach NotImplemented.
  • Negative checks: each of these mutations makes a test fail (10 of 10): union → last thread, union or tid lookup including exited threads, the ESRCH/EINVAL/EFAULT codes, the PID check, the which check, the level check, solosched value and null check.
  • The two tests pass 30 runs in a row. All 231 guest_ tests pass, and 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 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:4815310624 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3168). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-10T23:55:46Z. --- ### What Two libkernel exports that threw on `main`: - **`cpuset_getaffinity`**: before this PR, only the calling thread (`CPU_LEVEL_WHICH`, `CPU_WHICH_TID`, id -1) was handled; everything else hit `NotImplemented`. It now follows FreeBSD 11 `sys_cpuset_getaffinity` and `cpuset_which` (`sys/kern/kern_cpuset.c`), checking in the same order: - `ERANGE` for the size (unchanged). - `EINVAL` for an unknown `which`, then `ESRCH` for an unknown id. - `EINVAL` for an unknown level. - `EFAULT` for a null output. Supported cases: - **`CPU_WHICH_TID`**: id -1 or the id of any live thread of the process (`scePthreadGetthreadid`) → that thread's mask. - **`CPU_WHICH_PID`**: id -1 or `getpid()` → the union of the live threads' masks, as FreeBSD's `FOREACH_THREAD_IN_PROC` does. That is `0x1fff` when the threads keep the default, matching what prospero-win measured on a console (patch 0881: a game's `cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1)` is `0x1fff`). - **Other ids**: another process, or a thread that isn't a live thread of this one (including one that has exited but isn't joined yet), give `ESRCH`, as `p_cansee` does for a process outside the caller's jail. - **Still throwing**: `CPU_LEVEL_ROOT`/`CPU_LEVEL_CPUSET` and the cpuset, irq, jail and domain kinds, since the console's sets aren't known. - **`pthread_attr_setsolosched_np`**: it was declared without parameters. It now takes `(pthread_attr_t*, int)` as `scePthreadAttrSetsolosched` does (KytyPS5), returns `EINVAL` for a null or destroyed attribute like the other POSIX attribute setters, and stores the value for `scePthreadAttrGetsolosched`. TechnicalDebt has a line for each under "Unknown function info": the `ESRCH` for ids outside the process isn't measured, and the solosched signature is taken from the `sce` twin. ### Tested Linux (CachyOS), GCC 16, Release. - **`guest_available_cpumask`**: - own tid and -1, `getpid()` and -1, all giving the default mask; - a second thread pinned to the highest CPU while the main thread is pinned to the lowest: its tid gives its mask, the PID gives the union; - after that thread exits (before join) and after the join, the PID gives only the main thread's CPU and its tid gives `ESRCH`; - `ESRCH` for tid 1 and `getpid()+1`; - `EINVAL` for `which` 0/7 and level 0/4; - lookup errors before level errors (`ESRCH` for level 4/ROOT with an unknown tid); - level errors before `EFAULT`. - **`guest_pthread_attr`**: solosched round-trips 0 → 1 → 0 through `scePthreadAttrGetsolosched`, with `EINVAL` for null and destroyed attributes. - **On `main`** both tests stop: `pthread_attr_setsolosched_np` and `cpuset_getaffinity` for an explicit tid both reach `NotImplemented`. - **Negative checks**: each of these mutations makes a test fail (10 of 10): union → last thread, union or tid lookup including exited threads, the `ESRCH`/`EINVAL`/`EFAULT` codes, the PID check, the `which` check, the level check, solosched value and null check. - The two tests pass 30 runs in a row. All 231 `guest_` tests pass, and `tools/check_conventions.py` passes. ### Checklist - [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
github-actions[bot] commented 2026-10-10 23:56:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-10T23:56:06Z.


Progress report (e28fbc2 → cd03813)

📈 System libraries: 85.72% (+0.06%, +2 implemented functions)

✅ 2 implemented
Library Name
libkernel cpuset_getaffinity_nid_postfix
libkernel pthread_attr_setsolosched_np_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6103515014 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3168#issuecomment-6103515014). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-10T23:56:06Z. --- <!-- progress-report --> ### Progress report (e28fbc2 → cd03813) 📈 **System libraries**: 85.72% (+0.06%, +2 implemented functions) <details> <summary>✅ 2 implemented</summary> | Library | Name | | - | - | | libkernel | `cpuset_getaffinity_nid_postfix` | | libkernel | `pthread_attr_setsolosched_np_nid_postfix` | </details>
Dextroid17 commented 2026-10-11 00:04:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-11T00:04:38Z.

Review: COMMENTED


Two exports, and the more interesting one is documented the way a system call should be: cpuset_getaffinity now follows FreeBSD 11's sys_cpuset_getaffinity and cpuset_which, checks in the kernel's own order — ERANGE for the size, then EINVAL for an unknown which before ESRCH for an unknown id, then the level, then EFAULT for a null output — and the CPU_WHICH_PID union is FOREACH_THREAD_IN_PROC's behaviour rather than an approximation of it. Where the order decides which error comes back, following the source is the only defensible choice, and it is the detail a reader would otherwise have to check against the header themselves.

The 0x1fff figure being a console measurement is the strongest thing here. A libkernel contract is hard to establish from the emulator side, and having a patch number and a game's call to point at for the default mask puts the union case — the one that cannot be tested here — on something real rather than on an inference from FreeBSD.

The signature fix is right, and it is the convention this tree uses: a zero-argument stub becomes a real PthreadAttr* plus an int, an invalid attribute comes back as the guest EINVAL rather than an internal one, and the result goes through ToErrno so scePthreadAttrSetsolosched's code is translated exactly like its neighbours'.

And the union case is settled by the code rather than the console. For CPU_WHICH_PID the mask is built from the live threads' snapshot: GuestProcessAffinity folds in a thread's affinity only while its _finished flag is clear, and finishThread sets that flag before the join, so an exited thread stops contributing immediately, with no window in which a stopped thread's mask is still folded in. That is precisely what the console-side measurement cannot show, and it is worth one line in the entry for exactly that reason.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5481269098 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3168#pullrequestreview-5481269098). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-11T00:04:38Z. **Review:** COMMENTED --- Two exports, and the more interesting one is documented the way a system call should be: `cpuset_getaffinity` now follows FreeBSD 11's `sys_cpuset_getaffinity` and `cpuset_which`, checks in the kernel's own order — `ERANGE` for the size, then `EINVAL` for an unknown `which` before `ESRCH` for an unknown id, then the level, then `EFAULT` for a null output — and the `CPU_WHICH_PID` union is `FOREACH_THREAD_IN_PROC`'s behaviour rather than an approximation of it. Where the order decides which error comes back, following the source is the only defensible choice, and it is the detail a reader would otherwise have to check against the header themselves. **The `0x1fff` figure being a console measurement is the strongest thing here.** A libkernel contract is hard to establish from the emulator side, and having a patch number and a game's call to point at for the default mask puts the union case — the one that cannot be tested here — on something real rather than on an inference from FreeBSD. **The signature fix is right, and it is the convention this tree uses:** a zero-argument stub becomes a real `PthreadAttr*` plus an `int`, an invalid attribute comes back as the guest `EINVAL` rather than an internal one, and the result goes through `ToErrno` so `scePthreadAttrSetsolosched`'s code is translated exactly like its neighbours'. **And the union case is settled by the code rather than the console.** For `CPU_WHICH_PID` the mask is built from the live threads' snapshot: `GuestProcessAffinity` folds in a thread's affinity only while its `_finished` flag is clear, and `finishThread` sets that flag *before* the join, so an exited thread stops contributing immediately, with no window in which a stopped thread's mask is still folded in. That is precisely what the console-side measurement cannot show, and it is worth one line in the entry for exactly that reason. Written with AI assistance.
adriadam10 commented 2026-10-11 00:06:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T00:06:22Z.


Closing. The solosched half is the same change as screamm's branch from #3148, which has the title and log line (Returnal PPSA01285), so I'm carrying that commit instead in its own PR. For the cpuset_getaffinity half, the only title I found calling it is OpenNOW PS5 (PPSA99082, #2010). Its source calls cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1, 8, ...), which main already handles, so no title reaches the explicit-tid and PID paths yet. The branch is kept as backup/kernel-cpuset-solosched in case one does.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6103589420 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3168#issuecomment-6103589420). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T00:06:22Z. --- Closing. The solosched half is the same change as screamm's branch from #3148, which has the title and log line (Returnal PPSA01285), so I'm carrying that commit instead in its own PR. For the cpuset_getaffinity half, the only title I found calling it is OpenNOW PS5 (PPSA99082, #2010). Its source calls `cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1, 8, ...)`, which main already handles, so no title reaches the explicit-tid and PID paths yet. The branch is kept as `backup/kernel-cpuset-solosched` in case one does.

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