feat(libkernel_sys): write the AMM commands and wait for their completion #10799

Merged
EagleBearerYS merged 5 commits from upstream-pr/10799 into main 2026-10-27 02:00:12 +00:00
EagleBearerYS commented 2026-10-25 01:44:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-25T01:44:25Z.


What

Implements 13 of libkernel_sys's AMPR command exports on the libSceAmpr encoders:

  • the twelve AMM writers: sceKernelWriteMapCommand, sceKernelWriteMapWithGpuMaskIdCommand, sceKernelWriteMapDirectCommand, sceKernelWriteMapDirectWithGpuMaskIdCommand, sceKernelWriteUnmapCommand, sceKernelWriteRemapCommand, sceKernelWriteRemapWithGpuMaskIdCommand, sceKernelWriteMultiMapCommand, sceKernelWriteMultiMapWithGpuMaskIdCommand, sceKernelWriteModifyProtectCommand, sceKernelWriteModifyProtectWithGpuMaskIdCommand, sceKernelWriteModifyMtypeProtectWithGpuMaskIdCommand;
  • sceKernelWaitCommandBufferCompletion.

The AMM range and protection checks and the records they build move from libSceAmpr into the APR command buffer header that libSceAmpr and libkernel already share. Each kernel writer records exactly what its libSceAmpr command buffer sibling appends, with the same errors.

  • Signatures follow ampr_emu's libkernel hook targets (cfa85df379f6, include/ampr_libkernel_hook_targets.h), the commit the tree's AMPR debt entries already cite.
  • A null dst only reports the size, as for sceKernelWriteModifyMtypeProtectCommand.
  • A type with bits above 32 is cut to the record's 32-bit field, as sceKernelWriteModifyMtypeProtectCommand does and as the siblings' 32-bit type implies. The console's handling isn't measured; the debt entry says so, and guest_kernel_sys pins the behaviour (fe9d31edae).
  • The wait returns 0 once the AMM submission has run and SCE_KERNEL_ERROR_ESRCH for an id that was never submitted, as sceAmprAmmWaitCommandBufferCompletion does.
  • The completion wait checks the AMM queue's own submitted count (a666c83d20). The id each submission is given was stored for the wait after the queue's submit lock was released, so two concurrent submitters could store out of order, and a wait for the later id failed with ESRCH. The wait now compares the id with the AMM queue's submitted count, which _submit raises under the queue mutex before it returns. This also changes main's sceAmprAmmWaitCommandBufferCompletion, which uses the same check.

The five APR writers (WriteAddress, WriteCounter, WaitOnAddress, WaitOnCounter, EventQueueOnCompletion) stay NotImplemented: no source gives their kernel signatures.

