1 // SPDX-License-Identifier: GPL-2.0-only
3 * Test for x86 KVM_CAP_SYNC_REGS
5 * Copyright (C) 2018, Google LLC.
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.
12 #define _GNU_SOURCE /* for program_invocation_short_name */
17 #include <sys/ioctl.h>
19 #include "test_util.h"
21 #include "processor.h"
25 #define UCALL_PIO_PORT ((uint16_t)0x1000)
27 struct ucall uc_none = {
32 * ucall is embedded here to protect against compiler reshuffling registers
33 * before calling a function. In this test we only need to get KVM_EXIT_IO
34 * vmexit and preserve RBX, no additional information is needed.
38 asm volatile("1: in %[port], %%al\n"
41 : : [port] "d" (UCALL_PIO_PORT), "D" (&uc_none)
45 static void compare_regs(struct kvm_regs *left, struct kvm_regs *right)
47 #define REG_COMPARE(reg) \
48 TEST_ASSERT(left->reg == right->reg, \
50 " values did not match: 0x%llx, 0x%llx\n", \
51 left->reg, right->reg)
73 static void compare_sregs(struct kvm_sregs *left, struct kvm_sregs *right)
77 static void compare_vcpu_events(struct kvm_vcpu_events *left,
78 struct kvm_vcpu_events *right)
82 #define TEST_SYNC_FIELDS (KVM_SYNC_X86_REGS|KVM_SYNC_X86_SREGS|KVM_SYNC_X86_EVENTS)
83 #define INVALID_SYNC_FIELD 0x80000000
85 int main(int argc, char *argv[])
90 struct kvm_sregs sregs;
91 struct kvm_vcpu_events events;
94 /* Tell stdout not to buffer its content */
97 cap = kvm_check_cap(KVM_CAP_SYNC_REGS);
98 if ((cap & TEST_SYNC_FIELDS) != TEST_SYNC_FIELDS) {
99 print_skip("KVM_CAP_SYNC_REGS not supported");
102 if ((cap & INVALID_SYNC_FIELD) != 0) {
103 print_skip("The \"invalid\" field is not invalid");
108 vm = vm_create_default(VCPU_ID, 0, guest_code);
110 run = vcpu_state(vm, VCPU_ID);
112 /* Request reading invalid register set from VCPU. */
113 run->kvm_valid_regs = INVALID_SYNC_FIELD;
114 rv = _vcpu_run(vm, VCPU_ID);
115 TEST_ASSERT(rv < 0 && errno == EINVAL,
116 "Invalid kvm_valid_regs did not cause expected KVM_RUN error: %d\n",
118 vcpu_state(vm, VCPU_ID)->kvm_valid_regs = 0;
120 run->kvm_valid_regs = INVALID_SYNC_FIELD | TEST_SYNC_FIELDS;
121 rv = _vcpu_run(vm, VCPU_ID);
122 TEST_ASSERT(rv < 0 && errno == EINVAL,
123 "Invalid kvm_valid_regs did not cause expected KVM_RUN error: %d\n",
125 vcpu_state(vm, VCPU_ID)->kvm_valid_regs = 0;
127 /* Request setting invalid register set into VCPU. */
128 run->kvm_dirty_regs = INVALID_SYNC_FIELD;
129 rv = _vcpu_run(vm, VCPU_ID);
130 TEST_ASSERT(rv < 0 && errno == EINVAL,
131 "Invalid kvm_dirty_regs did not cause expected KVM_RUN error: %d\n",
133 vcpu_state(vm, VCPU_ID)->kvm_dirty_regs = 0;
135 run->kvm_dirty_regs = INVALID_SYNC_FIELD | TEST_SYNC_FIELDS;
136 rv = _vcpu_run(vm, VCPU_ID);
137 TEST_ASSERT(rv < 0 && errno == EINVAL,
138 "Invalid kvm_dirty_regs did not cause expected KVM_RUN error: %d\n",
140 vcpu_state(vm, VCPU_ID)->kvm_dirty_regs = 0;
142 /* Request and verify all valid register sets. */
143 /* TODO: BUILD TIME CHECK: TEST_ASSERT(KVM_SYNC_X86_NUM_FIELDS != 3); */
144 run->kvm_valid_regs = TEST_SYNC_FIELDS;
145 rv = _vcpu_run(vm, VCPU_ID);
146 TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
147 "Unexpected exit reason: %u (%s),\n",
149 exit_reason_str(run->exit_reason));
151 vcpu_regs_get(vm, VCPU_ID, ®s);
152 compare_regs(®s, &run->s.regs.regs);
154 vcpu_sregs_get(vm, VCPU_ID, &sregs);
155 compare_sregs(&sregs, &run->s.regs.sregs);
157 vcpu_events_get(vm, VCPU_ID, &events);
158 compare_vcpu_events(&events, &run->s.regs.events);
160 /* Set and verify various register values. */
161 run->s.regs.regs.rbx = 0xBAD1DEA;
162 run->s.regs.sregs.apic_base = 1 << 11;
163 /* TODO run->s.regs.events.XYZ = ABC; */
165 run->kvm_valid_regs = TEST_SYNC_FIELDS;
166 run->kvm_dirty_regs = KVM_SYNC_X86_REGS | KVM_SYNC_X86_SREGS;
167 rv = _vcpu_run(vm, VCPU_ID);
168 TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
169 "Unexpected exit reason: %u (%s),\n",
171 exit_reason_str(run->exit_reason));
172 TEST_ASSERT(run->s.regs.regs.rbx == 0xBAD1DEA + 1,
173 "rbx sync regs value incorrect 0x%llx.",
174 run->s.regs.regs.rbx);
175 TEST_ASSERT(run->s.regs.sregs.apic_base == 1 << 11,
176 "apic_base sync regs value incorrect 0x%llx.",
177 run->s.regs.sregs.apic_base);
179 vcpu_regs_get(vm, VCPU_ID, ®s);
180 compare_regs(®s, &run->s.regs.regs);
182 vcpu_sregs_get(vm, VCPU_ID, &sregs);
183 compare_sregs(&sregs, &run->s.regs.sregs);
185 vcpu_events_get(vm, VCPU_ID, &events);
186 compare_vcpu_events(&events, &run->s.regs.events);
188 /* Clear kvm_dirty_regs bits, verify new s.regs values are
189 * overwritten with existing guest values.
191 run->kvm_valid_regs = TEST_SYNC_FIELDS;
192 run->kvm_dirty_regs = 0;
193 run->s.regs.regs.rbx = 0xDEADBEEF;
194 rv = _vcpu_run(vm, VCPU_ID);
195 TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
196 "Unexpected exit reason: %u (%s),\n",
198 exit_reason_str(run->exit_reason));
199 TEST_ASSERT(run->s.regs.regs.rbx != 0xDEADBEEF,
200 "rbx sync regs value incorrect 0x%llx.",
201 run->s.regs.regs.rbx);
203 /* Clear kvm_valid_regs bits and kvm_dirty_bits.
204 * Verify s.regs values are not overwritten with existing guest values
205 * and that guest values are not overwritten with kvm_sync_regs values.
207 run->kvm_valid_regs = 0;
208 run->kvm_dirty_regs = 0;
209 run->s.regs.regs.rbx = 0xAAAA;
211 vcpu_regs_set(vm, VCPU_ID, ®s);
212 rv = _vcpu_run(vm, VCPU_ID);
213 TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
214 "Unexpected exit reason: %u (%s),\n",
216 exit_reason_str(run->exit_reason));
217 TEST_ASSERT(run->s.regs.regs.rbx == 0xAAAA,
218 "rbx sync regs value incorrect 0x%llx.",
219 run->s.regs.regs.rbx);
220 vcpu_regs_get(vm, VCPU_ID, ®s);
221 TEST_ASSERT(regs.rbx == 0xBAC0 + 1,
222 "rbx guest value incorrect 0x%llx.",
225 /* Clear kvm_valid_regs bits. Verify s.regs values are not overwritten
226 * with existing guest values but that guest values are overwritten
227 * with kvm_sync_regs values.
229 run->kvm_valid_regs = 0;
230 run->kvm_dirty_regs = TEST_SYNC_FIELDS;
231 run->s.regs.regs.rbx = 0xBBBB;
232 rv = _vcpu_run(vm, VCPU_ID);
233 TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
234 "Unexpected exit reason: %u (%s),\n",
236 exit_reason_str(run->exit_reason));
237 TEST_ASSERT(run->s.regs.regs.rbx == 0xBBBB,
238 "rbx sync regs value incorrect 0x%llx.",
239 run->s.regs.regs.rbx);
240 vcpu_regs_get(vm, VCPU_ID, ®s);
241 TEST_ASSERT(regs.rbx == 0xBBBB + 1,
242 "rbx guest value incorrect 0x%llx.",