fix(libkernel): post guest raises to the delivery drain mailbox on Windows #12005

Merged
akkadbakkad89 merged 2 commits from upstream-pr/12005 into main 2026-11-02 12:29:04 +00:00
akkadbakkad89 commented 2026-10-27 16:41:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by akkadbakkad89 on 2026-10-27T16:41:00Z.


What

Fixes the guest_raise_exception regression reported in #11813 (3x slowdown, occasional 5 s AddressWaking timeout) introduced by 540364a24.

That commit was right to stop queuing APCs to arbitrary host-code targets: an APC is observable (the wait returns WAIT_IO_COMPLETION) and its callback must not run a guest handler while the target holds host locks. But for a host-code target that is not inside a libkernel wait it left ArmReturnBreak as the only vehicle, and the unwind's raw stack-pop fallback misses ~99.5% of attempts on the sceKernelSyncOnAddressWake spinner stack shape (16,120 not-a-return-site rejections measured; raises surviving 100-1100+ deliverer sweeps).

Instead of an APC, TryDeliver now posts the QueuedDelivery once into a per-thread mailbox in TimedWait, drained by DrainApcs — the same place deferred APCs already run. No APC is queued, so host waits keep their contract (insideAlertableWait stays 0), no handler runs under a host lock, and every DeferringDelivery boundary (sceKernelSyncOnAddressWake, waits, semaphore/unpark paths) becomes a deterministic delivery point.

Delivery vehicles race through taken exactly as before. Two guards cover stale entries: closed (set when the queued delivery is released by any owner) and the thread's _finished flag, so an entry popped after the raise settled — or on a recycled thread id — is dropped instead of delivered. waitCount > 0 targets keep the existing APC path unchanged.

Related: #8592 adds a target-side deferral counter for shader-preparation host locks. Complementary — this PR's mailbox only fires at libkernel drain points; whichever lands second rebases the ThreadWaitState/TryDeliver region.

Tested

Windows 11, i7-10700, guest_raise_exception_tests.exe 6/6 PASS at ~10.5-11.5 s (parent-commit timing; regression window measured 11-57 s depending on host) and guest_raise_leaves_host_code_tests.exe 7/7 PASS: deliveries 40, outside the image 0, before the alertable wait 10, inside an alertable host wait 0. Instrumented run showed 52 drain-mailbox deliveries (the 50 AddressWaking rounds included) and 1 stale entry correctly skipped. No games run.

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: #11813
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5037657069 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12005). Originally posted by [akkadbakkad89](https://github.com/akkadbakkad89) on 2026-10-27T16:41:00Z. --- ### What Fixes the `guest_raise_exception` regression reported in #11813 (3x slowdown, occasional 5 s `AddressWaking` timeout) introduced by `540364a24`. That commit was right to stop queuing APCs to arbitrary host-code targets: an APC is observable (the wait returns `WAIT_IO_COMPLETION`) and its callback must not run a guest handler while the target holds host locks. But for a host-code target that is not inside a libkernel wait it left `ArmReturnBreak` as the only vehicle, and the unwind's raw stack-pop fallback misses ~99.5% of attempts on the `sceKernelSyncOnAddressWake` spinner stack shape (16,120 `not-a-return-site` rejections measured; raises surviving 100-1100+ deliverer sweeps). Instead of an APC, `TryDeliver` now posts the `QueuedDelivery` once into a per-thread mailbox in `TimedWait`, drained by `DrainApcs` — the same place deferred APCs already run. No APC is queued, so host waits keep their contract (`insideAlertableWait` stays 0), no handler runs under a host lock, and every `DeferringDelivery` boundary (`sceKernelSyncOnAddressWake`, waits, semaphore/unpark paths) becomes a deterministic delivery point. Delivery vehicles race through `taken` exactly as before. Two guards cover stale entries: `closed` (set when the queued delivery is released by any owner) and the thread's `_finished` flag, so an entry popped after the raise settled — or on a recycled thread id — is dropped instead of delivered. `waitCount > 0` targets keep the existing APC path unchanged. Related: #8592 adds a target-side deferral counter for shader-preparation host locks. Complementary — this PR's mailbox only fires at libkernel drain points; whichever lands second rebases the `ThreadWaitState`/`TryDeliver` region. ### Tested Windows 11, i7-10700, `guest_raise_exception_tests.exe` 6/6 PASS at ~10.5-11.5 s (parent-commit timing; regression window measured 11-57 s depending on host) and `guest_raise_leaves_host_code_tests.exe` 7/7 PASS: `deliveries 40, outside the image 0, before the alertable wait 10, inside an alertable host wait 0`. Instrumented run showed 52 drain-mailbox deliveries (the 50 `AddressWaking` rounds included) and 1 stale entry correctly skipped. No games run. ### 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: #11813 - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
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!12005
No description provided.