X-Git-Url: http://v3vee.org/palacios/gitweb/gitweb.cgi?a=blobdiff_plain;f=kitten%2Fkernel%2Ftime.c;fp=kitten%2Fkernel%2Ftime.c;h=1c5b7e70b0c556271f630674881b557a87b965c0;hb=66a1a4c7a9edcd7d8bc207aca093d694a6e6b5b2;hp=0000000000000000000000000000000000000000;hpb=f7cf9c19ecb0a589dd45ae0d2c91814bd3c2acc2;p=palacios-OLD.git diff --git a/kitten/kernel/time.c b/kitten/kernel/time.c new file mode 100644 index 0000000..1c5b7e7 --- /dev/null +++ b/kitten/kernel/time.c @@ -0,0 +1,62 @@ +#include +#include + +static uint64_t shift; +static uint64_t mult; +static uint64_t offset; + +/** + * Converts the input khz cycle counter frequency to a time source multiplier. + * The multiplier is used to convert cycle counts to nanoseconds. + */ +void +init_cycles2ns(uint32_t khz) +{ + /* + * Shift is used to obtain greater precision. + * Linux uses 22 for the x86 time stamp counter. + * For now we assume this will work for most cases. + */ + shift = 22; + + /* + * khz = cyc/(Million ns) + * mult/2^shift = ns/cyc + * mult = ns/cyc * 2^shift + * mult = 1Million/khz * 2^shift + * mult = 1000000 * 2^shift / khz + * mult = (1000000<> shift; +} + +/** + * Returns the current time in nanoseconds. + */ +uint64_t +get_time(void) +{ + return cycles2ns(get_cycles()) + offset; +} + +/** + * Sets the current time in nanoseconds. + */ +void +set_time(uint64_t ns) +{ + offset = ns - cycles2ns(get_cycles()); +} +