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