xref: /freebsd/stand/efi/loader/copy.c (revision 6032b6ba9596927aba15a8004ade521a593a7d58)
1ca987d46SWarner Losh /*-
2ca987d46SWarner Losh  * Copyright (c) 2013 The FreeBSD Foundation
3ca987d46SWarner Losh  * All rights reserved.
4ca987d46SWarner Losh  *
5ca987d46SWarner Losh  * This software was developed by Benno Rice under sponsorship from
6ca987d46SWarner Losh  * the FreeBSD Foundation.
7ca987d46SWarner Losh  * Redistribution and use in source and binary forms, with or without
8ca987d46SWarner Losh  * modification, are permitted provided that the following conditions
9ca987d46SWarner Losh  * are met:
10ca987d46SWarner Losh  * 1. Redistributions of source code must retain the above copyright
11ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer.
12ca987d46SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
13ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
14ca987d46SWarner Losh  *    documentation and/or other materials provided with the distribution.
15ca987d46SWarner Losh  *
16ca987d46SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17ca987d46SWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18ca987d46SWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19ca987d46SWarner Losh  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20ca987d46SWarner Losh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21ca987d46SWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22ca987d46SWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23ca987d46SWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24ca987d46SWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25ca987d46SWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26ca987d46SWarner Losh  * SUCH DAMAGE.
27ca987d46SWarner Losh  */
28ca987d46SWarner Losh 
29ca987d46SWarner Losh #include <sys/cdefs.h>
30ca987d46SWarner Losh __FBSDID("$FreeBSD$");
31ca987d46SWarner Losh 
32ca987d46SWarner Losh #include <sys/param.h>
33ca987d46SWarner Losh 
34ca987d46SWarner Losh #include <stand.h>
35ca987d46SWarner Losh #include <bootstrap.h>
36ca987d46SWarner Losh 
37ca987d46SWarner Losh #include <efi.h>
38ca987d46SWarner Losh #include <efilib.h>
39ca987d46SWarner Losh 
40ca987d46SWarner Losh #include "loader_efi.h"
41ca987d46SWarner Losh 
42f75caed6SKonstantin Belousov #define	M(x)	((x) * 1024 * 1024)
43f75caed6SKonstantin Belousov #define	G(x)	(1UL * (x) * 1024 * 1024 * 1024)
44f75caed6SKonstantin Belousov 
45f75caed6SKonstantin Belousov extern int boot_services_gone;
46f75caed6SKonstantin Belousov 
47ca987d46SWarner Losh #if defined(__i386__) || defined(__amd64__)
48ca987d46SWarner Losh #include <machine/cpufunc.h>
49ca987d46SWarner Losh #include <machine/specialreg.h>
50fd2ef8efSMaxim Sobolev #include <machine/vmparam.h>
51ca987d46SWarner Losh 
52ca987d46SWarner Losh /*
53ca987d46SWarner Losh  * The code is excerpted from sys/x86/x86/identcpu.c: identify_cpu(),
54ca987d46SWarner Losh  * identify_hypervisor(), and dev/hyperv/vmbus/hyperv.c: hyperv_identify().
55ca987d46SWarner Losh  */
56ca987d46SWarner Losh #define CPUID_LEAF_HV_MAXLEAF		0x40000000
57ca987d46SWarner Losh #define CPUID_LEAF_HV_INTERFACE		0x40000001
58ca987d46SWarner Losh #define CPUID_LEAF_HV_FEATURES		0x40000003
59ca987d46SWarner Losh #define CPUID_LEAF_HV_LIMITS		0x40000005
60ca987d46SWarner Losh #define CPUID_HV_IFACE_HYPERV		0x31237648	/* HV#1 */
61ca987d46SWarner Losh #define CPUID_HV_MSR_TIME_REFCNT	0x0002	/* MSR_HV_TIME_REF_COUNT */
62ca987d46SWarner Losh #define CPUID_HV_MSR_HYPERCALL		0x0020
63ca987d46SWarner Losh 
64ca987d46SWarner Losh static int
65ca987d46SWarner Losh running_on_hyperv(void)
66ca987d46SWarner Losh {
67ca987d46SWarner Losh 	char hv_vendor[16];
68ca987d46SWarner Losh 	uint32_t regs[4];
69ca987d46SWarner Losh 
70ca987d46SWarner Losh 	do_cpuid(1, regs);
71ca987d46SWarner Losh 	if ((regs[2] & CPUID2_HV) == 0)
72ca987d46SWarner Losh 		return (0);
73ca987d46SWarner Losh 
74ca987d46SWarner Losh 	do_cpuid(CPUID_LEAF_HV_MAXLEAF, regs);
75ca987d46SWarner Losh 	if (regs[0] < CPUID_LEAF_HV_LIMITS)
76ca987d46SWarner Losh 		return (0);
77ca987d46SWarner Losh 
78ca987d46SWarner Losh 	((uint32_t *)&hv_vendor)[0] = regs[1];
79ca987d46SWarner Losh 	((uint32_t *)&hv_vendor)[1] = regs[2];
80ca987d46SWarner Losh 	((uint32_t *)&hv_vendor)[2] = regs[3];
81ca987d46SWarner Losh 	hv_vendor[12] = '\0';
82ca987d46SWarner Losh 	if (strcmp(hv_vendor, "Microsoft Hv") != 0)
83ca987d46SWarner Losh 		return (0);
84ca987d46SWarner Losh 
85ca987d46SWarner Losh 	do_cpuid(CPUID_LEAF_HV_INTERFACE, regs);
86ca987d46SWarner Losh 	if (regs[0] != CPUID_HV_IFACE_HYPERV)
87ca987d46SWarner Losh 		return (0);
88ca987d46SWarner Losh 
89ca987d46SWarner Losh 	do_cpuid(CPUID_LEAF_HV_FEATURES, regs);
90ca987d46SWarner Losh 	if ((regs[0] & CPUID_HV_MSR_HYPERCALL) == 0)
91ca987d46SWarner Losh 		return (0);
92ca987d46SWarner Losh 	if ((regs[0] & CPUID_HV_MSR_TIME_REFCNT) == 0)
93ca987d46SWarner Losh 		return (0);
94ca987d46SWarner Losh 
95ca987d46SWarner Losh 	return (1);
96ca987d46SWarner Losh }
97ca987d46SWarner Losh 
98ca987d46SWarner Losh static void
99ca987d46SWarner Losh efi_verify_staging_size(unsigned long *nr_pages)
100ca987d46SWarner Losh {
101ca987d46SWarner Losh 	UINTN sz;
102ce37b71eSRebecca Cran 	EFI_MEMORY_DESCRIPTOR *map = NULL, *p;
103ca987d46SWarner Losh 	EFI_PHYSICAL_ADDRESS start, end;
104ca987d46SWarner Losh 	UINTN key, dsz;
105ca987d46SWarner Losh 	UINT32 dver;
106ca987d46SWarner Losh 	EFI_STATUS status;
107ca987d46SWarner Losh 	int i, ndesc;
108ca987d46SWarner Losh 	unsigned long available_pages = 0;
109ca987d46SWarner Losh 
110ca987d46SWarner Losh 	sz = 0;
111ce37b71eSRebecca Cran 
112ce37b71eSRebecca Cran 	for (;;) {
113ce37b71eSRebecca Cran 		status = BS->GetMemoryMap(&sz, map, &key, &dsz, &dver);
114ce37b71eSRebecca Cran 		if (!EFI_ERROR(status))
115ce37b71eSRebecca Cran 			break;
116ce37b71eSRebecca Cran 
117ca987d46SWarner Losh 		if (status != EFI_BUFFER_TOO_SMALL) {
118ce37b71eSRebecca Cran 			printf("Can't read memory map: %lu\n",
119ce37b71eSRebecca Cran 			    EFI_ERROR_CODE(status));
120ce37b71eSRebecca Cran 			goto out;
121ca987d46SWarner Losh 		}
122ca987d46SWarner Losh 
123ce37b71eSRebecca Cran 		free(map);
124ce37b71eSRebecca Cran 
125ce37b71eSRebecca Cran 		/* Allocate 10 descriptors more than the size reported,
126ce37b71eSRebecca Cran 		 * to allow for any fragmentation caused by calling
127ce37b71eSRebecca Cran 		 * malloc */
128ce37b71eSRebecca Cran 		map = malloc(sz + (10 * dsz));
129ce37b71eSRebecca Cran 		if (map == NULL) {
130ce37b71eSRebecca Cran 			printf("Unable to allocate memory\n");
131ca987d46SWarner Losh 			goto out;
132ca987d46SWarner Losh 		}
133ce37b71eSRebecca Cran 	}
134ca987d46SWarner Losh 
135ca987d46SWarner Losh 	ndesc = sz / dsz;
136ca987d46SWarner Losh 	for (i = 0, p = map; i < ndesc;
137ca987d46SWarner Losh 	     i++, p = NextMemoryDescriptor(p, dsz)) {
138ca987d46SWarner Losh 		start = p->PhysicalStart;
139ca987d46SWarner Losh 		end = start + p->NumberOfPages * EFI_PAGE_SIZE;
140ca987d46SWarner Losh 
141fd2ef8efSMaxim Sobolev 		if (KERNLOAD < start || KERNLOAD >= end)
142ca987d46SWarner Losh 			continue;
143ca987d46SWarner Losh 
144ca987d46SWarner Losh 		available_pages = p->NumberOfPages -
145fd2ef8efSMaxim Sobolev 			((KERNLOAD - start) >> EFI_PAGE_SHIFT);
146ca987d46SWarner Losh 		break;
147ca987d46SWarner Losh 	}
148ca987d46SWarner Losh 
149ca987d46SWarner Losh 	if (available_pages == 0) {
150ca987d46SWarner Losh 		printf("Can't find valid memory map for staging area!\n");
151ca987d46SWarner Losh 		goto out;
152ca987d46SWarner Losh 	}
153ca987d46SWarner Losh 
154ca987d46SWarner Losh 	i++;
155ca987d46SWarner Losh 	p = NextMemoryDescriptor(p, dsz);
156ca987d46SWarner Losh 
157ca987d46SWarner Losh 	for ( ; i < ndesc;
158ca987d46SWarner Losh 	     i++, p = NextMemoryDescriptor(p, dsz)) {
159ca987d46SWarner Losh 		if (p->Type != EfiConventionalMemory &&
160ca987d46SWarner Losh 		    p->Type != EfiLoaderData)
161ca987d46SWarner Losh 			break;
162ca987d46SWarner Losh 
163ca987d46SWarner Losh 		if (p->PhysicalStart != end)
164ca987d46SWarner Losh 			break;
165ca987d46SWarner Losh 
166ca987d46SWarner Losh 		end = p->PhysicalStart + p->NumberOfPages * EFI_PAGE_SIZE;
167ca987d46SWarner Losh 
168ca987d46SWarner Losh 		available_pages += p->NumberOfPages;
169ca987d46SWarner Losh 	}
170ca987d46SWarner Losh 
171ca987d46SWarner Losh 	if (*nr_pages > available_pages) {
172ca987d46SWarner Losh 		printf("Staging area's size is reduced: %ld -> %ld!\n",
173ca987d46SWarner Losh 		    *nr_pages, available_pages);
174ca987d46SWarner Losh 		*nr_pages = available_pages;
175ca987d46SWarner Losh 	}
176ca987d46SWarner Losh out:
177ca987d46SWarner Losh 	free(map);
178ca987d46SWarner Losh }
179ca987d46SWarner Losh #endif /* __i386__ || __amd64__ */
180ca987d46SWarner Losh 
181ca987d46SWarner Losh #ifndef EFI_STAGING_SIZE
1824d6047edSWarner Losh #if defined(__arm__)
183f75caed6SKonstantin Belousov #define	EFI_STAGING_SIZE	M(32)
18494e8f7c6SRebecca Cran #else
185f75caed6SKonstantin Belousov #define	EFI_STAGING_SIZE	M(64)
186ca987d46SWarner Losh #endif
18794e8f7c6SRebecca Cran #endif
188ca987d46SWarner Losh 
189f75caed6SKonstantin Belousov #if defined(__aarch64__) || defined(__amd64__) || defined(__arm__) || \
190f75caed6SKonstantin Belousov     defined(__riscv)
191f75caed6SKonstantin Belousov #define	EFI_STAGING_2M_ALIGN	1
192f75caed6SKonstantin Belousov #else
193f75caed6SKonstantin Belousov #define	EFI_STAGING_2M_ALIGN	0
194f75caed6SKonstantin Belousov #endif
195f75caed6SKonstantin Belousov 
196f75caed6SKonstantin Belousov #if defined(__amd64__)
197f75caed6SKonstantin Belousov #define	EFI_STAGING_SLOP	M(8)
198f75caed6SKonstantin Belousov #else
199f75caed6SKonstantin Belousov #define	EFI_STAGING_SLOP	0
200f75caed6SKonstantin Belousov #endif
201f75caed6SKonstantin Belousov 
202f75caed6SKonstantin Belousov static u_long staging_slop = EFI_STAGING_SLOP;
203f75caed6SKonstantin Belousov 
204e6bb174cSAndrew Turner EFI_PHYSICAL_ADDRESS	staging, staging_end, staging_base;
205ca987d46SWarner Losh int			stage_offset_set = 0;
206ca987d46SWarner Losh ssize_t			stage_offset;
207ca987d46SWarner Losh 
208f75caed6SKonstantin Belousov static void
209f75caed6SKonstantin Belousov efi_copy_free(void)
210f75caed6SKonstantin Belousov {
211f75caed6SKonstantin Belousov 	BS->FreePages(staging_base, (staging_end - staging_base) /
212f75caed6SKonstantin Belousov 	    EFI_PAGE_SIZE);
213f75caed6SKonstantin Belousov 	stage_offset_set = 0;
214f75caed6SKonstantin Belousov 	stage_offset = 0;
215f75caed6SKonstantin Belousov }
216f75caed6SKonstantin Belousov 
217f75caed6SKonstantin Belousov #ifdef __amd64__
218*6032b6baSKonstantin Belousov int copy_staging = COPY_STAGING_AUTO;
219f75caed6SKonstantin Belousov 
220f75caed6SKonstantin Belousov static int
221f75caed6SKonstantin Belousov command_copy_staging(int argc, char *argv[])
222f75caed6SKonstantin Belousov {
223f75caed6SKonstantin Belousov 	static const char *const mode[3] = {
224f75caed6SKonstantin Belousov 		[COPY_STAGING_ENABLE] = "enable",
225f75caed6SKonstantin Belousov 		[COPY_STAGING_DISABLE] = "disable",
226f75caed6SKonstantin Belousov 		[COPY_STAGING_AUTO] = "auto",
227f75caed6SKonstantin Belousov 	};
228f75caed6SKonstantin Belousov 	int prev, res;
229f75caed6SKonstantin Belousov 
230f75caed6SKonstantin Belousov 	res = CMD_OK;
231f75caed6SKonstantin Belousov 	if (argc > 2) {
232f75caed6SKonstantin Belousov 		res = CMD_ERROR;
233f75caed6SKonstantin Belousov 	} else if (argc == 2) {
234f75caed6SKonstantin Belousov 		prev = copy_staging;
235f75caed6SKonstantin Belousov 		if (strcmp(argv[1], "enable") == 0)
236f75caed6SKonstantin Belousov 			copy_staging = COPY_STAGING_ENABLE;
237f75caed6SKonstantin Belousov 		else if (strcmp(argv[1], "disable") == 0)
238f75caed6SKonstantin Belousov 			copy_staging = COPY_STAGING_DISABLE;
239f75caed6SKonstantin Belousov 		else if (strcmp(argv[1], "auto") == 0)
240f75caed6SKonstantin Belousov 			copy_staging = COPY_STAGING_AUTO;
241f75caed6SKonstantin Belousov 		else {
242f75caed6SKonstantin Belousov 			printf("usage: copy_staging enable|disable|auto\n");
243f75caed6SKonstantin Belousov 			res = CMD_ERROR;
244f75caed6SKonstantin Belousov 		}
245f75caed6SKonstantin Belousov 		if (res == CMD_OK && prev != copy_staging) {
246f75caed6SKonstantin Belousov 			printf("changed copy_staging, unloading kernel\n");
247f75caed6SKonstantin Belousov 			unload();
248f75caed6SKonstantin Belousov 			efi_copy_free();
249f75caed6SKonstantin Belousov 			efi_copy_init();
250f75caed6SKonstantin Belousov 		}
251f75caed6SKonstantin Belousov 	} else {
252f75caed6SKonstantin Belousov 		printf("copy staging: %s\n", mode[copy_staging]);
253f75caed6SKonstantin Belousov 	}
254f75caed6SKonstantin Belousov 	return (res);
255f75caed6SKonstantin Belousov }
256f75caed6SKonstantin Belousov COMMAND_SET(copy_staging, "copy_staging", "copy staging", command_copy_staging);
257f75caed6SKonstantin Belousov #endif
258f75caed6SKonstantin Belousov 
259f75caed6SKonstantin Belousov static int
260f75caed6SKonstantin Belousov command_staging_slop(int argc, char *argv[])
261f75caed6SKonstantin Belousov {
262f75caed6SKonstantin Belousov 	char *endp;
263f75caed6SKonstantin Belousov 	u_long new, prev;
264f75caed6SKonstantin Belousov 	int res;
265f75caed6SKonstantin Belousov 
266f75caed6SKonstantin Belousov 	res = CMD_OK;
267f75caed6SKonstantin Belousov 	if (argc > 2) {
268f75caed6SKonstantin Belousov 		res = CMD_ERROR;
269f75caed6SKonstantin Belousov 	} else if (argc == 2) {
270f75caed6SKonstantin Belousov 		new = strtoul(argv[1], &endp, 0);
271f75caed6SKonstantin Belousov 		if (*endp != '\0') {
272f75caed6SKonstantin Belousov 			printf("invalid slop value\n");
273f75caed6SKonstantin Belousov 			res = CMD_ERROR;
274f75caed6SKonstantin Belousov 		}
275f75caed6SKonstantin Belousov 		if (res == CMD_OK && staging_slop != new) {
276f75caed6SKonstantin Belousov 			printf("changed slop, unloading kernel\n");
277f75caed6SKonstantin Belousov 			unload();
278f75caed6SKonstantin Belousov 			efi_copy_free();
279f75caed6SKonstantin Belousov 			efi_copy_init();
280f75caed6SKonstantin Belousov 		}
281f75caed6SKonstantin Belousov 	} else {
282f75caed6SKonstantin Belousov 		printf("staging slop %#lx\n", staging_slop);
283f75caed6SKonstantin Belousov 	}
284f75caed6SKonstantin Belousov 	return (res);
285f75caed6SKonstantin Belousov }
286f75caed6SKonstantin Belousov COMMAND_SET(staging_slop, "staging_slop", "set staging slop",
287f75caed6SKonstantin Belousov     command_staging_slop);
288f75caed6SKonstantin Belousov 
289f75caed6SKonstantin Belousov #if defined(__i386__) || defined(__amd64__)
290f75caed6SKonstantin Belousov /*
291f75caed6SKonstantin Belousov  * The staging area must reside in the the first 1GB or 4GB physical
292f75caed6SKonstantin Belousov  * memory: see elf64_exec() in
293f75caed6SKonstantin Belousov  * boot/efi/loader/arch/amd64/elf64_freebsd.c.
294f75caed6SKonstantin Belousov  */
295f75caed6SKonstantin Belousov static EFI_PHYSICAL_ADDRESS
296f75caed6SKonstantin Belousov get_staging_max(void)
297f75caed6SKonstantin Belousov {
298f75caed6SKonstantin Belousov 	EFI_PHYSICAL_ADDRESS res;
299f75caed6SKonstantin Belousov 
300f75caed6SKonstantin Belousov #if defined(__i386__)
301f75caed6SKonstantin Belousov 	res = G(1);
302f75caed6SKonstantin Belousov #elif defined(__amd64__)
303f75caed6SKonstantin Belousov 	res = copy_staging == COPY_STAGING_ENABLE ? G(1) : G(4);
304f75caed6SKonstantin Belousov #endif
305f75caed6SKonstantin Belousov 	return (res);
306f75caed6SKonstantin Belousov }
307f75caed6SKonstantin Belousov #define	EFI_ALLOC_METHOD	AllocateMaxAddress
308f75caed6SKonstantin Belousov #else
309f75caed6SKonstantin Belousov #define	EFI_ALLOC_METHOD	AllocateAnyPages
310f75caed6SKonstantin Belousov #endif
311f75caed6SKonstantin Belousov 
312ca987d46SWarner Losh int
313ca987d46SWarner Losh efi_copy_init(void)
314ca987d46SWarner Losh {
315ca987d46SWarner Losh 	EFI_STATUS	status;
316ca987d46SWarner Losh 	unsigned long nr_pages;
317ca987d46SWarner Losh 
318f75caed6SKonstantin Belousov 	nr_pages = EFI_SIZE_TO_PAGES((EFI_STAGING_SIZE));
319ca987d46SWarner Losh 
320ca987d46SWarner Losh #if defined(__i386__) || defined(__amd64__)
321ca987d46SWarner Losh 	/*
322ca987d46SWarner Losh 	 * We'll decrease nr_pages, if it's too big. Currently we only
323ca987d46SWarner Losh 	 * apply this to FreeBSD VM running on Hyper-V. Why? Please see
324ca987d46SWarner Losh 	 * https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=211746#c28
325ca987d46SWarner Losh 	 */
326ca987d46SWarner Losh 	if (running_on_hyperv())
327ca987d46SWarner Losh 		efi_verify_staging_size(&nr_pages);
328ca987d46SWarner Losh 
329f75caed6SKonstantin Belousov 	staging = get_staging_max();
330ca987d46SWarner Losh #endif
331f75caed6SKonstantin Belousov 	status = BS->AllocatePages(EFI_ALLOC_METHOD, EfiLoaderData,
332f75caed6SKonstantin Belousov 	    nr_pages, &staging);
333ca987d46SWarner Losh 	if (EFI_ERROR(status)) {
334ca987d46SWarner Losh 		printf("failed to allocate staging area: %lu\n",
335ca987d46SWarner Losh 		    EFI_ERROR_CODE(status));
336ca987d46SWarner Losh 		return (status);
337ca987d46SWarner Losh 	}
338e6bb174cSAndrew Turner 	staging_base = staging;
339ca987d46SWarner Losh 	staging_end = staging + nr_pages * EFI_PAGE_SIZE;
340ca987d46SWarner Losh 
341f75caed6SKonstantin Belousov #if EFI_STAGING_2M_ALIGN
342ca987d46SWarner Losh 	/*
343ca987d46SWarner Losh 	 * Round the kernel load address to a 2MiB value. This is needed
344ca987d46SWarner Losh 	 * because the kernel builds a page table based on where it has
345ca987d46SWarner Losh 	 * been loaded in physical address space. As the kernel will use
346ca987d46SWarner Losh 	 * either a 1MiB or 2MiB page for this we need to make sure it
347ca987d46SWarner Losh 	 * is correctly aligned for both cases.
348ca987d46SWarner Losh 	 */
349f75caed6SKonstantin Belousov 	staging = roundup2(staging, M(2));
350ca987d46SWarner Losh #endif
351ca987d46SWarner Losh 
352ca987d46SWarner Losh 	return (0);
353ca987d46SWarner Losh }
354ca987d46SWarner Losh 
355e6bb174cSAndrew Turner static bool
356e6bb174cSAndrew Turner efi_check_space(vm_offset_t end)
357e6bb174cSAndrew Turner {
358f75caed6SKonstantin Belousov 	EFI_PHYSICAL_ADDRESS addr, new_base, new_staging;
359e6bb174cSAndrew Turner 	EFI_STATUS status;
360e6bb174cSAndrew Turner 	unsigned long nr_pages;
361e6bb174cSAndrew Turner 
362f75caed6SKonstantin Belousov 	end = roundup2(end, EFI_PAGE_SIZE);
363f75caed6SKonstantin Belousov 
364e6bb174cSAndrew Turner 	/* There is already enough space */
365f75caed6SKonstantin Belousov 	if (end + staging_slop <= staging_end)
366e6bb174cSAndrew Turner 		return (true);
367e6bb174cSAndrew Turner 
368f75caed6SKonstantin Belousov 	if (boot_services_gone) {
369f75caed6SKonstantin Belousov 		if (end <= staging_end)
370f75caed6SKonstantin Belousov 			return (true);
371f75caed6SKonstantin Belousov 		panic("efi_check_space: cannot expand staging area "
372f75caed6SKonstantin Belousov 		    "after boot services were exited\n");
373f75caed6SKonstantin Belousov 	}
374e6bb174cSAndrew Turner 
375f75caed6SKonstantin Belousov 	/*
376f75caed6SKonstantin Belousov 	 * Add slop at the end:
377f75caed6SKonstantin Belousov 	 * 1. amd64 kernel expects to do some very early allocations
378f75caed6SKonstantin Belousov 	 *    by carving out memory after kernend.  Slop guarantees
379f75caed6SKonstantin Belousov 	 *    that it does not ovewrite anything useful.
380f75caed6SKonstantin Belousov 	 * 2. It seems that initial calculation of the staging size
381f75caed6SKonstantin Belousov 	 *    could be somewhat smaller than actually copying in after
382f75caed6SKonstantin Belousov 	 *    boot services are exited.  Slop avoids calling
383f75caed6SKonstantin Belousov 	 *    BS->AllocatePages() when it cannot work.
384f75caed6SKonstantin Belousov 	 */
385f75caed6SKonstantin Belousov 	end += staging_slop;
386f75caed6SKonstantin Belousov 
387f75caed6SKonstantin Belousov 	nr_pages = EFI_SIZE_TO_PAGES(end - staging_end);
388e6bb174cSAndrew Turner #if defined(__i386__) || defined(__amd64__)
389f75caed6SKonstantin Belousov 	/*
390f75caed6SKonstantin Belousov 	 * i386 needs all memory to be allocated under the 1G boundary.
391f75caed6SKonstantin Belousov 	 * amd64 needs all memory to be allocated under the 1G or 4G boundary.
392f75caed6SKonstantin Belousov 	 */
393f75caed6SKonstantin Belousov 	if (end > get_staging_max())
394e6bb174cSAndrew Turner 		goto before_staging;
395e6bb174cSAndrew Turner #endif
396e6bb174cSAndrew Turner 
397e6bb174cSAndrew Turner 	/* Try to allocate more space after the previous allocation */
398e6bb174cSAndrew Turner 	addr = staging_end;
399e6bb174cSAndrew Turner 	status = BS->AllocatePages(AllocateAddress, EfiLoaderData, nr_pages,
400e6bb174cSAndrew Turner 	    &addr);
401e6bb174cSAndrew Turner 	if (!EFI_ERROR(status)) {
402e6bb174cSAndrew Turner 		staging_end = staging_end + nr_pages * EFI_PAGE_SIZE;
403e6bb174cSAndrew Turner 		return (true);
404e6bb174cSAndrew Turner 	}
405e6bb174cSAndrew Turner 
406e6bb174cSAndrew Turner before_staging:
407e6bb174cSAndrew Turner 	/* Try allocating space before the previous allocation */
408f75caed6SKonstantin Belousov 	if (staging < nr_pages * EFI_PAGE_SIZE)
409f75caed6SKonstantin Belousov 		goto expand;
410e6bb174cSAndrew Turner 	addr = staging - nr_pages * EFI_PAGE_SIZE;
411f75caed6SKonstantin Belousov #if EFI_STAGING_2M_ALIGN
412e6bb174cSAndrew Turner 	/* See efi_copy_init for why this is needed */
413f75caed6SKonstantin Belousov 	addr = rounddown2(addr, M(2));
414e6bb174cSAndrew Turner #endif
415e6bb174cSAndrew Turner 	nr_pages = EFI_SIZE_TO_PAGES(staging_base - addr);
416e6bb174cSAndrew Turner 	status = BS->AllocatePages(AllocateAddress, EfiLoaderData, nr_pages,
417e6bb174cSAndrew Turner 	    &addr);
418e6bb174cSAndrew Turner 	if (!EFI_ERROR(status)) {
419e6bb174cSAndrew Turner 		/*
420e6bb174cSAndrew Turner 		 * Move the old allocation and update the state so
421e6bb174cSAndrew Turner 		 * translation still works.
422e6bb174cSAndrew Turner 		 */
423e6bb174cSAndrew Turner 		staging_base = addr;
42467dc6bedSJohn Baldwin 		memmove((void *)(uintptr_t)staging_base,
42567dc6bedSJohn Baldwin 		    (void *)(uintptr_t)staging, staging_end - staging);
426f75caed6SKonstantin Belousov 		stage_offset -= staging - staging_base;
427e6bb174cSAndrew Turner 		staging = staging_base;
428e6bb174cSAndrew Turner 		return (true);
429e6bb174cSAndrew Turner 	}
430e6bb174cSAndrew Turner 
431f75caed6SKonstantin Belousov expand:
432f75caed6SKonstantin Belousov 	nr_pages = EFI_SIZE_TO_PAGES(end - (vm_offset_t)staging);
433f75caed6SKonstantin Belousov #if EFI_STAGING_2M_ALIGN
434f75caed6SKonstantin Belousov 	nr_pages += M(2) / EFI_PAGE_SIZE;
435f75caed6SKonstantin Belousov #endif
436f75caed6SKonstantin Belousov #if defined(__i386__) || defined(__amd64__)
437f75caed6SKonstantin Belousov 	new_base = get_staging_max();
438f75caed6SKonstantin Belousov #endif
439f75caed6SKonstantin Belousov 	status = BS->AllocatePages(EFI_ALLOC_METHOD, EfiLoaderData,
440f75caed6SKonstantin Belousov 	    nr_pages, &new_base);
441f75caed6SKonstantin Belousov 	if (!EFI_ERROR(status)) {
442f75caed6SKonstantin Belousov #if EFI_STAGING_2M_ALIGN
443f75caed6SKonstantin Belousov 		new_staging = roundup2(new_base, M(2));
444f75caed6SKonstantin Belousov #else
445f75caed6SKonstantin Belousov 		new_staging = new_base;
446f75caed6SKonstantin Belousov #endif
447f75caed6SKonstantin Belousov 		/*
448f75caed6SKonstantin Belousov 		 * Move the old allocation and update the state so
449f75caed6SKonstantin Belousov 		 * translation still works.
450f75caed6SKonstantin Belousov 		 */
451f75caed6SKonstantin Belousov 		memcpy((void *)(uintptr_t)new_staging,
452f75caed6SKonstantin Belousov 		    (void *)(uintptr_t)staging, staging_end - staging);
453f75caed6SKonstantin Belousov 		BS->FreePages(staging_base, (staging_end - staging_base) /
454f75caed6SKonstantin Belousov 		    EFI_PAGE_SIZE);
455f75caed6SKonstantin Belousov 		stage_offset -= staging - new_staging;
456f75caed6SKonstantin Belousov 		staging = new_staging;
457f75caed6SKonstantin Belousov 		staging_end = new_base + nr_pages * EFI_PAGE_SIZE;
458f75caed6SKonstantin Belousov 		staging_base = new_base;
459f75caed6SKonstantin Belousov 		return (true);
460f75caed6SKonstantin Belousov 	}
461f75caed6SKonstantin Belousov 
462e6bb174cSAndrew Turner 	printf("efi_check_space: Unable to expand staging area\n");
463e6bb174cSAndrew Turner 	return (false);
464e6bb174cSAndrew Turner }
465e6bb174cSAndrew Turner 
466ca987d46SWarner Losh void *
467ca987d46SWarner Losh efi_translate(vm_offset_t ptr)
468ca987d46SWarner Losh {
469ca987d46SWarner Losh 
470ca987d46SWarner Losh 	return ((void *)(ptr + stage_offset));
471ca987d46SWarner Losh }
472ca987d46SWarner Losh 
473ca987d46SWarner Losh ssize_t
474ca987d46SWarner Losh efi_copyin(const void *src, vm_offset_t dest, const size_t len)
475ca987d46SWarner Losh {
476ca987d46SWarner Losh 
477ca987d46SWarner Losh 	if (!stage_offset_set) {
478ca987d46SWarner Losh 		stage_offset = (vm_offset_t)staging - dest;
479ca987d46SWarner Losh 		stage_offset_set = 1;
480ca987d46SWarner Losh 	}
481ca987d46SWarner Losh 
482ca987d46SWarner Losh 	/* XXX: Callers do not check for failure. */
483e6bb174cSAndrew Turner 	if (!efi_check_space(dest + stage_offset + len)) {
484ca987d46SWarner Losh 		errno = ENOMEM;
485ca987d46SWarner Losh 		return (-1);
486ca987d46SWarner Losh 	}
487ca987d46SWarner Losh 	bcopy(src, (void *)(dest + stage_offset), len);
488ca987d46SWarner Losh 	return (len);
489ca987d46SWarner Losh }
490ca987d46SWarner Losh 
491ca987d46SWarner Losh ssize_t
492ca987d46SWarner Losh efi_copyout(const vm_offset_t src, void *dest, const size_t len)
493ca987d46SWarner Losh {
494ca987d46SWarner Losh 
495ca987d46SWarner Losh 	/* XXX: Callers do not check for failure. */
496ca987d46SWarner Losh 	if (src + stage_offset + len > staging_end) {
497ca987d46SWarner Losh 		errno = ENOMEM;
498ca987d46SWarner Losh 		return (-1);
499ca987d46SWarner Losh 	}
500ca987d46SWarner Losh 	bcopy((void *)(src + stage_offset), dest, len);
501ca987d46SWarner Losh 	return (len);
502ca987d46SWarner Losh }
503ca987d46SWarner Losh 
504ca987d46SWarner Losh ssize_t
505afc571b1SSimon J. Gerraty efi_readin(readin_handle_t fd, vm_offset_t dest, const size_t len)
506ca987d46SWarner Losh {
507ca987d46SWarner Losh 
5082192efc0SMitchell Horne 	if (!stage_offset_set) {
5092192efc0SMitchell Horne 		stage_offset = (vm_offset_t)staging - dest;
5102192efc0SMitchell Horne 		stage_offset_set = 1;
5112192efc0SMitchell Horne 	}
5122192efc0SMitchell Horne 
513e6bb174cSAndrew Turner 	if (!efi_check_space(dest + stage_offset + len)) {
514ca987d46SWarner Losh 		errno = ENOMEM;
515ca987d46SWarner Losh 		return (-1);
516ca987d46SWarner Losh 	}
517afc571b1SSimon J. Gerraty 	return (VECTX_READ(fd, (void *)(dest + stage_offset), len));
518ca987d46SWarner Losh }
519ca987d46SWarner Losh 
520ca987d46SWarner Losh void
521ca987d46SWarner Losh efi_copy_finish(void)
522ca987d46SWarner Losh {
523ca987d46SWarner Losh 	uint64_t	*src, *dst, *last;
524ca987d46SWarner Losh 
5250b600ec4SJohn Baldwin 	src = (uint64_t *)(uintptr_t)staging;
5260b600ec4SJohn Baldwin 	dst = (uint64_t *)(uintptr_t)(staging - stage_offset);
5270b600ec4SJohn Baldwin 	last = (uint64_t *)(uintptr_t)staging_end;
528ca987d46SWarner Losh 
529ca987d46SWarner Losh 	while (src < last)
530ca987d46SWarner Losh 		*dst++ = *src++;
531ca987d46SWarner Losh }
532f75caed6SKonstantin Belousov 
533f75caed6SKonstantin Belousov void
534f75caed6SKonstantin Belousov efi_copy_finish_nop(void)
535f75caed6SKonstantin Belousov {
536f75caed6SKonstantin Belousov }
537