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