199767f8919635c4928607450d9e0abb932109ceToomas Soome/*-
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 2014 The FreeBSD Foundation
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * are met:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1. Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation and/or other materials provided with the distribution.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
199767f8919635c4928607450d9e0abb932109ceToomas Soome * SUCH DAMAGE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/cdefs.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome__FBSDID("$FreeBSD$");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define __ELF_WORD_SIZE 64
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/param.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/exec.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/linker.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <string.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <machine/elf.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <stand.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <efi.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <efilib.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "bootstrap.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "platform/acfreebsd.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "acconfig.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define ACPI_SYSTEM_XFACE
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "actypes.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "actbl.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "loader_efi.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic EFI_GUID acpi_guid = ACPI_TABLE_GUID;
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic EFI_GUID acpi20_guid = ACPI_20_TABLE_GUID;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeextern int bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int elf64_exec(struct preloaded_file *amp);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int elf64_obj_exec(struct preloaded_file *amp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic struct file_format amd64_elf = { elf64_loadfile, elf64_exec };
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic struct file_format amd64_elf_obj = { elf64_obj_loadfile, elf64_obj_exec };
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct file_format *file_formats[] = {
199767f8919635c4928607450d9e0abb932109ceToomas Soome &amd64_elf,
199767f8919635c4928607450d9e0abb932109ceToomas Soome &amd64_elf_obj,
199767f8919635c4928607450d9e0abb932109ceToomas Soome NULL
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define PG_V 0x001
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define PG_RW 0x002
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define PG_U 0x004
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define PG_PS 0x080
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soometypedef u_int64_t p4_entry_t;
199767f8919635c4928607450d9e0abb932109ceToomas Soometypedef u_int64_t p3_entry_t;
199767f8919635c4928607450d9e0abb932109ceToomas Soometypedef u_int64_t p2_entry_t;
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic p4_entry_t *PT4;
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic p3_entry_t *PT3;
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic p2_entry_t *PT2;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void (*trampoline)(uint64_t stack, void *copy_finish, uint64_t kernend,
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint64_t modulep, p4_entry_t *pagetable,
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint64_t entry);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeextern uintptr_t amd64_tramp;
199767f8919635c4928607450d9e0abb932109ceToomas Soomeextern uint32_t amd64_tramp_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * There is an ELF kernel and one or more ELF modules loaded.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * We wish to start executing the kernel image, so make such
199767f8919635c4928607450d9e0abb932109ceToomas Soome * preparations as are required, and do so.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomeelf64_exec(struct preloaded_file *fp)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct file_metadata *md;
199767f8919635c4928607450d9e0abb932109ceToomas Soome Elf_Ehdr *ehdr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome vm_offset_t modulep, kernend, trampcode, trampstack;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int err, i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ACPI_TABLE_RSDP *rsdp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char buf[24];
199767f8919635c4928607450d9e0abb932109ceToomas Soome int revision;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome rsdp = efi_get_table(&acpi20_guid);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rsdp == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome rsdp = efi_get_table(&acpi_guid);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rsdp != NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(buf, "0x%016llx", (unsigned long long)rsdp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("hint.acpi.0.rsdp", buf, 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome revision = rsdp->Revision;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (revision == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome revision = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(buf, "%d", revision);
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("hint.acpi.0.revision", buf, 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome strncpy(buf, rsdp->OemId, sizeof(rsdp->OemId));
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf[sizeof(rsdp->OemId)] = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("hint.acpi.0.oem", buf, 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(buf, "0x%016x", rsdp->RsdtPhysicalAddress);
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("hint.acpi.0.rsdt", buf, 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (revision >= 2) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* XXX extended checksum? */
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(buf, "0x%016llx",
199767f8919635c4928607450d9e0abb932109ceToomas Soome (unsigned long long)rsdp->XsdtPhysicalAddress);
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("hint.acpi.0.xsdt", buf, 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(buf, "%d", rsdp->Length);
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("hint.acpi.0.xsdt_length", buf, 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((md = file_findmetadata(fp, MODINFOMD_ELFHDR)) == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(EFTYPE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome ehdr = (Elf_Ehdr *)&(md->md_data);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome trampcode = (vm_offset_t)0x0000000040000000;
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData, 1,
199767f8919635c4928607450d9e0abb932109ceToomas Soome (EFI_PHYSICAL_ADDRESS *)&trampcode);
199767f8919635c4928607450d9e0abb932109ceToomas Soome bzero((void *)trampcode, EFI_PAGE_SIZE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome trampstack = trampcode + EFI_PAGE_SIZE - 8;
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy((void *)&amd64_tramp, (void *)trampcode, amd64_tramp_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome trampoline = (void *)trampcode;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome PT4 = (p4_entry_t *)0x0000000040000000;
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData, 3,
199767f8919635c4928607450d9e0abb932109ceToomas Soome (EFI_PHYSICAL_ADDRESS *)&PT4);
199767f8919635c4928607450d9e0abb932109ceToomas Soome bzero(PT4, 3 * EFI_PAGE_SIZE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome PT3 = &PT4[512];
199767f8919635c4928607450d9e0abb932109ceToomas Soome PT2 = &PT3[512];
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This is kinda brutal, but every single 1GB VM memory segment points
199767f8919635c4928607450d9e0abb932109ceToomas Soome * to the same first 1GB of physical memory. But it is more than
199767f8919635c4928607450d9e0abb932109ceToomas Soome * adequate.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < 512; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Each slot of the L4 pages points to the same L3 page. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome PT4[i] = (p4_entry_t)PT3;
199767f8919635c4928607450d9e0abb932109ceToomas Soome PT4[i] |= PG_V | PG_RW | PG_U;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Each slot of the L3 pages points to the same L2 page. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome PT3[i] = (p3_entry_t)PT2;
199767f8919635c4928607450d9e0abb932109ceToomas Soome PT3[i] |= PG_V | PG_RW | PG_U;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* The L2 page slots are mapped with 2MB pages for 1GB. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome PT2[i] = i * (2 * 1024 * 1024);
199767f8919635c4928607450d9e0abb932109ceToomas Soome PT2[i] |= PG_V | PG_RW | PG_PS | PG_U;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Start @ 0x%lx ...\n", ehdr->e_entry);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = bi_load(fp->f_args, &modulep, &kernend);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (err != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(err);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome dev_cleanup();
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome trampoline(trampstack, efi_copy_finish, kernend, modulep, PT4,
199767f8919635c4928607450d9e0abb932109ceToomas Soome ehdr->e_entry);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome panic("exec returned");
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomeelf64_obj_exec(struct preloaded_file *fp __attribute((unused)))
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EFTYPE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}