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 "modinfo.h" 41 #include "libi386.h" 42 #include "btxv86.h" 43 44 #ifdef LOADER_GELI_SUPPORT 45 #include "geliboot.h" 46 #endif 47 48 /* 49 * Check to see if this CPU supports long mode. 50 */ 51 static int 52 bi_checkcpu(void) 53 { 54 char *cpu_vendor; 55 int vendor[3]; 56 int eflags; 57 unsigned int regs[4]; 58 59 /* Check for presence of "cpuid". */ 60 eflags = read_eflags(); 61 write_eflags(eflags ^ PSL_ID); 62 if (!((eflags ^ read_eflags()) & PSL_ID)) 63 return (0); 64 65 /* Fetch the vendor string. */ 66 do_cpuid(0, regs); 67 vendor[0] = regs[1]; 68 vendor[1] = regs[3]; 69 vendor[2] = regs[2]; 70 cpu_vendor = (char *)vendor; 71 72 /* Check for vendors that support AMD features. */ 73 if (strncmp(cpu_vendor, INTEL_VENDOR_ID, 12) != 0 && 74 strncmp(cpu_vendor, AMD_VENDOR_ID, 12) != 0 && 75 strncmp(cpu_vendor, HYGON_VENDOR_ID, 12) != 0 && 76 strncmp(cpu_vendor, CENTAUR_VENDOR_ID, 12) != 0) 77 return (0); 78 79 /* Has to support AMD features. */ 80 do_cpuid(0x80000000, regs); 81 if (!(regs[0] >= 0x80000001)) 82 return (0); 83 84 /* Check for long mode. */ 85 do_cpuid(0x80000001, regs); 86 return (regs[3] & AMDID_LM); 87 } 88 89 /* 90 * Load the information expected by an amd64 kernel. 91 * 92 * - The 'boothowto' argument is constructed 93 * - The 'bootdev' argument is constructed 94 * - The 'bootinfo' struct is constructed, and copied into the kernel space. 95 * - The kernel environment is copied into kernel space. 96 * - Module metadata are formatted and placed in kernel space. 97 */ 98 int 99 bi_load64(char *args, vm_offset_t *modulep, 100 vm_offset_t *kernendp, int add_smap) 101 { 102 struct preloaded_file *xp, *kfp; 103 struct i386_devdesc *rootdev; 104 struct file_metadata *md; 105 uint64_t kernend; 106 uint64_t envp; 107 uint64_t module; 108 uint64_t addr; 109 vm_offset_t size; 110 char *rootdevname; 111 int howto; 112 113 if (!bi_checkcpu()) { 114 printf("CPU doesn't support long mode\n"); 115 return (EINVAL); 116 } 117 118 howto = bi_getboothowto(args); 119 120 /* 121 * Allow the environment variable 'rootdev' to override the supplied device 122 * This should perhaps go to MI code and/or have $rootdev tested/set by 123 * MI code before launching the kernel. 124 */ 125 rootdevname = getenv("rootdev"); 126 i386_getdev((void **)(&rootdev), rootdevname, NULL); 127 if (rootdev == NULL) { /* bad $rootdev/$currdev */ 128 printf("can't determine root device\n"); 129 return(EINVAL); 130 } 131 132 /* Try reading the /etc/fstab file to select the root device */ 133 getrootmount(devformat(&rootdev->dd)); 134 135 addr = 0; 136 /* find the last module in the chain */ 137 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) { 138 if (addr < (xp->f_addr + xp->f_size)) 139 addr = xp->f_addr + xp->f_size; 140 } 141 /* pad to a page boundary */ 142 addr = roundup(addr, PAGE_SIZE); 143 144 addr = build_font_module(addr); 145 146 /* place the metadata before anything */ 147 module = *modulep = addr; 148 149 kfp = file_findfile(NULL, "elf kernel"); 150 if (kfp == NULL) 151 kfp = file_findfile(NULL, "elf64 kernel"); 152 if (kfp == NULL) 153 panic("can't find kernel file"); 154 kernend = 0; /* fill it in later */ 155 file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto); 156 file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp); 157 file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend); 158 file_addmetadata(kfp, MODINFOMD_MODULEP, sizeof module, &module); 159 if (add_smap != 0) 160 bios_addsmapdata(kfp); 161 #ifdef LOADER_GELI_SUPPORT 162 geli_export_key_metadata(kfp); 163 #endif 164 bi_load_vbe_data(kfp); 165 166 size = md_copymodules(0, true); 167 168 /* copy our environment */ 169 envp = roundup(addr + size, PAGE_SIZE); 170 addr = md_copyenv(envp); 171 172 /* set kernend */ 173 kernend = roundup(addr, PAGE_SIZE); 174 *kernendp = kernend; 175 176 /* patch MODINFOMD_KERNEND */ 177 md = file_findmetadata(kfp, MODINFOMD_KERNEND); 178 bcopy(&kernend, md->md_data, sizeof kernend); 179 180 /* patch MODINFOMD_ENVP */ 181 md = file_findmetadata(kfp, MODINFOMD_ENVP); 182 bcopy(&envp, md->md_data, sizeof envp); 183 184 /* copy module list and metadata */ 185 (void)md_copymodules(*modulep, true); 186 187 return(0); 188 } 189