Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


Merge branch 'devel'
[palacios.git] / kitten / kernel / time.c
diff --git a/kitten/kernel/time.c b/kitten/kernel/time.c
new file mode 100644 (file)
index 0000000..1c5b7e7
--- /dev/null
@@ -0,0 +1,62 @@
+#include <lwk/time.h>
+#include <arch/div64.h>
+
+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) / khz
+        */
+       mult = ((u64)1000000) << shift;
+       mult += khz/2; /* round for do_div */
+       do_div(mult, khz);
+}
+
+/**
+ * Converts cycles to nanoseconds.
+ * init_cycles2ns() must be called before this will work properly.
+ */
+uint64_t
+cycles2ns(uint64_t cycles)
+{
+       return (cycles * mult) >> 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());
+}
+