xref: /illumos-gate/usr/src/test/bhyve-tests/tests/inst_emul/wrmsr.c (revision a4955f4fa65e38d70c07d38e657a9aff43fa155f)
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 int
33 main(int argc, char *argv[])
34 {
35 	const char *test_suite_name = basename(argv[0]);
36 	struct vmctx *ctx = NULL;
37 	int err;
38 
39 	ctx = test_initialize(test_suite_name);
40 
41 	err = test_setup_vcpu(ctx, 0, MEM_LOC_PAYLOAD, MEM_LOC_STACK);
42 	if (err != 0) {
43 		test_fail_errno(err, "Could not initialize vcpu0");
44 	}
45 
46 	struct vm_entry ventry = { 0 };
47 	struct vm_exit vexit = { 0 };
48 	const uint32_t expected_code = 0x01020304;
49 	const uint64_t expected_wval = 0x05060708090a0b0c;
50 
51 	do {
52 		const enum vm_exit_kind kind =
53 		    test_run_vcpu(ctx, 0, &ventry, &vexit);
54 		switch (kind) {
55 		case VEK_REENTR:
56 			break;
57 		case VEK_UNHANDLED:
58 			/* Look for wrmsr of test value */
59 			if (vexit.exitcode != VM_EXITCODE_WRMSR) {
60 				test_fail_vmexit(&vexit);
61 			}
62 			if (vexit.u.msr.code != expected_code) {
63 				test_fail_msg("code %08x != %08x\n",
64 				    vexit.u.msr.code, expected_code);
65 			}
66 			if (vexit.u.msr.wval != expected_wval) {
67 				test_fail_msg("wval %lx != %lx\n",
68 				    vexit.u.msr.wval, expected_wval);
69 			}
70 			test_pass();
71 			break;
72 
73 		case VEK_TEST_PASS:
74 		case VEK_TEST_FAIL:
75 		default:
76 			test_fail_vmexit(&vexit);
77 			break;
78 		}
79 	} while (true);
80 }
81