12e0eb483SAtish Patra // SPDX-License-Identifier: GPL-2.0-only
22e0eb483SAtish Patra /*
32e0eb483SAtish Patra  * EFI stub implementation that is shared by arm and arm64 architectures.
42e0eb483SAtish Patra  * This should be #included by the EFI stub implementation files.
52e0eb483SAtish Patra  *
62e0eb483SAtish Patra  * Copyright (C) 2013,2014 Linaro Limited
72e0eb483SAtish Patra  *     Roy Franz <roy.franz@linaro.org
82e0eb483SAtish Patra  * Copyright (C) 2013 Red Hat, Inc.
92e0eb483SAtish Patra  *     Mark Salter <msalter@redhat.com>
102e0eb483SAtish Patra  */
112e0eb483SAtish Patra 
122e0eb483SAtish Patra #include <linux/efi.h>
132e0eb483SAtish Patra #include <linux/libfdt.h>
142e0eb483SAtish Patra #include <asm/efi.h>
152e0eb483SAtish Patra 
162e0eb483SAtish Patra #include "efistub.h"
172e0eb483SAtish Patra 
182e0eb483SAtish Patra /*
192e0eb483SAtish Patra  * This is the base address at which to start allocating virtual memory ranges
20b91540d5SAtish Patra  * for UEFI Runtime Services.
21b91540d5SAtish Patra  *
22b91540d5SAtish Patra  * For ARM/ARM64:
23b91540d5SAtish Patra  * This is in the low TTBR0 range so that we can use
242e0eb483SAtish Patra  * any allocation we choose, and eliminate the risk of a conflict after kexec.
252e0eb483SAtish Patra  * The value chosen is the largest non-zero power of 2 suitable for this purpose
262e0eb483SAtish Patra  * both on 32-bit and 64-bit ARM CPUs, to maximize the likelihood that it can
272e0eb483SAtish Patra  * be mapped efficiently.
282e0eb483SAtish Patra  * Since 32-bit ARM could potentially execute with a 1G/3G user/kernel split,
292e0eb483SAtish Patra  * map everything below 1 GB. (512 MB is a reasonable upper bound for the
302e0eb483SAtish Patra  * entire footprint of the UEFI runtime services memory regions)
31b91540d5SAtish Patra  *
32b91540d5SAtish Patra  * For RISC-V:
33b91540d5SAtish Patra  * There is no specific reason for which, this address (512MB) can't be used
34b91540d5SAtish Patra  * EFI runtime virtual address for RISC-V. It also helps to use EFI runtime
35b91540d5SAtish Patra  * services on both RV32/RV64. Keep the same runtime virtual address for RISC-V
36b91540d5SAtish Patra  * as well to minimize the code churn.
372e0eb483SAtish Patra  */
382e0eb483SAtish Patra #define EFI_RT_VIRTUAL_BASE	SZ_512M
392e0eb483SAtish Patra #define EFI_RT_VIRTUAL_SIZE	SZ_512M
402e0eb483SAtish Patra 
412e0eb483SAtish Patra #ifdef CONFIG_ARM64
422e0eb483SAtish Patra # define EFI_RT_VIRTUAL_LIMIT	DEFAULT_MAP_WINDOW_64
43*ead384d9SHuacai Chen #elif defined(CONFIG_RISCV) || defined(CONFIG_LOONGARCH)
44e8a62cc2SAlexandre Ghiti # define EFI_RT_VIRTUAL_LIMIT	TASK_SIZE_MIN
45*ead384d9SHuacai Chen #else /* Only if TASK_SIZE is a constant */
462e0eb483SAtish Patra # define EFI_RT_VIRTUAL_LIMIT	TASK_SIZE
472e0eb483SAtish Patra #endif
482e0eb483SAtish Patra 
49*ead384d9SHuacai Chen /*
50*ead384d9SHuacai Chen  * Some architectures map the EFI regions into the kernel's linear map using a
51*ead384d9SHuacai Chen  * fixed offset.
52*ead384d9SHuacai Chen  */
53*ead384d9SHuacai Chen #ifndef EFI_RT_VIRTUAL_OFFSET
54*ead384d9SHuacai Chen #define EFI_RT_VIRTUAL_OFFSET	0
55*ead384d9SHuacai Chen #endif
56*ead384d9SHuacai Chen 
572e0eb483SAtish Patra static u64 virtmap_base = EFI_RT_VIRTUAL_BASE;
58*ead384d9SHuacai Chen static bool flat_va_mapping = (EFI_RT_VIRTUAL_OFFSET != 0);
592e0eb483SAtish Patra 
60ccc27ae7SArd Biesheuvel const efi_system_table_t *efi_system_table;
612e0eb483SAtish Patra 
622e0eb483SAtish Patra static struct screen_info *setup_graphics(void)
632e0eb483SAtish Patra {
642e0eb483SAtish Patra 	efi_guid_t gop_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
652e0eb483SAtish Patra 	efi_status_t status;
662e0eb483SAtish Patra 	unsigned long size;
672e0eb483SAtish Patra 	void **gop_handle = NULL;
682e0eb483SAtish Patra 	struct screen_info *si = NULL;
692e0eb483SAtish Patra 
702e0eb483SAtish Patra 	size = 0;
712e0eb483SAtish Patra 	status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
722e0eb483SAtish Patra 			     &gop_proto, NULL, &size, gop_handle);
732e0eb483SAtish Patra 	if (status == EFI_BUFFER_TOO_SMALL) {
742e0eb483SAtish Patra 		si = alloc_screen_info();
752e0eb483SAtish Patra 		if (!si)
762e0eb483SAtish Patra 			return NULL;
77a5d8e55bSIngo Molnar 		status = efi_setup_gop(si, &gop_proto, size);
78a5d8e55bSIngo Molnar 		if (status != EFI_SUCCESS) {
79a5d8e55bSIngo Molnar 			free_screen_info(si);
80a5d8e55bSIngo Molnar 			return NULL;
81a5d8e55bSIngo Molnar 		}
822e0eb483SAtish Patra 	}
832e0eb483SAtish Patra 	return si;
842e0eb483SAtish Patra }
852e0eb483SAtish Patra 
8687cd6378SZou Wei static void install_memreserve_table(void)
872e0eb483SAtish Patra {
882e0eb483SAtish Patra 	struct linux_efi_memreserve *rsv;
892e0eb483SAtish Patra 	efi_guid_t memreserve_table_guid = LINUX_EFI_MEMRESERVE_TABLE_GUID;
902e0eb483SAtish Patra 	efi_status_t status;
912e0eb483SAtish Patra 
922e0eb483SAtish Patra 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, sizeof(*rsv),
932e0eb483SAtish Patra 			     (void **)&rsv);
942e0eb483SAtish Patra 	if (status != EFI_SUCCESS) {
95793473c2SArvind Sankar 		efi_err("Failed to allocate memreserve entry!\n");
962e0eb483SAtish Patra 		return;
972e0eb483SAtish Patra 	}
982e0eb483SAtish Patra 
992e0eb483SAtish Patra 	rsv->next = 0;
1002e0eb483SAtish Patra 	rsv->size = 0;
1012e0eb483SAtish Patra 	atomic_set(&rsv->count, 0);
1022e0eb483SAtish Patra 
1032e0eb483SAtish Patra 	status = efi_bs_call(install_configuration_table,
1042e0eb483SAtish Patra 			     &memreserve_table_guid, rsv);
1052e0eb483SAtish Patra 	if (status != EFI_SUCCESS)
106793473c2SArvind Sankar 		efi_err("Failed to install memreserve config table!\n");
1072e0eb483SAtish Patra }
1082e0eb483SAtish Patra 
1099e9888a0SArd Biesheuvel static u32 get_supported_rt_services(void)
1109e9888a0SArd Biesheuvel {
1119e9888a0SArd Biesheuvel 	const efi_rt_properties_table_t *rt_prop_table;
1129e9888a0SArd Biesheuvel 	u32 supported = EFI_RT_SUPPORTED_ALL;
1139e9888a0SArd Biesheuvel 
1149e9888a0SArd Biesheuvel 	rt_prop_table = get_efi_config_table(EFI_RT_PROPERTIES_TABLE_GUID);
1159e9888a0SArd Biesheuvel 	if (rt_prop_table)
1169e9888a0SArd Biesheuvel 		supported &= rt_prop_table->runtime_services_supported;
1179e9888a0SArd Biesheuvel 
1189e9888a0SArd Biesheuvel 	return supported;
1199e9888a0SArd Biesheuvel }
1209e9888a0SArd Biesheuvel 
1212e0eb483SAtish Patra /*
1222e0eb483SAtish Patra  * EFI entry point for the arm/arm64 EFI stubs.  This is the entrypoint
1232e0eb483SAtish Patra  * that is described in the PE/COFF header.  Most of the code is the same
1242e0eb483SAtish Patra  * for both archictectures, with the arch-specific code provided in the
1252e0eb483SAtish Patra  * handle_kernel_image() function.
1262e0eb483SAtish Patra  */
1276e99d321SArd Biesheuvel efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
1286e99d321SArd Biesheuvel 				   efi_system_table_t *sys_table_arg)
1292e0eb483SAtish Patra {
1302e0eb483SAtish Patra 	efi_loaded_image_t *image;
1312e0eb483SAtish Patra 	efi_status_t status;
1322e0eb483SAtish Patra 	unsigned long image_addr;
1332e0eb483SAtish Patra 	unsigned long image_size = 0;
1342e0eb483SAtish Patra 	/* addr/point and size pairs for memory management*/
1352e0eb483SAtish Patra 	unsigned long initrd_addr = 0;
1362e0eb483SAtish Patra 	unsigned long initrd_size = 0;
1372e0eb483SAtish Patra 	unsigned long fdt_addr = 0;  /* Original DTB */
1382e0eb483SAtish Patra 	unsigned long fdt_size = 0;
1392e0eb483SAtish Patra 	char *cmdline_ptr = NULL;
1402e0eb483SAtish Patra 	int cmdline_size = 0;
1412e0eb483SAtish Patra 	efi_guid_t loaded_image_proto = LOADED_IMAGE_PROTOCOL_GUID;
1422e0eb483SAtish Patra 	unsigned long reserve_addr = 0;
1432e0eb483SAtish Patra 	unsigned long reserve_size = 0;
1442e0eb483SAtish Patra 	enum efi_secureboot_mode secure_boot;
1452e0eb483SAtish Patra 	struct screen_info *si;
1462e0eb483SAtish Patra 	efi_properties_table_t *prop_tbl;
1472e0eb483SAtish Patra 
148ccc27ae7SArd Biesheuvel 	efi_system_table = sys_table_arg;
1492e0eb483SAtish Patra 
1502e0eb483SAtish Patra 	/* Check if we were booted by the EFI firmware */
151ccc27ae7SArd Biesheuvel 	if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
1522e0eb483SAtish Patra 		status = EFI_INVALID_PARAMETER;
1532e0eb483SAtish Patra 		goto fail;
1542e0eb483SAtish Patra 	}
1552e0eb483SAtish Patra 
1562e0eb483SAtish Patra 	status = check_platform_features();
1572e0eb483SAtish Patra 	if (status != EFI_SUCCESS)
1582e0eb483SAtish Patra 		goto fail;
1592e0eb483SAtish Patra 
1602e0eb483SAtish Patra 	/*
1612e0eb483SAtish Patra 	 * Get a handle to the loaded image protocol.  This is used to get
1622e0eb483SAtish Patra 	 * information about the running image, such as size and the command
1632e0eb483SAtish Patra 	 * line.
1642e0eb483SAtish Patra 	 */
165ccc27ae7SArd Biesheuvel 	status = efi_system_table->boottime->handle_protocol(handle,
1662e0eb483SAtish Patra 					&loaded_image_proto, (void *)&image);
1672e0eb483SAtish Patra 	if (status != EFI_SUCCESS) {
168793473c2SArvind Sankar 		efi_err("Failed to get loaded image protocol\n");
1692e0eb483SAtish Patra 		goto fail;
1702e0eb483SAtish Patra 	}
1712e0eb483SAtish Patra 
1722e0eb483SAtish Patra 	/*
1732e0eb483SAtish Patra 	 * Get the command line from EFI, using the LOADED_IMAGE
1742e0eb483SAtish Patra 	 * protocol. We are going to copy the command line into the
1752e0eb483SAtish Patra 	 * device tree, so this can be allocated anywhere.
1762e0eb483SAtish Patra 	 */
17727cd5511SArd Biesheuvel 	cmdline_ptr = efi_convert_cmdline(image, &cmdline_size);
1782e0eb483SAtish Patra 	if (!cmdline_ptr) {
179793473c2SArvind Sankar 		efi_err("getting command line via LOADED_IMAGE_PROTOCOL\n");
1802e0eb483SAtish Patra 		status = EFI_OUT_OF_RESOURCES;
1812e0eb483SAtish Patra 		goto fail;
1822e0eb483SAtish Patra 	}
1832e0eb483SAtish Patra 
1842e0eb483SAtish Patra 	if (IS_ENABLED(CONFIG_CMDLINE_EXTEND) ||
1852e0eb483SAtish Patra 	    IS_ENABLED(CONFIG_CMDLINE_FORCE) ||
186055042beSArvind Sankar 	    cmdline_size == 0) {
187055042beSArvind Sankar 		status = efi_parse_options(CONFIG_CMDLINE);
188055042beSArvind Sankar 		if (status != EFI_SUCCESS) {
189055042beSArvind Sankar 			efi_err("Failed to parse options\n");
190055042beSArvind Sankar 			goto fail_free_cmdline;
191055042beSArvind Sankar 		}
192055042beSArvind Sankar 	}
1932e0eb483SAtish Patra 
194055042beSArvind Sankar 	if (!IS_ENABLED(CONFIG_CMDLINE_FORCE) && cmdline_size > 0) {
195055042beSArvind Sankar 		status = efi_parse_options(cmdline_ptr);
196055042beSArvind Sankar 		if (status != EFI_SUCCESS) {
197055042beSArvind Sankar 			efi_err("Failed to parse options\n");
198055042beSArvind Sankar 			goto fail_free_cmdline;
199055042beSArvind Sankar 		}
200055042beSArvind Sankar 	}
2012e0eb483SAtish Patra 
202793473c2SArvind Sankar 	efi_info("Booting Linux Kernel...\n");
2032e0eb483SAtish Patra 
2042e0eb483SAtish Patra 	si = setup_graphics();
2052e0eb483SAtish Patra 
2062e0eb483SAtish Patra 	status = handle_kernel_image(&image_addr, &image_size,
2072e0eb483SAtish Patra 				     &reserve_addr,
2082e0eb483SAtish Patra 				     &reserve_size,
209416a9f84SArd Biesheuvel 				     image, handle);
2102e0eb483SAtish Patra 	if (status != EFI_SUCCESS) {
211793473c2SArvind Sankar 		efi_err("Failed to relocate kernel\n");
212055042beSArvind Sankar 		goto fail_free_screeninfo;
2132e0eb483SAtish Patra 	}
2142e0eb483SAtish Patra 
2152e0eb483SAtish Patra 	efi_retrieve_tpm2_eventlog();
2162e0eb483SAtish Patra 
2172e0eb483SAtish Patra 	/* Ask the firmware to clear memory on unclean shutdown */
2182e0eb483SAtish Patra 	efi_enable_reset_attack_mitigation();
2192e0eb483SAtish Patra 
2202e0eb483SAtish Patra 	secure_boot = efi_get_secureboot();
2212e0eb483SAtish Patra 
2222e0eb483SAtish Patra 	/*
2232e0eb483SAtish Patra 	 * Unauthenticated device tree data is a security hazard, so ignore
2242e0eb483SAtish Patra 	 * 'dtb=' unless UEFI Secure Boot is disabled.  We assume that secure
2252e0eb483SAtish Patra 	 * boot is enabled if we can't determine its state.
2262e0eb483SAtish Patra 	 */
2272e0eb483SAtish Patra 	if (!IS_ENABLED(CONFIG_EFI_ARMSTUB_DTB_LOADER) ||
2282e0eb483SAtish Patra 	     secure_boot != efi_secureboot_mode_disabled) {
2292e0eb483SAtish Patra 		if (strstr(cmdline_ptr, "dtb="))
2303839ab85SArvind Sankar 			efi_err("Ignoring DTB from command line.\n");
2312e0eb483SAtish Patra 	} else {
2322e0eb483SAtish Patra 		status = efi_load_dtb(image, &fdt_addr, &fdt_size);
2332e0eb483SAtish Patra 
2342e0eb483SAtish Patra 		if (status != EFI_SUCCESS) {
235793473c2SArvind Sankar 			efi_err("Failed to load device tree!\n");
2362e0eb483SAtish Patra 			goto fail_free_image;
2372e0eb483SAtish Patra 		}
2382e0eb483SAtish Patra 	}
2392e0eb483SAtish Patra 
2402e0eb483SAtish Patra 	if (fdt_addr) {
241793473c2SArvind Sankar 		efi_info("Using DTB from command line\n");
2422e0eb483SAtish Patra 	} else {
2432e0eb483SAtish Patra 		/* Look for a device tree configuration table entry. */
2442e0eb483SAtish Patra 		fdt_addr = (uintptr_t)get_fdt(&fdt_size);
2452e0eb483SAtish Patra 		if (fdt_addr)
246793473c2SArvind Sankar 			efi_info("Using DTB from configuration table\n");
2472e0eb483SAtish Patra 	}
2482e0eb483SAtish Patra 
2492e0eb483SAtish Patra 	if (!fdt_addr)
250793473c2SArvind Sankar 		efi_info("Generating empty DTB\n");
2512e0eb483SAtish Patra 
25220287d56SArd Biesheuvel 	efi_load_initrd(image, &initrd_addr, &initrd_size, ULONG_MAX,
25320287d56SArd Biesheuvel 			efi_get_max_initrd_addr(image_addr));
2542e0eb483SAtish Patra 
2552e0eb483SAtish Patra 	efi_random_get_seed();
2562e0eb483SAtish Patra 
2572e0eb483SAtish Patra 	/*
2582e0eb483SAtish Patra 	 * If the NX PE data feature is enabled in the properties table, we
2592e0eb483SAtish Patra 	 * should take care not to create a virtual mapping that changes the
2602e0eb483SAtish Patra 	 * relative placement of runtime services code and data regions, as
2612e0eb483SAtish Patra 	 * they may belong to the same PE/COFF executable image in memory.
2622e0eb483SAtish Patra 	 * The easiest way to achieve that is to simply use a 1:1 mapping.
2632e0eb483SAtish Patra 	 */
2642e0eb483SAtish Patra 	prop_tbl = get_efi_config_table(EFI_PROPERTIES_TABLE_GUID);
265*ead384d9SHuacai Chen 	flat_va_mapping |= prop_tbl &&
2662e0eb483SAtish Patra 			   (prop_tbl->memory_protection_attribute &
2672e0eb483SAtish Patra 			   EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA);
2682e0eb483SAtish Patra 
2699e9888a0SArd Biesheuvel 	/* force efi_novamap if SetVirtualAddressMap() is unsupported */
2709e9888a0SArd Biesheuvel 	efi_novamap |= !(get_supported_rt_services() &
2719e9888a0SArd Biesheuvel 			 EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP);
2729e9888a0SArd Biesheuvel 
2732e0eb483SAtish Patra 	/* hibernation expects the runtime regions to stay in the same place */
274980771f6SArd Biesheuvel 	if (!IS_ENABLED(CONFIG_HIBERNATION) && !efi_nokaslr && !flat_va_mapping) {
2752e0eb483SAtish Patra 		/*
2762e0eb483SAtish Patra 		 * Randomize the base of the UEFI runtime services region.
2772e0eb483SAtish Patra 		 * Preserve the 2 MB alignment of the region by taking a
2782e0eb483SAtish Patra 		 * shift of 21 bit positions into account when scaling
2792e0eb483SAtish Patra 		 * the headroom value using a 32-bit random value.
2802e0eb483SAtish Patra 		 */
2812e0eb483SAtish Patra 		static const u64 headroom = EFI_RT_VIRTUAL_LIMIT -
2822e0eb483SAtish Patra 					    EFI_RT_VIRTUAL_BASE -
2832e0eb483SAtish Patra 					    EFI_RT_VIRTUAL_SIZE;
2842e0eb483SAtish Patra 		u32 rnd;
2852e0eb483SAtish Patra 
2862e0eb483SAtish Patra 		status = efi_get_random_bytes(sizeof(rnd), (u8 *)&rnd);
2872e0eb483SAtish Patra 		if (status == EFI_SUCCESS) {
2882e0eb483SAtish Patra 			virtmap_base = EFI_RT_VIRTUAL_BASE +
2892e0eb483SAtish Patra 				       (((headroom >> 21) * rnd) >> (32 - 21));
2902e0eb483SAtish Patra 		}
2912e0eb483SAtish Patra 	}
2922e0eb483SAtish Patra 
2932e0eb483SAtish Patra 	install_memreserve_table();
2942e0eb483SAtish Patra 
2952e0eb483SAtish Patra 	status = allocate_new_fdt_and_exit_boot(handle, &fdt_addr,
2962e0eb483SAtish Patra 						initrd_addr, initrd_size,
2972e0eb483SAtish Patra 						cmdline_ptr, fdt_addr, fdt_size);
2982e0eb483SAtish Patra 	if (status != EFI_SUCCESS)
2992e0eb483SAtish Patra 		goto fail_free_initrd;
3002e0eb483SAtish Patra 
3012a55280aSArd Biesheuvel 	if (IS_ENABLED(CONFIG_ARM))
3022a55280aSArd Biesheuvel 		efi_handle_post_ebs_state();
3032a55280aSArd Biesheuvel 
3042e0eb483SAtish Patra 	efi_enter_kernel(image_addr, fdt_addr, fdt_totalsize((void *)fdt_addr));
3052e0eb483SAtish Patra 	/* not reached */
3062e0eb483SAtish Patra 
3072e0eb483SAtish Patra fail_free_initrd:
308793473c2SArvind Sankar 	efi_err("Failed to update FDT and exit boot services\n");
3092e0eb483SAtish Patra 
3102e0eb483SAtish Patra 	efi_free(initrd_size, initrd_addr);
3112e0eb483SAtish Patra 	efi_free(fdt_size, fdt_addr);
3122e0eb483SAtish Patra 
3132e0eb483SAtish Patra fail_free_image:
3142e0eb483SAtish Patra 	efi_free(image_size, image_addr);
3152e0eb483SAtish Patra 	efi_free(reserve_size, reserve_addr);
316055042beSArvind Sankar fail_free_screeninfo:
3172e0eb483SAtish Patra 	free_screen_info(si);
318055042beSArvind Sankar fail_free_cmdline:
31927cd5511SArd Biesheuvel 	efi_bs_call(free_pool, cmdline_ptr);
3202e0eb483SAtish Patra fail:
3212e0eb483SAtish Patra 	return status;
3222e0eb483SAtish Patra }
3232e0eb483SAtish Patra 
3242e0eb483SAtish Patra /*
3252e0eb483SAtish Patra  * efi_get_virtmap() - create a virtual mapping for the EFI memory map
3262e0eb483SAtish Patra  *
3272e0eb483SAtish Patra  * This function populates the virt_addr fields of all memory region descriptors
3282e0eb483SAtish Patra  * in @memory_map whose EFI_MEMORY_RUNTIME attribute is set. Those descriptors
3292e0eb483SAtish Patra  * are also copied to @runtime_map, and their total count is returned in @count.
3302e0eb483SAtish Patra  */
3312e0eb483SAtish Patra void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size,
3322e0eb483SAtish Patra 		     unsigned long desc_size, efi_memory_desc_t *runtime_map,
3332e0eb483SAtish Patra 		     int *count)
3342e0eb483SAtish Patra {
3352e0eb483SAtish Patra 	u64 efi_virt_base = virtmap_base;
3362e0eb483SAtish Patra 	efi_memory_desc_t *in, *out = runtime_map;
3372e0eb483SAtish Patra 	int l;
3382e0eb483SAtish Patra 
3392e0eb483SAtish Patra 	for (l = 0; l < map_size; l += desc_size) {
3402e0eb483SAtish Patra 		u64 paddr, size;
3412e0eb483SAtish Patra 
3422e0eb483SAtish Patra 		in = (void *)memory_map + l;
3432e0eb483SAtish Patra 		if (!(in->attribute & EFI_MEMORY_RUNTIME))
3442e0eb483SAtish Patra 			continue;
3452e0eb483SAtish Patra 
3462e0eb483SAtish Patra 		paddr = in->phys_addr;
3472e0eb483SAtish Patra 		size = in->num_pages * EFI_PAGE_SIZE;
3482e0eb483SAtish Patra 
349*ead384d9SHuacai Chen 		in->virt_addr = in->phys_addr + EFI_RT_VIRTUAL_OFFSET;
350980771f6SArd Biesheuvel 		if (efi_novamap) {
3512e0eb483SAtish Patra 			continue;
3522e0eb483SAtish Patra 		}
3532e0eb483SAtish Patra 
3542e0eb483SAtish Patra 		/*
3552e0eb483SAtish Patra 		 * Make the mapping compatible with 64k pages: this allows
3562e0eb483SAtish Patra 		 * a 4k page size kernel to kexec a 64k page size kernel and
3572e0eb483SAtish Patra 		 * vice versa.
3582e0eb483SAtish Patra 		 */
3592e0eb483SAtish Patra 		if (!flat_va_mapping) {
3602e0eb483SAtish Patra 
3612e0eb483SAtish Patra 			paddr = round_down(in->phys_addr, SZ_64K);
3622e0eb483SAtish Patra 			size += in->phys_addr - paddr;
3632e0eb483SAtish Patra 
3642e0eb483SAtish Patra 			/*
3652e0eb483SAtish Patra 			 * Avoid wasting memory on PTEs by choosing a virtual
3662e0eb483SAtish Patra 			 * base that is compatible with section mappings if this
3672e0eb483SAtish Patra 			 * region has the appropriate size and physical
3682e0eb483SAtish Patra 			 * alignment. (Sections are 2 MB on 4k granule kernels)
3692e0eb483SAtish Patra 			 */
3702e0eb483SAtish Patra 			if (IS_ALIGNED(in->phys_addr, SZ_2M) && size >= SZ_2M)
3712e0eb483SAtish Patra 				efi_virt_base = round_up(efi_virt_base, SZ_2M);
3722e0eb483SAtish Patra 			else
3732e0eb483SAtish Patra 				efi_virt_base = round_up(efi_virt_base, SZ_64K);
3742e0eb483SAtish Patra 
3752e0eb483SAtish Patra 			in->virt_addr += efi_virt_base - paddr;
3762e0eb483SAtish Patra 			efi_virt_base += size;
3772e0eb483SAtish Patra 		}
3782e0eb483SAtish Patra 
3792e0eb483SAtish Patra 		memcpy(out, in, desc_size);
3802e0eb483SAtish Patra 		out = (void *)out + desc_size;
3812e0eb483SAtish Patra 		++*count;
3822e0eb483SAtish Patra 	}
3832e0eb483SAtish Patra }
384