xref: /openbmc/linux/drivers/firmware/efi/libstub/mem.c (revision 15e3ae36)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <linux/efi.h>
4 #include <asm/efi.h>
5 
6 #include "efistub.h"
7 
8 #define EFI_MMAP_NR_SLACK_SLOTS	8
9 
10 static inline bool mmap_has_headroom(unsigned long buff_size,
11 				     unsigned long map_size,
12 				     unsigned long desc_size)
13 {
14 	unsigned long slack = buff_size - map_size;
15 
16 	return slack / desc_size >= EFI_MMAP_NR_SLACK_SLOTS;
17 }
18 
19 /**
20  * efi_get_memory_map() - get memory map
21  * @map:	on return pointer to memory map
22  *
23  * Retrieve the UEFI memory map. The allocated memory leaves room for
24  * up to EFI_MMAP_NR_SLACK_SLOTS additional memory map entries.
25  *
26  * Return:	status code
27  */
28 efi_status_t efi_get_memory_map(struct efi_boot_memmap *map)
29 {
30 	efi_memory_desc_t *m = NULL;
31 	efi_status_t status;
32 	unsigned long key;
33 	u32 desc_version;
34 
35 	*map->desc_size =	sizeof(*m);
36 	*map->map_size =	*map->desc_size * 32;
37 	*map->buff_size =	*map->map_size;
38 again:
39 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
40 			     *map->map_size, (void **)&m);
41 	if (status != EFI_SUCCESS)
42 		goto fail;
43 
44 	*map->desc_size = 0;
45 	key = 0;
46 	status = efi_bs_call(get_memory_map, map->map_size, m,
47 			     &key, map->desc_size, &desc_version);
48 	if (status == EFI_BUFFER_TOO_SMALL ||
49 	    !mmap_has_headroom(*map->buff_size, *map->map_size,
50 			       *map->desc_size)) {
51 		efi_bs_call(free_pool, m);
52 		/*
53 		 * Make sure there is some entries of headroom so that the
54 		 * buffer can be reused for a new map after allocations are
55 		 * no longer permitted.  Its unlikely that the map will grow to
56 		 * exceed this headroom once we are ready to trigger
57 		 * ExitBootServices()
58 		 */
59 		*map->map_size += *map->desc_size * EFI_MMAP_NR_SLACK_SLOTS;
60 		*map->buff_size = *map->map_size;
61 		goto again;
62 	}
63 
64 	if (status == EFI_SUCCESS) {
65 		if (map->key_ptr)
66 			*map->key_ptr = key;
67 		if (map->desc_ver)
68 			*map->desc_ver = desc_version;
69 	} else {
70 		efi_bs_call(free_pool, m);
71 	}
72 
73 fail:
74 	*map->map = m;
75 	return status;
76 }
77 
78 /**
79  * efi_allocate_pages() - Allocate memory pages
80  * @size:	minimum number of bytes to allocate
81  * @addr:	On return the address of the first allocated page. The first
82  *		allocated page has alignment EFI_ALLOC_ALIGN which is an
83  *		architecture dependent multiple of the page size.
84  * @max:	the address that the last allocated memory page shall not
85  *		exceed
86  *
87  * Allocate pages as EFI_LOADER_DATA. The allocated pages are aligned according
88  * to EFI_ALLOC_ALIGN. The last allocated page will not exceed the address
89  * given by @max.
90  *
91  * Return:	status code
92  */
93 efi_status_t efi_allocate_pages(unsigned long size, unsigned long *addr,
94 				unsigned long max)
95 {
96 	efi_physical_addr_t alloc_addr = ALIGN_DOWN(max + 1, EFI_ALLOC_ALIGN) - 1;
97 	int slack = EFI_ALLOC_ALIGN / EFI_PAGE_SIZE - 1;
98 	efi_status_t status;
99 
100 	size = round_up(size, EFI_ALLOC_ALIGN);
101 	status = efi_bs_call(allocate_pages, EFI_ALLOCATE_MAX_ADDRESS,
102 			     EFI_LOADER_DATA, size / EFI_PAGE_SIZE + slack,
103 			     &alloc_addr);
104 	if (status != EFI_SUCCESS)
105 		return status;
106 
107 	*addr = ALIGN((unsigned long)alloc_addr, EFI_ALLOC_ALIGN);
108 
109 	if (slack > 0) {
110 		int l = (alloc_addr % EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
111 
112 		if (l) {
113 			efi_bs_call(free_pages, alloc_addr, slack - l + 1);
114 			slack = l - 1;
115 		}
116 		if (slack)
117 			efi_bs_call(free_pages, *addr + size, slack);
118 	}
119 	return EFI_SUCCESS;
120 }
121 /**
122  * efi_low_alloc_above() - allocate pages at or above given address
123  * @size:	size of the memory area to allocate
124  * @align:	minimum alignment of the allocated memory area. It should
125  *		a power of two.
126  * @addr:	on exit the address of the allocated memory
127  * @min:	minimum address to used for the memory allocation
128  *
129  * Allocate at the lowest possible address that is not below @min as
130  * EFI_LOADER_DATA. The allocated pages are aligned according to @align but at
131  * least EFI_ALLOC_ALIGN. The first allocated page will not below the address
132  * given by @min.
133  *
134  * Return:	status code
135  */
136 efi_status_t efi_low_alloc_above(unsigned long size, unsigned long align,
137 				 unsigned long *addr, unsigned long min)
138 {
139 	unsigned long map_size, desc_size, buff_size;
140 	efi_memory_desc_t *map;
141 	efi_status_t status;
142 	unsigned long nr_pages;
143 	int i;
144 	struct efi_boot_memmap boot_map;
145 
146 	boot_map.map		= &map;
147 	boot_map.map_size	= &map_size;
148 	boot_map.desc_size	= &desc_size;
149 	boot_map.desc_ver	= NULL;
150 	boot_map.key_ptr	= NULL;
151 	boot_map.buff_size	= &buff_size;
152 
153 	status = efi_get_memory_map(&boot_map);
154 	if (status != EFI_SUCCESS)
155 		goto fail;
156 
157 	/*
158 	 * Enforce minimum alignment that EFI or Linux requires when
159 	 * requesting a specific address.  We are doing page-based (or
160 	 * larger) allocations, and both the address and size must meet
161 	 * alignment constraints.
162 	 */
163 	if (align < EFI_ALLOC_ALIGN)
164 		align = EFI_ALLOC_ALIGN;
165 
166 	size = round_up(size, EFI_ALLOC_ALIGN);
167 	nr_pages = size / EFI_PAGE_SIZE;
168 	for (i = 0; i < map_size / desc_size; i++) {
169 		efi_memory_desc_t *desc;
170 		unsigned long m = (unsigned long)map;
171 		u64 start, end;
172 
173 		desc = efi_early_memdesc_ptr(m, desc_size, i);
174 
175 		if (desc->type != EFI_CONVENTIONAL_MEMORY)
176 			continue;
177 
178 		if (efi_soft_reserve_enabled() &&
179 		    (desc->attribute & EFI_MEMORY_SP))
180 			continue;
181 
182 		if (desc->num_pages < nr_pages)
183 			continue;
184 
185 		start = desc->phys_addr;
186 		end = start + desc->num_pages * EFI_PAGE_SIZE;
187 
188 		if (start < min)
189 			start = min;
190 
191 		start = round_up(start, align);
192 		if ((start + size) > end)
193 			continue;
194 
195 		status = efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS,
196 				     EFI_LOADER_DATA, nr_pages, &start);
197 		if (status == EFI_SUCCESS) {
198 			*addr = start;
199 			break;
200 		}
201 	}
202 
203 	if (i == map_size / desc_size)
204 		status = EFI_NOT_FOUND;
205 
206 	efi_bs_call(free_pool, map);
207 fail:
208 	return status;
209 }
210 
211 /**
212  * efi_free() - free memory pages
213  * @size:	size of the memory area to free in bytes
214  * @addr:	start of the memory area to free (must be EFI_PAGE_SIZE
215  *		aligned)
216  *
217  * @size is rounded up to a multiple of EFI_ALLOC_ALIGN which is an
218  * architecture specific multiple of EFI_PAGE_SIZE. So this function should
219  * only be used to return pages allocated with efi_allocate_pages() or
220  * efi_low_alloc_above().
221  */
222 void efi_free(unsigned long size, unsigned long addr)
223 {
224 	unsigned long nr_pages;
225 
226 	if (!size)
227 		return;
228 
229 	nr_pages = round_up(size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
230 	efi_bs_call(free_pages, addr, nr_pages);
231 }
232 
233 /**
234  * efi_relocate_kernel() - copy memory area
235  * @image_addr:		pointer to address of memory area to copy
236  * @image_size:		size of memory area to copy
237  * @alloc_size:		minimum size of memory to allocate, must be greater or
238  *			equal to image_size
239  * @preferred_addr:	preferred target address
240  * @alignment:		minimum alignment of the allocated memory area. It
241  *			should be a power of two.
242  * @min_addr:		minimum target address
243  *
244  * Copy a memory area to a newly allocated memory area aligned according
245  * to @alignment but at least EFI_ALLOC_ALIGN. If the preferred address
246  * is not available, the allocated address will not be below @min_addr.
247  * On exit, @image_addr is updated to the target copy address that was used.
248  *
249  * This function is used to copy the Linux kernel verbatim. It does not apply
250  * any relocation changes.
251  *
252  * Return:		status code
253  */
254 efi_status_t efi_relocate_kernel(unsigned long *image_addr,
255 				 unsigned long image_size,
256 				 unsigned long alloc_size,
257 				 unsigned long preferred_addr,
258 				 unsigned long alignment,
259 				 unsigned long min_addr)
260 {
261 	unsigned long cur_image_addr;
262 	unsigned long new_addr = 0;
263 	efi_status_t status;
264 	unsigned long nr_pages;
265 	efi_physical_addr_t efi_addr = preferred_addr;
266 
267 	if (!image_addr || !image_size || !alloc_size)
268 		return EFI_INVALID_PARAMETER;
269 	if (alloc_size < image_size)
270 		return EFI_INVALID_PARAMETER;
271 
272 	cur_image_addr = *image_addr;
273 
274 	/*
275 	 * The EFI firmware loader could have placed the kernel image
276 	 * anywhere in memory, but the kernel has restrictions on the
277 	 * max physical address it can run at.  Some architectures
278 	 * also have a prefered address, so first try to relocate
279 	 * to the preferred address.  If that fails, allocate as low
280 	 * as possible while respecting the required alignment.
281 	 */
282 	nr_pages = round_up(alloc_size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
283 	status = efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS,
284 			     EFI_LOADER_DATA, nr_pages, &efi_addr);
285 	new_addr = efi_addr;
286 	/*
287 	 * If preferred address allocation failed allocate as low as
288 	 * possible.
289 	 */
290 	if (status != EFI_SUCCESS) {
291 		status = efi_low_alloc_above(alloc_size, alignment, &new_addr,
292 					     min_addr);
293 	}
294 	if (status != EFI_SUCCESS) {
295 		pr_efi_err("Failed to allocate usable memory for kernel.\n");
296 		return status;
297 	}
298 
299 	/*
300 	 * We know source/dest won't overlap since both memory ranges
301 	 * have been allocated by UEFI, so we can safely use memcpy.
302 	 */
303 	memcpy((void *)new_addr, (void *)cur_image_addr, image_size);
304 
305 	/* Return the new address of the relocated image. */
306 	*image_addr = new_addr;
307 
308 	return status;
309 }
310