Commit Graph

84 Commits

Author SHA1 Message Date
alfalfa-star f3a2dcff7d test(libkernel): map fixed over a hole inside a direct allocation
One 4-page direct mapping loses its second page to munmap, then pages
1-2 are mapped fixed. The range starts in the hole; before the fix the
unmap of the overlap threw "guest protection or unmap range has a
hole". The pages around it keep their data.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 12:34:12 +02:00
xXPBSXx aace6a3f38 fix(libkernel): report the largest free direct memory block from sceKernelAvailableDirectMemorySize
The call returned the first free gap in the range instead of the largest free block.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-11 00:58:28 +02:00
raghav314 be1f501525 fix(agc): import GPU-mapped guest memory when it is mapped
The registry calls an observer on the mapping thread, after the registry
lock is released, with the ranges a kernel mapping gave GPU access:
direct and flexible maps with it, and fixed remaps with it over a
registered range (a reservation). The mutation looks them up while it
still holds the lock and hands them over weakly, with the registry
generation it ended at, so the observer does not keep a range alive.
The driver imports those ranges on its current device, so an
address-based build finds the import instead of pinning the memory on
the queue that first uses it. A range that gains GPU access through
sceKernelMprotect, and AnyPS5's own libc heap, are still imported on
first use.

The import at map does not reconcile the other imports with the
registry, so its cost does not grow with the number of imports. It skips
a range that is gone, and any range when the imports were reconciled
with a newer registry since; the next lookup imports those as before.
It installs the pin waiter as the other import paths do, so unmapping a
range imported at map retires its import. The live import bytes are kept as a
running total instead of being summed over every import. Once the import
table belongs to the current device the import takes only the table's
lock; the first import on a device (which runs the write-watch probe)
and a device that was just replaced go through the GPU lock. Clearing
the observer waits for calls already running.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 00:23:23 +02:00
Dean 262c031692
Merge pull request #2907 from oneandonlydean/carry/libkernel-reserved-range-hint
fix(libkernel): map non-fixed hints inside a reserved range in place
2026-10-11 09:26:41 +13:00
ps54all 3444ebf5cd fix(libkernel): map non-fixed hints inside a reserved range in place
A map without MAP_FIXED whose hint lies wholly inside a range still
reserved by sceKernelReserveVirtualRange is placed at the hint, as a fixed
map into the reservation would be. Hints outside reservations and ranges
that are partly mapped keep their hint behaviour. The reservation is
checked under the registry mutation, so it cannot be released and its
address reused between the check and the map (MarcZX100, #2687).

Fixes #2687

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01A2cY3SsTYnh4u4ZtEr2Adx
2026-10-11 07:10:29 +13:00
Scott Schuster 58e871cf2a fix(libkernel): place sceKernelReserveVirtualRange at its address hint
Without MAP_FIXED the address is a hint, as for FreeBSD's mmap: the reservation goes at the hint when that range is free, else at the first free range above it. The hint was dropped, so every hinted reservation landed at the lowest free address.

RetroArch (PPSA99169) reserves its cores' direct-memory heap near 0x2000000000 outside the GPU window (0x200000000-0x300000000) and refuses a range inside it; with every reservation at 0x200400000 the heap was never made, and Dolphin's first aligned allocation returned null.

AI-assisted (Claude Code).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 07:59:00 -05:00
Dean b0cccf20a0
Merge pull request #861 from jeffrey12cali/fix/linux-guest-arena
fix(guest-arena): place guest address space in the PS5 window on Linux
2026-10-10 23:55:28 +13:00
Dean Galvin 766370108d fix(libkernel): read random devices in the Windows sceKernelPread
The Windows sceKernelPread stopped going through pread when it learned to
return errors, and with it lost the random device read, so a pread on
/dev/urandom reached ReadFile on a handle that is not a disk and failed
with ESPIPE. Read random devices first, as the Linux path does.

A pread into a read-only guest mapping now returns EFAULT instead of
throwing, matching Linux and the AIO read beside it, so the Windows
guest memory test checks for that return.
2026-10-10 23:26:37 +13:00
jeffrey12cali c4062623bd test(guest-arena): keep the fixed-mapping probe on Linux only
CheckUnmappedArenaRangeStaysReserved uses mmap, PROT_READ, PROT_WRITE,
MAP_FIXED_NOREPLACE and EEXIST directly, so the Windows build fails to compile it.
The probe exists to show that an unmapped arena range still rejects a hostile
MAP_FIXED_NOREPLACE mapping, which is Linux-specific behaviour.

Guard the function and its call site with __linux__. The other tests in the file
compile on both platforms.
2026-10-10 22:56:07 +13:00
jeffrey12cali e4e0702946 fix(guest-arena): place guest address space in the PS5 window on Linux
The arena constructor was Windows-only, so on Linux Available() was false: the guest heap fell back to the host allocator and direct memory landed wherever the kernel pleased. Titles that require a low virtual address rejected the mapping and re-issued the identical request, which surfaced as a start-up assert inside their own allocator.

Seed the window from the mappings that already exist instead of reserving it up front, because a PROT_NONE reservation of the whole range starves the kernel's own top-down mmap, which picks addresses just below the stack inside that range. Commit, reset and set-protection become portable so the arena owns its used ranges on both platforms, and fixed mappings are checked against the arena's used ranges rather than against EEXIST from a reservation.

The window-placement and no-overwrite checks in guest_memory were compiled only for Windows; they now run on Linux too, and the window check fails without this change.

Fixes #692
2026-10-10 22:56:07 +13:00
raghav314 c11a9cb59f fix(agc): address-based builds map only memory mapped for the GPU
The registry records whether a range was mapped or protected with GPU
access (0x10/0x20), and an address-based build leaves out ranges without
it, as the console's GPU page tables do. The main image is registered
without GPU access; the title's own mprotect with GPU bits adds it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 14:34:34 +05:30
Adrià 8fded89e6b
Merge pull request #1803 from tv7/fix/mprotect-guest-module-images
fix(libkernel): reprotect bundled guest module images on Windows
2026-10-10 10:45:37 +02:00
Adrià 4fbf6ca5a3
Merge pull request #1960 from Raghav314/fix/agc-import-leaves-pages-unarmed
fix(agc): host imports write-protect only pages a collect already protects
2026-10-10 10:45:04 +02:00
Adrià 874c144712
Merge pull request #1909 from TheAggie/feat/libkernel-mmap-aligned-super
feat(libkernel): accept MAP_ALIGNED_SUPER in mmap
2026-10-10 10:44:57 +02:00
Lexroid b1a14c6f36 fix(libkernel): unregister guest module images in dlclose and propagate failures
Move the image unregistration out of the Module destructor into
dlclose_nid_postfix. It runs while the handle is still registered, so a
failure (the image stays leased by GPU work) propagates to the caller and
leaves the module loaded and usable. Images shared by another open
handle stay registered until the last handle closes.

guest_memory now checks that dlclose under a held lease throws and keeps
the module, its symbols and its protection.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 10:23:50 +02:00
Lexroid 57c179c74d fix(libkernel): unregister guest module images when they are unloaded
A .guest.prx image registered by sceKernelMprotect stayed in the guest
allocation registry after dlclose freed it, so its dead ranges blocked
later mappings at that address and could reach GPU builds.

The Windows module destructor now unregisters the image before
FreeLibrary. Unregistering waits for outstanding leases on the image like
unmap and protect do; if the wait fails the module stays loaded.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 10:23:42 +02:00
Lexroid 9f1d7b0516 fix(libkernel): reprotect bundled guest module images on Windows
On Windows, sceKernelMprotect accepted only the executable's image and threw
"Memory protection of a foreign image is not supported" for every other
image. Bundled title modules are relinked into DLLs named <module>.guest.prx,
so a title that reprotects its own module pages failed. Dying Light 2's
bundled libNRD does this at start-up (#1794).

DoMprotect now also accepts images whose file name ends in .guest.prx, and
registers their committed pages like the executable's. Other images are
still refused.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 10:23:42 +02:00
Pouare514 03d975af3c fix(libkernel): reject malformed ranges in sceKernelReleaseDirectMemory
A zero, unaligned, overflowing or out-of-range length reached the physical pool, where a sub-page length silently freed a whole page and fragmented the block map, and an out-of-range length walked the page table past the backing store. Validate alignment and bounds up front and return EINVAL instead.
2026-10-10 08:22:59 +02:00
Adrià 40aeb97d57
Merge pull request #1152 from alfalfa-star/perf/linux-direct-memory-import-chunks
perf(agc): share one dma-buf import per 64 MiB of Linux direct memory
2026-10-10 08:14:40 +02:00
raghav314 c7f4690cd3 fix(agc): host imports write-protect only pages a collect already protects
Importing guest memory collected the write watch over the whole import,
which write-protected every page of it, so pages nothing had collected yet
took a write fault on their first CPU write. The import now collects only
the parts a collect already protects (GuestWriteWatchCollectArmed). The
rest stays unprotected until something collects it, which reports it as
written; a GPU write recorded into such memory (MarkWritten) first collects
the never-collected parts of its 64 KiB blocks
(GuestWriteWatchCollectFresh), so that report is older than the GPU's
results and does not read as a CPU write over them.

Those pages are stamped as written and their CPU pages are noted, but the
block's CPU stamp is left alone, so a GPU label record or a pending storage
image elsewhere in the block does not see a CPU write. StoredOver and
UnchangedSinceCollected read the noted pages whenever a block has them, so
a store to such a page is still seen by a check over that page. The extra
generation step happens only when something was collected; a block whose
write protection fails is unwatched and the write returns no generation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 11:38:28 +05:30
Scott Schuster 456dc87879 feat(libkernel): accept MAP_ALIGNED_SUPER in mmap
An anonymous private mapping with MAP_ALIGNED_SUPER (FreeBSD MAP_ALIGNED(1))
is placed on a 2 MiB superpage boundary. Yamagi Quake II requests it for its
hunk allocations of 2 MiB and more.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 00:47:40 -05:00
Dean b667d1e30d
Merge pull request #2328 from TheAggie/fix/libc-collect-writes-whole-range
fix(libc): check a whole range before collecting its writes with reset
2026-10-10 18:19:17 +13:00
Dean 698254f048
Merge pull request #2236 from mrk1987/fix/release-direct-memory-unmaps-views
fix(libkernel): unmap direct memory views when the memory is released
2026-10-10 17:45:32 +13:00
Dean b860fa0c9f
Merge pull request #1929 from MarcZX100/fix/virtualquery-partial-unmap
fix(libkernel): query non-readable live mappings
2026-10-10 17:42:50 +13:00
Dean 6dbc921d90
Merge pull request #1633 from babyhuey/feat/libkernel-madvise
feat(libkernel): implement madvise
2026-10-10 17:40:10 +13:00
Hannah Lyons d7e52cb295 feat(libkernel): implement madvise 2026-10-09 21:22:44 -04:00
alfalfa-star b7443c4424 perf(agc): share one dma-buf import per 64 MiB of Linux direct memory
Every direct memory allocation had a memfd of its own, so each one the
GPU used became its own udmabuf and its own imported buffer object. An
imported buffer object cannot be always valid in the GPU VM: amdgpu
validates every one of them at every command submission. PPSA14632
(LEGO Horizon Adventures) imports about 7000 allocations, 5800 of them
64 KiB, and each vkQueueSubmit took about 2.3 ms (5 s per 10 s, held
under the GPU mutex). A standalone test shows the cost follows the
count of imported dma-buf buffer objects, not their size or the count
of ordinary allocations.

- libkernel: on Linux all direct memory lives in one sparse memfd of
  DIRECT_MEMORY_SIZE, at file offset = physical address
  (PhysicalBacking keeps the file and its offset; SharedBacking returns
  that offset). A range allocated again is zeroed through a temporary
  mapping, as the old per-allocation memfd was.
- Driver: an import of shared direct memory that lies inside one
  64 MiB chunk of the file binds its own VkBuffer into that chunk's
  memory, one udmabuf import per chunk (HostImport::chunk holds it, so
  it lives until the last buffer bound to it retires). Ranges that
  cross a chunk or do not meet the buffer alignment are imported alone
  as before.

On PPSA14632 vkQueueSubmit drops from about 2400 us to 46 us on
average, the GPU idles 36 ms instead of 172 ms per present, and
presents go from about 40 to 80-97 per 10 s.

Released direct memory stays committed in the file (see
TechnicalDebt.md): a live chunk import pins its pages.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 03:21:53 +02:00
Saranxzi e06c459534 test(libkernel): cover virtual query after partial unmap
Carry over the direct mmap regression from PR #2267. The live-extent implementation is already present in the preceding commits.
2026-10-09 22:11:35 +00:00
MarcZX100 f7de40f9bb fix(libkernel): query non-readable live guest ranges 2026-10-09 22:10:33 +00:00
Praveen Kumar 6f62ec6a6f fix(libkernel): query current guest mapping extents
Use the current registered range after protection splits and partial unmaps, including FIND_NEXT lookups. Add a regression covering extents, protection and surviving contents.

AI-assisted: yes
2026-10-09 22:10:32 +00:00
Scott Schuster bbdb35b195 fix(libc): check a whole range before collecting its writes with reset
WindowsMappings::Collect resets each part of the range as it walks it:
it marks shared views seen and resets private pages' write-watch bits.
When it then met a NOACCESS view or memory that is not committed
private memory, it returned false with the earlier parts already reset,
so their writes were lost to the caller's fallback. With reset, the
whole range is now checked first and nothing is reset when it fails.

AI-assisted (Claude Code).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 10:36:55 -05:00
Andreas 24474e3d28 fix(libkernel): unmap direct memory views when the memory is released
Releasing direct memory left the virtual ranges that mapped it registered,
so sceKernelVirtualQuery reported them as flexible memory at offset 0 and
titles walking a range with FIND_NEXT released physical page 0.

Collect every view of the released pages before freeing them and unmap
them through UnmapRegistered, as shadPS4's MemoryManager::Free does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 14:42:17 +02:00
Scott Schuster a43f1411c4 fix(libc): align guest heap allocations to 32 bytes
Clang's PS4 and PS5 targets set the default new alignment and the suitable
alignment to 32 bytes, so PS5 code may store to a fresh malloc or operator
new result with 32-byte-aligned AVX instructions. The guest heap aligned
blocks to 16 bytes, and the arena's 32-byte classes put every small block
16 bytes past a 32-byte boundary after its header, so such a store faulted.
The minimum alignment is now 32 bytes; the padding still assumes only the
16-byte alignment the raw blocks guarantee.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 09:42:06 -05:00
Dean 8763483811
Merge pull request #1370 from gjonn/fix/kernel-fixed-map-replaces-overlap
fix(libkernel): replace partly overlapped allocations with a fixed mapping
2026-10-08 22:35:53 +13:00
Adrià Franch 53a31b512e test(guest_memory): pin mprotect on an unmapped and a wrapping range
Both return -1/EINVAL: FreeBSD kern_mprotect (sys/vm/vm_mmap.c) maps only
PROTECTION_FAILURE, RESOURCE_SHORTAGE and OUT_OF_BOUNDS to their own errno, so
KERN_INVALID_ADDRESS falls to EINVAL.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-10-08 08:24:12 +02:00
kjhq 35ae8742a9 feat(libkernel): implement POSIX mprotect
mprotect changes the protection of guest memory through the same path
as sceKernelMprotect and reports failures through errno: EINVAL for a
null or overflowing range or a range the allocation tracker rejects,
ENOMEM when allocation fails. A zero length succeeds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 08:24:06 +02:00
Adrià 311c0493bd
Merge pull request #957 from JAB-dev/feat/kernel-flexible-internal-hint
feat(libkernel): accept flag 0x8000 in sceKernelMapNamedFlexibleMemoryInternal
2026-10-08 07:42:56 +02:00
Gjon Gojcevic 5cf013a299 fix(libkernel): replace partly overlapped allocations with a fixed mapping
A fixed mapping without no-overwrite unmaps the registered pieces it overlaps before mapping, as it already did when the range lay inside registered allocations.
2026-10-07 20:31:08 -04:00
Adrià Franch c572650575 test(libkernel): cover a sub-page shared backing offset and a gap after a direct mapping
Co-Authored-By: Claude <noreply@anthropic.com>
2026-10-07 23:16:22 +02:00
alfalfa-star 815d274736 feat(agc): import shared direct memory through udmabuf on Linux
On Linux, direct memory is a memfd mapped MAP_SHARED. amdgpu accepts only
private anonymous memory for a host pointer import (libdrm sets
AMDGPU_GEM_USERPTR_ANONONLY), so every import of direct memory failed with
VK_ERROR_INVALID_EXTERNAL_HANDLE and its ranges were copied.

When a host pointer import fails, the range's memfd slice is turned into a
dma-buf through /dev/udmabuf and imported with VK_EXT_external_memory_dma_buf.
The direct memory backing is sealed against shrinking and growing, as
udmabuf requires. libkernel reports the backing of a guest range through
GuestArenaSharedBacking. Without /dev/udmabuf, or past its size limit, the
range is copied as before. APS5_TRACE_HOST_IMPORT now prints the mappings
behind a failed import on Linux too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 23:09:16 +02:00
Adrià 832247b0a0
Merge pull request #283 from SP4C3B4R-8/fix/fiber-windows-stack
fix(libSceFiber): pin fiber contexts writable under write tracking on Windows
2026-10-07 22:25:25 +02:00
SP4C3B4R-8 78cb1598ce test(libc): pinned shared pages stay writable and are collected as written
Also unpin the previous context when a fiber object is initialised again
without being finalised.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 21:57:20 +02:00
Adrià 3f7fe285a0
Merge pull request #900 from alfalfa-star/feat/libkernel-acp-protection
feat(libkernel): accept the audio coprocessor memory protection bits
2026-10-07 18:26:06 +02:00
Pouare514 c25ff91a65 fix(libkernel): accept GPU coherency prot bits for host mapping 2026-10-07 17:29:28 +02:00
alfalfa-star b567bb8eed feat(libkernel): accept the audio coprocessor memory protection bits
A title maps direct memory with protection 0x3f2, which adds bits
0x100 and 0x200 to the CPU, GPU and AMPR bits, and the mapping threw.
They are the audio coprocessor (ACP) read and write bits, as named in
the KytyPS5 and etaHEN headers. The coprocessor work runs on the host
CPU, so they map to host read and read/write access like the GPU bits.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 07:09:42 +02:00
JAB-dev 2dc53674f5 Merge branch 'main' of https://github.com/boykopovar/AnyPS5 into feat/kernel-flexible-internal-hint 2026-10-07 07:09:08 +02:00
Dean 0c952fe574
Merge pull request #994 from CnRJay/fix/map-no-overwrite-enomem
fix(libkernel): refuse fixed no-overwrite mappings over live memory with ENOMEM
2026-10-07 15:31:12 +13:00
Dean f6f47bf1de
Merge pull request #880 from adriadam10/feat/kernel-mtypeprotect-type
feat(kernel): apply the memory type in sceKernelMtypeprotect
2026-10-07 15:28:25 +13:00
Chad e9c8664d43 fix(libkernel): refuse fixed no-overwrite mappings over live memory with ENOMEM
MAP_FIXED | MAP_NO_OVERWRITE over a range that already holds an
allocation fails with ENOMEM and leaves the mapping in place, as in
fpPS4's TVirtualManager.check_fixed. UFC 6 (PPSA23566) relies on it to
probe for a free address and aborted on the throw.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 17:04:06 -05:00
CnR 7e3dcbc27e
feat(libkernel): implement sceKernelReleaseFlexibleMemory (#952)
* feat(libkernel): implement sceKernelReleaseFlexibleMemory

Unmaps the range through sceKernelMunmap, which returns its pages to
the flexible memory budget, as fpPS4 and prosper do.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(libkernel): keep the flexible memory release check apart from the batch map checks

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 10:20:07 +13:00