2023-08-12 19:38:25 +00:00
|
|
|
#include "Pledge.h"
|
2023-01-06 23:21:08 +00:00
|
|
|
#include "memory/MemoryManager.h"
|
|
|
|
#include "sys/Syscall.h"
|
2023-11-15 22:50:04 +00:00
|
|
|
#include "thread/Clock.h"
|
2023-08-12 19:38:25 +00:00
|
|
|
#include "thread/Scheduler.h"
|
2023-01-06 23:21:08 +00:00
|
|
|
#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];
|
|
|
|
|
2023-08-12 19:38:25 +00:00
|
|
|
auto* current = Scheduler::current();
|
|
|
|
|
|
|
|
TRY(check_pledge(current, Promise::p_stdio));
|
|
|
|
|
2023-11-15 22:50:04 +00:00
|
|
|
Clock* clock;
|
|
|
|
|
2023-01-06 23:21:08 +00:00
|
|
|
switch (id)
|
|
|
|
{
|
|
|
|
case CLOCK_MONOTONIC: {
|
2023-11-15 22:50:04 +00:00
|
|
|
clock = &g_monotonic_clock;
|
2023-01-06 23:21:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CLOCK_REALTIME: {
|
2023-11-15 22:50:04 +00:00
|
|
|
clock = &g_realtime_clock;
|
2023-01-06 23:21:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: return err(EINVAL);
|
|
|
|
}
|
|
|
|
|
2023-11-15 22:50:04 +00:00
|
|
|
struct timespec time;
|
|
|
|
clock->get_time(time);
|
|
|
|
if (!MemoryManager::copy_to_user_typed(ts, &time)) return err(EFAULT);
|
|
|
|
|
2023-01-06 23:21:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|