feat(libkernel): implement sched_getparam, sched_getscheduler and sched_setparam for the calling thread #9187

Merged
slider merged 6 commits from upstream-pr/9187 into main 2026-10-27 01:57:33 +00:00
slider commented 2026-10-21 14:43:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by slider on 2026-10-21T14:43:01Z.


What

Implements sched_getparam, sched_getscheduler and sched_setparam in libkernel for the calling thread (pid 0), following FreeBSD 11.4's sys/kern/p1003_1b.c:

  • sched_getparam(0, param) and sched_getscheduler(0) report the calling thread's priority and policy, the values pthread_getschedparam reports. They use the 256-767 scale that the PS5-measured sched_get_priority_max and sched_get_priority_min report.
  • sched_setparam(0, param) keeps the thread's policy and sets its priority through scePthreadSetprio, as FreeBSD's ksched_setparam keeps the policy and calls ksched_setscheduler. It accepts the priority ranges pthread_setschedparam accepts.
  • Errors follow FreeBSD's order:
    • sched_setparam copies the parameter in first, so a null pointer gives EFAULT before the pid is checked.
    • The getters check the pid first.
    • A pid other than 0 and the process's own fails with ESRCH, as getpriority already does here.
  • Removes the three NotImplemented placeholders from libkernel_sys/Export.Discovery.cpp (duplicate-export rule). Imports bind by NID.
  • Debt (docs/dev/debt/libkernel.md), each case throwing instead of guessing:
    • the process's own pid: FreeBSD then targets the process's first thread, which is not tracked;
    • a priority outside the policy's ranges: FreeBSD answers EPERM, the PS5's pthread layer EINVAL, and the console's answer is unverified.

Left out on purpose:

  • sched_setscheduler: FreeBSD requires PRIV_SCHED_SET, and whether a title holds it on the console is unverified.
  • sched_rr_get_interval: the value depends on the console's scheduler tuning.

Merges cleanly (git merge-tree) with the open boykopovar/AnyPS5#8373, boykopovar/AnyPS5#8890, boykopovar/AnyPS5#8987, boykopovar/AnyPS5#9141 and boykopovar/AnyPS5#9167, which edit the same files.

Tested

Linux (Ubuntu 24.04, GCC 13.3, Release):

  • New guest_sched_params passes. It covers agreement with pthread_getschedparam, the policy kept across sched_setparam, and both priority ranges and their edges. It also checks that a change made by another thread does not affect the caller, ESRCH for foreign pids, EFAULT and its order, and the throws.
  • guest_pthread_schedparam and guest_kernel_sys pass.
  • Each of three mutations makes the new test fail: treating the own pid as the caller, not storing the priority, and answering foreign pids.
  • tools/check_conventions.py --base origin/main passes.

