1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Test for x86 KVM_CAP_SYNC_REGS
4  *
5  * Copyright (C) 2018, Google LLC.
6  *
7  * Verifies expected behavior of x86 KVM_CAP_SYNC_REGS functionality,
8  * including requesting an invalid register set, updates to/from values
9  * in kvm_run.s.regs when kvm_valid_regs and kvm_dirty_regs are toggled.
10  */
11 
12 #define _GNU_SOURCE /* for program_invocation_short_name */
13 #include <fcntl.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <sys/ioctl.h>
18 
19 #include "test_util.h"
20 #include "kvm_util.h"
21 #include "processor.h"
22 
23 #define UCALL_PIO_PORT ((uint16_t)0x1000)
24 
25 struct ucall uc_none = {
26 	.cmd = UCALL_NONE,
27 };
28 
29 /*
30  * ucall is embedded here to protect against compiler reshuffling registers
31  * before calling a function. In this test we only need to get KVM_EXIT_IO
32  * vmexit and preserve RBX, no additional information is needed.
33  */
34 void guest_code(void)
35 {
36 	asm volatile("1: in %[port], %%al\n"
37 		     "add $0x1, %%rbx\n"
38 		     "jmp 1b"
39 		     : : [port] "d" (UCALL_PIO_PORT), "D" (&uc_none)
40 		     : "rax", "rbx");
41 }
42 
43 static void compare_regs(struct kvm_regs *left, struct kvm_regs *right)
44 {
45 #define REG_COMPARE(reg) \
46 	TEST_ASSERT(left->reg == right->reg, \
47 		    "Register " #reg \
48 		    " values did not match: 0x%llx, 0x%llx\n", \
49 		    left->reg, right->reg)
50 	REG_COMPARE(rax);
51 	REG_COMPARE(rbx);
52 	REG_COMPARE(rcx);
53 	REG_COMPARE(rdx);
54 	REG_COMPARE(rsi);
55 	REG_COMPARE(rdi);
56 	REG_COMPARE(rsp);
57 	REG_COMPARE(rbp);
58 	REG_COMPARE(r8);
59 	REG_COMPARE(r9);
60 	REG_COMPARE(r10);
61 	REG_COMPARE(r11);
62 	REG_COMPARE(r12);
63 	REG_COMPARE(r13);
64 	REG_COMPARE(r14);
65 	REG_COMPARE(r15);
66 	REG_COMPARE(rip);
67 	REG_COMPARE(rflags);
68 #undef REG_COMPARE
69 }
70 
71 static void compare_sregs(struct kvm_sregs *left, struct kvm_sregs *right)
72 {
73 }
74 
75 static void compare_vcpu_events(struct kvm_vcpu_events *left,
76 				struct kvm_vcpu_events *right)
77 {
78 }
79 
80 #define TEST_SYNC_FIELDS   (KVM_SYNC_X86_REGS|KVM_SYNC_X86_SREGS|KVM_SYNC_X86_EVENTS)
81 #define INVALID_SYNC_FIELD 0x80000000
82 
83 int main(int argc, char *argv[])
84 {
85 	struct kvm_vcpu *vcpu;
86 	struct kvm_vm *vm;
87 	struct kvm_run *run;
88 	struct kvm_regs regs;
89 	struct kvm_sregs sregs;
90 	struct kvm_vcpu_events events;
91 	int rv, cap;
92 
93 	/* Tell stdout not to buffer its content */
94 	setbuf(stdout, NULL);
95 
96 	cap = kvm_check_cap(KVM_CAP_SYNC_REGS);
97 	TEST_REQUIRE((cap & TEST_SYNC_FIELDS) == TEST_SYNC_FIELDS);
98 	TEST_REQUIRE(!(cap & INVALID_SYNC_FIELD));
99 
100 	vm = vm_create_with_one_vcpu(&vcpu, guest_code);
101 
102 	run = vcpu->run;
103 
104 	/* Request reading invalid register set from VCPU. */
105 	run->kvm_valid_regs = INVALID_SYNC_FIELD;
106 	rv = _vcpu_run(vcpu);
107 	TEST_ASSERT(rv < 0 && errno == EINVAL,
108 		    "Invalid kvm_valid_regs did not cause expected KVM_RUN error: %d\n",
109 		    rv);
110 	run->kvm_valid_regs = 0;
111 
112 	run->kvm_valid_regs = INVALID_SYNC_FIELD | TEST_SYNC_FIELDS;
113 	rv = _vcpu_run(vcpu);
114 	TEST_ASSERT(rv < 0 && errno == EINVAL,
115 		    "Invalid kvm_valid_regs did not cause expected KVM_RUN error: %d\n",
116 		    rv);
117 	run->kvm_valid_regs = 0;
118 
119 	/* Request setting invalid register set into VCPU. */
120 	run->kvm_dirty_regs = INVALID_SYNC_FIELD;
121 	rv = _vcpu_run(vcpu);
122 	TEST_ASSERT(rv < 0 && errno == EINVAL,
123 		    "Invalid kvm_dirty_regs did not cause expected KVM_RUN error: %d\n",
124 		    rv);
125 	run->kvm_dirty_regs = 0;
126 
127 	run->kvm_dirty_regs = INVALID_SYNC_FIELD | TEST_SYNC_FIELDS;
128 	rv = _vcpu_run(vcpu);
129 	TEST_ASSERT(rv < 0 && errno == EINVAL,
130 		    "Invalid kvm_dirty_regs did not cause expected KVM_RUN error: %d\n",
131 		    rv);
132 	run->kvm_dirty_regs = 0;
133 
134 	/* Request and verify all valid register sets. */
135 	/* TODO: BUILD TIME CHECK: TEST_ASSERT(KVM_SYNC_X86_NUM_FIELDS != 3); */
136 	run->kvm_valid_regs = TEST_SYNC_FIELDS;
137 	rv = _vcpu_run(vcpu);
138 	TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
139 		    "Unexpected exit reason: %u (%s),\n",
140 		    run->exit_reason,
141 		    exit_reason_str(run->exit_reason));
142 
143 	vcpu_regs_get(vcpu, &regs);
144 	compare_regs(&regs, &run->s.regs.regs);
145 
146 	vcpu_sregs_get(vcpu, &sregs);
147 	compare_sregs(&sregs, &run->s.regs.sregs);
148 
149 	vcpu_events_get(vcpu, &events);
150 	compare_vcpu_events(&events, &run->s.regs.events);
151 
152 	/* Set and verify various register values. */
153 	run->s.regs.regs.rbx = 0xBAD1DEA;
154 	run->s.regs.sregs.apic_base = 1 << 11;
155 	/* TODO run->s.regs.events.XYZ = ABC; */
156 
157 	run->kvm_valid_regs = TEST_SYNC_FIELDS;
158 	run->kvm_dirty_regs = KVM_SYNC_X86_REGS | KVM_SYNC_X86_SREGS;
159 	rv = _vcpu_run(vcpu);
160 	TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
161 		    "Unexpected exit reason: %u (%s),\n",
162 		    run->exit_reason,
163 		    exit_reason_str(run->exit_reason));
164 	TEST_ASSERT(run->s.regs.regs.rbx == 0xBAD1DEA + 1,
165 		    "rbx sync regs value incorrect 0x%llx.",
166 		    run->s.regs.regs.rbx);
167 	TEST_ASSERT(run->s.regs.sregs.apic_base == 1 << 11,
168 		    "apic_base sync regs value incorrect 0x%llx.",
169 		    run->s.regs.sregs.apic_base);
170 
171 	vcpu_regs_get(vcpu, &regs);
172 	compare_regs(&regs, &run->s.regs.regs);
173 
174 	vcpu_sregs_get(vcpu, &sregs);
175 	compare_sregs(&sregs, &run->s.regs.sregs);
176 
177 	vcpu_events_get(vcpu, &events);
178 	compare_vcpu_events(&events, &run->s.regs.events);
179 
180 	/* Clear kvm_dirty_regs bits, verify new s.regs values are
181 	 * overwritten with existing guest values.
182 	 */
183 	run->kvm_valid_regs = TEST_SYNC_FIELDS;
184 	run->kvm_dirty_regs = 0;
185 	run->s.regs.regs.rbx = 0xDEADBEEF;
186 	rv = _vcpu_run(vcpu);
187 	TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
188 		    "Unexpected exit reason: %u (%s),\n",
189 		    run->exit_reason,
190 		    exit_reason_str(run->exit_reason));
191 	TEST_ASSERT(run->s.regs.regs.rbx != 0xDEADBEEF,
192 		    "rbx sync regs value incorrect 0x%llx.",
193 		    run->s.regs.regs.rbx);
194 
195 	/* Clear kvm_valid_regs bits and kvm_dirty_bits.
196 	 * Verify s.regs values are not overwritten with existing guest values
197 	 * and that guest values are not overwritten with kvm_sync_regs values.
198 	 */
199 	run->kvm_valid_regs = 0;
200 	run->kvm_dirty_regs = 0;
201 	run->s.regs.regs.rbx = 0xAAAA;
202 	regs.rbx = 0xBAC0;
203 	vcpu_regs_set(vcpu, &regs);
204 	rv = _vcpu_run(vcpu);
205 	TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
206 		    "Unexpected exit reason: %u (%s),\n",
207 		    run->exit_reason,
208 		    exit_reason_str(run->exit_reason));
209 	TEST_ASSERT(run->s.regs.regs.rbx == 0xAAAA,
210 		    "rbx sync regs value incorrect 0x%llx.",
211 		    run->s.regs.regs.rbx);
212 	vcpu_regs_get(vcpu, &regs);
213 	TEST_ASSERT(regs.rbx == 0xBAC0 + 1,
214 		    "rbx guest value incorrect 0x%llx.",
215 		    regs.rbx);
216 
217 	/* Clear kvm_valid_regs bits. Verify s.regs values are not overwritten
218 	 * with existing guest values but that guest values are overwritten
219 	 * with kvm_sync_regs values.
220 	 */
221 	run->kvm_valid_regs = 0;
222 	run->kvm_dirty_regs = TEST_SYNC_FIELDS;
223 	run->s.regs.regs.rbx = 0xBBBB;
224 	rv = _vcpu_run(vcpu);
225 	TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
226 		    "Unexpected exit reason: %u (%s),\n",
227 		    run->exit_reason,
228 		    exit_reason_str(run->exit_reason));
229 	TEST_ASSERT(run->s.regs.regs.rbx == 0xBBBB,
230 		    "rbx sync regs value incorrect 0x%llx.",
231 		    run->s.regs.regs.rbx);
232 	vcpu_regs_get(vcpu, &regs);
233 	TEST_ASSERT(regs.rbx == 0xBBBB + 1,
234 		    "rbx guest value incorrect 0x%llx.",
235 		    regs.rbx);
236 
237 	kvm_vm_free(vm);
238 
239 	return 0;
240 }
241