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 2023 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 <signal.h>
23 #include <errno.h>
24 
25 #include <sys/types.h>
26 #include <sys/sysmacros.h>
27 #include <sys/debug.h>
28 #include <sys/vmm.h>
29 #include <sys/vmm_dev.h>
30 #include <vmmapi.h>
31 
32 #include "in_guest.h"
33 #include "test_defs.h"
34 
35 static bool timed_out = false;
36 
37 static void
sigalrm_handler(int sig)38 sigalrm_handler(int sig)
39 {
40 	timed_out = true;
41 }
42 
43 static void
configure_timeout(uint_t seconds)44 configure_timeout(uint_t seconds)
45 {
46 	struct sigaction sa = {
47 		.sa_handler = sigalrm_handler,
48 	};
49 	struct sigaction old_sa;
50 	if (sigaction(SIGALRM, &sa, &old_sa) != 0) {
51 		test_fail_errno(errno,
52 		    "could not prep signal handling for bad access");
53 	}
54 	(void) alarm(seconds);
55 }
56 
57 int
main(int argc,char * argv[])58 main(int argc, char *argv[])
59 {
60 	const char *test_suite_name = basename(argv[0]);
61 	struct vmctx *ctx = NULL;
62 	struct vcpu *vcpu;
63 	int err;
64 
65 	ctx = test_initialize(test_suite_name);
66 
67 	if ((vcpu = vm_vcpu_open(ctx, 0)) == NULL) {
68 		test_fail_errno(errno, "Could not open vcpu0");
69 	}
70 
71 	err = test_setup_vcpu(vcpu, MEM_LOC_PAYLOAD, MEM_LOC_STACK);
72 	if (err != 0) {
73 		test_fail_errno(err, "Could not initialize vcpu0");
74 	}
75 
76 	timespec_t ts = { 0 };
77 	err = vm_rtc_settime(ctx, &ts);
78 	if (err != 0) {
79 		test_fail_errno(err, "Could zero out RTC time");
80 	}
81 
82 	/* A successful payload should be wrapped up well before 8 seconds */
83 	configure_timeout(8);
84 
85 	struct vm_entry ventry = { 0 };
86 	struct vm_exit vexit = { 0 };
87 
88 	do {
89 		const enum vm_exit_kind kind =
90 		    test_run_vcpu(vcpu, &ventry, &vexit);
91 
92 		if (timed_out) {
93 			test_fail_msg("test timed out\n");
94 		}
95 
96 		switch (kind) {
97 		case VEK_REENTR:
98 			break;
99 		case VEK_TEST_PASS:
100 			test_pass();
101 			break;
102 		case VEK_TEST_FAIL:
103 			test_fail();
104 			break;
105 		case VEK_TEST_MSG:
106 			test_msg_print(ctx);
107 			break;
108 		default:
109 			test_fail_vmexit(&vexit);
110 			break;
111 		}
112 	} while (true);
113 }
114