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/param.h> 33 #include <sys/efi.h> 34 #include <sys/kernel.h> 35 #include <sys/linker.h> 36 #include <sys/lock.h> 37 #include <sys/module.h> 38 #include <sys/mutex.h> 39 #include <sys/clock.h> 40 #include <sys/proc.h> 41 #include <sys/rwlock.h> 42 #include <sys/sched.h> 43 #include <sys/sysctl.h> 44 #include <sys/systm.h> 45 #include <sys/vmmeter.h> 46 #include <isa/rtc.h> 47 #include <machine/efi.h> 48 #include <machine/md_var.h> 49 #include <machine/vmparam.h> 50 #include <vm/vm.h> 51 #include <vm/pmap.h> 52 #include <vm/vm_extern.h> 53 #include <vm/vm_map.h> 54 #include <vm/vm_object.h> 55 #include <vm/vm_page.h> 56 #include <vm/vm_pager.h> 57 58 static pml5_entry_t *efi_pml5; 59 static pml4_entry_t *efi_pml4; 60 static vm_object_t obj_1t1_pt; 61 static vm_page_t efi_pmltop_page; 62 static vm_pindex_t efi_1t1_idx; 63 64 void 65 efi_destroy_1t1_map(void) 66 { 67 vm_page_t m; 68 69 if (obj_1t1_pt != NULL) { 70 VM_OBJECT_RLOCK(obj_1t1_pt); 71 TAILQ_FOREACH(m, &obj_1t1_pt->memq, listq) 72 m->ref_count = VPRC_OBJREF; 73 vm_wire_sub(obj_1t1_pt->resident_page_count); 74 VM_OBJECT_RUNLOCK(obj_1t1_pt); 75 vm_object_deallocate(obj_1t1_pt); 76 } 77 78 obj_1t1_pt = NULL; 79 efi_pml4 = NULL; 80 efi_pml5 = NULL; 81 efi_pmltop_page = NULL; 82 } 83 84 /* 85 * Map a physical address from EFI runtime space into KVA space. Returns 0 to 86 * indicate a failed mapping so that the caller may handle error. 87 */ 88 vm_offset_t 89 efi_phys_to_kva(vm_paddr_t paddr) 90 { 91 92 if (paddr >= dmaplimit) 93 return (0); 94 return (PHYS_TO_DMAP(paddr)); 95 } 96 97 static vm_page_t 98 efi_1t1_page(void) 99 { 100 101 return (vm_page_grab(obj_1t1_pt, efi_1t1_idx++, VM_ALLOC_NOBUSY | 102 VM_ALLOC_WIRED | VM_ALLOC_ZERO)); 103 } 104 105 static pt_entry_t * 106 efi_1t1_pte(vm_offset_t va) 107 { 108 pml5_entry_t *pml5e; 109 pml4_entry_t *pml4e; 110 pdp_entry_t *pdpe; 111 pd_entry_t *pde; 112 pt_entry_t *pte; 113 vm_page_t m; 114 vm_pindex_t pml5_idx, pml4_idx, pdp_idx, pd_idx; 115 vm_paddr_t mphys; 116 117 pml4_idx = pmap_pml4e_index(va); 118 if (la57) { 119 pml5_idx = pmap_pml5e_index(va); 120 pml5e = &efi_pml5[pml5_idx]; 121 if (*pml5e == 0) { 122 m = efi_1t1_page(); 123 mphys = VM_PAGE_TO_PHYS(m); 124 *pml5e = mphys | X86_PG_RW | X86_PG_V; 125 } else { 126 mphys = *pml5e & PG_FRAME; 127 } 128 pml4e = (pml4_entry_t *)PHYS_TO_DMAP(mphys); 129 pml4e = &pml4e[pml4_idx]; 130 } else { 131 pml4e = &efi_pml4[pml4_idx]; 132 } 133 134 if (*pml4e == 0) { 135 m = efi_1t1_page(); 136 mphys = VM_PAGE_TO_PHYS(m); 137 *pml4e = mphys | X86_PG_RW | X86_PG_V; 138 } else { 139 mphys = *pml4e & PG_FRAME; 140 } 141 142 pdpe = (pdp_entry_t *)PHYS_TO_DMAP(mphys); 143 pdp_idx = pmap_pdpe_index(va); 144 pdpe += pdp_idx; 145 if (*pdpe == 0) { 146 m = efi_1t1_page(); 147 mphys = VM_PAGE_TO_PHYS(m); 148 *pdpe = mphys | X86_PG_RW | X86_PG_V; 149 } else { 150 mphys = *pdpe & PG_FRAME; 151 } 152 153 pde = (pd_entry_t *)PHYS_TO_DMAP(mphys); 154 pd_idx = pmap_pde_index(va); 155 pde += pd_idx; 156 if (*pde == 0) { 157 m = efi_1t1_page(); 158 mphys = VM_PAGE_TO_PHYS(m); 159 *pde = mphys | X86_PG_RW | X86_PG_V; 160 } else { 161 mphys = *pde & PG_FRAME; 162 } 163 164 pte = (pt_entry_t *)PHYS_TO_DMAP(mphys); 165 pte += pmap_pte_index(va); 166 KASSERT(*pte == 0, ("va %#jx *pt %#jx", va, *pte)); 167 168 return (pte); 169 } 170 171 bool 172 efi_create_1t1_map(struct efi_md *map, int ndesc, int descsz) 173 { 174 struct efi_md *p; 175 pt_entry_t *pte; 176 void *pml; 177 vm_page_t m; 178 vm_offset_t va; 179 uint64_t idx; 180 int bits, i, mode; 181 182 obj_1t1_pt = vm_pager_allocate(OBJT_PHYS, NULL, ptoa(1 + 183 NPML4EPG + NPML4EPG * NPDPEPG + NPML4EPG * NPDPEPG * NPDEPG), 184 VM_PROT_ALL, 0, NULL); 185 efi_1t1_idx = 0; 186 VM_OBJECT_WLOCK(obj_1t1_pt); 187 efi_pmltop_page = efi_1t1_page(); 188 VM_OBJECT_WUNLOCK(obj_1t1_pt); 189 pml = (void *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(efi_pmltop_page)); 190 if (la57) { 191 efi_pml5 = pml; 192 pmap_pinit_pml5(efi_pmltop_page); 193 } else { 194 efi_pml4 = pml; 195 pmap_pinit_pml4(efi_pmltop_page); 196 } 197 198 for (i = 0, p = map; i < ndesc; i++, p = efi_next_descriptor(p, 199 descsz)) { 200 if ((p->md_attr & EFI_MD_ATTR_RT) == 0) 201 continue; 202 if (p->md_virt != 0 && p->md_virt != p->md_phys) { 203 if (bootverbose) 204 printf("EFI Runtime entry %d is mapped\n", i); 205 goto fail; 206 } 207 if ((p->md_phys & EFI_PAGE_MASK) != 0) { 208 if (bootverbose) 209 printf("EFI Runtime entry %d is not aligned\n", 210 i); 211 goto fail; 212 } 213 if (p->md_phys + p->md_pages * EFI_PAGE_SIZE < p->md_phys || 214 p->md_phys + p->md_pages * EFI_PAGE_SIZE >= 215 VM_MAXUSER_ADDRESS) { 216 printf("EFI Runtime entry %d is not in mappable for RT:" 217 "base %#016jx %#jx pages\n", 218 i, (uintmax_t)p->md_phys, 219 (uintmax_t)p->md_pages); 220 goto fail; 221 } 222 if ((p->md_attr & EFI_MD_ATTR_WB) != 0) 223 mode = VM_MEMATTR_WRITE_BACK; 224 else if ((p->md_attr & EFI_MD_ATTR_WT) != 0) 225 mode = VM_MEMATTR_WRITE_THROUGH; 226 else if ((p->md_attr & EFI_MD_ATTR_WC) != 0) 227 mode = VM_MEMATTR_WRITE_COMBINING; 228 else if ((p->md_attr & EFI_MD_ATTR_WP) != 0) 229 mode = VM_MEMATTR_WRITE_PROTECTED; 230 else if ((p->md_attr & EFI_MD_ATTR_UC) != 0) 231 mode = VM_MEMATTR_UNCACHEABLE; 232 else { 233 if (bootverbose) 234 printf("EFI Runtime entry %d mapping " 235 "attributes unsupported\n", i); 236 mode = VM_MEMATTR_UNCACHEABLE; 237 } 238 bits = pmap_cache_bits(kernel_pmap, mode, FALSE) | X86_PG_RW | 239 X86_PG_V; 240 VM_OBJECT_WLOCK(obj_1t1_pt); 241 for (va = p->md_phys, idx = 0; idx < p->md_pages; idx++, 242 va += PAGE_SIZE) { 243 pte = efi_1t1_pte(va); 244 pte_store(pte, va | bits); 245 246 m = PHYS_TO_VM_PAGE(va); 247 if (m != NULL && VM_PAGE_TO_PHYS(m) == 0) { 248 vm_page_init_page(m, va, -1); 249 m->order = VM_NFREEORDER + 1; /* invalid */ 250 m->pool = VM_NFREEPOOL + 1; /* invalid */ 251 pmap_page_set_memattr_noflush(m, mode); 252 } 253 } 254 VM_OBJECT_WUNLOCK(obj_1t1_pt); 255 } 256 257 return (true); 258 259 fail: 260 efi_destroy_1t1_map(); 261 return (false); 262 } 263 264 /* 265 * Create an environment for the EFI runtime code call. The most 266 * important part is creating the required 1:1 physical->virtual 267 * mappings for the runtime segments. To do that, we manually create 268 * page table which unmap userspace but gives correct kernel mapping. 269 * The 1:1 mappings for runtime segments usually occupy low 4G of the 270 * physical address map. 271 * 272 * The 1:1 mappings were chosen over the SetVirtualAddressMap() EFI RT 273 * service, because there are some BIOSes which fail to correctly 274 * relocate itself on the call, requiring both 1:1 and virtual 275 * mapping. As result, we must provide 1:1 mapping anyway, so no 276 * reason to bother with the virtual map, and no need to add a 277 * complexity into loader. 278 * 279 * There is no need to disable interrupts around the change of %cr3, 280 * the kernel mappings are correct, while we only grabbed the 281 * userspace portion of VA. Interrupts handlers must not access 282 * userspace. Having interrupts enabled fixes the issue with 283 * firmware/SMM long operation, which would negatively affect IPIs, 284 * esp. TLB shootdown requests. 285 */ 286 int 287 efi_arch_enter(void) 288 { 289 pmap_t curpmap; 290 uint64_t cr3; 291 292 curpmap = PCPU_GET(curpmap); 293 PMAP_LOCK_ASSERT(curpmap, MA_OWNED); 294 curthread->td_md.md_efirt_dis_pf = vm_fault_disable_pagefaults(); 295 296 /* 297 * IPI TLB shootdown handler invltlb_pcid_handler() reloads 298 * %cr3 from the curpmap->pm_cr3, which would disable runtime 299 * segments mappings. Block the handler's action by setting 300 * curpmap to impossible value. See also comment in 301 * pmap.c:pmap_activate_sw(). 302 */ 303 if (pmap_pcid_enabled && !invpcid_works) 304 PCPU_SET(curpmap, NULL); 305 306 cr3 = VM_PAGE_TO_PHYS(efi_pmltop_page); 307 if (pmap_pcid_enabled) 308 cr3 |= pmap_get_pcid(curpmap); 309 load_cr3(cr3); 310 /* 311 * If PCID is enabled, the clear CR3_PCID_SAVE bit in the loaded %cr3 312 * causes TLB invalidation. 313 */ 314 if (!pmap_pcid_enabled) 315 invltlb(); 316 return (0); 317 } 318 319 void 320 efi_arch_leave(void) 321 { 322 pmap_t curpmap; 323 uint64_t cr3; 324 325 curpmap = &curproc->p_vmspace->vm_pmap; 326 cr3 = curpmap->pm_cr3; 327 if (pmap_pcid_enabled) { 328 cr3 |= pmap_get_pcid(curpmap); 329 if (!invpcid_works) 330 PCPU_SET(curpmap, curpmap); 331 } 332 load_cr3(cr3); 333 if (!pmap_pcid_enabled) 334 invltlb(); 335 vm_fault_enable_pagefaults(curthread->td_md.md_efirt_dis_pf); 336 } 337 338 /* XXX debug stuff */ 339 static int 340 efi_time_sysctl_handler(SYSCTL_HANDLER_ARGS) 341 { 342 struct efi_tm tm; 343 int error, val; 344 345 val = 0; 346 error = sysctl_handle_int(oidp, &val, 0, req); 347 if (error != 0 || req->newptr == NULL) 348 return (error); 349 error = efi_get_time(&tm); 350 if (error == 0) { 351 uprintf("EFI reports: Year %d Month %d Day %d Hour %d Min %d " 352 "Sec %d\n", tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, 353 tm.tm_min, tm.tm_sec); 354 } 355 return (error); 356 } 357 358 SYSCTL_PROC(_debug, OID_AUTO, efi_time, 359 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, 0, 360 efi_time_sysctl_handler, "I", 361 ""); 362