fix(libkernel): implement sceKernelWriteModifyMtypeProtectCommand and… #4258

Closed
Dextroid17 wants to merge 1 commit from upstream-pr/4258 into main
Dextroid17 commented 2026-10-12 02:56:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-12T02:56:41Z.


… sceKernelWriteRemapIntoPrtCommand

Resolves NIDs hrEIwWDKm-w and RPAHEYSOt1s, absent from libkernel and needed by Trails in the Sky 2nd Chapter (PPSA35169). Both mirror existing, tested libSceAmpr recorders (sceAmprAmmCommandBufferModifyMtypeProtect and sceAmprAmmCommandBufferRemapIntoPrt): same AmmProtectCommand/AmmRemapCommand payloads, same opcodes (AmmModifyMtypeProtect=24, AmmRemapIntoPrt=27).

Adds TestKernelAmmRecorders() to GuestAmpr.cpp covering success + opcode/size, the reserved-field 1011 default and override, invalid-argument/null-buffer/ unbound-buffer rejection, and exact-size-then-busy.

See issue #4214.

What

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 in TechnicalDebt
  • 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:
  • AI-assisted:
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4824381031 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4258). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-12T02:56:41Z. --- … sceKernelWriteRemapIntoPrtCommand Resolves NIDs hrEIwWDKm-w and RPAHEYSOt1s, absent from libkernel and needed by Trails in the Sky 2nd Chapter (PPSA35169). Both mirror existing, tested libSceAmpr recorders (sceAmprAmmCommandBufferModifyMtypeProtect and sceAmprAmmCommandBufferRemapIntoPrt): same AmmProtectCommand/AmmRemapCommand payloads, same opcodes (AmmModifyMtypeProtect=24, AmmRemapIntoPrt=27). Adds TestKernelAmmRecorders() to GuestAmpr.cpp covering success + opcode/size, the reserved-field 1011 default and override, invalid-argument/null-buffer/ unbound-buffer rejection, and exact-size-then-busy. See issue #4214. ### What ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. --> ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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: <!-- #PR, or none --> - [x] AI-assisted: <!-- yes / no -->
github-actions[bot] commented 2026-10-12 02:59:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-12T02:59:05Z.


Progress report (34a16b7 → 1d63b11)

📈 System libraries: 86.51% (+0.01%, +2 implemented functions)

✅ 2 implemented
Library Name
libkernel sceKernelWriteModifyMtypeProtectCommand
libkernel sceKernelWriteRemapIntoPrtCommand
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6116945485 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4258#issuecomment-6116945485). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-12T02:59:05Z. --- <!-- progress-report --> ### Progress report (34a16b7 → 1d63b11) 📈 **System libraries**: 86.51% (+0.01%, +2 implemented functions) <details> <summary>✅ 2 implemented</summary> | Library | Name | | - | - | | libkernel | `sceKernelWriteModifyMtypeProtectCommand` | | libkernel | `sceKernelWriteRemapIntoPrtCommand` | </details>
github-actions[bot] commented 2026-10-12 03:04:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-12T03:04:18Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#4244 sceKernelWriteModifyMtypeProtectCommand
sceKernelWriteRemapIntoPrtCommand
core/libs/prx/libkernel/Apr/src/Recorder.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6116993061 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4258#issuecomment-6116993061). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-12T03:04:18Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #4244 | | `sceKernelWriteModifyMtypeProtectCommand`<br>`sceKernelWriteRemapIntoPrtCommand` | `core/libs/prx/libkernel/Apr/src/Recorder.cpp` |
Dextroid17 commented 2026-10-12 03:31:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-12T03:31:10Z.


Carried by #4244 — a cherry-pick of my commit 1d63b117, so the patch and the author are preserved and the commit object is new — and that one has the PR template filled in. Thanks to @MarcZX100 for the carry.

Verified before closing:

  • The two diffs are byte-identical — sha256 c3cebbada5cb586b1e6e559742802f12daa43d4539591ac4d6ee5b907480ff23 for each, and even the index blob SHAs match, so the resulting trees hash the same. Same three files, same line counts: core/libs/prx/libkernel/Apr/src/Recorder.cpp (+55/-0), core/libs/prx/libkernel/CMakeLists.txt (+1/-0), core/libs/tests/GuestAmpr.cpp (+52/-0).
  • #4244 is the healthier copy: conventions passes there and fails here (this PR left ### What, ### Tested, AI-assisted and Depends on unfilled), and it reports CLEAN against this one's UNSTABLE.
  • The overlap bot lists the two against each other as the same exports and the same new file, which is the expected outcome for two copies of one change.

So #4244 already carries everything this PR would contribute. Closing this one to leave a single copy for review — #4244 is the copy to keep, and the title measurement it unblocks (Trails in the Sky 2nd Chapter, PPSA35169) is recorded there.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6117233683 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4258#issuecomment-6117233683). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-12T03:31:10Z. --- Carried by #4244 — a cherry-pick of my commit `1d63b117`, so the patch and the author are preserved and the commit object is new — and that one has the PR template filled in. Thanks to @MarcZX100 for the carry. Verified before closing: - **The two diffs are byte-identical** — `sha256 c3cebbada5cb586b1e6e559742802f12daa43d4539591ac4d6ee5b907480ff23` for each, and even the `index` blob SHAs match, so the resulting trees hash the same. Same three files, same line counts: `core/libs/prx/libkernel/Apr/src/Recorder.cpp` (+55/-0), `core/libs/prx/libkernel/CMakeLists.txt` (+1/-0), `core/libs/tests/GuestAmpr.cpp` (+52/-0). - **#4244 is the healthier copy**: `conventions` passes there and fails here (this PR left `### What`, `### Tested`, `AI-assisted` and `Depends on` unfilled), and it reports `CLEAN` against this one's `UNSTABLE`. - The overlap bot lists the two against each other as the same exports and the same new file, which is the expected outcome for two copies of one change. So #4244 already carries everything this PR would contribute. Closing this one to leave a single copy for review — #4244 is the copy to keep, and the title measurement it unblocks (Trails in the Sky 2nd Chapter, PPSA35169) is recorded there. Written with AI assistance.
overlordxrz-source commented 2026-10-12 03:48:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by overlordxrz-source on 2026-10-12T03:48:23Z.


