2 * Test for x86 KVM_CAP_SYNC_REGS
4 * Copyright (C) 2018, Google LLC.
6 * This work is licensed under the terms of the GNU GPL, version 2.
8 * Verifies expected behavior of x86 KVM_CAP_SYNC_REGS functionality,
9 * including requesting an invalid register set, updates to/from values
10 * in kvm_run.s.regs when kvm_valid_regs and kvm_dirty_regs are toggled.
13 #define _GNU_SOURCE /* for program_invocation_short_name */
18 #include <sys/ioctl.h>
20 #include "test_util.h"
30 asm volatile ("inc %r11");
34 static void compare_regs(struct kvm_regs
*left
, struct kvm_regs
*right
)
36 #define REG_COMPARE(reg) \
37 TEST_ASSERT(left->reg == right->reg, \
39 " values did not match: 0x%llx, 0x%llx\n", \
40 left->reg, right->reg)
62 static void compare_sregs(struct kvm_sregs
*left
, struct kvm_sregs
*right
)
66 static void compare_vcpu_events(struct kvm_vcpu_events
*left
,
67 struct kvm_vcpu_events
*right
)
71 #define TEST_SYNC_FIELDS (KVM_SYNC_X86_REGS|KVM_SYNC_X86_SREGS|KVM_SYNC_X86_EVENTS)
72 #define INVALID_SYNC_FIELD 0x80000000
74 int main(int argc
, char *argv
[])
79 struct kvm_sregs sregs
;
80 struct kvm_vcpu_events events
;
83 /* Tell stdout not to buffer its content */
86 cap
= kvm_check_cap(KVM_CAP_SYNC_REGS
);
87 if ((cap
& TEST_SYNC_FIELDS
) != TEST_SYNC_FIELDS
) {
88 fprintf(stderr
, "KVM_CAP_SYNC_REGS not supported, skipping test\n");
91 if ((cap
& INVALID_SYNC_FIELD
) != 0) {
92 fprintf(stderr
, "The \"invalid\" field is not invalid, skipping test\n");
97 vm
= vm_create_default(VCPU_ID
, 0, guest_code
);
99 run
= vcpu_state(vm
, VCPU_ID
);
101 /* Request reading invalid register set from VCPU. */
102 run
->kvm_valid_regs
= INVALID_SYNC_FIELD
;
103 rv
= _vcpu_run(vm
, VCPU_ID
);
104 TEST_ASSERT(rv
< 0 && errno
== EINVAL
,
105 "Invalid kvm_valid_regs did not cause expected KVM_RUN error: %d\n",
107 vcpu_state(vm
, VCPU_ID
)->kvm_valid_regs
= 0;
109 run
->kvm_valid_regs
= INVALID_SYNC_FIELD
| TEST_SYNC_FIELDS
;
110 rv
= _vcpu_run(vm
, VCPU_ID
);
111 TEST_ASSERT(rv
< 0 && errno
== EINVAL
,
112 "Invalid kvm_valid_regs did not cause expected KVM_RUN error: %d\n",
114 vcpu_state(vm
, VCPU_ID
)->kvm_valid_regs
= 0;
116 /* Request setting invalid register set into VCPU. */
117 run
->kvm_dirty_regs
= INVALID_SYNC_FIELD
;
118 rv
= _vcpu_run(vm
, VCPU_ID
);
119 TEST_ASSERT(rv
< 0 && errno
== EINVAL
,
120 "Invalid kvm_dirty_regs did not cause expected KVM_RUN error: %d\n",
122 vcpu_state(vm
, VCPU_ID
)->kvm_dirty_regs
= 0;
124 run
->kvm_dirty_regs
= INVALID_SYNC_FIELD
| TEST_SYNC_FIELDS
;
125 rv
= _vcpu_run(vm
, VCPU_ID
);
126 TEST_ASSERT(rv
< 0 && errno
== EINVAL
,
127 "Invalid kvm_dirty_regs did not cause expected KVM_RUN error: %d\n",
129 vcpu_state(vm
, VCPU_ID
)->kvm_dirty_regs
= 0;
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(vm
, VCPU_ID
);
135 TEST_ASSERT(run
->exit_reason
== KVM_EXIT_IO
,
136 "Unexpected exit reason: %u (%s),\n",
138 exit_reason_str(run
->exit_reason
));
140 vcpu_regs_get(vm
, VCPU_ID
, ®s
);
141 compare_regs(®s
, &run
->s
.regs
.regs
);
143 vcpu_sregs_get(vm
, VCPU_ID
, &sregs
);
144 compare_sregs(&sregs
, &run
->s
.regs
.sregs
);
146 vcpu_events_get(vm
, VCPU_ID
, &events
);
147 compare_vcpu_events(&events
, &run
->s
.regs
.events
);
149 /* Set and verify various register values. */
150 run
->s
.regs
.regs
.r11
= 0xBAD1DEA;
151 run
->s
.regs
.sregs
.apic_base
= 1 << 11;
152 /* TODO run->s.regs.events.XYZ = ABC; */
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(vm
, VCPU_ID
);
157 TEST_ASSERT(run
->exit_reason
== KVM_EXIT_IO
,
158 "Unexpected exit reason: %u (%s),\n",
160 exit_reason_str(run
->exit_reason
));
161 TEST_ASSERT(run
->s
.regs
.regs
.r11
== 0xBAD1DEA + 1,
162 "r11 sync regs value incorrect 0x%llx.",
163 run
->s
.regs
.regs
.r11
);
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
);
168 vcpu_regs_get(vm
, VCPU_ID
, ®s
);
169 compare_regs(®s
, &run
->s
.regs
.regs
);
171 vcpu_sregs_get(vm
, VCPU_ID
, &sregs
);
172 compare_sregs(&sregs
, &run
->s
.regs
.sregs
);
174 vcpu_events_get(vm
, VCPU_ID
, &events
);
175 compare_vcpu_events(&events
, &run
->s
.regs
.events
);
177 /* Clear kvm_dirty_regs bits, verify new s.regs values are
178 * overwritten with existing guest values.
180 run
->kvm_valid_regs
= TEST_SYNC_FIELDS
;
181 run
->kvm_dirty_regs
= 0;
182 run
->s
.regs
.regs
.r11
= 0xDEADBEEF;
183 rv
= _vcpu_run(vm
, VCPU_ID
);
184 TEST_ASSERT(run
->exit_reason
== KVM_EXIT_IO
,
185 "Unexpected exit reason: %u (%s),\n",
187 exit_reason_str(run
->exit_reason
));
188 TEST_ASSERT(run
->s
.regs
.regs
.r11
!= 0xDEADBEEF,
189 "r11 sync regs value incorrect 0x%llx.",
190 run
->s
.regs
.regs
.r11
);
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.
196 run
->kvm_valid_regs
= 0;
197 run
->kvm_dirty_regs
= 0;
198 run
->s
.regs
.regs
.r11
= 0xAAAA;
200 vcpu_regs_set(vm
, VCPU_ID
, ®s
);
201 rv
= _vcpu_run(vm
, VCPU_ID
);
202 TEST_ASSERT(run
->exit_reason
== KVM_EXIT_IO
,
203 "Unexpected exit reason: %u (%s),\n",
205 exit_reason_str(run
->exit_reason
));
206 TEST_ASSERT(run
->s
.regs
.regs
.r11
== 0xAAAA,
207 "r11 sync regs value incorrect 0x%llx.",
208 run
->s
.regs
.regs
.r11
);
209 vcpu_regs_get(vm
, VCPU_ID
, ®s
);
210 TEST_ASSERT(regs
.r11
== 0xBAC0 + 1,
211 "r11 guest value incorrect 0x%llx.",
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.
218 run
->kvm_valid_regs
= 0;
219 run
->kvm_dirty_regs
= TEST_SYNC_FIELDS
;
220 run
->s
.regs
.regs
.r11
= 0xBBBB;
221 rv
= _vcpu_run(vm
, VCPU_ID
);
222 TEST_ASSERT(run
->exit_reason
== KVM_EXIT_IO
,
223 "Unexpected exit reason: %u (%s),\n",
225 exit_reason_str(run
->exit_reason
));
226 TEST_ASSERT(run
->s
.regs
.regs
.r11
== 0xBBBB,
227 "r11 sync regs value incorrect 0x%llx.",
228 run
->s
.regs
.regs
.r11
);
229 vcpu_regs_get(vm
, VCPU_ID
, ®s
);
230 TEST_ASSERT(regs
.r11
== 0xBBBB + 1,
231 "r11 guest value incorrect 0x%llx.",