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 2022 Oxide Computer Company
14  */
15 
16 #include <stdio.h>
17 #include <unistd.h>
18 #include <stdlib.h>
19 #include <strings.h>
20 #include <libgen.h>
21 #include <assert.h>
22 #include <errno.h>
23 
24 #include <sys/types.h>
25 #include <sys/sysmacros.h>
26 #include <sys/debug.h>
27 #include <sys/vmm.h>
28 #include <sys/vmm_dev.h>
29 #include <vmmapi.h>
30 
31 #include "in_guest.h"
32 #include "test_defs.h"
33 
34 const char *strict_name = "STRICT_APICV";
35 
36 static bool
strict_apicv(void)37 strict_apicv(void)
38 {
39 	const char *strict_val;
40 
41 	if ((strict_val = getenv(strict_name)) != NULL) {
42 		if (strlen(strict_val) != 0 &&
43 		    strcmp(strict_val, "0") != 0) {
44 			return (true);
45 		}
46 	}
47 	return (false);
48 }
49 
50 int
main(int argc,char * argv[])51 main(int argc, char *argv[])
52 {
53 	const char *test_suite_name = basename(argv[0]);
54 	struct vmctx *ctx = NULL;
55 	struct vcpu *vcpu;
56 	int err;
57 
58 	ctx = test_initialize(test_suite_name);
59 
60 	if ((vcpu = vm_vcpu_open(ctx, 0)) == NULL) {
61 		test_fail_errno(errno, "Could not open vcpu0");
62 	}
63 
64 	err = test_setup_vcpu(vcpu, MEM_LOC_PAYLOAD, MEM_LOC_STACK);
65 	if (err != 0) {
66 		test_fail_errno(err, "Could not initialize vcpu0");
67 	}
68 
69 	/*
70 	 * Although x2APIC should be off by default, make doubly sure by
71 	 * explicitly setting it so.
72 	 */
73 	err = vm_set_x2apic_state(vcpu, X2APIC_DISABLED);
74 	if (err != 0) {
75 		test_fail_errno(err, "Could not disable x2apic on vcpu0");
76 	}
77 
78 	struct vm_entry ventry = { 0 };
79 	struct vm_exit vexit = { 0 };
80 
81 	do {
82 		const enum vm_exit_kind kind =
83 		    test_run_vcpu(vcpu, &ventry, &vexit);
84 		switch (kind) {
85 		case VEK_REENTR:
86 			break;
87 		case VEK_TEST_PASS:
88 			test_pass();
89 			break;
90 		case VEK_TEST_FAIL:
91 			test_fail_msg("payload signaled failure");
92 			break;
93 		case VEK_UNHANDLED:
94 			/*
95 			 * Not all APICv-accelerated accesses are properly
96 			 * handled by the in-kernel emulation today.
97 			 * (See: illumos #13847).
98 			 *
99 			 * To allow this test to be useful on systems without
100 			 * APICv, we suppress such failures unless explicitly
101 			 * strict handling is requested.
102 			 */
103 			if (vexit.exitcode == VM_EXITCODE_VMX &&
104 			    (vexit.u.vmx.exit_reason == 44 ||
105 			    vexit.u.vmx.exit_reason == 56)) {
106 				if (strict_apicv()) {
107 					test_fail_vmexit(&vexit);
108 				}
109 				(void) fprintf(stderr,
110 				    "Ignoring APICv access issue\n"
111 				    "If strictness is desired, "
112 				    "run with %s=1 in env\n", strict_name);
113 				test_pass();
114 			}
115 			test_fail_vmexit(&vexit);
116 			break;
117 		default:
118 			test_fail_vmexit(&vexit);
119 			break;
120 		}
121 	} while (true);
122 }
123