From 89e5c998288f537c7b1f7d62515802a0578a5a2e Mon Sep 17 00:00:00 2001 From: Matheus Date: Mon, 17 Oct 2022 11:08:22 -0300 Subject: [PATCH] timekeeper: initial implementation --- src/arch/x86_64/include/arch/timekeeper.h | 11 ++++++ src/arch/x86_64/main.c | 6 ++- src/arch/x86_64/timekeeper.c | 47 +++++++++++++++++++++++ 3 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 src/arch/x86_64/include/arch/timekeeper.h create mode 100644 src/arch/x86_64/timekeeper.c diff --git a/src/arch/x86_64/include/arch/timekeeper.h b/src/arch/x86_64/include/arch/timekeeper.h new file mode 100644 index 00000000..967fc24a --- /dev/null +++ b/src/arch/x86_64/include/arch/timekeeper.h @@ -0,0 +1,11 @@ +#ifndef _TIMEKEEPER_H_INCLUDE +#define _TIMEKEEPER_H_INCLUDE + +#include + +struct timespec arch_timekeeper_gettime(); +struct timespec arch_timekeeper_gettimefromboot(); + +void arch_timekeeper_init(); + +#endif diff --git a/src/arch/x86_64/main.c b/src/arch/x86_64/main.c index bc0848d1..a164cd57 100644 --- a/src/arch/x86_64/main.c +++ b/src/arch/x86_64/main.c @@ -18,6 +18,7 @@ #include #include #include +#include void kmain(){ @@ -29,8 +30,7 @@ void kmain(){ idt_bspinit(); - pmm_init(); - + pmm_init(); arch_mmu_init(); @@ -48,6 +48,8 @@ void kmain(){ hpet_init(); + arch_timekeeper_init(); + timer_init(); vfs_init(); diff --git a/src/arch/x86_64/timekeeper.c b/src/arch/x86_64/timekeeper.c new file mode 100644 index 00000000..e1cb9d66 --- /dev/null +++ b/src/arch/x86_64/timekeeper.c @@ -0,0 +1,47 @@ +#include +#include +#include +#include + +static time_t bootunixtime; +static time_t timerticksperus; +static time_t timerusatboot; + +static volatile struct limine_boot_time_request lim_boottime = { + .id = LIMINE_BOOT_TIME_REQUEST, + .revision = 0, +}; + +struct timespec arch_timekeeper_gettimefromboot(){ + struct timespec time; + + time_t timercurrentus = hpet_get_counter() / timerticksperus - timerusatboot; + + time.tv_sec = timercurrentus / US_IN_SECS; + time.tv_nsec = (timercurrentus % US_IN_SECS) * NS_IN_US; + + return time; + +} + +struct timespec arch_timekeeper_gettime(){ + struct timespec time; + struct timespec timefromboot = arch_timekeeper_gettimefromboot(); + time.tv_sec = bootunixtime + timefromboot.tv_sec; + time.tv_nsec = timefromboot.tv_nsec; + + return time; + +} + +void arch_timekeeper_init(){ + + if(!lim_boottime.response) + _panic("No limine boot time response\n", 0); + + bootunixtime = lim_boottime.response->boot_time; + + timerticksperus = hpet_get_ticksperus(); + + timerusatboot = hpet_get_counter() / timerticksperus; +}