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 #include <stand.h> 31 #include <sys/param.h> 32 #include <sys/reboot.h> 33 #include <sys/linker.h> 34 #include <machine/bootinfo.h> 35 #include <machine/cpufunc.h> 36 #include <machine/metadata.h> 37 #include <machine/psl.h> 38 #include <machine/specialreg.h> 39 #include "bootstrap.h" 40 #include "libi386.h" 41 #include "btxv86.h" 42 43 #ifdef LOADER_GELI_SUPPORT 44 #include "geliboot.h" 45 #endif 46 47 /* 48 * Copy module-related data into the load area, where it can be 49 * used as a directory for loaded modules. 50 * 51 * Module data is presented in a self-describing format. Each datum 52 * is preceded by a 32-bit identifier and a 32-bit size field. 53 * 54 * Currently, the following data are saved: 55 * 56 * MOD_NAME (variable) module name (string) 57 * MOD_TYPE (variable) module type (string) 58 * MOD_ARGS (variable) module parameters (string) 59 * MOD_ADDR sizeof(vm_offset_t) module load address 60 * MOD_SIZE sizeof(size_t) module size 61 * MOD_METADATA (variable) type-specific metadata 62 */ 63 #define COPY32(v, a, c) { \ 64 uint32_t x = (v); \ 65 if (c) \ 66 i386_copyin(&x, a, sizeof(x)); \ 67 a += sizeof(x); \ 68 } 69 70 #define MOD_STR(t, a, s, c) { \ 71 COPY32(t, a, c); \ 72 COPY32(strlen(s) + 1, a, c); \ 73 if (c) \ 74 i386_copyin(s, a, strlen(s) + 1); \ 75 a += roundup(strlen(s) + 1, sizeof(uint64_t));\ 76 } 77 78 #define MOD_NAME(a, s, c) MOD_STR(MODINFO_NAME, a, s, c) 79 #define MOD_TYPE(a, s, c) MOD_STR(MODINFO_TYPE, a, s, c) 80 #define MOD_ARGS(a, s, c) MOD_STR(MODINFO_ARGS, a, s, c) 81 82 #define MOD_VAR(t, a, s, c) { \ 83 COPY32(t, a, c); \ 84 COPY32(sizeof(s), a, c); \ 85 if (c) \ 86 i386_copyin(&s, a, sizeof(s)); \ 87 a += roundup(sizeof(s), sizeof(uint64_t)); \ 88 } 89 90 #define MOD_ADDR(a, s, c) MOD_VAR(MODINFO_ADDR, a, s, c) 91 #define MOD_SIZE(a, s, c) MOD_VAR(MODINFO_SIZE, a, s, c) 92 93 #define MOD_METADATA(a, mm, c) { \ 94 COPY32(MODINFO_METADATA | mm->md_type, a, c); \ 95 COPY32(mm->md_size, a, c); \ 96 if (c) \ 97 i386_copyin(mm->md_data, a, mm->md_size); \ 98 a += roundup(mm->md_size, sizeof(uint64_t));\ 99 } 100 101 #define MOD_END(a, c) { \ 102 COPY32(MODINFO_END, a, c); \ 103 COPY32(0, a, c); \ 104 } 105 106 static vm_offset_t 107 bi_copymodules64(vm_offset_t addr) 108 { 109 struct preloaded_file *fp; 110 struct file_metadata *md; 111 int c; 112 uint64_t v; 113 114 c = addr != 0; 115 /* start with the first module on the list, should be the kernel */ 116 for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) { 117 118 MOD_NAME(addr, fp->f_name, c); /* this field must come first */ 119 MOD_TYPE(addr, fp->f_type, c); 120 if (fp->f_args) 121 MOD_ARGS(addr, fp->f_args, c); 122 v = fp->f_addr; 123 MOD_ADDR(addr, v, c); 124 v = fp->f_size; 125 MOD_SIZE(addr, v, c); 126 for (md = fp->f_metadata; md != NULL; md = md->md_next) 127 if (!(md->md_type & MODINFOMD_NOCOPY)) 128 MOD_METADATA(addr, md, c); 129 } 130 MOD_END(addr, c); 131 return(addr); 132 } 133 134 /* 135 * Check to see if this CPU supports long mode. 136 */ 137 static int 138 bi_checkcpu(void) 139 { 140 char *cpu_vendor; 141 int vendor[3]; 142 int eflags; 143 unsigned int regs[4]; 144 145 /* Check for presence of "cpuid". */ 146 eflags = read_eflags(); 147 write_eflags(eflags ^ PSL_ID); 148 if (!((eflags ^ read_eflags()) & PSL_ID)) 149 return (0); 150 151 /* Fetch the vendor string. */ 152 do_cpuid(0, regs); 153 vendor[0] = regs[1]; 154 vendor[1] = regs[3]; 155 vendor[2] = regs[2]; 156 cpu_vendor = (char *)vendor; 157 158 /* Check for vendors that support AMD features. */ 159 if (strncmp(cpu_vendor, INTEL_VENDOR_ID, 12) != 0 && 160 strncmp(cpu_vendor, AMD_VENDOR_ID, 12) != 0 && 161 strncmp(cpu_vendor, HYGON_VENDOR_ID, 12) != 0 && 162 strncmp(cpu_vendor, CENTAUR_VENDOR_ID, 12) != 0) 163 return (0); 164 165 /* Has to support AMD features. */ 166 do_cpuid(0x80000000, regs); 167 if (!(regs[0] >= 0x80000001)) 168 return (0); 169 170 /* Check for long mode. */ 171 do_cpuid(0x80000001, regs); 172 return (regs[3] & AMDID_LM); 173 } 174 175 /* 176 * Load the information expected by an amd64 kernel. 177 * 178 * - The 'boothowto' argument is constructed 179 * - The 'bootdev' argument is constructed 180 * - The 'bootinfo' struct is constructed, and copied into the kernel space. 181 * - The kernel environment is copied into kernel space. 182 * - Module metadata are formatted and placed in kernel space. 183 */ 184 int 185 bi_load64(char *args, vm_offset_t *modulep, 186 vm_offset_t *kernendp, int add_smap) 187 { 188 struct preloaded_file *xp, *kfp; 189 struct i386_devdesc *rootdev; 190 struct file_metadata *md; 191 uint64_t kernend; 192 uint64_t envp; 193 uint64_t module; 194 uint64_t addr; 195 vm_offset_t size; 196 char *rootdevname; 197 int howto; 198 199 if (!bi_checkcpu()) { 200 printf("CPU doesn't support long mode\n"); 201 return (EINVAL); 202 } 203 204 howto = bi_getboothowto(args); 205 206 /* 207 * Allow the environment variable 'rootdev' to override the supplied device 208 * This should perhaps go to MI code and/or have $rootdev tested/set by 209 * MI code before launching the kernel. 210 */ 211 rootdevname = getenv("rootdev"); 212 i386_getdev((void **)(&rootdev), rootdevname, NULL); 213 if (rootdev == NULL) { /* bad $rootdev/$currdev */ 214 printf("can't determine root device\n"); 215 return(EINVAL); 216 } 217 218 /* Try reading the /etc/fstab file to select the root device */ 219 getrootmount(i386_fmtdev((void *)rootdev)); 220 221 addr = 0; 222 /* find the last module in the chain */ 223 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) { 224 if (addr < (xp->f_addr + xp->f_size)) 225 addr = xp->f_addr + xp->f_size; 226 } 227 /* pad to a page boundary */ 228 addr = roundup(addr, PAGE_SIZE); 229 230 addr = build_font_module(addr); 231 232 /* place the metadata before anything */ 233 module = *modulep = addr; 234 235 kfp = file_findfile(NULL, "elf kernel"); 236 if (kfp == NULL) 237 kfp = file_findfile(NULL, "elf64 kernel"); 238 if (kfp == NULL) 239 panic("can't find kernel file"); 240 kernend = 0; /* fill it in later */ 241 file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto); 242 file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp); 243 file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend); 244 file_addmetadata(kfp, MODINFOMD_MODULEP, sizeof module, &module); 245 if (add_smap != 0) 246 bios_addsmapdata(kfp); 247 #ifdef LOADER_GELI_SUPPORT 248 geli_export_key_metadata(kfp); 249 #endif 250 bi_load_vbe_data(kfp); 251 252 size = bi_copymodules64(0); 253 254 /* copy our environment */ 255 envp = roundup(addr + size, PAGE_SIZE); 256 addr = bi_copyenv(envp); 257 258 /* set kernend */ 259 kernend = roundup(addr, PAGE_SIZE); 260 *kernendp = kernend; 261 262 /* patch MODINFOMD_KERNEND */ 263 md = file_findmetadata(kfp, MODINFOMD_KERNEND); 264 bcopy(&kernend, md->md_data, sizeof kernend); 265 266 /* patch MODINFOMD_ENVP */ 267 md = file_findmetadata(kfp, MODINFOMD_ENVP); 268 bcopy(&envp, md->md_data, sizeof envp); 269 270 /* copy module list and metadata */ 271 (void)bi_copymodules64(*modulep); 272 273 return(0); 274 } 275