Luna/kernel/src/sys/clock_gettime.cpp
apio c4d2847da1
All checks were successful
continuous-integration/drone/push Build is passing
kernel: Rework the entire time system to use modular clocks
2023-11-15 23:50:04 +01:00

39 lines
832 B
C++

#include "Pledge.h"
#include "memory/MemoryManager.h"
#include "sys/Syscall.h"
#include "thread/Clock.h"
#include "thread/Scheduler.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];
auto* current = Scheduler::current();
TRY(check_pledge(current, Promise::p_stdio));
Clock* clock;
switch (id)
{
case CLOCK_MONOTONIC: {
clock = &g_monotonic_clock;
break;
}
case CLOCK_REALTIME: {
clock = &g_realtime_clock;
break;
}
default: return err(EINVAL);
}
struct timespec time;
clock->get_time(time);
if (!MemoryManager::copy_to_user_typed(ts, &time)) return err(EFAULT);
return 0;
}