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