feat(libkernel): implement swapcontext #9379

Merged
DavidSamir merged 4 commits from upstream-pr/9379 into main 2026-10-25 15:16:49 +00:00
DavidSamir commented 2026-10-21 19:47:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-21T19:47:38Z.


What

Implements swapcontext in libkernel and removes the libkernel_sys discovery stub that threw. In FreeBSD 11, swapcontext on amd64 is the system call sys_swapcontext in sys/kern/kern_context.c; libc exports it as _swapcontext and __sys_swapcontext, and there is no lib/libc/gen version in 11. This follows sys_swapcontext:

  • Arguments: EINVAL for a NULL oucp or ucp.
  • Saving: oucp gets the current machine context, with the return value cleared (GET_MC_CLEAR_RET), and the signal mask. The copy runs up to uc_link (UC_COPY_SIZE), so uc_flags, uc_link and uc_stack are not touched.
  • Loading: it then loads ucp. An invalid context gives EINVAL after oucp has been written, as in FreeBSD. It sets the signal mask and continues in ucp.
  • Resuming: resuming oucp later returns 0 from swapcontext.

How:

  • libkernel/System/src/UserContext.cpp:
    • The frame capture of getcontext moves, unchanged, into APS5_CONTEXT_CAPTURE / APS5_CONTEXT_RETURN.
    • A new entry, swapcontext_nid_postfix, uses the same capture and calls Aps5SwapcontextFinish_nid_no_patch. That is Aps5GetcontextFinish_nid_no_patch on oucp, then setcontext_nid_postfix(ucp).
    • So the saved rip and rsp are those of swapcontext's caller, exactly as for getcontext.
  • libkernel_sys/Export.Discovery.cpp: the stub is removed.

Moved to libkernel so libkernel imports reach it on Windows. The PS5 3.20 export lists have swapcontext in libkernel as well as libkernel_sys, and on Windows a libkernel import never searches libkernel_sys (WindowsEntryStubBuilder.cpp:175). So libkernel exports it directly, replacing the earlier libkernel_sys jump stub, and the duplicate-export rule holds.

Tested

Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, Release. This is a CPU-side export, so no GPU is involved.

New test guest_kernel_sys_swapcontext, which covers:

  • Arguments: the two EINVAL cases, and an invalid ucp that gives EINVAL with oucp already written.
  • Round trips: a makecontext coroutine. The main side swaps into it three times and resumes once more for the coroutine's final return through uc_link.
  • Registers: the main side swaps through a helper that loads fixed values into rbx, rbp and r12–r15 and checks them when swapcontext returns. The coroutine swaps back through a helper that overwrites all of them first.
  • Signal mask: the mask swaps with the context. SIGUSR1 is blocked on the main side, unblocked in the coroutine, and blocked again on return.

Results:

  • It passes, 3 of 3 runs, and guest_user_context (both tests) still passes on the moved capture. tools/check_conventions.py passes, and there are no warnings.
  • Link control: with the entry renamed, the test fails to link (undefined reference to swapcontext_nid_postfix), so it reaches libkernel's export, which is the only one.
  • Negative controls:
    • without the oucp capture, the test fails at line 144;
    • with the saved rbx corrupted, it fails at line 159;
    • with the saved signal mask lost, it fails at line 160.
  • Overlap: tools/pr_overlap.py crashes on this machine's partial clone, so I ran git merge-tree against every open PR that touches these files, including #6879, the other open PR on UserContext.cpp. It merges cleanly with all of them; #6879's conflicts with main are in other files.

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:4957419067 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9379). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-21T19:47:38Z. --- ### What Implements `swapcontext` in libkernel and removes the `libkernel_sys` discovery stub that threw. In FreeBSD 11, `swapcontext` on amd64 is the system call `sys_swapcontext` in `sys/kern/kern_context.c`; libc exports it as `_swapcontext` and `__sys_swapcontext`, and there is no `lib/libc/gen` version in 11. This follows `sys_swapcontext`: - **Arguments:** `EINVAL` for a NULL `oucp` or `ucp`. - **Saving:** `oucp` gets the current machine context, with the return value cleared (`GET_MC_CLEAR_RET`), and the signal mask. The copy runs up to `uc_link` (`UC_COPY_SIZE`), so `uc_flags`, `uc_link` and `uc_stack` are not touched. - **Loading:** it then loads `ucp`. An invalid context gives `EINVAL` after `oucp` has been written, as in FreeBSD. It sets the signal mask and continues in `ucp`. - **Resuming:** resuming `oucp` later returns 0 from `swapcontext`. How: - **`libkernel/System/src/UserContext.cpp`:** - The frame capture of `getcontext` moves, unchanged, into `APS5_CONTEXT_CAPTURE` / `APS5_CONTEXT_RETURN`. - A new entry, `swapcontext_nid_postfix`, uses the same capture and calls `Aps5SwapcontextFinish_nid_no_patch`. That is `Aps5GetcontextFinish_nid_no_patch` on `oucp`, then `setcontext_nid_postfix(ucp)`. - So the saved `rip` and `rsp` are those of `swapcontext`'s caller, exactly as for `getcontext`. - **`libkernel_sys/Export.Discovery.cpp`:** the stub is removed. **Moved to libkernel so libkernel imports reach it on Windows.** The PS5 3.20 export lists have `swapcontext` in libkernel as well as libkernel_sys, and on Windows a libkernel import never searches libkernel_sys (`WindowsEntryStubBuilder.cpp:175`). So libkernel exports it directly, replacing the earlier libkernel_sys jump stub, and the duplicate-export rule holds. ### Tested Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, Release. This is a CPU-side export, so no GPU is involved. New test `guest_kernel_sys_swapcontext`, which covers: - **Arguments:** the two `EINVAL` cases, and an invalid `ucp` that gives `EINVAL` with `oucp` already written. - **Round trips:** a `makecontext` coroutine. The main side swaps into it three times and resumes once more for the coroutine's final return through `uc_link`. - **Registers:** the main side swaps through a helper that loads fixed values into `rbx`, `rbp` and `r12`–`r15` and checks them when `swapcontext` returns. The coroutine swaps back through a helper that overwrites all of them first. - **Signal mask:** the mask swaps with the context. SIGUSR1 is blocked on the main side, unblocked in the coroutine, and blocked again on return. Results: - It passes, 3 of 3 runs, and `guest_user_context` (both tests) still passes on the moved capture. `tools/check_conventions.py` passes, and there are no warnings. - Link control: with the entry renamed, the test fails to link (`undefined reference to swapcontext_nid_postfix`), so it reaches libkernel's export, which is the only one. - Negative controls: - without the `oucp` capture, the test fails at line 144; - with the saved `rbx` corrupted, it fails at line 159; - with the saved signal mask lost, it fails at line 160. - Overlap: `tools/pr_overlap.py` crashes on this machine's partial clone, so I ran `git merge-tree` against every open PR that touches these files, including #6879, the other open PR on `UserContext.cpp`. It merges cleanly with all of them; #6879's conflicts with `main` are in other files. ### 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
xXPBSXx commented 2026-10-21 20:41:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-21T20:41:31Z.


Independent Windows run of this PR (data only).

Environment: Windows 10 Pro 10.0.19045, Ryzen 7 5800X3D, WinLibs GCC 15.2.0 r7 (MinGW-w64 UCRT), Release, BUILD_TESTING=ON. Merge-tree of the PR head against main is clean. I built only guest_kernel_sys_swapcontext_tests; ctest -N -R guest_kernel_sys_swapcontext lists exactly one test.

PR head tested: c643263717a7c979cd79fc7b5b0a9781967d2083.

Results (3 runs each):

  • PR head: guest_kernel_sys_swapcontext passes 3/3.
  • Baseline: I kept the PR's test and replaced only these production files: core/libs/prx/libkernel/System/src/UserContext.cpp, core/libs/prx/libkernel_sys/Export.Discovery.cpp with the version from the PR's merge base (3594eee9f3), then rebuilt. The test fails 3/3 (exit 0xc0000409). I restored the PR's files afterwards.

GPU: the run output has no Selected GPU line; no GPU claim is made.

