xref: /linux/drivers/firmware/efi/libstub/randomalloc.c (revision 4b660dbd9ee2059850fd30e0df420ca7a38a1856)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2016 Linaro Ltd;  <ard.biesheuvel@linaro.org>
4  */
5 
6 #include <linux/efi.h>
7 #include <linux/log2.h>
8 #include <asm/efi.h>
9 
10 #include "efistub.h"
11 
12 /*
13  * Return the number of slots covered by this entry, i.e., the number of
14  * addresses it covers that are suitably aligned and supply enough room
15  * for the allocation.
16  */
17 static unsigned long get_entry_num_slots(efi_memory_desc_t *md,
18 					 unsigned long size,
19 					 unsigned long align_shift,
20 					 u64 alloc_min, u64 alloc_max)
21 {
22 	unsigned long align = 1UL << align_shift;
23 	u64 first_slot, last_slot, region_end;
24 
25 	if (md->type != EFI_CONVENTIONAL_MEMORY)
26 		return 0;
27 
28 	if (efi_soft_reserve_enabled() &&
29 	    (md->attribute & EFI_MEMORY_SP))
30 		return 0;
31 
32 	region_end = min(md->phys_addr + md->num_pages * EFI_PAGE_SIZE - 1,
33 			 alloc_max);
34 	if (region_end < size)
35 		return 0;
36 
37 	first_slot = round_up(max(md->phys_addr, alloc_min), align);
38 	last_slot = round_down(region_end - size + 1, align);
39 
40 	if (first_slot > last_slot)
41 		return 0;
42 
43 	return ((unsigned long)(last_slot - first_slot) >> align_shift) + 1;
44 }
45 
46 /*
47  * The UEFI memory descriptors have a virtual address field that is only used
48  * when installing the virtual mapping using SetVirtualAddressMap(). Since it
49  * is unused here, we can reuse it to keep track of each descriptor's slot
50  * count.
51  */
52 #define MD_NUM_SLOTS(md)	((md)->virt_addr)
53 
54 efi_status_t efi_random_alloc(unsigned long size,
55 			      unsigned long align,
56 			      unsigned long *addr,
57 			      unsigned long random_seed,
58 			      int memory_type,
59 			      unsigned long alloc_min,
60 			      unsigned long alloc_max)
61 {
62 	unsigned long total_slots = 0, target_slot;
63 	unsigned long total_mirrored_slots = 0;
64 	struct efi_boot_memmap *map;
65 	efi_status_t status;
66 	int map_offset;
67 
68 	status = efi_get_memory_map(&map, false);
69 	if (status != EFI_SUCCESS)
70 		return status;
71 
72 	if (align < EFI_ALLOC_ALIGN)
73 		align = EFI_ALLOC_ALIGN;
74 
75 	size = round_up(size, EFI_ALLOC_ALIGN);
76 
77 	/* count the suitable slots in each memory map entry */
78 	for (map_offset = 0; map_offset < map->map_size; map_offset += map->desc_size) {
79 		efi_memory_desc_t *md = (void *)map->map + map_offset;
80 		unsigned long slots;
81 
82 		slots = get_entry_num_slots(md, size, ilog2(align), alloc_min,
83 					    alloc_max);
84 		MD_NUM_SLOTS(md) = slots;
85 		total_slots += slots;
86 		if (md->attribute & EFI_MEMORY_MORE_RELIABLE)
87 			total_mirrored_slots += slots;
88 	}
89 
90 	/* consider only mirrored slots for randomization if any exist */
91 	if (total_mirrored_slots > 0)
92 		total_slots = total_mirrored_slots;
93 
94 	/* find a random number between 0 and total_slots */
95 	target_slot = (total_slots * (u64)(random_seed & U32_MAX)) >> 32;
96 
97 	/*
98 	 * target_slot is now a value in the range [0, total_slots), and so
99 	 * it corresponds with exactly one of the suitable slots we recorded
100 	 * when iterating over the memory map the first time around.
101 	 *
102 	 * So iterate over the memory map again, subtracting the number of
103 	 * slots of each entry at each iteration, until we have found the entry
104 	 * that covers our chosen slot. Use the residual value of target_slot
105 	 * to calculate the randomly chosen address, and allocate it directly
106 	 * using EFI_ALLOCATE_ADDRESS.
107 	 */
108 	status = EFI_OUT_OF_RESOURCES;
109 	for (map_offset = 0; map_offset < map->map_size; map_offset += map->desc_size) {
110 		efi_memory_desc_t *md = (void *)map->map + map_offset;
111 		efi_physical_addr_t target;
112 		unsigned long pages;
113 
114 		if (total_mirrored_slots > 0 &&
115 		    !(md->attribute & EFI_MEMORY_MORE_RELIABLE))
116 			continue;
117 
118 		if (target_slot >= MD_NUM_SLOTS(md)) {
119 			target_slot -= MD_NUM_SLOTS(md);
120 			continue;
121 		}
122 
123 		target = round_up(max(md->phys_addr, alloc_min), align) + target_slot * align;
124 		pages = size / EFI_PAGE_SIZE;
125 
126 		status = efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS,
127 				     memory_type, pages, &target);
128 		if (status == EFI_SUCCESS)
129 			*addr = target;
130 		break;
131 	}
132 
133 	efi_bs_call(free_pool, map);
134 
135 	return status;
136 }
137