Repository navigation
fix(libkernel): lay out user contexts as the console's getcontext writes them - #15794
oneandonlydean merged 7 commits into
Conversation
✅ 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.
d7d38c7 to
90c30d5
Compare
BunnyFl
left a comment
There was a problem hiding this comment.
@root-over looks right, and firmware 13.42's own makecontext agrees with the new layout.
Firmware 13.42
system/common/lib/libSceLibcInternal.sprx (SHA-256 a01795ab49df639dd40abd6194956ada062244826824ce486c9acab227bf5285), makecontext at 0x8de70, read to its return (disassembly only, not measured on a console):
8dee7: cmpq $0x480,0x108(%rdi) ; mc_len must be 1152, at 0x40 + 0xc8
8def4: cmp $0x6,%edx ; at most 6 arguments
8def9: mov 0x4c8(%rdi),%rax ; uc_stack.ss_sp
8df05: mov 0x4d0(%rdi),%rcx ; uc_stack.ss_size
8df0c: cmp $0x7ff,%rcx ; must be above 0x7ff
8df15: movq $0x0,0x108(%rdi) ; otherwise mc_len is cleared
8dfee: mov %rdi,0x48(%rdi) ; mc_rdi = ucp
8dffa: movq $0x0,0x88(%rdi) ; mc_rbp = 0
8e00c: mov %r14,0xf8(%rdi) ; mc_rsp
8e013: mov %rax,0xe0(%rdi) ; mc_rip
system/common/lib/libkernel.sprx (SHA-256 4be40bf838a46e1ff83a1809960672526139b09dda6c4a6592cfeb485d29f243), swapcontext at 0xcad0, copies the context into a 0x500-byte stack buffer before the switch:
caef: mov $0x500,%edx
So mc_len 1152 at 0x108, mc_rip at 0xe0, mc_rsp at 0xf8 and sizeof(ucontext_t) 0x500 all match this PR's static_asserts. It also settles the one field the debt entry leaves open: uc_stack.ss_sp is at 0x4c8 and ss_size at 0x4d0 on 13.42, as you have them. You could cite that instead of "not measured" (non-blocking).
What I ran
Head 90c30d52be merged onto main 78e4248fea (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:
- 10 tests:
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. - Negative control:
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)
❌ AMD GPU test: failedPR head
Failing testsThis PR changes test files, so each failing test was also built and run on main with only the PR's test changes applied. ✅ there means the failure comes from the PR's other changes; ❌ means the test fails on main too, so this run does not show the PR caused it.
agc_graphics_profile_draw: Timeout (also failed when re-run alone), assertion and diagnosticsUnstable on main (5, these also failed in recent main runs on this machine; not counted against this PR)Failing on main too (10, not counted against this PR)Validated but not compared (65, not in the main baseline's validation pass)Validation IDs already present on main (4 IDs in 809 tests)Skipped (71)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. |
|
@root-over correcting my comment here: I had suggested a cause for the What the bot's reports say: on this PR its one new failure is AI-assisted: yes (Claude Code) |
|
@root-over here is the Head
One run of each. This doesn't reproduce the 60 s timeout from the bot's run (Linux, RX 6300, RADV) on this machine, and it doesn't tell us why the test timed out there; I have no Linux setup to run it on. AI-assisted: yes (Claude Code) |
|
Linux RADV run of Head
This does not reproduce the 60 s timeout on Linux and RADV either, and the time is the same with and without the change. The bot's run of #14145 against the same |
|
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
|
gabichuuuuu
left a comment
There was a problem hiding this comment.
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 90c30d52 onto main c1e920e7 (clean merge) and ran ctest -R '^(guest_main_stack|guest_user_context|guest_.*exception|guest_.*unwind|guest_.*context)'. All 171 tests pass on main and with this PR, guest_user_context and guest_user_context_exit included.
In docs/dev/debt/libkernel.md:
- The rewritten ksem entry drops its last clause,
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. - The new entry gives FreeBSD's layout as
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.
|
@root-over gabichuuuuu's two debt notes are right, and I missed the first one when I approved this head (cc @gabichuuuuu, thanks for catching it). Both read at head
My approval stands for the code; the ksem clause is the one fix I'd ask for. AI-assisted: yes (Claude Code) |
|
@gabichuuuuu @BunnyFl thanks, both fixed on top of
The code is unchanged. I also rebuilt these commits rebased onto |
|
@root-over thanks, both are fixed at
AI-assisted: yes (Claude Code) |
The getcontext debt entry named two titles next to their ids. Debt docs name PPSA ids only, so keep the ids.
|
Hi @root-over, I pushed one small docs commit to your branch (3c14750, on top of 3cdd718; no force-push) so the PR can merge without another round. In |
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)