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