All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 1/7] introduce time managment in xtf
@ 2018-04-09 14:35 Paul Semel
  2018-04-09 14:35 ` [PATCH v3 2/7] add current_time function to time manager Paul Semel
                   ` (6 more replies)
  0 siblings, 7 replies; 16+ messages in thread
From: Paul Semel @ 2018-04-09 14:35 UTC (permalink / raw)
  To: xen-devel; +Cc: wipawel, Paul Semel, roger.pau, andrew.cooper3

From: Paul Semel <phentex@amazon.de>

this file is introduce to be able to implement an inter domain
communication protocol over xenstore. For synchronization purpose, we do
really want to be able to "control" time

common/time.c: since_boot_time gets the time in nanoseconds from the
moment the VM has booted

Signed-off-by: Paul Semel <phentex@amazon.de>
---
 build/files.mk     |  1 +
 common/time.c      | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 include/xtf/time.h | 31 +++++++++++++++++++++
 3 files changed, 113 insertions(+)
 create mode 100644 common/time.c
 create mode 100644 include/xtf/time.h

diff --git a/build/files.mk b/build/files.mk
index 46b42d6..55ed1ca 100644
--- a/build/files.mk
+++ b/build/files.mk
@@ -16,6 +16,7 @@ obj-perarch += $(ROOT)/common/libc/vsnprintf.o
 obj-perarch += $(ROOT)/common/report.o
 obj-perarch += $(ROOT)/common/setup.o
 obj-perarch += $(ROOT)/common/xenbus.o
+obj-perarch += $(ROOT)/common/time.o
 
 obj-perenv += $(ROOT)/arch/x86/decode.o
 obj-perenv += $(ROOT)/arch/x86/desc.o
diff --git a/common/time.c b/common/time.c
new file mode 100644
index 0000000..7859c21
--- /dev/null
+++ b/common/time.c
@@ -0,0 +1,81 @@
+#include <xtf/types.h>
+#include <xtf/traps.h>
+#include <xtf/time.h>
+
+#include <arch/barrier.h>
+
+/* This function was taken from mini-os source code */
+/* It returns ((delta << shift) * mul_frac) >> 32 */
+static inline uint64_t scale_delta(uint64_t delta, uint32_t mul_frac, int shift)
+{
+    uint64_t product;
+#ifdef __i386__
+    uint32_t tmp1, tmp2;
+#endif
+
+    if ( shift < 0 )
+        delta >>= -shift;
+    else
+        delta <<= shift;
+
+#ifdef __i386__
+    __asm__ (
+            "mul  %5       ; "
+            "mov  %4,%%eax ; "
+            "mov  %%edx,%4 ; "
+            "mul  %5       ; "
+            "add  %4,%%eax ; "
+            "xor  %5,%5    ; "
+            "adc  %5,%%edx ; "
+            : "=A" (product), "=r" (tmp1), "=r" (tmp2)
+            : "a" ((uint32_t)delta), "1" ((uint32_t)(delta >> 32)), "2" (mul_frac) );
+#else
+    __asm__ (
+            "mul %%rdx ; shrd $32,%%rdx,%%rax"
+            : "=a" (product) : "0" (delta), "d" ((uint64_t)mul_frac) );
+#endif
+
+    return product;
+}
+
+
+uint64_t since_boot_time(void)
+{
+    uint64_t tsc;
+    uint32_t ver1, ver2;
+    uint64_t system_time;
+    uint64_t old_tsc;
+
+    do
+    {
+        do
+        {
+            ver1 = ACCESS_ONCE(shared_info.vcpu_info[0].time.version);
+            smp_rmb();
+        } while ( (ver1 & 1) == 1 );
+
+        system_time = ACCESS_ONCE(shared_info.vcpu_info[0].time.system_time);
+        old_tsc = ACCESS_ONCE(shared_info.vcpu_info[0].time.tsc_timestamp);
+        smp_rmb();
+        ver2 = ACCESS_ONCE(shared_info.vcpu_info[0].time.version);
+        smp_rmb();
+    } while ( ver1 != ver2 );
+
+    rdtsc(tsc);
+
+    system_time += scale_delta(tsc - old_tsc,
+                   ACCESS_ONCE(shared_info.vcpu_info[0].time.tsc_to_system_mul),
+                   ACCESS_ONCE(shared_info.vcpu_info[0].time.tsc_shift));
+
+    return system_time;
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/include/xtf/time.h b/include/xtf/time.h
new file mode 100644
index 0000000..b88da63
--- /dev/null
+++ b/include/xtf/time.h
@@ -0,0 +1,31 @@
+/**
+ * @file include/xtf/time.h
+ *
+ * Time management
+ */
+#ifndef XTF_TIME_H
+# define XTF_TIME_H
+
+#include <xtf/types.h>
+
+#define rdtsc(tsc) {\
+    uint32_t lo, hi;\
+    __asm__ volatile("rdtsc": "=a"(lo), "=d"(hi));\
+    tsc = ((uint64_t)hi << 32) | lo;\
+}
+
+
+/* Time from boot in nanoseconds */
+uint64_t since_boot_time(void);
+
+#endif /* XTF_TIME_H */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
-- 
2.16.1


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply related	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2018-04-10 10:39 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-04-09 14:35 [PATCH v3 1/7] introduce time managment in xtf Paul Semel
2018-04-09 14:35 ` [PATCH v3 2/7] add current_time function to time manager Paul Semel
2018-04-09 14:35 ` [PATCH v3 3/7] add gettimeofday function to time managment Paul Semel
2018-04-09 14:35 ` [PATCH v3 4/7] add nspin_sleep function to time manager Paul Semel
2018-04-09 14:35 ` [PATCH v3 5/7] add spin_sleep " Paul Semel
2018-04-09 14:35 ` [PATCH v3 6/7] add mspin_sleep " Paul Semel
2018-04-09 14:35 ` [PATCH v3 7/7] add sleep, msleep and NOW() macros " Paul Semel
2018-04-09 14:35 ` [PATCH v3 1/7] introduce time managment in xtf Roger Pau Monné
2018-04-09 14:45   ` Andrew Cooper
2018-04-09 15:12   ` Paul Semel
2018-04-10  8:08     ` Roger Pau Monné
2018-04-10  9:47       ` Paul Semel
2018-04-10 10:05         ` Roger Pau Monné
2018-04-10 10:32           ` Paul Semel
2018-04-10 10:36             ` Roger Pau Monné
2018-04-10 10:39               ` Paul Semel

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.