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