]> git.proxmox.com Git - mirror_ubuntu-disco-kernel.git/blob - arch/x86/platform/efi/efi_64.c
UBUNTU: Ubuntu-5.0.0-29.31
[mirror_ubuntu-disco-kernel.git] / arch / x86 / platform / efi / efi_64.c
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/pgtable.h>
43 #include <asm/tlbflush.h>
44 #include <asm/proto.h>
45 #include <asm/efi.h>
46 #include <asm/cacheflush.h>
47 #include <asm/fixmap.h>
48 #include <asm/realmode.h>
49 #include <asm/time.h>
50 #include <asm/pgalloc.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 static void __init early_code_mapping_set_exec(int executable)
61 {
62 efi_memory_desc_t *md;
63
64 if (!(__supported_pte_mask & _PAGE_NX))
65 return;
66
67 /* Make EFI service code area executable */
68 for_each_efi_memory_desc(md) {
69 if (md->type == EFI_RUNTIME_SERVICES_CODE ||
70 md->type == EFI_BOOT_SERVICES_CODE)
71 efi_set_executable(md, executable);
72 }
73 }
74
75 pgd_t * __init efi_call_phys_prolog(void)
76 {
77 unsigned long vaddr, addr_pgd, addr_p4d, addr_pud;
78 pgd_t *save_pgd, *pgd_k, *pgd_efi;
79 p4d_t *p4d, *p4d_k, *p4d_efi;
80 pud_t *pud;
81
82 int pgd;
83 int n_pgds, i, j;
84
85 if (!efi_enabled(EFI_OLD_MEMMAP)) {
86 efi_switch_mm(&efi_mm);
87 return NULL;
88 }
89
90 early_code_mapping_set_exec(1);
91
92 n_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT), PGDIR_SIZE);
93 save_pgd = kmalloc_array(n_pgds, sizeof(*save_pgd), GFP_KERNEL);
94
95 /*
96 * Build 1:1 identity mapping for efi=old_map usage. Note that
97 * PAGE_OFFSET is PGDIR_SIZE aligned when KASLR is disabled, while
98 * it is PUD_SIZE ALIGNED with KASLR enabled. So for a given physical
99 * address X, the pud_index(X) != pud_index(__va(X)), we can only copy
100 * PUD entry of __va(X) to fill in pud entry of X to build 1:1 mapping.
101 * This means here we can only reuse the PMD tables of the direct mapping.
102 */
103 for (pgd = 0; pgd < n_pgds; pgd++) {
104 addr_pgd = (unsigned long)(pgd * PGDIR_SIZE);
105 vaddr = (unsigned long)__va(pgd * PGDIR_SIZE);
106 pgd_efi = pgd_offset_k(addr_pgd);
107 save_pgd[pgd] = *pgd_efi;
108
109 p4d = p4d_alloc(&init_mm, pgd_efi, addr_pgd);
110 if (!p4d) {
111 pr_err("Failed to allocate p4d table!\n");
112 goto out;
113 }
114
115 for (i = 0; i < PTRS_PER_P4D; i++) {
116 addr_p4d = addr_pgd + i * P4D_SIZE;
117 p4d_efi = p4d + p4d_index(addr_p4d);
118
119 pud = pud_alloc(&init_mm, p4d_efi, addr_p4d);
120 if (!pud) {
121 pr_err("Failed to allocate pud table!\n");
122 goto out;
123 }
124
125 for (j = 0; j < PTRS_PER_PUD; j++) {
126 addr_pud = addr_p4d + j * PUD_SIZE;
127
128 if (addr_pud > (max_pfn << PAGE_SHIFT))
129 break;
130
131 vaddr = (unsigned long)__va(addr_pud);
132
133 pgd_k = pgd_offset_k(vaddr);
134 p4d_k = p4d_offset(pgd_k, vaddr);
135 pud[j] = *pud_offset(p4d_k, vaddr);
136 }
137 }
138 pgd_offset_k(pgd * PGDIR_SIZE)->pgd &= ~_PAGE_NX;
139 }
140
141 out:
142 __flush_tlb_all();
143
144 return save_pgd;
145 }
146
147 void __init efi_call_phys_epilog(pgd_t *save_pgd)
148 {
149 /*
150 * After the lock is released, the original page table is restored.
151 */
152 int pgd_idx, i;
153 int nr_pgds;
154 pgd_t *pgd;
155 p4d_t *p4d;
156 pud_t *pud;
157
158 if (!efi_enabled(EFI_OLD_MEMMAP)) {
159 efi_switch_mm(efi_scratch.prev_mm);
160 return;
161 }
162
163 nr_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT) , PGDIR_SIZE);
164
165 for (pgd_idx = 0; pgd_idx < nr_pgds; pgd_idx++) {
166 pgd = pgd_offset_k(pgd_idx * PGDIR_SIZE);
167 set_pgd(pgd_offset_k(pgd_idx * PGDIR_SIZE), save_pgd[pgd_idx]);
168
169 if (!pgd_present(*pgd))
170 continue;
171
172 for (i = 0; i < PTRS_PER_P4D; i++) {
173 p4d = p4d_offset(pgd,
174 pgd_idx * PGDIR_SIZE + i * P4D_SIZE);
175
176 if (!p4d_present(*p4d))
177 continue;
178
179 pud = (pud_t *)p4d_page_vaddr(*p4d);
180 pud_free(&init_mm, pud);
181 }
182
183 p4d = (p4d_t *)pgd_page_vaddr(*pgd);
184 p4d_free(&init_mm, p4d);
185 }
186
187 kfree(save_pgd);
188
189 __flush_tlb_all();
190 early_code_mapping_set_exec(0);
191 }
192
193 EXPORT_SYMBOL_GPL(efi_mm);
194
195 /*
196 * We need our own copy of the higher levels of the page tables
197 * because we want to avoid inserting EFI region mappings (EFI_VA_END
198 * to EFI_VA_START) into the standard kernel page tables. Everything
199 * else can be shared, see efi_sync_low_kernel_mappings().
200 *
201 * We don't want the pgd on the pgd_list and cannot use pgd_alloc() for the
202 * allocation.
203 */
204 int __init efi_alloc_page_tables(void)
205 {
206 pgd_t *pgd, *efi_pgd;
207 p4d_t *p4d;
208 pud_t *pud;
209 gfp_t gfp_mask;
210
211 if (efi_enabled(EFI_OLD_MEMMAP))
212 return 0;
213
214 gfp_mask = GFP_KERNEL | __GFP_ZERO;
215 efi_pgd = (pgd_t *)__get_free_pages(gfp_mask, PGD_ALLOCATION_ORDER);
216 if (!efi_pgd)
217 return -ENOMEM;
218
219 pgd = efi_pgd + pgd_index(EFI_VA_END);
220 p4d = p4d_alloc(&init_mm, pgd, EFI_VA_END);
221 if (!p4d) {
222 free_page((unsigned long)efi_pgd);
223 return -ENOMEM;
224 }
225
226 pud = pud_alloc(&init_mm, p4d, EFI_VA_END);
227 if (!pud) {
228 if (pgtable_l5_enabled())
229 free_page((unsigned long) pgd_page_vaddr(*pgd));
230 free_pages((unsigned long)efi_pgd, PGD_ALLOCATION_ORDER);
231 return -ENOMEM;
232 }
233
234 efi_mm.pgd = efi_pgd;
235 mm_init_cpumask(&efi_mm);
236 init_new_context(NULL, &efi_mm);
237
238 return 0;
239 }
240
241 /*
242 * Add low kernel mappings for passing arguments to EFI functions.
243 */
244 void efi_sync_low_kernel_mappings(void)
245 {
246 unsigned num_entries;
247 pgd_t *pgd_k, *pgd_efi;
248 p4d_t *p4d_k, *p4d_efi;
249 pud_t *pud_k, *pud_efi;
250 pgd_t *efi_pgd = efi_mm.pgd;
251
252 if (efi_enabled(EFI_OLD_MEMMAP))
253 return;
254
255 /*
256 * We can share all PGD entries apart from the one entry that
257 * covers the EFI runtime mapping space.
258 *
259 * Make sure the EFI runtime region mappings are guaranteed to
260 * only span a single PGD entry and that the entry also maps
261 * other important kernel regions.
262 */
263 MAYBE_BUILD_BUG_ON(pgd_index(EFI_VA_END) != pgd_index(MODULES_END));
264 MAYBE_BUILD_BUG_ON((EFI_VA_START & PGDIR_MASK) !=
265 (EFI_VA_END & PGDIR_MASK));
266
267 pgd_efi = efi_pgd + pgd_index(PAGE_OFFSET);
268 pgd_k = pgd_offset_k(PAGE_OFFSET);
269
270 num_entries = pgd_index(EFI_VA_END) - pgd_index(PAGE_OFFSET);
271 memcpy(pgd_efi, pgd_k, sizeof(pgd_t) * num_entries);
272
273 /*
274 * As with PGDs, we share all P4D entries apart from the one entry
275 * that covers the EFI runtime mapping space.
276 */
277 BUILD_BUG_ON(p4d_index(EFI_VA_END) != p4d_index(MODULES_END));
278 BUILD_BUG_ON((EFI_VA_START & P4D_MASK) != (EFI_VA_END & P4D_MASK));
279
280 pgd_efi = efi_pgd + pgd_index(EFI_VA_END);
281 pgd_k = pgd_offset_k(EFI_VA_END);
282 p4d_efi = p4d_offset(pgd_efi, 0);
283 p4d_k = p4d_offset(pgd_k, 0);
284
285 num_entries = p4d_index(EFI_VA_END);
286 memcpy(p4d_efi, p4d_k, sizeof(p4d_t) * num_entries);
287
288 /*
289 * We share all the PUD entries apart from those that map the
290 * EFI regions. Copy around them.
291 */
292 BUILD_BUG_ON((EFI_VA_START & ~PUD_MASK) != 0);
293 BUILD_BUG_ON((EFI_VA_END & ~PUD_MASK) != 0);
294
295 p4d_efi = p4d_offset(pgd_efi, EFI_VA_END);
296 p4d_k = p4d_offset(pgd_k, EFI_VA_END);
297 pud_efi = pud_offset(p4d_efi, 0);
298 pud_k = pud_offset(p4d_k, 0);
299
300 num_entries = pud_index(EFI_VA_END);
301 memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
302
303 pud_efi = pud_offset(p4d_efi, EFI_VA_START);
304 pud_k = pud_offset(p4d_k, EFI_VA_START);
305
306 num_entries = PTRS_PER_PUD - pud_index(EFI_VA_START);
307 memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
308 }
309
310 /*
311 * Wrapper for slow_virt_to_phys() that handles NULL addresses.
312 */
313 static inline phys_addr_t
314 virt_to_phys_or_null_size(void *va, unsigned long size)
315 {
316 bool bad_size;
317
318 if (!va)
319 return 0;
320
321 if (virt_addr_valid(va))
322 return virt_to_phys(va);
323
324 /*
325 * A fully aligned variable on the stack is guaranteed not to
326 * cross a page bounary. Try to catch strings on the stack by
327 * checking that 'size' is a power of two.
328 */
329 bad_size = size > PAGE_SIZE || !is_power_of_2(size);
330
331 WARN_ON(!IS_ALIGNED((unsigned long)va, size) || bad_size);
332
333 return slow_virt_to_phys(va);
334 }
335
336 #define virt_to_phys_or_null(addr) \
337 virt_to_phys_or_null_size((addr), sizeof(*(addr)))
338
339 int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages)
340 {
341 unsigned long pfn, text, pf;
342 struct page *page;
343 unsigned npages;
344 pgd_t *pgd = efi_mm.pgd;
345
346 if (efi_enabled(EFI_OLD_MEMMAP))
347 return 0;
348
349 /*
350 * It can happen that the physical address of new_memmap lands in memory
351 * which is not mapped in the EFI page table. Therefore we need to go
352 * and ident-map those pages containing the map before calling
353 * phys_efi_set_virtual_address_map().
354 */
355 pfn = pa_memmap >> PAGE_SHIFT;
356 pf = _PAGE_NX | _PAGE_RW | _PAGE_ENC;
357 if (kernel_map_pages_in_pgd(pgd, pfn, pa_memmap, num_pages, pf)) {
358 pr_err("Error ident-mapping new memmap (0x%lx)!\n", pa_memmap);
359 return 1;
360 }
361
362 /*
363 * Certain firmware versions are way too sentimential and still believe
364 * they are exclusive and unquestionable owners of the first physical page,
365 * even though they explicitly mark it as EFI_CONVENTIONAL_MEMORY
366 * (but then write-access it later during SetVirtualAddressMap()).
367 *
368 * Create a 1:1 mapping for this page, to avoid triple faults during early
369 * boot with such firmware. We are free to hand this page to the BIOS,
370 * as trim_bios_range() will reserve the first page and isolate it away
371 * from memory allocators anyway.
372 */
373 pf = _PAGE_RW;
374 if (sev_active())
375 pf |= _PAGE_ENC;
376
377 if (kernel_map_pages_in_pgd(pgd, 0x0, 0x0, 1, pf)) {
378 pr_err("Failed to create 1:1 mapping for the first page!\n");
379 return 1;
380 }
381
382 /*
383 * When making calls to the firmware everything needs to be 1:1
384 * mapped and addressable with 32-bit pointers. Map the kernel
385 * text and allocate a new stack because we can't rely on the
386 * stack pointer being < 4GB.
387 */
388 if (!IS_ENABLED(CONFIG_EFI_MIXED) || efi_is_native())
389 return 0;
390
391 page = alloc_page(GFP_KERNEL|__GFP_DMA32);
392 if (!page)
393 panic("Unable to allocate EFI runtime stack < 4GB\n");
394
395 efi_scratch.phys_stack = virt_to_phys(page_address(page));
396 efi_scratch.phys_stack += PAGE_SIZE; /* stack grows down */
397
398 npages = (_etext - _text) >> PAGE_SHIFT;
399 text = __pa(_text);
400 pfn = text >> PAGE_SHIFT;
401
402 pf = _PAGE_RW | _PAGE_ENC;
403 if (kernel_map_pages_in_pgd(pgd, pfn, text, npages, pf)) {
404 pr_err("Failed to map kernel text 1:1\n");
405 return 1;
406 }
407
408 return 0;
409 }
410
411 static void __init __map_region(efi_memory_desc_t *md, u64 va)
412 {
413 unsigned long flags = _PAGE_RW;
414 unsigned long pfn;
415 pgd_t *pgd = efi_mm.pgd;
416
417 if (!(md->attribute & EFI_MEMORY_WB))
418 flags |= _PAGE_PCD;
419
420 if (sev_active() && md->type != EFI_MEMORY_MAPPED_IO)
421 flags |= _PAGE_ENC;
422
423 pfn = md->phys_addr >> PAGE_SHIFT;
424 if (kernel_map_pages_in_pgd(pgd, pfn, va, md->num_pages, flags))
425 pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
426 md->phys_addr, va);
427 }
428
429 void __init efi_map_region(efi_memory_desc_t *md)
430 {
431 unsigned long size = md->num_pages << PAGE_SHIFT;
432 u64 pa = md->phys_addr;
433
434 if (efi_enabled(EFI_OLD_MEMMAP))
435 return old_map_region(md);
436
437 /*
438 * Make sure the 1:1 mappings are present as a catch-all for b0rked
439 * firmware which doesn't update all internal pointers after switching
440 * to virtual mode and would otherwise crap on us.
441 */
442 __map_region(md, md->phys_addr);
443
444 /*
445 * Enforce the 1:1 mapping as the default virtual address when
446 * booting in EFI mixed mode, because even though we may be
447 * running a 64-bit kernel, the firmware may only be 32-bit.
448 */
449 if (!efi_is_native () && IS_ENABLED(CONFIG_EFI_MIXED)) {
450 md->virt_addr = md->phys_addr;
451 return;
452 }
453
454 efi_va -= size;
455
456 /* Is PA 2M-aligned? */
457 if (!(pa & (PMD_SIZE - 1))) {
458 efi_va &= PMD_MASK;
459 } else {
460 u64 pa_offset = pa & (PMD_SIZE - 1);
461 u64 prev_va = efi_va;
462
463 /* get us the same offset within this 2M page */
464 efi_va = (efi_va & PMD_MASK) + pa_offset;
465
466 if (efi_va > prev_va)
467 efi_va -= PMD_SIZE;
468 }
469
470 if (efi_va < EFI_VA_END) {
471 pr_warn(FW_WARN "VA address range overflow!\n");
472 return;
473 }
474
475 /* Do the VA map */
476 __map_region(md, efi_va);
477 md->virt_addr = efi_va;
478 }
479
480 /*
481 * kexec kernel will use efi_map_region_fixed to map efi runtime memory ranges.
482 * md->virt_addr is the original virtual address which had been mapped in kexec
483 * 1st kernel.
484 */
485 void __init efi_map_region_fixed(efi_memory_desc_t *md)
486 {
487 __map_region(md, md->phys_addr);
488 __map_region(md, md->virt_addr);
489 }
490
491 void __iomem *__init efi_ioremap(unsigned long phys_addr, unsigned long size,
492 u32 type, u64 attribute)
493 {
494 unsigned long last_map_pfn;
495
496 if (type == EFI_MEMORY_MAPPED_IO)
497 return ioremap(phys_addr, size);
498
499 last_map_pfn = init_memory_mapping(phys_addr, phys_addr + size);
500 if ((last_map_pfn << PAGE_SHIFT) < phys_addr + size) {
501 unsigned long top = last_map_pfn << PAGE_SHIFT;
502 efi_ioremap(top, size - (top - phys_addr), type, attribute);
503 }
504
505 if (!(attribute & EFI_MEMORY_WB))
506 efi_memory_uc((u64)(unsigned long)__va(phys_addr), size);
507
508 return (void __iomem *)__va(phys_addr);
509 }
510
511 void __init parse_efi_setup(u64 phys_addr, u32 data_len)
512 {
513 efi_setup = phys_addr + sizeof(struct setup_data);
514 }
515
516 static int __init efi_update_mappings(efi_memory_desc_t *md, unsigned long pf)
517 {
518 unsigned long pfn;
519 pgd_t *pgd = efi_mm.pgd;
520 int err1, err2;
521
522 /* Update the 1:1 mapping */
523 pfn = md->phys_addr >> PAGE_SHIFT;
524 err1 = kernel_map_pages_in_pgd(pgd, pfn, md->phys_addr, md->num_pages, pf);
525 if (err1) {
526 pr_err("Error while updating 1:1 mapping PA 0x%llx -> VA 0x%llx!\n",
527 md->phys_addr, md->virt_addr);
528 }
529
530 err2 = kernel_map_pages_in_pgd(pgd, pfn, md->virt_addr, md->num_pages, pf);
531 if (err2) {
532 pr_err("Error while updating VA mapping PA 0x%llx -> VA 0x%llx!\n",
533 md->phys_addr, md->virt_addr);
534 }
535
536 return err1 || err2;
537 }
538
539 static int __init efi_update_mem_attr(struct mm_struct *mm, efi_memory_desc_t *md)
540 {
541 unsigned long pf = 0;
542
543 if (md->attribute & EFI_MEMORY_XP)
544 pf |= _PAGE_NX;
545
546 if (!(md->attribute & EFI_MEMORY_RO))
547 pf |= _PAGE_RW;
548
549 if (sev_active())
550 pf |= _PAGE_ENC;
551
552 return efi_update_mappings(md, pf);
553 }
554
555 void __init efi_runtime_update_mappings(void)
556 {
557 efi_memory_desc_t *md;
558
559 if (efi_enabled(EFI_OLD_MEMMAP)) {
560 if (__supported_pte_mask & _PAGE_NX)
561 runtime_code_page_mkexec();
562 return;
563 }
564
565 /*
566 * Use the EFI Memory Attribute Table for mapping permissions if it
567 * exists, since it is intended to supersede EFI_PROPERTIES_TABLE.
568 */
569 if (efi_enabled(EFI_MEM_ATTR)) {
570 efi_memattr_apply_permissions(NULL, efi_update_mem_attr);
571 return;
572 }
573
574 /*
575 * EFI_MEMORY_ATTRIBUTES_TABLE is intended to replace
576 * EFI_PROPERTIES_TABLE. So, use EFI_PROPERTIES_TABLE to update
577 * permissions only if EFI_MEMORY_ATTRIBUTES_TABLE is not
578 * published by the firmware. Even if we find a buggy implementation of
579 * EFI_MEMORY_ATTRIBUTES_TABLE, don't fall back to
580 * EFI_PROPERTIES_TABLE, because of the same reason.
581 */
582
583 if (!efi_enabled(EFI_NX_PE_DATA))
584 return;
585
586 for_each_efi_memory_desc(md) {
587 unsigned long pf = 0;
588
589 if (!(md->attribute & EFI_MEMORY_RUNTIME))
590 continue;
591
592 if (!(md->attribute & EFI_MEMORY_WB))
593 pf |= _PAGE_PCD;
594
595 if ((md->attribute & EFI_MEMORY_XP) ||
596 (md->type == EFI_RUNTIME_SERVICES_DATA))
597 pf |= _PAGE_NX;
598
599 if (!(md->attribute & EFI_MEMORY_RO) &&
600 (md->type != EFI_RUNTIME_SERVICES_CODE))
601 pf |= _PAGE_RW;
602
603 if (sev_active())
604 pf |= _PAGE_ENC;
605
606 efi_update_mappings(md, pf);
607 }
608 }
609
610 void __init efi_dump_pagetable(void)
611 {
612 #ifdef CONFIG_EFI_PGT_DUMP
613 if (efi_enabled(EFI_OLD_MEMMAP))
614 ptdump_walk_pgd_level(NULL, swapper_pg_dir);
615 else
616 ptdump_walk_pgd_level(NULL, efi_mm.pgd);
617 #endif
618 }
619
620 /*
621 * Makes the calling thread switch to/from efi_mm context. Can be used
622 * in a kernel thread and user context. Preemption needs to remain disabled
623 * while the EFI-mm is borrowed. mmgrab()/mmdrop() is not used because the mm
624 * can not change under us.
625 * It should be ensured that there are no concurent calls to this function.
626 */
627 void efi_switch_mm(struct mm_struct *mm)
628 {
629 efi_scratch.prev_mm = current->active_mm;
630 current->active_mm = mm;
631 switch_mm(efi_scratch.prev_mm, mm, NULL);
632 }
633
634 #ifdef CONFIG_EFI_MIXED
635 extern efi_status_t efi64_thunk(u32, ...);
636
637 static DEFINE_SPINLOCK(efi_runtime_lock);
638
639 #define runtime_service32(func) \
640 ({ \
641 u32 table = (u32)(unsigned long)efi.systab; \
642 u32 *rt, *___f; \
643 \
644 rt = (u32 *)(table + offsetof(efi_system_table_32_t, runtime)); \
645 ___f = (u32 *)(*rt + offsetof(efi_runtime_services_32_t, func)); \
646 *___f; \
647 })
648
649 /*
650 * Switch to the EFI page tables early so that we can access the 1:1
651 * runtime services mappings which are not mapped in any other page
652 * tables. This function must be called before runtime_service32().
653 *
654 * Also, disable interrupts because the IDT points to 64-bit handlers,
655 * which aren't going to function correctly when we switch to 32-bit.
656 */
657 #define efi_thunk(f, ...) \
658 ({ \
659 efi_status_t __s; \
660 u32 __func; \
661 \
662 arch_efi_call_virt_setup(); \
663 \
664 __func = runtime_service32(f); \
665 __s = efi64_thunk(__func, __VA_ARGS__); \
666 \
667 arch_efi_call_virt_teardown(); \
668 \
669 __s; \
670 })
671
672 efi_status_t efi_thunk_set_virtual_address_map(
673 void *phys_set_virtual_address_map,
674 unsigned long memory_map_size,
675 unsigned long descriptor_size,
676 u32 descriptor_version,
677 efi_memory_desc_t *virtual_map)
678 {
679 efi_status_t status;
680 unsigned long flags;
681 u32 func;
682
683 efi_sync_low_kernel_mappings();
684 local_irq_save(flags);
685
686 efi_switch_mm(&efi_mm);
687
688 func = (u32)(unsigned long)phys_set_virtual_address_map;
689 status = efi64_thunk(func, memory_map_size, descriptor_size,
690 descriptor_version, virtual_map);
691
692 efi_switch_mm(efi_scratch.prev_mm);
693 local_irq_restore(flags);
694
695 return status;
696 }
697
698 static efi_status_t efi_thunk_get_time(efi_time_t *tm, efi_time_cap_t *tc)
699 {
700 efi_status_t status;
701 u32 phys_tm, phys_tc;
702 unsigned long flags;
703
704 spin_lock(&rtc_lock);
705 spin_lock_irqsave(&efi_runtime_lock, flags);
706
707 phys_tm = virt_to_phys_or_null(tm);
708 phys_tc = virt_to_phys_or_null(tc);
709
710 status = efi_thunk(get_time, phys_tm, phys_tc);
711
712 spin_unlock_irqrestore(&efi_runtime_lock, flags);
713 spin_unlock(&rtc_lock);
714
715 return status;
716 }
717
718 static efi_status_t efi_thunk_set_time(efi_time_t *tm)
719 {
720 efi_status_t status;
721 u32 phys_tm;
722 unsigned long flags;
723
724 spin_lock(&rtc_lock);
725 spin_lock_irqsave(&efi_runtime_lock, flags);
726
727 phys_tm = virt_to_phys_or_null(tm);
728
729 status = efi_thunk(set_time, phys_tm);
730
731 spin_unlock_irqrestore(&efi_runtime_lock, flags);
732 spin_unlock(&rtc_lock);
733
734 return status;
735 }
736
737 static efi_status_t
738 efi_thunk_get_wakeup_time(efi_bool_t *enabled, efi_bool_t *pending,
739 efi_time_t *tm)
740 {
741 efi_status_t status;
742 u32 phys_enabled, phys_pending, phys_tm;
743 unsigned long flags;
744
745 spin_lock(&rtc_lock);
746 spin_lock_irqsave(&efi_runtime_lock, flags);
747
748 phys_enabled = virt_to_phys_or_null(enabled);
749 phys_pending = virt_to_phys_or_null(pending);
750 phys_tm = virt_to_phys_or_null(tm);
751
752 status = efi_thunk(get_wakeup_time, phys_enabled,
753 phys_pending, phys_tm);
754
755 spin_unlock_irqrestore(&efi_runtime_lock, flags);
756 spin_unlock(&rtc_lock);
757
758 return status;
759 }
760
761 static efi_status_t
762 efi_thunk_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
763 {
764 efi_status_t status;
765 u32 phys_tm;
766 unsigned long flags;
767
768 spin_lock(&rtc_lock);
769 spin_lock_irqsave(&efi_runtime_lock, flags);
770
771 phys_tm = virt_to_phys_or_null(tm);
772
773 status = efi_thunk(set_wakeup_time, enabled, phys_tm);
774
775 spin_unlock_irqrestore(&efi_runtime_lock, flags);
776 spin_unlock(&rtc_lock);
777
778 return status;
779 }
780
781 static unsigned long efi_name_size(efi_char16_t *name)
782 {
783 return ucs2_strsize(name, EFI_VAR_NAME_LEN) + 1;
784 }
785
786 static efi_status_t
787 efi_thunk_get_variable(efi_char16_t *name, efi_guid_t *vendor,
788 u32 *attr, unsigned long *data_size, void *data)
789 {
790 efi_status_t status;
791 u32 phys_name, phys_vendor, phys_attr;
792 u32 phys_data_size, phys_data;
793 unsigned long flags;
794
795 spin_lock_irqsave(&efi_runtime_lock, flags);
796
797 phys_data_size = virt_to_phys_or_null(data_size);
798 phys_vendor = virt_to_phys_or_null(vendor);
799 phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
800 phys_attr = virt_to_phys_or_null(attr);
801 phys_data = virt_to_phys_or_null_size(data, *data_size);
802
803 status = efi_thunk(get_variable, phys_name, phys_vendor,
804 phys_attr, phys_data_size, phys_data);
805
806 spin_unlock_irqrestore(&efi_runtime_lock, flags);
807
808 return status;
809 }
810
811 static efi_status_t
812 efi_thunk_set_variable(efi_char16_t *name, efi_guid_t *vendor,
813 u32 attr, unsigned long data_size, void *data)
814 {
815 u32 phys_name, phys_vendor, phys_data;
816 efi_status_t status;
817 unsigned long flags;
818
819 spin_lock_irqsave(&efi_runtime_lock, flags);
820
821 phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
822 phys_vendor = virt_to_phys_or_null(vendor);
823 phys_data = virt_to_phys_or_null_size(data, data_size);
824
825 /* If data_size is > sizeof(u32) we've got problems */
826 status = efi_thunk(set_variable, phys_name, phys_vendor,
827 attr, data_size, phys_data);
828
829 spin_unlock_irqrestore(&efi_runtime_lock, flags);
830
831 return status;
832 }
833
834 static efi_status_t
835 efi_thunk_set_variable_nonblocking(efi_char16_t *name, efi_guid_t *vendor,
836 u32 attr, unsigned long data_size,
837 void *data)
838 {
839 u32 phys_name, phys_vendor, phys_data;
840 efi_status_t status;
841 unsigned long flags;
842
843 if (!spin_trylock_irqsave(&efi_runtime_lock, flags))
844 return EFI_NOT_READY;
845
846 phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
847 phys_vendor = virt_to_phys_or_null(vendor);
848 phys_data = virt_to_phys_or_null_size(data, data_size);
849
850 /* If data_size is > sizeof(u32) we've got problems */
851 status = efi_thunk(set_variable, phys_name, phys_vendor,
852 attr, data_size, phys_data);
853
854 spin_unlock_irqrestore(&efi_runtime_lock, flags);
855
856 return status;
857 }
858
859 static efi_status_t
860 efi_thunk_get_next_variable(unsigned long *name_size,
861 efi_char16_t *name,
862 efi_guid_t *vendor)
863 {
864 efi_status_t status;
865 u32 phys_name_size, phys_name, phys_vendor;
866 unsigned long flags;
867
868 spin_lock_irqsave(&efi_runtime_lock, flags);
869
870 phys_name_size = virt_to_phys_or_null(name_size);
871 phys_vendor = virt_to_phys_or_null(vendor);
872 phys_name = virt_to_phys_or_null_size(name, *name_size);
873
874 status = efi_thunk(get_next_variable, phys_name_size,
875 phys_name, phys_vendor);
876
877 spin_unlock_irqrestore(&efi_runtime_lock, flags);
878
879 return status;
880 }
881
882 static efi_status_t
883 efi_thunk_get_next_high_mono_count(u32 *count)
884 {
885 efi_status_t status;
886 u32 phys_count;
887 unsigned long flags;
888
889 spin_lock_irqsave(&efi_runtime_lock, flags);
890
891 phys_count = virt_to_phys_or_null(count);
892 status = efi_thunk(get_next_high_mono_count, phys_count);
893
894 spin_unlock_irqrestore(&efi_runtime_lock, flags);
895
896 return status;
897 }
898
899 static void
900 efi_thunk_reset_system(int reset_type, efi_status_t status,
901 unsigned long data_size, efi_char16_t *data)
902 {
903 u32 phys_data;
904 unsigned long flags;
905
906 spin_lock_irqsave(&efi_runtime_lock, flags);
907
908 phys_data = virt_to_phys_or_null_size(data, data_size);
909
910 efi_thunk(reset_system, reset_type, status, data_size, phys_data);
911
912 spin_unlock_irqrestore(&efi_runtime_lock, flags);
913 }
914
915 static efi_status_t
916 efi_thunk_update_capsule(efi_capsule_header_t **capsules,
917 unsigned long count, unsigned long sg_list)
918 {
919 /*
920 * To properly support this function we would need to repackage
921 * 'capsules' because the firmware doesn't understand 64-bit
922 * pointers.
923 */
924 return EFI_UNSUPPORTED;
925 }
926
927 static efi_status_t
928 efi_thunk_query_variable_info(u32 attr, u64 *storage_space,
929 u64 *remaining_space,
930 u64 *max_variable_size)
931 {
932 efi_status_t status;
933 u32 phys_storage, phys_remaining, phys_max;
934 unsigned long flags;
935
936 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
937 return EFI_UNSUPPORTED;
938
939 spin_lock_irqsave(&efi_runtime_lock, flags);
940
941 phys_storage = virt_to_phys_or_null(storage_space);
942 phys_remaining = virt_to_phys_or_null(remaining_space);
943 phys_max = virt_to_phys_or_null(max_variable_size);
944
945 status = efi_thunk(query_variable_info, attr, phys_storage,
946 phys_remaining, phys_max);
947
948 spin_unlock_irqrestore(&efi_runtime_lock, flags);
949
950 return status;
951 }
952
953 static efi_status_t
954 efi_thunk_query_variable_info_nonblocking(u32 attr, u64 *storage_space,
955 u64 *remaining_space,
956 u64 *max_variable_size)
957 {
958 efi_status_t status;
959 u32 phys_storage, phys_remaining, phys_max;
960 unsigned long flags;
961
962 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
963 return EFI_UNSUPPORTED;
964
965 if (!spin_trylock_irqsave(&efi_runtime_lock, flags))
966 return EFI_NOT_READY;
967
968 phys_storage = virt_to_phys_or_null(storage_space);
969 phys_remaining = virt_to_phys_or_null(remaining_space);
970 phys_max = virt_to_phys_or_null(max_variable_size);
971
972 status = efi_thunk(query_variable_info, attr, phys_storage,
973 phys_remaining, phys_max);
974
975 spin_unlock_irqrestore(&efi_runtime_lock, flags);
976
977 return status;
978 }
979
980 static efi_status_t
981 efi_thunk_query_capsule_caps(efi_capsule_header_t **capsules,
982 unsigned long count, u64 *max_size,
983 int *reset_type)
984 {
985 /*
986 * To properly support this function we would need to repackage
987 * 'capsules' because the firmware doesn't understand 64-bit
988 * pointers.
989 */
990 return EFI_UNSUPPORTED;
991 }
992
993 void efi_thunk_runtime_setup(void)
994 {
995 efi.get_time = efi_thunk_get_time;
996 efi.set_time = efi_thunk_set_time;
997 efi.get_wakeup_time = efi_thunk_get_wakeup_time;
998 efi.set_wakeup_time = efi_thunk_set_wakeup_time;
999 efi.get_variable = efi_thunk_get_variable;
1000 efi.get_next_variable = efi_thunk_get_next_variable;
1001 efi.set_variable = efi_thunk_set_variable;
1002 efi.set_variable_nonblocking = efi_thunk_set_variable_nonblocking;
1003 efi.get_next_high_mono_count = efi_thunk_get_next_high_mono_count;
1004 efi.reset_system = efi_thunk_reset_system;
1005 efi.query_variable_info = efi_thunk_query_variable_info;
1006 efi.query_variable_info_nonblocking = efi_thunk_query_variable_info_nonblocking;
1007 efi.update_capsule = efi_thunk_update_capsule;
1008 efi.query_capsule_caps = efi_thunk_query_capsule_caps;
1009 }
1010 #endif /* CONFIG_EFI_MIXED */