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