xref: /freebsd/stand/efi/loader/arch/amd64/elf64_freebsd.c (revision a0b956f5ac5e0941f9e74e24c1c53e05ad061a38)
1 /*-
2  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3  * Copyright (c) 2014 The FreeBSD Foundation
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #define __ELF_WORD_SIZE 64
32 #include <sys/param.h>
33 #include <sys/exec.h>
34 #include <sys/linker.h>
35 #include <string.h>
36 #include <machine/elf.h>
37 #include <stand.h>
38 #include <vm/vm.h>
39 #include <vm/pmap.h>
40 
41 #include <efi.h>
42 #include <efilib.h>
43 
44 #include "bootstrap.h"
45 
46 #include "platform/acfreebsd.h"
47 #include "acconfig.h"
48 #define ACPI_SYSTEM_XFACE
49 #include "actypes.h"
50 #include "actbl.h"
51 
52 #include "loader_efi.h"
53 
54 static EFI_GUID acpi_guid = ACPI_TABLE_GUID;
55 static EFI_GUID acpi20_guid = ACPI_20_TABLE_GUID;
56 
57 extern int bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp,
58     bool exit_bs);
59 
60 static int	elf64_exec(struct preloaded_file *amp);
61 static int	elf64_obj_exec(struct preloaded_file *amp);
62 
63 static struct file_format amd64_elf = {
64 	.l_load = elf64_loadfile,
65 	.l_exec = elf64_exec,
66 };
67 static struct file_format amd64_elf_obj = {
68 	.l_load = elf64_obj_loadfile,
69 	.l_exec = elf64_obj_exec,
70 };
71 
72 extern struct file_format multiboot2;
73 extern struct file_format multiboot2_obj;
74 
75 struct file_format *file_formats[] = {
76 	&multiboot2,
77 	&multiboot2_obj,
78 	&amd64_elf,
79 	&amd64_elf_obj,
80 	NULL
81 };
82 
83 static pml4_entry_t *PT4;
84 static pdp_entry_t *PT3;
85 static pdp_entry_t *PT3_l, *PT3_u;
86 static pd_entry_t *PT2;
87 static pd_entry_t *PT2_l0, *PT2_l1, *PT2_l2, *PT2_l3, *PT2_u0, *PT2_u1;
88 
89 extern EFI_PHYSICAL_ADDRESS staging;
90 
91 static void (*trampoline)(uint64_t stack, void *copy_finish, uint64_t kernend,
92     uint64_t modulep, pml4_entry_t *pagetable, uint64_t entry);
93 
94 extern uintptr_t amd64_tramp;
95 extern uint32_t amd64_tramp_size;
96 
97 /*
98  * There is an ELF kernel and one or more ELF modules loaded.
99  * We wish to start executing the kernel image, so make such
100  * preparations as are required, and do so.
101  */
102 static int
103 elf64_exec(struct preloaded_file *fp)
104 {
105 	struct file_metadata	*md;
106 	Elf_Ehdr 		*ehdr;
107 	vm_offset_t		modulep, kernend, trampcode, trampstack;
108 	int			err, i;
109 	ACPI_TABLE_RSDP		*rsdp;
110 	char			buf[24];
111 	int			revision;
112 	bool			copy_auto;
113 
114 	copy_auto = copy_staging == COPY_STAGING_AUTO;
115 	if (copy_auto)
116 		copy_staging = fp->f_kernphys_relocatable ?
117 		    COPY_STAGING_DISABLE : COPY_STAGING_ENABLE;
118 
119 	/*
120 	 * Report the RSDP to the kernel. While this can be found with
121 	 * a BIOS boot, the RSDP may be elsewhere when booted from UEFI.
122 	 */
123 
124 	rsdp = efi_get_table(&acpi20_guid);
125 	if (rsdp == NULL) {
126 		rsdp = efi_get_table(&acpi_guid);
127 	}
128 	if (rsdp != NULL) {
129 		sprintf(buf, "0x%016llx", (unsigned long long)rsdp);
130 		setenv("acpi.rsdp", buf, 1);
131 		revision = rsdp->Revision;
132 		if (revision == 0)
133 			revision = 1;
134 		sprintf(buf, "%d", revision);
135 		setenv("acpi.revision", buf, 1);
136 		strncpy(buf, rsdp->OemId, sizeof(rsdp->OemId));
137 		buf[sizeof(rsdp->OemId)] = '\0';
138 		setenv("acpi.oem", buf, 1);
139 		sprintf(buf, "0x%016x", rsdp->RsdtPhysicalAddress);
140 		setenv("acpi.rsdt", buf, 1);
141 		if (revision >= 2) {
142 			/* XXX extended checksum? */
143 			sprintf(buf, "0x%016llx",
144 			    (unsigned long long)rsdp->XsdtPhysicalAddress);
145 			setenv("acpi.xsdt", buf, 1);
146 			sprintf(buf, "%d", rsdp->Length);
147 			setenv("acpi.xsdt_length", buf, 1);
148 		}
149 	}
150 
151 	if ((md = file_findmetadata(fp, MODINFOMD_ELFHDR)) == NULL)
152 		return (EFTYPE);
153 	ehdr = (Elf_Ehdr *)&(md->md_data);
154 
155 	trampcode = copy_staging == COPY_STAGING_ENABLE ?
156 	    (vm_offset_t)0x0000000040000000 /* 1G */ :
157 	    (vm_offset_t)0x0000000100000000; /* 4G */;
158 	err = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData, 1,
159 	    (EFI_PHYSICAL_ADDRESS *)&trampcode);
160 	if (EFI_ERROR(err)) {
161 		printf("Unable to allocate trampoline\n");
162 		if (copy_auto)
163 			copy_staging = COPY_STAGING_AUTO;
164 		return (ENOMEM);
165 	}
166 	bzero((void *)trampcode, EFI_PAGE_SIZE);
167 	trampstack = trampcode + EFI_PAGE_SIZE - 8;
168 	bcopy((void *)&amd64_tramp, (void *)trampcode, amd64_tramp_size);
169 	trampoline = (void *)trampcode;
170 
171 	if (copy_staging == COPY_STAGING_ENABLE) {
172 		PT4 = (pml4_entry_t *)0x0000000040000000;
173 		err = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData, 3,
174 		    (EFI_PHYSICAL_ADDRESS *)&PT4);
175 		if (EFI_ERROR(err)) {
176 			printf("Unable to allocate trampoline page table\n");
177 			BS->FreePages(trampcode, 1);
178 			if (copy_auto)
179 				copy_staging = COPY_STAGING_AUTO;
180 			return (ENOMEM);
181 		}
182 		bzero(PT4, 3 * EFI_PAGE_SIZE);
183 		PT3 = &PT4[512];
184 		PT2 = &PT3[512];
185 
186 		/*
187 		 * This is kinda brutal, but every single 1GB VM
188 		 * memory segment points to the same first 1GB of
189 		 * physical memory.  But it is more than adequate.
190 		 */
191 		for (i = 0; i < NPTEPG; i++) {
192 			/*
193 			 * Each slot of the L4 pages points to the
194 			 * same L3 page.
195 			 */
196 			PT4[i] = (pml4_entry_t)PT3;
197 			PT4[i] |= PG_V | PG_RW;
198 
199 			/*
200 			 * Each slot of the L3 pages points to the
201 			 * same L2 page.
202 			 */
203 			PT3[i] = (pdp_entry_t)PT2;
204 			PT3[i] |= PG_V | PG_RW;
205 
206 			/*
207 			 * The L2 page slots are mapped with 2MB pages for 1GB.
208 			 */
209 			PT2[i] = (pd_entry_t)i * (2 * 1024 * 1024);
210 			PT2[i] |= PG_V | PG_RW | PG_PS;
211 		}
212 	} else {
213 		PT4 = (pml4_entry_t *)0x0000000100000000; /* 4G */
214 		err = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData, 9,
215 		    (EFI_PHYSICAL_ADDRESS *)&PT4);
216 		if (EFI_ERROR(err)) {
217 			printf("Unable to allocate trampoline page table\n");
218 			BS->FreePages(trampcode, 9);
219 			if (copy_auto)
220 				copy_staging = COPY_STAGING_AUTO;
221 			return (ENOMEM);
222 		}
223 
224 		bzero(PT4, 9 * EFI_PAGE_SIZE);
225 
226 		PT3_l = &PT4[NPML4EPG * 1];
227 		PT3_u = &PT4[NPML4EPG * 2];
228 		PT2_l0 = &PT4[NPML4EPG * 3];
229 		PT2_l1 = &PT4[NPML4EPG * 4];
230 		PT2_l2 = &PT4[NPML4EPG * 5];
231 		PT2_l3 = &PT4[NPML4EPG * 6];
232 		PT2_u0 = &PT4[NPML4EPG * 7];
233 		PT2_u1 = &PT4[NPML4EPG * 8];
234 
235 		/* 1:1 mapping of lower 4G */
236 		PT4[0] = (pml4_entry_t)PT3_l | PG_V | PG_RW;
237 		PT3_l[0] = (pdp_entry_t)PT2_l0 | PG_V | PG_RW;
238 		PT3_l[1] = (pdp_entry_t)PT2_l1 | PG_V | PG_RW;
239 		PT3_l[2] = (pdp_entry_t)PT2_l2 | PG_V | PG_RW;
240 		PT3_l[3] = (pdp_entry_t)PT2_l3 | PG_V | PG_RW;
241 		for (i = 0; i < 4 * NPDEPG; i++) {
242 			PT2_l0[i] = ((pd_entry_t)i << PDRSHIFT) | PG_V |
243 			    PG_RW | PG_PS;
244 		}
245 
246 		/* mapping of kernel 2G below top */
247 		PT4[NPML4EPG - 1] = (pml4_entry_t)PT3_u | PG_V | PG_RW;
248 		PT3_u[NPDPEPG - 2] = (pdp_entry_t)PT2_u0 | PG_V | PG_RW;
249 		PT3_u[NPDPEPG - 1] = (pdp_entry_t)PT2_u1 | PG_V | PG_RW;
250 		/* compat mapping of phys @0 */
251 		PT2_u0[0] = PG_PS | PG_V | PG_RW;
252 		/* this maps past staging area */
253 		for (i = 1; i < 2 * NPDEPG; i++) {
254 			PT2_u0[i] = ((pd_entry_t)staging +
255 			    ((pd_entry_t)i - 1) * NBPDR) |
256 			    PG_V | PG_RW | PG_PS;
257 		}
258 	}
259 
260 	printf("staging %#lx (%scopying) tramp %p PT4 %p\n",
261 	    staging, copy_staging == COPY_STAGING_ENABLE ? "" : "not ",
262 	    trampoline, PT4);
263 	printf("Start @ 0x%lx ...\n", ehdr->e_entry);
264 
265 	efi_time_fini();
266 	err = bi_load(fp->f_args, &modulep, &kernend, true);
267 	if (err != 0) {
268 		efi_time_init();
269 		if (copy_auto)
270 			copy_staging = COPY_STAGING_AUTO;
271 		return (err);
272 	}
273 
274 	dev_cleanup();
275 
276 	trampoline(trampstack, copy_staging == COPY_STAGING_ENABLE ?
277 	    efi_copy_finish : efi_copy_finish_nop, kernend, modulep,
278 	    PT4, ehdr->e_entry);
279 
280 	panic("exec returned");
281 }
282 
283 static int
284 elf64_obj_exec(struct preloaded_file *fp)
285 {
286 
287 	return (EFTYPE);
288 }
289