feat(libkernel): export sceKernelAllocateDirectMemoryForApp and sceKernelProtectDirectMemoryForPID #12826
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12826"
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 Nixitov on 2026-10-29T11:42:44Z.
What
Export
sceKernelAllocateDirectMemoryForAppandsceKernelProtectDirectMemoryForPIDfromlibkernel, which is where titles import them from. Both were declared only inlibkernel_sys, as throwing discovery stubs, so the loader stopped before the title started.Sources. PS5_MihawkOS (
mihawk-99/PS5_MihawkOS,PPSA99170) declares both innative/pw_dmem_share.h, at commit8a9c117:Its
docs/PLAN.md, section "Direct memory shared between processes", reports readinglibkernel_sys.sprx10.01: the protect call is one ioctl0xc018800fon the dmem device carrying{offset, length, pid, prot}, the pid variant refuses pid 0, and a console run (dmem-share-2026-10-12-05; with the title,dmem-title-share-2026-10-12-01) hadsceKernelProtectDirectMemoryForPID(offset, 64 MiB, 0x33, pidB)return 0, after which a second process could map the range.fNoviuIwykcissceKernelAllocateDirectMemoryForApp; the header gives the six arguments ofsceKernelAllocateDirectMemory, andPLAN.mdshows the same allocation reached through both calls, so it forwards tosceKernelAllocateDirectMemoryand shares that validation and allocator. The forward is assumed from the shared signature:ForApp's own argument checks and error codes are not measured, and a guest has one direct memory range, so an app-specific range restriction has nothing separate to observe.cBp0Ad-24LAissceKernelProtectDirectMemoryForPID; it keeps the known prototype(int64_t offset, size_t len, int prot, int pid)and throws: the three sources are a third-party repository, a static reading of one firmware and a payload-only measurement, the error for pid 0 or a pid that does not exist is unknown, and a guest runs as the only process, so there is no second process to grant a range to.Both entries are in
docs/dev/debt/libkernel.md.The copies in
libkernel_sysare removed because the same export may live in one library only; the loader finds both NIDs inlibkernel.With the change the title loads with no unresolved import and reaches its own
/app0/assets/lookup.Tested
Windows 11, MinGW GCC 15.2.0, current
mainba5b5330b, with PS5_MihawkOS v4.0.0-alpha-4 (PPSA99170,mihawk-99/PS5_MihawkOS). Onmainthe title stops with:With this PR the relinked title loads with 0 unresolved imports and reaches its own
/app0/assets/lookup. New testguest_direct_memory_for_app(core/libs/tests/GuestDirectMemoryForApp.cpp) covers theEINVALcases (zero and unaligned length, null output, end before start) and an aligned allocation inside the direct memory range that is released afterwards;ctest --test-dir build -R guest_direct_memory_for_apppasses, andpython tools/check_conventions.py --base origin/mainreports all checks passed.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 XairRrR on 2026-10-29T11:52:23Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
The change merges cleanly, builds, exports both NIDs from
libkerneland its new test passes. It needs one documentation correction: the debt entry says no prototype or measurement exists forsceKernelProtectDirectMemoryForPID, but the public source of the title this PR unblocks has both. Neither export's signature has a cited source.What I ran
Head
bf1ae463892c7e3bde4bc81c65efafb95875f84b, merged with--no-ffontomainba5b5330b37724bca4b99f30faab504653635a04in a clean worktree. Arch Linux 7.2.11-zen, GCC 16.2.1, Intel Core i9-14900KF, NVIDIA GeForce RTX 4070 Ti SUPER (driver 615.78.08); Release, Ninja,BUILD_TESTING=ON.Results
maingit diff --checkcheck_conventions.pylibs, new test)guest_direct_memory_for_appguest_memory,guest_memory_lifetime,guest_memory_pool_stats,guest_kernel_sys,guest_kernel_sys_constantsfNoviuIwykcandcBp0Ad-24LAeach appear once inlibkernel.prxand not inlibkernel_sys.prxctest
Negative control
I restored the five production files from
main(bothApi.hpp,Export.cpp,Unimplemented.cpp,Export.Discovery.cpp) and kept the test and its CMake entry. The test no longer compiles, so it does not pass without the change. This is the compiler message, translated from the host's French locale:After that I restored the merged files, rebuilt, and the test passed again.
Findings
Blocking
The debt entry says something that a public source contradicts. docs/dev/debt/libkernel.md:327 says that "no prototype, argument order or measured result was found" for
sceKernelProtectDirectMemoryForPID. The public repository of the title this PR unblocks, PS5_MihawkOS, has all three:int sceKernelProtectDirectMemoryForPID(int64_t offset, size_t length, int prot, int pid)andint sceKernelAllocateDirectMemoryForApp(int64_t, int64_t, size_t, size_t, int, int64_t*).libkernel_sys.sprx10.01. It says the call is one ioctl0xc018800fwith{offset, length, pid, prot}and that the PID variant refuses pid 0. It also records a console run in whichsceKernelProtectDirectMemoryForPID(offset, 64 MiB, 0x33, pidB)returned 0 and a second process could then map the range.The reverse-engineering guide ranks a console measurement and the library's own code as the strongest sources. CONTRIBUTING.md:78 asks debt entries to record known signatures. A debt entry saying no such source exists will send the next contributor in the wrong direction.
Suggested fix: rewrite the entry to give the known signature with these sources and their limits (static reading of firmware 10.01, one console run, a third-party repository). Then say what still throws and why. For example, it is unknown which error is returned for pid 0 or for a pid that does not exist, and a single guest process has no second process to grant. Throwing is still reasonable with that much known.
Neither export cites the source of its signature or behaviour. CONTRIBUTING.md:9 and the guide's Citing it section require the PR and the debt entry to say where a signature and behaviour come from. The PR states that
sceKernelAllocateDirectMemoryForApphas the six arguments ofsceKernelAllocateDirectMemoryand the same validation (core/libs/prx/libkernel/DirectMemory/Export.cpp:239-241), but gives no source. The header above supports the signature, and the PLAN.md section shows the compositor using both calls for the same allocation. That supports forwarding, but it is not a measurement ofForApp's own validation and error codes. Suggested fix: cite the source in the PR body. Add a debt entry saying the forward is assumed from the shared signature and thatForApp's own argument checks and errors are unmeasured.Non-blocking
sceKernelProtectDirectMemoryForPID(void). With the prototype known, declaring(int64_t offset, size_t len, int prot, int pid)would document it and save a later signature change. The NID comes from the name, so this does not affect resolution.GuestDirectMemoryForApp.cpponly checks that the forward reachessceKernelAllocateDirectMemory's checks. That is enough for a forward. AnyForApp-specific behaviour added later should get its own case.Not verified
/app0/assets/. The title's header has a comment saying "Only payloads import these calls" (pw_dmem_share.h:12). I could not check whether the v4.0.0-alpha-4 eboot imports them itself, as the PR log shows.WIN32PATHbranch of the new test entry was not exercised.To get this to a good state
sceKernelProtectDirectMemoryForPIDdebt entry to give the known prototype and sources, and say what is still unknown.sceKernelAllocateDirectMemoryForApp's signature in the PR body, and add a debt entry noting that its own validation is unmeasured.sceKernelProtectDirectMemoryForPIDparameters.Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-10-29T12:00:46Z.
Review: APPROVED
Re-checked — approved
ea1f12ccdrewrites thesceKernelProtectDirectMemoryForPIDdebt entry around the MihawkOS sources, adds a debt entry for thesceKernelAllocateDirectMemoryForAppforward and declares the knownProtectDirectMemoryForPIDparameters; both blocking findings are resolved and the merged tree builds and passes the focused tests.What changed since my last review
ea1f12ccd8b2385c66d49c42f802eab84bd0f678, "docs(libkernel): cite the sources of both direct memory exports":sceKernelProtectDirectMemoryForPIDentry is rewritten, and a newsceKernelAllocateDirectMemoryForAppentry is added.(int64_t offset, size_t len, int prot, int pid). The body still callsNotImplemented_nid_no_patch.native/pw_dmem_share.handdocs/PLAN.mdat MihawkOS commit8a9c117, and saying thatForApp's own checks are unmeasured.guest_direct_memory_for_app, a passingcheck_conventions.py, and that PPSA99170 still loads. I accept the title run as author-reported.mainmoved fromba5b5330bto5454cd2e4. The test and its CMake entry did not change.What I ran
Head
ea1f12ccd8b2385c66d49c42f802eab84bd0f678, merged with--no-ffontomain5454cd2e455994d992aca39335ce60507eeda1ebin a clean worktree. Same machine as before: Arch Linux 7.2.11-zen, GCC 16.2.1, Intel Core i9-14900KF, NVIDIA GeForce RTX 4070 Ti SUPER (driver 615.78.08); Release, Ninja,BUILD_TESTING=ON.I also read
native/pw_dmem_share.handdocs/PLAN.mdat MihawkOS8a9c117to check what the new debt entries say about them.maingit diff --checkcheck_conventions.pydebt-length, see below)libs, new test)Unimplemented.cpporDirectMemory/Export.cppwith the named, unused parametersguest_direct_memory_for_appand the five related memory andkernel_systestsfNoviuIwykcandcBp0Ad-24LAeach once inlibkernel.prx, not inlibkernel_sys.prxcheck_conventions.py and ctest
Negative control: the test and its CMake entry are unchanged since
bf1ae46, and the new commit only renames the stub's parameters. I did not repeat the control. The earlier one still applies: without the production files the test does not compile.Previous findings
0xc018800freading and both console runs. It also lists the limits: a third-party source, one firmware, an unknown error for pid 0 or a missing pid, and no second guest process. I checked these against the header and against PLAN.md.sceKernelAllocateDirectMemoryForAppentry cite the header's line 17 declaration. The entry says the forward is assumed from the shared signature, and thatForApp's own checks and errors are unmeasured.ProtectDirectMemoryForPIDparameters): Resolved inUnimplemented.cpp:131andApi.hpp:211.ForApp-specific behaviour.New findings
Blocking
None.
Non-blocking
check_conventions.pywarns about entries over 1000 (debt-length, CONTRIBUTING.md:78). My request for sources partly caused this. It could be cut without losing anything: drop the two run IDs (PLAN.md links them), and replace the PLAN.md narrative with "per a static reading oflibkernel_sys.sprx10.01 and one console run in PLAN.md". Keep the prototype, the unknown error codes and the single-process reason for throwing.Not verified
Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-10-29T12:08:28Z.
Review: APPROVED
Re-checked — approved
d4d53e2decuts thesceKernelProtectDirectMemoryForPIDdebt entry from 1350 to 854 characters, which resolves my one remaining non-blocking finding. The change touches only documentation, so the merge preview is clean andcheck_conventions.pynow reports no findings.What changed since my last review
d4d53e2deadd1e768f7d2278a40275f9445367c2, "docs(libkernel): trim the ProtectDirectMemoryForPID debt entry". It changes one line, docs/dev/debt/libkernel.md:327, and no code, tests or build files.pw_dmem_share.hlink, the static reading oflibkernel_sys.sprx10.01, the one console run, the unknown error for pid 0 or a missing pid, and the single-process reason. It drops the run IDs, the link toPLAN.mdand the "third-party repository / one firmware / payload-only" sentence. ThesceKernelAllocateDirectMemoryForAppentry on line 328 is unchanged and is 914 characters.mainis still5454cd2e4.What I ran
Head
d4d53e2deadd1e768f7d2278a40275f9445367c2,main5454cd2e455994d992aca39335ce60507eeda1eb, in a clean worktree:main(merge-treeand a real merge)git diff --checkcheck_conventions.pyAll conventions checks passed.(thedebt-lengthwarning is gone)pw_dmem_share.hlink in the entryI did not rebuild or rerun the tests, because the new commit only changes documentation. The build, the six focused tests and the NID checks from my previous review on
ea1f12ccdstill apply to the code.Previous findings
ProtectDirectMemoryForPIDentry was 1350 characters and triggereddebt-length): Resolved. The entry is now 854 characters, andcheck_conventions.pypasses on the merged tree. My earlier blocking findings were already resolved inea1f12ccd, and the trimmed entry still states the prototype, its source and the open gaps.oneandonlydeanasks for changes to wording ("no prototype, argument order or measured result was found", the speculative no-op wording). That wording came frombf1ae46and was already removed inea1f12ccd. It is not on the current head.New findings
Blocking
None.
Non-blocking
pw_dmem_share.hlink on line 327 uses the short SHA8a9c117, while line 328 and the PR body use the full8a9c117cb265d0f54ead53b0aa37cdc8c0dc9c91. The link resolves today. However, a short SHA can become ambiguous as that repository grows, and the two entries would read more consistently with the full SHA. Optional.Not verified