Windows: the changed sources and the test pass a syntax-only check with MinGW-w64 GCC 13 (not the supported 15.2); not built or 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
  • 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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4953450525 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9187). Originally posted by [slider](https://github.com/slider) on 2026-10-21T14:43:01Z. --- ### What Implements `sched_getparam`, `sched_getscheduler` and `sched_setparam` in libkernel for the calling thread (pid 0), following FreeBSD 11.4's `sys/kern/p1003_1b.c`: - `sched_getparam(0, param)` and `sched_getscheduler(0)` report the calling thread's priority and policy, the values `pthread_getschedparam` reports. They use the 256-767 scale that the PS5-measured `sched_get_priority_max` and `sched_get_priority_min` report. - `sched_setparam(0, param)` keeps the thread's policy and sets its priority through `scePthreadSetprio`, as FreeBSD's `ksched_setparam` keeps the policy and calls `ksched_setscheduler`. It accepts the priority ranges `pthread_setschedparam` accepts. - Errors follow FreeBSD's order: - `sched_setparam` copies the parameter in first, so a null pointer gives `EFAULT` before the pid is checked. - The getters check the pid first. - A pid other than 0 and the process's own fails with `ESRCH`, as `getpriority` already does here. - Removes the three `NotImplemented` placeholders from `libkernel_sys/Export.Discovery.cpp` (duplicate-export rule). Imports bind by NID. - Debt (`docs/dev/debt/libkernel.md`), each case throwing instead of guessing: - the process's own pid: FreeBSD then targets the process's first thread, which is not tracked; - a priority outside the policy's ranges: FreeBSD answers `EPERM`, the PS5's pthread layer `EINVAL`, and the console's answer is unverified. Left out on purpose: - `sched_setscheduler`: FreeBSD requires `PRIV_SCHED_SET`, and whether a title holds it on the console is unverified. - `sched_rr_get_interval`: the value depends on the console's scheduler tuning. Merges cleanly (`git merge-tree`) with the open boykopovar/AnyPS5#8373, boykopovar/AnyPS5#8890, boykopovar/AnyPS5#8987, boykopovar/AnyPS5#9141 and boykopovar/AnyPS5#9167, which edit the same files. ### Tested Linux (Ubuntu 24.04, GCC 13.3, Release): - New `guest_sched_params` passes. It covers agreement with `pthread_getschedparam`, the policy kept across `sched_setparam`, and both priority ranges and their edges. It also checks that a change made by another thread does not affect the caller, `ESRCH` for foreign pids, `EFAULT` and its order, and the throws. - `guest_pthread_schedparam` and `guest_kernel_sys` pass. - Each of three mutations makes the new test fail: treating the own pid as the caller, not storing the priority, and answering foreign pids. - `tools/check_conventions.py --base origin/main` passes. Windows: the changed sources and the test pass a syntax-only check with MinGW-w64 GCC 13 (not the supported 15.2); not built or run. ### 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 under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
harisibrar1 commented 2026-10-22 04:12:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-22T04:12:32Z.

Review: COMMENTED


Checked against main 6ef5a1c4c and FreeBSD 11.4 sys/kern/ksched.c / p1003_1b.c:

  • sched_setparam accepts 896-959 (Pthread/Posix/Thread.cpp:243 reuses the pthread-layer AcceptsSchedParam from line 48). In FreeBSD, ksched_setparam goes to ksched_setscheduler, which for FIFO/RR accepts only P1B_PRIO_MIN..P1B_PRIO_MAX and returns EPERM otherwise (ksched.c:178-188). Those are the constants ksched_get_priority_min/max report, and main's sched_get_priority_max returns 767 (System/src/Process.cpp:474-479). So sched_setparam(0, {896}) reporting success contradicts both the source and the PR's own debt text ("on the 256-767 scale of sched_get_priority_max").
  • Suggested fix: bound sched_setparam by sched_get_priority_min_nid_postfix(policy)/sched_get_priority_max_nid_postfix(policy) and throw for 896-959 like 255/768, since the console's answer is unverified. Then move 896 and 959 in GuestSchedParams.cpp:55 from the success loop to the throw loop.
  • Related: main's rtprio_thread debt entry treats the pthread-priority-to-kernel mapping as unmeasured. By the same reasoning, sched_getparam for a thread at 896-959 (set via pthread_setschedparam) reports a value outside the kernel range, and could throw rather than pass it through.
  • The rest matches the source as far as I can see: the EFAULT-before-ESRCH order in sched_setparam, pid-first in the getters, ESRCH for foreign pids consistent with getpriority, the throw for the own pid (FIRST_THREAD_IN_PROC is not tracked), and the policy kept across sched_setparam.
  • Builds on Windows (WinLibs GCC 15.2.0 r7, full BUILD_TESTING=ON build) merged onto main 6ef5a1c4c; the only failing step is the existing ImageArrayWindowPastLayerLimit.cpp alignment error that #8310 fixes. Not run here: this machine's Smart App Control blocks freshly built test executables.

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5609168838 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9187#pullrequestreview-5609168838). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-22T04:12:32Z. **Review:** COMMENTED --- Checked against main 6ef5a1c4c and FreeBSD 11.4 `sys/kern/ksched.c` / `p1003_1b.c`: - `sched_setparam` accepts 896-959 (`Pthread/Posix/Thread.cpp:243` reuses the pthread-layer `AcceptsSchedParam` from line 48). In FreeBSD, `ksched_setparam` goes to `ksched_setscheduler`, which for FIFO/RR accepts only `P1B_PRIO_MIN..P1B_PRIO_MAX` and returns EPERM otherwise (ksched.c:178-188). Those are the constants `ksched_get_priority_min`/`max` report, and main's `sched_get_priority_max` returns 767 (`System/src/Process.cpp:474-479`). So `sched_setparam(0, {896})` reporting success contradicts both the source and the PR's own debt text ("on the 256-767 scale of `sched_get_priority_max`"). - Suggested fix: bound `sched_setparam` by `sched_get_priority_min_nid_postfix(policy)`/`sched_get_priority_max_nid_postfix(policy)` and throw for 896-959 like 255/768, since the console's answer is unverified. Then move 896 and 959 in `GuestSchedParams.cpp:55` from the success loop to the throw loop. - Related: main's `rtprio_thread` debt entry treats the pthread-priority-to-kernel mapping as unmeasured. By the same reasoning, `sched_getparam` for a thread at 896-959 (set via `pthread_setschedparam`) reports a value outside the kernel range, and could throw rather than pass it through. - The rest matches the source as far as I can see: the EFAULT-before-ESRCH order in `sched_setparam`, pid-first in the getters, ESRCH for foreign pids consistent with `getpriority`, the throw for the own pid (`FIRST_THREAD_IN_PROC` is not tracked), and the policy kept across `sched_setparam`. - Builds on Windows (WinLibs GCC 15.2.0 r7, full `BUILD_TESTING=ON` build) merged onto main 6ef5a1c4c; the only failing step is the existing `ImageArrayWindowPastLayerLimit.cpp` alignment error that #8310 fixes. Not run here: this machine's Smart App Control blocks freshly built test executables. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
MarcZX10 commented 2026-10-22 07:49:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T07:49:26Z.

Review: CHANGES_REQUESTED


I independently reviewed this head and agree with the priority-range concern already raised above. The implementation's AcceptsSchedParam accepts 896–959, and GuestSchedParams.cpp asserts that sched_setparam(0,...) succeeds for 896 and 959. On current main, sched_get_priority_min/max reports 256–767. The PR describes that same 256–767 scale, while FreeBSD 11.4's ksched_setparam validates through ksched_setscheduler and rejects priorities outside the reported policy range. Please either provide console evidence that establishes 896–959 as valid for this API and align the documented range, or keep those values out of the success path (and test them as unverified/rejected). The separate pthread API accepting a value does not establish that sched_setparam must accept it. I built guest_sched_params_tests from this head in Release mode successfully. I then ran ctest --test-dir build -R '^guest_sched_params$' --output-on-failure; it aborts before test assertions because this host's Intel Xeon E5-1620 v2 lacks LZCNT, BMI1, BMI2, MOVBE, AVX2, and FMA required by the project. The hosted product build/test jobs are also skipped, so there is no passing execution of this PR's focused test available in the checks I could run. One repository item also needs attention before this is merge-ready: this branch is 596 commits behind current main. Please update it and rerun the overlap/conventions checks; the existing overlap report still lists closed #9369 and should be refreshed. I did not approve because the success-path range remains unsupported and the required test has not executed on a capable host.

I’ve noticed that holding this PR because of #5239 isn’t necessary. It’s fine for PRs to keep coming in; they’ll be handled in due course. Please disregard my earlier request to hold or defer it.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5610855450 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9187#pullrequestreview-5610855450). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T07:49:26Z. **Review:** CHANGES_REQUESTED --- I independently reviewed this head and agree with the priority-range concern already raised above. The implementation's `AcceptsSchedParam` accepts 896–959, and `GuestSchedParams.cpp` asserts that `sched_setparam(0,...)` succeeds for 896 and 959. On current `main`, `sched_get_priority_min/max` reports 256–767. The PR describes that same 256–767 scale, while FreeBSD 11.4's `ksched_setparam` validates through `ksched_setscheduler` and rejects priorities outside the reported policy range. Please either provide console evidence that establishes 896–959 as valid for this API and align the documented range, or keep those values out of the success path (and test them as unverified/rejected). The separate pthread API accepting a value does not establish that `sched_setparam` must accept it. I built `guest_sched_params_tests` from this head in Release mode successfully. I then ran `ctest --test-dir build -R '^guest_sched_params$' --output-on-failure`; it aborts before test assertions because this host's Intel Xeon E5-1620 v2 lacks LZCNT, BMI1, BMI2, MOVBE, AVX2, and FMA required by the project. The hosted product build/test jobs are also skipped, so there is no passing execution of this PR's focused test available in the checks I could run. One repository item also needs attention before this is merge-ready: this branch is 596 commits behind current `main`. Please update it and rerun the overlap/conventions checks; the existing overlap report still lists closed #9369 and should be refreshed. I did not approve because the success-path range remains unsupported and the required test has not executed on a capable host. I’ve noticed that holding this PR because of #5239 isn’t necessary. It’s fine for PRs to keep coming in; they’ll be handled in due course. Please disregard my earlier request to hold or defer it.
slider commented 2026-10-22 09:16:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by slider on 2026-10-22T09:16:45Z.


Thanks @harisibrar1 and @MarcZX10. In https://github.com/boykopovar/AnyPS5/pull/9187/commits/679f42be111946d2ad70993a82caf840341ee31f:

  • sched_setparam now accepts only sched_get_priority_min..sched_get_priority_max (256-767) and throws outside it, 896-959 included, where FreeBSD's ksched answers EPERM.
  • sched_getparam throws for a thread pthread_setschedparam put in 896-959, rather than reporting a value outside the reported scale.
  • The test moves 896 and 959 to the throw loop and adds that case; the debt entry is updated.
  • Rebased onto current main.

guest_sched_params, guest_pthread_schedparam and guest_kernel_sys pass on Linux; the changed files pass a MinGW syntax check. I don't have a title that calls these, so Unblocks stays none. Fine with it waiting behind title work per #5239.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6337215605 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9187#issuecomment-6337215605). Originally posted by [slider](https://github.com/slider) on 2026-10-22T09:16:45Z. --- Thanks @harisibrar1 and @MarcZX10. In https://github.com/boykopovar/AnyPS5/pull/9187/commits/679f42be111946d2ad70993a82caf840341ee31f: - `sched_setparam` now accepts only `sched_get_priority_min`..`sched_get_priority_max` (256-767) and throws outside it, 896-959 included, where FreeBSD's ksched answers EPERM. - `sched_getparam` throws for a thread `pthread_setschedparam` put in 896-959, rather than reporting a value outside the reported scale. - The test moves 896 and 959 to the throw loop and adds that case; the debt entry is updated. - Rebased onto current main. `guest_sched_params`, `guest_pthread_schedparam` and `guest_kernel_sys` pass on Linux; the changed files pass a MinGW syntax check. I don't have a title that calls these, so `Unblocks` stays none. Fine with it waiting behind title work per #5239.
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!9187
No description provided.