xref: /illumos-gate/usr/src/test/bhyve-tests/tests/kdev/wrmsr_tsc.c (revision 2a347f247c40dcf93ba3b7ba10016ff63485e2e3)
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 2023 Oxide Computer Company
14  */
15 
16 /*
17  * Test guest writes to the TSC
18  */
19 
20 #include <unistd.h>
21 #include <stdlib.h>
22 #include <libgen.h>
23 #include <errno.h>
24 #include <err.h>
25 
26 #include <sys/vmm_data.h>
27 #include <sys/vmm_dev.h>
28 #include <vmmapi.h>
29 
30 #include "in_guest.h"
31 #include "test_defs.h"
32 
33 int
34 main(int argc, char *argv[])
35 {
36 	const char *test_suite_name = basename(argv[0]);
37 	struct vmctx *ctx = NULL;
38 	int err;
39 
40 	ctx = test_initialize(test_suite_name);
41 
42 	err = test_setup_vcpu(ctx, 0, MEM_LOC_PAYLOAD, MEM_LOC_STACK);
43 	if (err != 0) {
44 		test_fail_errno(err, "Could not initialize vcpu0");
45 	}
46 
47 	struct vm_entry ventry = { 0 }; struct vm_exit vexit = { 0 };
48 	bool half_read = false;
49 	uint64_t tsc;
50 
51 	do {
52 		const enum vm_exit_kind kind =
53 		    test_run_vcpu(ctx, 0, &ventry, &vexit);
54 
55 		if (kind == VEK_REENTR) {
56 			continue;
57 		} else if (kind != VEK_UNHANDLED) {
58 			test_fail_vmexit(&vexit);
59 		}
60 
61 		uint32_t val;
62 		if (vexit_match_inout(&vexit, false, IOP_TEST_VALUE, 4,
63 		    &val)) {
64 			if (!half_read) {
65 				/* low 32-bits of TSC first */
66 				tsc = val;
67 				half_read = true;
68 				ventry_fulfill_inout(&vexit, &ventry, 0);
69 			} else {
70 				/* high 32-bits of TSC */
71 				tsc |= ((uint64_t)val << 32);
72 
73 				/*
74 				 * Check that the TSC reading is at least the
75 				 * high value it was set to by the guest.
76 				 *
77 				 * If we wanted to be more precise about it, we
78 				 * could get the host frequency and calculate
79 				 * ppm error.
80 				 *
81 				 */
82 				if (tsc < TSC_TARGET_WRVAL) {
83 					test_fail_msg("TSC %lu < %lu", tsc,
84 					    TSC_TARGET_WRVAL);
85 				} else {
86 					test_pass();
87 				}
88 			}
89 		} else {
90 			test_fail_vmexit(&vexit);
91 		}
92 	} while (true);
93 }
94