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 pdp_entry_t *PT3_l, *PT3_u; 86 static pd_entry_t *PT2; 87 static pd_entry_t *PT2_l0, *PT2_l1, *PT2_l2, *PT2_l3, *PT2_u0, *PT2_u1; 88 89 extern EFI_PHYSICAL_ADDRESS staging; 90 91 static void (*trampoline)(uint64_t stack, void *copy_finish, uint64_t kernend, 92 uint64_t modulep, pml4_entry_t *pagetable, uint64_t entry); 93 94 extern uintptr_t amd64_tramp; 95 extern uint32_t amd64_tramp_size; 96 97 /* 98 * There is an ELF kernel and one or more ELF modules loaded. 99 * We wish to start executing the kernel image, so make such 100 * preparations as are required, and do so. 101 */ 102 static int 103 elf64_exec(struct preloaded_file *fp) 104 { 105 struct file_metadata *md; 106 Elf_Ehdr *ehdr; 107 vm_offset_t modulep, kernend, trampcode, trampstack; 108 int err, i; 109 ACPI_TABLE_RSDP *rsdp; 110 char buf[24]; 111 int revision; 112 bool copy_auto; 113 114 copy_auto = copy_staging == COPY_STAGING_AUTO; 115 if (copy_auto) 116 copy_staging = fp->f_kernphys_relocatable ? 117 COPY_STAGING_DISABLE : COPY_STAGING_ENABLE; 118 119 /* 120 * Report the RSDP to the kernel. While this can be found with 121 * a BIOS boot, the RSDP may be elsewhere when booted from UEFI. 122 * The old code used the 'hints' method to communite this to 123 * the kernel. However, while convenient, the 'hints' method 124 * is fragile and does not work when static hints are compiled 125 * into the kernel. Instead, move to setting different tunables 126 * that start with acpi. The old 'hints' can be removed before 127 * we branch for FreeBSD 12. 128 */ 129 130 rsdp = efi_get_table(&acpi20_guid); 131 if (rsdp == NULL) { 132 rsdp = efi_get_table(&acpi_guid); 133 } 134 if (rsdp != NULL) { 135 sprintf(buf, "0x%016llx", (unsigned long long)rsdp); 136 setenv("hint.acpi.0.rsdp", buf, 1); 137 setenv("acpi.rsdp", buf, 1); 138 revision = rsdp->Revision; 139 if (revision == 0) 140 revision = 1; 141 sprintf(buf, "%d", revision); 142 setenv("hint.acpi.0.revision", buf, 1); 143 setenv("acpi.revision", buf, 1); 144 strncpy(buf, rsdp->OemId, sizeof(rsdp->OemId)); 145 buf[sizeof(rsdp->OemId)] = '\0'; 146 setenv("hint.acpi.0.oem", buf, 1); 147 setenv("acpi.oem", buf, 1); 148 sprintf(buf, "0x%016x", rsdp->RsdtPhysicalAddress); 149 setenv("hint.acpi.0.rsdt", buf, 1); 150 setenv("acpi.rsdt", buf, 1); 151 if (revision >= 2) { 152 /* XXX extended checksum? */ 153 sprintf(buf, "0x%016llx", 154 (unsigned long long)rsdp->XsdtPhysicalAddress); 155 setenv("hint.acpi.0.xsdt", buf, 1); 156 setenv("acpi.xsdt", buf, 1); 157 sprintf(buf, "%d", rsdp->Length); 158 setenv("hint.acpi.0.xsdt_length", buf, 1); 159 setenv("acpi.xsdt_length", buf, 1); 160 } 161 } 162 163 if ((md = file_findmetadata(fp, MODINFOMD_ELFHDR)) == NULL) 164 return (EFTYPE); 165 ehdr = (Elf_Ehdr *)&(md->md_data); 166 167 trampcode = copy_staging == COPY_STAGING_ENABLE ? 168 (vm_offset_t)0x0000000040000000 /* 1G */ : 169 (vm_offset_t)0x0000000100000000; /* 4G */; 170 err = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData, 1, 171 (EFI_PHYSICAL_ADDRESS *)&trampcode); 172 if (EFI_ERROR(err)) { 173 printf("Unable to allocate trampoline\n"); 174 if (copy_auto) 175 copy_staging = COPY_STAGING_AUTO; 176 return (ENOMEM); 177 } 178 bzero((void *)trampcode, EFI_PAGE_SIZE); 179 trampstack = trampcode + EFI_PAGE_SIZE - 8; 180 bcopy((void *)&amd64_tramp, (void *)trampcode, amd64_tramp_size); 181 trampoline = (void *)trampcode; 182 183 if (copy_staging == COPY_STAGING_ENABLE) { 184 PT4 = (pml4_entry_t *)0x0000000040000000; 185 err = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData, 3, 186 (EFI_PHYSICAL_ADDRESS *)&PT4); 187 if (EFI_ERROR(err)) { 188 printf("Unable to allocate trampoline page table\n"); 189 BS->FreePages(trampcode, 1); 190 if (copy_auto) 191 copy_staging = COPY_STAGING_AUTO; 192 return (ENOMEM); 193 } 194 bzero(PT4, 3 * EFI_PAGE_SIZE); 195 PT3 = &PT4[512]; 196 PT2 = &PT3[512]; 197 198 /* 199 * This is kinda brutal, but every single 1GB VM 200 * memory segment points to the same first 1GB of 201 * physical memory. But it is more than adequate. 202 */ 203 for (i = 0; i < NPTEPG; i++) { 204 /* 205 * Each slot of the L4 pages points to the 206 * same L3 page. 207 */ 208 PT4[i] = (pml4_entry_t)PT3; 209 PT4[i] |= PG_V | PG_RW; 210 211 /* 212 * Each slot of the L3 pages points to the 213 * same L2 page. 214 */ 215 PT3[i] = (pdp_entry_t)PT2; 216 PT3[i] |= PG_V | PG_RW; 217 218 /* 219 * The L2 page slots are mapped with 2MB pages for 1GB. 220 */ 221 PT2[i] = (pd_entry_t)i * (2 * 1024 * 1024); 222 PT2[i] |= PG_V | PG_RW | PG_PS; 223 } 224 } else { 225 PT4 = (pml4_entry_t *)0x0000000100000000; /* 4G */ 226 err = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData, 9, 227 (EFI_PHYSICAL_ADDRESS *)&PT4); 228 if (EFI_ERROR(err)) { 229 printf("Unable to allocate trampoline page table\n"); 230 BS->FreePages(trampcode, 9); 231 if (copy_auto) 232 copy_staging = COPY_STAGING_AUTO; 233 return (ENOMEM); 234 } 235 236 bzero(PT4, 9 * EFI_PAGE_SIZE); 237 238 PT3_l = &PT4[NPML4EPG * 1]; 239 PT3_u = &PT4[NPML4EPG * 2]; 240 PT2_l0 = &PT4[NPML4EPG * 3]; 241 PT2_l1 = &PT4[NPML4EPG * 4]; 242 PT2_l2 = &PT4[NPML4EPG * 5]; 243 PT2_l3 = &PT4[NPML4EPG * 6]; 244 PT2_u0 = &PT4[NPML4EPG * 7]; 245 PT2_u1 = &PT4[NPML4EPG * 8]; 246 247 /* 1:1 mapping of lower 4G */ 248 PT4[0] = (pml4_entry_t)PT3_l | PG_V | PG_RW; 249 PT3_l[0] = (pdp_entry_t)PT2_l0 | PG_V | PG_RW; 250 PT3_l[1] = (pdp_entry_t)PT2_l1 | PG_V | PG_RW; 251 PT3_l[2] = (pdp_entry_t)PT2_l2 | PG_V | PG_RW; 252 PT3_l[3] = (pdp_entry_t)PT2_l3 | PG_V | PG_RW; 253 for (i = 0; i < 4 * NPDEPG; i++) { 254 PT2_l0[i] = ((pd_entry_t)i << PDRSHIFT) | PG_V | 255 PG_RW | PG_PS; 256 } 257 258 /* mapping of kernel 2G below top */ 259 PT4[NPML4EPG - 1] = (pml4_entry_t)PT3_u | PG_V | PG_RW; 260 PT3_u[NPDPEPG - 2] = (pdp_entry_t)PT2_u0 | PG_V | PG_RW; 261 PT3_u[NPDPEPG - 1] = (pdp_entry_t)PT2_u1 | PG_V | PG_RW; 262 /* compat mapping of phys @0 */ 263 PT2_u0[0] = PG_PS | PG_V | PG_RW; 264 /* this maps past staging area */ 265 for (i = 1; i < 2 * NPDEPG; i++) { 266 PT2_u0[i] = ((pd_entry_t)staging + 267 ((pd_entry_t)i - 1) * NBPDR) | 268 PG_V | PG_RW | PG_PS; 269 } 270 } 271 272 printf("staging %#lx (%scoping) tramp %p PT4 %p\n", 273 staging, copy_staging == COPY_STAGING_ENABLE ? "" : "not ", 274 trampoline, PT4); 275 printf("Start @ 0x%lx ...\n", ehdr->e_entry); 276 277 efi_time_fini(); 278 err = bi_load(fp->f_args, &modulep, &kernend, true); 279 if (err != 0) { 280 efi_time_init(); 281 if (copy_auto) 282 copy_staging = COPY_STAGING_AUTO; 283 return (err); 284 } 285 286 dev_cleanup(); 287 288 trampoline(trampstack, copy_staging == COPY_STAGING_ENABLE ? 289 efi_copy_finish : efi_copy_finish_nop, kernend, modulep, 290 PT4, ehdr->e_entry); 291 292 panic("exec returned"); 293 } 294 295 static int 296 elf64_obj_exec(struct preloaded_file *fp) 297 { 298 299 return (EFTYPE); 300 } 301