fix(libkernel): record AmmModifyMtypeProtect and AmmRemapIntoPrt commands #4266

Merged
ducloc080202 merged 1 commit from upstream-pr/4266 into main 2026-10-12 07:26:27 +00:00
ducloc080202 commented 2026-10-12 03:34:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ducloc080202 on 2026-10-12T03:34:09Z.


What

Fixes #4214. Trails in the Sky 2nd Chapter (PPSA35169) stops at process start on two missing libkernel exports: sceKernelWriteModifyMtypeProtectCommand (hrEIwWDKm-w) and sceKernelWriteRemapIntoPrtCommand (RPAHEYSOt1s). Both are recorder-side functions that serialize an AmmModifyMtypeProtect / AmmRemapIntoPrt command into the caller's buffer; the execution side already handles both opcodes.

Signatures are taken from ampr_emu's libkernel hook targets, which forward to the real kernel (same source this project's AMM work already cites): the ModifyMtypeProtect writer takes (dst, va, size, type, prot, protMask, outSize), the RemapIntoPrt writer takes (dst, va, remapVa, size, prot, opcode, outSize), and a null dst only measures while a null outSize only records. An opcode of 0 is recorded as 1011, matching the existing sceAmprAmmCommandBufferRemapIntoPrt. Range, protection and mask checks mirror the libSceAmpr siblings, and the recorded bytes are identical to theirs so the submitted buffer executes on the existing paths. Uncertainty is documented in TechnicalDebt.

Tested

