]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
KVM: selftests: Extend nested_invalid_cr3_test to cover SVM
authorYosry Ahmed <yosry.ahmed@linux.dev>
Tue, 21 Oct 2025 07:47:19 +0000 (07:47 +0000)
committerSean Christopherson <seanjc@google.com>
Fri, 21 Nov 2025 00:19:55 +0000 (16:19 -0800)
Add SVM L1 code to run the nested guest, and allow the test to run with
SVM as well as VMX.

Signed-off-by: Yosry Ahmed <yosry.ahmed@linux.dev>
Link: https://patch.msgid.link/20251021074736.1324328-7-yosry.ahmed@linux.dev
Signed-off-by: Sean Christopherson <seanjc@google.com>
tools/testing/selftests/kvm/x86/nested_invalid_cr3_test.c

index e839bcf5f8adaab17a43c33d6a88b3cc53f33813..a6b6da9cf7fe1d1d4c613d012bb2c85429a74729 100644 (file)
@@ -7,6 +7,7 @@
  */
 #include "kvm_util.h"
 #include "vmx.h"
+#include "svm_util.h"
 #include "kselftest.h"
 
 
@@ -17,6 +18,28 @@ static void l2_guest_code(void)
        vmcall();
 }
 
+static void l1_svm_code(struct svm_test_data *svm)
+{
+       unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
+       uintptr_t save_cr3;
+
+       generic_svm_setup(svm, l2_guest_code,
+                         &l2_guest_stack[L2_GUEST_STACK_SIZE]);
+
+       /* Try to run L2 with invalid CR3 and make sure it fails */
+       save_cr3 = svm->vmcb->save.cr3;
+       svm->vmcb->save.cr3 = -1ull;
+       run_guest(svm->vmcb, svm->vmcb_gpa);
+       GUEST_ASSERT(svm->vmcb->control.exit_code == SVM_EXIT_ERR);
+
+       /* Now restore CR3 and make sure L2 runs successfully */
+       svm->vmcb->save.cr3 = save_cr3;
+       run_guest(svm->vmcb, svm->vmcb_gpa);
+       GUEST_ASSERT(svm->vmcb->control.exit_code == SVM_EXIT_VMMCALL);
+
+       GUEST_DONE();
+}
+
 static void l1_vmx_code(struct vmx_pages *vmx_pages)
 {
        unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
@@ -43,16 +66,30 @@ static void l1_vmx_code(struct vmx_pages *vmx_pages)
        GUEST_DONE();
 }
 
+static void l1_guest_code(void *data)
+{
+       if (this_cpu_has(X86_FEATURE_VMX))
+               l1_vmx_code(data);
+       else
+               l1_svm_code(data);
+}
+
 int main(int argc, char *argv[])
 {
        struct kvm_vcpu *vcpu;
        struct kvm_vm *vm;
        vm_vaddr_t guest_gva = 0;
 
-       TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_VMX));
+       TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_VMX) ||
+                    kvm_cpu_has(X86_FEATURE_SVM));
+
+       vm = vm_create_with_one_vcpu(&vcpu, l1_guest_code);
+
+       if (kvm_cpu_has(X86_FEATURE_VMX))
+               vcpu_alloc_vmx(vm, &guest_gva);
+       else
+               vcpu_alloc_svm(vm, &guest_gva);
 
-       vm = vm_create_with_one_vcpu(&vcpu, l1_vmx_code);
-       vcpu_alloc_vmx(vm, &guest_gva);
        vcpu_args_set(vcpu, 1, guest_gva);
 
        for (;;) {