xref: /illumos-gate/usr/src/test/bhyve-tests/tests/inst_emul/exit_paging.c (revision e00bdde3c6d406f40f53f3025defadc22f7ec31a)
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/mman.h>
28 #include <sys/vmm.h>
29 #include <sys/vmm_dev.h>
30 #include <vmmapi.h>
31 
32 #include "in_guest.h"
33 
34 int
35 main(int argc, char *argv[])
36 {
37 	const char *test_suite_name = basename(argv[0]);
38 	struct vmctx *ctx = NULL;
39 	struct vcpu *vcpu;
40 	int err;
41 
42 	ctx = test_initialize(test_suite_name);
43 
44 	if ((vcpu = vm_vcpu_open(ctx, 0)) == NULL) {
45 		test_fail_errno(errno, "Could not open vcpu0");
46 	}
47 
48 	err = test_setup_vcpu(vcpu, MEM_LOC_PAYLOAD, MEM_LOC_STACK);
49 	if (err != 0) {
50 		test_fail_errno(err, "Could not initialize vcpu0");
51 	}
52 
53 	struct vm_entry ventry = { 0 };
54 	struct vm_exit vexit = { 0 };
55 
56 	const uintptr_t expected_gpa = MEM_LOC_ROM;
57 	const int expected_ftype = PROT_WRITE;
58 
59 	do {
60 		const enum vm_exit_kind kind =
61 		    test_run_vcpu(vcpu, &ventry, &vexit);
62 		switch (kind) {
63 		case VEK_REENTR:
64 			break;
65 		case VEK_UNHANDLED:
66 			if (vexit.exitcode != VM_EXITCODE_PAGING) {
67 				test_fail_vmexit(&vexit);
68 			}
69 			if (vexit.u.paging.gpa != expected_gpa) {
70 				test_fail_msg("gpa %08x != %08x\n",
71 				    vexit.u.paging.gpa, expected_gpa);
72 			}
73 			if (vexit.u.paging.fault_type != expected_ftype) {
74 				test_fail_msg("fault_type %x != %x\n",
75 				    vexit.u.paging.fault_type, expected_ftype);
76 			}
77 			test_pass();
78 			break;
79 
80 		default:
81 			test_fail_vmexit(&vexit);
82 			break;
83 		}
84 	} while (true);
85 }
86