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>
42 #include "bootstrap.h"
44 #include "platform/acfreebsd.h"
46 #define ACPI_SYSTEM_XFACE
47 #define ACPI_USE_SYSTEM_INTTYPES
51 #include "loader_efi.h"
53 static EFI_GUID acpi_guid = ACPI_TABLE_GUID;
54 static EFI_GUID acpi20_guid = ACPI_20_TABLE_GUID;
56 extern int bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp);
58 static int elf64_exec(struct preloaded_file *amp);
59 static int elf64_obj_exec(struct preloaded_file *amp);
61 static struct file_format amd64_elf = { elf64_loadfile, elf64_exec };
62 static struct file_format amd64_elf_obj = { elf64_obj_loadfile, elf64_obj_exec };
64 struct file_format *file_formats[] = {
75 typedef u_int64_t p4_entry_t;
76 typedef u_int64_t p3_entry_t;
77 typedef u_int64_t p2_entry_t;
78 static p4_entry_t *PT4;
79 static p3_entry_t *PT3;
80 static p2_entry_t *PT2;
82 static void (*trampoline)(uint64_t stack, void *copy_finish, uint64_t kernend,
83 uint64_t modulep, p4_entry_t *pagetable,
86 extern uintptr_t amd64_tramp;
87 extern uint32_t amd64_tramp_size;
90 * There is an ELF kernel and one or more ELF modules loaded.
91 * We wish to start executing the kernel image, so make such
92 * preparations as are required, and do so.
95 elf64_exec(struct preloaded_file *fp)
97 struct file_metadata *md;
99 vm_offset_t modulep, kernend, trampcode, trampstack;
101 ACPI_TABLE_RSDP *rsdp;
105 rsdp = efi_get_table(&acpi20_guid);
107 rsdp = efi_get_table(&acpi_guid);
110 sprintf(buf, "0x%016llx", (unsigned long long)rsdp);
111 setenv("hint.acpi.0.rsdp", buf, 1);
112 revision = rsdp->Revision;
115 sprintf(buf, "%d", revision);
116 setenv("hint.acpi.0.revision", buf, 1);
117 strncpy(buf, rsdp->OemId, sizeof(rsdp->OemId));
118 buf[sizeof(rsdp->OemId)] = '\0';
119 setenv("hint.acpi.0.oem", buf, 1);
120 sprintf(buf, "0x%016x", rsdp->RsdtPhysicalAddress);
121 setenv("hint.acpi.0.rsdt", buf, 1);
123 /* XXX extended checksum? */
124 sprintf(buf, "0x%016llx",
125 (unsigned long long)rsdp->XsdtPhysicalAddress);
126 setenv("hint.acpi.0.xsdt", buf, 1);
127 sprintf(buf, "%d", rsdp->Length);
128 setenv("hint.acpi.0.xsdt_length", buf, 1);
132 if ((md = file_findmetadata(fp, MODINFOMD_ELFHDR)) == NULL)
134 ehdr = (Elf_Ehdr *)&(md->md_data);
136 trampcode = (vm_offset_t)0x0000000040000000;
137 err = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData, 1,
138 (EFI_PHYSICAL_ADDRESS *)&trampcode);
139 bzero((void *)trampcode, EFI_PAGE_SIZE);
140 trampstack = trampcode + EFI_PAGE_SIZE - 8;
141 bcopy((void *)&amd64_tramp, (void *)trampcode, amd64_tramp_size);
142 trampoline = (void *)trampcode;
144 PT4 = (p4_entry_t *)0x0000000040000000;
145 err = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData, 3,
146 (EFI_PHYSICAL_ADDRESS *)&PT4);
147 bzero(PT4, 3 * EFI_PAGE_SIZE);
153 * This is kinda brutal, but every single 1GB VM memory segment points
154 * to the same first 1GB of physical memory. But it is more than
157 for (i = 0; i < 512; i++) {
158 /* Each slot of the L4 pages points to the same L3 page. */
159 PT4[i] = (p4_entry_t)PT3;
160 PT4[i] |= PG_V | PG_RW | PG_U;
162 /* Each slot of the L3 pages points to the same L2 page. */
163 PT3[i] = (p3_entry_t)PT2;
164 PT3[i] |= PG_V | PG_RW | PG_U;
166 /* The L2 page slots are mapped with 2MB pages for 1GB. */
167 PT2[i] = i * (2 * 1024 * 1024);
168 PT2[i] |= PG_V | PG_RW | PG_PS | PG_U;
171 printf("Start @ 0x%lx ...\n", ehdr->e_entry);
173 err = bi_load(fp->f_args, &modulep, &kernend);
179 trampoline(trampstack, efi_copy_finish, kernend, modulep, PT4,
182 panic("exec returned");
186 elf64_obj_exec(struct preloaded_file *fp)