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