xref: /illumos-gate/usr/src/test/bhyve-tests/tests/inst_emul/imul.c (revision 1fde93bfc5537d4261d6ef3ac9db44a76d697e75)
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 
32 #define	MMIO_TEST_BASE	0x10001000
33 #define	MMIO_TEST_END	0x10002000
34 
35 static bool
36 handle_test_mmio(const struct vm_exit *vexit, struct vm_entry *ventry)
37 {
38 	/* expecting only reads */
39 	if (vexit->u.mmio.read == 0) {
40 		return (false);
41 	}
42 
43 	/* expecting only in the [0x10001000 - 0x10002000) range */
44 	if (vexit->u.mmio.gpa < MMIO_TEST_BASE ||
45 	    vexit->u.mmio.gpa >= MMIO_TEST_END) {
46 		return (false);
47 	}
48 
49 	/*
50 	 * Emit a pattern of the lowest 16 bits of the address, ascending by the
51 	 * 2-byte stride for every 2 additional bytes, as the result.
52 	 *
53 	 * For example, an 8-byte read of 0x00001234 would result in:
54 	 * 0x123a123812361234 being returned
55 	 */
56 	const uint16_t addr = vexit->u.mmio.gpa;
57 	uint64_t val = 0;
58 	switch (vexit->u.mmio.bytes) {
59 	case 8:
60 		val |= (uint64_t)(addr + 6) << 48;
61 		val |= (uint64_t)(addr + 4) << 32;
62 		/* FALLTHROUGH */
63 	case 4:
64 		val |= (uint32_t)(addr + 2) << 16;
65 		/* FALLTHROUGH */
66 	case 2:
67 		val |= addr;
68 		break;
69 	default:
70 		/* expect only 2/4/8-byte reads */
71 		return (false);
72 	}
73 
74 	ventry_fulfill_mmio(vexit, ventry, val);
75 	return (true);
76 }
77 
78 int
79 main(int argc, char *argv[])
80 {
81 	const char *test_suite_name = basename(argv[0]);
82 	struct vmctx *ctx = NULL;
83 	int err;
84 
85 	ctx = test_initialize(test_suite_name);
86 
87 	err = test_setup_vcpu(ctx, 0, MEM_LOC_PAYLOAD, MEM_LOC_STACK);
88 	if (err != 0) {
89 		test_fail_errno(err, "Could not initialize vcpu0");
90 	}
91 
92 	struct vm_entry ventry = { 0 };
93 	struct vm_exit vexit = { 0 };
94 
95 	do {
96 		const enum vm_exit_kind kind =
97 		    test_run_vcpu(ctx, 0, &ventry, &vexit);
98 		switch (kind) {
99 		case VEK_REENTR:
100 			break;
101 		case VEK_UNHANDLED:
102 			if (!handle_test_mmio(&vexit, &ventry)) {
103 				test_fail_vmexit(&vexit);
104 			}
105 			break;
106 		case VEK_TEST_PASS:
107 			test_pass();
108 			break;
109 		case VEK_TEST_FAIL:
110 		default:
111 			test_fail_vmexit(&vexit);
112 			break;
113 		}
114 	} while (true);
115 }
116