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