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