xref: /illumos-gate/usr/src/uts/intel/io/vmm/x86.c (revision 7802dc577d81e5451376b767f12514ec82872c30)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2011 NetApp, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 /*
31  * This file and its contents are supplied under the terms of the
32  * Common Development and Distribution License ("CDDL"), version 1.0.
33  * You may only use this file in accordance with the terms of version
34  * 1.0 of the CDDL.
35  *
36  * A full copy of the text of the CDDL should have accompanied this
37  * source.  A copy of the CDDL is also available via the Internet at
38  * http://www.illumos.org/license/CDDL.
39  *
40  * Copyright 2014 Pluribus Networks Inc.
41  * Copyright 2018 Joyent, Inc.
42  * Copyright 2020 Oxide Computer Company
43  */
44 
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
47 
48 #include <sys/param.h>
49 #include <sys/pcpu.h>
50 #include <sys/systm.h>
51 #include <sys/sysctl.h>
52 #include <sys/x86_archext.h>
53 
54 #include <machine/clock.h>
55 #include <machine/cpufunc.h>
56 #include <machine/md_var.h>
57 #include <machine/segments.h>
58 #include <machine/specialreg.h>
59 
60 #include <machine/vmm.h>
61 #include <sys/vmm_kernel.h>
62 
63 #include "vmm_host.h"
64 #include "vmm_util.h"
65 
66 /*
67  * Return 'true' if the capability 'cap' is enabled in this virtual cpu
68  * and 'false' otherwise.
69  */
70 bool
71 vm_cpuid_capability(struct vm *vm, int vcpuid, enum vm_cpuid_capability cap)
72 {
73 	bool rv;
74 
75 	KASSERT(cap > 0 && cap < VCC_LAST, ("%s: invalid vm_cpu_capability %d",
76 	    __func__, cap));
77 
78 	/*
79 	 * Simply passthrough the capabilities of the host cpu for now.
80 	 */
81 	rv = false;
82 	switch (cap) {
83 #ifdef __FreeBSD__
84 	case VCC_NO_EXECUTE:
85 		if (amd_feature & AMDID_NX)
86 			rv = true;
87 		break;
88 	case VCC_FFXSR:
89 		if (amd_feature & AMDID_FFXSR)
90 			rv = true;
91 		break;
92 	case VCC_TCE:
93 		if (amd_feature2 & AMDID2_TCE)
94 			rv = true;
95 		break;
96 #else
97 	case VCC_NO_EXECUTE:
98 		if (is_x86_feature(x86_featureset, X86FSET_NX))
99 			rv = true;
100 		break;
101 	/* XXXJOY: No kernel detection for FFXR or TCE at present, so ignore */
102 	case VCC_FFXSR:
103 	case VCC_TCE:
104 		break;
105 #endif
106 	default:
107 		panic("%s: unknown vm_cpu_capability %d", __func__, cap);
108 	}
109 	return (rv);
110 }
111 
112 bool
113 validate_guest_xcr0(uint64_t val, uint64_t limit_mask)
114 {
115 	/* x87 feature must be enabled */
116 	if ((val & XFEATURE_ENABLED_X87) == 0) {
117 		return (false);
118 	}
119 	/* AVX cannot be enabled without SSE */
120 	if ((val & (XFEATURE_ENABLED_SSE | XFEATURE_ENABLED_AVX)) ==
121 	    XFEATURE_ENABLED_SSE) {
122 		return (false);
123 	}
124 	/* No bits should be outside what we dictate to be allowed */
125 	if ((val & ~limit_mask) != 0) {
126 		return (false);
127 	}
128 
129 	return (true);
130 }
131