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