Luna/kernel/src/sys/clock_gettime.cpp

35 lines
1.0 KiB
C++

#include "arch/Timer.h"
#include "memory/MemoryManager.h"
#include "sys/Syscall.h"
#include <bits/clockid.h>
#include <bits/timespec.h>
Result<u64> sys_clock_gettime(Registers*, SyscallArgs args)
{
clockid_t id = (clockid_t)args[0];
struct timespec* ts = (struct timespec*)args[1];
switch (id)
{
case CLOCK_MONOTONIC: {
usize ticks = Timer::ticks_ns();
struct timespec kernel_ts;
kernel_ts.tv_sec = (time_t)(ticks / NS_PER_SECOND);
kernel_ts.tv_nsec = (long)(ticks % NS_PER_SECOND);
if (!MemoryManager::copy_to_user_typed(ts, &kernel_ts)) return err(EFAULT);
break;
}
case CLOCK_REALTIME: {
usize clock = Timer::clock_ns();
struct timespec kernel_ts;
kernel_ts.tv_sec = (time_t)(clock / NS_PER_SECOND);
kernel_ts.tv_nsec = (long)(clock % NS_PER_SECOND);
if (!MemoryManager::copy_to_user_typed(ts, &kernel_ts)) return err(EFAULT);
break;
}
default: return err(EINVAL);
}
return 0;
}