Skip to content

fix(libkernel): follow the 13.60 kernel's cpuset affinity size and jail checks - #22325

Merged
oneandonlydean merged 2 commits into
boykopovar:mainfrom
fogydes:fix/libkernel-cpuset-getaffinity-size
Nov 17, 2026
Merged

oneandonlydean merged 2 commits into
boykopovar:mainfrom
fogydes:fix/libkernel-cpuset-getaffinity-size

Conversation

@fogydes

@fogydes fogydes commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

What

Follow the firmware 13.60 kernel's handlers for cpuset_getaffinity (syscall 487) and cpuset_setaffinity (syscall 488) in the cases that still threw or diverged:

  1. 17 to 23 byte sets in cpuset_getaffinity no longer throw: every size outside 8 to 16 bytes returns -1/ERANGE before any other check. Syscall 487's thunk (kernel-13.60 @ 0xffffffff9e655cc0) goes through 0xffffffff9ec2de60 to 0xffffffff9ec2dac0, which loads cpusetsize (@ 0xffffffff9ec2dae5), compares cpusetsize - 17 with -9 (@ 0xffffffff9ec2dae9..0xffffffff9ec2daf1) and returns r12d = 0x22 (set @ 0xffffffff9ec2dad8) at 0xffffffff9ec2dd66.
  2. Jail targets (CPU_WHICH_JAIL) return ESRCH from both calls at every level instead of throwing. Both handlers call the target lookup at 0xffffffff9ec2c5e0: getaffinity from 0xffffffff9ec2db28, setaffinity's core (0xffffffff9ec2de90, reached from 488 through 0xffffffff9ec2e320) from 0xffffffff9ec2e10e (ROOT/CPUSET) and 0xffffffff9ec2e184 (WHICH), after its size check, copyin and check on the bytes past 8. The lookup's jail case (0xffffffff9ec2c67e) passes the caller's cr_prison and the id to 0xffffffff9efd9ae0. That walk starts at the prison's first child and never compares the caller's own prison, so a miss returns 3 (ESRCH) at 0xffffffff9ec2c81a. A title can't create jails, so every id misses.
  3. cpuset_getaffinity null mask: a null mask now gives EFAULT after the target and level errors, as the handler copies out last. A missing thread with a null mask now answers ESRCH.

Still throwing: numbered sets, WHICH interrupt and domain masks, the ROOT/CPUSET and WHICH-cpuset setters. The dump settles their lookup, but not the PS5 set and interrupt IDs a title would see. cpuset_getid's jail case uses the same lookup and should answer ESRCH too. I left it out because #21987 (open) rewrites that function's debt entry, and two fragments can't change one entry; it can follow once #21987 lands.

Debt (docs/dev/debt/changes/fix-libkernel-cpuset-getaffinity-size.md):

  • the cpuset_setaffinity and cpuset_getaffinity entries are rewritten with the kernel addresses, each under 1000 characters;
  • the stale "accept up to 32 bytes" entry is removed (both calls already returned ERANGE from 24 bytes on main).

Reachability: cpuset_getaffinity is imported by 3 homebrew titles in the import sweep (cpuset_setaffinity by 5), and ProsperoWeb (PRWB00001) calls it. No title is known to pass these exact arguments; this removes throws from imported exports, it doesn't unblock a known stop.

Overlap:

Tested

  • Windows 11 x64, MinGW GCC 15.2 / Ninja, head b761169 on main cb28e72
  • guest_available_cpumask and guest_kernel_sys_cpuset: 2/2 passed. New cases:
    • 17, 20, 23 and 24 byte sets return ERANGE, leave the buffer untouched, and do so with a null mask too;
    • jail ids -1, 0 and 1 at levels 1 to 3 return ESRCH from both calls, with no output written and before a null-mask EFAULT;
    • a missing thread with a null mask returns ESRCH, and level 0 with a null mask returns EINVAL.
  • Negative controls:
    • with main's Thread.cpp, guest_available_cpumask fails (cpuset_getaffinity with a size of 17 to 23 bytes not implemented);
    • with the size fix alone, it still fails on the jail cases;
    • the final code passes.
  • python tools/check_conventions.py --base origin/main: all checks passed; python tools/debt_fold.py --check: exit 0
  • Progress: libkernel unchanged at 712/751 done, because both calls keep the throwing paths listed above
  • Linux not run

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

The firmware 13.60 kernel's cpuset_getaffinity handler accepts set sizes
from 8 to 16 bytes and returns ERANGE for every other size before its
other checks, so the 17 to 23 byte range no longer throws.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@fogydes fogydes changed the title fix(libkernel): return ERANGE for 17 to 23 byte cpuset_getaffinity sets fix(libkernel): follow the 13.60 kernel's cpuset size, jail and level checks Nov 17, 2026
The firmware 13.60 kernel looks a jail up only among the caller's
descendant jails, so cpuset_getaffinity and cpuset_setaffinity answer a
jail target with ESRCH. cpuset_getaffinity also reports target and level
errors before a null mask, as the handler copies out last.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@fogydes
fogydes force-pushed the fix/libkernel-cpuset-getaffinity-size branch from f385019 to b761169 Compare November 17, 2026 12:27
@fogydes fogydes changed the title fix(libkernel): follow the 13.60 kernel's cpuset size, jail and level checks fix(libkernel): follow the 13.60 kernel's cpuset affinity size and jail checks Nov 17, 2026

@PrivacyOnion PrivacyOnion left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against b76116919c:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_available_cpumask_tests built cleanly.
  • CTest (guest_available_cpumask):
    1/1 Test #5439: guest_available_cpumask ..........   Passed    0.02 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libkernel/Pthread/Posix/Thread.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #5439: guest_available_cpumask ..........Subprocess aborted***Exception:   0.47 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified cpuset_getaffinity_nid_postfix size boundary check: 'if (size < sizeof(KernelCpumask) || size > 16) return fail(GuestErange);' correctly returns ERANGE for size < 8 and size in [17, 23] as documented for 13.60 kernel behavior.
    • Verified cpuset_getaffinity_nid_postfix error ordering: size check (ERANGE) -> target lookup (WhichJail returns ESRCH, WhichThread/WhichProcess checks) -> level check -> null mask check (EFAULT) -> GuestCopyOut. null mask with missing thread or jail correctly yields ESRCH before EFAULT.
    • Verified cpuset_setaffinity_nid_postfix WhichJail handling: returns ESRCH at LevelRoot, LevelCpuset, and LevelWhich after size check and copyin/zero-tail validation.
    • Checked technical debt fragment in docs/dev/debt/changes/fix-libkernel-cpuset-getaffinity-size.md: correctly formats change and remove sections per TechnicalDebt.md rules without introducing untracked or non-debt files.
    • Verified unit tests in core/libs/tests/GuestAvailableCpumask.cpp: exercises sizes 17, 20, 23, 24 with valid and null masks, invalid id/level ordering with null mask, and WhichJail across levels 1..3 with get and set affinity.

Looks Good To Me

@oneandonlydean
oneandonlydean merged commit 99d95b9 into boykopovar:main Nov 17, 2026
14 of 16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants