Commit Graph
219 Commits
Author SHA1 Message Date
Evgeniy KazarinandClaude Opus 5.5 69f843c910 perf(libkernel): reuse released direct-memory sections that never held data
The title allocates, maps and releases about 48 direct-memory blocks per frame.
Each allocation created a pagefile-backed section and each release closed it.
A released section whose pages were never stored to (resident pages keep their
data in private memory and saved buffers, not in the section) is still blank,
so up to 256 such sections of at most 16 MiB, 256 MiB in total, are kept by
size and handed to the next allocation of that size instead of a new
CreateFileMappingW. APS5_NO_DIRECT_MEMORY_SECTION_POOL keeps the old behaviour.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-21 10:32:44 +08:00
Dean e7ce7102ab Merge pull request #7468 from Dripl1x/codex/direct-memory-alias-index
perf(libkernel): find direct-memory aliases through the physical index
2026-10-21 13:57:13 +13:00
Dean 49fd13a741 Merge pull request #7958 from Lindamea1/fix/libkernel-available-direct-memory-enomem
fix(libkernel): return ENOMEM from sceKernelAvailableDirectMemorySize when the range has no free block
2026-10-20 17:40:24 +13:00
Adrià a700809679 Merge pull request #7448 from Tamalero/fix/libkernel-release-direct-memory-order
fix(libkernel): unmap released direct memory views before freeing the physical range
2026-10-19 23:27:29 +02:00
Lindamea1andClaude Opus 5.5 179316c452 fix(libkernel): return ENOMEM from sceKernelAvailableDirectMemorySize when the range has no free block
A search range with no free direct memory returned EAGAIN (0x80020023).
A PS5 returns ENOMEM (0x8002000c) for an occupied range, as the
PS5_RetroArch author's console capture records, and PS5_PayloadSDK's
ps5_memory_query, which RetroArch uses for its memory status, gives up
on any other error.

LargestFree now returns the error: ENOMEM when no block is found, and
EAGAIN as before for a negative start, an end at or below the start and
an alignment that overflows when rounded to a page. guest_memory expects
ENOMEM for an occupied range and EAGAIN for those arguments. The
unmeasured argument handling is recorded in the libkernel debt file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 16:24:55 +01:00
Adrià ebf1a90270 Merge pull request #6825 from hugodearaujo/fix/report-implied-read-protection
fix(libkernel): report the read bit of every write bit in a mapping's protection
2026-10-19 14:04:45 +02:00
Adrià 3691f6ffc2 Merge pull request #6826 from hugodearaujo/fix/page-table-stats-console
fix(libkernel): count page-table entries the way the console does
2026-10-19 13:24:22 +02:00
Adrià Franch 86bf06c87b fix(libkernel): report the read bit for a replaced guest thread stack too 2026-10-19 12:06:01 +02:00
hugodearaujo 514b6849ec fix(libkernel): report the read bit of every write bit in a mapping's protection
A PS5 (firmware 13.60) reports CPU write 0x2 with CPU read 0x1, 0x80 with 0x40 and 0x200 with 0x100 after a map, sceKernelMprotect, sceKernelMtypeprotect or POSIX mprotect; GPU write 0x20 keeps GPU read 0x10 clear. sceKernelVirtualQuery and sceKernelQueryMemoryProtection now report the protection that way.
2026-10-19 12:05:39 +02:00
Adrià 22410061d5 Merge pull request #6542 from wachi35676/fix/libkernel-windows-shared-file-mmap
feat(libkernel): map regular files with mmap on Windows
2026-10-19 08:37:27 +02:00
TamaleroandClaude Opus 5.5 b574f2a51f fix(libkernel): claim a released direct memory range before unmapping its views
The checked release tested that the range was allocated, released the pool lock and then
unmapped a snapshot of its views. Two concurrent checked releases of one range both passed the
test and took the same snapshot; the second then unmapped views the first had already removed
and terminated on "unmap address is not registered" instead of returning ENOENT.

A release now claims its pages in the pool under the pool lock before it looks at the views,
and frees them, dropping the claim, after the unmap. A checked release of pages another release
has claimed returns ENOENT as if they were already free; a plain release waits for that release
to finish. The pages stay allocated while claimed, so they still cannot be reallocated during
the unmap's lease wait.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-18 22:40:10 -07:00
Doğukan Atlıhan 1ad6c746b8 fix(libkernel): replace anonymous Windows stack mappings 2026-10-19 06:42:30 +02:00
Adrià decb14c505 Merge pull request #6491 from Ondra97/codex/fix-flexible-fixed-remap-budget
fix(libkernel): account for fixed flexible memory replacements
2026-10-19 04:30:17 +02:00
Adrià bebc294607 Merge pull request #6443 from EagleBearerYS/fix/libkernel-host-stack-guard-page
fix(libkernel): keep a protected host stack's guard page, and protect the Linux main stack
2026-10-19 02:41:09 +02:00
Muhammad Wasif Ali WasifandClaude Opus 5.5 ddb774ad26 feat(libkernel): map regular files with mmap on Windows
A shared mapping is a view of a file section created on the descriptor's
handle, read-write when the descriptor is, otherwise read-only. A private
mapping is a copy of the file read at mmap, as POSIX allows. Anonymous
shm_open objects and JIT shared memory descriptors are temporary files on
Windows, so mmap now maps them too (it failed with EOPNOTSUPP before).
mprotect of a mapped view above the view's protection fails with EACCES
before any bookkeeping changes. The cases Windows cannot represent are
listed in the libkernel debt.

Dusklight (PPSA99305) uses SQLite in WAL mode, which maps its -shm file.
On Windows main f0634088b every start logged "Failed to start
transaction: disk I/O error" for its Dawn and pipeline caches (3 of 3
runs); with this change the caches initialise and their WAL files fill
(4 of 4 runs). The title reaches its menu either way.

A shared mapping may end inside the 16 KiB page that holds the end of
the file, as POSIX mmap allows: the view ends with the file's last 4 KiB
host page, which Windows zero-fills past the end, the rest of the guest
page is private zero pages, and the file never grows. A mapping that
reaches a whole page past the end still fails with EOPNOTSUPP.