Not run: other platforms, the other tests in the same family, the full repository CTest, and any title or console run.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6326089323 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9379#issuecomment-6326089323). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-21T20:41:31Z. --- Independent Windows run of this PR (data only). **Environment:** Windows 10 Pro 10.0.19045, Ryzen 7 5800X3D, WinLibs GCC 15.2.0 r7 (MinGW-w64 UCRT), Release, BUILD_TESTING=ON. Merge-tree of the PR head against `main` is clean. I built only `guest_kernel_sys_swapcontext_tests`; `ctest -N -R guest_kernel_sys_swapcontext` lists exactly one test. **PR head tested:** `c643263717a7c979cd79fc7b5b0a9781967d2083`. **Results (3 runs each):** - PR head: `guest_kernel_sys_swapcontext` passes 3/3. - Baseline: I kept the PR's test and replaced only these production files: `core/libs/prx/libkernel/System/src/UserContext.cpp`, `core/libs/prx/libkernel_sys/Export.Discovery.cpp` with the version from the PR's merge base (`3594eee9f3`), then rebuilt. The test fails 3/3 (exit 0xc0000409). I restored the PR's files afterwards. **GPU:** the run output has no `Selected GPU` line; no GPU claim is made. **Not run:** other platforms, the other tests in the same family, the full repository CTest, and any title or console run. AI-assisted (Claude Code).
jotitadev commented 2026-10-21 23:33:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-21T23:33:59Z.

Review: COMMENTED


Linux run of 08bd05957, since the description only lists Windows.

Machine: Linux x86_64, i7-8700 (6 cores / 12 threads), Ubuntu 26.04, GCC 15.2, Release, BUILD_TESTING=ON, full build. Head 08bd05957 on its own base (main 3594eee9f).

  • Build ok. guest_kernel_sys_swapcontext passes.
  • Restoring libkernel_sys/Export.Discovery.cpp to that base and keeping the test aborts it with swapcontext_nid_postfix not implemented, so the test covers the change.

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5607511843 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9379#pullrequestreview-5607511843). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-21T23:33:59Z. **Review:** COMMENTED --- Linux run of `08bd05957`, since the description only lists Windows. Machine: Linux x86_64, i7-8700 (6 cores / 12 threads), Ubuntu 26.04, GCC 15.2, Release, `BUILD_TESTING=ON`, full build. Head `08bd05957` on its own base (`main` `3594eee9f`). - Build ok. `guest_kernel_sys_swapcontext` passes. - Restoring `libkernel_sys/Export.Discovery.cpp` to that base and keeping the test aborts it with `swapcontext_nid_postfix not implemented`, so the test covers the change. Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
wachi35676 commented 2026-10-21 23:40:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-21T23:40:12Z.


Linux run (the description and the earlier run are Windows only) and two extra checks, on both hosts.

Head 08bd059578 merged onto main a6dcea927 (clean). Windows 11 Home 10.0.26300, Ryzen 5 9600X, WinLibs GCC 15.2.0 (MinGW-w64 UCRT), Release, BUILD_TESTING=ON; WSL2 Ubuntu 24.04, GCC 13.3.0, Release.

  • guest_kernel_sys_swapcontext, guest_user_context, guest_user_context_exit: 3/3 on Linux, 3/3 on Windows.
  • Negative control on Linux: with UserContext.cpp and Export.Discovery.cpp from main, guest_kernel_sys_swapcontext aborts (swapcontext_nid_postfix not implemented).
  • check_conventions.py --base a6dcea927: passes.
  • The libkernel placement is right: EXH1U-UBywI is in both the PS5 3.20 libkernel (L8904) and libkernel_sys (L9959) lists.

Probe with a makecontext coroutine (same output on Windows and Linux):

  • FPU state per context: main sets MXCSR rounding to up, the coroutine sets toward-zero and swaps back. Main sees "up", the coroutine sees "toward zero" when resumed, and main sees "up" after the coroutine returns through uc_link. This matches set_mcontext restoring the target's FPU state.
  • Signal 32 in the target mask: swapcontext to a context whose uc_sigmask has signals 30 and 32 leaves both blocked (mask word0=0xa0000000). In FreeBSD 11.4 the exported swapcontext goes through libthr's __thr_swapcontext, which removes SIGCANCEL (signal 32) from the new mask before __sys_swapcontext (thr_sig.c L747-L761). main's setcontext behaves the same way, so this isn't new here. It might be worth a line in the debt entry.

