Repository navigation
Conversation
Contributor
✅ AMD GPU test: passedPR head
Unstable on main (2, these also failed in recent main runs on this machine; not counted against this PR)Failing on main too (12, not counted against this PR)Validation IDs already present on main (8 IDs in 802 tests)Skipped (62)ReproduceAutomated build and test on a contributor's AMD machine; it does not replace upstream CI. A new comment is posted for each new push. Full logs. |
…tes them getcontext, setcontext, swapcontext and makecontext used FreeBSD 11's ucontext_t, with mcontext_t at 0x10 and 800 bytes long. The console's is the layout the exception handler path already uses: mcontext_t at 0x40, 1152 bytes (mc_len 0x480), with lbrfrom, lbrto, aux1 and aux2 after mc_ownedfp, an 832-byte mc_fpstate, and uc_link at 0x4c0. The Naruto: Rise of a Ninja and Forza Horizon ports (PPSA99680, PPSA99690) read it so on firmware 9.00: getcontext reports mc_len 1152 and writes 1216 bytes, and their fibers set mc_rip at 0xe0, mc_rsp at 0xf8 and mc_rbp at 0x88 after getcontext. Here swapcontext read mc_rip at 0xb0 instead, resumed the fiber at getcontext's return address on a stack that had moved on, and both titles crashed at start. UserMcontext and UserUcontext now take that layout. getcontext still copies the 512-byte FXSAVE image to the start of mc_fpstate and zeroes the new fields; guest_user_context checks those fields in place of mc_xfpustate and mc_xfpustate_len.
…tion path Exception.cpp kept its own GuestMcontext and GuestUcontext, the layout UserContext.hpp now has. It uses UserMcontext and UserUcontext through aliases, so the exception handlers and getcontext/setcontext cannot drift apart again. The static assertions on the offsets live in UserContext.hpp. No behaviour changes.
The Naruto and Forza Horizon ports build their fibers without makecontext: after getcontext they store the entry stack at offset 0xf8 (mc_rsp), 0 at 0x88 (mc_rbp) and the entry function at 0xe0 (mc_rip) of a 2 KiB buffer, then switch with swapcontext. guest_user_context now does the same with raw offsets, checks that getcontext reports mc_len 1152 at 0x108 and writes nothing from uc_link at 0x4c0 on, and switches into the fiber and back three times.
…s from The getcontext entry described FreeBSD 11's layout, taken from the sizes PPSA99039's OpenSSL ASYNC passes, and called the console's own layout unverified. It now gives the layout the exception path and getcontext share, the firmware 9.00 readings from the Naruto and Forza Horizon ports behind it, and the part that is still not measured: uc_stack after uc_link. PPSA99039 does not call these functions at start. The FXSAVE entry names mc_lbrfrom, mc_lbrto, mc_aux1 and mc_aux2, which getcontext leaves 0, in place of mc_xfpustate and mc_xfpustate_len, which the console layout does not have.
root-over
force-pushed
the
fix/libkernel-console-ucontext-layout
branch
from
November 5, 2026 08:00
d7d38c7 to
90c30d5
Compare
This branch has not been deployed
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
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: feat(libkernel): cancel blocked POSIX reads with owned operations #12682, fix(libkernel): preserve blocking sockets over nonblocking native transport #12688, feat(libkernel): cancel blocked POSIX writes with partial progress #12700 and feat(libkernel): support deferred cancellation in POSIX accept #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. feat(libkernel): sceKernelRaiseException on macOS hosts #4630, feat(aot): consolidate native ARM64 compiler, runtime and consumers #5021, fix(agc): defer Windows exceptions during shader preparation #8592, feat(libkernel_sys): implement pthread_suspend_all_np and pthread_single_np #12642 and fix(relinker): match AppContent utility import to provider #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)