feat(libkernel): implement cpuset_getaffinity for any thread or the process and pthread_attr_setsolosched_np #3168
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3168"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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 hitNotImplemented. It now follows FreeBSD 11sys_cpuset_getaffinityandcpuset_which(sys/kern/kern_cpuset.c), checking in the same order:ERANGEfor the size (unchanged).EINVALfor an unknownwhich, thenESRCHfor an unknown id.EINVALfor an unknown level.EFAULTfor 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 orgetpid()→ the union of the live threads' masks, as FreeBSD'sFOREACH_THREAD_IN_PROCdoes. That is0x1fffwhen the threads keep the default, matching what prospero-win measured on a console (patch 0881: a game'scpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1)is0x1fff).ESRCH, asp_canseedoes for a process outside the caller's jail.CPU_LEVEL_ROOT/CPU_LEVEL_CPUSETand 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)asscePthreadAttrSetsoloscheddoes (KytyPS5), returnsEINVALfor a null or destroyed attribute like the other POSIX attribute setters, and stores the value forscePthreadAttrGetsolosched.TechnicalDebt has a line for each under "Unknown function info": the
ESRCHfor ids outside the process isn't measured, and the solosched signature is taken from thescetwin.Tested
Linux (CachyOS), GCC 16, Release.
guest_available_cpumask:getpid()and -1, all giving the default mask;ESRCH;ESRCHfor tid 1 andgetpid()+1;EINVALforwhich0/7 and level 0/4;ESRCHfor level 4/ROOT with an unknown tid);EFAULT.guest_pthread_attr: solosched round-trips 0 → 1 → 0 throughscePthreadAttrGetsolosched, withEINVALfor null and destroyed attributes.mainboth tests stop:pthread_attr_setsolosched_npandcpuset_getaffinityfor an explicit tid both reachNotImplemented.ESRCH/EINVAL/EFAULTcodes, the PID check, thewhichcheck, the level check, solosched value and null check.guest_tests pass, andtools/check_conventions.pypasses.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)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
cpuset_getaffinity_nid_postfixpthread_attr_setsolosched_np_nid_postfixImported 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_getaffinitynow follows FreeBSD 11'ssys_cpuset_getaffinityandcpuset_which, checks in the kernel's own order —ERANGEfor the size, thenEINVALfor an unknownwhichbeforeESRCHfor an unknown id, then the level, thenEFAULTfor a null output — and theCPU_WHICH_PIDunion isFOREACH_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
0x1ffffigure 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 anint, an invalid attribute comes back as the guestEINVALrather than an internal one, and the result goes throughToErrnososcePthreadAttrSetsolosched's code is translated exactly like its neighbours'.And the union case is settled by the code rather than the console. For
CPU_WHICH_PIDthe mask is built from the live threads' snapshot:GuestProcessAffinityfolds in a thread's affinity only while its_finishedflag is clear, andfinishThreadsets 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.
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 asbackup/kernel-cpuset-soloschedin case one does.Pull request closed