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 
45 	ctx = create_test_vm(suite_name);
46 	if (ctx == NULL) {
47 		errx(EXIT_FAILURE, "could not open test VM");
48 	}
49 
50 	if (vm_activate_cpu(ctx, 0) != 0) {
51 		err(EXIT_FAILURE, "could not activate vcpu0");
52 	}
53 
54 	const int vmfd = vm_get_device_fd(ctx);
55 	int error;
56 
57 	/* Pause the instance before attempting to manipulate vlapic data */
58 	if (ioctl(vmfd, VM_PAUSE, 0) != 0) {
59 		err(EXIT_FAILURE, "VM_PAUSE failed");
60 	}
61 
62 	struct vdi_lapic_v1 lapic_data;
63 	struct vm_data_xfer xfer = {
64 		.vdx_vcpuid = 0,
65 		.vdx_class = VDC_LAPIC,
66 		.vdx_version = 1,
67 		.vdx_len = sizeof (lapic_data),
68 		.vdx_data = &lapic_data,
69 	};
70 
71 	/* Read the existing lapic data to get a baseline */
72 	if (ioctl(vmfd, VM_DATA_READ, &xfer) != 0) {
73 		err(EXIT_FAILURE, "VM_DATA_READ of lapic failed");
74 	}
75 
76 	/* Writing that exact same data back should be fine */
77 	if (ioctl(vmfd, VM_DATA_WRITE, &xfer) != 0) {
78 		err(EXIT_FAILURE, "VM_DATA_WRITE of lapic failed");
79 	}
80 
81 	/* Simulate ICR being loaded with a meaningful (but short) value */
82 	lapic_data.vl_lapic.vlp_icr_timer = TIMER_TEST_VAL;
83 	/*
84 	 * Pretend as if timer is scheduled to fire 100s (in the future) after
85 	 * VM boot time.  With the ICR value, this should trigger the overage
86 	 * detection and clamping.
87 	 */
88 	lapic_data.vl_timer_target = 1000000000UL * 100;
89 
90 	/* Try to write the outlandish timer result */
91 	if (ioctl(vmfd, VM_DATA_WRITE, &xfer) != 0) {
92 		err(EXIT_FAILURE, "VM_DATA_WRITE of lapic failed");
93 	}
94 
95 	/*
96 	 * The timer will not actually be scheduled (and thus observable via
97 	 * CCR) until the instance is resumed...
98 	 */
99 	if (ioctl(vmfd, VM_RESUME, 0) != 0) {
100 		err(EXIT_FAILURE, "VM_RESUME failed");
101 	}
102 
103 	/* Now simulate a read of CCR from that LAPIC */
104 	uint64_t ccr_value = 0;
105 	error = vm_readwrite_kernemu_device(ctx, 0, APIC_ADDR_CCR,
106 	    false, 4, &ccr_value);
107 	if (error != 0) {
108 		err(EXIT_FAILURE, "could not emulate MMIO of LAPIC CCR");
109 	}
110 	if (ccr_value != TIMER_TEST_VAL) {
111 		errx(EXIT_FAILURE, "CCR not clamped: %lx != %lx",
112 		    ccr_value, TIMER_TEST_VAL);
113 	}
114 
115 	vm_destroy(ctx);
116 	(void) printf("%s\tPASS\n", suite_name);
117 	return (EXIT_SUCCESS);
118 }
119