amd64: add time system

Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
Michael Shaw 2026-07-21 07:39:27 -04:00
commit 36145eafbc
9 changed files with 179 additions and 2 deletions

View file

@ -2,3 +2,5 @@
obj-y += atomic.o
obj-y += spinlock.o
obj-y += proc.o
obj-y += time.o
obj-y += tsc.o

32
amd64/kernel/time.c Normal file
View file

@ -0,0 +1,32 @@
/* 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);

10
amd64/kernel/tsc.S Normal file
View file

@ -0,0 +1,10 @@
/* SPDX-License-Identifier: GPL-2.0-only */
.intel_syntax noprefix
.text
.global rdtsc
rdtsc:
rdtsc
shl rdx, 32
or rax, rdx
ret