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