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 
18 #define VCPU_ID		5
19 
20 #define SHINFO_REGION_GVA	0xc0000000ULL
21 #define SHINFO_REGION_GPA	0xc0000000ULL
22 #define SHINFO_REGION_SLOT	10
23 #define PAGE_SIZE		4096
24 
25 #define PVTIME_ADDR	(SHINFO_REGION_GPA + PAGE_SIZE)
26 #define RUNSTATE_ADDR	(SHINFO_REGION_GPA + PAGE_SIZE + 0x20)
27 #define VCPU_INFO_ADDR	(SHINFO_REGION_GPA + 0x40)
28 
29 #define RUNSTATE_VADDR	(SHINFO_REGION_GVA + PAGE_SIZE + 0x20)
30 #define VCPU_INFO_VADDR	(SHINFO_REGION_GVA + 0x40)
31 
32 #define EVTCHN_VECTOR	0x10
33 
34 static struct kvm_vm *vm;
35 
36 #define XEN_HYPERCALL_MSR	0x40000000
37 
38 #define MIN_STEAL_TIME		50000
39 
40 struct pvclock_vcpu_time_info {
41         u32   version;
42         u32   pad0;
43         u64   tsc_timestamp;
44         u64   system_time;
45         u32   tsc_to_system_mul;
46         s8    tsc_shift;
47         u8    flags;
48         u8    pad[2];
49 } __attribute__((__packed__)); /* 32 bytes */
50 
51 struct pvclock_wall_clock {
52         u32   version;
53         u32   sec;
54         u32   nsec;
55 } __attribute__((__packed__));
56 
57 struct vcpu_runstate_info {
58     uint32_t state;
59     uint64_t state_entry_time;
60     uint64_t time[4];
61 };
62 
63 struct arch_vcpu_info {
64     unsigned long cr2;
65     unsigned long pad; /* sizeof(vcpu_info_t) == 64 */
66 };
67 
68 struct vcpu_info {
69         uint8_t evtchn_upcall_pending;
70         uint8_t evtchn_upcall_mask;
71         unsigned long evtchn_pending_sel;
72         struct arch_vcpu_info arch;
73         struct pvclock_vcpu_time_info time;
74 }; /* 64 bytes (x86) */
75 
76 #define RUNSTATE_running  0
77 #define RUNSTATE_runnable 1
78 #define RUNSTATE_blocked  2
79 #define RUNSTATE_offline  3
80 
81 static void evtchn_handler(struct ex_regs *regs)
82 {
83 	struct vcpu_info *vi = (void *)VCPU_INFO_VADDR;
84 	vi->evtchn_upcall_pending = 0;
85 
86 	GUEST_SYNC(0x20);
87 }
88 
89 static void guest_code(void)
90 {
91 	struct vcpu_runstate_info *rs = (void *)RUNSTATE_VADDR;
92 
93 	__asm__ __volatile__(
94 		"sti\n"
95 		"nop\n"
96 	);
97 
98 	/* Trigger an interrupt injection */
99 	GUEST_SYNC(0);
100 
101 	/* Test having the host set runstates manually */
102 	GUEST_SYNC(RUNSTATE_runnable);
103 	GUEST_ASSERT(rs->time[RUNSTATE_runnable] != 0);
104 	GUEST_ASSERT(rs->state == 0);
105 
106 	GUEST_SYNC(RUNSTATE_blocked);
107 	GUEST_ASSERT(rs->time[RUNSTATE_blocked] != 0);
108 	GUEST_ASSERT(rs->state == 0);
109 
110 	GUEST_SYNC(RUNSTATE_offline);
111 	GUEST_ASSERT(rs->time[RUNSTATE_offline] != 0);
112 	GUEST_ASSERT(rs->state == 0);
113 
114 	/* Test runstate time adjust */
115 	GUEST_SYNC(4);
116 	GUEST_ASSERT(rs->time[RUNSTATE_blocked] == 0x5a);
117 	GUEST_ASSERT(rs->time[RUNSTATE_offline] == 0x6b6b);
118 
119 	/* Test runstate time set */
120 	GUEST_SYNC(5);
121 	GUEST_ASSERT(rs->state_entry_time >= 0x8000);
122 	GUEST_ASSERT(rs->time[RUNSTATE_runnable] == 0);
123 	GUEST_ASSERT(rs->time[RUNSTATE_blocked] == 0x6b6b);
124 	GUEST_ASSERT(rs->time[RUNSTATE_offline] == 0x5a);
125 
126 	/* sched_yield() should result in some 'runnable' time */
127 	GUEST_SYNC(6);
128 	GUEST_ASSERT(rs->time[RUNSTATE_runnable] >= MIN_STEAL_TIME);
129 
130 	GUEST_DONE();
131 }
132 
133 static int cmp_timespec(struct timespec *a, struct timespec *b)
134 {
135 	if (a->tv_sec > b->tv_sec)
136 		return 1;
137 	else if (a->tv_sec < b->tv_sec)
138 		return -1;
139 	else if (a->tv_nsec > b->tv_nsec)
140 		return 1;
141 	else if (a->tv_nsec < b->tv_nsec)
142 		return -1;
143 	else
144 		return 0;
145 }
146 
147 int main(int argc, char *argv[])
148 {
149 	struct timespec min_ts, max_ts, vm_ts;
150 
151 	int xen_caps = kvm_check_cap(KVM_CAP_XEN_HVM);
152 	if (!(xen_caps & KVM_XEN_HVM_CONFIG_SHARED_INFO) ) {
153 		print_skip("KVM_XEN_HVM_CONFIG_SHARED_INFO not available");
154 		exit(KSFT_SKIP);
155 	}
156 
157 	bool do_runstate_tests = !!(xen_caps & KVM_XEN_HVM_CONFIG_RUNSTATE);
158 
159 	clock_gettime(CLOCK_REALTIME, &min_ts);
160 
161 	vm = vm_create_default(VCPU_ID, 0, (void *) guest_code);
162 	vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
163 
164 	/* Map a region for the shared_info page */
165 	vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
166 				    SHINFO_REGION_GPA, SHINFO_REGION_SLOT, 2, 0);
167 	virt_map(vm, SHINFO_REGION_GVA, SHINFO_REGION_GPA, 2);
168 
169 	struct kvm_xen_hvm_config hvmc = {
170 		.flags = KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL,
171 		.msr = XEN_HYPERCALL_MSR,
172 	};
173 	vm_ioctl(vm, KVM_XEN_HVM_CONFIG, &hvmc);
174 
175 	struct kvm_xen_hvm_attr lm = {
176 		.type = KVM_XEN_ATTR_TYPE_LONG_MODE,
177 		.u.long_mode = 1,
178 	};
179 	vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &lm);
180 
181 	struct kvm_xen_hvm_attr ha = {
182 		.type = KVM_XEN_ATTR_TYPE_SHARED_INFO,
183 		.u.shared_info.gfn = SHINFO_REGION_GPA / PAGE_SIZE,
184 	};
185 	vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &ha);
186 
187 	struct kvm_xen_vcpu_attr vi = {
188 		.type = KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO,
189 		.u.gpa = VCPU_INFO_ADDR,
190 	};
191 	vcpu_ioctl(vm, VCPU_ID, KVM_XEN_VCPU_SET_ATTR, &vi);
192 
193 	struct kvm_xen_vcpu_attr pvclock = {
194 		.type = KVM_XEN_VCPU_ATTR_TYPE_VCPU_TIME_INFO,
195 		.u.gpa = PVTIME_ADDR,
196 	};
197 	vcpu_ioctl(vm, VCPU_ID, KVM_XEN_VCPU_SET_ATTR, &pvclock);
198 
199 	struct kvm_xen_hvm_attr vec = {
200 		.type = KVM_XEN_ATTR_TYPE_UPCALL_VECTOR,
201 		.u.vector = EVTCHN_VECTOR,
202 	};
203 	vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &vec);
204 
205 	vm_init_descriptor_tables(vm);
206 	vcpu_init_descriptor_tables(vm, VCPU_ID);
207 	vm_install_exception_handler(vm, EVTCHN_VECTOR, evtchn_handler);
208 
209 	if (do_runstate_tests) {
210 		struct kvm_xen_vcpu_attr st = {
211 			.type = KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_ADDR,
212 			.u.gpa = RUNSTATE_ADDR,
213 		};
214 		vcpu_ioctl(vm, VCPU_ID, KVM_XEN_VCPU_SET_ATTR, &st);
215 	}
216 
217 	struct vcpu_info *vinfo = addr_gpa2hva(vm, VCPU_INFO_VADDR);
218 	vinfo->evtchn_upcall_pending = 0;
219 
220 	struct vcpu_runstate_info *rs = addr_gpa2hva(vm, RUNSTATE_ADDR);
221 	rs->state = 0x5a;
222 
223 	bool evtchn_irq_expected = false;
224 
225 	for (;;) {
226 		volatile struct kvm_run *run = vcpu_state(vm, VCPU_ID);
227 		struct ucall uc;
228 
229 		vcpu_run(vm, VCPU_ID);
230 
231 		TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
232 			    "Got exit_reason other than KVM_EXIT_IO: %u (%s)\n",
233 			    run->exit_reason,
234 			    exit_reason_str(run->exit_reason));
235 
236 		switch (get_ucall(vm, VCPU_ID, &uc)) {
237 		case UCALL_ABORT:
238 			TEST_FAIL("%s", (const char *)uc.args[0]);
239 			/* NOT REACHED */
240 		case UCALL_SYNC: {
241 			struct kvm_xen_vcpu_attr rst;
242 			long rundelay;
243 
244 			if (do_runstate_tests)
245 				TEST_ASSERT(rs->state_entry_time == rs->time[0] +
246 					    rs->time[1] + rs->time[2] + rs->time[3],
247 					    "runstate times don't add up");
248 
249 			switch (uc.args[1]) {
250 			case 0:
251 				evtchn_irq_expected = true;
252 				vinfo->evtchn_upcall_pending = 1;
253 				break;
254 
255 			case RUNSTATE_runnable...RUNSTATE_offline:
256 				TEST_ASSERT(!evtchn_irq_expected, "Event channel IRQ not seen");
257 				if (!do_runstate_tests)
258 					goto done;
259 				rst.type = KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_CURRENT;
260 				rst.u.runstate.state = uc.args[1];
261 				vcpu_ioctl(vm, VCPU_ID, KVM_XEN_VCPU_SET_ATTR, &rst);
262 				break;
263 			case 4:
264 				rst.type = KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_ADJUST;
265 				memset(&rst.u, 0, sizeof(rst.u));
266 				rst.u.runstate.state = (uint64_t)-1;
267 				rst.u.runstate.time_blocked =
268 					0x5a - rs->time[RUNSTATE_blocked];
269 				rst.u.runstate.time_offline =
270 					0x6b6b - rs->time[RUNSTATE_offline];
271 				rst.u.runstate.time_runnable = -rst.u.runstate.time_blocked -
272 					rst.u.runstate.time_offline;
273 				vcpu_ioctl(vm, VCPU_ID, KVM_XEN_VCPU_SET_ATTR, &rst);
274 				break;
275 
276 			case 5:
277 				rst.type = KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_DATA;
278 				memset(&rst.u, 0, sizeof(rst.u));
279 				rst.u.runstate.state = RUNSTATE_running;
280 				rst.u.runstate.state_entry_time = 0x6b6b + 0x5a;
281 				rst.u.runstate.time_blocked = 0x6b6b;
282 				rst.u.runstate.time_offline = 0x5a;
283 				vcpu_ioctl(vm, VCPU_ID, KVM_XEN_VCPU_SET_ATTR, &rst);
284 				break;
285 			case 6:
286 				/* Yield until scheduler delay exceeds target */
287 				rundelay = get_run_delay() + MIN_STEAL_TIME;
288 				do {
289 					sched_yield();
290 				} while (get_run_delay() < rundelay);
291 				break;
292 			case 0x20:
293 				TEST_ASSERT(evtchn_irq_expected, "Unexpected event channel IRQ");
294 				evtchn_irq_expected = false;
295 				break;
296 			}
297 			break;
298 		}
299 		case UCALL_DONE:
300 			goto done;
301 		default:
302 			TEST_FAIL("Unknown ucall 0x%lx.", uc.cmd);
303 		}
304 	}
305 
306  done:
307 	clock_gettime(CLOCK_REALTIME, &max_ts);
308 
309 	/*
310 	 * Just a *really* basic check that things are being put in the
311 	 * right place. The actual calculations are much the same for
312 	 * Xen as they are for the KVM variants, so no need to check.
313 	 */
314 	struct pvclock_wall_clock *wc;
315 	struct pvclock_vcpu_time_info *ti, *ti2;
316 
317 	wc = addr_gpa2hva(vm, SHINFO_REGION_GPA + 0xc00);
318 	ti = addr_gpa2hva(vm, SHINFO_REGION_GPA + 0x40 + 0x20);
319 	ti2 = addr_gpa2hva(vm, PVTIME_ADDR);
320 
321 	vm_ts.tv_sec = wc->sec;
322 	vm_ts.tv_nsec = wc->nsec;
323         TEST_ASSERT(wc->version && !(wc->version & 1),
324 		    "Bad wallclock version %x", wc->version);
325 	TEST_ASSERT(cmp_timespec(&min_ts, &vm_ts) <= 0, "VM time too old");
326 	TEST_ASSERT(cmp_timespec(&max_ts, &vm_ts) >= 0, "VM time too new");
327 
328 	TEST_ASSERT(ti->version && !(ti->version & 1),
329 		    "Bad time_info version %x", ti->version);
330 	TEST_ASSERT(ti2->version && !(ti2->version & 1),
331 		    "Bad time_info version %x", ti->version);
332 
333 	if (do_runstate_tests) {
334 		/*
335 		 * Fetch runstate and check sanity. Strictly speaking in the
336 		 * general case we might not expect the numbers to be identical
337 		 * but in this case we know we aren't running the vCPU any more.
338 		 */
339 		struct kvm_xen_vcpu_attr rst = {
340 			.type = KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_DATA,
341 		};
342 		vcpu_ioctl(vm, VCPU_ID, KVM_XEN_VCPU_GET_ATTR, &rst);
343 
344 		TEST_ASSERT(rs->state == rst.u.runstate.state, "Runstate mismatch");
345 		TEST_ASSERT(rs->state_entry_time == rst.u.runstate.state_entry_time,
346 			    "State entry time mismatch");
347 		TEST_ASSERT(rs->time[RUNSTATE_running] == rst.u.runstate.time_running,
348 			    "Running time mismatch");
349 		TEST_ASSERT(rs->time[RUNSTATE_runnable] == rst.u.runstate.time_runnable,
350 			    "Runnable time mismatch");
351 		TEST_ASSERT(rs->time[RUNSTATE_blocked] == rst.u.runstate.time_blocked,
352 			    "Blocked time mismatch");
353 		TEST_ASSERT(rs->time[RUNSTATE_offline] == rst.u.runstate.time_offline,
354 			    "Offline time mismatch");
355 
356 		TEST_ASSERT(rs->state_entry_time == rs->time[0] +
357 			    rs->time[1] + rs->time[2] + rs->time[3],
358 			    "runstate times don't add up");
359 	}
360 	kvm_vm_free(vm);
361 	return 0;
362 }
363