fix(libkernel): lay out user contexts as the console's getcontext writes them #15794
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/15794"
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 root-over on 2026-11-04T15:56:36Z.
What
Two catalog Xbox 360 ports by the same author, Naruto: Rise of a Ninja (PPSA99680) and Forza Horizon (PPSA99690), crash about 2 s after start on
mainwith a jump to0xa3/0xad.Both build their fibers without
makecontext(port patch,Fiber::Create):getcontextinto a 2 KiB buffer.0xf8, 0 at0x88and the entry function at0xe0.swapcontext.The same patch records what firmware 9.00 does: "getcontext reports mc_len=1152 and writes 1216 bytes".
That is the layout AnyPS5's exception path already uses (
GuestUcontextinException.cpp):mcontextat 0x40,mc_ripat 0xe0,mc_rspat 0xf8,sizeof(GuestMcontext)0x480 (1152),uc_linkat 0x4c0 (1216).getcontext,setcontext,swapcontextandmakecontextinstead used FreeBSD 11's layout:mcontextat 0x10, 800 bytes,mc_ripat 0xb0. Soswapcontextresumed the fiber atgetcontext's old return address, on a stack frame that had already returned. That frame's savedr13had since been overwritten by a return address, and the nextcall *%r13crashed. I traced it with gdb, single-stepping from the lastpthread_mutex_unlockto the crash:getcontextat0x4abf7d,swapcontextfrom0x12a5e9a, then a second return at0x4abf82.Four commits:
fix:UserMcontextandUserUcontexttake the console layout:lbrfrom,lbrto,aux1andaux2aftermc_ownedfp;mc_fpstateat mcontext + 0x100, still holding the 512-byte FXSAVE image at its start;fsbase,gsbase,spare[6];uc_linkat 0x4c0.guest_user_contextchecks the new fields in place ofmc_xfpustate.refactor:Exception.cppusesUserMcontext/UserUcontextthrough aliases instead of its own copy of the same layout. No behaviour change.test:guest_user_contextsets up a fiber the way the ports do, with raw offsets 0xf8/0x88/0xe0, and switches into it and back three times. It checksmc_len1152 at 0x108 and that nothing is written from 0x4c0 on.docs: the debt entry gives the layout and its source, and keepsuc_stackat 0x4c8 (FreeBSD's order afteruc_link) as not measured.The previous layout came from the sizes PPSA99039 (EVO Player)'s OpenSSL
ASYNCpasses. EVO Player makes nogetcontext/makecontext/swapcontext/setcontextcall in its first 45 s (gdb breakpoints on all four), and still runs.Tested
Ubuntu, kernel 7.0, GCC 15.2.0, Release.
main0xa3after 2 snative fiber round-trip check: passed, then looks for/app0/game/naruto.moduleand exits 0 (no game data here)0xadafter 2 snative fiber round-trip check: passed, then exits 0 the same waymainand on this PR:guest_user_context,guest_user_context_exit,guest_kernel_sys,guest_kernel_sys_swapcontext,guest_exception_handler,guest_raise_exception,guest_signals,guest_signal_handler_mask,guest_sce_v2_exceptions,fiber_contextless,register_context_capture,register_context_restore,register_context_threads.tools/check_conventions.py: passes.Exception.cppuses the same field names, andFltSave(512 bytes) still fitsmc_fpstate, which the existingstatic_assertchecks.tools/pr_overlap.py: #12682, #12688, #12700 and #12965 editGuestUserContext.cpplines 302-303 and the same debt entry. They store metadata inmc_spare, which the new layout keeps (6 words instead of 4), so whichever lands second keeps both. #4630, #5021, #8592, #12642 and #13486 touch other parts ofException.cpp.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 BunnyFl on 2026-11-05T15:13:36Z.
Review: APPROVED
@root-over looks right, and firmware 13.42's own
makecontextagrees with the new layout.Firmware 13.42
system/common/lib/libSceLibcInternal.sprx(SHA-256a01795ab49df639dd40abd6194956ada062244826824ce486c9acab227bf5285),makecontextat0x8de70, read to its return (disassembly only, not measured on a console):system/common/lib/libkernel.sprx(SHA-2564be40bf838a46e1ff83a1809960672526139b09dda6c4a6592cfeb485d29f243),swapcontextat0xcad0, copies the context into a0x500-byte stack buffer before the switch:So
mc_len1152 at 0x108,mc_ripat 0xe0,mc_rspat 0xf8 andsizeof(ucontext_t)0x500 all match this PR'sstatic_asserts. It also settles the one field the debt entry leaves open:uc_stack.ss_spis at 0x4c8 andss_sizeat 0x4d0 on 13.42, as you have them. You could cite that instead of "not measured" (non-blocking).What I ran
Head
90c30d52bemerged ontomain78e4248fea(clean merge), Windows 11, WinLibs GCC 15.2.0, Release,BUILD_TESTING=ON, test executables rebuilt. Your description says Windows wasn't built; this is a Windows run:guest_user_context,guest_user_context_exit,guest_kernel_sys,guest_kernel_sys_swapcontext,guest_exception_handler,guest_raise_exception,guest_signals,guest_signal_handler_mask,guest_sce_v2_exceptionsandfiber_contextlesspass.register_context_capture,_restoreand_threadsaren't registered in this Windows build.main'sUserContext.hppandException.cppwith yourGuestUserContext.cpp(itsmc_xfpustatecheck put back so it compiles,CheckConsoleOffsetskept):guest_user_contextaborts (0xc0000409). My log doesn't show which check stopped it.check_conventions.pypasses.pr_overlap.py --branchfinds only shared lines, no shared exports, with #12682, #12688, #12700 and #12965, the four you named.Approving.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-07T07:27:33Z.
Review: COMMENTED
A Windows check, and two notes on the debt entries.
On Windows 11 x64 (WinLibs GCC 15.2.0 r7 UCRT, Release, full build) I merged head
90c30d52ontomainc1e920e7(clean merge) and ranctest -R '^(guest_main_stack|guest_user_context|guest_.*exception|guest_.*unwind|guest_.*context)'. All 171 tests pass onmainand with this PR,guest_user_contextandguest_user_context_exitincluded.In
docs/dev/debt/libkernel.md:ksem_getvalue rejects an unwritable value pointer with EFAULT (14) via checked guest copy, as FreeBSD's sys_ksem_getvalue does through copyout.mainstill has it (L230), and it has nothing to do with this change.mcontextat 0x10, which is right, sinceuc_sigmaskis 16 bytes. ThesceKernelRaiseExceptionentry, which this PR does not touch, still says the handler'sucontextuses "FreeBSD's amd64 layout (16-byte sigmask,mcontextat 0x40,mc_rspat 0xf8)". After this change, that is the console layout that getcontext shares, not FreeBSD's.AI-assisted: yes.