fix(libkernel): map unbacked AMM PRT pages to one read-only zero page on Windows #10025

Merged
mehmetbayoglu merged 4 commits from upstream-pr/10025 into main 2026-11-03 08:09:49 +00:00
mehmetbayoglu commented 2026-10-23 04:56:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mehmetbayoglu on 2026-10-23T04:56:36Z.


What

I got the idea from BiladeiroSupremo's AnyPS5Wolverine fork (856adabc59, for Marvel's Wolverine) and thought I would port it as a PR, implemented more optimally against main.

On Windows the unbacked pages of an AMM PRT range (MapAsPrt, UnmapToPrt) were private read-only commits. Windows has no shared zero page for private memory, so every page that was read, by the guest or by the GPU path copying it, took its own host page; a large PRT range grows the process by its whole size. They are now read-only views of one 16 KiB pagefile-backed section, so a PRT range costs one host page of memory however large it is. A view cannot be longer than its section, so the range still takes one placeholder split and one view per 16 KiB page, as main's per-page commit did; the timings are under Tested and the cost is in the debt entry. Linux and macOS are unchanged: their private read-only pages already share the kernel zero page.

  • WindowsMappings::MapZero / GuestArenaMapZero_nid_postfix replace the placeholders of a range with views of the zero section.
  • DoMapZero in DirectMemory takes the same path as DoMapAnon (the shared part is now MapAnonymous) and maps the zero views instead of committing; it only accepts a protection that is read-only on the host. _zeroPrt calls it.
  • The host import treats a mapped view whose allocation protection is PAGE_READONLY like read-only private memory and fails the import as before, instead of asking for a read-write alias of a section that cannot have one.

What differs from his version:

  • The zero section is created PAGE_READONLY, not PAGE_READWRITE. No view of it can ever be writable: VirtualProtect to read-write fails, sceKernelMprotect hits the existing check against the maximum protection of a mapped view and throws, and a read-write MapViewOfFile3 alias is refused. In his version the section is writable, so anything that later raised the protection without remapping, or aliased it for a GPU import, would write the one page every zero page reads.
  • No duplicated DoMapZero body: DoMapAnon and DoMapZero share MapAnonymous, so placement, overlap and bookkeeping stay in one place.
  • An all-zero range given to a host import fails the import the way read-only private memory did on main, instead of reaching GuestArenaMapAlias_nid_postfix, which throws for views it does not track.

What the console does when sceKernelMprotect makes an unbacked PRT page writable is not known; it throws and is recorded in docs/dev/debt/libkernel.md.