PS5LM Plus (PPSA99581) loads its model with llama.cpp, which maps the
whole GGUF file shared and read-only. With a 491,400,032-byte model,
Windows main 5de7a46e0 and this change without the last page logged
"llama_model_load: error loading model: mmap failed: Operation not
supported" (3 of 3 runs); now the model loads and the completion of
"The capital of France is" starts with " Paris" (3 of 3 runs).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 05:02:25 +05:00
Adrià 57a9970094 Merge pull request #6311 from Ondra97/codex/fix-posix-flexible-memory-release
fix(libkernel): release flexible memory budget through munmap
2026-10-19 00:06:26 +02:00
Adrià 1288433e8e Merge pull request #6184 from MarcZX10/codex/cache-inaccessible-guest-pages-6183
fix(agc): cache inaccessible guest pages
2026-10-18 22:37:09 +02:00
Ondra97 393506c161 fix(libkernel): account for fixed flexible memory replacements 2026-10-18 11:07:14 +02:00
TamaleroandClaude Opus 5.5 268e96df8b fix(libkernel): unmap released direct memory views before freeing the physical range
sceKernelReleaseDirectMemory and sceKernelCheckedReleaseDirectMemory returned the range to the
allocator before unmapping its views. The unmap waits for GPU leases on the views, and during
that wait another allocation could take the freed pages and map them while the old view was
still mapped and host-imported, so GPU writes through it landed in the new owner's memory.
Both calls now unmap the views first; the checked call still reports ENOENT for an unallocated
range before it touches any view.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-17 23:51:55 -07:00
Dean 1b0c7cfa02 Merge pull request #7112 from Pouare514/fix/flexible-release-scope
fix(libkernel): scope flexible release to tracked ranges with release-then-record
2026-10-18 19:22:54 +13:00
Evgeniy Kazarin d2ca227e8f perf(libkernel): find direct-memory aliases through the physical index 2026-10-18 05:55:38 +03:00
MarcZX100 a352941e77 fix(libkernel): release flexible accounting after fixed mmap 2026-10-17 20:22:14 +00:00
Pouare514 82dff58494 fix(libkernel): scope flexible release to tracked ranges with release-then-record 2026-10-17 14:03:19 +02:00
milescovielloandClaude Opus 5.5 cbf006cb4b fix(libkernel): return 0 from sceKernelMprotect and sceKernelMtypeprotect for a zero length
mprotect already returns 0 for a zero length. The sceKernel entry
points passed it to DoMprotect, which throws for an empty range.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-17 06:26:57 +00:00
Yunus Shaikh 2ac72d48bc Merge main into fix/libkernel-host-stack-guard-page 2026-10-16 22:29:52 -07:00
Adrià 5b70a50680 Merge pull request #5955 from fma-rc/fix/libkernel-linux-guest-module-protect
fix(libkernel): register a relinked guest module's image when Linux protects its pages
2026-10-16 23:29:46 +02:00
Alexej Szpevák d97d6efb1b fix(libkernel): refuse descriptor calls on app0.zar files
flock, fstatfs, ftruncate, fsync, fdatasync, getdirentries, fchmod, futimes and mmap on an archived descriptor acted on its null device and succeeded or failed with an unrelated errno. They now throw NotImplemented, each covered by the kernel archive test; mmap is noexcept, so its refusal is checked through the terminate handler.
2026-10-16 20:03:32 +02:00
Tamalero 13a3902954 fix(agc): cache inaccessible guest pages
Remember inaccessible arena pages and Linux mapping gaps so repeated access checks do not rescan host mappings. Invalidate the cache when guest stack protections change.
2026-10-16 17:44:23 +00:00
hugodearaujo 5106b98df6 fix(libkernel): count page-table entries the way the console does
A PS5 (firmware 13.60) reports 8192 entries per side. It takes one cpu entry for each 2 MiB granule holding a committed mapping of any protection, and one gpu entry for each 1 GiB region holding a GPU-accessible mapping plus one for each granule of such a mapping that is not a whole granule of direct memory at a 2 MiB-aligned physical address. Shared granules and regions count once. sceKernelGetPageTableStats reported 2048 entries and charged each range per granule, CPU-accessible ones only to the cpu side.
2026-10-16 09:49:46 -03:00
Yunus Shaikh 9a48a9605c fix(libkernel): map a protected main stack up to its top on Linux
A protect below the main thread's stack mapping mapped only the
protected pages. Linux keeps a growing stack one guard gap (1 MiB by
default) above a readable mapping, so after a read-only protect, or a
protect later restored to read/write, at the bottom of the reported
stack the stack could no longer grow into the megabyte above it and a
deep enough call faulted where the console has stack pages. The pages
between the protect and the top of the stack are now mapped as well,
so the stack never has to grow past them.
2026-10-16 07:28:23 +00:00
Yunus Shaikh d5c12169fc fix(libkernel): map a host stack's unmapped pages in runs on Linux
MapUnmappedPages probed every 4 KiB page of the protected range with
msync before mapping it. Under an unlimited stack limit the main
thread's reported stack reaches down to the heap, tens of TiB, so a
guest protect over the whole stack ran for hours. Mapped and unmapped
runs are now found by halving the remaining range, and the unmapped
ones are mapped without reserving swap, as the stack itself grows.
2026-10-16 07:24:41 +00:00
Adrià 02a1b772ed Merge pull request #5770 from simonbronk/fix/kernel-mprotect-undefined-bits
fix(libkernel): drop undefined protection bits in sceKernelMprotect
2026-10-15 17:53:02 +02:00
Yunus Shaikh f29ee47366 refactor(libkernel): record a host stack protect's guarded part on its own line
The stack branch now records the reserve and guard below a protected
range with a separate RecordProtection call, and keeps the call that
records the protected range itself as main has it. The ranges recorded
are the same: RecordProtection replaces what it covers, so the two calls
together record the whole requested range, as before. #6184 adds a line
next to that call, and the two no longer touch.
2026-10-15 08:52:21 -07:00
Yunus Shaikh 5796ddfcfd fix(libkernel): commit a protect at the bottom of a host stack's reserve
HotSpot makes its stack guard zones with an RW protect of the lowest
pages of the thread's stack, then PROT_NONE, as it commits them on
native Windows (os::pd_create_stack_guard_pages, then os::guard_memory).
Below a host stack's guard page such a protect was only recorded, so the
pages stayed reserved: they could not be written after the RW protect,
and the zones never held.

