xref: /illumos-gate/usr/src/test/bhyve-tests/tests/kdev/guest_tsc_adjust.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 that adjusting the guest TSC with VMM time data interface is visible
18  * in guest.
19  *
20  * Note: requires `vmm_allow_state_writes` to be set
21  */
22 
23 #include <unistd.h>
24 #include <stdlib.h>
25 #include <libgen.h>
26 #include <errno.h>
27 #include <err.h>
28 
29 #include <sys/vmm_data.h>
30 #include <sys/vmm_dev.h>
31 #include <vmmapi.h>
32 
33 #include "in_guest.h"
34 
35 int
36 main(int argc, char *argv[])
37 {
38 	const char *test_suite_name = basename(argv[0]);
39 	struct vmctx *ctx = NULL;
40 	struct vcpu *vcpu;
41 	int err;
42 
43 	ctx = test_initialize(test_suite_name);
44 
45 	if ((vcpu = vm_vcpu_open(ctx, 0)) == NULL) {
46 		test_fail_errno(errno, "Could not open vcpu0");
47 	}
48 
49 	err = test_setup_vcpu(vcpu, MEM_LOC_PAYLOAD, MEM_LOC_STACK);
50 	if (err != 0) {
51 		test_fail_errno(err, "Could not initialize vcpu0");
52 	}
53 
54 	const int vmfd = vm_get_device_fd(ctx);
55 
56 	/* Read time data to get baseline guest time values */
57 	struct vdi_time_info_v1 time_info;
58 	struct vm_data_xfer xfer = {
59 		.vdx_class = VDC_VMM_TIME,
60 		.vdx_version = 1,
61 		.vdx_len = sizeof (struct vdi_time_info_v1),
62 		.vdx_data = &time_info,
63 	};
64 	if (ioctl(vmfd, VM_DATA_READ, &xfer) != 0) {
65 		errx(EXIT_FAILURE, "VMM_DATA_READ of time info failed");
66 	}
67 
68 	/* Change the guest TSC to a much larger value */
69 	uint64_t expect_tsc = 500000000000;
70 	time_info.vt_guest_tsc = expect_tsc;
71 	if (ioctl(vmfd, VM_DATA_WRITE, &xfer) != 0) {
72 		int error;
73 		error = errno;
74 		if (error == EPERM) {
75 			warn("VMM_DATA_WRITE got EPERM: is "
76 			    "vmm_allow_state_writes set?");
77 		}
78 		errx(EXIT_FAILURE, "VMM_DATA_WRITE of time info failed");
79 	}
80 
81 	struct vm_entry ventry = { 0 };
82 	struct vm_exit vexit = { 0 };
83 
84 	bool half_read = false;
85 	uint64_t tsc;
86 
87 	do {
88 		const enum vm_exit_kind kind =
89 		    test_run_vcpu(vcpu, &ventry, &vexit);
90 
91 		if (kind == VEK_REENTR) {
92 			continue;
93 		} else if (kind != VEK_UNHANDLED) {
94 			test_fail_vmexit(&vexit);
95 		}
96 
97 		uint32_t val;
98 		if (vexit_match_inout(&vexit, false, IOP_TEST_VALUE, 4,
99 		    &val)) {
100 			if (!half_read) {
101 				/* low 32-bits of TSC first */
102 				tsc = val;
103 				half_read = true;
104 				ventry_fulfill_inout(&vexit, &ventry, 0);
105 			} else {
106 				/* high 32-bits of TSC */
107 				tsc |= ((uint64_t)val << 32);
108 
109 				/*
110 				 * Check that the TSC reading is at least the
111 				 * absurdly high value it was set to.
112 				 */
113 				if (tsc >= expect_tsc) {
114 					(void) printf("tsc=%ld\n", tsc);
115 					test_pass();
116 				} else {
117 					test_fail_msg("TSC %lu < %lu\n", tsc,
118 					    expect_tsc);
119 				}
120 			}
121 		} else {
122 			test_fail_vmexit(&vexit);
123 		}
124 	} while (true);
125 }
126