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