[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 14:10:02 +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=6cw3bfJtDC0R+BfQwjbWt3WAdiIRl6rVaL1Pclzwzww=; b=Jseol74iH9K9d+jX8JNunpKJ8h2+yoziUKJId5Ds4Wp2jjZOfHNjU0tEMrrLr+yYrgLWn5EgVZc5FeLqk+V4EqzegU4JQEh4IxuVIjxS9cHTpIheKJEGISvJ07+rk48qsldWNW3Wli8gxKtfyuvtRHp545FoJrL8cSDKc2t9YXB/GK53du+QW3bAP+vesAyOsH590IFMyRtev2wXD4fwswrlcaQBIYpe7e4BdupMngtN4ezuiMwpd9hrtt2z0uCQ1kyRjsl7ueT0WEVDaAHpAcr4l4ppGSI75Gf3t1NlQiEIDd3ZJmZI/glEDLNBU7P4JRLTeAn5RSx7shi485AFaw==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=j5LiMl+oXSxKPQedwp04xfg5kbdpjFPXDpHmBOKj2aDZTcCX2oeJ3/lVzhwtePPtDWKCsKDlofWZFvQpL6q2ZjBqgrIsWc/uAejT1mkhx97c0BtVMqTPb1cNXVpURyrHF69YChkKpDGYBbhF/lw3tsZzz+pH1v2nQeK9PoO1qiBFmX8oHX0jLVkVWMd2tFRE+apa44qWxy2YWAF5j0lp8bCgwmqTC9F5YCfNkUGJBm+9q0rJORI1eViu86sMipBNvav6Pcs2eOMJA0OJM2/JkSACLryYPKmzMQZYYvK0LGKw32eFM5qMIcmLgr6ZgWWIQ541psxu3qaQSn2HzgvvIA==
  • Authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=citrix.com;
  • Delivery-date: Tue, 11 Nov 2025 14:10:14 +0000
  • List-id: Developer list for the Windows PV Drivers subproject <win-pv-devel.lists.xenproject.org>
  • Msip_labels:
  • Thread-index: AQHcUnYx6BC9hGrxbUKAeTuwcG9sPLTtKyAhgABXCKc=
  • Thread-topic: [PATCH] Correct system time calculation

UnsignedMultiplyExtract128 is not available in the 22000 EWDK

Owen

________________________________________
From: win-pv-devel <win-pv-devel-bounces@xxxxxxxxxxxxxxxxxxxx> on behalf of 
Owen Smith <owen.smith@xxxxxxxxxx>
Sent: 11 November 2025 8:53 AM
To: Tu Dinh; win-pv-devel@xxxxxxxxxxxxxxxxxxxx
Subject: Re: [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®.