AI-assisted: yes (Claude Code); results above were run on this machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6329019012 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9379#issuecomment-6329019012). Originally posted by [wachi35676](https://github.com/wachi35676) on 2026-10-21T23:40:12Z. --- Linux run (the description and the earlier run are Windows only) and two extra checks, on both hosts. Head `08bd059578` merged onto main `a6dcea927` (clean). Windows 11 Home 10.0.26300, Ryzen 5 9600X, WinLibs GCC 15.2.0 (MinGW-w64 UCRT), Release, `BUILD_TESTING=ON`; WSL2 Ubuntu 24.04, GCC 13.3.0, Release. - `guest_kernel_sys_swapcontext`, `guest_user_context`, `guest_user_context_exit`: 3/3 on Linux, 3/3 on Windows. - Negative control on Linux: with `UserContext.cpp` and `Export.Discovery.cpp` from main, `guest_kernel_sys_swapcontext` aborts (`swapcontext_nid_postfix not implemented`). - `check_conventions.py --base a6dcea927`: passes. - The libkernel placement is right: `EXH1U-UBywI` is in both the PS5 3.20 [libkernel](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libkernel.c) (L8904) and [libkernel_sys](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libkernel_sys.c) (L9959) lists. Probe with a `makecontext` coroutine (same output on Windows and Linux): - **FPU state per context:** main sets MXCSR rounding to up, the coroutine sets toward-zero and swaps back. Main sees "up", the coroutine sees "toward zero" when resumed, and main sees "up" after the coroutine returns through `uc_link`. This matches `set_mcontext` restoring the target's FPU state. - **Signal 32 in the target mask:** `swapcontext` to a context whose `uc_sigmask` has signals 30 and 32 leaves both blocked (`mask word0=0xa0000000`). In FreeBSD 11.4 the exported `swapcontext` goes through libthr's `__thr_swapcontext`, which removes `SIGCANCEL` (signal 32) from the new mask before `__sys_swapcontext` ([thr_sig.c L747-L761](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/libthr/thread/thr_sig.c#L747-L761)). main's `setcontext` behaves the same way, so this isn't new here. It might be worth a line in the debt entry. AI-assisted: yes (Claude Code); results above were run on this machine.
DavidSamir commented 2026-10-22 01:56:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-22T01:56:12Z.


Linux: Ubuntu 24.04.1 LTS (WSL2), GCC 13.3.0, Release; head 08bd059578 merged onto main 8c26eac3c.

  • guest_kernel_sys_swapcontext passes 3/3, and guest_user_context (the moved capture) passes 3/3.
  • Link control: with the entry renamed, the test fails to link: undefined reference to swapcontext_nid_postfix, as listed.
  • Negative control: with the saved rbx corrupted (machine.rbx = frame->rbx + 1), it fails at line 159, as listed. I did not repeat the oucp and signal-mask controls.

Run from the PC (Windows host, Linux in WSL2), not from the PR author's machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6330813480 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9379#issuecomment-6330813480). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-22T01:56:12Z. --- Linux: Ubuntu 24.04.1 LTS (WSL2), GCC 13.3.0, Release; head `08bd059578` merged onto `main` `8c26eac3c`. - `guest_kernel_sys_swapcontext` passes 3/3, and `guest_user_context` (the moved capture) passes 3/3. - Link control: with the entry renamed, the test fails to link: `undefined reference to swapcontext_nid_postfix`, as listed. - Negative control: with the saved `rbx` corrupted (`machine.rbx = frame->rbx + 1`), it fails at line 159, as listed. I did not repeat the `oucp` and signal-mask controls. Run from the PC (Windows host, Linux in WSL2), not from the PR author's machine.
MarcZX10 commented 2026-10-22 06:34:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T06:34:46Z.

