Skip to content

Commit 6fdbfac

Browse files
Pull up following revision(s) (requested by riastradh in ticket #1170):
sys/arch/xen/xen/xen_clock.c: revision 1.22 xen: Use ordered rdtsc() function, rather than inline rdtsc asm. This will issue either lfence, mfence, or cpuid before the rdtsc instruction, depending on the CPU identification, in order to prevent reordering with respect to surrounding loads -- particularly those used to query the host's systime parameters. May help with: PR port-xen/59437: Xen time stability
1 parent 50af184 commit 6fdbfac

1 file changed

Lines changed: 7 additions & 21 deletions

File tree

sys/arch/xen/xen/xen_clock.c

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $NetBSD: xen_clock.c,v 1.8.18.2 2024/10/13 14:38:13 martin Exp $ */
1+
/* $NetBSD: xen_clock.c,v 1.8.18.3 2025/10/03 09:58:44 martin Exp $ */
22

33
/*-
44
* Copyright (c) 2017, 2018 The NetBSD Foundation, Inc.
@@ -36,7 +36,7 @@
3636
#endif
3737

3838
#include <sys/cdefs.h>
39-
__KERNEL_RCSID(0, "$NetBSD: xen_clock.c,v 1.8.18.2 2024/10/13 14:38:13 martin Exp $");
39+
__KERNEL_RCSID(0, "$NetBSD: xen_clock.c,v 1.8.18.3 2025/10/03 09:58:44 martin Exp $");
4040

4141
#include <sys/param.h>
4242
#include <sys/types.h>
@@ -114,26 +114,12 @@ static void xen_timepush_intr(void *);
114114
static int sysctl_xen_timepush(SYSCTLFN_ARGS);
115115
#endif
116116

117-
/*
118-
* xen_rdtsc()
119-
*
120-
* Read the local pCPU's tsc.
121-
*/
122-
static inline uint64_t
123-
xen_rdtsc(void)
124-
{
125-
uint32_t lo, hi;
126-
127-
asm volatile("rdtsc" : "=a"(lo), "=d"(hi));
128-
129-
return ((uint64_t)hi << 32) | lo;
130-
}
131-
132117
/*
133118
* struct xen_vcputime_ticket
134119
*
135120
* State for a vCPU read section, during which a caller may read
136-
* from fields of a struct vcpu_time_info and call xen_rdtsc.
121+
* from fields of a struct vcpu_time_info and call rdtsc.
122+
*
137123
* Caller must enter with xen_vcputime_enter, exit with
138124
* xen_vcputime_exit, and be prepared to retry if
139125
* xen_vcputime_exit fails.
@@ -256,7 +242,7 @@ xen_vcputime_systime_ns(void)
256242
tsc_shift = vt->tsc_shift;
257243

258244
/* Read the CPU's tsc. */
259-
tsc = xen_rdtsc();
245+
tsc = rdtsc();
260246
} while (!xen_vcputime_exit(vt, &ticket));
261247

262248
/*
@@ -538,7 +524,7 @@ xen_delay(unsigned n)
538524
/* Get the starting tsc and tsc frequency. */
539525
do {
540526
vt = xen_vcputime_enter(&ticket);
541-
tsc_start = last_tsc = xen_rdtsc();
527+
tsc_start = last_tsc = rdtsc();
542528
tsc_to_system_mul = vt->tsc_to_system_mul;
543529
tsc_shift = vt->tsc_shift;
544530
} while (!xen_vcputime_exit(vt, &ticket));
@@ -549,7 +535,7 @@ xen_delay(unsigned n)
549535
* backwards meaning we've probably migrated pCPUs.
550536
*/
551537
for (;;) {
552-
tsc = xen_rdtsc();
538+
tsc = rdtsc();
553539
if (__predict_false(tsc < last_tsc))
554540
break;
555541
if (xen_tsc_to_ns_delta(tsc - tsc_start,

0 commit comments

Comments
 (0)