Path: blob/main/stand/efi/loader/arch/amd64/elf64_freebsd.c
34907 views
/*-1* Copyright (c) 1998 Michael Smith <[email protected]>2* Copyright (c) 2014 The FreeBSD Foundation3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627#define __ELF_WORD_SIZE 6428#include <sys/param.h>29#include <sys/exec.h>30#include <sys/linker.h>31#include <string.h>32#include <machine/elf.h>33#include <stand.h>34#include <vm/vm.h>35#include <vm/pmap.h>3637#include <efi.h>38#include <efilib.h>3940#include "bootstrap.h"4142#include "loader_efi.h"4344static int elf64_exec(struct preloaded_file *amp);45static int elf64_obj_exec(struct preloaded_file *amp);4647static struct file_format amd64_elf = {48.l_load = elf64_loadfile,49.l_exec = elf64_exec,50};51static struct file_format amd64_elf_obj = {52.l_load = elf64_obj_loadfile,53.l_exec = elf64_obj_exec,54};5556extern struct file_format multiboot2;57extern struct file_format multiboot2_obj;5859struct file_format *file_formats[] = {60&multiboot2,61&multiboot2_obj,62&amd64_elf,63&amd64_elf_obj,64NULL65};6667static pml4_entry_t *PT4;68static pdp_entry_t *PT3;69static pdp_entry_t *PT3_l, *PT3_u;70static pd_entry_t *PT2;71static pd_entry_t *PT2_l0, *PT2_l1, *PT2_l2, *PT2_l3, *PT2_u0, *PT2_u1;7273static void (*trampoline)(uint64_t stack, void *copy_finish, uint64_t kernend,74uint64_t modulep, pml4_entry_t *pagetable, uint64_t entry);7576extern uintptr_t amd64_tramp;77extern uint32_t amd64_tramp_size;7879/*80* There is an ELF kernel and one or more ELF modules loaded.81* We wish to start executing the kernel image, so make such82* preparations as are required, and do so.83*/84static int85elf64_exec(struct preloaded_file *fp)86{87struct file_metadata *md;88Elf_Ehdr *ehdr;89vm_offset_t modulep, kernend, trampcode, trampstack;90int err, i;91bool copy_auto;9293copy_auto = copy_staging == COPY_STAGING_AUTO;94if (copy_auto)95copy_staging = fp->f_kernphys_relocatable ?96COPY_STAGING_DISABLE : COPY_STAGING_ENABLE;9798if ((md = file_findmetadata(fp, MODINFOMD_ELFHDR)) == NULL)99return (EFTYPE);100ehdr = (Elf_Ehdr *)&(md->md_data);101102trampcode = copy_staging == COPY_STAGING_ENABLE ?103(vm_offset_t)G(1) : (vm_offset_t)G(4);104err = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData, 1,105(EFI_PHYSICAL_ADDRESS *)&trampcode);106if (EFI_ERROR(err)) {107printf("Unable to allocate trampoline\n");108if (copy_auto)109copy_staging = COPY_STAGING_AUTO;110return (ENOMEM);111}112bzero((void *)trampcode, EFI_PAGE_SIZE);113trampstack = trampcode + EFI_PAGE_SIZE - 8;114bcopy((void *)&amd64_tramp, (void *)trampcode, amd64_tramp_size);115trampoline = (void *)trampcode;116117if (copy_staging == COPY_STAGING_ENABLE) {118PT4 = (pml4_entry_t *)G(1);119err = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData, 3,120(EFI_PHYSICAL_ADDRESS *)&PT4);121if (EFI_ERROR(err)) {122printf("Unable to allocate trampoline page table\n");123BS->FreePages(trampcode, 1);124if (copy_auto)125copy_staging = COPY_STAGING_AUTO;126return (ENOMEM);127}128bzero(PT4, 3 * EFI_PAGE_SIZE);129PT3 = &PT4[512];130PT2 = &PT3[512];131132/*133* This is kinda brutal, but every single 1GB VM134* memory segment points to the same first 1GB of135* physical memory. But it is more than adequate.136*/137for (i = 0; i < NPTEPG; i++) {138/*139* Each slot of the L4 pages points to the140* same L3 page.141*/142PT4[i] = (pml4_entry_t)PT3;143PT4[i] |= PG_V | PG_RW;144145/*146* Each slot of the L3 pages points to the147* same L2 page.148*/149PT3[i] = (pdp_entry_t)PT2;150PT3[i] |= PG_V | PG_RW;151152/*153* The L2 page slots are mapped with 2MB pages for 1GB.154*/155PT2[i] = (pd_entry_t)i * M(2);156PT2[i] |= PG_V | PG_RW | PG_PS;157}158} else {159PT4 = (pml4_entry_t *)G(4);160err = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData, 9,161(EFI_PHYSICAL_ADDRESS *)&PT4);162if (EFI_ERROR(err)) {163printf("Unable to allocate trampoline page table\n");164BS->FreePages(trampcode, 9);165if (copy_auto)166copy_staging = COPY_STAGING_AUTO;167return (ENOMEM);168}169170bzero(PT4, 9 * EFI_PAGE_SIZE);171172PT3_l = &PT4[NPML4EPG * 1];173PT3_u = &PT4[NPML4EPG * 2];174PT2_l0 = &PT4[NPML4EPG * 3];175PT2_l1 = &PT4[NPML4EPG * 4];176PT2_l2 = &PT4[NPML4EPG * 5];177PT2_l3 = &PT4[NPML4EPG * 6];178PT2_u0 = &PT4[NPML4EPG * 7];179PT2_u1 = &PT4[NPML4EPG * 8];180181/* 1:1 mapping of lower 4G */182PT4[0] = (pml4_entry_t)PT3_l | PG_V | PG_RW;183PT3_l[0] = (pdp_entry_t)PT2_l0 | PG_V | PG_RW;184PT3_l[1] = (pdp_entry_t)PT2_l1 | PG_V | PG_RW;185PT3_l[2] = (pdp_entry_t)PT2_l2 | PG_V | PG_RW;186PT3_l[3] = (pdp_entry_t)PT2_l3 | PG_V | PG_RW;187for (i = 0; i < 4 * NPDEPG; i++) {188PT2_l0[i] = ((pd_entry_t)i << PDRSHIFT) | PG_V |189PG_RW | PG_PS;190}191192/* mapping of kernel 2G below top */193PT4[NPML4EPG - 1] = (pml4_entry_t)PT3_u | PG_V | PG_RW;194PT3_u[NPDPEPG - 2] = (pdp_entry_t)PT2_u0 | PG_V | PG_RW;195PT3_u[NPDPEPG - 1] = (pdp_entry_t)PT2_u1 | PG_V | PG_RW;196/* compat mapping of phys @0 */197PT2_u0[0] = PG_PS | PG_V | PG_RW;198/* this maps past staging area */199for (i = 1; i < 2 * NPDEPG; i++) {200PT2_u0[i] = ((pd_entry_t)staging +201((pd_entry_t)i - 1) * NBPDR) |202PG_V | PG_RW | PG_PS;203}204}205206printf("staging %#lx (%scopying) tramp %p PT4 %p\n",207staging, copy_staging == COPY_STAGING_ENABLE ? "" : "not ",208trampoline, PT4);209printf("Start @ 0x%lx ...\n", ehdr->e_entry);210211/*212* we have to cleanup here because net_cleanup() doesn't work after213* we call ExitBootServices214*/215dev_cleanup();216217efi_time_fini();218err = bi_load(fp->f_args, &modulep, &kernend, true);219if (err != 0) {220efi_time_init();221if (copy_auto)222copy_staging = COPY_STAGING_AUTO;223return (err);224}225226trampoline(trampstack, copy_staging == COPY_STAGING_ENABLE ?227efi_copy_finish : efi_copy_finish_nop, kernend, modulep,228PT4, ehdr->e_entry);229230panic("exec returned");231}232233static int234elf64_obj_exec(struct preloaded_file *fp)235{236237return (EFTYPE);238}239240241