Tested

  • Windows 11 Pro, MinGW GCC 15.2, full build with -DBUILD_TESTING=ON.

  • guest_ampr_markers (GuestAmpr.cpp, TestAmmPrt) now checks after MapAsPrt and after UnmapToPrt that every unbacked page is a PAGE_READONLY mapped view, reads zero, and refuses VirtualProtect to read-write. It does not check sceKernelMprotect on these pages, since the console behaviour there is unmeasured. It fails on main and passes with this change.

  • 84 of the 85 AMM, memory, mapping, write-watch, host-import and kernel tests pass (ctest -R "amm|ampr|memory|arena|mapping|host_import|write_watch|mmap|mprotect|guest_alloc|bda|kernel"); agc_host_import_sparse is skipped on Windows.

  • ctest -R "agc|guest_buffer": 901 of 907 pass, 3 skip (sparse dma-buf and stencil export). agc_driver_sample_order, agc_driver_sample_order_grid_reference and agc_driver_sample_order_recipe_depth_locations fail on this machine with and without this change.

  • Validation, on main (9d6adeb550) and on this change, same machine and GPU: VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ANYPS5_NO_SHADER_CACHE=1 ANYPS5_REQUIRE_VULKAN=1 ctest -V -R "host_import|guest_buffer|ampr" (Vulkan SDK 1.4.363.0). Both run 17 tests with 0 failures (agc_host_import_sparse skipped), no VUID- errors, and neither log reports the layer as not found.

  • Rebased on main 7feb37ee3e (head c73b65f4b9). ctest of the AMM, AMPR, memory, mmap, mprotect, flexible memory, named shared memory, write-watch and windows_mappings_release tests: 29 of 29 pass. Conventions pass.

  • MapAsPrt / UnmapToPrt wall time, i7-13700K, Windows 11 Pro, WinLibs GCC 15.2.0 r7, Release, from a temporary harness (not committed) that submits one AMM command and times submit plus wait. main 7feb37ee3e and this change, same machine; medians of 5 runs (4 GiB, two passes on main and three on this change) and of 9 runs (256 MiB, three passes each):

    Operation 256 MiB main 256 MiB this PR 4 GiB main 4 GiB this PR
    MapAsPrt over a reserved range 33.5 to 33.9 ms 41.0 to 42.2 ms 853 to 856 ms 1054 to 1100 ms
    UnmapToPrt on an unbacked PRT range 83.6 to 85.4 ms 88.6 to 91.6 ms 3235 to 3398 ms 3199 to 3238 ms
    MapAsPrt over a PRT range 82.7 to 87.0 ms 87.9 to 89.9 ms 3275 to 3390 ms 3172 to 3267 ms
    UnmapToPrt on a backed range 37.6 to 38.5 ms 44.7 to 46.2 ms not run not run
    Commit charge added by MapAsPrt 258 MiB 1.2 MiB 4.04 GiB 21 MiB

    main commits these pages with a 16 KiB granule, so it also splits a placeholder and commits once per page; the change costs about 23% more on a fresh map and the same on a toggle. The first run of each 4 GiB series is about 40% faster than the rest on both. One 4 GiB pass of each side ran on an idle machine; the other passes ran while a game bench was running and agree with it. The 4 GiB backed case was not run, since it needs 4 GiB of pool memory.

  • Not tested in a title.

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: NVIDIA GeForce RTX 4070 Ti, driver 617.42, Windows 11 Pro
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4978025846 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10025). Originally posted by [mehmetbayoglu](https://github.com/mehmetbayoglu) on 2026-10-23T04:56:36Z. --- ### What I got the idea from BiladeiroSupremo's AnyPS5Wolverine fork (856adabc59, for Marvel's Wolverine) and thought I would port it as a PR, implemented more optimally against main. On Windows the unbacked pages of an AMM PRT range (`MapAsPrt`, `UnmapToPrt`) were private read-only commits. Windows has no shared zero page for private memory, so every page that was read, by the guest or by the GPU path copying it, took its own host page; a large PRT range grows the process by its whole size. They are now read-only views of one 16 KiB pagefile-backed section, so a PRT range costs one host page of memory however large it is. A view cannot be longer than its section, so the range still takes one placeholder split and one view per 16 KiB page, as main's per-page commit did; the timings are under Tested and the cost is in the debt entry. Linux and macOS are unchanged: their private read-only pages already share the kernel zero page. - `WindowsMappings::MapZero` / `GuestArenaMapZero_nid_postfix` replace the placeholders of a range with views of the zero section. - `DoMapZero` in DirectMemory takes the same path as `DoMapAnon` (the shared part is now `MapAnonymous`) and maps the zero views instead of committing; it only accepts a protection that is read-only on the host. `_zeroPrt` calls it. - The host import treats a mapped view whose allocation protection is `PAGE_READONLY` like read-only private memory and fails the import as before, instead of asking for a read-write alias of a section that cannot have one. What differs from his version: - The zero section is created `PAGE_READONLY`, not `PAGE_READWRITE`. No view of it can ever be writable: `VirtualProtect` to read-write fails, `sceKernelMprotect` hits the existing check against the maximum protection of a mapped view and throws, and a read-write `MapViewOfFile3` alias is refused. In his version the section is writable, so anything that later raised the protection without remapping, or aliased it for a GPU import, would write the one page every zero page reads. - No duplicated `DoMapZero` body: `DoMapAnon` and `DoMapZero` share `MapAnonymous`, so placement, overlap and bookkeeping stay in one place. - An all-zero range given to a host import fails the import the way read-only private memory did on main, instead of reaching `GuestArenaMapAlias_nid_postfix`, which throws for views it does not track. What the console does when `sceKernelMprotect` makes an unbacked PRT page writable is not known; it throws and is recorded in `docs/dev/debt/libkernel.md`. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> - Windows 11 Pro, MinGW GCC 15.2, full build with `-DBUILD_TESTING=ON`. - `guest_ampr_markers` (`GuestAmpr.cpp`, `TestAmmPrt`) now checks after `MapAsPrt` and after `UnmapToPrt` that every unbacked page is a `PAGE_READONLY` mapped view, reads zero, and refuses `VirtualProtect` to read-write. It does not check `sceKernelMprotect` on these pages, since the console behaviour there is unmeasured. It fails on `main` and passes with this change. - 84 of the 85 AMM, memory, mapping, write-watch, host-import and kernel tests pass (`ctest -R "amm|ampr|memory|arena|mapping|host_import|write_watch|mmap|mprotect|guest_alloc|bda|kernel"`); `agc_host_import_sparse` is skipped on Windows. - `ctest -R "agc|guest_buffer"`: 901 of 907 pass, 3 skip (sparse dma-buf and stencil export). `agc_driver_sample_order`, `agc_driver_sample_order_grid_reference` and `agc_driver_sample_order_recipe_depth_locations` fail on this machine with and without this change. - Validation, on `main` (9d6adeb550) and on this change, same machine and GPU: `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ANYPS5_NO_SHADER_CACHE=1 ANYPS5_REQUIRE_VULKAN=1 ctest -V -R "host_import|guest_buffer|ampr"` (Vulkan SDK 1.4.363.0). Both run 17 tests with 0 failures (`agc_host_import_sparse` skipped), no `VUID-` errors, and neither log reports the layer as not found. - Rebased on main 7feb37ee3e (head c73b65f4b9). `ctest` of the AMM, AMPR, memory, mmap, mprotect, flexible memory, named shared memory, write-watch and `windows_mappings_release` tests: 29 of 29 pass. Conventions pass. - `MapAsPrt` / `UnmapToPrt` wall time, i7-13700K, Windows 11 Pro, WinLibs GCC 15.2.0 r7, Release, from a temporary harness (not committed) that submits one AMM command and times submit plus wait. main 7feb37ee3e and this change, same machine; medians of 5 runs (4 GiB, two passes on main and three on this change) and of 9 runs (256 MiB, three passes each): | Operation | 256 MiB main | 256 MiB this PR | 4 GiB main | 4 GiB this PR | |---|---|---|---|---| | `MapAsPrt` over a reserved range | 33.5 to 33.9 ms | 41.0 to 42.2 ms | 853 to 856 ms | 1054 to 1100 ms | | `UnmapToPrt` on an unbacked PRT range | 83.6 to 85.4 ms | 88.6 to 91.6 ms | 3235 to 3398 ms | 3199 to 3238 ms | | `MapAsPrt` over a PRT range | 82.7 to 87.0 ms | 87.9 to 89.9 ms | 3275 to 3390 ms | 3172 to 3267 ms | | `UnmapToPrt` on a backed range | 37.6 to 38.5 ms | 44.7 to 46.2 ms | not run | not run | | Commit charge added by `MapAsPrt` | 258 MiB | 1.2 MiB | 4.04 GiB | 21 MiB | main commits these pages with a 16 KiB granule, so it also splits a placeholder and commits once per page; the change costs about 23% more on a fresh map and the same on a toggle. The first run of each 4 GiB series is about 40% faster than the rest on both. One 4 GiB pass of each side ran on an idle machine; the other passes ran while a game bench was running and agree with it. The 4 GiB backed case was not run, since it needs 4 GiB of pool memory. - Not tested in a title. ### 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 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: NVIDIA GeForce RTX 4070 Ti, driver 617.42, Windows 11 Pro - [x] AI-assisted: yes
MarcZX10 commented 2026-10-23 05:01:42 +00:00 (Migrated from github.com)

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

Review: APPROVED


Reviewed head 47bbfb00f2bc3662100dbc9d8fe9fa3191a905c3. The change keeps the Windows zero page in a pagefile-backed section whose maximum protection is read-only, replaces PRT mappings with views of that section, and routes fixed remaps through the same mapping path. The existing sceKernelMprotect maximum-view-protection check rejects attempts to elevate those views. The host-import scan now treats read-only mapped views as read-only and follows its copy fallback rather than requesting a writable alias. I found no correctness issue in the inspected paths.

The added AMM regression checks the mapping type and protections after MapAsPrt and UnmapToPrt, zero reads, VirtualProtect refusal, and sceKernelMprotect refusal; the PR also reports that the test fails on main and passes with this change. The author reports a Windows 11 full build, 84/85 relevant AMM/memory/mapping tests passing with the remaining host-import sparse test skipped, and 901/907 AGC/guest-buffer tests passing with three expected skips and three pre-existing sample-order failures reproduced on main. The author's Vulkan validation comparison reports 17 tests on each revision with zero failures and no VUID errors.

I independently ran git diff --check upstream/main...upstream/pr-10025 and python3 tools/check_conventions.py --base upstream/main --head upstream/pr-10025; both passed. Hosted eligibility, conventions, intake, labels, report, ASAN relinker, Ubuntu/Windows/macOS relinker matrix, register-context, and write-watch checks passed. Linux and Windows product jobs were skipped by workflow policy. I could not run the Windows guest tests in this Linux review environment; the Windows runtime results above are author-reported. No title validation is reported, and the console behavior of making an unbacked PRT page writable remains explicitly unknown.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5624091353 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10025#pullrequestreview-5624091353). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T05:01:42Z. **Review:** APPROVED --- Reviewed head `47bbfb00f2bc3662100dbc9d8fe9fa3191a905c3`. The change keeps the Windows zero page in a pagefile-backed section whose maximum protection is read-only, replaces PRT mappings with views of that section, and routes fixed remaps through the same mapping path. The existing `sceKernelMprotect` maximum-view-protection check rejects attempts to elevate those views. The host-import scan now treats read-only mapped views as read-only and follows its copy fallback rather than requesting a writable alias. I found no correctness issue in the inspected paths. The added AMM regression checks the mapping type and protections after `MapAsPrt` and `UnmapToPrt`, zero reads, `VirtualProtect` refusal, and `sceKernelMprotect` refusal; the PR also reports that the test fails on main and passes with this change. The author reports a Windows 11 full build, 84/85 relevant AMM/memory/mapping tests passing with the remaining host-import sparse test skipped, and 901/907 AGC/guest-buffer tests passing with three expected skips and three pre-existing sample-order failures reproduced on main. The author's Vulkan validation comparison reports 17 tests on each revision with zero failures and no VUID errors. I independently ran `git diff --check upstream/main...upstream/pr-10025` and `python3 tools/check_conventions.py --base upstream/main --head upstream/pr-10025`; both passed. Hosted eligibility, conventions, intake, labels, report, ASAN relinker, Ubuntu/Windows/macOS relinker matrix, register-context, and write-watch checks passed. Linux and Windows product jobs were skipped by workflow policy. I could not run the Windows guest tests in this Linux review environment; the Windows runtime results above are author-reported. No title validation is reported, and the console behavior of making an unbacked PRT page writable remains explicitly unknown.
MarcZX10 commented 2026-10-23 05:41:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T05:41:16Z.

Review: COMMENTED


I rechecked the exact head 47bbfb00f2bc3662100dbc9d8fe9fa3191a905c3 against current main 0db47b263d1019043f7d28fad4e7ee0a1ea5ec6a. The current-main conventions and whitespace checks pass, and the branch is based on 9d6adeb5500c6b65e6591bc8e0a3c71b38c4b3e9; current main changes do not touch this PR’s nine paths. Hosted eligibility, conventions, intake, labels, report, relinker matrix/ASAN, register-context, and write-watch jobs pass; Linux/Windows product jobs are skipped by workflow policy. I reviewed the updated test evidence and the author’s Windows validation-layer A/B report. I did not rerun Windows tests locally. The new overlap report flags #9879 in GuestBufferMemory.cpp; please coordinate whether it shares the read-only import path and record an order/dependency if needed. The rest of my earlier review remains applicable: console behavior for making an unbacked PRT page writable is explicitly unknown and recorded in debt.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5624368970 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10025#pullrequestreview-5624368970). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T05:41:16Z. **Review:** COMMENTED --- I rechecked the exact head `47bbfb00f2bc3662100dbc9d8fe9fa3191a905c3` against current main `0db47b263d1019043f7d28fad4e7ee0a1ea5ec6a`. The current-main conventions and whitespace checks pass, and the branch is based on `9d6adeb5500c6b65e6591bc8e0a3c71b38c4b3e9`; current main changes do not touch this PR’s nine paths. Hosted eligibility, conventions, intake, labels, report, relinker matrix/ASAN, register-context, and write-watch jobs pass; Linux/Windows product jobs are skipped by workflow policy. I reviewed the updated test evidence and the author’s Windows validation-layer A/B report. I did not rerun Windows tests locally. The new overlap report flags #9879 in GuestBufferMemory.cpp; please coordinate whether it shares the read-only import path and record an order/dependency if needed. The rest of my earlier review remains applicable: console behavior for making an unbacked PRT page writable is explicitly unknown and recorded in debt.
CnRJay commented 2026-11-01 19:49:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-11-01T19:49:49Z.

Review: COMMENTED


WindowsMappings::MapZero does a placeholder split and a MapViewOfFile3 for every 16 KiB page, under the WindowsMappings mutex. The description says a PRT range now costs "one host page however large it is", which is true for committed memory, but the range now costs one view per 16 KiB where main did one reset and one commit. A 4 GiB range is 262,144 splits and 262,144 views, paid again every time MapAsPrt / UnmapToPrt toggles it.

  • What is the wall time of MapAsPrt and UnmapToPrt for a 256 MiB and a 4 GiB range on main and on this change, same machine? The test in GuestAmpr.cpp maps a couple of pages, so it cannot show this.
  • I assume the view per page is there because a view cannot be longer than the 16 KiB section. If so, that cost belongs in the debt entry next to the memory saving.

You mention in the thread that none of your titles import the PRT AMM functions, so there is no range size from a real title to check this against yet.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5729923498 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10025#pullrequestreview-5729923498). Originally posted by [CnRJay](https://github.com/CnRJay) on 2026-11-01T19:49:49Z. **Review:** COMMENTED --- `WindowsMappings::MapZero` does a placeholder `split` and a `MapViewOfFile3` for every 16 KiB page, under the `WindowsMappings` mutex. The description says a PRT range now costs "one host page however large it is", which is true for committed memory, but the range now costs one view per 16 KiB where main did one reset and one commit. A 4 GiB range is 262,144 splits and 262,144 views, paid again every time `MapAsPrt` / `UnmapToPrt` toggles it. - What is the wall time of `MapAsPrt` and `UnmapToPrt` for a 256 MiB and a 4 GiB range on main and on this change, same machine? The test in `GuestAmpr.cpp` maps a couple of pages, so it cannot show this. - I assume the view per page is there because a view cannot be longer than the 16 KiB section. If so, that cost belongs in the debt entry next to the memory saving. You mention in the thread that none of your titles import the PRT AMM functions, so there is no range size from a real title to check this against yet.
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!10025
No description provided.