xref: /illumos-gate/usr/src/test/bhyve-tests/tests/kdev/vhpet_freq.c (revision b8052df9f609edb713f6828c9eecc3d7be19dfb3)
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 
23 #include <sys/types.h>
24 #include <sys/sysmacros.h>
25 #include <sys/debug.h>
26 #include <sys/vmm.h>
27 #include <sys/vmm_dev.h>
28 #include <vmmapi.h>
29 
30 #include "in_guest.h"
31 #include "test_defs.h"
32 
33 typedef struct reading {
34 	hrtime_t	when;
35 	uint32_t	value;
36 } reading_t;
37 
38 static bool
39 check_reading(reading_t before, reading_t after, uint_t tick_margin,
40     uint_t ppm_margin)
41 {
42 	hrtime_t time_delta = after.when - before.when;
43 	uint32_t tick_delta;
44 
45 	if (after.value < before.value) {
46 		/* handle rollover */
47 		tick_delta = (UINT32_MAX - before.value) + after.value;
48 	} else {
49 		tick_delta = after.value - before.value;
50 	}
51 
52 	/* is the number of ticks OK? */
53 	if (tick_delta < HPET_TARGET_TICKS) {
54 		test_fail_msg("inadequate passage of ticks %u < %u\n",
55 		    tick_delta, HPET_TARGET_TICKS);
56 	} else if ((tick_delta - HPET_TARGET_TICKS) > tick_margin) {
57 		(void) printf("%u ticks outside margin %u\n", tick_delta,
58 		    HPET_TARGET_TICKS + tick_margin);
59 		return (false);
60 	}
61 
62 	hrtime_t time_target = (tick_delta * NANOSEC) / HPET_FREQ;
63 
64 	hrtime_t offset;
65 	if (time_delta < time_target) {
66 		offset = time_target - time_delta;
67 	} else {
68 		offset = time_delta - time_target;
69 	}
70 	uint64_t ppm = (offset * 1000000) / time_target;
71 	(void) printf("margin limits: ticks=%u ppm=%lu\n",
72 	    tick_margin, ppm_margin);
73 	(void) printf("%u ticks in %lu ns (error %lu ppm)\n",
74 	    tick_delta, time_delta, ppm);
75 	if (ppm > ppm_margin) {
76 		(void) printf("UNACCEPTABLE!\n");
77 		return (false);
78 	}
79 	return (true);
80 }
81 
82 int
83 main(int argc, char *argv[])
84 {
85 	const char *test_suite_name = basename(argv[0]);
86 	struct vmctx *ctx = NULL;
87 	int err;
88 
89 	ctx = test_initialize(test_suite_name);
90 
91 	err = test_setup_vcpu(ctx, 0, MEM_LOC_PAYLOAD, MEM_LOC_STACK);
92 	if (err != 0) {
93 		test_fail_errno(err, "Could not initialize vcpu0");
94 	}
95 
96 	struct vm_entry ventry = { 0 };
97 	struct vm_exit vexit = { 0 };
98 	reading_t readings[2];
99 	uint_t nread = 0;
100 	uint_t nrepeat = 0;
101 
102 	const uint_t margin_ticks = MAX(1, HPET_TARGET_TICKS / 10000);
103 	const uint_t margin_ppm = 400;
104 
105 	do {
106 		const enum vm_exit_kind kind =
107 		    test_run_vcpu(ctx, 0, &ventry, &vexit);
108 		if (kind == VEK_REENTR) {
109 			continue;
110 		} else if (kind != VEK_UNHANDLED) {
111 			test_fail_vmexit(&vexit);
112 		}
113 
114 		uint32_t v;
115 		if (vexit_match_inout(&vexit, false, IOP_TEST_VALUE, 4, &v)) {
116 			readings[nread].when = gethrtime();
117 			readings[nread].value = v;
118 
119 			ventry_fulfill_inout(&vexit, &ventry, 0);
120 
121 			nread++;
122 			if (nread != 2) {
123 				continue;
124 			}
125 
126 			if (check_reading(readings[0], readings[1],
127 			    margin_ticks, margin_ppm)) {
128 				test_pass();
129 			} else {
130 				nrepeat++;
131 				if (nrepeat < 3) {
132 					nread = 0;
133 					(void) printf("retry %u\n", nrepeat);
134 					continue;
135 				}
136 				test_fail_msg("bad result after %u retries\n",
137 				    nrepeat);
138 			}
139 		} else {
140 			test_fail_vmexit(&vexit);
141 		}
142 
143 	} while (true);
144 
145 	return (0);
146 }
147