xref: /freebsd/stand/efi/loader/copy.c (revision 9e5787d2284e187abb5b654d924394a65772e004)
1 /*-
2  * Copyright (c) 2013 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Benno Rice under sponsorship from
6  * the FreeBSD Foundation.
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 
34 #include <stand.h>
35 #include <bootstrap.h>
36 
37 #include <efi.h>
38 #include <efilib.h>
39 
40 #include "loader_efi.h"
41 
42 #if defined(__i386__) || defined(__amd64__)
43 #include <machine/cpufunc.h>
44 #include <machine/specialreg.h>
45 
46 /*
47  * The code is excerpted from sys/x86/x86/identcpu.c: identify_cpu(),
48  * identify_hypervisor(), and dev/hyperv/vmbus/hyperv.c: hyperv_identify().
49  */
50 #define CPUID_LEAF_HV_MAXLEAF		0x40000000
51 #define CPUID_LEAF_HV_INTERFACE		0x40000001
52 #define CPUID_LEAF_HV_FEATURES		0x40000003
53 #define CPUID_LEAF_HV_LIMITS		0x40000005
54 #define CPUID_HV_IFACE_HYPERV		0x31237648	/* HV#1 */
55 #define CPUID_HV_MSR_TIME_REFCNT	0x0002	/* MSR_HV_TIME_REF_COUNT */
56 #define CPUID_HV_MSR_HYPERCALL		0x0020
57 
58 static int
59 running_on_hyperv(void)
60 {
61 	char hv_vendor[16];
62 	uint32_t regs[4];
63 
64 	do_cpuid(1, regs);
65 	if ((regs[2] & CPUID2_HV) == 0)
66 		return (0);
67 
68 	do_cpuid(CPUID_LEAF_HV_MAXLEAF, regs);
69 	if (regs[0] < CPUID_LEAF_HV_LIMITS)
70 		return (0);
71 
72 	((uint32_t *)&hv_vendor)[0] = regs[1];
73 	((uint32_t *)&hv_vendor)[1] = regs[2];
74 	((uint32_t *)&hv_vendor)[2] = regs[3];
75 	hv_vendor[12] = '\0';
76 	if (strcmp(hv_vendor, "Microsoft Hv") != 0)
77 		return (0);
78 
79 	do_cpuid(CPUID_LEAF_HV_INTERFACE, regs);
80 	if (regs[0] != CPUID_HV_IFACE_HYPERV)
81 		return (0);
82 
83 	do_cpuid(CPUID_LEAF_HV_FEATURES, regs);
84 	if ((regs[0] & CPUID_HV_MSR_HYPERCALL) == 0)
85 		return (0);
86 	if ((regs[0] & CPUID_HV_MSR_TIME_REFCNT) == 0)
87 		return (0);
88 
89 	return (1);
90 }
91 
92 #define KERNEL_PHYSICAL_BASE (2*1024*1024)
93 
94 static void
95 efi_verify_staging_size(unsigned long *nr_pages)
96 {
97 	UINTN sz;
98 	EFI_MEMORY_DESCRIPTOR *map = NULL, *p;
99 	EFI_PHYSICAL_ADDRESS start, end;
100 	UINTN key, dsz;
101 	UINT32 dver;
102 	EFI_STATUS status;
103 	int i, ndesc;
104 	unsigned long available_pages = 0;
105 
106 	sz = 0;
107 
108 	for (;;) {
109 		status = BS->GetMemoryMap(&sz, map, &key, &dsz, &dver);
110 		if (!EFI_ERROR(status))
111 			break;
112 
113 		if (status != EFI_BUFFER_TOO_SMALL) {
114 			printf("Can't read memory map: %lu\n",
115 			    EFI_ERROR_CODE(status));
116 			goto out;
117 		}
118 
119 		free(map);
120 
121 		/* Allocate 10 descriptors more than the size reported,
122 		 * to allow for any fragmentation caused by calling
123 		 * malloc */
124 		map = malloc(sz + (10 * dsz));
125 		if (map == NULL) {
126 			printf("Unable to allocate memory\n");
127 			goto out;
128 		}
129 	}
130 
131 	ndesc = sz / dsz;
132 	for (i = 0, p = map; i < ndesc;
133 	     i++, p = NextMemoryDescriptor(p, dsz)) {
134 		start = p->PhysicalStart;
135 		end = start + p->NumberOfPages * EFI_PAGE_SIZE;
136 
137 		if (KERNEL_PHYSICAL_BASE < start ||
138 		    KERNEL_PHYSICAL_BASE >= end)
139 			continue;
140 
141 		available_pages = p->NumberOfPages -
142 			((KERNEL_PHYSICAL_BASE - start) >> EFI_PAGE_SHIFT);
143 		break;
144 	}
145 
146 	if (available_pages == 0) {
147 		printf("Can't find valid memory map for staging area!\n");
148 		goto out;
149 	}
150 
151 	i++;
152 	p = NextMemoryDescriptor(p, dsz);
153 
154 	for ( ; i < ndesc;
155 	     i++, p = NextMemoryDescriptor(p, dsz)) {
156 		if (p->Type != EfiConventionalMemory &&
157 		    p->Type != EfiLoaderData)
158 			break;
159 
160 		if (p->PhysicalStart != end)
161 			break;
162 
163 		end = p->PhysicalStart + p->NumberOfPages * EFI_PAGE_SIZE;
164 
165 		available_pages += p->NumberOfPages;
166 	}
167 
168 	if (*nr_pages > available_pages) {
169 		printf("Staging area's size is reduced: %ld -> %ld!\n",
170 		    *nr_pages, available_pages);
171 		*nr_pages = available_pages;
172 	}
173 out:
174 	free(map);
175 }
176 #endif /* __i386__ || __amd64__ */
177 
178 #ifndef EFI_STAGING_SIZE
179 #if defined(__amd64__)
180 #define	EFI_STAGING_SIZE	100
181 #elif defined(__arm__)
182 #define	EFI_STAGING_SIZE	32
183 #else
184 #define	EFI_STAGING_SIZE	64
185 #endif
186 #endif
187 
188 EFI_PHYSICAL_ADDRESS	staging, staging_end, staging_base;
189 int			stage_offset_set = 0;
190 ssize_t			stage_offset;
191 
192 int
193 efi_copy_init(void)
194 {
195 	EFI_STATUS	status;
196 
197 	unsigned long nr_pages;
198 
199 	nr_pages = EFI_SIZE_TO_PAGES((EFI_STAGING_SIZE) * 1024 * 1024);
200 
201 #if defined(__i386__) || defined(__amd64__)
202 	/*
203 	 * We'll decrease nr_pages, if it's too big. Currently we only
204 	 * apply this to FreeBSD VM running on Hyper-V. Why? Please see
205 	 * https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=211746#c28
206 	 */
207 	if (running_on_hyperv())
208 		efi_verify_staging_size(&nr_pages);
209 
210 	/*
211 	 * The staging area must reside in the the first 1GB physical
212 	 * memory: see elf64_exec() in
213 	 * boot/efi/loader/arch/amd64/elf64_freebsd.c.
214 	 */
215 	staging = 1024*1024*1024;
216 	status = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData,
217 	    nr_pages, &staging);
218 #else
219 	status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData,
220 	    nr_pages, &staging);
221 #endif
222 	if (EFI_ERROR(status)) {
223 		printf("failed to allocate staging area: %lu\n",
224 		    EFI_ERROR_CODE(status));
225 		return (status);
226 	}
227 	staging_base = staging;
228 	staging_end = staging + nr_pages * EFI_PAGE_SIZE;
229 
230 #if defined(__aarch64__) || defined(__arm__) || defined(__riscv)
231 	/*
232 	 * Round the kernel load address to a 2MiB value. This is needed
233 	 * because the kernel builds a page table based on where it has
234 	 * been loaded in physical address space. As the kernel will use
235 	 * either a 1MiB or 2MiB page for this we need to make sure it
236 	 * is correctly aligned for both cases.
237 	 */
238 	staging = roundup2(staging, 2 * 1024 * 1024);
239 #endif
240 
241 	return (0);
242 }
243 
244 static bool
245 efi_check_space(vm_offset_t end)
246 {
247 	EFI_PHYSICAL_ADDRESS addr;
248 	EFI_STATUS status;
249 	unsigned long nr_pages;
250 
251 	/* There is already enough space */
252 	if (end <= staging_end)
253 		return (true);
254 
255 	end = roundup2(end, EFI_PAGE_SIZE);
256 	nr_pages = EFI_SIZE_TO_PAGES(end - staging_end);
257 
258 #if defined(__i386__) || defined(__amd64__)
259 	/* X86 needs all memory to be allocated under the 1G boundary */
260 	if (end > 1024*1024*1024)
261 		goto before_staging;
262 #endif
263 
264 	/* Try to allocate more space after the previous allocation */
265 	addr = staging_end;
266 	status = BS->AllocatePages(AllocateAddress, EfiLoaderData, nr_pages,
267 	    &addr);
268 	if (!EFI_ERROR(status)) {
269 		staging_end = staging_end + nr_pages * EFI_PAGE_SIZE;
270 		return (true);
271 	}
272 
273 before_staging:
274 	/* Try allocating space before the previous allocation */
275 	if (staging < nr_pages * EFI_PAGE_SIZE) {
276 		printf("Not enough space before allocation\n");
277 		return (false);
278 	}
279 	addr = staging - nr_pages * EFI_PAGE_SIZE;
280 #if defined(__aarch64__) || defined(__arm__) || defined(__riscv)
281 	/* See efi_copy_init for why this is needed */
282 	addr = rounddown2(addr, 2 * 1024 * 1024);
283 #endif
284 	nr_pages = EFI_SIZE_TO_PAGES(staging_base - addr);
285 	status = BS->AllocatePages(AllocateAddress, EfiLoaderData, nr_pages,
286 	    &addr);
287 	if (!EFI_ERROR(status)) {
288 		/*
289 		 * Move the old allocation and update the state so
290 		 * translation still works.
291 		 */
292 		staging_base = addr;
293 		memmove((void *)staging_base, (void *)staging,
294 		    staging_end - staging);
295 		stage_offset -= (staging - staging_base);
296 		staging = staging_base;
297 		return (true);
298 	}
299 
300 	printf("efi_check_space: Unable to expand staging area\n");
301 	return (false);
302 }
303 
304 void *
305 efi_translate(vm_offset_t ptr)
306 {
307 
308 	return ((void *)(ptr + stage_offset));
309 }
310 
311 ssize_t
312 efi_copyin(const void *src, vm_offset_t dest, const size_t len)
313 {
314 
315 	if (!stage_offset_set) {
316 		stage_offset = (vm_offset_t)staging - dest;
317 		stage_offset_set = 1;
318 	}
319 
320 	/* XXX: Callers do not check for failure. */
321 	if (!efi_check_space(dest + stage_offset + len)) {
322 		errno = ENOMEM;
323 		return (-1);
324 	}
325 	bcopy(src, (void *)(dest + stage_offset), len);
326 	return (len);
327 }
328 
329 ssize_t
330 efi_copyout(const vm_offset_t src, void *dest, const size_t len)
331 {
332 
333 	/* XXX: Callers do not check for failure. */
334 	if (src + stage_offset + len > staging_end) {
335 		errno = ENOMEM;
336 		return (-1);
337 	}
338 	bcopy((void *)(src + stage_offset), dest, len);
339 	return (len);
340 }
341 
342 
343 ssize_t
344 efi_readin(readin_handle_t fd, vm_offset_t dest, const size_t len)
345 {
346 
347 	if (!stage_offset_set) {
348 		stage_offset = (vm_offset_t)staging - dest;
349 		stage_offset_set = 1;
350 	}
351 
352 	if (!efi_check_space(dest + stage_offset + len)) {
353 		errno = ENOMEM;
354 		return (-1);
355 	}
356 	return (VECTX_READ(fd, (void *)(dest + stage_offset), len));
357 }
358 
359 void
360 efi_copy_finish(void)
361 {
362 	uint64_t	*src, *dst, *last;
363 
364 	src = (uint64_t *)(uintptr_t)staging;
365 	dst = (uint64_t *)(uintptr_t)(staging - stage_offset);
366 	last = (uint64_t *)(uintptr_t)staging_end;
367 
368 	while (src < last)
369 		*dst++ = *src++;
370 }
371