1 /*-
2 * Copyright (c) 2011 NetApp, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: releng/10.1/sys/amd64/vmm/intel/ept.c 266339 2014-05-17 19:11:08Z jhb $
27 */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD: releng/10.1/sys/amd64/vmm/intel/ept.c 266339 2014-05-17 19:11:08Z jhb $");
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 "vmm_ipi.h"
47 #include "vmx_msr.h"
48 #include "ept.h"
49
50 #define EPT_SUPPORTS_EXEC_ONLY(cap) ((cap) & (1UL << 0))
51 #define EPT_PWL4(cap) ((cap) & (1UL << 6))
52 #define EPT_MEMORY_TYPE_WB(cap) ((cap) & (1UL << 14))
53 #define EPT_PDE_SUPERPAGE(cap) ((cap) & (1UL << 16)) /* 2MB pages */
54 #define EPT_PDPTE_SUPERPAGE(cap) ((cap) & (1UL << 17)) /* 1GB pages */
55 #define INVEPT_SUPPORTED(cap) ((cap) & (1UL << 20))
56 #define AD_BITS_SUPPORTED(cap) ((cap) & (1UL << 21))
57 #define INVVPID_SUPPORTED(cap) ((cap) & (1UL << 32))
58
59 #define INVVPID_ALL_TYPES_MASK 0xF0000000000UL
60 #define INVVPID_ALL_TYPES_SUPPORTED(cap) \
61 (((cap) & INVVPID_ALL_TYPES_MASK) == INVVPID_ALL_TYPES_MASK)
62
63 #define INVEPT_ALL_TYPES_MASK 0x6000000UL
64 #define INVEPT_ALL_TYPES_SUPPORTED(cap) \
65 (((cap) & INVEPT_ALL_TYPES_MASK) == INVEPT_ALL_TYPES_MASK)
66
67 #define EPT_PWLEVELS 4 /* page walk levels */
68 #define EPT_ENABLE_AD_BITS (1 << 6)
69
70 SYSCTL_DECL(_hw_vmm);
71 SYSCTL_NODE(_hw_vmm, OID_AUTO, ept, CTLFLAG_RW, NULL, NULL);
72
73 static int ept_enable_ad_bits;
74
75 static int ept_pmap_flags;
76 SYSCTL_INT(_hw_vmm_ept, OID_AUTO, pmap_flags, CTLFLAG_RD,
77 &ept_pmap_flags, 0, NULL);
78
79 int
80 ept_init(int ipinum)
81 {
82 int use_hw_ad_bits, use_superpages, use_exec_only;
83 uint64_t cap;
84
85 cap = rdmsr(MSR_VMX_EPT_VPID_CAP);
86
87 /*
88 * Verify that:
89 * - page walk length is 4 steps
90 * - extended page tables can be laid out in write-back memory
91 * - invvpid instruction with all possible types is supported
92 * - invept instruction with all possible types is supported
93 */
94 if (!EPT_PWL4(cap) ||
95 !EPT_MEMORY_TYPE_WB(cap) ||
96 !INVVPID_SUPPORTED(cap) ||
97 !INVVPID_ALL_TYPES_SUPPORTED(cap) ||
98 !INVEPT_SUPPORTED(cap) ||
99 !INVEPT_ALL_TYPES_SUPPORTED(cap))
100 return (EINVAL);
101
102 ept_pmap_flags = ipinum & PMAP_NESTED_IPIMASK;
103
104 use_superpages = 1;
105 TUNABLE_INT_FETCH("hw.vmm.ept.use_superpages", &use_superpages);
106 if (use_superpages && EPT_PDE_SUPERPAGE(cap))
107 ept_pmap_flags |= PMAP_PDE_SUPERPAGE; /* 2MB superpage */
108
109 use_hw_ad_bits = 1;
110 TUNABLE_INT_FETCH("hw.vmm.ept.use_hw_ad_bits", &use_hw_ad_bits);
111 if (use_hw_ad_bits && AD_BITS_SUPPORTED(cap))
112 ept_enable_ad_bits = 1;
113 else
114 ept_pmap_flags |= PMAP_EMULATE_AD_BITS;
115
116 use_exec_only = 1;
117 TUNABLE_INT_FETCH("hw.vmm.ept.use_exec_only", &use_exec_only);
118 if (use_exec_only && EPT_SUPPORTS_EXEC_ONLY(cap))
119 ept_pmap_flags |= PMAP_SUPPORTS_EXEC_ONLY;
120
121 return (0);
122 }
123
124 #if 0
125 static void
126 ept_dump(uint64_t *ptp, int nlevels)
127 {
128 int i, t, tabs;
129 uint64_t *ptpnext, ptpval;
130
131 if (--nlevels < 0)
132 return;
133
134 tabs = 3 - nlevels;
135 for (t = 0; t < tabs; t++)
136 printf("\t");
137 printf("PTP = %p\n", ptp);
138
139 for (i = 0; i < 512; i++) {
140 ptpval = ptp[i];
141
142 if (ptpval == 0)
143 continue;
144
145 for (t = 0; t < tabs; t++)
146 printf("\t");
147 printf("%3d 0x%016lx\n", i, ptpval);
148
149 if (nlevels != 0 && (ptpval & EPT_PG_SUPERPAGE) == 0) {
150 ptpnext = (uint64_t *)
151 PHYS_TO_DMAP(ptpval & EPT_ADDR_MASK);
152 ept_dump(ptpnext, nlevels);
153 }
154 }
155 }
156 #endif
157
158 static void
159 invept_single_context(void *arg)
160 {
161 struct invept_desc desc = *(struct invept_desc *)arg;
162
163 invept(INVEPT_TYPE_SINGLE_CONTEXT, desc);
164 }
165
166 void
167 ept_invalidate_mappings(u_long eptp)
168 {
169 struct invept_desc invept_desc = { 0 };
170
171 invept_desc.eptp = eptp;
172
173 smp_rendezvous(NULL, invept_single_context, NULL, &invept_desc);
174 }
175
176 static int
177 ept_pinit(pmap_t pmap)
178 {
179
180 return (pmap_pinit_type(pmap, PT_EPT, ept_pmap_flags));
181 }
182
183 struct vmspace *
184 ept_vmspace_alloc(vm_offset_t min, vm_offset_t max)
185 {
186
187 return (vmspace_alloc(min, max, ept_pinit));
188 }
189
190 void
191 ept_vmspace_free(struct vmspace *vmspace)
192 {
193
194 vmspace_free(vmspace);
195 }
196
197 uint64_t
198 eptp(uint64_t pml4)
199 {
200 uint64_t eptp_val;
201
202 eptp_val = pml4 | (EPT_PWLEVELS - 1) << 3 | PAT_WRITE_BACK;
203 if (ept_enable_ad_bits)
204 eptp_val |= EPT_ENABLE_AD_BITS;
205
206 return (eptp_val);
207 }
Cache object: be121c9e091c802e31bde04a56361d23
|