feat(libkernel): add gettime getres gettimeofday impl

This commit is contained in:
boykopovar
2026-09-13 11:21:37 +03:00
parent 4781694512
commit f19a234f38
4 changed files with 159 additions and 27 deletions
+1 -1
View File
@@ -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).
+1 -20
View File
@@ -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;
+148
View File
@@ -1,12 +1,16 @@
#include "prx/libkernel/Time/include/Time.hpp"
#include "prx/libc/include/General.hpp"
#include <cerrno>
#include <cstdint>
#include <stdexcept>
#include <string>
#ifdef _WIN32
#include <windows.h>
#else
#include <time.h>
#include <sys/time.h>
#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<std::uint64_t>(ft.dwHighDateTime) << 32) | ft.dwLowDateTime;
t -= 116444736000000000ULL;
t *= 100ULL;
tp->tv_sec = static_cast<std::int64_t>(t / 1000000000ULL);
tp->tv_nsec = static_cast<std::int64_t>(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<std::int64_t>(nanos / 1000000000ULL);
tp->tv_nsec = static_cast<std::int64_t>(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<std::int64_t>(ts.tv_sec);
tp->tv_nsec = static_cast<std::int64_t>(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<std::uint64_t>(ft.dwHighDateTime) << 32) | ft.dwLowDateTime;
t -= 116444736000000000ULL;
tv->tv_sec = static_cast<std::int64_t>(t / 10000000ULL);
tv->tv_usec = static_cast<std::int64_t>((t % 10000000ULL) / 10ULL);
#else
struct timeval native{};
if (gettimeofday(&native, nullptr) != 0) {
return -1;
}
tv->tv_sec = static_cast<std::int64_t>(native.tv_sec);
tv->tv_usec = static_cast<std::int64_t>(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<std::uint64_t>(f.QuadPart);
}();
std::uint64_t nsPerTick = (1000000000ULL + freq - 1ULL) / freq;
res->tv_sec = 0;
res->tv_nsec = static_cast<std::int64_t>(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<std::int64_t>(ts.tv_sec);
res->tv_nsec = static_cast<std::int64_t>(ts.tv_nsec);
return 0;
#endif
}
}
@@ -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);
}