xref: /freebsd/stand/efi/loader/arch/amd64/elf64_freebsd.c (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
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 
59 static int	elf64_exec(struct preloaded_file *amp);
60 static int	elf64_obj_exec(struct preloaded_file *amp);
61 
62 static struct file_format amd64_elf = {
63 	.l_load = elf64_loadfile,
64 	.l_exec = elf64_exec,
65 };
66 static struct file_format amd64_elf_obj = {
67 	.l_load = elf64_obj_loadfile,
68 	.l_exec = elf64_obj_exec,
69 };
70 
71 struct file_format *file_formats[] = {
72 	&amd64_elf,
73 	&amd64_elf_obj,
74 	NULL
75 };
76 
77 static pml4_entry_t *PT4;
78 static pdp_entry_t *PT3;
79 static pd_entry_t *PT2;
80 
81 static void (*trampoline)(uint64_t stack, void *copy_finish, uint64_t kernend,
82     uint64_t modulep, pml4_entry_t *pagetable, uint64_t entry);
83 
84 extern uintptr_t amd64_tramp;
85 extern uint32_t amd64_tramp_size;
86 
87 /*
88  * There is an ELF kernel and one or more ELF modules loaded.
89  * We wish to start executing the kernel image, so make such
90  * preparations as are required, and do so.
91  */
92 static int
93 elf64_exec(struct preloaded_file *fp)
94 {
95 	struct file_metadata	*md;
96 	Elf_Ehdr 		*ehdr;
97 	vm_offset_t		modulep, kernend, trampcode, trampstack;
98 	int			err, i;
99 	ACPI_TABLE_RSDP		*rsdp;
100 	char			buf[24];
101 	int			revision;
102 
103 	/*
104 	 * Report the RSDP to the kernel. While this can be found with
105 	 * a BIOS boot, the RSDP may be elsewhere when booted from UEFI.
106 	 * The old code used the 'hints' method to communite this to
107 	 * the kernel. However, while convenient, the 'hints' method
108 	 * is fragile and does not work when static hints are compiled
109 	 * into the kernel. Instead, move to setting different tunables
110 	 * that start with acpi. The old 'hints' can be removed before
111 	 * we branch for FreeBSD 12.
112 	 */
113 
114 	rsdp = efi_get_table(&acpi20_guid);
115 	if (rsdp == NULL) {
116 		rsdp = efi_get_table(&acpi_guid);
117 	}
118 	if (rsdp != NULL) {
119 		sprintf(buf, "0x%016llx", (unsigned long long)rsdp);
120 		setenv("hint.acpi.0.rsdp", buf, 1);
121 		setenv("acpi.rsdp", buf, 1);
122 		revision = rsdp->Revision;
123 		if (revision == 0)
124 			revision = 1;
125 		sprintf(buf, "%d", revision);
126 		setenv("hint.acpi.0.revision", buf, 1);
127 		setenv("acpi.revision", buf, 1);
128 		strncpy(buf, rsdp->OemId, sizeof(rsdp->OemId));
129 		buf[sizeof(rsdp->OemId)] = '\0';
130 		setenv("hint.acpi.0.oem", buf, 1);
131 		setenv("acpi.oem", buf, 1);
132 		sprintf(buf, "0x%016x", rsdp->RsdtPhysicalAddress);
133 		setenv("hint.acpi.0.rsdt", buf, 1);
134 		setenv("acpi.rsdt", buf, 1);
135 		if (revision >= 2) {
136 			/* XXX extended checksum? */
137 			sprintf(buf, "0x%016llx",
138 			    (unsigned long long)rsdp->XsdtPhysicalAddress);
139 			setenv("hint.acpi.0.xsdt", buf, 1);
140 			setenv("acpi.xsdt", buf, 1);
141 			sprintf(buf, "%d", rsdp->Length);
142 			setenv("hint.acpi.0.xsdt_length", buf, 1);
143 			setenv("acpi.xsdt_length", buf, 1);
144 		}
145 	}
146 
147 	if ((md = file_findmetadata(fp, MODINFOMD_ELFHDR)) == NULL)
148 		return(EFTYPE);
149 	ehdr = (Elf_Ehdr *)&(md->md_data);
150 
151 	trampcode = (vm_offset_t)0x0000000040000000;
152 	err = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData, 1,
153 	    (EFI_PHYSICAL_ADDRESS *)&trampcode);
154 	bzero((void *)trampcode, EFI_PAGE_SIZE);
155 	trampstack = trampcode + EFI_PAGE_SIZE - 8;
156 	bcopy((void *)&amd64_tramp, (void *)trampcode, amd64_tramp_size);
157 	trampoline = (void *)trampcode;
158 
159 	PT4 = (pml4_entry_t *)0x0000000040000000;
160 	err = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData, 3,
161 	    (EFI_PHYSICAL_ADDRESS *)&PT4);
162 	bzero(PT4, 3 * EFI_PAGE_SIZE);
163 
164 	PT3 = &PT4[512];
165 	PT2 = &PT3[512];
166 
167 	/*
168 	 * This is kinda brutal, but every single 1GB VM memory segment points
169 	 * to the same first 1GB of physical memory.  But it is more than
170 	 * adequate.
171 	 */
172 	for (i = 0; i < 512; i++) {
173 		/* Each slot of the L4 pages points to the same L3 page. */
174 		PT4[i] = (pml4_entry_t)PT3;
175 		PT4[i] |= PG_V | PG_RW;
176 
177 		/* Each slot of the L3 pages points to the same L2 page. */
178 		PT3[i] = (pdp_entry_t)PT2;
179 		PT3[i] |= PG_V | PG_RW;
180 
181 		/* The L2 page slots are mapped with 2MB pages for 1GB. */
182 		PT2[i] = i * (2 * 1024 * 1024);
183 		PT2[i] |= PG_V | PG_RW | PG_PS;
184 	}
185 
186 	printf("Start @ 0x%lx ...\n", ehdr->e_entry);
187 
188 	efi_time_fini();
189 	err = bi_load(fp->f_args, &modulep, &kernend);
190 	if (err != 0) {
191 		efi_time_init();
192 		return(err);
193 	}
194 
195 	dev_cleanup();
196 
197 	trampoline(trampstack, efi_copy_finish, kernend, modulep, PT4,
198 	    ehdr->e_entry);
199 
200 	panic("exec returned");
201 }
202 
203 static int
204 elf64_obj_exec(struct preloaded_file *fp)
205 {
206 
207 	return (EFTYPE);
208 }
209