feat(libkernel): map regular files with mmap on Windows #6542
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6542"
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 wachi35676 on 2026-10-15T19:28:12Z.
What
mmapof a regular file now works on Windows. Before, it failed withEOPNOTSUPP(PosixMemory.cpp; libkernel debt: "Windows maps no files"). Linux gained the same feature in #5053.CreateFileMappingW), read-write when the descriptor allows writing and read-only otherwise. They are mapped page by page throughWindowsMappings::Map, which now takes the section's view protection instead of always using execute-read-write (newGuestArenaMapFile_nid_postfix). Writes through the descriptor and through the mapping see each other.mmaptime (POSIX leaves it unspecified whether later file changes show). The partial last page is zero-filled, as on FreeBSD.shm_openobjects and JIT shared-memory descriptors are temporary files on Windows, sommapnow maps them too; the debt entries for both are updated.mprotectof a mapped view above the view's protection fails withEACCESbefore anything changes, as FreeBSD'svm_map_protectdoes. This covers writing through a read-only descriptor and addingPROT_EXEC.EOPNOTSUPPand are listed indocs/dev/debt/libkernel.md:PROT_EXECon a file view;ftruncateshrinking a file fails withEINVALwhile any shared mapping of it exists;mmapcalls make separate sections, so write tracking does not treat them as aliases.Title: Dusklight (PPSA99305, v0.3.0, the author's GPL-3.0 release) uses SQLite in WAL mode, which maps its
-shmfile withmmap(MAP_SHARED, fd).main: both of its caches fail on every start:Failed to start transaction: disk I/O error,SQLite DB init failedandFailed to begin pipeline cache schema transaction(3 of 3 runs).SQLite cache init succeededin 4 of 4 runs, the WAL files fill (dawn_cache.db-wal1.4 MB), and a second start reuses the cache. The title reaches the same menu as before, since there is no disc image.Tested
CheckRegularFileMappinginGuestMemory.cppnow also runs on Windows, with new checks: apwriteshows through a live shared mapping, a refusedmprotectreturnsEACCESand leaves the view usable, andremovesucceeds aftermunmap.GuestShmOpen.cppandGuestJitSharedMemory.cppgainmmapchecks.main, all three tests fail at the firstmmap; with this change they pass.main44df21632: 424 of 424guest_*,libkernel*,kernel*,libc*and stream tests pass, excludingagc_/video_out.mprotectto read-write on a read-only file view followed by a write gives the normal access violation, not the abort that bookkeeping changed beforeVirtualProtectwould cause.guest_memory,guest_shm_open,guest_jit_shared_memory,guest_mmap_fixed_anonymous,guest_mprotect_unmapped,guest_mlockandguest_msyncpass, both withmain's library sources and with this change (the library change is Windows-only; the new assertions also hold on Linux). The full tree builds.main5d9dcc42d(onlydocs/dev/debt/libkernel.mdconflicted, with the kqueue entries from #5833 and #5847; the code is unchanged), rebuilt and re-tested on Windows: 424 of 424 again.python tools/check_conventions.py --base origin/mainpasses.Mappings that end past the end of the file (Windows). A shared mapping may end inside the 16 KiB guest page that holds the end of the file, as POSIX
mmapallows. The section keeps the file's size, so the file never grows. In that last guest page:EOPNOTSUPP, and a read-write alias of such a page throws.PS5LM Plus (PPSA99581, v0.8.4) needs this. It runs llama.cpp, whose CPU backend maps the whole GGUF model
MAP_SHARED, PROT_READ.-dev none -ngl 0, Windowsmainand the previous revision both loggedllama_model_load: error loading model: mmap failed: Operation not supported, 3 of 3 runs each.Tested for this revision (rebased on
5de7a46e0):CheckRegularFileMappingcovers the read-only and read-write cases. It fails on the previous revision (mmapreturnsMAP_FAILED, errno 45) and passes now.guest_memory,guest_shm_open,guest_jit_shared_memory,guest_mmap_fixed_anonymous,guest_mprotect_unmapped,guest_mlockandguest_msyncpass.mprotectacross the view/private boundary (refused withEACCES, nothing partly applied),munmapandMAP_FIXEDover the last page (no leftover allocations), write tracking on both parts, and the end-of-file bounds at several file sizes and offsets.Overlap: #7299 (named shared memory on Windows) conflicts with this in
docs/dev/debt/libkernel.md. With both, named objects would also take the file-mapping path here, so whichever lands second should reconcile the access checks.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)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T20:24:09Z.
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.
main)core/libs/tests/GuestMemory.cppL2467docs/dev/debt/libkernel.mdL113-114docs/dev/debt/libkernel.mdL130core/libs/prx/libc/include/WindowsMappings.hppL536core/libs/prx/libc/include/WindowsMappings.hppL79docs/dev/debt/libkernel.mdL114-115Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
core/libs/prx/libc/src/GuestArena.cppcore/libs/prx/libc/include/GuestArena.hppcore/libs/prx/libc/src/GuestArena.cppcore/libs/prx/libkernel/DirectMemory/DirectMemory.cppcore/libs/tests/GuestMemory.cppcore/libs/prx/libc/src/GuestArena.cppcore/libs/prx/libkernel/DirectMemory/DirectMemory.cppcore/libs/tests/GuestMemory.cppdocs/dev/debt/libkernel.mdcore/libs/tests/GuestJitSharedMemory.cppcore/libs/tests/GuestMemory.cppcore/libs/tests/GuestShmOpen.cppdocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mdcore/libs/prx/libc/src/GuestArena.cppcore/libs/prx/libkernel/DirectMemory/PosixMemory.cppcore/libs/tests/GuestMemory.cppdocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mdcore/libs/prx/libc/src/GuestArena.cppcore/libs/prx/libkernel/DirectMemory/DirectMemory.cppdocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mdcore/libs/prx/libkernel/DirectMemory/DirectMemory.cppcore/libs/tests/GuestMemory.cppdocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mdcore/libs/prx/libkernel/DirectMemory/DirectMemory.cppdocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mdcore/libs/prx/libc/src/GuestArena.cppcore/libs/prx/libkernel/DirectMemory/DirectMemory.cppcore/libs/prx/libc/src/GuestArena.cppcore/libs/prx/libkernel/DirectMemory/DirectMemory.cppcore/libs/prx/libkernel/DirectMemory/PosixMemory.cppcore/libs/tests/GuestMemory.cppdocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mdImported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-17T12:02:18Z.
Rebase handoff — rebased onto current
main(authorship unchanged). One docs-debt conflict, unioned.Verified on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release,
BUILD_TESTING=ON, AMD RX 9070 XT / RADV):tools/check_conventions.py --base origin/mainpassesmainregression (agc_driver_tessellationsegfault, #4727), not this PR (your Windows mmap change is Windows-gated)To pick it up:
Head is
9006b028e.Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-17T16:34:14Z.
Thanks for the rebase, @blawesom. I've pushed my own rebase instead (
39d9005fe, onmain66702f1cf), because thedocs/dev/debt/libkernel.mdunion in9006b028ehad left the file inconsistent:JIT shared memoryandkqueue and kevententries appeared twice;main'sEVFILT_VNODE, sceKernelAddFileEvent and sceKernelDeleteFileEvententry was gone (196 entries before, 196 with a duplicate pair and one missing after).The new head keeps
main's kqueue entry and this PR's JIT entry, so the only entries that change are the three this PR edits. The code is byte-identical to the previous head.Windows 11, WinLibs GCC 15.2.0: full build passes, and 454/454
guest_*,libkernel*,nid*,libc*and stream tests pass.AI-assisted: yes (Claude Code); results above were run on this machine.
Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-17T23:53:31Z.
I extended this PR to support shared mappings that end inside the guest page holding the end of the file. It is rebased on
5de7a46e0and is still one commit, now2eabf6382.Found with PS5LM Plus (PPSA99581, v0.8.4). Its llama.cpp CPU backend maps the whole model file shared and read-only. The test model is Qwen2.5-0.5B-Instruct q4_k_m from Qwen's Hugging Face repo (Apache-2.0), 491,400,032 bytes, so the rounded length runs past the end of the file. The previous revision refused that and gave
mmap failed: Operation not supportedin 3 of 3 runs.A Windows view can't cover host pages past the section size, and a larger section would grow the file.
MapViewOfFile3does accept the exact remaining size when it replaces a 4 KiB-rounded placeholder, and zero-fills the rest of that host page. So the last guest page is now that view plus private zero pages, and every file byte stays shared.With this revision the model loads, and "The capital of France is" completes as " Paris. It is the largest city in Europe…" in 4 of 4 runs. The description has the test results and a note on the overlap with #7299.
AI-assisted: yes (Claude Code); results above were run on this machine.
Imported from boykopovar/AnyPS5. Originally posted by screamm on 2026-10-18T13:38:59Z.
Windows run of head
2eabf6382, merged ontomain0d078f75d(clean merge). The PR has since been rebased to45a702a10; its diff against the merge base is line-identical apart from hunk offsets and one unrelated context line in the debt doc, so the results below apply to the new head as well (not rebuilt).libspass, with no warning in the files the PR changes.--repeat until-fail:3:guest_memory(which holdsCheckRegularFileMapping),guest_shm_openandguest_jit_shared_memorypass 3/3.PosixMemory.cpp,DirectMemory.cpp,WindowsMappings.hppandGuestArena.cpp/.hppfrommainand the tests kept, all three fail on 3 of 3 runs.guest_memorystops atGuestMemory.cpp:2440, the first sharedmmap. With the source restored they pass again.ctest -j8 --timeout 120 -E '^agc_driver_|^video_out_flip'): 928 of 936 pass and 2 are skipped. Six tests timed out in the first pass while the host was busy (hardware_oracle_compare,conventions_pr_template,guest_file_lock_process,conversion_diagnostics,guest_none_relocations,tls_function_coverage_register_address_loads). All six pass serially (hardware_oracle_compareonce hit its own 20 s limit, then 11 s and 12 s). I did not run the suite on plainmain.tools/check_conventions.py --base origin/mainandgit diff --checkpass. The diff adds no environment variable and no export.MAP_FIXEDover the hole works); a file mappingMAP_FIXEDover anonymous pages at a file offset works; a whole page past the end,PROT_EXECand an empty file return 45; a write through the view reaches the file when read through another handle. 256 MiB shared read-only:mmap200-360 ms,munmap130-200 ms. Against themainsources the mapping checks fail with errno 45.Titles. PS5LM Plus is not a local title, so I could not run the case in the description. I ran five relinked titles that import
mmapfor 45 s with a local diagnosticlibkernelthat logs everymmapwith a descriptor: only PPSA99169 RetroArch (its frontend picker) maps files. It callsmmap(len=0x200008, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)twice, on the Mesa cache filesradv-shader-cache/mesa_shader_cache_db/indexandradv_builtin_shaders/index(2097160 bytes, so the same read-write end-of-file page case as the model), and both succeed. PPSA85300 VLC, PPSA99997, PPSA99039 and PPSA99420 map no file in that window. PPSA99169, 60 s, two runs each: no stop line onmain(4303 and 4428 frames) or with the PR (4800 and 5302 frames), all four killed at 60 s with 12 frame dumps; noFAIL:,FATAL:,not implementedorDEVICE_LOSTline. Frame counts do not compare because a build was running on the host. What does change: with the PR the picker goes on to open and writepart0/mesa_cache.idxandpart0/mesa_cache.dbin both cache directories (20929 and 2469 bytes after one run); onmainthemmapis refused and those files are never opened. The PR run usedmain's runtime from3e3e8b905withlibkernelandlibcbuilt from the merge, which is one AGC commit older than the merge base.Notes on the diff, none blocking:
mapSharedcallsMapViewOfFile3(..., MEM_REPLACE_PLACEHOLDER)with the exact byte count to the end of the file. The MSDN page says the size must be a multiple of the page size and must match the placeholder when replacing one. It works on this host for every end-of-file case above, but it depends on behaviour that is not documented, and I only had one Windows build.0xccand0xaaI had written). It never reaches the file. Only the private zero pages after it are private to the mapping. The debt entry could say so.msyncis still only a validity check and never callsFlushViewOfFile, somsync(MS_SYNC)gives no durability on Windows for a mapped file. MSDN also says a mapped file andReadFile/WriteFileare not necessarily coherent (remote files are not); the local file case behaved as the PR says.mmaptime, so a large private mapping costs the read and the memory. Not measured on a large file.sceKernelMunmap) merges with this PR without a conflict. I built the pair: the fiveguest_flexible_memory_*tests,guest_memory,guest_shm_open,guest_jit_shared_memoryandguest_truncatepass, and a 256 MiB view unmaps in the same time. File views are not charged to the flexible budget andsceKernelMunmapreleases only recorded flexible ranges.File::IsDirectoryDescriptor. This PR rejects directories withFile::DirectoryDescriptorPath, which can return nothing if #6228's path lookup fails, and a directory handle reportsFILE_TYPE_DISK; whichever lands second should useIsDirectoryDescriptorthere. I did not build that combination. #6321 conflicts inGuestMemory.cpp.Not checked: PS5LM Plus, Windows builds other than 10.0.26220, AMD and Intel GPUs, Linux and macOS, a large private mapping, the suite on plain
main.Written with Claude Code assistance (validation by a subagent, reviewed in this session).