1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * steal/stolen time test
4  *
5  * Copyright (C) 2020, Red Hat, Inc.
6  */
7 #define _GNU_SOURCE
8 #include <stdio.h>
9 #include <time.h>
10 #include <sched.h>
11 #include <pthread.h>
12 #include <linux/kernel.h>
13 #include <asm/kvm.h>
14 #include <asm/kvm_para.h>
15 
16 #include "test_util.h"
17 #include "kvm_util.h"
18 #include "processor.h"
19 
20 #define NR_VCPUS		4
21 #define ST_GPA_BASE		(1 << 30)
22 
23 static void *st_gva[NR_VCPUS];
24 static uint64_t guest_stolen_time[NR_VCPUS];
25 
26 #if defined(__x86_64__)
27 
28 /* steal_time must have 64-byte alignment */
29 #define STEAL_TIME_SIZE		((sizeof(struct kvm_steal_time) + 63) & ~63)
30 
31 static void check_status(struct kvm_steal_time *st)
32 {
33 	GUEST_ASSERT(!(READ_ONCE(st->version) & 1));
34 	GUEST_ASSERT(READ_ONCE(st->flags) == 0);
35 	GUEST_ASSERT(READ_ONCE(st->preempted) == 0);
36 }
37 
38 static void guest_code(int cpu)
39 {
40 	struct kvm_steal_time *st = st_gva[cpu];
41 	uint32_t version;
42 
43 	GUEST_ASSERT(rdmsr(MSR_KVM_STEAL_TIME) == ((uint64_t)st_gva[cpu] | KVM_MSR_ENABLED));
44 
45 	memset(st, 0, sizeof(*st));
46 	GUEST_SYNC(0);
47 
48 	check_status(st);
49 	WRITE_ONCE(guest_stolen_time[cpu], st->steal);
50 	version = READ_ONCE(st->version);
51 	check_status(st);
52 	GUEST_SYNC(1);
53 
54 	check_status(st);
55 	GUEST_ASSERT(version < READ_ONCE(st->version));
56 	WRITE_ONCE(guest_stolen_time[cpu], st->steal);
57 	check_status(st);
58 	GUEST_DONE();
59 }
60 
61 static void steal_time_init(struct kvm_vm *vm)
62 {
63 	int i;
64 
65 	if (!(kvm_get_supported_cpuid_entry(KVM_CPUID_FEATURES)->eax &
66 	      KVM_FEATURE_STEAL_TIME)) {
67 		print_skip("steal-time not supported");
68 		exit(KSFT_SKIP);
69 	}
70 
71 	for (i = 0; i < NR_VCPUS; ++i) {
72 		int ret;
73 
74 		/* ST_GPA_BASE is identity mapped */
75 		st_gva[i] = (void *)(ST_GPA_BASE + i * STEAL_TIME_SIZE);
76 		sync_global_to_guest(vm, st_gva[i]);
77 
78 		ret = _vcpu_set_msr(vm, i, MSR_KVM_STEAL_TIME, (ulong)st_gva[i] | KVM_STEAL_RESERVED_MASK);
79 		TEST_ASSERT(ret == 0, "Bad GPA didn't fail");
80 
81 		vcpu_set_msr(vm, i, MSR_KVM_STEAL_TIME, (ulong)st_gva[i] | KVM_MSR_ENABLED);
82 	}
83 }
84 
85 static void steal_time_dump(struct kvm_vm *vm, uint32_t vcpuid)
86 {
87 	struct kvm_steal_time *st = addr_gva2hva(vm, (ulong)st_gva[vcpuid]);
88 	int i;
89 
90 	pr_info("VCPU%d:\n", vcpuid);
91 	pr_info("    steal:     %lld\n", st->steal);
92 	pr_info("    version:   %d\n", st->version);
93 	pr_info("    flags:     %d\n", st->flags);
94 	pr_info("    preempted: %d\n", st->preempted);
95 	pr_info("    u8_pad:    ");
96 	for (i = 0; i < 3; ++i)
97 		pr_info("%d", st->u8_pad[i]);
98 	pr_info("\n    pad:       ");
99 	for (i = 0; i < 11; ++i)
100 		pr_info("%d", st->pad[i]);
101 	pr_info("\n");
102 }
103 
104 #elif defined(__aarch64__)
105 
106 /* PV_TIME_ST must have 64-byte alignment */
107 #define STEAL_TIME_SIZE		((sizeof(struct st_time) + 63) & ~63)
108 
109 #define SMCCC_ARCH_FEATURES	0x80000001
110 #define PV_TIME_FEATURES	0xc5000020
111 #define PV_TIME_ST		0xc5000021
112 
113 struct st_time {
114 	uint32_t rev;
115 	uint32_t attr;
116 	uint64_t st_time;
117 };
118 
119 static int64_t smccc(uint32_t func, uint64_t arg)
120 {
121 	struct arm_smccc_res res;
122 
123 	smccc_hvc(func, arg, 0, 0, 0, 0, 0, 0, &res);
124 	return res.a0;
125 }
126 
127 static void check_status(struct st_time *st)
128 {
129 	GUEST_ASSERT(READ_ONCE(st->rev) == 0);
130 	GUEST_ASSERT(READ_ONCE(st->attr) == 0);
131 }
132 
133 static void guest_code(int cpu)
134 {
135 	struct st_time *st;
136 	int64_t status;
137 
138 	status = smccc(SMCCC_ARCH_FEATURES, PV_TIME_FEATURES);
139 	GUEST_ASSERT(status == 0);
140 	status = smccc(PV_TIME_FEATURES, PV_TIME_FEATURES);
141 	GUEST_ASSERT(status == 0);
142 	status = smccc(PV_TIME_FEATURES, PV_TIME_ST);
143 	GUEST_ASSERT(status == 0);
144 
145 	status = smccc(PV_TIME_ST, 0);
146 	GUEST_ASSERT(status != -1);
147 	GUEST_ASSERT(status == (ulong)st_gva[cpu]);
148 
149 	st = (struct st_time *)status;
150 	GUEST_SYNC(0);
151 
152 	check_status(st);
153 	WRITE_ONCE(guest_stolen_time[cpu], st->st_time);
154 	GUEST_SYNC(1);
155 
156 	check_status(st);
157 	WRITE_ONCE(guest_stolen_time[cpu], st->st_time);
158 	GUEST_DONE();
159 }
160 
161 static void steal_time_init(struct kvm_vm *vm)
162 {
163 	struct kvm_device_attr dev = {
164 		.group = KVM_ARM_VCPU_PVTIME_CTRL,
165 		.attr = KVM_ARM_VCPU_PVTIME_IPA,
166 	};
167 	int i, ret;
168 
169 	ret = _vcpu_ioctl(vm, 0, KVM_HAS_DEVICE_ATTR, &dev);
170 	if (ret != 0 && errno == ENXIO) {
171 		print_skip("steal-time not supported");
172 		exit(KSFT_SKIP);
173 	}
174 
175 	for (i = 0; i < NR_VCPUS; ++i) {
176 		uint64_t st_ipa;
177 
178 		vcpu_ioctl(vm, i, KVM_HAS_DEVICE_ATTR, &dev);
179 
180 		dev.addr = (uint64_t)&st_ipa;
181 
182 		/* ST_GPA_BASE is identity mapped */
183 		st_gva[i] = (void *)(ST_GPA_BASE + i * STEAL_TIME_SIZE);
184 		sync_global_to_guest(vm, st_gva[i]);
185 
186 		st_ipa = (ulong)st_gva[i] | 1;
187 		ret = _vcpu_ioctl(vm, i, KVM_SET_DEVICE_ATTR, &dev);
188 		TEST_ASSERT(ret == -1 && errno == EINVAL, "Bad IPA didn't report EINVAL");
189 
190 		st_ipa = (ulong)st_gva[i];
191 		vcpu_ioctl(vm, i, KVM_SET_DEVICE_ATTR, &dev);
192 
193 		ret = _vcpu_ioctl(vm, i, KVM_SET_DEVICE_ATTR, &dev);
194 		TEST_ASSERT(ret == -1 && errno == EEXIST, "Set IPA twice without EEXIST");
195 
196 	}
197 }
198 
199 static void steal_time_dump(struct kvm_vm *vm, uint32_t vcpuid)
200 {
201 	struct st_time *st = addr_gva2hva(vm, (ulong)st_gva[vcpuid]);
202 
203 	pr_info("VCPU%d:\n", vcpuid);
204 	pr_info("    rev:     %d\n", st->rev);
205 	pr_info("    attr:    %d\n", st->attr);
206 	pr_info("    st_time: %ld\n", st->st_time);
207 }
208 
209 #endif
210 
211 static void *do_steal_time(void *arg)
212 {
213 	struct timespec ts, stop;
214 
215 	clock_gettime(CLOCK_MONOTONIC, &ts);
216 	stop = timespec_add_ns(ts, MIN_RUN_DELAY_NS);
217 
218 	while (1) {
219 		clock_gettime(CLOCK_MONOTONIC, &ts);
220 		if (timespec_to_ns(timespec_sub(ts, stop)) >= 0)
221 			break;
222 	}
223 
224 	return NULL;
225 }
226 
227 static void run_vcpu(struct kvm_vm *vm, uint32_t vcpuid)
228 {
229 	struct ucall uc;
230 
231 	vcpu_args_set(vm, vcpuid, 1, vcpuid);
232 
233 	vcpu_ioctl(vm, vcpuid, KVM_RUN, NULL);
234 
235 	switch (get_ucall(vm, vcpuid, &uc)) {
236 	case UCALL_SYNC:
237 	case UCALL_DONE:
238 		break;
239 	case UCALL_ABORT:
240 		TEST_ASSERT(false, "%s at %s:%ld", (const char *)uc.args[0],
241 			    __FILE__, uc.args[1]);
242 	default:
243 		TEST_ASSERT(false, "Unexpected exit: %s",
244 			    exit_reason_str(vcpu_state(vm, vcpuid)->exit_reason));
245 	}
246 }
247 
248 int main(int ac, char **av)
249 {
250 	struct kvm_vm *vm;
251 	pthread_attr_t attr;
252 	pthread_t thread;
253 	cpu_set_t cpuset;
254 	unsigned int gpages;
255 	long stolen_time;
256 	long run_delay;
257 	bool verbose;
258 	int i;
259 
260 	verbose = ac > 1 && (!strncmp(av[1], "-v", 3) || !strncmp(av[1], "--verbose", 10));
261 
262 	/* Set CPU affinity so we can force preemption of the VCPU */
263 	CPU_ZERO(&cpuset);
264 	CPU_SET(0, &cpuset);
265 	pthread_attr_init(&attr);
266 	pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &cpuset);
267 	pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
268 
269 	/* Create a one VCPU guest and an identity mapped memslot for the steal time structure */
270 	vm = vm_create_default(0, 0, guest_code);
271 	gpages = vm_calc_num_guest_pages(VM_MODE_DEFAULT, STEAL_TIME_SIZE * NR_VCPUS);
272 	vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, ST_GPA_BASE, 1, gpages, 0);
273 	virt_map(vm, ST_GPA_BASE, ST_GPA_BASE, gpages);
274 	ucall_init(vm, NULL);
275 
276 	/* Add the rest of the VCPUs */
277 	for (i = 1; i < NR_VCPUS; ++i)
278 		vm_vcpu_add_default(vm, i, guest_code);
279 
280 	steal_time_init(vm);
281 
282 	/* Run test on each VCPU */
283 	for (i = 0; i < NR_VCPUS; ++i) {
284 		/* First VCPU run initializes steal-time */
285 		run_vcpu(vm, i);
286 
287 		/* Second VCPU run, expect guest stolen time to be <= run_delay */
288 		run_vcpu(vm, i);
289 		sync_global_from_guest(vm, guest_stolen_time[i]);
290 		stolen_time = guest_stolen_time[i];
291 		run_delay = get_run_delay();
292 		TEST_ASSERT(stolen_time <= run_delay,
293 			    "Expected stolen time <= %ld, got %ld",
294 			    run_delay, stolen_time);
295 
296 		/* Steal time from the VCPU. The steal time thread has the same CPU affinity as the VCPUs. */
297 		run_delay = get_run_delay();
298 		pthread_create(&thread, &attr, do_steal_time, NULL);
299 		do
300 			sched_yield();
301 		while (get_run_delay() - run_delay < MIN_RUN_DELAY_NS);
302 		pthread_join(thread, NULL);
303 		run_delay = get_run_delay() - run_delay;
304 		TEST_ASSERT(run_delay >= MIN_RUN_DELAY_NS,
305 			    "Expected run_delay >= %ld, got %ld",
306 			    MIN_RUN_DELAY_NS, run_delay);
307 
308 		/* Run VCPU again to confirm stolen time is consistent with run_delay */
309 		run_vcpu(vm, i);
310 		sync_global_from_guest(vm, guest_stolen_time[i]);
311 		stolen_time = guest_stolen_time[i] - stolen_time;
312 		TEST_ASSERT(stolen_time >= run_delay,
313 			    "Expected stolen time >= %ld, got %ld",
314 			    run_delay, stolen_time);
315 
316 		if (verbose) {
317 			pr_info("VCPU%d: total-stolen-time=%ld test-stolen-time=%ld", i,
318 				guest_stolen_time[i], stolen_time);
319 			if (stolen_time == run_delay)
320 				pr_info(" (BONUS: guest test-stolen-time even exactly matches test-run_delay)");
321 			pr_info("\n");
322 			steal_time_dump(vm, i);
323 		}
324 	}
325 
326 	return 0;
327 }
328