Commit Graph
160 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 b13183e59e Merge pull request #7473 from Dripl1x/codex/skip-clean-resident-write-watch
perf(libc): skip write-watch queries for proven clean resident ranges
2026-10-21 14:55:09 +13:00
Dean c82867c77c Merge pull request #7472 from Dripl1x/codex/reuse-unreferenced-saved-memory
perf(libc): reuse saved direct-memory buffers when unreferenced
2026-10-21 13:25:04 +13:00
Evgeniy Kazarin 7898b5bd3a fix(libc): avoid repeated resident write faults 2026-10-21 03:55:51 +08:00
Evgeniy Kazarin a627328cfa perf(libc): skip write-watch queries for proven clean resident ranges 2026-10-21 03:53:50 +08:00
Evgeniy Kazarin 9d5f8326bf perf(libc): collect resident writes directly into the output buffer 2026-10-21 03:52:09 +08:00
Seif Allah Karoui be27ffa12c test(libkernel): harden signed-byte memory protection coverage 2026-10-20 12:06:06 +01: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
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
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
TamaleroandClaude Opus 5.5 9f5efd4f97 test(libkernel): pass the release under test with its declared calling convention
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-18 00:46:13 -07: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 1d9bf1e029 perf(libc): reuse saved direct-memory buffers when unreferenced 2026-10-18 06:10:30 +03:00
Dean 5a86770667 Merge pull request #7242 from MarcZX100/codex/fix-mmap-flexible-replacement-7120
fix(libkernel): release flexible accounting after fixed mmap
2026-10-18 12:08:50 +13:00
Dean e5961b2b35 Merge pull request #6264 from MarcZX100/codex/host-write-map-alias-6260
fix(libc): defer resident alias demotion during host writes
2026-10-18 10:32:22 +13: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
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
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
Romans Pokrovskis 50748f79f2 test(libc): retain the open-host-write alias regression
Retain the regression originally supplied by Pipodi in closed PR5851 (22091410a), adapted here through e1c6af65b. Current-main-equivalent libc/kernel source returns an alias during the open HostWrite and fails this assertion five times. This test is a reproduction, not a new implementation or an upstream PR.

(cherry picked from commit 312170cd0c)
2026-10-15 14:52:30 +00:00
JJenkx ad78fbcdf7 fix(libkernel): stabilize virtual-query layout across hosts 2026-10-15 08:16:22 -04: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
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 cf1c80d8ce test(libkernel): cover reservation hints with no room above them
guest_memory reserves with a hint above the guest arena and with a hint
on the arena's last page for two pages, and checks that both land inside
the arena, the second below its hint.
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à 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
Amirtheshwaran Navaneethakrishnan 7781eda113 feat(libkernel): implement mincore
Implement mincore (+hFjyohmOU0) in libkernel following FreeBSD 11's sys_mincore (sys/vm/vm_mmap.c). Validate page alignment, return EFAULT for null vector, check range bounds and residency via sceKernelVirtualQuery, and report MINCORE_INCORE (1) for resident pages. Required by Java 25 LAN / DNS (PPSA99869).
2026-10-14 14:40:08 +02:00
Yossi Yadgar e53268d9e9 test(libkernel): cover shared and private mappings of a regular file 2026-10-14 09:04:47 +03:00
Dean ee50655414 Merge pull request #5109 from pushkarreddyy/feat/libkernel-foreign-module-protect
feat(libkernel): support sceKernelMprotect on foreign guest module images on Windows
2026-10-14 17:26:42 +13:00
Dean Galvin 70fbb186c0 test(libkernel): check the libkernel.prx rejection on its own image
&sceKernelMprotect in the test executable is its import thunk, so the
call protected a page of the executable's own code read-only and the
next call through it crashed. Use the libkernel.prx image instead.
2026-10-14 16:24:33 +13:00
Dean Galvin 7f2c2a51c3 test(libkernel): give the module image protection test its own .elf0 module
The shared guest_memory_module gained a .elf0 section, so on Windows its
data was no longer counted against flexible memory and
CheckGuestModuleUsesFlexibleMemory failed. Build the .elf0 variant as a
separate guest_memory_elf0_module, keep the shared module as on main, and
check that a .guest.prx module without .elf0 is rejected.
2026-10-14 16:11:16 +13:00
pushkarreddyy 516b06f945 feat(libkernel): support sceKernelMprotect on foreign guest module images on Windows 2026-10-14 08:19:25 +05:30
Evgeniy Kazarin 6215101d28 feat(libkernel): support fixed anonymous mmap
Map fixed private anonymous ranges through the existing guest arena. Preserve occupied ranges for MAP_EXCL and use FreeBSD 11.4 errors.

