fix(libkernel): zero sceKernelWaitEqueue's count on every failure as firmware 13.42 does #15123

Merged
BunnyFl merged 4 commits from upstream-pr/15123 into main 2026-11-15 19:15:55 +00:00
BunnyFl commented 2026-11-02 17:32:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by BunnyFl on 2026-11-02T17:32:55Z.


What

sceKernelWaitEqueue now stores 0 in the result count on every failure and rejects a non-positive event count before it looks at the queue, as firmware 13.42's does.

Firmware 13.42's sceKernelWaitEqueue (NID fzyMKs9kim0, libkernel.sprx at 0x1cf60, read from disassembly, not measured on a console):

1cf83: test %edx,%edx ; jle 0x1d042        num <= 0, checked first
1d042: movl $0x0,(%rbx) ; mov $0x80020016   *out = 0, EINVAL
1cfdc: call 0x1440                          (eq, 0, 0, ev, num, timeout or 0)
1cfe9: mov %eax,(%rbx) ; xor %eax,%eax      count > 0: *out = count, return 0
1d032: movl $0x0,(%rbx) ; mov $0x8002003c   count == 0: *out = 0, ETIMEDOUT
1d139: movl $0x0,(%rbx)                     count < 0 (EINTR retried): *out = 0,
1d14d: lea -0x7ffe0000(%rcx),%eax           return 0x80020000 + errno (0 if errno is 0)

So every error return leaves *out == 0. On main:

  • when the queue is deleted while a thread waits on it, *out got SCE_KERNEL_ERROR_EBADF (0x80020009), because the internal wait result was stored before it was checked;
  • the early EBADF (unknown queue), EFAULT (null event list) and EINVAL (count below 1) returns left *out unchanged;
  • an unknown queue with a count below 1 returned EBADF, where the firmware returns EINVAL.

A null result pointer still returns SCE_KERNEL_ERROR_EINVAL without a store; the firmware stores to it unchecked. That divergence and the untested null-event-list case are listed under Functional in docs/dev/debt/libkernel.md.

VerifyFailedWaitClearsCount in GuestEqueueEvents.cpp checks the result and count == 0 for counts 0 and −1, a null event list, a poll timeout, a deleted queue (with counts 2 and 0), and a queue deleted from another thread during a 1 s wait. #14680 adds return-code checks for the same calls to the same file; its expectations are unchanged by this PR.

Tested

Windows 11, WinLibs MinGW-w64 GCC 15.2.0 (UCRT, POSIX threads), BUILD_TESTING=ON.

  • guest_equeue_events passes. With main's Equeue.cpp and this PR's test, it aborts (0xc0000409).
  • The ctest tests of these seven executables, which also call sceKernelWaitEqueue (guest_ampr_tests, guest_http2_tests, guest_job_manager_failure_tests, guest_kernel_error_tests, guest_kqueue_tests, guest_socket_event_tests, guest_video_out_hdr_tests): 9 of 9 pass.
  • libSceJobManager waits through sceKernelWaitEqueue itself (Export.cpp:785), so I also ran every JobManager test (guest_job_manager_*, job_manager_compute_progress, and agc_driver_job_manager_compute* on the GPU): 31 of 31 pass.
  • tools/check_conventions.py --base upstream/main passes; tools/pr_overlap.py finds no overlap.
  • Not run on Linux or macOS.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5124615105 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15123). Originally posted by [BunnyFl](https://github.com/BunnyFl) on 2026-11-02T17:32:55Z. --- ### What `sceKernelWaitEqueue` now stores 0 in the result count on every failure and rejects a non-positive event count before it looks at the queue, as firmware 13.42's does. Firmware 13.42's `sceKernelWaitEqueue` (NID `fzyMKs9kim0`, `libkernel.sprx` at `0x1cf60`, read from disassembly, not measured on a console): ``` 1cf83: test %edx,%edx ; jle 0x1d042 num <= 0, checked first 1d042: movl $0x0,(%rbx) ; mov $0x80020016 *out = 0, EINVAL 1cfdc: call 0x1440 (eq, 0, 0, ev, num, timeout or 0) 1cfe9: mov %eax,(%rbx) ; xor %eax,%eax count > 0: *out = count, return 0 1d032: movl $0x0,(%rbx) ; mov $0x8002003c count == 0: *out = 0, ETIMEDOUT 1d139: movl $0x0,(%rbx) count < 0 (EINTR retried): *out = 0, 1d14d: lea -0x7ffe0000(%rcx),%eax return 0x80020000 + errno (0 if errno is 0) ``` So every error return leaves `*out == 0`. On `main`: - when the queue is deleted while a thread waits on it, `*out` got `SCE_KERNEL_ERROR_EBADF` (`0x80020009`), because the internal wait result was stored before it was checked; - the early EBADF (unknown queue), EFAULT (null event list) and EINVAL (count below 1) returns left `*out` unchanged; - an unknown queue with a count below 1 returned EBADF, where the firmware returns EINVAL. A null result pointer still returns `SCE_KERNEL_ERROR_EINVAL` without a store; the firmware stores to it unchecked. That divergence and the untested null-event-list case are listed under Functional in `docs/dev/debt/libkernel.md`. `VerifyFailedWaitClearsCount` in `GuestEqueueEvents.cpp` checks the result and `count == 0` for counts 0 and −1, a null event list, a poll timeout, a deleted queue (with counts 2 and 0), and a queue deleted from another thread during a 1 s wait. #14680 adds return-code checks for the same calls to the same file; its expectations are unchanged by this PR. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Windows 11, WinLibs MinGW-w64 GCC 15.2.0 (UCRT, POSIX threads), `BUILD_TESTING=ON`. - `guest_equeue_events` passes. With `main`'s `Equeue.cpp` and this PR's test, it aborts (`0xc0000409`). - The ctest tests of these seven executables, which also call `sceKernelWaitEqueue` (`guest_ampr_tests`, `guest_http2_tests`, `guest_job_manager_failure_tests`, `guest_kernel_error_tests`, `guest_kqueue_tests`, `guest_socket_event_tests`, `guest_video_out_hdr_tests`): 9 of 9 pass. - `libSceJobManager` waits through `sceKernelWaitEqueue` itself (`Export.cpp:785`), so I also ran every JobManager test (`guest_job_manager_*`, `job_manager_compute_progress`, and `agc_driver_job_manager_compute*` on the GPU): 31 of 31 pass. - `tools/check_conventions.py --base upstream/main` passes; `tools/pr_overlap.py` finds no overlap. - Not run on Linux or macOS. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!15123
No description provided.