A protect of reserved pages that starts at the bottom of the reserve and
stops short of the guard page now commits and protects them. The guard
page and the reserve above the range stay as they are, so the stack still
grows through the guard down to it, and the stack limit in the TEB still
follows the committed stack.

Windows moves the guard down by committing the pages below it as
read/write guard pages, committed or not. The zones therefore hold until
the stack is within one guard region of them (three pages on GitHub's
runners), and the pages the guard reaches then come in read/write. The
test checks both, and the debt entry says so.
2026-10-15 08:10:42 -07:00
Yunus Shaikh 3724c577d8 fix(libkernel): keep a host stack's guard page and reserve when protecting it on Windows
Committing the reserved pages of a host thread's stack before protecting
it, as the stack branch did for a range the arena doesn't own, also
cleared the guard page's PAGE_GUARD. The thread was left with no guard
and no reserve: an overflow ran into whatever lay below the stack instead
of raising a stack overflow, and the stack limit in the thread's TEB,
which Windows moves only when the guard page is hit, stopped following
the stack. A range that began inside the reserve also left the reserved
pages below it with no guard above them, so the stack could no longer
grow into them.

The stack branch now leaves the guard page and the reserved pages below
it as they are, and commits and protects only what lies above them;
reserved pages above the guard are still committed first. The protection
is recorded for the whole range, and a range of only reserved and guard
pages changes nothing on the host. The test protects the main thread's
whole stack and a reserved thread's whole stack and upper part, then
checks that the guard page is still there and that the stack grows
through it, also after a PROT_NONE protect of its lowest page. The debt
file records that those pages come in read/write as the stack grows.
2026-10-15 08:10:42 -07:00
Yunus Shaikh b5909a5ee0 fix(libkernel): map the unmapped pages of a host stack before protecting it on Linux
On Linux the guest's main runs on the host main thread's stack, which
the kernel maps only as it grows. sceKernelIsStack reports the whole
range that pthread_getattr_np gives, which is sized by RLIMIT_STACK and
is mostly not mapped yet. A protection of that range failed with ENOMEM,
so mprotect returned EINVAL and sceKernelMprotect stopped the title.

