libkernel: implement _umtx_op #414

Closed
github-bot wants to merge 1 commit from gh-pr/414/head into gh-pr/414/base
Owner

@⁠MehmetCambaz on GitHub · 2026-08-30 10:01 UTC · original
MehmetCambaz:feat/umtx-op → main · +129 −0 · 1 files · 1 commits · closed without merging

<html>

Problem

Sony's libkernel exports FreeBSD's _umtx_op and nothing in the tree implements it. Every blocking pthread wait and every wake is built on this one call, so a title whose threads contend on anything at all makes no progress.

PAC-MAN WORLD 2 Re-PAC (PPSA18189, reported in #411) called it around 15,000 times a second from two of its own modules and never rendered a frame: no shader translated, no draw submitted, and the graphics thread parked on flip and end-of-pipe queues that timed out forever.

This is the answer to your question on #411 — it is not the dump, and it is not AGC v13. With this implemented the title reaches its 3D scenes and holds 60 fps on 4498d75:

  | before | after -- | -- | -- draws (prim_type) | 0 | 27,891 flips | 0 | 11,161 equeue timeouts | 276 | 20

How it was identified

Not guessed. The unresolved import stub was made to report its caller, that code page was read back out of guest memory and disassembled, and the wrapper around the call turned out to validate a timespec, pass operation 2, and return 60 on an expired timeout:

70b: cmpq $0x0,(%r8)     ; validate timespec.tv_sec
718: jle  0x72c
71a: movl $0x2,%esi      ; op = 2
72c: movl $0x3c,%eax     ; return 60

UMTX_OP_WAIT, UMTX_OP_WAKE, and ETIMEDOUT — which POSIX_ETIMEDOUT in errno.h already spells as 60. The name _umtx_op then hashes to the imported NID exactly.

Three decisions worth reviewing

A wake never wakes exactly one waiter. Buckets are shared between addresses, so waking one could wake a waiter on a neighbouring address and leave the intended thread asleep. A spurious wakeup costs a recheck; a missed one is a hang.

Waits are bounded even when the guest asks for an unbounded one. A wake raised before its wait began cannot be recovered, and the objects seen in practice hold their compare value at zero on both sides of a wake. Leaving the wait unbounded reproducibly hung the title before its first frame.

An unimplemented operation reports success, not an error. That is exactly what the unresolved stub did before, so a title reaching one behaves as it does today rather than meeting a new failure. The log names it once.

Risk to titles that already work

A title that never calls _umtx_op is untouched — the only additions are one entry in the Posix table and a static array of buckets. Tetris Forever makes 0 calls to it, and its flips and draw counts are unchanged.

Test plan

Windows 11 25H2, clang-cl 20.1.8, RTX 2060, on 4498d75.

  • ctest: 35/35.
  • PAC-MAN WORLD 2 Re-PAC: black screen → 3D scenes at 60 fps.
  • Tetris Forever: 0 calls, no change in behaviour, no errors.
  • No new test — this is a blocking primitive whose effect is only observable in a real title.

AI use

Written with AI assistance (Claude). It instrumented the import stub, disassembled the guest caller, wrote the implementation, and ran the builds and measurements on my machine. I reviewed the diff and the results.

</html>Problem

Sony's libkernel exports FreeBSD's _umtx_op and nothing in the tree implements it. Every blocking pthread wait and every wake is built on this one call, so a title whose threads contend on anything at all makes no progress.

PAC-MAN WORLD 2 Re-PAC (PPSA18189, reported in #411) called it around 15,000 times a second from two of its own modules and never rendered a frame: no shader translated, no draw submitted, and the graphics thread parked on flip and end-of-pipe queues that timed out forever.

This is the answer to your question on #411 — it is not the dump, and it is not AGC v13. With this implemented the title reaches its 3D scenes and holds 60 fps on 4498d75:

before	after

draws (prim_type) 0 27,891
flips 0 11,161
equeue timeouts 276 20
How it was identified

Not guessed. The unresolved import stub was made to report its caller, that code page was read back out of guest memory and disassembled, and the wrapper around the call turned out to validate a timespec, pass operation 2, and return 60 on an expired timeout:

70b: cmpq $0x0,(%r8) ; validate timespec.tv_sec
718: jle 0x72c
71a: movl $0x2,%esi ; op = 2
72c: movl $0x3c,%eax ; return 60

UMTX_OP_WAIT, UMTX_OP_WAKE, and ETIMEDOUT — which POSIX_ETIMEDOUT in errno.h already spells as 60. The name _umtx_op then hashes to the imported NID exactly.

Three decisions worth reviewing

A wake never wakes exactly one waiter. Buckets are shared between addresses, so waking one could wake a waiter on a neighbouring address and leave the intended thread asleep. A spurious wakeup costs a recheck; a missed one is a hang.

Waits are bounded even when the guest asks for an unbounded one. A wake raised before its wait began cannot be recovered, and the objects seen in practice hold their compare value at zero on both sides of a wake. Leaving the wait unbounded reproducibly hung the title before its first frame.

An unimplemented operation reports success, not an error. That is exactly what the unresolved stub did before, so a title reaching one behaves as it does today rather than meeting a new failure. The log names it once.

Risk to titles that already work

A title that never calls _umtx_op is untouched — the only additions are one entry in the Posix table and a static array of buckets. Tetris Forever makes 0 calls to it, and its flips and draw counts are unchanged.

Test plan

Windows 11 25H2, clang-cl 20.1.8, RTX 2060, on 4498d75.

ctest: 35/35.
PAC-MAN WORLD 2 Re-PAC: black screen → 3D scenes at 60 fps.
Tetris Forever: 0 calls, no change in behaviour, no errors.
No new test — this is a blocking primitive whose effect is only observable in a real title.
AI use

Written with AI assistance (Claude). It instrumented the import stub, disassembled the guest caller, wrote the implementation, and ran the builds and measurements on my machine. I reviewed the diff and the results.

**@⁠MehmetCambaz** on GitHub · 2026-08-30 10:01 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/414) `MehmetCambaz:feat/umtx-op` → `main` · +129 −0 · 1 files · 1 commits · closed without merging <html> <body> <!--StartFragment--><h3 dir="ltr">Problem</h3><p dir="ltr">Sony's libkernel exports FreeBSD's <code>_umtx_op</code> and nothing in the tree implements it. Every blocking pthread wait and every wake is built on this one call, so a title whose threads contend on anything at all makes no progress.</p><p dir="ltr">PAC-MAN WORLD 2 Re-PAC (PPSA18189, reported in <a href="https://github.com/MehmetCambaz/KytyPS5/issues/411" target="_blank" rel="noreferrer">#411</a>) called it around <strong>15,000 times a second</strong> from two of its own modules and never rendered a frame: no shader translated, no draw submitted, and the graphics thread parked on flip and end-of-pipe queues that timed out forever.</p><p dir="ltr">This is the answer to your question on <a href="https://github.com/MehmetCambaz/KytyPS5/issues/411" target="_blank" rel="noreferrer">#411</a> — it is not the dump, and it is not AGC v13. With this implemented the title reaches its 3D scenes and holds 60 fps on <code>4498d75</code>:</p><div style="">   | before | after -- | -- | -- draws (prim_type) | 0 | 27,891 flips | 0 | 11,161 equeue timeouts | 276 | 20 </div><h3 dir="ltr">How it was identified</h3><p dir="ltr">Not guessed. The unresolved import stub was made to report its caller, that code page was read back out of guest memory and disassembled, and the wrapper around the call turned out to validate a timespec, pass operation 2, and return 60 on an expired timeout:</p><div><div style=""><div><div style=""><pre>70b: cmpq $0x0,(%r8) ; validate timespec.tv_sec 718: jle 0x72c 71a: movl $0x2,%esi ; op = 2 72c: movl $0x3c,%eax ; return 60</pre></div><div><div></div></div></div></div></div><p dir="ltr"><code>UMTX_OP_WAIT</code>, <code>UMTX_OP_WAKE</code>, and <code>ETIMEDOUT</code> — which <code>POSIX_ETIMEDOUT</code> in <code>errno.h</code> already spells as 60. The name <code>_umtx_op</code> then hashes to the imported NID exactly.</p><h3 dir="ltr">Three decisions worth reviewing</h3><p dir="ltr"><strong>A wake never wakes exactly one waiter.</strong> Buckets are shared between addresses, so waking one could wake a waiter on a neighbouring address and leave the intended thread asleep. A spurious wakeup costs a recheck; a missed one is a hang.</p><p dir="ltr"><strong>Waits are bounded even when the guest asks for an unbounded one.</strong> A wake raised before its wait began cannot be recovered, and the objects seen in practice hold their compare value at zero on both sides of a wake. Leaving the wait unbounded reproducibly hung the title before its first frame.</p><p dir="ltr"><strong>An unimplemented operation reports success, not an error.</strong> That is exactly what the unresolved stub did before, so a title reaching one behaves as it does today rather than meeting a new failure. The log names it once.</p><h3 dir="ltr">Risk to titles that already work</h3><p dir="ltr">A title that never calls <code>_umtx_op</code> is untouched — the only additions are one entry in the Posix table and a static array of buckets. Tetris Forever makes <strong>0</strong> calls to it, and its flips and draw counts are unchanged.</p><h3 dir="ltr">Test plan</h3><p dir="ltr">Windows 11 25H2, clang-cl 20.1.8, RTX 2060, on <code>4498d75</code>.</p><ul dir="ltr"><li style=""><input disabled="" readonly="" type="checkbox" checked=""> <code>ctest</code>: 35/35.</li><li style=""><input disabled="" readonly="" type="checkbox" checked=""> PAC-MAN WORLD 2 Re-PAC: black screen → 3D scenes at 60 fps.</li><li style=""><input disabled="" readonly="" type="checkbox" checked=""> Tetris Forever: 0 calls, no change in behaviour, no errors.</li><li style=""><input disabled="" readonly="" type="checkbox" checked=""> No new test — this is a blocking primitive whose effect is only observable in a real title.</li></ul><h3 dir="ltr">AI use</h3><p dir="ltr">Written with AI assistance (Claude). It instrumented the import stub, disassembled the guest caller, wrote the implementation, and ran the builds and measurements on my machine. I reviewed the diff and the results.</p><!--EndFragment--> </body> </html>Problem Sony's libkernel exports FreeBSD's _umtx_op and nothing in the tree implements it. Every blocking pthread wait and every wake is built on this one call, so a title whose threads contend on anything at all makes no progress. PAC-MAN WORLD 2 Re-PAC (PPSA18189, reported in [#411](https://github.com/MehmetCambaz/KytyPS5/issues/411)) called it around 15,000 times a second from two of its own modules and never rendered a frame: no shader translated, no draw submitted, and the graphics thread parked on flip and end-of-pipe queues that timed out forever. This is the answer to your question on [#411](https://github.com/MehmetCambaz/KytyPS5/issues/411) — it is not the dump, and it is not AGC v13. With this implemented the title reaches its 3D scenes and holds 60 fps on 4498d75: before after draws (prim_type) 0 27,891 flips 0 11,161 equeue timeouts 276 20 How it was identified Not guessed. The unresolved import stub was made to report its caller, that code page was read back out of guest memory and disassembled, and the wrapper around the call turned out to validate a timespec, pass operation 2, and return 60 on an expired timeout: 70b: cmpq $0x0,(%r8) ; validate timespec.tv_sec 718: jle 0x72c 71a: movl $0x2,%esi ; op = 2 72c: movl $0x3c,%eax ; return 60 UMTX_OP_WAIT, UMTX_OP_WAKE, and ETIMEDOUT — which POSIX_ETIMEDOUT in errno.h already spells as 60. The name _umtx_op then hashes to the imported NID exactly. Three decisions worth reviewing A wake never wakes exactly one waiter. Buckets are shared between addresses, so waking one could wake a waiter on a neighbouring address and leave the intended thread asleep. A spurious wakeup costs a recheck; a missed one is a hang. Waits are bounded even when the guest asks for an unbounded one. A wake raised before its wait began cannot be recovered, and the objects seen in practice hold their compare value at zero on both sides of a wake. Leaving the wait unbounded reproducibly hung the title before its first frame. An unimplemented operation reports success, not an error. That is exactly what the unresolved stub did before, so a title reaching one behaves as it does today rather than meeting a new failure. The log names it once. Risk to titles that already work A title that never calls _umtx_op is untouched — the only additions are one entry in the Posix table and a static array of buckets. Tetris Forever makes 0 calls to it, and its flips and draw counts are unchanged. Test plan Windows 11 25H2, clang-cl 20.1.8, RTX 2060, on 4498d75. ctest: 35/35. PAC-MAN WORLD 2 Re-PAC: black screen → 3D scenes at 60 fps. Tetris Forever: 0 calls, no change in behaviour, no errors. No new test — this is a blocking primitive whose effect is only observable in a real title. AI use Written with AI assistance (Claude). It instrumented the import stub, disassembled the guest caller, wrote the implementation, and ran the builds and measurements on my machine. I reviewed the diff and the results.
Sony's libkernel exports FreeBSD's _umtx_op, and nothing in the tree implemented
it. Every blocking pthread wait and every wake is built on this one call, so a
title whose threads contend on anything at all makes no progress without it.

PAC-MAN WORLD 2 Re-PAC (PPSA18189) called it around 15000 times a second from
two of its own modules and never rendered a frame: no shader was ever
translated, no draw was ever submitted, and the graphics thread sat on flip and
end-of-pipe event queues that timed out forever. With the call implemented the
same title reaches its 3D scenes and holds 60 fps.

Identified from the guest side rather than guessed. The unresolved import stub
was made to report its caller, the caller's code page was read back out of guest
memory and disassembled, and the wrapper around the call validates a timespec,
passes operation 2, and returns 60 on an expired timeout. That is UMTX_OP_WAIT,
UMTX_OP_WAKE and ETIMEDOUT, which POSIX_ETIMEDOUT in errno.h already spells 60.
The name then hashes to the imported NID exactly.

Three details are the difference between working and hanging:

A wake never wakes exactly one waiter. Buckets are shared between addresses, so
waking one could wake a waiter on a neighbouring address and leave the intended
thread asleep. Waking a thread that did not need it is a spurious wakeup and
every caller of this primitive rechecks its own condition; failing to wake the
one that did need it is a hang.

Waits are bounded even when the guest asks for an unbounded one. A wake raised
before its wait began cannot be recovered afterwards, and the objects seen in
practice hold their compare value at zero on both sides of a wake, so nothing in
the value tells a late waiter that it already missed its turn. Leaving the wait
unbounded reproducibly hung the title before its first frame.

An operation that is not implemented reports success rather than an error. That
is what the unresolved import stub did before this existed, so a title that
reaches one behaves exactly as it does today instead of meeting a new failure.
The log names the operation once so the gap stays visible.

A title that never calls _umtx_op is untouched: the only additions are one entry
in the Posix table and a static array of buckets.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:19:35 +00:00
github-bot closed this pull request 2026-11-26 09:19:35 +00:00

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.