xref: /illumos-gate/usr/src/test/bhyve-tests/tests/kdev/rdmsr_tsc.c (revision fc910014e8a32a65612105835a10995f2c13d942)
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 reads of the TSC via rdmsr (following a write 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 	struct vcpu *vcpu;
39 	int err;
40 
41 	ctx = test_initialize(test_suite_name);
42 
43 	if ((vcpu = vm_vcpu_open(ctx, 0)) == NULL) {
44 		test_fail_errno(errno, "Could not open vcpu0");
45 	}
46 
47 	err = test_setup_vcpu(vcpu, MEM_LOC_PAYLOAD, MEM_LOC_STACK);
48 	if (err != 0) {
49 		test_fail_errno(err, "Could not initialize vcpu0");
50 	}
51 
52 	struct vm_entry ventry = { 0 }; struct vm_exit vexit = { 0 };
53 	bool half_read = false;
54 	uint64_t tsc;
55 
56 	do {
57 		const enum vm_exit_kind kind =
58 		    test_run_vcpu(vcpu, &ventry, &vexit);
59 
60 		if (kind == VEK_REENTR) {
61 			continue;
62 		} else if (kind != VEK_UNHANDLED) {
63 			test_fail_vmexit(&vexit);
64 		}
65 
66 		uint32_t val;
67 		if (vexit_match_inout(&vexit, false, IOP_TEST_VALUE, 4,
68 		    &val)) {
69 			if (!half_read) {
70 				/* low 32-bits of TSC first */
71 				tsc = val;
72 				half_read = true;
73 				ventry_fulfill_inout(&vexit, &ventry, 0);
74 			} else {
75 				/* high 32-bits of TSC */
76 				tsc |= ((uint64_t)val << 32);
77 				printf("tsc=%lu\n", tsc);
78 
79 				/*
80 				 * Check that the TSC reading is at least the
81 				 * high value it was set to by the guest.
82 				 *
83 				 * If we wanted to be more precise about it, we
84 				 * could get the host frequency and calculate
85 				 * ppm error.
86 				 *
87 				 */
88 				if (tsc < TSC_TARGET_WRVAL) {
89 					test_fail_msg("TSC %lu < %lu", tsc,
90 					    TSC_TARGET_WRVAL);
91 				} else {
92 					test_pass();
93 				}
94 			}
95 		} else {
96 			test_fail_vmexit(&vexit);
97 		}
98 	} while (true);
99 }
100