Skip to content

Commit 6b878cb

Browse files
rchatresean-jc
authored andcommitted
KVM: selftests: Add guest udelay() utility for x86
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 cc57828 ("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 <[email protected]> Link: https://lore.kernel.org/r/5aa86285d1c1d7fe1960e3fe490f4b22273977e6.1718214999.git.reinette.chatre@intel.com Co-developed-by: Sean Christopherson <[email protected]> Signed-off-by: Sean Christopherson <[email protected]>
1 parent dd10340 commit 6b878cb

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

tools/testing/selftests/kvm/include/x86_64/processor.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
extern bool host_cpu_is_intel;
2525
extern bool host_cpu_is_amd;
26+
extern uint64_t guest_tsc_khz;
2627

2728
/* Forced emulation prefix, used to invoke the emulator unconditionally. */
2829
#define KVM_FEP "ud2; .byte 'k', 'v', 'm';"
@@ -815,6 +816,23 @@ static inline void cpu_relax(void)
815816
asm volatile("rep; nop" ::: "memory");
816817
}
817818

819+
static inline void udelay(unsigned long usec)
820+
{
821+
uint64_t start, now, cycles;
822+
823+
GUEST_ASSERT(guest_tsc_khz);
824+
cycles = guest_tsc_khz / 1000 * usec;
825+
826+
/*
827+
* Deliberately don't PAUSE, a.k.a. cpu_relax(), so that the delay is
828+
* as accurate as possible, e.g. doesn't trigger PAUSE-Loop VM-Exits.
829+
*/
830+
start = rdtsc();
831+
do {
832+
now = rdtsc();
833+
} while (now - start < cycles);
834+
}
835+
818836
#define ud2() \
819837
__asm__ __volatile__( \
820838
"ud2\n" \

tools/testing/selftests/kvm/lib/x86_64/processor.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ vm_vaddr_t exception_handlers;
2525
bool host_cpu_is_amd;
2626
bool host_cpu_is_intel;
2727
bool is_forced_emulation_enabled;
28+
uint64_t guest_tsc_khz;
2829

2930
static void regs_dump(FILE *stream, struct kvm_regs *regs, uint8_t indent)
3031
{
@@ -616,6 +617,11 @@ void assert_on_unhandled_exception(struct kvm_vcpu *vcpu)
616617

617618
void kvm_arch_vm_post_create(struct kvm_vm *vm)
618619
{
620+
int r;
621+
622+
TEST_ASSERT(kvm_has_cap(KVM_CAP_GET_TSC_KHZ),
623+
"Require KVM_GET_TSC_KHZ to provide udelay() to guest.");
624+
619625
vm_create_irqchip(vm);
620626
vm_init_descriptor_tables(vm);
621627

@@ -628,6 +634,11 @@ void kvm_arch_vm_post_create(struct kvm_vm *vm)
628634

629635
vm_sev_ioctl(vm, KVM_SEV_INIT2, &init);
630636
}
637+
638+
r = __vm_ioctl(vm, KVM_GET_TSC_KHZ, NULL);
639+
TEST_ASSERT(r > 0, "KVM_GET_TSC_KHZ did not provide a valid TSC frequency.");
640+
guest_tsc_khz = r;
641+
sync_global_to_guest(vm, guest_tsc_khz);
631642
}
632643

633644
void vcpu_arch_set_entry_point(struct kvm_vcpu *vcpu, void *guest_code)

0 commit comments

Comments
 (0)