xref: /linux/tools/testing/selftests/kvm/x86/svm_vmcall_test.c (revision 67f8bc848ee31831336bd478e57d2f993551902e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * svm_vmcall_test
4  *
5  * Copyright (C) 2020, Red Hat, Inc.
6  *
7  * Nested SVM testing: VMCALL
8  */
9 
10 #include "test_util.h"
11 #include "kvm_util.h"
12 #include "processor.h"
13 #include "svm_util.h"
14 
15 static void l2_guest_code(struct svm_test_data *svm)
16 {
17 	__asm__ __volatile__("vmcall");
18 }
19 
20 static void l1_guest_code(struct svm_test_data *svm)
21 {
22 	struct vmcb *vmcb = svm->vmcb;
23 
24 	/* Prepare for L2 execution. */
25 	generic_svm_setup(svm, l2_guest_code);
26 
27 	run_guest(vmcb, svm->vmcb_gpa);
28 
29 	GUEST_ASSERT(vmcb->control.exit_code == SVM_EXIT_VMMCALL);
30 	GUEST_DONE();
31 }
32 
33 int main(int argc, char *argv[])
34 {
35 	struct kvm_vcpu *vcpu;
36 	gva_t svm_gva;
37 	struct kvm_vm *vm;
38 
39 	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SVM));
40 
41 	vm = vm_create_with_one_vcpu(&vcpu, l1_guest_code);
42 
43 	vcpu_alloc_svm(vm, &svm_gva);
44 	vcpu_args_set(vcpu, 1, svm_gva);
45 
46 	for (;;) {
47 		struct ucall uc;
48 
49 		vcpu_run(vcpu);
50 		TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
51 
52 		switch (get_ucall(vcpu, &uc)) {
53 		case UCALL_ABORT:
54 			REPORT_GUEST_ASSERT(uc);
55 			/* NOT REACHED */
56 		case UCALL_SYNC:
57 			break;
58 		case UCALL_DONE:
59 			goto done;
60 		default:
61 			TEST_FAIL("Unknown ucall 0x%lx.", uc.cmd);
62 		}
63 	}
64 done:
65 	kvm_vm_free(vm);
66 	return 0;
67 }
68