summaryrefslogtreecommitdiff
path: root/usr/src/test/bhyve-tests/tests/vmm/cpuid_ioctl.c
blob: 21da30937259f637b3d65cd554c1c180ab9658ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2022 Oxide Computer Company
 */

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <libgen.h>
#include <err.h>
#include <errno.h>
#include <strings.h>

#include <sys/vmm.h>
#include <sys/vmm_dev.h>
#include <vmmapi.h>

#include "common.h"

int
main(int argc, char *argv[])
{
	const char *suite_name = basename(argv[0]);
	struct vmctx *ctx;

	ctx = create_test_vm(suite_name);
	if (ctx == NULL) {
		perror("could open test VM");
		return (EXIT_FAILURE);
	}
	int vmfd = vm_get_device_fd(ctx);

	struct vm_vcpu_cpuid_config cfg = { 0 };
	struct vcpu_cpuid_entry *entries = NULL;

	if (ioctl(vmfd, VM_GET_CPUID, &cfg) != 0) {
		err(EXIT_FAILURE, "ioctl(VM_GET_CPUID) failed");
	}
	if (cfg.vvcc_flags != VCC_FLAG_LEGACY_HANDLING) {
		errx(EXIT_FAILURE,
		    "cpuid handling did not default to legacy-style");
	}

	cfg.vvcc_flags = ~VCC_FLAG_LEGACY_HANDLING;
	if (ioctl(vmfd, VM_SET_CPUID, &cfg) == 0) {
		errx(EXIT_FAILURE,
		    "ioctl(VM_SET_CPUID) did not reject invalid flags");
	}

	entries = calloc(VMM_MAX_CPUID_ENTRIES + 1,
	    sizeof (struct vcpu_cpuid_entry));
	if (entries == NULL) {
		errx(EXIT_FAILURE, "could not allocate cpuid entries");
	}

	cfg.vvcc_flags = VCC_FLAG_LEGACY_HANDLING;
	cfg.vvcc_nent = 1;
	cfg.vvcc_entries = entries;
	if (ioctl(vmfd, VM_SET_CPUID, &cfg) == 0) {
		errx(EXIT_FAILURE,
		    "ioctl(VM_SET_CPUID) did not reject entries when "
		    "legacy-style handling was requested");
	}

	cfg.vvcc_flags = 0;
	cfg.vvcc_nent = VMM_MAX_CPUID_ENTRIES + 1;
	if (ioctl(vmfd, VM_SET_CPUID, &cfg) == 0) {
		errx(EXIT_FAILURE,
		    "ioctl(VM_SET_CPUID) did not reject excessive entry count");
	}

	cfg.vvcc_nent = 1;
	entries[0].vce_flags = ~0;
	if (ioctl(vmfd, VM_SET_CPUID, &cfg) == 0) {
		errx(EXIT_FAILURE,
		    "ioctl(VM_SET_CPUID) did not invalid entry flags");
	}
	entries[0].vce_flags = 0;

	/* Actually set some entries to use for GET_CPUID testing */
	const uint_t valid_entries = (VMM_MAX_CPUID_ENTRIES / 2);
	for (uint_t i = 0; i < valid_entries; i++) {
		entries[i].vce_function = i;
	}
	cfg.vvcc_nent = valid_entries;
	if (ioctl(vmfd, VM_SET_CPUID, &cfg) != 0) {
		err(EXIT_FAILURE,
		    "ioctl(VM_SET_CPUID) unable to set valid entries");
	}

	/* Try with no entries buffer */
	bzero(&cfg, sizeof (cfg));
	if (ioctl(vmfd, VM_GET_CPUID, &cfg) == 0 || errno != E2BIG) {
		errx(EXIT_FAILURE,
		    "ioctl(VM_GET_CPUID) did not fail absent buffer");
	}
	if (cfg.vvcc_nent != valid_entries) {
		errx(EXIT_FAILURE,
		    "ioctl(VM_GET_CPUID) did not emit entry count "
		    "(expected %u, got %u)", valid_entries, cfg.vvcc_nent);
	}

	/* Try with too-small entries buffer */
	cfg.vvcc_nent = 1;
	cfg.vvcc_entries = entries;
	bzero(entries, valid_entries * sizeof (struct vcpu_cpuid_entry));
	if (ioctl(vmfd, VM_GET_CPUID, &cfg) == 0 || errno != E2BIG) {
		errx(EXIT_FAILURE,
		    "ioctl(VM_GET_CPUID) did not fail too-small buffer");
	}
	if (cfg.vvcc_nent != valid_entries) {
		errx(EXIT_FAILURE,
		    "ioctl(VM_GET_CPUID) did not emit entry count "
		    "(expected %u, got %u)", valid_entries, cfg.vvcc_nent);
	}

	/* Try with adequate entries buffer */
	cfg.vvcc_nent = valid_entries;
	if (ioctl(vmfd, VM_GET_CPUID, &cfg) != 0) {
		err(EXIT_FAILURE, "ioctl(VM_GET_CPUID) failed");
	}
	if (cfg.vvcc_nent != valid_entries) {
		errx(EXIT_FAILURE,
		    "ioctl(VM_GET_CPUID) did not emit entry count "
		    "(expected %u, got %u)", valid_entries, cfg.vvcc_nent);
	}
	for (uint_t i = 0; i < valid_entries; i++) {
		if (entries[i].vce_function != i) {
			errx(EXIT_FAILURE, "unexpected entry contents");
		}
	}

	/*
	 * The legacy handling is simply using the host values with certain
	 * modifications (masking, etc) applied.  The base leaf should be
	 * exactly the same as we read from the host.
	 *
	 * Since a bhyve compat header has an inline-asm cpuid wrapper, use that
	 * for now for querying the host
	 */
	struct vm_legacy_cpuid legacy  = { 0 };
	if (ioctl(vmfd, VM_LEGACY_CPUID, &legacy) != 0) {
		err(EXIT_FAILURE, "ioctl(VM_CPUID_LEGACY) failed");
	}

	uint32_t basic_cpuid[4];
	cpuid_count(0, 0, basic_cpuid);
	if (basic_cpuid[0] != legacy.vlc_eax ||
	    basic_cpuid[1] != legacy.vlc_ebx ||
	    basic_cpuid[2] != legacy.vlc_ecx ||
	    basic_cpuid[3] != legacy.vlc_edx) {
		errx(EXIT_FAILURE, "legacy cpuid mismatch");
	}

	vm_destroy(ctx);
	(void) printf("%s\tPASS\n", suite_name);
	return (EXIT_SUCCESS);
}