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 #define __ELF_WORD_SIZE 64
30 #include <sys/param.h>
31 #include <sys/exec.h>
32 #include <sys/linker.h>
33 #include <string.h>
34 #include <machine/elf.h>
35 #include <stand.h>
36 #include <vm/vm.h>
37 #include <vm/pmap.h>
38 
39 #include <efi.h>
40 #include <efilib.h>
41 
42 #include "bootstrap.h"
43 
44 #include "platform/acfreebsd.h"
45 #include "acconfig.h"
46 #define ACPI_SYSTEM_XFACE
47 #include "actypes.h"
48 #include "actbl.h"
49 
50 #include "loader_efi.h"
51 
52 static EFI_GUID acpi_guid = ACPI_TABLE_GUID;
53 static EFI_GUID acpi20_guid = ACPI_20_TABLE_GUID;
54 
55 extern int bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp,
56     bool exit_bs);
57 
58 static int	elf64_exec(struct preloaded_file *amp);
59 static int	elf64_obj_exec(struct preloaded_file *amp);
60 
61 static struct file_format amd64_elf = {
62 	.l_load = elf64_loadfile,
63 	.l_exec = elf64_exec,
64 };
65 static struct file_format amd64_elf_obj = {
66 	.l_load = elf64_obj_loadfile,
67 	.l_exec = elf64_obj_exec,
68 };
69 
70 extern struct file_format multiboot2;
71 extern struct file_format multiboot2_obj;
72 
73 struct file_format *file_formats[] = {
74 	&multiboot2,
75 	&multiboot2_obj,
76 	&amd64_elf,
77 	&amd64_elf_obj,
78 	NULL
79 };
80 
81 static pml4_entry_t *PT4;
82 static pdp_entry_t *PT3;
83 static pdp_entry_t *PT3_l, *PT3_u;
84 static pd_entry_t *PT2;
85 static pd_entry_t *PT2_l0, *PT2_l1, *PT2_l2, *PT2_l3, *PT2_u0, *PT2_u1;
86 
87 extern EFI_PHYSICAL_ADDRESS staging;
88 
89 static void (*trampoline)(uint64_t stack, void *copy_finish, uint64_t kernend,
90     uint64_t modulep, pml4_entry_t *pagetable, uint64_t entry);
91 
92 extern uintptr_t amd64_tramp;
93 extern uint32_t amd64_tramp_size;
94 
95 /*
96  * There is an ELF kernel and one or more ELF modules loaded.
97  * We wish to start executing the kernel image, so make such
98  * preparations as are required, and do so.
99  */
100 static int
101 elf64_exec(struct preloaded_file *fp)
102 {
103 	struct file_metadata	*md;
104 	Elf_Ehdr 		*ehdr;
105 	vm_offset_t		modulep, kernend, trampcode, trampstack;
106 	int			err, i;
107 	ACPI_TABLE_RSDP		*rsdp;
108 	char			buf[24];
109 	int			revision;
110 	bool			copy_auto;
111 
112 	copy_auto = copy_staging == COPY_STAGING_AUTO;
113 	if (copy_auto)
114 		copy_staging = fp->f_kernphys_relocatable ?
115 		    COPY_STAGING_DISABLE : COPY_STAGING_ENABLE;
116 
117 	/*
118 	 * Report the RSDP to the kernel. While this can be found with
119 	 * a BIOS boot, the RSDP may be elsewhere when booted from UEFI.
120 	 */
121 
122 	rsdp = efi_get_table(&acpi20_guid);
123 	if (rsdp == NULL) {
124 		rsdp = efi_get_table(&acpi_guid);
125 	}
126 	if (rsdp != NULL) {
127 		sprintf(buf, "0x%016llx", (unsigned long long)rsdp);
128 		setenv("acpi.rsdp", buf, 1);
129 		revision = rsdp->Revision;
130 		if (revision == 0)
131 			revision = 1;
132 		sprintf(buf, "%d", revision);
133 		setenv("acpi.revision", buf, 1);
134 		strncpy(buf, rsdp->OemId, sizeof(rsdp->OemId));
135 		buf[sizeof(rsdp->OemId)] = '\0';
136 		setenv("acpi.oem", buf, 1);
137 		sprintf(buf, "0x%016x", rsdp->RsdtPhysicalAddress);
138 		setenv("acpi.rsdt", buf, 1);
139 		if (revision >= 2) {
140 			/* XXX extended checksum? */
141 			sprintf(buf, "0x%016llx",
142 			    (unsigned long long)rsdp->XsdtPhysicalAddress);
143 			setenv("acpi.xsdt", buf, 1);
144 			sprintf(buf, "%d", rsdp->Length);
145 			setenv("acpi.xsdt_length", buf, 1);
146 		}
147 	}
148 
149 	if ((md = file_findmetadata(fp, MODINFOMD_ELFHDR)) == NULL)
150 		return (EFTYPE);
151 	ehdr = (Elf_Ehdr *)&(md->md_data);
152 
153 	trampcode = copy_staging == COPY_STAGING_ENABLE ?
154 	    (vm_offset_t)0x0000000040000000 /* 1G */ :
155 	    (vm_offset_t)0x0000000100000000; /* 4G */;
156 	err = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData, 1,
157 	    (EFI_PHYSICAL_ADDRESS *)&trampcode);
158 	if (EFI_ERROR(err)) {
159 		printf("Unable to allocate trampoline\n");
160 		if (copy_auto)
161 			copy_staging = COPY_STAGING_AUTO;
162 		return (ENOMEM);
163 	}
164 	bzero((void *)trampcode, EFI_PAGE_SIZE);
165 	trampstack = trampcode + EFI_PAGE_SIZE - 8;
166 	bcopy((void *)&amd64_tramp, (void *)trampcode, amd64_tramp_size);
167 	trampoline = (void *)trampcode;
168 
169 	if (copy_staging == COPY_STAGING_ENABLE) {
170 		PT4 = (pml4_entry_t *)0x0000000040000000; /* 1G */
171 		err = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData, 3,
172 		    (EFI_PHYSICAL_ADDRESS *)&PT4);
173 		if (EFI_ERROR(err)) {
174 			printf("Unable to allocate trampoline page table\n");
175 			BS->FreePages(trampcode, 1);
176 			if (copy_auto)
177 				copy_staging = COPY_STAGING_AUTO;
178 			return (ENOMEM);
179 		}
180 		bzero(PT4, 3 * EFI_PAGE_SIZE);
181 		PT3 = &PT4[512];
182 		PT2 = &PT3[512];
183 
184 		/*
185 		 * This is kinda brutal, but every single 1GB VM
186 		 * memory segment points to the same first 1GB of
187 		 * physical memory.  But it is more than adequate.
188 		 */
189 		for (i = 0; i < NPTEPG; i++) {
190 			/*
191 			 * Each slot of the L4 pages points to the
192 			 * same L3 page.
193 			 */
194 			PT4[i] = (pml4_entry_t)PT3;
195 			PT4[i] |= PG_V | PG_RW;
196 
197 			/*
198 			 * Each slot of the L3 pages points to the
199 			 * same L2 page.
200 			 */
201 			PT3[i] = (pdp_entry_t)PT2;
202 			PT3[i] |= PG_V | PG_RW;
203 
204 			/*
205 			 * The L2 page slots are mapped with 2MB pages for 1GB.
206 			 */
207 			PT2[i] = (pd_entry_t)i * (2 * 1024 * 1024);
208 			PT2[i] |= PG_V | PG_RW | PG_PS;
209 		}
210 	} else {
211 		PT4 = (pml4_entry_t *)0x0000000100000000; /* 4G */
212 		err = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData, 9,
213 		    (EFI_PHYSICAL_ADDRESS *)&PT4);
214 		if (EFI_ERROR(err)) {
215 			printf("Unable to allocate trampoline page table\n");
216 			BS->FreePages(trampcode, 9);
217 			if (copy_auto)
218 				copy_staging = COPY_STAGING_AUTO;
219 			return (ENOMEM);
220 		}
221 
222 		bzero(PT4, 9 * EFI_PAGE_SIZE);
223 
224 		PT3_l = &PT4[NPML4EPG * 1];
225 		PT3_u = &PT4[NPML4EPG * 2];
226 		PT2_l0 = &PT4[NPML4EPG * 3];
227 		PT2_l1 = &PT4[NPML4EPG * 4];
228 		PT2_l2 = &PT4[NPML4EPG * 5];
229 		PT2_l3 = &PT4[NPML4EPG * 6];
230 		PT2_u0 = &PT4[NPML4EPG * 7];
231 		PT2_u1 = &PT4[NPML4EPG * 8];
232 
233 		/* 1:1 mapping of lower 4G */
234 		PT4[0] = (pml4_entry_t)PT3_l | PG_V | PG_RW;
235 		PT3_l[0] = (pdp_entry_t)PT2_l0 | PG_V | PG_RW;
236 		PT3_l[1] = (pdp_entry_t)PT2_l1 | PG_V | PG_RW;
237 		PT3_l[2] = (pdp_entry_t)PT2_l2 | PG_V | PG_RW;
238 		PT3_l[3] = (pdp_entry_t)PT2_l3 | PG_V | PG_RW;
239 		for (i = 0; i < 4 * NPDEPG; i++) {
240 			PT2_l0[i] = ((pd_entry_t)i << PDRSHIFT) | PG_V |
241 			    PG_RW | PG_PS;
242 		}
243 
244 		/* mapping of kernel 2G below top */
245 		PT4[NPML4EPG - 1] = (pml4_entry_t)PT3_u | PG_V | PG_RW;
246 		PT3_u[NPDPEPG - 2] = (pdp_entry_t)PT2_u0 | PG_V | PG_RW;
247 		PT3_u[NPDPEPG - 1] = (pdp_entry_t)PT2_u1 | PG_V | PG_RW;
248 		/* compat mapping of phys @0 */
249 		PT2_u0[0] = PG_PS | PG_V | PG_RW;
250 		/* this maps past staging area */
251 		for (i = 1; i < 2 * NPDEPG; i++) {
252 			PT2_u0[i] = ((pd_entry_t)staging +
253 			    ((pd_entry_t)i - 1) * NBPDR) |
254 			    PG_V | PG_RW | PG_PS;
255 		}
256 	}
257 
258 	printf("staging %#lx (%scopying) tramp %p PT4 %p\n",
259 	    staging, copy_staging == COPY_STAGING_ENABLE ? "" : "not ",
260 	    trampoline, PT4);
261 	printf("Start @ 0x%lx ...\n", ehdr->e_entry);
262 
263 	efi_time_fini();
264 	err = bi_load(fp->f_args, &modulep, &kernend, true);
265 	if (err != 0) {
266 		efi_time_init();
267 		if (copy_auto)
268 			copy_staging = COPY_STAGING_AUTO;
269 		return (err);
270 	}
271 
272 	dev_cleanup();
273 
274 	trampoline(trampstack, copy_staging == COPY_STAGING_ENABLE ?
275 	    efi_copy_finish : efi_copy_finish_nop, kernend, modulep,
276 	    PT4, ehdr->e_entry);
277 
278 	panic("exec returned");
279 }
280 
281 static int
282 elf64_obj_exec(struct preloaded_file *fp)
283 {
284 
285 	return (EFTYPE);
286 }
287