Windows 11, WinLibs MinGW-w64 GCC 15.2.0 r7, Release full build with BUILD_TESTING=ON. New TestKernelWriteCommands in GuestAmpr.cpp: measure mode, record mode (header + byte equality with the libSceAmpr sibling recorders), outSize-null recording, opcode 0/42 recording (1011/42), submit-and-execute round trip through sceAmprAmmSubmitCommandBuffer3 (protection change observed via sceKernelQueryMemoryProtection, remapped page readable in the PRT range), invalid arguments rejected with EINVAL leaving dst/outSize untouched, and null/null calls. Without the fix the test binary fails to link with undefined reference to both functions (the title's exact symptom); with the fix guest_ampr_markers passes. python tools/nid_names.py --nid hrEIwWDKm-w --nid RPAHEYSOt1s reports both NIDs as already-implemented. python tools/check_conventions.py --base origin/main and git diff --check pass.

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: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4824599759 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4266). Originally posted by [ducloc080202](https://github.com/ducloc080202) on 2026-10-12T03:34:09Z. --- ### What Fixes #4214. Trails in the Sky 2nd Chapter (PPSA35169) stops at process start on two missing `libkernel` exports: `sceKernelWriteModifyMtypeProtectCommand` (`hrEIwWDKm-w`) and `sceKernelWriteRemapIntoPrtCommand` (`RPAHEYSOt1s`). Both are recorder-side functions that serialize an `AmmModifyMtypeProtect` / `AmmRemapIntoPrt` command into the caller's buffer; the execution side already handles both opcodes. Signatures are taken from ampr_emu's libkernel hook targets, which forward to the real kernel (same source this project's AMM work already cites): the `ModifyMtypeProtect` writer takes `(dst, va, size, type, prot, protMask, outSize)`, the `RemapIntoPrt` writer takes `(dst, va, remapVa, size, prot, opcode, outSize)`, and a null `dst` only measures while a null `outSize` only records. An `opcode` of 0 is recorded as 1011, matching the existing `sceAmprAmmCommandBufferRemapIntoPrt`. Range, protection and mask checks mirror the `libSceAmpr` siblings, and the recorded bytes are identical to theirs so the submitted buffer executes on the existing paths. Uncertainty is documented in TechnicalDebt. ### Tested Windows 11, WinLibs MinGW-w64 GCC 15.2.0 r7, Release full build with BUILD_TESTING=ON. New `TestKernelWriteCommands` in `GuestAmpr.cpp`: measure mode, record mode (header + byte equality with the `libSceAmpr` sibling recorders), `outSize`-null recording, opcode 0/42 recording (1011/42), submit-and-execute round trip through `sceAmprAmmSubmitCommandBuffer3` (protection change observed via `sceKernelQueryMemoryProtection`, remapped page readable in the PRT range), invalid arguments rejected with `EINVAL` leaving `dst`/`outSize` untouched, and null/null calls. Without the fix the test binary fails to link with `undefined reference` to both functions (the title's exact symptom); with the fix `guest_ampr_markers` passes. `python tools/nid_names.py --nid hrEIwWDKm-w --nid RPAHEYSOt1s` reports both NIDs as already-implemented. `python tools/check_conventions.py --base origin/main` and `git diff --check` pass. ### 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 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: none - [x] AI-assisted: yes
MarcZX10 commented 2026-10-12 03:38:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-12T03:38:58Z.


Potential ABI conflict with the open #4244: both define sceKernelWriteModifyMtypeProtectCommand and sceKernelWriteRemapIntoPrtCommand, but #4244 takes an Apr::CommandBufferObject* and appends to its bound buffer, while this PR takes a raw dst plus outSize and supports a measure call. Those prototypes cannot both represent the same NIDs safely. #4258 was confirmed byte-identical to #4244 and closed as its duplicate, leaving #4244 as the competing implementation. The signature evidence here (ampr_emu hook targets) differs from #4244’s sibling-recorder inference; #4214 establishes the imports but not the call ABI. This needs reconciliation against a title call/register capture or another trusted source before both copies proceed.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6117304778 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4266#issuecomment-6117304778). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-12T03:38:58Z. --- Potential ABI conflict with the open #4244: both define `sceKernelWriteModifyMtypeProtectCommand` and `sceKernelWriteRemapIntoPrtCommand`, but #4244 takes an `Apr::CommandBufferObject*` and appends to its bound buffer, while this PR takes a raw `dst` plus `outSize` and supports a measure call. Those prototypes cannot both represent the same NIDs safely. #4258 was confirmed byte-identical to #4244 and closed as its duplicate, leaving #4244 as the competing implementation. The signature evidence here (ampr_emu hook targets) differs from #4244’s sibling-recorder inference; #4214 establishes the imports but not the call ABI. This needs reconciliation against a title call/register capture or another trusted source before both copies proceed.
MarcZX10 commented 2026-10-12 03:43:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-12T03:43:35Z.

Review: COMMENTED


Following up on the ABI mismatch noted earlier: has either implementation been checked against a captured guest call site or a trusted SDK header? The recorder-buffer prototype and the raw output-buffer prototype cannot both bind the same NIDs safely. A register/call-site trace or source signature would resolve which contract the exports need.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5486346238 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4266#pullrequestreview-5486346238). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-12T03:43:35Z. **Review:** COMMENTED --- Following up on the ABI mismatch noted earlier: has either implementation been checked against a captured guest call site or a trusted SDK header? The recorder-buffer prototype and the raw output-buffer prototype cannot both bind the same NIDs safely. A register/call-site trace or source signature would resolve which contract the exports need.
ducloc080202 commented 2026-10-12 03:43:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ducloc080202 on 2026-10-12T03:43:58Z.


Thanks for catching the conflict — you're right that the two PRs can't both represent these NIDs. My duplicate check (gh search prs for both names) came back empty before #4244 was up, so I missed it; sorry for the extra reconciliation work.

On the ABI question, the two proposals agree on the recorded bytes (same structs, same opcodes, same 0-to-1011 default — my test even byte-compares the Write* output against the libSceAmpr sibling recorders), so execution behavior is identical either way. The dispute is only the call ABI: raw dst + outSize with a measure mode (this PR) vs Apr::CommandBufferObject* append ( #4244).

The evidence for raw dst + outSize:

  1. ampr_emu hooks the real libkernel on real hardware with exactly these 7-arg signatures (include/ampr_libkernel_hook_targets.h: sceKernelWriteModifyMtypeProtectCommand(void*, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t*) and sceKernelWriteRemapIntoPrtCommand(uint32_t*, uint64_t, uint64_t, uint64_t, int, int, uint64_t*), both forwarded to the real kernel).
  2. Decisively, ampr_emu's own AmmCommandBuffer calls the real kernel writer with dst == nullptr to measure (src/ampr_emu_command_buffer_amm.cpp, e.g. Emu::ammWriteModifyMtypeProtectCommand(nullptr, va, size, ..., out) inside cb_append_amm_kernel_record), and depends on getting the size back. Under a CommandBufferObject* ABI, a null buffer returns EINVAL — which is exactly what #4244's own new test asserts (sceKernelWriteModifyMtypeProtectCommand(nullptr, ...) == invalidArgument). So if #4244's ABI were the real one, ampr_emu's measure path would fail on every append on console, but ampr_emu works there.
  3. Structurally, no Measure siblings for these two exist anywhere, while every libSceAmpr CommandBuffer* recorder has a Measure* partner. The Write*Command name plus the built-in null-dst measure explains that absence; a CommandBuffer* theory leaves it unexplained.

So I believe the CommandBufferObject* form is a sibling-recorder inference that happens to produce the right bytes but the wrong call ABI, and a real title calling these NIDs would pass a raw cursor + outSize, which #4244's form would misread (its buffer param would receive the title's dst pointer, and there is no outSize at all).

Happy to carry over anything useful from #4244 if maintainers agree this ABI is the right one — e.g. its separate Recorder.cpp file layout vs my in-Apr.cpp placement — or to yield if someone has a title register capture showing otherwise.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6117349462 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4266#issuecomment-6117349462). Originally posted by [ducloc080202](https://github.com/ducloc080202) on 2026-10-12T03:43:58Z. --- Thanks for catching the conflict — you're right that the two PRs can't both represent these NIDs. My duplicate check (`gh search prs` for both names) came back empty before #4244 was up, so I missed it; sorry for the extra reconciliation work. On the ABI question, the two proposals agree on the recorded bytes (same structs, same opcodes, same 0-to-1011 default — my test even byte-compares the `Write*` output against the `libSceAmpr` sibling recorders), so execution behavior is identical either way. The dispute is only the call ABI: raw `dst` + `outSize` with a measure mode (this PR) vs `Apr::CommandBufferObject*` append ( #4244). The evidence for raw `dst` + `outSize`: 1. ampr_emu hooks the real `libkernel` on real hardware with exactly these 7-arg signatures (`include/ampr_libkernel_hook_targets.h`: `sceKernelWriteModifyMtypeProtectCommand(void*, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t*)` and `sceKernelWriteRemapIntoPrtCommand(uint32_t*, uint64_t, uint64_t, uint64_t, int, int, uint64_t*)`, both forwarded to the real kernel). 2. Decisively, ampr_emu's own `AmmCommandBuffer` calls the *real* kernel writer with `dst == nullptr` to measure (`src/ampr_emu_command_buffer_amm.cpp`, e.g. `Emu::ammWriteModifyMtypeProtectCommand(nullptr, va, size, ..., out)` inside `cb_append_amm_kernel_record`), and depends on getting the size back. Under a `CommandBufferObject*` ABI, a null buffer returns `EINVAL` — which is exactly what #4244's own new test asserts (`sceKernelWriteModifyMtypeProtectCommand(nullptr, ...) == invalidArgument`). So if #4244's ABI were the real one, ampr_emu's measure path would fail on every append on console, but ampr_emu works there. 3. Structurally, no `Measure` siblings for these two exist anywhere, while every `libSceAmpr` `CommandBuffer*` recorder has a `Measure*` partner. The `Write*Command` name plus the built-in null-`dst` measure explains that absence; a `CommandBuffer*` theory leaves it unexplained. So I believe the `CommandBufferObject*` form is a sibling-recorder inference that happens to produce the right bytes but the wrong call ABI, and a real title calling these NIDs would pass a raw cursor + `outSize`, which #4244's form would misread (its `buffer` param would receive the title's `dst` pointer, and there is no `outSize` at all). Happy to carry over anything useful from #4244 if maintainers agree this ABI is the right one — e.g. its separate `Recorder.cpp` file layout vs my in-`Apr.cpp` placement — or to yield if someone has a title register capture showing otherwise.
github-actions[bot] commented 2026-10-12 03:44:47 +00:00 (Migrated from github.com)

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


Progress report (d146ec1 → 2cf929d)

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

✅ 2 implemented
Library Name
libkernel sceKernelWriteModifyMtypeProtectCommand
libkernel sceKernelWriteRemapIntoPrtCommand
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6117356666 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4266#issuecomment-6117356666). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-12T03:44:47Z. --- <!-- progress-report --> ### Progress report (d146ec1 → 2cf929d) 📈 **System libraries**: 86.55% (+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:52:12 +00:00 (Migrated from github.com)

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


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
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6117422974 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4266#issuecomment-6117422974). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-12T03:52:12Z. --- <!-- 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` | |
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!4266
No description provided.