1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * x86_64 specific EFI support functions 4 * Based on Extensible Firmware Interface Specification version 1.0 5 * 6 * Copyright (C) 2005-2008 Intel Co. 7 * Fenghua Yu <fenghua.yu@intel.com> 8 * Bibo Mao <bibo.mao@intel.com> 9 * Chandramouli Narayanan <mouli@linux.intel.com> 10 * Huang Ying <ying.huang@intel.com> 11 * 12 * Code to convert EFI to E820 map has been implemented in elilo bootloader 13 * based on a EFI patch by Edgar Hucek. Based on the E820 map, the page table 14 * is setup appropriately for EFI runtime code. 15 * - mouli 06/14/2007. 16 * 17 */ 18 19 #define pr_fmt(fmt) "efi: " fmt 20 21 #include <linux/kernel.h> 22 #include <linux/init.h> 23 #include <linux/mm.h> 24 #include <linux/types.h> 25 #include <linux/spinlock.h> 26 #include <linux/memblock.h> 27 #include <linux/ioport.h> 28 #include <linux/mc146818rtc.h> 29 #include <linux/efi.h> 30 #include <linux/export.h> 31 #include <linux/uaccess.h> 32 #include <linux/io.h> 33 #include <linux/reboot.h> 34 #include <linux/slab.h> 35 #include <linux/ucs2_string.h> 36 #include <linux/mem_encrypt.h> 37 #include <linux/sched/task.h> 38 39 #include <asm/setup.h> 40 #include <asm/page.h> 41 #include <asm/e820/api.h> 42 #include <asm/tlbflush.h> 43 #include <asm/proto.h> 44 #include <asm/efi.h> 45 #include <asm/cacheflush.h> 46 #include <asm/fixmap.h> 47 #include <asm/realmode.h> 48 #include <asm/time.h> 49 #include <asm/pgalloc.h> 50 #include <asm/sev-es.h> 51 52 /* 53 * We allocate runtime services regions top-down, starting from -4G, i.e. 54 * 0xffff_ffff_0000_0000 and limit EFI VA mapping space to 64G. 55 */ 56 static u64 efi_va = EFI_VA_START; 57 58 struct efi_scratch efi_scratch; 59 60 EXPORT_SYMBOL_GPL(efi_mm); 61 62 /* 63 * We need our own copy of the higher levels of the page tables 64 * because we want to avoid inserting EFI region mappings (EFI_VA_END 65 * to EFI_VA_START) into the standard kernel page tables. Everything 66 * else can be shared, see efi_sync_low_kernel_mappings(). 67 * 68 * We don't want the pgd on the pgd_list and cannot use pgd_alloc() for the 69 * allocation. 70 */ 71 int __init efi_alloc_page_tables(void) 72 { 73 pgd_t *pgd, *efi_pgd; 74 p4d_t *p4d; 75 pud_t *pud; 76 gfp_t gfp_mask; 77 78 gfp_mask = GFP_KERNEL | __GFP_ZERO; 79 efi_pgd = (pgd_t *)__get_free_pages(gfp_mask, PGD_ALLOCATION_ORDER); 80 if (!efi_pgd) 81 return -ENOMEM; 82 83 pgd = efi_pgd + pgd_index(EFI_VA_END); 84 p4d = p4d_alloc(&init_mm, pgd, EFI_VA_END); 85 if (!p4d) { 86 free_page((unsigned long)efi_pgd); 87 return -ENOMEM; 88 } 89 90 pud = pud_alloc(&init_mm, p4d, EFI_VA_END); 91 if (!pud) { 92 if (pgtable_l5_enabled()) 93 free_page((unsigned long) pgd_page_vaddr(*pgd)); 94 free_pages((unsigned long)efi_pgd, PGD_ALLOCATION_ORDER); 95 return -ENOMEM; 96 } 97 98 efi_mm.pgd = efi_pgd; 99 mm_init_cpumask(&efi_mm); 100 init_new_context(NULL, &efi_mm); 101 102 return 0; 103 } 104 105 /* 106 * Add low kernel mappings for passing arguments to EFI functions. 107 */ 108 void efi_sync_low_kernel_mappings(void) 109 { 110 unsigned num_entries; 111 pgd_t *pgd_k, *pgd_efi; 112 p4d_t *p4d_k, *p4d_efi; 113 pud_t *pud_k, *pud_efi; 114 pgd_t *efi_pgd = efi_mm.pgd; 115 116 /* 117 * We can share all PGD entries apart from the one entry that 118 * covers the EFI runtime mapping space. 119 * 120 * Make sure the EFI runtime region mappings are guaranteed to 121 * only span a single PGD entry and that the entry also maps 122 * other important kernel regions. 123 */ 124 MAYBE_BUILD_BUG_ON(pgd_index(EFI_VA_END) != pgd_index(MODULES_END)); 125 MAYBE_BUILD_BUG_ON((EFI_VA_START & PGDIR_MASK) != 126 (EFI_VA_END & PGDIR_MASK)); 127 128 pgd_efi = efi_pgd + pgd_index(PAGE_OFFSET); 129 pgd_k = pgd_offset_k(PAGE_OFFSET); 130 131 num_entries = pgd_index(EFI_VA_END) - pgd_index(PAGE_OFFSET); 132 memcpy(pgd_efi, pgd_k, sizeof(pgd_t) * num_entries); 133 134 /* 135 * As with PGDs, we share all P4D entries apart from the one entry 136 * that covers the EFI runtime mapping space. 137 */ 138 BUILD_BUG_ON(p4d_index(EFI_VA_END) != p4d_index(MODULES_END)); 139 BUILD_BUG_ON((EFI_VA_START & P4D_MASK) != (EFI_VA_END & P4D_MASK)); 140 141 pgd_efi = efi_pgd + pgd_index(EFI_VA_END); 142 pgd_k = pgd_offset_k(EFI_VA_END); 143 p4d_efi = p4d_offset(pgd_efi, 0); 144 p4d_k = p4d_offset(pgd_k, 0); 145 146 num_entries = p4d_index(EFI_VA_END); 147 memcpy(p4d_efi, p4d_k, sizeof(p4d_t) * num_entries); 148 149 /* 150 * We share all the PUD entries apart from those that map the 151 * EFI regions. Copy around them. 152 */ 153 BUILD_BUG_ON((EFI_VA_START & ~PUD_MASK) != 0); 154 BUILD_BUG_ON((EFI_VA_END & ~PUD_MASK) != 0); 155 156 p4d_efi = p4d_offset(pgd_efi, EFI_VA_END); 157 p4d_k = p4d_offset(pgd_k, EFI_VA_END); 158 pud_efi = pud_offset(p4d_efi, 0); 159 pud_k = pud_offset(p4d_k, 0); 160 161 num_entries = pud_index(EFI_VA_END); 162 memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries); 163 164 pud_efi = pud_offset(p4d_efi, EFI_VA_START); 165 pud_k = pud_offset(p4d_k, EFI_VA_START); 166 167 num_entries = PTRS_PER_PUD - pud_index(EFI_VA_START); 168 memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries); 169 } 170 171 /* 172 * Wrapper for slow_virt_to_phys() that handles NULL addresses. 173 */ 174 static inline phys_addr_t 175 virt_to_phys_or_null_size(void *va, unsigned long size) 176 { 177 phys_addr_t pa; 178 179 if (!va) 180 return 0; 181 182 if (virt_addr_valid(va)) 183 return virt_to_phys(va); 184 185 pa = slow_virt_to_phys(va); 186 187 /* check if the object crosses a page boundary */ 188 if (WARN_ON((pa ^ (pa + size - 1)) & PAGE_MASK)) 189 return 0; 190 191 return pa; 192 } 193 194 #define virt_to_phys_or_null(addr) \ 195 virt_to_phys_or_null_size((addr), sizeof(*(addr))) 196 197 int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages) 198 { 199 unsigned long pfn, text, pf, rodata; 200 struct page *page; 201 unsigned npages; 202 pgd_t *pgd = efi_mm.pgd; 203 204 /* 205 * It can happen that the physical address of new_memmap lands in memory 206 * which is not mapped in the EFI page table. Therefore we need to go 207 * and ident-map those pages containing the map before calling 208 * phys_efi_set_virtual_address_map(). 209 */ 210 pfn = pa_memmap >> PAGE_SHIFT; 211 pf = _PAGE_NX | _PAGE_RW | _PAGE_ENC; 212 if (kernel_map_pages_in_pgd(pgd, pfn, pa_memmap, num_pages, pf)) { 213 pr_err("Error ident-mapping new memmap (0x%lx)!\n", pa_memmap); 214 return 1; 215 } 216 217 /* 218 * Certain firmware versions are way too sentimential and still believe 219 * they are exclusive and unquestionable owners of the first physical page, 220 * even though they explicitly mark it as EFI_CONVENTIONAL_MEMORY 221 * (but then write-access it later during SetVirtualAddressMap()). 222 * 223 * Create a 1:1 mapping for this page, to avoid triple faults during early 224 * boot with such firmware. We are free to hand this page to the BIOS, 225 * as trim_bios_range() will reserve the first page and isolate it away 226 * from memory allocators anyway. 227 */ 228 if (kernel_map_pages_in_pgd(pgd, 0x0, 0x0, 1, pf)) { 229 pr_err("Failed to create 1:1 mapping for the first page!\n"); 230 return 1; 231 } 232 233 /* 234 * When SEV-ES is active, the GHCB as set by the kernel will be used 235 * by firmware. Create a 1:1 unencrypted mapping for each GHCB. 236 */ 237 if (sev_es_efi_map_ghcbs(pgd)) { 238 pr_err("Failed to create 1:1 mapping for the GHCBs!\n"); 239 return 1; 240 } 241 242 /* 243 * When making calls to the firmware everything needs to be 1:1 244 * mapped and addressable with 32-bit pointers. Map the kernel 245 * text and allocate a new stack because we can't rely on the 246 * stack pointer being < 4GB. 247 */ 248 if (!efi_is_mixed()) 249 return 0; 250 251 page = alloc_page(GFP_KERNEL|__GFP_DMA32); 252 if (!page) { 253 pr_err("Unable to allocate EFI runtime stack < 4GB\n"); 254 return 1; 255 } 256 257 efi_scratch.phys_stack = page_to_phys(page + 1); /* stack grows down */ 258 259 npages = (_etext - _text) >> PAGE_SHIFT; 260 text = __pa(_text); 261 pfn = text >> PAGE_SHIFT; 262 263 pf = _PAGE_ENC; 264 if (kernel_map_pages_in_pgd(pgd, pfn, text, npages, pf)) { 265 pr_err("Failed to map kernel text 1:1\n"); 266 return 1; 267 } 268 269 npages = (__end_rodata - __start_rodata) >> PAGE_SHIFT; 270 rodata = __pa(__start_rodata); 271 pfn = rodata >> PAGE_SHIFT; 272 273 pf = _PAGE_NX | _PAGE_ENC; 274 if (kernel_map_pages_in_pgd(pgd, pfn, rodata, npages, pf)) { 275 pr_err("Failed to map kernel rodata 1:1\n"); 276 return 1; 277 } 278 279 return 0; 280 } 281 282 static void __init __map_region(efi_memory_desc_t *md, u64 va) 283 { 284 unsigned long flags = _PAGE_RW; 285 unsigned long pfn; 286 pgd_t *pgd = efi_mm.pgd; 287 288 /* 289 * EFI_RUNTIME_SERVICES_CODE regions typically cover PE/COFF 290 * executable images in memory that consist of both R-X and 291 * RW- sections, so we cannot apply read-only or non-exec 292 * permissions just yet. However, modern EFI systems provide 293 * a memory attributes table that describes those sections 294 * with the appropriate restricted permissions, which are 295 * applied in efi_runtime_update_mappings() below. All other 296 * regions can be mapped non-executable at this point, with 297 * the exception of boot services code regions, but those will 298 * be unmapped again entirely in efi_free_boot_services(). 299 */ 300 if (md->type != EFI_BOOT_SERVICES_CODE && 301 md->type != EFI_RUNTIME_SERVICES_CODE) 302 flags |= _PAGE_NX; 303 304 if (!(md->attribute & EFI_MEMORY_WB)) 305 flags |= _PAGE_PCD; 306 307 if (sev_active() && md->type != EFI_MEMORY_MAPPED_IO) 308 flags |= _PAGE_ENC; 309 310 pfn = md->phys_addr >> PAGE_SHIFT; 311 if (kernel_map_pages_in_pgd(pgd, pfn, va, md->num_pages, flags)) 312 pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n", 313 md->phys_addr, va); 314 } 315 316 void __init efi_map_region(efi_memory_desc_t *md) 317 { 318 unsigned long size = md->num_pages << PAGE_SHIFT; 319 u64 pa = md->phys_addr; 320 321 /* 322 * Make sure the 1:1 mappings are present as a catch-all for b0rked 323 * firmware which doesn't update all internal pointers after switching 324 * to virtual mode and would otherwise crap on us. 325 */ 326 __map_region(md, md->phys_addr); 327 328 /* 329 * Enforce the 1:1 mapping as the default virtual address when 330 * booting in EFI mixed mode, because even though we may be 331 * running a 64-bit kernel, the firmware may only be 32-bit. 332 */ 333 if (efi_is_mixed()) { 334 md->virt_addr = md->phys_addr; 335 return; 336 } 337 338 efi_va -= size; 339 340 /* Is PA 2M-aligned? */ 341 if (!(pa & (PMD_SIZE - 1))) { 342 efi_va &= PMD_MASK; 343 } else { 344 u64 pa_offset = pa & (PMD_SIZE - 1); 345 u64 prev_va = efi_va; 346 347 /* get us the same offset within this 2M page */ 348 efi_va = (efi_va & PMD_MASK) + pa_offset; 349 350 if (efi_va > prev_va) 351 efi_va -= PMD_SIZE; 352 } 353 354 if (efi_va < EFI_VA_END) { 355 pr_warn(FW_WARN "VA address range overflow!\n"); 356 return; 357 } 358 359 /* Do the VA map */ 360 __map_region(md, efi_va); 361 md->virt_addr = efi_va; 362 } 363 364 /* 365 * kexec kernel will use efi_map_region_fixed to map efi runtime memory ranges. 366 * md->virt_addr is the original virtual address which had been mapped in kexec 367 * 1st kernel. 368 */ 369 void __init efi_map_region_fixed(efi_memory_desc_t *md) 370 { 371 __map_region(md, md->phys_addr); 372 __map_region(md, md->virt_addr); 373 } 374 375 void __init parse_efi_setup(u64 phys_addr, u32 data_len) 376 { 377 efi_setup = phys_addr + sizeof(struct setup_data); 378 } 379 380 static int __init efi_update_mappings(efi_memory_desc_t *md, unsigned long pf) 381 { 382 unsigned long pfn; 383 pgd_t *pgd = efi_mm.pgd; 384 int err1, err2; 385 386 /* Update the 1:1 mapping */ 387 pfn = md->phys_addr >> PAGE_SHIFT; 388 err1 = kernel_map_pages_in_pgd(pgd, pfn, md->phys_addr, md->num_pages, pf); 389 if (err1) { 390 pr_err("Error while updating 1:1 mapping PA 0x%llx -> VA 0x%llx!\n", 391 md->phys_addr, md->virt_addr); 392 } 393 394 err2 = kernel_map_pages_in_pgd(pgd, pfn, md->virt_addr, md->num_pages, pf); 395 if (err2) { 396 pr_err("Error while updating VA mapping PA 0x%llx -> VA 0x%llx!\n", 397 md->phys_addr, md->virt_addr); 398 } 399 400 return err1 || err2; 401 } 402 403 static int __init efi_update_mem_attr(struct mm_struct *mm, efi_memory_desc_t *md) 404 { 405 unsigned long pf = 0; 406 407 if (md->attribute & EFI_MEMORY_XP) 408 pf |= _PAGE_NX; 409 410 if (!(md->attribute & EFI_MEMORY_RO)) 411 pf |= _PAGE_RW; 412 413 if (sev_active()) 414 pf |= _PAGE_ENC; 415 416 return efi_update_mappings(md, pf); 417 } 418 419 void __init efi_runtime_update_mappings(void) 420 { 421 efi_memory_desc_t *md; 422 423 /* 424 * Use the EFI Memory Attribute Table for mapping permissions if it 425 * exists, since it is intended to supersede EFI_PROPERTIES_TABLE. 426 */ 427 if (efi_enabled(EFI_MEM_ATTR)) { 428 efi_memattr_apply_permissions(NULL, efi_update_mem_attr); 429 return; 430 } 431 432 /* 433 * EFI_MEMORY_ATTRIBUTES_TABLE is intended to replace 434 * EFI_PROPERTIES_TABLE. So, use EFI_PROPERTIES_TABLE to update 435 * permissions only if EFI_MEMORY_ATTRIBUTES_TABLE is not 436 * published by the firmware. Even if we find a buggy implementation of 437 * EFI_MEMORY_ATTRIBUTES_TABLE, don't fall back to 438 * EFI_PROPERTIES_TABLE, because of the same reason. 439 */ 440 441 if (!efi_enabled(EFI_NX_PE_DATA)) 442 return; 443 444 for_each_efi_memory_desc(md) { 445 unsigned long pf = 0; 446 447 if (!(md->attribute & EFI_MEMORY_RUNTIME)) 448 continue; 449 450 if (!(md->attribute & EFI_MEMORY_WB)) 451 pf |= _PAGE_PCD; 452 453 if ((md->attribute & EFI_MEMORY_XP) || 454 (md->type == EFI_RUNTIME_SERVICES_DATA)) 455 pf |= _PAGE_NX; 456 457 if (!(md->attribute & EFI_MEMORY_RO) && 458 (md->type != EFI_RUNTIME_SERVICES_CODE)) 459 pf |= _PAGE_RW; 460 461 if (sev_active()) 462 pf |= _PAGE_ENC; 463 464 efi_update_mappings(md, pf); 465 } 466 } 467 468 void __init efi_dump_pagetable(void) 469 { 470 #ifdef CONFIG_EFI_PGT_DUMP 471 ptdump_walk_pgd_level(NULL, &efi_mm); 472 #endif 473 } 474 475 /* 476 * Makes the calling thread switch to/from efi_mm context. Can be used 477 * in a kernel thread and user context. Preemption needs to remain disabled 478 * while the EFI-mm is borrowed. mmgrab()/mmdrop() is not used because the mm 479 * can not change under us. 480 * It should be ensured that there are no concurent calls to this function. 481 */ 482 void efi_switch_mm(struct mm_struct *mm) 483 { 484 efi_scratch.prev_mm = current->active_mm; 485 current->active_mm = mm; 486 switch_mm(efi_scratch.prev_mm, mm, NULL); 487 } 488 489 static DEFINE_SPINLOCK(efi_runtime_lock); 490 491 /* 492 * DS and ES contain user values. We need to save them. 493 * The 32-bit EFI code needs a valid DS, ES, and SS. There's no 494 * need to save the old SS: __KERNEL_DS is always acceptable. 495 */ 496 #define __efi_thunk(func, ...) \ 497 ({ \ 498 unsigned short __ds, __es; \ 499 efi_status_t ____s; \ 500 \ 501 savesegment(ds, __ds); \ 502 savesegment(es, __es); \ 503 \ 504 loadsegment(ss, __KERNEL_DS); \ 505 loadsegment(ds, __KERNEL_DS); \ 506 loadsegment(es, __KERNEL_DS); \ 507 \ 508 ____s = efi64_thunk(efi.runtime->mixed_mode.func, __VA_ARGS__); \ 509 \ 510 loadsegment(ds, __ds); \ 511 loadsegment(es, __es); \ 512 \ 513 ____s ^= (____s & BIT(31)) | (____s & BIT_ULL(31)) << 32; \ 514 ____s; \ 515 }) 516 517 /* 518 * Switch to the EFI page tables early so that we can access the 1:1 519 * runtime services mappings which are not mapped in any other page 520 * tables. 521 * 522 * Also, disable interrupts because the IDT points to 64-bit handlers, 523 * which aren't going to function correctly when we switch to 32-bit. 524 */ 525 #define efi_thunk(func...) \ 526 ({ \ 527 efi_status_t __s; \ 528 \ 529 arch_efi_call_virt_setup(); \ 530 \ 531 __s = __efi_thunk(func); \ 532 \ 533 arch_efi_call_virt_teardown(); \ 534 \ 535 __s; \ 536 }) 537 538 static efi_status_t __init __no_sanitize_address 539 efi_thunk_set_virtual_address_map(unsigned long memory_map_size, 540 unsigned long descriptor_size, 541 u32 descriptor_version, 542 efi_memory_desc_t *virtual_map) 543 { 544 efi_status_t status; 545 unsigned long flags; 546 547 efi_sync_low_kernel_mappings(); 548 local_irq_save(flags); 549 550 efi_switch_mm(&efi_mm); 551 552 status = __efi_thunk(set_virtual_address_map, memory_map_size, 553 descriptor_size, descriptor_version, virtual_map); 554 555 efi_switch_mm(efi_scratch.prev_mm); 556 local_irq_restore(flags); 557 558 return status; 559 } 560 561 static efi_status_t efi_thunk_get_time(efi_time_t *tm, efi_time_cap_t *tc) 562 { 563 return EFI_UNSUPPORTED; 564 } 565 566 static efi_status_t efi_thunk_set_time(efi_time_t *tm) 567 { 568 return EFI_UNSUPPORTED; 569 } 570 571 static efi_status_t 572 efi_thunk_get_wakeup_time(efi_bool_t *enabled, efi_bool_t *pending, 573 efi_time_t *tm) 574 { 575 return EFI_UNSUPPORTED; 576 } 577 578 static efi_status_t 579 efi_thunk_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm) 580 { 581 return EFI_UNSUPPORTED; 582 } 583 584 static unsigned long efi_name_size(efi_char16_t *name) 585 { 586 return ucs2_strsize(name, EFI_VAR_NAME_LEN) + 1; 587 } 588 589 static efi_status_t 590 efi_thunk_get_variable(efi_char16_t *name, efi_guid_t *vendor, 591 u32 *attr, unsigned long *data_size, void *data) 592 { 593 u8 buf[24] __aligned(8); 594 efi_guid_t *vnd = PTR_ALIGN((efi_guid_t *)buf, sizeof(*vnd)); 595 efi_status_t status; 596 u32 phys_name, phys_vendor, phys_attr; 597 u32 phys_data_size, phys_data; 598 unsigned long flags; 599 600 spin_lock_irqsave(&efi_runtime_lock, flags); 601 602 *vnd = *vendor; 603 604 phys_data_size = virt_to_phys_or_null(data_size); 605 phys_vendor = virt_to_phys_or_null(vnd); 606 phys_name = virt_to_phys_or_null_size(name, efi_name_size(name)); 607 phys_attr = virt_to_phys_or_null(attr); 608 phys_data = virt_to_phys_or_null_size(data, *data_size); 609 610 if (!phys_name || (data && !phys_data)) 611 status = EFI_INVALID_PARAMETER; 612 else 613 status = efi_thunk(get_variable, phys_name, phys_vendor, 614 phys_attr, phys_data_size, phys_data); 615 616 spin_unlock_irqrestore(&efi_runtime_lock, flags); 617 618 return status; 619 } 620 621 static efi_status_t 622 efi_thunk_set_variable(efi_char16_t *name, efi_guid_t *vendor, 623 u32 attr, unsigned long data_size, void *data) 624 { 625 u8 buf[24] __aligned(8); 626 efi_guid_t *vnd = PTR_ALIGN((efi_guid_t *)buf, sizeof(*vnd)); 627 u32 phys_name, phys_vendor, phys_data; 628 efi_status_t status; 629 unsigned long flags; 630 631 spin_lock_irqsave(&efi_runtime_lock, flags); 632 633 *vnd = *vendor; 634 635 phys_name = virt_to_phys_or_null_size(name, efi_name_size(name)); 636 phys_vendor = virt_to_phys_or_null(vnd); 637 phys_data = virt_to_phys_or_null_size(data, data_size); 638 639 if (!phys_name || (data && !phys_data)) 640 status = EFI_INVALID_PARAMETER; 641 else 642 status = efi_thunk(set_variable, phys_name, phys_vendor, 643 attr, data_size, phys_data); 644 645 spin_unlock_irqrestore(&efi_runtime_lock, flags); 646 647 return status; 648 } 649 650 static efi_status_t 651 efi_thunk_set_variable_nonblocking(efi_char16_t *name, efi_guid_t *vendor, 652 u32 attr, unsigned long data_size, 653 void *data) 654 { 655 u8 buf[24] __aligned(8); 656 efi_guid_t *vnd = PTR_ALIGN((efi_guid_t *)buf, sizeof(*vnd)); 657 u32 phys_name, phys_vendor, phys_data; 658 efi_status_t status; 659 unsigned long flags; 660 661 if (!spin_trylock_irqsave(&efi_runtime_lock, flags)) 662 return EFI_NOT_READY; 663 664 *vnd = *vendor; 665 666 phys_name = virt_to_phys_or_null_size(name, efi_name_size(name)); 667 phys_vendor = virt_to_phys_or_null(vnd); 668 phys_data = virt_to_phys_or_null_size(data, data_size); 669 670 if (!phys_name || (data && !phys_data)) 671 status = EFI_INVALID_PARAMETER; 672 else 673 status = efi_thunk(set_variable, phys_name, phys_vendor, 674 attr, data_size, phys_data); 675 676 spin_unlock_irqrestore(&efi_runtime_lock, flags); 677 678 return status; 679 } 680 681 static efi_status_t 682 efi_thunk_get_next_variable(unsigned long *name_size, 683 efi_char16_t *name, 684 efi_guid_t *vendor) 685 { 686 u8 buf[24] __aligned(8); 687 efi_guid_t *vnd = PTR_ALIGN((efi_guid_t *)buf, sizeof(*vnd)); 688 efi_status_t status; 689 u32 phys_name_size, phys_name, phys_vendor; 690 unsigned long flags; 691 692 spin_lock_irqsave(&efi_runtime_lock, flags); 693 694 *vnd = *vendor; 695 696 phys_name_size = virt_to_phys_or_null(name_size); 697 phys_vendor = virt_to_phys_or_null(vnd); 698 phys_name = virt_to_phys_or_null_size(name, *name_size); 699 700 if (!phys_name) 701 status = EFI_INVALID_PARAMETER; 702 else 703 status = efi_thunk(get_next_variable, phys_name_size, 704 phys_name, phys_vendor); 705 706 spin_unlock_irqrestore(&efi_runtime_lock, flags); 707 708 *vendor = *vnd; 709 return status; 710 } 711 712 static efi_status_t 713 efi_thunk_get_next_high_mono_count(u32 *count) 714 { 715 return EFI_UNSUPPORTED; 716 } 717 718 static void 719 efi_thunk_reset_system(int reset_type, efi_status_t status, 720 unsigned long data_size, efi_char16_t *data) 721 { 722 u32 phys_data; 723 unsigned long flags; 724 725 spin_lock_irqsave(&efi_runtime_lock, flags); 726 727 phys_data = virt_to_phys_or_null_size(data, data_size); 728 729 efi_thunk(reset_system, reset_type, status, data_size, phys_data); 730 731 spin_unlock_irqrestore(&efi_runtime_lock, flags); 732 } 733 734 static efi_status_t 735 efi_thunk_update_capsule(efi_capsule_header_t **capsules, 736 unsigned long count, unsigned long sg_list) 737 { 738 /* 739 * To properly support this function we would need to repackage 740 * 'capsules' because the firmware doesn't understand 64-bit 741 * pointers. 742 */ 743 return EFI_UNSUPPORTED; 744 } 745 746 static efi_status_t 747 efi_thunk_query_variable_info(u32 attr, u64 *storage_space, 748 u64 *remaining_space, 749 u64 *max_variable_size) 750 { 751 efi_status_t status; 752 u32 phys_storage, phys_remaining, phys_max; 753 unsigned long flags; 754 755 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) 756 return EFI_UNSUPPORTED; 757 758 spin_lock_irqsave(&efi_runtime_lock, flags); 759 760 phys_storage = virt_to_phys_or_null(storage_space); 761 phys_remaining = virt_to_phys_or_null(remaining_space); 762 phys_max = virt_to_phys_or_null(max_variable_size); 763 764 status = efi_thunk(query_variable_info, attr, phys_storage, 765 phys_remaining, phys_max); 766 767 spin_unlock_irqrestore(&efi_runtime_lock, flags); 768 769 return status; 770 } 771 772 static efi_status_t 773 efi_thunk_query_variable_info_nonblocking(u32 attr, u64 *storage_space, 774 u64 *remaining_space, 775 u64 *max_variable_size) 776 { 777 efi_status_t status; 778 u32 phys_storage, phys_remaining, phys_max; 779 unsigned long flags; 780 781 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) 782 return EFI_UNSUPPORTED; 783 784 if (!spin_trylock_irqsave(&efi_runtime_lock, flags)) 785 return EFI_NOT_READY; 786 787 phys_storage = virt_to_phys_or_null(storage_space); 788 phys_remaining = virt_to_phys_or_null(remaining_space); 789 phys_max = virt_to_phys_or_null(max_variable_size); 790 791 status = efi_thunk(query_variable_info, attr, phys_storage, 792 phys_remaining, phys_max); 793 794 spin_unlock_irqrestore(&efi_runtime_lock, flags); 795 796 return status; 797 } 798 799 static efi_status_t 800 efi_thunk_query_capsule_caps(efi_capsule_header_t **capsules, 801 unsigned long count, u64 *max_size, 802 int *reset_type) 803 { 804 /* 805 * To properly support this function we would need to repackage 806 * 'capsules' because the firmware doesn't understand 64-bit 807 * pointers. 808 */ 809 return EFI_UNSUPPORTED; 810 } 811 812 void __init efi_thunk_runtime_setup(void) 813 { 814 if (!IS_ENABLED(CONFIG_EFI_MIXED)) 815 return; 816 817 efi.get_time = efi_thunk_get_time; 818 efi.set_time = efi_thunk_set_time; 819 efi.get_wakeup_time = efi_thunk_get_wakeup_time; 820 efi.set_wakeup_time = efi_thunk_set_wakeup_time; 821 efi.get_variable = efi_thunk_get_variable; 822 efi.get_next_variable = efi_thunk_get_next_variable; 823 efi.set_variable = efi_thunk_set_variable; 824 efi.set_variable_nonblocking = efi_thunk_set_variable_nonblocking; 825 efi.get_next_high_mono_count = efi_thunk_get_next_high_mono_count; 826 efi.reset_system = efi_thunk_reset_system; 827 efi.query_variable_info = efi_thunk_query_variable_info; 828 efi.query_variable_info_nonblocking = efi_thunk_query_variable_info_nonblocking; 829 efi.update_capsule = efi_thunk_update_capsule; 830 efi.query_capsule_caps = efi_thunk_query_capsule_caps; 831 } 832 833 efi_status_t __init __no_sanitize_address 834 efi_set_virtual_address_map(unsigned long memory_map_size, 835 unsigned long descriptor_size, 836 u32 descriptor_version, 837 efi_memory_desc_t *virtual_map, 838 unsigned long systab_phys) 839 { 840 const efi_system_table_t *systab = (efi_system_table_t *)systab_phys; 841 efi_status_t status; 842 unsigned long flags; 843 844 if (efi_is_mixed()) 845 return efi_thunk_set_virtual_address_map(memory_map_size, 846 descriptor_size, 847 descriptor_version, 848 virtual_map); 849 efi_switch_mm(&efi_mm); 850 851 kernel_fpu_begin(); 852 853 /* Disable interrupts around EFI calls: */ 854 local_irq_save(flags); 855 status = efi_call(efi.runtime->set_virtual_address_map, 856 memory_map_size, descriptor_size, 857 descriptor_version, virtual_map); 858 local_irq_restore(flags); 859 860 kernel_fpu_end(); 861 862 /* grab the virtually remapped EFI runtime services table pointer */ 863 efi.runtime = READ_ONCE(systab->runtime); 864 865 efi_switch_mm(efi_scratch.prev_mm); 866 867 return status; 868 } 869