fix(kernel,agc): honor GPU-only memory protection bits #23770

Merged
birdmarty merged 2 commits from upstream-pr/23770 into main 2026-11-20 04:57:46 +00:00
birdmarty commented 2026-11-19 16:11:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by birdmarty on 2026-11-19T16:11:12Z.


What

Far Cry 6 (PPSA01870 01.000.013) draws no text on its splash screen and main menu: the buttons are there, empty. The title maps the memory its UI text is drawn into (a 16 MiB block at 0x23f300000) with protection 0x30, GPU read and write without CPU read or write, and fills it from a compute shader through storage images.

LinuxProtFromSce already commits such memory read/write on the host (0x10 gives read, 0x20 read and write). GuestAllocations registered it with readable = false and writable = false, though, and the AGC driver only takes readable ranges into its leases. So GPU-only memory was missing from the leases, the BDA table, host imports and RegisteredReadableCovers, and storage-image results stored there were dropped as "memory no longer registered".

A registered range now keeps the GPU write bit (gpuWritable), and the driver asks two questions instead of the CPU bits:

  • HostReadable() = readable || gpu
  • HostWritable() = writable || gpuWritable

These are exactly the host protections LinuxProtFromSce gives the pages, so the driver reads and writes only pages the host lets it.

What stays the same:

  • CPU-side checks keep the CPU bits. sceKernelSyncOnAddressWait32 on GPU-only memory still returns EFAULT (new case), and sceKernelVirtualQuery reports the same protection as before.
  • The existing 5-argument Add and Protect set gpuWritable = gpu && writable, so HostWritable() equals writable for every caller that does not pass the new argument. HostReadable() differs from readable only for ranges with GPU access and no CPU read, which only libkernel's mappings and sceKernelMprotect create; they now pass the GPU write bit.
  • Adjacent ranges still join into one run (host import runs, GuestAllocationsAcquireMapped) when their CPU bits, GPU bit and host writability match, so every run that joins on main still joins. They split only where host writability differs, for example 0x11 next to 0x31.
  • A protection change gives the range a new backing serial, as before, so cached views and dispatches see a change of the GPU write bit.
  • Frame capture still skips ranges the CPU cannot read, as on main; the debt fragment records it.

Changes

  • libc/include/GuestAllocations.hpp, libc/src/GuestAllocations.cpp:
    • adds Range::gpuWritable, HostReadable() and HostWritable();
    • adds GuestAllocationsAddGpu_nid_no_patch and GuestAllocationsProtectGpu_nid_no_patch behind 6-argument Mutation::Add and Mutation::Protect. The 5-argument forms forward to them;
    • gpuWritable requires gpu, and survives Find, splits and unmaps;
    • GuestAllocationsEnd's GPU mapping notice and GuestAllocationsAcquire, AcquireMapped and AcquireRegistered take HostReadable() ranges.
  • libkernel/DirectMemory/DirectMemory.cpp: sceKernelMapDirectMemory, flexible mappings, fixed remaps and sceKernelMprotect pass prot & 0x20 as the GPU write bit.
  • libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp, Execution/src/ShaderMemory.cpp: the lease, import, mirror, BDA and registration checks use HostReadable() and HostWritable().
  • Tests:
    • Three existing cases registered ranges with GPU access to stand for unreadable memory: registeredReadableCoverTests, leaseSnapshotTests and CheckMappedLeasesPreserveReadableRuns. Those inputs now drop the GPU bit, and each test gained the GPU-only variant.
    • 0x30 and 0x10 through sceKernelMapDirectMemory, sceKernelMprotect and sceKernelMapNamedFlexibleMemory.
    • EFAULT from sceKernelSyncOnAddressWait32 on GPU-only memory.
    • BDA table entries and write permission for GPU-only ranges, copied and imported.
    • Run joining by host writability.
    • agc_storage_gpu_only_writeback: a storage image in 0x30 memory is cleared on the GPU and written back, with and without a host import.
  • docs/dev/debt/changes/fix-kernel-agc-gpu-only-protection.md: frame capture does not record GPU-only memory.

Tested

Windows 11 Pro (build 26300), MinGW-w64 GCC 15.3.0, Release build with tests, AMD Radeon RX 6800.

