feat(libkernel): implement _umtx_op wait and wake operations #1482
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1482"
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 awake-devel on 2026-10-08T08:54:59Z.
What
Hollow Knight: Silksong (PPSA12544) ships its own
sce_module/libc.prxand Unity'sMedia/Modules/PS5Util.prx. Both import_umtx_op(NID04AjkP0jO9U) from libkernel, which AnyPS5 does not export, so on Linux the relinked title stops at load withundefined symbol: 04AjkP0jO9U.This adds
_umtx_opto libkernel for the operations those modules pass, following FreeBSD 11's kern_umtx.c:UMTX_OP_WAITobjequalsval; return 0 at once when it differs (do_wait, compat32 = 0)UMTX_OP_WAIT_UINT/_PRIVATEdo_wait, compat32 = 1)UMTX_OP_WAKE/_PRIVATEvalwaiters;val <= 0still wakes one (umtxq_signal_queue)Timeouts follow
umtx_copyin_umtx_time: whenuaddr(the size) is at most 16,uaddr2is a relativetimespec; otherwise it is an_umtx_time, absolute withUMTX_ABSTIMEon its clock. A negative second count or a nanosecond count outside [0, 1e9) fails withEINVAL, and a wait that times out fails withETIMEDOUT. Errors are returned as -1 with the guesterrnoset, like the system call. A null object givesEFAULT(exceptWAKE_PRIVATE, which does not look up the address). Operations 0 and 1 fail withEOPNOTSUPPand operations above 26 withEINVAL, as in FreeBSD 11'sop_table. The mutex, condition variable, rwlock, semaphore, shm and robust-list operations throwNotImplemented, as does a misaligned object.Waiters use the existing
sceKernelSyncOnAddressWaitqueue andTimedWait(no platform-specific code), so_umtx_opandsceKernelSyncOnAddressWakewake each other's waiters.What the title calls. I disassembled the two modules:
PS5Util.prx: a wait helper at 0x700 (UMTX_OP_WAIT, atimespecbuilt from a microsecond timeout) and a wake helper at 0x750 (UMTX_OP_WAKE). The result is read through__error.libc.prx: helpers at 0x116b80 (UMTX_OP_WAIT_UINT), 0x116b90 (UMTX_OP_WAIT) and 0x116ba0 (UMTX_OP_WAKE). They are used by exported atomic-wait functions (name stringSceLibcV2AtomicWait) and by a call_once (SceLibcIV2Once), which waits with operation 1 and wakes every waiter withUMTX_OP_WAKE(INT_MAX).Two things in these modules disagree with FreeBSD and could not be checked without a console. Both are recorded in TechnicalDebt:
sceKernelSyncOnAddressWait), while PS5Util passes atimespec. With FreeBSD's rule, libc's 4-byte value is read as atimespec. The nanosecond half is then almost always out of range, so the wait returnsEINVALat once and libc's loop checks again. Only the untimed waits (null timeout) block.EOPNOTSUPP. The once loop then checks the flag again until it reaches the done state, so it spins instead of sleeping.AI-assisted: the call sites were found and this change was written with Claude Code.
This PR was investigated and written by Claude Code (AI), and posted by me after review.
Tested
Arch Linux (kernel 7.2.8), i7-8750H, GTX 1050 Ti Max-Q (NVIDIA 580.178.04), GCC 16.2.1, Release build. Each run is compared with unmodified
mainbc1fbe81on the same machine. All runs setAPS5_WRITE_WATCH_IMPORTS=watch; without it this NVIDIA driver creates no Vulkan device (#1289) and the GPU tests are skipped.ctest -j4: 424/424 passed, 0 skipped, 0 failed (baseline 423/423, 0 skipped). The only new test isguest_umtx_op.VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ctest -j2 -V: 424/424 passed. 27Validation Errorlines, the same as the baseline, with identical per-VUID counts.New test
core/libs/tests/GuestUmtxOp.cpp(modelled onGuestSyncOnAddress) covers:WAITand 32-bit forWAIT_UINT;timespec, relative_umtx_timeand absoluteCLOCK_MONOTONICtimeouts, including one in the past;INT_MAX), with a waiter on another address left untouched;Mutation check. With the test kept and the fix changed, the test failed every time:
UMTX_OP_WAITcompares 32 bits: test fails;The fix was then restored byte for byte and rebuilt, and
guest_umtx_opandguest_sync_on_addresspassed.Windows: not tested. The change has no Windows-specific code; it uses
std::uint64_tfor the guestu_long.Silksong (PPSA12544), Linux, relinked with #846 (
--module-dir Media/Plugins) and local throwing stubs for unrelated missing imports. Onmainthe loader fails withundefined symbol: 04AjkP0jO9U. With this change, all modules load, the title starts, reads/app0/Media/boot.config, and stops at a later, unrelated gap:sceKernelLoadStartModule with start arguments not implemented. The log does not show whether_umtx_opran before that point, so the wait and wake paths are covered only by the unit test.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)🤖 Generated with Claude Code
https://claude.ai/code/session_01HeC9bA3n74TzJRYv9AgBE8
Imported from boykopovar/AnyPS5. Originally posted by popcoder565 on 2026-10-08T09:58:58Z.
Tested on Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, Intel Core i7-12700H (20 logical CPUs). I merged it onto
main73ac9ef7together with #1477, #1482 and #1490 to share one build.guest_umtx_op: 5 of 5 runs pass, 0.59 to 0.62 s each.agc_driver_srgb8_color_target, an NVIDIA import flake (#1434).Tested with AI assistance (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by fisherjoey on 2026-10-09T01:13:26Z.
One difference from FreeBSD 11 in the WAIT_UINT path.
do_waittakesu_long id, and with compat32 it reads the word withfueword32, zero-extends it intotmpand then checkstmp == id(kern_umtx.c:965-978 on stable/11). So avalwith any of the high 32 bits set never matches and the call returns 0 right away.Here the case does
static_cast<std::uint32_t>(value)first, so a stored 1 withval = 0x700000001sleeps. GuestUmtxOp.cpp:85 asserts that (ETIMEDOUT), while FreeBSD 11 would return 0 for the same call. Sign extension hits the same path: anintcomparand of -1 passed as 0xffffffffffffffff against a stored 0xffffffff spins on FreeBSD and blocks here.I checked it on the PR head (85c43cde, merged onto main
29e533ac, Linux, GCC 16.2.1, Release). The PR's ownguest_umtx_oppasses, and a small probe gives:valWAIT_UINTandWAIT_UINT_PRIVATEWAIT_UINTWAIT_UINTIt doesn't matter much in practice, and I can't say what the PS5 kernel does. Since the description says this follows
do_wait, though, the minimal change is to pass the full value toUmtxWait, return 0 whenexpected > std::numeric_limits<TValue>::max()(after the timeout/null/alignment checks, to keep the error order), and flip line 85 to expect 0.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-09T09:22:10Z.
For the two cases where PPSA12544's own
libc.prxdoesn't match FreeBSD 11 (a 4-byte microsecond timeout with size 0 on the WAIT ops, and operation 1 in its call_once), please throw instead of returningEINVAL/EOPNOTSUPPinSyncOnAddress.cpp. The title's libc shows the console handles those calls some other way, and returning FreeBSD's error makes it spin. Unknown behaviour throws here. The rest can go in.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-09T23:30:59Z.
Review: CHANGES_REQUESTED
Thanks for the detailed write-up. Two things before this can go in:
NotImplemented_nid_no_patch) instead of returning an error in the two cases where the title'slibc.prxdisagrees with FreeBSD 11: the 4-byte microsecond timeout with size argument 0 on the WAIT ops, and operation 1 (SyncOnAddress.cpp:250-254,UmtxDeadlineat:128). Please also say how a libc call is told apart from PS5Util'stimespeccall if both pass size 0; if they can't be told apart, say so in TechnicalDebt and throw only on what is certain.do_waitwith compat32 compares the zero-extended word against the fullval, soWAIT_UINTwith any high bit set invalreturns 0 at once.SyncOnAddress.cpp:243truncates to 32 bits, andGuestUmtxOp.cpp:83asserts the opposite. Pass the full value and return 0 when it doesn't fit (after the timeout/null/alignment checks), and flip that assertion.Update the TechnicalDebt line (
TechnicalDebt.md:132) to match. Happy to look again once pushed.Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-09T23:49:22Z.
Progress report (
4c9686f→3879e7e)📉 System libraries: 85.42% (-0.03%, +1 declared functions)
🆕 1 declared as stubs
_umtx_op_nid_postfixImported from boykopovar/AnyPS5. Originally posted by babyhuey on 2026-10-10T01:55:11Z.
Checked the current head (85c43cde, before the change requested above) on
mainee501326(clean merge), Linux (Ubuntu 24.04, GCC 13) and Windows 11 (MinGW-w64 GCC from WinLibs), Release.guest_umtx_oppasses 5/5 on both (--repeat until-fail:5); no CRT assertion dialog on Windows._umtx_oponmain, and all three get past it on both, to the same next stop:app.elf: symbol lookup error: app.elf: undefined symbol: JZKw5+Wrnaw(pthread_sigmask)FAIL: unresolved ELF import JZKw5+Wrnaw (pthread_sigmask) from libkernel.prxProsperoLight (PPSA99002) and Lichess (PPSA99009) also reach
_umtx_oponceflockfileandsceHttpSetRecvBlockSizeexist (#1578).Imported from boykopovar/AnyPS5. Originally posted by babyhuey on 2026-10-10T20:24:39Z.
For priority: on
main56fb1b3bthis is now the next stop for 10 catalog titles (catalog builds withsce_module/libc.prxunwrapped as #2605 requires; tracked in #1578), withundefined symbol: 04AjkP0jO9Uon Linux andFAIL: unresolved ELF import 04AjkP0jO9U (_umtx_op) from libkernel.prxon Windows:SymphonyStation5 (PPSA17641), VITA5 (PPSA39410), Stremio Plus (PPSA74126), PokeMMO (PPSA98001), ProsperoLight (PPSA99002), PS5 OpenGL Showcase (PPSA99005), ProsperoPuzzles (PPSA99006), ProsperoLichess (PPSA99009), Homebrew UI Lab (PPSA99050) and Cloud Play (PPSA99600), plus ccleste, RommPS and vkQuake once
utime(#2967) is in.The two changes requested above are the only open items I can see. The
WAIT_UINTone is mechanical; the size-0 timeout one needs the PS5Util disassembly from the description, so it is @awake-devel's call.Imported from boykopovar/AnyPS5. Originally posted by fma-rc on 2026-10-10T20:29:09Z.
Tested the current head (85c43cde, merged cleanly onto
main56fb1b3b) on Arch Linux, GCC 16.2.1, Release, and looked at a second title'slibc.prx, which may help with the operation 1 question.Tests
guest_umtx_op: 100 of 100 with--repeat until-fail:100;guest_sync_on_address: 20 of 20.UMTX_OP_WAITwaiter is released bysceKernelSyncOnAddressWake, and asceKernelSyncOnAddressWait64waiter byUMTX_OP_WAKE; both return 0 right after the wake. The PR's test does not cover this.Astro Bot (PPSA21567, 01.019)
Its
sce_module/libc.prximports_umtx_optoo.main56fb1b3b:symbol lookup error: .../sce_module/libc.prx.guest.prx: undefined symbol: 04AjkP0jO9U.GetImageResource dword 0 is not a valid runtime value), within the 120 s run. With a temporary log line in_umtx_op, the function was not called once in that run, so like the Silksong run this only shows the import resolving.What this libc passes
The only direct references to the import that I found in the module's text segment are two helpers, which reach it through a shared stub that sets the fourth argument (size) to 0:
Each helper has one caller, and both are the call_once pair:
So in this libc the once loop waits with
UMTX_OP_WAIT(2),val= 1 (the "running" state), a null timeout and size 0, and the finisher wakes withUMTX_OP_WAKE(INT_MAX). The1is the comparand, not the operation. This build has noUMTX_OP_WAIT_UINThelper and no timed wait.@awake-devel could you check whether the
1in PPSA12544's call_once is also the value argument of the helper at 0x116b90 rather than the operation? I don't have that title, so I can't tell; the two libcs are different builds (different helper addresses, and yours has aWAIT_UINThelper).For the Astro Bot shape, the PR already does the right thing: four threads in
_umtx_op(&state, UMTX_OP_WAIT, 1, 0, nullptr)withstate= 1 stay blocked (0 of 4 returned after 300 ms), and after storing 2 and_umtx_op(&state, UMTX_OP_WAKE, INT_MAX, 0, nullptr)all four return 0.Not checked: Windows, and the two points already under review (the
WAIT_UINTcomparison and the 4-byte timeout).Imported from boykopovar/AnyPS5. Originally posted by awake-devel on 2026-10-10T20:37:46Z.
I'll take a look around Monday, busy this weekend, sorry
Imported from boykopovar/AnyPS5. Originally posted by babyhuey on 2026-10-10T20:44:29Z.
@awake-devel no rush. In case it saves you time on Monday, I put the two mechanical parts on top of your commit in babyhuey:help/umtx-op-1482 (8ec80b31), for you to pull in or ignore:
UMTX_OP_WAIT_UINTand_PRIVATEpass the fullvaltoUmtxWait, which returns 0 when it doesn't fit in 32 bits, after the timeout, null and alignment checks.GuestUmtxOp.cppnow expects 0 for0x700000001and for0xffffffffffffffffagainst a stored0xffffffff.NotImplemented; operation 0 keepsEOPNOTSUPP. The TechnicalDebt line says so.I left the size-0 microsecond timeout alone, since telling it apart from PS5Util's
timespecneeds your disassembly. Merged ontomain56fb1b3b,guest_umtx_opandguest_sync_on_addresspass on Linux andcheck_conventions.pypasses.Imported from boykopovar/AnyPS5. Originally posted by GustavoGamarra95 on 2026-10-11T02:48:05Z.
Another title this unblocks on Linux: God of War Sons of Sparta (PPSA28997, Unity IL2CPP).
On
mainthe loader stops atundefined symbol: 04AjkP0jO9UfromMedia/Modules/PS5Util.prx. With head 85c43cde applied (onmain942a174e, together with #1431 forsceKernelRaiseException), all modules load, and the title boots and plays through several levels, with saves and trophies working._umtx_opruns a lot there. During gameplay the main thread and another Unity thread were inside_umtx_op→WaitOnAddressUntil, called fromPS5Util.prx, in 38 and 36 of 40eu-stacksamples. Those are the frame waits between Unity's threads, so the wait and wake path is exercised continuously. I saw no hang or lost wake-up over several play sessions, the longest about ten minutes.Imported from boykopovar/AnyPS5. Originally posted by overlordxrz-source on 2026-10-11T03:58:00Z.
For priority, an update from a catalog run on
main6c9cb3a5(Linux)._umtx_op(04AjkP0jO9U) is the first stop for these 9 titles:Two more stop here once the relinker gets past their SDK syscall stubs with the opt-in flag discussed in #2136: OliSe (PPSA01153) and PS5CEMU-HAR (PPSA99360). That makes 11.
No rush, @awake-devel. Thanks for the PR.
Imported from boykopovar/AnyPS5. Originally posted by LeandroLP23 on 2026-10-11T08:29:29Z.
Windows data point, two more catalog titles. Head
85c43cdemerged ontomainf60923bf, Windows 11 (26300), WinLibs GCC 15.2.0 r7, Release, titles relinked with their ownlibc.prxunwrapped:mainFAIL: unresolved ELF import 04AjkP0jO9U from libkernel.prx7Psx1DlAyE4(getprogname, #3347)guest_umtx_oppasses 3/3 here. With #3347 on top as well, the OpenGL showcase loads, brings up Mesa and stops in the shader recompiler (#2631), and ProsperoRadio stops atsetcontext.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T09:22:15Z.
Review: APPROVED
Thanks all for the testing. Pushed the review fixes on top of your commit:
UMTX_OP_WAIT_UINT/_PRIVATEnow pass the fullvaland return 0 at once when it doesn't fit in 32 bits (FreeBSD's zero-extended compare), after the timeout/null/alignment checks; test flipped. Commit by @babyhuey (help/umtx-op-1482), cherry-picked.NotImplemented_nid_no_patch; operation 0 keepsEOPNOTSUPP.timespec(negative seconds, nanoseconds outside [0, 1e9)) throws instead of returningEINVAL. A libc microsecond value can't be told apart from atimespecby the size alone, so a value that reads as a validtimespecis waited on as one; this is in TechnicalDebt.EINVALtests now pass a size.Rebased on main;
guest_umtx_opandguest_sync_on_addresspass; the test no longer links without the change.