From f19a234f38f3e75b6fc558a4d103485372eccd12 Mon Sep 17 00:00:00 2001 From: boykopovar Date: Sun, 13 Sep 2026 11:21:37 +0300 Subject: [PATCH] feat(libkernel): add gettime getres gettimeofday impl --- README.md | 2 +- core/libs/prx/libkernel/Export.cpp | 21 +-- core/libs/prx/libkernel/Time/Time.cpp | 148 ++++++++++++++++++ core/libs/prx/libkernel/Time/include/Time.hpp | 15 +- 4 files changed, 159 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index d3b80bdc3..49968c7a0 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Execution reaches `_start`, stack unwinding and exception handling tables are bu Shader initialization via `sceAgcCreate*` passes. Audio output and video output initialization pass -Now: `sceNpTrophy2RegisterUnlockCallback not implemented`. +Now: `sceAgcDcbResetQueue not implemented`. On Windows, after printing an unhandled exception the process exits with STATUS_STACK_BUFFER_OVERRUN due to the difficulty of manually [implementing proper exception handling](core/libs/prx/libc/src/exception). diff --git a/core/libs/prx/libkernel/Export.cpp b/core/libs/prx/libkernel/Export.cpp index baf0bb71f..1b6db2dc4 100644 --- a/core/libs/prx/libkernel/Export.cpp +++ b/core/libs/prx/libkernel/Export.cpp @@ -7,6 +7,7 @@ #include "Equeue/Equeue.hpp" #include "prx/libkernel/File/include/File.hpp" #include "prx/libkernel/Semaphore/include/Semaphore.hpp" +#include "prx/libkernel/Time/include/Time.hpp" extern "C" { @@ -33,19 +34,6 @@ int APS5_VABI chmod_nid_postfix(const char* path, int mode) { return 0; } -int APS5_VABI clock_getres_nid_postfix(int clock_id, KernelTimespec* res) { - (void)clock_id; - (void)res; - NotImplemented_nid_no_patch(__func__); - return 0; -} - -int APS5_VABI clock_gettime_nid_postfix(int clock_id, KernelTimespec* time) { - (void)clock_id; - (void)time; - NotImplemented_nid_no_patch(__func__); - return 0; -} int APS5_VABI close_nid_postfix(int d) { (void)d; @@ -125,13 +113,6 @@ int APS5_VABI getsockopt_nid_postfix(int s, int level, int optname, void* optval return 0; } -int APS5_VABI gettimeofday_nid_postfix(KernelTimeval* time, KernelTimezone* timezone) { - (void)time; - (void)timezone; - NotImplemented_nid_no_patch(__func__); - return 0; -} - const char* APS5_VABI inet_ntop_nid_postfix(int af, const void* src, char* dst, uint32_t size) { (void)af; (void)src; diff --git a/core/libs/prx/libkernel/Time/Time.cpp b/core/libs/prx/libkernel/Time/Time.cpp index 100825e08..96ae9f02a 100644 --- a/core/libs/prx/libkernel/Time/Time.cpp +++ b/core/libs/prx/libkernel/Time/Time.cpp @@ -1,12 +1,16 @@ #include "prx/libkernel/Time/include/Time.hpp" +#include "prx/libc/include/General.hpp" #include #include +#include +#include #ifdef _WIN32 #include #else #include +#include #endif static std::uint64_t GetMonotonicNanos() { @@ -88,4 +92,148 @@ int APS5_VABI nanosleep_nid_postfix(const KernelTimespec* rqtp, KernelTimespec* return sceKernelNanosleep(rqtp, rmtp); } +int APS5_VABI clock_gettime_nid_postfix(int clockId, KernelTimespec* tp) { + if (tp == nullptr) { + APS5_INVALID_ARG_EX; + } +#ifdef _WIN32 + if (clockId == 0 || clockId == 9 || clockId == 10) { + FILETIME ft{}; + GetSystemTimePreciseAsFileTime(&ft); + std::uint64_t t = (static_cast(ft.dwHighDateTime) << 32) | ft.dwLowDateTime; + t -= 116444736000000000ULL; + t *= 100ULL; + tp->tv_sec = static_cast(t / 1000000000ULL); + tp->tv_nsec = static_cast(t % 1000000000ULL); + return 0; + } + if (clockId == 4 || clockId == 1 || clockId == 5 || clockId == 7 || clockId == 8 || clockId == 11 || clockId == 12) { + std::uint64_t nanos = GetMonotonicNanos(); + tp->tv_sec = static_cast(nanos / 1000000000ULL); + tp->tv_nsec = static_cast(nanos % 1000000000ULL); + return 0; + } + throw std::runtime_error(std::string(__func__) + ": unsupported clock_id " + std::to_string(clockId)); +#else + clockid_t nativeId; + switch (clockId) { + case 0: + case 9: + case 10: + nativeId = CLOCK_REALTIME; + break; + case 4: + case 7: + case 11: + nativeId = CLOCK_MONOTONIC; + break; + case 5: + case 8: + case 12: + nativeId = CLOCK_MONOTONIC; + break; + case 14: + nativeId = CLOCK_THREAD_CPUTIME_ID; + break; + case 15: + nativeId = CLOCK_PROCESS_CPUTIME_ID; + break; + default: + throw std::runtime_error(std::string(__func__) + ": unsupported clock_id " + std::to_string(clockId)); + } + struct timespec ts{}; + if (clock_gettime(nativeId, &ts) != 0) { + return -1; + } + tp->tv_sec = static_cast(ts.tv_sec); + tp->tv_nsec = static_cast(ts.tv_nsec); + return 0; +#endif +} + +int APS5_VABI gettimeofday_nid_postfix(KernelTimeval* tv, KernelTimezone* tz) { + if (tv == nullptr) { + APS5_INVALID_ARG_EX; + } +#ifdef _WIN32 + FILETIME ft{}; + GetSystemTimePreciseAsFileTime(&ft); + std::uint64_t t = (static_cast(ft.dwHighDateTime) << 32) | ft.dwLowDateTime; + t -= 116444736000000000ULL; + tv->tv_sec = static_cast(t / 10000000ULL); + tv->tv_usec = static_cast((t % 10000000ULL) / 10ULL); +#else + struct timeval native{}; + if (gettimeofday(&native, nullptr) != 0) { + return -1; + } + tv->tv_sec = static_cast(native.tv_sec); + tv->tv_usec = static_cast(native.tv_usec); +#endif + if (tz != nullptr) { + tz->tz_minuteswest = 0; + tz->tz_dsttime = 0; + } + return 0; +} + +int APS5_VABI clock_getres_nid_postfix(int clockId, KernelTimespec* res) { + if (res == nullptr) { + APS5_INVALID_ARG_EX; + } +#ifdef _WIN32 + if (clockId == 0 || clockId == 9) { + res->tv_sec = 0; + res->tv_nsec = 100LL; + return 0; + } + if (clockId == 4 || clockId == 1 || clockId == 5 || clockId == 7 || clockId == 8 || clockId == 10 || clockId == 11 || clockId == 12) { + static const std::uint64_t freq = [] { + LARGE_INTEGER f{}; + QueryPerformanceFrequency(&f); + return static_cast(f.QuadPart); + }(); + std::uint64_t nsPerTick = (1000000000ULL + freq - 1ULL) / freq; + res->tv_sec = 0; + res->tv_nsec = static_cast(nsPerTick); + return 0; + } + throw std::runtime_error(std::string(__func__) + ": unsupported clock_id " + std::to_string(clockId)); +#else + clockid_t nativeId; + switch (clockId) { + case 0: + case 9: + case 10: + nativeId = CLOCK_REALTIME; + break; + case 4: + case 7: + case 11: + nativeId = CLOCK_MONOTONIC; + break; + case 5: + case 8: + case 12: + nativeId = CLOCK_MONOTONIC; + break; + case 14: + nativeId = CLOCK_THREAD_CPUTIME_ID; + break; + case 15: + nativeId = CLOCK_PROCESS_CPUTIME_ID; + break; + default: + throw std::runtime_error(std::string(__func__) + ": unsupported clock_id " + std::to_string(clockId)); + } + struct timespec ts{}; + if (clock_getres(nativeId, &ts) != 0) { + return -1; + } + res->tv_sec = static_cast(ts.tv_sec); + res->tv_nsec = static_cast(ts.tv_nsec); + return 0; +#endif +} + } diff --git a/core/libs/prx/libkernel/Time/include/Time.hpp b/core/libs/prx/libkernel/Time/include/Time.hpp index e4d93914b..71427c05d 100644 --- a/core/libs/prx/libkernel/Time/include/Time.hpp +++ b/core/libs/prx/libkernel/Time/include/Time.hpp @@ -7,12 +7,15 @@ extern "C" { -std::uint64_t APS5_VABI sceKernelGetProcessTime(); -std::uint64_t APS5_VABI sceKernelGetProcessTimeCounter(); -std::uint64_t APS5_VABI sceKernelGetProcessTimeCounterFrequency(); -int APS5_VABI sceKernelUsleep_nid_postfix(KernelUseconds microseconds); -int APS5_VABI sceKernelNanosleep(const KernelTimespec* rqtp, KernelTimespec* rmtp); -int APS5_VABI nanosleep_nid_postfix(const KernelTimespec* rqtp, KernelTimespec* rmtp); + std::uint64_t APS5_VABI sceKernelGetProcessTime(); + std::uint64_t APS5_VABI sceKernelGetProcessTimeCounter(); + std::uint64_t APS5_VABI sceKernelGetProcessTimeCounterFrequency(); + int APS5_VABI sceKernelUsleep_nid_postfix(KernelUseconds microseconds); + int APS5_VABI sceKernelNanosleep(const KernelTimespec* rqtp, KernelTimespec* rmtp); + int APS5_VABI nanosleep_nid_postfix(const KernelTimespec* rqtp, KernelTimespec* rmtp); + int APS5_VABI clock_gettime_nid_postfix(int clockId, KernelTimespec* tp); + int APS5_VABI clock_getres_nid_postfix(int clockId, KernelTimespec* res); + int APS5_VABI gettimeofday_nid_postfix(KernelTimeval* tv, KernelTimezone* tz); }