Commit Graph
146 Commits
Author SHA1 Message Date
Adrià 5faed506a5 Merge pull request #8428 from hypermoist/feat/libkernel-pthread-cleanup-push
feat(libkernel): implement pthread_cleanup_push and pthread_cleanup_pop
2026-10-21 15:32:45 +02:00
SebastianandClaude Opus 5.5 74bd5d9fa5 feat(libkernel): add mutex ownership, relative lock and spin/yield accessors
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QnkpF2oEMofseVz9Z7WEAC
2026-10-21 08:00:19 +00:00
Dean ef1f41c4c9 Merge pull request #8061 from Dragost/fix/libkernel-pthread-yield-busy-spin
fix(libkernel): prevent 100% CPU busy-spinning on pthread yield on Windows
2026-10-21 20:57:20 +13:00
Dragost 2716dee730 fix(libkernel): prevent 100% CPU busy-spinning on pthread yield on Windows 2026-10-21 09:30:27 +02:00
Ondra97 07486d7cfa fix(libkernel): report current thread scheduling policy in attributes 2026-10-20 19:05:08 +02:00
hypermistandClaude Opus 5.5 fa0e7b8c13 feat(libkernel): implement pthread_cleanup_push and pthread_cleanup_pop
The exported forms allocate the handler record themselves and free it
when it is popped, as libkernel does; __pthread_cleanup_pop_imp now frees
such records too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RbLgECLQ2bpDM7oe2urGZK
2026-10-21 01:33:25 +13:00
Dean 56a0be39dd Merge pull request #7957 from Lindamea1/fix/libkernel-attrget-esrch-ended-thread
fix(libkernel): return ESRCH from scePthreadAttrGet for a thread that has ended
2026-10-20 17:40:20 +13:00
Adrià 88fb5592c6 Merge pull request #7611 from Lindamea1/fix/kernel-pthread-attr-sched-getters
feat(libkernel): implement scePthreadAttrGetschedpolicy and scePthreadAttrGetinheritsched
2026-10-20 01:23:13 +02:00
Lindamea1andClaude Opus 5.5 3237083172 fix(libkernel): return ESRCH from scePthreadAttrGet for a thread that has ended
scePthreadAttrGet copied the attributes of any non-null handle, so a
thread that was joined or had finished still answered 0, and a thread
that ended detached was read after its object had been freed. FreeBSD
11.4's _pthread_attr_get_np returns ESRCH for a dead or reclaimed thread,
and Monster Hunter Wilds (PPSA24191) reuses a slot of its thread table
only when scePthreadAttrGet returns 0x80020003.

After the EINVAL checks it now returns ESRCH unless the handle is a live
guest thread, and copies the fields while holding the thread registry
lock (LockLiveGuestThread), so a detached thread exiting during the call
is not freed under the copy. GuestThreadIsLive uses the same lookup.
pthread_attr_get_np, which the title does not import, is left as it is;
the debt entry records the difference.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 16:25:14 +01:00
Adrià 2eb4de8e13 Merge pull request #6711 from Ondra97/codex/fix-rwlock-unlock-lifetime
fix(libkernel): notify rwlock waiters before unlocking state
2026-10-19 11:03:06 +02:00
Doğukan Atlıhan 1ad6c746b8 fix(libkernel): replace anonymous Windows stack mappings 2026-10-19 06:42:30 +02:00
klosixgaming-arch 30cbb2fd66 fix(libkernel): refuse to detach or rejoin a thread that is being joined
PPSA34015 (EA SPORTS FC 27) stopped with "Joining guest thread: Access is
denied": scePthreadJoin waited on a thread that another call released
under it, and then waited on its closed handle. As FreeBSD 11.4's
thr_join.c and thr_detach.c do with the joiner field, scePthreadDetach
now returns EINVAL while a thread waits to join the target, a second
concurrent scePthreadJoin returns EOPNOTSUPP, a cancelled join backs out,
and a completed join leaves the thread detached.
2026-10-19 03:30:58 +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
Adrià 5768b25925 Merge pull request #6244 from xXPBSXx/fix/pthread-destroyed-einval
fix(libkernel): return EINVAL for pthread mutex and cond operations on a destroyed object
2026-10-18 23:27:09 +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
xXPBSXxandClaude Sonnet 5.5 c664ffbec4 fix(libkernel): return EINVAL for pthread mutex and cond operations on a destroyed object
Locking, trylocking, unlocking a destroyed mutex and signalling, broadcasting,
waiting on or destroying a destroyed condition variable threw a C++ exception
into the guest. FreeBSD 11.4 libthr returns EINVAL for all of them (thr_mutex.c
check_and_init_mutex and mutex_unlock_common compare against THR_MUTEX_DESTROYED,
thr_cond.c CHECK_AND_INIT_COND and _pthread_cond_destroy against
THR_COND_DESTROYED). The condition wait path already reported EINVAL for a
destroyed mutex.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-18 21:23:54 +02:00
Lindamea1andClaude Opus 5.5 2514eadcbd feat(libkernel): implement scePthreadAttrGetschedpolicy and scePthreadAttrGetinheritsched
libkernel exported scePthreadAttrSetschedpolicy and
scePthreadAttrSetinheritsched but not the matching getters,
scePthreadAttrGetschedpolicy (NID NMyIQ9WgWbU) and
scePthreadAttrGetinheritsched (NID lpMP8HhkBbg), so a title that
imports either one stopped at startup on the unresolved import. Dragon
Ball Xenoverse 2 (PPSA08587) imports both.

