[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] Correct system time calculation



SharedInfoGetTime uses the naive formula ((Tsc << TscShift) *
TscSystemMul) >> 32 to calculate the current system time. However, there
are two issues: firstly, TscShift may be negative (to indicate a right
shift); and secondly, multiplying the shifted Tsc with TscSystemMul will
throw away the result's upper 64 bits.

Adjust for negative shift amounts and use UnsignedMultiplyExtract128 to
correctly perform the multiplication.

Signed-off-by: Tu Dinh <ngoc-tu.dinh@xxxxxxxxxx>
---
 src/xenbus/shared_info.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/src/xenbus/shared_info.c b/src/xenbus/shared_info.c
index 06e0aec..fe29470 100644
--- a/src/xenbus/shared_info.c
+++ b/src/xenbus/shared_info.c
@@ -356,6 +356,7 @@ SharedInfoGetTime(
     ULONGLONG                       NanoSeconds;
     ULONGLONG                       Timestamp;
     ULONGLONG                       Tsc;
+    ULONGLONG                       Age;
     ULONGLONG                       SystemTime;
     ULONG                           TscSystemMul;
     CHAR                            TscShift;
@@ -404,10 +405,15 @@ SharedInfoGetTime(
     KeLowerIrql(Irql);
 
     // Number of elapsed ticks since timestamp was captured
-    Tsc -= Timestamp;
+    Age = Tsc - Timestamp;
+    if (TscShift < 0)
+        Age >>= -TscShift;
+    else
+        Age <<= TscShift;
+    Age = UnsignedMultiplyExtract128(Age, TscSystemMul, 32);
 
     // Time in nanoseconds since boot
-    SystemTime += ((Tsc << TscShift) * TscSystemMul) >> 32;
+    SystemTime += Age;
 
     Trace("WALLCLOCK TIME AT BOOT: Seconds = %llu NanoSeconds = %llu\n",
           Seconds,
-- 
2.51.0.windows.2



--
Ngoc Tu Dinh | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.