2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3 * Copyright (c) 2014 The FreeBSD Foundation
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #define __ELF_WORD_SIZE 64
32 #include <sys/param.h>
34 #include <sys/linker.h>
36 #include <machine/elf.h>
44 #include "bootstrap.h"
46 #include "platform/acfreebsd.h"
48 #define ACPI_SYSTEM_XFACE
52 #include "loader_efi.h"
54 static EFI_GUID acpi_guid = ACPI_TABLE_GUID;
55 static EFI_GUID acpi20_guid = ACPI_20_TABLE_GUID;
57 extern int bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp);
59 static int elf64_exec(struct preloaded_file *amp);
60 static int elf64_obj_exec(struct preloaded_file *amp);
62 static struct file_format amd64_elf = {
63 .l_load = elf64_loadfile,
66 static struct file_format amd64_elf_obj = {
67 .l_load = elf64_obj_loadfile,
68 .l_exec = elf64_obj_exec,
71 struct file_format *file_formats[] = {
77 static pml4_entry_t *PT4;
78 static pdp_entry_t *PT3;
79 static pd_entry_t *PT2;
81 static void (*trampoline)(uint64_t stack, void *copy_finish, uint64_t kernend,
82 uint64_t modulep, pml4_entry_t *pagetable, uint64_t entry);
84 extern uintptr_t amd64_tramp;
85 extern uint32_t amd64_tramp_size;
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.
93 elf64_exec(struct preloaded_file *fp)
95 struct file_metadata *md;
97 vm_offset_t modulep, kernend, trampcode, trampstack;
99 ACPI_TABLE_RSDP *rsdp;
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.
114 rsdp = efi_get_table(&acpi20_guid);
116 rsdp = efi_get_table(&acpi_guid);
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;
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);
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);
147 if ((md = file_findmetadata(fp, MODINFOMD_ELFHDR)) == NULL)
149 ehdr = (Elf_Ehdr *)&(md->md_data);
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;
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);
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
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 | PG_U;
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 | PG_U;
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 | PG_U;
186 printf("Start @ 0x%lx ...\n", ehdr->e_entry);
189 err = bi_load(fp->f_args, &modulep, &kernend);
197 trampoline(trampstack, efi_copy_finish, kernend, modulep, PT4,
200 panic("exec returned");
204 elf64_obj_exec(struct preloaded_file *fp)