xref: /illumos-gate/usr/src/test/bhyve-tests/tests/kdev/rdmsr_tsc.c (revision 717646f7112314de3f464bc0b75f034f009c861e)
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 	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 				printf("tsc=%lu\n", tsc);
73 
74 				/*
75 				 * Check that the TSC reading is at least the
76 				 * high value it was set to by the guest.
77 				 *
78 				 * If we wanted to be more precise about it, we
79 				 * could get the host frequency and calculate
80 				 * ppm error.
81 				 *
82 				 */
83 				if (tsc < TSC_TARGET_WRVAL) {
84 					test_fail_msg("TSC %lu < %lu", tsc,
85 					    TSC_TARGET_WRVAL);
86 				} else {
87 					test_pass();
88 				}
89 			}
90 		} else {
91 			test_fail_vmexit(&vexit);
92 		}
93 	} while (true);
94 }
95