diff mbox series

[1/2] selftests: kvm: introduce cpu_has_svm() check

Message ID 20200529130407.57176-1-vkuznets@redhat.com (mailing list archive)
State New, archived
Headers show
Series [1/2] selftests: kvm: introduce cpu_has_svm() check | expand

Commit Message

Vitaly Kuznetsov May 29, 2020, 1:04 p.m. UTC
More tests may want to check if the CPU is Intel or AMD in
guest code, separate cpu_has_svm() and put it as static
inline to svm_util.h.

Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
---
 tools/testing/selftests/kvm/include/x86_64/svm_util.h | 10 ++++++++++
 tools/testing/selftests/kvm/x86_64/state_test.c       |  9 +--------
 2 files changed, 11 insertions(+), 8 deletions(-)

Comments

Sean Christopherson May 29, 2020, 3:18 p.m. UTC | #1
On Fri, May 29, 2020 at 03:04:06PM +0200, Vitaly Kuznetsov wrote:
> More tests may want to check if the CPU is Intel or AMD in
> guest code, separate cpu_has_svm() and put it as static
> inline to svm_util.h.
> 
> Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
> ---
>  tools/testing/selftests/kvm/include/x86_64/svm_util.h | 10 ++++++++++
>  tools/testing/selftests/kvm/x86_64/state_test.c       |  9 +--------
>  2 files changed, 11 insertions(+), 8 deletions(-)
> 
> diff --git a/tools/testing/selftests/kvm/include/x86_64/svm_util.h b/tools/testing/selftests/kvm/include/x86_64/svm_util.h
> index cd037917fece..b1057773206a 100644
> --- a/tools/testing/selftests/kvm/include/x86_64/svm_util.h
> +++ b/tools/testing/selftests/kvm/include/x86_64/svm_util.h
> @@ -35,4 +35,14 @@ void generic_svm_setup(struct svm_test_data *svm, void *guest_rip, void *guest_r
>  void run_guest(struct vmcb *vmcb, uint64_t vmcb_gpa);
>  void nested_svm_check_supported(void);
>  
> +static inline bool cpu_has_svm(void)
> +{
> +	u32 eax = 0x80000001, ecx;
> +
> +	asm volatile("cpuid" :
> +		     "=a" (eax), "=c" (ecx) : "0" (eax) : "ebx", "edx");

	u32 eax, ecx;

	asm("cpuid" : "=a" (eax), "=c" (ecx) : "a" (0x80000001) : "ebx", "edx");

The volatile shouldn't be needed, e.g. no one should be using this purely
for its seralization properties, and I don't see any reason to put the leaf
number into a variable.

Alternatively, adding a proper cpuid framework to processor.h would likely
be useful in the long run.

> +
> +	return ecx & CPUID_SVM;
> +}
> +
Vitaly Kuznetsov June 1, 2020, 8:14 a.m. UTC | #2
Sean Christopherson <sean.j.christopherson@intel.com> writes:

> On Fri, May 29, 2020 at 03:04:06PM +0200, Vitaly Kuznetsov wrote:
>> More tests may want to check if the CPU is Intel or AMD in
>> guest code, separate cpu_has_svm() and put it as static
>> inline to svm_util.h.
>> 
>> Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
>> ---
>>  tools/testing/selftests/kvm/include/x86_64/svm_util.h | 10 ++++++++++
>>  tools/testing/selftests/kvm/x86_64/state_test.c       |  9 +--------
>>  2 files changed, 11 insertions(+), 8 deletions(-)
>> 
>> diff --git a/tools/testing/selftests/kvm/include/x86_64/svm_util.h b/tools/testing/selftests/kvm/include/x86_64/svm_util.h
>> index cd037917fece..b1057773206a 100644
>> --- a/tools/testing/selftests/kvm/include/x86_64/svm_util.h
>> +++ b/tools/testing/selftests/kvm/include/x86_64/svm_util.h
>> @@ -35,4 +35,14 @@ void generic_svm_setup(struct svm_test_data *svm, void *guest_rip, void *guest_r
>>  void run_guest(struct vmcb *vmcb, uint64_t vmcb_gpa);
>>  void nested_svm_check_supported(void);
>>  
>> +static inline bool cpu_has_svm(void)
>> +{
>> +	u32 eax = 0x80000001, ecx;
>> +
>> +	asm volatile("cpuid" :
>> +		     "=a" (eax), "=c" (ecx) : "0" (eax) : "ebx", "edx");
>
> 	u32 eax, ecx;
>
> 	asm("cpuid" : "=a" (eax), "=c" (ecx) : "a" (0x80000001) : "ebx", "edx");
>
> The volatile shouldn't be needed, e.g. no one should be using this purely
> for its seralization properties, and I don't see any reason to put the leaf
> number into a variable.
>
> Alternatively, adding a proper cpuid framework to processor.h would likely
> be useful in the long run.
>

All true, even better would be to find a way to include the definition
of native_cpuid*() from arch/x86/include/asm/processor.h but I didn't
explore these options yet, was trying to address the immediate issue
with Paolo's SVM series. It can probably be done when there is a second
user of cpuid in tests which needs to check something different from SVM
bit.
diff mbox series

Patch

diff --git a/tools/testing/selftests/kvm/include/x86_64/svm_util.h b/tools/testing/selftests/kvm/include/x86_64/svm_util.h
index cd037917fece..b1057773206a 100644
--- a/tools/testing/selftests/kvm/include/x86_64/svm_util.h
+++ b/tools/testing/selftests/kvm/include/x86_64/svm_util.h
@@ -35,4 +35,14 @@  void generic_svm_setup(struct svm_test_data *svm, void *guest_rip, void *guest_r
 void run_guest(struct vmcb *vmcb, uint64_t vmcb_gpa);
 void nested_svm_check_supported(void);
 
+static inline bool cpu_has_svm(void)
+{
+	u32 eax = 0x80000001, ecx;
+
+	asm volatile("cpuid" :
+		     "=a" (eax), "=c" (ecx) : "0" (eax) : "ebx", "edx");
+
+	return ecx & CPUID_SVM;
+}
+
 #endif /* SELFTEST_KVM_SVM_UTILS_H */
diff --git a/tools/testing/selftests/kvm/x86_64/state_test.c b/tools/testing/selftests/kvm/x86_64/state_test.c
index af8b6df6a13e..d43b6f99b66c 100644
--- a/tools/testing/selftests/kvm/x86_64/state_test.c
+++ b/tools/testing/selftests/kvm/x86_64/state_test.c
@@ -137,20 +137,13 @@  static void vmx_l1_guest_code(struct vmx_pages *vmx_pages)
 	GUEST_ASSERT(vmresume());
 }
 
-static u32 cpuid_ecx(u32 eax)
-{
-	u32 ecx;
-	asm volatile("cpuid" : "=a" (eax), "=c" (ecx) : "0" (eax) : "ebx", "edx");
-	return ecx;
-}
-
 static void __attribute__((__flatten__)) guest_code(void *arg)
 {
 	GUEST_SYNC(1);
 	GUEST_SYNC(2);
 
 	if (arg) {
-		if (cpuid_ecx(0x80000001) & CPUID_SVM)
+		if (cpu_has_svm())
 			svm_l1_guest_code(arg);
 		else
 			vmx_l1_guest_code(arg);