xref: /illumos-gate/usr/src/test/bhyve-tests/tests/kdev/vlapic_freq.c (revision 201ceb75ab95f9bf1f42ea1dc9ab363b43ba47cf)
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
40 check_reading(reading_t before, reading_t after, uint_t divisor,
41     uint_t tick_margin, uint_t ppm_margin)
42 {
43 	hrtime_t time_delta = after.when - before.when;
44 	uint32_t tick_delta;
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 	/* timer is counting down, so act appropriately */
53 	if (after.value > before.value) {
54 		/* handle rollover */
55 		tick_delta = (UINT32_MAX - after.value) + before.value;
56 	} else {
57 		tick_delta = before.value - after.value;
58 	}
59 
60 	/* is the number of ticks OK? */
61 	if (tick_delta < LAPIC_TARGET_TICKS) {
62 		test_fail_msg("inadequate passage of ticks %u < %u\n",
63 		    tick_delta, LAPIC_TARGET_TICKS);
64 	} else if ((tick_delta - LAPIC_TARGET_TICKS) > tick_margin) {
65 		(void) printf("%u ticks outside margin %u\n", tick_delta,
66 		    LAPIC_TARGET_TICKS + tick_margin);
67 		return (false);
68 	}
69 
70 	hrtime_t time_target = (tick_delta * NANOSEC * divisor) / LAPIC_FREQ;
71 
72 	hrtime_t offset;
73 	if (time_delta < time_target) {
74 		offset = time_target - time_delta;
75 	} else {
76 		offset = time_delta - time_target;
77 	}
78 	uint64_t ppm = (offset * 1000000) / time_target;
79 	(void) printf("params: tick_margin=%u ppm_margin=%lu divisor=%u\n",
80 	    tick_margin, ppm_margin, divisor);
81 	(void) printf("%u ticks in %lu ns (error %lu ppm)\n",
82 	    tick_delta, time_delta, ppm);
83 	if (ppm > ppm_margin) {
84 		(void) printf("UNACCEPTABLE!\n");
85 		return (false);
86 	}
87 	return (true);
88 }
89 
90 
91 static void
92 test_for_divisor(struct vcpu *vcpu, uint_t divisor, struct vm_entry *ventry,
93     struct vm_exit *vexit)
94 {
95 	reading_t readings[2];
96 	uint_t nread = 0;
97 	uint_t nrepeat = 0;
98 
99 	const uint_t margin_ticks = MAX(1, LAPIC_TARGET_TICKS / 5000);
100 	const uint_t margin_ppm = 400;
101 
102 	do {
103 		const enum vm_exit_kind kind =
104 		    test_run_vcpu(vcpu, ventry, vexit);
105 		if (kind == VEK_REENTR) {
106 			continue;
107 		} else if (kind != VEK_UNHANDLED) {
108 			test_fail_vmexit(vexit);
109 		}
110 
111 		/* input the divisor */
112 		if (vexit_match_inout(vexit, true, IOP_TEST_PARAM, 4, NULL)) {
113 			ventry_fulfill_inout(vexit, ventry, divisor);
114 			continue;
115 		}
116 
117 		uint32_t v;
118 		if (vexit_match_inout(vexit, false, IOP_TEST_VALUE, 4, &v)) {
119 			readings[nread].when = gethrtime();
120 			readings[nread].value = v;
121 			ventry_fulfill_inout(vexit, ventry, 0);
122 
123 			nread++;
124 			if (nread != 2) {
125 				continue;
126 			}
127 
128 			if (check_reading(readings[0], readings[1], divisor,
129 			    margin_ticks, margin_ppm)) {
130 				(void) printf("good result\n");
131 				return;
132 			} else {
133 				nrepeat++;
134 				if (nrepeat < 3) {
135 					nread = 0;
136 					(void) printf("retry %u\n", nrepeat);
137 					continue;
138 				}
139 				test_fail_msg("bad result after %u retries\n",
140 				    nrepeat);
141 			}
142 		} else {
143 			test_fail_vmexit(vexit);
144 		}
145 	} while (true);
146 }
147 
148 int
149 main(int argc, char *argv[])
150 {
151 	const char *test_suite_name = basename(argv[0]);
152 	struct vmctx *ctx = NULL;
153 	struct vcpu *vcpu;
154 	int err;
155 
156 	ctx = test_initialize(test_suite_name);
157 
158 	if ((vcpu = vm_vcpu_open(ctx, 0)) == NULL) {
159 		test_fail_errno(errno, "Could not open vcpu0");
160 	}
161 
162 	err = test_setup_vcpu(vcpu, MEM_LOC_PAYLOAD, MEM_LOC_STACK);
163 	if (err != 0) {
164 		test_fail_errno(err, "Could not initialize vcpu0");
165 	}
166 
167 	struct vm_entry ventry = { 0 };
168 	struct vm_exit vexit = { 0 };
169 
170 	test_for_divisor(vcpu, 2, &ventry, &vexit);
171 	test_for_divisor(vcpu, 4, &ventry, &vexit);
172 	test_for_divisor(vcpu, 16, &ventry, &vexit);
173 	test_pass();
174 	return (0);
175 }
176