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