]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
KVM: selftests: Add guest udelay() utility for x86
authorReinette Chatre <reinette.chatre@intel.com>
Wed, 12 Jun 2024 18:16:11 +0000 (11:16 -0700)
committerSean Christopherson <seanjc@google.com>
Fri, 28 Jun 2024 18:40:38 +0000 (11:40 -0700)
Add udelay() for x86 tests to allow busy waiting in the guest for a
specific duration, and to match ARM and RISC-V's udelay() in the hopes
of eventually making udelay() available on all architectures.

Get the guest's TSC frequency using KVM_GET_TSC_KHZ and expose it to all
VMs via a new global, guest_tsc_khz.  Assert that KVM_GET_TSC_KHZ returns
a valid frequency, instead of simply skipping tests, which would require
detecting which tests actually need/want udelay().  KVM hasn't returned an
error for KVM_GET_TSC_KHZ since commit cc578287e322 ("KVM: Infrastructure
for software and hardware based TSC rate scaling"), which predates KVM
selftests by 6+ years (KVM_GET_TSC_KHZ itself predates KVM selftest by 7+
years).

Note, if the GUEST_ASSERT() in udelay() somehow fires and the test doesn't
check for guest asserts, then the test will fail with a very cryptic
message.  But fixing that, e.g. by automatically handling guest asserts,
is a much larger task, and practically speaking the odds of a test afoul
of this wart are infinitesimally small.

Signed-off-by: Reinette Chatre <reinette.chatre@intel.com>
Link: https://lore.kernel.org/r/5aa86285d1c1d7fe1960e3fe490f4b22273977e6.1718214999.git.reinette.chatre@intel.com
Co-developed-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
tools/testing/selftests/kvm/include/x86_64/processor.h
tools/testing/selftests/kvm/lib/x86_64/processor.c

index 8eb57de0b5876db37d79508f28d1cee3c205df1b..8501735c6f416446fc37dfd1a44cdbf497e24d05 100644 (file)
@@ -23,6 +23,7 @@
 
 extern bool host_cpu_is_intel;
 extern bool host_cpu_is_amd;
+extern uint64_t guest_tsc_khz;
 
 /* Forced emulation prefix, used to invoke the emulator unconditionally. */
 #define KVM_FEP "ud2; .byte 'k', 'v', 'm';"
@@ -815,6 +816,23 @@ static inline void cpu_relax(void)
        asm volatile("rep; nop" ::: "memory");
 }
 
+static inline void udelay(unsigned long usec)
+{
+       uint64_t start, now, cycles;
+
+       GUEST_ASSERT(guest_tsc_khz);
+       cycles = guest_tsc_khz / 1000 * usec;
+
+       /*
+        * Deliberately don't PAUSE, a.k.a. cpu_relax(), so that the delay is
+        * as accurate as possible, e.g. doesn't trigger PAUSE-Loop VM-Exits.
+        */
+       start = rdtsc();
+       do {
+               now = rdtsc();
+       } while (now - start < cycles);
+}
+
 #define ud2()                  \
        __asm__ __volatile__(   \
                "ud2\n" \
index c664e446136bc6eb02dd2d9101c885cb1dad85d4..d547f121813d869bdfab8945df96bdb9fb7f26af 100644 (file)
@@ -25,6 +25,7 @@ vm_vaddr_t exception_handlers;
 bool host_cpu_is_amd;
 bool host_cpu_is_intel;
 bool is_forced_emulation_enabled;
+uint64_t guest_tsc_khz;
 
 static void regs_dump(FILE *stream, struct kvm_regs *regs, uint8_t indent)
 {
@@ -616,6 +617,11 @@ void assert_on_unhandled_exception(struct kvm_vcpu *vcpu)
 
 void kvm_arch_vm_post_create(struct kvm_vm *vm)
 {
+       int r;
+
+       TEST_ASSERT(kvm_has_cap(KVM_CAP_GET_TSC_KHZ),
+                   "Require KVM_GET_TSC_KHZ to provide udelay() to guest.");
+
        vm_create_irqchip(vm);
        vm_init_descriptor_tables(vm);
 
@@ -628,6 +634,11 @@ void kvm_arch_vm_post_create(struct kvm_vm *vm)
 
                vm_sev_ioctl(vm, KVM_SEV_INIT2, &init);
        }
+
+       r = __vm_ioctl(vm, KVM_GET_TSC_KHZ, NULL);
+       TEST_ASSERT(r > 0, "KVM_GET_TSC_KHZ did not provide a valid TSC frequency.");
+       guest_tsc_khz = r;
+       sync_global_to_guest(vm, guest_tsc_khz);
 }
 
 void vcpu_arch_set_entry_point(struct kvm_vcpu *vcpu, void *guest_code)