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