xref: /freebsd/stand/efi/loader/arch/i386/elf64_freebsd.c (revision f8ca5d45c3c1829759ecd87cb95d53e5ab7d0811)
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 #define __ELF_WORD_SIZE 64
29 #include <sys/param.h>
30 #include <sys/linker.h>
31 #include <machine/elf.h>
32 
33 #include <efi.h>
34 #include <efilib.h>
35 
36 #include "bootstrap.h"
37 
38 #include "loader_efi.h"
39 
40 extern int bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp,
41     bool exit_bs);
42 
43 static int	elf64_exec(struct preloaded_file *amp);
44 static int	elf64_obj_exec(struct preloaded_file *amp);
45 
46 static struct file_format amd64_elf = {
47 	.l_load = elf64_loadfile,
48 	.l_exec = elf64_exec
49 };
50 
51 static struct file_format amd64_elf_obj = {
52 	.l_load = elf64_obj_loadfile,
53 	.l_exec = elf64_obj_exec
54 };
55 
56 struct file_format *file_formats[] = {
57 	&amd64_elf,
58 	&amd64_elf_obj,
59 	NULL
60 };
61 
62 struct gdtr {
63 	uint16_t size;
64 	uint64_t ptr;
65 } __packed;
66 
67 #define PG_V	0x001
68 #define PG_RW	0x002
69 #define PG_PS	0x080
70 
71 #define GDT_P	0x00800000000000
72 #define GDT_E	0x00080000000000
73 #define GDT_S	0x00100000000000
74 #define GDT_RW	0x00020000000000
75 #define GDT_L	0x20000000000000
76 
77 #define M(x)	((x) * 1024 * 1024)
78 #define G(x)	(1ULL * (x) * 1024 * 1024 * 1024)
79 
80 typedef uint64_t p4_entry_t;
81 typedef uint64_t p3_entry_t;
82 typedef uint64_t p2_entry_t;
83 typedef uint64_t gdt_t;
84 
85 static p4_entry_t *PT4;
86 static p3_entry_t *PT3;
87 static p3_entry_t *PT3_l, *PT3_u;
88 static p2_entry_t *PT2;
89 static p2_entry_t *PT2_l0, *PT2_l1, *PT2_l2, *PT2_l3, *PT2_u0, *PT2_u1;
90 static gdt_t *GDT;
91 
92 extern EFI_PHYSICAL_ADDRESS staging;
93 
94 static void (*trampoline)(uint32_t stack, void *copy_finish, uint32_t kernend,
95     uint32_t modulep, uint64_t *pagetable, struct gdtr *gdtr, uint64_t entry);
96 
97 extern void *amd64_tramp;
98 extern uint32_t amd64_tramp_size;
99 
100 /*
101  * There is an ELF kernel and one or more ELF modules loaded.
102  * We wish to start executing the kernel image, so make such
103  * preparations as are required, and do so.
104  */
105 static int
elf64_exec(struct preloaded_file * fp)106 elf64_exec(struct preloaded_file *fp)
107 {
108 	EFI_PHYSICAL_ADDRESS	ptr;
109 	EFI_ALLOCATE_TYPE	type;
110 	EFI_STATUS		err;
111 	struct file_metadata	*md;
112 	struct gdtr		*gdtr;
113 	Elf_Ehdr 		*ehdr;
114 	vm_offset_t		modulep, kernend, trampstack;
115 	int i;
116 
117 	switch (copy_staging) {
118 	case COPY_STAGING_ENABLE:
119 		type = AllocateMaxAddress;
120 		break;
121 	case COPY_STAGING_DISABLE:
122 		type = AllocateAnyPages;
123 		break;
124 	case COPY_STAGING_AUTO:
125 		type = fp->f_kernphys_relocatable ?
126 		    AllocateAnyPages : AllocateMaxAddress;
127 		break;
128 	}
129 
130 	if ((md = file_findmetadata(fp, MODINFOMD_ELFHDR)) == NULL)
131 		return (EFTYPE);
132 	ehdr = (Elf_Ehdr *)&(md->md_data);
133 
134 	/*
135 	 * Make our temporary stack 32 bytes big, which is
136 	 * a little more than we need.
137 	 */
138 	ptr = G(1);
139 	err = BS->AllocatePages(type, EfiLoaderCode,
140 	    EFI_SIZE_TO_PAGES(amd64_tramp_size + 32), &ptr);
141 	if (EFI_ERROR(err)) {
142 		printf("Unable to allocate trampoline\n");
143 		return (ENOMEM);
144 	}
145 
146 	trampoline = (void *)(uintptr_t)ptr;
147 	trampstack = ptr + amd64_tramp_size + 32;
148 	bcopy(&amd64_tramp, trampoline, amd64_tramp_size);
149 
150 	ptr = G(1);
151 	err = BS->AllocatePages(type, EfiLoaderData,
152 	    EFI_SIZE_TO_PAGES(sizeof(struct gdtr) + sizeof(uint64_t) * 2), &ptr);
153 	if (EFI_ERROR(err)) {
154 		printf("Unable to allocate GDT\n");
155 		BS->FreePages((uintptr_t)trampoline, 1);
156 		return (ENOMEM);
157 	}
158 	GDT = (gdt_t *)(uintptr_t)ptr;
159 	GDT[1] = GDT_P | GDT_E | GDT_S | GDT_RW | GDT_L; /* CS */
160 	GDT[0] = 0;
161 	gdtr = (struct gdtr *)&GDT[2];
162 	gdtr->size = sizeof(uint64_t) * 2 - 1;
163 	gdtr->ptr = (uintptr_t)GDT;
164 
165 	if (type == AllocateMaxAddress) {
166 		/* Copy staging enabled */
167 
168 		ptr = G(1);
169 		err = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData,
170 		    EFI_SIZE_TO_PAGES(512 * 3 * sizeof(uint64_t)), &ptr);
171 		if (EFI_ERROR(err)) {
172 			printf("Unable to allocate trampoline page table\n");
173 			BS->FreePages((uintptr_t)trampoline, 1);
174 			BS->FreePages((uintptr_t)GDT, 1);
175 			return (ENOMEM);
176 		}
177 		PT4 = (p4_entry_t *)(uintptr_t)ptr;
178 
179 		PT3 = &PT4[512];
180 		PT2 = &PT3[512];
181 
182 		/*
183 		 * This is kinda brutal, but every single 1GB VM
184 		 * memory segment points to the same first 1GB of
185 		 * physical memory.  But it is more than adequate.
186 		 */
187 		for (i = 0; i < 512; i++) {
188 			/*
189 			 * Each slot of the L4 pages points to the
190 			 * same L3 page.
191 			 */
192 			PT4[i] = (uintptr_t)PT3 | PG_V | PG_RW;
193 
194 			/*
195 			 * Each slot of the L3 pages points to the
196 			 * same L2 page.
197 			 */
198 			PT3[i] = (uintptr_t)PT2 | PG_V | PG_RW;
199 
200 			/*
201 			 * The L2 page slots are mapped with 2MB pages for 1GB.
202 			 */
203 			PT2[i] = (i * M(2)) | PG_V | PG_RW | PG_PS;
204 		}
205 	} else {
206 		err = BS->AllocatePages(AllocateAnyPages, EfiLoaderData,
207 		    EFI_SIZE_TO_PAGES(512 * 9 * sizeof(uint64_t)), &ptr);
208 		if (EFI_ERROR(err)) {
209 			printf("Unable to allocate trampoline page table\n");
210 			BS->FreePages((uintptr_t)trampoline, 1);
211 			BS->FreePages((uintptr_t)GDT, 1);
212 			return (ENOMEM);
213 		}
214 		PT4 = (p4_entry_t *)(uintptr_t)ptr;
215 
216 		PT3_l = &PT4[512];
217 		PT3_u = &PT3_l[512];
218 		PT2_l0 = &PT3_u[512];
219 		PT2_l1 = &PT2_l0[512];
220 		PT2_l2 = &PT2_l1[512];
221 		PT2_l3 = &PT2_l2[512];
222 		PT2_u0 = &PT2_l3[512];
223 		PT2_u1 = &PT2_u0[512];
224 
225 		/* 1:1 mapping of lower 4G */
226 		PT4[0] = (uintptr_t)PT3_l | PG_V | PG_RW;
227 		PT3_l[0] = (uintptr_t)PT2_l0 | PG_V | PG_RW;
228 		PT3_l[1] = (uintptr_t)PT2_l1 | PG_V | PG_RW;
229 		PT3_l[2] = (uintptr_t)PT2_l2 | PG_V | PG_RW;
230 		PT3_l[3] = (uintptr_t)PT2_l3 | PG_V | PG_RW;
231 		for (i = 0; i < 2048; i++) {
232 			PT2_l0[i] = ((p2_entry_t)i * M(2)) | PG_V | PG_RW | PG_PS;
233 		}
234 
235 		/* mapping of kernel 2G below top */
236 		PT4[511] = (uintptr_t)PT3_u | PG_V | PG_RW;
237 		PT3_u[511] = (uintptr_t)PT2_u1 | PG_V | PG_RW;
238 		PT3_u[510] = (uintptr_t)PT2_u0 | PG_V | PG_RW;
239 		/* compat mapping of phys @0 */
240 		PT2_u0[0] = PG_PS | PG_V | PG_RW;
241 		/* this maps past staging area */
242 		for (i = 1; i < 1024; i++) {
243 			PT2_u0[i] = (staging + (i - 1) * M(2))
244 			| PG_V | PG_RW | PG_PS;
245 		}
246 	}
247 
248 	printf(
249 	    "staging %#llx (%scopying) tramp %p PT4 %p GDT %p\n"
250 	    "Start @ %#llx ...\n", staging,
251 	    type == AllocateMaxAddress ? "" : "not ", trampoline, PT4, GDT,
252 	    ehdr->e_entry
253 	);
254 
255 	efi_time_fini();
256 	err = bi_load(fp->f_args, &modulep, &kernend, true);
257 	if (err != 0) {
258 		efi_time_init();
259 		return (err);
260 	}
261 
262 	dev_cleanup();
263 
264 	trampoline(trampstack, type == AllocateMaxAddress ? efi_copy_finish :
265 	    efi_copy_finish_nop, kernend, modulep, PT4, gdtr, ehdr->e_entry);
266 
267 	panic("exec returned");
268 }
269 
270 static int
elf64_obj_exec(struct preloaded_file * fp)271 elf64_obj_exec(struct preloaded_file *fp)
272 {
273 	return (EFTYPE);
274 }
275