Tested

  • GitHub's runners (run 40149380911): guest_kernel_sys passes 10 of 10 on Linux and on Windows, 0 skipped. The libc and libkernel suites pass: Linux 143 and 206 tests run, Windows 170 and 222, 0 failed. Main's implementation doesn't build against the new test, because main declares the writers without parameters.
  • fe9d31edae, the wide type cases (run): guest_kernel_sys and the AMPR tests (guest_ampr_markers, guest_amm_placement, guest_ampr_threads, guest_ampr_cpp_error and ^guest_apr_read_) pass 10 runs in a row. Linux: 10 run, 10 pass, 0 skip, 0 fail. Windows: 12 run, 12 pass, 0 skip, 0 fail. The new cases check that type 0x1_0000_0003 records the same bytes as the libSceAmpr sibling given 3, for the map and map direct writers, with and without the GPU mask id, and for the mtype-protect writer. They pin behaviour the previous head already had. The libc and libkernel suites pass on both (143 and 206 tests on Linux, 170 and 222 on Windows), run with APS5_SKIP_TIMING_TESTS=1, which relaxes precise_wait's median bounds on GitHub's hosted runners. Main's implementation doesn't build against the test.
  • a666c83d20, the completion wait (run), red/green against fe9d31edae's implementation: guest_kernel_sys gains a case where four threads submit 4,000 empty AMM buffers each while another thread waits on the newest id any of them has published, through sceKernelWaitCommandBufferCompletion and sceAmprAmmWaitCommandBufferCompletion, and every wait must succeed.
    • Linux: with fe9d31edae's implementation it fails in all 3 runs, at that check. With the fix, guest_kernel_sys and the AMPR tests pass 10 runs in a row: 10 run, 10 pass, 0 skip, 0 fail.
    • Windows: fe9d31edae's implementation passed its 3 runs, as the interleaving is timing-dependent. With the fix: 12 run, 12 pass, 0 skip, 0 fail, 10 runs in a row.
    • With the window widened: a scratch build of fe9d31edae's implementation with a 200 µs sleep between the submit and the stored id, not part of this PR, fails the case in all 3 runs on both runners (run).
    • The libc and libkernel suites pass on both (143 and 206 tests on Linux, 170 and 222 on Windows), run with APS5_SKIP_TIMING_TESTS=1, which relaxes precise_wait's median bounds on GitHub's hosted runners.
  • Red/green per commit (Linux, Ubuntu 24.04, gcc 13.3, CI's configure flags): with each commit's product change reverted and its test kept, the writer checks fail 41 of 41 (12 of 12 writers) and the wait checks 4 of 4; with it, all pass.
  • guest_kernel_sys compares every writer with its libSceAmpr sibling on valid and invalid arguments, with and without a destination and a size output. It also submits a map written by sceKernelWriteMapCommand, waits for it with sceKernelWaitCommandBufferCompletion, and uses the mapped page.
  • The AMPR tests (guest_ampr_markers, guest_amm_placement, guest_ampr_threads, guest_ampr_cpp_error, guest_apr_read_*) pass unchanged.
  • Full ctest on Linux: 2,191 tests, 2,096 passed, 90 skipped, 5 failed; the 5 are IPv6 tests that host cannot run.

No title tested.

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. Merge order with Gruzzberry's #11031 (getfsstat), #11032 (kenv) and #11034 (swapon), adjacent lines in docs/dev/debt/libkernel_sys.md and core/libs/tests/GuestKernelSys.cpp, and with #11216 (kldload) and #11231 (kldsym), neighbouring lines: this PR first, then those rebase onto it
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

Related: #9632 (libkernel_sys, separate rows).

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5000875531 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10799). Originally posted by [EagleBearerYS](https://github.com/EagleBearerYS) on 2026-10-25T01:44:25Z. --- ### What Implements 13 of libkernel_sys's AMPR command exports on the libSceAmpr encoders: - the twelve AMM writers: sceKernelWriteMapCommand, sceKernelWriteMapWithGpuMaskIdCommand, sceKernelWriteMapDirectCommand, sceKernelWriteMapDirectWithGpuMaskIdCommand, sceKernelWriteUnmapCommand, sceKernelWriteRemapCommand, sceKernelWriteRemapWithGpuMaskIdCommand, sceKernelWriteMultiMapCommand, sceKernelWriteMultiMapWithGpuMaskIdCommand, sceKernelWriteModifyProtectCommand, sceKernelWriteModifyProtectWithGpuMaskIdCommand, sceKernelWriteModifyMtypeProtectWithGpuMaskIdCommand; - sceKernelWaitCommandBufferCompletion. The AMM range and protection checks and the records they build move from libSceAmpr into the APR command buffer header that libSceAmpr and libkernel already share. Each kernel writer records exactly what its libSceAmpr command buffer sibling appends, with the same errors. - Signatures follow ampr_emu's libkernel hook targets ([cfa85df379f6, include/ampr_libkernel_hook_targets.h](https://github.com/drakmor/ampr_emu/blob/cfa85df379f6eeeb165d7badf9b648e266fe77b7/include/ampr_libkernel_hook_targets.h)), the commit the tree's AMPR debt entries already cite. - A null `dst` only reports the size, as for sceKernelWriteModifyMtypeProtectCommand. - A `type` with bits above 32 is cut to the record's 32-bit field, as sceKernelWriteModifyMtypeProtectCommand does and as the siblings' 32-bit `type` implies. The console's handling isn't measured; the debt entry says so, and `guest_kernel_sys` pins the behaviour (fe9d31edae). - The wait returns 0 once the AMM submission has run and SCE_KERNEL_ERROR_ESRCH for an id that was never submitted, as sceAmprAmmWaitCommandBufferCompletion does. - **The completion wait checks the AMM queue's own submitted count** (a666c83d20). The id each submission is given was stored for the wait after the queue's submit lock was released, so two concurrent submitters could store out of order, and a wait for the later id failed with ESRCH. The wait now compares the id with the AMM queue's `submitted` count, which `_submit` raises under the queue mutex before it returns. This also changes `main`'s sceAmprAmmWaitCommandBufferCompletion, which uses the same check. The five APR writers (WriteAddress, WriteCounter, WaitOnAddress, WaitOnCounter, EventQueueOnCompletion) stay NotImplemented: no source gives their kernel signatures. ### Tested - **GitHub's runners** ([run 40149380911](https://github.com/EagleBearerYS/AnyPS5/actions/runs/40149380911)): `guest_kernel_sys` passes 10 of 10 on Linux and on Windows, 0 skipped. The libc and libkernel suites pass: Linux 143 and 206 tests run, Windows 170 and 222, 0 failed. Main's implementation doesn't build against the new test, because main declares the writers without parameters. - **fe9d31edae, the wide `type` cases** ([run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/40155988210)): `guest_kernel_sys` and the AMPR tests (`guest_ampr_markers`, `guest_amm_placement`, `guest_ampr_threads`, `guest_ampr_cpp_error` and `^guest_apr_read_`) pass 10 runs in a row. Linux: 10 run, 10 pass, 0 skip, 0 fail. Windows: 12 run, 12 pass, 0 skip, 0 fail. The new cases check that `type` 0x1_0000_0003 records the same bytes as the libSceAmpr sibling given 3, for the map and map direct writers, with and without the GPU mask id, and for the mtype-protect writer. They pin behaviour the previous head already had. The libc and libkernel suites pass on both (143 and 206 tests on Linux, 170 and 222 on Windows), run with `APS5_SKIP_TIMING_TESTS=1`, which relaxes `precise_wait`'s median bounds on GitHub's hosted runners. Main's implementation doesn't build against the test. - **a666c83d20, the completion wait** ([run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/40161321640)), red/green against fe9d31edae's implementation: `guest_kernel_sys` gains a case where four threads submit 4,000 empty AMM buffers each while another thread waits on the newest id any of them has published, through sceKernelWaitCommandBufferCompletion and sceAmprAmmWaitCommandBufferCompletion, and every wait must succeed. - **Linux:** with fe9d31edae's implementation it fails in all 3 runs, at that check. With the fix, `guest_kernel_sys` and the AMPR tests pass 10 runs in a row: 10 run, 10 pass, 0 skip, 0 fail. - **Windows:** fe9d31edae's implementation passed its 3 runs, as the interleaving is timing-dependent. With the fix: 12 run, 12 pass, 0 skip, 0 fail, 10 runs in a row. - **With the window widened:** a scratch build of fe9d31edae's implementation with a 200 µs sleep between the submit and the stored id, not part of this PR, fails the case in all 3 runs on both runners ([run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/40161362794)). - The libc and libkernel suites pass on both (143 and 206 tests on Linux, 170 and 222 on Windows), run with `APS5_SKIP_TIMING_TESTS=1`, which relaxes `precise_wait`'s median bounds on GitHub's hosted runners. - **Red/green per commit** (Linux, Ubuntu 24.04, gcc 13.3, CI's configure flags): with each commit's product change reverted and its test kept, the writer checks fail 41 of 41 (12 of 12 writers) and the wait checks 4 of 4; with it, all pass. - `guest_kernel_sys` compares every writer with its libSceAmpr sibling on valid and invalid arguments, with and without a destination and a size output. It also submits a map written by sceKernelWriteMapCommand, waits for it with sceKernelWaitCommandBufferCompletion, and uses the mapped page. - The AMPR tests (guest_ampr_markers, guest_amm_placement, guest_ampr_threads, guest_ampr_cpp_error, guest_apr_read_*) pass unchanged. - Full ctest on Linux: 2,191 tests, 2,096 passed, 90 skipped, 5 failed; the 5 are IPv6 tests that host cannot run. No title tested. ### 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. Merge order with Gruzzberry's #11031 (`getfsstat`), #11032 (`kenv`) and #11034 (`swapon`), adjacent lines in `docs/dev/debt/libkernel_sys.md` and `core/libs/tests/GuestKernelSys.cpp`, and with #11216 (`kldload`) and #11231 (`kldsym`), neighbouring lines: this PR first, then those rebase onto it - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes Related: #9632 (libkernel_sys, separate rows).
MarcZX10 commented 2026-10-25 01:55:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T01:55:40Z.

Review: CHANGES_REQUESTED


I reviewed this against current main (941ace5) and the exact PR head afd5c4a20e3a0e6bd89021279b67bc43e59f9323. The encoding reuse and writer parity tests are thorough, but the new wait export has a concrete hang case in the shared implementation.\n\nAmmSubmitted_nid_no_patch(id) accepts every nonzero ID up to lastSubmit, then _waitSubmitted waits until queue.completed >= id || id > queue.submitted. If a prior submission was queued and a later ID was submitted through the regular (non-AMM) queue, that later ID can be <= lastSubmit but > the AMM queue’s submitted; sceKernelWaitCommandBufferCompletion then waits forever because neither predicate can become true. IDs are queue-local in _submit (each _queue(amm) tracks its own submitted/completed counters), while lastSubmit is only a scalar shared AMM marker. Please make the wait validate against the AMM queue’s own submitted range and ensure unknown IDs return without blocking; add a regression test that interleaves an AMM ID with a regular APR submission.\n\nI also noticed the new writers intentionally ignore gpuMaskId, matching the existing AMM encoder signatures, but I found no debt entry explaining that unsupported mask behavior. Please document whether the mask is intentionally ignored in this single-device emulator or preserve unsupported values until the behavior is evidenced.\n\ngit diff --check passes; I did not run the product suite locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5641020931 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10799#pullrequestreview-5641020931). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T01:55:40Z. **Review:** CHANGES_REQUESTED --- I reviewed this against current `main` (`941ace5`) and the exact PR head `afd5c4a20e3a0e6bd89021279b67bc43e59f9323`. The encoding reuse and writer parity tests are thorough, but the new wait export has a concrete hang case in the shared implementation.\n\n`AmmSubmitted_nid_no_patch(id)` accepts every nonzero ID up to `lastSubmit`, then `_waitSubmitted` waits until `queue.completed >= id || id > queue.submitted`. If a prior submission was queued and a later ID was submitted through the regular (non-AMM) queue, that later ID can be <= `lastSubmit` but > the AMM queue’s `submitted`; `sceKernelWaitCommandBufferCompletion` then waits forever because neither predicate can become true. IDs are queue-local in `_submit` (each `_queue(amm)` tracks its own submitted/completed counters), while `lastSubmit` is only a scalar shared AMM marker. Please make the wait validate against the AMM queue’s own submitted range and ensure unknown IDs return without blocking; add a regression test that interleaves an AMM ID with a regular APR submission.\n\nI also noticed the new writers intentionally ignore `gpuMaskId`, matching the existing AMM encoder signatures, but I found no debt entry explaining that unsupported mask behavior. Please document whether the mask is intentionally ignored in this single-device emulator or preserve unsupported values until the behavior is evidenced.\n\n`git diff --check` passes; I did not run the product suite locally.
MarcZX10 commented 2026-10-25 02:42:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T02:42:12Z.

Review: COMMENTED


Reviewed exact head afd5c4a20e3a0e6bd89021279b67bc43e59f9323. The shared command encoders preserve the existing libSceAmpr writer records and the new tests compare record bytes, size-query behavior, error paths and a submitted map/wait round trip. Current-main conventions and git diff --check pass in the PR worktree. The PR reports focused Linux/Windows guest test runs; the current hosted upstream run has policy checks green and product jobs skipped, and I did not rerun the integrated build locally. I left one question about the 64-bit type input being narrowed to a 32-bit command field; please resolve that before I approve.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5641207649 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10799#pullrequestreview-5641207649). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T02:42:12Z. **Review:** COMMENTED --- Reviewed exact head `afd5c4a20e3a0e6bd89021279b67bc43e59f9323`. The shared command encoders preserve the existing libSceAmpr writer records and the new tests compare record bytes, size-query behavior, error paths and a submitted map/wait round trip. Current-main conventions and `git diff --check` pass in the PR worktree. The PR reports focused Linux/Windows guest test runs; the current hosted upstream run has policy checks green and product jobs skipped, and I did not rerun the integrated build locally. I left one question about the 64-bit `type` input being narrowed to a 32-bit command field; please resolve that before I approve.
MarcZX10 commented 2026-10-25 04:11:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T04:11:28Z.

Review: CHANGES_REQUESTED


Correction to my earlier review: I rechecked the completion wait invariant. lastSubmit is updated only from AMM submissions, so the interleaved regular-APR ID scenario I described cannot by itself make a valid AMM ID wait forever; I withdraw that finding. The current head does address my earlier 64-bit type question in the body/debt and adds tests pinning the truncation behavior, though that remains an author-stated assumption rather than console evidence. I found a separate concurrency bug in the completion marker: AmmSubmit_nid_no_patch calls _submit, then stores its returned ID into lastSubmit after _submit has released the AMM queue submit lock. Two concurrent AMM submissions can therefore complete those steps out of order: id 1 returns and pauses, id 2 returns and stores 2, then id 1 stores 1. A later wait for the already-submitted id 2 is rejected as ESRCH. Please publish the AMM high-water mark monotonically (or update it while submissions are serialized) and add a concurrent regression that proves a later completed submission remains waitable after an earlier submitter publishes late. The exact current diff otherwise contains focused byte-parity and map/wait tests; I did not run product tests locally, and hosted product jobs are skipped.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5641450518 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10799#pullrequestreview-5641450518). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T04:11:28Z. **Review:** CHANGES_REQUESTED --- Correction to my earlier review: I rechecked the completion wait invariant. lastSubmit is updated only from AMM submissions, so the interleaved regular-APR ID scenario I described cannot by itself make a valid AMM ID wait forever; I withdraw that finding. The current head does address my earlier 64-bit type question in the body/debt and adds tests pinning the truncation behavior, though that remains an author-stated assumption rather than console evidence. I found a separate concurrency bug in the completion marker: AmmSubmit_nid_no_patch calls _submit, then stores its returned ID into lastSubmit after _submit has released the AMM queue submit lock. Two concurrent AMM submissions can therefore complete those steps out of order: id 1 returns and pauses, id 2 returns and stores 2, then id 1 stores 1. A later wait for the already-submitted id 2 is rejected as ESRCH. Please publish the AMM high-water mark monotonically (or update it while submissions are serialized) and add a concurrent regression that proves a later completed submission remains waitable after an earlier submitter publishes late. The exact current diff otherwise contains focused byte-parity and map/wait tests; I did not run product tests locally, and hosted product jobs are skipped.
MarcZX10 commented 2026-10-25 05:32:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T05:32:42Z.

Review: APPROVED


Reviewed the exact head a666c83d20 against current main e7e3e9c. The former publication race is removed: AMM waits validate the ID against queue.submitted while holding queue.mutex, the same counter lock used to allocate IDs in _submit, then wait on completion. This prevents an earlier submitter from overwriting a later ID, and rejects IDs not yet issued without blocking. The new concurrent regression drives four AMM submitters and a waiter through both wait exports; the author reports focused Linux and Windows runs, and the prior-head red/green evidence is documented in the PR. I independently ran the current conventions checker on this exact head; it passes. GitHub conventions, labels, eligibility, and report checks pass; hosted product build/test jobs are skipped, so I did not treat them as independent product validation. The PR branch is behind current main, so it still needs a rebase before merge.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5641724231 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10799#pullrequestreview-5641724231). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T05:32:42Z. **Review:** APPROVED --- Reviewed the exact head a666c83d20178370f700b21ed2c007978bfa99b5 against current main e7e3e9c. The former publication race is removed: AMM waits validate the ID against queue.submitted while holding queue.mutex, the same counter lock used to allocate IDs in _submit, then wait on completion. This prevents an earlier submitter from overwriting a later ID, and rejects IDs not yet issued without blocking. The new concurrent regression drives four AMM submitters and a waiter through both wait exports; the author reports focused Linux and Windows runs, and the prior-head red/green evidence is documented in the PR. I independently ran the current conventions checker on this exact head; it passes. GitHub conventions, labels, eligibility, and report checks pass; hosted product build/test jobs are skipped, so I did not treat them as independent product validation. The PR branch is behind current main, so it still needs a rebase before merge.
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!10799
No description provided.