xref: /illumos-gate/usr/src/test/bhyve-tests/tests/vmm/default_capabs.c (revision d48be21240dfd051b689384ce2b23479d757f2d8)
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 vmctx *ctx)
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(ctx, 0, 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 
67 	ctx = create_test_vm(suite_name);
68 	if (ctx == NULL) {
69 		errx(EXIT_FAILURE, "could not open test VM");
70 	}
71 
72 	/* Check the capabs on a freshly created instance */
73 	check_caps(ctx);
74 
75 	/* Force the instance through a reinit before checking them again */
76 	if (vm_reinit(ctx, 0) != 0) {
77 		err(EXIT_FAILURE, "vm_reinit failed");
78 	}
79 	check_caps(ctx);
80 
81 	vm_destroy(ctx);
82 	(void) printf("%s\tPASS\n", suite_name);
83 	return (EXIT_SUCCESS);
84 }
85