Skip to content

Commit 0a9eb2a

Browse files
yosrym93sean-jc
authored andcommitted
KVM: selftests: Extend vmx_close_while_nested_test to cover SVM
Add SVM L1 code to run the nested guest, and allow the test to run with SVM as well as VMX. Reviewed-by: Jim Mattson <jmattson@google.com> Signed-off-by: Yosry Ahmed <yosry.ahmed@linux.dev> Link: https://patch.msgid.link/20251021074736.1324328-4-yosry.ahmed@linux.dev [sean: rename to "nested_close_kvm_test" to provide nested_* sorting] Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 9e4ce7a commit 0a9eb2a

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

tools/testing/selftests/kvm/Makefile.kvm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ TEST_GEN_PROGS_x86 += x86/kvm_pv_test
8888
TEST_GEN_PROGS_x86 += x86/kvm_buslock_test
8989
TEST_GEN_PROGS_x86 += x86/monitor_mwait_test
9090
TEST_GEN_PROGS_x86 += x86/msrs_test
91+
TEST_GEN_PROGS_x86 += x86/nested_close_kvm_test
9192
TEST_GEN_PROGS_x86 += x86/nested_emulation_test
9293
TEST_GEN_PROGS_x86 += x86/nested_exceptions_test
9394
TEST_GEN_PROGS_x86 += x86/platform_info_test
@@ -111,7 +112,6 @@ TEST_GEN_PROGS_x86 += x86/ucna_injection_test
111112
TEST_GEN_PROGS_x86 += x86/userspace_io_test
112113
TEST_GEN_PROGS_x86 += x86/userspace_msr_exit_test
113114
TEST_GEN_PROGS_x86 += x86/vmx_apic_access_test
114-
TEST_GEN_PROGS_x86 += x86/vmx_close_while_nested_test
115115
TEST_GEN_PROGS_x86 += x86/vmx_dirty_log_test
116116
TEST_GEN_PROGS_x86 += x86/vmx_exception_with_invalid_guest_state
117117
TEST_GEN_PROGS_x86 += x86/vmx_msrs_test

tools/testing/selftests/kvm/x86/vmx_close_while_nested_test.c renamed to tools/testing/selftests/kvm/x86/nested_close_kvm_test.c

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// SPDX-License-Identifier: GPL-2.0-only
22
/*
3-
* vmx_close_while_nested
4-
*
53
* Copyright (C) 2019, Red Hat, Inc.
64
*
75
* Verify that nothing bad happens if a KVM user exits with open
@@ -12,6 +10,7 @@
1210
#include "kvm_util.h"
1311
#include "processor.h"
1412
#include "vmx.h"
13+
#include "svm_util.h"
1514

1615
#include <string.h>
1716
#include <sys/ioctl.h>
@@ -22,16 +21,17 @@ enum {
2221
PORT_L0_EXIT = 0x2000,
2322
};
2423

24+
#define L2_GUEST_STACK_SIZE 64
25+
2526
static void l2_guest_code(void)
2627
{
2728
/* Exit to L0 */
2829
asm volatile("inb %%dx, %%al"
2930
: : [port] "d" (PORT_L0_EXIT) : "rax");
3031
}
3132

32-
static void l1_guest_code(struct vmx_pages *vmx_pages)
33+
static void l1_vmx_code(struct vmx_pages *vmx_pages)
3334
{
34-
#define L2_GUEST_STACK_SIZE 64
3535
unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
3636

3737
GUEST_ASSERT(prepare_for_vmx_operation(vmx_pages));
@@ -45,19 +45,43 @@ static void l1_guest_code(struct vmx_pages *vmx_pages)
4545
GUEST_ASSERT(0);
4646
}
4747

48+
static void l1_svm_code(struct svm_test_data *svm)
49+
{
50+
unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
51+
52+
/* Prepare the VMCB for L2 execution. */
53+
generic_svm_setup(svm, l2_guest_code,
54+
&l2_guest_stack[L2_GUEST_STACK_SIZE]);
55+
56+
run_guest(svm->vmcb, svm->vmcb_gpa);
57+
GUEST_ASSERT(0);
58+
}
59+
60+
static void l1_guest_code(void *data)
61+
{
62+
if (this_cpu_has(X86_FEATURE_VMX))
63+
l1_vmx_code(data);
64+
else
65+
l1_svm_code(data);
66+
}
67+
4868
int main(int argc, char *argv[])
4969
{
50-
vm_vaddr_t vmx_pages_gva;
70+
vm_vaddr_t guest_gva;
5171
struct kvm_vcpu *vcpu;
5272
struct kvm_vm *vm;
5373

54-
TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_VMX));
74+
TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_VMX) ||
75+
kvm_cpu_has(X86_FEATURE_SVM));
5576

5677
vm = vm_create_with_one_vcpu(&vcpu, l1_guest_code);
5778

58-
/* Allocate VMX pages and shared descriptors (vmx_pages). */
59-
vcpu_alloc_vmx(vm, &vmx_pages_gva);
60-
vcpu_args_set(vcpu, 1, vmx_pages_gva);
79+
if (kvm_cpu_has(X86_FEATURE_VMX))
80+
vcpu_alloc_vmx(vm, &guest_gva);
81+
else
82+
vcpu_alloc_svm(vm, &guest_gva);
83+
84+
vcpu_args_set(vcpu, 1, guest_gva);
6185

6286
for (;;) {
6387
volatile struct kvm_run *run = vcpu->run;

0 commit comments

Comments
 (0)