xref: /freebsd/sys/amd64/vmm/intel/ept.c (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
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 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/types.h>
35 #include <sys/systm.h>
36 #include <sys/smp.h>
37 #include <sys/sysctl.h>
38 
39 #include <vm/vm.h>
40 #include <vm/pmap.h>
41 #include <vm/vm_extern.h>
42 
43 #include <machine/vmm.h>
44 
45 #include "vmx_cpufunc.h"
46 #include "ept.h"
47 
48 #define	EPT_SUPPORTS_EXEC_ONLY(cap)	((cap) & (1UL << 0))
49 #define	EPT_PWL4(cap)			((cap) & (1UL << 6))
50 #define	EPT_MEMORY_TYPE_WB(cap)		((cap) & (1UL << 14))
51 #define	EPT_PDE_SUPERPAGE(cap)		((cap) & (1UL << 16))	/* 2MB pages */
52 #define	EPT_PDPTE_SUPERPAGE(cap)	((cap) & (1UL << 17))	/* 1GB pages */
53 #define	INVEPT_SUPPORTED(cap)		((cap) & (1UL << 20))
54 #define	AD_BITS_SUPPORTED(cap)		((cap) & (1UL << 21))
55 #define	INVVPID_SUPPORTED(cap)		((cap) & (1UL << 32))
56 
57 #define	INVVPID_ALL_TYPES_MASK		0xF0000000000UL
58 #define	INVVPID_ALL_TYPES_SUPPORTED(cap)	\
59 	(((cap) & INVVPID_ALL_TYPES_MASK) == INVVPID_ALL_TYPES_MASK)
60 
61 #define	INVEPT_ALL_TYPES_MASK		0x6000000UL
62 #define	INVEPT_ALL_TYPES_SUPPORTED(cap)		\
63 	(((cap) & INVEPT_ALL_TYPES_MASK) == INVEPT_ALL_TYPES_MASK)
64 
65 #define	EPT_PWLEVELS		4		/* page walk levels */
66 #define	EPT_ENABLE_AD_BITS	(1 << 6)
67 
68 SYSCTL_DECL(_hw_vmm);
69 SYSCTL_NODE(_hw_vmm, OID_AUTO, ept, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
70     NULL);
71 
72 static int ept_enable_ad_bits;
73 
74 static int ept_pmap_flags;
75 SYSCTL_INT(_hw_vmm_ept, OID_AUTO, pmap_flags, CTLFLAG_RD,
76     &ept_pmap_flags, 0, NULL);
77 
78 int
79 ept_init(int ipinum)
80 {
81 	int use_hw_ad_bits, use_superpages, use_exec_only;
82 	uint64_t cap;
83 
84 	cap = rdmsr(MSR_VMX_EPT_VPID_CAP);
85 
86 	/*
87 	 * Verify that:
88 	 * - page walk length is 4 steps
89 	 * - extended page tables can be laid out in write-back memory
90 	 * - invvpid instruction with all possible types is supported
91 	 * - invept instruction with all possible types is supported
92 	 */
93 	if (!EPT_PWL4(cap) ||
94 	    !EPT_MEMORY_TYPE_WB(cap) ||
95 	    !INVVPID_SUPPORTED(cap) ||
96 	    !INVVPID_ALL_TYPES_SUPPORTED(cap) ||
97 	    !INVEPT_SUPPORTED(cap) ||
98 	    !INVEPT_ALL_TYPES_SUPPORTED(cap))
99 		return (EINVAL);
100 
101 	ept_pmap_flags = ipinum & PMAP_NESTED_IPIMASK;
102 
103 	use_superpages = 1;
104 	TUNABLE_INT_FETCH("hw.vmm.ept.use_superpages", &use_superpages);
105 	if (use_superpages && EPT_PDE_SUPERPAGE(cap))
106 		ept_pmap_flags |= PMAP_PDE_SUPERPAGE;	/* 2MB superpage */
107 
108 	use_hw_ad_bits = 1;
109 	TUNABLE_INT_FETCH("hw.vmm.ept.use_hw_ad_bits", &use_hw_ad_bits);
110 	if (use_hw_ad_bits && AD_BITS_SUPPORTED(cap))
111 		ept_enable_ad_bits = 1;
112 	else
113 		ept_pmap_flags |= PMAP_EMULATE_AD_BITS;
114 
115 	use_exec_only = 1;
116 	TUNABLE_INT_FETCH("hw.vmm.ept.use_exec_only", &use_exec_only);
117 	if (use_exec_only && EPT_SUPPORTS_EXEC_ONLY(cap))
118 		ept_pmap_flags |= PMAP_SUPPORTS_EXEC_ONLY;
119 
120 	return (0);
121 }
122 
123 #if 0
124 static void
125 ept_dump(uint64_t *ptp, int nlevels)
126 {
127 	int i, t, tabs;
128 	uint64_t *ptpnext, ptpval;
129 
130 	if (--nlevels < 0)
131 		return;
132 
133 	tabs = 3 - nlevels;
134 	for (t = 0; t < tabs; t++)
135 		printf("\t");
136 	printf("PTP = %p\n", ptp);
137 
138 	for (i = 0; i < 512; i++) {
139 		ptpval = ptp[i];
140 
141 		if (ptpval == 0)
142 			continue;
143 
144 		for (t = 0; t < tabs; t++)
145 			printf("\t");
146 		printf("%3d 0x%016lx\n", i, ptpval);
147 
148 		if (nlevels != 0 && (ptpval & EPT_PG_SUPERPAGE) == 0) {
149 			ptpnext = (uint64_t *)
150 				  PHYS_TO_DMAP(ptpval & EPT_ADDR_MASK);
151 			ept_dump(ptpnext, nlevels);
152 		}
153 	}
154 }
155 #endif
156 
157 static void
158 invept_single_context(void *arg)
159 {
160 	struct invept_desc desc = *(struct invept_desc *)arg;
161 
162 	invept(INVEPT_TYPE_SINGLE_CONTEXT, desc);
163 }
164 
165 void
166 ept_invalidate_mappings(u_long eptp)
167 {
168 	struct invept_desc invept_desc = { 0 };
169 
170 	invept_desc.eptp = eptp;
171 
172 	smp_rendezvous(NULL, invept_single_context, NULL, &invept_desc);
173 }
174 
175 static int
176 ept_pinit(pmap_t pmap)
177 {
178 
179 	return (pmap_pinit_type(pmap, PT_EPT, ept_pmap_flags));
180 }
181 
182 struct vmspace *
183 ept_vmspace_alloc(vm_offset_t min, vm_offset_t max)
184 {
185 
186 	return (vmspace_alloc(min, max, ept_pinit));
187 }
188 
189 void
190 ept_vmspace_free(struct vmspace *vmspace)
191 {
192 
193 	vmspace_free(vmspace);
194 }
195 
196 uint64_t
197 eptp(uint64_t pml4)
198 {
199 	uint64_t eptp_val;
200 
201 	eptp_val = pml4 | (EPT_PWLEVELS - 1) << 3 | PAT_WRITE_BACK;
202 	if (ept_enable_ad_bits)
203 		eptp_val |= EPT_ENABLE_AD_BITS;
204 
205 	return (eptp_val);
206 }
207