1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * svm_vmcall_test
4  *
5  * Copyright © 2021 Amazon.com, Inc. or its affiliates.
6  *
7  * Xen shared_info / pvclock testing
8  */
9 
10 #include "test_util.h"
11 #include "kvm_util.h"
12 #include "processor.h"
13 
14 #include <stdint.h>
15 #include <time.h>
16 #include <sched.h>
17 #include <signal.h>
18 #include <pthread.h>
19 
20 #define NR_TEST_VCPUS 20
21 
22 static struct kvm_vm *vm;
23 pthread_spinlock_t create_lock;
24 
25 #define TEST_TSC_KHZ    2345678UL
26 #define TEST_TSC_OFFSET 200000000
27 
28 uint64_t tsc_sync;
29 static void guest_code(void)
30 {
31 	uint64_t start_tsc, local_tsc, tmp;
32 
33 	start_tsc = rdtsc();
34 	do {
35 		tmp = READ_ONCE(tsc_sync);
36 		local_tsc = rdtsc();
37 		WRITE_ONCE(tsc_sync, local_tsc);
38 		if (unlikely(local_tsc < tmp))
39 			GUEST_SYNC_ARGS(0, local_tsc, tmp, 0, 0);
40 
41 	} while (local_tsc - start_tsc < 5000 * TEST_TSC_KHZ);
42 
43 	GUEST_DONE();
44 }
45 
46 
47 static void *run_vcpu(void *_cpu_nr)
48 {
49 	unsigned long vcpu_id = (unsigned long)_cpu_nr;
50 	unsigned long failures = 0;
51 	static bool first_cpu_done;
52 	struct kvm_vcpu *vcpu;
53 
54 	/* The kernel is fine, but vm_vcpu_add() needs locking */
55 	pthread_spin_lock(&create_lock);
56 
57 	vcpu = vm_vcpu_add(vm, vcpu_id, guest_code);
58 
59 	if (!first_cpu_done) {
60 		first_cpu_done = true;
61 		vcpu_set_msr(vcpu, MSR_IA32_TSC, TEST_TSC_OFFSET);
62 	}
63 
64 	pthread_spin_unlock(&create_lock);
65 
66 	for (;;) {
67 		volatile struct kvm_run *run = vcpu->run;
68                 struct ucall uc;
69 
70 		vcpu_run(vcpu);
71                 TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
72                             "Got exit_reason other than KVM_EXIT_IO: %u (%s)\n",
73                             run->exit_reason,
74                             exit_reason_str(run->exit_reason));
75 
76 		switch (get_ucall(vcpu, &uc)) {
77                 case UCALL_DONE:
78 			goto out;
79 
80                 case UCALL_SYNC:
81 			printf("Guest %d sync %lx %lx %ld\n", vcpu->id,
82 			       uc.args[2], uc.args[3], uc.args[2] - uc.args[3]);
83 			failures++;
84 			break;
85 
86                 default:
87                         TEST_FAIL("Unknown ucall %lu", uc.cmd);
88 		}
89 	}
90  out:
91 	return (void *)failures;
92 }
93 
94 int main(int argc, char *argv[])
95 {
96 	TEST_REQUIRE(kvm_has_cap(KVM_CAP_VM_TSC_CONTROL));
97 
98 	vm = vm_create(NR_TEST_VCPUS);
99 	vm_ioctl(vm, KVM_SET_TSC_KHZ, (void *) TEST_TSC_KHZ);
100 
101 	pthread_spin_init(&create_lock, PTHREAD_PROCESS_PRIVATE);
102 	pthread_t cpu_threads[NR_TEST_VCPUS];
103 	unsigned long cpu;
104 	for (cpu = 0; cpu < NR_TEST_VCPUS; cpu++)
105 		pthread_create(&cpu_threads[cpu], NULL, run_vcpu, (void *)cpu);
106 
107 	unsigned long failures = 0;
108 	for (cpu = 0; cpu < NR_TEST_VCPUS; cpu++) {
109 		void *this_cpu_failures;
110 		pthread_join(cpu_threads[cpu], &this_cpu_failures);
111 		failures += (unsigned long)this_cpu_failures;
112 	}
113 
114 	TEST_ASSERT(!failures, "TSC sync failed");
115 	pthread_spin_destroy(&create_lock);
116 	kvm_vm_free(vm);
117 	return 0;
118 }
119