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  */
guest_code(void)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 
compare_regs(struct kvm_regs * left,struct kvm_regs * right)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 
compare_sregs(struct kvm_sregs * left,struct kvm_sregs * right)71 static void compare_sregs(struct kvm_sregs *left, struct kvm_sregs *right)
72 {
73 }
74 
compare_vcpu_events(struct kvm_vcpu_events * left,struct kvm_vcpu_events * right)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 
main(int argc,char * argv[])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 	cap = kvm_check_cap(KVM_CAP_SYNC_REGS);
94 	TEST_REQUIRE((cap & TEST_SYNC_FIELDS) == TEST_SYNC_FIELDS);
95 	TEST_REQUIRE(!(cap & INVALID_SYNC_FIELD));
96 
97 	vm = vm_create_with_one_vcpu(&vcpu, guest_code);
98 
99 	run = vcpu->run;
100 
101 	/* Request reading invalid register set from VCPU. */
102 	run->kvm_valid_regs = INVALID_SYNC_FIELD;
103 	rv = _vcpu_run(vcpu);
104 	TEST_ASSERT(rv < 0 && errno == EINVAL,
105 		    "Invalid kvm_valid_regs did not cause expected KVM_RUN error: %d\n",
106 		    rv);
107 	run->kvm_valid_regs = 0;
108 
109 	run->kvm_valid_regs = INVALID_SYNC_FIELD | TEST_SYNC_FIELDS;
110 	rv = _vcpu_run(vcpu);
111 	TEST_ASSERT(rv < 0 && errno == EINVAL,
112 		    "Invalid kvm_valid_regs did not cause expected KVM_RUN error: %d\n",
113 		    rv);
114 	run->kvm_valid_regs = 0;
115 
116 	/* Request setting invalid register set into VCPU. */
117 	run->kvm_dirty_regs = INVALID_SYNC_FIELD;
118 	rv = _vcpu_run(vcpu);
119 	TEST_ASSERT(rv < 0 && errno == EINVAL,
120 		    "Invalid kvm_dirty_regs did not cause expected KVM_RUN error: %d\n",
121 		    rv);
122 	run->kvm_dirty_regs = 0;
123 
124 	run->kvm_dirty_regs = INVALID_SYNC_FIELD | TEST_SYNC_FIELDS;
125 	rv = _vcpu_run(vcpu);
126 	TEST_ASSERT(rv < 0 && errno == EINVAL,
127 		    "Invalid kvm_dirty_regs did not cause expected KVM_RUN error: %d\n",
128 		    rv);
129 	run->kvm_dirty_regs = 0;
130 
131 	/* Request and verify all valid register sets. */
132 	/* TODO: BUILD TIME CHECK: TEST_ASSERT(KVM_SYNC_X86_NUM_FIELDS != 3); */
133 	run->kvm_valid_regs = TEST_SYNC_FIELDS;
134 	rv = _vcpu_run(vcpu);
135 	TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
136 		    "Unexpected exit reason: %u (%s),\n",
137 		    run->exit_reason,
138 		    exit_reason_str(run->exit_reason));
139 
140 	vcpu_regs_get(vcpu, &regs);
141 	compare_regs(&regs, &run->s.regs.regs);
142 
143 	vcpu_sregs_get(vcpu, &sregs);
144 	compare_sregs(&sregs, &run->s.regs.sregs);
145 
146 	vcpu_events_get(vcpu, &events);
147 	compare_vcpu_events(&events, &run->s.regs.events);
148 
149 	/* Set and verify various register values. */
150 	run->s.regs.regs.rbx = 0xBAD1DEA;
151 	run->s.regs.sregs.apic_base = 1 << 11;
152 	/* TODO run->s.regs.events.XYZ = ABC; */
153 
154 	run->kvm_valid_regs = TEST_SYNC_FIELDS;
155 	run->kvm_dirty_regs = KVM_SYNC_X86_REGS | KVM_SYNC_X86_SREGS;
156 	rv = _vcpu_run(vcpu);
157 	TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
158 		    "Unexpected exit reason: %u (%s),\n",
159 		    run->exit_reason,
160 		    exit_reason_str(run->exit_reason));
161 	TEST_ASSERT(run->s.regs.regs.rbx == 0xBAD1DEA + 1,
162 		    "rbx sync regs value incorrect 0x%llx.",
163 		    run->s.regs.regs.rbx);
164 	TEST_ASSERT(run->s.regs.sregs.apic_base == 1 << 11,
165 		    "apic_base sync regs value incorrect 0x%llx.",
166 		    run->s.regs.sregs.apic_base);
167 
168 	vcpu_regs_get(vcpu, &regs);
169 	compare_regs(&regs, &run->s.regs.regs);
170 
171 	vcpu_sregs_get(vcpu, &sregs);
172 	compare_sregs(&sregs, &run->s.regs.sregs);
173 
174 	vcpu_events_get(vcpu, &events);
175 	compare_vcpu_events(&events, &run->s.regs.events);
176 
177 	/* Clear kvm_dirty_regs bits, verify new s.regs values are
178 	 * overwritten with existing guest values.
179 	 */
180 	run->kvm_valid_regs = TEST_SYNC_FIELDS;
181 	run->kvm_dirty_regs = 0;
182 	run->s.regs.regs.rbx = 0xDEADBEEF;
183 	rv = _vcpu_run(vcpu);
184 	TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
185 		    "Unexpected exit reason: %u (%s),\n",
186 		    run->exit_reason,
187 		    exit_reason_str(run->exit_reason));
188 	TEST_ASSERT(run->s.regs.regs.rbx != 0xDEADBEEF,
189 		    "rbx sync regs value incorrect 0x%llx.",
190 		    run->s.regs.regs.rbx);
191 
192 	/* Clear kvm_valid_regs bits and kvm_dirty_bits.
193 	 * Verify s.regs values are not overwritten with existing guest values
194 	 * and that guest values are not overwritten with kvm_sync_regs values.
195 	 */
196 	run->kvm_valid_regs = 0;
197 	run->kvm_dirty_regs = 0;
198 	run->s.regs.regs.rbx = 0xAAAA;
199 	regs.rbx = 0xBAC0;
200 	vcpu_regs_set(vcpu, &regs);
201 	rv = _vcpu_run(vcpu);
202 	TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
203 		    "Unexpected exit reason: %u (%s),\n",
204 		    run->exit_reason,
205 		    exit_reason_str(run->exit_reason));
206 	TEST_ASSERT(run->s.regs.regs.rbx == 0xAAAA,
207 		    "rbx sync regs value incorrect 0x%llx.",
208 		    run->s.regs.regs.rbx);
209 	vcpu_regs_get(vcpu, &regs);
210 	TEST_ASSERT(regs.rbx == 0xBAC0 + 1,
211 		    "rbx guest value incorrect 0x%llx.",
212 		    regs.rbx);
213 
214 	/* Clear kvm_valid_regs bits. Verify s.regs values are not overwritten
215 	 * with existing guest values but that guest values are overwritten
216 	 * with kvm_sync_regs values.
217 	 */
218 	run->kvm_valid_regs = 0;
219 	run->kvm_dirty_regs = TEST_SYNC_FIELDS;
220 	run->s.regs.regs.rbx = 0xBBBB;
221 	rv = _vcpu_run(vcpu);
222 	TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
223 		    "Unexpected exit reason: %u (%s),\n",
224 		    run->exit_reason,
225 		    exit_reason_str(run->exit_reason));
226 	TEST_ASSERT(run->s.regs.regs.rbx == 0xBBBB,
227 		    "rbx sync regs value incorrect 0x%llx.",
228 		    run->s.regs.regs.rbx);
229 	vcpu_regs_get(vcpu, &regs);
230 	TEST_ASSERT(regs.rbx == 0xBBBB + 1,
231 		    "rbx guest value incorrect 0x%llx.",
232 		    regs.rbx);
233 
234 	kvm_vm_free(vm);
235 
236 	return 0;
237 }
238