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 * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
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 * 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 Soomestatic EFI_GUID acpi20_guid = ACPI_20_TABLE_GUID;
199767f8919635c4928607450d9e0abb932109ceToomas Soomeextern int bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int elf64_exec(struct preloaded_file *amp);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int elf64_obj_exec(struct preloaded_file *amp);
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 Soomestatic void (*trampoline)(uint64_t stack, void *copy_finish, uint64_t kernend,
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 vm_offset_t modulep, kernend, trampcode, trampstack;
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(buf, "0x%016llx", (unsigned long long)rsdp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome strncpy(buf, rsdp->OemId, sizeof(rsdp->OemId));
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(buf, "0x%016x", rsdp->RsdtPhysicalAddress);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* XXX extended checksum? */
199767f8919635c4928607450d9e0abb932109ceToomas Soome (unsigned long long)rsdp->XsdtPhysicalAddress);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((md = file_findmetadata(fp, MODINFOMD_ELFHDR)) == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData, 1,
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy((void *)&amd64_tramp, (void *)trampcode, amd64_tramp_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData, 3,
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 for (i = 0; i < 512; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Each slot of the L4 pages points to the same L3 page. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Each slot of the L3 pages points to the same L2 page. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* The L2 page slots are mapped with 2MB pages for 1GB. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome trampoline(trampstack, efi_copy_finish, kernend, modulep, PT4,