diff --git a/src/core/libraries/kernel/thread_management.cpp b/src/core/libraries/kernel/thread_management.cpp index c6f8b2766..218a743af 100644 --- a/src/core/libraries/kernel/thread_management.cpp +++ b/src/core/libraries/kernel/thread_management.cpp @@ -820,65 +820,6 @@ int PS4_SYSV_ABI posix_pthread_mutexattr_setprotocol(ScePthreadMutexattr* attr, return result; } -int PS4_SYSV_ABI sceKernelClockGettime(s32 clock_id, SceKernelTimespec* tp) { - if (tp == nullptr) { - return SCE_KERNEL_ERROR_EFAULT; - } - clockid_t pclock_id = CLOCK_REALTIME; - switch (clock_id) { - case 0: - pclock_id = CLOCK_REALTIME; - break; - case 13: - case 4: - pclock_id = CLOCK_MONOTONIC; - break; - default: - UNREACHABLE(); - } - - timespec t{}; - int result = clock_gettime(pclock_id, &t); - tp->tv_sec = t.tv_sec; - tp->tv_nsec = t.tv_nsec; - if (result == 0) { - return SCE_OK; - } - return SCE_KERNEL_ERROR_EINVAL; -} - -int PS4_SYSV_ABI clock_gettime(s32 clock_id, SceKernelTimespec* time) { - int result = sceKernelClockGettime(clock_id, time); - if (result < 0) { - UNREACHABLE(); // TODO return posix error code - } - return result; -} - -int PS4_SYSV_ABI sceKernelNanosleep(const SceKernelTimespec* rqtp, SceKernelTimespec* rmtp) { - - if (rqtp == nullptr) { - return SCE_KERNEL_ERROR_EFAULT; - } - - if (rqtp->tv_sec < 0 || rqtp->tv_nsec < 0) { - return SCE_KERNEL_ERROR_EINVAL; - } - - u64 nanos = rqtp->tv_sec * 1000000000 + rqtp->tv_nsec; - std::this_thread::sleep_for(std::chrono::nanoseconds(nanos)); - if (rmtp != nullptr) { - UNREACHABLE(); // not supported yet - } - return SCE_OK; -} -int PS4_SYSV_ABI nanosleep(const SceKernelTimespec* rqtp, SceKernelTimespec* rmtp) { - int result = sceKernelNanosleep(rqtp, rmtp); - if (result < 0) { - UNREACHABLE(); // TODO return posix error code - } - return result; -} static int pthread_copy_attributes(ScePthreadAttr* dst, const ScePthreadAttr* src) { if (dst == nullptr || *dst == nullptr || src == nullptr || *src == nullptr) { return SCE_KERNEL_ERROR_EINVAL; @@ -1288,10 +1229,6 @@ void pthreadSymbolsRegister(Core::Loader::SymbolsResolver* sym) { posix_pthread_mutexattr_setprotocol); LIB_FUNCTION("HF7lK46xzjY", "libScePosix", 1, "libkernel", 1, 1, posix_pthread_mutexattr_destroy); - LIB_FUNCTION("QBi7HCK03hw", "libkernel", 1, "libkernel", 1, 1, sceKernelClockGettime); - LIB_FUNCTION("lLMT9vJAck0", "libkernel", 1, "libkernel", 1, 1, clock_gettime); - LIB_FUNCTION("lLMT9vJAck0", "libScePosix", 1, "libkernel", 1, 1, clock_gettime); - LIB_FUNCTION("yS8U2TGCe1A", "libScePosix", 1, "libkernel", 1, 1, nanosleep); // openorbis weird functions LIB_FUNCTION("7H0iTOciTLo", "libkernel", 1, "libkernel", 1, 1, posix_pthread_mutex_lock); diff --git a/src/core/libraries/kernel/thread_management.h b/src/core/libraries/kernel/thread_management.h index ab97d0a47..a97def169 100644 --- a/src/core/libraries/kernel/thread_management.h +++ b/src/core/libraries/kernel/thread_management.h @@ -41,11 +41,6 @@ using OrbisPthreadRwlockattr = PthreadRwLockAttrInernal*; using pthreadEntryFunc = PS4_SYSV_ABI void* (*)(void*); -struct SceKernelTimespec { - int64_t tv_sec; - int64_t tv_nsec; -}; - struct PthreadInternal { u8 reserved[4096]; std::string name; diff --git a/src/core/libraries/kernel/time_management.cpp b/src/core/libraries/kernel/time_management.cpp index 1b33c59ac..f5f7ab705 100644 --- a/src/core/libraries/kernel/time_management.cpp +++ b/src/core/libraries/kernel/time_management.cpp @@ -51,8 +51,57 @@ u32 PS4_SYSV_ABI sceKernelSleep(u32 seconds) { return 0; } -int PS4_SYSV_ABI posix_nanosleep(timespec* requested_time, timespec* remaining) { - return nanosleep(requested_time, remaining); +int PS4_SYSV_ABI sceKernelClockGettime(s32 clock_id, OrbisKernelTimespec* tp) { + if (tp == nullptr) { + return SCE_KERNEL_ERROR_EFAULT; + } + clockid_t pclock_id = CLOCK_REALTIME; + switch (clock_id) { + case 0: + pclock_id = CLOCK_REALTIME; + break; + case 13: + case 4: + pclock_id = CLOCK_MONOTONIC; + break; + default: + UNREACHABLE(); + } + + timespec t{}; + int result = clock_gettime(pclock_id, &t); + tp->tv_sec = t.tv_sec; + tp->tv_nsec = t.tv_nsec; + if (result == 0) { + return SCE_OK; + } + return SCE_KERNEL_ERROR_EINVAL; +} + +int PS4_SYSV_ABI clock_gettime(s32 clock_id, OrbisKernelTimespec* time) { + int result = sceKernelClockGettime(clock_id, time); + if (result < 0) { + UNREACHABLE(); // TODO return posix error code + } + return result; +} + +int PS4_SYSV_ABI posix_nanosleep(const OrbisKernelTimespec* rqtp, OrbisKernelTimespec* rmtp) { + const auto* request = reinterpret_cast(rqtp); + auto* remain = reinterpret_cast(rmtp); + return nanosleep(request, remain); +} + +int PS4_SYSV_ABI sceKernelNanosleep(const OrbisKernelTimespec* rqtp, OrbisKernelTimespec* rmtp) { + if (!rqtp || !rmtp) { + return SCE_KERNEL_ERROR_EFAULT; + } + + if (rqtp->tv_sec < 0 || rqtp->tv_nsec < 0) { + return SCE_KERNEL_ERROR_EINVAL; + } + + return posix_nanosleep(rqtp, rmtp); } int PS4_SYSV_ABI sceKernelGettimeofday(OrbisKernelTimeval* tp) { @@ -97,6 +146,9 @@ void timeSymbolsRegister(Core::Loader::SymbolsResolver* sym) { LIB_FUNCTION("-ZR+hG7aDHw", "libkernel", 1, "libkernel", 1, 1, sceKernelSleep); LIB_FUNCTION("0wu33hunNdE", "libScePosix", 1, "libkernel", 1, 1, sceKernelSleep); LIB_FUNCTION("yS8U2TGCe1A", "libkernel", 1, "libkernel", 1, 1, posix_nanosleep); + LIB_FUNCTION("QBi7HCK03hw", "libkernel", 1, "libkernel", 1, 1, sceKernelClockGettime); + LIB_FUNCTION("lLMT9vJAck0", "libkernel", 1, "libkernel", 1, 1, clock_gettime); + LIB_FUNCTION("lLMT9vJAck0", "libScePosix", 1, "libkernel", 1, 1, clock_gettime); } } // namespace Libraries::Kernel diff --git a/src/core/libraries/kernel/time_management.h b/src/core/libraries/kernel/time_management.h index dd395e7e7..64689bf67 100644 --- a/src/core/libraries/kernel/time_management.h +++ b/src/core/libraries/kernel/time_management.h @@ -12,8 +12,8 @@ class SymbolsResolver; namespace Libraries::Kernel { struct OrbisKernelTimeval { - u64 tv_sec; - u64 tv_usec; + s64 tv_sec; + s64 tv_usec; }; struct OrbisKernelTimezone { @@ -21,6 +21,11 @@ struct OrbisKernelTimezone { s32 tz_dsttime; }; +struct OrbisKernelTimespec { + s64 tv_sec; + s64 tv_nsec; +}; + u64 PS4_SYSV_ABI sceKernelGetTscFrequency(); u64 PS4_SYSV_ABI sceKernelGetProcessTime(); u64 PS4_SYSV_ABI sceKernelGetProcessTimeCounter();