feat(libkernel_sys): finish sched_setscheduler and sigreturn #10838

Merged
EagleBearerYS merged 7 commits from upstream-pr/10838 into main 2026-10-27 12:20:32 +00:00
EagleBearerYS commented 2026-10-25 03:54:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-25T03:54:54Z.


What

Finishes two libkernel_sys calls:

  • sched_setscheduler follows FreeBSD 11's sys_sched_setscheduler: EFAULT, ESRCH for another process, EINVAL for an unknown policy. Pid 0 sets the calling thread through scePthreadSetschedparam.
  • sigreturn applies FreeBSD 11's amd64 sys_sigreturn and set_fpcontext checks and restores the context through the path setcontext uses.

Values the console's answer is unknown for still throw: a scheduler policy scePthreadSetschedparam rejects, the first thread by pid, a non-user %cs, an extended FPU state. The debt file has an entry for each.

chown, lchown and fchownat keep main's owner check, following the review of #8457 (comment, comment): the guest is modelled as root, and FreeBSD's root keeps the privilege to change a file's owner, so another owner or group still throws after the path lookup until a console measurement shows EPERM.

Related: #9187 (sched_getparam, sched_getscheduler, sched_setparam) and #9389 (sigqueue) each touch the line next to one this PR changes, so whichever merges second needs a one-line merge. #8457 and #10434 have merged, and this branch merges them.

Tested

  • GitHub's runners at 477c70fc01, this head (run, a CI-only commit on 477c70fc01): guest_kernel_sys, guest_user_context, guest_user_context_exit, guest_sched_priority, guest_semaphore and guest_pthread_sem pass 10 runs in a row, 6 run, 6 pass, 0 skip, 0 fail on Linux and on Windows. The libc and libkernel suites pass on both (143 and 206 tests on Linux, 170 and 222 on Windows), run with APS5_SKIP_TIMING_TESTS=1, which relaxes precise_wait's median bounds on GitHub's hosted runners.
  • GitHub's runners (run 40158841678, whose commit 5adf33a28f is a CI-only commit on 477c70fc01): guest_kernel_sys, guest_user_context, guest_user_context_exit, guest_sched_priority, guest_semaphore and guest_pthread_sem pass 10 of 10 on Linux and on Windows, 0 skipped. Main's implementation doesn't build against the new tests, because main declares sigreturn without parameters. The libc and libkernel suites pass on both.
  • Red/green per commit (Linux, Ubuntu 24.04, gcc 13.3, CI flags): each commit's new checks fail with its product change reverted and pass with it.

No title tested.

  • e9dd2e2f52, a merge of main 9a2020c220. The only conflict was docs/dev/debt/libkernel_sys.md, where #10434 (Retr0G28, linkat) rewrote the linkat entry next to this PR's chown entry; the merge keeps both, and the PR's own added and removed lines are unchanged. On GitHub's runners (run), guest_kernel_sys, guest_user_context, guest_user_context_exit, guest_sched_priority, guest_semaphore and guest_pthread_sem pass 10 runs in a row, 6 run, 6 pass, 0 skip, 0 fail on Linux and on Windows; main's code doesn't build against the new tests. The libc and libkernel suites pass on both (144 and 207 tests on Linux, 171 and 223 on Windows), run with APS5_SKIP_TIMING_TESTS=1, which relaxes precise_wait's median bounds on GitHub's hosted runners.
  • 8daa84f8cc, a merge of main 4e43321bd6 and the owner-check change (run). The merge (e5e06128e7) keeps both sides: #11186 extends the module-info debt entry next to this PR's two new entries; #8457 calls RequireGuestOwner from fchown, so that helper stays; and #9379's swapcontext keeps Aps5SwapcontextFinish in the extern "C" block beside setcontext, which this PR moves after its RestoreContext helper. 8daa84f8cc then puts chown, lchown and fchownat back on RequireGuestOwner, so their owner path is main's again; the EPERM assertions are gone, with no throw assertions in their place, and the lookup error checks stay. guest_kernel_sys, guest_kernel_sys_swapcontext, guest_user_context, guest_user_context_exit, guest_sched_priority, guest_semaphore and guest_pthread_sem pass 10 runs in a row; with main's implementation the test file doesn't build, as main declares sched_setscheduler with no parameters. Linux: 7 run, 7 pass, 0 skip, 0 fail. Windows: 7 run, 7 pass, 0 skip, 0 fail. Suites: libc and libkernel pass on Linux (145 and 210), and libc passes on Windows (172). Windows' libkernel suite runs 231 tests and fails guest_timed_join_import_libkernel_sys and guest_timed_join_import_libkernel_web, which this PR doesn't touch; main 88f99678bf fails the same two in this verify's Windows job (run). The suites ran with APS5_SKIP_TIMING_TESTS=1, which relaxes precise_wait's median bounds on GitHub's hosted runners.

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: #9187, #9389 (merge order only; this PR takes a one-line merge after each, see Related); merge order with Gruzzberry's #11032 (kenv, adjacent lines in core/libs/tests/GuestKernelSys.cpp): this PR first, then #11032 rebases onto it
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5001611056 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10838). Originally posted by [EagleBearerYS](https://github.com/EagleBearerYS) on 2026-10-25T03:54:54Z. --- ### What Finishes two libkernel_sys calls: - `sched_setscheduler` follows FreeBSD 11's `sys_sched_setscheduler`: `EFAULT`, `ESRCH` for another process, `EINVAL` for an unknown policy. Pid 0 sets the calling thread through `scePthreadSetschedparam`. - `sigreturn` applies FreeBSD 11's amd64 `sys_sigreturn` and `set_fpcontext` checks and restores the context through the path `setcontext` uses. Values the console's answer is unknown for still throw: a scheduler policy `scePthreadSetschedparam` rejects, the first thread by pid, a non-user `%cs`, an extended FPU state. The debt file has an entry for each. `chown`, `lchown` and `fchownat` keep `main`'s owner check, following the review of #8457 ([comment](https://github.com/boykopovar/AnyPS5/pull/8457#issuecomment-6293992394), [comment](https://github.com/boykopovar/AnyPS5/pull/8457#issuecomment-6375618867)): the guest is modelled as root, and FreeBSD's root keeps the privilege to change a file's owner, so another owner or group still throws after the path lookup until a console measurement shows `EPERM`. Related: #9187 (`sched_getparam`, `sched_getscheduler`, `sched_setparam`) and #9389 (`sigqueue`) each touch the line next to one this PR changes, so whichever merges second needs a one-line merge. #8457 and #10434 have merged, and this branch merges them. ### Tested - **GitHub's runners at 477c70fc01**, this head ([run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/40164018236), a CI-only commit on 477c70fc01): `guest_kernel_sys`, `guest_user_context`, `guest_user_context_exit`, `guest_sched_priority`, `guest_semaphore` and `guest_pthread_sem` pass 10 runs in a row, 6 run, 6 pass, 0 skip, 0 fail on Linux and on Windows. The libc and libkernel suites pass on both (143 and 206 tests on Linux, 170 and 222 on Windows), run with `APS5_SKIP_TIMING_TESTS=1`, which relaxes `precise_wait`'s median bounds on GitHub's hosted runners. - **GitHub's runners** ([run 40158841678](https://github.com/EagleBearerYS/AnyPS5/actions/runs/40158841678), whose commit 5adf33a28f is a CI-only commit on 477c70fc01): `guest_kernel_sys`, `guest_user_context`, `guest_user_context_exit`, `guest_sched_priority`, `guest_semaphore` and `guest_pthread_sem` pass 10 of 10 on Linux and on Windows, 0 skipped. Main's implementation doesn't build against the new tests, because main declares `sigreturn` without parameters. The libc and libkernel suites pass on both. - **Red/green per commit** (Linux, Ubuntu 24.04, gcc 13.3, CI flags): each commit's new checks fail with its product change reverted and pass with it. No title tested. - **e9dd2e2f52, a merge of `main` 9a2020c220.** The only conflict was `docs/dev/debt/libkernel_sys.md`, where #10434 (Retr0G28, `linkat`) rewrote the `linkat` entry next to this PR's `chown` entry; the merge keeps both, and the PR's own added and removed lines are unchanged. On GitHub's runners ([run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/40169445703)), `guest_kernel_sys`, `guest_user_context`, `guest_user_context_exit`, `guest_sched_priority`, `guest_semaphore` and `guest_pthread_sem` pass 10 runs in a row, 6 run, 6 pass, 0 skip, 0 fail on Linux and on Windows; `main`'s code doesn't build against the new tests. The libc and libkernel suites pass on both (144 and 207 tests on Linux, 171 and 223 on Windows), run with `APS5_SKIP_TIMING_TESTS=1`, which relaxes `precise_wait`'s median bounds on GitHub's hosted runners. - **8daa84f8cc, a merge of `main` 4e43321bd6 and the owner-check change** ([run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/40305167345)). The merge (e5e06128e7) keeps both sides: #11186 extends the module-info debt entry next to this PR's two new entries; #8457 calls `RequireGuestOwner` from `fchown`, so that helper stays; and #9379's swapcontext keeps `Aps5SwapcontextFinish` in the `extern "C"` block beside `setcontext`, which this PR moves after its `RestoreContext` helper. 8daa84f8cc then puts `chown`, `lchown` and `fchownat` back on `RequireGuestOwner`, so their owner path is `main`'s again; the `EPERM` assertions are gone, with no throw assertions in their place, and the lookup error checks stay. `guest_kernel_sys`, `guest_kernel_sys_swapcontext`, `guest_user_context`, `guest_user_context_exit`, `guest_sched_priority`, `guest_semaphore` and `guest_pthread_sem` pass 10 runs in a row; with `main`'s implementation the test file doesn't build, as `main` declares `sched_setscheduler` with no parameters. Linux: 7 run, 7 pass, 0 skip, 0 fail. Windows: 7 run, 7 pass, 0 skip, 0 fail. Suites: libc and libkernel pass on Linux (145 and 210), and libc passes on Windows (172). Windows' libkernel suite runs 231 tests and fails `guest_timed_join_import_libkernel_sys` and `guest_timed_join_import_libkernel_web`, which this PR doesn't touch; `main` 88f99678bf fails the same two in this verify's Windows job ([run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/40238050809)). The suites ran with `APS5_SKIP_TIMING_TESTS=1`, which relaxes `precise_wait`'s median bounds on GitHub's hosted runners. ### 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: #9187, #9389 (merge order only; this PR takes a one-line merge after each, see Related); merge order with Gruzzberry's #11032 (`kenv`, adjacent lines in `core/libs/tests/GuestKernelSys.cpp`): this PR first, then #11032 rebases onto it - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-25 04:05:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T04:05:11Z.

Review: DISMISSED


The Linux/Windows verification run is green (10/10 on each runner), and the chown and scheduler paths appear consistent with the cited FreeBSD behavior. I found one blocking gap in the new sigreturn restore path.

FreeBSD 11.4 sys_sigreturn copies the saved machine context into the trapframe and then restores mc_fsbase and mc_gsbase (machdep.c, lines 3149-3155). Here, SigreturnContext_nid_no_patch reaches RestoreContext, whose RegisterFrame contains neither bases nor the saved segment selectors; Aps5ContextRestore_nid_no_patch pushes the current %cs/%ss. As a result, a context with different fsbase, gsbase, or another RPL-3 code selector returns successfully while silently restoring the current host state instead. The debt text currently says those bases are restored as FreeBSD would.

Please route these values through a safe guest-context mechanism, or explicitly reject unsupported non-current values with NotImplemented_nid_no_patch until they can be represented. Add a regression that changes the bases and a valid-but-different user %cs so a successful return cannot silently discard them. The reference is FreeBSD 11.4 sys_sigreturn.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5641427953 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10838#pullrequestreview-5641427953). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T04:05:11Z. **Review:** DISMISSED --- The Linux/Windows verification run is green (10/10 on each runner), and the chown and scheduler paths appear consistent with the cited FreeBSD behavior. I found one blocking gap in the new `sigreturn` restore path. FreeBSD 11.4 `sys_sigreturn` copies the saved machine context into the trapframe and then restores `mc_fsbase` and `mc_gsbase` (`machdep.c`, lines 3149-3155). Here, `SigreturnContext_nid_no_patch` reaches `RestoreContext`, whose `RegisterFrame` contains neither bases nor the saved segment selectors; `Aps5ContextRestore_nid_no_patch` pushes the current `%cs`/`%ss`. As a result, a context with different `fsbase`, `gsbase`, or another RPL-3 code selector returns successfully while silently restoring the current host state instead. The debt text currently says those bases are restored as FreeBSD would. Please route these values through a safe guest-context mechanism, or explicitly reject unsupported non-current values with `NotImplemented_nid_no_patch` until they can be represented. Add a regression that changes the bases and a valid-but-different user `%cs` so a successful return cannot silently discard them. The reference is [FreeBSD 11.4 `sys_sigreturn`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/amd64/amd64/machdep.c#L3149-L3155).
MarcZX10 commented 2026-10-25 04:54:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T04:54:48Z.

Review: COMMENTED


Review note on exact-head verification for the new sigreturn path.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5641619932 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10838#pullrequestreview-5641619932). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T04:54:48Z. **Review:** COMMENTED --- Review note on exact-head verification for the new `sigreturn` path.
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!10838
No description provided.