Implement them as FreeBSD 11.4 libthr implements
pthread_attr_getschedpolicy and pthread_attr_getinheritsched
(lib/libthr/thread/thr_attr.c): a null or uninitialized attribute
returns EINVAL (0x80020016), as does a null output of
scePthreadAttrGetschedpolicy; otherwise the attribute's policy or
inheritance is stored and 0 is returned. libthr writes through a null
output of the inheritance getter unchecked, so that case throws, as
pthread_attr_getinheritsched and scePthreadAttrGetsolosched do.

Neither getter was measured on a console. The scePthreadAttr entry in
docs/dev/debt/libkernel.md, which describes the calls measured on a
PS5, now says so and records the throw.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-18 17:45:49 +01:00
Adrià a0f3dc4014 Merge pull request #6091 from Ondra97/codex/fix-cond-canceled-signal
fix(libkernel): preserve condition signals across cancellation
2026-10-18 17:33:22 +02:00
Karim Ibrahim 267cb56a0d fix(libkernel): report the precommitted stack of an adopted thread on Windows
An adopted thread (the main thread, or a host thread that calls
scePthreadSelf) reported its whole Windows stack reservation, and so
did sceKernelIsStack on a host thread before adoption. A relinked
executable reserves 0x210000 bytes for its main thread and commits the
top 0x200000, so the lowest 64 KiB of the reported stack hold only
reserved pages and Windows' guard pages. mprotect fails there, so
HotSpot in Java 25 LAN / DNS (PPSA99869) could not create the main
thread's stack guard pages ("Attempt to allocate stack guard pages
failed").

