Repository navigation
feat(libkernel): implement getcontext, setcontext and makecontext - #3421
Merged
adriadam10 merged 1 commit intoOct 11, 2026
Merged
Conversation
PSXS5, SwanStationPS5 and Nuvio stop at start-up on setcontext on main, and EVO Player and ProsperoStore do on Windows. The three are imported by the statically linked OpenSSL's ASYNC code. The three use FreeBSD 11's amd64 ucontext_t (mcontext at 0x10, mc_len 800, uc_link at 0x330, 0x370 bytes), which the titles are built against: EVO Player's ASYNC_is_capable captures into a 0x370-byte stack buffer, and async_fibre_makecontext sets uc_link at 0x330 and uc_stack at 0x338. getcontext saves every register at its call in assembly and stores the context as FreeBSD's sys_getcontext does: rax and rdx as 0, the carry flag clear, the FXSAVE image and the signal mask, and nothing from uc_link on. setcontext checks mc_len and mc_flags as set_mcontext does, installs the signal mask, masks MXCSR with the host's MXCSR_MASK and returns through iretq, so every general register and the flags are restored and nothing is written below the target stack pointer. makecontext follows FreeBSD 11's amd64 libc. Windows checks guard-page growth and exception unwinding against the stack bounds in the TEB. setcontext and longjmp now set them to the stack they switch to, which makecontext registers and pins writable as libSceFiber does for fiber stacks. OpenSSL enters a context with setcontext and then switches with _setjmp and _longjmp.
9 tasks done
Collaborator
|
Checked on Linux: build, |
9 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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)