1 /*- 2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #define __ELF_WORD_SIZE 64 31 #include <sys/param.h> 32 #include <sys/exec.h> 33 #include <sys/linker.h> 34 #ifdef DEBUG 35 #include <machine/_inttypes.h> 36 #endif 37 #include <string.h> 38 #include <i386/include/bootinfo.h> 39 #include <machine/elf.h> 40 #include <stand.h> 41 42 #include "bootstrap.h" 43 #include "libuserboot.h" 44 45 static int elf64_exec(struct preloaded_file *amp); 46 static int elf64_obj_exec(struct preloaded_file *amp); 47 48 struct file_format amd64_elf = { elf64_loadfile, elf64_exec }; 49 struct file_format amd64_elf_obj = { elf64_obj_loadfile, elf64_obj_exec }; 50 51 #define MSR_EFER 0xc0000080 52 #define EFER_LME 0x00000100 53 #define EFER_LMA 0x00000400 /* Long mode active (R) */ 54 #define CR4_PAE 0x00000020 55 #define CR4_VMXE (1UL << 13) 56 #define CR4_PSE 0x00000010 57 #define CR0_PG 0x80000000 58 #define CR0_PE 0x00000001 /* Protected mode Enable */ 59 #define CR0_NE 0x00000020 /* Numeric Error enable (EX16 vs IRQ13) */ 60 61 #define PG_V 0x001 62 #define PG_RW 0x002 63 #define PG_PS 0x080 64 65 typedef uint64_t p4_entry_t; 66 typedef uint64_t p3_entry_t; 67 typedef uint64_t p2_entry_t; 68 69 #define GUEST_NULL_SEL 0 70 #define GUEST_CODE_SEL 1 71 #define GUEST_DATA_SEL 2 72 #define GUEST_GDTR_LIMIT (3 * 8 - 1) 73 74 static void 75 setup_freebsd_gdt(uint64_t *gdtr) 76 { 77 gdtr[GUEST_NULL_SEL] = 0; 78 gdtr[GUEST_CODE_SEL] = 0x0020980000000000; 79 gdtr[GUEST_DATA_SEL] = 0x0000900000000000; 80 } 81 82 /* 83 * There is an ELF kernel and one or more ELF modules loaded. 84 * We wish to start executing the kernel image, so make such 85 * preparations as are required, and do so. 86 */ 87 static int 88 elf64_exec(struct preloaded_file *fp) 89 { 90 struct file_metadata *md; 91 Elf_Ehdr *ehdr; 92 vm_offset_t modulep, kernend; 93 int err; 94 int i; 95 uint32_t stack[1024]; 96 p4_entry_t PT4[512]; 97 p3_entry_t PT3[512]; 98 p2_entry_t PT2[512]; 99 uint64_t gdtr[3]; 100 101 if ((md = file_findmetadata(fp, MODINFOMD_ELFHDR)) == NULL) 102 return(EFTYPE); 103 ehdr = (Elf_Ehdr *)&(md->md_data); 104 105 err = bi_load64(fp->f_args, &modulep, &kernend); 106 if (err != 0) 107 return(err); 108 109 bzero(PT4, PAGE_SIZE); 110 bzero(PT3, PAGE_SIZE); 111 bzero(PT2, PAGE_SIZE); 112 113 /* 114 * Build a scratch stack at physical 0x1000, page tables: 115 * PT4 at 0x2000, 116 * PT3 at 0x3000, 117 * PT2 at 0x4000, 118 * gdtr at 0x5000, 119 */ 120 121 /* 122 * This is kinda brutal, but every single 1GB VM memory segment 123 * points to the same first 1GB of physical memory. But it is 124 * more than adequate. 125 */ 126 for (i = 0; i < 512; i++) { 127 /* Each slot of the level 4 pages points to the same level 3 page */ 128 PT4[i] = (p4_entry_t) 0x3000; 129 PT4[i] |= PG_V | PG_RW; 130 131 /* Each slot of the level 3 pages points to the same level 2 page */ 132 PT3[i] = (p3_entry_t) 0x4000; 133 PT3[i] |= PG_V | PG_RW; 134 135 /* The level 2 page slots are mapped with 2MB pages for 1GB. */ 136 PT2[i] = i * (2 * 1024 * 1024); 137 PT2[i] |= PG_V | PG_RW | PG_PS; 138 } 139 140 #ifdef DEBUG 141 printf("Start @ %#"PRIx64" ...\n", ehdr->e_entry); 142 #endif 143 144 dev_cleanup(); 145 146 stack[0] = 0; /* return address */ 147 stack[1] = modulep; 148 stack[2] = kernend; 149 CALLBACK(copyin, stack, 0x1000, sizeof(stack)); 150 CALLBACK(copyin, PT4, 0x2000, sizeof(PT4)); 151 CALLBACK(copyin, PT3, 0x3000, sizeof(PT3)); 152 CALLBACK(copyin, PT2, 0x4000, sizeof(PT2)); 153 CALLBACK(setreg, 4, 0x1000); 154 155 CALLBACK(setmsr, MSR_EFER, EFER_LMA | EFER_LME); 156 CALLBACK(setcr, 4, CR4_PAE | CR4_VMXE); 157 CALLBACK(setcr, 3, 0x2000); 158 CALLBACK(setcr, 0, CR0_PG | CR0_PE | CR0_NE); 159 160 setup_freebsd_gdt(gdtr); 161 CALLBACK(copyin, gdtr, 0x5000, sizeof(gdtr)); 162 CALLBACK(setgdt, 0x5000, sizeof(gdtr)); 163 164 CALLBACK(exec, ehdr->e_entry); 165 166 panic("exec returned"); 167 } 168 169 static int 170 elf64_obj_exec(struct preloaded_file *fp) 171 { 172 173 return (EFTYPE); 174 } 175