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 43ead384d9SHuacai Chen #elif defined(CONFIG_RISCV) || defined(CONFIG_LOONGARCH) 44e8a62cc2SAlexandre Ghiti # define EFI_RT_VIRTUAL_LIMIT TASK_SIZE_MIN 45ead384d9SHuacai Chen #else /* Only if TASK_SIZE is a constant */ 462e0eb483SAtish Patra # define EFI_RT_VIRTUAL_LIMIT TASK_SIZE 472e0eb483SAtish Patra #endif 482e0eb483SAtish Patra 49ead384d9SHuacai Chen /* 50ead384d9SHuacai Chen * Some architectures map the EFI regions into the kernel's linear map using a 51ead384d9SHuacai Chen * fixed offset. 52ead384d9SHuacai Chen */ 53ead384d9SHuacai Chen #ifndef EFI_RT_VIRTUAL_OFFSET 54ead384d9SHuacai Chen #define EFI_RT_VIRTUAL_OFFSET 0 55ead384d9SHuacai Chen #endif 56ead384d9SHuacai Chen 572e0eb483SAtish Patra static u64 virtmap_base = EFI_RT_VIRTUAL_BASE; 58ead384d9SHuacai 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 fdt_addr = 0; /* Original DTB */ 1362e0eb483SAtish Patra unsigned long fdt_size = 0; 1372e0eb483SAtish Patra char *cmdline_ptr = NULL; 1382e0eb483SAtish Patra int cmdline_size = 0; 1392e0eb483SAtish Patra efi_guid_t loaded_image_proto = LOADED_IMAGE_PROTOCOL_GUID; 1402e0eb483SAtish Patra unsigned long reserve_addr = 0; 1412e0eb483SAtish Patra unsigned long reserve_size = 0; 1422e0eb483SAtish Patra enum efi_secureboot_mode secure_boot; 1432e0eb483SAtish Patra struct screen_info *si; 1442e0eb483SAtish Patra efi_properties_table_t *prop_tbl; 1452e0eb483SAtish Patra 146ccc27ae7SArd Biesheuvel efi_system_table = sys_table_arg; 1472e0eb483SAtish Patra 1482e0eb483SAtish Patra /* Check if we were booted by the EFI firmware */ 149ccc27ae7SArd Biesheuvel if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) { 1502e0eb483SAtish Patra status = EFI_INVALID_PARAMETER; 1512e0eb483SAtish Patra goto fail; 1522e0eb483SAtish Patra } 1532e0eb483SAtish Patra 1542e0eb483SAtish Patra status = check_platform_features(); 1552e0eb483SAtish Patra if (status != EFI_SUCCESS) 1562e0eb483SAtish Patra goto fail; 1572e0eb483SAtish Patra 1582e0eb483SAtish Patra /* 1592e0eb483SAtish Patra * Get a handle to the loaded image protocol. This is used to get 1602e0eb483SAtish Patra * information about the running image, such as size and the command 1612e0eb483SAtish Patra * line. 1622e0eb483SAtish Patra */ 163ccc27ae7SArd Biesheuvel status = efi_system_table->boottime->handle_protocol(handle, 1642e0eb483SAtish Patra &loaded_image_proto, (void *)&image); 1652e0eb483SAtish Patra if (status != EFI_SUCCESS) { 166793473c2SArvind Sankar efi_err("Failed to get loaded image protocol\n"); 1672e0eb483SAtish Patra goto fail; 1682e0eb483SAtish Patra } 1692e0eb483SAtish Patra 1702e0eb483SAtish Patra /* 1712e0eb483SAtish Patra * Get the command line from EFI, using the LOADED_IMAGE 1722e0eb483SAtish Patra * protocol. We are going to copy the command line into the 1732e0eb483SAtish Patra * device tree, so this can be allocated anywhere. 1742e0eb483SAtish Patra */ 17527cd5511SArd Biesheuvel cmdline_ptr = efi_convert_cmdline(image, &cmdline_size); 1762e0eb483SAtish Patra if (!cmdline_ptr) { 177793473c2SArvind Sankar efi_err("getting command line via LOADED_IMAGE_PROTOCOL\n"); 1782e0eb483SAtish Patra status = EFI_OUT_OF_RESOURCES; 1792e0eb483SAtish Patra goto fail; 1802e0eb483SAtish Patra } 1812e0eb483SAtish Patra 1822e0eb483SAtish Patra if (IS_ENABLED(CONFIG_CMDLINE_EXTEND) || 1832e0eb483SAtish Patra IS_ENABLED(CONFIG_CMDLINE_FORCE) || 184055042beSArvind Sankar cmdline_size == 0) { 185055042beSArvind Sankar status = efi_parse_options(CONFIG_CMDLINE); 186055042beSArvind Sankar if (status != EFI_SUCCESS) { 187055042beSArvind Sankar efi_err("Failed to parse options\n"); 188055042beSArvind Sankar goto fail_free_cmdline; 189055042beSArvind Sankar } 190055042beSArvind Sankar } 1912e0eb483SAtish Patra 192055042beSArvind Sankar if (!IS_ENABLED(CONFIG_CMDLINE_FORCE) && cmdline_size > 0) { 193055042beSArvind Sankar status = efi_parse_options(cmdline_ptr); 194055042beSArvind Sankar if (status != EFI_SUCCESS) { 195055042beSArvind Sankar efi_err("Failed to parse options\n"); 196055042beSArvind Sankar goto fail_free_cmdline; 197055042beSArvind Sankar } 198055042beSArvind Sankar } 1992e0eb483SAtish Patra 200793473c2SArvind Sankar efi_info("Booting Linux Kernel...\n"); 2012e0eb483SAtish Patra 2022e0eb483SAtish Patra si = setup_graphics(); 2032e0eb483SAtish Patra 2042e0eb483SAtish Patra status = handle_kernel_image(&image_addr, &image_size, 2052e0eb483SAtish Patra &reserve_addr, 2062e0eb483SAtish Patra &reserve_size, 207416a9f84SArd Biesheuvel image, handle); 2082e0eb483SAtish Patra if (status != EFI_SUCCESS) { 209793473c2SArvind Sankar efi_err("Failed to relocate kernel\n"); 210055042beSArvind Sankar goto fail_free_screeninfo; 2112e0eb483SAtish Patra } 2122e0eb483SAtish Patra 2132e0eb483SAtish Patra efi_retrieve_tpm2_eventlog(); 2142e0eb483SAtish Patra 2152e0eb483SAtish Patra /* Ask the firmware to clear memory on unclean shutdown */ 2162e0eb483SAtish Patra efi_enable_reset_attack_mitigation(); 2172e0eb483SAtish Patra 2182e0eb483SAtish Patra secure_boot = efi_get_secureboot(); 2192e0eb483SAtish Patra 2202e0eb483SAtish Patra /* 2212e0eb483SAtish Patra * Unauthenticated device tree data is a security hazard, so ignore 2222e0eb483SAtish Patra * 'dtb=' unless UEFI Secure Boot is disabled. We assume that secure 2232e0eb483SAtish Patra * boot is enabled if we can't determine its state. 2242e0eb483SAtish Patra */ 2252e0eb483SAtish Patra if (!IS_ENABLED(CONFIG_EFI_ARMSTUB_DTB_LOADER) || 2262e0eb483SAtish Patra secure_boot != efi_secureboot_mode_disabled) { 2272e0eb483SAtish Patra if (strstr(cmdline_ptr, "dtb=")) 2283839ab85SArvind Sankar efi_err("Ignoring DTB from command line.\n"); 2292e0eb483SAtish Patra } else { 2302e0eb483SAtish Patra status = efi_load_dtb(image, &fdt_addr, &fdt_size); 2312e0eb483SAtish Patra 232*f4dc7fffSArd Biesheuvel if (status != EFI_SUCCESS && status != EFI_NOT_READY) { 233793473c2SArvind Sankar efi_err("Failed to load device tree!\n"); 2342e0eb483SAtish Patra goto fail_free_image; 2352e0eb483SAtish Patra } 2362e0eb483SAtish Patra } 2372e0eb483SAtish Patra 2382e0eb483SAtish Patra if (fdt_addr) { 239793473c2SArvind Sankar efi_info("Using DTB from command line\n"); 2402e0eb483SAtish Patra } else { 2412e0eb483SAtish Patra /* Look for a device tree configuration table entry. */ 2422e0eb483SAtish Patra fdt_addr = (uintptr_t)get_fdt(&fdt_size); 2432e0eb483SAtish Patra if (fdt_addr) 244793473c2SArvind Sankar efi_info("Using DTB from configuration table\n"); 2452e0eb483SAtish Patra } 2462e0eb483SAtish Patra 2472e0eb483SAtish Patra if (!fdt_addr) 248793473c2SArvind Sankar efi_info("Generating empty DTB\n"); 2492e0eb483SAtish Patra 250*f4dc7fffSArd Biesheuvel efi_load_initrd(image, ULONG_MAX, efi_get_max_initrd_addr(image_addr), 251*f4dc7fffSArd Biesheuvel NULL); 2522e0eb483SAtish Patra 2532e0eb483SAtish Patra efi_random_get_seed(); 2542e0eb483SAtish Patra 2552e0eb483SAtish Patra /* 2562e0eb483SAtish Patra * If the NX PE data feature is enabled in the properties table, we 2572e0eb483SAtish Patra * should take care not to create a virtual mapping that changes the 2582e0eb483SAtish Patra * relative placement of runtime services code and data regions, as 2592e0eb483SAtish Patra * they may belong to the same PE/COFF executable image in memory. 2602e0eb483SAtish Patra * The easiest way to achieve that is to simply use a 1:1 mapping. 2612e0eb483SAtish Patra */ 2622e0eb483SAtish Patra prop_tbl = get_efi_config_table(EFI_PROPERTIES_TABLE_GUID); 263ead384d9SHuacai Chen flat_va_mapping |= prop_tbl && 2642e0eb483SAtish Patra (prop_tbl->memory_protection_attribute & 2652e0eb483SAtish Patra EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA); 2662e0eb483SAtish Patra 2679e9888a0SArd Biesheuvel /* force efi_novamap if SetVirtualAddressMap() is unsupported */ 2689e9888a0SArd Biesheuvel efi_novamap |= !(get_supported_rt_services() & 2699e9888a0SArd Biesheuvel EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP); 2709e9888a0SArd Biesheuvel 2712e0eb483SAtish Patra /* hibernation expects the runtime regions to stay in the same place */ 272980771f6SArd Biesheuvel if (!IS_ENABLED(CONFIG_HIBERNATION) && !efi_nokaslr && !flat_va_mapping) { 2732e0eb483SAtish Patra /* 2742e0eb483SAtish Patra * Randomize the base of the UEFI runtime services region. 2752e0eb483SAtish Patra * Preserve the 2 MB alignment of the region by taking a 2762e0eb483SAtish Patra * shift of 21 bit positions into account when scaling 2772e0eb483SAtish Patra * the headroom value using a 32-bit random value. 2782e0eb483SAtish Patra */ 2792e0eb483SAtish Patra static const u64 headroom = EFI_RT_VIRTUAL_LIMIT - 2802e0eb483SAtish Patra EFI_RT_VIRTUAL_BASE - 2812e0eb483SAtish Patra EFI_RT_VIRTUAL_SIZE; 2822e0eb483SAtish Patra u32 rnd; 2832e0eb483SAtish Patra 2842e0eb483SAtish Patra status = efi_get_random_bytes(sizeof(rnd), (u8 *)&rnd); 2852e0eb483SAtish Patra if (status == EFI_SUCCESS) { 2862e0eb483SAtish Patra virtmap_base = EFI_RT_VIRTUAL_BASE + 2872e0eb483SAtish Patra (((headroom >> 21) * rnd) >> (32 - 21)); 2882e0eb483SAtish Patra } 2892e0eb483SAtish Patra } 2902e0eb483SAtish Patra 2912e0eb483SAtish Patra install_memreserve_table(); 2922e0eb483SAtish Patra 293*f4dc7fffSArd Biesheuvel status = allocate_new_fdt_and_exit_boot(handle, &fdt_addr, cmdline_ptr, 294*f4dc7fffSArd Biesheuvel fdt_addr, fdt_size); 2952e0eb483SAtish Patra if (status != EFI_SUCCESS) 296*f4dc7fffSArd Biesheuvel goto fail_free_fdt; 2972e0eb483SAtish Patra 2982a55280aSArd Biesheuvel if (IS_ENABLED(CONFIG_ARM)) 2992a55280aSArd Biesheuvel efi_handle_post_ebs_state(); 3002a55280aSArd Biesheuvel 3012e0eb483SAtish Patra efi_enter_kernel(image_addr, fdt_addr, fdt_totalsize((void *)fdt_addr)); 3022e0eb483SAtish Patra /* not reached */ 3032e0eb483SAtish Patra 304*f4dc7fffSArd Biesheuvel fail_free_fdt: 305793473c2SArvind Sankar efi_err("Failed to update FDT and exit boot services\n"); 3062e0eb483SAtish Patra 3072e0eb483SAtish Patra efi_free(fdt_size, fdt_addr); 3082e0eb483SAtish Patra 3092e0eb483SAtish Patra fail_free_image: 3102e0eb483SAtish Patra efi_free(image_size, image_addr); 3112e0eb483SAtish Patra efi_free(reserve_size, reserve_addr); 312055042beSArvind Sankar fail_free_screeninfo: 3132e0eb483SAtish Patra free_screen_info(si); 314055042beSArvind Sankar fail_free_cmdline: 31527cd5511SArd Biesheuvel efi_bs_call(free_pool, cmdline_ptr); 3162e0eb483SAtish Patra fail: 3172e0eb483SAtish Patra return status; 3182e0eb483SAtish Patra } 3192e0eb483SAtish Patra 3202e0eb483SAtish Patra /* 321f80d2604SArd Biesheuvel * efi_allocate_virtmap() - create a pool allocation for the virtmap 322f80d2604SArd Biesheuvel * 323f80d2604SArd Biesheuvel * Create an allocation that is of sufficient size to hold all the memory 324f80d2604SArd Biesheuvel * descriptors that will be passed to SetVirtualAddressMap() to inform the 325f80d2604SArd Biesheuvel * firmware about the virtual mapping that will be used under the OS to call 326f80d2604SArd Biesheuvel * into the firmware. 327f80d2604SArd Biesheuvel */ 328f80d2604SArd Biesheuvel efi_status_t efi_alloc_virtmap(efi_memory_desc_t **virtmap, 329f80d2604SArd Biesheuvel unsigned long *desc_size, u32 *desc_ver) 330f80d2604SArd Biesheuvel { 331f80d2604SArd Biesheuvel unsigned long size, mmap_key; 332f80d2604SArd Biesheuvel efi_status_t status; 333f80d2604SArd Biesheuvel 334f80d2604SArd Biesheuvel /* 335f80d2604SArd Biesheuvel * Use the size of the current memory map as an upper bound for the 336f80d2604SArd Biesheuvel * size of the buffer we need to pass to SetVirtualAddressMap() to 337f80d2604SArd Biesheuvel * cover all EFI_MEMORY_RUNTIME regions. 338f80d2604SArd Biesheuvel */ 339f80d2604SArd Biesheuvel size = 0; 340f80d2604SArd Biesheuvel status = efi_bs_call(get_memory_map, &size, NULL, &mmap_key, desc_size, 341f80d2604SArd Biesheuvel desc_ver); 342f80d2604SArd Biesheuvel if (status != EFI_BUFFER_TOO_SMALL) 343f80d2604SArd Biesheuvel return EFI_LOAD_ERROR; 344f80d2604SArd Biesheuvel 345f80d2604SArd Biesheuvel return efi_bs_call(allocate_pool, EFI_LOADER_DATA, size, 346f80d2604SArd Biesheuvel (void **)virtmap); 347f80d2604SArd Biesheuvel } 348f80d2604SArd Biesheuvel 349f80d2604SArd Biesheuvel /* 3502e0eb483SAtish Patra * efi_get_virtmap() - create a virtual mapping for the EFI memory map 3512e0eb483SAtish Patra * 3522e0eb483SAtish Patra * This function populates the virt_addr fields of all memory region descriptors 3532e0eb483SAtish Patra * in @memory_map whose EFI_MEMORY_RUNTIME attribute is set. Those descriptors 3542e0eb483SAtish Patra * are also copied to @runtime_map, and their total count is returned in @count. 3552e0eb483SAtish Patra */ 3562e0eb483SAtish Patra void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size, 3572e0eb483SAtish Patra unsigned long desc_size, efi_memory_desc_t *runtime_map, 3582e0eb483SAtish Patra int *count) 3592e0eb483SAtish Patra { 3602e0eb483SAtish Patra u64 efi_virt_base = virtmap_base; 3612e0eb483SAtish Patra efi_memory_desc_t *in, *out = runtime_map; 3622e0eb483SAtish Patra int l; 3632e0eb483SAtish Patra 364f80d2604SArd Biesheuvel *count = 0; 365f80d2604SArd Biesheuvel 3662e0eb483SAtish Patra for (l = 0; l < map_size; l += desc_size) { 3672e0eb483SAtish Patra u64 paddr, size; 3682e0eb483SAtish Patra 3692e0eb483SAtish Patra in = (void *)memory_map + l; 3702e0eb483SAtish Patra if (!(in->attribute & EFI_MEMORY_RUNTIME)) 3712e0eb483SAtish Patra continue; 3722e0eb483SAtish Patra 3732e0eb483SAtish Patra paddr = in->phys_addr; 3742e0eb483SAtish Patra size = in->num_pages * EFI_PAGE_SIZE; 3752e0eb483SAtish Patra 376ead384d9SHuacai Chen in->virt_addr = in->phys_addr + EFI_RT_VIRTUAL_OFFSET; 377980771f6SArd Biesheuvel if (efi_novamap) { 3782e0eb483SAtish Patra continue; 3792e0eb483SAtish Patra } 3802e0eb483SAtish Patra 3812e0eb483SAtish Patra /* 3822e0eb483SAtish Patra * Make the mapping compatible with 64k pages: this allows 3832e0eb483SAtish Patra * a 4k page size kernel to kexec a 64k page size kernel and 3842e0eb483SAtish Patra * vice versa. 3852e0eb483SAtish Patra */ 3862e0eb483SAtish Patra if (!flat_va_mapping) { 3872e0eb483SAtish Patra 3882e0eb483SAtish Patra paddr = round_down(in->phys_addr, SZ_64K); 3892e0eb483SAtish Patra size += in->phys_addr - paddr; 3902e0eb483SAtish Patra 3912e0eb483SAtish Patra /* 3922e0eb483SAtish Patra * Avoid wasting memory on PTEs by choosing a virtual 3932e0eb483SAtish Patra * base that is compatible with section mappings if this 3942e0eb483SAtish Patra * region has the appropriate size and physical 3952e0eb483SAtish Patra * alignment. (Sections are 2 MB on 4k granule kernels) 3962e0eb483SAtish Patra */ 3972e0eb483SAtish Patra if (IS_ALIGNED(in->phys_addr, SZ_2M) && size >= SZ_2M) 3982e0eb483SAtish Patra efi_virt_base = round_up(efi_virt_base, SZ_2M); 3992e0eb483SAtish Patra else 4002e0eb483SAtish Patra efi_virt_base = round_up(efi_virt_base, SZ_64K); 4012e0eb483SAtish Patra 4022e0eb483SAtish Patra in->virt_addr += efi_virt_base - paddr; 4032e0eb483SAtish Patra efi_virt_base += size; 4042e0eb483SAtish Patra } 4052e0eb483SAtish Patra 4062e0eb483SAtish Patra memcpy(out, in, desc_size); 4072e0eb483SAtish Patra out = (void *)out + desc_size; 4082e0eb483SAtish Patra ++*count; 4092e0eb483SAtish Patra } 4102e0eb483SAtish Patra } 411