From 5c6a9d22c4bbc626ac4c618be07731405d38f01b Mon Sep 17 00:00:00 2001 From: hugodearaujo Date: Sun, 11 Oct 2026 19:51:43 -0300 Subject: [PATCH] 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 --- core/libs/prx/libkernel/CMakeLists.txt | 10 ++ .../prx/libkernel/Pthread/Posix/Thread.cpp | 24 ++-- .../prx/libkernel/Pthread/include/Pthread.hpp | 1 + .../libs/prx/libkernel/Pthread/src/Thread.cpp | 3 + core/libs/tests/GuestPthreadSchedparam.cpp | 108 ++++++++++++++++++ docs/dev/debt/libkernel.md | 2 +- 6 files changed, 138 insertions(+), 10 deletions(-) create mode 100644 core/libs/tests/GuestPthreadSchedparam.cpp diff --git a/core/libs/prx/libkernel/CMakeLists.txt b/core/libs/prx/libkernel/CMakeLists.txt index 2fcf255d4795..a183b8e4b940 100644 --- a/core/libs/prx/libkernel/CMakeLists.txt +++ b/core/libs/prx/libkernel/CMakeLists.txt @@ -380,6 +380,16 @@ if(BUILD_TESTING) set_tests_properties(guest_pthread_attr PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() + add_test_executable(guest_pthread_schedparam_tests ${LIBS_INCLUDE_DIR}/tests/GuestPthreadSchedparam.cpp) + target_include_directories(guest_pthread_schedparam_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(guest_pthread_schedparam_tests PRIVATE libkernel libc) + configure_windows_unwind(guest_pthread_schedparam_tests) + add_test(NAME guest_pthread_schedparam COMMAND guest_pthread_schedparam_tests) + set_tests_properties(guest_pthread_schedparam PROPERTIES TIMEOUT 20) + if(WIN32) + set_tests_properties(guest_pthread_schedparam PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() add_test_executable(guest_pthread_mutexattr_tests ${LIBS_INCLUDE_DIR}/tests/GuestPthreadMutexattr.cpp) target_include_directories(guest_pthread_mutexattr_tests PRIVATE ${LIBS_INCLUDE_DIR}) target_link_libraries(guest_pthread_mutexattr_tests PRIVATE libkernel libc) diff --git a/core/libs/prx/libkernel/Pthread/Posix/Thread.cpp b/core/libs/prx/libkernel/Pthread/Posix/Thread.cpp index 37beb11416a0..f0de083f0536 100644 --- a/core/libs/prx/libkernel/Pthread/Posix/Thread.cpp +++ b/core/libs/prx/libkernel/Pthread/Posix/Thread.cpp @@ -29,6 +29,12 @@ int APS5_VABI scePthreadGetprio(Pthread thread, int* prio); } static constexpr int GUEST_SCHED_FIFO = 1; +static constexpr int GUEST_SCHED_RR = 3; + +static bool AcceptsSchedParam(int policy, int priority) { + if (policy != GUEST_SCHED_FIFO && policy != GUEST_SCHED_RR) return false; + return (priority >= 256 && priority <= 767) || (priority >= 896 && priority <= 959); +} static constexpr std::size_t GUEST_CPUSET_BYTES = 32; static constexpr int GUEST_ERANGE = 34; @@ -109,20 +115,21 @@ void APS5_VABI pthread_exit_nid_postfix(void* value) { int APS5_VABI scePthreadGetschedparam(Pthread thread, int* policy, KernelSchedParam* param) { if (!policy || !param) return SCE_KERNEL_ERROR_EINVAL; - *policy = GUEST_SCHED_FIFO; - return scePthreadGetprio(thread, ¶m->sched_priority); + const int result = scePthreadGetprio(thread, ¶m->sched_priority); + if (result == 0) *policy = thread->schedPolicy.load(std::memory_order_relaxed); + return result; } int APS5_VABI scePthreadSetschedparam(Pthread thread, int policy, const KernelSchedParam* param) { - (void)policy; - if (!param) return SCE_KERNEL_ERROR_EINVAL; - return scePthreadSetprio(thread, param->sched_priority); + if (!param || !AcceptsSchedParam(policy, param->sched_priority)) return SCE_KERNEL_ERROR_EINVAL; + const int result = scePthreadSetprio(thread, param->sched_priority); + if (result == 0) thread->schedPolicy.store(policy, std::memory_order_relaxed); + return result; } int APS5_VABI pthread_getschedparam_nid_postfix(Pthread thread, int* policy, KernelSchedParam* param) { if (!policy || !param) return PosixThread::GUEST_EINVAL; - *policy = GUEST_SCHED_FIFO; - return PosixThread::ToErrno(scePthreadGetprio(thread, ¶m->sched_priority)); + return PosixThread::ToErrno(scePthreadGetschedparam(thread, policy, param)); } int APS5_VABI pthread_getaffinity_np_nid_postfix(Pthread thread, std::size_t cpusetSize, void* cpuset) { @@ -200,9 +207,8 @@ int APS5_VABI pthread_setprio_nid_postfix(Pthread thread, int prio) { } int APS5_VABI pthread_setschedparam_nid_postfix(Pthread thread, int policy, const KernelSchedParam* param) { - (void)policy; if (!param) return PosixThread::GUEST_EINVAL; - return PosixThread::ToErrno(scePthreadSetprio(thread, param->sched_priority)); + return PosixThread::ToErrno(scePthreadSetschedparam(thread, policy, param)); } void APS5_VABI pthread_testcancel_nid_postfix(void) { diff --git a/core/libs/prx/libkernel/Pthread/include/Pthread.hpp b/core/libs/prx/libkernel/Pthread/include/Pthread.hpp index 8328f6b4fc2e..a139d7e9552d 100644 --- a/core/libs/prx/libkernel/Pthread/include/Pthread.hpp +++ b/core/libs/prx/libkernel/Pthread/include/Pthread.hpp @@ -117,6 +117,7 @@ struct PthreadPrivate { std::atomic references{2}; std::atomic inWait{false}; std::atomic pendingException{0}; + std::atomic schedPolicy{1}; void* wakeEvent = nullptr; void* stackAddress = nullptr; std::size_t stackSize = 0; diff --git a/core/libs/prx/libkernel/Pthread/src/Thread.cpp b/core/libs/prx/libkernel/Pthread/src/Thread.cpp index c02e54bab748..a2ea58f0aa10 100644 --- a/core/libs/prx/libkernel/Pthread/src/Thread.cpp +++ b/core/libs/prx/libkernel/Pthread/src/Thread.cpp @@ -194,6 +194,9 @@ PthreadPrivate::PthreadPrivate(const PthreadAttrPrivate* attr) : _finished(false const auto* parent = CurrentGuestState().current; const KernelCpumask inherited = parent ? parent->affinity.load(std::memory_order_relaxed) : registry.processAffinity; affinity.store(attr && attr->_affinityExplicit ? attr->_affinity : inherited, std::memory_order_relaxed); + constexpr int ExplicitSched = 0; + const int inheritedPolicy = parent ? parent->schedPolicy.load(std::memory_order_relaxed) : schedPolicy.load(std::memory_order_relaxed); + schedPolicy.store(attr && attr->_inheritsched == ExplicitSched ? attr->_schedpolicy : inheritedPolicy, std::memory_order_relaxed); if (registry.nextThread > static_cast(CPU_CLOCK_ID_MASK)) throw std::runtime_error("Guest thread CPU clock identifiers are exhausted"); cpuClockThread = registry.nextThread++; diff --git a/core/libs/tests/GuestPthreadSchedparam.cpp b/core/libs/tests/GuestPthreadSchedparam.cpp new file mode 100644 index 000000000000..6b6b07b2b840 --- /dev/null +++ b/core/libs/tests/GuestPthreadSchedparam.cpp @@ -0,0 +1,108 @@ +#include "SceTypes.hpp" +#include +#include + +extern "C" { +int APS5_VABI scePthreadCreate(Pthread* thread, const PthreadAttr* attr, PthreadEntry entry, void* arg, const char* name); +int APS5_VABI scePthreadJoin(Pthread thread, void** retval); +Pthread APS5_VABI scePthreadSelf(void); +int APS5_VABI scePthreadGetschedparam(Pthread thread, int* policy, KernelSchedParam* param); +int APS5_VABI scePthreadSetschedparam(Pthread thread, int policy, const KernelSchedParam* param); +int APS5_VABI pthread_getschedparam_nid_postfix(Pthread thread, int* policy, KernelSchedParam* param); +int APS5_VABI pthread_setschedparam_nid_postfix(Pthread thread, int policy, const KernelSchedParam* param); +int APS5_VABI scePthreadAttrInit(PthreadAttr* attr); +int APS5_VABI scePthreadAttrDestroy(PthreadAttr* attr); +int APS5_VABI scePthreadAttrSetinheritsched(PthreadAttr* attr, int inheritSched); +int APS5_VABI scePthreadAttrSetschedpolicy(PthreadAttr* attr, int policy); +} + +static constexpr int SCE_OK = 0; +static constexpr int SCE_KERNEL_ERROR_EINVAL = static_cast(0x80020016); +static constexpr int GUEST_EINVAL = 22; +static constexpr int SCHED_FIFO = 1; +static constexpr int SCHED_OTHER = 2; +static constexpr int SCHED_RR = 3; +static constexpr int EXPLICIT_SCHED = 0; +static constexpr int INHERIT_SCHED = 4; + +static void Require(bool value) { if (!value) std::abort(); } + +static void RequireSched(int policy, int priority) { + int current = -1; + KernelSchedParam param{}; + Require(scePthreadGetschedparam(scePthreadSelf(), ¤t, ¶m) == SCE_OK); + Require(current == policy && param.sched_priority == priority); + current = -1; + param.sched_priority = -1; + Require(pthread_getschedparam_nid_postfix(scePthreadSelf(), ¤t, ¶m) == 0); + Require(current == policy && param.sched_priority == priority); +} + +static int SceSet(int policy, int priority) { + const KernelSchedParam param{priority}; + return scePthreadSetschedparam(scePthreadSelf(), policy, ¶m); +} + +static int PosixSet(int policy, int priority) { + const KernelSchedParam param{priority}; + return pthread_setschedparam_nid_postfix(scePthreadSelf(), policy, ¶m); +} + +static void* APS5_VABI Run(void*) { + int policy = -1; + KernelSchedParam param{}; + Require(scePthreadGetschedparam(scePthreadSelf(), &policy, ¶m) == SCE_OK); + Require(policy == SCHED_FIFO); + + for (int priority : {256, 767, 896, 959}) { + Require(SceSet(SCHED_FIFO, priority) == SCE_OK); + RequireSched(SCHED_FIFO, priority); + } + for (int priority : {-1, 0, 255, 768, 895, 960, 1023, 4096}) { + Require(SceSet(SCHED_FIFO, priority) == SCE_KERNEL_ERROR_EINVAL); + Require(PosixSet(SCHED_FIFO, priority) == GUEST_EINVAL); + RequireSched(SCHED_FIFO, 959); + } + + Require(SceSet(SCHED_RR, 300) == SCE_OK); + RequireSched(SCHED_RR, 300); + for (int other : {-1, 0, SCHED_OTHER, 4}) { + Require(SceSet(other, 700) == SCE_KERNEL_ERROR_EINVAL); + Require(PosixSet(other, 700) == GUEST_EINVAL); + RequireSched(SCHED_RR, 300); + } + Require(PosixSet(SCHED_FIFO, 700) == 0); + RequireSched(SCHED_FIFO, 700); + Require(scePthreadSetschedparam(scePthreadSelf(), SCHED_FIFO, nullptr) == SCE_KERNEL_ERROR_EINVAL); + return nullptr; +} + +static void* APS5_VABI ReportPolicy(void* arg) { + int policy = -1; + KernelSchedParam param{}; + Require(scePthreadGetschedparam(scePthreadSelf(), &policy, ¶m) == SCE_OK); + *static_cast(arg) = policy; + return nullptr; +} + +static int CreatedPolicy(int inheritSched, int policy) { + PthreadAttr attr = nullptr; + Require(scePthreadAttrInit(&attr) == SCE_OK); + Require(scePthreadAttrSetinheritsched(&attr, inheritSched) == SCE_OK); + Require(scePthreadAttrSetschedpolicy(&attr, policy) == SCE_OK); + int reported = -1; + Pthread thread = nullptr; + Require(scePthreadCreate(&thread, &attr, ReportPolicy, &reported, "policy") == SCE_OK); + Require(scePthreadJoin(thread, nullptr) == SCE_OK); + Require(scePthreadAttrDestroy(&attr) == SCE_OK); + return reported; +} + +int main() { + Pthread thread = nullptr; + Require(scePthreadCreate(&thread, nullptr, Run, nullptr, "schedparam") == SCE_OK); + Require(scePthreadJoin(thread, nullptr) == SCE_OK); + Require(CreatedPolicy(EXPLICIT_SCHED, SCHED_RR) == SCHED_RR); + Require(CreatedPolicy(EXPLICIT_SCHED, SCHED_FIFO) == SCHED_FIFO); + Require(CreatedPolicy(INHERIT_SCHED, SCHED_RR) == SCHED_FIFO); +} diff --git a/docs/dev/debt/libkernel.md b/docs/dev/debt/libkernel.md index be4a318e02b1..2e2f627ca0f0 100644 --- a/docs/dev/debt/libkernel.md +++ b/docs/dev/debt/libkernel.md @@ -86,7 +86,7 @@ - [uDGXwUKqi8s](../../../core/libs/prx/libkernel/Unimplemented.cpp) (libkernel) - unknown name, signature - [sceKernelGetCpuTemperature](../../../core/libs/prx/libkernel/Unimplemented.cpp) (libkernel) - imported by PPSA99902, which fails to load without it; added without an implementation because its behavior is unverified. - [LinuxProtFromSce](../../../core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp) (libkernel) - protection bits 0x100 and 0x200 are accepted and assumed not to affect host CPU access; host protection is derived only from the read, write and execute bits -- [sched_get_priority_max, sched_get_priority_min](../../../core/libs/prx/libkernel/System/src/Process.cpp) (libkernel) - measured on a PS5 (firmware 13.60): `SCHED_FIFO` (1) and `SCHED_RR` (3) report a maximum of 767 and a minimum of 256, and every other policy, `SCHED_OTHER` (2) included, returns -1 with `EINVAL`. `pthread_setschedparam` ignores the policy and does not check the priority, where the console rejects `SCHED_OTHER` and priorities below 256 with `EINVAL` +- [sched_get_priority_max, sched_get_priority_min](../../../core/libs/prx/libkernel/System/src/Process.cpp) (libkernel) - measured on a PS5 (firmware 13.60): `SCHED_FIFO` (1) and `SCHED_RR` (3) report a maximum of 767 and a minimum of 256, and every other policy, `SCHED_OTHER` (2) included, returns -1 with `EINVAL`. `pthread_setschedparam` and `scePthreadSetschedparam` accept those two policies with a priority of 256 to 767 or 896 to 959 and keep the policy, as measured; a created thread takes the policy of explicit scheduling attributes, or its creator's when it inherits. When it inherits, it still takes the priority of its attributes, where the console gives it its creator's priority - [inet_pton, inet_ntop](../../../core/libs/prx/libkernel/Socket/src/AddressConversion.cpp) (libkernel) - the text forms are assumed to be those of FreeBSD 11.4 and are unverified on a console: `inet_pton` accepts only four decimal parts without leading zeros for `AF_INET` and the RFC 4291 forms for `AF_INET6` (one `::` standing for at least one group, a dotted-quad tail only as the last 32 bits, no zone id), and `inet_ntop` prints lowercase hex with the first longest run of two or more zero groups compressed, and a dotted tail only for `::a.b.c.d` and `::ffff:a.b.c.d`. They no longer depend on the host resolver library - [sceKernelIsThreadSanitizerEnabled](../../../core/libs/prx/libkernel/System/src/Sanitizer.cpp) (libkernel, NID `LzoM-wVLJDE`) - no source gives the signature; `int (void)` is assumed from `sceKernelIsAddressSanitizerEnabled` and from PPSA23226's bundled `libSceJobManager_nosubmission.prx`, which passes no arguments and tests only the 32-bit result