1366f6083SPeter Grehan /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3c49761ddSPedro F. Giffuni *
4366f6083SPeter Grehan * Copyright (c) 2011 NetApp, Inc.
5366f6083SPeter Grehan * All rights reserved.
6366f6083SPeter Grehan *
7366f6083SPeter Grehan * Redistribution and use in source and binary forms, with or without
8366f6083SPeter Grehan * modification, are permitted provided that the following conditions
9366f6083SPeter Grehan * are met:
10366f6083SPeter Grehan * 1. Redistributions of source code must retain the above copyright
11366f6083SPeter Grehan * notice, this list of conditions and the following disclaimer.
12366f6083SPeter Grehan * 2. Redistributions in binary form must reproduce the above copyright
13366f6083SPeter Grehan * notice, this list of conditions and the following disclaimer in the
14366f6083SPeter Grehan * documentation and/or other materials provided with the distribution.
15366f6083SPeter Grehan *
16366f6083SPeter Grehan * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17366f6083SPeter Grehan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18366f6083SPeter Grehan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19366f6083SPeter Grehan * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20366f6083SPeter Grehan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21366f6083SPeter Grehan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22366f6083SPeter Grehan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23366f6083SPeter Grehan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24366f6083SPeter Grehan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25366f6083SPeter Grehan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26366f6083SPeter Grehan * SUCH DAMAGE.
27366f6083SPeter Grehan */
28366f6083SPeter Grehan
29318224bbSNeel Natu #include <sys/param.h>
30318224bbSNeel Natu #include <sys/kernel.h>
31366f6083SPeter Grehan #include <sys/types.h>
32366f6083SPeter Grehan #include <sys/systm.h>
33366f6083SPeter Grehan #include <sys/smp.h>
34318224bbSNeel Natu #include <sys/sysctl.h>
35366f6083SPeter Grehan
36366f6083SPeter Grehan #include <vm/vm.h>
37366f6083SPeter Grehan #include <vm/pmap.h>
38318224bbSNeel Natu #include <vm/vm_extern.h>
39366f6083SPeter Grehan
40366f6083SPeter Grehan #include <machine/vmm.h>
41318224bbSNeel Natu
42366f6083SPeter Grehan #include "vmx_cpufunc.h"
43366f6083SPeter Grehan #include "ept.h"
44366f6083SPeter Grehan
45318224bbSNeel Natu #define EPT_SUPPORTS_EXEC_ONLY(cap) ((cap) & (1UL << 0))
46366f6083SPeter Grehan #define EPT_PWL4(cap) ((cap) & (1UL << 6))
47366f6083SPeter Grehan #define EPT_MEMORY_TYPE_WB(cap) ((cap) & (1UL << 14))
48366f6083SPeter Grehan #define EPT_PDE_SUPERPAGE(cap) ((cap) & (1UL << 16)) /* 2MB pages */
49366f6083SPeter Grehan #define EPT_PDPTE_SUPERPAGE(cap) ((cap) & (1UL << 17)) /* 1GB pages */
50366f6083SPeter Grehan #define INVEPT_SUPPORTED(cap) ((cap) & (1UL << 20))
51318224bbSNeel Natu #define AD_BITS_SUPPORTED(cap) ((cap) & (1UL << 21))
52318224bbSNeel Natu #define INVVPID_SUPPORTED(cap) ((cap) & (1UL << 32))
53366f6083SPeter Grehan
54366f6083SPeter Grehan #define INVVPID_ALL_TYPES_MASK 0xF0000000000UL
55366f6083SPeter Grehan #define INVVPID_ALL_TYPES_SUPPORTED(cap) \
56366f6083SPeter Grehan (((cap) & INVVPID_ALL_TYPES_MASK) == INVVPID_ALL_TYPES_MASK)
57366f6083SPeter Grehan
58366f6083SPeter Grehan #define INVEPT_ALL_TYPES_MASK 0x6000000UL
59366f6083SPeter Grehan #define INVEPT_ALL_TYPES_SUPPORTED(cap) \
60366f6083SPeter Grehan (((cap) & INVEPT_ALL_TYPES_MASK) == INVEPT_ALL_TYPES_MASK)
61366f6083SPeter Grehan
62318224bbSNeel Natu #define EPT_PWLEVELS 4 /* page walk levels */
63318224bbSNeel Natu #define EPT_ENABLE_AD_BITS (1 << 6)
64366f6083SPeter Grehan
65318224bbSNeel Natu SYSCTL_DECL(_hw_vmm);
66b40598c5SPawel Biernacki SYSCTL_NODE(_hw_vmm, OID_AUTO, ept, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
67b40598c5SPawel Biernacki NULL);
68366f6083SPeter Grehan
69318224bbSNeel Natu static int ept_enable_ad_bits;
70366f6083SPeter Grehan
71318224bbSNeel Natu static int ept_pmap_flags;
72318224bbSNeel Natu SYSCTL_INT(_hw_vmm_ept, OID_AUTO, pmap_flags, CTLFLAG_RD,
73318224bbSNeel Natu &ept_pmap_flags, 0, NULL);
74e60f5d77SPeter Grehan
75366f6083SPeter Grehan int
ept_init(int ipinum)76add611fdSNeel Natu ept_init(int ipinum)
77366f6083SPeter Grehan {
78318224bbSNeel Natu int use_hw_ad_bits, use_superpages, use_exec_only;
79366f6083SPeter Grehan uint64_t cap;
80366f6083SPeter Grehan
81366f6083SPeter Grehan cap = rdmsr(MSR_VMX_EPT_VPID_CAP);
82366f6083SPeter Grehan
83366f6083SPeter Grehan /*
84366f6083SPeter Grehan * Verify that:
85366f6083SPeter Grehan * - page walk length is 4 steps
86366f6083SPeter Grehan * - extended page tables can be laid out in write-back memory
87366f6083SPeter Grehan * - invvpid instruction with all possible types is supported
88366f6083SPeter Grehan * - invept instruction with all possible types is supported
89366f6083SPeter Grehan */
90366f6083SPeter Grehan if (!EPT_PWL4(cap) ||
91366f6083SPeter Grehan !EPT_MEMORY_TYPE_WB(cap) ||
92366f6083SPeter Grehan !INVVPID_SUPPORTED(cap) ||
93366f6083SPeter Grehan !INVVPID_ALL_TYPES_SUPPORTED(cap) ||
94366f6083SPeter Grehan !INVEPT_SUPPORTED(cap) ||
95366f6083SPeter Grehan !INVEPT_ALL_TYPES_SUPPORTED(cap))
96366f6083SPeter Grehan return (EINVAL);
97366f6083SPeter Grehan
98add611fdSNeel Natu ept_pmap_flags = ipinum & PMAP_NESTED_IPIMASK;
995515bb73SNeel Natu
100318224bbSNeel Natu use_superpages = 1;
101318224bbSNeel Natu TUNABLE_INT_FETCH("hw.vmm.ept.use_superpages", &use_superpages);
102318224bbSNeel Natu if (use_superpages && EPT_PDE_SUPERPAGE(cap))
103318224bbSNeel Natu ept_pmap_flags |= PMAP_PDE_SUPERPAGE; /* 2MB superpage */
104366f6083SPeter Grehan
105318224bbSNeel Natu use_hw_ad_bits = 1;
106318224bbSNeel Natu TUNABLE_INT_FETCH("hw.vmm.ept.use_hw_ad_bits", &use_hw_ad_bits);
107318224bbSNeel Natu if (use_hw_ad_bits && AD_BITS_SUPPORTED(cap))
108318224bbSNeel Natu ept_enable_ad_bits = 1;
109318224bbSNeel Natu else
110318224bbSNeel Natu ept_pmap_flags |= PMAP_EMULATE_AD_BITS;
111366f6083SPeter Grehan
112318224bbSNeel Natu use_exec_only = 1;
113318224bbSNeel Natu TUNABLE_INT_FETCH("hw.vmm.ept.use_exec_only", &use_exec_only);
114318224bbSNeel Natu if (use_exec_only && EPT_SUPPORTS_EXEC_ONLY(cap))
115318224bbSNeel Natu ept_pmap_flags |= PMAP_SUPPORTS_EXEC_ONLY;
116366f6083SPeter Grehan
117366f6083SPeter Grehan return (0);
118366f6083SPeter Grehan }
119366f6083SPeter Grehan
120bda273f2SNeel Natu #if 0
121bda273f2SNeel Natu static void
122bda273f2SNeel Natu ept_dump(uint64_t *ptp, int nlevels)
123bda273f2SNeel Natu {
124bda273f2SNeel Natu int i, t, tabs;
125bda273f2SNeel Natu uint64_t *ptpnext, ptpval;
126bda273f2SNeel Natu
127bda273f2SNeel Natu if (--nlevels < 0)
128bda273f2SNeel Natu return;
129bda273f2SNeel Natu
130bda273f2SNeel Natu tabs = 3 - nlevels;
131bda273f2SNeel Natu for (t = 0; t < tabs; t++)
132bda273f2SNeel Natu printf("\t");
133bda273f2SNeel Natu printf("PTP = %p\n", ptp);
134bda273f2SNeel Natu
135bda273f2SNeel Natu for (i = 0; i < 512; i++) {
136bda273f2SNeel Natu ptpval = ptp[i];
137bda273f2SNeel Natu
138bda273f2SNeel Natu if (ptpval == 0)
139bda273f2SNeel Natu continue;
140bda273f2SNeel Natu
141bda273f2SNeel Natu for (t = 0; t < tabs; t++)
142bda273f2SNeel Natu printf("\t");
143bda273f2SNeel Natu printf("%3d 0x%016lx\n", i, ptpval);
144bda273f2SNeel Natu
145bda273f2SNeel Natu if (nlevels != 0 && (ptpval & EPT_PG_SUPERPAGE) == 0) {
146bda273f2SNeel Natu ptpnext = (uint64_t *)
147bda273f2SNeel Natu PHYS_TO_DMAP(ptpval & EPT_ADDR_MASK);
148bda273f2SNeel Natu ept_dump(ptpnext, nlevels);
149bda273f2SNeel Natu }
150bda273f2SNeel Natu }
151bda273f2SNeel Natu }
152bda273f2SNeel Natu #endif
153bda273f2SNeel Natu
154366f6083SPeter Grehan static void
invept_single_context(void * arg)155366f6083SPeter Grehan invept_single_context(void *arg)
156366f6083SPeter Grehan {
157366f6083SPeter Grehan struct invept_desc desc = *(struct invept_desc *)arg;
158366f6083SPeter Grehan
159366f6083SPeter Grehan invept(INVEPT_TYPE_SINGLE_CONTEXT, desc);
160366f6083SPeter Grehan }
161366f6083SPeter Grehan
162366f6083SPeter Grehan void
ept_invalidate_mappings(u_long eptp)163318224bbSNeel Natu ept_invalidate_mappings(u_long eptp)
164366f6083SPeter Grehan {
165366f6083SPeter Grehan struct invept_desc invept_desc = { 0 };
166366f6083SPeter Grehan
167318224bbSNeel Natu invept_desc.eptp = eptp;
168366f6083SPeter Grehan
169366f6083SPeter Grehan smp_rendezvous(NULL, invept_single_context, NULL, &invept_desc);
170366f6083SPeter Grehan }
171318224bbSNeel Natu
172318224bbSNeel Natu static int
ept_pinit(pmap_t pmap)173318224bbSNeel Natu ept_pinit(pmap_t pmap)
174318224bbSNeel Natu {
175318224bbSNeel Natu
176318224bbSNeel Natu return (pmap_pinit_type(pmap, PT_EPT, ept_pmap_flags));
177318224bbSNeel Natu }
178318224bbSNeel Natu
179318224bbSNeel Natu struct vmspace *
ept_vmspace_alloc(vm_offset_t min,vm_offset_t max)180318224bbSNeel Natu ept_vmspace_alloc(vm_offset_t min, vm_offset_t max)
181318224bbSNeel Natu {
182318224bbSNeel Natu
183318224bbSNeel Natu return (vmspace_alloc(min, max, ept_pinit));
184318224bbSNeel Natu }
185318224bbSNeel Natu
186318224bbSNeel Natu void
ept_vmspace_free(struct vmspace * vmspace)187318224bbSNeel Natu ept_vmspace_free(struct vmspace *vmspace)
188318224bbSNeel Natu {
189318224bbSNeel Natu
190318224bbSNeel Natu vmspace_free(vmspace);
191318224bbSNeel Natu }
192318224bbSNeel Natu
193318224bbSNeel Natu uint64_t
eptp(uint64_t pml4)194318224bbSNeel Natu eptp(uint64_t pml4)
195318224bbSNeel Natu {
196318224bbSNeel Natu uint64_t eptp_val;
197318224bbSNeel Natu
198318224bbSNeel Natu eptp_val = pml4 | (EPT_PWLEVELS - 1) << 3 | PAT_WRITE_BACK;
199318224bbSNeel Natu if (ept_enable_ad_bits)
200318224bbSNeel Natu eptp_val |= EPT_ENABLE_AD_BITS;
201318224bbSNeel Natu
202318224bbSNeel Natu return (eptp_val);
203318224bbSNeel Natu }
204