Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 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 | |
Ard Biesheuvel | bd66947 | 2014-07-02 14:54:42 +0200 | [diff] [blame] | 15 | #include <linux/efi.h> |
Ard Biesheuvel | 0ce3cc0 | 2015-09-25 23:02:19 +0100 | [diff] [blame] | 16 | #include <linux/sort.h> |
Ard Biesheuvel | bd66947 | 2014-07-02 14:54:42 +0200 | [diff] [blame] | 17 | #include <asm/efi.h> |
| 18 | |
| 19 | #include "efistub.h" |
| 20 | |
Ard Biesheuvel | e69176d | 2017-04-04 17:09:10 +0100 | [diff] [blame] | 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 | |
Ard Biesheuvel | 197dece | 2017-04-17 10:32:01 +0100 | [diff] [blame] | 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 | |
Ard Biesheuvel | e69176d | 2017-04-04 17:09:10 +0100 | [diff] [blame] | 41 | static u64 virtmap_base = EFI_RT_VIRTUAL_BASE; |
| 42 | |
Ard Biesheuvel | bd66947 | 2014-07-02 14:54:42 +0200 | [diff] [blame] | 43 | void efi_char16_printk(efi_system_table_t *sys_table_arg, |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 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 | |
Ard Biesheuvel | f0827e1 | 2016-04-25 21:06:54 +0100 | [diff] [blame] | 52 | static 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 | } |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 71 | |
Ard Biesheuvel | b844470 | 2018-09-21 09:32:45 -0700 | [diff] [blame] | 72 | void install_memreserve_table(efi_system_table_t *sys_table_arg) |
| 73 | { |
| 74 | struct linux_efi_memreserve *rsv; |
| 75 | efi_guid_t memreserve_table_guid = LINUX_EFI_MEMRESERVE_TABLE_GUID; |
| 76 | efi_status_t status; |
| 77 | |
Ard Biesheuvel | eff8962 | 2018-11-14 09:55:43 -0800 | [diff] [blame] | 78 | if (IS_ENABLED(CONFIG_ARM)) |
| 79 | return; |
| 80 | |
Ard Biesheuvel | b844470 | 2018-09-21 09:32:45 -0700 | [diff] [blame] | 81 | status = efi_call_early(allocate_pool, EFI_LOADER_DATA, sizeof(*rsv), |
| 82 | (void **)&rsv); |
| 83 | if (status != EFI_SUCCESS) { |
| 84 | pr_efi_err(sys_table_arg, "Failed to allocate memreserve entry!\n"); |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | rsv->next = 0; |
| 89 | rsv->base = 0; |
| 90 | rsv->size = 0; |
| 91 | |
| 92 | status = efi_call_early(install_configuration_table, |
| 93 | &memreserve_table_guid, |
| 94 | rsv); |
| 95 | if (status != EFI_SUCCESS) |
| 96 | pr_efi_err(sys_table_arg, "Failed to install memreserve config table!\n"); |
| 97 | } |
| 98 | |
| 99 | |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 100 | /* |
| 101 | * This function handles the architcture specific differences between arm and |
| 102 | * arm64 regarding where the kernel image must be loaded and any memory that |
| 103 | * must be reserved. On failure it is required to free all |
| 104 | * all allocations it has made. |
| 105 | */ |
Ard Biesheuvel | bd66947 | 2014-07-02 14:54:42 +0200 | [diff] [blame] | 106 | efi_status_t handle_kernel_image(efi_system_table_t *sys_table, |
| 107 | unsigned long *image_addr, |
| 108 | unsigned long *image_size, |
| 109 | unsigned long *reserve_addr, |
| 110 | unsigned long *reserve_size, |
| 111 | unsigned long dram_base, |
| 112 | efi_loaded_image_t *image); |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 113 | /* |
| 114 | * EFI entry point for the arm/arm64 EFI stubs. This is the entrypoint |
| 115 | * that is described in the PE/COFF header. Most of the code is the same |
| 116 | * for both archictectures, with the arch-specific code provided in the |
| 117 | * handle_kernel_image() function. |
| 118 | */ |
Ard Biesheuvel | ddeeefe | 2015-01-12 20:28:20 +0000 | [diff] [blame] | 119 | unsigned long efi_entry(void *handle, efi_system_table_t *sys_table, |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 120 | unsigned long *image_addr) |
| 121 | { |
| 122 | efi_loaded_image_t *image; |
| 123 | efi_status_t status; |
| 124 | unsigned long image_size = 0; |
| 125 | unsigned long dram_base; |
| 126 | /* addr/point and size pairs for memory management*/ |
| 127 | unsigned long initrd_addr; |
| 128 | u64 initrd_size = 0; |
Ard Biesheuvel | 345c736 | 2014-04-03 17:46:58 +0200 | [diff] [blame] | 129 | unsigned long fdt_addr = 0; /* Original DTB */ |
Ard Biesheuvel | a643375 | 2015-03-04 13:02:29 +0100 | [diff] [blame] | 130 | unsigned long fdt_size = 0; |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 131 | char *cmdline_ptr = NULL; |
| 132 | int cmdline_size = 0; |
| 133 | unsigned long new_fdt_addr; |
| 134 | efi_guid_t loaded_image_proto = LOADED_IMAGE_PROTOCOL_GUID; |
| 135 | unsigned long reserve_addr = 0; |
| 136 | unsigned long reserve_size = 0; |
David Howells | de8cb45 | 2017-02-06 11:22:43 +0000 | [diff] [blame] | 137 | enum efi_secureboot_mode secure_boot; |
Ard Biesheuvel | f0827e1 | 2016-04-25 21:06:54 +0100 | [diff] [blame] | 138 | struct screen_info *si; |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 139 | |
| 140 | /* Check if we were booted by the EFI firmware */ |
| 141 | if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) |
| 142 | goto fail; |
| 143 | |
Ard Biesheuvel | b9d6769b | 2016-02-17 12:36:03 +0000 | [diff] [blame] | 144 | status = check_platform_features(sys_table); |
| 145 | if (status != EFI_SUCCESS) |
| 146 | goto fail; |
| 147 | |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 148 | /* |
| 149 | * Get a handle to the loaded image protocol. This is used to get |
| 150 | * information about the running image, such as size and the command |
| 151 | * line. |
| 152 | */ |
| 153 | status = sys_table->boottime->handle_protocol(handle, |
| 154 | &loaded_image_proto, (void *)&image); |
| 155 | if (status != EFI_SUCCESS) { |
| 156 | pr_efi_err(sys_table, "Failed to get loaded image protocol\n"); |
| 157 | goto fail; |
| 158 | } |
| 159 | |
| 160 | dram_base = get_dram_base(sys_table); |
| 161 | if (dram_base == EFI_ERROR) { |
| 162 | pr_efi_err(sys_table, "Failed to find DRAM base\n"); |
| 163 | goto fail; |
| 164 | } |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 165 | |
| 166 | /* |
| 167 | * Get the command line from EFI, using the LOADED_IMAGE |
| 168 | * protocol. We are going to copy the command line into the |
| 169 | * device tree, so this can be allocated anywhere. |
| 170 | */ |
| 171 | cmdline_ptr = efi_convert_cmdline(sys_table, image, &cmdline_size); |
| 172 | if (!cmdline_ptr) { |
| 173 | pr_efi_err(sys_table, "getting command line via LOADED_IMAGE_PROTOCOL\n"); |
Ard Biesheuvel | 2b5fe07 | 2016-01-26 14:48:29 +0100 | [diff] [blame] | 174 | goto fail; |
| 175 | } |
| 176 | |
Ard Biesheuvel | eeff7d6 | 2017-04-04 17:09:09 +0100 | [diff] [blame] | 177 | if (IS_ENABLED(CONFIG_CMDLINE_EXTEND) || |
| 178 | IS_ENABLED(CONFIG_CMDLINE_FORCE) || |
| 179 | cmdline_size == 0) |
| 180 | efi_parse_options(CONFIG_CMDLINE); |
| 181 | |
| 182 | if (!IS_ENABLED(CONFIG_CMDLINE_FORCE) && cmdline_size > 0) |
| 183 | efi_parse_options(cmdline_ptr); |
| 184 | |
| 185 | pr_efi(sys_table, "Booting Linux Kernel...\n"); |
| 186 | |
Ard Biesheuvel | f0827e1 | 2016-04-25 21:06:54 +0100 | [diff] [blame] | 187 | si = setup_graphics(sys_table); |
| 188 | |
Ard Biesheuvel | 2b5fe07 | 2016-01-26 14:48:29 +0100 | [diff] [blame] | 189 | status = handle_kernel_image(sys_table, image_addr, &image_size, |
| 190 | &reserve_addr, |
| 191 | &reserve_size, |
| 192 | dram_base, image); |
| 193 | if (status != EFI_SUCCESS) { |
| 194 | pr_efi_err(sys_table, "Failed to relocate kernel\n"); |
| 195 | goto fail_free_cmdline; |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 196 | } |
| 197 | |
Matthew Garrett | ccc829b | 2017-08-25 16:50:15 +0100 | [diff] [blame] | 198 | /* Ask the firmware to clear memory on unclean shutdown */ |
| 199 | efi_enable_reset_attack_mitigation(sys_table); |
| 200 | |
Linn Crosetto | 73a64925 | 2016-04-25 21:06:36 +0100 | [diff] [blame] | 201 | secure_boot = efi_get_secureboot(sys_table); |
Linn Crosetto | 73a64925 | 2016-04-25 21:06:36 +0100 | [diff] [blame] | 202 | |
Ard Biesheuvel | 345c736 | 2014-04-03 17:46:58 +0200 | [diff] [blame] | 203 | /* |
David Howells | de8cb45 | 2017-02-06 11:22:43 +0000 | [diff] [blame] | 204 | * Unauthenticated device tree data is a security hazard, so ignore |
| 205 | * 'dtb=' unless UEFI Secure Boot is disabled. We assume that secure |
| 206 | * boot is enabled if we can't determine its state. |
Ard Biesheuvel | 345c736 | 2014-04-03 17:46:58 +0200 | [diff] [blame] | 207 | */ |
Ard Biesheuvel | 3d7ee34 | 2018-07-11 11:40:38 +0200 | [diff] [blame] | 208 | if (!IS_ENABLED(CONFIG_EFI_ARMSTUB_DTB_LOADER) || |
| 209 | secure_boot != efi_secureboot_mode_disabled) { |
| 210 | if (strstr(cmdline_ptr, "dtb=")) |
| 211 | pr_efi(sys_table, "Ignoring DTB from command line.\n"); |
Ard Biesheuvel | 345c736 | 2014-04-03 17:46:58 +0200 | [diff] [blame] | 212 | } else { |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 213 | status = handle_cmdline_files(sys_table, image, cmdline_ptr, |
| 214 | "dtb=", |
Ard Biesheuvel | a643375 | 2015-03-04 13:02:29 +0100 | [diff] [blame] | 215 | ~0UL, &fdt_addr, &fdt_size); |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 216 | |
| 217 | if (status != EFI_SUCCESS) { |
| 218 | pr_efi_err(sys_table, "Failed to load device tree!\n"); |
Ard Biesheuvel | 2b5fe07 | 2016-01-26 14:48:29 +0100 | [diff] [blame] | 219 | goto fail_free_image; |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 220 | } |
| 221 | } |
Mark Rutland | 0bcaa90 | 2014-10-23 16:33:33 +0100 | [diff] [blame] | 222 | |
| 223 | if (fdt_addr) { |
| 224 | pr_efi(sys_table, "Using DTB from command line\n"); |
| 225 | } else { |
Ard Biesheuvel | 345c736 | 2014-04-03 17:46:58 +0200 | [diff] [blame] | 226 | /* Look for a device tree configuration table entry. */ |
Ard Biesheuvel | a643375 | 2015-03-04 13:02:29 +0100 | [diff] [blame] | 227 | fdt_addr = (uintptr_t)get_fdt(sys_table, &fdt_size); |
Mark Rutland | 0bcaa90 | 2014-10-23 16:33:33 +0100 | [diff] [blame] | 228 | if (fdt_addr) |
| 229 | pr_efi(sys_table, "Using DTB from configuration table\n"); |
| 230 | } |
| 231 | |
| 232 | if (!fdt_addr) |
| 233 | pr_efi(sys_table, "Generating empty DTB\n"); |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 234 | |
Ard Biesheuvel | 138728dd | 2017-04-04 17:02:37 +0100 | [diff] [blame] | 235 | status = handle_cmdline_files(sys_table, image, cmdline_ptr, "initrd=", |
| 236 | efi_get_max_initrd_addr(dram_base, |
| 237 | *image_addr), |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 238 | (unsigned long *)&initrd_addr, |
| 239 | (unsigned long *)&initrd_size); |
| 240 | if (status != EFI_SUCCESS) |
| 241 | pr_efi_err(sys_table, "Failed initrd from command line!\n"); |
| 242 | |
Ard Biesheuvel | 568bc4e | 2016-11-12 21:32:33 +0000 | [diff] [blame] | 243 | efi_random_get_seed(sys_table); |
| 244 | |
Ard Biesheuvel | 38fb665 | 2017-10-25 11:04:48 +0100 | [diff] [blame] | 245 | /* hibernation expects the runtime regions to stay in the same place */ |
| 246 | if (!IS_ENABLED(CONFIG_HIBERNATION) && !nokaslr()) { |
Ard Biesheuvel | e69176d | 2017-04-04 17:09:10 +0100 | [diff] [blame] | 247 | /* |
| 248 | * Randomize the base of the UEFI runtime services region. |
| 249 | * Preserve the 2 MB alignment of the region by taking a |
| 250 | * shift of 21 bit positions into account when scaling |
| 251 | * the headroom value using a 32-bit random value. |
| 252 | */ |
Ard Biesheuvel | 197dece | 2017-04-17 10:32:01 +0100 | [diff] [blame] | 253 | static const u64 headroom = EFI_RT_VIRTUAL_LIMIT - |
| 254 | EFI_RT_VIRTUAL_BASE - |
| 255 | EFI_RT_VIRTUAL_SIZE; |
Ard Biesheuvel | e69176d | 2017-04-04 17:09:10 +0100 | [diff] [blame] | 256 | u32 rnd; |
| 257 | |
| 258 | status = efi_get_random_bytes(sys_table, sizeof(rnd), |
| 259 | (u8 *)&rnd); |
| 260 | if (status == EFI_SUCCESS) { |
| 261 | virtmap_base = EFI_RT_VIRTUAL_BASE + |
| 262 | (((headroom >> 21) * rnd) >> (32 - 21)); |
| 263 | } |
| 264 | } |
| 265 | |
Ard Biesheuvel | b844470 | 2018-09-21 09:32:45 -0700 | [diff] [blame] | 266 | install_memreserve_table(sys_table); |
| 267 | |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 268 | new_fdt_addr = fdt_addr; |
| 269 | status = allocate_new_fdt_and_exit_boot(sys_table, handle, |
Ard Biesheuvel | 138728dd | 2017-04-04 17:02:37 +0100 | [diff] [blame] | 270 | &new_fdt_addr, efi_get_max_fdt_addr(dram_base), |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 271 | initrd_addr, initrd_size, cmdline_ptr, |
| 272 | fdt_addr, fdt_size); |
| 273 | |
| 274 | /* |
| 275 | * If all went well, we need to return the FDT address to the |
| 276 | * calling function so it can be passed to kernel as part of |
| 277 | * the kernel boot protocol. |
| 278 | */ |
| 279 | if (status == EFI_SUCCESS) |
| 280 | return new_fdt_addr; |
| 281 | |
| 282 | pr_efi_err(sys_table, "Failed to update FDT and exit boot services\n"); |
| 283 | |
| 284 | efi_free(sys_table, initrd_size, initrd_addr); |
| 285 | efi_free(sys_table, fdt_size, fdt_addr); |
| 286 | |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 287 | fail_free_image: |
| 288 | efi_free(sys_table, image_size, *image_addr); |
| 289 | efi_free(sys_table, reserve_size, reserve_addr); |
Ard Biesheuvel | 2b5fe07 | 2016-01-26 14:48:29 +0100 | [diff] [blame] | 290 | fail_free_cmdline: |
Ard Biesheuvel | f0827e1 | 2016-04-25 21:06:54 +0100 | [diff] [blame] | 291 | free_screen_info(sys_table, si); |
Ard Biesheuvel | 2b5fe07 | 2016-01-26 14:48:29 +0100 | [diff] [blame] | 292 | efi_free(sys_table, cmdline_size, (unsigned long)cmdline_ptr); |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 293 | fail: |
| 294 | return EFI_ERROR; |
| 295 | } |
Ard Biesheuvel | f3cdfd2 | 2014-10-20 16:27:26 +0200 | [diff] [blame] | 296 | |
Ard Biesheuvel | 0ce3cc0 | 2015-09-25 23:02:19 +0100 | [diff] [blame] | 297 | static int cmp_mem_desc(const void *l, const void *r) |
| 298 | { |
| 299 | const efi_memory_desc_t *left = l, *right = r; |
| 300 | |
| 301 | return (left->phys_addr > right->phys_addr) ? 1 : -1; |
| 302 | } |
| 303 | |
| 304 | /* |
| 305 | * Returns whether region @left ends exactly where region @right starts, |
| 306 | * or false if either argument is NULL. |
| 307 | */ |
| 308 | static bool regions_are_adjacent(efi_memory_desc_t *left, |
| 309 | efi_memory_desc_t *right) |
| 310 | { |
| 311 | u64 left_end; |
| 312 | |
| 313 | if (left == NULL || right == NULL) |
| 314 | return false; |
| 315 | |
| 316 | left_end = left->phys_addr + left->num_pages * EFI_PAGE_SIZE; |
| 317 | |
| 318 | return left_end == right->phys_addr; |
| 319 | } |
| 320 | |
| 321 | /* |
| 322 | * Returns whether region @left and region @right have compatible memory type |
| 323 | * mapping attributes, and are both EFI_MEMORY_RUNTIME regions. |
| 324 | */ |
| 325 | static bool regions_have_compatible_memory_type_attrs(efi_memory_desc_t *left, |
| 326 | efi_memory_desc_t *right) |
| 327 | { |
| 328 | static const u64 mem_type_mask = EFI_MEMORY_WB | EFI_MEMORY_WT | |
| 329 | EFI_MEMORY_WC | EFI_MEMORY_UC | |
| 330 | EFI_MEMORY_RUNTIME; |
| 331 | |
| 332 | return ((left->attribute ^ right->attribute) & mem_type_mask) == 0; |
| 333 | } |
| 334 | |
Ard Biesheuvel | f3cdfd2 | 2014-10-20 16:27:26 +0200 | [diff] [blame] | 335 | /* |
| 336 | * efi_get_virtmap() - create a virtual mapping for the EFI memory map |
| 337 | * |
| 338 | * This function populates the virt_addr fields of all memory region descriptors |
| 339 | * in @memory_map whose EFI_MEMORY_RUNTIME attribute is set. Those descriptors |
| 340 | * are also copied to @runtime_map, and their total count is returned in @count. |
| 341 | */ |
| 342 | void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size, |
| 343 | unsigned long desc_size, efi_memory_desc_t *runtime_map, |
| 344 | int *count) |
| 345 | { |
Ard Biesheuvel | e69176d | 2017-04-04 17:09:10 +0100 | [diff] [blame] | 346 | u64 efi_virt_base = virtmap_base; |
Ard Biesheuvel | 0ce3cc0 | 2015-09-25 23:02:19 +0100 | [diff] [blame] | 347 | efi_memory_desc_t *in, *prev = NULL, *out = runtime_map; |
Ard Biesheuvel | f3cdfd2 | 2014-10-20 16:27:26 +0200 | [diff] [blame] | 348 | int l; |
| 349 | |
Ard Biesheuvel | 0ce3cc0 | 2015-09-25 23:02:19 +0100 | [diff] [blame] | 350 | /* |
| 351 | * To work around potential issues with the Properties Table feature |
| 352 | * introduced in UEFI 2.5, which may split PE/COFF executable images |
| 353 | * in memory into several RuntimeServicesCode and RuntimeServicesData |
| 354 | * regions, we need to preserve the relative offsets between adjacent |
| 355 | * EFI_MEMORY_RUNTIME regions with the same memory type attributes. |
| 356 | * The easiest way to find adjacent regions is to sort the memory map |
| 357 | * before traversing it. |
| 358 | */ |
Ard Biesheuvel | 29f9007 | 2017-10-22 15:14:57 +0100 | [diff] [blame] | 359 | if (IS_ENABLED(CONFIG_ARM64)) |
| 360 | sort(memory_map, map_size / desc_size, desc_size, cmp_mem_desc, |
| 361 | NULL); |
Ard Biesheuvel | 0ce3cc0 | 2015-09-25 23:02:19 +0100 | [diff] [blame] | 362 | |
| 363 | for (l = 0; l < map_size; l += desc_size, prev = in) { |
Ard Biesheuvel | f3cdfd2 | 2014-10-20 16:27:26 +0200 | [diff] [blame] | 364 | u64 paddr, size; |
| 365 | |
Ard Biesheuvel | 0ce3cc0 | 2015-09-25 23:02:19 +0100 | [diff] [blame] | 366 | in = (void *)memory_map + l; |
Ard Biesheuvel | f3cdfd2 | 2014-10-20 16:27:26 +0200 | [diff] [blame] | 367 | if (!(in->attribute & EFI_MEMORY_RUNTIME)) |
| 368 | continue; |
| 369 | |
Ard Biesheuvel | 0ce3cc0 | 2015-09-25 23:02:19 +0100 | [diff] [blame] | 370 | paddr = in->phys_addr; |
| 371 | size = in->num_pages * EFI_PAGE_SIZE; |
| 372 | |
Ard Biesheuvel | f3cdfd2 | 2014-10-20 16:27:26 +0200 | [diff] [blame] | 373 | /* |
| 374 | * Make the mapping compatible with 64k pages: this allows |
| 375 | * a 4k page size kernel to kexec a 64k page size kernel and |
| 376 | * vice versa. |
| 377 | */ |
Ard Biesheuvel | 29f9007 | 2017-10-22 15:14:57 +0100 | [diff] [blame] | 378 | if ((IS_ENABLED(CONFIG_ARM64) && |
| 379 | !regions_are_adjacent(prev, in)) || |
Ard Biesheuvel | 0ce3cc0 | 2015-09-25 23:02:19 +0100 | [diff] [blame] | 380 | !regions_have_compatible_memory_type_attrs(prev, in)) { |
Ard Biesheuvel | f3cdfd2 | 2014-10-20 16:27:26 +0200 | [diff] [blame] | 381 | |
Ard Biesheuvel | 0ce3cc0 | 2015-09-25 23:02:19 +0100 | [diff] [blame] | 382 | paddr = round_down(in->phys_addr, SZ_64K); |
| 383 | size += in->phys_addr - paddr; |
| 384 | |
| 385 | /* |
| 386 | * Avoid wasting memory on PTEs by choosing a virtual |
| 387 | * base that is compatible with section mappings if this |
| 388 | * region has the appropriate size and physical |
| 389 | * alignment. (Sections are 2 MB on 4k granule kernels) |
| 390 | */ |
| 391 | if (IS_ALIGNED(in->phys_addr, SZ_2M) && size >= SZ_2M) |
| 392 | efi_virt_base = round_up(efi_virt_base, SZ_2M); |
| 393 | else |
| 394 | efi_virt_base = round_up(efi_virt_base, SZ_64K); |
| 395 | } |
Ard Biesheuvel | f3cdfd2 | 2014-10-20 16:27:26 +0200 | [diff] [blame] | 396 | |
| 397 | in->virt_addr = efi_virt_base + in->phys_addr - paddr; |
| 398 | efi_virt_base += size; |
| 399 | |
| 400 | memcpy(out, in, desc_size); |
| 401 | out = (void *)out + desc_size; |
| 402 | ++*count; |
| 403 | } |
| 404 | } |