feat(libkernel): implement cpuset_setaffinity for the calling thread or process #3323

Merged
TheAggie merged 1 commit from upstream-pr/3323 into main 2026-10-11 07:57:16 +00:00
TheAggie commented 2026-10-11 07:36:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by TheAggie on 2026-10-11T07:36:59Z.


What

Implements cpuset_setaffinity (libkernel, NID 0Ma-LQjv6So) for the calling thread and the calling process.

XPSemu (PPSA97358, catalog ZIP sha256-checked) stops on it on main 5c7ceb07, now that sceKernelGetAppInfo (#3056) is merged:

FAIL: unresolved ELF import 0Ma-LQjv6So from libkernel.prx

The title calls it at 0xC7944B as cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, 8, &mask). On ERANGE it retries with sizes 16 and 32. Any other failure takes its error path.

The checks follow FreeBSD 11 sys_cpuset_setaffinity (sys/kern/kern_cpuset.c), in the same order:

  • Size: ERANGE outside 8 to 32 bytes, the same range cpuset_getaffinity already uses.
  • Mask pointer: EFAULT when it is null.
  • Bytes past the first 8: must be zero, else EINVAL.
  • Calling thread (CPU_WHICH_TID, id -1): sets that thread's mask.
  • Calling process (CPU_WHICH_PID, id -1 or getpid()): sets the mask of every guest thread that is alive.
  • Effective mask: the request ANDed with sceKernelGetAvailableCpumask. If nothing is left, EDEADLK.
  • Anything else: throws (NotImplemented_nid_no_patch).

FreeBSD gives the process one cpuset that later threads join. Here, a thread created after a process-level call still starts with its attribute's mask. That gap is listed in TechnicalDebt (Functional).

Tested

Windows 11 Pro (26200), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, on main 5c7ceb07.

  • guest_available_cpumask: extended with a thread that:

    • sets its own mask, with bits above the available set dropped;
    • checks EINVAL for a nonzero high byte, ERANGE for 4 and 33 bytes, EFAULT for a null mask, and EDEADLK for a mask with no available CPU;
    • sets the process mask and checks that a second, waiting thread picks it up.

    If the process path only sets the calling thread, the test fails.

  • Related tests: guest_pthread*, guest_thread* and cpu_topology all pass (18/18).

  • XPSemu: with this PR, the next stop is sigsetjmp (aRo9AhFUXcM).

