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 * $FreeBSD: head/sys/amd64/amd64/efirt.c 307391 2016-10-16 06:07:43Z kib $ 32 */ 33 34 #include <sys/param.h> 35 #include <sys/efi.h> 36 #include <sys/kernel.h> 37 #include <sys/linker.h> 38 #include <sys/lock.h> 39 #include <sys/module.h> 40 #include <sys/proc.h> 41 #include <sys/sched.h> 42 #include <sys/sysctl.h> 43 #include <sys/systm.h> 44 #include <sys/proc.h> 45 #include <sys/thread.h> 46 #include <sys/globaldata.h> 47 48 #include <vm/vm.h> 49 #include <vm/pmap.h> 50 #include <vm/vm_map.h> 51 #include <vm/vm_object.h> 52 #include <vm/vm_param.h> 53 #include <vm/vm_page.h> 54 #include <vm/vm_pager.h> 55 #include <vm/vm_extern.h> 56 57 #include <vm/vm_page2.h> 58 #include <sys/thread2.h> 59 60 #include <machine/efi.h> 61 #include <machine/metadata.h> 62 #include <machine/md_var.h> 63 #include <machine/smp.h> 64 #include <machine/vmparam.h> 65 66 static struct efi_systbl *efi_systbl; 67 static struct efi_cfgtbl *efi_cfgtbl; 68 static struct efi_rt *efi_runtime; 69 70 static int efi_status2err[25] = { 71 0, /* EFI_SUCCESS */ 72 ENOEXEC, /* EFI_LOAD_ERROR */ 73 EINVAL, /* EFI_INVALID_PARAMETER */ 74 ENOSYS, /* EFI_UNSUPPORTED */ 75 EMSGSIZE, /* EFI_BAD_BUFFER_SIZE */ 76 EOVERFLOW, /* EFI_BUFFER_TOO_SMALL */ 77 EBUSY, /* EFI_NOT_READY */ 78 EIO, /* EFI_DEVICE_ERROR */ 79 EROFS, /* EFI_WRITE_PROTECTED */ 80 EAGAIN, /* EFI_OUT_OF_RESOURCES */ 81 EIO, /* EFI_VOLUME_CORRUPTED */ 82 ENOSPC, /* EFI_VOLUME_FULL */ 83 ENXIO, /* EFI_NO_MEDIA */ 84 ESTALE, /* EFI_MEDIA_CHANGED */ 85 ENOENT, /* EFI_NOT_FOUND */ 86 EACCES, /* EFI_ACCESS_DENIED */ 87 ETIMEDOUT, /* EFI_NO_RESPONSE */ 88 EADDRNOTAVAIL, /* EFI_NO_MAPPING */ 89 ETIMEDOUT, /* EFI_TIMEOUT */ 90 EDOOFUS, /* EFI_NOT_STARTED */ 91 EALREADY, /* EFI_ALREADY_STARTED */ 92 ECANCELED, /* EFI_ABORTED */ 93 EPROTO, /* EFI_ICMP_ERROR */ 94 EPROTO, /* EFI_TFTP_ERROR */ 95 EPROTO /* EFI_PROTOCOL_ERROR */ 96 }; 97 98 MALLOC_DEFINE(M_EFI, "efi", "EFI BIOS"); 99 100 static int 101 efi_status_to_errno(efi_status status) 102 { 103 u_long code; 104 105 code = status & 0x3ffffffffffffffful; 106 return (code < nitems(efi_status2err) ? efi_status2err[code] : EDOOFUS); 107 } 108 109 static struct lock efi_lock; 110 static struct lock resettodr_lock; 111 static mcontext_t efi_ctx; 112 static struct vmspace *efi_savevm; 113 static struct vmspace *efi_vmspace; 114 static vm_object_t efi_obj; 115 static struct efi_md *efi_map; 116 static int efi_ndesc; 117 static int efi_descsz; 118 119 static void 120 efi_destroy_1t1_map(void) 121 { 122 vm_object_t obj; 123 vm_page_t m; 124 125 if ((obj = efi_obj) != NULL) { 126 efi_obj = NULL; 127 vm_object_hold(obj); 128 vm_object_reference_locked(obj); /* match deallocate */ 129 } 130 if (efi_vmspace) { 131 pmap_remove_pages(vmspace_pmap(efi_vmspace), 132 VM_MIN_USER_ADDRESS, VM_MAX_USER_ADDRESS); 133 vm_map_remove(&efi_vmspace->vm_map, 134 VM_MIN_USER_ADDRESS, 135 VM_MAX_USER_ADDRESS); 136 vmspace_rel(efi_vmspace); 137 efi_vmspace = NULL; 138 } 139 if (obj) { 140 while ((m = RB_ROOT(&obj->rb_memq)) != NULL) { 141 vm_page_busy_wait(m, FALSE, "efipg"); 142 vm_page_unwire(m, 1); 143 m->flags &= ~(PG_MAPPED | PG_WRITEABLE); 144 cdev_pager_free_page(obj, m); 145 kfree(m, M_EFI); 146 } 147 vm_object_drop(obj); 148 vm_object_deallocate(obj); 149 } 150 } 151 152 static int 153 efi_pg_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot, 154 vm_ooffset_t foff, struct ucred *cred, u_short *color) 155 { 156 *color = 0; 157 return 0; 158 } 159 160 static void 161 efi_pg_dtor(void *handle) 162 { 163 } 164 165 static int 166 efi_pg_fault(vm_object_t obj, vm_ooffset_t offset, int prot, vm_page_t *mres) 167 { 168 vm_page_t m; 169 170 m = *mres; 171 if ((m->flags & PG_FICTITIOUS) == 0) { 172 *mres = NULL; 173 vm_page_remove(m); 174 vm_page_free(m); 175 m = NULL; 176 } 177 if (m == NULL) { 178 kprintf("efi_pg_fault: unmapped pg @%016jx\n", offset); 179 return VM_PAGER_ERROR; 180 } 181 182 /* 183 * Shouldn't get hit, we pre-loaded all the pages. 184 */ 185 kprintf("efi_pg_fault: ok %p/%p @%016jx m=%016jx,%016jx\n", 186 obj, efi_obj, offset, m->pindex, m->phys_addr); 187 188 return VM_PAGER_OK; 189 } 190 191 static struct cdev_pager_ops efi_pager_ops = { 192 .cdev_pg_fault = efi_pg_fault, 193 .cdev_pg_ctor = efi_pg_ctor, 194 .cdev_pg_dtor = efi_pg_dtor 195 }; 196 197 static bool 198 efi_create_1t1_map(struct efi_md *map, int ndesc, int descsz) 199 { 200 vm_page_t m; 201 struct efi_md *p; 202 int i; 203 int count; 204 int result; 205 206 efi_map = map; 207 efi_ndesc = ndesc; 208 efi_descsz = descsz; 209 210 efi_vmspace = vmspace_alloc(VM_MIN_USER_ADDRESS, VM_MAX_USER_ADDRESS); 211 pmap_pinit2(vmspace_pmap(efi_vmspace)); 212 efi_obj = cdev_pager_allocate(NULL, OBJT_MGTDEVICE, &efi_pager_ops, 213 VM_MAX_USER_ADDRESS, 214 VM_PROT_READ | VM_PROT_WRITE, 215 0, proc0.p_ucred); 216 vm_object_hold(efi_obj); 217 218 count = vm_map_entry_reserve(MAP_RESERVE_COUNT); 219 vm_map_lock(&efi_vmspace->vm_map); 220 result = vm_map_insert(&efi_vmspace->vm_map, &count, efi_obj, NULL, 221 0, 0, VM_MAX_USER_ADDRESS, 222 VM_MAPTYPE_NORMAL, 223 VM_SUBSYS_EFI, 224 VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE, 225 VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE, 226 0); 227 vm_map_unlock(&efi_vmspace->vm_map); 228 if (result != KERN_SUCCESS) 229 goto fail; 230 231 for (i = 0, p = map; 232 i < ndesc; i++, p = efi_next_descriptor(p, descsz)) { 233 vm_offset_t va; 234 uint64_t idx; 235 int mode; 236 237 if ((p->md_attr & EFI_MD_ATTR_RT) == 0) 238 continue; 239 if (p->md_virt != NULL) { 240 if (bootverbose) 241 kprintf("EFI Runtime entry %d is mapped\n", i); 242 goto fail; 243 } 244 if ((p->md_phys & EFI_PAGE_MASK) != 0) { 245 if (bootverbose) 246 kprintf("EFI Runtime entry %d is not aligned\n", 247 i); 248 goto fail; 249 } 250 if (p->md_phys + p->md_pages * EFI_PAGE_SIZE < p->md_phys || 251 p->md_phys + p->md_pages * EFI_PAGE_SIZE >= 252 VM_MAX_USER_ADDRESS) { 253 kprintf("EFI Runtime entry %d is not in mappable for RT:" 254 "base %#016jx %#jx pages\n", 255 i, (uintmax_t)p->md_phys, 256 (uintmax_t)p->md_pages); 257 goto fail; 258 } 259 260 if ((p->md_attr & EFI_MD_ATTR_WB) != 0) 261 mode = VM_MEMATTR_WRITE_BACK; 262 else if ((p->md_attr & EFI_MD_ATTR_WT) != 0) 263 mode = VM_MEMATTR_WRITE_THROUGH; 264 else if ((p->md_attr & EFI_MD_ATTR_WC) != 0) 265 mode = VM_MEMATTR_WRITE_COMBINING; 266 else if ((p->md_attr & EFI_MD_ATTR_WP) != 0) 267 mode = VM_MEMATTR_WRITE_PROTECTED; 268 else if ((p->md_attr & EFI_MD_ATTR_UC) != 0) 269 mode = VM_MEMATTR_UNCACHEABLE; 270 else { 271 if (bootverbose) 272 kprintf("EFI Runtime entry %d mapping " 273 "attributes unsupported\n", i); 274 mode = VM_MEMATTR_UNCACHEABLE; 275 } 276 277 if (bootverbose) { 278 kprintf("efirt: map %016jx-%016jx\n", 279 p->md_phys, 280 p->md_phys + IDX_TO_OFF(p->md_pages)); 281 } 282 283 for (va = p->md_phys, idx = 0; idx < p->md_pages; idx++, 284 va += PAGE_SIZE) { 285 m = kmalloc(sizeof(*m), M_EFI, M_WAITOK | M_ZERO); 286 /*m->flags |= PG_WRITEABLE;*/ 287 vm_page_initfake(m, va, mode); /* va is phys addr */ 288 m->valid = VM_PAGE_BITS_ALL; 289 m->dirty = m->valid; 290 vm_page_insert(m, efi_obj, OFF_TO_IDX(va)); 291 vm_page_wakeup(m); 292 } 293 } 294 vm_object_drop(efi_obj); 295 vm_map_entry_release(count); 296 297 return true; 298 299 fail: 300 vm_object_drop(efi_obj); 301 vm_map_entry_release(count); 302 efi_destroy_1t1_map(); 303 304 return false; 305 } 306 307 /* 308 * Create an environment for the EFI runtime code call. The most 309 * important part is creating the required 1:1 physical->virtual 310 * mappings for the runtime segments. To do that, we manually create 311 * page table which unmap userspace but gives correct kernel mapping. 312 * The 1:1 mappings for runtime segments usually occupy low 4G of the 313 * physical address map. 314 * 315 * The 1:1 mappings were chosen over the SetVirtualAddressMap() EFI RT 316 * service, because there are some BIOSes which fail to correctly 317 * relocate itself on the call, requiring both 1:1 and virtual 318 * mapping. As result, we must provide 1:1 mapping anyway, so no 319 * reason to bother with the virtual map, and no need to add a 320 * complexity into loader. 321 * 322 * The fpu_kern_enter() call allows firmware to use FPU, as mandated 323 * by the specification. In particular, CR0.TS bit is cleared. Also 324 * it enters critical section, giving us neccessary protection against 325 * context switch. 326 * 327 * There is no need to disable interrupts around the change of %cr3, 328 * the kernel mappings are correct, while we only grabbed the 329 * userspace portion of VA. Interrupts handlers must not access 330 * userspace. Having interrupts enabled fixes the issue with 331 * firmware/SMM long operation, which would negatively affect IPIs, 332 * esp. TLB shootdown requests. 333 */ 334 static int 335 efi_enter(void) 336 { 337 thread_t td = curthread; 338 339 if (efi_runtime == NULL) 340 return (ENXIO); 341 lockmgr(&efi_lock, LK_EXCLUSIVE); 342 efi_savevm = td->td_lwp->lwp_vmspace; 343 pmap_setlwpvm(td->td_lwp, efi_vmspace); 344 npxpush(&efi_ctx); 345 cpu_invltlb(); 346 347 return (0); 348 } 349 350 static void 351 efi_leave(void) 352 { 353 thread_t td = curthread; 354 355 pmap_setlwpvm(td->td_lwp, efi_savevm); 356 npxpop(&efi_ctx); 357 cpu_invltlb(); 358 efi_savevm = NULL; 359 lockmgr(&efi_lock, LK_RELEASE); 360 } 361 362 static int 363 efi_init(void) 364 { 365 struct efi_map_header *efihdr; 366 struct efi_md *map; 367 caddr_t kmdp; 368 size_t efisz; 369 370 lockinit(&efi_lock, "efi", 0, LK_CANRECURSE); 371 lockinit(&resettodr_lock, "efitodr", 0, LK_CANRECURSE); 372 373 if (efi_systbl_phys == 0) { 374 if (bootverbose) 375 kprintf("EFI systbl not available\n"); 376 return (ENXIO); 377 } 378 efi_systbl = (struct efi_systbl *)PHYS_TO_DMAP(efi_systbl_phys); 379 if (efi_systbl->st_hdr.th_sig != EFI_SYSTBL_SIG) { 380 efi_systbl = NULL; 381 if (bootverbose) 382 kprintf("EFI systbl signature invalid\n"); 383 return (ENXIO); 384 } 385 efi_cfgtbl = (efi_systbl->st_cfgtbl == 0) ? NULL : 386 (struct efi_cfgtbl *)efi_systbl->st_cfgtbl; 387 if (efi_cfgtbl == NULL) { 388 if (bootverbose) 389 kprintf("EFI config table is not present\n"); 390 } 391 392 kmdp = preload_search_by_type("elf kernel"); 393 if (kmdp == NULL) 394 kmdp = preload_search_by_type("elf64 kernel"); 395 efihdr = (struct efi_map_header *)preload_search_info(kmdp, 396 MODINFO_METADATA | MODINFOMD_EFI_MAP); 397 if (efihdr == NULL) { 398 if (bootverbose) 399 kprintf("EFI map is not present\n"); 400 return (ENXIO); 401 } 402 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf; 403 map = (struct efi_md *)((uint8_t *)efihdr + efisz); 404 if (efihdr->descriptor_size == 0) 405 return (ENOMEM); 406 407 if (!efi_create_1t1_map(map, efihdr->memory_size / 408 efihdr->descriptor_size, efihdr->descriptor_size)) { 409 if (bootverbose) 410 kprintf("EFI cannot create runtime map\n"); 411 return (ENOMEM); 412 } 413 414 efi_runtime = (efi_systbl->st_rt == 0) ? NULL : 415 (struct efi_rt *)efi_systbl->st_rt; 416 if (efi_runtime == NULL) { 417 if (bootverbose) 418 kprintf("EFI runtime services table is not present\n"); 419 efi_destroy_1t1_map(); 420 return (ENXIO); 421 } 422 423 return (0); 424 } 425 426 static void 427 efi_uninit(void) 428 { 429 efi_destroy_1t1_map(); 430 431 efi_systbl = NULL; 432 efi_cfgtbl = NULL; 433 efi_runtime = NULL; 434 435 lockuninit(&efi_lock); 436 lockuninit(&resettodr_lock); 437 } 438 439 int 440 efi_get_table(struct uuid *uuid, void **ptr) 441 { 442 struct efi_cfgtbl *ct; 443 u_long count; 444 445 if (efi_cfgtbl == NULL) 446 return (ENXIO); 447 count = efi_systbl->st_entries; 448 ct = efi_cfgtbl; 449 while (count--) { 450 if (!bcmp(&ct->ct_uuid, uuid, sizeof(*uuid))) { 451 *ptr = (void *)PHYS_TO_DMAP(ct->ct_data); 452 return (0); 453 } 454 ct++; 455 } 456 return (ENOENT); 457 } 458 459 char SaveCode[1024]; 460 461 int 462 efi_get_time_locked(struct efi_tm *tm) 463 { 464 efi_status status; 465 int error; 466 467 KKASSERT(lockowned(&resettodr_lock) != 0); 468 error = efi_enter(); 469 if (error != 0) 470 return (error); 471 status = efi_runtime->rt_gettime(tm, NULL); 472 efi_leave(); 473 error = efi_status_to_errno(status); 474 475 return (error); 476 } 477 478 int 479 efi_get_time(struct efi_tm *tm) 480 { 481 int error; 482 483 if (efi_runtime == NULL) 484 return (ENXIO); 485 lockmgr(&resettodr_lock, LK_EXCLUSIVE); 486 error = efi_get_time_locked(tm); 487 lockmgr(&resettodr_lock, LK_RELEASE); 488 489 return (error); 490 } 491 492 int 493 efi_reset_system(void) 494 { 495 int error; 496 497 error = efi_enter(); 498 if (error != 0) 499 return (error); 500 efi_runtime->rt_reset(EFI_RESET_WARM, 0, 0, NULL); 501 efi_leave(); 502 return (EIO); 503 } 504 505 int 506 efi_set_time_locked(struct efi_tm *tm) 507 { 508 efi_status status; 509 int error; 510 511 KKASSERT(lockowned(&resettodr_lock) != 0); 512 error = efi_enter(); 513 if (error != 0) 514 return (error); 515 status = efi_runtime->rt_settime(tm); 516 efi_leave(); 517 error = efi_status_to_errno(status); 518 return (error); 519 } 520 521 int 522 efi_set_time(struct efi_tm *tm) 523 { 524 int error; 525 526 if (efi_runtime == NULL) 527 return (ENXIO); 528 lockmgr(&resettodr_lock, LK_EXCLUSIVE); 529 error = efi_set_time_locked(tm); 530 lockmgr(&resettodr_lock, LK_RELEASE); 531 return (error); 532 } 533 534 int 535 efi_var_get(efi_char *name, struct uuid *vendor, uint32_t *attrib, 536 size_t *datasize, void *data) 537 { 538 efi_status status; 539 int error; 540 541 error = efi_enter(); 542 if (error != 0) 543 return (error); 544 status = efi_runtime->rt_getvar(name, vendor, attrib, datasize, data); 545 efi_leave(); 546 error = efi_status_to_errno(status); 547 return (error); 548 } 549 550 int 551 efi_var_nextname(size_t *namesize, efi_char *name, struct uuid *vendor) 552 { 553 efi_status status; 554 int error; 555 556 error = efi_enter(); 557 if (error != 0) 558 return (error); 559 status = efi_runtime->rt_scanvar(namesize, name, vendor); 560 efi_leave(); 561 error = efi_status_to_errno(status); 562 return (error); 563 } 564 565 int 566 efi_var_set(efi_char *name, struct uuid *vendor, uint32_t attrib, 567 size_t datasize, void *data) 568 { 569 efi_status status; 570 int error; 571 572 error = efi_enter(); 573 if (error != 0) 574 return (error); 575 status = efi_runtime->rt_setvar(name, vendor, attrib, datasize, data); 576 efi_leave(); 577 error = efi_status_to_errno(status); 578 return (error); 579 } 580 581 static int 582 efirt_modevents(module_t m, int event, void *arg __unused) 583 { 584 585 switch (event) { 586 case MOD_LOAD: 587 return (efi_init()); 588 589 case MOD_UNLOAD: 590 efi_uninit(); 591 return (0); 592 593 case MOD_SHUTDOWN: 594 return (0); 595 596 default: 597 return (EOPNOTSUPP); 598 } 599 } 600 601 static moduledata_t efirt_moddata = { 602 .name = "efirt", 603 .evhand = efirt_modevents, 604 .priv = NULL, 605 }; 606 607 DECLARE_MODULE(efirt, efirt_moddata, SI_SUB_DRIVERS, SI_ORDER_ANY); 608 MODULE_VERSION(efirt, 1); 609 610 611 /* XXX debug stuff */ 612 static int 613 efi_time_sysctl_handler(SYSCTL_HANDLER_ARGS) 614 { 615 struct efi_tm tm; 616 int error, val; 617 618 val = 0; 619 error = sysctl_handle_int(oidp, &val, 0, req); 620 if (error != 0 || req->newptr == NULL) 621 return (error); 622 error = efi_get_time(&tm); 623 if (error == 0) { 624 uprintf("EFI reports: Year %d Month %d Day %d Hour %d Min %d " 625 "Sec %d\n", tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, 626 tm.tm_min, tm.tm_sec); 627 } 628 return (error); 629 } 630 631 SYSCTL_PROC(_debug, OID_AUTO, efi_time, CTLTYPE_INT | CTLFLAG_RW, NULL, 0, 632 efi_time_sysctl_handler, "I", ""); 633