2 * Copyright (c) 2004 Marcel Moolenaar
3 * Copyright (c) 2001 Doug Rabson
4 * Copyright (c) 2016 The FreeBSD Foundation
7 * Portions of this software were developed by Konstantin Belousov
8 * under sponsorship from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/linker.h>
40 #include <sys/module.h>
41 #include <sys/mutex.h>
42 #include <sys/clock.h>
44 #include <sys/rwlock.h>
45 #include <sys/sched.h>
46 #include <sys/sysctl.h>
47 #include <sys/systm.h>
48 #include <machine/fpu.h>
49 #include <machine/efi.h>
50 #include <machine/metadata.h>
51 #include <machine/md_var.h>
52 #include <machine/smp.h>
53 #include <machine/vmparam.h>
56 #include <vm/vm_map.h>
57 #include <vm/vm_object.h>
58 #include <vm/vm_page.h>
59 #include <vm/vm_pager.h>
61 static struct efi_systbl *efi_systbl;
62 static struct efi_cfgtbl *efi_cfgtbl;
63 static struct efi_rt *efi_runtime;
65 static int efi_status2err[25] = {
67 ENOEXEC, /* EFI_LOAD_ERROR */
68 EINVAL, /* EFI_INVALID_PARAMETER */
69 ENOSYS, /* EFI_UNSUPPORTED */
70 EMSGSIZE, /* EFI_BAD_BUFFER_SIZE */
71 EOVERFLOW, /* EFI_BUFFER_TOO_SMALL */
72 EBUSY, /* EFI_NOT_READY */
73 EIO, /* EFI_DEVICE_ERROR */
74 EROFS, /* EFI_WRITE_PROTECTED */
75 EAGAIN, /* EFI_OUT_OF_RESOURCES */
76 EIO, /* EFI_VOLUME_CORRUPTED */
77 ENOSPC, /* EFI_VOLUME_FULL */
78 ENXIO, /* EFI_NO_MEDIA */
79 ESTALE, /* EFI_MEDIA_CHANGED */
80 ENOENT, /* EFI_NOT_FOUND */
81 EACCES, /* EFI_ACCESS_DENIED */
82 ETIMEDOUT, /* EFI_NO_RESPONSE */
83 EADDRNOTAVAIL, /* EFI_NO_MAPPING */
84 ETIMEDOUT, /* EFI_TIMEOUT */
85 EDOOFUS, /* EFI_NOT_STARTED */
86 EALREADY, /* EFI_ALREADY_STARTED */
87 ECANCELED, /* EFI_ABORTED */
88 EPROTO, /* EFI_ICMP_ERROR */
89 EPROTO, /* EFI_TFTP_ERROR */
90 EPROTO /* EFI_PROTOCOL_ERROR */
94 efi_status_to_errno(efi_status status)
98 code = status & 0x3ffffffffffffffful;
99 return (code < nitems(efi_status2err) ? efi_status2err[code] : EDOOFUS);
102 static struct mtx efi_lock;
103 static pml4_entry_t *efi_pml4;
104 static vm_object_t obj_1t1_pt;
105 static vm_page_t efi_pml4_page;
108 efi_destroy_1t1_map(void)
112 if (obj_1t1_pt != NULL) {
113 VM_OBJECT_RLOCK(obj_1t1_pt);
114 TAILQ_FOREACH(m, &obj_1t1_pt->memq, listq)
116 atomic_subtract_int(&vm_cnt.v_wire_count,
117 obj_1t1_pt->resident_page_count);
118 VM_OBJECT_RUNLOCK(obj_1t1_pt);
119 vm_object_deallocate(obj_1t1_pt);
124 efi_pml4_page = NULL;
128 efi_1t1_page(vm_pindex_t idx)
131 return (vm_page_grab(obj_1t1_pt, idx, VM_ALLOC_NOBUSY |
132 VM_ALLOC_WIRED | VM_ALLOC_ZERO));
136 efi_1t1_pte(vm_offset_t va)
143 vm_pindex_t pml4_idx, pdp_idx, pd_idx;
146 pml4_idx = pmap_pml4e_index(va);
147 pml4e = &efi_pml4[pml4_idx];
149 m = efi_1t1_page(1 + pml4_idx);
150 mphys = VM_PAGE_TO_PHYS(m);
151 *pml4e = mphys | X86_PG_RW | X86_PG_V;
153 mphys = *pml4e & ~PAGE_MASK;
156 pdpe = (pdp_entry_t *)PHYS_TO_DMAP(mphys);
157 pdp_idx = pmap_pdpe_index(va);
160 m = efi_1t1_page(1 + NPML4EPG + (pml4_idx + 1) * (pdp_idx + 1));
161 mphys = VM_PAGE_TO_PHYS(m);
162 *pdpe = mphys | X86_PG_RW | X86_PG_V;
164 mphys = *pdpe & ~PAGE_MASK;
167 pde = (pd_entry_t *)PHYS_TO_DMAP(mphys);
168 pd_idx = pmap_pde_index(va);
171 m = efi_1t1_page(1 + NPML4EPG + NPML4EPG * NPDPEPG +
172 (pml4_idx + 1) * (pdp_idx + 1) * (pd_idx + 1));
173 mphys = VM_PAGE_TO_PHYS(m);
174 *pde = mphys | X86_PG_RW | X86_PG_V;
176 mphys = *pde & ~PAGE_MASK;
179 pte = (pt_entry_t *)PHYS_TO_DMAP(mphys);
180 pte += pmap_pte_index(va);
181 KASSERT(*pte == 0, ("va %#jx *pt %#jx", va, *pte));
187 efi_create_1t1_map(struct efi_md *map, int ndesc, int descsz)
195 obj_1t1_pt = vm_pager_allocate(OBJT_PHYS, NULL, 1 + NPML4EPG +
196 NPML4EPG * NPDPEPG + NPML4EPG * NPDPEPG * NPDEPG,
197 VM_PROT_ALL, 0, NULL);
198 VM_OBJECT_WLOCK(obj_1t1_pt);
199 efi_pml4_page = efi_1t1_page(0);
200 VM_OBJECT_WUNLOCK(obj_1t1_pt);
201 efi_pml4 = (pml4_entry_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(efi_pml4_page));
202 pmap_pinit_pml4(efi_pml4_page);
204 for (i = 0, p = map; i < ndesc; i++, p = efi_next_descriptor(p,
206 if ((p->md_attr & EFI_MD_ATTR_RT) == 0)
208 if (p->md_virt != NULL) {
210 printf("EFI Runtime entry %d is mapped\n", i);
213 if ((p->md_phys & EFI_PAGE_MASK) != 0) {
215 printf("EFI Runtime entry %d is not aligned\n",
219 if (p->md_phys + p->md_pages * EFI_PAGE_SIZE < p->md_phys ||
220 p->md_phys + p->md_pages * EFI_PAGE_SIZE >=
221 VM_MAXUSER_ADDRESS) {
222 printf("EFI Runtime entry %d is not in mappable for RT:"
223 "base %#016jx %#jx pages\n",
224 i, (uintmax_t)p->md_phys,
225 (uintmax_t)p->md_pages);
228 if ((p->md_attr & EFI_MD_ATTR_WB) != 0)
229 mode = VM_MEMATTR_WRITE_BACK;
230 else if ((p->md_attr & EFI_MD_ATTR_WT) != 0)
231 mode = VM_MEMATTR_WRITE_THROUGH;
232 else if ((p->md_attr & EFI_MD_ATTR_WC) != 0)
233 mode = VM_MEMATTR_WRITE_COMBINING;
234 else if ((p->md_attr & EFI_MD_ATTR_WP) != 0)
235 mode = VM_MEMATTR_WRITE_PROTECTED;
236 else if ((p->md_attr & EFI_MD_ATTR_UC) != 0)
237 mode = VM_MEMATTR_UNCACHEABLE;
240 printf("EFI Runtime entry %d mapping "
241 "attributes unsupported\n", i);
242 mode = VM_MEMATTR_UNCACHEABLE;
244 bits = pmap_cache_bits(kernel_pmap, mode, FALSE) | X86_PG_RW |
246 VM_OBJECT_WLOCK(obj_1t1_pt);
247 for (va = p->md_phys, idx = 0; idx < p->md_pages; idx++,
249 pte = efi_1t1_pte(va);
250 pte_store(pte, va | bits);
252 VM_OBJECT_WUNLOCK(obj_1t1_pt);
258 efi_destroy_1t1_map();
263 * Create an environment for the EFI runtime code call. The most
264 * important part is creating the required 1:1 physical->virtual
265 * mappings for the runtime segments. To do that, we manually create
266 * page table which unmap userspace but gives correct kernel mapping.
267 * The 1:1 mappings for runtime segments usually occupy low 4G of the
268 * physical address map.
270 * The 1:1 mappings were chosen over the SetVirtualAddressMap() EFI RT
271 * service, because there are some BIOSes which fail to correctly
272 * relocate itself on the call, requiring both 1:1 and virtual
273 * mapping. As result, we must provide 1:1 mapping anyway, so no
274 * reason to bother with the virtual map, and no need to add a
275 * complexity into loader.
277 * The fpu_kern_enter() call allows firmware to use FPU, as mandated
278 * by the specification. In particular, CR0.TS bit is cleared. Also
279 * it enters critical section, giving us neccessary protection against
282 * There is no need to disable interrupts around the change of %cr3,
283 * the kernel mappings are correct, while we only grabbed the
284 * userspace portion of VA. Interrupts handlers must not access
285 * userspace. Having interrupts enabled fixes the issue with
286 * firmware/SMM long operation, which would negatively affect IPIs,
287 * esp. TLB shootdown requests.
295 if (efi_runtime == NULL)
297 curpmap = PCPU_GET(curpmap);
300 error = fpu_kern_enter(curthread, NULL, FPU_KERN_NOCTX);
302 PMAP_UNLOCK(curpmap);
307 * IPI TLB shootdown handler invltlb_pcid_handler() reloads
308 * %cr3 from the curpmap->pm_cr3, which would disable runtime
309 * segments mappings. Block the handler's action by setting
310 * curpmap to impossible value. See also comment in
311 * pmap.c:pmap_activate_sw().
313 if (pmap_pcid_enabled && !invpcid_works)
314 PCPU_SET(curpmap, NULL);
316 load_cr3(VM_PAGE_TO_PHYS(efi_pml4_page) | (pmap_pcid_enabled ?
317 curpmap->pm_pcids[PCPU_GET(cpuid)].pm_pcid : 0));
319 * If PCID is enabled, the clear CR3_PCID_SAVE bit in the loaded %cr3
320 * causes TLB invalidation.
322 if (!pmap_pcid_enabled)
332 curpmap = &curproc->p_vmspace->vm_pmap;
333 if (pmap_pcid_enabled && !invpcid_works)
334 PCPU_SET(curpmap, curpmap);
335 load_cr3(curpmap->pm_cr3 | (pmap_pcid_enabled ?
336 curpmap->pm_pcids[PCPU_GET(cpuid)].pm_pcid : 0));
337 if (!pmap_pcid_enabled)
340 fpu_kern_leave(curthread, NULL);
341 mtx_unlock(&efi_lock);
342 PMAP_UNLOCK(curpmap);
348 struct efi_map_header *efihdr;
353 mtx_init(&efi_lock, "efi", NULL, MTX_DEF);
355 if (efi_systbl_phys == 0) {
357 printf("EFI systbl not available\n");
360 efi_systbl = (struct efi_systbl *)PHYS_TO_DMAP(efi_systbl_phys);
361 if (efi_systbl->st_hdr.th_sig != EFI_SYSTBL_SIG) {
364 printf("EFI systbl signature invalid\n");
367 efi_cfgtbl = (efi_systbl->st_cfgtbl == 0) ? NULL :
368 (struct efi_cfgtbl *)efi_systbl->st_cfgtbl;
369 if (efi_cfgtbl == NULL) {
371 printf("EFI config table is not present\n");
374 kmdp = preload_search_by_type("elf kernel");
376 kmdp = preload_search_by_type("elf64 kernel");
377 efihdr = (struct efi_map_header *)preload_search_info(kmdp,
378 MODINFO_METADATA | MODINFOMD_EFI_MAP);
379 if (efihdr == NULL) {
381 printf("EFI map is not present\n");
384 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
385 map = (struct efi_md *)((uint8_t *)efihdr + efisz);
386 if (efihdr->descriptor_size == 0)
389 if (!efi_create_1t1_map(map, efihdr->memory_size /
390 efihdr->descriptor_size, efihdr->descriptor_size)) {
392 printf("EFI cannot create runtime map\n");
396 efi_runtime = (efi_systbl->st_rt == 0) ? NULL :
397 (struct efi_rt *)efi_systbl->st_rt;
398 if (efi_runtime == NULL) {
400 printf("EFI runtime services table is not present\n");
401 efi_destroy_1t1_map();
412 efi_destroy_1t1_map();
418 mtx_destroy(&efi_lock);
422 efi_get_table(struct uuid *uuid, void **ptr)
424 struct efi_cfgtbl *ct;
427 if (efi_cfgtbl == NULL)
429 count = efi_systbl->st_entries;
432 if (!bcmp(&ct->ct_uuid, uuid, sizeof(*uuid))) {
433 *ptr = (void *)PHYS_TO_DMAP(ct->ct_data);
442 efi_get_time_locked(struct efi_tm *tm)
447 mtx_assert(&resettodr_lock, MA_OWNED);
451 status = efi_runtime->rt_gettime(tm, NULL);
453 error = efi_status_to_errno(status);
458 efi_get_time(struct efi_tm *tm)
462 if (efi_runtime == NULL)
464 mtx_lock(&resettodr_lock);
465 error = efi_get_time_locked(tm);
466 mtx_unlock(&resettodr_lock);
471 efi_reset_system(void)
478 efi_runtime->rt_reset(EFI_RESET_WARM, 0, 0, NULL);
484 efi_set_time_locked(struct efi_tm *tm)
489 mtx_assert(&resettodr_lock, MA_OWNED);
493 status = efi_runtime->rt_settime(tm);
495 error = efi_status_to_errno(status);
500 efi_set_time(struct efi_tm *tm)
504 if (efi_runtime == NULL)
506 mtx_lock(&resettodr_lock);
507 error = efi_set_time_locked(tm);
508 mtx_unlock(&resettodr_lock);
513 efi_var_get(efi_char *name, struct uuid *vendor, uint32_t *attrib,
514 size_t *datasize, void *data)
522 status = efi_runtime->rt_getvar(name, vendor, attrib, datasize, data);
524 error = efi_status_to_errno(status);
529 efi_var_nextname(size_t *namesize, efi_char *name, struct uuid *vendor)
537 status = efi_runtime->rt_scanvar(namesize, name, vendor);
539 error = efi_status_to_errno(status);
544 efi_var_set(efi_char *name, struct uuid *vendor, uint32_t attrib,
545 size_t datasize, void *data)
553 status = efi_runtime->rt_setvar(name, vendor, attrib, datasize, data);
555 error = efi_status_to_errno(status);
560 efirt_modevents(module_t m, int event, void *arg __unused)
579 static moduledata_t efirt_moddata = {
581 .evhand = efirt_modevents,
584 DECLARE_MODULE(efirt, efirt_moddata, SI_SUB_VM_CONF, SI_ORDER_ANY);
585 MODULE_VERSION(efirt, 1);
587 /* XXX debug stuff */
589 efi_time_sysctl_handler(SYSCTL_HANDLER_ARGS)
595 error = sysctl_handle_int(oidp, &val, 0, req);
596 if (error != 0 || req->newptr == NULL)
598 error = efi_get_time(&tm);
600 uprintf("EFI reports: Year %d Month %d Day %d Hour %d Min %d "
601 "Sec %d\n", tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour,
602 tm.tm_min, tm.tm_sec);
607 SYSCTL_PROC(_debug, OID_AUTO, efi_time, CTLTYPE_INT | CTLFLAG_RW, NULL, 0,
608 efi_time_sysctl_handler, "I", "");