Kernel/LibC: add clock_gettime() for CLOCK_MONOTONIC

This gets the number of milliseconds since boot
This commit is contained in:
Bananymous 2023-07-06 00:38:29 +03:00
parent 4086d7c3be
commit 1fb305fa45
7 changed files with 40 additions and 0 deletions

View File

@ -99,6 +99,8 @@ namespace Kernel
BAN::ErrorOr<long> sys_termid(char*) const;
BAN::ErrorOr<long> sys_clock_gettime(clockid_t, timespec*) const;
TTY& tty() { ASSERT(m_tty); return *m_tty; }
static Process& current() { return Thread::current().process(); }

View File

@ -835,6 +835,24 @@ namespace Kernel
return 0;
}
BAN::ErrorOr<long> Process::sys_clock_gettime(clockid_t clock_id, timespec* tp) const
{
switch (clock_id)
{
case CLOCK_MONOTONIC:
{
uint64_t time_ms = PIT::ms_since_boot();
tp->tv_sec = time_ms / 1000;
tp->tv_nsec = (time_ms % 1000) * 1000000;
break;
}
default:
return BAN::Error::from_errno(ENOTSUP);
}
return 0;
}
BAN::ErrorOr<long> Process::sys_setuid(uid_t uid)
{
if (uid < 0 || uid >= 1'000'000'000)

View File

@ -129,6 +129,9 @@ namespace Kernel
case SYS_SET_PWD:
ret = Process::current().sys_setpwd((const char*)arg1);
break;
case SYS_CLOCK_GETTIME:
ret = Process::current().sys_clock_gettime((clockid_t)arg1, (timespec*)arg2);
break;
default:
dwarnln("Unknown syscall {}", syscall);
break;

View File

@ -14,6 +14,7 @@ set(LIBC_SOURCES
sys/stat.cpp
sys/wait.cpp
termios.cpp
time.cpp
unistd.cpp
math.S
icxxabi.cpp

View File

@ -38,6 +38,7 @@ __BEGIN_DECLS
#define SYS_GET_EGID 31
#define SYS_GET_PWD 32
#define SYS_SET_PWD 33
#define SYS_CLOCK_GETTIME 34
__END_DECLS

8
libc/time.cpp Normal file
View File

@ -0,0 +1,8 @@
#include <sys/syscall.h>
#include <time.h>
#include <unistd.h>
int clock_gettime(clockid_t clock_id, struct timespec* tp)
{
return syscall(SYS_CLOCK_GETTIME, clock_id, tp);
}

View File

@ -244,6 +244,13 @@ long syscall(long syscall, ...)
ret = Kernel::syscall(SYS_SET_PWD, (uintptr_t)path);
break;
}
case SYS_CLOCK_GETTIME:
{
clockid_t clock_id = va_arg(args, clockid_t);
timespec* tp = va_arg(args, timespec*);
ret = Kernel::syscall(SYS_CLOCK_GETTIME, clock_id, (uintptr_t)tp);
break;
}
default:
puts("LibC: Unhandeled syscall");
ret = -ENOSYS;