vbos/amd64/kernel/time.c
Michael Shaw 36145eafbc amd64: add time system
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-07-21 07:39:27 -04:00

32 lines
777 B
C

/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/requests.h>
#include <kernel/time.h>
#include <kernel/common.h>
#define NSEC_PER_SEC 1000000000
struct {
u64 tsc_hz;
time_t boot_tsc;
time_t boot_unix_seconds;
} timekeeper = {0};
struct timespec get_realtime()
{
u64 elapsed = rdtsc() - timekeeper.boot_tsc;
u64 sec = elapsed / timekeeper.tsc_hz;
u64 r = elapsed % timekeeper.tsc_hz;
struct timespec ret = {.tv_sec = timekeeper.boot_unix_seconds + sec, .tv_nsec = (r * NSEC_PER_SEC) / timekeeper.tsc_hz};
return ret;
}
void time_init()
{
timekeeper.tsc_hz = tsc_calibrate();
timekeeper.boot_unix_seconds = arch_get_real_time();
timekeeper.boot_tsc = rdtsc();
krdy("time system ready. tsc_hz: %llu\n", timekeeper.tsc_hz);
}
DRIVER_ENTRY(time_init);