When everything above the bottom allocation granule is committed
read-write, the host stack is now reported from there, as created
guest threads report only their committed stack. Other host stacks
keep the whole range. The committed-pages walk is shared with
StartNativeThread and throws when VirtualQuery fails.
2026-10-18 16:24:10 +02:00
Dean cb1d6e5bd6 Merge pull request #7123 from thuanquang/fix/guest-frame-chain-zero-record
fix(libkernel): end guest thread, fiber and context frame chains at a zero record
2026-10-18 22:04:31 +13:00
milescovielloandClaude Opus 5.5 4ebc7d7d43 fix(libkernel): answer a join of the calling thread before its detach state
scePthreadJoin checked whether the thread was detached before whether it
was the calling thread. So a detached thread joining itself got EINVAL
instead of EDEADLK, and so did the main thread, which scePthreadSelf
adopts as detached. FreeBSD's join checks the calling thread right after
the null check and before it looks at the detach state. The self check
now comes first; the null, detached and already-joined answers are
unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-18 02:41:38 +00:00
Dean c0a7468f6f Merge pull request #7270 from Lindamea1/fix/cond-init-unset-attr
fix(libkernel): accept an uninitialized attribute in scePthreadCondInit
2026-10-18 10:39:14 +13:00
Dean 770e207ce5 Merge pull request #7115 from milescoviello/fix/asan-stack-reserve
fix(libkernel): measure the host stack reserve from the frame address
2026-10-18 10:25:49 +13:00
Lindamea1andClaude Opus 5.5 111d81786b fix(libkernel): accept an uninitialized attribute in scePthreadCondInit
scePthreadCondInit, and pthread_cond_init through it, threw when attr
pointed to a null attribute: a zeroed one that was never initialized or
one that scePthreadCondattrDestroy has set to null. FreeBSD 11.4 libthr
(thr_cond.c, cond_init) takes a null attribute as the default one and
creates a CLOCK_REALTIME condition variable. A PS5 (firmware 13.60)
takes a zeroed attribute as the default one in scePthreadMutexInit
(#4158). Take it as the default here too, as scePthreadMutexInit and
scePthreadCreate already do.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-17 21:54:15 +01:00
Dean b95768d1b0 Merge pull request #6540 from JJenkx/fix/kernel-cpuset-thread-id
feat(libkernel): support cpuset affinity by guest thread ID
2026-10-18 09:43:55 +13:00
Dean 4827f36cb5 Merge pull request #7029 from milescoviello/fix/condattr-setclock-einval
fix(libkernel): return EINVAL from scePthreadCondattrSetclock and Destroy for a null attribute
2026-10-18 02:22:59 +13:00
thuanquangandClaude Opus 5.5 ff9bf22281 fix(libkernel): end guest thread, fiber and context frame chains at a zero record
Unreal Engine walks the frame-pointer chain until it reads a return
address of 0 and never checks the frame pointer itself. Guest threads
started with whatever rbp the host code held, so the walk ran into host
start-up frames (glibc's saved rbp 0x21 in a LEGO Batman core dump,
address 8 in Returnal on Windows) and faulted. Fibers ended their chain
with a null frame, and makecontext started its function with rbp 0, so
the same walker read address 8 there too.

Guest threads now start through a thunk that points rbp at a
{saved rbp 0, return address 0} record, as the relinker's entry stub
does for the main thread; the fiber trampolines push the same record,
and the makecontext start function is built with frame pointers so its
frame, over the zero return address makecontext stores, ends the chain.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-17 19:27:53 +07:00
milescovielloandClaude Opus 5.5 e88b7de3c1 fix(libkernel): measure the host stack reserve from the frame address
MeasureStackReserve took the distance from the stack top to a local
variable. In an ANYPS5_ENABLE_ASAN build that local lives on
AddressSanitizer's fake stack (detect_stack_use_after_return is on by
default on Linux), away from the thread's stack, so the reserve could
come out negative. scePthreadCreate then asked the guest arena for a
stack of nearly 2^64 bytes and threw 'guest address space arena
exhausted'. The frame address is on the real stack in every build.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-17 12:06:42 +00:00
milescovielloandClaude Opus 5.5 62ac8d655f fix(libkernel): return EINVAL from scePthreadCondattrSetclock and Destroy for a null attribute
Both threw std::invalid_argument for a null or uninitialized attribute.
FreeBSD's libthr returns EINVAL for one, as the other scePthread
attribute calls here already do. The clock id is still stored as
given.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-17 06:42:41 +00:00
milescovielloandClaude Opus 5.5 19b236a4c5 fix(libkernel): return EINVAL from the scePthreadAttr stack and guard size calls
scePthreadAttrGetstack, Getstackaddr, Getguardsize, Setguardsize,
Setstack and Setstackaddr threw for a null attribute, a null pointer
argument or a stack below the 16 KiB minimum. FreeBSD's libthr returns
EINVAL for those, as the pthread_attr_* wrappers here and the other
scePthreadAttr calls already do.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-17 06:21:18 +00:00
Yunus Shaikh fabc89ad6a fix(libkernel): keep the cached host stack range in two scalar thread-locals
HostStackLimits cached the queried stack range in one thread_local
std::pair. GCC gives that 16-byte object 16-byte alignment and stores it
with movaps. With CI's wild 0.10.0 link, libkernel.prx's PT_TLS segment
starts at an address that is 8 mod 16 (p_align 16), so the pair sits at
TLS offset 0x38. When libkernel.prx is loaded by dlopen its TLS block is
allocated dynamically and the pair ends up 8 bytes off a 16-byte boundary.
The first store then faults: guest_memory_lifetime's child, which loads
libkernel through its module and calls DoMprotect after static
destruction, died with SIGSEGV in HostStackLimits.

