xref: /OK3568_Linux_fs/kernel/drivers/firmware/efi/libstub/efi-stub.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * EFI stub implementation that is shared by arm and arm64 architectures.
4*4882a593Smuzhiyun  * This should be #included by the EFI stub implementation files.
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Copyright (C) 2013,2014 Linaro Limited
7*4882a593Smuzhiyun  *     Roy Franz <roy.franz@linaro.org
8*4882a593Smuzhiyun  * Copyright (C) 2013 Red Hat, Inc.
9*4882a593Smuzhiyun  *     Mark Salter <msalter@redhat.com>
10*4882a593Smuzhiyun  */
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include <linux/efi.h>
13*4882a593Smuzhiyun #include <linux/libfdt.h>
14*4882a593Smuzhiyun #include <asm/efi.h>
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #include "efistub.h"
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun /*
19*4882a593Smuzhiyun  * This is the base address at which to start allocating virtual memory ranges
20*4882a593Smuzhiyun  * for UEFI Runtime Services.
21*4882a593Smuzhiyun  *
22*4882a593Smuzhiyun  * For ARM/ARM64:
23*4882a593Smuzhiyun  * This is in the low TTBR0 range so that we can use
24*4882a593Smuzhiyun  * any allocation we choose, and eliminate the risk of a conflict after kexec.
25*4882a593Smuzhiyun  * The value chosen is the largest non-zero power of 2 suitable for this purpose
26*4882a593Smuzhiyun  * both on 32-bit and 64-bit ARM CPUs, to maximize the likelihood that it can
27*4882a593Smuzhiyun  * be mapped efficiently.
28*4882a593Smuzhiyun  * Since 32-bit ARM could potentially execute with a 1G/3G user/kernel split,
29*4882a593Smuzhiyun  * map everything below 1 GB. (512 MB is a reasonable upper bound for the
30*4882a593Smuzhiyun  * entire footprint of the UEFI runtime services memory regions)
31*4882a593Smuzhiyun  *
32*4882a593Smuzhiyun  * For RISC-V:
33*4882a593Smuzhiyun  * There is no specific reason for which, this address (512MB) can't be used
34*4882a593Smuzhiyun  * EFI runtime virtual address for RISC-V. It also helps to use EFI runtime
35*4882a593Smuzhiyun  * services on both RV32/RV64. Keep the same runtime virtual address for RISC-V
36*4882a593Smuzhiyun  * as well to minimize the code churn.
37*4882a593Smuzhiyun  */
38*4882a593Smuzhiyun #define EFI_RT_VIRTUAL_BASE	SZ_512M
39*4882a593Smuzhiyun #define EFI_RT_VIRTUAL_SIZE	SZ_512M
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun #ifdef CONFIG_ARM64
42*4882a593Smuzhiyun # define EFI_RT_VIRTUAL_LIMIT	DEFAULT_MAP_WINDOW_64
43*4882a593Smuzhiyun #else
44*4882a593Smuzhiyun # define EFI_RT_VIRTUAL_LIMIT	TASK_SIZE
45*4882a593Smuzhiyun #endif
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun static u64 virtmap_base = EFI_RT_VIRTUAL_BASE;
48*4882a593Smuzhiyun static bool flat_va_mapping;
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun const efi_system_table_t *efi_system_table;
51*4882a593Smuzhiyun 
setup_graphics(void)52*4882a593Smuzhiyun static struct screen_info *setup_graphics(void)
53*4882a593Smuzhiyun {
54*4882a593Smuzhiyun 	efi_guid_t gop_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
55*4882a593Smuzhiyun 	efi_status_t status;
56*4882a593Smuzhiyun 	unsigned long size;
57*4882a593Smuzhiyun 	void **gop_handle = NULL;
58*4882a593Smuzhiyun 	struct screen_info *si = NULL;
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	size = 0;
61*4882a593Smuzhiyun 	status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
62*4882a593Smuzhiyun 			     &gop_proto, NULL, &size, gop_handle);
63*4882a593Smuzhiyun 	if (status == EFI_BUFFER_TOO_SMALL) {
64*4882a593Smuzhiyun 		si = alloc_screen_info();
65*4882a593Smuzhiyun 		if (!si)
66*4882a593Smuzhiyun 			return NULL;
67*4882a593Smuzhiyun 		status = efi_setup_gop(si, &gop_proto, size);
68*4882a593Smuzhiyun 		if (status != EFI_SUCCESS) {
69*4882a593Smuzhiyun 			free_screen_info(si);
70*4882a593Smuzhiyun 			return NULL;
71*4882a593Smuzhiyun 		}
72*4882a593Smuzhiyun 	}
73*4882a593Smuzhiyun 	return si;
74*4882a593Smuzhiyun }
75*4882a593Smuzhiyun 
install_memreserve_table(void)76*4882a593Smuzhiyun static void install_memreserve_table(void)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun 	struct linux_efi_memreserve *rsv;
79*4882a593Smuzhiyun 	efi_guid_t memreserve_table_guid = LINUX_EFI_MEMRESERVE_TABLE_GUID;
80*4882a593Smuzhiyun 	efi_status_t status;
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, sizeof(*rsv),
83*4882a593Smuzhiyun 			     (void **)&rsv);
84*4882a593Smuzhiyun 	if (status != EFI_SUCCESS) {
85*4882a593Smuzhiyun 		efi_err("Failed to allocate memreserve entry!\n");
86*4882a593Smuzhiyun 		return;
87*4882a593Smuzhiyun 	}
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	rsv->next = 0;
90*4882a593Smuzhiyun 	rsv->size = 0;
91*4882a593Smuzhiyun 	atomic_set(&rsv->count, 0);
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	status = efi_bs_call(install_configuration_table,
94*4882a593Smuzhiyun 			     &memreserve_table_guid, rsv);
95*4882a593Smuzhiyun 	if (status != EFI_SUCCESS)
96*4882a593Smuzhiyun 		efi_err("Failed to install memreserve config table!\n");
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun 
get_supported_rt_services(void)99*4882a593Smuzhiyun static u32 get_supported_rt_services(void)
100*4882a593Smuzhiyun {
101*4882a593Smuzhiyun 	const efi_rt_properties_table_t *rt_prop_table;
102*4882a593Smuzhiyun 	u32 supported = EFI_RT_SUPPORTED_ALL;
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun 	rt_prop_table = get_efi_config_table(EFI_RT_PROPERTIES_TABLE_GUID);
105*4882a593Smuzhiyun 	if (rt_prop_table)
106*4882a593Smuzhiyun 		supported &= rt_prop_table->runtime_services_supported;
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	return supported;
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun /*
112*4882a593Smuzhiyun  * EFI entry point for the arm/arm64 EFI stubs.  This is the entrypoint
113*4882a593Smuzhiyun  * that is described in the PE/COFF header.  Most of the code is the same
114*4882a593Smuzhiyun  * for both archictectures, with the arch-specific code provided in the
115*4882a593Smuzhiyun  * handle_kernel_image() function.
116*4882a593Smuzhiyun  */
efi_pe_entry(efi_handle_t handle,efi_system_table_t * sys_table_arg)117*4882a593Smuzhiyun efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
118*4882a593Smuzhiyun 				   efi_system_table_t *sys_table_arg)
119*4882a593Smuzhiyun {
120*4882a593Smuzhiyun 	efi_loaded_image_t *image;
121*4882a593Smuzhiyun 	efi_status_t status;
122*4882a593Smuzhiyun 	unsigned long image_addr;
123*4882a593Smuzhiyun 	unsigned long image_size = 0;
124*4882a593Smuzhiyun 	/* addr/point and size pairs for memory management*/
125*4882a593Smuzhiyun 	unsigned long initrd_addr = 0;
126*4882a593Smuzhiyun 	unsigned long initrd_size = 0;
127*4882a593Smuzhiyun 	unsigned long fdt_addr = 0;  /* Original DTB */
128*4882a593Smuzhiyun 	unsigned long fdt_size = 0;
129*4882a593Smuzhiyun 	char *cmdline_ptr = NULL;
130*4882a593Smuzhiyun 	int cmdline_size = 0;
131*4882a593Smuzhiyun 	efi_guid_t loaded_image_proto = LOADED_IMAGE_PROTOCOL_GUID;
132*4882a593Smuzhiyun 	unsigned long reserve_addr = 0;
133*4882a593Smuzhiyun 	unsigned long reserve_size = 0;
134*4882a593Smuzhiyun 	enum efi_secureboot_mode secure_boot;
135*4882a593Smuzhiyun 	struct screen_info *si;
136*4882a593Smuzhiyun 	efi_properties_table_t *prop_tbl;
137*4882a593Smuzhiyun 	unsigned long max_addr;
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	efi_system_table = sys_table_arg;
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 	/* Check if we were booted by the EFI firmware */
142*4882a593Smuzhiyun 	if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
143*4882a593Smuzhiyun 		status = EFI_INVALID_PARAMETER;
144*4882a593Smuzhiyun 		goto fail;
145*4882a593Smuzhiyun 	}
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	status = check_platform_features();
148*4882a593Smuzhiyun 	if (status != EFI_SUCCESS)
149*4882a593Smuzhiyun 		goto fail;
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	/*
152*4882a593Smuzhiyun 	 * Get a handle to the loaded image protocol.  This is used to get
153*4882a593Smuzhiyun 	 * information about the running image, such as size and the command
154*4882a593Smuzhiyun 	 * line.
155*4882a593Smuzhiyun 	 */
156*4882a593Smuzhiyun 	status = efi_system_table->boottime->handle_protocol(handle,
157*4882a593Smuzhiyun 					&loaded_image_proto, (void *)&image);
158*4882a593Smuzhiyun 	if (status != EFI_SUCCESS) {
159*4882a593Smuzhiyun 		efi_err("Failed to get loaded image protocol\n");
160*4882a593Smuzhiyun 		goto fail;
161*4882a593Smuzhiyun 	}
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	/*
164*4882a593Smuzhiyun 	 * Get the command line from EFI, using the LOADED_IMAGE
165*4882a593Smuzhiyun 	 * protocol. We are going to copy the command line into the
166*4882a593Smuzhiyun 	 * device tree, so this can be allocated anywhere.
167*4882a593Smuzhiyun 	 */
168*4882a593Smuzhiyun 	cmdline_ptr = efi_convert_cmdline(image, &cmdline_size);
169*4882a593Smuzhiyun 	if (!cmdline_ptr) {
170*4882a593Smuzhiyun 		efi_err("getting command line via LOADED_IMAGE_PROTOCOL\n");
171*4882a593Smuzhiyun 		status = EFI_OUT_OF_RESOURCES;
172*4882a593Smuzhiyun 		goto fail;
173*4882a593Smuzhiyun 	}
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 	if (IS_ENABLED(CONFIG_CMDLINE_EXTEND) ||
176*4882a593Smuzhiyun 	    IS_ENABLED(CONFIG_CMDLINE_FORCE) ||
177*4882a593Smuzhiyun 	    cmdline_size == 0) {
178*4882a593Smuzhiyun 		status = efi_parse_options(CONFIG_CMDLINE);
179*4882a593Smuzhiyun 		if (status != EFI_SUCCESS) {
180*4882a593Smuzhiyun 			efi_err("Failed to parse options\n");
181*4882a593Smuzhiyun 			goto fail_free_cmdline;
182*4882a593Smuzhiyun 		}
183*4882a593Smuzhiyun 	}
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	if (!IS_ENABLED(CONFIG_CMDLINE_FORCE) && cmdline_size > 0) {
186*4882a593Smuzhiyun 		status = efi_parse_options(cmdline_ptr);
187*4882a593Smuzhiyun 		if (status != EFI_SUCCESS) {
188*4882a593Smuzhiyun 			efi_err("Failed to parse options\n");
189*4882a593Smuzhiyun 			goto fail_free_cmdline;
190*4882a593Smuzhiyun 		}
191*4882a593Smuzhiyun 	}
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	efi_info("Booting Linux Kernel...\n");
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun 	si = setup_graphics();
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 	status = handle_kernel_image(&image_addr, &image_size,
198*4882a593Smuzhiyun 				     &reserve_addr,
199*4882a593Smuzhiyun 				     &reserve_size,
200*4882a593Smuzhiyun 				     image);
201*4882a593Smuzhiyun 	if (status != EFI_SUCCESS) {
202*4882a593Smuzhiyun 		efi_err("Failed to relocate kernel\n");
203*4882a593Smuzhiyun 		goto fail_free_screeninfo;
204*4882a593Smuzhiyun 	}
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 	efi_retrieve_tpm2_eventlog();
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 	/* Ask the firmware to clear memory on unclean shutdown */
209*4882a593Smuzhiyun 	efi_enable_reset_attack_mitigation();
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun 	secure_boot = efi_get_secureboot();
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 	/*
214*4882a593Smuzhiyun 	 * Unauthenticated device tree data is a security hazard, so ignore
215*4882a593Smuzhiyun 	 * 'dtb=' unless UEFI Secure Boot is disabled.  We assume that secure
216*4882a593Smuzhiyun 	 * boot is enabled if we can't determine its state.
217*4882a593Smuzhiyun 	 */
218*4882a593Smuzhiyun 	if (!IS_ENABLED(CONFIG_EFI_ARMSTUB_DTB_LOADER) ||
219*4882a593Smuzhiyun 	     secure_boot != efi_secureboot_mode_disabled) {
220*4882a593Smuzhiyun 		if (strstr(cmdline_ptr, "dtb="))
221*4882a593Smuzhiyun 			efi_err("Ignoring DTB from command line.\n");
222*4882a593Smuzhiyun 	} else {
223*4882a593Smuzhiyun 		status = efi_load_dtb(image, &fdt_addr, &fdt_size);
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 		if (status != EFI_SUCCESS) {
226*4882a593Smuzhiyun 			efi_err("Failed to load device tree!\n");
227*4882a593Smuzhiyun 			goto fail_free_image;
228*4882a593Smuzhiyun 		}
229*4882a593Smuzhiyun 	}
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 	if (fdt_addr) {
232*4882a593Smuzhiyun 		efi_info("Using DTB from command line\n");
233*4882a593Smuzhiyun 	} else {
234*4882a593Smuzhiyun 		/* Look for a device tree configuration table entry. */
235*4882a593Smuzhiyun 		fdt_addr = (uintptr_t)get_fdt(&fdt_size);
236*4882a593Smuzhiyun 		if (fdt_addr)
237*4882a593Smuzhiyun 			efi_info("Using DTB from configuration table\n");
238*4882a593Smuzhiyun 	}
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun 	if (!fdt_addr)
241*4882a593Smuzhiyun 		efi_info("Generating empty DTB\n");
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun 	if (!efi_noinitrd) {
244*4882a593Smuzhiyun 		max_addr = efi_get_max_initrd_addr(image_addr);
245*4882a593Smuzhiyun 		status = efi_load_initrd(image, &initrd_addr, &initrd_size,
246*4882a593Smuzhiyun 					 ULONG_MAX, max_addr);
247*4882a593Smuzhiyun 		if (status != EFI_SUCCESS)
248*4882a593Smuzhiyun 			efi_err("Failed to load initrd!\n");
249*4882a593Smuzhiyun 	}
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	efi_random_get_seed();
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun 	/*
254*4882a593Smuzhiyun 	 * If the NX PE data feature is enabled in the properties table, we
255*4882a593Smuzhiyun 	 * should take care not to create a virtual mapping that changes the
256*4882a593Smuzhiyun 	 * relative placement of runtime services code and data regions, as
257*4882a593Smuzhiyun 	 * they may belong to the same PE/COFF executable image in memory.
258*4882a593Smuzhiyun 	 * The easiest way to achieve that is to simply use a 1:1 mapping.
259*4882a593Smuzhiyun 	 */
260*4882a593Smuzhiyun 	prop_tbl = get_efi_config_table(EFI_PROPERTIES_TABLE_GUID);
261*4882a593Smuzhiyun 	flat_va_mapping = prop_tbl &&
262*4882a593Smuzhiyun 			  (prop_tbl->memory_protection_attribute &
263*4882a593Smuzhiyun 			   EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA);
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun 	/* force efi_novamap if SetVirtualAddressMap() is unsupported */
266*4882a593Smuzhiyun 	efi_novamap |= !(get_supported_rt_services() &
267*4882a593Smuzhiyun 			 EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP);
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	/* hibernation expects the runtime regions to stay in the same place */
270*4882a593Smuzhiyun 	if (!IS_ENABLED(CONFIG_HIBERNATION) && !efi_nokaslr && !flat_va_mapping) {
271*4882a593Smuzhiyun 		/*
272*4882a593Smuzhiyun 		 * Randomize the base of the UEFI runtime services region.
273*4882a593Smuzhiyun 		 * Preserve the 2 MB alignment of the region by taking a
274*4882a593Smuzhiyun 		 * shift of 21 bit positions into account when scaling
275*4882a593Smuzhiyun 		 * the headroom value using a 32-bit random value.
276*4882a593Smuzhiyun 		 */
277*4882a593Smuzhiyun 		static const u64 headroom = EFI_RT_VIRTUAL_LIMIT -
278*4882a593Smuzhiyun 					    EFI_RT_VIRTUAL_BASE -
279*4882a593Smuzhiyun 					    EFI_RT_VIRTUAL_SIZE;
280*4882a593Smuzhiyun 		u32 rnd;
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun 		status = efi_get_random_bytes(sizeof(rnd), (u8 *)&rnd);
283*4882a593Smuzhiyun 		if (status == EFI_SUCCESS) {
284*4882a593Smuzhiyun 			virtmap_base = EFI_RT_VIRTUAL_BASE +
285*4882a593Smuzhiyun 				       (((headroom >> 21) * rnd) >> (32 - 21));
286*4882a593Smuzhiyun 		}
287*4882a593Smuzhiyun 	}
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun 	install_memreserve_table();
290*4882a593Smuzhiyun 
291*4882a593Smuzhiyun 	status = allocate_new_fdt_and_exit_boot(handle, &fdt_addr,
292*4882a593Smuzhiyun 						efi_get_max_fdt_addr(image_addr),
293*4882a593Smuzhiyun 						initrd_addr, initrd_size,
294*4882a593Smuzhiyun 						cmdline_ptr, fdt_addr, fdt_size);
295*4882a593Smuzhiyun 	if (status != EFI_SUCCESS)
296*4882a593Smuzhiyun 		goto fail_free_initrd;
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 	if (IS_ENABLED(CONFIG_ARM))
299*4882a593Smuzhiyun 		efi_handle_post_ebs_state();
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun 	efi_enter_kernel(image_addr, fdt_addr, fdt_totalsize((void *)fdt_addr));
302*4882a593Smuzhiyun 	/* not reached */
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun fail_free_initrd:
305*4882a593Smuzhiyun 	efi_err("Failed to update FDT and exit boot services\n");
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun 	efi_free(initrd_size, initrd_addr);
308*4882a593Smuzhiyun 	efi_free(fdt_size, fdt_addr);
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun fail_free_image:
311*4882a593Smuzhiyun 	efi_free(image_size, image_addr);
312*4882a593Smuzhiyun 	efi_free(reserve_size, reserve_addr);
313*4882a593Smuzhiyun fail_free_screeninfo:
314*4882a593Smuzhiyun 	free_screen_info(si);
315*4882a593Smuzhiyun fail_free_cmdline:
316*4882a593Smuzhiyun 	efi_bs_call(free_pool, cmdline_ptr);
317*4882a593Smuzhiyun fail:
318*4882a593Smuzhiyun 	return status;
319*4882a593Smuzhiyun }
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun /*
322*4882a593Smuzhiyun  * efi_get_virtmap() - create a virtual mapping for the EFI memory map
323*4882a593Smuzhiyun  *
324*4882a593Smuzhiyun  * This function populates the virt_addr fields of all memory region descriptors
325*4882a593Smuzhiyun  * in @memory_map whose EFI_MEMORY_RUNTIME attribute is set. Those descriptors
326*4882a593Smuzhiyun  * are also copied to @runtime_map, and their total count is returned in @count.
327*4882a593Smuzhiyun  */
efi_get_virtmap(efi_memory_desc_t * memory_map,unsigned long map_size,unsigned long desc_size,efi_memory_desc_t * runtime_map,int * count)328*4882a593Smuzhiyun void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size,
329*4882a593Smuzhiyun 		     unsigned long desc_size, efi_memory_desc_t *runtime_map,
330*4882a593Smuzhiyun 		     int *count)
331*4882a593Smuzhiyun {
332*4882a593Smuzhiyun 	u64 efi_virt_base = virtmap_base;
333*4882a593Smuzhiyun 	efi_memory_desc_t *in, *out = runtime_map;
334*4882a593Smuzhiyun 	int l;
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun 	for (l = 0; l < map_size; l += desc_size) {
337*4882a593Smuzhiyun 		u64 paddr, size;
338*4882a593Smuzhiyun 
339*4882a593Smuzhiyun 		in = (void *)memory_map + l;
340*4882a593Smuzhiyun 		if (!(in->attribute & EFI_MEMORY_RUNTIME))
341*4882a593Smuzhiyun 			continue;
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun 		paddr = in->phys_addr;
344*4882a593Smuzhiyun 		size = in->num_pages * EFI_PAGE_SIZE;
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun 		in->virt_addr = in->phys_addr;
347*4882a593Smuzhiyun 		if (efi_novamap) {
348*4882a593Smuzhiyun 			continue;
349*4882a593Smuzhiyun 		}
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun 		/*
352*4882a593Smuzhiyun 		 * Make the mapping compatible with 64k pages: this allows
353*4882a593Smuzhiyun 		 * a 4k page size kernel to kexec a 64k page size kernel and
354*4882a593Smuzhiyun 		 * vice versa.
355*4882a593Smuzhiyun 		 */
356*4882a593Smuzhiyun 		if (!flat_va_mapping) {
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun 			paddr = round_down(in->phys_addr, SZ_64K);
359*4882a593Smuzhiyun 			size += in->phys_addr - paddr;
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun 			/*
362*4882a593Smuzhiyun 			 * Avoid wasting memory on PTEs by choosing a virtual
363*4882a593Smuzhiyun 			 * base that is compatible with section mappings if this
364*4882a593Smuzhiyun 			 * region has the appropriate size and physical
365*4882a593Smuzhiyun 			 * alignment. (Sections are 2 MB on 4k granule kernels)
366*4882a593Smuzhiyun 			 */
367*4882a593Smuzhiyun 			if (IS_ALIGNED(in->phys_addr, SZ_2M) && size >= SZ_2M)
368*4882a593Smuzhiyun 				efi_virt_base = round_up(efi_virt_base, SZ_2M);
369*4882a593Smuzhiyun 			else
370*4882a593Smuzhiyun 				efi_virt_base = round_up(efi_virt_base, SZ_64K);
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun 			in->virt_addr += efi_virt_base - paddr;
373*4882a593Smuzhiyun 			efi_virt_base += size;
374*4882a593Smuzhiyun 		}
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun 		memcpy(out, in, desc_size);
377*4882a593Smuzhiyun 		out = (void *)out + desc_size;
378*4882a593Smuzhiyun 		++*count;
379*4882a593Smuzhiyun 	}
380*4882a593Smuzhiyun }
381