Review: DISMISSED


I reviewed the capture/restore path and the test report. The refactor preserves the existing getcontext capture sequence, and swapcontext captures the outgoing register frame before delegating to the existing context-save and setcontext paths. I did not find a substantiated implementation defect in the reviewed diff. Manual overlap check for #6879: both PRs touch UserContext.cpp, but #9379 adds the swapcontext assembly entry and finish helper while #6879 adds Emscripten guards/stubs. Those hunks merge independently. Pairwise merge-tree reports conflicts in DirectMemory.cpp and docs/dev/debt/shader.md; please rerun the overlap check after rebasing because the PR heads have diverged substantially. Validation/evidence: - python3 tools/check_conventions.py --base upstream/main --head refs/review/9379: passed. - git diff --check upstream/main...refs/review/9379: passed. - The head merges cleanly with current main, but is 483 commits behind it; CONTRIBUTING.md asks branches to stay up to date with main. - I attempted a local full configure for the focused guest test. Configuration stops before compilation because the SDL2 submodule is absent and the host lacks XRandR support. I therefore did not claim a local test run. - The PR and independent contributor reports include passing Windows and Linux guest_kernel_sys_swapcontext runs, repeated 3/3, plus register/signal-mask negative controls. Another report confirms the FreeBSD 11.4 interval/argument behavior and checks the dual export placement. These are useful independent results. - Hosted conventions, eligibility, labels and report pass; product build/test jobs skip under the path filters. Please also rebase and rerun current checks before requesting merge. I am requesting changes for the documented branch-freshness and overlap requirements, not because I found a code defect. The queue comment is not a reason to hold this PR; it is fine to keep submitting PRs and they will be handled in due course.

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:5610140908 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9379#pullrequestreview-5610140908). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T06:34:46Z. **Review:** DISMISSED --- I reviewed the capture/restore path and the test report. The refactor preserves the existing `getcontext` capture sequence, and `swapcontext` captures the outgoing register frame before delegating to the existing context-save and `setcontext` paths. I did not find a substantiated implementation defect in the reviewed diff. Manual overlap check for #6879: both PRs touch `UserContext.cpp`, but #9379 adds the `swapcontext` assembly entry and finish helper while #6879 adds Emscripten guards/stubs. Those hunks merge independently. Pairwise merge-tree reports conflicts in `DirectMemory.cpp` and `docs/dev/debt/shader.md`; please rerun the overlap check after rebasing because the PR heads have diverged substantially. Validation/evidence: - `python3 tools/check_conventions.py --base upstream/main --head refs/review/9379`: passed. - `git diff --check upstream/main...refs/review/9379`: passed. - The head merges cleanly with current `main`, but is 483 commits behind it; CONTRIBUTING.md asks branches to stay up to date with `main`. - I attempted a local full configure for the focused guest test. Configuration stops before compilation because the SDL2 submodule is absent and the host lacks XRandR support. I therefore did not claim a local test run. - The PR and independent contributor reports include passing Windows and Linux `guest_kernel_sys_swapcontext` runs, repeated 3/3, plus register/signal-mask negative controls. Another report confirms the FreeBSD 11.4 interval/argument behavior and checks the dual export placement. These are useful independent results. - Hosted conventions, eligibility, labels and report pass; product build/test jobs skip under the path filters. Please also rebase and rerun current checks before requesting merge. I am requesting changes for the documented branch-freshness and overlap requirements, not because I found a code defect. The queue comment is not a reason to hold this PR; it is fine to keep submitting PRs and they will be handled in due course. 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.
DavidSamir commented 2026-10-22 08:48:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-22T08:48:04Z.


Thanks for the careful review, @MarcZX10, and for checking the code path itself.

