Skip to content

Commit 41a23ab

Browse files
vittyvkbonzini
authored andcommitted
KVM: selftests: do not substitute SVM/VMX check with KVM_CAP_NESTED_STATE check
state_test/smm_test use KVM_CAP_NESTED_STATE check as an indicator for nested VMX/SVM presence and this is incorrect. Check for the required features dirrectly. Signed-off-by: Vitaly Kuznetsov <[email protected]> Message-Id: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 77f81f3 commit 41a23ab

File tree

6 files changed

+30
-17
lines changed

6 files changed

+30
-17
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ struct svm_test_data {
3333
struct svm_test_data *vcpu_alloc_svm(struct kvm_vm *vm, vm_vaddr_t *p_svm_gva);
3434
void generic_svm_setup(struct svm_test_data *svm, void *guest_rip, void *guest_rsp);
3535
void run_guest(struct vmcb *vmcb, uint64_t vmcb_gpa);
36+
bool nested_svm_supported(void);
3637
void nested_svm_check_supported(void);
3738

3839
static inline bool cpu_has_svm(void)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,7 @@ bool prepare_for_vmx_operation(struct vmx_pages *vmx);
603603
void prepare_vmcs(struct vmx_pages *vmx, void *guest_rip, void *guest_rsp);
604604
bool load_vmcs(struct vmx_pages *vmx);
605605

606+
bool nested_vmx_supported(void);
606607
void nested_vmx_check_supported(void);
607608

608609
void nested_pg_map(struct vmx_pages *vmx, struct kvm_vm *vm,

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,18 @@ void run_guest(struct vmcb *vmcb, uint64_t vmcb_gpa)
148148
: "r15", "memory");
149149
}
150150

151-
void nested_svm_check_supported(void)
151+
bool nested_svm_supported(void)
152152
{
153153
struct kvm_cpuid_entry2 *entry =
154154
kvm_get_supported_cpuid_entry(0x80000001);
155155

156-
if (!(entry->ecx & CPUID_SVM)) {
156+
return entry->ecx & CPUID_SVM;
157+
}
158+
159+
void nested_svm_check_supported(void)
160+
{
161+
if (!nested_svm_supported()) {
157162
print_skip("nested SVM not enabled");
158163
exit(KSFT_SKIP);
159164
}
160165
}
161-

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,11 +379,16 @@ void prepare_vmcs(struct vmx_pages *vmx, void *guest_rip, void *guest_rsp)
379379
init_vmcs_guest_state(guest_rip, guest_rsp);
380380
}
381381

382-
void nested_vmx_check_supported(void)
382+
bool nested_vmx_supported(void)
383383
{
384384
struct kvm_cpuid_entry2 *entry = kvm_get_supported_cpuid_entry(1);
385385

386-
if (!(entry->ecx & CPUID_VMX)) {
386+
return entry->ecx & CPUID_VMX;
387+
}
388+
389+
void nested_vmx_check_supported(void)
390+
{
391+
if (!nested_vmx_supported()) {
387392
print_skip("nested VMX not enabled");
388393
exit(KSFT_SKIP);
389394
}

tools/testing/selftests/kvm/x86_64/smm_test.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,17 @@ int main(int argc, char *argv[])
118118
vcpu_set_msr(vm, VCPU_ID, MSR_IA32_SMBASE, SMRAM_GPA);
119119

120120
if (kvm_check_cap(KVM_CAP_NESTED_STATE)) {
121-
if (kvm_get_supported_cpuid_entry(0x80000001)->ecx & CPUID_SVM)
121+
if (nested_svm_supported())
122122
vcpu_alloc_svm(vm, &nested_gva);
123-
else
123+
else if (nested_vmx_supported())
124124
vcpu_alloc_vmx(vm, &nested_gva);
125-
vcpu_args_set(vm, VCPU_ID, 1, nested_gva);
126-
} else {
127-
pr_info("will skip SMM test with VMX enabled\n");
128-
vcpu_args_set(vm, VCPU_ID, 1, 0);
129125
}
130126

127+
if (!nested_gva)
128+
pr_info("will skip SMM test with VMX enabled\n");
129+
130+
vcpu_args_set(vm, VCPU_ID, 1, nested_gva);
131+
131132
for (stage = 1;; stage++) {
132133
_vcpu_run(vm, VCPU_ID);
133134
TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,

tools/testing/selftests/kvm/x86_64/state_test.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,16 +171,17 @@ int main(int argc, char *argv[])
171171
vcpu_regs_get(vm, VCPU_ID, &regs1);
172172

173173
if (kvm_check_cap(KVM_CAP_NESTED_STATE)) {
174-
if (kvm_get_supported_cpuid_entry(0x80000001)->ecx & CPUID_SVM)
174+
if (nested_svm_supported())
175175
vcpu_alloc_svm(vm, &nested_gva);
176-
else
176+
else if (nested_vmx_supported())
177177
vcpu_alloc_vmx(vm, &nested_gva);
178-
vcpu_args_set(vm, VCPU_ID, 1, nested_gva);
179-
} else {
180-
pr_info("will skip nested state checks\n");
181-
vcpu_args_set(vm, VCPU_ID, 1, 0);
182178
}
183179

180+
if (!nested_gva)
181+
pr_info("will skip nested state checks\n");
182+
183+
vcpu_args_set(vm, VCPU_ID, 1, nested_gva);
184+
184185
for (stage = 1;; stage++) {
185186
_vcpu_run(vm, VCPU_ID);
186187
TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,

0 commit comments

Comments
 (0)