xref: /illumos-gate/usr/src/test/bhyve-tests/tests/vmm/import_vlapic.c (revision 32640292339b07090f10ce34d455f98711077343)
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 #include <stdio.h>
17 #include <unistd.h>
18 #include <stdlib.h>
19 #include <fcntl.h>
20 #include <libgen.h>
21 #include <sys/stat.h>
22 #include <errno.h>
23 #include <err.h>
24 #include <assert.h>
25 #include <sys/sysmacros.h>
26 #include <stdbool.h>
27 
28 #include <sys/vmm.h>
29 #include <sys/vmm_dev.h>
30 #include <sys/vmm_data.h>
31 #include <vmmapi.h>
32 
33 #include "common.h"
34 
35 #define	APIC_ADDR_CCR	0xfee00390
36 
37 #define	TIMER_TEST_VAL	0x10000
38 
39 int
40 main(int argc, char *argv[])
41 {
42 	const char *suite_name = basename(argv[0]);
43 	struct vmctx *ctx;
44 	struct vcpu *vcpu;
45 
46 	ctx = create_test_vm(suite_name);
47 	if (ctx == NULL) {
48 		errx(EXIT_FAILURE, "could not open test VM");
49 	}
50 
51 	if ((vcpu = vm_vcpu_open(ctx, 0)) == NULL) {
52 		err(EXIT_FAILURE, "Could not open vcpu0");
53 	}
54 
55 	if (vm_activate_cpu(vcpu) != 0) {
56 		err(EXIT_FAILURE, "could not activate vcpu0");
57 	}
58 
59 	const int vmfd = vm_get_device_fd(ctx);
60 	int error;
61 
62 	/* Pause the instance before attempting to manipulate vlapic data */
63 	if (ioctl(vmfd, VM_PAUSE, 0) != 0) {
64 		err(EXIT_FAILURE, "VM_PAUSE failed");
65 	}
66 
67 	struct vdi_lapic_v1 lapic_data;
68 	struct vm_data_xfer xfer = {
69 		.vdx_vcpuid = 0,
70 		.vdx_class = VDC_LAPIC,
71 		.vdx_version = 1,
72 		.vdx_len = sizeof (lapic_data),
73 		.vdx_data = &lapic_data,
74 	};
75 
76 	/* Read the existing lapic data to get a baseline */
77 	if (ioctl(vmfd, VM_DATA_READ, &xfer) != 0) {
78 		err(EXIT_FAILURE, "VM_DATA_READ of lapic failed");
79 	}
80 
81 	/* Writing that exact same data back should be fine */
82 	if (ioctl(vmfd, VM_DATA_WRITE, &xfer) != 0) {
83 		err(EXIT_FAILURE, "VM_DATA_WRITE of lapic failed");
84 	}
85 
86 	/* Simulate ICR being loaded with a meaningful (but short) value */
87 	lapic_data.vl_lapic.vlp_icr_timer = TIMER_TEST_VAL;
88 	/*
89 	 * Pretend as if timer is scheduled to fire 100s (in the future) after
90 	 * VM boot time.  With the ICR value, this should trigger the overage
91 	 * detection and clamping.
92 	 */
93 	lapic_data.vl_timer_target = 1000000000UL * 100;
94 
95 	/* Try to write the outlandish timer result */
96 	if (ioctl(vmfd, VM_DATA_WRITE, &xfer) != 0) {
97 		err(EXIT_FAILURE, "VM_DATA_WRITE of lapic failed");
98 	}
99 
100 	/*
101 	 * The timer will not actually be scheduled (and thus observable via
102 	 * CCR) until the instance is resumed...
103 	 */
104 	if (ioctl(vmfd, VM_RESUME, 0) != 0) {
105 		err(EXIT_FAILURE, "VM_RESUME failed");
106 	}
107 
108 	/* Now simulate a read of CCR from that LAPIC */
109 	uint64_t ccr_value = 0;
110 	error = vm_readwrite_kernemu_device(vcpu, APIC_ADDR_CCR,
111 	    false, 4, &ccr_value);
112 	if (error != 0) {
113 		err(EXIT_FAILURE, "could not emulate MMIO of LAPIC CCR");
114 	}
115 	if (ccr_value != TIMER_TEST_VAL) {
116 		errx(EXIT_FAILURE, "CCR not clamped: %lx != %x",
117 		    ccr_value, TIMER_TEST_VAL);
118 	}
119 
120 	vm_destroy(ctx);
121 	(void) printf("%s\tPASS\n", suite_name);
122 	return (EXIT_SUCCESS);
123 }
124