1ca987d46SWarner Losh /*-
2ca987d46SWarner Losh * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3ca987d46SWarner Losh * Copyright (c) 2014 The FreeBSD Foundation
4ca987d46SWarner Losh * All rights reserved.
5ca987d46SWarner Losh *
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 #define __ELF_WORD_SIZE 64
29ca987d46SWarner Losh #include <sys/param.h>
30ca987d46SWarner Losh #include <sys/exec.h>
31ca987d46SWarner Losh #include <sys/linker.h>
32ca987d46SWarner Losh #include <string.h>
33ca987d46SWarner Losh #include <machine/elf.h>
34ca987d46SWarner Losh #include <stand.h>
35ca987d46SWarner Losh #include <vm/vm.h>
36ca987d46SWarner Losh #include <vm/pmap.h>
37ca987d46SWarner Losh
38ca987d46SWarner Losh #include <efi.h>
39ca987d46SWarner Losh #include <efilib.h>
40ca987d46SWarner Losh
41ca987d46SWarner Losh #include "bootstrap.h"
42ca987d46SWarner Losh
43ca987d46SWarner Losh #include "loader_efi.h"
44ca987d46SWarner Losh
45ca987d46SWarner Losh static int elf64_exec(struct preloaded_file *amp);
46ca987d46SWarner Losh static int elf64_obj_exec(struct preloaded_file *amp);
47ca987d46SWarner Losh
48ca987d46SWarner Losh static struct file_format amd64_elf = {
49ca987d46SWarner Losh .l_load = elf64_loadfile,
50ca987d46SWarner Losh .l_exec = elf64_exec,
51ca987d46SWarner Losh };
52ca987d46SWarner Losh static struct file_format amd64_elf_obj = {
53ca987d46SWarner Losh .l_load = elf64_obj_loadfile,
54ca987d46SWarner Losh .l_exec = elf64_obj_exec,
55ca987d46SWarner Losh };
56ca987d46SWarner Losh
57adda2797SRoger Pau Monné extern struct file_format multiboot2;
58adda2797SRoger Pau Monné extern struct file_format multiboot2_obj;
59adda2797SRoger Pau Monné
60ca987d46SWarner Losh struct file_format *file_formats[] = {
61adda2797SRoger Pau Monné &multiboot2,
62adda2797SRoger Pau Monné &multiboot2_obj,
63ca987d46SWarner Losh &amd64_elf,
64ca987d46SWarner Losh &amd64_elf_obj,
65ca987d46SWarner Losh NULL
66ca987d46SWarner Losh };
67ca987d46SWarner Losh
68ca987d46SWarner Losh static pml4_entry_t *PT4;
69ca987d46SWarner Losh static pdp_entry_t *PT3;
70f75caed6SKonstantin Belousov static pdp_entry_t *PT3_l, *PT3_u;
71ca987d46SWarner Losh static pd_entry_t *PT2;
72f75caed6SKonstantin Belousov static pd_entry_t *PT2_l0, *PT2_l1, *PT2_l2, *PT2_l3, *PT2_u0, *PT2_u1;
73f75caed6SKonstantin Belousov
74ca987d46SWarner Losh static void (*trampoline)(uint64_t stack, void *copy_finish, uint64_t kernend,
75ca987d46SWarner Losh uint64_t modulep, pml4_entry_t *pagetable, uint64_t entry);
76ca987d46SWarner Losh
77ca987d46SWarner Losh extern uintptr_t amd64_tramp;
78ca987d46SWarner Losh extern uint32_t amd64_tramp_size;
79ca987d46SWarner Losh
80ca987d46SWarner Losh /*
81ca987d46SWarner Losh * There is an ELF kernel and one or more ELF modules loaded.
82ca987d46SWarner Losh * We wish to start executing the kernel image, so make such
83ca987d46SWarner Losh * preparations as are required, and do so.
84ca987d46SWarner Losh */
85ca987d46SWarner Losh static int
elf64_exec(struct preloaded_file * fp)86ca987d46SWarner Losh elf64_exec(struct preloaded_file *fp)
87ca987d46SWarner Losh {
88ca987d46SWarner Losh struct file_metadata *md;
89ca987d46SWarner Losh Elf_Ehdr *ehdr;
90ca987d46SWarner Losh vm_offset_t modulep, kernend, trampcode, trampstack;
91ca987d46SWarner Losh int err, i;
92f75caed6SKonstantin Belousov bool copy_auto;
93f75caed6SKonstantin Belousov
94f75caed6SKonstantin Belousov copy_auto = copy_staging == COPY_STAGING_AUTO;
95f75caed6SKonstantin Belousov if (copy_auto)
96f75caed6SKonstantin Belousov copy_staging = fp->f_kernphys_relocatable ?
97f75caed6SKonstantin Belousov COPY_STAGING_DISABLE : COPY_STAGING_ENABLE;
98ca987d46SWarner Losh
99ca987d46SWarner Losh if ((md = file_findmetadata(fp, MODINFOMD_ELFHDR)) == NULL)
100ca987d46SWarner Losh return (EFTYPE);
101ca987d46SWarner Losh ehdr = (Elf_Ehdr *)&(md->md_data);
102ca987d46SWarner Losh
103f75caed6SKonstantin Belousov trampcode = copy_staging == COPY_STAGING_ENABLE ?
104*ea0f267cSAhmad Khalifa (vm_offset_t)G(1) : (vm_offset_t)G(4);
105ca987d46SWarner Losh err = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData, 1,
106ca987d46SWarner Losh (EFI_PHYSICAL_ADDRESS *)&trampcode);
107f75caed6SKonstantin Belousov if (EFI_ERROR(err)) {
108f75caed6SKonstantin Belousov printf("Unable to allocate trampoline\n");
109f75caed6SKonstantin Belousov if (copy_auto)
110f75caed6SKonstantin Belousov copy_staging = COPY_STAGING_AUTO;
111f75caed6SKonstantin Belousov return (ENOMEM);
112f75caed6SKonstantin Belousov }
113ca987d46SWarner Losh bzero((void *)trampcode, EFI_PAGE_SIZE);
114ca987d46SWarner Losh trampstack = trampcode + EFI_PAGE_SIZE - 8;
115ca987d46SWarner Losh bcopy((void *)&amd64_tramp, (void *)trampcode, amd64_tramp_size);
116ca987d46SWarner Losh trampoline = (void *)trampcode;
117ca987d46SWarner Losh
118f75caed6SKonstantin Belousov if (copy_staging == COPY_STAGING_ENABLE) {
119*ea0f267cSAhmad Khalifa PT4 = (pml4_entry_t *)G(1);
120ca987d46SWarner Losh err = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData, 3,
121ca987d46SWarner Losh (EFI_PHYSICAL_ADDRESS *)&PT4);
122f75caed6SKonstantin Belousov if (EFI_ERROR(err)) {
123f75caed6SKonstantin Belousov printf("Unable to allocate trampoline page table\n");
124f75caed6SKonstantin Belousov BS->FreePages(trampcode, 1);
125f75caed6SKonstantin Belousov if (copy_auto)
126f75caed6SKonstantin Belousov copy_staging = COPY_STAGING_AUTO;
127f75caed6SKonstantin Belousov return (ENOMEM);
128f75caed6SKonstantin Belousov }
129ca987d46SWarner Losh bzero(PT4, 3 * EFI_PAGE_SIZE);
130ca987d46SWarner Losh PT3 = &PT4[512];
131ca987d46SWarner Losh PT2 = &PT3[512];
132ca987d46SWarner Losh
133ca987d46SWarner Losh /*
134f75caed6SKonstantin Belousov * This is kinda brutal, but every single 1GB VM
135f75caed6SKonstantin Belousov * memory segment points to the same first 1GB of
136f75caed6SKonstantin Belousov * physical memory. But it is more than adequate.
137ca987d46SWarner Losh */
138f75caed6SKonstantin Belousov for (i = 0; i < NPTEPG; i++) {
139f75caed6SKonstantin Belousov /*
140f75caed6SKonstantin Belousov * Each slot of the L4 pages points to the
141f75caed6SKonstantin Belousov * same L3 page.
142f75caed6SKonstantin Belousov */
143ca987d46SWarner Losh PT4[i] = (pml4_entry_t)PT3;
1445c170925SMark Johnston PT4[i] |= PG_V | PG_RW;
145ca987d46SWarner Losh
146f75caed6SKonstantin Belousov /*
147f75caed6SKonstantin Belousov * Each slot of the L3 pages points to the
148f75caed6SKonstantin Belousov * same L2 page.
149f75caed6SKonstantin Belousov */
150ca987d46SWarner Losh PT3[i] = (pdp_entry_t)PT2;
1515c170925SMark Johnston PT3[i] |= PG_V | PG_RW;
152ca987d46SWarner Losh
153f75caed6SKonstantin Belousov /*
154f75caed6SKonstantin Belousov * The L2 page slots are mapped with 2MB pages for 1GB.
155f75caed6SKonstantin Belousov */
156*ea0f267cSAhmad Khalifa PT2[i] = (pd_entry_t)i * M(2);
1575c170925SMark Johnston PT2[i] |= PG_V | PG_RW | PG_PS;
158ca987d46SWarner Losh }
159f75caed6SKonstantin Belousov } else {
160*ea0f267cSAhmad Khalifa PT4 = (pml4_entry_t *)G(4);
161f75caed6SKonstantin Belousov err = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData, 9,
162f75caed6SKonstantin Belousov (EFI_PHYSICAL_ADDRESS *)&PT4);
163f75caed6SKonstantin Belousov if (EFI_ERROR(err)) {
164f75caed6SKonstantin Belousov printf("Unable to allocate trampoline page table\n");
165f75caed6SKonstantin Belousov BS->FreePages(trampcode, 9);
166f75caed6SKonstantin Belousov if (copy_auto)
167f75caed6SKonstantin Belousov copy_staging = COPY_STAGING_AUTO;
168f75caed6SKonstantin Belousov return (ENOMEM);
169f75caed6SKonstantin Belousov }
170ca987d46SWarner Losh
171f75caed6SKonstantin Belousov bzero(PT4, 9 * EFI_PAGE_SIZE);
172f75caed6SKonstantin Belousov
173f75caed6SKonstantin Belousov PT3_l = &PT4[NPML4EPG * 1];
174f75caed6SKonstantin Belousov PT3_u = &PT4[NPML4EPG * 2];
175f75caed6SKonstantin Belousov PT2_l0 = &PT4[NPML4EPG * 3];
176f75caed6SKonstantin Belousov PT2_l1 = &PT4[NPML4EPG * 4];
177f75caed6SKonstantin Belousov PT2_l2 = &PT4[NPML4EPG * 5];
178f75caed6SKonstantin Belousov PT2_l3 = &PT4[NPML4EPG * 6];
179f75caed6SKonstantin Belousov PT2_u0 = &PT4[NPML4EPG * 7];
180f75caed6SKonstantin Belousov PT2_u1 = &PT4[NPML4EPG * 8];
181f75caed6SKonstantin Belousov
182f75caed6SKonstantin Belousov /* 1:1 mapping of lower 4G */
183f75caed6SKonstantin Belousov PT4[0] = (pml4_entry_t)PT3_l | PG_V | PG_RW;
184f75caed6SKonstantin Belousov PT3_l[0] = (pdp_entry_t)PT2_l0 | PG_V | PG_RW;
185f75caed6SKonstantin Belousov PT3_l[1] = (pdp_entry_t)PT2_l1 | PG_V | PG_RW;
186f75caed6SKonstantin Belousov PT3_l[2] = (pdp_entry_t)PT2_l2 | PG_V | PG_RW;
187f75caed6SKonstantin Belousov PT3_l[3] = (pdp_entry_t)PT2_l3 | PG_V | PG_RW;
188f75caed6SKonstantin Belousov for (i = 0; i < 4 * NPDEPG; i++) {
189f75caed6SKonstantin Belousov PT2_l0[i] = ((pd_entry_t)i << PDRSHIFT) | PG_V |
190f75caed6SKonstantin Belousov PG_RW | PG_PS;
191f75caed6SKonstantin Belousov }
192f75caed6SKonstantin Belousov
193f75caed6SKonstantin Belousov /* mapping of kernel 2G below top */
194f75caed6SKonstantin Belousov PT4[NPML4EPG - 1] = (pml4_entry_t)PT3_u | PG_V | PG_RW;
195f75caed6SKonstantin Belousov PT3_u[NPDPEPG - 2] = (pdp_entry_t)PT2_u0 | PG_V | PG_RW;
196f75caed6SKonstantin Belousov PT3_u[NPDPEPG - 1] = (pdp_entry_t)PT2_u1 | PG_V | PG_RW;
197f75caed6SKonstantin Belousov /* compat mapping of phys @0 */
198f75caed6SKonstantin Belousov PT2_u0[0] = PG_PS | PG_V | PG_RW;
199f75caed6SKonstantin Belousov /* this maps past staging area */
200f75caed6SKonstantin Belousov for (i = 1; i < 2 * NPDEPG; i++) {
201f75caed6SKonstantin Belousov PT2_u0[i] = ((pd_entry_t)staging +
202f75caed6SKonstantin Belousov ((pd_entry_t)i - 1) * NBPDR) |
203f75caed6SKonstantin Belousov PG_V | PG_RW | PG_PS;
204f75caed6SKonstantin Belousov }
205f75caed6SKonstantin Belousov }
206f75caed6SKonstantin Belousov
2070e3ce6d0SMark Johnston printf("staging %#lx (%scopying) tramp %p PT4 %p\n",
208f75caed6SKonstantin Belousov staging, copy_staging == COPY_STAGING_ENABLE ? "" : "not ",
209f75caed6SKonstantin Belousov trampoline, PT4);
210ca987d46SWarner Losh printf("Start @ 0x%lx ...\n", ehdr->e_entry);
211ca987d46SWarner Losh
212ca987d46SWarner Losh efi_time_fini();
213ed87efbeSRoger Pau Monné err = bi_load(fp->f_args, &modulep, &kernend, true);
214ca987d46SWarner Losh if (err != 0) {
215ca987d46SWarner Losh efi_time_init();
216f75caed6SKonstantin Belousov if (copy_auto)
217f75caed6SKonstantin Belousov copy_staging = COPY_STAGING_AUTO;
218ca987d46SWarner Losh return (err);
219ca987d46SWarner Losh }
220ca987d46SWarner Losh
221ca987d46SWarner Losh dev_cleanup();
222ca987d46SWarner Losh
223f75caed6SKonstantin Belousov trampoline(trampstack, copy_staging == COPY_STAGING_ENABLE ?
224f75caed6SKonstantin Belousov efi_copy_finish : efi_copy_finish_nop, kernend, modulep,
225f75caed6SKonstantin Belousov PT4, ehdr->e_entry);
226ca987d46SWarner Losh
227ca987d46SWarner Losh panic("exec returned");
228ca987d46SWarner Losh }
229ca987d46SWarner Losh
230ca987d46SWarner Losh static int
elf64_obj_exec(struct preloaded_file * fp)231ca987d46SWarner Losh elf64_obj_exec(struct preloaded_file *fp)
232ca987d46SWarner Losh {
233ca987d46SWarner Losh
234ca987d46SWarner Losh return (EFTYPE);
235ca987d46SWarner Losh }
236