Cover replacement, exclusivity, range validation, rounding, and memory access.
2026-10-14 03:48:36 +03:00
thibautandClaude Opus 5.5 f32a9407e4 test(libc): require resident direct memory in the resident run tests
CheckBreakingAResidentRunKeepsConcurrentAccesses and
CheckRemappedDirectMemoryIsChargedOnce passed on main, where a singly
mapped direct memory page is a shared view and no resident run exists.
Both now require their first mapping to be private committed memory, so
on main they fail there instead of passing without exercising a run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01V1W4jdNSKTV7xwuS6xQPJV
2026-10-14 02:39:10 +02:00
DotDebianandClaude Opus 5.5 d9852df2a2 fix(libc): track writes to singly mapped direct memory without faults on Windows
A direct memory page mapped at one guest address now lives in private
MEM_WRITE_WATCH memory at that address, and its writes are collected with
GetWriteWatch like flexible memory: no fault is raised. A page mapped at a
second address moves its whole resident run into the section, behind a
read-only fence while it is copied, and every address becomes a view of it,
tracked by write protection. Unmapped resident pages that are not blank are
kept in a private reservation until they are mapped again, not in the
section.

Ported onto main's 4 KiB subpage tracking of shared views: resident runs
keep a per-page collected bit and enter the fresh ranges, so Fresh and
Armed collections select them like shared views; a demoted run carries its
collected bits into the views and reports every page once. MapAlias throws
through refuse() when a resident run extends outside the aliased range, and
the caller keeps main's fallback.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01V1W4jdNSKTV7xwuS6xQPJV
2026-10-14 02:39:10 +02:00
NikoandClaude Opus 5.5 44767a7b97 perf(libc): size each write-watch query to the region it covers
WindowsMappings::Collect handed GetWriteWatch the whole remaining output
capacity for every private region it walked. The call's cost follows that
capacity, not the size of the range: on Windows 11 (26200) a query of one
16 KiB region took 0.35 us with room for 4 entries and 6.5 us with room
for the 65536 the write tracker passes. Flexible memory is committed in
16 KiB allocations, one region each, so a resetting walk over 33 MiB of it
took 14.7 ms, 5.8 times a memcmp of the same bytes.

Each query now gets room for the pages its region spans, which is the most
it can report, so every walk returns the same pages; the 33 MiB walk takes
1.23 ms. guest_memory checks that a walk over eight 16 KiB allocations
reports every stored page once, in order, with room for 32, 5 and 1 pages.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-13 13:40:33 +02:00
Mouad-Scriptz-0andClaude Fable 5.1 df3d027ebe perf(libkernel): find guest allocations and direct mappings by address
sceKernelVirtualQuery, sceKernelMunmap, sceKernelReleaseDirectMemory and the
reservation paths walked a copy of the whole allocation registry or of every
direct mapping under one lock on each call. The registry now answers lookups
from its ordered map, applies range replacements as patches instead of
rebuilding the map, and direct mappings carry an index by physical offset so a
release finds only the mappings it covers. Minecraft (PPSA17221) holds about
7,000 direct mappings and spent its world generation in these walks.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-13 23:11:32 +13:00
Dean 90285be0af Merge pull request #4367 from MarcZX100/codex/carry-windows-alias-4260
fix(libc): alias non-contiguous shared guest views on Windows
2026-10-13 22:00:55 +13:00
Adrià a373241de4 Merge pull request #4582 from jagrat04/feat/libkernel-madvise-madv-protect-pr
feat(libkernel): accept MADV_PROTECT advisory hint in madvise
2026-10-13 09:44:18 +02:00
Angel21PC 2d1cc82bce fix(libc): alias non-contiguous shared guest views on Windows
On Windows, a host import of shared guest memory goes through a
read-write alias from WindowsMappings::MapAlias. The alias was a single
MapViewOfFile3 of one section, so MapAlias threw when the views of the
range were not one contiguous run of that section. One
sceKernelMapDirectMemory over two adjacent direct memory allocations
gives such a range, as each allocation has its own section, and so do
pages remapped in place to another physical order.

