xref: /illumos-gate/usr/src/test/bhyve-tests/tests/vmm/default_capabs.c (revision c093b3ec6d35e1fe023174ed7f6ca6b90690d526)
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 <fcntl.h>
20 #include <libgen.h>
21 #include <sys/stat.h>
22 #include <errno.h>
23 #include <err.h>
24 #include <assert.h>
25 #include <sys/sysmacros.h>
26 #include <stdbool.h>
27 
28 #include <sys/vmm.h>
29 #include <sys/vmm_dev.h>
30 #include <vmmapi.h>
31 
32 #include "common.h"
33 
34 
35 static void
36 check_caps(struct vcpu *vcpu)
37 {
38 	struct capcheck {
39 		enum vm_cap_type cap;
40 		bool enabled;
41 	} checks[] = {
42 		{ .cap = VM_CAP_HALT_EXIT, .enabled = true, },
43 		{ .cap = VM_CAP_PAUSE_EXIT, .enabled = false, }
44 	};
45 
46 	for (uint_t i = 0; i < ARRAY_SIZE(checks); i++) {
47 		const char *capname = vm_capability_type2name(checks[i].cap);
48 
49 		int val;
50 		if (vm_get_capability(vcpu, checks[i].cap, &val) != 0) {
51 			err(EXIT_FAILURE, "could not query %s", capname);
52 		}
53 		const bool actual = (val != 0);
54 		if (actual != checks[i].enabled) {
55 			errx(EXIT_FAILURE, "cap %s unexpected state %d != %d",
56 			    capname, actual, checks[i].enabled);
57 		}
58 	}
59 }
60 
61 int
62 main(int argc, char *argv[])
63 {
64 	const char *suite_name = basename(argv[0]);
65 	struct vmctx *ctx;
66 	struct vcpu *vcpu;
67 
68 	ctx = create_test_vm(suite_name);
69 	if (ctx == NULL) {
70 		errx(EXIT_FAILURE, "could not open test VM");
71 	}
72 
73 	if ((vcpu = vm_vcpu_open(ctx, 0)) == NULL) {
74 		err(EXIT_FAILURE, "Could not open vcpu0");
75 	}
76 
77 	/* Check the capabs on a freshly created instance */
78 	check_caps(vcpu);
79 
80 	/* Force the instance through a reinit before checking them again */
81 	if (vm_reinit(ctx, 0) != 0) {
82 		err(EXIT_FAILURE, "vm_reinit failed");
83 	}
84 	check_caps(vcpu);
85 
86 	vm_vcpu_close(vcpu);
87 	vm_destroy(ctx);
88 	(void) printf("%s\tPASS\n", suite_name);
89 	return (EXIT_SUCCESS);
90 }
91