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