1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * EFI application memory management 4 * 5 * Copyright (c) 2016 Alexander Graf 6 */ 7 8 #include <common.h> 9 #include <efi_loader.h> 10 #include <inttypes.h> 11 #include <malloc.h> 12 #include <mapmem.h> 13 #include <watchdog.h> 14 #include <linux/list_sort.h> 15 16 DECLARE_GLOBAL_DATA_PTR; 17 18 efi_uintn_t efi_memory_map_key; 19 20 struct efi_mem_list { 21 struct list_head link; 22 struct efi_mem_desc desc; 23 }; 24 25 #define EFI_CARVE_NO_OVERLAP -1 26 #define EFI_CARVE_LOOP_AGAIN -2 27 #define EFI_CARVE_OVERLAPS_NONRAM -3 28 29 /* This list contains all memory map items */ 30 LIST_HEAD(efi_mem); 31 32 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER 33 void *efi_bounce_buffer; 34 #endif 35 36 /* 37 * U-Boot services each EFI AllocatePool request as a separate 38 * (multiple) page allocation. We have to track the number of pages 39 * to be able to free the correct amount later. 40 * EFI requires 8 byte alignment for pool allocations, so we can 41 * prepend each allocation with an 64 bit header tracking the 42 * allocation size, and hand out the remainder to the caller. 43 */ 44 struct efi_pool_allocation { 45 u64 num_pages; 46 char data[] __aligned(ARCH_DMA_MINALIGN); 47 }; 48 49 /* 50 * Sorts the memory list from highest address to lowest address 51 * 52 * When allocating memory we should always start from the highest 53 * address chunk, so sort the memory list such that the first list 54 * iterator gets the highest address and goes lower from there. 55 */ 56 static int efi_mem_cmp(void *priv, struct list_head *a, struct list_head *b) 57 { 58 struct efi_mem_list *mema = list_entry(a, struct efi_mem_list, link); 59 struct efi_mem_list *memb = list_entry(b, struct efi_mem_list, link); 60 61 if (mema->desc.physical_start == memb->desc.physical_start) 62 return 0; 63 else if (mema->desc.physical_start < memb->desc.physical_start) 64 return 1; 65 else 66 return -1; 67 } 68 69 static void efi_mem_sort(void) 70 { 71 list_sort(NULL, &efi_mem, efi_mem_cmp); 72 } 73 74 /** efi_mem_carve_out - unmap memory region 75 * 76 * @map: memory map 77 * @carve_desc: memory region to unmap 78 * @overlap_only_ram: the carved out region may only overlap RAM 79 * Return Value: the number of overlapping pages which have been 80 * removed from the map, 81 * EFI_CARVE_NO_OVERLAP, if the regions don't overlap, 82 * EFI_CARVE_OVERLAPS_NONRAM, if the carve and map overlap, 83 * and the map contains anything but free ram 84 * (only when overlap_only_ram is true), 85 * EFI_CARVE_LOOP_AGAIN, if the mapping list should be 86 * traversed again, as it has been altered. 87 * 88 * Unmaps all memory occupied by the carve_desc region from the list entry 89 * pointed to by map. 90 * 91 * In case of EFI_CARVE_OVERLAPS_NONRAM it is the callers responsibility 92 * to re-add the already carved out pages to the mapping. 93 */ 94 static s64 efi_mem_carve_out(struct efi_mem_list *map, 95 struct efi_mem_desc *carve_desc, 96 bool overlap_only_ram) 97 { 98 struct efi_mem_list *newmap; 99 struct efi_mem_desc *map_desc = &map->desc; 100 uint64_t map_start = map_desc->physical_start; 101 uint64_t map_end = map_start + (map_desc->num_pages << EFI_PAGE_SHIFT); 102 uint64_t carve_start = carve_desc->physical_start; 103 uint64_t carve_end = carve_start + 104 (carve_desc->num_pages << EFI_PAGE_SHIFT); 105 106 /* check whether we're overlapping */ 107 if ((carve_end <= map_start) || (carve_start >= map_end)) 108 return EFI_CARVE_NO_OVERLAP; 109 110 /* We're overlapping with non-RAM, warn the caller if desired */ 111 if (overlap_only_ram && (map_desc->type != EFI_CONVENTIONAL_MEMORY)) 112 return EFI_CARVE_OVERLAPS_NONRAM; 113 114 /* Sanitize carve_start and carve_end to lie within our bounds */ 115 carve_start = max(carve_start, map_start); 116 carve_end = min(carve_end, map_end); 117 118 /* Carving at the beginning of our map? Just move it! */ 119 if (carve_start == map_start) { 120 if (map_end == carve_end) { 121 /* Full overlap, just remove map */ 122 list_del(&map->link); 123 free(map); 124 } else { 125 map->desc.physical_start = carve_end; 126 map->desc.num_pages = (map_end - carve_end) 127 >> EFI_PAGE_SHIFT; 128 } 129 130 return (carve_end - carve_start) >> EFI_PAGE_SHIFT; 131 } 132 133 /* 134 * Overlapping maps, just split the list map at carve_start, 135 * it will get moved or removed in the next iteration. 136 * 137 * [ map_desc |__carve_start__| newmap ] 138 */ 139 140 /* Create a new map from [ carve_start ... map_end ] */ 141 newmap = calloc(1, sizeof(*newmap)); 142 newmap->desc = map->desc; 143 newmap->desc.physical_start = carve_start; 144 newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT; 145 /* Insert before current entry (descending address order) */ 146 list_add_tail(&newmap->link, &map->link); 147 148 /* Shrink the map to [ map_start ... carve_start ] */ 149 map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT; 150 151 return EFI_CARVE_LOOP_AGAIN; 152 } 153 154 uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type, 155 bool overlap_only_ram) 156 { 157 struct list_head *lhandle; 158 struct efi_mem_list *newlist; 159 bool carve_again; 160 uint64_t carved_pages = 0; 161 162 debug("%s: 0x%" PRIx64 " 0x%" PRIx64 " %d %s\n", __func__, 163 start, pages, memory_type, overlap_only_ram ? "yes" : "no"); 164 165 if (memory_type >= EFI_MAX_MEMORY_TYPE) 166 return EFI_INVALID_PARAMETER; 167 168 if (!pages) 169 return start; 170 171 ++efi_memory_map_key; 172 newlist = calloc(1, sizeof(*newlist)); 173 newlist->desc.type = memory_type; 174 newlist->desc.physical_start = start; 175 newlist->desc.virtual_start = start; 176 newlist->desc.num_pages = pages; 177 178 switch (memory_type) { 179 case EFI_RUNTIME_SERVICES_CODE: 180 case EFI_RUNTIME_SERVICES_DATA: 181 newlist->desc.attribute = EFI_MEMORY_WB | EFI_MEMORY_RUNTIME; 182 break; 183 case EFI_MMAP_IO: 184 newlist->desc.attribute = EFI_MEMORY_RUNTIME; 185 break; 186 default: 187 newlist->desc.attribute = EFI_MEMORY_WB; 188 break; 189 } 190 191 /* Add our new map */ 192 do { 193 carve_again = false; 194 list_for_each(lhandle, &efi_mem) { 195 struct efi_mem_list *lmem; 196 s64 r; 197 198 lmem = list_entry(lhandle, struct efi_mem_list, link); 199 r = efi_mem_carve_out(lmem, &newlist->desc, 200 overlap_only_ram); 201 switch (r) { 202 case EFI_CARVE_OVERLAPS_NONRAM: 203 /* 204 * The user requested to only have RAM overlaps, 205 * but we hit a non-RAM region. Error out. 206 */ 207 return 0; 208 case EFI_CARVE_NO_OVERLAP: 209 /* Just ignore this list entry */ 210 break; 211 case EFI_CARVE_LOOP_AGAIN: 212 /* 213 * We split an entry, but need to loop through 214 * the list again to actually carve it. 215 */ 216 carve_again = true; 217 break; 218 default: 219 /* We carved a number of pages */ 220 carved_pages += r; 221 carve_again = true; 222 break; 223 } 224 225 if (carve_again) { 226 /* The list changed, we need to start over */ 227 break; 228 } 229 } 230 } while (carve_again); 231 232 if (overlap_only_ram && (carved_pages != pages)) { 233 /* 234 * The payload wanted to have RAM overlaps, but we overlapped 235 * with an unallocated region. Error out. 236 */ 237 return 0; 238 } 239 240 /* Add our new map */ 241 list_add_tail(&newlist->link, &efi_mem); 242 243 /* And make sure memory is listed in descending order */ 244 efi_mem_sort(); 245 246 return start; 247 } 248 249 static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr) 250 { 251 struct list_head *lhandle; 252 253 list_for_each(lhandle, &efi_mem) { 254 struct efi_mem_list *lmem = list_entry(lhandle, 255 struct efi_mem_list, link); 256 struct efi_mem_desc *desc = &lmem->desc; 257 uint64_t desc_len = desc->num_pages << EFI_PAGE_SHIFT; 258 uint64_t desc_end = desc->physical_start + desc_len; 259 uint64_t curmax = min(max_addr, desc_end); 260 uint64_t ret = curmax - len; 261 262 /* We only take memory from free RAM */ 263 if (desc->type != EFI_CONVENTIONAL_MEMORY) 264 continue; 265 266 /* Out of bounds for max_addr */ 267 if ((ret + len) > max_addr) 268 continue; 269 270 /* Out of bounds for upper map limit */ 271 if ((ret + len) > desc_end) 272 continue; 273 274 /* Out of bounds for lower map limit */ 275 if (ret < desc->physical_start) 276 continue; 277 278 /* Return the highest address in this map within bounds */ 279 return ret; 280 } 281 282 return 0; 283 } 284 285 /* 286 * Allocate memory pages. 287 * 288 * @type type of allocation to be performed 289 * @memory_type usage type of the allocated memory 290 * @pages number of pages to be allocated 291 * @memory allocated memory 292 * @return status code 293 */ 294 efi_status_t efi_allocate_pages(int type, int memory_type, 295 efi_uintn_t pages, uint64_t *memory) 296 { 297 u64 len = pages << EFI_PAGE_SHIFT; 298 efi_status_t r = EFI_SUCCESS; 299 uint64_t addr; 300 301 if (!memory) 302 return EFI_INVALID_PARAMETER; 303 304 switch (type) { 305 case EFI_ALLOCATE_ANY_PAGES: 306 /* Any page */ 307 addr = efi_find_free_memory(len, gd->start_addr_sp); 308 if (!addr) { 309 r = EFI_NOT_FOUND; 310 break; 311 } 312 break; 313 case EFI_ALLOCATE_MAX_ADDRESS: 314 /* Max address */ 315 addr = efi_find_free_memory(len, *memory); 316 if (!addr) { 317 r = EFI_NOT_FOUND; 318 break; 319 } 320 break; 321 case EFI_ALLOCATE_ADDRESS: 322 /* Exact address, reserve it. The addr is already in *memory. */ 323 addr = *memory; 324 break; 325 default: 326 /* UEFI doesn't specify other allocation types */ 327 r = EFI_INVALID_PARAMETER; 328 break; 329 } 330 331 if (r == EFI_SUCCESS) { 332 uint64_t ret; 333 334 /* Reserve that map in our memory maps */ 335 ret = efi_add_memory_map(addr, pages, memory_type, true); 336 if (ret == addr) { 337 *memory = (uintptr_t)map_sysmem(addr, len); 338 } else { 339 /* Map would overlap, bail out */ 340 r = EFI_OUT_OF_RESOURCES; 341 } 342 } 343 344 return r; 345 } 346 347 void *efi_alloc(uint64_t len, int memory_type) 348 { 349 uint64_t ret = 0; 350 uint64_t pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT; 351 efi_status_t r; 352 353 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type, pages, 354 &ret); 355 if (r == EFI_SUCCESS) 356 return (void*)(uintptr_t)ret; 357 358 return NULL; 359 } 360 361 /* 362 * Free memory pages. 363 * 364 * @memory start of the memory area to be freed 365 * @pages number of pages to be freed 366 * @return status code 367 */ 368 efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages) 369 { 370 uint64_t r = 0; 371 uint64_t addr = map_to_sysmem((void *)(uintptr_t)memory); 372 373 r = efi_add_memory_map(addr, pages, EFI_CONVENTIONAL_MEMORY, false); 374 /* Merging of adjacent free regions is missing */ 375 376 if (r == addr) 377 return EFI_SUCCESS; 378 379 return EFI_NOT_FOUND; 380 } 381 382 /* 383 * Allocate memory from pool. 384 * 385 * @pool_type type of the pool from which memory is to be allocated 386 * @size number of bytes to be allocated 387 * @buffer allocated memory 388 * @return status code 389 */ 390 efi_status_t efi_allocate_pool(int pool_type, efi_uintn_t size, void **buffer) 391 { 392 efi_status_t r; 393 struct efi_pool_allocation *alloc; 394 u64 num_pages = (size + sizeof(struct efi_pool_allocation) + 395 EFI_PAGE_MASK) >> EFI_PAGE_SHIFT; 396 397 if (!buffer) 398 return EFI_INVALID_PARAMETER; 399 400 if (size == 0) { 401 *buffer = NULL; 402 return EFI_SUCCESS; 403 } 404 405 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, pool_type, num_pages, 406 (uint64_t *)&alloc); 407 408 if (r == EFI_SUCCESS) { 409 alloc->num_pages = num_pages; 410 *buffer = alloc->data; 411 } 412 413 return r; 414 } 415 416 /* 417 * Free memory from pool. 418 * 419 * @buffer start of memory to be freed 420 * @return status code 421 */ 422 efi_status_t efi_free_pool(void *buffer) 423 { 424 efi_status_t r; 425 struct efi_pool_allocation *alloc; 426 427 if (buffer == NULL) 428 return EFI_INVALID_PARAMETER; 429 430 alloc = container_of(buffer, struct efi_pool_allocation, data); 431 /* Sanity check, was the supplied address returned by allocate_pool */ 432 assert(((uintptr_t)alloc & EFI_PAGE_MASK) == 0); 433 434 r = efi_free_pages((uintptr_t)alloc, alloc->num_pages); 435 436 return r; 437 } 438 439 /* 440 * Get map describing memory usage. 441 * 442 * @memory_map_size on entry the size, in bytes, of the memory map buffer, 443 * on exit the size of the copied memory map 444 * @memory_map buffer to which the memory map is written 445 * @map_key key for the memory map 446 * @descriptor_size size of an individual memory descriptor 447 * @descriptor_version version number of the memory descriptor structure 448 * @return status code 449 */ 450 efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size, 451 struct efi_mem_desc *memory_map, 452 efi_uintn_t *map_key, 453 efi_uintn_t *descriptor_size, 454 uint32_t *descriptor_version) 455 { 456 efi_uintn_t map_size = 0; 457 int map_entries = 0; 458 struct list_head *lhandle; 459 efi_uintn_t provided_map_size; 460 461 if (!memory_map_size) 462 return EFI_INVALID_PARAMETER; 463 464 provided_map_size = *memory_map_size; 465 466 list_for_each(lhandle, &efi_mem) 467 map_entries++; 468 469 map_size = map_entries * sizeof(struct efi_mem_desc); 470 471 *memory_map_size = map_size; 472 473 if (provided_map_size < map_size) 474 return EFI_BUFFER_TOO_SMALL; 475 476 if (!memory_map) 477 return EFI_INVALID_PARAMETER; 478 479 if (descriptor_size) 480 *descriptor_size = sizeof(struct efi_mem_desc); 481 482 if (descriptor_version) 483 *descriptor_version = EFI_MEMORY_DESCRIPTOR_VERSION; 484 485 /* Copy list into array */ 486 /* Return the list in ascending order */ 487 memory_map = &memory_map[map_entries - 1]; 488 list_for_each(lhandle, &efi_mem) { 489 struct efi_mem_list *lmem; 490 491 lmem = list_entry(lhandle, struct efi_mem_list, link); 492 *memory_map = lmem->desc; 493 memory_map--; 494 } 495 496 if (map_key) 497 *map_key = efi_memory_map_key; 498 499 return EFI_SUCCESS; 500 } 501 502 __weak void efi_add_known_memory(void) 503 { 504 int i; 505 506 /* Add RAM */ 507 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { 508 u64 ram_start = gd->bd->bi_dram[i].start; 509 u64 ram_size = gd->bd->bi_dram[i].size; 510 u64 start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK; 511 u64 pages = (ram_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT; 512 513 efi_add_memory_map(start, pages, EFI_CONVENTIONAL_MEMORY, 514 false); 515 } 516 } 517 518 /* Add memory regions for U-Boot's memory and for the runtime services code */ 519 static void add_u_boot_and_runtime(void) 520 { 521 unsigned long runtime_start, runtime_end, runtime_pages; 522 unsigned long uboot_start, uboot_pages; 523 unsigned long uboot_stack_size = 16 * 1024 * 1024; 524 525 /* Add U-Boot */ 526 uboot_start = (gd->start_addr_sp - uboot_stack_size) & ~EFI_PAGE_MASK; 527 uboot_pages = (gd->ram_top - uboot_start) >> EFI_PAGE_SHIFT; 528 efi_add_memory_map(uboot_start, uboot_pages, EFI_LOADER_DATA, false); 529 530 /* Add Runtime Services */ 531 runtime_start = (ulong)&__efi_runtime_start & ~EFI_PAGE_MASK; 532 runtime_end = (ulong)&__efi_runtime_stop; 533 runtime_end = (runtime_end + EFI_PAGE_MASK) & ~EFI_PAGE_MASK; 534 runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT; 535 efi_add_memory_map(runtime_start, runtime_pages, 536 EFI_RUNTIME_SERVICES_CODE, false); 537 } 538 539 int efi_memory_init(void) 540 { 541 efi_add_known_memory(); 542 543 if (!IS_ENABLED(CONFIG_SANDBOX)) 544 add_u_boot_and_runtime(); 545 546 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER 547 /* Request a 32bit 64MB bounce buffer region */ 548 uint64_t efi_bounce_buffer_addr = 0xffffffff; 549 550 if (efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS, EFI_LOADER_DATA, 551 (64 * 1024 * 1024) >> EFI_PAGE_SHIFT, 552 &efi_bounce_buffer_addr) != EFI_SUCCESS) 553 return -1; 554 555 efi_bounce_buffer = (void*)(uintptr_t)efi_bounce_buffer_addr; 556 #endif 557 558 return 0; 559 } 560