xref: /openbmc/linux/drivers/firmware/efi/libstub/randomalloc.c (revision 840d9a813c8eaa5c55d86525e374a97ca5023b53)
10ed02bdaSArd Biesheuvel // SPDX-License-Identifier: GPL-2.0
20ed02bdaSArd Biesheuvel /*
30ed02bdaSArd Biesheuvel  * Copyright (C) 2016 Linaro Ltd;  <ard.biesheuvel@linaro.org>
40ed02bdaSArd Biesheuvel  */
50ed02bdaSArd Biesheuvel 
60ed02bdaSArd Biesheuvel #include <linux/efi.h>
70ed02bdaSArd Biesheuvel #include <linux/log2.h>
80ed02bdaSArd Biesheuvel #include <asm/efi.h>
90ed02bdaSArd Biesheuvel 
100ed02bdaSArd Biesheuvel #include "efistub.h"
110ed02bdaSArd Biesheuvel 
120ed02bdaSArd Biesheuvel /*
130ed02bdaSArd Biesheuvel  * Return the number of slots covered by this entry, i.e., the number of
140ed02bdaSArd Biesheuvel  * addresses it covers that are suitably aligned and supply enough room
150ed02bdaSArd Biesheuvel  * for the allocation.
160ed02bdaSArd Biesheuvel  */
get_entry_num_slots(efi_memory_desc_t * md,unsigned long size,unsigned long align_shift,u64 alloc_min,u64 alloc_max)170ed02bdaSArd Biesheuvel static unsigned long get_entry_num_slots(efi_memory_desc_t *md,
180ed02bdaSArd Biesheuvel 					 unsigned long size,
19bc5ddcefSArd Biesheuvel 					 unsigned long align_shift,
200e7ca435SArd Biesheuvel 					 u64 alloc_min, u64 alloc_max)
210ed02bdaSArd Biesheuvel {
220ed02bdaSArd Biesheuvel 	unsigned long align = 1UL << align_shift;
230ed02bdaSArd Biesheuvel 	u64 first_slot, last_slot, region_end;
240ed02bdaSArd Biesheuvel 
250ed02bdaSArd Biesheuvel 	if (md->type != EFI_CONVENTIONAL_MEMORY)
260ed02bdaSArd Biesheuvel 		return 0;
270ed02bdaSArd Biesheuvel 
28b5bfb235SArd Biesheuvel 	if (md->attribute & EFI_MEMORY_HOT_PLUGGABLE)
29b5bfb235SArd Biesheuvel 		return 0;
30b5bfb235SArd Biesheuvel 
310ed02bdaSArd Biesheuvel 	if (efi_soft_reserve_enabled() &&
320ed02bdaSArd Biesheuvel 	    (md->attribute & EFI_MEMORY_SP))
330ed02bdaSArd Biesheuvel 		return 0;
340ed02bdaSArd Biesheuvel 
350ed02bdaSArd Biesheuvel 	region_end = min(md->phys_addr + md->num_pages * EFI_PAGE_SIZE - 1,
360e7ca435SArd Biesheuvel 			 alloc_max);
374152433cSBenjamin Herrenschmidt 	if (region_end < size)
384152433cSBenjamin Herrenschmidt 		return 0;
390ed02bdaSArd Biesheuvel 
400e7ca435SArd Biesheuvel 	first_slot = round_up(max(md->phys_addr, alloc_min), align);
410ed02bdaSArd Biesheuvel 	last_slot = round_down(region_end - size + 1, align);
420ed02bdaSArd Biesheuvel 
430ed02bdaSArd Biesheuvel 	if (first_slot > last_slot)
440ed02bdaSArd Biesheuvel 		return 0;
450ed02bdaSArd Biesheuvel 
460ed02bdaSArd Biesheuvel 	return ((unsigned long)(last_slot - first_slot) >> align_shift) + 1;
470ed02bdaSArd Biesheuvel }
480ed02bdaSArd Biesheuvel 
490ed02bdaSArd Biesheuvel /*
500ed02bdaSArd Biesheuvel  * The UEFI memory descriptors have a virtual address field that is only used
510ed02bdaSArd Biesheuvel  * when installing the virtual mapping using SetVirtualAddressMap(). Since it
520ed02bdaSArd Biesheuvel  * is unused here, we can reuse it to keep track of each descriptor's slot
530ed02bdaSArd Biesheuvel  * count.
540ed02bdaSArd Biesheuvel  */
550ed02bdaSArd Biesheuvel #define MD_NUM_SLOTS(md)	((md)->virt_addr)
560ed02bdaSArd Biesheuvel 
efi_random_alloc(unsigned long size,unsigned long align,unsigned long * addr,unsigned long random_seed,int memory_type,unsigned long alloc_min,unsigned long alloc_max)570ed02bdaSArd Biesheuvel efi_status_t efi_random_alloc(unsigned long size,
580ed02bdaSArd Biesheuvel 			      unsigned long align,
590ed02bdaSArd Biesheuvel 			      unsigned long *addr,
609cf42bcaSArd Biesheuvel 			      unsigned long random_seed,
61bc5ddcefSArd Biesheuvel 			      int memory_type,
620e7ca435SArd Biesheuvel 			      unsigned long alloc_min,
630e7ca435SArd Biesheuvel 			      unsigned long alloc_max)
640ed02bdaSArd Biesheuvel {
65eab31265SArd Biesheuvel 	unsigned long total_slots = 0, target_slot;
66a6cfe03cSArd Biesheuvel 	unsigned long total_mirrored_slots = 0;
67eab31265SArd Biesheuvel 	struct efi_boot_memmap *map;
680ed02bdaSArd Biesheuvel 	efi_status_t status;
690ed02bdaSArd Biesheuvel 	int map_offset;
700ed02bdaSArd Biesheuvel 
71171539f5SArd Biesheuvel 	status = efi_get_memory_map(&map, false);
720ed02bdaSArd Biesheuvel 	if (status != EFI_SUCCESS)
730ed02bdaSArd Biesheuvel 		return status;
740ed02bdaSArd Biesheuvel 
750ed02bdaSArd Biesheuvel 	if (align < EFI_ALLOC_ALIGN)
760ed02bdaSArd Biesheuvel 		align = EFI_ALLOC_ALIGN;
770ed02bdaSArd Biesheuvel 
78*19f4e715SArd Biesheuvel 	/* Avoid address 0x0, as it can be mistaken for NULL */
79*19f4e715SArd Biesheuvel 	if (alloc_min == 0)
80*19f4e715SArd Biesheuvel 		alloc_min = align;
81*19f4e715SArd Biesheuvel 
82e1df73e2SArd Biesheuvel 	size = round_up(size, EFI_ALLOC_ALIGN);
83e1df73e2SArd Biesheuvel 
840ed02bdaSArd Biesheuvel 	/* count the suitable slots in each memory map entry */
85eab31265SArd Biesheuvel 	for (map_offset = 0; map_offset < map->map_size; map_offset += map->desc_size) {
86eab31265SArd Biesheuvel 		efi_memory_desc_t *md = (void *)map->map + map_offset;
870ed02bdaSArd Biesheuvel 		unsigned long slots;
880ed02bdaSArd Biesheuvel 
890e7ca435SArd Biesheuvel 		slots = get_entry_num_slots(md, size, ilog2(align), alloc_min,
900e7ca435SArd Biesheuvel 					    alloc_max);
910ed02bdaSArd Biesheuvel 		MD_NUM_SLOTS(md) = slots;
920ed02bdaSArd Biesheuvel 		total_slots += slots;
93a6cfe03cSArd Biesheuvel 		if (md->attribute & EFI_MEMORY_MORE_RELIABLE)
94a6cfe03cSArd Biesheuvel 			total_mirrored_slots += slots;
950ed02bdaSArd Biesheuvel 	}
960ed02bdaSArd Biesheuvel 
97a6cfe03cSArd Biesheuvel 	/* consider only mirrored slots for randomization if any exist */
98a6cfe03cSArd Biesheuvel 	if (total_mirrored_slots > 0)
99a6cfe03cSArd Biesheuvel 		total_slots = total_mirrored_slots;
100a6cfe03cSArd Biesheuvel 
1010ed02bdaSArd Biesheuvel 	/* find a random number between 0 and total_slots */
102c37c9162SArd Biesheuvel 	target_slot = (total_slots * (u64)(random_seed & U32_MAX)) >> 32;
1030ed02bdaSArd Biesheuvel 
1040ed02bdaSArd Biesheuvel 	/*
1050ed02bdaSArd Biesheuvel 	 * target_slot is now a value in the range [0, total_slots), and so
1060ed02bdaSArd Biesheuvel 	 * it corresponds with exactly one of the suitable slots we recorded
1070ed02bdaSArd Biesheuvel 	 * when iterating over the memory map the first time around.
1080ed02bdaSArd Biesheuvel 	 *
1090ed02bdaSArd Biesheuvel 	 * So iterate over the memory map again, subtracting the number of
1100ed02bdaSArd Biesheuvel 	 * slots of each entry at each iteration, until we have found the entry
1110ed02bdaSArd Biesheuvel 	 * that covers our chosen slot. Use the residual value of target_slot
1120ed02bdaSArd Biesheuvel 	 * to calculate the randomly chosen address, and allocate it directly
1130ed02bdaSArd Biesheuvel 	 * using EFI_ALLOCATE_ADDRESS.
1140ed02bdaSArd Biesheuvel 	 */
1150b1d9debSArd Biesheuvel 	status = EFI_OUT_OF_RESOURCES;
116eab31265SArd Biesheuvel 	for (map_offset = 0; map_offset < map->map_size; map_offset += map->desc_size) {
117eab31265SArd Biesheuvel 		efi_memory_desc_t *md = (void *)map->map + map_offset;
1180ed02bdaSArd Biesheuvel 		efi_physical_addr_t target;
1190ed02bdaSArd Biesheuvel 		unsigned long pages;
1200ed02bdaSArd Biesheuvel 
121a6cfe03cSArd Biesheuvel 		if (total_mirrored_slots > 0 &&
122a6cfe03cSArd Biesheuvel 		    !(md->attribute & EFI_MEMORY_MORE_RELIABLE))
123a6cfe03cSArd Biesheuvel 			continue;
124a6cfe03cSArd Biesheuvel 
1250ed02bdaSArd Biesheuvel 		if (target_slot >= MD_NUM_SLOTS(md)) {
1260ed02bdaSArd Biesheuvel 			target_slot -= MD_NUM_SLOTS(md);
1270ed02bdaSArd Biesheuvel 			continue;
1280ed02bdaSArd Biesheuvel 		}
1290ed02bdaSArd Biesheuvel 
13090048007SArd Biesheuvel 		target = round_up(max_t(u64, md->phys_addr, alloc_min), align) + target_slot * align;
131e1df73e2SArd Biesheuvel 		pages = size / EFI_PAGE_SIZE;
1320ed02bdaSArd Biesheuvel 
1330ed02bdaSArd Biesheuvel 		status = efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS,
1349cf42bcaSArd Biesheuvel 				     memory_type, pages, &target);
1350ed02bdaSArd Biesheuvel 		if (status == EFI_SUCCESS)
1360ed02bdaSArd Biesheuvel 			*addr = target;
1370ed02bdaSArd Biesheuvel 		break;
1380ed02bdaSArd Biesheuvel 	}
1390ed02bdaSArd Biesheuvel 
140eab31265SArd Biesheuvel 	efi_bs_call(free_pool, map);
1410ed02bdaSArd Biesheuvel 
1420ed02bdaSArd Biesheuvel 	return status;
1430ed02bdaSArd Biesheuvel }
144