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