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