@Dextroid17 thanks for tracking these down. One thing about the signature, before this merges:

On the console these two are the libkernel writers that libSceAmpr's AMM command buffer calls. They don't record into a CommandBufferObject. drakmor/ampr_emu hooks them as:

int sceKernelWriteModifyMtypeProtectCommand(void* dst, uint64_t va, uint64_t size, uint64_t type, uint64_t prot, uint64_t protMask, uint64_t* outSize);
int sceKernelWriteRemapIntoPrtCommand(uint32_t* dst, uint64_t va, uint64_t remapVa, uint64_t size, int prot, int opcode, uint64_t* outSize);

Its AmmCommandBuffer (L327-L403) calls each writer twice. The first call passes dst = nullptr and reads the size from outSize. The second passes buffer + offset with outSize = nullptr, and then the caller advances its own offset. With the CommandBufferObject* form in this PR:

  • the size-only call returns EINVAL, because !buffer is true
  • the write call reads base/size/offset out of the title's raw buffer memory (offset 0x10 and so on), so it gets EPERM on a zeroed buffer, or writes to a pointer taken from command bytes

Two smaller points from the same source:

  • ampr_emu builds allocatePaForPrt from this same writer with protMask = 1019 (L376-L388). 1019 has bit 0x8 set, so ValidProtect rejects it. Writing an AmmAllocatePaForPrt record for that mask keeps the existing executor path.
  • The 1011 default is applied in libSceAmpr before the writer is called (L397), so the writer itself can record the opcode as given.

I have a version along these lines with a submission test: the records are written into a plain byte buffer, compared byte-for-byte with the libSceAmpr recorder output, then submitted with sceAmprAmmSubmitCommandBuffer3 and their effects checked. If it helps, I can push it as a branch for you to take into this PR. That seems better than a competing PR.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6117388901 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4258#issuecomment-6117388901). Originally posted by [overlordxrz-source](https://github.com/overlordxrz-source) on 2026-10-12T03:48:23Z. --- @Dextroid17 thanks for tracking these down. One thing about the signature, before this merges: On the console these two are the libkernel *writers* that libSceAmpr's AMM command buffer calls. They don't record into a `CommandBufferObject`. [drakmor/ampr_emu](https://github.com/drakmor/ampr_emu/blob/cfa85df379f6eeeb165d7badf9b648e266fe77b7/include/ampr_libkernel_hook_targets.h#L112-L114) hooks them as: ``` int sceKernelWriteModifyMtypeProtectCommand(void* dst, uint64_t va, uint64_t size, uint64_t type, uint64_t prot, uint64_t protMask, uint64_t* outSize); int sceKernelWriteRemapIntoPrtCommand(uint32_t* dst, uint64_t va, uint64_t remapVa, uint64_t size, int prot, int opcode, uint64_t* outSize); ``` Its `AmmCommandBuffer` ([L327-L403](https://github.com/drakmor/ampr_emu/blob/cfa85df379f6eeeb165d7badf9b648e266fe77b7/src/ampr_emu_command_buffer_amm.cpp#L327-L403)) calls each writer twice. The first call passes `dst = nullptr` and reads the size from `outSize`. The second passes `buffer + offset` with `outSize = nullptr`, and then the caller advances its own offset. With the `CommandBufferObject*` form in this PR: - the size-only call returns `EINVAL`, because `!buffer` is true - the write call reads `base`/`size`/`offset` out of the title's raw buffer memory (offset 0x10 and so on), so it gets `EPERM` on a zeroed buffer, or writes to a pointer taken from command bytes Two smaller points from the same source: - ampr_emu builds `allocatePaForPrt` from this same writer with `protMask = 1019` ([L376-L388](https://github.com/drakmor/ampr_emu/blob/cfa85df379f6eeeb165d7badf9b648e266fe77b7/src/ampr_emu_command_buffer_amm.cpp#L376-L388)). 1019 has bit 0x8 set, so `ValidProtect` rejects it. Writing an `AmmAllocatePaForPrt` record for that mask keeps the existing executor path. - The 1011 default is applied in libSceAmpr before the writer is called ([L397](https://github.com/drakmor/ampr_emu/blob/cfa85df379f6eeeb165d7badf9b648e266fe77b7/src/ampr_emu_command_buffer_amm.cpp#L397)), so the writer itself can record the opcode as given. I have a version along these lines with a submission test: the records are written into a plain byte buffer, compared byte-for-byte with the libSceAmpr recorder output, then submitted with `sceAmprAmmSubmitCommandBuffer3` and their effects checked. If it helps, I can push it as a branch for you to take into this PR. That seems better than a competing PR. AI-assisted: yes

Pull request closed

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!4258
No description provided.