Tom Rini | 70df9d6 | 2018-05-07 17:02:21 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 2 | /* |
| 3 | * EFI application memory management |
| 4 | * |
| 5 | * Copyright (c) 2016 Alexander Graf |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
Heinrich Schuchardt | 4564025 | 2023-03-19 09:20:22 +0100 | [diff] [blame] | 8 | #define LOG_CATEGORY LOGC_EFI |
| 9 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 10 | #include <efi_loader.h> |
Simon Glass | 6980b6b | 2019-11-14 12:57:45 -0700 | [diff] [blame] | 11 | #include <init.h> |
Sughosh Ganu | fa0ddab | 2024-10-15 21:07:06 +0530 | [diff] [blame] | 12 | #include <lmb.h> |
Heinrich Schuchardt | 4564025 | 2023-03-19 09:20:22 +0100 | [diff] [blame] | 13 | #include <log.h> |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 14 | #include <malloc.h> |
Alexander Graf | cd40517 | 2018-06-18 17:23:15 +0200 | [diff] [blame] | 15 | #include <mapmem.h> |
Simon Glass | 3e85217 | 2018-03-08 21:53:27 +0100 | [diff] [blame] | 16 | #include <watchdog.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 17 | #include <asm/cache.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 18 | #include <asm/global_data.h> |
Heinrich Schuchardt | 211ff08 | 2024-04-05 10:12:57 +0200 | [diff] [blame] | 19 | #include <asm/sections.h> |
Alexander Graf | de2a13b | 2016-03-30 16:38:29 +0200 | [diff] [blame] | 20 | #include <linux/list_sort.h> |
Alexander Graf | 15937a2 | 2018-09-17 13:54:33 +0200 | [diff] [blame] | 21 | #include <linux/sizes.h> |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 22 | |
| 23 | DECLARE_GLOBAL_DATA_PTR; |
| 24 | |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 25 | /* Magic number identifying memory allocated from pool */ |
| 26 | #define EFI_ALLOC_POOL_MAGIC 0x1fe67ddf6491caa2 |
| 27 | |
Heinrich Schuchardt | 0f233c4 | 2018-07-02 12:53:55 +0200 | [diff] [blame] | 28 | efi_uintn_t efi_memory_map_key; |
| 29 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 30 | struct efi_mem_list { |
| 31 | struct list_head link; |
| 32 | struct efi_mem_desc desc; |
| 33 | }; |
| 34 | |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 35 | #define EFI_CARVE_NO_OVERLAP -1 |
| 36 | #define EFI_CARVE_LOOP_AGAIN -2 |
| 37 | #define EFI_CARVE_OVERLAPS_NONRAM -3 |
Heinrich Schuchardt | 17fac0e | 2023-07-30 12:27:03 +0200 | [diff] [blame] | 38 | #define EFI_CARVE_OUT_OF_RESOURCES -4 |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 39 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 40 | /* This list contains all memory map items */ |
Bin Meng | c8025a3 | 2023-04-05 20:15:18 +0800 | [diff] [blame] | 41 | static LIST_HEAD(efi_mem); |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 42 | |
Alexander Graf | 7c00a3c | 2016-05-11 18:25:48 +0200 | [diff] [blame] | 43 | #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER |
| 44 | void *efi_bounce_buffer; |
| 45 | #endif |
| 46 | |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 47 | /** |
Heinrich Schuchardt | 79f882a | 2019-07-14 12:28:56 +0200 | [diff] [blame] | 48 | * struct efi_pool_allocation - memory block allocated from pool |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 49 | * |
| 50 | * @num_pages: number of pages allocated |
| 51 | * @checksum: checksum |
Heinrich Schuchardt | 79f882a | 2019-07-14 12:28:56 +0200 | [diff] [blame] | 52 | * @data: allocated pool memory |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 53 | * |
Heinrich Schuchardt | 79f882a | 2019-07-14 12:28:56 +0200 | [diff] [blame] | 54 | * U-Boot services each UEFI AllocatePool() request as a separate |
| 55 | * (multiple) page allocation. We have to track the number of pages |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 56 | * to be able to free the correct amount later. |
Heinrich Schuchardt | 79f882a | 2019-07-14 12:28:56 +0200 | [diff] [blame] | 57 | * |
| 58 | * The checksum calculated in function checksum() is used in FreePool() to avoid |
| 59 | * freeing memory not allocated by AllocatePool() and duplicate freeing. |
| 60 | * |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 61 | * EFI requires 8 byte alignment for pool allocations, so we can |
Heinrich Schuchardt | 79f882a | 2019-07-14 12:28:56 +0200 | [diff] [blame] | 62 | * prepend each allocation with these header fields. |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 63 | */ |
| 64 | struct efi_pool_allocation { |
| 65 | u64 num_pages; |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 66 | u64 checksum; |
Rob Clark | e597fd4 | 2017-09-13 18:05:36 -0400 | [diff] [blame] | 67 | char data[] __aligned(ARCH_DMA_MINALIGN); |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 68 | }; |
| 69 | |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 70 | /** |
| 71 | * checksum() - calculate checksum for memory allocated from pool |
| 72 | * |
| 73 | * @alloc: allocation header |
| 74 | * Return: checksum, always non-zero |
| 75 | */ |
| 76 | static u64 checksum(struct efi_pool_allocation *alloc) |
| 77 | { |
| 78 | u64 addr = (uintptr_t)alloc; |
| 79 | u64 ret = (addr >> 32) ^ (addr << 32) ^ alloc->num_pages ^ |
| 80 | EFI_ALLOC_POOL_MAGIC; |
| 81 | if (!ret) |
| 82 | ++ret; |
| 83 | return ret; |
| 84 | } |
| 85 | |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 86 | /** |
| 87 | * efi_mem_cmp() - comparator function for sorting memory map |
| 88 | * |
Alexander Graf | de2a13b | 2016-03-30 16:38:29 +0200 | [diff] [blame] | 89 | * Sorts the memory list from highest address to lowest address |
| 90 | * |
| 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. |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 94 | * |
| 95 | * @priv: unused |
| 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 |
Alexander Graf | de2a13b | 2016-03-30 16:38:29 +0200 | [diff] [blame] | 99 | */ |
| 100 | static int efi_mem_cmp(void *priv, struct list_head *a, struct list_head *b) |
| 101 | { |
| 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); |
| 104 | |
| 105 | if (mema->desc.physical_start == memb->desc.physical_start) |
| 106 | return 0; |
| 107 | else if (mema->desc.physical_start < memb->desc.physical_start) |
| 108 | return 1; |
| 109 | else |
| 110 | return -1; |
| 111 | } |
| 112 | |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 113 | /** |
| 114 | * desc_get_end() - get end address of memory area |
| 115 | * |
| 116 | * @desc: memory descriptor |
| 117 | * Return: end address + 1 |
| 118 | */ |
Alexander Graf | d728814 | 2018-09-16 17:05:29 +0200 | [diff] [blame] | 119 | static uint64_t desc_get_end(struct efi_mem_desc *desc) |
| 120 | { |
| 121 | return desc->physical_start + (desc->num_pages << EFI_PAGE_SHIFT); |
| 122 | } |
| 123 | |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 124 | /** |
| 125 | * efi_mem_sort() - sort memory map |
| 126 | * |
| 127 | * Sort the memory map and then try to merge adjacent memory areas. |
| 128 | */ |
Alexander Graf | de2a13b | 2016-03-30 16:38:29 +0200 | [diff] [blame] | 129 | static void efi_mem_sort(void) |
| 130 | { |
Sughosh Ganu | 21ad4cc | 2024-07-30 16:41:31 +0530 | [diff] [blame] | 131 | struct efi_mem_list *lmem; |
Alexander Graf | d728814 | 2018-09-16 17:05:29 +0200 | [diff] [blame] | 132 | struct efi_mem_list *prevmem = NULL; |
| 133 | bool merge_again = true; |
| 134 | |
Alexander Graf | de2a13b | 2016-03-30 16:38:29 +0200 | [diff] [blame] | 135 | list_sort(NULL, &efi_mem, efi_mem_cmp); |
Alexander Graf | d728814 | 2018-09-16 17:05:29 +0200 | [diff] [blame] | 136 | |
| 137 | /* Now merge entries that can be merged */ |
| 138 | while (merge_again) { |
| 139 | merge_again = false; |
Sughosh Ganu | 21ad4cc | 2024-07-30 16:41:31 +0530 | [diff] [blame] | 140 | list_for_each_entry(lmem, &efi_mem, link) { |
Sughosh Ganu | 7d2c279 | 2024-07-30 16:41:30 +0530 | [diff] [blame] | 141 | struct efi_mem_desc *prev; |
Alexander Graf | d728814 | 2018-09-16 17:05:29 +0200 | [diff] [blame] | 142 | struct efi_mem_desc *cur; |
| 143 | uint64_t pages; |
| 144 | |
Alexander Graf | d728814 | 2018-09-16 17:05:29 +0200 | [diff] [blame] | 145 | if (!prevmem) { |
| 146 | prevmem = lmem; |
| 147 | continue; |
| 148 | } |
| 149 | |
| 150 | cur = &lmem->desc; |
Sughosh Ganu | 7d2c279 | 2024-07-30 16:41:30 +0530 | [diff] [blame] | 151 | prev = &prevmem->desc; |
Alexander Graf | d728814 | 2018-09-16 17:05:29 +0200 | [diff] [blame] | 152 | |
| 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); |
| 162 | free(lmem); |
| 163 | |
| 164 | merge_again = true; |
| 165 | break; |
| 166 | } |
| 167 | |
| 168 | prevmem = lmem; |
| 169 | } |
| 170 | } |
Alexander Graf | de2a13b | 2016-03-30 16:38:29 +0200 | [diff] [blame] | 171 | } |
| 172 | |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 173 | /** |
| 174 | * efi_mem_carve_out() - unmap memory region |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 175 | * |
Sughosh Ganu | c9e6eb6 | 2024-10-15 21:07:16 +0530 | [diff] [blame] | 176 | * @map: memory map |
| 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 |
| 186 | * true), |
| 187 | * EFI_CARVE_LOOP_AGAIN, if the mapping list should |
| 188 | * be traversed again, as it has been altered. |
Heinrich Schuchardt | f00caa5 | 2018-05-27 16:45:09 +0200 | [diff] [blame] | 189 | * |
| 190 | * Unmaps all memory occupied by the carve_desc region from the list entry |
| 191 | * pointed to by map. |
Stefan Brüns | 1b00dc1 | 2016-10-01 23:32:23 +0200 | [diff] [blame] | 192 | * |
| 193 | * In case of EFI_CARVE_OVERLAPS_NONRAM it is the callers responsibility |
Heinrich Schuchardt | f00caa5 | 2018-05-27 16:45:09 +0200 | [diff] [blame] | 194 | * to re-add the already carved out pages to the mapping. |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 195 | */ |
Heinrich Schuchardt | f00caa5 | 2018-05-27 16:45:09 +0200 | [diff] [blame] | 196 | static s64 efi_mem_carve_out(struct efi_mem_list *map, |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 197 | struct efi_mem_desc *carve_desc, |
Sughosh Ganu | c9e6eb6 | 2024-10-15 21:07:16 +0530 | [diff] [blame] | 198 | bool overlap_conventional) |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 199 | { |
| 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); |
| 207 | |
| 208 | /* check whether we're overlapping */ |
| 209 | if ((carve_end <= map_start) || (carve_start >= map_end)) |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 210 | return EFI_CARVE_NO_OVERLAP; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 211 | |
| 212 | /* We're overlapping with non-RAM, warn the caller if desired */ |
Sughosh Ganu | c9e6eb6 | 2024-10-15 21:07:16 +0530 | [diff] [blame] | 213 | if (overlap_conventional && (map_desc->type != EFI_CONVENTIONAL_MEMORY)) |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 214 | return EFI_CARVE_OVERLAPS_NONRAM; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 215 | |
| 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); |
| 219 | |
| 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); |
Stefan Brüns | 4e9ad8d | 2016-10-01 23:32:29 +0200 | [diff] [blame] | 225 | free(map); |
| 226 | } else { |
| 227 | map->desc.physical_start = carve_end; |
Heinrich Schuchardt | e83aafa | 2019-04-11 20:08:54 +0200 | [diff] [blame] | 228 | map->desc.virtual_start = carve_end; |
Stefan Brüns | 4e9ad8d | 2016-10-01 23:32:29 +0200 | [diff] [blame] | 229 | map->desc.num_pages = (map_end - carve_end) |
| 230 | >> EFI_PAGE_SHIFT; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 231 | } |
| 232 | |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 233 | return (carve_end - carve_start) >> EFI_PAGE_SHIFT; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | /* |
| 237 | * Overlapping maps, just split the list map at carve_start, |
| 238 | * it will get moved or removed in the next iteration. |
| 239 | * |
| 240 | * [ map_desc |__carve_start__| newmap ] |
| 241 | */ |
| 242 | |
| 243 | /* Create a new map from [ carve_start ... map_end ] */ |
| 244 | newmap = calloc(1, sizeof(*newmap)); |
Heinrich Schuchardt | 17fac0e | 2023-07-30 12:27:03 +0200 | [diff] [blame] | 245 | if (!newmap) |
| 246 | return EFI_CARVE_OUT_OF_RESOURCES; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 247 | newmap->desc = map->desc; |
| 248 | newmap->desc.physical_start = carve_start; |
Heinrich Schuchardt | e83aafa | 2019-04-11 20:08:54 +0200 | [diff] [blame] | 249 | newmap->desc.virtual_start = carve_start; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 250 | newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT; |
Stefan Brüns | 9d65651 | 2016-10-01 23:32:28 +0200 | [diff] [blame] | 251 | /* Insert before current entry (descending address order) */ |
| 252 | list_add_tail(&newmap->link, &map->link); |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 253 | |
| 254 | /* Shrink the map to [ map_start ... carve_start ] */ |
| 255 | map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT; |
| 256 | |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 257 | return EFI_CARVE_LOOP_AGAIN; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 258 | } |
| 259 | |
Bryan O'Donoghue | 89a22b4 | 2019-07-15 12:00:39 +0100 | [diff] [blame] | 260 | /** |
Michael Walle | 282d386 | 2020-05-17 12:29:19 +0200 | [diff] [blame] | 261 | * efi_add_memory_map_pg() - add pages to the memory map |
Bryan O'Donoghue | 89a22b4 | 2019-07-15 12:00:39 +0100 | [diff] [blame] | 262 | * |
Sughosh Ganu | c9e6eb6 | 2024-10-15 21:07:16 +0530 | [diff] [blame] | 263 | * @start: start address, must be a multiple of |
| 264 | * EFI_PAGE_SIZE |
| 265 | * @pages: number of pages to add |
| 266 | * @memory_type: type of memory added |
| 267 | * @overlap_conventional: region may only overlap free(conventional) |
| 268 | * memory |
| 269 | * Return: status code |
Bryan O'Donoghue | 89a22b4 | 2019-07-15 12:00:39 +0100 | [diff] [blame] | 270 | */ |
Sughosh Ganu | a3af5ba | 2024-10-15 21:07:07 +0530 | [diff] [blame] | 271 | efi_status_t efi_add_memory_map_pg(u64 start, u64 pages, |
Sughosh Ganu | c9e6eb6 | 2024-10-15 21:07:16 +0530 | [diff] [blame] | 272 | int memory_type, |
| 273 | bool overlap_conventional) |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 274 | { |
Sughosh Ganu | 21ad4cc | 2024-07-30 16:41:31 +0530 | [diff] [blame] | 275 | struct efi_mem_list *lmem; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 276 | struct efi_mem_list *newlist; |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 277 | bool carve_again; |
| 278 | uint64_t carved_pages = 0; |
Heinrich Schuchardt | 485975b | 2019-06-04 20:55:12 +0200 | [diff] [blame] | 279 | struct efi_event *evt; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 280 | |
Heinrich Schuchardt | 706a2fe | 2019-04-04 21:50:02 +0200 | [diff] [blame] | 281 | EFI_PRINT("%s: 0x%llx 0x%llx %d %s\n", __func__, |
Sughosh Ganu | c9e6eb6 | 2024-10-15 21:07:16 +0530 | [diff] [blame] | 282 | start, pages, memory_type, overlap_conventional ? |
| 283 | "yes" : "no"); |
Andreas Färber | 102f8bd | 2016-07-17 06:57:11 +0200 | [diff] [blame] | 284 | |
Heinrich Schuchardt | 0f233c4 | 2018-07-02 12:53:55 +0200 | [diff] [blame] | 285 | if (memory_type >= EFI_MAX_MEMORY_TYPE) |
| 286 | return EFI_INVALID_PARAMETER; |
| 287 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 288 | if (!pages) |
Bryan O'Donoghue | 89a22b4 | 2019-07-15 12:00:39 +0100 | [diff] [blame] | 289 | return EFI_SUCCESS; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 290 | |
Heinrich Schuchardt | 0f233c4 | 2018-07-02 12:53:55 +0200 | [diff] [blame] | 291 | ++efi_memory_map_key; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 292 | newlist = calloc(1, sizeof(*newlist)); |
Heinrich Schuchardt | 71c2444 | 2023-07-30 12:36:17 +0200 | [diff] [blame] | 293 | if (!newlist) |
| 294 | return EFI_OUT_OF_RESOURCES; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 295 | newlist->desc.type = memory_type; |
| 296 | newlist->desc.physical_start = start; |
| 297 | newlist->desc.virtual_start = start; |
| 298 | newlist->desc.num_pages = pages; |
| 299 | |
| 300 | switch (memory_type) { |
| 301 | case EFI_RUNTIME_SERVICES_CODE: |
| 302 | case EFI_RUNTIME_SERVICES_DATA: |
Eugeniu Rosca | c86c081 | 2018-07-14 22:53:30 +0200 | [diff] [blame] | 303 | newlist->desc.attribute = EFI_MEMORY_WB | EFI_MEMORY_RUNTIME; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 304 | break; |
| 305 | case EFI_MMAP_IO: |
Eugeniu Rosca | c86c081 | 2018-07-14 22:53:30 +0200 | [diff] [blame] | 306 | newlist->desc.attribute = EFI_MEMORY_RUNTIME; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 307 | break; |
| 308 | default: |
Eugeniu Rosca | c86c081 | 2018-07-14 22:53:30 +0200 | [diff] [blame] | 309 | newlist->desc.attribute = EFI_MEMORY_WB; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 310 | break; |
| 311 | } |
| 312 | |
| 313 | /* Add our new map */ |
| 314 | do { |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 315 | carve_again = false; |
Sughosh Ganu | 21ad4cc | 2024-07-30 16:41:31 +0530 | [diff] [blame] | 316 | list_for_each_entry(lmem, &efi_mem, link) { |
Heinrich Schuchardt | f00caa5 | 2018-05-27 16:45:09 +0200 | [diff] [blame] | 317 | s64 r; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 318 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 319 | r = efi_mem_carve_out(lmem, &newlist->desc, |
Sughosh Ganu | c9e6eb6 | 2024-10-15 21:07:16 +0530 | [diff] [blame] | 320 | overlap_conventional); |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 321 | switch (r) { |
Heinrich Schuchardt | 17fac0e | 2023-07-30 12:27:03 +0200 | [diff] [blame] | 322 | case EFI_CARVE_OUT_OF_RESOURCES: |
| 323 | free(newlist); |
| 324 | return EFI_OUT_OF_RESOURCES; |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 325 | case EFI_CARVE_OVERLAPS_NONRAM: |
| 326 | /* |
| 327 | * The user requested to only have RAM overlaps, |
| 328 | * but we hit a non-RAM region. Error out. |
| 329 | */ |
Heinrich Schuchardt | 9f0d3ad | 2023-07-30 12:59:32 +0200 | [diff] [blame] | 330 | free(newlist); |
Bryan O'Donoghue | 89a22b4 | 2019-07-15 12:00:39 +0100 | [diff] [blame] | 331 | return EFI_NO_MAPPING; |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 332 | case EFI_CARVE_NO_OVERLAP: |
| 333 | /* Just ignore this list entry */ |
| 334 | break; |
| 335 | case EFI_CARVE_LOOP_AGAIN: |
| 336 | /* |
| 337 | * We split an entry, but need to loop through |
| 338 | * the list again to actually carve it. |
| 339 | */ |
| 340 | carve_again = true; |
| 341 | break; |
| 342 | default: |
| 343 | /* We carved a number of pages */ |
| 344 | carved_pages += r; |
| 345 | carve_again = true; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 346 | break; |
| 347 | } |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 348 | |
| 349 | if (carve_again) { |
| 350 | /* The list changed, we need to start over */ |
| 351 | break; |
| 352 | } |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 353 | } |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 354 | } while (carve_again); |
| 355 | |
Sughosh Ganu | c9e6eb6 | 2024-10-15 21:07:16 +0530 | [diff] [blame] | 356 | if (overlap_conventional && (carved_pages != pages)) { |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 357 | /* |
| 358 | * The payload wanted to have RAM overlaps, but we overlapped |
| 359 | * with an unallocated region. Error out. |
| 360 | */ |
Heinrich Schuchardt | 9f0d3ad | 2023-07-30 12:59:32 +0200 | [diff] [blame] | 361 | free(newlist); |
Bryan O'Donoghue | 89a22b4 | 2019-07-15 12:00:39 +0100 | [diff] [blame] | 362 | return EFI_NO_MAPPING; |
Alexander Graf | 66d8623 | 2016-05-27 12:25:03 +0200 | [diff] [blame] | 363 | } |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 364 | |
| 365 | /* Add our new map */ |
| 366 | list_add_tail(&newlist->link, &efi_mem); |
| 367 | |
Alexander Graf | de2a13b | 2016-03-30 16:38:29 +0200 | [diff] [blame] | 368 | /* And make sure memory is listed in descending order */ |
| 369 | efi_mem_sort(); |
| 370 | |
Heinrich Schuchardt | 485975b | 2019-06-04 20:55:12 +0200 | [diff] [blame] | 371 | /* Notify that the memory map was changed */ |
| 372 | list_for_each_entry(evt, &efi_events, link) { |
| 373 | if (evt->group && |
| 374 | !guidcmp(evt->group, |
| 375 | &efi_guid_event_group_memory_map_change)) { |
Heinrich Schuchardt | 7b4b8d86 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 376 | efi_signal_event(evt); |
Heinrich Schuchardt | 485975b | 2019-06-04 20:55:12 +0200 | [diff] [blame] | 377 | break; |
| 378 | } |
| 379 | } |
| 380 | |
Bryan O'Donoghue | 89a22b4 | 2019-07-15 12:00:39 +0100 | [diff] [blame] | 381 | return EFI_SUCCESS; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 382 | } |
| 383 | |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 384 | /** |
Michael Walle | 282d386 | 2020-05-17 12:29:19 +0200 | [diff] [blame] | 385 | * efi_add_memory_map() - add memory area to the memory map |
| 386 | * |
| 387 | * @start: start address of the memory area |
| 388 | * @size: length in bytes of the memory area |
| 389 | * @memory_type: type of memory added |
| 390 | * |
| 391 | * Return: status code |
| 392 | * |
| 393 | * This function automatically aligns the start and size of the memory area |
| 394 | * to EFI_PAGE_SIZE. |
| 395 | */ |
| 396 | efi_status_t efi_add_memory_map(u64 start, u64 size, int memory_type) |
| 397 | { |
| 398 | u64 pages; |
| 399 | |
| 400 | pages = efi_size_in_pages(size + (start & EFI_PAGE_MASK)); |
| 401 | start &= ~EFI_PAGE_MASK; |
| 402 | |
| 403 | return efi_add_memory_map_pg(start, pages, memory_type, false); |
| 404 | } |
| 405 | |
| 406 | /** |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 407 | * efi_check_allocated() - validate address to be freed |
| 408 | * |
| 409 | * Check that the address is within allocated memory: |
| 410 | * |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 411 | * * The address must be in a range of the memory map. |
| 412 | * * The address may not point to EFI_CONVENTIONAL_MEMORY. |
| 413 | * |
| 414 | * Page alignment is not checked as this is not a requirement of |
| 415 | * efi_free_pool(). |
| 416 | * |
Heinrich Schuchardt | 906f685 | 2019-05-11 08:21:17 +0200 | [diff] [blame] | 417 | * @addr: address of page to be freed |
| 418 | * @must_be_allocated: return success if the page is allocated |
| 419 | * Return: status code |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 420 | */ |
Heinrich Schuchardt | 906f685 | 2019-05-11 08:21:17 +0200 | [diff] [blame] | 421 | static efi_status_t efi_check_allocated(u64 addr, bool must_be_allocated) |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 422 | { |
| 423 | struct efi_mem_list *item; |
| 424 | |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 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); |
| 428 | |
| 429 | if (addr >= start && addr < end) { |
Heinrich Schuchardt | 906f685 | 2019-05-11 08:21:17 +0200 | [diff] [blame] | 430 | if (must_be_allocated ^ |
| 431 | (item->desc.type == EFI_CONVENTIONAL_MEMORY)) |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 432 | return EFI_SUCCESS; |
| 433 | else |
| 434 | return EFI_NOT_FOUND; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | return EFI_NOT_FOUND; |
| 439 | } |
| 440 | |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 441 | /** |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 442 | * efi_allocate_pages - allocate memory pages |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 443 | * |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 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 |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 448 | * Return: status code |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 449 | */ |
Heinrich Schuchardt | 9822894 | 2021-08-17 15:05:31 +0200 | [diff] [blame] | 450 | efi_status_t efi_allocate_pages(enum efi_allocate_type type, |
| 451 | enum efi_memory_type memory_type, |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 452 | efi_uintn_t pages, uint64_t *memory) |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 453 | { |
Heinrich Schuchardt | f0c16f9 | 2023-07-30 11:31:08 +0200 | [diff] [blame] | 454 | u64 len; |
Sughosh Ganu | fa0ddab | 2024-10-15 21:07:06 +0530 | [diff] [blame] | 455 | uint flags; |
Heinrich Schuchardt | fbdb93c | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 456 | efi_status_t ret; |
Sughosh Ganu | fa0ddab | 2024-10-15 21:07:06 +0530 | [diff] [blame] | 457 | phys_addr_t addr; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 458 | |
Heinrich Schuchardt | f727233 | 2019-04-23 00:30:53 +0200 | [diff] [blame] | 459 | /* Check import parameters */ |
| 460 | if (memory_type >= EFI_PERSISTENT_MEMORY_TYPE && |
| 461 | memory_type <= 0x6FFFFFFF) |
| 462 | return EFI_INVALID_PARAMETER; |
Heinrich Schuchardt | a137c96 | 2018-07-02 12:53:53 +0200 | [diff] [blame] | 463 | if (!memory) |
| 464 | return EFI_INVALID_PARAMETER; |
Heinrich Schuchardt | f0c16f9 | 2023-07-30 11:31:08 +0200 | [diff] [blame] | 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; |
Heinrich Schuchardt | a137c96 | 2018-07-02 12:53:53 +0200 | [diff] [blame] | 470 | |
Sughosh Ganu | fa0ddab | 2024-10-15 21:07:06 +0530 | [diff] [blame] | 471 | flags = LMB_NOOVERWRITE | LMB_NONOTIFY; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 472 | switch (type) { |
Heinrich Schuchardt | 3b7b3b6 | 2018-01-30 21:08:00 +0100 | [diff] [blame] | 473 | case EFI_ALLOCATE_ANY_PAGES: |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 474 | /* Any page */ |
Ilias Apalodimas | 7d182fb | 2024-10-23 18:26:36 +0300 | [diff] [blame] | 475 | addr = (u64)lmb_alloc_base_flags(len, EFI_PAGE_SIZE, |
| 476 | LMB_ALLOC_ANYWHERE, flags); |
Heinrich Schuchardt | fbdb93c | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 477 | if (!addr) |
| 478 | return EFI_OUT_OF_RESOURCES; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 479 | break; |
Heinrich Schuchardt | 3b7b3b6 | 2018-01-30 21:08:00 +0100 | [diff] [blame] | 480 | case EFI_ALLOCATE_MAX_ADDRESS: |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 481 | /* Max address */ |
Sughosh Ganu | fa0ddab | 2024-10-15 21:07:06 +0530 | [diff] [blame] | 482 | addr = map_to_sysmem((void *)(uintptr_t)*memory); |
| 483 | addr = (u64)lmb_alloc_base_flags(len, EFI_PAGE_SIZE, addr, |
| 484 | flags); |
Heinrich Schuchardt | fbdb93c | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 485 | if (!addr) |
| 486 | return EFI_OUT_OF_RESOURCES; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 487 | break; |
Heinrich Schuchardt | 3b7b3b6 | 2018-01-30 21:08:00 +0100 | [diff] [blame] | 488 | case EFI_ALLOCATE_ADDRESS: |
Heinrich Schuchardt | 1c180b8 | 2022-11-06 01:52:13 +0100 | [diff] [blame] | 489 | if (*memory & EFI_PAGE_MASK) |
| 490 | return EFI_NOT_FOUND; |
Sughosh Ganu | fa0ddab | 2024-10-15 21:07:06 +0530 | [diff] [blame] | 491 | |
| 492 | addr = map_to_sysmem((void *)(uintptr_t)*memory); |
| 493 | addr = (u64)lmb_alloc_addr_flags(addr, len, flags); |
| 494 | if (!addr) |
Heinrich Schuchardt | fbdb93c | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 495 | return EFI_NOT_FOUND; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 496 | break; |
| 497 | default: |
| 498 | /* UEFI doesn't specify other allocation types */ |
Heinrich Schuchardt | fbdb93c | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 499 | return EFI_INVALID_PARAMETER; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 500 | } |
| 501 | |
Sughosh Ganu | fa0ddab | 2024-10-15 21:07:06 +0530 | [diff] [blame] | 502 | addr = (u64)(uintptr_t)map_sysmem(addr, 0); |
Heinrich Schuchardt | fbdb93c | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 503 | /* Reserve that map in our memory maps */ |
Michael Walle | 282d386 | 2020-05-17 12:29:19 +0200 | [diff] [blame] | 504 | ret = efi_add_memory_map_pg(addr, pages, memory_type, true); |
| 505 | if (ret != EFI_SUCCESS) |
Heinrich Schuchardt | fbdb93c | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 506 | /* Map would overlap, bail out */ |
| 507 | return EFI_OUT_OF_RESOURCES; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 508 | |
Heinrich Schuchardt | fbdb93c | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 509 | *memory = addr; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 510 | |
Heinrich Schuchardt | fbdb93c | 2019-05-11 08:43:30 +0200 | [diff] [blame] | 511 | return EFI_SUCCESS; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 512 | } |
| 513 | |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 514 | /** |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 515 | * efi_free_pages() - free memory pages |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 516 | * |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 517 | * @memory: start of the memory area to be freed |
| 518 | * @pages: number of pages to be freed |
| 519 | * Return: status code |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 520 | */ |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 521 | efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages) |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 522 | { |
Sughosh Ganu | fa0ddab | 2024-10-15 21:07:06 +0530 | [diff] [blame] | 523 | u64 len; |
Sughosh Ganu | fa0ddab | 2024-10-15 21:07:06 +0530 | [diff] [blame] | 524 | long status; |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 525 | efi_status_t ret; |
| 526 | |
Heinrich Schuchardt | 906f685 | 2019-05-11 08:21:17 +0200 | [diff] [blame] | 527 | ret = efi_check_allocated(memory, true); |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 528 | if (ret != EFI_SUCCESS) |
| 529 | return ret; |
Stefan Brüns | a4f4302 | 2016-10-01 23:32:27 +0200 | [diff] [blame] | 530 | |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 531 | /* Sanity check */ |
Heinrich Schuchardt | 7bf4bc9 | 2019-04-25 18:41:40 +0200 | [diff] [blame] | 532 | if (!memory || (memory & EFI_PAGE_MASK) || !pages) { |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 533 | printf("%s: illegal free 0x%llx, 0x%zx\n", __func__, |
| 534 | memory, pages); |
| 535 | return EFI_INVALID_PARAMETER; |
| 536 | } |
| 537 | |
Sughosh Ganu | fa0ddab | 2024-10-15 21:07:06 +0530 | [diff] [blame] | 538 | len = (u64)pages << EFI_PAGE_SHIFT; |
Ilias Apalodimas | e9e55c7 | 2024-10-24 14:01:55 +0300 | [diff] [blame] | 539 | /* |
| 540 | * The 'memory' variable for sandbox holds a pointer which has already |
| 541 | * been mapped with map_sysmem() from efi_allocate_pages(). Convert |
| 542 | * it back to an address LMB understands |
| 543 | */ |
Sughosh Ganu | fa0ddab | 2024-10-15 21:07:06 +0530 | [diff] [blame] | 544 | status = lmb_free_flags(map_to_sysmem((void *)(uintptr_t)memory), len, |
Ilias Apalodimas | e9e55c7 | 2024-10-24 14:01:55 +0300 | [diff] [blame] | 545 | LMB_NOOVERWRITE); |
Sughosh Ganu | fa0ddab | 2024-10-15 21:07:06 +0530 | [diff] [blame] | 546 | if (status) |
| 547 | return EFI_NOT_FOUND; |
| 548 | |
Bryan O'Donoghue | 89a22b4 | 2019-07-15 12:00:39 +0100 | [diff] [blame] | 549 | return ret; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 550 | } |
| 551 | |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 552 | /** |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 553 | * efi_alloc_aligned_pages() - allocate aligned memory pages |
Ilias Apalodimas | 6e01c23 | 2021-10-11 15:10:23 +0300 | [diff] [blame] | 554 | * |
| 555 | * @len: len in bytes |
| 556 | * @memory_type: usage type of the allocated memory |
| 557 | * @align: alignment in bytes |
| 558 | * Return: aligned memory or NULL |
| 559 | */ |
| 560 | void *efi_alloc_aligned_pages(u64 len, int memory_type, size_t align) |
| 561 | { |
| 562 | u64 req_pages = efi_size_in_pages(len); |
| 563 | u64 true_pages = req_pages + efi_size_in_pages(align) - 1; |
| 564 | u64 free_pages; |
| 565 | u64 aligned_mem; |
| 566 | efi_status_t r; |
| 567 | u64 mem; |
| 568 | |
| 569 | /* align must be zero or a power of two */ |
| 570 | if (align & (align - 1)) |
| 571 | return NULL; |
| 572 | |
| 573 | /* Check for overflow */ |
| 574 | if (true_pages < req_pages) |
| 575 | return NULL; |
| 576 | |
| 577 | if (align < EFI_PAGE_SIZE) { |
| 578 | r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type, |
| 579 | req_pages, &mem); |
| 580 | return (r == EFI_SUCCESS) ? (void *)(uintptr_t)mem : NULL; |
| 581 | } |
| 582 | |
| 583 | r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type, |
| 584 | true_pages, &mem); |
| 585 | if (r != EFI_SUCCESS) |
| 586 | return NULL; |
| 587 | |
| 588 | aligned_mem = ALIGN(mem, align); |
| 589 | /* Free pages before alignment */ |
| 590 | free_pages = efi_size_in_pages(aligned_mem - mem); |
| 591 | if (free_pages) |
| 592 | efi_free_pages(mem, free_pages); |
| 593 | |
| 594 | /* Free trailing pages */ |
| 595 | free_pages = true_pages - (req_pages + free_pages); |
| 596 | if (free_pages) { |
| 597 | mem = aligned_mem + req_pages * EFI_PAGE_SIZE; |
| 598 | efi_free_pages(mem, free_pages); |
| 599 | } |
| 600 | |
| 601 | return (void *)(uintptr_t)aligned_mem; |
| 602 | } |
| 603 | |
| 604 | /** |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 605 | * efi_allocate_pool - allocate memory from pool |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 606 | * |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 607 | * @pool_type: type of the pool from which memory is to be allocated |
| 608 | * @size: number of bytes to be allocated |
| 609 | * @buffer: allocated memory |
| 610 | * Return: status code |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 611 | */ |
Heinrich Schuchardt | 9822894 | 2021-08-17 15:05:31 +0200 | [diff] [blame] | 612 | efi_status_t efi_allocate_pool(enum efi_memory_type pool_type, efi_uintn_t size, void **buffer) |
Stefan Brüns | 5a09aef | 2016-10-09 22:17:18 +0200 | [diff] [blame] | 613 | { |
| 614 | efi_status_t r; |
Heinrich Schuchardt | 657e979 | 2019-03-18 20:01:59 +0100 | [diff] [blame] | 615 | u64 addr; |
Alexander Graf | cd40517 | 2018-06-18 17:23:15 +0200 | [diff] [blame] | 616 | struct efi_pool_allocation *alloc; |
Heinrich Schuchardt | dcdca29 | 2018-11-18 17:58:49 +0100 | [diff] [blame] | 617 | u64 num_pages = efi_size_in_pages(size + |
| 618 | sizeof(struct efi_pool_allocation)); |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 619 | |
Heinrich Schuchardt | a137c96 | 2018-07-02 12:53:53 +0200 | [diff] [blame] | 620 | if (!buffer) |
| 621 | return EFI_INVALID_PARAMETER; |
| 622 | |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 623 | if (size == 0) { |
| 624 | *buffer = NULL; |
| 625 | return EFI_SUCCESS; |
| 626 | } |
Stefan Brüns | 5a09aef | 2016-10-09 22:17:18 +0200 | [diff] [blame] | 627 | |
Heinrich Schuchardt | 0f7186b | 2018-05-26 10:32:27 +0200 | [diff] [blame] | 628 | r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, pool_type, num_pages, |
Heinrich Schuchardt | 657e979 | 2019-03-18 20:01:59 +0100 | [diff] [blame] | 629 | &addr); |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 630 | if (r == EFI_SUCCESS) { |
Heinrich Schuchardt | 657e979 | 2019-03-18 20:01:59 +0100 | [diff] [blame] | 631 | alloc = (struct efi_pool_allocation *)(uintptr_t)addr; |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 632 | alloc->num_pages = num_pages; |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 633 | alloc->checksum = checksum(alloc); |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 634 | *buffer = alloc->data; |
| 635 | } |
| 636 | |
| 637 | return r; |
| 638 | } |
| 639 | |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 640 | /** |
Heinrich Schuchardt | 4564025 | 2023-03-19 09:20:22 +0100 | [diff] [blame] | 641 | * efi_alloc() - allocate boot services data pool memory |
| 642 | * |
| 643 | * Allocate memory from pool and zero it out. |
| 644 | * |
| 645 | * @size: number of bytes to allocate |
| 646 | * Return: pointer to allocated memory or NULL |
| 647 | */ |
| 648 | void *efi_alloc(size_t size) |
| 649 | { |
| 650 | void *buf; |
| 651 | |
| 652 | if (efi_allocate_pool(EFI_BOOT_SERVICES_DATA, size, &buf) != |
| 653 | EFI_SUCCESS) { |
Heinrich Schuchardt | f39486a | 2024-10-17 20:05:07 +0200 | [diff] [blame] | 654 | log_err("out of memory\n"); |
Heinrich Schuchardt | 4564025 | 2023-03-19 09:20:22 +0100 | [diff] [blame] | 655 | return NULL; |
| 656 | } |
| 657 | memset(buf, 0, size); |
| 658 | |
| 659 | return buf; |
| 660 | } |
| 661 | |
| 662 | /** |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 663 | * efi_free_pool() - free memory from pool |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 664 | * |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 665 | * @buffer: start of memory to be freed |
| 666 | * Return: status code |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 667 | */ |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 668 | efi_status_t efi_free_pool(void *buffer) |
| 669 | { |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 670 | efi_status_t ret; |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 671 | struct efi_pool_allocation *alloc; |
| 672 | |
Heinrich Schuchardt | 56b587f7 | 2019-06-12 07:17:04 +0200 | [diff] [blame] | 673 | if (!buffer) |
| 674 | return EFI_INVALID_PARAMETER; |
| 675 | |
Heinrich Schuchardt | 906f685 | 2019-05-11 08:21:17 +0200 | [diff] [blame] | 676 | ret = efi_check_allocated((uintptr_t)buffer, true); |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 677 | if (ret != EFI_SUCCESS) |
| 678 | return ret; |
xypron.glpk@gmx.de | 34ddae7 | 2017-07-14 19:12:39 +0200 | [diff] [blame] | 679 | |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 680 | alloc = container_of(buffer, struct efi_pool_allocation, data); |
Heinrich Schuchardt | b4e2ba3 | 2019-03-26 05:31:41 +0100 | [diff] [blame] | 681 | |
| 682 | /* Check that this memory was allocated by efi_allocate_pool() */ |
| 683 | if (((uintptr_t)alloc & EFI_PAGE_MASK) || |
| 684 | alloc->checksum != checksum(alloc)) { |
| 685 | printf("%s: illegal free 0x%p\n", __func__, buffer); |
| 686 | return EFI_INVALID_PARAMETER; |
| 687 | } |
| 688 | /* Avoid double free */ |
| 689 | alloc->checksum = 0; |
Stefan Brüns | 67b67d9 | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 690 | |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 691 | ret = efi_free_pages((uintptr_t)alloc, alloc->num_pages); |
Stefan Brüns | 5a09aef | 2016-10-09 22:17:18 +0200 | [diff] [blame] | 692 | |
Heinrich Schuchardt | ac7f842 | 2019-05-10 21:21:30 +0200 | [diff] [blame] | 693 | return ret; |
Stefan Brüns | 5a09aef | 2016-10-09 22:17:18 +0200 | [diff] [blame] | 694 | } |
| 695 | |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 696 | /** |
| 697 | * efi_get_memory_map() - get map describing memory usage. |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 698 | * |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 699 | * @memory_map_size: on entry the size, in bytes, of the memory map buffer, |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 700 | * on exit the size of the copied memory map |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 701 | * @memory_map: buffer to which the memory map is written |
| 702 | * @map_key: key for the memory map |
| 703 | * @descriptor_size: size of an individual memory descriptor |
| 704 | * @descriptor_version: version number of the memory descriptor structure |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 705 | * Return: status code |
Heinrich Schuchardt | 1b32cae | 2017-12-04 20:52:41 +0100 | [diff] [blame] | 706 | */ |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 707 | efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size, |
| 708 | struct efi_mem_desc *memory_map, |
| 709 | efi_uintn_t *map_key, |
| 710 | efi_uintn_t *descriptor_size, |
| 711 | uint32_t *descriptor_version) |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 712 | { |
Sughosh Ganu | 38dcdb8 | 2024-07-30 16:41:29 +0530 | [diff] [blame] | 713 | size_t map_entries; |
Heinrich Schuchardt | 798a441 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 714 | efi_uintn_t map_size = 0; |
Sughosh Ganu | 21ad4cc | 2024-07-30 16:41:31 +0530 | [diff] [blame] | 715 | struct efi_mem_list *lmem; |
Heinrich Schuchardt | 9b16646 | 2018-08-06 22:28:18 +0200 | [diff] [blame] | 716 | efi_uintn_t provided_map_size; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 717 | |
Heinrich Schuchardt | e1a1a09 | 2018-07-02 12:53:54 +0200 | [diff] [blame] | 718 | if (!memory_map_size) |
| 719 | return EFI_INVALID_PARAMETER; |
| 720 | |
Heinrich Schuchardt | 9b16646 | 2018-08-06 22:28:18 +0200 | [diff] [blame] | 721 | provided_map_size = *memory_map_size; |
| 722 | |
Sughosh Ganu | 38dcdb8 | 2024-07-30 16:41:29 +0530 | [diff] [blame] | 723 | map_entries = list_count_nodes(&efi_mem); |
Alexander Graf | 9b59102 | 2016-04-11 23:51:02 +0200 | [diff] [blame] | 724 | |
| 725 | map_size = map_entries * sizeof(struct efi_mem_desc); |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 726 | |
Rob Clark | a84c974 | 2017-07-26 14:34:05 -0400 | [diff] [blame] | 727 | *memory_map_size = map_size; |
| 728 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 729 | if (descriptor_size) |
| 730 | *descriptor_size = sizeof(struct efi_mem_desc); |
| 731 | |
Mian Yousaf Kaukab | 338cf56 | 2016-09-05 23:59:22 +0200 | [diff] [blame] | 732 | if (descriptor_version) |
| 733 | *descriptor_version = EFI_MEMORY_DESCRIPTOR_VERSION; |
| 734 | |
AKASHI Takahiro | 3ee138d | 2020-03-11 15:18:18 +0900 | [diff] [blame] | 735 | if (provided_map_size < map_size) |
| 736 | return EFI_BUFFER_TOO_SMALL; |
| 737 | |
| 738 | if (!memory_map) |
| 739 | return EFI_INVALID_PARAMETER; |
| 740 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 741 | /* Copy list into array */ |
Heinrich Schuchardt | e1a1a09 | 2018-07-02 12:53:54 +0200 | [diff] [blame] | 742 | /* Return the list in ascending order */ |
| 743 | memory_map = &memory_map[map_entries - 1]; |
Sughosh Ganu | 21ad4cc | 2024-07-30 16:41:31 +0530 | [diff] [blame] | 744 | list_for_each_entry(lmem, &efi_mem, link) { |
Heinrich Schuchardt | e1a1a09 | 2018-07-02 12:53:54 +0200 | [diff] [blame] | 745 | *memory_map = lmem->desc; |
| 746 | memory_map--; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 747 | } |
| 748 | |
Heinrich Schuchardt | e1a1a09 | 2018-07-02 12:53:54 +0200 | [diff] [blame] | 749 | if (map_key) |
Heinrich Schuchardt | 0f233c4 | 2018-07-02 12:53:55 +0200 | [diff] [blame] | 750 | *map_key = efi_memory_map_key; |
xypron.glpk@gmx.de | e436e63 | 2017-07-21 19:05:44 +0200 | [diff] [blame] | 751 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 752 | return EFI_SUCCESS; |
| 753 | } |
| 754 | |
Park, Aiden | e009e7a | 2019-09-03 17:43:43 +0000 | [diff] [blame] | 755 | /** |
Heinrich Schuchardt | 13ce2d9 | 2023-01-05 18:26:01 +0100 | [diff] [blame] | 756 | * efi_get_memory_map_alloc() - allocate map describing memory usage |
| 757 | * |
| 758 | * The caller is responsible for calling FreePool() if the call succeeds. |
| 759 | * |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 760 | * @map_size: size of the memory map |
| 761 | * @memory_map: buffer to which the memory map is written |
Heinrich Schuchardt | 13ce2d9 | 2023-01-05 18:26:01 +0100 | [diff] [blame] | 762 | * Return: status code |
| 763 | */ |
| 764 | efi_status_t efi_get_memory_map_alloc(efi_uintn_t *map_size, |
| 765 | struct efi_mem_desc **memory_map) |
| 766 | { |
| 767 | efi_status_t ret; |
| 768 | |
| 769 | *memory_map = NULL; |
| 770 | *map_size = 0; |
| 771 | ret = efi_get_memory_map(map_size, *memory_map, NULL, NULL, NULL); |
| 772 | if (ret == EFI_BUFFER_TOO_SMALL) { |
| 773 | *map_size += sizeof(struct efi_mem_desc); /* for the map */ |
| 774 | ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, *map_size, |
| 775 | (void **)memory_map); |
| 776 | if (ret != EFI_SUCCESS) |
| 777 | return ret; |
| 778 | ret = efi_get_memory_map(map_size, *memory_map, |
| 779 | NULL, NULL, NULL); |
| 780 | if (ret != EFI_SUCCESS) { |
| 781 | efi_free_pool(*memory_map); |
| 782 | *memory_map = NULL; |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | return ret; |
| 787 | } |
| 788 | |
| 789 | /** |
Sughosh Ganu | fbb00c2 | 2024-10-15 21:07:14 +0530 | [diff] [blame] | 790 | * efi_add_known_memory() - add memory types to the EFI memory map |
Park, Aiden | e009e7a | 2019-09-03 17:43:43 +0000 | [diff] [blame] | 791 | * |
Sughosh Ganu | fbb00c2 | 2024-10-15 21:07:14 +0530 | [diff] [blame] | 792 | * This function is to be used to add different memory types other |
| 793 | * than EFI_CONVENTIONAL_MEMORY to the EFI memory map. The conventional |
| 794 | * memory is handled by the LMB module and gets added to the memory |
| 795 | * map through the LMB module. |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 796 | * |
Sughosh Ganu | fbb00c2 | 2024-10-15 21:07:14 +0530 | [diff] [blame] | 797 | * This function may be overridden for architectures specific purposes. |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 798 | */ |
York Sun | a8e9f0a | 2017-03-06 09:02:27 -0800 | [diff] [blame] | 799 | __weak void efi_add_known_memory(void) |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 800 | { |
York Sun | a8e9f0a | 2017-03-06 09:02:27 -0800 | [diff] [blame] | 801 | } |
| 802 | |
Heinrich Schuchardt | b9b838d | 2023-01-08 01:00:00 +0100 | [diff] [blame] | 803 | /** |
| 804 | * add_u_boot_and_runtime() - add U-Boot code to memory map |
| 805 | * |
| 806 | * Add memory regions for U-Boot's memory and for the runtime services code. |
| 807 | */ |
Simon Glass | 7863943 | 2018-06-18 17:23:12 +0200 | [diff] [blame] | 808 | static void add_u_boot_and_runtime(void) |
York Sun | a8e9f0a | 2017-03-06 09:02:27 -0800 | [diff] [blame] | 809 | { |
| 810 | unsigned long runtime_start, runtime_end, runtime_pages; |
Alexander Graf | 15937a2 | 2018-09-17 13:54:33 +0200 | [diff] [blame] | 811 | unsigned long runtime_mask = EFI_PAGE_MASK; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 812 | |
Alexander Graf | 15937a2 | 2018-09-17 13:54:33 +0200 | [diff] [blame] | 813 | #if defined(__aarch64__) |
| 814 | /* |
| 815 | * Runtime Services must be 64KiB aligned according to the |
| 816 | * "AArch64 Platforms" section in the UEFI spec (2.7+). |
| 817 | */ |
| 818 | |
| 819 | runtime_mask = SZ_64K - 1; |
| 820 | #endif |
| 821 | |
| 822 | /* |
| 823 | * Add Runtime Services. We mark surrounding boottime code as runtime as |
| 824 | * well to fulfill the runtime alignment constraints but avoid padding. |
| 825 | */ |
Ilias Apalodimas | 2af9553 | 2024-04-04 09:35:35 +0300 | [diff] [blame] | 826 | runtime_start = (uintptr_t)__efi_runtime_start & ~runtime_mask; |
| 827 | runtime_end = (uintptr_t)__efi_runtime_stop; |
Alexander Graf | 15937a2 | 2018-09-17 13:54:33 +0200 | [diff] [blame] | 828 | runtime_end = (runtime_end + runtime_mask) & ~runtime_mask; |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 829 | runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT; |
Michael Walle | 282d386 | 2020-05-17 12:29:19 +0200 | [diff] [blame] | 830 | efi_add_memory_map_pg(runtime_start, runtime_pages, |
| 831 | EFI_RUNTIME_SERVICES_CODE, false); |
Simon Glass | 7863943 | 2018-06-18 17:23:12 +0200 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | int efi_memory_init(void) |
| 835 | { |
| 836 | efi_add_known_memory(); |
| 837 | |
Heinrich Schuchardt | a69976e | 2019-11-08 20:42:53 +0100 | [diff] [blame] | 838 | add_u_boot_and_runtime(); |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 839 | |
Alexander Graf | 7c00a3c | 2016-05-11 18:25:48 +0200 | [diff] [blame] | 840 | #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER |
| 841 | /* Request a 32bit 64MB bounce buffer region */ |
| 842 | uint64_t efi_bounce_buffer_addr = 0xffffffff; |
| 843 | |
Heinrich Schuchardt | 296b55c | 2022-11-29 16:00:41 +0100 | [diff] [blame] | 844 | if (efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS, EFI_BOOT_SERVICES_DATA, |
Alexander Graf | 7c00a3c | 2016-05-11 18:25:48 +0200 | [diff] [blame] | 845 | (64 * 1024 * 1024) >> EFI_PAGE_SHIFT, |
| 846 | &efi_bounce_buffer_addr) != EFI_SUCCESS) |
| 847 | return -1; |
| 848 | |
| 849 | efi_bounce_buffer = (void*)(uintptr_t)efi_bounce_buffer_addr; |
| 850 | #endif |
| 851 | |
Alexander Graf | 8623f92 | 2016-03-04 01:10:04 +0100 | [diff] [blame] | 852 | return 0; |
| 853 | } |