1*ca987d46SWarner Losh /*- 2*ca987d46SWarner Losh * Copyright (c) 1998 Michael Smith <msmith@freebsd.org> 3*ca987d46SWarner Losh * Copyright (c) 2014 The FreeBSD Foundation 4*ca987d46SWarner Losh * All rights reserved. 5*ca987d46SWarner Losh * 6*ca987d46SWarner Losh * Redistribution and use in source and binary forms, with or without 7*ca987d46SWarner Losh * modification, are permitted provided that the following conditions 8*ca987d46SWarner Losh * are met: 9*ca987d46SWarner Losh * 1. Redistributions of source code must retain the above copyright 10*ca987d46SWarner Losh * notice, this list of conditions and the following disclaimer. 11*ca987d46SWarner Losh * 2. Redistributions in binary form must reproduce the above copyright 12*ca987d46SWarner Losh * notice, this list of conditions and the following disclaimer in the 13*ca987d46SWarner Losh * documentation and/or other materials provided with the distribution. 14*ca987d46SWarner Losh * 15*ca987d46SWarner Losh * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16*ca987d46SWarner Losh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17*ca987d46SWarner Losh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18*ca987d46SWarner Losh * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19*ca987d46SWarner Losh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20*ca987d46SWarner Losh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21*ca987d46SWarner Losh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22*ca987d46SWarner Losh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23*ca987d46SWarner Losh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24*ca987d46SWarner Losh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25*ca987d46SWarner Losh * SUCH DAMAGE. 26*ca987d46SWarner Losh */ 27*ca987d46SWarner Losh 28*ca987d46SWarner Losh #include <sys/cdefs.h> 29*ca987d46SWarner Losh __FBSDID("$FreeBSD$"); 30*ca987d46SWarner Losh 31*ca987d46SWarner Losh #define __ELF_WORD_SIZE 64 32*ca987d46SWarner Losh #include <sys/param.h> 33*ca987d46SWarner Losh #include <sys/exec.h> 34*ca987d46SWarner Losh #include <sys/linker.h> 35*ca987d46SWarner Losh #include <string.h> 36*ca987d46SWarner Losh #include <machine/elf.h> 37*ca987d46SWarner Losh #include <stand.h> 38*ca987d46SWarner Losh #include <vm/vm.h> 39*ca987d46SWarner Losh #include <vm/pmap.h> 40*ca987d46SWarner Losh 41*ca987d46SWarner Losh #include <efi.h> 42*ca987d46SWarner Losh #include <efilib.h> 43*ca987d46SWarner Losh 44*ca987d46SWarner Losh #include "bootstrap.h" 45*ca987d46SWarner Losh 46*ca987d46SWarner Losh #include "platform/acfreebsd.h" 47*ca987d46SWarner Losh #include "acconfig.h" 48*ca987d46SWarner Losh #define ACPI_SYSTEM_XFACE 49*ca987d46SWarner Losh #include "actypes.h" 50*ca987d46SWarner Losh #include "actbl.h" 51*ca987d46SWarner Losh 52*ca987d46SWarner Losh #include "loader_efi.h" 53*ca987d46SWarner Losh 54*ca987d46SWarner Losh static EFI_GUID acpi_guid = ACPI_TABLE_GUID; 55*ca987d46SWarner Losh static EFI_GUID acpi20_guid = ACPI_20_TABLE_GUID; 56*ca987d46SWarner Losh 57*ca987d46SWarner Losh extern int bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp); 58*ca987d46SWarner Losh 59*ca987d46SWarner Losh static int elf64_exec(struct preloaded_file *amp); 60*ca987d46SWarner Losh static int elf64_obj_exec(struct preloaded_file *amp); 61*ca987d46SWarner Losh 62*ca987d46SWarner Losh static struct file_format amd64_elf = { 63*ca987d46SWarner Losh .l_load = elf64_loadfile, 64*ca987d46SWarner Losh .l_exec = elf64_exec, 65*ca987d46SWarner Losh }; 66*ca987d46SWarner Losh static struct file_format amd64_elf_obj = { 67*ca987d46SWarner Losh .l_load = elf64_obj_loadfile, 68*ca987d46SWarner Losh .l_exec = elf64_obj_exec, 69*ca987d46SWarner Losh }; 70*ca987d46SWarner Losh 71*ca987d46SWarner Losh struct file_format *file_formats[] = { 72*ca987d46SWarner Losh &amd64_elf, 73*ca987d46SWarner Losh &amd64_elf_obj, 74*ca987d46SWarner Losh NULL 75*ca987d46SWarner Losh }; 76*ca987d46SWarner Losh 77*ca987d46SWarner Losh static pml4_entry_t *PT4; 78*ca987d46SWarner Losh static pdp_entry_t *PT3; 79*ca987d46SWarner Losh static pd_entry_t *PT2; 80*ca987d46SWarner Losh 81*ca987d46SWarner Losh static void (*trampoline)(uint64_t stack, void *copy_finish, uint64_t kernend, 82*ca987d46SWarner Losh uint64_t modulep, pml4_entry_t *pagetable, uint64_t entry); 83*ca987d46SWarner Losh 84*ca987d46SWarner Losh extern uintptr_t amd64_tramp; 85*ca987d46SWarner Losh extern uint32_t amd64_tramp_size; 86*ca987d46SWarner Losh 87*ca987d46SWarner Losh /* 88*ca987d46SWarner Losh * There is an ELF kernel and one or more ELF modules loaded. 89*ca987d46SWarner Losh * We wish to start executing the kernel image, so make such 90*ca987d46SWarner Losh * preparations as are required, and do so. 91*ca987d46SWarner Losh */ 92*ca987d46SWarner Losh static int 93*ca987d46SWarner Losh elf64_exec(struct preloaded_file *fp) 94*ca987d46SWarner Losh { 95*ca987d46SWarner Losh struct file_metadata *md; 96*ca987d46SWarner Losh Elf_Ehdr *ehdr; 97*ca987d46SWarner Losh vm_offset_t modulep, kernend, trampcode, trampstack; 98*ca987d46SWarner Losh int err, i; 99*ca987d46SWarner Losh ACPI_TABLE_RSDP *rsdp; 100*ca987d46SWarner Losh char buf[24]; 101*ca987d46SWarner Losh int revision; 102*ca987d46SWarner Losh 103*ca987d46SWarner Losh /* 104*ca987d46SWarner Losh * Report the RSDP to the kernel. While this can be found with 105*ca987d46SWarner Losh * a BIOS boot, the RSDP may be elsewhere when booted from UEFI. 106*ca987d46SWarner Losh * The old code used the 'hints' method to communite this to 107*ca987d46SWarner Losh * the kernel. However, while convenient, the 'hints' method 108*ca987d46SWarner Losh * is fragile and does not work when static hints are compiled 109*ca987d46SWarner Losh * into the kernel. Instead, move to setting different tunables 110*ca987d46SWarner Losh * that start with acpi. The old 'hints' can be removed before 111*ca987d46SWarner Losh * we branch for FreeBSD 12. 112*ca987d46SWarner Losh */ 113*ca987d46SWarner Losh 114*ca987d46SWarner Losh rsdp = efi_get_table(&acpi20_guid); 115*ca987d46SWarner Losh if (rsdp == NULL) { 116*ca987d46SWarner Losh rsdp = efi_get_table(&acpi_guid); 117*ca987d46SWarner Losh } 118*ca987d46SWarner Losh if (rsdp != NULL) { 119*ca987d46SWarner Losh sprintf(buf, "0x%016llx", (unsigned long long)rsdp); 120*ca987d46SWarner Losh setenv("hint.acpi.0.rsdp", buf, 1); 121*ca987d46SWarner Losh setenv("acpi.rsdp", buf, 1); 122*ca987d46SWarner Losh revision = rsdp->Revision; 123*ca987d46SWarner Losh if (revision == 0) 124*ca987d46SWarner Losh revision = 1; 125*ca987d46SWarner Losh sprintf(buf, "%d", revision); 126*ca987d46SWarner Losh setenv("hint.acpi.0.revision", buf, 1); 127*ca987d46SWarner Losh setenv("acpi.revision", buf, 1); 128*ca987d46SWarner Losh strncpy(buf, rsdp->OemId, sizeof(rsdp->OemId)); 129*ca987d46SWarner Losh buf[sizeof(rsdp->OemId)] = '\0'; 130*ca987d46SWarner Losh setenv("hint.acpi.0.oem", buf, 1); 131*ca987d46SWarner Losh setenv("acpi.oem", buf, 1); 132*ca987d46SWarner Losh sprintf(buf, "0x%016x", rsdp->RsdtPhysicalAddress); 133*ca987d46SWarner Losh setenv("hint.acpi.0.rsdt", buf, 1); 134*ca987d46SWarner Losh setenv("acpi.rsdt", buf, 1); 135*ca987d46SWarner Losh if (revision >= 2) { 136*ca987d46SWarner Losh /* XXX extended checksum? */ 137*ca987d46SWarner Losh sprintf(buf, "0x%016llx", 138*ca987d46SWarner Losh (unsigned long long)rsdp->XsdtPhysicalAddress); 139*ca987d46SWarner Losh setenv("hint.acpi.0.xsdt", buf, 1); 140*ca987d46SWarner Losh setenv("acpi.xsdt", buf, 1); 141*ca987d46SWarner Losh sprintf(buf, "%d", rsdp->Length); 142*ca987d46SWarner Losh setenv("hint.acpi.0.xsdt_length", buf, 1); 143*ca987d46SWarner Losh setenv("acpi.xsdt_length", buf, 1); 144*ca987d46SWarner Losh } 145*ca987d46SWarner Losh } 146*ca987d46SWarner Losh 147*ca987d46SWarner Losh if ((md = file_findmetadata(fp, MODINFOMD_ELFHDR)) == NULL) 148*ca987d46SWarner Losh return(EFTYPE); 149*ca987d46SWarner Losh ehdr = (Elf_Ehdr *)&(md->md_data); 150*ca987d46SWarner Losh 151*ca987d46SWarner Losh trampcode = (vm_offset_t)0x0000000040000000; 152*ca987d46SWarner Losh err = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData, 1, 153*ca987d46SWarner Losh (EFI_PHYSICAL_ADDRESS *)&trampcode); 154*ca987d46SWarner Losh bzero((void *)trampcode, EFI_PAGE_SIZE); 155*ca987d46SWarner Losh trampstack = trampcode + EFI_PAGE_SIZE - 8; 156*ca987d46SWarner Losh bcopy((void *)&amd64_tramp, (void *)trampcode, amd64_tramp_size); 157*ca987d46SWarner Losh trampoline = (void *)trampcode; 158*ca987d46SWarner Losh 159*ca987d46SWarner Losh PT4 = (pml4_entry_t *)0x0000000040000000; 160*ca987d46SWarner Losh err = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData, 3, 161*ca987d46SWarner Losh (EFI_PHYSICAL_ADDRESS *)&PT4); 162*ca987d46SWarner Losh bzero(PT4, 3 * EFI_PAGE_SIZE); 163*ca987d46SWarner Losh 164*ca987d46SWarner Losh PT3 = &PT4[512]; 165*ca987d46SWarner Losh PT2 = &PT3[512]; 166*ca987d46SWarner Losh 167*ca987d46SWarner Losh /* 168*ca987d46SWarner Losh * This is kinda brutal, but every single 1GB VM memory segment points 169*ca987d46SWarner Losh * to the same first 1GB of physical memory. But it is more than 170*ca987d46SWarner Losh * adequate. 171*ca987d46SWarner Losh */ 172*ca987d46SWarner Losh for (i = 0; i < 512; i++) { 173*ca987d46SWarner Losh /* Each slot of the L4 pages points to the same L3 page. */ 174*ca987d46SWarner Losh PT4[i] = (pml4_entry_t)PT3; 175*ca987d46SWarner Losh PT4[i] |= PG_V | PG_RW | PG_U; 176*ca987d46SWarner Losh 177*ca987d46SWarner Losh /* Each slot of the L3 pages points to the same L2 page. */ 178*ca987d46SWarner Losh PT3[i] = (pdp_entry_t)PT2; 179*ca987d46SWarner Losh PT3[i] |= PG_V | PG_RW | PG_U; 180*ca987d46SWarner Losh 181*ca987d46SWarner Losh /* The L2 page slots are mapped with 2MB pages for 1GB. */ 182*ca987d46SWarner Losh PT2[i] = i * (2 * 1024 * 1024); 183*ca987d46SWarner Losh PT2[i] |= PG_V | PG_RW | PG_PS | PG_U; 184*ca987d46SWarner Losh } 185*ca987d46SWarner Losh 186*ca987d46SWarner Losh printf("Start @ 0x%lx ...\n", ehdr->e_entry); 187*ca987d46SWarner Losh 188*ca987d46SWarner Losh efi_time_fini(); 189*ca987d46SWarner Losh err = bi_load(fp->f_args, &modulep, &kernend); 190*ca987d46SWarner Losh if (err != 0) { 191*ca987d46SWarner Losh efi_time_init(); 192*ca987d46SWarner Losh return(err); 193*ca987d46SWarner Losh } 194*ca987d46SWarner Losh 195*ca987d46SWarner Losh dev_cleanup(); 196*ca987d46SWarner Losh 197*ca987d46SWarner Losh trampoline(trampstack, efi_copy_finish, kernend, modulep, PT4, 198*ca987d46SWarner Losh ehdr->e_entry); 199*ca987d46SWarner Losh 200*ca987d46SWarner Losh panic("exec returned"); 201*ca987d46SWarner Losh } 202*ca987d46SWarner Losh 203*ca987d46SWarner Losh static int 204*ca987d46SWarner Losh elf64_obj_exec(struct preloaded_file *fp) 205*ca987d46SWarner Losh { 206*ca987d46SWarner Losh 207*ca987d46SWarner Losh return (EFTYPE); 208*ca987d46SWarner Losh } 209