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 <asm/efi.h> 142e0eb483SAtish Patra 152e0eb483SAtish Patra #include "efistub.h" 162e0eb483SAtish Patra 172e0eb483SAtish Patra /* 182e0eb483SAtish Patra * This is the base address at which to start allocating virtual memory ranges 19b91540d5SAtish Patra * for UEFI Runtime Services. 20b91540d5SAtish Patra * 21b91540d5SAtish Patra * For ARM/ARM64: 22b91540d5SAtish Patra * This is in the low TTBR0 range so that we can use 232e0eb483SAtish Patra * any allocation we choose, and eliminate the risk of a conflict after kexec. 242e0eb483SAtish Patra * The value chosen is the largest non-zero power of 2 suitable for this purpose 252e0eb483SAtish Patra * both on 32-bit and 64-bit ARM CPUs, to maximize the likelihood that it can 262e0eb483SAtish Patra * be mapped efficiently. 272e0eb483SAtish Patra * Since 32-bit ARM could potentially execute with a 1G/3G user/kernel split, 282e0eb483SAtish Patra * map everything below 1 GB. (512 MB is a reasonable upper bound for the 292e0eb483SAtish Patra * entire footprint of the UEFI runtime services memory regions) 30b91540d5SAtish Patra * 31b91540d5SAtish Patra * For RISC-V: 32b91540d5SAtish Patra * There is no specific reason for which, this address (512MB) can't be used 33b91540d5SAtish Patra * EFI runtime virtual address for RISC-V. It also helps to use EFI runtime 34b91540d5SAtish Patra * services on both RV32/RV64. Keep the same runtime virtual address for RISC-V 35b91540d5SAtish Patra * as well to minimize the code churn. 362e0eb483SAtish Patra */ 372e0eb483SAtish Patra #define EFI_RT_VIRTUAL_BASE SZ_512M 382e0eb483SAtish Patra #define EFI_RT_VIRTUAL_SIZE SZ_512M 392e0eb483SAtish Patra 402e0eb483SAtish Patra #ifdef CONFIG_ARM64 412e0eb483SAtish Patra # define EFI_RT_VIRTUAL_LIMIT DEFAULT_MAP_WINDOW_64 42ead384d9SHuacai Chen #elif defined(CONFIG_RISCV) || defined(CONFIG_LOONGARCH) 43e8a62cc2SAlexandre Ghiti # define EFI_RT_VIRTUAL_LIMIT TASK_SIZE_MIN 44ead384d9SHuacai Chen #else /* Only if TASK_SIZE is a constant */ 452e0eb483SAtish Patra # define EFI_RT_VIRTUAL_LIMIT TASK_SIZE 462e0eb483SAtish Patra #endif 472e0eb483SAtish Patra 48ead384d9SHuacai Chen /* 49ead384d9SHuacai Chen * Some architectures map the EFI regions into the kernel's linear map using a 50ead384d9SHuacai Chen * fixed offset. 51ead384d9SHuacai Chen */ 52ead384d9SHuacai Chen #ifndef EFI_RT_VIRTUAL_OFFSET 53ead384d9SHuacai Chen #define EFI_RT_VIRTUAL_OFFSET 0 54ead384d9SHuacai Chen #endif 55ead384d9SHuacai Chen 562e0eb483SAtish Patra static u64 virtmap_base = EFI_RT_VIRTUAL_BASE; 57ead384d9SHuacai Chen static bool flat_va_mapping = (EFI_RT_VIRTUAL_OFFSET != 0); 582e0eb483SAtish Patra 59ccc27ae7SArd Biesheuvel const efi_system_table_t *efi_system_table; 602e0eb483SAtish Patra 612e0eb483SAtish Patra static struct screen_info *setup_graphics(void) 622e0eb483SAtish Patra { 632e0eb483SAtish Patra efi_guid_t gop_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID; 642e0eb483SAtish Patra efi_status_t status; 652e0eb483SAtish Patra unsigned long size; 662e0eb483SAtish Patra void **gop_handle = NULL; 672e0eb483SAtish Patra struct screen_info *si = NULL; 682e0eb483SAtish Patra 692e0eb483SAtish Patra size = 0; 702e0eb483SAtish Patra status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL, 712e0eb483SAtish Patra &gop_proto, NULL, &size, gop_handle); 722e0eb483SAtish Patra if (status == EFI_BUFFER_TOO_SMALL) { 732e0eb483SAtish Patra si = alloc_screen_info(); 742e0eb483SAtish Patra if (!si) 752e0eb483SAtish Patra return NULL; 76a5d8e55bSIngo Molnar status = efi_setup_gop(si, &gop_proto, size); 77a5d8e55bSIngo Molnar if (status != EFI_SUCCESS) { 78a5d8e55bSIngo Molnar free_screen_info(si); 79a5d8e55bSIngo Molnar return NULL; 80a5d8e55bSIngo Molnar } 812e0eb483SAtish Patra } 822e0eb483SAtish Patra return si; 832e0eb483SAtish Patra } 842e0eb483SAtish Patra 8587cd6378SZou Wei static void install_memreserve_table(void) 862e0eb483SAtish Patra { 872e0eb483SAtish Patra struct linux_efi_memreserve *rsv; 882e0eb483SAtish Patra efi_guid_t memreserve_table_guid = LINUX_EFI_MEMRESERVE_TABLE_GUID; 892e0eb483SAtish Patra efi_status_t status; 902e0eb483SAtish Patra 912e0eb483SAtish Patra status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, sizeof(*rsv), 922e0eb483SAtish Patra (void **)&rsv); 932e0eb483SAtish Patra if (status != EFI_SUCCESS) { 94793473c2SArvind Sankar efi_err("Failed to allocate memreserve entry!\n"); 952e0eb483SAtish Patra return; 962e0eb483SAtish Patra } 972e0eb483SAtish Patra 982e0eb483SAtish Patra rsv->next = 0; 992e0eb483SAtish Patra rsv->size = 0; 1002e0eb483SAtish Patra atomic_set(&rsv->count, 0); 1012e0eb483SAtish Patra 1022e0eb483SAtish Patra status = efi_bs_call(install_configuration_table, 1032e0eb483SAtish Patra &memreserve_table_guid, rsv); 1042e0eb483SAtish Patra if (status != EFI_SUCCESS) 105793473c2SArvind Sankar efi_err("Failed to install memreserve config table!\n"); 1062e0eb483SAtish Patra } 1072e0eb483SAtish Patra 1089e9888a0SArd Biesheuvel static u32 get_supported_rt_services(void) 1099e9888a0SArd Biesheuvel { 1109e9888a0SArd Biesheuvel const efi_rt_properties_table_t *rt_prop_table; 1119e9888a0SArd Biesheuvel u32 supported = EFI_RT_SUPPORTED_ALL; 1129e9888a0SArd Biesheuvel 1139e9888a0SArd Biesheuvel rt_prop_table = get_efi_config_table(EFI_RT_PROPERTIES_TABLE_GUID); 1149e9888a0SArd Biesheuvel if (rt_prop_table) 1159e9888a0SArd Biesheuvel supported &= rt_prop_table->runtime_services_supported; 1169e9888a0SArd Biesheuvel 1179e9888a0SArd Biesheuvel return supported; 1189e9888a0SArd Biesheuvel } 1199e9888a0SArd Biesheuvel 1202e0eb483SAtish Patra /* 1212e0eb483SAtish Patra * EFI entry point for the arm/arm64 EFI stubs. This is the entrypoint 1222e0eb483SAtish Patra * that is described in the PE/COFF header. Most of the code is the same 1232e0eb483SAtish Patra * for both archictectures, with the arch-specific code provided in the 1242e0eb483SAtish Patra * handle_kernel_image() function. 1252e0eb483SAtish Patra */ 1266e99d321SArd Biesheuvel efi_status_t __efiapi efi_pe_entry(efi_handle_t handle, 1276e99d321SArd Biesheuvel efi_system_table_t *sys_table_arg) 1282e0eb483SAtish Patra { 1292e0eb483SAtish Patra efi_loaded_image_t *image; 1302e0eb483SAtish Patra efi_status_t status; 1312e0eb483SAtish Patra unsigned long image_addr; 1322e0eb483SAtish Patra unsigned long image_size = 0; 1332e0eb483SAtish Patra /* addr/point and size pairs for memory management*/ 1342e0eb483SAtish Patra char *cmdline_ptr = NULL; 1352e0eb483SAtish Patra int cmdline_size = 0; 1362e0eb483SAtish Patra efi_guid_t loaded_image_proto = LOADED_IMAGE_PROTOCOL_GUID; 1372e0eb483SAtish Patra unsigned long reserve_addr = 0; 1382e0eb483SAtish Patra unsigned long reserve_size = 0; 1392e0eb483SAtish Patra struct screen_info *si; 1402e0eb483SAtish Patra efi_properties_table_t *prop_tbl; 1412e0eb483SAtish Patra 142ccc27ae7SArd Biesheuvel efi_system_table = sys_table_arg; 1432e0eb483SAtish Patra 1442e0eb483SAtish Patra /* Check if we were booted by the EFI firmware */ 145ccc27ae7SArd Biesheuvel if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) { 1462e0eb483SAtish Patra status = EFI_INVALID_PARAMETER; 1472e0eb483SAtish Patra goto fail; 1482e0eb483SAtish Patra } 1492e0eb483SAtish Patra 1502e0eb483SAtish Patra status = check_platform_features(); 1512e0eb483SAtish Patra if (status != EFI_SUCCESS) 1522e0eb483SAtish Patra goto fail; 1532e0eb483SAtish Patra 1542e0eb483SAtish Patra /* 1552e0eb483SAtish Patra * Get a handle to the loaded image protocol. This is used to get 1562e0eb483SAtish Patra * information about the running image, such as size and the command 1572e0eb483SAtish Patra * line. 1582e0eb483SAtish Patra */ 159ccc27ae7SArd Biesheuvel status = efi_system_table->boottime->handle_protocol(handle, 1602e0eb483SAtish Patra &loaded_image_proto, (void *)&image); 1612e0eb483SAtish Patra if (status != EFI_SUCCESS) { 162793473c2SArvind Sankar efi_err("Failed to get loaded image protocol\n"); 1632e0eb483SAtish Patra goto fail; 1642e0eb483SAtish Patra } 1652e0eb483SAtish Patra 1662e0eb483SAtish Patra /* 1672e0eb483SAtish Patra * Get the command line from EFI, using the LOADED_IMAGE 1682e0eb483SAtish Patra * protocol. We are going to copy the command line into the 1692e0eb483SAtish Patra * device tree, so this can be allocated anywhere. 1702e0eb483SAtish Patra */ 17127cd5511SArd Biesheuvel cmdline_ptr = efi_convert_cmdline(image, &cmdline_size); 1722e0eb483SAtish Patra if (!cmdline_ptr) { 173793473c2SArvind Sankar efi_err("getting command line via LOADED_IMAGE_PROTOCOL\n"); 1742e0eb483SAtish Patra status = EFI_OUT_OF_RESOURCES; 1752e0eb483SAtish Patra goto fail; 1762e0eb483SAtish Patra } 1772e0eb483SAtish Patra 1782e0eb483SAtish Patra if (IS_ENABLED(CONFIG_CMDLINE_EXTEND) || 1792e0eb483SAtish Patra IS_ENABLED(CONFIG_CMDLINE_FORCE) || 180055042beSArvind Sankar cmdline_size == 0) { 181055042beSArvind Sankar status = efi_parse_options(CONFIG_CMDLINE); 182055042beSArvind Sankar if (status != EFI_SUCCESS) { 183055042beSArvind Sankar efi_err("Failed to parse options\n"); 184055042beSArvind Sankar goto fail_free_cmdline; 185055042beSArvind Sankar } 186055042beSArvind Sankar } 1872e0eb483SAtish Patra 188055042beSArvind Sankar if (!IS_ENABLED(CONFIG_CMDLINE_FORCE) && cmdline_size > 0) { 189055042beSArvind Sankar status = efi_parse_options(cmdline_ptr); 190055042beSArvind Sankar if (status != EFI_SUCCESS) { 191055042beSArvind Sankar efi_err("Failed to parse options\n"); 192055042beSArvind Sankar goto fail_free_cmdline; 193055042beSArvind Sankar } 194055042beSArvind Sankar } 1952e0eb483SAtish Patra 196793473c2SArvind Sankar efi_info("Booting Linux Kernel...\n"); 1972e0eb483SAtish Patra 1982e0eb483SAtish Patra si = setup_graphics(); 1992e0eb483SAtish Patra 2002e0eb483SAtish Patra status = handle_kernel_image(&image_addr, &image_size, 2012e0eb483SAtish Patra &reserve_addr, 2022e0eb483SAtish Patra &reserve_size, 203416a9f84SArd Biesheuvel image, handle); 2042e0eb483SAtish Patra if (status != EFI_SUCCESS) { 205793473c2SArvind Sankar efi_err("Failed to relocate kernel\n"); 206055042beSArvind Sankar goto fail_free_screeninfo; 2072e0eb483SAtish Patra } 2082e0eb483SAtish Patra 2092e0eb483SAtish Patra efi_retrieve_tpm2_eventlog(); 2102e0eb483SAtish Patra 2112e0eb483SAtish Patra /* Ask the firmware to clear memory on unclean shutdown */ 2122e0eb483SAtish Patra efi_enable_reset_attack_mitigation(); 2132e0eb483SAtish Patra 214f4dc7fffSArd Biesheuvel efi_load_initrd(image, ULONG_MAX, efi_get_max_initrd_addr(image_addr), 215f4dc7fffSArd Biesheuvel NULL); 2162e0eb483SAtish Patra 2172e0eb483SAtish Patra efi_random_get_seed(); 2182e0eb483SAtish Patra 2192e0eb483SAtish Patra /* 2202e0eb483SAtish Patra * If the NX PE data feature is enabled in the properties table, we 2212e0eb483SAtish Patra * should take care not to create a virtual mapping that changes the 2222e0eb483SAtish Patra * relative placement of runtime services code and data regions, as 2232e0eb483SAtish Patra * they may belong to the same PE/COFF executable image in memory. 2242e0eb483SAtish Patra * The easiest way to achieve that is to simply use a 1:1 mapping. 2252e0eb483SAtish Patra */ 2262e0eb483SAtish Patra prop_tbl = get_efi_config_table(EFI_PROPERTIES_TABLE_GUID); 227ead384d9SHuacai Chen flat_va_mapping |= prop_tbl && 2282e0eb483SAtish Patra (prop_tbl->memory_protection_attribute & 2292e0eb483SAtish Patra EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA); 2302e0eb483SAtish Patra 2319e9888a0SArd Biesheuvel /* force efi_novamap if SetVirtualAddressMap() is unsupported */ 2329e9888a0SArd Biesheuvel efi_novamap |= !(get_supported_rt_services() & 2339e9888a0SArd Biesheuvel EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP); 2349e9888a0SArd Biesheuvel 2352e0eb483SAtish Patra /* hibernation expects the runtime regions to stay in the same place */ 236980771f6SArd Biesheuvel if (!IS_ENABLED(CONFIG_HIBERNATION) && !efi_nokaslr && !flat_va_mapping) { 2372e0eb483SAtish Patra /* 2382e0eb483SAtish Patra * Randomize the base of the UEFI runtime services region. 2392e0eb483SAtish Patra * Preserve the 2 MB alignment of the region by taking a 2402e0eb483SAtish Patra * shift of 21 bit positions into account when scaling 2412e0eb483SAtish Patra * the headroom value using a 32-bit random value. 2422e0eb483SAtish Patra */ 2432e0eb483SAtish Patra static const u64 headroom = EFI_RT_VIRTUAL_LIMIT - 2442e0eb483SAtish Patra EFI_RT_VIRTUAL_BASE - 2452e0eb483SAtish Patra EFI_RT_VIRTUAL_SIZE; 2462e0eb483SAtish Patra u32 rnd; 2472e0eb483SAtish Patra 2482e0eb483SAtish Patra status = efi_get_random_bytes(sizeof(rnd), (u8 *)&rnd); 2492e0eb483SAtish Patra if (status == EFI_SUCCESS) { 2502e0eb483SAtish Patra virtmap_base = EFI_RT_VIRTUAL_BASE + 2512e0eb483SAtish Patra (((headroom >> 21) * rnd) >> (32 - 21)); 2522e0eb483SAtish Patra } 2532e0eb483SAtish Patra } 2542e0eb483SAtish Patra 2552e0eb483SAtish Patra install_memreserve_table(); 2562e0eb483SAtish Patra 257*4fc8e738SArd Biesheuvel status = efi_boot_kernel(handle, image, image_addr, cmdline_ptr); 2582e0eb483SAtish Patra 2592e0eb483SAtish Patra efi_free(image_size, image_addr); 2602e0eb483SAtish Patra efi_free(reserve_size, reserve_addr); 261055042beSArvind Sankar fail_free_screeninfo: 2622e0eb483SAtish Patra free_screen_info(si); 263055042beSArvind Sankar fail_free_cmdline: 26427cd5511SArd Biesheuvel efi_bs_call(free_pool, cmdline_ptr); 2652e0eb483SAtish Patra fail: 2662e0eb483SAtish Patra return status; 2672e0eb483SAtish Patra } 2682e0eb483SAtish Patra 2692e0eb483SAtish Patra /* 270f80d2604SArd Biesheuvel * efi_allocate_virtmap() - create a pool allocation for the virtmap 271f80d2604SArd Biesheuvel * 272f80d2604SArd Biesheuvel * Create an allocation that is of sufficient size to hold all the memory 273f80d2604SArd Biesheuvel * descriptors that will be passed to SetVirtualAddressMap() to inform the 274f80d2604SArd Biesheuvel * firmware about the virtual mapping that will be used under the OS to call 275f80d2604SArd Biesheuvel * into the firmware. 276f80d2604SArd Biesheuvel */ 277f80d2604SArd Biesheuvel efi_status_t efi_alloc_virtmap(efi_memory_desc_t **virtmap, 278f80d2604SArd Biesheuvel unsigned long *desc_size, u32 *desc_ver) 279f80d2604SArd Biesheuvel { 280f80d2604SArd Biesheuvel unsigned long size, mmap_key; 281f80d2604SArd Biesheuvel efi_status_t status; 282f80d2604SArd Biesheuvel 283f80d2604SArd Biesheuvel /* 284f80d2604SArd Biesheuvel * Use the size of the current memory map as an upper bound for the 285f80d2604SArd Biesheuvel * size of the buffer we need to pass to SetVirtualAddressMap() to 286f80d2604SArd Biesheuvel * cover all EFI_MEMORY_RUNTIME regions. 287f80d2604SArd Biesheuvel */ 288f80d2604SArd Biesheuvel size = 0; 289f80d2604SArd Biesheuvel status = efi_bs_call(get_memory_map, &size, NULL, &mmap_key, desc_size, 290f80d2604SArd Biesheuvel desc_ver); 291f80d2604SArd Biesheuvel if (status != EFI_BUFFER_TOO_SMALL) 292f80d2604SArd Biesheuvel return EFI_LOAD_ERROR; 293f80d2604SArd Biesheuvel 294f80d2604SArd Biesheuvel return efi_bs_call(allocate_pool, EFI_LOADER_DATA, size, 295f80d2604SArd Biesheuvel (void **)virtmap); 296f80d2604SArd Biesheuvel } 297f80d2604SArd Biesheuvel 298f80d2604SArd Biesheuvel /* 2992e0eb483SAtish Patra * efi_get_virtmap() - create a virtual mapping for the EFI memory map 3002e0eb483SAtish Patra * 3012e0eb483SAtish Patra * This function populates the virt_addr fields of all memory region descriptors 3022e0eb483SAtish Patra * in @memory_map whose EFI_MEMORY_RUNTIME attribute is set. Those descriptors 3032e0eb483SAtish Patra * are also copied to @runtime_map, and their total count is returned in @count. 3042e0eb483SAtish Patra */ 3052e0eb483SAtish Patra void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size, 3062e0eb483SAtish Patra unsigned long desc_size, efi_memory_desc_t *runtime_map, 3072e0eb483SAtish Patra int *count) 3082e0eb483SAtish Patra { 3092e0eb483SAtish Patra u64 efi_virt_base = virtmap_base; 3102e0eb483SAtish Patra efi_memory_desc_t *in, *out = runtime_map; 3112e0eb483SAtish Patra int l; 3122e0eb483SAtish Patra 313f80d2604SArd Biesheuvel *count = 0; 314f80d2604SArd Biesheuvel 3152e0eb483SAtish Patra for (l = 0; l < map_size; l += desc_size) { 3162e0eb483SAtish Patra u64 paddr, size; 3172e0eb483SAtish Patra 3182e0eb483SAtish Patra in = (void *)memory_map + l; 3192e0eb483SAtish Patra if (!(in->attribute & EFI_MEMORY_RUNTIME)) 3202e0eb483SAtish Patra continue; 3212e0eb483SAtish Patra 3222e0eb483SAtish Patra paddr = in->phys_addr; 3232e0eb483SAtish Patra size = in->num_pages * EFI_PAGE_SIZE; 3242e0eb483SAtish Patra 325ead384d9SHuacai Chen in->virt_addr = in->phys_addr + EFI_RT_VIRTUAL_OFFSET; 326980771f6SArd Biesheuvel if (efi_novamap) { 3272e0eb483SAtish Patra continue; 3282e0eb483SAtish Patra } 3292e0eb483SAtish Patra 3302e0eb483SAtish Patra /* 3312e0eb483SAtish Patra * Make the mapping compatible with 64k pages: this allows 3322e0eb483SAtish Patra * a 4k page size kernel to kexec a 64k page size kernel and 3332e0eb483SAtish Patra * vice versa. 3342e0eb483SAtish Patra */ 3352e0eb483SAtish Patra if (!flat_va_mapping) { 3362e0eb483SAtish Patra 3372e0eb483SAtish Patra paddr = round_down(in->phys_addr, SZ_64K); 3382e0eb483SAtish Patra size += in->phys_addr - paddr; 3392e0eb483SAtish Patra 3402e0eb483SAtish Patra /* 3412e0eb483SAtish Patra * Avoid wasting memory on PTEs by choosing a virtual 3422e0eb483SAtish Patra * base that is compatible with section mappings if this 3432e0eb483SAtish Patra * region has the appropriate size and physical 3442e0eb483SAtish Patra * alignment. (Sections are 2 MB on 4k granule kernels) 3452e0eb483SAtish Patra */ 3462e0eb483SAtish Patra if (IS_ALIGNED(in->phys_addr, SZ_2M) && size >= SZ_2M) 3472e0eb483SAtish Patra efi_virt_base = round_up(efi_virt_base, SZ_2M); 3482e0eb483SAtish Patra else 3492e0eb483SAtish Patra efi_virt_base = round_up(efi_virt_base, SZ_64K); 3502e0eb483SAtish Patra 3512e0eb483SAtish Patra in->virt_addr += efi_virt_base - paddr; 3522e0eb483SAtish Patra efi_virt_base += size; 3532e0eb483SAtish Patra } 3542e0eb483SAtish Patra 3552e0eb483SAtish Patra memcpy(out, in, desc_size); 3562e0eb483SAtish Patra out = (void *)out + desc_size; 3572e0eb483SAtish Patra ++*count; 3582e0eb483SAtish Patra } 3592e0eb483SAtish Patra } 360