File Index
Files and directories
Sign in
FILE

xen_shinfo_test.c

File preview with syntax highlighting.

linux-6.19.14/tools/testing/selftests/kvm/x86/xen_shinfo_test.c C
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Copyright © 2021 Amazon.com, Inc. or its affiliates.
   4 */
   5
   6#include "test_util.h"
   7#include "kvm_util.h"
   8#include "processor.h"
   9
  10#include <stdint.h>
  11#include <time.h>
  12#include <sched.h>
  13#include <signal.h>
  14#include <pthread.h>
  15
  16#include <sys/eventfd.h>
  17
  18#define SHINFO_REGION_GVA	0xc0000000ULL
  19#define SHINFO_REGION_GPA	0xc0000000ULL
  20#define SHINFO_REGION_SLOT	10
  21
  22#define DUMMY_REGION_GPA	(SHINFO_REGION_GPA + (3 * PAGE_SIZE))
  23#define DUMMY_REGION_SLOT	11
  24
  25#define DUMMY_REGION_GPA_2	(SHINFO_REGION_GPA + (4 * PAGE_SIZE))
  26#define DUMMY_REGION_SLOT_2	12
  27
  28#define SHINFO_ADDR	(SHINFO_REGION_GPA)
  29#define VCPU_INFO_ADDR	(SHINFO_REGION_GPA + 0x40)
  30#define PVTIME_ADDR	(SHINFO_REGION_GPA + PAGE_SIZE)
  31#define RUNSTATE_ADDR	(SHINFO_REGION_GPA + PAGE_SIZE + PAGE_SIZE - 15)
  32
  33#define SHINFO_VADDR	(SHINFO_REGION_GVA)
  34#define VCPU_INFO_VADDR	(SHINFO_REGION_GVA + 0x40)
  35#define RUNSTATE_VADDR	(SHINFO_REGION_GVA + PAGE_SIZE + PAGE_SIZE - 15)
  36
  37#define EVTCHN_VECTOR	0x10
  38
  39#define EVTCHN_TEST1 15
  40#define EVTCHN_TEST2 66
  41#define EVTCHN_TIMER 13
  42
  43enum {
  44	TEST_INJECT_VECTOR = 0,
  45	TEST_RUNSTATE_runnable,
  46	TEST_RUNSTATE_blocked,
  47	TEST_RUNSTATE_offline,
  48	TEST_RUNSTATE_ADJUST,
  49	TEST_RUNSTATE_DATA,
  50	TEST_STEAL_TIME,
  51	TEST_EVTCHN_MASKED,
  52	TEST_EVTCHN_UNMASKED,
  53	TEST_EVTCHN_SLOWPATH,
  54	TEST_EVTCHN_SEND_IOCTL,
  55	TEST_EVTCHN_HCALL,
  56	TEST_EVTCHN_HCALL_SLOWPATH,
  57	TEST_EVTCHN_HCALL_EVENTFD,
  58	TEST_TIMER_SETUP,
  59	TEST_TIMER_WAIT,
  60	TEST_TIMER_RESTORE,
  61	TEST_POLL_READY,
  62	TEST_POLL_TIMEOUT,
  63	TEST_POLL_MASKED,
  64	TEST_POLL_WAKE,
  65	SET_VCPU_INFO,
  66	TEST_TIMER_PAST,
  67	TEST_LOCKING_SEND_RACE,
  68	TEST_LOCKING_POLL_RACE,
  69	TEST_LOCKING_POLL_TIMEOUT,
  70	TEST_DONE,
  71
  72	TEST_GUEST_SAW_IRQ,
  73};
  74
  75#define XEN_HYPERCALL_MSR	0x40000000
  76
  77#define MIN_STEAL_TIME		50000
  78
  79#define SHINFO_RACE_TIMEOUT	2	/* seconds */
  80
  81#define __HYPERVISOR_set_timer_op	15
  82#define __HYPERVISOR_sched_op		29
  83#define __HYPERVISOR_event_channel_op	32
  84
  85#define SCHEDOP_poll			3
  86
  87#define EVTCHNOP_send			4
  88
  89#define EVTCHNSTAT_interdomain		2
  90
  91struct evtchn_send {
  92	u32 port;
  93};
  94
  95struct sched_poll {
  96	u32 *ports;
  97	unsigned int nr_ports;
  98	u64 timeout;
  99};
 100
 101struct pvclock_vcpu_time_info {
 102	u32   version;
 103	u32   pad0;
 104	u64   tsc_timestamp;
 105	u64   system_time;
 106	u32   tsc_to_system_mul;
 107	s8    tsc_shift;
 108	u8    flags;
 109	u8    pad[2];
 110} __attribute__((__packed__)); /* 32 bytes */
 111
 112struct pvclock_wall_clock {
 113	u32   version;
 114	u32   sec;
 115	u32   nsec;
 116} __attribute__((__packed__));
 117
 118struct vcpu_runstate_info {
 119	uint32_t state;
 120	uint64_t state_entry_time;
 121	uint64_t time[5]; /* Extra field for overrun check */
 122};
 123
 124struct compat_vcpu_runstate_info {
 125	uint32_t state;
 126	uint64_t state_entry_time;
 127	uint64_t time[5];
 128} __attribute__((__packed__));
 129
 130struct arch_vcpu_info {
 131	unsigned long cr2;
 132	unsigned long pad; /* sizeof(vcpu_info_t) == 64 */
 133};
 134
 135struct vcpu_info {
 136	uint8_t evtchn_upcall_pending;
 137	uint8_t evtchn_upcall_mask;
 138	unsigned long evtchn_pending_sel;
 139	struct arch_vcpu_info arch;
 140	struct pvclock_vcpu_time_info time;
 141}; /* 64 bytes (x86) */
 142
 143struct shared_info {
 144	struct vcpu_info vcpu_info[32];
 145	unsigned long evtchn_pending[64];
 146	unsigned long evtchn_mask[64];
 147	struct pvclock_wall_clock wc;
 148	uint32_t wc_sec_hi;
 149	/* arch_shared_info here */
 150};
 151
 152#define RUNSTATE_running  0
 153#define RUNSTATE_runnable 1
 154#define RUNSTATE_blocked  2
 155#define RUNSTATE_offline  3
 156
 157static const char *runstate_names[] = {
 158	"running",
 159	"runnable",
 160	"blocked",
 161	"offline"
 162};
 163
 164struct {
 165	struct kvm_irq_routing info;
 166	struct kvm_irq_routing_entry entries[2];
 167} irq_routes;
 168
 169static volatile bool guest_saw_irq;
 170
 171static void evtchn_handler(struct ex_regs *regs)
 172{
 173	struct vcpu_info *vi = (void *)VCPU_INFO_VADDR;
 174
 175	vcpu_arch_put_guest(vi->evtchn_upcall_pending, 0);
 176	vcpu_arch_put_guest(vi->evtchn_pending_sel, 0);
 177	guest_saw_irq = true;
 178
 179	GUEST_SYNC(TEST_GUEST_SAW_IRQ);
 180}
 181
 182static void guest_wait_for_irq(void)
 183{
 184	while (!guest_saw_irq)
 185		__asm__ __volatile__ ("rep nop" : : : "memory");
 186	guest_saw_irq = false;
 187}
 188
 189static void guest_code(void)
 190{
 191	struct vcpu_runstate_info *rs = (void *)RUNSTATE_VADDR;
 192	int i;
 193
 194	sti_nop();
 195
 196	/* Trigger an interrupt injection */
 197	GUEST_SYNC(TEST_INJECT_VECTOR);
 198
 199	guest_wait_for_irq();
 200
 201	/* Test having the host set runstates manually */
 202	GUEST_SYNC(TEST_RUNSTATE_runnable);
 203	GUEST_ASSERT(rs->time[RUNSTATE_runnable] != 0);
 204	GUEST_ASSERT(rs->state == 0);
 205
 206	GUEST_SYNC(TEST_RUNSTATE_blocked);
 207	GUEST_ASSERT(rs->time[RUNSTATE_blocked] != 0);
 208	GUEST_ASSERT(rs->state == 0);
 209
 210	GUEST_SYNC(TEST_RUNSTATE_offline);
 211	GUEST_ASSERT(rs->time[RUNSTATE_offline] != 0);
 212	GUEST_ASSERT(rs->state == 0);
 213
 214	/* Test runstate time adjust */
 215	GUEST_SYNC(TEST_RUNSTATE_ADJUST);
 216	GUEST_ASSERT(rs->time[RUNSTATE_blocked] == 0x5a);
 217	GUEST_ASSERT(rs->time[RUNSTATE_offline] == 0x6b6b);
 218
 219	/* Test runstate time set */
 220	GUEST_SYNC(TEST_RUNSTATE_DATA);
 221	GUEST_ASSERT(rs->state_entry_time >= 0x8000);
 222	GUEST_ASSERT(rs->time[RUNSTATE_runnable] == 0);
 223	GUEST_ASSERT(rs->time[RUNSTATE_blocked] == 0x6b6b);
 224	GUEST_ASSERT(rs->time[RUNSTATE_offline] == 0x5a);
 225
 226	/* sched_yield() should result in some 'runnable' time */
 227	GUEST_SYNC(TEST_STEAL_TIME);
 228	GUEST_ASSERT(rs->time[RUNSTATE_runnable] >= MIN_STEAL_TIME);
 229
 230	/* Attempt to deliver a *masked* interrupt */
 231	GUEST_SYNC(TEST_EVTCHN_MASKED);
 232
 233	/* Wait until we see the bit set */
 234	struct shared_info *si = (void *)SHINFO_VADDR;
 235	while (!si->evtchn_pending[0])
 236		__asm__ __volatile__ ("rep nop" : : : "memory");
 237
 238	/* Now deliver an *unmasked* interrupt */
 239	GUEST_SYNC(TEST_EVTCHN_UNMASKED);
 240
 241	guest_wait_for_irq();
 242
 243	/* Change memslots and deliver an interrupt */
 244	GUEST_SYNC(TEST_EVTCHN_SLOWPATH);
 245
 246	guest_wait_for_irq();
 247
 248	/* Deliver event channel with KVM_XEN_HVM_EVTCHN_SEND */
 249	GUEST_SYNC(TEST_EVTCHN_SEND_IOCTL);
 250
 251	guest_wait_for_irq();
 252
 253	GUEST_SYNC(TEST_EVTCHN_HCALL);
 254
 255	/* Our turn. Deliver event channel (to ourselves) with
 256	 * EVTCHNOP_send hypercall. */
 257	struct evtchn_send s = { .port = 127 };
 258	xen_hypercall(__HYPERVISOR_event_channel_op, EVTCHNOP_send, &s);
 259
 260	guest_wait_for_irq();
 261
 262	GUEST_SYNC(TEST_EVTCHN_HCALL_SLOWPATH);
 263
 264	/*
 265	 * Same again, but this time the host has messed with memslots so it
 266	 * should take the slow path in kvm_xen_set_evtchn().
 267	 */
 268	xen_hypercall(__HYPERVISOR_event_channel_op, EVTCHNOP_send, &s);
 269
 270	guest_wait_for_irq();
 271
 272	GUEST_SYNC(TEST_EVTCHN_HCALL_EVENTFD);
 273
 274	/* Deliver "outbound" event channel to an eventfd which
 275	 * happens to be one of our own irqfds. */
 276	s.port = 197;
 277	xen_hypercall(__HYPERVISOR_event_channel_op, EVTCHNOP_send, &s);
 278
 279	guest_wait_for_irq();
 280
 281	GUEST_SYNC(TEST_TIMER_SETUP);
 282
 283	/* Set a timer 100ms in the future. */
 284	xen_hypercall(__HYPERVISOR_set_timer_op,
 285		      rs->state_entry_time + 100000000, NULL);
 286
 287	GUEST_SYNC(TEST_TIMER_WAIT);
 288
 289	/* Now wait for the timer */
 290	guest_wait_for_irq();
 291
 292	GUEST_SYNC(TEST_TIMER_RESTORE);
 293
 294	/* The host has 'restored' the timer. Just wait for it. */
 295	guest_wait_for_irq();
 296
 297	GUEST_SYNC(TEST_POLL_READY);
 298
 299	/* Poll for an event channel port which is already set */
 300	u32 ports[1] = { EVTCHN_TIMER };
 301	struct sched_poll p = {
 302		.ports = ports,
 303		.nr_ports = 1,
 304		.timeout = 0,
 305	};
 306
 307	xen_hypercall(__HYPERVISOR_sched_op, SCHEDOP_poll, &p);
 308
 309	GUEST_SYNC(TEST_POLL_TIMEOUT);
 310
 311	/* Poll for an unset port and wait for the timeout. */
 312	p.timeout = 100000000;
 313	xen_hypercall(__HYPERVISOR_sched_op, SCHEDOP_poll, &p);
 314
 315	GUEST_SYNC(TEST_POLL_MASKED);
 316
 317	/* A timer will wake the masked port we're waiting on, while we poll */
 318	p.timeout = 0;
 319	xen_hypercall(__HYPERVISOR_sched_op, SCHEDOP_poll, &p);
 320
 321	GUEST_SYNC(TEST_POLL_WAKE);
 322
 323	/* Set the vcpu_info to point at exactly the place it already is to
 324	 * make sure the attribute is functional. */
 325	GUEST_SYNC(SET_VCPU_INFO);
 326
 327	/* A timer wake an *unmasked* port which should wake us with an
 328	 * actual interrupt, while we're polling on a different port. */
 329	ports[0]++;
 330	p.timeout = 0;
 331	xen_hypercall(__HYPERVISOR_sched_op, SCHEDOP_poll, &p);
 332
 333	guest_wait_for_irq();
 334
 335	GUEST_SYNC(TEST_TIMER_PAST);
 336
 337	/* Timer should have fired already */
 338	guest_wait_for_irq();
 339
 340	GUEST_SYNC(TEST_LOCKING_SEND_RACE);
 341	/* Racing host ioctls */
 342
 343	guest_wait_for_irq();
 344
 345	GUEST_SYNC(TEST_LOCKING_POLL_RACE);
 346	/* Racing vmcall against host ioctl */
 347
 348	ports[0] = 0;
 349
 350	p = (struct sched_poll) {
 351		.ports = ports,
 352		.nr_ports = 1,
 353		.timeout = 0
 354	};
 355
 356wait_for_timer:
 357	/*
 358	 * Poll for a timer wake event while the worker thread is mucking with
 359	 * the shared info.  KVM XEN drops timer IRQs if the shared info is
 360	 * invalid when the timer expires.  Arbitrarily poll 100 times before
 361	 * giving up and asking the VMM to re-arm the timer.  100 polls should
 362	 * consume enough time to beat on KVM without taking too long if the
 363	 * timer IRQ is dropped due to an invalid event channel.
 364	 */
 365	for (i = 0; i < 100 && !guest_saw_irq; i++)
 366		__xen_hypercall(__HYPERVISOR_sched_op, SCHEDOP_poll, &p);
 367
 368	/*
 369	 * Re-send the timer IRQ if it was (likely) dropped due to the timer
 370	 * expiring while the event channel was invalid.
 371	 */
 372	if (!guest_saw_irq) {
 373		GUEST_SYNC(TEST_LOCKING_POLL_TIMEOUT);
 374		goto wait_for_timer;
 375	}
 376	guest_saw_irq = false;
 377
 378	GUEST_SYNC(TEST_DONE);
 379}
 380
 381static struct shared_info *shinfo;
 382static struct vcpu_info *vinfo;
 383static struct kvm_vcpu *vcpu;
 384
 385static void handle_alrm(int sig)
 386{
 387	if (vinfo)
 388		printf("evtchn_upcall_pending 0x%x\n", vinfo->evtchn_upcall_pending);
 389	vcpu_dump(stdout, vcpu, 0);
 390	TEST_FAIL("IRQ delivery timed out");
 391}
 392
 393static void *juggle_shinfo_state(void *arg)
 394{
 395	struct kvm_vm *vm = (struct kvm_vm *)arg;
 396
 397	struct kvm_xen_hvm_attr cache_activate_gfn = {
 398		.type = KVM_XEN_ATTR_TYPE_SHARED_INFO,
 399		.u.shared_info.gfn = SHINFO_REGION_GPA / PAGE_SIZE
 400	};
 401
 402	struct kvm_xen_hvm_attr cache_deactivate_gfn = {
 403		.type = KVM_XEN_ATTR_TYPE_SHARED_INFO,
 404		.u.shared_info.gfn = KVM_XEN_INVALID_GFN
 405	};
 406
 407	struct kvm_xen_hvm_attr cache_activate_hva = {
 408		.type = KVM_XEN_ATTR_TYPE_SHARED_INFO_HVA,
 409		.u.shared_info.hva = (unsigned long)shinfo
 410	};
 411
 412	struct kvm_xen_hvm_attr cache_deactivate_hva = {
 413		.type = KVM_XEN_ATTR_TYPE_SHARED_INFO,
 414		.u.shared_info.hva = 0
 415	};
 416
 417	int xen_caps = kvm_check_cap(KVM_CAP_XEN_HVM);
 418
 419	for (;;) {
 420		__vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &cache_activate_gfn);
 421		pthread_testcancel();
 422		__vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &cache_deactivate_gfn);
 423
 424		if (xen_caps & KVM_XEN_HVM_CONFIG_SHARED_INFO_HVA) {
 425			__vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &cache_activate_hva);
 426			pthread_testcancel();
 427			__vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &cache_deactivate_hva);
 428		}
 429	}
 430
 431	return NULL;
 432}
 433
 434int main(int argc, char *argv[])
 435{
 436	struct kvm_xen_hvm_attr evt_reset;
 437	struct kvm_vm *vm;
 438	pthread_t thread;
 439	bool verbose;
 440	int ret;
 441
 442	verbose = argc > 1 && (!strncmp(argv[1], "-v", 3) ||
 443			       !strncmp(argv[1], "--verbose", 10));
 444
 445	int xen_caps = kvm_check_cap(KVM_CAP_XEN_HVM);
 446	TEST_REQUIRE(xen_caps & KVM_XEN_HVM_CONFIG_SHARED_INFO);
 447
 448	bool do_runstate_tests = !!(xen_caps & KVM_XEN_HVM_CONFIG_RUNSTATE);
 449	bool do_runstate_flag = !!(xen_caps & KVM_XEN_HVM_CONFIG_RUNSTATE_UPDATE_FLAG);
 450	bool do_eventfd_tests = !!(xen_caps & KVM_XEN_HVM_CONFIG_EVTCHN_2LEVEL);
 451	bool do_evtchn_tests = do_eventfd_tests && !!(xen_caps & KVM_XEN_HVM_CONFIG_EVTCHN_SEND);
 452	bool has_shinfo_hva = !!(xen_caps & KVM_XEN_HVM_CONFIG_SHARED_INFO_HVA);
 453
 454	vm = vm_create_with_one_vcpu(&vcpu, guest_code);
 455
 456	/* Map a region for the shared_info page */
 457	vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
 458				    SHINFO_REGION_GPA, SHINFO_REGION_SLOT, 3, 0);
 459	virt_map(vm, SHINFO_REGION_GVA, SHINFO_REGION_GPA, 3);
 460
 461	shinfo = addr_gpa2hva(vm, SHINFO_VADDR);
 462
 463	int zero_fd = open("/dev/zero", O_RDONLY);
 464	TEST_ASSERT(zero_fd != -1, "Failed to open /dev/zero");
 465
 466	struct kvm_xen_hvm_config hvmc = {
 467		.flags = KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL,
 468		.msr = XEN_HYPERCALL_MSR,
 469	};
 470
 471	/* Let the kernel know that we *will* use it for sending all
 472	 * event channels, which lets it intercept SCHEDOP_poll */
 473	if (do_evtchn_tests)
 474		hvmc.flags |= KVM_XEN_HVM_CONFIG_EVTCHN_SEND;
 475
 476	vm_ioctl(vm, KVM_XEN_HVM_CONFIG, &hvmc);
 477
 478	struct kvm_xen_hvm_attr lm = {
 479		.type = KVM_XEN_ATTR_TYPE_LONG_MODE,
 480		.u.long_mode = 1,
 481	};
 482	vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &lm);
 483
 484	if (do_runstate_flag) {
 485		struct kvm_xen_hvm_attr ruf = {
 486			.type = KVM_XEN_ATTR_TYPE_RUNSTATE_UPDATE_FLAG,
 487			.u.runstate_update_flag = 1,
 488		};
 489		vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &ruf);
 490
 491		ruf.u.runstate_update_flag = 0;
 492		vm_ioctl(vm, KVM_XEN_HVM_GET_ATTR, &ruf);
 493		TEST_ASSERT(ruf.u.runstate_update_flag == 1,
 494			    "Failed to read back RUNSTATE_UPDATE_FLAG attr");
 495	}
 496
 497	struct kvm_xen_hvm_attr ha = {};
 498
 499	if (has_shinfo_hva) {
 500		ha.type = KVM_XEN_ATTR_TYPE_SHARED_INFO_HVA;
 501		ha.u.shared_info.hva = (unsigned long)shinfo;
 502	} else {
 503		ha.type = KVM_XEN_ATTR_TYPE_SHARED_INFO;
 504		ha.u.shared_info.gfn = SHINFO_ADDR / PAGE_SIZE;
 505	}
 506
 507	vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &ha);
 508
 509	/*
 510	 * Test what happens when the HVA of the shinfo page is remapped after
 511	 * the kernel has a reference to it. But make sure we copy the clock
 512	 * info over since that's only set at setup time, and we test it later.
 513	 */
 514	struct pvclock_wall_clock wc_copy = shinfo->wc;
 515	void *m = mmap(shinfo, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_FIXED|MAP_PRIVATE, zero_fd, 0);
 516	TEST_ASSERT(m == shinfo, "Failed to map /dev/zero over shared info");
 517	shinfo->wc = wc_copy;
 518
 519	struct kvm_xen_vcpu_attr vi = {
 520		.type = KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO,
 521		.u.gpa = VCPU_INFO_ADDR,
 522	};
 523	vcpu_ioctl(vcpu, KVM_XEN_VCPU_SET_ATTR, &vi);
 524
 525	struct kvm_xen_vcpu_attr pvclock = {
 526		.type = KVM_XEN_VCPU_ATTR_TYPE_VCPU_TIME_INFO,
 527		.u.gpa = PVTIME_ADDR,
 528	};
 529	vcpu_ioctl(vcpu, KVM_XEN_VCPU_SET_ATTR, &pvclock);
 530
 531	struct kvm_xen_hvm_attr vec = {
 532		.type = KVM_XEN_ATTR_TYPE_UPCALL_VECTOR,
 533		.u.vector = EVTCHN_VECTOR,
 534	};
 535	vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &vec);
 536
 537	vm_install_exception_handler(vm, EVTCHN_VECTOR, evtchn_handler);
 538
 539	if (do_runstate_tests) {
 540		struct kvm_xen_vcpu_attr st = {
 541			.type = KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_ADDR,
 542			.u.gpa = RUNSTATE_ADDR,
 543		};
 544		vcpu_ioctl(vcpu, KVM_XEN_VCPU_SET_ATTR, &st);
 545	}
 546
 547	int irq_fd[2] = { -1, -1 };
 548
 549	if (do_eventfd_tests) {
 550		irq_fd[0] = kvm_new_eventfd();
 551		irq_fd[1] = kvm_new_eventfd();
 552
 553		irq_routes.info.nr = 2;
 554
 555		irq_routes.entries[0].gsi = 32;
 556		irq_routes.entries[0].type = KVM_IRQ_ROUTING_XEN_EVTCHN;
 557		irq_routes.entries[0].u.xen_evtchn.port = EVTCHN_TEST1;
 558		irq_routes.entries[0].u.xen_evtchn.vcpu = vcpu->id;
 559		irq_routes.entries[0].u.xen_evtchn.priority = KVM_IRQ_ROUTING_XEN_EVTCHN_PRIO_2LEVEL;
 560
 561		irq_routes.entries[1].gsi = 33;
 562		irq_routes.entries[1].type = KVM_IRQ_ROUTING_XEN_EVTCHN;
 563		irq_routes.entries[1].u.xen_evtchn.port = EVTCHN_TEST2;
 564		irq_routes.entries[1].u.xen_evtchn.vcpu = vcpu->id;
 565		irq_routes.entries[1].u.xen_evtchn.priority = KVM_IRQ_ROUTING_XEN_EVTCHN_PRIO_2LEVEL;
 566
 567		vm_ioctl(vm, KVM_SET_GSI_ROUTING, &irq_routes.info);
 568
 569		kvm_assign_irqfd(vm, 32, irq_fd[0]);
 570		kvm_assign_irqfd(vm, 33, irq_fd[1]);
 571
 572		struct sigaction sa = { };
 573		sa.sa_handler = handle_alrm;
 574		sigaction(SIGALRM, &sa, NULL);
 575	}
 576
 577	struct kvm_xen_vcpu_attr tmr = {
 578		.type = KVM_XEN_VCPU_ATTR_TYPE_TIMER,
 579		.u.timer.port = EVTCHN_TIMER,
 580		.u.timer.priority = KVM_IRQ_ROUTING_XEN_EVTCHN_PRIO_2LEVEL,
 581		.u.timer.expires_ns = 0
 582	};
 583
 584	if (do_evtchn_tests) {
 585		struct kvm_xen_hvm_attr inj = {
 586			.type = KVM_XEN_ATTR_TYPE_EVTCHN,
 587			.u.evtchn.send_port = 127,
 588			.u.evtchn.type = EVTCHNSTAT_interdomain,
 589			.u.evtchn.flags = 0,
 590			.u.evtchn.deliver.port.port = EVTCHN_TEST1,
 591			.u.evtchn.deliver.port.vcpu = vcpu->id + 1,
 592			.u.evtchn.deliver.port.priority = KVM_IRQ_ROUTING_XEN_EVTCHN_PRIO_2LEVEL,
 593		};
 594		vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &inj);
 595
 596		/* Test migration to a different vCPU */
 597		inj.u.evtchn.flags = KVM_XEN_EVTCHN_UPDATE;
 598		inj.u.evtchn.deliver.port.vcpu = vcpu->id;
 599		vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &inj);
 600
 601		inj.u.evtchn.send_port = 197;
 602		inj.u.evtchn.deliver.eventfd.port = 0;
 603		inj.u.evtchn.deliver.eventfd.fd = irq_fd[1];
 604		inj.u.evtchn.flags = 0;
 605		vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &inj);
 606
 607		vcpu_ioctl(vcpu, KVM_XEN_VCPU_SET_ATTR, &tmr);
 608	}
 609	vinfo = addr_gpa2hva(vm, VCPU_INFO_VADDR);
 610	vinfo->evtchn_upcall_pending = 0;
 611
 612	struct vcpu_runstate_info *rs = addr_gpa2hva(vm, RUNSTATE_ADDR);
 613	rs->state = 0x5a;
 614
 615	bool evtchn_irq_expected = false;
 616
 617	for (;;) {
 618		struct ucall uc;
 619
 620		vcpu_run(vcpu);
 621		TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
 622
 623		switch (get_ucall(vcpu, &uc)) {
 624		case UCALL_ABORT:
 625			REPORT_GUEST_ASSERT(uc);
 626			/* NOT REACHED */
 627		case UCALL_SYNC: {
 628			struct kvm_xen_vcpu_attr rst;
 629			long rundelay;
 630
 631			if (do_runstate_tests)
 632				TEST_ASSERT(rs->state_entry_time == rs->time[0] +
 633					    rs->time[1] + rs->time[2] + rs->time[3],
 634					    "runstate times don't add up");
 635
 636			switch (uc.args[1]) {
 637			case TEST_INJECT_VECTOR:
 638				if (verbose)
 639					printf("Delivering evtchn upcall\n");
 640				evtchn_irq_expected = true;
 641				vinfo->evtchn_upcall_pending = 1;
 642				break;
 643
 644			case TEST_RUNSTATE_runnable...TEST_RUNSTATE_offline:
 645				TEST_ASSERT(!evtchn_irq_expected, "Event channel IRQ not seen");
 646				if (!do_runstate_tests)
 647					goto done;
 648				if (verbose)
 649					printf("Testing runstate %s\n", runstate_names[uc.args[1]]);
 650				rst.type = KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_CURRENT;
 651				rst.u.runstate.state = uc.args[1] + RUNSTATE_runnable -
 652					TEST_RUNSTATE_runnable;
 653				vcpu_ioctl(vcpu, KVM_XEN_VCPU_SET_ATTR, &rst);
 654				break;
 655
 656			case TEST_RUNSTATE_ADJUST:
 657				if (verbose)
 658					printf("Testing RUNSTATE_ADJUST\n");
 659				rst.type = KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_ADJUST;
 660				memset(&rst.u, 0, sizeof(rst.u));
 661				rst.u.runstate.state = (uint64_t)-1;
 662				rst.u.runstate.time_blocked =
 663					0x5a - rs->time[RUNSTATE_blocked];
 664				rst.u.runstate.time_offline =
 665					0x6b6b - rs->time[RUNSTATE_offline];
 666				rst.u.runstate.time_runnable = -rst.u.runstate.time_blocked -
 667					rst.u.runstate.time_offline;
 668				vcpu_ioctl(vcpu, KVM_XEN_VCPU_SET_ATTR, &rst);
 669				break;
 670
 671			case TEST_RUNSTATE_DATA:
 672				if (verbose)
 673					printf("Testing RUNSTATE_DATA\n");
 674				rst.type = KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_DATA;
 675				memset(&rst.u, 0, sizeof(rst.u));
 676				rst.u.runstate.state = RUNSTATE_running;
 677				rst.u.runstate.state_entry_time = 0x6b6b + 0x5a;
 678				rst.u.runstate.time_blocked = 0x6b6b;
 679				rst.u.runstate.time_offline = 0x5a;
 680				vcpu_ioctl(vcpu, KVM_XEN_VCPU_SET_ATTR, &rst);
 681				break;
 682
 683			case TEST_STEAL_TIME:
 684				if (verbose)
 685					printf("Testing steal time\n");
 686				/* Yield until scheduler delay exceeds target */
 687				rundelay = get_run_delay() + MIN_STEAL_TIME;
 688				do {
 689					sched_yield();
 690				} while (get_run_delay() < rundelay);
 691				break;
 692
 693			case TEST_EVTCHN_MASKED:
 694				if (!do_eventfd_tests)
 695					goto done;
 696				if (verbose)
 697					printf("Testing masked event channel\n");
 698				shinfo->evtchn_mask[0] = 1UL << EVTCHN_TEST1;
 699				eventfd_write(irq_fd[0], 1UL);
 700				alarm(1);
 701				break;
 702
 703			case TEST_EVTCHN_UNMASKED:
 704				if (verbose)
 705					printf("Testing unmasked event channel\n");
 706				/* Unmask that, but deliver the other one */
 707				shinfo->evtchn_pending[0] = 0;
 708				shinfo->evtchn_mask[0] = 0;
 709				eventfd_write(irq_fd[1], 1UL);
 710				evtchn_irq_expected = true;
 711				alarm(1);
 712				break;
 713
 714			case TEST_EVTCHN_SLOWPATH:
 715				TEST_ASSERT(!evtchn_irq_expected,
 716					    "Expected event channel IRQ but it didn't happen");
 717				shinfo->evtchn_pending[1] = 0;
 718				if (verbose)
 719					printf("Testing event channel after memslot change\n");
 720				vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
 721							    DUMMY_REGION_GPA, DUMMY_REGION_SLOT, 1, 0);
 722				eventfd_write(irq_fd[0], 1UL);
 723				evtchn_irq_expected = true;
 724				alarm(1);
 725				break;
 726
 727			case TEST_EVTCHN_SEND_IOCTL:
 728				TEST_ASSERT(!evtchn_irq_expected,
 729					    "Expected event channel IRQ but it didn't happen");
 730				if (!do_evtchn_tests)
 731					goto done;
 732
 733				shinfo->evtchn_pending[0] = 0;
 734				if (verbose)
 735					printf("Testing injection with KVM_XEN_HVM_EVTCHN_SEND\n");
 736
 737				struct kvm_irq_routing_xen_evtchn e;
 738				e.port = EVTCHN_TEST2;
 739				e.vcpu = vcpu->id;
 740				e.priority = KVM_IRQ_ROUTING_XEN_EVTCHN_PRIO_2LEVEL;
 741
 742				vm_ioctl(vm, KVM_XEN_HVM_EVTCHN_SEND, &e);
 743				evtchn_irq_expected = true;
 744				alarm(1);
 745				break;
 746
 747			case TEST_EVTCHN_HCALL:
 748				TEST_ASSERT(!evtchn_irq_expected,
 749					    "Expected event channel IRQ but it didn't happen");
 750				shinfo->evtchn_pending[1] = 0;
 751
 752				if (verbose)
 753					printf("Testing guest EVTCHNOP_send direct to evtchn\n");
 754				evtchn_irq_expected = true;
 755				alarm(1);
 756				break;
 757
 758			case TEST_EVTCHN_HCALL_SLOWPATH:
 759				TEST_ASSERT(!evtchn_irq_expected,
 760					    "Expected event channel IRQ but it didn't happen");
 761				shinfo->evtchn_pending[0] = 0;
 762
 763				if (verbose)
 764					printf("Testing guest EVTCHNOP_send direct to evtchn after memslot change\n");
 765				vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
 766							    DUMMY_REGION_GPA_2, DUMMY_REGION_SLOT_2, 1, 0);
 767				evtchn_irq_expected = true;
 768				alarm(1);
 769				break;
 770
 771			case TEST_EVTCHN_HCALL_EVENTFD:
 772				TEST_ASSERT(!evtchn_irq_expected,
 773					    "Expected event channel IRQ but it didn't happen");
 774				shinfo->evtchn_pending[0] = 0;
 775
 776				if (verbose)
 777					printf("Testing guest EVTCHNOP_send to eventfd\n");
 778				evtchn_irq_expected = true;
 779				alarm(1);
 780				break;
 781
 782			case TEST_TIMER_SETUP:
 783				TEST_ASSERT(!evtchn_irq_expected,
 784					    "Expected event channel IRQ but it didn't happen");
 785				shinfo->evtchn_pending[1] = 0;
 786
 787				if (verbose)
 788					printf("Testing guest oneshot timer\n");
 789				break;
 790
 791			case TEST_TIMER_WAIT:
 792				memset(&tmr, 0, sizeof(tmr));
 793				tmr.type = KVM_XEN_VCPU_ATTR_TYPE_TIMER;
 794				vcpu_ioctl(vcpu, KVM_XEN_VCPU_GET_ATTR, &tmr);
 795				TEST_ASSERT(tmr.u.timer.port == EVTCHN_TIMER,
 796					    "Timer port not returned");
 797				TEST_ASSERT(tmr.u.timer.priority == KVM_IRQ_ROUTING_XEN_EVTCHN_PRIO_2LEVEL,
 798					    "Timer priority not returned");
 799				TEST_ASSERT(tmr.u.timer.expires_ns > rs->state_entry_time,
 800					    "Timer expiry not returned");
 801				evtchn_irq_expected = true;
 802				alarm(1);
 803				break;
 804
 805			case TEST_TIMER_RESTORE:
 806				TEST_ASSERT(!evtchn_irq_expected,
 807					    "Expected event channel IRQ but it didn't happen");
 808				shinfo->evtchn_pending[0] = 0;
 809
 810				if (verbose)
 811					printf("Testing restored oneshot timer\n");
 812
 813				tmr.u.timer.expires_ns = rs->state_entry_time + 100000000;
 814				vcpu_ioctl(vcpu, KVM_XEN_VCPU_SET_ATTR, &tmr);
 815				evtchn_irq_expected = true;
 816				alarm(1);
 817				break;
 818
 819			case TEST_POLL_READY:
 820				TEST_ASSERT(!evtchn_irq_expected,
 821					    "Expected event channel IRQ but it didn't happen");
 822
 823				if (verbose)
 824					printf("Testing SCHEDOP_poll with already pending event\n");
 825				shinfo->evtchn_pending[0] = shinfo->evtchn_mask[0] = 1UL << EVTCHN_TIMER;
 826				alarm(1);
 827				break;
 828
 829			case TEST_POLL_TIMEOUT:
 830				if (verbose)
 831					printf("Testing SCHEDOP_poll timeout\n");
 832				shinfo->evtchn_pending[0] = 0;
 833				alarm(1);
 834				break;
 835
 836			case TEST_POLL_MASKED:
 837				if (verbose)
 838					printf("Testing SCHEDOP_poll wake on masked event\n");
 839
 840				tmr.u.timer.expires_ns = rs->state_entry_time + 100000000;
 841				vcpu_ioctl(vcpu, KVM_XEN_VCPU_SET_ATTR, &tmr);
 842				alarm(1);
 843				break;
 844
 845			case TEST_POLL_WAKE:
 846				shinfo->evtchn_pending[0] = shinfo->evtchn_mask[0] = 0;
 847				if (verbose)
 848					printf("Testing SCHEDOP_poll wake on unmasked event\n");
 849
 850				evtchn_irq_expected = true;
 851				tmr.u.timer.expires_ns = rs->state_entry_time + 100000000;
 852				vcpu_ioctl(vcpu, KVM_XEN_VCPU_SET_ATTR, &tmr);
 853
 854				/* Read it back and check the pending time is reported correctly */
 855				tmr.u.timer.expires_ns = 0;
 856				vcpu_ioctl(vcpu, KVM_XEN_VCPU_GET_ATTR, &tmr);
 857				TEST_ASSERT(tmr.u.timer.expires_ns == rs->state_entry_time + 100000000,
 858					    "Timer not reported pending");
 859				alarm(1);
 860				break;
 861
 862			case SET_VCPU_INFO:
 863				if (has_shinfo_hva) {
 864					struct kvm_xen_vcpu_attr vih = {
 865						.type = KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO_HVA,
 866						.u.hva = (unsigned long)vinfo
 867					};
 868					vcpu_ioctl(vcpu, KVM_XEN_VCPU_SET_ATTR, &vih);
 869				}
 870				break;
 871
 872			case TEST_TIMER_PAST:
 873				TEST_ASSERT(!evtchn_irq_expected,
 874					    "Expected event channel IRQ but it didn't happen");
 875				/* Read timer and check it is no longer pending */
 876				vcpu_ioctl(vcpu, KVM_XEN_VCPU_GET_ATTR, &tmr);
 877				TEST_ASSERT(!tmr.u.timer.expires_ns, "Timer still reported pending");
 878
 879				shinfo->evtchn_pending[0] = 0;
 880				if (verbose)
 881					printf("Testing timer in the past\n");
 882
 883				evtchn_irq_expected = true;
 884				tmr.u.timer.expires_ns = rs->state_entry_time - 100000000ULL;
 885				vcpu_ioctl(vcpu, KVM_XEN_VCPU_SET_ATTR, &tmr);
 886				alarm(1);
 887				break;
 888
 889			case TEST_LOCKING_SEND_RACE:
 890				TEST_ASSERT(!evtchn_irq_expected,
 891					    "Expected event channel IRQ but it didn't happen");
 892				alarm(0);
 893
 894				if (verbose)
 895					printf("Testing shinfo lock corruption (KVM_XEN_HVM_EVTCHN_SEND)\n");
 896
 897				ret = pthread_create(&thread, NULL, &juggle_shinfo_state, (void *)vm);
 898				TEST_ASSERT(ret == 0, "pthread_create() failed: %s", strerror(ret));
 899
 900				struct kvm_irq_routing_xen_evtchn uxe = {
 901					.port = 1,
 902					.vcpu = vcpu->id,
 903					.priority = KVM_IRQ_ROUTING_XEN_EVTCHN_PRIO_2LEVEL
 904				};
 905
 906				evtchn_irq_expected = true;
 907				for (time_t t = time(NULL) + SHINFO_RACE_TIMEOUT; time(NULL) < t;)
 908					__vm_ioctl(vm, KVM_XEN_HVM_EVTCHN_SEND, &uxe);
 909				break;
 910
 911			case TEST_LOCKING_POLL_RACE:
 912				TEST_ASSERT(!evtchn_irq_expected,
 913					    "Expected event channel IRQ but it didn't happen");
 914
 915				if (verbose)
 916					printf("Testing shinfo lock corruption (SCHEDOP_poll)\n");
 917
 918				shinfo->evtchn_pending[0] = 1;
 919
 920				evtchn_irq_expected = true;
 921				tmr.u.timer.expires_ns = rs->state_entry_time +
 922							 SHINFO_RACE_TIMEOUT * 1000000000ULL;
 923				vcpu_ioctl(vcpu, KVM_XEN_VCPU_SET_ATTR, &tmr);
 924				break;
 925
 926			case TEST_LOCKING_POLL_TIMEOUT:
 927				/*
 928				 * Optional and possibly repeated sync point.
 929				 * Injecting the timer IRQ may fail if the
 930				 * shinfo is invalid when the timer expires.
 931				 * If the timer has expired but the IRQ hasn't
 932				 * been delivered, rearm the timer and retry.
 933				 */
 934				vcpu_ioctl(vcpu, KVM_XEN_VCPU_GET_ATTR, &tmr);
 935
 936				/* Resume the guest if the timer is still pending. */
 937				if (tmr.u.timer.expires_ns)
 938					break;
 939
 940				/* All done if the IRQ was delivered. */
 941				if (!evtchn_irq_expected)
 942					break;
 943
 944				tmr.u.timer.expires_ns = rs->state_entry_time +
 945							 SHINFO_RACE_TIMEOUT * 1000000000ULL;
 946				vcpu_ioctl(vcpu, KVM_XEN_VCPU_SET_ATTR, &tmr);
 947				break;
 948			case TEST_DONE:
 949				TEST_ASSERT(!evtchn_irq_expected,
 950					    "Expected event channel IRQ but it didn't happen");
 951
 952				ret = pthread_cancel(thread);
 953				TEST_ASSERT(ret == 0, "pthread_cancel() failed: %s", strerror(ret));
 954
 955				ret = pthread_join(thread, 0);
 956				TEST_ASSERT(ret == 0, "pthread_join() failed: %s", strerror(ret));
 957				goto done;
 958
 959			case TEST_GUEST_SAW_IRQ:
 960				TEST_ASSERT(evtchn_irq_expected, "Unexpected event channel IRQ");
 961				evtchn_irq_expected = false;
 962				break;
 963			}
 964			break;
 965		}
 966		case UCALL_DONE:
 967			goto done;
 968		default:
 969			TEST_FAIL("Unknown ucall 0x%lx.", uc.cmd);
 970		}
 971	}
 972
 973 done:
 974	evt_reset.type = KVM_XEN_ATTR_TYPE_EVTCHN;
 975	evt_reset.u.evtchn.flags = KVM_XEN_EVTCHN_RESET;
 976	vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &evt_reset);
 977
 978	alarm(0);
 979
 980	/*
 981	 * Just a *really* basic check that things are being put in the
 982	 * right place. The actual calculations are much the same for
 983	 * Xen as they are for the KVM variants, so no need to check.
 984	 */
 985	struct pvclock_wall_clock *wc;
 986	struct pvclock_vcpu_time_info *ti, *ti2;
 987	struct kvm_clock_data kcdata;
 988	long long delta;
 989
 990	wc = addr_gpa2hva(vm, SHINFO_REGION_GPA + 0xc00);
 991	ti = addr_gpa2hva(vm, SHINFO_REGION_GPA + 0x40 + 0x20);
 992	ti2 = addr_gpa2hva(vm, PVTIME_ADDR);
 993
 994	if (verbose) {
 995		printf("Wall clock (v %d) %d.%09d\n", wc->version, wc->sec, wc->nsec);
 996		printf("Time info 1: v %u tsc %" PRIu64 " time %" PRIu64 " mul %u shift %u flags %x\n",
 997		       ti->version, ti->tsc_timestamp, ti->system_time, ti->tsc_to_system_mul,
 998		       ti->tsc_shift, ti->flags);
 999		printf("Time info 2: v %u tsc %" PRIu64 " time %" PRIu64 " mul %u shift %u flags %x\n",
1000		       ti2->version, ti2->tsc_timestamp, ti2->system_time, ti2->tsc_to_system_mul,
1001		       ti2->tsc_shift, ti2->flags);
1002	}
1003
1004	TEST_ASSERT(wc->version && !(wc->version & 1),
1005		    "Bad wallclock version %x", wc->version);
1006
1007	vm_ioctl(vm, KVM_GET_CLOCK, &kcdata);
1008
1009	if (kcdata.flags & KVM_CLOCK_REALTIME) {
1010		if (verbose) {
1011			printf("KVM_GET_CLOCK clock: %lld.%09lld\n",
1012			       kcdata.clock / NSEC_PER_SEC, kcdata.clock % NSEC_PER_SEC);
1013			printf("KVM_GET_CLOCK realtime: %lld.%09lld\n",
1014			       kcdata.realtime / NSEC_PER_SEC, kcdata.realtime % NSEC_PER_SEC);
1015		}
1016
1017		delta = (wc->sec * NSEC_PER_SEC + wc->nsec) - (kcdata.realtime - kcdata.clock);
1018
1019		/*
1020		 * KVM_GET_CLOCK gives CLOCK_REALTIME which jumps on leap seconds updates but
1021		 * unfortunately KVM doesn't currently offer a CLOCK_TAI alternative. Accept 1s
1022		 * delta as testing clock accuracy is not the goal here. The test just needs to
1023		 * check that the value in shinfo is somewhat sane.
1024		 */
1025		TEST_ASSERT(llabs(delta) < NSEC_PER_SEC,
1026			    "Guest's epoch from shinfo %d.%09d differs from KVM_GET_CLOCK %lld.%lld",
1027			    wc->sec, wc->nsec, (kcdata.realtime - kcdata.clock) / NSEC_PER_SEC,
1028			    (kcdata.realtime - kcdata.clock) % NSEC_PER_SEC);
1029	} else {
1030		pr_info("Missing KVM_CLOCK_REALTIME, skipping shinfo epoch sanity check\n");
1031	}
1032
1033	TEST_ASSERT(ti->version && !(ti->version & 1),
1034		    "Bad time_info version %x", ti->version);
1035	TEST_ASSERT(ti2->version && !(ti2->version & 1),
1036		    "Bad time_info version %x", ti->version);
1037
1038	if (do_runstate_tests) {
1039		/*
1040		 * Fetch runstate and check sanity. Strictly speaking in the
1041		 * general case we might not expect the numbers to be identical
1042		 * but in this case we know we aren't running the vCPU any more.
1043		 */
1044		struct kvm_xen_vcpu_attr rst = {
1045			.type = KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_DATA,
1046		};
1047		vcpu_ioctl(vcpu, KVM_XEN_VCPU_GET_ATTR, &rst);
1048
1049		if (verbose) {
1050			printf("Runstate: %s(%d), entry %" PRIu64 " ns\n",
1051			       rs->state <= RUNSTATE_offline ? runstate_names[rs->state] : "unknown",
1052			       rs->state, rs->state_entry_time);
1053			for (int i = RUNSTATE_running; i <= RUNSTATE_offline; i++) {
1054				printf("State %s: %" PRIu64 " ns\n",
1055				       runstate_names[i], rs->time[i]);
1056			}
1057		}
1058
1059		/*
1060		 * Exercise runstate info at all points across the page boundary, in
1061		 * 32-bit and 64-bit mode. In particular, test the case where it is
1062		 * configured in 32-bit mode and then switched to 64-bit mode while
1063		 * active, which takes it onto the second page.
1064		 */
1065		unsigned long runstate_addr;
1066		struct compat_vcpu_runstate_info *crs;
1067		for (runstate_addr = SHINFO_REGION_GPA + PAGE_SIZE + PAGE_SIZE - sizeof(*rs) - 4;
1068		     runstate_addr < SHINFO_REGION_GPA + PAGE_SIZE + PAGE_SIZE + 4; runstate_addr++) {
1069
1070			rs = addr_gpa2hva(vm, runstate_addr);
1071			crs = (void *)rs;
1072
1073			memset(rs, 0xa5, sizeof(*rs));
1074
1075			/* Set to compatibility mode */
1076			lm.u.long_mode = 0;
1077			vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &lm);
1078
1079			/* Set runstate to new address (kernel will write it) */
1080			struct kvm_xen_vcpu_attr st = {
1081				.type = KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_ADDR,
1082				.u.gpa = runstate_addr,
1083			};
1084			vcpu_ioctl(vcpu, KVM_XEN_VCPU_SET_ATTR, &st);
1085
1086			if (verbose)
1087				printf("Compatibility runstate at %08lx\n", runstate_addr);
1088
1089			TEST_ASSERT(crs->state == rst.u.runstate.state, "Runstate mismatch");
1090			TEST_ASSERT(crs->state_entry_time == rst.u.runstate.state_entry_time,
1091				    "State entry time mismatch");
1092			TEST_ASSERT(crs->time[RUNSTATE_running] == rst.u.runstate.time_running,
1093				    "Running time mismatch");
1094			TEST_ASSERT(crs->time[RUNSTATE_runnable] == rst.u.runstate.time_runnable,
1095				    "Runnable time mismatch");
1096			TEST_ASSERT(crs->time[RUNSTATE_blocked] == rst.u.runstate.time_blocked,
1097				    "Blocked time mismatch");
1098			TEST_ASSERT(crs->time[RUNSTATE_offline] == rst.u.runstate.time_offline,
1099				    "Offline time mismatch");
1100			TEST_ASSERT(crs->time[RUNSTATE_offline + 1] == 0xa5a5a5a5a5a5a5a5ULL,
1101				    "Structure overrun");
1102			TEST_ASSERT(crs->state_entry_time == crs->time[0] +
1103				    crs->time[1] + crs->time[2] + crs->time[3],
1104				    "runstate times don't add up");
1105
1106
1107			/* Now switch to 64-bit mode */
1108			lm.u.long_mode = 1;
1109			vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &lm);
1110
1111			memset(rs, 0xa5, sizeof(*rs));
1112
1113			/* Don't change the address, just trigger a write */
1114			struct kvm_xen_vcpu_attr adj = {
1115				.type = KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_ADJUST,
1116				.u.runstate.state = (uint64_t)-1
1117			};
1118			vcpu_ioctl(vcpu, KVM_XEN_VCPU_SET_ATTR, &adj);
1119
1120			if (verbose)
1121				printf("64-bit runstate at %08lx\n", runstate_addr);
1122
1123			TEST_ASSERT(rs->state == rst.u.runstate.state, "Runstate mismatch");
1124			TEST_ASSERT(rs->state_entry_time == rst.u.runstate.state_entry_time,
1125				    "State entry time mismatch");
1126			TEST_ASSERT(rs->time[RUNSTATE_running] == rst.u.runstate.time_running,
1127				    "Running time mismatch");
1128			TEST_ASSERT(rs->time[RUNSTATE_runnable] == rst.u.runstate.time_runnable,
1129				    "Runnable time mismatch");
1130			TEST_ASSERT(rs->time[RUNSTATE_blocked] == rst.u.runstate.time_blocked,
1131				    "Blocked time mismatch");
1132			TEST_ASSERT(rs->time[RUNSTATE_offline] == rst.u.runstate.time_offline,
1133				    "Offline time mismatch");
1134			TEST_ASSERT(rs->time[RUNSTATE_offline + 1] == 0xa5a5a5a5a5a5a5a5ULL,
1135				    "Structure overrun");
1136
1137			TEST_ASSERT(rs->state_entry_time == rs->time[0] +
1138				    rs->time[1] + rs->time[2] + rs->time[3],
1139				    "runstate times don't add up");
1140		}
1141	}
1142
1143	kvm_vm_free(vm);
1144	return 0;
1145}
~/ Read-only file preview.