1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2022 Oxide Computer Company
14  */
15 
16 #include <stdio.h>
17 #include <unistd.h>
18 #include <stdlib.h>
19 #include <strings.h>
20 #include <libgen.h>
21 #include <assert.h>
22 #include <errno.h>
23 
24 #include <sys/types.h>
25 #include <sys/sysmacros.h>
26 #include <sys/debug.h>
27 #include <sys/vmm.h>
28 #include <sys/vmm_dev.h>
29 #include <vmmapi.h>
30 
31 #include "in_guest.h"
32 #include "test_defs.h"
33 
34 typedef struct reading {
35 	hrtime_t	when;
36 	uint32_t	value;
37 } reading_t;
38 
39 static bool
check_reading(reading_t before,reading_t after,uint_t divisor,uint_t loops,uint_t tick_margin,uint_t ppm_margin)40 check_reading(reading_t before, reading_t after, uint_t divisor, uint_t loops,
41     uint_t tick_margin, uint_t ppm_margin)
42 {
43 	const hrtime_t time_delta = after.when - before.when;
44 
45 
46 	/*
47 	 * The ticks margin should shrink proportionally to how coarsely the
48 	 * timer clock is being divided.
49 	 */
50 	tick_margin /= divisor;
51 
52 	/*
53 	 * The 'before' measurement includes the ticks which occurred between
54 	 * programming the timer and taking the first reading.  The 'after'
55 	 * measurement includes the number of loops (each consisting of the
56 	 * target tick count) plus however many ticks had transpired since the
57 	 * most recent roll-over.
58 	 */
59 	const uint32_t tick_delta =
60 	    loops * LAPIC_TARGET_TICKS + before.value - after.value;
61 	const uint32_t tick_target = loops * LAPIC_TARGET_TICKS;
62 
63 	/* is the number of ticks OK? */
64 	if (tick_delta < tick_target) {
65 		if ((tick_target - tick_delta) > tick_margin) {
66 			(void) printf("%u ticks outside margin %u\n",
67 			    tick_delta, tick_target - tick_margin);
68 		}
69 	} else if ((tick_delta - tick_target) > tick_margin) {
70 		(void) printf("%u ticks outside margin %u\n", tick_delta,
71 		    tick_target + tick_margin);
72 		return (false);
73 	}
74 
75 	hrtime_t time_target = (tick_delta * NANOSEC * divisor) / LAPIC_FREQ;
76 
77 	hrtime_t offset;
78 	if (time_delta < time_target) {
79 		offset = time_target - time_delta;
80 	} else {
81 		offset = time_delta - time_target;
82 	}
83 	uint64_t ppm = (offset * 1000000) / time_target;
84 	(void) printf("params: tick_margin=%u ppm_margin=%lu divisor=%u\n",
85 	    tick_margin, ppm_margin, divisor);
86 	(void) printf("%u ticks in %lu ns (error %lu ppm)\n",
87 	    tick_delta, time_delta, ppm);
88 	if (ppm > ppm_margin) {
89 		(void) printf("UNACCEPTABLE!\n");
90 		return (false);
91 	}
92 	return (true);
93 }
94 
95 
96 static void
run_test(struct vcpu * vcpu,uint_t divisor,uint_t loops,struct vm_entry * ventry,struct vm_exit * vexit)97 run_test(struct vcpu *vcpu, uint_t divisor, uint_t loops,
98     struct vm_entry *ventry, struct vm_exit *vexit)
99 {
100 	reading_t readings[2];
101 	uint_t nread = 0;
102 	uint_t nrepeat = 0;
103 
104 	const uint_t margin_ticks = MAX(1, LAPIC_TARGET_TICKS / 5000);
105 	const uint_t margin_ppm = 400;
106 
107 	do {
108 		const enum vm_exit_kind kind =
109 		    test_run_vcpu(vcpu, ventry, vexit);
110 		if (kind == VEK_REENTR) {
111 			continue;
112 		} else if (kind != VEK_UNHANDLED) {
113 			test_fail_vmexit(vexit);
114 		}
115 
116 		/* input the divisor (bits 0-15) and loop count (bits 16-31) */
117 		if (vexit_match_inout(vexit, true, IOP_TEST_PARAM0, 2, NULL)) {
118 			ventry_fulfill_inout(vexit, ventry, divisor);
119 			continue;
120 		}
121 		/* input the loop count */
122 		if (vexit_match_inout(vexit, true, IOP_TEST_PARAM1, 2, NULL)) {
123 			ventry_fulfill_inout(vexit, ventry, loops);
124 			continue;
125 		}
126 
127 		uint32_t v;
128 		if (vexit_match_inout(vexit, false, IOP_TEST_VALUE, 4, &v)) {
129 			readings[nread].when = gethrtime();
130 			readings[nread].value = v;
131 			ventry_fulfill_inout(vexit, ventry, 0);
132 
133 			nread++;
134 			if (nread != 2) {
135 				continue;
136 			}
137 
138 			if (check_reading(readings[0], readings[1], divisor,
139 			    loops, margin_ticks, margin_ppm)) {
140 				(void) printf("good result\n");
141 				return;
142 			} else {
143 				nrepeat++;
144 				if (nrepeat < 3) {
145 					nread = 0;
146 					(void) printf("retry %u\n", nrepeat);
147 					continue;
148 				}
149 				test_fail_msg("bad result after %u retries\n",
150 				    nrepeat);
151 			}
152 		} else {
153 			test_fail_vmexit(vexit);
154 		}
155 	} while (true);
156 }
157 
158 int
main(int argc,char * argv[])159 main(int argc, char *argv[])
160 {
161 	const char *test_suite_name = basename(argv[0]);
162 	struct vmctx *ctx = NULL;
163 	struct vcpu *vcpu;
164 	int err;
165 
166 	ctx = test_initialize(test_suite_name);
167 
168 	if ((vcpu = vm_vcpu_open(ctx, 0)) == NULL) {
169 		test_fail_errno(errno, "Could not open vcpu0");
170 	}
171 
172 	err = test_setup_vcpu(vcpu, MEM_LOC_PAYLOAD, MEM_LOC_STACK);
173 	if (err != 0) {
174 		test_fail_errno(err, "Could not initialize vcpu0");
175 	}
176 
177 	struct vm_entry ventry = { 0 };
178 	struct vm_exit vexit = { 0 };
179 
180 	run_test(vcpu, 4, 3, &ventry, &vexit);
181 	run_test(vcpu, 2, 4, &ventry, &vexit);
182 	test_pass();
183 	return (0);
184 }
185