1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2018, Red Hat, Inc.
4  *
5  * Tests for Enlightened VMCS, including nested guest state.
6  */
7 #define _GNU_SOURCE /* for program_invocation_short_name */
8 #include <fcntl.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/ioctl.h>
13 
14 #include "test_util.h"
15 
16 #include "kvm_util.h"
17 
18 #include "vmx.h"
19 
20 #define VCPU_ID		5
21 
l2_guest_code(void)22 void l2_guest_code(void)
23 {
24 	GUEST_SYNC(6);
25 
26 	GUEST_SYNC(7);
27 
28 	/* Done, exit to L1 and never come back.  */
29 	vmcall();
30 }
31 
l1_guest_code(struct vmx_pages * vmx_pages)32 void l1_guest_code(struct vmx_pages *vmx_pages)
33 {
34 #define L2_GUEST_STACK_SIZE 64
35 	unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
36 
37 	enable_vp_assist(vmx_pages->vp_assist_gpa, vmx_pages->vp_assist);
38 
39 	GUEST_ASSERT(vmx_pages->vmcs_gpa);
40 	GUEST_ASSERT(prepare_for_vmx_operation(vmx_pages));
41 	GUEST_SYNC(3);
42 	GUEST_ASSERT(load_vmcs(vmx_pages));
43 	GUEST_ASSERT(vmptrstz() == vmx_pages->enlightened_vmcs_gpa);
44 
45 	GUEST_SYNC(4);
46 	GUEST_ASSERT(vmptrstz() == vmx_pages->enlightened_vmcs_gpa);
47 
48 	prepare_vmcs(vmx_pages, l2_guest_code,
49 		     &l2_guest_stack[L2_GUEST_STACK_SIZE]);
50 
51 	GUEST_SYNC(5);
52 	GUEST_ASSERT(vmptrstz() == vmx_pages->enlightened_vmcs_gpa);
53 	GUEST_ASSERT(!vmlaunch());
54 	GUEST_ASSERT(vmptrstz() == vmx_pages->enlightened_vmcs_gpa);
55 	GUEST_SYNC(8);
56 	GUEST_ASSERT(!vmresume());
57 	GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
58 	GUEST_SYNC(9);
59 }
60 
guest_code(struct vmx_pages * vmx_pages)61 void guest_code(struct vmx_pages *vmx_pages)
62 {
63 	GUEST_SYNC(1);
64 	GUEST_SYNC(2);
65 
66 	if (vmx_pages)
67 		l1_guest_code(vmx_pages);
68 
69 	GUEST_DONE();
70 }
71 
main(int argc,char * argv[])72 int main(int argc, char *argv[])
73 {
74 	vm_vaddr_t vmx_pages_gva = 0;
75 
76 	struct kvm_regs regs1, regs2;
77 	struct kvm_vm *vm;
78 	struct kvm_run *run;
79 	struct kvm_x86_state *state;
80 	struct ucall uc;
81 	int stage;
82 
83 	/* Create VM */
84 	vm = vm_create_default(VCPU_ID, 0, guest_code);
85 
86 	vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
87 
88 	if (!kvm_check_cap(KVM_CAP_NESTED_STATE) ||
89 	    !kvm_check_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS)) {
90 		printf("capabilities not available, skipping test\n");
91 		exit(KSFT_SKIP);
92 	}
93 
94 	vcpu_enable_evmcs(vm, VCPU_ID);
95 
96 	run = vcpu_state(vm, VCPU_ID);
97 
98 	vcpu_regs_get(vm, VCPU_ID, &regs1);
99 
100 	vcpu_alloc_vmx(vm, &vmx_pages_gva);
101 	vcpu_args_set(vm, VCPU_ID, 1, vmx_pages_gva);
102 
103 	for (stage = 1;; stage++) {
104 		_vcpu_run(vm, VCPU_ID);
105 		TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
106 			    "Stage %d: unexpected exit reason: %u (%s),\n",
107 			    stage, run->exit_reason,
108 			    exit_reason_str(run->exit_reason));
109 
110 		switch (get_ucall(vm, VCPU_ID, &uc)) {
111 		case UCALL_ABORT:
112 			TEST_ASSERT(false, "%s at %s:%d", (const char *)uc.args[0],
113 				    __FILE__, uc.args[1]);
114 			/* NOT REACHED */
115 		case UCALL_SYNC:
116 			break;
117 		case UCALL_DONE:
118 			goto done;
119 		default:
120 			TEST_ASSERT(false, "Unknown ucall 0x%x.", uc.cmd);
121 		}
122 
123 		/* UCALL_SYNC is handled here.  */
124 		TEST_ASSERT(!strcmp((const char *)uc.args[0], "hello") &&
125 			    uc.args[1] == stage, "Unexpected register values vmexit #%lx, got %lx",
126 			    stage, (ulong)uc.args[1]);
127 
128 		state = vcpu_save_state(vm, VCPU_ID);
129 		memset(&regs1, 0, sizeof(regs1));
130 		vcpu_regs_get(vm, VCPU_ID, &regs1);
131 
132 		kvm_vm_release(vm);
133 
134 		/* Restore state in a new VM.  */
135 		kvm_vm_restart(vm, O_RDWR);
136 		vm_vcpu_add(vm, VCPU_ID);
137 		vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
138 		vcpu_enable_evmcs(vm, VCPU_ID);
139 		vcpu_load_state(vm, VCPU_ID, state);
140 		run = vcpu_state(vm, VCPU_ID);
141 		free(state);
142 
143 		memset(&regs2, 0, sizeof(regs2));
144 		vcpu_regs_get(vm, VCPU_ID, &regs2);
145 		TEST_ASSERT(!memcmp(&regs1, &regs2, sizeof(regs2)),
146 			    "Unexpected register values after vcpu_load_state; rdi: %lx rsi: %lx",
147 			    (ulong) regs2.rdi, (ulong) regs2.rsi);
148 	}
149 
150 done:
151 	kvm_vm_free(vm);
152 }
153