amd64: add time system
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
parent
bb71cfd98f
commit
36145eafbc
9 changed files with 179 additions and 2 deletions
|
|
@ -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
32
amd64/kernel/time.c
Normal 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
10
amd64/kernel/tsc.S
Normal 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
|
||||
Loading…
Reference in a new issue