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