git: 26d105199ec8 - main - libc: Fall back to rdtsc when using pvclock and rdtscp is not available

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Fri, 03 Feb 2023 16:50:31 UTC
The branch main has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=26d105199ec89d6eaec8a9ca70e3b05ca65e3aac

commit 26d105199ec89d6eaec8a9ca70e3b05ca65e3aac
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2023-02-03 15:53:20 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2023-02-03 16:47:11 +0000

    libc: Fall back to rdtsc when using pvclock and rdtscp is not available
    
    In preparation for a follow-up revision wherein kvmclock may export
    timekeeping info to userspace even in the absence of AMDID_RDTSCP, fall
    back to using rdtsc when rdtscp isn't available.  This mimics
    pvclock_read_time_info() in the kernel.
    
    Reviewed by:    kib
    Tested by:      Shrikanth R Kamath <kshrikanth@juniper.net>
    MFC after:      2 weeks
    Sponsored by:   Klara, Inc.
    Sponsored by:   Juniper Networks, Inc.
    Differential Revision:  https://reviews.freebsd.org/D38341
---
 lib/libc/x86/sys/__vdso_gettc.c | 42 ++++++++++++++++++++++++++++++++++++++---
 1 file changed, 39 insertions(+), 3 deletions(-)

diff --git a/lib/libc/x86/sys/__vdso_gettc.c b/lib/libc/x86/sys/__vdso_gettc.c
index b3f3de9f4203..7159d46c0113 100644
--- a/lib/libc/x86/sys/__vdso_gettc.c
+++ b/lib/libc/x86/sys/__vdso_gettc.c
@@ -101,6 +101,13 @@ rdtsc32_mb_lfence(void)
 	return (rdtsc32());
 }
 
+static uint64_t
+rdtsc_mb_lfence(void)
+{
+	lfence();
+	return (rdtsc());
+}
+
 static u_int
 rdtsc32_mb_mfence(void)
 {
@@ -108,38 +115,62 @@ rdtsc32_mb_mfence(void)
 	return (rdtsc32());
 }
 
+static uint64_t
+rdtsc_mb_mfence(void)
+{
+	mfence();
+	return (rdtsc());
+}
+
 static u_int
 rdtsc32_mb_none(void)
 {
 	return (rdtsc32());
 }
 
+static uint64_t
+rdtsc_mb_none(void)
+{
+	return (rdtsc());
+}
+
 static u_int
 rdtscp32_(void)
 {
 	return (rdtscp32());
 }
 
+static uint64_t
+rdtscp_(void)
+{
+	return (rdtscp());
+}
+
 struct tsc_selector_tag {
 	u_int (*ts_rdtsc32)(void);
+	uint64_t (*ts_rdtsc)(void);
 	u_int (*ts_rdtsc_low)(const struct vdso_timehands *);
 };
 
 static const struct tsc_selector_tag tsc_selector[] = {
 	[0] = {				/* Intel, LFENCE */
 		.ts_rdtsc32 =	rdtsc32_mb_lfence,
+		.ts_rdtsc =	rdtsc_mb_lfence,
 		.ts_rdtsc_low =	rdtsc_low_mb_lfence,
 	},
 	[1] = {				/* AMD, MFENCE */
 		.ts_rdtsc32 =	rdtsc32_mb_mfence,
+		.ts_rdtsc =	rdtsc_mb_mfence,
 		.ts_rdtsc_low =	rdtsc_low_mb_mfence,
 	},
 	[2] = {				/* No SSE2 */
-		.ts_rdtsc32 = rdtsc32_mb_none,
-		.ts_rdtsc_low = rdtsc_low_mb_none,
+		.ts_rdtsc32 =	rdtsc32_mb_none,
+		.ts_rdtsc =	rdtsc_mb_none,
+		.ts_rdtsc_low =	rdtsc_low_mb_none,
 	},
 	[3] = {				/* RDTSCP */
 		.ts_rdtsc32 =	rdtscp32_,
+		.ts_rdtsc =	rdtscp_,
 		.ts_rdtsc_low =	rdtscp_low,
 	},
 };
@@ -193,6 +224,11 @@ DEFINE_UIFUNC(static, u_int, __vdso_gettc_rdtsc32, (void))
 	return (tsc_selector[tsc_selector_idx(cpu_feature)].ts_rdtsc32);
 }
 
+DEFINE_UIFUNC(static, uint64_t, __vdso_gettc_rdtsc, (void))
+{
+	return (tsc_selector[tsc_selector_idx(cpu_feature)].ts_rdtsc);
+}
+
 #define	HPET_DEV_MAP_MAX	10
 static volatile char *hpet_dev_map[HPET_DEV_MAP_MAX];
 
@@ -325,7 +361,7 @@ __vdso_pvclock_gettc(const struct vdso_timehands *th, u_int *tc)
 		version = atomic_load_acq_32(&ti->version);
 		stable = (ti->flags & th->th_x86_pvc_stable_mask) != 0;
 		if (stable) {
-			tsc = rdtscp();
+			tsc = __vdso_gettc_rdtsc();
 		} else {
 			(void)rdtscp_aux(&cpuid_ti);
 			ti = &pvclock_timeinfos[cpuid_ti];