Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions core/libs/prx/libkernel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,16 @@ if(BUILD_TESTING)
set_tests_properties(guest_pthread_attr PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
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:$<TARGET_FILE_DIR:libkernel>")
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)
Expand Down
24 changes: 15 additions & 9 deletions core/libs/prx/libkernel/Pthread/Posix/Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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, &param->sched_priority);
const int result = scePthreadGetprio(thread, &param->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, &param->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) {
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions core/libs/prx/libkernel/Pthread/include/Pthread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ struct PthreadPrivate {
std::atomic<unsigned> references{2};
std::atomic<bool> inWait{false};
std::atomic<int> pendingException{0};
std::atomic<int> schedPolicy{1};
void* wakeEvent = nullptr;
void* stackAddress = nullptr;
std::size_t stackSize = 0;
Expand Down
3 changes: 3 additions & 0 deletions core/libs/prx/libkernel/Pthread/src/Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(CPU_CLOCK_ID_MASK))
throw std::runtime_error("Guest thread CPU clock identifiers are exhausted");
cpuClockThread = registry.nextThread++;
Expand Down
108 changes: 108 additions & 0 deletions core/libs/tests/GuestPthreadSchedparam.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#include "SceTypes.hpp"
#include <cstdlib>
#include <initializer_list>

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<int>(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(), &current, &param) == SCE_OK);
Require(current == policy && param.sched_priority == priority);
current = -1;
param.sched_priority = -1;
Require(pthread_getschedparam_nid_postfix(scePthreadSelf(), &current, &param) == 0);
Require(current == policy && param.sched_priority == priority);
}

static int SceSet(int policy, int priority) {
const KernelSchedParam param{priority};
return scePthreadSetschedparam(scePthreadSelf(), policy, &param);
}

static int PosixSet(int policy, int priority) {
const KernelSchedParam param{priority};
return pthread_setschedparam_nid_postfix(scePthreadSelf(), policy, &param);
}

static void* APS5_VABI Run(void*) {
int policy = -1;
KernelSchedParam param{};
Require(scePthreadGetschedparam(scePthreadSelf(), &policy, &param) == 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, &param) == SCE_OK);
*static_cast<int*>(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);
}
2 changes: 1 addition & 1 deletion docs/dev/debt/libkernel.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading