added sceMutexInit functions

This commit is contained in:
georgemoralis 2023-11-10 19:52:41 +02:00
parent 86bc2d427f
commit 91885e8a41
4 changed files with 181 additions and 30 deletions

View File

@ -14,6 +14,7 @@
#ifdef _WIN64 #ifdef _WIN64
#include <windows.h> #include <windows.h>
#endif #endif
#include "thread_management.h"
namespace Core::Libraries::LibKernel { namespace Core::Libraries::LibKernel {
@ -50,6 +51,7 @@ void LibKernel_Register(Loader::SymbolsResolver* sym) {
Core::Libraries::LibKernel::fileSystemSymbolsRegister(sym); Core::Libraries::LibKernel::fileSystemSymbolsRegister(sym);
Core::Libraries::LibKernel::timeSymbolsRegister(sym); Core::Libraries::LibKernel::timeSymbolsRegister(sym);
Core::Libraries::LibKernel::pthreadSymbolsRegister(sym);
} }
} // namespace Core::Libraries::LibKernel } // namespace Core::Libraries::LibKernel

View File

@ -1,19 +1,32 @@
#include "common/debug.h"
#include "core/hle/libraries/libkernel/thread_management.h" #include "core/hle/libraries/libkernel/thread_management.h"
#include <core/hle/libraries/libs.h>
#include "common/debug.h"
#include "common/log.h"
#include "core/hle/error_codes.h" #include "core/hle/error_codes.h"
namespace Core::Kernel { namespace Core::Libraries::LibKernel {
thread_local PthreadInternal g_pthread_self{}; thread_local PthreadInternal g_pthread_self{};
PThreadCxt* g_pthread_cxt = nullptr; PThreadCxt* g_pthread_cxt = nullptr;
constexpr bool log_pthread_file = true; // disable it to disable logging
void init_pthreads() {
g_pthread_cxt = new PThreadCxt{};
ScePthreadMutexattr default_mutexattr = nullptr;
scePthreadMutexattrInit(&default_mutexattr);
g_pthread_cxt->setDefaultMutexattr(default_mutexattr);
}
void Pthread_Init_Self_MainThread() { void Pthread_Init_Self_MainThread() {
scePthreadAttrInit(&g_pthread_self.attr); scePthreadAttrInit(&g_pthread_self.attr);
g_pthread_self.pth = pthread_self(); g_pthread_self.pth = pthread_self();
g_pthread_self.name = "Main_Thread"; g_pthread_self.name = "Main_Thread";
} }
int scePthreadAttrInit(ScePthreadAttr* attr) { int PS4_SYSV_ABI scePthreadAttrInit(ScePthreadAttr* attr) {
*attr = new PthreadAttrInternal{}; *attr = new PthreadAttrInternal{};
int result = pthread_attr_init(&(*attr)->pth_attr); int result = pthread_attr_init(&(*attr)->pth_attr);
@ -36,7 +49,7 @@ int scePthreadAttrInit(ScePthreadAttr* attr) {
} }
} }
int scePthreadAttrSetdetachstate(ScePthreadAttr* attr, int detachstate) { int PS4_SYSV_ABI scePthreadAttrSetdetachstate(ScePthreadAttr* attr, int detachstate) {
if (attr == nullptr || *attr == nullptr) { if (attr == nullptr || *attr == nullptr) {
return SCE_KERNEL_ERROR_EINVAL; return SCE_KERNEL_ERROR_EINVAL;
} }
@ -58,7 +71,7 @@ int scePthreadAttrSetdetachstate(ScePthreadAttr* attr, int detachstate) {
return SCE_KERNEL_ERROR_EINVAL; return SCE_KERNEL_ERROR_EINVAL;
} }
int scePthreadAttrSetinheritsched(ScePthreadAttr* attr, int inheritSched) { int PS4_SYSV_ABI scePthreadAttrSetinheritsched(ScePthreadAttr* attr, int inheritSched) {
if (attr == nullptr || *attr == nullptr) { if (attr == nullptr || *attr == nullptr) {
return SCE_KERNEL_ERROR_EINVAL; return SCE_KERNEL_ERROR_EINVAL;
} }
@ -78,7 +91,7 @@ int scePthreadAttrSetinheritsched(ScePthreadAttr* attr, int inheritSched) {
return SCE_KERNEL_ERROR_EINVAL; return SCE_KERNEL_ERROR_EINVAL;
} }
int scePthreadAttrSetschedparam(ScePthreadAttr* attr, const SceKernelSchedParam* param) { int PS4_SYSV_ABI scePthreadAttrSetschedparam(ScePthreadAttr* attr, const SceKernelSchedParam* param) {
if (param == nullptr || attr == nullptr || *attr == nullptr) { if (param == nullptr || attr == nullptr || *attr == nullptr) {
return SCE_KERNEL_ERROR_EINVAL; return SCE_KERNEL_ERROR_EINVAL;
} }
@ -100,7 +113,7 @@ int scePthreadAttrSetschedparam(ScePthreadAttr* attr, const SceKernelSchedParam*
return SCE_KERNEL_ERROR_EINVAL; return SCE_KERNEL_ERROR_EINVAL;
} }
int scePthreadAttrSetschedpolicy(ScePthreadAttr* attr, int policy) { int PS4_SYSV_ABI scePthreadAttrSetschedpolicy(ScePthreadAttr* attr, int policy) {
if (attr == nullptr || *attr == nullptr) { if (attr == nullptr || *attr == nullptr) {
return SCE_KERNEL_ERROR_EINVAL; return SCE_KERNEL_ERROR_EINVAL;
} }
@ -119,4 +132,98 @@ int scePthreadAttrSetschedpolicy(ScePthreadAttr* attr, int policy) {
return SCE_KERNEL_ERROR_EINVAL; return SCE_KERNEL_ERROR_EINVAL;
} }
} // namespace Core::Kernel /****
* Mutex calls
*/
int PS4_SYSV_ABI scePthreadMutexInit(ScePthreadMutex* mutex, const ScePthreadMutexattr* attr, const char* name) {
PRINT_FUNCTION_NAME();
if (mutex == nullptr) {
return SCE_KERNEL_ERROR_EINVAL;
}
if (attr == nullptr) {
attr = g_pthread_cxt->getDefaultMutexattr();
}
*mutex = new PthreadMutexInternal{};
if (name != nullptr) {
(*mutex)->name = name;
}
int result = pthread_mutex_init(&(*mutex)->pth_mutex, &(*attr)->pth_mutex_attr);
if (name != nullptr) {
LOG_INFO_IF(log_pthread_file, "mutex_init name={},result={}\n");
}
switch (result) {
case 0: return SCE_OK;
case EAGAIN: return SCE_KERNEL_ERROR_EAGAIN;
case EINVAL: return SCE_KERNEL_ERROR_EINVAL;
case ENOMEM: return SCE_KERNEL_ERROR_ENOMEM;
default: return SCE_KERNEL_ERROR_EINVAL;
}
}
int PS4_SYSV_ABI scePthreadMutexattrInit(ScePthreadMutexattr* attr) {
*attr = new PthreadMutexattrInternal{};
int result = pthread_mutexattr_init(&(*attr)->pth_mutex_attr);
result = (result == 0 ? scePthreadMutexattrSettype(attr, 1) : result);
result = (result == 0 ? scePthreadMutexattrSetprotocol(attr, 0) : result);
switch (result) {
case 0: return SCE_OK;
case ENOMEM: return SCE_KERNEL_ERROR_ENOMEM;
default: return SCE_KERNEL_ERROR_EINVAL;
}
}
int PS4_SYSV_ABI scePthreadMutexattrSettype(ScePthreadMutexattr* attr, int type) {
int ptype = PTHREAD_MUTEX_DEFAULT;
switch (type) {
case 1: ptype = PTHREAD_MUTEX_ERRORCHECK; break;
case 2: ptype = PTHREAD_MUTEX_RECURSIVE; break;
case 3:
case 4: ptype = PTHREAD_MUTEX_NORMAL; break;
default: LOG_TRACE_IF(log_pthread_file, "scePthreadMutexattrSettype invalid type: {}\n", type); std::exit(0);
}
int result = pthread_mutexattr_settype(&(*attr)->pth_mutex_attr, ptype);
return result == 0 ? SCE_OK : SCE_KERNEL_ERROR_EINVAL;
}
int PS4_SYSV_ABI scePthreadMutexattrSetprotocol(ScePthreadMutexattr* attr, int protocol) {
int pprotocol = PTHREAD_PRIO_NONE;
switch (protocol) {
case 0: pprotocol = PTHREAD_PRIO_NONE; break;
case 1: pprotocol = PTHREAD_PRIO_INHERIT; break;
case 2: pprotocol = PTHREAD_PRIO_PROTECT; break;
default: LOG_TRACE_IF(log_pthread_file, "scePthreadMutexattrSetprotocol invalid protocol: {}\n", protocol); std::exit(0);
}
int result = 0; // pthread_mutexattr_setprotocol(&(*attr)->p, pprotocol); //it appears that pprotocol has issues in winpthreads
(*attr)->pprotocol = pprotocol;
return result == 0 ? SCE_OK : SCE_KERNEL_ERROR_EINVAL;
}
int PS4_SYSV_ABI posix_pthread_mutex_init(ScePthreadMutex* mutex, const ScePthreadMutexattr* attr) {
LOG_INFO_IF(log_pthread_file, "posix pthread_mutex_init redirect to scePthreadMutexInit\n");
int result = scePthreadMutexInit(mutex, attr, nullptr);
if (result < 0) {
BREAKPOINT(); // posix calls different only for their return values
}
return result;
}
void pthreadSymbolsRegister(Loader::SymbolsResolver* sym) {
LIB_FUNCTION("cmo1RIYva9o", "libkernel", 1, "libkernel", 1, 1, scePthreadMutexInit);
LIB_FUNCTION("F8bUHwAG284", "libkernel", 1, "libkernel", 1, 1, scePthreadMutexattrInit);
LIB_FUNCTION("iMp8QpE+XO4", "libkernel", 1, "libkernel", 1, 1, scePthreadMutexattrSettype);
LIB_FUNCTION("1FGvU0i9saQ", "libkernel", 1, "libkernel", 1, 1, scePthreadMutexattrSetprotocol);
// posix calls
LIB_FUNCTION("ttHNfU+qDBU", "libScePosix", 1, "libkernel", 1, 1, posix_pthread_mutex_init);
}
} // namespace Core::Libraries::LibKernel

View File

@ -3,15 +3,25 @@
#include <pthread.h> #include <pthread.h>
#include <sched.h> #include <sched.h>
#include "common/types.h"
#include <string> #include <string>
namespace Core::Kernel { #include "common/types.h"
namespace Core::Loader {
class SymbolsResolver;
}
namespace Core::Libraries::LibKernel {
struct PthreadAttrInternal; struct PthreadAttrInternal;
struct PthreadMutexInternal;
struct PthreadMutexattrInternal;
using SceKernelSchedParam = ::sched_param; using SceKernelSchedParam = ::sched_param;
using ScePthreadAttr = PthreadAttrInternal*; using ScePthreadAttr = PthreadAttrInternal*;
using ScePthreadMutex = PthreadMutexInternal*;
using ScePthreadMutexattr = PthreadMutexattrInternal*;
struct PthreadInternal { struct PthreadInternal {
u08 reserved[4096]; u08 reserved[4096];
@ -29,14 +39,46 @@ struct PthreadAttrInternal {
pthread_attr_t pth_attr; pthread_attr_t pth_attr;
}; };
class PThreadCxt {}; struct PthreadMutexInternal {
u08 reserved[256];
std::string name;
pthread_mutex_t pth_mutex;
};
struct PthreadMutexattrInternal {
u08 reserved[64];
pthread_mutexattr_t pth_mutex_attr;
int pprotocol;
};
class PThreadCxt {
public:
ScePthreadMutexattr* getDefaultMutexattr() { return &m_default_mutexattr; }
void setDefaultMutexattr(ScePthreadMutexattr attr) { m_default_mutexattr = attr; }
private:
ScePthreadMutexattr m_default_mutexattr = nullptr;
};
void init_pthreads();
void Pthread_Init_Self_MainThread(); void Pthread_Init_Self_MainThread();
int scePthreadAttrInit(ScePthreadAttr* attr); int PS4_SYSV_ABI scePthreadAttrInit(ScePthreadAttr* attr);
int scePthreadAttrSetdetachstate(ScePthreadAttr* attr, int detachstate); int PS4_SYSV_ABI scePthreadAttrSetdetachstate(ScePthreadAttr* attr, int detachstate);
int scePthreadAttrSetinheritsched(ScePthreadAttr* attr, int inheritSched); int PS4_SYSV_ABI scePthreadAttrSetinheritsched(ScePthreadAttr* attr, int inheritSched);
int scePthreadAttrSetschedparam(ScePthreadAttr* attr, const SceKernelSchedParam* param); int PS4_SYSV_ABI scePthreadAttrSetschedparam(ScePthreadAttr* attr, const SceKernelSchedParam* param);
int scePthreadAttrSetschedpolicy(ScePthreadAttr* attr, int policy); int PS4_SYSV_ABI scePthreadAttrSetschedpolicy(ScePthreadAttr* attr, int policy);
} // namespace Core::Kernel /***
* Mutex calls
*/
int PS4_SYSV_ABI scePthreadMutexInit(ScePthreadMutex* mutex, const ScePthreadMutexattr* attr, const char* name);
int PS4_SYSV_ABI scePthreadMutexattrInit(ScePthreadMutexattr* attr);
int PS4_SYSV_ABI scePthreadMutexattrSettype(ScePthreadMutexattr* attr, int type);
int PS4_SYSV_ABI scePthreadMutexattrSetprotocol(ScePthreadMutexattr* attr, int protocol);
/****
* Posix calls
*/
int PS4_SYSV_ABI posix_pthread_mutex_init(ScePthreadMutex* mutex, const ScePthreadMutexattr* attr);
void pthreadSymbolsRegister(Loader::SymbolsResolver* sym);
} // namespace Core::Libraries::LibKernel

View File

@ -1,19 +1,22 @@
#include <cstdio>
#include <cinttypes>
#include <thread>
#include <fmt/core.h>
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include <Zydis/Zydis.h> #include <Zydis/Zydis.h>
#include <fmt/core.h>
#include <cinttypes>
#include <cstdio>
#include <thread>
#include "Util/config.h"
#include "common/discord.h" #include "common/discord.h"
#include "common/types.h"
#include "common/log.h" #include "common/log.h"
#include "common/singleton.h" #include "common/singleton.h"
#include "common/types.h"
#include "core/PS4/HLE/Graphics/video_out.h" #include "core/PS4/HLE/Graphics/video_out.h"
#include "Util/config.h"
#include "emulator.h"
#include "core/hle/libraries/libs.h" #include "core/hle/libraries/libs.h"
#include "core/linker.h" #include "core/linker.h"
#include "emuTimer.h" #include "emuTimer.h"
#include "emulator.h"
#include <core/hle/libraries/libkernel/thread_management.h>
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
if (argc == 1) { if (argc == 1) {
@ -22,6 +25,7 @@ int main(int argc, char* argv[]) {
} }
Config::load("config.toml"); Config::load("config.toml");
Common::Log::Init(true); Common::Log::Init(true);
Core::Libraries::LibKernel::init_pthreads();
auto width = Config::getScreenWidth(); auto width = Config::getScreenWidth();
auto height = Config::getScreenHeight(); auto height = Config::getScreenHeight();
Emu::emuInit(width, height); Emu::emuInit(width, height);
@ -34,11 +38,7 @@ int main(int argc, char* argv[]) {
auto linker = Common::Singleton<Core::Linker>::Instance(); auto linker = Common::Singleton<Core::Linker>::Instance();
Core::Libraries::InitHLELibs(&linker->getHLESymbols()); Core::Libraries::InitHLELibs(&linker->getHLESymbols());
linker->LoadModule(path); linker->LoadModule(path);
std::jthread mainthread( std::jthread mainthread([linker](std::stop_token stop_token, void*) { linker->Execute(); }, nullptr);
[linker](std::stop_token stop_token, void*) {
linker->Execute();
},
nullptr);
Discord::RPC discordRPC; Discord::RPC discordRPC;
discordRPC.init(); discordRPC.init();
discordRPC.update(Discord::RPCStatus::Idling, ""); discordRPC.update(Discord::RPCStatus::Idling, "");