Tests ran on main 77d2c4ae60 and on this change, same machine and GPU. The set is 49 test programs: the four that hold the changed tests (guest_memory_tests, guest_sync_on_address_tests, agc_driver_graphics_tests, agc_driver_recorder_tests) and every other guest memory, host import, mirror, storage image, write tracking and BDA test program.

  • main: 368 tests, 364 passed, 4 skipped.

  • This change: 369 tests (the new agc_storage_gpu_only_writeback included), 365 passed, 4 skipped. The same four skip on both sides:

    • agc_host_import_sparse;
    • agc_driver_copied_writer_label_order;
    • agc_driver_copied_writer_label_order_drain;
    • agc_driver_partially_committed_volume_page_samples.
  • The same set again with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1, VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation (Vulkan SDK 1.4.363.0), VK_LOADER_DEBUG=layer and ctest -V: the same results on both sides.

    • Both logs show Insert instance layer "VK_LAYER_KHRONOS_validation": 232 times on main, 233 with this change (one more test).
    • Neither shows was not found but was requested by env var VK_INSTANCE_LAYERS.
    • Neither has a VUID- line.
  • With this change's HostReadable() and HostWritable() reduced to the CPU bits, which is what main checks:

    • agc_storage_gpu_only_writeback fails the way the title does:
      [gpu] storage image 0x200000000+0x40000: memory no longer registered; 1 pending ranges dropped
      AGC graphics: gpu-only storage write-back dropped or corrupted texels at +0
      
    • guest_memory and agc_driver_graphics fail on the new GPU-only cases.
    • guest_sync_on_address passes either way, since the CPU result does not change.
  • The run-joining case fails if adjacent ranges must match in the GPU write bit itself rather than in host writability.

  • public_headers (65 tests, GuestAllocations.hpp changed): passed.

  • agc_shader_memory_tests does not link with this toolchain, on main or with this change (ld: .rsrc merge failure: multiple non-default manifests), so its tests are not in the counts.

  • tools/check_conventions.py --base origin/main and tools/debt_fold.py --check: passed.

  • tools/pr_overlap.py: two open pull requests change the same lines. Neither needs this change, nor this change them.

    • #15297 rewrites joinedHeapRange. The only shared line is the joined Range, where this change passes first.readable and first.gpuWritable; whichever lands second keeps both.
    • #23613 adds lines next to the mutation.Protect call in RemapFixedIntoRegistered.

    76 pull requests that conflict with main share files and could not be compared.

Far Cry 6 (PPSA01870 01.000.013): with this change the splash screen and the main menu draw their text. Taken on an earlier main (d31916ed37) with #21241; the after build also had #16053 reverted, which #20827 has since replaced on main.

Far Cry 6 main menu before and after this change

Far Cry 6 options menu after this change

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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: PPSA01870 #21239
  • GPUs tested: AMD Radeon RX 6800 (vendor=0x1002, device=0x73bf), AMD proprietary driver 26.10.1 (Windows driver 32.0.21045.16010, Vulkan 1.4.315), Windows 11 Pro (build 26300); Khronos validation layer on main and this change, no new VUIDs
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5398541155 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23770). Originally posted by [birdmarty](https://github.com/birdmarty) on 2026-11-19T16:11:12Z. --- ### What Far Cry 6 (PPSA01870 01.000.013) draws no text on its splash screen and main menu: the buttons are there, empty. The title maps the memory its UI text is drawn into (a 16 MiB block at `0x23f300000`) with protection `0x30`, GPU read and write without CPU read or write, and fills it from a compute shader through storage images. `LinuxProtFromSce` already commits such memory read/write on the host (`0x10` gives read, `0x20` read and write). `GuestAllocations` registered it with `readable = false` and `writable = false`, though, and the AGC driver only takes `readable` ranges into its leases. So GPU-only memory was missing from the leases, the BDA table, host imports and `RegisteredReadableCovers`, and storage-image results stored there were dropped as "memory no longer registered". A registered range now keeps the GPU write bit (`gpuWritable`), and the driver asks two questions instead of the CPU bits: - `HostReadable()` = `readable || gpu` - `HostWritable()` = `writable || gpuWritable` These are exactly the host protections `LinuxProtFromSce` gives the pages, so the driver reads and writes only pages the host lets it. What stays the same: - CPU-side checks keep the CPU bits. `sceKernelSyncOnAddressWait32` on GPU-only memory still returns `EFAULT` (new case), and `sceKernelVirtualQuery` reports the same protection as before. - The existing 5-argument `Add` and `Protect` set `gpuWritable = gpu && writable`, so `HostWritable()` equals `writable` for every caller that does not pass the new argument. `HostReadable()` differs from `readable` only for ranges with GPU access and no CPU read, which only `libkernel`'s mappings and `sceKernelMprotect` create; they now pass the GPU write bit. - Adjacent ranges still join into one run (host import runs, `GuestAllocationsAcquireMapped`) when their CPU bits, GPU bit and host writability match, so every run that joins on `main` still joins. They split only where host writability differs, for example `0x11` next to `0x31`. - A protection change gives the range a new backing serial, as before, so cached views and dispatches see a change of the GPU write bit. - Frame capture still skips ranges the CPU cannot read, as on `main`; the debt fragment records it. #### Changes - `libc/include/GuestAllocations.hpp`, `libc/src/GuestAllocations.cpp`: - adds `Range::gpuWritable`, `HostReadable()` and `HostWritable()`; - adds `GuestAllocationsAddGpu_nid_no_patch` and `GuestAllocationsProtectGpu_nid_no_patch` behind 6-argument `Mutation::Add` and `Mutation::Protect`. The 5-argument forms forward to them; - `gpuWritable` requires `gpu`, and survives `Find`, splits and unmaps; - `GuestAllocationsEnd`'s GPU mapping notice and `GuestAllocationsAcquire`, `AcquireMapped` and `AcquireRegistered` take `HostReadable()` ranges. - `libkernel/DirectMemory/DirectMemory.cpp`: `sceKernelMapDirectMemory`, flexible mappings, fixed remaps and `sceKernelMprotect` pass `prot & 0x20` as the GPU write bit. - `libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp`, `Execution/src/ShaderMemory.cpp`: the lease, import, mirror, BDA and registration checks use `HostReadable()` and `HostWritable()`. - Tests: - Three existing cases registered ranges with GPU access to stand for unreadable memory: `registeredReadableCoverTests`, `leaseSnapshotTests` and `CheckMappedLeasesPreserveReadableRuns`. Those inputs now drop the GPU bit, and each test gained the GPU-only variant. - `0x30` and `0x10` through `sceKernelMapDirectMemory`, `sceKernelMprotect` and `sceKernelMapNamedFlexibleMemory`. - `EFAULT` from `sceKernelSyncOnAddressWait32` on GPU-only memory. - BDA table entries and write permission for GPU-only ranges, copied and imported. - Run joining by host writability. - `agc_storage_gpu_only_writeback`: a storage image in `0x30` memory is cleared on the GPU and written back, with and without a host import. - `docs/dev/debt/changes/fix-kernel-agc-gpu-only-protection.md`: frame capture does not record GPU-only memory. ### Tested Windows 11 Pro (build 26300), MinGW-w64 GCC 15.3.0, Release build with tests, AMD Radeon RX 6800. Tests ran on `main` `77d2c4ae60` and on this change, same machine and GPU. The set is 49 test programs: the four that hold the changed tests (`guest_memory_tests`, `guest_sync_on_address_tests`, `agc_driver_graphics_tests`, `agc_driver_recorder_tests`) and every other guest memory, host import, mirror, storage image, write tracking and BDA test program. - `main`: 368 tests, 364 passed, 4 skipped. - This change: 369 tests (the new `agc_storage_gpu_only_writeback` included), 365 passed, 4 skipped. The same four skip on both sides: - `agc_host_import_sparse`; - `agc_driver_copied_writer_label_order`; - `agc_driver_copied_writer_label_order_drain`; - `agc_driver_partially_committed_volume_page_samples`. - The same set again with `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1`, `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation` (Vulkan SDK 1.4.363.0), `VK_LOADER_DEBUG=layer` and `ctest -V`: the same results on both sides. - Both logs show `Insert instance layer "VK_LAYER_KHRONOS_validation"`: 232 times on `main`, 233 with this change (one more test). - Neither shows `was not found but was requested by env var VK_INSTANCE_LAYERS`. - Neither has a `VUID-` line. - With this change's `HostReadable()` and `HostWritable()` reduced to the CPU bits, which is what `main` checks: - `agc_storage_gpu_only_writeback` fails the way the title does: ``` [gpu] storage image 0x200000000+0x40000: memory no longer registered; 1 pending ranges dropped AGC graphics: gpu-only storage write-back dropped or corrupted texels at +0 ``` - `guest_memory` and `agc_driver_graphics` fail on the new GPU-only cases. - `guest_sync_on_address` passes either way, since the CPU result does not change. - The run-joining case fails if adjacent ranges must match in the GPU write bit itself rather than in host writability. - `public_headers` (65 tests, `GuestAllocations.hpp` changed): passed. - `agc_shader_memory_tests` does not link with this toolchain, on `main` or with this change (`ld: .rsrc merge failure: multiple non-default manifests`), so its tests are not in the counts. - `tools/check_conventions.py --base origin/main` and `tools/debt_fold.py --check`: passed. - `tools/pr_overlap.py`: two open pull requests change the same lines. Neither needs this change, nor this change them. - #15297 rewrites `joinedHeapRange`. The only shared line is the joined `Range`, where this change passes `first.readable` and `first.gpuWritable`; whichever lands second keeps both. - #23613 adds lines next to the `mutation.Protect` call in `RemapFixedIntoRegistered`. 76 pull requests that conflict with `main` share files and could not be compared. Far Cry 6 (PPSA01870 01.000.013): with this change the splash screen and the main menu draw their text. Taken on an earlier `main` (`d31916ed37`) with #21241; the after build also had #16053 reverted, which #20827 has since replaced on `main`. ![Far Cry 6 main menu before and after this change](https://raw.githubusercontent.com/birdmarty/AnyPS5/pr-assets/pr-23770/1-fc6-main-menu-before-after.png) ![Far Cry 6 options menu after this change](https://raw.githubusercontent.com/birdmarty/AnyPS5/pr-assets/pr-23770/2-fc6-options-after.png) ### 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) and the licence notice of code adapted from upstream (see AGENTS.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: PPSA01870 #21239 - [x] GPUs tested: AMD Radeon RX 6800 (vendor=0x1002, device=0x73bf), AMD proprietary driver 26.10.1 (Windows driver 32.0.21045.16010, Vulkan 1.4.315), Windows 11 Pro (build 26300); Khronos validation layer on `main` and this change, no new VUIDs - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-11-19 16:21:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-19T16:21:02Z.

Review: COMMENTED


Reviewed — no blocking findings

The inspected changes consistently track GPU write access separately from CPU permissions, and I found no concrete defect in the reviewed paths.

What I ran

Inspected the prepared diff for head 69dc84cb083fb980ad295d38a6b9f9af28e20620 and targeted source context through repo_read.py. No commands or tests were run.

Results

Check Result Note
Diff and targeted source review passed No concrete defect found in inspected paths.
Tests and build not run No execution environment was available.

Findings

Blocking

None.

Non-blocking

None.

Not verified

Tests and builds were not run; author-reported results in the PR description were not independently confirmed. Full file text was not inspected for the oversized changed files core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp, core/libs/prx/libSceAgcDriver/tests/Graphics.cpp, and core/libs/prx/libSceAgcDriver/tests/Recorder.cpp; their changed regions were inspected. The remaining unchanged portions of other changed files were not reviewed.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5978953375 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23770#pullrequestreview-5978953375). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-19T16:21:02Z. **Review:** COMMENTED --- # Reviewed — no blocking findings The inspected changes consistently track GPU write access separately from CPU permissions, and I found no concrete defect in the reviewed paths. ## What I ran Inspected the prepared diff for head `69dc84cb083fb980ad295d38a6b9f9af28e20620` and targeted source context through `repo_read.py`. No commands or tests were run. ## Results | Check | Result | Note | |---|---|---| | Diff and targeted source review | passed | No concrete defect found in inspected paths. | | Tests and build | not run | No execution environment was available. | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified Tests and builds were not run; author-reported results in the PR description were not independently confirmed. Full file text was not inspected for the oversized changed files `core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp`, `core/libs/prx/libSceAgcDriver/tests/Graphics.cpp`, and `core/libs/prx/libSceAgcDriver/tests/Recorder.cpp`; their changed regions were inspected. The remaining unchanged portions of other changed files were not reviewed. ## To get this to a good state Nothing is required.
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!23770
No description provided.