xref: /illumos-gate/usr/src/test/bhyve-tests/tests/inst_emul/triple_fault.c (revision e3ae4b35c024af1196582063ecee3ab79367227d)
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 enum vm_suspend_how expected_how = VM_SUSPEND_TRIPLEFAULT;
49 
50 	do {
51 		const enum vm_exit_kind kind =
52 		    test_run_vcpu(ctx, 0, &ventry, &vexit);
53 		switch (kind) {
54 		case VEK_REENTR:
55 			break;
56 		case VEK_UNHANDLED:
57 			/* We expect to immediately triple-fault */
58 			if (vexit.exitcode != VM_EXITCODE_SUSPENDED) {
59 				test_fail_vmexit(&vexit);
60 			}
61 			if (vexit.u.suspended.how != expected_how) {
62 				test_fail_msg("suspend_how %d != %d\n",
63 				    vexit.u.suspended.how, expected_how);
64 			}
65 			test_pass();
66 			break;
67 
68 		case VEK_TEST_PASS:
69 		case VEK_TEST_FAIL:
70 		default:
71 			test_fail_vmexit(&vexit);
72 			break;
73 		}
74 	} while (true);
75 }
76