feat(libkernel): implement getcontext, setcontext and makecontext #3421
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3421"
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 DariusHQ on 2026-10-11T10:05:05Z.
What
getcontext(dar03AOn+nM) andsetcontext(2VRU7xiqLO8) in libkernel andmakecontext(GipcbdDM5cI) in libSceLibcInternal. This replaces #1841, which was closed because no title stopped at them yet and because it used the wrongucontextlayout. Both points are now settled on that thread:setcontextonmainon Linux (@babyhuey).FAIL: unresolved ELF import 2VRU7xiqLO8 from libkernel.prx(@LeandroLP23, @orishamir).In these titles the callers are the statically linked OpenSSL's ASYNC code (
crypto/async/arch/async_posix.c).Layout. The three functions now use FreeBSD 11's amd64
ucontext_t, because the titles are built against that layout:mcontextat 0x10,mc_len800,mc_fpstateat 0xe0 in it,uc_linkat 0x330,uc_stackat 0x338, 0x370 bytes in all. In EVO Player 0.10.0:ASYNC_is_capablecaptures into a 0x370-byte buffer on the stack (sub rsp, 0x370; lea rdi, [rbp-0x370]; call getcontext);async_fibre_makecontextstoresss_spat +0x338,ss_sizeat +0x340 anduc_linkat +0x330;env_initfollows the 0x370-byte context and its 96-bytejmp_buf, at +0x3d0.The exception handler's 0x500-byte context in
Exception.cppis unrelated to these functions and stays as it is. The layout lives inSystem/include/UserContext.hpp.getcontext is written in assembly, so it saves every register as it was at the call. It stores the context as FreeBSD's
sys_getcontextdoes:raxandrdxas 0, the carry flag clear,ripandrspas after the return, the FXSAVE image,mc_lenand the signal mask (throughsigprocmask). LikeUC_COPY_SIZE, it writes only the 0x330 bytes beforeuc_link, souc_link,uc_stackanduc_flagskeep what the caller stored.setcontext fails with
EINVALon a null context, a wrongmc_lenor unknownmc_flagsbits, asset_mcontextdoes. It installs the context's signal mask and masks MXCSR with the host'sMXCSR_MASK, asfpusetregsdoes, because FXRSTOR faults on reserved bits. It returns throughiretq, so every general register,rip,rspand thePSL_USERCHANGEflags come from the context. Nothing is written below the target stack pointer, which matters for a context taken in a signal handler.makecontext follows FreeBSD 11's
lib/libc/amd64/gen/makecontext.c: at most six arguments and a stack of at leastMINSIGSTKSZ(2048) bytes, otherwisemc_lenbecomes 0. Its wrapper calls the function, thensetcontext(uc_link), orexit(0)when there is no link.Windows stack bounds. Windows checks guard-page growth against
StackBaseandDeallocationStackin the TEB. It checks exception dispatch and unwinding againstStackLimitandStackBase. So the TEB must describe the stack the thread runs on, as libSceFiber already keeps it for fiber stacks.GuestStacks, keeps the TEB in step forsetcontextandlongjmp.makecontextregisters its stack and pins it writable, as fiber stacks are pinned.rspfromVirtualQuery; for a thread stack, that is its currentStackLimittoStackBase.longjmpneeds this because OpenSSL leaves a fibre with_longjmp. Without it, the thread would run on its own stack with the fibre's bounds, and its next guard-page hit would raiseSTATUS_GUARD_PAGE_VIOLATION. Linux gets the three functions and no other change.TechnicalDebt records:
makecontext;setcontextleave out;Tested
Windows 11 (26200), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, full build with
BUILD_TESTING=ONona73a1af9c.ctest: 824/825 passed. The failure iswindows_exceptions(0xc0000409, "through runtime frames"), which fails the same way with this branch's libc changes reverted tomain.guest_user_contextandguest_user_context_exitpassed 200 runs in a row (--repeat until-fail:200).guest_user_contextcovers:mc_len, unknownmc_flags;makecontextwith seven arguments, a 2047-byte stack, no stack, and an invalid context;rax/rdx0, carry clear,rip,rsp, MXCSR, the x87 control word, the signal mask, the zeroed fields, and that nothing fromuc_linkto the end of the 0x370 bytes changes;makecontextwith six 64-bit arguments, an aligned entry stack and a return throughuc_link;jmp_buf, flag):setcontextinto a job, then four rounds ofsetjmp/longjmpboth ways;setcontext;guest_user_context_exitchecks that a context withoutuc_linkexits with 0.Mutation check: the tests catch each of these. I ran the first one on this branch; the others ran on #1841, whose code is the same here apart from the layout:
getcontextwriting the wholeucontextinstead of stopping atuc_link;getcontextkeeping the carry flag or savingrcxasrbx;setcontextorlongjmpkeeping the old bounds, ormakecontextnot registering its stack;setcontextkeeping reserved MXCSR bits, ignoring the context's flags, not loadingr11, keeping the current signal mask, or accepting a wrongmc_len;makecontextshifting its arguments or misaligning the entry stack;uc_linkaborting instead of exiting.tools/import_audit.pyon EVO Player 0.10.0 relinked with--windows --registry(eboot.binandlibc.prxunwrapped): 0 absent imports with this branch, and the three NIDs resolve. I did not run a title. @LeandroLP23 offered to run EVO Player and ProsperoRadio on Windows with it.Not run on Linux (no Linux host here). The Linux path is the same assembly without the TEB call; CI runs it.
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-11T10:05:31Z.
Progress report (
24cb0d2→cf1545d)📈 System libraries: 85.61% (+0.01%, +2 implemented functions)
✅ 2 implemented
makecontext_nid_postfixsetcontext_nid_postfixImported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T10:21:44Z.
Checked on Linux: build,
guest_user_contextandguest_user_context_exitpass, and a mutation (getcontextkeeping the carry flag) makes the test abort.makecontextmatches FreeBSD 11'smakecontext.c, the TechnicalDebt entries cover the unverified console layout, the FXSAVE-only state and the Windows stack pin, and no other open PR touches these functions. The Windows stack-bounds path is covered by the author's Windows 11 run, not reproduced here; merging.