mirror of
https://github.com/Mathewnd/Astral.git
synced 2026-10-27 12:48:22 +00:00
kernel: initial timekeeper implementation
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
#include <arch/acpi.h>
|
||||
#include <arch/apic.h>
|
||||
#include <arch/hpet.h>
|
||||
#include <kernel/timekeeper.h>
|
||||
|
||||
static cpu_t bsp_cpu;
|
||||
|
||||
@@ -30,6 +31,8 @@ void kernel_entry() {
|
||||
arch_acpi_init();
|
||||
arch_apic_init();
|
||||
arch_apic_initap();
|
||||
arch_hpet_init();
|
||||
// XXX fall back to another clock source
|
||||
time_t u = arch_hpet_init();
|
||||
timekeeper_init(arch_hpet_ticks, u);
|
||||
__assert(!"kernel entry end");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
#ifndef _TIMEKEEPER_H
|
||||
#define _TIMEKEEPER_H
|
||||
|
||||
#include <time.h>
|
||||
|
||||
void timekeeper_init(time_t (*tick)(), time_t ticksperus);
|
||||
timespec_t timekeeper_timefromboot();
|
||||
timespec_t timekeeper_time();
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,41 @@
|
||||
#include <kernel/timekeeper.h>
|
||||
#include <limine.h>
|
||||
#include <logging.h>
|
||||
|
||||
static volatile struct limine_boot_time_request timereq = {
|
||||
.id = LIMINE_BOOT_TIME_REQUEST,
|
||||
.revision = 0
|
||||
};
|
||||
|
||||
static time_t bootunix;
|
||||
static time_t ticksperus;
|
||||
static time_t (*clockticks)();
|
||||
static time_t initclockticks;
|
||||
|
||||
timespec_t timekeeper_timefromboot() {
|
||||
timespec_t ts;
|
||||
time_t ticks = clockticks() - initclockticks;
|
||||
time_t uspassed = ticks / ticksperus;
|
||||
ts.s = uspassed / 1000000;
|
||||
ts.ns = (uspassed * 1000) % 1000000000;
|
||||
return ts;
|
||||
}
|
||||
|
||||
timespec_t timekeeper_time() {
|
||||
timespec_t fromboot = timekeeper_timefromboot();
|
||||
timespec_t unix;
|
||||
unix.s = bootunix;
|
||||
unix.ns = 0;
|
||||
return timespec_add(unix, fromboot);
|
||||
}
|
||||
|
||||
// tick is a function that returns the amount of ticks since the timer's initialisation
|
||||
void timekeeper_init(time_t (*tick)(), time_t _ticksperus) {
|
||||
__assert(timereq.response);
|
||||
bootunix = timereq.response->boot_time;
|
||||
printf("timekeeper: unix time at boot: %lu\n", bootunix);
|
||||
initclockticks = tick();
|
||||
ticksperus = _ticksperus;
|
||||
clockticks = tick;
|
||||
printf("timekeeper: %lu clock ticks at init (%lu ticks per us)\n", initclockticks, ticksperus);
|
||||
}
|
||||
Reference in New Issue
Block a user