xref: /freebsd/stand/efi/loader/copy.c (revision 3b216bfb6cce24aa84519315138be8d23ac5d613)
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 #include <machine/vmparam.h>
46 
47 /*
48  * The code is excerpted from sys/x86/x86/identcpu.c: identify_cpu(),
49  * identify_hypervisor(), and dev/hyperv/vmbus/hyperv.c: hyperv_identify().
50  */
51 #define CPUID_LEAF_HV_MAXLEAF		0x40000000
52 #define CPUID_LEAF_HV_INTERFACE		0x40000001
53 #define CPUID_LEAF_HV_FEATURES		0x40000003
54 #define CPUID_LEAF_HV_LIMITS		0x40000005
55 #define CPUID_HV_IFACE_HYPERV		0x31237648	/* HV#1 */
56 #define CPUID_HV_MSR_TIME_REFCNT	0x0002	/* MSR_HV_TIME_REF_COUNT */
57 #define CPUID_HV_MSR_HYPERCALL		0x0020
58 
59 static int
60 running_on_hyperv(void)
61 {
62 	char hv_vendor[16];
63 	uint32_t regs[4];
64 
65 	do_cpuid(1, regs);
66 	if ((regs[2] & CPUID2_HV) == 0)
67 		return (0);
68 
69 	do_cpuid(CPUID_LEAF_HV_MAXLEAF, regs);
70 	if (regs[0] < CPUID_LEAF_HV_LIMITS)
71 		return (0);
72 
73 	((uint32_t *)&hv_vendor)[0] = regs[1];
74 	((uint32_t *)&hv_vendor)[1] = regs[2];
75 	((uint32_t *)&hv_vendor)[2] = regs[3];
76 	hv_vendor[12] = '\0';
77 	if (strcmp(hv_vendor, "Microsoft Hv") != 0)
78 		return (0);
79 
80 	do_cpuid(CPUID_LEAF_HV_INTERFACE, regs);
81 	if (regs[0] != CPUID_HV_IFACE_HYPERV)
82 		return (0);
83 
84 	do_cpuid(CPUID_LEAF_HV_FEATURES, regs);
85 	if ((regs[0] & CPUID_HV_MSR_HYPERCALL) == 0)
86 		return (0);
87 	if ((regs[0] & CPUID_HV_MSR_TIME_REFCNT) == 0)
88 		return (0);
89 
90 	return (1);
91 }
92 
93 static void
94 efi_verify_staging_size(unsigned long *nr_pages)
95 {
96 	UINTN sz;
97 	EFI_MEMORY_DESCRIPTOR *map = NULL, *p;
98 	EFI_PHYSICAL_ADDRESS start, end;
99 	UINTN key, dsz;
100 	UINT32 dver;
101 	EFI_STATUS status;
102 	int i, ndesc;
103 	unsigned long available_pages = 0;
104 
105 	sz = 0;
106 
107 	for (;;) {
108 		status = BS->GetMemoryMap(&sz, map, &key, &dsz, &dver);
109 		if (!EFI_ERROR(status))
110 			break;
111 
112 		if (status != EFI_BUFFER_TOO_SMALL) {
113 			printf("Can't read memory map: %lu\n",
114 			    EFI_ERROR_CODE(status));
115 			goto out;
116 		}
117 
118 		free(map);
119 
120 		/* Allocate 10 descriptors more than the size reported,
121 		 * to allow for any fragmentation caused by calling
122 		 * malloc */
123 		map = malloc(sz + (10 * dsz));
124 		if (map == NULL) {
125 			printf("Unable to allocate memory\n");
126 			goto out;
127 		}
128 	}
129 
130 	ndesc = sz / dsz;
131 	for (i = 0, p = map; i < ndesc;
132 	     i++, p = NextMemoryDescriptor(p, dsz)) {
133 		start = p->PhysicalStart;
134 		end = start + p->NumberOfPages * EFI_PAGE_SIZE;
135 
136 		if (KERNLOAD < start || KERNLOAD >= end)
137 			continue;
138 
139 		available_pages = p->NumberOfPages -
140 			((KERNLOAD - start) >> EFI_PAGE_SHIFT);
141 		break;
142 	}
143 
144 	if (available_pages == 0) {
145 		printf("Can't find valid memory map for staging area!\n");
146 		goto out;
147 	}
148 
149 	i++;
150 	p = NextMemoryDescriptor(p, dsz);
151 
152 	for ( ; i < ndesc;
153 	     i++, p = NextMemoryDescriptor(p, dsz)) {
154 		if (p->Type != EfiConventionalMemory &&
155 		    p->Type != EfiLoaderData)
156 			break;
157 
158 		if (p->PhysicalStart != end)
159 			break;
160 
161 		end = p->PhysicalStart + p->NumberOfPages * EFI_PAGE_SIZE;
162 
163 		available_pages += p->NumberOfPages;
164 	}
165 
166 	if (*nr_pages > available_pages) {
167 		printf("Staging area's size is reduced: %ld -> %ld!\n",
168 		    *nr_pages, available_pages);
169 		*nr_pages = available_pages;
170 	}
171 out:
172 	free(map);
173 }
174 #endif /* __i386__ || __amd64__ */
175 
176 #ifndef EFI_STAGING_SIZE
177 #if defined(__arm__)
178 #define	EFI_STAGING_SIZE	32
179 #else
180 #define	EFI_STAGING_SIZE	64
181 #endif
182 #endif
183 
184 EFI_PHYSICAL_ADDRESS	staging, staging_end, staging_base;
185 int			stage_offset_set = 0;
186 ssize_t			stage_offset;
187 
188 int
189 efi_copy_init(void)
190 {
191 	EFI_STATUS	status;
192 
193 	unsigned long nr_pages;
194 
195 	nr_pages = EFI_SIZE_TO_PAGES((EFI_STAGING_SIZE) * 1024 * 1024);
196 
197 #if defined(__i386__) || defined(__amd64__)
198 	/*
199 	 * We'll decrease nr_pages, if it's too big. Currently we only
200 	 * apply this to FreeBSD VM running on Hyper-V. Why? Please see
201 	 * https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=211746#c28
202 	 */
203 	if (running_on_hyperv())
204 		efi_verify_staging_size(&nr_pages);
205 
206 	/*
207 	 * The staging area must reside in the the first 1GB physical
208 	 * memory: see elf64_exec() in
209 	 * boot/efi/loader/arch/amd64/elf64_freebsd.c.
210 	 */
211 	staging = 1024*1024*1024;
212 	status = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData,
213 	    nr_pages, &staging);
214 #else
215 	status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData,
216 	    nr_pages, &staging);
217 #endif
218 	if (EFI_ERROR(status)) {
219 		printf("failed to allocate staging area: %lu\n",
220 		    EFI_ERROR_CODE(status));
221 		return (status);
222 	}
223 	staging_base = staging;
224 	staging_end = staging + nr_pages * EFI_PAGE_SIZE;
225 
226 #if defined(__aarch64__) || defined(__arm__) || defined(__riscv)
227 	/*
228 	 * Round the kernel load address to a 2MiB value. This is needed
229 	 * because the kernel builds a page table based on where it has
230 	 * been loaded in physical address space. As the kernel will use
231 	 * either a 1MiB or 2MiB page for this we need to make sure it
232 	 * is correctly aligned for both cases.
233 	 */
234 	staging = roundup2(staging, 2 * 1024 * 1024);
235 #endif
236 
237 	return (0);
238 }
239 
240 static bool
241 efi_check_space(vm_offset_t end)
242 {
243 	EFI_PHYSICAL_ADDRESS addr;
244 	EFI_STATUS status;
245 	unsigned long nr_pages;
246 
247 	/* There is already enough space */
248 	if (end <= staging_end)
249 		return (true);
250 
251 	end = roundup2(end, EFI_PAGE_SIZE);
252 	nr_pages = EFI_SIZE_TO_PAGES(end - staging_end);
253 
254 #if defined(__i386__) || defined(__amd64__)
255 	/* X86 needs all memory to be allocated under the 1G boundary */
256 	if (end > 1024*1024*1024)
257 		goto before_staging;
258 #endif
259 
260 	/* Try to allocate more space after the previous allocation */
261 	addr = staging_end;
262 	status = BS->AllocatePages(AllocateAddress, EfiLoaderData, nr_pages,
263 	    &addr);
264 	if (!EFI_ERROR(status)) {
265 		staging_end = staging_end + nr_pages * EFI_PAGE_SIZE;
266 		return (true);
267 	}
268 
269 before_staging:
270 	/* Try allocating space before the previous allocation */
271 	if (staging < nr_pages * EFI_PAGE_SIZE) {
272 		printf("Not enough space before allocation\n");
273 		return (false);
274 	}
275 	addr = staging - nr_pages * EFI_PAGE_SIZE;
276 #if defined(__aarch64__) || defined(__arm__) || defined(__riscv)
277 	/* See efi_copy_init for why this is needed */
278 	addr = rounddown2(addr, 2 * 1024 * 1024);
279 #endif
280 	nr_pages = EFI_SIZE_TO_PAGES(staging_base - addr);
281 	status = BS->AllocatePages(AllocateAddress, EfiLoaderData, nr_pages,
282 	    &addr);
283 	if (!EFI_ERROR(status)) {
284 		/*
285 		 * Move the old allocation and update the state so
286 		 * translation still works.
287 		 */
288 		staging_base = addr;
289 		memmove((void *)(uintptr_t)staging_base,
290 		    (void *)(uintptr_t)staging, staging_end - staging);
291 		stage_offset -= (staging - staging_base);
292 		staging = staging_base;
293 		return (true);
294 	}
295 
296 	printf("efi_check_space: Unable to expand staging area\n");
297 	return (false);
298 }
299 
300 void *
301 efi_translate(vm_offset_t ptr)
302 {
303 
304 	return ((void *)(ptr + stage_offset));
305 }
306 
307 ssize_t
308 efi_copyin(const void *src, vm_offset_t dest, const size_t len)
309 {
310 
311 	if (!stage_offset_set) {
312 		stage_offset = (vm_offset_t)staging - dest;
313 		stage_offset_set = 1;
314 	}
315 
316 	/* XXX: Callers do not check for failure. */
317 	if (!efi_check_space(dest + stage_offset + len)) {
318 		errno = ENOMEM;
319 		return (-1);
320 	}
321 	bcopy(src, (void *)(dest + stage_offset), len);
322 	return (len);
323 }
324 
325 ssize_t
326 efi_copyout(const vm_offset_t src, void *dest, const size_t len)
327 {
328 
329 	/* XXX: Callers do not check for failure. */
330 	if (src + stage_offset + len > staging_end) {
331 		errno = ENOMEM;
332 		return (-1);
333 	}
334 	bcopy((void *)(src + stage_offset), dest, len);
335 	return (len);
336 }
337 
338 
339 ssize_t
340 efi_readin(readin_handle_t fd, vm_offset_t dest, const size_t len)
341 {
342 
343 	if (!stage_offset_set) {
344 		stage_offset = (vm_offset_t)staging - dest;
345 		stage_offset_set = 1;
346 	}
347 
348 	if (!efi_check_space(dest + stage_offset + len)) {
349 		errno = ENOMEM;
350 		return (-1);
351 	}
352 	return (VECTX_READ(fd, (void *)(dest + stage_offset), len));
353 }
354 
355 void
356 efi_copy_finish(void)
357 {
358 	uint64_t	*src, *dst, *last;
359 
360 	src = (uint64_t *)(uintptr_t)staging;
361 	dst = (uint64_t *)(uintptr_t)(staging - stage_offset);
362 	last = (uint64_t *)(uintptr_t)staging_end;
363 
364 	while (src < last)
365 		*dst++ = *src++;
366 }
367