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