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