From ce95520f38bb3ebeb03416a3bc9b25ffaff00560 Mon Sep 17 00:00:00 2001 From: georgemoralis Date: Tue, 18 Jul 2023 18:54:46 +0300 Subject: [PATCH] some pthread work nothing exciting --- CMakeLists.txt | 2 +- src/Core/PS4/HLE/ErrorCodes.h | 5 + src/Core/PS4/HLE/Kernel/ThreadManagement.cpp | 121 +++++++++++++++++++ src/Core/PS4/HLE/Kernel/ThreadManagement.h | 34 +++++- 4 files changed, 160 insertions(+), 2 deletions(-) create mode 100644 src/Core/PS4/HLE/ErrorCodes.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 95d7b1d7..d6acb9db 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,7 +34,7 @@ add_executable(shadps4 src/Core/Memory.h src/Core/PS4/Linker.cpp src/Core/PS4/Linker.h - "src/Util/Singleton.h" "src/Util/Disassembler.cpp" "src/Util/Disassembler.h" "src/Util/StringUtil.h" "src/Core/PS4/Util/aerolib.h" "src/Core/PS4/Loader/SymbolsResolver.h" "src/Core/PS4/Loader/SymbolsResolver.cpp" "src/Core/PS4/HLE/Libs.cpp" "src/Core/PS4/HLE/Libs.h" "src/Core/PS4/HLE/LibC.cpp" "src/Core/PS4/HLE/LibC.h" "src/Lib/Timer.cpp" "src/Lib/Timer.h" "src/Core/PS4/HLE/LibKernel.cpp" "src/Core/PS4/HLE/LibKernel.h" "src/Core/PS4/HLE/LibSceVideoOut.cpp" "src/Core/PS4/HLE/LibSceVideoOut.h" "src/Core/PS4/HLE/LibSceGnmDriver.cpp" "src/Core/PS4/HLE/LibSceGnmDriver.h" "src/Core/PS4/HLE/Kernel/ThreadManagement.cpp" "src/Core/PS4/HLE/Kernel/ThreadManagement.h") + "src/Util/Singleton.h" "src/Util/Disassembler.cpp" "src/Util/Disassembler.h" "src/Util/StringUtil.h" "src/Core/PS4/Util/aerolib.h" "src/Core/PS4/Loader/SymbolsResolver.h" "src/Core/PS4/Loader/SymbolsResolver.cpp" "src/Core/PS4/HLE/Libs.cpp" "src/Core/PS4/HLE/Libs.h" "src/Core/PS4/HLE/LibC.cpp" "src/Core/PS4/HLE/LibC.h" "src/Lib/Timer.cpp" "src/Lib/Timer.h" "src/Core/PS4/HLE/LibKernel.cpp" "src/Core/PS4/HLE/LibKernel.h" "src/Core/PS4/HLE/LibSceVideoOut.cpp" "src/Core/PS4/HLE/LibSceVideoOut.h" "src/Core/PS4/HLE/LibSceGnmDriver.cpp" "src/Core/PS4/HLE/LibSceGnmDriver.h" "src/Core/PS4/HLE/Kernel/ThreadManagement.cpp" "src/Core/PS4/HLE/Kernel/ThreadManagement.h" "src/Core/PS4/HLE/ErrorCodes.h") find_package(OpenGL REQUIRED) target_link_libraries(shadps4 PUBLIC fmt spdlog IMGUI SDL3-shared ${OPENGL_LIBRARY}) diff --git a/src/Core/PS4/HLE/ErrorCodes.h b/src/Core/PS4/HLE/ErrorCodes.h new file mode 100644 index 00000000..5ff20660 --- /dev/null +++ b/src/Core/PS4/HLE/ErrorCodes.h @@ -0,0 +1,5 @@ +#pragma once +constexpr int SCE_OK = 0; + +constexpr int SCE_KERNEL_ERROR_ENOMEM = 0x8002000c;//Insufficient memory +constexpr int SCE_KERNEL_ERROR_EINVAL = 0x80020016;//null or invalid states diff --git a/src/Core/PS4/HLE/Kernel/ThreadManagement.cpp b/src/Core/PS4/HLE/Kernel/ThreadManagement.cpp index 646d14a6..e6cb0fba 100644 --- a/src/Core/PS4/HLE/Kernel/ThreadManagement.cpp +++ b/src/Core/PS4/HLE/Kernel/ThreadManagement.cpp @@ -1,4 +1,125 @@ +#include "ThreadManagement.h" +#include "../ErrorCodes.h" + namespace HLE::Libs::LibKernel::ThreadManagement { +thread_local PthreadInternal* g_pthread_self = nullptr; +PThreadCxt* g_pthread_cxt = nullptr; + + + +int scePthreadAttrInit(ScePthreadAttr* attr) { + + *attr = new PthreadAttrInternal{}; + + int result = pthread_attr_init(&(*attr)->p); + + (*attr)->affinity = 0x7f; + (*attr)->guard_size = 0x1000; + + SceKernelSchedParam param{}; + param.sched_priority = 700; + + result = (result == 0 ? scePthreadAttrSetinheritsched(attr, PTHREAD_INHERIT_SCHED) : result); + result = (result == 0 ? scePthreadAttrSetschedparam(attr, ¶m) : result); + result = (result == 0 ? scePthreadAttrSetschedpolicy(attr, SCHED_OTHER) : result); + result = (result == 0 ? scePthreadAttrSetdetachstate(attr, PTHREAD_CREATE_JOINABLE) : result); + + switch (result) { + case 0: return SCE_OK; + case ENOMEM: return SCE_KERNEL_ERROR_ENOMEM; + default: return SCE_KERNEL_ERROR_EINVAL; + } +} + +int scePthreadAttrSetdetachstate(ScePthreadAttr* attr, int detachstate) { + + if (attr == nullptr || *attr == nullptr) { + return SCE_KERNEL_ERROR_EINVAL; + } + + int pstate = PTHREAD_CREATE_JOINABLE; + switch (detachstate) { + case 0: pstate = PTHREAD_CREATE_JOINABLE; break; + case 1: pstate = PTHREAD_CREATE_DETACHED; break; + default: + __debugbreak(); //unknown state + } + + int result = pthread_attr_setdetachstate(&(*attr)->p, pstate); + + (*attr)->detached = (pstate == PTHREAD_CREATE_DETACHED); + + if (result == 0) { + return SCE_OK; + } + return SCE_KERNEL_ERROR_EINVAL; +} + +int scePthreadAttrSetinheritsched(ScePthreadAttr* attr, int inheritSched) { + + if (attr == nullptr || *attr == nullptr) { + return SCE_KERNEL_ERROR_EINVAL; + } + + int pinherit_sched = PTHREAD_INHERIT_SCHED; + switch (inheritSched) { + case 0: pinherit_sched = PTHREAD_EXPLICIT_SCHED; break; + case 4: pinherit_sched = PTHREAD_INHERIT_SCHED; break; + default: __debugbreak(); // unknown inheritSched + } + + int result = pthread_attr_setinheritsched(&(*attr)->p, pinherit_sched); + + if (result == 0) { + return SCE_OK; + } + return SCE_KERNEL_ERROR_EINVAL; +} + +int scePthreadAttrSetschedparam(ScePthreadAttr* attr, const SceKernelSchedParam* param) { + + if (param == nullptr || attr == nullptr || *attr == nullptr) { + return SCE_KERNEL_ERROR_EINVAL; + } + + SceKernelSchedParam pparam{}; + if (param->sched_priority <= 478) { + pparam.sched_priority = +2; + } else if (param->sched_priority >= 733) { + pparam.sched_priority = -2; + } else { + pparam.sched_priority = 0; + } + + int result = pthread_attr_setschedparam(&(*attr)->p, &pparam); + + if (result == 0) { + return SCE_OK; + } + return SCE_KERNEL_ERROR_EINVAL; +} + +int scePthreadAttrSetschedpolicy(ScePthreadAttr* attr, int policy) { + + if (attr == nullptr || *attr == nullptr) { + return SCE_KERNEL_ERROR_EINVAL; + } + + if (policy!= SCHED_OTHER) + { + __debugbreak();//invest if policy is other and if winpthreadlibrary support it + } + + (*attr)->policy = policy; + + int result = pthread_attr_setschedpolicy(&(*attr)->p, policy); + + if (result == 0) { + return SCE_OK; + } + return SCE_KERNEL_ERROR_EINVAL; +} + }; \ No newline at end of file diff --git a/src/Core/PS4/HLE/Kernel/ThreadManagement.h b/src/Core/PS4/HLE/Kernel/ThreadManagement.h index 975aee2f..7685045e 100644 --- a/src/Core/PS4/HLE/Kernel/ThreadManagement.h +++ b/src/Core/PS4/HLE/Kernel/ThreadManagement.h @@ -1,5 +1,37 @@ #pragma once +#include +#include +#include "../../../../types.h" + +extern "C" { +struct sched_param; +} namespace HLE::Libs::LibKernel::ThreadManagement { -}; \ No newline at end of file +struct PthreadAttrInternal; + +using SceKernelSchedParam = struct sched_param; +using ScePthreadAttr = PthreadAttrInternal*; + +struct PthreadInternal { + pthread_t p; +}; +struct PthreadAttrInternal { + u64 affinity; + size_t guard_size; + int policy; + bool detached; + pthread_attr_t p; +}; + +class PThreadCxt {}; + +//HLE FUNCTIONS +int scePthreadAttrInit(ScePthreadAttr* attr); +int scePthreadAttrSetdetachstate(ScePthreadAttr* attr, int detachstate); +int scePthreadAttrSetinheritsched(ScePthreadAttr* attr, int inheritSched); +int scePthreadAttrSetschedparam(ScePthreadAttr* attr, const SceKernelSchedParam* param); +int scePthreadAttrSetschedpolicy(ScePthreadAttr* attr, int policy); + +} // namespace HLE::Libs::LibKernel::ThreadManagement \ No newline at end of file