fix(libkernel): map unbacked AMM PRT pages to one read-only zero page on Windows #10025
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/10025"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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_postfixreplace the placeholders of a range with views of the zero section.DoMapZeroin DirectMemory takes the same path asDoMapAnon(the shared part is nowMapAnonymous) and maps the zero views instead of committing; it only accepts a protection that is read-only on the host._zeroPrtcalls it.PAGE_READONLYlike 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:
PAGE_READONLY, notPAGE_READWRITE. No view of it can ever be writable:VirtualProtectto read-write fails,sceKernelMprotecthits the existing check against the maximum protection of a mapped view and throws, and a read-writeMapViewOfFile3alias 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.DoMapZerobody:DoMapAnonandDoMapZeroshareMapAnonymous, so placement, overlap and bookkeeping stay in one place.GuestArenaMapAlias_nid_postfix, which throws for views it does not track.What the console does when
sceKernelMprotectmakes an unbacked PRT page writable is not known; it throws and is recorded indocs/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 afterMapAsPrtand afterUnmapToPrtthat every unbacked page is aPAGE_READONLYmapped view, reads zero, and refusesVirtualProtectto read-write. It does not checksceKernelMprotecton these pages, since the console behaviour there is unmeasured. It fails onmainand 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_sparseis 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_referenceandagc_driver_sample_order_recipe_depth_locationsfail 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_sparseskipped), noVUID-errors, and neither log reports the layer as not found.Rebased on main
7feb37ee3e(headc73b65f4b9).ctestof the AMM, AMPR, memory, mmap, mprotect, flexible memory, named shared memory, write-watch andwindows_mappings_releasetests: 29 of 29 pass. Conventions pass.MapAsPrt/UnmapToPrtwall 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. main7feb37ee3eand 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):MapAsPrtover a reserved rangeUnmapToPrton an unbacked PRT rangeMapAsPrtover a PRT rangeUnmapToPrton a backed rangeMapAsPrtmain 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
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)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 existingsceKernelMprotectmaximum-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
MapAsPrtandUnmapToPrt, zero reads,VirtualProtectrefusal, andsceKernelMprotectrefusal; 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-10025andpython3 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.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T05:41:16Z.
Review: COMMENTED
I rechecked the exact head
47bbfb00f2bc3662100dbc9d8fe9fa3191a905c3against current main0db47b263d1019043f7d28fad4e7ee0a1ea5ec6a. The current-main conventions and whitespace checks pass, and the branch is based on9d6adeb5500c6b65e6591bc8e0a3c71b38c4b3e9; 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.Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-11-01T19:49:49Z.
Review: COMMENTED
WindowsMappings::MapZerodoes a placeholdersplitand aMapViewOfFile3for every 16 KiB page, under theWindowsMappingsmutex. 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 timeMapAsPrt/UnmapToPrttoggles it.MapAsPrtandUnmapToPrtfor a 256 MiB and a 4 GiB range on main and on this change, same machine? The test inGuestAmpr.cppmaps a couple of pages, so it cannot show this.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.