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

Re: [PATCH] Correct system time calculation


  • To: Tu Dinh <ngoc-tu.dinh@xxxxxxxxxx>, "win-pv-devel@xxxxxxxxxxxxxxxxxxxx" <win-pv-devel@xxxxxxxxxxxxxxxxxxxx>
  • From: Owen Smith <owen.smith@xxxxxxxxxx>
  • Date: Tue, 11 Nov 2025 08:53:55 +0000
  • Accept-language: en-GB, en-US
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=citrix.com; dmarc=pass action=none header.from=citrix.com; dkim=pass header.d=citrix.com; arc=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=RGmDg7ne1fBocdhPdKEj0G8RKnIjbYW2UqawBGDbl4o=; b=rR+ni/972mxjipxAdlUdfPhFlcnBVCzSjgKD+kZVHmmAQx1ESHlph45GG0t/DDA3gqIHjDc+DWQLtqlgBT/iNQ9MUunh2jFEQRoziiEtnSeb0/hQOX2OkA36eXF05rVDjIlVX8awbL0RdLAIpPvq3HStkampH8zq+n+ugoBd1Gk9HL+mR0wFvE/LAuTwkRmS+jd38sM2QaEouMsfYKK0YOZeFYrU4A3vYY8zzJRQXZZhUd0k33rXNUlyeT9lz54OHQ4EcIVmqWYquy0fRgVlCHxKZUSbWN2I1Nb0up6P/KXKgtI9FoqQNNByZmF+wc9VwE3PmmIFhl0EuQfWdlyjgA==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=bQ/EFYtt6s1+3WnaeJMjJv8HqlaARddsLFPy/l6J/wWdutaDnwKSFhBvvb1awpRJ+/q5FZJ5XoobHrw9ouz7vHQbZjBDn/DQ4jahff8hpQtcRBbmbfImMAzmCnYfbI9I/q5CBtBAGRPnzSaHmdI5dnBp/Obhg4cJmqtQbieJoViitml2Peb++uMfvga2wagDfTTb8It/flMR309uY9oJER6pT3Fz7FQHxOjBn4Y6oil7ui3MJxVgFikd8w5DBeDfiblitzxgu0/ghVV8vLXJopB2TMHHISmgp0f5B9cFdG0SKfXgH5TOg/2H/OGM9cMEoDYkmInsvtI2PYA/6JN0eA==
  • Authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=citrix.com;
  • Delivery-date: Tue, 11 Nov 2025 08:54:04 +0000
  • List-id: Developer list for the Windows PV Drivers subproject <win-pv-devel.lists.xenproject.org>
  • Msip_labels:
  • Thread-index: AQHcUnYx6BC9hGrxbUKAeTuwcG9sPLTtKyAh
  • Thread-topic: [PATCH] Correct system time calculation

Interestingly, xen.h defines the time calculation in a comment, but does not 
account for tsc_shift being a signed int8_t value.

https://xenbits.xen.org/gitweb/?p=xen.git;a=blob;f=xen/include/public/xen.h;h=82b9c05a76b7faedded8778fb8274a0d3d5d31e4;hb=HEAD#l677

Reviewed-by: Owen Smith <owen.smith@xxxxxxxxxx>

________________________________________
From: win-pv-devel <win-pv-devel-bounces@xxxxxxxxxxxxxxxxxxxx> on behalf of Tu 
Dinh <ngoc-tu.dinh@xxxxxxxxxx>
Sent: 10 November 2025 7:13 PM
To: win-pv-devel@xxxxxxxxxxxxxxxxxxxx
Cc: Tu Dinh
Subject: [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®.