1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright © 2021 Amazon.com, Inc. or its affiliates.
7 * Xen shared_info / pvclock testing
10 #include "test_util.h"
12 #include "processor.h"
19 #include <sys/eventfd.h>
23 #define SHINFO_REGION_GVA 0xc0000000ULL
24 #define SHINFO_REGION_GPA 0xc0000000ULL
25 #define SHINFO_REGION_SLOT 10
27 #define DUMMY_REGION_GPA (SHINFO_REGION_GPA + (2 * PAGE_SIZE))
28 #define DUMMY_REGION_SLOT 11
30 #define SHINFO_ADDR (SHINFO_REGION_GPA)
31 #define PVTIME_ADDR (SHINFO_REGION_GPA + PAGE_SIZE)
32 #define RUNSTATE_ADDR (SHINFO_REGION_GPA + PAGE_SIZE + 0x20)
33 #define VCPU_INFO_ADDR (SHINFO_REGION_GPA + 0x40)
35 #define SHINFO_VADDR (SHINFO_REGION_GVA)
36 #define RUNSTATE_VADDR (SHINFO_REGION_GVA + PAGE_SIZE + 0x20)
37 #define VCPU_INFO_VADDR (SHINFO_REGION_GVA + 0x40)
39 #define EVTCHN_VECTOR 0x10
41 static struct kvm_vm *vm;
43 #define XEN_HYPERCALL_MSR 0x40000000
45 #define MIN_STEAL_TIME 50000
47 struct pvclock_vcpu_time_info {
52 u32 tsc_to_system_mul;
56 } __attribute__((__packed__)); /* 32 bytes */
58 struct pvclock_wall_clock {
62 } __attribute__((__packed__));
64 struct vcpu_runstate_info {
66 uint64_t state_entry_time;
70 struct arch_vcpu_info {
72 unsigned long pad; /* sizeof(vcpu_info_t) == 64 */
76 uint8_t evtchn_upcall_pending;
77 uint8_t evtchn_upcall_mask;
78 unsigned long evtchn_pending_sel;
79 struct arch_vcpu_info arch;
80 struct pvclock_vcpu_time_info time;
81 }; /* 64 bytes (x86) */
84 struct vcpu_info vcpu_info[32];
85 unsigned long evtchn_pending[64];
86 unsigned long evtchn_mask[64];
87 struct pvclock_wall_clock wc;
89 /* arch_shared_info here */
92 #define RUNSTATE_running 0
93 #define RUNSTATE_runnable 1
94 #define RUNSTATE_blocked 2
95 #define RUNSTATE_offline 3
97 static const char *runstate_names[] = {
105 struct kvm_irq_routing info;
106 struct kvm_irq_routing_entry entries[2];
109 static void evtchn_handler(struct ex_regs *regs)
111 struct vcpu_info *vi = (void *)VCPU_INFO_VADDR;
112 vi->evtchn_upcall_pending = 0;
113 vi->evtchn_pending_sel = 0;
118 static void guest_code(void)
120 struct vcpu_runstate_info *rs = (void *)RUNSTATE_VADDR;
122 __asm__ __volatile__(
127 /* Trigger an interrupt injection */
130 /* Test having the host set runstates manually */
131 GUEST_SYNC(RUNSTATE_runnable);
132 GUEST_ASSERT(rs->time[RUNSTATE_runnable] != 0);
133 GUEST_ASSERT(rs->state == 0);
135 GUEST_SYNC(RUNSTATE_blocked);
136 GUEST_ASSERT(rs->time[RUNSTATE_blocked] != 0);
137 GUEST_ASSERT(rs->state == 0);
139 GUEST_SYNC(RUNSTATE_offline);
140 GUEST_ASSERT(rs->time[RUNSTATE_offline] != 0);
141 GUEST_ASSERT(rs->state == 0);
143 /* Test runstate time adjust */
145 GUEST_ASSERT(rs->time[RUNSTATE_blocked] == 0x5a);
146 GUEST_ASSERT(rs->time[RUNSTATE_offline] == 0x6b6b);
148 /* Test runstate time set */
150 GUEST_ASSERT(rs->state_entry_time >= 0x8000);
151 GUEST_ASSERT(rs->time[RUNSTATE_runnable] == 0);
152 GUEST_ASSERT(rs->time[RUNSTATE_blocked] == 0x6b6b);
153 GUEST_ASSERT(rs->time[RUNSTATE_offline] == 0x5a);
155 /* sched_yield() should result in some 'runnable' time */
157 GUEST_ASSERT(rs->time[RUNSTATE_runnable] >= MIN_STEAL_TIME);
159 /* Attempt to deliver a *masked* interrupt */
162 /* Wait until we see the bit set */
163 struct shared_info *si = (void *)SHINFO_VADDR;
164 while (!si->evtchn_pending[0])
165 __asm__ __volatile__ ("rep nop" : : : "memory");
167 /* Now deliver an *unmasked* interrupt */
170 while (!si->evtchn_pending[1])
171 __asm__ __volatile__ ("rep nop" : : : "memory");
173 /* Change memslots and deliver an interrupt */
177 __asm__ __volatile__ ("rep nop" : : : "memory");
180 static int cmp_timespec(struct timespec *a, struct timespec *b)
182 if (a->tv_sec > b->tv_sec)
184 else if (a->tv_sec < b->tv_sec)
186 else if (a->tv_nsec > b->tv_nsec)
188 else if (a->tv_nsec < b->tv_nsec)
194 static void handle_alrm(int sig)
196 TEST_FAIL("IRQ delivery timed out");
199 int main(int argc, char *argv[])
201 struct timespec min_ts, max_ts, vm_ts;
204 verbose = argc > 1 && (!strncmp(argv[1], "-v", 3) ||
205 !strncmp(argv[1], "--verbose", 10));
207 int xen_caps = kvm_check_cap(KVM_CAP_XEN_HVM);
208 if (!(xen_caps & KVM_XEN_HVM_CONFIG_SHARED_INFO) ) {
209 print_skip("KVM_XEN_HVM_CONFIG_SHARED_INFO not available");
213 bool do_runstate_tests = !!(xen_caps & KVM_XEN_HVM_CONFIG_RUNSTATE);
214 bool do_eventfd_tests = !!(xen_caps & KVM_XEN_HVM_CONFIG_EVTCHN_2LEVEL);
216 clock_gettime(CLOCK_REALTIME, &min_ts);
218 vm = vm_create_default(VCPU_ID, 0, (void *) guest_code);
219 vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
221 /* Map a region for the shared_info page */
222 vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
223 SHINFO_REGION_GPA, SHINFO_REGION_SLOT, 2, 0);
224 virt_map(vm, SHINFO_REGION_GVA, SHINFO_REGION_GPA, 2);
226 struct shared_info *shinfo = addr_gpa2hva(vm, SHINFO_VADDR);
228 int zero_fd = open("/dev/zero", O_RDONLY);
229 TEST_ASSERT(zero_fd != -1, "Failed to open /dev/zero");
231 struct kvm_xen_hvm_config hvmc = {
232 .flags = KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL,
233 .msr = XEN_HYPERCALL_MSR,
235 vm_ioctl(vm, KVM_XEN_HVM_CONFIG, &hvmc);
237 struct kvm_xen_hvm_attr lm = {
238 .type = KVM_XEN_ATTR_TYPE_LONG_MODE,
241 vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &lm);
243 struct kvm_xen_hvm_attr ha = {
244 .type = KVM_XEN_ATTR_TYPE_SHARED_INFO,
245 .u.shared_info.gfn = SHINFO_REGION_GPA / PAGE_SIZE,
247 vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &ha);
250 * Test what happens when the HVA of the shinfo page is remapped after
251 * the kernel has a reference to it. But make sure we copy the clock
252 * info over since that's only set at setup time, and we test it later.
254 struct pvclock_wall_clock wc_copy = shinfo->wc;
255 void *m = mmap(shinfo, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_FIXED|MAP_PRIVATE, zero_fd, 0);
256 TEST_ASSERT(m == shinfo, "Failed to map /dev/zero over shared info");
257 shinfo->wc = wc_copy;
259 struct kvm_xen_vcpu_attr vi = {
260 .type = KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO,
261 .u.gpa = VCPU_INFO_ADDR,
263 vcpu_ioctl(vm, VCPU_ID, KVM_XEN_VCPU_SET_ATTR, &vi);
265 struct kvm_xen_vcpu_attr pvclock = {
266 .type = KVM_XEN_VCPU_ATTR_TYPE_VCPU_TIME_INFO,
267 .u.gpa = PVTIME_ADDR,
269 vcpu_ioctl(vm, VCPU_ID, KVM_XEN_VCPU_SET_ATTR, &pvclock);
271 struct kvm_xen_hvm_attr vec = {
272 .type = KVM_XEN_ATTR_TYPE_UPCALL_VECTOR,
273 .u.vector = EVTCHN_VECTOR,
275 vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &vec);
277 vm_init_descriptor_tables(vm);
278 vcpu_init_descriptor_tables(vm, VCPU_ID);
279 vm_install_exception_handler(vm, EVTCHN_VECTOR, evtchn_handler);
281 if (do_runstate_tests) {
282 struct kvm_xen_vcpu_attr st = {
283 .type = KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_ADDR,
284 .u.gpa = RUNSTATE_ADDR,
286 vcpu_ioctl(vm, VCPU_ID, KVM_XEN_VCPU_SET_ATTR, &st);
289 int irq_fd[2] = { -1, -1 };
291 if (do_eventfd_tests) {
292 irq_fd[0] = eventfd(0, 0);
293 irq_fd[1] = eventfd(0, 0);
295 /* Unexpected, but not a KVM failure */
296 if (irq_fd[0] == -1 || irq_fd[1] == -1)
297 do_eventfd_tests = false;
300 if (do_eventfd_tests) {
301 irq_routes.info.nr = 2;
303 irq_routes.entries[0].gsi = 32;
304 irq_routes.entries[0].type = KVM_IRQ_ROUTING_XEN_EVTCHN;
305 irq_routes.entries[0].u.xen_evtchn.port = 15;
306 irq_routes.entries[0].u.xen_evtchn.vcpu = VCPU_ID;
307 irq_routes.entries[0].u.xen_evtchn.priority = KVM_IRQ_ROUTING_XEN_EVTCHN_PRIO_2LEVEL;
309 irq_routes.entries[1].gsi = 33;
310 irq_routes.entries[1].type = KVM_IRQ_ROUTING_XEN_EVTCHN;
311 irq_routes.entries[1].u.xen_evtchn.port = 66;
312 irq_routes.entries[1].u.xen_evtchn.vcpu = VCPU_ID;
313 irq_routes.entries[1].u.xen_evtchn.priority = KVM_IRQ_ROUTING_XEN_EVTCHN_PRIO_2LEVEL;
315 vm_ioctl(vm, KVM_SET_GSI_ROUTING, &irq_routes);
317 struct kvm_irqfd ifd = { };
321 vm_ioctl(vm, KVM_IRQFD, &ifd);
325 vm_ioctl(vm, KVM_IRQFD, &ifd);
327 struct sigaction sa = { };
328 sa.sa_handler = handle_alrm;
329 sigaction(SIGALRM, &sa, NULL);
332 struct vcpu_info *vinfo = addr_gpa2hva(vm, VCPU_INFO_VADDR);
333 vinfo->evtchn_upcall_pending = 0;
335 struct vcpu_runstate_info *rs = addr_gpa2hva(vm, RUNSTATE_ADDR);
338 bool evtchn_irq_expected = false;
341 volatile struct kvm_run *run = vcpu_state(vm, VCPU_ID);
344 vcpu_run(vm, VCPU_ID);
346 TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
347 "Got exit_reason other than KVM_EXIT_IO: %u (%s)\n",
349 exit_reason_str(run->exit_reason));
351 switch (get_ucall(vm, VCPU_ID, &uc)) {
353 TEST_FAIL("%s", (const char *)uc.args[0]);
356 struct kvm_xen_vcpu_attr rst;
359 if (do_runstate_tests)
360 TEST_ASSERT(rs->state_entry_time == rs->time[0] +
361 rs->time[1] + rs->time[2] + rs->time[3],
362 "runstate times don't add up");
364 switch (uc.args[1]) {
367 printf("Delivering evtchn upcall\n");
368 evtchn_irq_expected = true;
369 vinfo->evtchn_upcall_pending = 1;
372 case RUNSTATE_runnable...RUNSTATE_offline:
373 TEST_ASSERT(!evtchn_irq_expected, "Event channel IRQ not seen");
374 if (!do_runstate_tests)
377 printf("Testing runstate %s\n", runstate_names[uc.args[1]]);
378 rst.type = KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_CURRENT;
379 rst.u.runstate.state = uc.args[1];
380 vcpu_ioctl(vm, VCPU_ID, KVM_XEN_VCPU_SET_ATTR, &rst);
385 printf("Testing RUNSTATE_ADJUST\n");
386 rst.type = KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_ADJUST;
387 memset(&rst.u, 0, sizeof(rst.u));
388 rst.u.runstate.state = (uint64_t)-1;
389 rst.u.runstate.time_blocked =
390 0x5a - rs->time[RUNSTATE_blocked];
391 rst.u.runstate.time_offline =
392 0x6b6b - rs->time[RUNSTATE_offline];
393 rst.u.runstate.time_runnable = -rst.u.runstate.time_blocked -
394 rst.u.runstate.time_offline;
395 vcpu_ioctl(vm, VCPU_ID, KVM_XEN_VCPU_SET_ATTR, &rst);
400 printf("Testing RUNSTATE_DATA\n");
401 rst.type = KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_DATA;
402 memset(&rst.u, 0, sizeof(rst.u));
403 rst.u.runstate.state = RUNSTATE_running;
404 rst.u.runstate.state_entry_time = 0x6b6b + 0x5a;
405 rst.u.runstate.time_blocked = 0x6b6b;
406 rst.u.runstate.time_offline = 0x5a;
407 vcpu_ioctl(vm, VCPU_ID, KVM_XEN_VCPU_SET_ATTR, &rst);
412 printf("Testing steal time\n");
413 /* Yield until scheduler delay exceeds target */
414 rundelay = get_run_delay() + MIN_STEAL_TIME;
417 } while (get_run_delay() < rundelay);
421 if (!do_eventfd_tests)
424 printf("Testing masked event channel\n");
425 shinfo->evtchn_mask[0] = 0x8000;
426 eventfd_write(irq_fd[0], 1UL);
432 printf("Testing unmasked event channel\n");
433 /* Unmask that, but deliver the other one */
434 shinfo->evtchn_pending[0] = 0;
435 shinfo->evtchn_mask[0] = 0;
436 eventfd_write(irq_fd[1], 1UL);
437 evtchn_irq_expected = true;
443 printf("Testing event channel after memslot change\n");
444 vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
445 DUMMY_REGION_GPA, DUMMY_REGION_SLOT, 1, 0);
446 eventfd_write(irq_fd[0], 1UL);
447 evtchn_irq_expected = true;
452 TEST_ASSERT(evtchn_irq_expected, "Unexpected event channel IRQ");
453 evtchn_irq_expected = false;
454 if (shinfo->evtchn_pending[1] &&
455 shinfo->evtchn_pending[0])
464 TEST_FAIL("Unknown ucall 0x%lx.", uc.cmd);
469 clock_gettime(CLOCK_REALTIME, &max_ts);
472 * Just a *really* basic check that things are being put in the
473 * right place. The actual calculations are much the same for
474 * Xen as they are for the KVM variants, so no need to check.
476 struct pvclock_wall_clock *wc;
477 struct pvclock_vcpu_time_info *ti, *ti2;
479 wc = addr_gpa2hva(vm, SHINFO_REGION_GPA + 0xc00);
480 ti = addr_gpa2hva(vm, SHINFO_REGION_GPA + 0x40 + 0x20);
481 ti2 = addr_gpa2hva(vm, PVTIME_ADDR);
484 printf("Wall clock (v %d) %d.%09d\n", wc->version, wc->sec, wc->nsec);
485 printf("Time info 1: v %u tsc %" PRIu64 " time %" PRIu64 " mul %u shift %u flags %x\n",
486 ti->version, ti->tsc_timestamp, ti->system_time, ti->tsc_to_system_mul,
487 ti->tsc_shift, ti->flags);
488 printf("Time info 2: v %u tsc %" PRIu64 " time %" PRIu64 " mul %u shift %u flags %x\n",
489 ti2->version, ti2->tsc_timestamp, ti2->system_time, ti2->tsc_to_system_mul,
490 ti2->tsc_shift, ti2->flags);
493 vm_ts.tv_sec = wc->sec;
494 vm_ts.tv_nsec = wc->nsec;
495 TEST_ASSERT(wc->version && !(wc->version & 1),
496 "Bad wallclock version %x", wc->version);
497 TEST_ASSERT(cmp_timespec(&min_ts, &vm_ts) <= 0, "VM time too old");
498 TEST_ASSERT(cmp_timespec(&max_ts, &vm_ts) >= 0, "VM time too new");
500 TEST_ASSERT(ti->version && !(ti->version & 1),
501 "Bad time_info version %x", ti->version);
502 TEST_ASSERT(ti2->version && !(ti2->version & 1),
503 "Bad time_info version %x", ti->version);
505 if (do_runstate_tests) {
507 * Fetch runstate and check sanity. Strictly speaking in the
508 * general case we might not expect the numbers to be identical
509 * but in this case we know we aren't running the vCPU any more.
511 struct kvm_xen_vcpu_attr rst = {
512 .type = KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_DATA,
514 vcpu_ioctl(vm, VCPU_ID, KVM_XEN_VCPU_GET_ATTR, &rst);
517 printf("Runstate: %s(%d), entry %" PRIu64 " ns\n",
518 rs->state <= RUNSTATE_offline ? runstate_names[rs->state] : "unknown",
519 rs->state, rs->state_entry_time);
520 for (int i = RUNSTATE_running; i <= RUNSTATE_offline; i++) {
521 printf("State %s: %" PRIu64 " ns\n",
522 runstate_names[i], rs->time[i]);
525 TEST_ASSERT(rs->state == rst.u.runstate.state, "Runstate mismatch");
526 TEST_ASSERT(rs->state_entry_time == rst.u.runstate.state_entry_time,
527 "State entry time mismatch");
528 TEST_ASSERT(rs->time[RUNSTATE_running] == rst.u.runstate.time_running,
529 "Running time mismatch");
530 TEST_ASSERT(rs->time[RUNSTATE_runnable] == rst.u.runstate.time_runnable,
531 "Runnable time mismatch");
532 TEST_ASSERT(rs->time[RUNSTATE_blocked] == rst.u.runstate.time_blocked,
533 "Blocked time mismatch");
534 TEST_ASSERT(rs->time[RUNSTATE_offline] == rst.u.runstate.time_offline,
535 "Offline time mismatch");
537 TEST_ASSERT(rs->state_entry_time == rs->time[0] +
538 rs->time[1] + rs->time[2] + rs->time[3],
539 "runstate times don't add up");