Two 8-byte thread-locals carry no 16-byte alignment assumption, so the
cache works with either linker.
2026-10-16 22:29:52 -07:00
Yunus Shaikh 2ac72d48bc Merge main into fix/libkernel-host-stack-guard-page 2026-10-16 22:29:52 -07:00
Dean 72a848ca52 Merge pull request #4533 from Roojool/contrib/g07-pthread-getname
fix(libkernel): answer pthread_getname_np for a joined thread with ESRCH
2026-10-17 13:05:38 +13: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
Yunus Shaikh 8d2053a21e fix(libkernel): keep a host thread's stack range once queried on Linux
glibc reports the main thread's stack as reaching down to the end of
the mapping below it. The pages a protect maps under the main stack
become that mapping, so after the first protect sceKernelIsStack
reported a stack starting above them, and a second protect of the same
pages, such as restoring them to read/write, failed as outside any
stack. Each thread now queries its host stack once, which also stops
every protect on the main thread from reading /proc/self/maps.
2026-10-16 07:27:37 +00:00
Ondra97 f2b4a71aed fix(libkernel): finish rwlock notifications before releasing state 2026-10-16 07:52:22 +02:00
JJenkx 7777219ee1 feat(libkernel): support cpuset affinity by guest thread ID 2026-10-15 15:25:34 -04:00
Ondra97 241053ad9f fix(libkernel): preserve condition signals across cancellation 2026-10-14 23:34:45 +02:00
Adrià b0a4753de1 Merge pull request #5230 from hugodearaujo/fix/inherit-creator-priority
fix(libkernel): give an inheriting thread its creator's priority
2026-10-14 21:07:30 +02:00
Rujul Talekar e41f7f292e fix(libkernel): answer a second scePthreadJoin with ESRCH 2026-10-14 10:57:05 +05:30
JJenkx d1527840ea fix(libkernel): retire stack metadata before releasing mappings 2026-10-13 20:30:24 -04:00
Evgeniy KazarinandClaude Opus 5.5 99eaf73102 perf(libkernel): wake guest mutex waiters on release on Windows
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 02:18:33 +03:00
Adrià 9c05bf25f5 Merge pull request #4585 from jagrat04/feat/libkernel-cpuset-affinity-errors-pr
feat(libkernel): return ESRCH and EINVAL for unsupported cpuset affinity targets
2026-10-13 17:23:01 +02:00
hugodearaujoandClaude Opus 5.5 143eb6aaed fix(libkernel): give an inheriting thread its creator's priority
Measured on a PS5 (firmware 13.60): a thread created with
PTHREAD_INHERIT_SCHED, a null attribute or a default attribute runs at
its creator's priority, whatever priority the attribute holds; with
explicit scheduling it takes the attribute's priority.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-13 12:02:33 -03:00
MarcZX100 9b6e7f66c4 fix(libkernel): protect host-only guest stack pages 2026-10-13 16:03:36 +02:00
Cesar RodriguezandClaude Opus 5.5 7637cac13e fix(libkernel): place guest thread stacks inside the application area
On Linux, glibc maps the stack of every guest thread near the top of the
host address space (around 0x7fff00000000). On the console, application
stacks lie inside the application area below 0xFC00000000, and guest code
relies on that: ChakraCore's garbage collector indexes a 4 MiB table with
address >> 18, so it only covers addresses below 1 TiB and reports
Js::OutOfMemoryException for a stack above it.

Map each guest thread's stack with DoMapAnon, which allocates from the
guest arena, behind a PROT_NONE guard page, and pass it to pthread_attr_setstack.
The stack is freed after a join; the stack of a detached thread is freed once its
host thread is gone from /proc/self/task.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-13 16:03:36 +02:00
hugodearaujoandClaude Opus 5.5 5c6a9d22c4 fix(libkernel): check and keep the scheduling policy of a thread
On a PS5 (firmware 13.60) pthread_setschedparam and
scePthreadSetschedparam accept only SCHED_FIFO and SCHED_RR with a
priority of 256 to 767 or 896 to 959, return EINVAL otherwise, and
keep the policy for pthread_getschedparam. A created thread takes the
policy of explicit scheduling attributes, or its creator's when it
inherits. Here any policy and priority were accepted and SCHED_FIFO
was always reported.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-13 12:53:19 +02:00
jagrat Singh 9efabff1cb feat(libkernel): return ESRCH and EINVAL for unsupported cpuset affinity targets
Allow calling threads to query and set affinity using their own thread ID in addition to -1. Return ESRCH for non-matching thread or process IDs and EINVAL for invalid level/which arguments instead of throwing NotImplemented, matching FreeBSD cpuset semantics.
2026-10-13 15:58:11 +05:30
Dean a6ed539e70 Merge pull request #4659 from AaravMehta-07/codex/sce-pthread-sem-overflow
fix(libkernel): guard SCE pthread semaphore count overflow
2026-10-13 17:49:51 +13:00
Dean b7025517e1 Merge pull request #4656 from milescoviello/fix/rwlock-writer-preference
fix(libkernel): hold back new rwlock readers while a writer waits
2026-10-13 17:49:30 +13:00