xref: /illumos-gate/usr/src/test/bhyve-tests/tests/kdev/vatpit_freq.c (revision c093b3ec6d35e1fe023174ed7f6ca6b90690d526)
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 	uint16_t	value;
37 } reading_t;
38 
39 static bool
40 check_reading(reading_t before, reading_t after, uint_t tick_margin,
41     uint_t ppm_margin)
42 {
43 	hrtime_t time_delta = after.when - before.when;
44 	uint16_t tick_delta;
45 
46 	tick_delta = before.value - after.value;
47 
48 	/* is the number of ticks OK? */
49 	if (tick_delta < ATPIT_TARGET_TICKS) {
50 		test_fail_msg("inadequate passage of ticks %u < %u\n",
51 		    tick_delta, ATPIT_TARGET_TICKS);
52 	} else if ((tick_delta - ATPIT_TARGET_TICKS) > tick_margin) {
53 		(void) printf("%u ticks outside margin %u\n", tick_delta,
54 		    ATPIT_TARGET_TICKS + tick_margin);
55 		return (false);
56 	}
57 
58 	hrtime_t time_target = (tick_delta * NANOSEC) / ATPIT_FREQ;
59 
60 	hrtime_t offset;
61 	if (time_delta < time_target) {
62 		offset = time_target - time_delta;
63 	} else {
64 		offset = time_delta - time_target;
65 	}
66 	uint64_t ppm = (offset * 1000000) / time_target;
67 	(void) printf("margin limits: ticks=%u ppm=%lu\n",
68 	    tick_margin, ppm_margin);
69 	(void) printf("%u ticks in %lu ns (error %lu ppm)\n",
70 	    tick_delta, time_delta, ppm);
71 	if (ppm > ppm_margin) {
72 		(void) printf("UNACCEPTABLE!\n");
73 		return (false);
74 	}
75 	return (true);
76 }
77 
78 int
79 main(int argc, char *argv[])
80 {
81 	const char *test_suite_name = basename(argv[0]);
82 	struct vmctx *ctx = NULL;
83 	struct vcpu *vcpu;
84 	int err;
85 
86 	ctx = test_initialize(test_suite_name);
87 
88 	if ((vcpu = vm_vcpu_open(ctx, 0)) == NULL) {
89 		test_fail_errno(errno, "Could not open vcpu0");
90 	}
91 
92 	err = test_setup_vcpu(vcpu, MEM_LOC_PAYLOAD, MEM_LOC_STACK);
93 	if (err != 0) {
94 		test_fail_errno(err, "Could not initialize vcpu0");
95 	}
96 
97 	struct vm_entry ventry = { 0 };
98 	struct vm_exit vexit = { 0 };
99 	reading_t readings[2];
100 	uint_t nread = 0;
101 	uint_t nrepeat = 0;
102 
103 	/*
104 	 * Since the PIT is slower to read back (requiring 3 emulated reads),
105 	 * operate with a more loose ticks margin.
106 	 */
107 	const uint_t margin_ticks = MAX(1, ATPIT_TARGET_TICKS / 2500);
108 	const uint_t margin_ppm = 400;
109 
110 	do {
111 		const enum vm_exit_kind kind =
112 		    test_run_vcpu(vcpu, &ventry, &vexit);
113 		if (kind == VEK_REENTR) {
114 			continue;
115 		} else if (kind != VEK_UNHANDLED) {
116 			test_fail_vmexit(&vexit);
117 		}
118 
119 		uint32_t v;
120 		if (vexit_match_inout(&vexit, false, IOP_TEST_VALUE, 2, &v)) {
121 			readings[nread].when = gethrtime();
122 			readings[nread].value = v;
123 
124 			ventry_fulfill_inout(&vexit, &ventry, 0);
125 
126 			nread++;
127 			if (nread != 2) {
128 				continue;
129 			}
130 
131 			if (check_reading(readings[0], readings[1],
132 			    margin_ticks, margin_ppm)) {
133 				test_pass();
134 			} else {
135 				nrepeat++;
136 				if (nrepeat < 3) {
137 					nread = 0;
138 					(void) printf("retry %u\n", nrepeat);
139 					continue;
140 				}
141 				test_fail_msg("bad result after %u retries\n",
142 				    nrepeat);
143 			}
144 		} else {
145 			test_fail_vmexit(&vexit);
146 		}
147 
148 	} while (true);
149 
150 	return (0);
151 }
152