fix(libkernel): place a fixed mapping at any page-aligned address whatever its alignment #20349
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/20349"
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 Gag21 on 2026-11-15T02:37:17Z.
What
With
MAP_FIXED,MapPlacedchecked the requested address against the alignment argument, so a fixed mapping at a page-aligned address that is not a multiple of that alignment threw:A PS4 title (CUSA01709) hits this during its first heap setup. With
APS5_TRACE_MEMORY=1the run shows an anonymous mapping of 0x17d98000 bytes at 0x5000000000 and an allocation of 0x22268000 bytes of direct memory; the title then maps 0x22268000 bytes at the fixed address right after the first mapping, 0x5017d98000, with an alignment of 0x200000. That address is aligned to 0x8000, not to 0x200000.Both references agree that the alignment only matters when the kernel picks the address:
vm_mmap, the baselinedocs/dev/REVERSE_ENGINEERING.mdgives for libkernel: withMAP_FIXED,sys_mmapchecks only that the address is page-aligned (if (addr & PAGE_MASK) return (EINVAL);), andvm_mmap_objectturnsMAP_ALIGNEDinto a search constraint only in thefitit(not fixed) branch.MemoryManager::MapMemory, at the commit the GNM and kernel debt entries already cite: the alignment goes toSearchFreeonly whenFixedis not set.So a fixed mapping now needs only a page-aligned address. The alignment value itself is still validated (a power of two no smaller than a page), as before and as shadPS4 does.
MapPlacedused the alignment for nothing else on the fixed path, so this covers every caller that reaches it withMAP_FIXED: the direct, flexible, anonymous and file-backed maps andsceKernelReserveVirtualRange.docs/dev/debt/libkernel.mdgets an entry: this follows FreeBSD and shadPS4 and is not measured on a console.Tested
Windows 11 Pro 10.0.26300, i5-6600K, NVIDIA GeForce RTX 3070, WinLibs GCC 16.2.0, CMake 4.4.4, Ninja, Release,
BUILD_TESTING=ON, onmain61f33fc26c.CheckFixedDirectMapIgnoresAlignmentinguest_memory(core/libs/tests/GuestMemory.cpp): maps two 2 MiB pages of direct memory with an alignment of 0x200000, unmaps them, then maps 2 MiB withMAP_FIXEDat that base plus one page, with the same alignment. It checks the mapping lands at the requested address, thatsceKernelVirtualQueryreports it as direct memory at the right offset with read/write protection, and that its last byte holds a write.DirectMemory.cppline reverted and the test kept,guest_memoryfails withInvalid memory address, alignment or range; with the change it passes.memory|mmap|mprotect|munmap|direct|flexible|protection|page_table|arena|reserve|virtual|write_watch|ampr|apr_. Withctest -j 3, 13 graphics tests failed; run one at a time, 12 of them pass. The last,agc_image_memory_accounting, fails the same way onmain61f33fc26con this machine (a Vulkan allocation returning -2 in its test image), twice onmainand once on this branch, so it is not caused by this change.Ebony Heap : 0x5000000000 381+546MBytes), starts its worker threads and opens its Vulkan device. Without it, it stops at the mapping with the message above.python tools/check_conventions.py --base origin/mainpasses.python tools/pr_overlap.py --branchfailed closed (exit 2) on 34 open pull requests that conflict withmain. Three of them changeDirectMemory.cpporGuestMemory.cpp(#7149, #16372, #18293); none of their patches to those files changes the fixed-mapping check. The other 31 share onlydocs/dev/debt/libkernel.md.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-11-15T03:07:24Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this change; the fixed path validates the requested range at page alignment while retaining the existing alignment-argument validation.
What I ran
Reviewed head
3a56f1ce6b5ea623ff37c0411be27f32f48bd67ausing the prepared patch and pinned repository reader. No tests or builds were run.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
No programmatic checks or console behavior were independently verified. Read:
core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp,core/libs/tests/GuestMemory.cpp, anddocs/dev/debt/libkernel.md; all three files listed in the manifest as reviewed were read.files.jsonwas unavailable through the reader. No changed files were excluded in the manifest.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-15T03:07:34Z.
Review: APPROVED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
3a56f1ce6b:tools/check_conventions.pypassed;git diff --checkclean.guest_memory_testsbuilt cleanly.guest_memory_pool_stats,guest_memory_resource,guest_memory_lifetime,guest_memory,guest_mmap_fixed_anonymous,guest_mmap_executable):core/libs/prx/libkernel/DirectMemory/DirectMemory.cppreverted tomainand the new tests kept, the suite fails: Restoring the change makes it pass again.Generated withfooter from the description; the AI-assisted checkbox can stay.Looks Good To Me