Commit Graph

105 Commits

Author SHA1 Message Date
David 064f5ca825 feat(libkernel): implement pthread_attr_setsolosched_np
The POSIX alias takes (attr, int), returns EINVAL (22) for a null or
uninitialized attribute like the other pthread_attr setters, and
stores the value through scePthreadAttrSetsolosched, returning its
result in errno form. The signature and the stored int follow the SCE
twin in KytyPS5 and sharpemu; PS5PCEM implements the alias the same
way. The value is not applied to scheduling, recorded in TechnicalDebt.

Returnal (PPSA01285 v03.000.005) stops on it after eager binding
(#2872, #1155):

    what(): pthread_attr_setsolosched_np_nid_postfix not implemented

LEGO Horizon Adventures (PPSA14632, #734) and Palworld (PPSA23126,
#2598) import it.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-11 02:06:30 +02:00
Hussein Kadhim 709c12e133 fix(libkernel): return EPERM or EINVAL when a condition wait gets a mutex the caller does not hold
pthread_cond_wait and pthread_cond_timedwait, and their sce counterparts, threw a
host exception when the mutex was unlocked, locked by another thread, or not yet
initialized, and dereferenced the adaptive static initializer. FreeBSD returns
EPERM for these (EINVAL for a destroyed mutex) before touching the condition.

(cherry picked from commit a78bb8a855ca3b277f128c6a9e52ff3949b9cb80)
2026-10-11 01:53:12 +02:00
Zayd Zaari 718eb9cc3e fix(libkernel): return EBUSY when destroying locked rwlock 2026-10-11 01:37:26 +02:00
Adrià dc105f8abf
Merge pull request #2973 from babyhuey/feat/libkernel-rwlockattr
feat(libkernel): implement pthread_rwlockattr_init and pthread_rwlockattr_destroy
2026-10-10 23:53:36 +02:00
Dean 0ed05471e5
Merge pull request #2899 from MarcZX100/codex/mutexattr-prioceiling-2689
feat(libkernel): implement mutex priority ceiling attributes
2026-10-11 09:02:05 +13:00
Hannah Lyons 5a43c9b0b9 feat(libkernel): implement pthread_rwlockattr_init and pthread_rwlockattr_destroy 2026-10-10 15:07:43 -04:00
ps54all 08728f95b9 feat(libkernel): implement pthread_mutexattr_setprioceiling and getprioceiling
The mutex attribute now keeps its protocol and priority ceiling the way
FreeBSD's libthr does: the ceiling can be set and read only under
PTHREAD_PRIO_PROTECT (EINVAL otherwise), and setting a protocol resets it
to the highest SCHED_RR priority. Mutexes still do not apply priority
protection; that is listed as a silent stub.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit d10c024f8fdbcdfced0187550f272333264f07df)
2026-10-10 18:12:31 +00:00
Hannah Lyons d2c160be76 feat(libkernel): implement cpuset_getaffinity for the calling thread 2026-10-10 12:40:56 -04:00
Dean Galvin 84d90152b4 fix(libkernel): keep host stack room below guest thread stacks
Since guest threads get exactly the stack size they request, host code
that a guest thread calls runs on that small stack too. sceAgcCreateShader
reaches Driver::RegisterShader, whose frame alone is about 50 KiB, so a
title whose job threads ask for 48 KiB overflows the stack (#2603).

Each guest thread now gets a fixed 1 MiB of host stack below the range it
asked for. The guest still sees the requested size and address range
through scePthreadAttrGet and sceKernelIsStack; only host calls made from
the thread use the extra room. Running the prepared-shader driver tests on
a limited main-thread stack needs between 128 and 192 KiB for shader
registration, Vulkan setup included, so 1 MiB leaves several times that.

Switching to a separate host stack at each HLE entry point would need a
trampoline on every export that may go deep, handling of stack-passed
arguments and unwinding across the switch, and a list of which exports
qualify. A margin below the guest range covers every host call with no
per-export list.

On Windows the committed size was the requested size, and the margin
below it depended on the executable's default stack reserve; requests at
or above that reserve had none. The commit now includes the same margin.
macOS does not run guest threads.

guest_pthread_stack now also calls host code that uses 256 KiB of stack
below each thread's guest range; without the margin it crashes on the
16 KiB thread.

Fixes #2603
2026-10-10 22:41:03 +13:00
Nicolas Abosch ef9d0dd7d2 chore(libkernel): drop the pthread_cancel comment 2026-10-10 11:32:09 +02:00
Nicolas Abosch 3acca27f27 feat(libkernel): implement thread cancellation
scePthreadCancel and pthread_cancel threw, scePthreadTestcancel did
nothing and the cleanup handler exports did not exist.

A cancel request is now recorded on the thread and acted on at
scePthreadTestcancel, the condition variable waits, scePthreadSem and
sem waits, scePthreadJoin and the sleeps, and under their POSIX names.
A thread blocked in one of the waits is woken; a condition wait takes
its mutex back before the thread exits. The thread exits with
PTHREAD_CANCELED through scePthreadExit, which now runs the handlers
of __pthread_cleanup_push_imp and __pthread_cleanup_pop_imp.
scePthreadSetcancelstate and scePthreadSetcanceltype reject unknown
values, and pthread_setcanceltype is added.

Constants, the handler node layout and the order of events follow
FreeBSD libthr. A sleep is not interrupted, file and socket calls are
not cancellation points and the asynchronous type acts like the
deferred one except at the two set calls (TechnicalDebt). New test:
guest_pthread_cancel.
2026-10-10 11:32:09 +02:00
Adrià 6cc3358332
Merge pull request #2184 from EdouardSence/fix/libkernel-pthread-tid
fix(libkernel): start the pthread object with the thread id
2026-10-10 10:27:44 +02:00
EdouardSence 94409c9f32 fix(libkernel): keep the thread id 64-bit on every host
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 09:58:05 +02:00
EdouardSence 9ba1b06a4e fix(libkernel): start the pthread object with the thread id
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 09:58:05 +02:00
QRMOE 022e6be86a fix(libkernel): reject overlong POSIX thread names
AI-assisted: yes. Match the firmware 13.60 probe in PR #1798: names of 32 bytes or more return guest ENAMETOOLONG=63 and preserve the prior name. The complete final change passed a full Windows build and eight focused runtime tests.
2026-10-10 09:53:55 +02:00
NevesHenry faf983ae01 fix(libkernel): give Linux guest threads the stack size they request
scePthreadCreate on Linux started guest threads with std::thread, so every
thread got the host default stack (RLIMIT_STACK, usually 8 MiB) whatever the
title asked for. A thread that asks for more than that and uses it crashes
with SIGSEGV. Once the thread ran, scePthreadAttrGet also reported the host
size instead of the requested one.

Create the thread with pthread_create and a stack of the requested size plus
what glibc keeps at its top (static TLS, the thread control block and the
start_thread frame). That reserve is measured once with a probe thread.
The thread reports [top - reserve - size, top - reserve) as its stack, as
Windows reports [top - size, top), and the creator waits until the range is
set, as on Windows.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-10 08:57:00 +02:00
Dean ac57f46711
Merge pull request #2275 from mrk1987/fix/libkernel-rwlock-concurrent-first-lock
fix(libkernel): initialize guest rwlocks before they are shared
2026-10-10 17:46:25 +13:00
hamb3r 2d783b7962 fix(libkernel): keep Unity GC delivery out of MinGW TLS waits 2026-10-10 03:06:40 +03:00
Adrià a291ae0f88
Merge pull request #1483 from Aryansh1520/fix/libkernel-mutex-destroy-busy
fix(libkernel): return EBUSY when destroying a locked mutex
2026-10-10 01:32:32 +02:00
Adrià c2ac5dd93f
Merge pull request #1676 from Recapi/feat/libkernel-thread-cpu-clock
feat(libkernel): implement pthread_getcpuclockid and per-thread CPU clocks
2026-10-10 00:01:18 +02:00
Adrià 2dbc137693
Merge pull request #1798 from hugodearaujo/feat/libkernel-pthread-getname-np
feat(libkernel): implement pthread_getname_np
2026-10-09 23:51:56 +02:00
Dean 0c8d5356d7
Merge pull request #1480 from awake-devel/fix/cond-wait-recursive-mutex
fix(libkernel): release every level of a recursive mutex during a cond wait
2026-10-10 08:05:19 +13:00
Dean fffb830363
Merge pull request #1477 from awake-devel/fix/current-cpu-in-guest-mask
fix(libkernel): report the current CPU within the thread's guest affinity
2026-10-10 08:04:50 +13:00
Andreas 69063dc1ef fix(libkernel): initialize guest rwlocks before they are shared
winpthreads initializes a PTHREAD_RWLOCK_INITIALIZER lock on its first
use and fails a concurrent first lock with EINVAL. std::shared_timed_mutex
ignores that result in release builds, so two guest threads could both
hold a fresh guest rwlock.

Lock and unlock the mutex once when the guest rwlock is created, before
another thread can reach it, and record the remaining host exposure.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 16:13:41 +02:00
Renan Pires 942a105c39 feat(libkernel): implement pthread_getcpuclockid and per-thread CPU clocks 2026-10-08 23:50:03 -03:00
hugodearaujo 3c7859c41d feat(libkernel): implement pthread_getname_np
Hades (PPSA03355) stops at load on main with "undefined symbol:
9HzfhdtESio", the NID of pthread_getname_np, which it calls on
pthread_self() with a 32-byte buffer. Measured on a PS5 (firmware 13.60)
through dlopen of libkernel: the function takes (thread, buffer), copies
the name and its terminator, returns an empty name for an unnamed
thread, ESRCH for a null or joined thread and EFAULT for a null buffer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 15:23:30 -03:00
Adrià f7e98dad39
Merge pull request #1453 from mrk1987/fix/posix-pthread-setstack
feat(libkernel): export POSIX pthread_attr_setstack
2026-10-08 16:07:11 +02:00
Aryan Sharma d77bc7c363 fix(libkernel): return EBUSY when destroying a locked mutex
scePthreadMutexDestroy threw when the mutex was locked. FreeBSD libthr,
which the console's libkernel follows, returns EBUSY and leaves the
mutex usable; PPSA28997 destroys a locked mutex during its intro and
was aborted.
2026-10-08 14:25:16 +05:30
clem-gb 7406c975d2 fix(libkernel): release every level of a recursive mutex during a cond wait
A condition wait on a recursive mutex locked more than once unlocked the
native mutex only once, so the thread that should signal blocked forever
on it. Unlock the remaining levels before the wait and lock them again
after it, as FreeBSD libthr does in _mutex_cv_unlock and _mutex_cv_lock.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HeC9bA3n74TzJRYv9AgBE8
2026-10-08 10:53:58 +02:00
clem-gb 2ba8f75e15 fix(libkernel): report the current CPU within the thread's guest affinity
sceKernelGetCurrentCpu returned the raw host CPU index. Guest affinities
are bookkeeping only and sceKernelGetAvailableCpumask advertises CPUs 0
to 12, so on a host with more than 13 logical CPUs a title could get 13
or higher, and a thread pinned to one CPU could get another one.

The host index now selects a CPU of the calling thread's affinity
clipped to the available mask, so the result is always an advertised
CPU and a thread pinned to one CPU reports that CPU.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HeC9bA3n74TzJRYv9AgBE8
2026-10-08 10:47:33 +02:00
mrk1987 137a282d70 feat(libkernel): export POSIX pthread_attr_setstack 2026-10-08 09:21:14 +02:00
overlordxrz-source 7ecf00c63b fix(libkernel): return EINVAL for thread stack sizes below 16 KiB 2026-10-07 22:31:44 -04:00
Merijn e5b44c4536 Merge branch 'main' of https://github.com/boykopovar/AnyPS5 into feat/kernel-raise-exception-windows
# Conflicts:
#	docs/dev/TechnicalDebt.md
2026-10-07 21:23:46 +02:00
Merijn 51b6079b5b fix(libkernel): no allocation while the target is suspended; own the wait count
The APC delivery was allocated after SuspendThread, so a target holding
the allocator lock deadlocked the raising thread; it is now allocated
before the suspend. The wait count a raise checks lived in the target's
thread_local storage and dangled once the thread exited; it is now a
member of PthreadPrivate, which TimedWait counts into through a
thread-local pointer bound when the thread starts, and a raise on a
thread that already finished returns ESRCH. TechnicalDebt gets one line
per unverified choice: Windows only, the FreeBSD ucontext layout, the
signal mask, the YMM upper halves, and delivery to threads outside
TimedWait.

AI-assisted: yes

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 21:10:25 +02:00
Merijn b92894c628 Merge branch 'main' of https://github.com/boykopovar/AnyPS5 into feat/kernel-raise-exception-windows
# Conflicts:
#	core/libs/prx/libkernel/CMakeLists.txt
#	core/libs/prx/libkernel/System/src/Exception.cpp
2026-10-07 07:45:49 +02:00
alfalfa-star e91fe0f063 feat(libs): declare the exports PPSA14632 imports that no library provides
LEGO Horizon Adventures imports 19 functions that no library exports
and no open pull request adds, so it fails eager binding. They are
declared as throwing stubs. The two libSceAjmWVorbis imports have no
known name and are exported by NID from libSceAjm.native.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 07:10:13 +02:00
Dean fde401d41f
Merge pull request #992 from CnRJay/feat/ppsa23566-missing-exports
feat(libs): declare the exports PPSA23566 imports that no library provides
2026-10-07 13:02:56 +13:00
Adrià ab9437e34d
feat(libkernel): implement scePthreadTestcancel and pthread_testcancel (#834) 2026-10-07 11:43:32 +13:00
Chad df98b8899e feat(libs): name the PPSA23566 stubs from the shadPS4 NID list and implement sleep
20 of the 31 NIDs have names in shadPS4's aerolib list. They are declared
under those names; POSIX sleep is implemented. The NpPartner001 and Psml
MFSR2 exports stay unnamed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 17:28:39 -05:00
Chad 69973800a7 fix(libkernel): return EINVAL for unknown mutex attribute types
UFC 6 (PPSA23566) passes type 0 to pthread_mutexattr_settype during
startup. libkernel rejects it with EINVAL and leaves the attribute
unchanged, as fpPS4's decompiled body does, instead of aborting.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 17:04:06 -05:00
Chad 7cbf45b660 feat(libs): declare the exports PPSA23566 imports that no library provides
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 17:04:05 -05:00
Dean 0056fc5027
Merge pull request #793 from CnRJay/feat/barrierattr-process-shared
feat(libkernel): accept PTHREAD_PROCESS_SHARED barrier attributes
2026-10-07 10:18:43 +13:00
Dean 465c8f6b81
Merge pull request #878 from adriadam10/feat/kernel-adaptive-mutex-initializer
feat(kernel): lazily initialize the adaptive static mutex initializer
2026-10-07 10:11:42 +13:00
Chad ac91853ae1 Merge remote-tracking branch 'upstream/main' into feat/barrierattr-process-shared 2026-10-06 13:40:57 -05:00
Adrià Franch fe557fb0e2 feat(kernel): lazily initialize the adaptive static mutex initializer
Co-Authored-By: Claude <noreply@anthropic.com>
2026-10-06 14:23:25 +02:00
Merijn c321404196 Merge branch 'main' of https://github.com/boykopovar/AnyPS5 into feat/kernel-raise-exception-windows
# Conflicts:
#	core/libs/prx/libkernel/Pthread/src/Thread.cpp
#	docs/dev/TechnicalDebt.md
2026-10-06 09:59:16 +02:00
Merijn 17332edb44 Merge branch 'main' of https://github.com/boykopovar/AnyPS5 into feat/kernel-is-stack
# Conflicts:
#	core/libs/prx/libkernel/Pthread/src/Thread.cpp
2026-10-06 09:58:27 +02:00
raghav314 670ffb9da3 feat(libkernel): implement sceKernelGetAvailableCpumask
Returns the CPU mask threads get by default (DEFAULT_THREAD_AFFINITY,
0x1FFF), the value itself rather than an error code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 09:41:52 +05:30
Chad d4b01b72c0 feat(libkernel): accept PTHREAD_PROCESS_SHARED barrier attributes
The PS5 kernel follows FreeBSD 11, whose libthr accepts process-shared
barriers. Guest code runs in a single process, so a process-shared
barrier synchronises the same threads a private one does; the attribute
is stored and reported back by getpshared.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-05 20:11:20 -05:00
Dean 90f20b393d
Merge pull request #699 from merwie/feat/libs-ppsa12544-exports
feat(libs): add exports imported by PPSA12544
2026-10-06 12:18:11 +13:00