improved mutex creation

This commit is contained in:
georgemoralis 2023-11-11 12:13:43 +02:00
parent 402ee7d006
commit 7f0cf8784b
3 changed files with 30 additions and 18 deletions

View File

@ -1,15 +1,16 @@
#include "common/log.h" #include "core/hle/libraries/libkernel/libkernel.h"
#include "common/debug.h" #include "common/debug.h"
#include "common/log.h"
#include "common/singleton.h" #include "common/singleton.h"
#include "core/loader/elf.h"
#include "core/hle/kernel/Objects/physical_memory.h" #include "core/hle/kernel/Objects/physical_memory.h"
#include "core/hle/kernel/cpu_management.h" #include "core/hle/kernel/cpu_management.h"
#include "core/hle/kernel/event_queues.h" #include "core/hle/kernel/event_queues.h"
#include "core/hle/kernel/memory_management.h" #include "core/hle/kernel/memory_management.h"
#include "core/hle/libraries/libkernel/libkernel.h"
#include "core/hle/libraries/libkernel/file_system.h" #include "core/hle/libraries/libkernel/file_system.h"
#include "core/hle/libraries/libkernel/time_management.h" #include "core/hle/libraries/libkernel/time_management.h"
#include "core/hle/libraries/libs.h" #include "core/hle/libraries/libs.h"
#include "core/loader/elf.h"
#ifdef _WIN64 #ifdef _WIN64
#include <windows.h> #include <windows.h>
@ -25,13 +26,12 @@ int32_t PS4_SYSV_ABI sceKernelReleaseDirectMemory(off_t start, size_t len) {
return 0; return 0;
} }
static PS4_SYSV_ABI void stack_chk_fail() { static PS4_SYSV_ABI void stack_chk_fail() { BREAKPOINT(); }
BREAKPOINT();
}
int PS4_SYSV_ABI sceKernelMunmap(void* addr, size_t len) { int PS4_SYSV_ABI sceKernelMunmap(void* addr, size_t len) { BREAKPOINT(); }
BREAKPOINT();
} static thread_local int libc_error;
int* PS4_SYSV_ABI __Error() { return &libc_error; }
void LibKernel_Register(Loader::SymbolsResolver* sym) { void LibKernel_Register(Loader::SymbolsResolver* sym) {
// obj // obj
@ -48,6 +48,7 @@ void LibKernel_Register(Loader::SymbolsResolver* sym) {
// misc // misc
LIB_FUNCTION("WslcK1FQcGI", "libkernel", 1, "libkernel", 1, 1, Kernel::sceKernelIsNeoMode); LIB_FUNCTION("WslcK1FQcGI", "libkernel", 1, "libkernel", 1, 1, Kernel::sceKernelIsNeoMode);
LIB_FUNCTION("Ou3iL1abvng", "libkernel", 1, "libkernel", 1, 1, stack_chk_fail); LIB_FUNCTION("Ou3iL1abvng", "libkernel", 1, "libkernel", 1, 1, stack_chk_fail);
LIB_FUNCTION("9BcDykPmo1I", "libkernel", 1, "libkernel", 1, 1, __Error);
Core::Libraries::LibKernel::fileSystemSymbolsRegister(sym); Core::Libraries::LibKernel::fileSystemSymbolsRegister(sym);
Core::Libraries::LibKernel::timeSymbolsRegister(sym); Core::Libraries::LibKernel::timeSymbolsRegister(sym);

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <sys/types.h> #include <sys/types.h>
#include "common/types.h" #include "common/types.h"
namespace Core::Loader { namespace Core::Loader {
@ -10,6 +11,7 @@ class SymbolsResolver;
namespace Core::Libraries::LibKernel { namespace Core::Libraries::LibKernel {
int32_t PS4_SYSV_ABI sceKernelReleaseDirectMemory(off_t start, size_t len); int32_t PS4_SYSV_ABI sceKernelReleaseDirectMemory(off_t start, size_t len);
int* PS4_SYSV_ABI __Error();
void LibKernel_Register(Loader::SymbolsResolver* sym); void LibKernel_Register(Loader::SymbolsResolver* sym);

View File

@ -151,6 +151,18 @@ int PS4_SYSV_ABI scePthreadMutexInit(ScePthreadMutex* mutex, const ScePthreadMut
default: return SCE_KERNEL_ERROR_EINVAL; default: return SCE_KERNEL_ERROR_EINVAL;
} }
} }
void* createMutex(void* addr) {
if (addr == nullptr || *static_cast<void**>(addr) != nullptr) {
return addr;
}
auto vaddr = reinterpret_cast<u64>(addr);
std::string name = fmt::format("mutex{:#x}",vaddr);
scePthreadMutexInit(static_cast<ScePthreadMutex*>(addr), nullptr, name.c_str());
return addr;
}
int PS4_SYSV_ABI scePthreadMutexattrInit(ScePthreadMutexattr* attr) { int PS4_SYSV_ABI scePthreadMutexattrInit(ScePthreadMutexattr* attr) {
*attr = new PthreadMutexattrInternal{}; *attr = new PthreadMutexattrInternal{};
@ -197,9 +209,7 @@ int PS4_SYSV_ABI scePthreadMutexattrSetprotocol(ScePthreadMutexattr* attr, int p
} }
int PS4_SYSV_ABI scePthreadMutexLock(ScePthreadMutex* mutex) { int PS4_SYSV_ABI scePthreadMutexLock(ScePthreadMutex* mutex) {
PRINT_FUNCTION_NAME(); PRINT_FUNCTION_NAME();
if (mutex != nullptr || *static_cast<void**>((void*)mutex) == nullptr) { mutex = static_cast<ScePthreadMutex*>(createMutex(mutex));
scePthreadMutexInit(mutex, nullptr, "nomame"); // init mutex if it doesn't exist
}
if (mutex == nullptr) { if (mutex == nullptr) {
return SCE_KERNEL_ERROR_EINVAL; return SCE_KERNEL_ERROR_EINVAL;
@ -217,9 +227,7 @@ int PS4_SYSV_ABI scePthreadMutexLock(ScePthreadMutex* mutex) {
} }
int PS4_SYSV_ABI scePthreadMutexUnlock(ScePthreadMutex* mutex) { int PS4_SYSV_ABI scePthreadMutexUnlock(ScePthreadMutex* mutex) {
PRINT_FUNCTION_NAME(); PRINT_FUNCTION_NAME();
if (mutex != nullptr || *static_cast<void**>((void*)mutex) == nullptr) { mutex = static_cast<ScePthreadMutex*>(createMutex(mutex));
scePthreadMutexInit(mutex, nullptr, "nomame"); // init mutex if it doesn't exist this probably won't need
}
if (mutex == nullptr) { if (mutex == nullptr) {
return SCE_KERNEL_ERROR_EINVAL; return SCE_KERNEL_ERROR_EINVAL;
} }
@ -284,6 +292,7 @@ void pthreadSymbolsRegister(Loader::SymbolsResolver* sym) {
// openorbis weird functions // openorbis weird functions
LIB_FUNCTION("7H0iTOciTLo", "libkernel", 1, "libkernel", 1, 1, posix_pthread_mutex_lock); LIB_FUNCTION("7H0iTOciTLo", "libkernel", 1, "libkernel", 1, 1, posix_pthread_mutex_lock);
LIB_FUNCTION("2Z+PpY6CaJg", "libkernel", 1, "libkernel", 1, 1, posix_pthread_mutex_unlock);
} }
} // namespace Core::Libraries::LibKernel } // namespace Core::Libraries::LibKernel