xref: /illumos-gate/usr/src/test/bhyve-tests/tests/kdev/vrtc_ops.c (revision 4c2bdae20e15dfc656ce2c87808008f4da4fc3f0)
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
38 sigalrm_handler(int sig)
39 {
40 	timed_out = true;
41 }
42 
43 static void
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
58 main(int argc, char *argv[])
59 {
60 	const char *test_suite_name = basename(argv[0]);
61 	struct vmctx *ctx = NULL;
62 	int err;
63 
64 	ctx = test_initialize(test_suite_name);
65 
66 	err = test_setup_vcpu(ctx, 0, MEM_LOC_PAYLOAD, MEM_LOC_STACK);
67 	if (err != 0) {
68 		test_fail_errno(err, "Could not initialize vcpu0");
69 	}
70 
71 	timespec_t ts = { 0 };
72 	err = vm_rtc_settime(ctx, &ts);
73 	if (err != 0) {
74 		test_fail_errno(err, "Could zero out RTC time");
75 	}
76 
77 	/* A successful payload should be wrapped up well before 8 seconds */
78 	configure_timeout(8);
79 
80 	struct vm_entry ventry = { 0 };
81 	struct vm_exit vexit = { 0 };
82 
83 	do {
84 		const enum vm_exit_kind kind =
85 		    test_run_vcpu(ctx, 0, &ventry, &vexit);
86 
87 		if (timed_out) {
88 			test_fail_msg("test timed out\n");
89 		}
90 
91 		switch (kind) {
92 		case VEK_REENTR:
93 			break;
94 		case VEK_TEST_PASS:
95 			test_pass();
96 			break;
97 		case VEK_TEST_FAIL:
98 			test_fail();
99 			break;
100 		case VEK_TEST_MSG:
101 			test_msg_print(ctx);
102 			break;
103 		default:
104 			test_fail_vmexit(&vexit);
105 			break;
106 		}
107 	} while (true);
108 }
109