When the stack branch's mprotect fails with ENOMEM, the range's unmapped
pages are now mapped read-write at their own addresses
(MAP_FIXED_NOREPLACE) and the protection is applied again. This matches
how the reserved pages of a host stack are committed on Windows.
2026-10-15 08:10:42 -07:00
Adrià d8862a63ba Merge pull request #5703 from EagleBearerYS/fix/libkernel-protect-reserved-host-stack
fix(libkernel): commit a reserved host stack before protecting it
2026-10-15 17:04:17 +02:00
Ondra97 0cbec9f8f4 fix(libkernel): release flexible memory budget through munmap 2026-10-15 12:50:29 +02:00
al3w0f205 e03e6f7a4e feat(libkernel): add munlockall export
Follows mlockall (#4386) for XPSemu (PPSA97358, QEMU os-posix.c), which
imports munlockall (NpLBpgVV7PU) from libkernel.prx. Predicted stop:

  FAIL: unresolved ELF import NpLBpgVV7PU from libkernel.prx

The NID is computed with tools/nid_names.py and matches aerolib.

Follows FreeBSD 11.4 sys/vm/vm_mmap.c sys_munlockall
(8c639945a44588f6cfdddfb057bb5aad88735da0): checks PRIV_VM_MUNLOCK,
clears wire state and returns 0. To match, munlockall unlocks ranges
locked by mlock (through VirtualUnlock on Windows, tolerating
ERROR_NOT_LOCKED, and munlockall on Linux).
2026-10-14 18:07:34 -05:00
simonbronkandClaude Opus 5.5 3da081aaad fix(libkernel): drop undefined protection bits in sceKernelMprotect
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 21:51:52 +02:00
Adrià 9811e9d4b7 Merge pull request #5218 from YossiYad/fix/guest-arena-mapping-hint
fix(libkernel): fall back below a mapping hint with no room above it
2026-10-14 21:05:23 +02:00
Adrià 066442c961 Merge pull request #5143 from xXPBSXx/fix/mprotect-unmapped-einval-v2
fix(libkernel): fail sceKernelMprotect over an unmapped range with EINVAL instead of aborting
2026-10-14 21:05:05 +02:00
Adrià 587b63d2a3 Merge pull request #5073 from krishalivaibhav/codex/memory-service-lifetime
fix(memory): preserve process-owned services during exit
2026-10-14 20:33:53 +02:00
fma-rc c1783f9d64 fix(libkernel): register a relinked guest module's image when Linux protects its pages 2026-10-14 18:24:45 +02:00
Yossi Yadgar a016b4f2bc fix(libkernel): fall back below a mapping hint with no room above it
A mapping without MAP_FIXED treated its address as the lowest place it
could go, and a hint at or above the end of the guest arena threw on the
host. FreeBSD 11.4's vm_mmap_object places such mappings with
vm_map_find_min, which takes the address as a hint only: it tries at or
above it, then again from its minimum address.

GuestArenaAllocateFromHint does the same in the arena: at or above the
hint when it lies below the arena's end and a range fits there, otherwise
from the arena's start. MapPlaced and the Windows mmap_aligned use it for
hinted mappings; GuestArenaAllocateAtOrAbove keeps its meaning for the
callers that need a range above an address.

PS5CEMU-HAR (PPSA99360) reserves its JIT range with
sceKernelReserveVirtualRange and a hint near its own code, a host address
above the arena here, and aborted with "mapping address hint is above the
guest address space arena" when launching a 3DS title.
2026-10-14 17:41:31 +03:00
Adrià 5adefa6f89 Merge pull request #5643 from Amirtheshwaran/feat/libkernel-mincore
feat(libkernel): implement mincore
2026-10-14 16:25:54 +02:00
Adrià 1b55ee4ab3 Merge pull request #5610 from Dripl1x/codex/feat-libkernel-fixed-anonymous-mmap
feat(libkernel): support fixed anonymous mmap
2026-10-14 14:52:53 +02:00
Adrià a628765e58 Merge pull request #5053 from YossiYad/feat/libkernel-mmap-regular-files
feat(libkernel): map regular files with mmap on Linux
2026-10-14 14:41:32 +02:00
Adrià FranchandClaude 2854d51352 fix(libkernel): follow kern_mincore order and report reserved pages as not resident
Co-Authored-By: Claude <noreply@anthropic.com>
2026-10-14 14:40:12 +02:00
Amirtheshwaran Navaneethakrishnan 3699eff904 fix(libkernel): round mincore address down as FreeBSD does
Follow FreeBSD 11's sys_mincore by rounding unaligned address down to page boundary rather than returning EINVAL. Update guest memory test and document unmodeled residency/referenced/modified bits in libkernel technical debt.
2026-10-14 14:40:08 +02:00