xref: /linux/arch/x86/platform/efi/efi.c (revision 6f7e6393d1ce636bb7ec77a7fe7b77458fddf701)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Common EFI (Extensible Firmware Interface) support functions
4  * Based on Extensible Firmware Interface Specification version 1.0
5  *
6  * Copyright (C) 1999 VA Linux Systems
7  * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
8  * Copyright (C) 1999-2002 Hewlett-Packard Co.
9  *	David Mosberger-Tang <davidm@hpl.hp.com>
10  *	Stephane Eranian <eranian@hpl.hp.com>
11  * Copyright (C) 2005-2008 Intel Co.
12  *	Fenghua Yu <fenghua.yu@intel.com>
13  *	Bibo Mao <bibo.mao@intel.com>
14  *	Chandramouli Narayanan <mouli@linux.intel.com>
15  *	Huang Ying <ying.huang@intel.com>
16  * Copyright (C) 2013 SuSE Labs
17  *	Borislav Petkov <bp@suse.de> - runtime services VA mapping
18  *
19  * Copied from efi_32.c to eliminate the duplicated code between EFI
20  * 32/64 support code. --ying 2007-10-26
21  *
22  * All EFI Runtime Services are not implemented yet as EFI only
23  * supports physical mode addressing on SoftSDV. This is to be fixed
24  * in a future version.  --drummond 1999-07-20
25  *
26  * Implemented EFI runtime services and virtual mode calls.  --davidm
27  *
28  * Goutham Rao: <goutham.rao@intel.com>
29  *	Skip non-WB memory and ignore empty memory ranges.
30  */
31 
32 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33 
34 #include <linux/kernel.h>
35 #include <linux/init.h>
36 #include <linux/efi.h>
37 #include <linux/efi-bgrt.h>
38 #include <linux/export.h>
39 #include <linux/memblock.h>
40 #include <linux/slab.h>
41 #include <linux/spinlock.h>
42 #include <linux/uaccess.h>
43 #include <linux/time.h>
44 #include <linux/io.h>
45 #include <linux/reboot.h>
46 #include <linux/bcd.h>
47 
48 #include <asm/setup.h>
49 #include <asm/efi.h>
50 #include <asm/e820/api.h>
51 #include <asm/time.h>
52 #include <asm/tlbflush.h>
53 #include <asm/x86_init.h>
54 #include <asm/uv/uv.h>
55 
56 static unsigned long efi_systab_phys __initdata;
57 static unsigned long efi_runtime, efi_nr_tables;
58 
59 unsigned long efi_fw_vendor, efi_config_table;
60 
61 static const efi_config_table_type_t arch_tables[] __initconst = {
62 #ifdef CONFIG_X86_UV
63 	{UV_SYSTEM_TABLE_GUID,		&uv_systab_phys,	"UVsystab"	},
64 #endif
65 	{},
66 };
67 
68 static const unsigned long * const efi_tables[] = {
69 	&efi.acpi,
70 	&efi.acpi20,
71 	&efi.smbios,
72 	&efi.smbios3,
73 #ifdef CONFIG_X86_UV
74 	&uv_systab_phys,
75 #endif
76 	&efi_fw_vendor,
77 	&efi_runtime,
78 	&efi_config_table,
79 	&efi.esrt,
80 	&efi_mem_attr_table,
81 #ifdef CONFIG_EFI_RCI2_TABLE
82 	&rci2_table_phys,
83 #endif
84 	&efi.tpm_log,
85 	&efi.tpm_final_log,
86 	&efi_rng_seed,
87 #ifdef CONFIG_LOAD_UEFI_KEYS
88 	&efi.mokvar_table,
89 #endif
90 #ifdef CONFIG_EFI_COCO_SECRET
91 	&efi.coco_secret,
92 #endif
93 #ifdef CONFIG_UNACCEPTED_MEMORY
94 	&efi.unaccepted,
95 #endif
96 };
97 
98 u64 efi_setup;		/* efi setup_data physical address */
99 
100 static int add_efi_memmap __initdata;
101 static int __init setup_add_efi_memmap(char *arg)
102 {
103 	add_efi_memmap = 1;
104 	return 0;
105 }
106 early_param("add_efi_memmap", setup_add_efi_memmap);
107 
108 /*
109  * Tell the kernel about the EFI memory map.  This might include
110  * more than the max 128 entries that can fit in the passed in e820
111  * legacy (zeropage) memory map, but the kernel's e820 table can hold
112  * E820_MAX_ENTRIES.
113  */
114 
115 static void __init do_add_efi_memmap(void)
116 {
117 	efi_memory_desc_t *md;
118 
119 	if (!efi_enabled(EFI_MEMMAP))
120 		return;
121 
122 	for_each_efi_memory_desc(md) {
123 		unsigned long long start = md->phys_addr;
124 		unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
125 		int e820_type;
126 
127 		switch (md->type) {
128 		case EFI_LOADER_CODE:
129 		case EFI_LOADER_DATA:
130 		case EFI_BOOT_SERVICES_CODE:
131 		case EFI_BOOT_SERVICES_DATA:
132 		case EFI_CONVENTIONAL_MEMORY:
133 			if (efi_soft_reserve_enabled()
134 			    && (md->attribute & EFI_MEMORY_SP))
135 				e820_type = E820_TYPE_SOFT_RESERVED;
136 			else if (md->attribute & EFI_MEMORY_WB)
137 				e820_type = E820_TYPE_RAM;
138 			else
139 				e820_type = E820_TYPE_RESERVED;
140 			break;
141 		case EFI_ACPI_RECLAIM_MEMORY:
142 			e820_type = E820_TYPE_ACPI;
143 			break;
144 		case EFI_ACPI_MEMORY_NVS:
145 			e820_type = E820_TYPE_NVS;
146 			break;
147 		case EFI_UNUSABLE_MEMORY:
148 			e820_type = E820_TYPE_UNUSABLE;
149 			break;
150 		case EFI_PERSISTENT_MEMORY:
151 			e820_type = E820_TYPE_PMEM;
152 			break;
153 		default:
154 			/*
155 			 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
156 			 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
157 			 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
158 			 */
159 			e820_type = E820_TYPE_RESERVED;
160 			break;
161 		}
162 
163 		e820__range_add(start, size, e820_type);
164 	}
165 	e820__update_table(e820_table);
166 }
167 
168 /*
169  * Given add_efi_memmap defaults to 0 and there is no alternative
170  * e820 mechanism for soft-reserved memory, import the full EFI memory
171  * map if soft reservations are present and enabled. Otherwise, the
172  * mechanism to disable the kernel's consideration of EFI_MEMORY_SP is
173  * the efi=nosoftreserve option.
174  */
175 static bool do_efi_soft_reserve(void)
176 {
177 	efi_memory_desc_t *md;
178 
179 	if (!efi_enabled(EFI_MEMMAP))
180 		return false;
181 
182 	if (!efi_soft_reserve_enabled())
183 		return false;
184 
185 	for_each_efi_memory_desc(md)
186 		if (md->type == EFI_CONVENTIONAL_MEMORY &&
187 		    (md->attribute & EFI_MEMORY_SP))
188 			return true;
189 	return false;
190 }
191 
192 int __init efi_memblock_x86_reserve_range(void)
193 {
194 	struct efi_info *e = &boot_params.efi_info;
195 	struct efi_memory_map_data data;
196 	phys_addr_t pmap;
197 	int rv;
198 
199 	if (efi_enabled(EFI_PARAVIRT))
200 		return 0;
201 
202 	/* Can't handle firmware tables above 4GB on i386 */
203 	if (IS_ENABLED(CONFIG_X86_32) && e->efi_memmap_hi > 0) {
204 		pr_err("Memory map is above 4GB, disabling EFI.\n");
205 		return -EINVAL;
206 	}
207 	pmap = (phys_addr_t)(e->efi_memmap | ((u64)e->efi_memmap_hi << 32));
208 
209 	data.phys_map		= pmap;
210 	data.size 		= e->efi_memmap_size;
211 	data.desc_size		= e->efi_memdesc_size;
212 	data.desc_version	= e->efi_memdesc_version;
213 
214 	if (!efi_enabled(EFI_PARAVIRT)) {
215 		rv = efi_memmap_init_early(&data);
216 		if (rv)
217 			return rv;
218 	}
219 
220 	if (add_efi_memmap || do_efi_soft_reserve())
221 		do_add_efi_memmap();
222 
223 	WARN(efi.memmap.desc_version != 1,
224 	     "Unexpected EFI_MEMORY_DESCRIPTOR version %ld",
225 	     efi.memmap.desc_version);
226 
227 	memblock_reserve(pmap, efi.memmap.nr_map * efi.memmap.desc_size);
228 	set_bit(EFI_PRESERVE_BS_REGIONS, &efi.flags);
229 
230 	return 0;
231 }
232 
233 #define OVERFLOW_ADDR_SHIFT	(64 - EFI_PAGE_SHIFT)
234 #define OVERFLOW_ADDR_MASK	(U64_MAX << OVERFLOW_ADDR_SHIFT)
235 #define U64_HIGH_BIT		(~(U64_MAX >> 1))
236 
237 static bool __init efi_memmap_entry_valid(const efi_memory_desc_t *md, int i)
238 {
239 	u64 end = (md->num_pages << EFI_PAGE_SHIFT) + md->phys_addr - 1;
240 	u64 end_hi = 0;
241 	char buf[64];
242 
243 	if (md->num_pages == 0) {
244 		end = 0;
245 	} else if (md->num_pages > EFI_PAGES_MAX ||
246 		   EFI_PAGES_MAX - md->num_pages <
247 		   (md->phys_addr >> EFI_PAGE_SHIFT)) {
248 		end_hi = (md->num_pages & OVERFLOW_ADDR_MASK)
249 			>> OVERFLOW_ADDR_SHIFT;
250 
251 		if ((md->phys_addr & U64_HIGH_BIT) && !(end & U64_HIGH_BIT))
252 			end_hi += 1;
253 	} else {
254 		return true;
255 	}
256 
257 	pr_warn_once(FW_BUG "Invalid EFI memory map entries:\n");
258 
259 	if (end_hi) {
260 		pr_warn("mem%02u: %s range=[0x%016llx-0x%llx%016llx] (invalid)\n",
261 			i, efi_md_typeattr_format(buf, sizeof(buf), md),
262 			md->phys_addr, end_hi, end);
263 	} else {
264 		pr_warn("mem%02u: %s range=[0x%016llx-0x%016llx] (invalid)\n",
265 			i, efi_md_typeattr_format(buf, sizeof(buf), md),
266 			md->phys_addr, end);
267 	}
268 	return false;
269 }
270 
271 static void __init efi_clean_memmap(void)
272 {
273 	efi_memory_desc_t *out = efi.memmap.map;
274 	const efi_memory_desc_t *in = out;
275 	const efi_memory_desc_t *end = efi.memmap.map_end;
276 	int i, n_removal;
277 
278 	for (i = n_removal = 0; in < end; i++) {
279 		if (efi_memmap_entry_valid(in, i)) {
280 			if (out != in)
281 				memcpy(out, in, efi.memmap.desc_size);
282 			out = (void *)out + efi.memmap.desc_size;
283 		} else {
284 			n_removal++;
285 		}
286 		in = (void *)in + efi.memmap.desc_size;
287 	}
288 
289 	if (n_removal > 0) {
290 		struct efi_memory_map_data data = {
291 			.phys_map	= efi.memmap.phys_map,
292 			.desc_version	= efi.memmap.desc_version,
293 			.desc_size	= efi.memmap.desc_size,
294 			.size		= efi.memmap.desc_size * (efi.memmap.nr_map - n_removal),
295 			.flags		= 0,
296 		};
297 
298 		pr_warn("Removing %d invalid memory map entries.\n", n_removal);
299 		efi_memmap_install(&data);
300 	}
301 }
302 
303 /*
304  * Firmware can use EfiMemoryMappedIO to request that MMIO regions be
305  * mapped by the OS so they can be accessed by EFI runtime services, but
306  * should have no other significance to the OS (UEFI r2.10, sec 7.2).
307  * However, most bootloaders and EFI stubs convert EfiMemoryMappedIO
308  * regions to E820_TYPE_RESERVED entries, which prevent Linux from
309  * allocating space from them (see remove_e820_regions()).
310  *
311  * Some platforms use EfiMemoryMappedIO entries for PCI MMCONFIG space and
312  * PCI host bridge windows, which means Linux can't allocate BAR space for
313  * hot-added devices.
314  *
315  * Remove large EfiMemoryMappedIO regions from the E820 map to avoid this
316  * problem.
317  *
318  * Retain small EfiMemoryMappedIO regions because on some platforms, these
319  * describe non-window space that's included in host bridge _CRS.  If we
320  * assign that space to PCI devices, they don't work.
321  */
322 static void __init efi_remove_e820_mmio(void)
323 {
324 	efi_memory_desc_t *md;
325 	u64 size, start, end;
326 	int i = 0;
327 
328 	for_each_efi_memory_desc(md) {
329 		if (md->type == EFI_MEMORY_MAPPED_IO) {
330 			size = md->num_pages << EFI_PAGE_SHIFT;
331 			start = md->phys_addr;
332 			end = start + size - 1;
333 			if (size >= 256*1024) {
334 				pr_info("Remove mem%02u: MMIO range=[0x%08llx-0x%08llx] (%lluMB) from e820 map\n",
335 					i, start, end, size >> 20);
336 				e820__range_remove(start, size, E820_TYPE_RESERVED);
337 			} else {
338 				pr_info("Not removing mem%02u: MMIO range=[0x%08llx-0x%08llx] (%lluKB) from e820 map\n",
339 					i, start, end, size >> 10);
340 			}
341 		}
342 		i++;
343 	}
344 }
345 
346 void __init efi_print_memmap(void)
347 {
348 	efi_memory_desc_t *md;
349 	int i = 0;
350 
351 	for_each_efi_memory_desc(md) {
352 		char buf[64];
353 
354 		pr_info("mem%02u: %s range=[0x%016llx-0x%016llx] (%lluMB)\n",
355 			i++, efi_md_typeattr_format(buf, sizeof(buf), md),
356 			md->phys_addr,
357 			md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT) - 1,
358 			(md->num_pages >> (20 - EFI_PAGE_SHIFT)));
359 	}
360 }
361 
362 static int __init efi_systab_init(unsigned long phys)
363 {
364 	int size = efi_enabled(EFI_64BIT) ? sizeof(efi_system_table_64_t)
365 					  : sizeof(efi_system_table_32_t);
366 	const efi_table_hdr_t *hdr;
367 	bool over4g = false;
368 	void *p;
369 	int ret;
370 
371 	hdr = p = early_memremap_ro(phys, size);
372 	if (p == NULL) {
373 		pr_err("Couldn't map the system table!\n");
374 		return -ENOMEM;
375 	}
376 
377 	ret = efi_systab_check_header(hdr);
378 	if (ret) {
379 		early_memunmap(p, size);
380 		return ret;
381 	}
382 
383 	if (efi_enabled(EFI_64BIT)) {
384 		const efi_system_table_64_t *systab64 = p;
385 
386 		efi_runtime	= systab64->runtime;
387 		over4g		= systab64->runtime > U32_MAX;
388 
389 		if (efi_setup) {
390 			struct efi_setup_data *data;
391 
392 			data = early_memremap_ro(efi_setup, sizeof(*data));
393 			if (!data) {
394 				early_memunmap(p, size);
395 				return -ENOMEM;
396 			}
397 
398 			efi_fw_vendor		= (unsigned long)data->fw_vendor;
399 			efi_config_table	= (unsigned long)data->tables;
400 
401 			over4g |= data->fw_vendor	> U32_MAX ||
402 				  data->tables		> U32_MAX;
403 
404 			early_memunmap(data, sizeof(*data));
405 		} else {
406 			efi_fw_vendor		= systab64->fw_vendor;
407 			efi_config_table	= systab64->tables;
408 
409 			over4g |= systab64->fw_vendor	> U32_MAX ||
410 				  systab64->tables	> U32_MAX;
411 		}
412 		efi_nr_tables = systab64->nr_tables;
413 	} else {
414 		const efi_system_table_32_t *systab32 = p;
415 
416 		efi_fw_vendor		= systab32->fw_vendor;
417 		efi_runtime		= systab32->runtime;
418 		efi_config_table	= systab32->tables;
419 		efi_nr_tables		= systab32->nr_tables;
420 	}
421 
422 	efi.runtime_version = hdr->revision;
423 
424 	efi_systab_report_header(hdr, efi_fw_vendor);
425 	early_memunmap(p, size);
426 
427 	if (IS_ENABLED(CONFIG_X86_32) && over4g) {
428 		pr_err("EFI data located above 4GB, disabling EFI.\n");
429 		return -EINVAL;
430 	}
431 
432 	return 0;
433 }
434 
435 static int __init efi_config_init(const efi_config_table_type_t *arch_tables)
436 {
437 	void *config_tables;
438 	int sz, ret;
439 
440 	if (efi_nr_tables == 0)
441 		return 0;
442 
443 	if (efi_enabled(EFI_64BIT))
444 		sz = sizeof(efi_config_table_64_t);
445 	else
446 		sz = sizeof(efi_config_table_32_t);
447 
448 	/*
449 	 * Let's see what config tables the firmware passed to us.
450 	 */
451 	config_tables = early_memremap(efi_config_table, efi_nr_tables * sz);
452 	if (config_tables == NULL) {
453 		pr_err("Could not map Configuration table!\n");
454 		return -ENOMEM;
455 	}
456 
457 	ret = efi_config_parse_tables(config_tables, efi_nr_tables,
458 				      arch_tables);
459 
460 	early_memunmap(config_tables, efi_nr_tables * sz);
461 	return ret;
462 }
463 
464 void __init efi_init(void)
465 {
466 	if (IS_ENABLED(CONFIG_X86_32) &&
467 	    (boot_params.efi_info.efi_systab_hi ||
468 	     boot_params.efi_info.efi_memmap_hi)) {
469 		pr_info("Table located above 4GB, disabling EFI.\n");
470 		return;
471 	}
472 
473 	efi_systab_phys = boot_params.efi_info.efi_systab |
474 			  ((__u64)boot_params.efi_info.efi_systab_hi << 32);
475 
476 	if (efi_systab_init(efi_systab_phys))
477 		return;
478 
479 	if (efi_reuse_config(efi_config_table, efi_nr_tables))
480 		return;
481 
482 	if (efi_config_init(arch_tables))
483 		return;
484 
485 	/*
486 	 * Note: We currently don't support runtime services on an EFI
487 	 * that doesn't match the kernel 32/64-bit mode.
488 	 */
489 
490 	if (!efi_runtime_supported())
491 		pr_err("No EFI runtime due to 32/64-bit mismatch with kernel\n");
492 
493 	if (!efi_runtime_supported() || efi_runtime_disabled()) {
494 		efi_memmap_unmap();
495 		return;
496 	}
497 
498 	set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
499 	efi_clean_memmap();
500 
501 	efi_remove_e820_mmio();
502 
503 	if (efi_enabled(EFI_DBG))
504 		efi_print_memmap();
505 }
506 
507 /* Merge contiguous regions of the same type and attribute */
508 static void __init efi_merge_regions(void)
509 {
510 	efi_memory_desc_t *md, *prev_md = NULL;
511 
512 	for_each_efi_memory_desc(md) {
513 		u64 prev_size;
514 
515 		if (!prev_md) {
516 			prev_md = md;
517 			continue;
518 		}
519 
520 		if (prev_md->type != md->type ||
521 		    prev_md->attribute != md->attribute) {
522 			prev_md = md;
523 			continue;
524 		}
525 
526 		prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
527 
528 		if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
529 			prev_md->num_pages += md->num_pages;
530 			md->type = EFI_RESERVED_TYPE;
531 			md->attribute = 0;
532 			continue;
533 		}
534 		prev_md = md;
535 	}
536 }
537 
538 static void *realloc_pages(void *old_memmap, int old_shift)
539 {
540 	void *ret;
541 
542 	ret = (void *)__get_free_pages(GFP_KERNEL, old_shift + 1);
543 	if (!ret)
544 		goto out;
545 
546 	/*
547 	 * A first-time allocation doesn't have anything to copy.
548 	 */
549 	if (!old_memmap)
550 		return ret;
551 
552 	memcpy(ret, old_memmap, PAGE_SIZE << old_shift);
553 
554 out:
555 	free_pages((unsigned long)old_memmap, old_shift);
556 	return ret;
557 }
558 
559 /*
560  * Iterate the EFI memory map in reverse order because the regions
561  * will be mapped top-down. The end result is the same as if we had
562  * mapped things forward, but doesn't require us to change the
563  * existing implementation of efi_map_region().
564  */
565 static inline void *efi_map_next_entry_reverse(void *entry)
566 {
567 	/* Initial call */
568 	if (!entry)
569 		return efi.memmap.map_end - efi.memmap.desc_size;
570 
571 	entry -= efi.memmap.desc_size;
572 	if (entry < efi.memmap.map)
573 		return NULL;
574 
575 	return entry;
576 }
577 
578 /*
579  * efi_map_next_entry - Return the next EFI memory map descriptor
580  * @entry: Previous EFI memory map descriptor
581  *
582  * This is a helper function to iterate over the EFI memory map, which
583  * we do in different orders depending on the current configuration.
584  *
585  * To begin traversing the memory map @entry must be %NULL.
586  *
587  * Returns %NULL when we reach the end of the memory map.
588  */
589 static void *efi_map_next_entry(void *entry)
590 {
591 	if (efi_enabled(EFI_64BIT)) {
592 		/*
593 		 * Starting in UEFI v2.5 the EFI_PROPERTIES_TABLE
594 		 * config table feature requires us to map all entries
595 		 * in the same order as they appear in the EFI memory
596 		 * map. That is to say, entry N must have a lower
597 		 * virtual address than entry N+1. This is because the
598 		 * firmware toolchain leaves relative references in
599 		 * the code/data sections, which are split and become
600 		 * separate EFI memory regions. Mapping things
601 		 * out-of-order leads to the firmware accessing
602 		 * unmapped addresses.
603 		 *
604 		 * Since we need to map things this way whether or not
605 		 * the kernel actually makes use of
606 		 * EFI_PROPERTIES_TABLE, let's just switch to this
607 		 * scheme by default for 64-bit.
608 		 */
609 		return efi_map_next_entry_reverse(entry);
610 	}
611 
612 	/* Initial call */
613 	if (!entry)
614 		return efi.memmap.map;
615 
616 	entry += efi.memmap.desc_size;
617 	if (entry >= efi.memmap.map_end)
618 		return NULL;
619 
620 	return entry;
621 }
622 
623 static bool should_map_region(efi_memory_desc_t *md)
624 {
625 	/*
626 	 * Runtime regions always require runtime mappings (obviously).
627 	 */
628 	if (md->attribute & EFI_MEMORY_RUNTIME)
629 		return true;
630 
631 	/*
632 	 * 32-bit EFI doesn't suffer from the bug that requires us to
633 	 * reserve boot services regions, and mixed mode support
634 	 * doesn't exist for 32-bit kernels.
635 	 */
636 	if (IS_ENABLED(CONFIG_X86_32))
637 		return false;
638 
639 	/*
640 	 * EFI specific purpose memory may be reserved by default
641 	 * depending on kernel config and boot options.
642 	 */
643 	if (md->type == EFI_CONVENTIONAL_MEMORY &&
644 	    efi_soft_reserve_enabled() &&
645 	    (md->attribute & EFI_MEMORY_SP))
646 		return false;
647 
648 	/*
649 	 * Map all of RAM so that we can access arguments in the 1:1
650 	 * mapping when making EFI runtime calls.
651 	 */
652 	if (efi_is_mixed()) {
653 		if (md->type == EFI_CONVENTIONAL_MEMORY ||
654 		    md->type == EFI_LOADER_DATA ||
655 		    md->type == EFI_LOADER_CODE)
656 			return true;
657 	}
658 
659 	/*
660 	 * Map boot services regions as a workaround for buggy
661 	 * firmware that accesses them even when they shouldn't.
662 	 *
663 	 * See efi_{reserve,free}_boot_services().
664 	 */
665 	if (md->type == EFI_BOOT_SERVICES_CODE ||
666 	    md->type == EFI_BOOT_SERVICES_DATA)
667 		return true;
668 
669 	return false;
670 }
671 
672 /*
673  * Map the efi memory ranges of the runtime services and update new_mmap with
674  * virtual addresses.
675  */
676 static void * __init efi_map_regions(int *count, int *pg_shift)
677 {
678 	void *p, *new_memmap = NULL;
679 	unsigned long left = 0;
680 	unsigned long desc_size;
681 	efi_memory_desc_t *md;
682 
683 	desc_size = efi.memmap.desc_size;
684 
685 	p = NULL;
686 	while ((p = efi_map_next_entry(p))) {
687 		md = p;
688 
689 		if (!should_map_region(md))
690 			continue;
691 
692 		efi_map_region(md);
693 
694 		if (left < desc_size) {
695 			new_memmap = realloc_pages(new_memmap, *pg_shift);
696 			if (!new_memmap)
697 				return NULL;
698 
699 			left += PAGE_SIZE << *pg_shift;
700 			(*pg_shift)++;
701 		}
702 
703 		memcpy(new_memmap + (*count * desc_size), md, desc_size);
704 
705 		left -= desc_size;
706 		(*count)++;
707 	}
708 
709 	return new_memmap;
710 }
711 
712 static void __init kexec_enter_virtual_mode(void)
713 {
714 #ifdef CONFIG_KEXEC_CORE
715 	efi_memory_desc_t *md;
716 	unsigned int num_pages;
717 
718 	/*
719 	 * We don't do virtual mode, since we don't do runtime services, on
720 	 * non-native EFI.
721 	 */
722 	if (efi_is_mixed()) {
723 		efi_memmap_unmap();
724 		clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
725 		return;
726 	}
727 
728 	if (efi_alloc_page_tables()) {
729 		pr_err("Failed to allocate EFI page tables\n");
730 		clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
731 		return;
732 	}
733 
734 	/*
735 	* Map efi regions which were passed via setup_data. The virt_addr is a
736 	* fixed addr which was used in first kernel of a kexec boot.
737 	*/
738 	for_each_efi_memory_desc(md)
739 		efi_map_region_fixed(md); /* FIXME: add error handling */
740 
741 	/*
742 	 * Unregister the early EFI memmap from efi_init() and install
743 	 * the new EFI memory map.
744 	 */
745 	efi_memmap_unmap();
746 
747 	if (efi_memmap_init_late(efi.memmap.phys_map,
748 				 efi.memmap.desc_size * efi.memmap.nr_map)) {
749 		pr_err("Failed to remap late EFI memory map\n");
750 		clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
751 		return;
752 	}
753 
754 	num_pages = ALIGN(efi.memmap.nr_map * efi.memmap.desc_size, PAGE_SIZE);
755 	num_pages >>= PAGE_SHIFT;
756 
757 	if (efi_setup_page_tables(efi.memmap.phys_map, num_pages)) {
758 		clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
759 		return;
760 	}
761 
762 	efi_sync_low_kernel_mappings();
763 	efi_native_runtime_setup();
764 	efi_runtime_update_mappings();
765 #endif
766 }
767 
768 /*
769  * This function will switch the EFI runtime services to virtual mode.
770  * Essentially, we look through the EFI memmap and map every region that
771  * has the runtime attribute bit set in its memory descriptor into the
772  * efi_pgd page table.
773  *
774  * The new method does a pagetable switch in a preemption-safe manner
775  * so that we're in a different address space when calling a runtime
776  * function. For function arguments passing we do copy the PUDs of the
777  * kernel page table into efi_pgd prior to each call.
778  *
779  * Specially for kexec boot, efi runtime maps in previous kernel should
780  * be passed in via setup_data. In that case runtime ranges will be mapped
781  * to the same virtual addresses as the first kernel, see
782  * kexec_enter_virtual_mode().
783  */
784 static void __init __efi_enter_virtual_mode(void)
785 {
786 	int count = 0, pg_shift = 0;
787 	void *new_memmap = NULL;
788 	efi_status_t status;
789 	unsigned long pa;
790 
791 	if (efi_alloc_page_tables()) {
792 		pr_err("Failed to allocate EFI page tables\n");
793 		goto err;
794 	}
795 
796 	efi_merge_regions();
797 	new_memmap = efi_map_regions(&count, &pg_shift);
798 	if (!new_memmap) {
799 		pr_err("Error reallocating memory, EFI runtime non-functional!\n");
800 		goto err;
801 	}
802 
803 	pa = __pa(new_memmap);
804 
805 	/*
806 	 * Unregister the early EFI memmap from efi_init() and install
807 	 * the new EFI memory map that we are about to pass to the
808 	 * firmware via SetVirtualAddressMap().
809 	 */
810 	efi_memmap_unmap();
811 
812 	if (efi_memmap_init_late(pa, efi.memmap.desc_size * count)) {
813 		pr_err("Failed to remap late EFI memory map\n");
814 		goto err;
815 	}
816 
817 	if (efi_enabled(EFI_DBG)) {
818 		pr_info("EFI runtime memory map:\n");
819 		efi_print_memmap();
820 	}
821 
822 	if (efi_setup_page_tables(pa, 1 << pg_shift))
823 		goto err;
824 
825 	efi_sync_low_kernel_mappings();
826 
827 	status = efi_set_virtual_address_map(efi.memmap.desc_size * count,
828 					     efi.memmap.desc_size,
829 					     efi.memmap.desc_version,
830 					     (efi_memory_desc_t *)pa,
831 					     efi_systab_phys);
832 	if (status != EFI_SUCCESS) {
833 		pr_err("Unable to switch EFI into virtual mode (status=%lx)!\n",
834 		       status);
835 		goto err;
836 	}
837 
838 	efi_check_for_embedded_firmwares();
839 	efi_free_boot_services();
840 
841 	if (!efi_is_mixed())
842 		efi_native_runtime_setup();
843 	else
844 		efi_thunk_runtime_setup();
845 
846 	/*
847 	 * Apply more restrictive page table mapping attributes now that
848 	 * SVAM() has been called and the firmware has performed all
849 	 * necessary relocation fixups for the new virtual addresses.
850 	 */
851 	efi_runtime_update_mappings();
852 
853 	/* clean DUMMY object */
854 	efi_delete_dummy_variable();
855 	return;
856 
857 err:
858 	clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
859 }
860 
861 void __init efi_enter_virtual_mode(void)
862 {
863 	if (efi_enabled(EFI_PARAVIRT))
864 		return;
865 
866 	efi.runtime = (efi_runtime_services_t *)efi_runtime;
867 
868 	if (efi_setup)
869 		kexec_enter_virtual_mode();
870 	else
871 		__efi_enter_virtual_mode();
872 
873 	efi_dump_pagetable();
874 }
875 
876 bool efi_is_table_address(unsigned long phys_addr)
877 {
878 	unsigned int i;
879 
880 	if (phys_addr == EFI_INVALID_TABLE_ADDR)
881 		return false;
882 
883 	for (i = 0; i < ARRAY_SIZE(efi_tables); i++)
884 		if (*(efi_tables[i]) == phys_addr)
885 			return true;
886 
887 	return false;
888 }
889 
890 #define EFI_FIELD(var) efi_ ## var
891 
892 #define EFI_ATTR_SHOW(name) \
893 static ssize_t name##_show(struct kobject *kobj, \
894 				struct kobj_attribute *attr, char *buf) \
895 { \
896 	return sprintf(buf, "0x%lx\n", EFI_FIELD(name)); \
897 }
898 
899 EFI_ATTR_SHOW(fw_vendor);
900 EFI_ATTR_SHOW(runtime);
901 EFI_ATTR_SHOW(config_table);
902 
903 struct kobj_attribute efi_attr_fw_vendor = __ATTR_RO(fw_vendor);
904 struct kobj_attribute efi_attr_runtime = __ATTR_RO(runtime);
905 struct kobj_attribute efi_attr_config_table = __ATTR_RO(config_table);
906 
907 umode_t efi_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n)
908 {
909 	if (attr == &efi_attr_fw_vendor.attr) {
910 		if (efi_enabled(EFI_PARAVIRT) ||
911 				efi_fw_vendor == EFI_INVALID_TABLE_ADDR)
912 			return 0;
913 	} else if (attr == &efi_attr_runtime.attr) {
914 		if (efi_runtime == EFI_INVALID_TABLE_ADDR)
915 			return 0;
916 	} else if (attr == &efi_attr_config_table.attr) {
917 		if (efi_config_table == EFI_INVALID_TABLE_ADDR)
918 			return 0;
919 	}
920 	return attr->mode;
921 }
922 
923 enum efi_secureboot_mode __x86_ima_efi_boot_mode(void)
924 {
925 	return boot_params.secure_boot;
926 }
927