1 /* $OpenBSD: exec_i386.c,v 1.3 2019/12/12 13:09:35 bluhm Exp $ */ 2 3 /* 4 * Copyright (c) 1997-1998 Michael Shalayeff 5 * Copyright (c) 1997 Tobias Weingartner 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 */ 30 31 #include <sys/param.h> 32 #include <sys/disklabel.h> 33 #include <dev/cons.h> 34 #include <lib/libsa/loadfile.h> 35 #include <machine/biosvar.h> 36 #include <machine/pte.h> 37 #include <machine/specialreg.h> 38 #include <stand/boot/bootarg.h> 39 40 #include "cmd.h" 41 #include "disk.h" 42 #include "libsa.h" 43 44 #ifdef SOFTRAID 45 #include <dev/softraidvar.h> 46 #include <lib/libsa/softraid.h> 47 #include "softraid_amd64.h" 48 #endif 49 50 #include <efi.h> 51 #include <efiapi.h> 52 #include "eficall.h" 53 #include "efiboot.h" 54 55 extern EFI_BOOT_SERVICES *BS; 56 57 typedef void (*startfuncp)(int, int, int, int, int, int, int, int) 58 __attribute__ ((noreturn)); 59 60 void ucode_load(void); 61 void protect_writeable(uint64_t, size_t); 62 extern struct cmd_state cmd; 63 64 char *bootmac = NULL; 65 66 void 67 run_loadfile(uint64_t *marks, int howto) 68 { 69 u_long entry; 70 #ifdef EXEC_DEBUG 71 extern int debug; 72 #endif 73 dev_t bootdev = bootdev_dip->bootdev; 74 size_t ac = BOOTARG_LEN; 75 caddr_t av = (caddr_t)BOOTARG_OFF; 76 bios_consdev_t cd; 77 extern int com_speed; /* from bioscons.c */ 78 extern int com_addr; 79 bios_ddb_t ddb; 80 extern int db_console; 81 bios_bootduid_t bootduid; 82 #ifdef SOFTRAID 83 bios_bootsr_t bootsr; 84 struct sr_boot_volume *bv; 85 #endif 86 int i; 87 u_long delta; 88 extern u_long efi_loadaddr; 89 90 if ((av = alloc(ac)) == NULL) 91 panic("alloc for bootarg"); 92 efi_makebootargs(); 93 delta = DEFAULT_KERNEL_ADDRESS - efi_loadaddr; 94 if (sa_cleanup != NULL) 95 (*sa_cleanup)(); 96 97 cd.consdev = cn_tab->cn_dev; 98 cd.conspeed = com_speed; 99 cd.consaddr = com_addr; 100 cd.consfreq = 0; 101 addbootarg(BOOTARG_CONSDEV, sizeof(cd), &cd); 102 103 if (bootmac != NULL) 104 addbootarg(BOOTARG_BOOTMAC, sizeof(bios_bootmac_t), bootmac); 105 106 if (db_console != -1) { 107 ddb.db_console = db_console; 108 addbootarg(BOOTARG_DDB, sizeof(ddb), &ddb); 109 } 110 111 bcopy(bootdev_dip->disklabel.d_uid, &bootduid.duid, sizeof(bootduid)); 112 addbootarg(BOOTARG_BOOTDUID, sizeof(bootduid), &bootduid); 113 114 ucode_load(); 115 116 #ifdef SOFTRAID 117 if (bootdev_dip->sr_vol != NULL) { 118 bv = bootdev_dip->sr_vol; 119 bzero(&bootsr, sizeof(bootsr)); 120 bcopy(&bv->sbv_uuid, &bootsr.uuid, sizeof(bootsr.uuid)); 121 if (bv->sbv_maskkey != NULL) 122 bcopy(bv->sbv_maskkey, &bootsr.maskkey, 123 sizeof(bootsr.maskkey)); 124 addbootarg(BOOTARG_BOOTSR, sizeof(bios_bootsr_t), &bootsr); 125 explicit_bzero(&bootsr, sizeof(bootsr)); 126 } 127 128 sr_clear_keys(); 129 #endif 130 131 entry = marks[MARK_ENTRY] & 0x0fffffff; 132 entry += delta; 133 134 printf("entry point at 0x%lx\n", entry); 135 136 /* Sync the memory map and call ExitBootServices() */ 137 efi_cleanup(); 138 139 /* Pass memory map to the kernel */ 140 mem_pass(); 141 142 /* 143 * This code may be used both for 64bit and 32bit. Make sure the 144 * bootarg is always 32bit, even on amd64. 145 */ 146 #ifdef __amd64__ 147 makebootargs32(av, &ac); 148 #else 149 makebootargs(av, &ac); 150 #endif 151 152 /* 153 * Move the loaded kernel image to the usual place after calling 154 * ExitBootServices(). 155 */ 156 #ifdef __amd64__ 157 protect_writeable(marks[MARK_START] + delta, 158 marks[MARK_END] - marks[MARK_START]); 159 #endif 160 memmove((void *)marks[MARK_START] + delta, (void *)marks[MARK_START], 161 marks[MARK_END] - marks[MARK_START]); 162 for (i = 0; i < MARK_MAX; i++) 163 marks[i] += delta; 164 165 #ifdef __amd64__ 166 (*run_i386)((u_long)run_i386, entry, howto, bootdev, BOOTARG_APIVER, 167 marks[MARK_END], extmem, cnvmem, ac, (intptr_t)av); 168 #else 169 /* stack and the gung is ok at this point, so, no need for asm setup */ 170 (*(startfuncp)entry)(howto, bootdev, BOOTARG_APIVER, marks[MARK_END], 171 extmem, cnvmem, ac, (int)av); 172 #endif 173 /* not reached */ 174 } 175 176 void 177 ucode_load(void) 178 { 179 EFI_PHYSICAL_ADDRESS addr; 180 uint32_t model, family, stepping; 181 uint32_t dummy, signature; 182 uint32_t vendor[4]; 183 bios_ucode_t uc; 184 struct stat sb; 185 char path[128]; 186 size_t buflen; 187 char *buf; 188 int fd; 189 190 CPUID(0, dummy, vendor[0], vendor[2], vendor[1]); 191 vendor[3] = 0; /* NULL-terminate */ 192 if (strcmp((char *)vendor, "GenuineIntel") != 0) 193 return; 194 195 CPUID(1, signature, dummy, dummy, dummy); 196 family = (signature >> 8) & 0x0f; 197 model = (signature >> 4) & 0x0f; 198 if (family == 0x6 || family == 0xf) { 199 family += (signature >> 20) & 0xff; 200 model += ((signature >> 16) & 0x0f) << 4; 201 } 202 stepping = (signature >> 0) & 0x0f; 203 204 snprintf(path, sizeof(path), "%s:/etc/firmware/intel/%02x-%02x-%02x", 205 cmd.bootdev, family, model, stepping); 206 207 fd = open(path, 0); 208 if (fd == -1) 209 return; 210 211 if (fstat(fd, &sb) == -1) 212 return; 213 214 buflen = sb.st_size; 215 addr = 16 * 1024 * 1024; 216 if (EFI_CALL(BS->AllocatePages, AllocateMaxAddress, EfiLoaderData, 217 EFI_SIZE_TO_PAGES(buflen), &addr) != EFI_SUCCESS) { 218 printf("cannot allocate memory for ucode\n"); 219 return; 220 } 221 buf = (char *)((paddr_t)addr); 222 223 if (read(fd, buf, buflen) != buflen) { 224 close(fd); 225 return; 226 } 227 228 uc.uc_addr = (uint64_t)buf; 229 uc.uc_size = (uint64_t)buflen; 230 addbootarg(BOOTARG_UCODE, sizeof(uc), &uc); 231 232 close(fd); 233 } 234 235 #ifdef __amd64__ 236 void 237 protect_writeable(uint64_t addr, size_t len) 238 { 239 uint64_t end = addr + len; 240 uint64_t *cr3, *p; 241 size_t off; 242 243 __asm volatile("movq %%cr3, %0;" : "=r"(cr3) : :); 244 245 for (addr &= ~(uint64_t)PAGE_MASK; addr < end; addr += PAGE_SIZE) { 246 off = (addr & L4_MASK) >> L4_SHIFT; 247 p = (void *)(cr3[off] & ~(uint64_t)PAGE_MASK); 248 off = (addr & L3_MASK) >> L3_SHIFT; 249 p = (void *)(p[off] & ~(uint64_t)PAGE_MASK); 250 off = (addr & L2_MASK) >> L2_SHIFT; 251 p = (void *)(p[off] & ~(uint64_t)PAGE_MASK); 252 off = (addr & L1_MASK) >> L1_SHIFT; 253 p[off] |= PG_RW; 254 } 255 256 /* tlb flush */ 257 __asm volatile("movq %0,%%cr3" : : "r"(cr3) :); 258 } 259 #endif 260