xref: /f-stack/freebsd/amd64/amd64/efirt_machdep.c (revision 22ce4aff)
1 /*-
2  * Copyright (c) 2004 Marcel Moolenaar
3  * Copyright (c) 2001 Doug Rabson
4  * Copyright (c) 2016 The FreeBSD Foundation
5  * All rights reserved.
6  *
7  * Portions of this software were developed by Konstantin Belousov
8  * under sponsorship from the FreeBSD Foundation.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <sys/param.h>
36 #include <sys/efi.h>
37 #include <sys/kernel.h>
38 #include <sys/linker.h>
39 #include <sys/lock.h>
40 #include <sys/module.h>
41 #include <sys/mutex.h>
42 #include <sys/clock.h>
43 #include <sys/proc.h>
44 #include <sys/rwlock.h>
45 #include <sys/sched.h>
46 #include <sys/sysctl.h>
47 #include <sys/systm.h>
48 #include <sys/vmmeter.h>
49 #include <isa/rtc.h>
50 #include <machine/fpu.h>
51 #include <machine/efi.h>
52 #include <machine/metadata.h>
53 #include <machine/md_var.h>
54 #include <machine/smp.h>
55 #include <machine/vmparam.h>
56 #include <vm/vm.h>
57 #include <vm/pmap.h>
58 #include <vm/vm_extern.h>
59 #include <vm/vm_map.h>
60 #include <vm/vm_object.h>
61 #include <vm/vm_page.h>
62 #include <vm/vm_pager.h>
63 
64 static pml5_entry_t *efi_pml5;
65 static pml4_entry_t *efi_pml4;
66 static vm_object_t obj_1t1_pt;
67 static vm_page_t efi_pmltop_page;
68 static vm_pindex_t efi_1t1_idx;
69 
70 void
efi_destroy_1t1_map(void)71 efi_destroy_1t1_map(void)
72 {
73 	vm_page_t m;
74 
75 	if (obj_1t1_pt != NULL) {
76 		VM_OBJECT_RLOCK(obj_1t1_pt);
77 		TAILQ_FOREACH(m, &obj_1t1_pt->memq, listq)
78 			m->ref_count = VPRC_OBJREF;
79 		vm_wire_sub(obj_1t1_pt->resident_page_count);
80 		VM_OBJECT_RUNLOCK(obj_1t1_pt);
81 		vm_object_deallocate(obj_1t1_pt);
82 	}
83 
84 	obj_1t1_pt = NULL;
85 	efi_pml4 = NULL;
86 	efi_pml5 = NULL;
87 	efi_pmltop_page = NULL;
88 }
89 
90 /*
91  * Map a physical address from EFI runtime space into KVA space.  Returns 0 to
92  * indicate a failed mapping so that the caller may handle error.
93  */
94 vm_offset_t
efi_phys_to_kva(vm_paddr_t paddr)95 efi_phys_to_kva(vm_paddr_t paddr)
96 {
97 
98 	if (paddr >= dmaplimit)
99 		return (0);
100 	return (PHYS_TO_DMAP(paddr));
101 }
102 
103 static vm_page_t
efi_1t1_page(void)104 efi_1t1_page(void)
105 {
106 
107 	return (vm_page_grab(obj_1t1_pt, efi_1t1_idx++, VM_ALLOC_NOBUSY |
108 	    VM_ALLOC_WIRED | VM_ALLOC_ZERO));
109 }
110 
111 static pt_entry_t *
efi_1t1_pte(vm_offset_t va)112 efi_1t1_pte(vm_offset_t va)
113 {
114 	pml5_entry_t *pml5e;
115 	pml4_entry_t *pml4e;
116 	pdp_entry_t *pdpe;
117 	pd_entry_t *pde;
118 	pt_entry_t *pte;
119 	vm_page_t m;
120 	vm_pindex_t pml5_idx, pml4_idx, pdp_idx, pd_idx;
121 	vm_paddr_t mphys;
122 
123 	pml4_idx = pmap_pml4e_index(va);
124 	if (la57) {
125 		pml5_idx = pmap_pml5e_index(va);
126 		pml5e = &efi_pml5[pml5_idx];
127 		if (*pml5e == 0) {
128 			m = efi_1t1_page();
129 			mphys = VM_PAGE_TO_PHYS(m);
130 			*pml5e = mphys | X86_PG_RW | X86_PG_V;
131 		} else {
132 			mphys = *pml5e & PG_FRAME;
133 		}
134 		pml4e = (pml4_entry_t *)PHYS_TO_DMAP(mphys);
135 		pml4e = &pml4e[pml4_idx];
136 	} else {
137 		pml4e = &efi_pml4[pml4_idx];
138 	}
139 
140 	if (*pml4e == 0) {
141 		m = efi_1t1_page();
142 		mphys =  VM_PAGE_TO_PHYS(m);
143 		*pml4e = mphys | X86_PG_RW | X86_PG_V;
144 	} else {
145 		mphys = *pml4e & PG_FRAME;
146 	}
147 
148 	pdpe = (pdp_entry_t *)PHYS_TO_DMAP(mphys);
149 	pdp_idx = pmap_pdpe_index(va);
150 	pdpe += pdp_idx;
151 	if (*pdpe == 0) {
152 		m = efi_1t1_page();
153 		mphys =  VM_PAGE_TO_PHYS(m);
154 		*pdpe = mphys | X86_PG_RW | X86_PG_V;
155 	} else {
156 		mphys = *pdpe & PG_FRAME;
157 	}
158 
159 	pde = (pd_entry_t *)PHYS_TO_DMAP(mphys);
160 	pd_idx = pmap_pde_index(va);
161 	pde += pd_idx;
162 	if (*pde == 0) {
163 		m = efi_1t1_page();
164 		mphys = VM_PAGE_TO_PHYS(m);
165 		*pde = mphys | X86_PG_RW | X86_PG_V;
166 	} else {
167 		mphys = *pde & PG_FRAME;
168 	}
169 
170 	pte = (pt_entry_t *)PHYS_TO_DMAP(mphys);
171 	pte += pmap_pte_index(va);
172 	KASSERT(*pte == 0, ("va %#jx *pt %#jx", va, *pte));
173 
174 	return (pte);
175 }
176 
177 bool
efi_create_1t1_map(struct efi_md * map,int ndesc,int descsz)178 efi_create_1t1_map(struct efi_md *map, int ndesc, int descsz)
179 {
180 	struct efi_md *p;
181 	pt_entry_t *pte;
182 	void *pml;
183 	vm_offset_t va;
184 	uint64_t idx;
185 	int bits, i, mode;
186 
187 	obj_1t1_pt = vm_pager_allocate(OBJT_PHYS, NULL, ptoa(1 +
188 	    NPML4EPG + NPML4EPG * NPDPEPG + NPML4EPG * NPDPEPG * NPDEPG),
189 	    VM_PROT_ALL, 0, NULL);
190 	efi_1t1_idx = 0;
191 	VM_OBJECT_WLOCK(obj_1t1_pt);
192 	efi_pmltop_page = efi_1t1_page();
193 	VM_OBJECT_WUNLOCK(obj_1t1_pt);
194 	pml = (void *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(efi_pmltop_page));
195 	if (la57) {
196 		efi_pml5 = pml;
197 		pmap_pinit_pml5(efi_pmltop_page);
198 	} else {
199 		efi_pml4 = pml;
200 		pmap_pinit_pml4(efi_pmltop_page);
201 	}
202 
203 	for (i = 0, p = map; i < ndesc; i++, p = efi_next_descriptor(p,
204 	    descsz)) {
205 		if ((p->md_attr & EFI_MD_ATTR_RT) == 0)
206 			continue;
207 		if (p->md_virt != NULL && (uint64_t)p->md_virt != p->md_phys) {
208 			if (bootverbose)
209 				printf("EFI Runtime entry %d is mapped\n", i);
210 			goto fail;
211 		}
212 		if ((p->md_phys & EFI_PAGE_MASK) != 0) {
213 			if (bootverbose)
214 				printf("EFI Runtime entry %d is not aligned\n",
215 				    i);
216 			goto fail;
217 		}
218 		if (p->md_phys + p->md_pages * EFI_PAGE_SIZE < p->md_phys ||
219 		    p->md_phys + p->md_pages * EFI_PAGE_SIZE >=
220 		    VM_MAXUSER_ADDRESS) {
221 			printf("EFI Runtime entry %d is not in mappable for RT:"
222 			    "base %#016jx %#jx pages\n",
223 			    i, (uintmax_t)p->md_phys,
224 			    (uintmax_t)p->md_pages);
225 			goto fail;
226 		}
227 		if ((p->md_attr & EFI_MD_ATTR_WB) != 0)
228 			mode = VM_MEMATTR_WRITE_BACK;
229 		else if ((p->md_attr & EFI_MD_ATTR_WT) != 0)
230 			mode = VM_MEMATTR_WRITE_THROUGH;
231 		else if ((p->md_attr & EFI_MD_ATTR_WC) != 0)
232 			mode = VM_MEMATTR_WRITE_COMBINING;
233 		else if ((p->md_attr & EFI_MD_ATTR_WP) != 0)
234 			mode = VM_MEMATTR_WRITE_PROTECTED;
235 		else if ((p->md_attr & EFI_MD_ATTR_UC) != 0)
236 			mode = VM_MEMATTR_UNCACHEABLE;
237 		else {
238 			if (bootverbose)
239 				printf("EFI Runtime entry %d mapping "
240 				    "attributes unsupported\n", i);
241 			mode = VM_MEMATTR_UNCACHEABLE;
242 		}
243 		bits = pmap_cache_bits(kernel_pmap, mode, FALSE) | X86_PG_RW |
244 		    X86_PG_V;
245 		VM_OBJECT_WLOCK(obj_1t1_pt);
246 		for (va = p->md_phys, idx = 0; idx < p->md_pages; idx++,
247 		    va += PAGE_SIZE) {
248 			pte = efi_1t1_pte(va);
249 			pte_store(pte, va | bits);
250 		}
251 		VM_OBJECT_WUNLOCK(obj_1t1_pt);
252 	}
253 
254 	return (true);
255 
256 fail:
257 	efi_destroy_1t1_map();
258 	return (false);
259 }
260 
261 /*
262  * Create an environment for the EFI runtime code call.  The most
263  * important part is creating the required 1:1 physical->virtual
264  * mappings for the runtime segments.  To do that, we manually create
265  * page table which unmap userspace but gives correct kernel mapping.
266  * The 1:1 mappings for runtime segments usually occupy low 4G of the
267  * physical address map.
268  *
269  * The 1:1 mappings were chosen over the SetVirtualAddressMap() EFI RT
270  * service, because there are some BIOSes which fail to correctly
271  * relocate itself on the call, requiring both 1:1 and virtual
272  * mapping.  As result, we must provide 1:1 mapping anyway, so no
273  * reason to bother with the virtual map, and no need to add a
274  * complexity into loader.
275  *
276  * The fpu_kern_enter() call allows firmware to use FPU, as mandated
277  * by the specification.  In particular, CR0.TS bit is cleared.  Also
278  * it enters critical section, giving us neccessary protection against
279  * context switch.
280  *
281  * There is no need to disable interrupts around the change of %cr3,
282  * the kernel mappings are correct, while we only grabbed the
283  * userspace portion of VA.  Interrupts handlers must not access
284  * userspace.  Having interrupts enabled fixes the issue with
285  * firmware/SMM long operation, which would negatively affect IPIs,
286  * esp. TLB shootdown requests.
287  */
288 int
efi_arch_enter(void)289 efi_arch_enter(void)
290 {
291 	pmap_t curpmap;
292 
293 	curpmap = PCPU_GET(curpmap);
294 	PMAP_LOCK_ASSERT(curpmap, MA_OWNED);
295 	curthread->td_md.md_efirt_dis_pf = vm_fault_disable_pagefaults();
296 
297 	/*
298 	 * IPI TLB shootdown handler invltlb_pcid_handler() reloads
299 	 * %cr3 from the curpmap->pm_cr3, which would disable runtime
300 	 * segments mappings.  Block the handler's action by setting
301 	 * curpmap to impossible value.  See also comment in
302 	 * pmap.c:pmap_activate_sw().
303 	 */
304 	if (pmap_pcid_enabled && !invpcid_works)
305 		PCPU_SET(curpmap, NULL);
306 
307 	load_cr3(VM_PAGE_TO_PHYS(efi_pmltop_page) | (pmap_pcid_enabled ?
308 	    curpmap->pm_pcids[PCPU_GET(cpuid)].pm_pcid : 0));
309 	/*
310 	 * If PCID is enabled, the clear CR3_PCID_SAVE bit in the loaded %cr3
311 	 * causes TLB invalidation.
312 	 */
313 	if (!pmap_pcid_enabled)
314 		invltlb();
315 	return (0);
316 }
317 
318 void
efi_arch_leave(void)319 efi_arch_leave(void)
320 {
321 	pmap_t curpmap;
322 
323 	curpmap = &curproc->p_vmspace->vm_pmap;
324 	if (pmap_pcid_enabled && !invpcid_works)
325 		PCPU_SET(curpmap, curpmap);
326 	load_cr3(curpmap->pm_cr3 | (pmap_pcid_enabled ?
327 	    curpmap->pm_pcids[PCPU_GET(cpuid)].pm_pcid : 0));
328 	if (!pmap_pcid_enabled)
329 		invltlb();
330 	vm_fault_enable_pagefaults(curthread->td_md.md_efirt_dis_pf);
331 }
332 
333 /* XXX debug stuff */
334 static int
efi_time_sysctl_handler(SYSCTL_HANDLER_ARGS)335 efi_time_sysctl_handler(SYSCTL_HANDLER_ARGS)
336 {
337 	struct efi_tm tm;
338 	int error, val;
339 
340 	val = 0;
341 	error = sysctl_handle_int(oidp, &val, 0, req);
342 	if (error != 0 || req->newptr == NULL)
343 		return (error);
344 	error = efi_get_time(&tm);
345 	if (error == 0) {
346 		uprintf("EFI reports: Year %d Month %d Day %d Hour %d Min %d "
347 		    "Sec %d\n", tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour,
348 		    tm.tm_min, tm.tm_sec);
349 	}
350 	return (error);
351 }
352 
353 SYSCTL_PROC(_debug, OID_AUTO, efi_time,
354     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, 0,
355     efi_time_sysctl_handler, "I",
356     "");
357