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