Not tested on Linux.

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

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4817446161 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3323). Originally posted by [TheAggie](https://github.com/TheAggie) on 2026-10-11T07:36:59Z. --- ### What Implements `cpuset_setaffinity` (libkernel, NID `0Ma-LQjv6So`) for the calling thread and the calling process. XPSemu (PPSA97358, catalog ZIP sha256-checked) stops on it on `main` 5c7ceb07, now that `sceKernelGetAppInfo` (#3056) is merged: ``` FAIL: unresolved ELF import 0Ma-LQjv6So from libkernel.prx ``` The title calls it at `0xC7944B` as `cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, 8, &mask)`. On `ERANGE` it retries with sizes 16 and 32. Any other failure takes its error path. The checks follow FreeBSD 11 `sys_cpuset_setaffinity` (`sys/kern/kern_cpuset.c`), in the same order: - **Size:** `ERANGE` outside 8 to 32 bytes, the same range `cpuset_getaffinity` already uses. - **Mask pointer:** `EFAULT` when it is null. - **Bytes past the first 8:** must be zero, else `EINVAL`. - **Calling thread** (`CPU_WHICH_TID`, id -1): sets that thread's mask. - **Calling process** (`CPU_WHICH_PID`, id -1 or `getpid()`): sets the mask of every guest thread that is alive. - **Effective mask:** the request ANDed with `sceKernelGetAvailableCpumask`. If nothing is left, `EDEADLK`. - **Anything else:** throws (`NotImplemented_nid_no_patch`). FreeBSD gives the process one cpuset that later threads join. Here, a thread created after a process-level call still starts with its attribute's mask. That gap is listed in TechnicalDebt (Functional). ### Tested Windows 11 Pro (26200), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, on `main` 5c7ceb07. - **`guest_available_cpumask`:** extended with a thread that: - sets its own mask, with bits above the available set dropped; - checks `EINVAL` for a nonzero high byte, `ERANGE` for 4 and 33 bytes, `EFAULT` for a null mask, and `EDEADLK` for a mask with no available CPU; - sets the process mask and checks that a second, waiting thread picks it up. If the process path only sets the calling thread, the test fails. - **Related tests:** `guest_pthread*`, `guest_thread*` and `cpu_topology` all pass (18/18). - **XPSemu:** with this PR, the next stop is `sigsetjmp` (`aRo9AhFUXcM`). Not tested on Linux. ### 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 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-11 07:37:19 +00:00 (Migrated from github.com)

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


Progress report (2bb78d2 → bb4b938)

📉 System libraries: 85.8% (-0.03%, +1 declared functions)

🆕 1 declared as stubs
Library Name
libkernel cpuset_setaffinity_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6106745281 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3323#issuecomment-6106745281). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-11T07:37:19Z. --- <!-- progress-report --> ### Progress report (2bb78d2 → bb4b938) 📉 **System libraries**: 85.8% (-0.03%, +1 declared functions) <details> <summary>🆕 1 declared as stubs</summary> | Library | Name | | - | - | | libkernel | `cpuset_setaffinity_nid_postfix` | </details>
Mouhand-Kaddo commented 2026-10-11 07:52:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Mouhand-Kaddo on 2026-10-11T07:52:27Z.


Additional platform validation of standalone PR head bb4b938e34b704aec4030945caa91bad73de1997 (2026-10-11):

Linux: passed — Arch Linux in WSL2 (kernel 6.6.87.2), GCC 16.2.1 20260810; CMake 4.3.1 / Ninja; Release, BUILD_TESTING=ON.

19 checks, 0 failures, 0 CTest skips. Built the selected targets and dependencies; this is focused validation, not a full build or full-suite run.

Checks: guest_thread_callback_abi, cpu_topology, guest_thread_atexit, guest_pthread_barrier, guest_thread_self, guest_pthread_getname, guest_pthread_attr, guest_pthread_mutexattr, guest_pthread_start, guest_pthread_stack, guest_thread_cpu_clock, guest_pthread_sem, guest_pthread_cancel, guest_pthread_cancel_recovery, guest_pthread_cancel_recovery_coarse, guest_pthread_rwlock, guest_pthread_key, guest_available_cpumask, guest_pthread_rename.

Command after building: ctest --test-dir <build> --verbose --no-tests=error --timeout 30 -R "^(guest_available_cpumask$|guest_pthread|guest_thread|cpu_topology$)".

No title/gameplay validation. These results cover the standalone head, not a merge with current main.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6106861083 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3323#issuecomment-6106861083). Originally posted by [Mouhand-Kaddo](https://github.com/Mouhand-Kaddo) on 2026-10-11T07:52:27Z. --- <!-- anyps5-platform-validation:bb4b938e34b704aec4030945caa91bad73de1997 --> Additional platform validation of standalone PR head `bb4b938e34b704aec4030945caa91bad73de1997` (2026-10-11): **Linux: passed** — Arch Linux in WSL2 (kernel 6.6.87.2), GCC 16.2.1 20260810; CMake 4.3.1 / Ninja; Release, `BUILD_TESTING=ON`. 19 checks, 0 failures, 0 CTest skips. Built the selected targets and dependencies; this is focused validation, not a full build or full-suite run. Checks: `guest_thread_callback_abi`, `cpu_topology`, `guest_thread_atexit`, `guest_pthread_barrier`, `guest_thread_self`, `guest_pthread_getname`, `guest_pthread_attr`, `guest_pthread_mutexattr`, `guest_pthread_start`, `guest_pthread_stack`, `guest_thread_cpu_clock`, `guest_pthread_sem`, `guest_pthread_cancel`, `guest_pthread_cancel_recovery`, `guest_pthread_cancel_recovery_coarse`, `guest_pthread_rwlock`, `guest_pthread_key`, `guest_available_cpumask`, `guest_pthread_rename`. Command after building: `ctest --test-dir <build> --verbose --no-tests=error --timeout 30 -R "^(guest_available_cpumask$|guest_pthread|guest_thread|cpu_topology$)"`. No title/gameplay validation. These results cover the standalone head, not a merge with current main.
adriadam10 commented 2026-10-11 07:57:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T07:57:13Z.


Checked the build and ctest on Linux (guest_available_cpumask passes; without the change it doesn't link), the error order against FreeBSD kern_cpuset_setaffinity, and the TechnicalDebt entry (Functional, gap documented). Linux validation by @Mouhand-Kaddo also noted. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6106896362 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3323#issuecomment-6106896362). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T07:57:13Z. --- Checked the build and ctest on Linux (`guest_available_cpumask` passes; without the change it doesn't link), the error order against FreeBSD `kern_cpuset_setaffinity`, and the TechnicalDebt entry (Functional, gap documented). Linux validation by @Mouhand-Kaddo also noted. Merging.
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!3323
No description provided.