Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v4.19 374 lines 12 kB view raw
1/* 2 * EFI stub implementation that is shared by arm and arm64 architectures. 3 * This should be #included by the EFI stub implementation files. 4 * 5 * Copyright (C) 2013,2014 Linaro Limited 6 * Roy Franz <roy.franz@linaro.org 7 * Copyright (C) 2013 Red Hat, Inc. 8 * Mark Salter <msalter@redhat.com> 9 * 10 * This file is part of the Linux kernel, and is made available under the 11 * terms of the GNU General Public License version 2. 12 * 13 */ 14 15#include <linux/efi.h> 16#include <linux/sort.h> 17#include <asm/efi.h> 18 19#include "efistub.h" 20 21/* 22 * This is the base address at which to start allocating virtual memory ranges 23 * for UEFI Runtime Services. This is in the low TTBR0 range so that we can use 24 * any allocation we choose, and eliminate the risk of a conflict after kexec. 25 * The value chosen is the largest non-zero power of 2 suitable for this purpose 26 * both on 32-bit and 64-bit ARM CPUs, to maximize the likelihood that it can 27 * be mapped efficiently. 28 * Since 32-bit ARM could potentially execute with a 1G/3G user/kernel split, 29 * map everything below 1 GB. (512 MB is a reasonable upper bound for the 30 * entire footprint of the UEFI runtime services memory regions) 31 */ 32#define EFI_RT_VIRTUAL_BASE SZ_512M 33#define EFI_RT_VIRTUAL_SIZE SZ_512M 34 35#ifdef CONFIG_ARM64 36# define EFI_RT_VIRTUAL_LIMIT TASK_SIZE_64 37#else 38# define EFI_RT_VIRTUAL_LIMIT TASK_SIZE 39#endif 40 41static u64 virtmap_base = EFI_RT_VIRTUAL_BASE; 42 43void efi_char16_printk(efi_system_table_t *sys_table_arg, 44 efi_char16_t *str) 45{ 46 struct efi_simple_text_output_protocol *out; 47 48 out = (struct efi_simple_text_output_protocol *)sys_table_arg->con_out; 49 out->output_string(out, str); 50} 51 52static struct screen_info *setup_graphics(efi_system_table_t *sys_table_arg) 53{ 54 efi_guid_t gop_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID; 55 efi_status_t status; 56 unsigned long size; 57 void **gop_handle = NULL; 58 struct screen_info *si = NULL; 59 60 size = 0; 61 status = efi_call_early(locate_handle, EFI_LOCATE_BY_PROTOCOL, 62 &gop_proto, NULL, &size, gop_handle); 63 if (status == EFI_BUFFER_TOO_SMALL) { 64 si = alloc_screen_info(sys_table_arg); 65 if (!si) 66 return NULL; 67 efi_setup_gop(sys_table_arg, si, &gop_proto, size); 68 } 69 return si; 70} 71 72/* 73 * This function handles the architcture specific differences between arm and 74 * arm64 regarding where the kernel image must be loaded and any memory that 75 * must be reserved. On failure it is required to free all 76 * all allocations it has made. 77 */ 78efi_status_t handle_kernel_image(efi_system_table_t *sys_table, 79 unsigned long *image_addr, 80 unsigned long *image_size, 81 unsigned long *reserve_addr, 82 unsigned long *reserve_size, 83 unsigned long dram_base, 84 efi_loaded_image_t *image); 85/* 86 * EFI entry point for the arm/arm64 EFI stubs. This is the entrypoint 87 * that is described in the PE/COFF header. Most of the code is the same 88 * for both archictectures, with the arch-specific code provided in the 89 * handle_kernel_image() function. 90 */ 91unsigned long efi_entry(void *handle, efi_system_table_t *sys_table, 92 unsigned long *image_addr) 93{ 94 efi_loaded_image_t *image; 95 efi_status_t status; 96 unsigned long image_size = 0; 97 unsigned long dram_base; 98 /* addr/point and size pairs for memory management*/ 99 unsigned long initrd_addr; 100 u64 initrd_size = 0; 101 unsigned long fdt_addr = 0; /* Original DTB */ 102 unsigned long fdt_size = 0; 103 char *cmdline_ptr = NULL; 104 int cmdline_size = 0; 105 unsigned long new_fdt_addr; 106 efi_guid_t loaded_image_proto = LOADED_IMAGE_PROTOCOL_GUID; 107 unsigned long reserve_addr = 0; 108 unsigned long reserve_size = 0; 109 enum efi_secureboot_mode secure_boot; 110 struct screen_info *si; 111 112 /* Check if we were booted by the EFI firmware */ 113 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) 114 goto fail; 115 116 status = check_platform_features(sys_table); 117 if (status != EFI_SUCCESS) 118 goto fail; 119 120 /* 121 * Get a handle to the loaded image protocol. This is used to get 122 * information about the running image, such as size and the command 123 * line. 124 */ 125 status = sys_table->boottime->handle_protocol(handle, 126 &loaded_image_proto, (void *)&image); 127 if (status != EFI_SUCCESS) { 128 pr_efi_err(sys_table, "Failed to get loaded image protocol\n"); 129 goto fail; 130 } 131 132 dram_base = get_dram_base(sys_table); 133 if (dram_base == EFI_ERROR) { 134 pr_efi_err(sys_table, "Failed to find DRAM base\n"); 135 goto fail; 136 } 137 138 /* 139 * Get the command line from EFI, using the LOADED_IMAGE 140 * protocol. We are going to copy the command line into the 141 * device tree, so this can be allocated anywhere. 142 */ 143 cmdline_ptr = efi_convert_cmdline(sys_table, image, &cmdline_size); 144 if (!cmdline_ptr) { 145 pr_efi_err(sys_table, "getting command line via LOADED_IMAGE_PROTOCOL\n"); 146 goto fail; 147 } 148 149 if (IS_ENABLED(CONFIG_CMDLINE_EXTEND) || 150 IS_ENABLED(CONFIG_CMDLINE_FORCE) || 151 cmdline_size == 0) 152 efi_parse_options(CONFIG_CMDLINE); 153 154 if (!IS_ENABLED(CONFIG_CMDLINE_FORCE) && cmdline_size > 0) 155 efi_parse_options(cmdline_ptr); 156 157 pr_efi(sys_table, "Booting Linux Kernel...\n"); 158 159 si = setup_graphics(sys_table); 160 161 status = handle_kernel_image(sys_table, image_addr, &image_size, 162 &reserve_addr, 163 &reserve_size, 164 dram_base, image); 165 if (status != EFI_SUCCESS) { 166 pr_efi_err(sys_table, "Failed to relocate kernel\n"); 167 goto fail_free_cmdline; 168 } 169 170 /* Ask the firmware to clear memory on unclean shutdown */ 171 efi_enable_reset_attack_mitigation(sys_table); 172 173 secure_boot = efi_get_secureboot(sys_table); 174 175 /* 176 * Unauthenticated device tree data is a security hazard, so ignore 177 * 'dtb=' unless UEFI Secure Boot is disabled. We assume that secure 178 * boot is enabled if we can't determine its state. 179 */ 180 if (!IS_ENABLED(CONFIG_EFI_ARMSTUB_DTB_LOADER) || 181 secure_boot != efi_secureboot_mode_disabled) { 182 if (strstr(cmdline_ptr, "dtb=")) 183 pr_efi(sys_table, "Ignoring DTB from command line.\n"); 184 } else { 185 status = handle_cmdline_files(sys_table, image, cmdline_ptr, 186 "dtb=", 187 ~0UL, &fdt_addr, &fdt_size); 188 189 if (status != EFI_SUCCESS) { 190 pr_efi_err(sys_table, "Failed to load device tree!\n"); 191 goto fail_free_image; 192 } 193 } 194 195 if (fdt_addr) { 196 pr_efi(sys_table, "Using DTB from command line\n"); 197 } else { 198 /* Look for a device tree configuration table entry. */ 199 fdt_addr = (uintptr_t)get_fdt(sys_table, &fdt_size); 200 if (fdt_addr) 201 pr_efi(sys_table, "Using DTB from configuration table\n"); 202 } 203 204 if (!fdt_addr) 205 pr_efi(sys_table, "Generating empty DTB\n"); 206 207 status = handle_cmdline_files(sys_table, image, cmdline_ptr, "initrd=", 208 efi_get_max_initrd_addr(dram_base, 209 *image_addr), 210 (unsigned long *)&initrd_addr, 211 (unsigned long *)&initrd_size); 212 if (status != EFI_SUCCESS) 213 pr_efi_err(sys_table, "Failed initrd from command line!\n"); 214 215 efi_random_get_seed(sys_table); 216 217 /* hibernation expects the runtime regions to stay in the same place */ 218 if (!IS_ENABLED(CONFIG_HIBERNATION) && !nokaslr()) { 219 /* 220 * Randomize the base of the UEFI runtime services region. 221 * Preserve the 2 MB alignment of the region by taking a 222 * shift of 21 bit positions into account when scaling 223 * the headroom value using a 32-bit random value. 224 */ 225 static const u64 headroom = EFI_RT_VIRTUAL_LIMIT - 226 EFI_RT_VIRTUAL_BASE - 227 EFI_RT_VIRTUAL_SIZE; 228 u32 rnd; 229 230 status = efi_get_random_bytes(sys_table, sizeof(rnd), 231 (u8 *)&rnd); 232 if (status == EFI_SUCCESS) { 233 virtmap_base = EFI_RT_VIRTUAL_BASE + 234 (((headroom >> 21) * rnd) >> (32 - 21)); 235 } 236 } 237 238 new_fdt_addr = fdt_addr; 239 status = allocate_new_fdt_and_exit_boot(sys_table, handle, 240 &new_fdt_addr, efi_get_max_fdt_addr(dram_base), 241 initrd_addr, initrd_size, cmdline_ptr, 242 fdt_addr, fdt_size); 243 244 /* 245 * If all went well, we need to return the FDT address to the 246 * calling function so it can be passed to kernel as part of 247 * the kernel boot protocol. 248 */ 249 if (status == EFI_SUCCESS) 250 return new_fdt_addr; 251 252 pr_efi_err(sys_table, "Failed to update FDT and exit boot services\n"); 253 254 efi_free(sys_table, initrd_size, initrd_addr); 255 efi_free(sys_table, fdt_size, fdt_addr); 256 257fail_free_image: 258 efi_free(sys_table, image_size, *image_addr); 259 efi_free(sys_table, reserve_size, reserve_addr); 260fail_free_cmdline: 261 free_screen_info(sys_table, si); 262 efi_free(sys_table, cmdline_size, (unsigned long)cmdline_ptr); 263fail: 264 return EFI_ERROR; 265} 266 267static int cmp_mem_desc(const void *l, const void *r) 268{ 269 const efi_memory_desc_t *left = l, *right = r; 270 271 return (left->phys_addr > right->phys_addr) ? 1 : -1; 272} 273 274/* 275 * Returns whether region @left ends exactly where region @right starts, 276 * or false if either argument is NULL. 277 */ 278static bool regions_are_adjacent(efi_memory_desc_t *left, 279 efi_memory_desc_t *right) 280{ 281 u64 left_end; 282 283 if (left == NULL || right == NULL) 284 return false; 285 286 left_end = left->phys_addr + left->num_pages * EFI_PAGE_SIZE; 287 288 return left_end == right->phys_addr; 289} 290 291/* 292 * Returns whether region @left and region @right have compatible memory type 293 * mapping attributes, and are both EFI_MEMORY_RUNTIME regions. 294 */ 295static bool regions_have_compatible_memory_type_attrs(efi_memory_desc_t *left, 296 efi_memory_desc_t *right) 297{ 298 static const u64 mem_type_mask = EFI_MEMORY_WB | EFI_MEMORY_WT | 299 EFI_MEMORY_WC | EFI_MEMORY_UC | 300 EFI_MEMORY_RUNTIME; 301 302 return ((left->attribute ^ right->attribute) & mem_type_mask) == 0; 303} 304 305/* 306 * efi_get_virtmap() - create a virtual mapping for the EFI memory map 307 * 308 * This function populates the virt_addr fields of all memory region descriptors 309 * in @memory_map whose EFI_MEMORY_RUNTIME attribute is set. Those descriptors 310 * are also copied to @runtime_map, and their total count is returned in @count. 311 */ 312void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size, 313 unsigned long desc_size, efi_memory_desc_t *runtime_map, 314 int *count) 315{ 316 u64 efi_virt_base = virtmap_base; 317 efi_memory_desc_t *in, *prev = NULL, *out = runtime_map; 318 int l; 319 320 /* 321 * To work around potential issues with the Properties Table feature 322 * introduced in UEFI 2.5, which may split PE/COFF executable images 323 * in memory into several RuntimeServicesCode and RuntimeServicesData 324 * regions, we need to preserve the relative offsets between adjacent 325 * EFI_MEMORY_RUNTIME regions with the same memory type attributes. 326 * The easiest way to find adjacent regions is to sort the memory map 327 * before traversing it. 328 */ 329 if (IS_ENABLED(CONFIG_ARM64)) 330 sort(memory_map, map_size / desc_size, desc_size, cmp_mem_desc, 331 NULL); 332 333 for (l = 0; l < map_size; l += desc_size, prev = in) { 334 u64 paddr, size; 335 336 in = (void *)memory_map + l; 337 if (!(in->attribute & EFI_MEMORY_RUNTIME)) 338 continue; 339 340 paddr = in->phys_addr; 341 size = in->num_pages * EFI_PAGE_SIZE; 342 343 /* 344 * Make the mapping compatible with 64k pages: this allows 345 * a 4k page size kernel to kexec a 64k page size kernel and 346 * vice versa. 347 */ 348 if ((IS_ENABLED(CONFIG_ARM64) && 349 !regions_are_adjacent(prev, in)) || 350 !regions_have_compatible_memory_type_attrs(prev, in)) { 351 352 paddr = round_down(in->phys_addr, SZ_64K); 353 size += in->phys_addr - paddr; 354 355 /* 356 * Avoid wasting memory on PTEs by choosing a virtual 357 * base that is compatible with section mappings if this 358 * region has the appropriate size and physical 359 * alignment. (Sections are 2 MB on 4k granule kernels) 360 */ 361 if (IS_ALIGNED(in->phys_addr, SZ_2M) && size >= SZ_2M) 362 efi_virt_base = round_up(efi_virt_base, SZ_2M); 363 else 364 efi_virt_base = round_up(efi_virt_base, SZ_64K); 365 } 366 367 in->virt_addr = efi_virt_base + in->phys_addr - paddr; 368 efi_virt_base += size; 369 370 memcpy(out, in, desc_size); 371 out = (void *)out + desc_size; 372 ++*count; 373 } 374}