fix(libkernel): post guest raises to the delivery drain mailbox on Windows #12005
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12005"
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 akkadbakkad89 on 2026-10-27T16:41:00Z.
What
Fixes the
guest_raise_exceptionregression reported in #11813 (3x slowdown, occasional 5 sAddressWakingtimeout) introduced by540364a24.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 leftArmReturnBreakas the only vehicle, and the unwind's raw stack-pop fallback misses ~99.5% of attempts on thesceKernelSyncOnAddressWakespinner stack shape (16,120not-a-return-siterejections measured; raises surviving 100-1100+ deliverer sweeps).Instead of an APC,
TryDelivernow posts theQueuedDeliveryonce into a per-thread mailbox inTimedWait, drained byDrainApcs— the same place deferred APCs already run. No APC is queued, so host waits keep their contract (insideAlertableWaitstays 0), no handler runs under a host lock, and everyDeferringDeliveryboundary (sceKernelSyncOnAddressWake, waits, semaphore/unpark paths) becomes a deterministic delivery point.Delivery vehicles race through
takenexactly as before. Two guards cover stale entries:closed(set when the queued delivery is released by any owner) and the thread's_finishedflag, so an entry popped after the raise settled — or on a recycled thread id — is dropped instead of delivered.waitCount > 0targets 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/TryDeliverregion.Tested
Windows 11, i7-10700,
guest_raise_exception_tests.exe6/6 PASS at ~10.5-11.5 s (parent-commit timing; regression window measured 11-57 s depending on host) andguest_raise_leaves_host_code_tests.exe7/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 50AddressWakingrounds included) and 1 stale entry correctly skipped. No games run.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)