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 #define VCPU_ID		5
16 
17 static struct kvm_vm *vm;
18 
19 static void l2_guest_code(struct svm_test_data *svm)
20 {
21 	__asm__ __volatile__("vmcall");
22 }
23 
24 static void l1_guest_code(struct svm_test_data *svm)
25 {
26 	#define L2_GUEST_STACK_SIZE 64
27 	unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
28 	struct vmcb *vmcb = svm->vmcb;
29 
30 	/* Prepare for L2 execution. */
31 	generic_svm_setup(svm, l2_guest_code,
32 			  &l2_guest_stack[L2_GUEST_STACK_SIZE]);
33 
34 	run_guest(vmcb, svm->vmcb_gpa);
35 
36 	GUEST_ASSERT(vmcb->control.exit_code == SVM_EXIT_VMMCALL);
37 	GUEST_DONE();
38 }
39 
40 int main(int argc, char *argv[])
41 {
42 	vm_vaddr_t svm_gva;
43 
44 	nested_svm_check_supported();
45 
46 	vm = vm_create_default(VCPU_ID, 0, (void *) l1_guest_code);
47 
48 	vcpu_alloc_svm(vm, &svm_gva);
49 	vcpu_args_set(vm, VCPU_ID, 1, svm_gva);
50 
51 	for (;;) {
52 		volatile struct kvm_run *run = vcpu_state(vm, VCPU_ID);
53 		struct ucall uc;
54 
55 		vcpu_run(vm, VCPU_ID);
56 		TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
57 			    "Got exit_reason other than KVM_EXIT_IO: %u (%s)\n",
58 			    run->exit_reason,
59 			    exit_reason_str(run->exit_reason));
60 
61 		switch (get_ucall(vm, VCPU_ID, &uc)) {
62 		case UCALL_ABORT:
63 			TEST_FAIL("%s", (const char *)uc.args[0]);
64 			/* NOT REACHED */
65 		case UCALL_SYNC:
66 			break;
67 		case UCALL_DONE:
68 			goto done;
69 		default:
70 			TEST_FAIL("Unknown ucall 0x%lx.", uc.cmd);
71 		}
72 	}
73 done:
74 	kvm_vm_free(vm);
75 	return 0;
76 }
77