fix(libkernel): give a raise target time to run between host-code samples #10076

Merged
JJenkx merged 1 commit from upstream-pr/10076 into main 2026-10-23 11:45:10 +00:00
JJenkx commented 2026-10-23 08:36:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-23T08:36:13Z.


What

On Windows, sceKernelRaiseException (RaiseOn) suspends the target and reads its context. While the target is in host code, it resumes the target and samples again right after SwitchToThread (#7652), until the target is back in guest code or the 50 ms host-code limit (#8512) runs out.

On this machine the resample came so fast that the target barely ran between samples. guest_raise_in_host_code then timed out at 30 s on every run of main, including on its own (3 of 3, ctest -R ^guest_raise_in_host_code$).

With a local fprintf at the delivery decision (not part of this PR), in the raise that hung:

  • 4,989 samples over 50.0 ms all found the writer in ucrtbase;
  • the limit then ran out and the handler was redirected into fwrite;
  • it deadlocked there on the stream lock that the main thread's fputc needs.

In a raise that did get through, the target sat at one RIP for 14,695 of 14,703 samples and the raise took 145 ms. The writer is not mostly in host code: run alone, its loop spends 39–40% of its time in fwrite/fseek and the rest in its own spin. It was the sampling that kept it from leaving.

RaiseOn now spins for 20 µs (YieldProcessor) before each new host-code sample, so the target gets to run. Other retries keep SwitchToThread. The 50 ms limit, the system-call extension and the wait and NtContinue/winpthreads handling are unchanged. The libkernel debt entry for the Windows raise records the spin and that 20 µs is a measured choice.

Tested

Windows 11 Enterprise 22H2 (10.0.22621), AMD Ryzen 9 5950X (16 cores / 32 threads), MinGW-w64 GCC 15.2 UCRT r7, Release.

head guest_raise_in_host_code longest raise raises redirected in host code
main 21554447d0 / 72fe76ff89 timeout (30 s) in every run: 1 in the full suite, 3 alone, 1 instrumented — the last raise, after 50 ms
this change (instrumented copy) 5/5 passed 0.46–0.53 ms 0 of 100 per run
this PR fcc01c57a6 3/3 passed 0.34–0.35 ms —

On this PR, guest_raise_exception, guest_raise_nested_sleep, guest_raise_in_system_call and guest_raise_in_host_code all passed in 3 of 3 runs. guest_raise_exception took 14.2 s here, run alone. In the full main suite (ctest -j 8) it passed in 20.1 s, but that is not a matched comparison, so I make no timing claim. tools/check_conventions.py passes.

Not tested: other CPUs and Windows builds. #7652's author saw the test pass, so whether SwitchToThread alone is fast enough probably depends on the machine. Linux and macOS do not use this path.

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 (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4980095834 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10076). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-23T08:36:13Z. --- ### What On Windows, `sceKernelRaiseException` (`RaiseOn`) suspends the target and reads its context. While the target is in host code, it resumes the target and samples again right after `SwitchToThread` (#7652), until the target is back in guest code or the 50 ms host-code limit (#8512) runs out. On this machine the resample came so fast that the target barely ran between samples. `guest_raise_in_host_code` then timed out at 30 s on every run of `main`, including on its own (3 of 3, `ctest -R ^guest_raise_in_host_code$`). With a local `fprintf` at the delivery decision (not part of this PR), in the raise that hung: - 4,989 samples over 50.0 ms all found the writer in `ucrtbase`; - the limit then ran out and the handler was redirected into `fwrite`; - it deadlocked there on the stream lock that the main thread's `fputc` needs. In a raise that did get through, the target sat at one RIP for 14,695 of 14,703 samples and the raise took 145 ms. The writer is not mostly in host code: run alone, its loop spends 39–40% of its time in `fwrite`/`fseek` and the rest in its own spin. It was the sampling that kept it from leaving. `RaiseOn` now spins for 20 µs (`YieldProcessor`) before each new host-code sample, so the target gets to run. Other retries keep `SwitchToThread`. The 50 ms limit, the system-call extension and the wait and `NtContinue`/winpthreads handling are unchanged. The libkernel debt entry for the Windows raise records the spin and that 20 µs is a measured choice. ### Tested Windows 11 Enterprise 22H2 (10.0.22621), AMD Ryzen 9 5950X (16 cores / 32 threads), MinGW-w64 GCC 15.2 UCRT r7, Release. | head | `guest_raise_in_host_code` | longest raise | raises redirected in host code | |---|---|---|---| | `main` `21554447d0` / `72fe76ff89` | timeout (30 s) in every run: 1 in the full suite, 3 alone, 1 instrumented | — | the last raise, after 50 ms | | this change (instrumented copy) | 5/5 passed | 0.46–0.53 ms | 0 of 100 per run | | this PR `fcc01c57a6` | 3/3 passed | 0.34–0.35 ms | — | On this PR, `guest_raise_exception`, `guest_raise_nested_sleep`, `guest_raise_in_system_call` and `guest_raise_in_host_code` all passed in 3 of 3 runs. `guest_raise_exception` took 14.2 s here, run alone. In the full `main` suite (`ctest -j 8`) it passed in 20.1 s, but that is not a matched comparison, so I make no timing claim. `tools/check_conventions.py` passes. Not tested: other CPUs and Windows builds. #7652's author saw the test pass, so whether `SwitchToThread` alone is fast enough probably depends on the machine. Linux and macOS do not use this path. ### Checklist - [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 (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!10076
No description provided.