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