Unmapping waits for a GPU lease on a mapped part with the registry lock released,
and that wait came after the reservation had checked the free parts. A free part
taken during the wait made the call fail after the unmap. The wait now comes
first, so the check sees a part taken during it.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
A MAP_FIXED reservation over a range with mappings below registered image memory
unmapped them and then failed with "guest image memory cannot be unmapped", so
sceKernelMemoryPoolReserve returned EINVAL with a mapping gone. Such a range is
now refused before anything is unmapped, with the answer given before.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
sceKernelReserveVirtualRange with MAP_FIXED over a range that was partly mapped
and partly free ended the process with "fixed mapping ... overlaps a registered
guest allocation", and sceKernelMemoryPoolReserve returned EINVAL. Without
no-overwrite they now replace the mapped parts, as FreeBSD 11.4's vm_map_fixed
deletes the range before it inserts, and as shadPS4 does. A range that also
holds a host region still fails with ENOMEM, before anything is unmapped.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
A second firmware 13.00 payload run tried every single flag bit on a block,
in a released range before another block and past the last block, plus
negative offsets. Bits 1 to 27 are ignored; bit 28 returns EINVAL; bit 30
returns EINVAL only on a block and EACCES where none is found; bits 29 and
31 return EACCES where no block is found and write other records on a block,
so they throw there. Find-next keeps working with other bits set. A negative
offset returns EACCES for flags 0, bit 30 and bit 31; with find-next it
returned a block, so that throws. Unmeasured combinations throw and are
listed in the debt entry.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A payload process on firmware 13.00 got the block for flags 2 and 4 and for
other bits besides bit 30 (EINVAL), a size of 0 returned 0 without writing,
and flags 2, 4 and 8 where no block is found returned EACCES. Bit 31 wrote
another record and a size of 0 with no block was not measured, so both throw.
System software 12.40 returned EINVAL for flags 2 and 4; the debt entry keeps
that.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
mprotect returns EINVAL only when the page-rounded end of the range
wraps, as FreeBSD 11.4 kern_mprotect does, and skips the part of the
range below the first page or at or above 0x800000000000, as
vm_map_protect clamps with VM_MAP_RANGE_CHECK. A range at NULL or
outside the user map now returns 0 and changes nothing instead of
EINVAL, matching the unmapped ranges measured on a PS5 (firmware 13.60).
On Windows the protection walk ends where VirtualQuery reports an
address above the process address space, so such a range is a gap.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
MAP_SHARED | MAP_ANON, with or without MAP_FIXED, maps zero-filled memory
through the private anonymous path; fd -1 and offset 0 stay required.
MAP_ALIGNED(n) places a map without MAP_FIXED at a 2^n aligned address,
fails with EINVAL for n below the 16 KiB page shift or from 64, and is
ignored with MAP_FIXED, for anonymous and file maps; from 47 no user
address is so aligned and the map fails with ENOMEM. A hint inside a
sceKernelReserveVirtualRange reservation is rounded up to the requested
alignment before it is placed there, as vm_map_find searches upward from
it; this also applies to sceKernelMapDirectMemory and
sceKernelMapDirectMemory2, and the caller's address is written only once
the map succeeds. MAP_ALIGNED_SUPER aligns only maps of 2 MiB or more
to 2 MiB, as pmap_align_superpage does. MAP_HASSEMAPHORE, MAP_NOSYNC, MAP_NOCORE and
MAP_PREFAULT_READ are accepted. All of these failed with EOPNOTSUPP.
Other flags still fail with EOPNOTSUPP and are recorded as debt;
FreeBSD ignores 0x20 and 0x40 and refuses the rest of its unknown bits
with EINVAL. Source: FreeBSD 11.4 sys/vm/vm_mmap.c kern_mmap_fpcheck and
vm_mmap_object, sys/vm/vm_map.c vm_map_find, sys/sys/mman.h.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
sceKernelVirtualQuery set is_committed for every registered or
host-queried range. A PS5 payload (firmware 13.590.001) reports it only
for direct memory, and 0 for flexible memory whether touched or not,
stacks, images and reservations. It is now set only for a direct
mapping.
mincore was the only reader of is_committed and used it to tell a
mapping from a reservation, so it now asks GuestReservation directly
and still reports every mapped page as resident and a reserved one as
not.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Port the Linux read traps of GuestWriteWatch to Windows: a trapped
range is made PAGE_NOACCESS and a vectored exception handler added with
the first resolver runs the resolver on its own thread. Traps are set
only on read-write resident guest arena pages that WindowsMappings does
not protect itself. The arena's commit, reset and mappings and
libkernel's mprotect resolve traps over their range first, and an alias
of a resident run drops the traps in it.
No library sets traps on Windows yet.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* perf(agc): trap CPU reads of label readbacks on Linux instead of storing them
* fix(libc): carry the GPU mutex bypass and trapped ranges to the Linux read traps
* perf(agc): scan pending images once per label and trap only host-imported ones on Linux
* perf(agc): skip trapped images at a label while the pending set and the traps are unchanged
* fix(agc): resolve read traps on Linux before a host pointer import pins its pages
* fix(agc): keep label read traps off host pointer imports except on NVIDIA's proprietary driver
* test(agc): check that a range is not trappable once its import is gone with a host-importing context
* test(agc): check that GPU submissions survive a held read trap over the imports label traps are set on
* test(agc): run the read trap import submit test on a CPU device and report skips as device limitations
* fix(agc): let the draw committer read a label-trapped page instead of waiting for its resolver
* fix(agc): store what a label trap's resolver can before giving the blocked draw committer its held ranges unstored
* fix(agc): leave a read-trapped range unimported instead of resolving its traps under the write tracker lock
* test(libc): check that registering a new mapping over a read trap keeps the mapping's protection
* fix(libc): include <atomic> for the Windows read trap generation
Co-authored-by: Dean <158058829+oneandonlydean@users.noreply.github.com>
Planet Zoo (PPSA18026) marks a 32 GiB range at 0xd000000000 as PRT with
the AMM, so its unbacked pages are zero pages with GPU read protection
(0x10). Since the AGC driver takes GPU-only memory into its leases
(#23770), every address-based build took that range in. It could neither
import it (over the host import budget) nor mirror it, so it counted it
as a copy and aborted: "an address-based build copies 32761 MiB of 3
registered ranges, past 64 MiB (APS5_ADDRESS_COPY_MAX_MIB)".
Zero pages now keep their GPU read protection (sceKernelVirtualQuery is
unchanged) but are registered without the GPU bits, so the driver leaves
them out of its leases and host imports, as it left all GPU-only memory
before #23770. Pages the AMM maps into the range keep their protection's
bits. Outside Windows, DoMapZero now passes the zero flag to
MapAnonymous instead of calling DoMapAnon; the mapping itself is
unchanged.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* test(kernel): pin sceKernelMtypeprotect results for memory types 10, 127 and -1 and for protection 0x400
* fix(kernel): refuse memory types 10, 127 and -1 and protection 0x400 in sceKernelMtypeprotect
* docs(debt): state the PS5 results of sceKernelMtypeprotect for memory types and protection bits
* perf(libkernel): back flexible memory with a memfd on Linux
* docs(debt): describe the flexible memory backing on Linux
* fix(libkernel): keep flexible memory anonymous and host-imported when its memfd path fails
* fix(libkernel): reject empty flexible backing queries and keep remainders when a hole punch fails
* docs(debt): shorten the flexible memory backing entry and note a failed hole punch
* fix(agc): discard a failed dma-buf import before its host-pointer retry
sceKernelMapDirectMemory, sceKernelMapDirectMemory2,
sceKernelMapFlexibleMemory, sceKernelMapNamedFlexibleMemory and
sceKernelReserveVirtualRange passed the guest address slot straight to
DoMapDirect, _mapFlexible and DoReserveVirtual, which read the hint and
stored the mapped address through it, and read it again for
_releaseFlexible and the range name. An unmapped or read-only slot
crashed the host.
Each export now reads the hint into a local with
GuestCopyIn_nid_no_patch and checks the slot with
GuestWritable_nid_no_patch before mapping, so an unusable slot returns
SCE_KERNEL_ERROR_EFAULT and maps nothing, maps through the local, and
stores the result with one GuestCopyOut_nid_no_patch. errno is kept
across the copies, as the batch map code in the same file does. A null
slot still throws, and results for usable slots are unchanged.
A batch map type-protect entry passed its protection to DoMtypeprotect,
whose protection validation throws std::invalid_argument for bits outside
0x3F7, so an undefined bit ended the title instead of returning an error.
The entry now answers SCE_KERNEL_ERROR_EINVAL (0x80020016) through the
existing UndefinedProtection() helper, before its range is looked at, and
leaves the range untouched.
The single sceKernelMtypeprotect change and the batch protect masking are
left to #19183 and #22051.
The pool's first-fit search stepped through allocated memory by at most
the requested length and checked the release claims, under the global
direct-memory lock, at every step. A title that allocates small blocks
above a large allocated prefix walked the whole prefix on every call:
SuperTuxKart (PPSA69421) allocates about 1000 64 KiB blocks per second
above 1.4 GiB, at about 22,000 steps and lock acquisitions each.
The search now checks the page map first and moves past the whole used
run in one step, never past the search end, and checks the release
claims only for a window whose pages are free. Both checks still have to
pass, and only windows that overlap a used page or a claim are skipped,
so the first fitting address is unchanged.
On a PS5 payload process (firmware 11.60) madvise(0x1000), (0x2000), (0x3000)
and (0x3fff) fail with EINVAL and (0x4000), (0x4001) return 0, whatever the
length. The address is now checked against the same minimum as posix_madvise
(PS5_PAGE_SIZE, 0x4000); the upper bound already matched.
sceKernelBatchMap2 and sceKernelMemoryPoolBatch read each entry of the
guest batch array through the guest pointer and passed &entry.start of
that array into DoMapDirect and _mapFlexible, which read the hint and
wrote the result pointer through the raw guest memory. An unreadable
entries array or unwritable result slot faulted the host instead of
returning an error.
Each entry is now read with GuestCopyIn_nid_no_patch into a local, the
map operations run against the local, and the start a map operation
produced is stored back to the entry with GuestCopyOut_nid_no_patch.
A copy failure returns SCE_KERNEL_ERROR_EFAULT and stops the batch;
num_entries_out keeps reporting the number of entries whose operation
ran and whose start was stored.
sceKernelSetVirtualRangeName and the named-mapping calls read the name in
place with strnlen, so an unreadable name crashed the host. The name is now
copied through CopyGuestString_nid_no_patch, page by page and stopping at
the terminator. An unreadable name returns EFAULT from
sceKernelSetVirtualRangeName, as a null one does, leaves a named mapping
unnamed, and throws in sceKernelMapNamedSystemFlexibleMemory, as a null
name does.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>