diff mbox series

[V7,1/2] KVM: selftests: Add x86_64 guest udelay() utility

Message ID 6cd5b8bba5905813dacf71249865b316146dd1d2.1717695426.git.reinette.chatre@intel.com (mailing list archive)
State New
Headers show
Series KVM: x86: Make bus clock frequency for vAPIC timer configurable | expand

Commit Message

Reinette Chatre June 6, 2024, 5:42 p.m. UTC
Create udelay() utility for x86_64 tests to match
udelay() available to ARM and RISC-V tests.

Calibrate guest frequency using the KVM_GET_TSC_KHZ ioctl()
and share it between host and guest with the new
tsc_khz global variable.

Signed-off-by: Reinette Chatre <reinette.chatre@intel.com>
---
 .../selftests/kvm/include/x86_64/processor.h      | 15 +++++++++++++++
 .../testing/selftests/kvm/lib/x86_64/processor.c  |  8 ++++++++
 2 files changed, 23 insertions(+)

Comments

Reinette Chatre June 8, 2024, 3:12 p.m. UTC | #1
On 6/6/24 10:42 AM, Reinette Chatre wrote:
> Create udelay() utility for x86_64 tests to match
> udelay() available to ARM and RISC-V tests.
> 
> Calibrate guest frequency using the KVM_GET_TSC_KHZ ioctl()
> and share it between host and guest with the new
> tsc_khz global variable.
> 
> Signed-off-by: Reinette Chatre <reinette.chatre@intel.com>
> ---
>   .../selftests/kvm/include/x86_64/processor.h      | 15 +++++++++++++++
>   .../testing/selftests/kvm/lib/x86_64/processor.c  |  8 ++++++++
>   2 files changed, 23 insertions(+)
> 
> diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h
> index 8eb57de0b587..f3a5881dd821 100644
> --- a/tools/testing/selftests/kvm/include/x86_64/processor.h
> +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h
> @@ -23,6 +23,7 @@
>   
>   extern bool host_cpu_is_intel;
>   extern bool host_cpu_is_amd;
> +extern unsigned long tsc_khz;
>   
>   /* Forced emulation prefix, used to invoke the emulator unconditionally. */
>   #define KVM_FEP "ud2; .byte 'k', 'v', 'm';"
> @@ -815,6 +816,20 @@ static inline void cpu_relax(void)
>   	asm volatile("rep; nop" ::: "memory");
>   }
>   
> +static inline void udelay(unsigned long usec)
> +{
> +	unsigned long cycles = tsc_khz / 1000 * usec;
> +	uint64_t start, now;
> +
> +	start = rdtsc();
> +	for (;;) {
> +		now = rdtsc();
> +		if (now - start >= cycles)
> +			break;
> +		cpu_relax();
> +	}
> +}
> +
>   #define ud2()			\
>   	__asm__ __volatile__(	\
>   		"ud2\n"	\
> diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c b/tools/testing/selftests/kvm/lib/x86_64/processor.c
> index c664e446136b..6558fece8a36 100644
> --- a/tools/testing/selftests/kvm/lib/x86_64/processor.c
> +++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c
> @@ -25,6 +25,7 @@ vm_vaddr_t exception_handlers;
>   bool host_cpu_is_amd;
>   bool host_cpu_is_intel;
>   bool is_forced_emulation_enabled;
> +unsigned long tsc_khz;
>   
>   static void regs_dump(FILE *stream, struct kvm_regs *regs, uint8_t indent)
>   {
> @@ -628,6 +629,13 @@ void kvm_arch_vm_post_create(struct kvm_vm *vm)
>   
>   		vm_sev_ioctl(vm, KVM_SEV_INIT2, &init);
>   	}
> +
> +	if (kvm_has_cap(KVM_CAP_GET_TSC_KHZ)) {
> +		tsc_khz = __vm_ioctl(vm, KVM_GET_TSC_KHZ, NULL);
> +		if (tsc_khz < 0)

This incorrect type usage will be fixed in next version that I plan
to send on Monday.

> +			tsc_khz = 0;
> +		sync_global_to_guest(vm, tsc_khz);
> +	}
>   }
>   
>   void vcpu_arch_set_entry_point(struct kvm_vcpu *vcpu, void *guest_code)

Reinette
diff mbox series

Patch

diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h
index 8eb57de0b587..f3a5881dd821 100644
--- a/tools/testing/selftests/kvm/include/x86_64/processor.h
+++ b/tools/testing/selftests/kvm/include/x86_64/processor.h
@@ -23,6 +23,7 @@ 
 
 extern bool host_cpu_is_intel;
 extern bool host_cpu_is_amd;
+extern unsigned long tsc_khz;
 
 /* Forced emulation prefix, used to invoke the emulator unconditionally. */
 #define KVM_FEP "ud2; .byte 'k', 'v', 'm';"
@@ -815,6 +816,20 @@  static inline void cpu_relax(void)
 	asm volatile("rep; nop" ::: "memory");
 }
 
+static inline void udelay(unsigned long usec)
+{
+	unsigned long cycles = tsc_khz / 1000 * usec;
+	uint64_t start, now;
+
+	start = rdtsc();
+	for (;;) {
+		now = rdtsc();
+		if (now - start >= cycles)
+			break;
+		cpu_relax();
+	}
+}
+
 #define ud2()			\
 	__asm__ __volatile__(	\
 		"ud2\n"	\
diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c b/tools/testing/selftests/kvm/lib/x86_64/processor.c
index c664e446136b..6558fece8a36 100644
--- a/tools/testing/selftests/kvm/lib/x86_64/processor.c
+++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c
@@ -25,6 +25,7 @@  vm_vaddr_t exception_handlers;
 bool host_cpu_is_amd;
 bool host_cpu_is_intel;
 bool is_forced_emulation_enabled;
+unsigned long tsc_khz;
 
 static void regs_dump(FILE *stream, struct kvm_regs *regs, uint8_t indent)
 {
@@ -628,6 +629,13 @@  void kvm_arch_vm_post_create(struct kvm_vm *vm)
 
 		vm_sev_ioctl(vm, KVM_SEV_INIT2, &init);
 	}
+
+	if (kvm_has_cap(KVM_CAP_GET_TSC_KHZ)) {
+		tsc_khz = __vm_ioctl(vm, KVM_GET_TSC_KHZ, NULL);
+		if (tsc_khz < 0)
+			tsc_khz = 0;
+		sync_global_to_guest(vm, tsc_khz);
+	}
 }
 
 void vcpu_arch_set_entry_point(struct kvm_vcpu *vcpu, void *guest_code)