feat(libkernel): implement swapcontext #9379
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9379"
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 DavidSamir on 2026-10-21T19:47:38Z.
What
Implements
swapcontextin libkernel and removes thelibkernel_sysdiscovery stub that threw. In FreeBSD 11,swapcontexton amd64 is the system callsys_swapcontextinsys/kern/kern_context.c; libc exports it as_swapcontextand__sys_swapcontext, and there is nolib/libc/genversion in 11. This followssys_swapcontext:EINVALfor a NULLoucporucp.oucpgets the current machine context, with the return value cleared (GET_MC_CLEAR_RET), and the signal mask. The copy runs up touc_link(UC_COPY_SIZE), souc_flags,uc_linkanduc_stackare not touched.ucp. An invalid context givesEINVALafteroucphas been written, as in FreeBSD. It sets the signal mask and continues inucp.oucplater returns 0 fromswapcontext.How:
libkernel/System/src/UserContext.cpp:getcontextmoves, unchanged, intoAPS5_CONTEXT_CAPTURE/APS5_CONTEXT_RETURN.swapcontext_nid_postfix, uses the same capture and callsAps5SwapcontextFinish_nid_no_patch. That isAps5GetcontextFinish_nid_no_patchonoucp, thensetcontext_nid_postfix(ucp).ripandrspare those ofswapcontext's caller, exactly as forgetcontext.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
swapcontextin 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:EINVALcases, and an invaliducpthat givesEINVALwithoucpalready written.makecontextcoroutine. The main side swaps into it three times and resumes once more for the coroutine's final return throughuc_link.rbx,rbpandr12–r15and checks them whenswapcontextreturns. The coroutine swaps back through a helper that overwrites all of them first.Results:
guest_user_context(both tests) still passes on the moved capture.tools/check_conventions.pypasses, and there are no warnings.undefined reference to swapcontext_nid_postfix), so it reaches libkernel's export, which is the only one.oucpcapture, the test fails at line 144;rbxcorrupted, it fails at line 159;tools/pr_overlap.pycrashes on this machine's partial clone, so I rangit merge-treeagainst every open PR that touches these files, including #6879, the other open PR onUserContext.cpp. It merges cleanly with all of them; #6879's conflicts withmainare in other files.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)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
mainis clean. I built onlyguest_kernel_sys_swapcontext_tests;ctest -N -R guest_kernel_sys_swapcontextlists exactly one test.PR head tested:
c643263717a7c979cd79fc7b5b0a9781967d2083.Results (3 runs each):
guest_kernel_sys_swapcontextpasses 3/3.core/libs/prx/libkernel/System/src/UserContext.cpp,core/libs/prx/libkernel_sys/Export.Discovery.cppwith 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 GPUline; 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).
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. Head08bd05957on its own base (main3594eee9f).guest_kernel_sys_swapcontextpasses.libkernel_sys/Export.Discovery.cppto that base and keeping the test aborts it withswapcontext_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.
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
08bd059578merged onto maina6dcea927(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.UserContext.cppandExport.Discovery.cppfrom main,guest_kernel_sys_swapcontextaborts (swapcontext_nid_postfix not implemented).check_conventions.py --base a6dcea927: passes.EXH1U-UBywIis in both the PS5 3.20 libkernel (L8904) and libkernel_sys (L9959) lists.Probe with a
makecontextcoroutine (same output on Windows and Linux):uc_link. This matchesset_mcontextrestoring the target's FPU state.swapcontextto a context whoseuc_sigmaskhas signals 30 and 32 leaves both blocked (mask word0=0xa0000000). In FreeBSD 11.4 the exportedswapcontextgoes through libthr's__thr_swapcontext, which removesSIGCANCEL(signal 32) from the new mask before__sys_swapcontext(thr_sig.c L747-L761). main'ssetcontextbehaves 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.
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
08bd059578merged ontomain8c26eac3c.guest_kernel_sys_swapcontextpasses 3/3, andguest_user_context(the moved capture) passes 3/3.undefined reference to swapcontext_nid_postfix, as listed.rbxcorrupted (machine.rbx = frame->rbx + 1), it fails at line 159, as listed. I did not repeat theoucpand signal-mask controls.Run from the PC (Windows host, Linux in WSL2), not from the PR author's machine.
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
getcontextcapture sequence, andswapcontextcaptures the outgoing register frame before delegating to the existing context-save andsetcontextpaths. I did not find a substantiated implementation defect in the reviewed diff. Manual overlap check for #6879: both PRs touchUserContext.cpp, but #9379 adds theswapcontextassembly entry and finish helper while #6879 adds Emscripten guards/stubs. Those hunks merge independently. Pairwise merge-tree reports conflicts inDirectMemory.cppanddocs/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 currentmain, but is 483 commits behind it; CONTRIBUTING.md asks branches to stay up to date withmain. - 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 Linuxguest_kernel_sys_swapcontextruns, 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.
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(NIDEXH1U-UBywI) today, soUnblocksstaysnonefor 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(currentmaind6eafa54d0), 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
Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-22T13:32:17Z.
Review: COMMENTED
Checked against main
b786b907c8and FreeBSD 11.4sys/kern/kern_context.c(sys_swapcontext, L104-131):sys_swapcontext:EINVALfor a NULLoucporucp,oucpwritten up touc_linkwithrax/rdxand CF cleared, thenucploaded, with an invaliducpreturning -1/EINVAL afteroucpis written. There is nolib/libc/gen/swapcontext.cin 11.4, so the syscall is the right reference.getcontext. rdi and rsi survive it, the frame passed in rdx is 16-byte aligned at the call, and the restore builds itsiretqframe on the current stack, so the target's stack and red zone are untouched.docs/dev/debt/libkernel.md, whose context-gap entries (L152-153 on main) name onlygetcontextandsetcontext. Those gaps are the console layout not verified, the upper YMM halves andmc_flags/fsbase/gsbasenot restored, and the Windows TEB stack bounds.swapcontextinherits all of them through the shared capture andsetcontext, 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.