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