summaryrefslogtreecommitdiff
path: root/usr/src/test/bhyve-tests/tests/kdev/vlapic_freq.c
blob: cf462b1acc15d33025a4967f6e7d4a9eaff748c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2022 Oxide Computer Company
 */

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <strings.h>
#include <libgen.h>
#include <assert.h>

#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/debug.h>
#include <sys/vmm.h>
#include <sys/vmm_dev.h>
#include <vmmapi.h>

#include "in_guest.h"
#include "test_defs.h"

typedef struct reading {
	hrtime_t	when;
	uint32_t	value;
} reading_t;

static bool
check_reading(reading_t before, reading_t after, uint_t divisor,
    uint_t tick_margin, uint_t ppm_margin)
{
	hrtime_t time_delta = after.when - before.when;
	uint32_t tick_delta;

	/*
	 * The ticks margin should shrink proportionally to how coarsely the
	 * timer clock is being divided.
	 */
	tick_margin /= divisor;

	/* timer is counting down, so act appropriately */
	if (after.value > before.value) {
		/* handle rollover */
		tick_delta = (UINT32_MAX - after.value) + before.value;
	} else {
		tick_delta = before.value - after.value;
	}

	/* is the number of ticks OK? */
	if (tick_delta < LAPIC_TARGET_TICKS) {
		test_fail_msg("inadequate passage of ticks %u < %u\n",
		    tick_delta, LAPIC_TARGET_TICKS);
	} else if ((tick_delta - LAPIC_TARGET_TICKS) > tick_margin) {
		(void) printf("%u ticks outside margin %u\n", tick_delta,
		    LAPIC_TARGET_TICKS + tick_margin);
		return (false);
	}

	hrtime_t time_target = (tick_delta * NANOSEC * divisor) / LAPIC_FREQ;

	hrtime_t offset;
	if (time_delta < time_target) {
		offset = time_target - time_delta;
	} else {
		offset = time_delta - time_target;
	}
	uint64_t ppm = (offset * 1000000) / time_target;
	(void) printf("params: tick_margin=%u ppm_margin=%lu divisor=%u\n",
	    tick_margin, ppm_margin, divisor);
	(void) printf("%u ticks in %lu ns (error %lu ppm)\n",
	    tick_delta, time_delta, ppm);
	if (ppm > ppm_margin) {
		(void) printf("UNACCEPTABLE!\n");
		return (false);
	}
	return (true);
}


static void
test_for_divisor(struct vmctx *ctx, uint_t divisor, struct vm_entry *ventry,
    struct vm_exit *vexit)
{
	reading_t readings[2];
	uint_t nread = 0;
	uint_t nrepeat = 0;

	const uint_t margin_ticks = MAX(1, LAPIC_TARGET_TICKS / 5000);
	const uint_t margin_ppm = 400;

	do {
		const enum vm_exit_kind kind =
		    test_run_vcpu(ctx, 0, ventry, vexit);
		if (kind == VEK_REENTR) {
			continue;
		} else if (kind != VEK_UNHANDLED) {
			test_fail_vmexit(vexit);
		}

		/* input the divisor */
		if (vexit_match_inout(vexit, true, IOP_TEST_PARAM, 4, NULL)) {
			ventry_fulfill_inout(vexit, ventry, divisor);
			continue;
		}

		uint32_t v;
		if (vexit_match_inout(vexit, false, IOP_TEST_VALUE, 4, &v)) {
			readings[nread].when = gethrtime();
			readings[nread].value = v;
			ventry_fulfill_inout(vexit, ventry, 0);

			nread++;
			if (nread != 2) {
				continue;
			}

			if (check_reading(readings[0], readings[1], divisor,
			    margin_ticks, margin_ppm)) {
				(void) printf("good result\n");
				return;
			} else {
				nrepeat++;
				if (nrepeat < 3) {
					nread = 0;
					(void) printf("retry %u\n", nrepeat);
					continue;
				}
				test_fail_msg("bad result after %u retries\n",
				    nrepeat);
			}
		} else {
			test_fail_vmexit(vexit);
		}
	} while (true);
}

int
main(int argc, char *argv[])
{
	const char *test_suite_name = basename(argv[0]);
	struct vmctx *ctx = NULL;
	int err;

	ctx = test_initialize(test_suite_name);

	err = test_setup_vcpu(ctx, 0, MEM_LOC_PAYLOAD, MEM_LOC_STACK);
	if (err != 0) {
		test_fail_errno(err, "Could not initialize vcpu0");
	}

	struct vm_entry ventry = { 0 };
	struct vm_exit vexit = { 0 };

	test_for_divisor(ctx, 2, &ventry, &vexit);
	test_for_divisor(ctx, 4, &ventry, &vexit);
	test_for_divisor(ctx, 16, &ventry, &vexit);
	test_pass();
	return (0);
}