On the intake point: this PR implements a documented export (FreeBSD 11's swapcontext (libkernel and libkernel_sys, as the PS5 3.20 lists show)), with a focused test and negative controls, and replaces a throwing stub. I searched the import tables of the 98 homebrew titles I convert: none imports swapcontext (NID EXH1U-UBywI) today, so Unblocks stays none for now. I'll add the title the moment one is known to stop on it. If a maintainer asks on this PR to hold it, I'll convert it to a draft right away.

The branch is now up to date with main (new merge commit, no force-push). The focused tests rerun on it are listed below.

Merge commit 7f150d8ee0 (current main d6eafa54d0), Windows 11 + WinLibs GCC 15.2: guest_kernel_sys_swapcontext, guest_user_context and guest_user_context_exit pass; check_conventions.py passes.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6336675317 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9379#issuecomment-6336675317). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-22T08:48:04Z. --- Thanks for the careful review, @MarcZX10, and for checking the code path itself. On the intake point: this PR implements a documented export (FreeBSD 11's swapcontext (libkernel and libkernel_sys, as the PS5 3.20 lists show)), with a focused test and negative controls, and replaces a throwing stub. I searched the import tables of the 98 homebrew titles I convert: none imports `swapcontext` (NID `EXH1U-UBywI`) today, so `Unblocks` stays `none` for now. I'll add the title the moment one is known to stop on it. If a maintainer asks on this PR to hold it, I'll convert it to a draft right away. The branch is now up to date with `main` (new merge commit, no force-push). The focused tests rerun on it are listed below. Merge commit `7f150d8ee0` (current `main` `d6eafa54d0`), Windows 11 + WinLibs GCC 15.2: guest_kernel_sys_swapcontext, guest_user_context and guest_user_context_exit pass; check_conventions.py passes. AI-assisted: yes
harisibrar1 commented 2026-10-22 13:32:17 +00:00 (Migrated from github.com)

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

Review: COMMENTED


Checked against main b786b907c8 and FreeBSD 11.4 sys/kern/kern_context.c (sys_swapcontext, L104-131):

  • The entry and finish follow sys_swapcontext: EINVAL for a NULL oucp or ucp, oucp written up to uc_link with rax/rdx and CF cleared, then ucp loaded, with an invalid ucp returning -1/EINVAL after oucp is written. There is no lib/libc/gen/swapcontext.c in 11.4, so the syscall is the right reference.
  • The capture is byte-identical to main's getcontext. rdi and rsi survive it, the frame passed in rdx is 16-byte aligned at the call, and the restore builds its iretq frame on the current stack, so the target's stack and red zone are untouched.
  • One small documentation point: the PR doesn't touch docs/dev/debt/libkernel.md, whose context-gap entries (L152-153 on main) name only getcontext and setcontext. Those gaps are the console layout not verified, the upper YMM halves and mc_flags/fsbase/gsbase not restored, and the Windows TEB stack bounds. swapcontext inherits all of them through the shared capture and setcontext, so it could be added to those lines. The SIGCANCEL note wachi35676 suggested could go in the same edit.

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

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5615029990 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9379#pullrequestreview-5615029990). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-22T13:32:17Z. **Review:** COMMENTED --- Checked against main b786b907c8 and FreeBSD 11.4 `sys/kern/kern_context.c` (`sys_swapcontext`, L104-131): - The entry and finish follow `sys_swapcontext`: `EINVAL` for a NULL `oucp` or `ucp`, `oucp` written up to `uc_link` with `rax`/`rdx` and CF cleared, then `ucp` loaded, with an invalid `ucp` returning -1/EINVAL after `oucp` is written. There is no `lib/libc/gen/swapcontext.c` in 11.4, so the syscall is the right reference. - The capture is byte-identical to main's `getcontext`. rdi and rsi survive it, the frame passed in rdx is 16-byte aligned at the call, and the restore builds its `iretq` frame on the current stack, so the target's stack and red zone are untouched. - One small documentation point: the PR doesn't touch `docs/dev/debt/libkernel.md`, whose context-gap entries (L152-153 on main) name only `getcontext` and `setcontext`. Those gaps are the console layout not verified, the upper YMM halves and `mc_flags`/`fsbase`/`gsbase` not restored, and the Windows TEB stack bounds. `swapcontext` inherits all of them through the shared capture and `setcontext`, so it could be added to those lines. The SIGCANCEL note wachi35676 suggested could go in the same edit. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
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!9379
No description provided.