1 // SPDX-License-Identifier: GPL-2.0+
3 * EFI application memory management
5 * Copyright (c) 2016 Alexander Graf
8 #define LOG_CATEGORY LOGC_EFI
10 #include <efi_loader.h>
17 #include <asm/cache.h>
18 #include <asm/global_data.h>
19 #include <asm/sections.h>
20 #include <linux/list_sort.h>
21 #include <linux/sizes.h>
23 DECLARE_GLOBAL_DATA_PTR;
25 /* Magic number identifying memory allocated from pool */
26 #define EFI_ALLOC_POOL_MAGIC 0x1fe67ddf6491caa2
28 efi_uintn_t efi_memory_map_key;
31 struct list_head link;
32 struct efi_mem_desc desc;
35 #define EFI_CARVE_NO_OVERLAP -1
36 #define EFI_CARVE_LOOP_AGAIN -2
37 #define EFI_CARVE_OVERLAPS_NONRAM -3
38 #define EFI_CARVE_OUT_OF_RESOURCES -4
40 /* This list contains all memory map items */
41 static LIST_HEAD(efi_mem);
43 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
44 void *efi_bounce_buffer;
48 * struct efi_pool_allocation - memory block allocated from pool
50 * @num_pages: number of pages allocated
52 * @data: allocated pool memory
54 * U-Boot services each UEFI AllocatePool() request as a separate
55 * (multiple) page allocation. We have to track the number of pages
56 * to be able to free the correct amount later.
58 * The checksum calculated in function checksum() is used in FreePool() to avoid
59 * freeing memory not allocated by AllocatePool() and duplicate freeing.
61 * EFI requires 8 byte alignment for pool allocations, so we can
62 * prepend each allocation with these header fields.
64 struct efi_pool_allocation {
67 char data[] __aligned(ARCH_DMA_MINALIGN);
71 * checksum() - calculate checksum for memory allocated from pool
73 * @alloc: allocation header
74 * Return: checksum, always non-zero
76 static u64 checksum(struct efi_pool_allocation *alloc)
78 u64 addr = (uintptr_t)alloc;
79 u64 ret = (addr >> 32) ^ (addr << 32) ^ alloc->num_pages ^
87 * efi_mem_cmp() - comparator function for sorting memory map
89 * Sorts the memory list from highest address to lowest address
91 * When allocating memory we should always start from the highest
92 * address chunk, so sort the memory list such that the first list
93 * iterator gets the highest address and goes lower from there.
96 * @a: first memory area
97 * @b: second memory area
98 * Return: 1 if @a is before @b, -1 if @b is before @a, 0 if equal
100 static int efi_mem_cmp(void *priv, struct list_head *a, struct list_head *b)
102 struct efi_mem_list *mema = list_entry(a, struct efi_mem_list, link);
103 struct efi_mem_list *memb = list_entry(b, struct efi_mem_list, link);
105 if (mema->desc.physical_start == memb->desc.physical_start)
107 else if (mema->desc.physical_start < memb->desc.physical_start)
114 * desc_get_end() - get end address of memory area
116 * @desc: memory descriptor
117 * Return: end address + 1
119 static uint64_t desc_get_end(struct efi_mem_desc *desc)
121 return desc->physical_start + (desc->num_pages << EFI_PAGE_SHIFT);
125 * efi_mem_sort() - sort memory map
127 * Sort the memory map and then try to merge adjacent memory areas.
129 static void efi_mem_sort(void)
131 struct efi_mem_list *lmem;
132 struct efi_mem_list *prevmem = NULL;
133 bool merge_again = true;
135 list_sort(NULL, &efi_mem, efi_mem_cmp);
137 /* Now merge entries that can be merged */
138 while (merge_again) {
140 list_for_each_entry(lmem, &efi_mem, link) {
141 struct efi_mem_desc *prev;
142 struct efi_mem_desc *cur;
151 prev = &prevmem->desc;
153 if ((desc_get_end(cur) == prev->physical_start) &&
154 (prev->type == cur->type) &&
155 (prev->attribute == cur->attribute)) {
156 /* There is an existing map before, reuse it */
157 pages = cur->num_pages;
158 prev->num_pages += pages;
159 prev->physical_start -= pages << EFI_PAGE_SHIFT;
160 prev->virtual_start -= pages << EFI_PAGE_SHIFT;
161 list_del(&lmem->link);
174 * efi_mem_carve_out() - unmap memory region
177 * @carve_desc: memory region to unmap
178 * @overlap_conventional: the carved out region may only overlap free,
179 * or conventional memory
180 * Return: the number of overlapping pages which have been
181 * removed from the map,
182 * EFI_CARVE_NO_OVERLAP, if the regions don't
183 * overlap, EFI_CARVE_OVERLAPS_NONRAM, if the carve
184 * and map overlap, and the map contains anything
185 * but free ram(only when overlap_conventional is
187 * EFI_CARVE_LOOP_AGAIN, if the mapping list should
188 * be traversed again, as it has been altered.
190 * Unmaps all memory occupied by the carve_desc region from the list entry
193 * In case of EFI_CARVE_OVERLAPS_NONRAM it is the callers responsibility
194 * to re-add the already carved out pages to the mapping.
196 static s64 efi_mem_carve_out(struct efi_mem_list *map,
197 struct efi_mem_desc *carve_desc,
198 bool overlap_conventional)
200 struct efi_mem_list *newmap;
201 struct efi_mem_desc *map_desc = &map->desc;
202 uint64_t map_start = map_desc->physical_start;
203 uint64_t map_end = map_start + (map_desc->num_pages << EFI_PAGE_SHIFT);
204 uint64_t carve_start = carve_desc->physical_start;
205 uint64_t carve_end = carve_start +
206 (carve_desc->num_pages << EFI_PAGE_SHIFT);
208 /* check whether we're overlapping */
209 if ((carve_end <= map_start) || (carve_start >= map_end))
210 return EFI_CARVE_NO_OVERLAP;
212 /* We're overlapping with non-RAM, warn the caller if desired */
213 if (overlap_conventional && (map_desc->type != EFI_CONVENTIONAL_MEMORY))
214 return EFI_CARVE_OVERLAPS_NONRAM;
216 /* Sanitize carve_start and carve_end to lie within our bounds */
217 carve_start = max(carve_start, map_start);
218 carve_end = min(carve_end, map_end);
220 /* Carving at the beginning of our map? Just move it! */
221 if (carve_start == map_start) {
222 if (map_end == carve_end) {
223 /* Full overlap, just remove map */
224 list_del(&map->link);
227 map->desc.physical_start = carve_end;
228 map->desc.virtual_start = carve_end;
229 map->desc.num_pages = (map_end - carve_end)
233 return (carve_end - carve_start) >> EFI_PAGE_SHIFT;
237 * Overlapping maps, just split the list map at carve_start,
238 * it will get moved or removed in the next iteration.
240 * [ map_desc |__carve_start__| newmap ]
243 /* Create a new map from [ carve_start ... map_end ] */
244 newmap = calloc(1, sizeof(*newmap));
246 return EFI_CARVE_OUT_OF_RESOURCES;
247 newmap->desc = map->desc;
248 newmap->desc.physical_start = carve_start;
249 newmap->desc.virtual_start = carve_start;
250 newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT;
251 /* Insert before current entry (descending address order) */
252 list_add_tail(&newmap->link, &map->link);
254 /* Shrink the map to [ map_start ... carve_start ] */
255 map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT;
257 return EFI_CARVE_LOOP_AGAIN;
261 * efi_add_memory_map_pg() - add pages to the memory map
263 * @start: start address, must be a multiple of
265 * @pages: number of pages to add
266 * @memory_type: type of memory added
267 * @overlap_conventional: region may only overlap free(conventional)
269 * Return: status code
271 efi_status_t efi_add_memory_map_pg(u64 start, u64 pages,
273 bool overlap_conventional)
275 struct efi_mem_list *lmem;
276 struct efi_mem_list *newlist;
278 uint64_t carved_pages = 0;
279 struct efi_event *evt;
281 EFI_PRINT("%s: 0x%llx 0x%llx %d %s\n", __func__,
282 start, pages, memory_type, overlap_conventional ?
285 if (memory_type >= EFI_MAX_MEMORY_TYPE)
286 return EFI_INVALID_PARAMETER;
291 ++efi_memory_map_key;
292 newlist = calloc(1, sizeof(*newlist));
294 return EFI_OUT_OF_RESOURCES;
295 newlist->desc.type = memory_type;
296 newlist->desc.physical_start = start;
297 newlist->desc.virtual_start = start;
298 newlist->desc.num_pages = pages;
300 switch (memory_type) {
301 case EFI_RUNTIME_SERVICES_CODE:
302 case EFI_RUNTIME_SERVICES_DATA:
303 newlist->desc.attribute = EFI_MEMORY_WB | EFI_MEMORY_RUNTIME;
306 newlist->desc.attribute = EFI_MEMORY_RUNTIME;
309 newlist->desc.attribute = EFI_MEMORY_WB;
313 /* Add our new map */
316 list_for_each_entry(lmem, &efi_mem, link) {
319 r = efi_mem_carve_out(lmem, &newlist->desc,
320 overlap_conventional);
322 case EFI_CARVE_OUT_OF_RESOURCES:
324 return EFI_OUT_OF_RESOURCES;
325 case EFI_CARVE_OVERLAPS_NONRAM:
327 * The user requested to only have RAM overlaps,
328 * but we hit a non-RAM region. Error out.
331 return EFI_NO_MAPPING;
332 case EFI_CARVE_NO_OVERLAP:
333 /* Just ignore this list entry */
335 case EFI_CARVE_LOOP_AGAIN:
337 * We split an entry, but need to loop through
338 * the list again to actually carve it.
343 /* We carved a number of pages */
350 /* The list changed, we need to start over */
354 } while (carve_again);
356 if (overlap_conventional && (carved_pages != pages)) {
358 * The payload wanted to have RAM overlaps, but we overlapped
359 * with an unallocated region. Error out.
362 return EFI_NO_MAPPING;
365 /* Add our new map */
366 list_add_tail(&newlist->link, &efi_mem);
368 /* And make sure memory is listed in descending order */
371 /* Notify that the memory map was changed */
372 list_for_each_entry(evt, &efi_events, link) {
375 &efi_guid_event_group_memory_map_change)) {
376 efi_signal_event(evt);
385 * efi_add_memory_map() - add memory area to the memory map
387 * @start: start address of the memory area
388 * @size: length in bytes of the memory area
389 * @memory_type: type of memory added
391 * Return: status code
393 * This function automatically aligns the start and size of the memory area
396 efi_status_t efi_add_memory_map(u64 start, u64 size, int memory_type)
400 pages = efi_size_in_pages(size + (start & EFI_PAGE_MASK));
401 start &= ~EFI_PAGE_MASK;
403 return efi_add_memory_map_pg(start, pages, memory_type, false);
407 * efi_check_allocated() - validate address to be freed
409 * Check that the address is within allocated memory:
411 * * The address must be in a range of the memory map.
412 * * The address may not point to EFI_CONVENTIONAL_MEMORY.
414 * Page alignment is not checked as this is not a requirement of
417 * @addr: address of page to be freed
418 * @must_be_allocated: return success if the page is allocated
419 * Return: status code
421 static efi_status_t efi_check_allocated(u64 addr, bool must_be_allocated)
423 struct efi_mem_list *item;
425 list_for_each_entry(item, &efi_mem, link) {
426 u64 start = item->desc.physical_start;
427 u64 end = start + (item->desc.num_pages << EFI_PAGE_SHIFT);
429 if (addr >= start && addr < end) {
430 if (must_be_allocated ^
431 (item->desc.type == EFI_CONVENTIONAL_MEMORY))
434 return EFI_NOT_FOUND;
438 return EFI_NOT_FOUND;
442 * efi_allocate_pages - allocate memory pages
444 * @type: type of allocation to be performed
445 * @memory_type: usage type of the allocated memory
446 * @pages: number of pages to be allocated
447 * @memory: allocated memory
448 * Return: status code
450 efi_status_t efi_allocate_pages(enum efi_allocate_type type,
451 enum efi_memory_type memory_type,
452 efi_uintn_t pages, uint64_t *memory)
459 /* Check import parameters */
460 if (memory_type >= EFI_PERSISTENT_MEMORY_TYPE &&
461 memory_type <= 0x6FFFFFFF)
462 return EFI_INVALID_PARAMETER;
464 return EFI_INVALID_PARAMETER;
465 len = (u64)pages << EFI_PAGE_SHIFT;
466 /* Catch possible overflow on 64bit systems */
467 if (sizeof(efi_uintn_t) == sizeof(u64) &&
468 (len >> EFI_PAGE_SHIFT) != (u64)pages)
469 return EFI_OUT_OF_RESOURCES;
471 flags = LMB_NOOVERWRITE | LMB_NONOTIFY;
473 case EFI_ALLOCATE_ANY_PAGES:
475 addr = (u64)lmb_alloc_base(len, EFI_PAGE_SIZE,
476 LMB_ALLOC_ANYWHERE, flags);
478 return EFI_OUT_OF_RESOURCES;
480 case EFI_ALLOCATE_MAX_ADDRESS:
482 addr = map_to_sysmem((void *)(uintptr_t)*memory);
483 addr = (u64)lmb_alloc_base(len, EFI_PAGE_SIZE, addr,
486 return EFI_OUT_OF_RESOURCES;
488 case EFI_ALLOCATE_ADDRESS:
489 if (*memory & EFI_PAGE_MASK)
490 return EFI_NOT_FOUND;
492 addr = map_to_sysmem((void *)(uintptr_t)*memory);
493 addr = (u64)lmb_alloc_addr(addr, len, flags);
495 return EFI_NOT_FOUND;
498 /* UEFI doesn't specify other allocation types */
499 return EFI_INVALID_PARAMETER;
502 efi_addr = (u64)(uintptr_t)map_sysmem(addr, 0);
503 /* Reserve that map in our memory maps */
504 ret = efi_add_memory_map_pg(efi_addr, pages, memory_type, true);
505 if (ret != EFI_SUCCESS) {
506 /* Map would overlap, bail out */
507 lmb_free_flags(addr, (u64)pages << EFI_PAGE_SHIFT, flags);
508 unmap_sysmem((void *)(uintptr_t)efi_addr);
509 return EFI_OUT_OF_RESOURCES;
518 * efi_free_pages() - free memory pages
520 * @memory: start of the memory area to be freed
521 * @pages: number of pages to be freed
522 * Return: status code
524 efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages)
530 ret = efi_check_allocated(memory, true);
531 if (ret != EFI_SUCCESS)
535 if (!memory || (memory & EFI_PAGE_MASK) || !pages) {
536 printf("%s: illegal free 0x%llx, 0x%zx\n", __func__,
538 return EFI_INVALID_PARAMETER;
541 len = (u64)pages << EFI_PAGE_SHIFT;
543 * The 'memory' variable for sandbox holds a pointer which has already
544 * been mapped with map_sysmem() from efi_allocate_pages(). Convert
545 * it back to an address LMB understands
547 status = lmb_free_flags(map_to_sysmem((void *)(uintptr_t)memory), len,
550 return EFI_NOT_FOUND;
552 unmap_sysmem((void *)(uintptr_t)memory);
558 * efi_alloc_aligned_pages() - allocate aligned memory pages
561 * @memory_type: usage type of the allocated memory
562 * @align: alignment in bytes
563 * Return: aligned memory or NULL
565 void *efi_alloc_aligned_pages(u64 len, int memory_type, size_t align)
567 u64 req_pages = efi_size_in_pages(len);
568 u64 true_pages = req_pages + efi_size_in_pages(align) - 1;
574 /* align must be zero or a power of two */
575 if (align & (align - 1))
578 /* Check for overflow */
579 if (true_pages < req_pages)
582 if (align < EFI_PAGE_SIZE) {
583 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type,
585 return (r == EFI_SUCCESS) ? (void *)(uintptr_t)mem : NULL;
588 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type,
590 if (r != EFI_SUCCESS)
593 aligned_mem = ALIGN(mem, align);
594 /* Free pages before alignment */
595 free_pages = efi_size_in_pages(aligned_mem - mem);
597 efi_free_pages(mem, free_pages);
599 /* Free trailing pages */
600 free_pages = true_pages - (req_pages + free_pages);
602 mem = aligned_mem + req_pages * EFI_PAGE_SIZE;
603 efi_free_pages(mem, free_pages);
606 return (void *)(uintptr_t)aligned_mem;
610 * efi_allocate_pool - allocate memory from pool
612 * @pool_type: type of the pool from which memory is to be allocated
613 * @size: number of bytes to be allocated
614 * @buffer: allocated memory
615 * Return: status code
617 efi_status_t efi_allocate_pool(enum efi_memory_type pool_type, efi_uintn_t size, void **buffer)
621 struct efi_pool_allocation *alloc;
622 u64 num_pages = efi_size_in_pages(size +
623 sizeof(struct efi_pool_allocation));
626 return EFI_INVALID_PARAMETER;
633 r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, pool_type, num_pages,
635 if (r == EFI_SUCCESS) {
636 alloc = (struct efi_pool_allocation *)(uintptr_t)addr;
637 alloc->num_pages = num_pages;
638 alloc->checksum = checksum(alloc);
639 *buffer = alloc->data;
646 * efi_alloc() - allocate boot services data pool memory
648 * Allocate memory from pool and zero it out.
650 * @size: number of bytes to allocate
651 * Return: pointer to allocated memory or NULL
653 void *efi_alloc(size_t size)
657 if (efi_allocate_pool(EFI_BOOT_SERVICES_DATA, size, &buf) !=
659 log_err("out of memory\n");
662 memset(buf, 0, size);
668 * efi_free_pool() - free memory from pool
670 * @buffer: start of memory to be freed
671 * Return: status code
673 efi_status_t efi_free_pool(void *buffer)
676 struct efi_pool_allocation *alloc;
679 return EFI_INVALID_PARAMETER;
681 ret = efi_check_allocated((uintptr_t)buffer, true);
682 if (ret != EFI_SUCCESS)
685 alloc = container_of(buffer, struct efi_pool_allocation, data);
687 /* Check that this memory was allocated by efi_allocate_pool() */
688 if (((uintptr_t)alloc & EFI_PAGE_MASK) ||
689 alloc->checksum != checksum(alloc)) {
690 printf("%s: illegal free 0x%p\n", __func__, buffer);
691 return EFI_INVALID_PARAMETER;
693 /* Avoid double free */
696 ret = efi_free_pages((uintptr_t)alloc, alloc->num_pages);
702 * efi_get_memory_map() - get map describing memory usage.
704 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
705 * on exit the size of the copied memory map
706 * @memory_map: buffer to which the memory map is written
707 * @map_key: key for the memory map
708 * @descriptor_size: size of an individual memory descriptor
709 * @descriptor_version: version number of the memory descriptor structure
710 * Return: status code
712 efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size,
713 struct efi_mem_desc *memory_map,
714 efi_uintn_t *map_key,
715 efi_uintn_t *descriptor_size,
716 uint32_t *descriptor_version)
719 efi_uintn_t map_size = 0;
720 struct efi_mem_list *lmem;
721 efi_uintn_t provided_map_size;
723 if (!memory_map_size)
724 return EFI_INVALID_PARAMETER;
726 provided_map_size = *memory_map_size;
728 map_entries = list_count_nodes(&efi_mem);
730 map_size = map_entries * sizeof(struct efi_mem_desc);
732 *memory_map_size = map_size;
735 *descriptor_size = sizeof(struct efi_mem_desc);
737 if (descriptor_version)
738 *descriptor_version = EFI_MEMORY_DESCRIPTOR_VERSION;
740 if (provided_map_size < map_size)
741 return EFI_BUFFER_TOO_SMALL;
744 return EFI_INVALID_PARAMETER;
746 /* Copy list into array */
747 /* Return the list in ascending order */
748 memory_map = &memory_map[map_entries - 1];
749 list_for_each_entry(lmem, &efi_mem, link) {
750 *memory_map = lmem->desc;
755 *map_key = efi_memory_map_key;
761 * efi_get_memory_map_alloc() - allocate map describing memory usage
763 * The caller is responsible for calling FreePool() if the call succeeds.
765 * @map_size: size of the memory map
766 * @memory_map: buffer to which the memory map is written
767 * Return: status code
769 efi_status_t efi_get_memory_map_alloc(efi_uintn_t *map_size,
770 struct efi_mem_desc **memory_map)
776 ret = efi_get_memory_map(map_size, *memory_map, NULL, NULL, NULL);
777 if (ret == EFI_BUFFER_TOO_SMALL) {
778 *map_size += sizeof(struct efi_mem_desc); /* for the map */
779 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, *map_size,
780 (void **)memory_map);
781 if (ret != EFI_SUCCESS)
783 ret = efi_get_memory_map(map_size, *memory_map,
785 if (ret != EFI_SUCCESS) {
786 efi_free_pool(*memory_map);
795 * efi_add_known_memory() - add memory types to the EFI memory map
797 * This function is to be used to add different memory types other
798 * than EFI_CONVENTIONAL_MEMORY to the EFI memory map. The conventional
799 * memory is handled by the LMB module and gets added to the memory
800 * map through the LMB module.
802 * This function may be overridden for architectures specific purposes.
804 __weak void efi_add_known_memory(void)
809 * add_u_boot_and_runtime() - add U-Boot code to memory map
811 * Add memory regions for U-Boot's memory and for the runtime services code.
813 static void add_u_boot_and_runtime(void)
815 unsigned long runtime_start, runtime_end, runtime_pages;
816 unsigned long runtime_mask = EFI_PAGE_MASK;
817 unsigned long uboot_start, uboot_pages;
818 unsigned long uboot_stack_size = CONFIG_STACK_SIZE;
821 uboot_start = ((uintptr_t)map_sysmem(gd->start_addr_sp, 0) -
822 uboot_stack_size) & ~EFI_PAGE_MASK;
823 uboot_pages = ((uintptr_t)map_sysmem(gd->ram_top - 1, 0) -
824 uboot_start + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
825 efi_add_memory_map_pg(uboot_start, uboot_pages, EFI_BOOT_SERVICES_CODE,
827 #if defined(__aarch64__)
829 * Runtime Services must be 64KiB aligned according to the
830 * "AArch64 Platforms" section in the UEFI spec (2.7+).
833 runtime_mask = SZ_64K - 1;
837 * Add Runtime Services. We mark surrounding boottime code as runtime as
838 * well to fulfill the runtime alignment constraints but avoid padding.
840 runtime_start = (uintptr_t)__efi_runtime_start & ~runtime_mask;
841 runtime_end = (uintptr_t)__efi_runtime_stop;
842 runtime_end = (runtime_end + runtime_mask) & ~runtime_mask;
843 runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT;
844 efi_add_memory_map_pg(runtime_start, runtime_pages,
845 EFI_RUNTIME_SERVICES_CODE, false);
848 int efi_memory_init(void)
850 efi_add_known_memory();
852 add_u_boot_and_runtime();
854 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
855 /* Request a 32bit 64MB bounce buffer region */
856 uint64_t efi_bounce_buffer_addr = 0xffffffff;
858 if (efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS, EFI_BOOT_SERVICES_DATA,
859 (64 * 1024 * 1024) >> EFI_PAGE_SHIFT,
860 &efi_bounce_buffer_addr) != EFI_SUCCESS)
863 efi_bounce_buffer = (void*)(uintptr_t)efi_bounce_buffer_addr;