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