Such ranges are now aliased run by run: a placeholder reservation is
split at each run of consecutive pages of one section, and each run
maps that section at its offset. UnmapAlias unmaps every run and forgets
the alias even when one unmap fails. A contiguous range keeps the single
view, and a range with a page that is not a shared view is still
refused.

guest_memory maps two adjacent direct memory allocations of two pages
and one page with one call, and two pages in reverse physical order.
It reads and writes every page through the alias. The first test also
checks that the alias has one view per run and that unmapping it frees
its pages. A range whose second page is flexible memory is still
refused.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit fc6868d8b6)
2026-10-13 07:31:17 +00:00
Dean cc55e1410a Merge pull request #4661 from fdfawef324/fix/libc-shared-write-pages
fix(libc): track writes to shared guest memory per 4 KiB page on Windows
2026-10-13 17:46:44 +13:00
jagrat Singh 59bd0c1782 feat(libkernel): accept MADV_PROTECT advisory hint in madvise 2026-10-13 04:21:59 +05:30
Yunus Shaikh 56d35172f5 fix(libkernel): accept flag 0x8000 in sceKernelMapFlexibleMemory
The libc.prx heap that Animal Well (PPSA22520 01.000.011, SDK 9.04)
bundles maps each arena with sceKernelMapFlexibleMemory(&addr, len, 3,
0x8000). It threw "Unsupported memory mapping flags 0x8000" before the
first frame. The flag is ignored as sceKernelMapNamedFlexibleMemoryInternal
already does; other unknown flags still throw.
2026-10-12 15:38:48 -07:00
fdfawef324andClaude Opus 5.5 1eb13fdf02 fix(libc): track writes to shared guest memory per 4 KiB page on Windows
Writes into shared (direct) guest memory on Windows are caught by write
protecting each 16 KiB view and taking the fault. The fault reopened
and invalidated the whole view, so a collect reported all four 4 KiB
pages of a view whenever any byte of it was written, including pages
nobody wrote. A GPU surface that ends or starts inside such a view
then had its edge pages taken for CPU writes whenever the title wrote
the memory next to it, and the storage write-back kept the guest's
stale bytes there instead of the GPU's results.

Each view now keeps a write generation, a seen generation and the
write protection per 4 KiB page. A fault invalidates and reopens only
its page, a host write only the pages it covers, and a collect reports
and re-arms only the pages it covers. VirtualProtect is called once
per contiguous run of pages whose protection changes, so arming or
pinning a view takes one call when those pages are contiguous and two
when they are not. A guest store that fills a whole view takes up to
four faults instead of one.

A collect does not settle a page that it cannot write protect because
a host write is open on one of the page's views; it reports the page
again until a collect can arm it. main settled such a page and relied
on the end of the host write invalidating the whole view, so a guest
store into it during the host write was reported only after the host
write ended. With only the written pages invalidated there, the store
would not be reported at all.

A collect that runs out of room partway through a view now settles the
pages it already reported there and nothing after them; before, it
settled none of them and reported them again at the next collect.

CheckSharedWritePages writes one byte into the second 4 KiB page of a
view and requires a collect of the view's last two pages to report
nothing; main reports both. It also checks a store across two pages
and a file read into one page. CheckSharedWritesDuringHostWrite stores
into a fault-opened page of a view after a collect while a host write
is open on another page of it, and requires every collect of the view
and of its alias to report that page until the host write has ended.
agc_shared_write_partial_collect resets the two interior pages of a
view after writes to all four and requires the outer two to stay
pending. The existing shared-memory checks in guest_memory,
agc_shared_write_partial_collect and guest_socket_host_write (the
recvfrom sender outputs) now expect the pages written instead of the
whole view.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 18:03:53 -04:00
babyhuey ff56dadc0a feat(libkernel): export sceKernelInternalMemoryGetAvailableSize 2026-10-12 17:18:53 -04:00