On Thu, Mar 18, 2021 at 03:09:49PM +0100, Vitaly Kuznetsov wrote: > Introduce a new selftest for Hyper-V clocksources (MSR-based reference TSC > and TSC page). As a starting point, test the following: > 1) Reference TSC is 1Ghz clock. > 2) Reference TSC and TSC page give the same reading. > 3) TSC page gets updated upon KVM_SET_CLOCK call. > 4) TSC page does not get updated when guest opted for reenlightenment. > 5) Disabled TSC page doesn't get updated. > > Signed-off-by: Vitaly Kuznetsov <vkuznets@xxxxxxxxxx> > --- > tools/testing/selftests/kvm/.gitignore | 1 + > tools/testing/selftests/kvm/Makefile | 1 + > .../selftests/kvm/x86_64/hyperv_clock.c | 233 ++++++++++++++++++ > 3 files changed, 235 insertions(+) > create mode 100644 tools/testing/selftests/kvm/x86_64/hyperv_clock.c > > diff --git a/tools/testing/selftests/kvm/.gitignore b/tools/testing/selftests/kvm/.gitignore > index 32b87cc77c8e..22be05c55f13 100644 > --- a/tools/testing/selftests/kvm/.gitignore > +++ b/tools/testing/selftests/kvm/.gitignore > @@ -9,6 +9,7 @@ > /x86_64/evmcs_test > /x86_64/get_cpuid_test > /x86_64/kvm_pv_test > +/x86_64/hyperv_clock > /x86_64/hyperv_cpuid > /x86_64/mmio_warning_test > /x86_64/platform_info_test > diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile > index a6d61f451f88..c3672e9087d3 100644 > --- a/tools/testing/selftests/kvm/Makefile > +++ b/tools/testing/selftests/kvm/Makefile > @@ -41,6 +41,7 @@ LIBKVM_s390x = lib/s390x/processor.c lib/s390x/ucall.c lib/s390x/diag318_test_ha > TEST_GEN_PROGS_x86_64 = x86_64/cr4_cpuid_sync_test > TEST_GEN_PROGS_x86_64 += x86_64/evmcs_test > TEST_GEN_PROGS_x86_64 += x86_64/get_cpuid_test > +TEST_GEN_PROGS_x86_64 += x86_64/hyperv_clock > TEST_GEN_PROGS_x86_64 += x86_64/hyperv_cpuid > TEST_GEN_PROGS_x86_64 += x86_64/kvm_pv_test > TEST_GEN_PROGS_x86_64 += x86_64/mmio_warning_test > diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_clock.c b/tools/testing/selftests/kvm/x86_64/hyperv_clock.c > new file mode 100644 > index 000000000000..39d6491d8458 > --- /dev/null > +++ b/tools/testing/selftests/kvm/x86_64/hyperv_clock.c > @@ -0,0 +1,233 @@ > +// SPDX-License-Identifier: GPL-2.0-only > +/* > + * Copyright (C) 2021, Red Hat, Inc. > + * > + * Tests for Hyper-V clocksources > + */ > +#include "test_util.h" > +#include "kvm_util.h" > +#include "processor.h" > + > +struct ms_hyperv_tsc_page { > + volatile u32 tsc_sequence; > + u32 reserved1; > + volatile u64 tsc_scale; > + volatile s64 tsc_offset; > +} __packed; > + > +#define HV_X64_MSR_GUEST_OS_ID 0x40000000 > +#define HV_X64_MSR_TIME_REF_COUNT 0x40000020 > +#define HV_X64_MSR_REFERENCE_TSC 0x40000021 > +#define HV_X64_MSR_TSC_FREQUENCY 0x40000022 > +#define HV_X64_MSR_REENLIGHTENMENT_CONTROL 0x40000106 > +#define HV_X64_MSR_TSC_EMULATION_CONTROL 0x40000107 > + > +/* Simplified mul_u64_u64_shr() */ > +static inline u64 mul_u64_u64_shr64(u64 a, u64 b) > +{ > + union { > + u64 ll; > + struct { > + u32 low, high; > + } l; > + } rm, rn, rh, a0, b0; > + u64 c; > + > + a0.ll = a; > + b0.ll = b; > + > + rm.ll = (u64)a0.l.low * b0.l.high; > + rn.ll = (u64)a0.l.high * b0.l.low; > + rh.ll = (u64)a0.l.high * b0.l.high; > + > + rh.l.low = c = rm.l.high + rn.l.high + rh.l.low; > + rh.l.high = (c >> 32) + rh.l.high; > + > + return rh.ll; > +} > + > +static inline void nop_loop(void) > +{ > + int i; > + > + for (i = 0; i < 1000000; i++) > + asm volatile("nop"); > +} > + > +static inline void check_tsc_msr_rdtsc(void) > +{ > + u64 tsc_freq, r1, r2, t1, t2; > + s64 delta_ns; > + > + tsc_freq = rdmsr(HV_X64_MSR_TSC_FREQUENCY); > + GUEST_ASSERT(tsc_freq > 0); > + > + /* First, check MSR-based clocksource */ > + r1 = rdtsc(); > + t1 = rdmsr(HV_X64_MSR_TIME_REF_COUNT); > + nop_loop(); > + r2 = rdtsc(); > + t2 = rdmsr(HV_X64_MSR_TIME_REF_COUNT); > + > + GUEST_ASSERT(t2 > t1); > + > + /* HV_X64_MSR_TIME_REF_COUNT is in 100ns */ > + delta_ns = ((t2 - t1) * 100) - ((r2 - r1) * 1000000000 / tsc_freq); > + if (delta_ns < 0) > + delta_ns = -delta_ns; I think this should be monotonically increasing: 1. r1 = rdtsc(); 2. t1 = rdmsr(HV_X64_MSR_TIME_REF_COUNT); 3. nop_loop(); 4. r2 = rdtsc(); 5. t2 = rdmsr(HV_X64_MSR_TIME_REF_COUNT); => r1 <= t1 <= r2 <= t2 > + > + /* 1% tolerance */ > + GUEST_ASSERT(delta_ns * 100 < (t2 - t1) * 100); > +} Doesnt an unbounded schedule-out/schedule-in (which resembles overloaded host) of the qemu-kvm vcpu in any of the points 1,2,3,4,5 break the assertion above?