xref: /illumos-gate/usr/src/uts/intel/io/vmm/x86.c (revision 32640292)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 /*
29  * This file and its contents are supplied under the terms of the
30  * Common Development and Distribution License ("CDDL"), version 1.0.
31  * You may only use this file in accordance with the terms of version
32  * 1.0 of the CDDL.
33  *
34  * A full copy of the text of the CDDL should have accompanied this
35  * source.  A copy of the CDDL is also available via the Internet at
36  * http://www.illumos.org/license/CDDL.
37  *
38  * Copyright 2014 Pluribus Networks Inc.
39  * Copyright 2018 Joyent, Inc.
40  * Copyright 2020 Oxide Computer Company
41  */
42 
43 #include <sys/cdefs.h>
44 
45 #include <sys/param.h>
46 #include <sys/pcpu.h>
47 #include <sys/systm.h>
48 #include <sys/sysctl.h>
49 #include <sys/x86_archext.h>
50 
51 #include <machine/clock.h>
52 #include <machine/cpufunc.h>
53 #include <machine/md_var.h>
54 #include <machine/segments.h>
55 #include <machine/specialreg.h>
56 
57 #include <machine/vmm.h>
58 #include <sys/vmm_kernel.h>
59 
60 #include "vmm_host.h"
61 #include "vmm_util.h"
62 
63 /*
64  * Return 'true' if the capability 'cap' is enabled in this virtual cpu
65  * and 'false' otherwise.
66  */
67 bool
vm_cpuid_capability(struct vm * vm,int vcpuid,enum vm_cpuid_capability cap)68 vm_cpuid_capability(struct vm *vm, int vcpuid, enum vm_cpuid_capability cap)
69 {
70 	bool rv;
71 
72 	KASSERT(cap > 0 && cap < VCC_LAST, ("%s: invalid vm_cpu_capability %d",
73 	    __func__, cap));
74 
75 	/*
76 	 * Simply passthrough the capabilities of the host cpu for now.
77 	 */
78 	rv = false;
79 	switch (cap) {
80 #ifdef __FreeBSD__
81 	case VCC_NO_EXECUTE:
82 		if (amd_feature & AMDID_NX)
83 			rv = true;
84 		break;
85 	case VCC_FFXSR:
86 		if (amd_feature & AMDID_FFXSR)
87 			rv = true;
88 		break;
89 	case VCC_TCE:
90 		if (amd_feature2 & AMDID2_TCE)
91 			rv = true;
92 		break;
93 #else
94 	case VCC_NO_EXECUTE:
95 		if (is_x86_feature(x86_featureset, X86FSET_NX))
96 			rv = true;
97 		break;
98 	/* XXXJOY: No kernel detection for FFXR or TCE at present, so ignore */
99 	case VCC_FFXSR:
100 	case VCC_TCE:
101 		break;
102 #endif
103 	default:
104 		panic("%s: unknown vm_cpu_capability %d", __func__, cap);
105 	}
106 	return (rv);
107 }
108 
109 bool
validate_guest_xcr0(uint64_t val,uint64_t limit_mask)110 validate_guest_xcr0(uint64_t val, uint64_t limit_mask)
111 {
112 	/* x87 feature must be enabled */
113 	if ((val & XFEATURE_ENABLED_X87) == 0) {
114 		return (false);
115 	}
116 	/* AVX cannot be enabled without SSE */
117 	if ((val & (XFEATURE_ENABLED_SSE | XFEATURE_ENABLED_AVX)) ==
118 	    XFEATURE_ENABLED_SSE) {
119 		return (false);
120 	}
121 	/* No bits should be outside what we dictate to be allowed */
122 	if ((val & ~limit_mask) != 0) {
123 		return (false);
124 	}
125 
126 	return (true);
127 }
128