1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 /* 30 * This file contains the functionality that mimics the boot operations 31 * on SPARC systems or the old boot.bin/multiboot programs on x86 systems. 32 * The x86 kernel now does everything on its own. 33 */ 34 35 #include <sys/types.h> 36 #include <sys/bootconf.h> 37 #include <sys/bootsvcs.h> 38 #include <sys/bootinfo.h> 39 #include <sys/multiboot.h> 40 #include <sys/bootvfs.h> 41 #include <sys/bootprops.h> 42 #include <sys/varargs.h> 43 #include <sys/param.h> 44 #include <sys/machparam.h> 45 #include <sys/archsystm.h> 46 #include <sys/boot_console.h> 47 #include <sys/cmn_err.h> 48 #include <sys/systm.h> 49 #include <sys/promif.h> 50 #include <sys/archsystm.h> 51 #include <sys/x86_archext.h> 52 #include <sys/kobj.h> 53 #include <sys/privregs.h> 54 #include <sys/sysmacros.h> 55 #include <sys/ctype.h> 56 #include <vm/kboot_mmu.h> 57 #include <vm/hat_pte.h> 58 #include "acpi_fw.h" 59 60 static int have_console = 0; /* set once primitive console is initialized */ 61 static char *boot_args = ""; 62 63 /* 64 * Debugging macros 65 */ 66 static uint_t kbm_debug = 0; 67 #define DBG_MSG(s) { if (kbm_debug) bop_printf(NULL, "%s", s); } 68 #define DBG(x) { if (kbm_debug) \ 69 bop_printf(NULL, "%s is %" PRIx64 "\n", #x, (uint64_t)(x)); \ 70 } 71 72 #define PUT_STRING(s) { \ 73 char *cp; \ 74 for (cp = (s); *cp; ++cp) \ 75 bcons_putchar(*cp); \ 76 } 77 78 struct xboot_info *xbootp; /* boot info from "glue" code in low memory */ 79 bootops_t bootop; /* simple bootops we'll pass on to kernel */ 80 struct bsys_mem bm; 81 82 static uintptr_t next_virt; /* next available virtual address */ 83 static paddr_t next_phys; /* next available physical address from dboot */ 84 static paddr_t high_phys = -(paddr_t)1; /* last used physical address */ 85 86 /* 87 * buffer for vsnprintf for console I/O 88 */ 89 #define BUFFERSIZE 256 90 static char buffer[BUFFERSIZE]; 91 /* 92 * stuff to store/report/manipulate boot property settings. 93 */ 94 typedef struct bootprop { 95 struct bootprop *bp_next; 96 char *bp_name; 97 uint_t bp_vlen; 98 char *bp_value; 99 } bootprop_t; 100 101 static bootprop_t *bprops = NULL; 102 static char *curr_page = NULL; /* ptr to avail bprop memory */ 103 static int curr_space = 0; /* amount of memory at curr_page */ 104 105 /* 106 * some allocator statistics 107 */ 108 static ulong_t total_bop_alloc_scratch = 0; 109 static ulong_t total_bop_alloc_kernel = 0; 110 111 static void build_firmware_properties(void); 112 113 /* 114 * Allocate aligned physical memory at boot time. This allocator allocates 115 * from the highest possible addresses. This avoids exhausting memory that 116 * would be useful for DMA buffers. 117 */ 118 paddr_t 119 do_bop_phys_alloc(uint64_t size, uint64_t align) 120 { 121 paddr_t pa = 0; 122 paddr_t start; 123 paddr_t end; 124 struct memlist *ml = (struct memlist *)xbootp->bi_phys_install; 125 126 /* 127 * Be careful if high memory usage is limited in startup.c 128 * Since there are holes in the low part of the physical address 129 * space we can treat physmem as a pfn (not just a pgcnt) and 130 * get a conservative upper limit. 131 */ 132 extern pgcnt_t physmem; 133 if (physmem != 0 && high_phys > pfn_to_pa(physmem)) 134 high_phys = pfn_to_pa(physmem); 135 136 /* 137 * find the highest available memory in physinstalled 138 */ 139 size = P2ROUNDUP(size, align); 140 for (; ml; ml = ml->next) { 141 start = ml->address; 142 end = P2ALIGN(start + ml->size, align); 143 if (start < next_phys) 144 start = next_phys; 145 if (end > high_phys) 146 end = P2ALIGN(high_phys, align); 147 148 if (end <= start) 149 continue; 150 if (end - start < size) 151 continue; 152 153 if (end - size > pa) 154 pa = end - size; 155 } 156 if (pa != 0) { 157 high_phys = pa; 158 return (pa); 159 } 160 panic("do_bop_phys_alloc(0x%" PRIx64 ", 0x%" PRIx64 ") Out of memory\n", 161 size, align); 162 /*NOTREACHED*/ 163 } 164 165 static uintptr_t 166 alloc_vaddr(size_t size, paddr_t align) 167 { 168 uintptr_t rv; 169 170 next_virt = P2ROUNDUP(next_virt, (uintptr_t)align); 171 rv = (uintptr_t)next_virt; 172 next_virt += size; 173 return (rv); 174 } 175 176 /* 177 * Allocate virtual memory. The size is always rounded up to a multiple 178 * of base pagesize. 179 */ 180 181 /*ARGSUSED*/ 182 static caddr_t 183 do_bsys_alloc(bootops_t *bop, caddr_t virthint, size_t size, int align) 184 { 185 paddr_t a = align; /* same type as pa for masking */ 186 uint_t pgsize; 187 paddr_t pa; 188 uintptr_t va; 189 ssize_t s; /* the aligned size */ 190 uint_t level; 191 uint_t is_kernel = (virthint != 0); 192 193 if (a < MMU_PAGESIZE) 194 a = MMU_PAGESIZE; 195 else if (!ISP2(a)) 196 prom_panic("do_bsys_alloc() incorrect alignment"); 197 size = P2ROUNDUP(size, MMU_PAGESIZE); 198 199 /* 200 * Use the next aligned virtual address if we weren't given one. 201 */ 202 if (virthint == NULL) { 203 virthint = (caddr_t)alloc_vaddr(size, a); 204 total_bop_alloc_scratch += size; 205 } else { 206 total_bop_alloc_kernel += size; 207 } 208 209 /* 210 * allocate the physical memory 211 */ 212 pa = do_bop_phys_alloc(size, a); 213 214 /* 215 * Add the mappings to the page tables, try large pages first. 216 */ 217 va = (uintptr_t)virthint; 218 s = size; 219 level = 1; 220 pgsize = xbootp->bi_use_pae ? TWO_MEG : FOUR_MEG; 221 if (xbootp->bi_use_largepage && a == pgsize) { 222 while (IS_P2ALIGNED(pa, pgsize) && IS_P2ALIGNED(va, pgsize) && 223 s >= pgsize) { 224 kbm_map(va, pa, level, is_kernel); 225 va += pgsize; 226 pa += pgsize; 227 s -= pgsize; 228 } 229 } 230 231 /* 232 * Map remaining pages use small mappings 233 */ 234 level = 0; 235 pgsize = MMU_PAGESIZE; 236 while (s > 0) { 237 kbm_map(va, pa, level, is_kernel); 238 va += pgsize; 239 pa += pgsize; 240 s -= pgsize; 241 } 242 return (virthint); 243 } 244 245 /* 246 * Free virtual memory - we'll just ignore these. 247 */ 248 /*ARGSUSED*/ 249 static void 250 do_bsys_free(bootops_t *bop, caddr_t virt, size_t size) 251 { 252 bop_printf(NULL, "do_bsys_free(virt=0x%p, size=0x%lx) ignored\n", 253 (void *)virt, size); 254 } 255 256 /* 257 * Old interface 258 */ 259 /*ARGSUSED*/ 260 static caddr_t 261 do_bsys_ealloc( 262 bootops_t *bop, 263 caddr_t virthint, 264 size_t size, 265 int align, 266 int flags) 267 { 268 prom_panic("unsupported call to BOP_EALLOC()\n"); 269 return (0); 270 } 271 272 273 static void 274 bsetprop(char *name, int nlen, void *value, int vlen) 275 { 276 uint_t size; 277 uint_t need_size; 278 bootprop_t *b; 279 280 /* 281 * align the size to 16 byte boundary 282 */ 283 size = sizeof (bootprop_t) + nlen + 1 + vlen; 284 size = (size + 0xf) & ~0xf; 285 if (size > curr_space) { 286 need_size = (size + (MMU_PAGEOFFSET)) & MMU_PAGEMASK; 287 curr_page = do_bsys_alloc(NULL, 0, need_size, MMU_PAGESIZE); 288 curr_space = need_size; 289 } 290 291 /* 292 * use a bootprop_t at curr_page and link into list 293 */ 294 b = (bootprop_t *)curr_page; 295 curr_page += sizeof (bootprop_t); 296 curr_space -= sizeof (bootprop_t); 297 b->bp_next = bprops; 298 bprops = b; 299 300 /* 301 * follow by name and ending zero byte 302 */ 303 b->bp_name = curr_page; 304 bcopy(name, curr_page, nlen); 305 curr_page += nlen; 306 *curr_page++ = 0; 307 curr_space -= nlen + 1; 308 309 /* 310 * copy in value, but no ending zero byte 311 */ 312 b->bp_value = curr_page; 313 b->bp_vlen = vlen; 314 if (vlen > 0) { 315 bcopy(value, curr_page, vlen); 316 curr_page += vlen; 317 curr_space -= vlen; 318 } 319 320 /* 321 * align new values of curr_page, curr_space 322 */ 323 while (curr_space & 0xf) { 324 ++curr_page; 325 --curr_space; 326 } 327 } 328 329 static void 330 bsetprops(char *name, char *value) 331 { 332 bsetprop(name, strlen(name), value, strlen(value) + 1); 333 } 334 335 static void 336 bsetprop64(char *name, uint64_t value) 337 { 338 bsetprop(name, strlen(name), (void *)&value, sizeof (value)); 339 } 340 341 static void 342 bsetpropsi(char *name, int value) 343 { 344 char prop_val[32]; 345 346 (void) snprintf(prop_val, sizeof (prop_val), "%d", value); 347 bsetprops(name, prop_val); 348 } 349 350 /* 351 * to find the size of the buffer to allocate 352 */ 353 /*ARGSUSED*/ 354 static int 355 do_bsys_getproplen(bootops_t *bop, char *name) 356 { 357 bootprop_t *b; 358 359 for (b = bprops; b; b = b->bp_next) { 360 if (strcmp(name, b->bp_name) != 0) 361 continue; 362 return (b->bp_vlen); 363 } 364 return (-1); 365 } 366 367 /* 368 * get the value associated with this name 369 */ 370 /*ARGSUSED*/ 371 static int 372 do_bsys_getprop(bootops_t *bop, char *name, void *value) 373 { 374 bootprop_t *b; 375 376 for (b = bprops; b; b = b->bp_next) { 377 if (strcmp(name, b->bp_name) != 0) 378 continue; 379 bcopy(b->bp_value, value, b->bp_vlen); 380 return (0); 381 } 382 return (-1); 383 } 384 385 /* 386 * get the name of the next property in succession from the standalone 387 */ 388 /*ARGSUSED*/ 389 static char * 390 do_bsys_nextprop(bootops_t *bop, char *name) 391 { 392 bootprop_t *b; 393 394 /* 395 * A null name is a special signal for the 1st boot property 396 */ 397 if (name == NULL || strlen(name) == 0) { 398 if (bprops == NULL) 399 return (NULL); 400 return (bprops->bp_name); 401 } 402 403 for (b = bprops; b; b = b->bp_next) { 404 if (name != b->bp_name) 405 continue; 406 b = b->bp_next; 407 if (b == NULL) 408 return (NULL); 409 return (b->bp_name); 410 } 411 return (NULL); 412 } 413 414 /* 415 * 2nd part of building the table of boot properties. This includes: 416 * - values from /boot/solaris/bootenv.rc (ie. eeprom(1m) values) 417 * 418 * lines look like one of: 419 * ^$ 420 * ^# comment till end of line 421 * setprop name 'value' 422 * setprop name value 423 * setprop name "value" 424 * 425 * we do single character I/O since this is really just looking at memory 426 */ 427 void 428 boot_prop_finish(void) 429 { 430 int fd; 431 char *line; 432 int c; 433 int bytes_read; 434 char *name; 435 int n_len; 436 char *value; 437 int v_len; 438 char *inputdev; /* these override the comand line if serial ports */ 439 char *outputdev; 440 char *consoledev; 441 442 DBG_MSG("Opening /boot/solaris/bootenv.rc\n"); 443 fd = BRD_OPEN(bfs_ops, "/boot/solaris/bootenv.rc", 0); 444 DBG(fd); 445 446 line = do_bsys_alloc(NULL, NULL, MMU_PAGESIZE, MMU_PAGESIZE); 447 while (fd >= 0) { 448 449 /* 450 * get a line 451 */ 452 for (c = 0; ; ++c) { 453 bytes_read = BRD_READ(bfs_ops, fd, line + c, 1); 454 if (bytes_read == 0) { 455 if (c == 0) 456 goto done; 457 break; 458 } 459 if (line[c] == '\n') 460 break; 461 } 462 line[c] = 0; 463 464 /* 465 * ignore comment lines 466 */ 467 c = 0; 468 while (ISSPACE(line[c])) 469 ++c; 470 if (line[c] == '#' || line[c] == 0) 471 continue; 472 473 /* 474 * must have "setprop " or "setprop\t" 475 */ 476 if (strncmp(line + c, "setprop ", 8) != 0 && 477 strncmp(line + c, "setprop\t", 8) != 0) 478 continue; 479 c += 8; 480 while (ISSPACE(line[c])) 481 ++c; 482 if (line[c] == 0) 483 continue; 484 485 /* 486 * gather up the property name 487 */ 488 name = line + c; 489 n_len = 0; 490 while (line[c] && !ISSPACE(line[c])) 491 ++n_len, ++c; 492 493 /* 494 * gather up the value, if any 495 */ 496 value = ""; 497 v_len = 0; 498 while (ISSPACE(line[c])) 499 ++c; 500 if (line[c] != 0) { 501 value = line + c; 502 while (line[c] && !ISSPACE(line[c])) 503 ++v_len, ++c; 504 } 505 506 if (v_len >= 2 && value[0] == value[v_len - 1] && 507 (value[0] == '\'' || value[0] == '"')) { 508 ++value; 509 v_len -= 2; 510 } 511 name[n_len] = 0; 512 if (v_len > 0) 513 value[v_len] = 0; 514 else 515 continue; 516 517 /* 518 * ignore "boot-file" property, it's now meaningless 519 */ 520 if (strcmp(name, "boot-file") == 0) 521 continue; 522 if (strcmp(name, "boot-args") == 0 && 523 strlen(boot_args) > 0) 524 continue; 525 526 /* 527 * If console was explicitly set on the command line it will 528 * override a setting in bootenv.rc 529 */ 530 if (strcmp(name, "console") == 0 && 531 do_bsys_getproplen(NULL, "console") > 0) 532 continue; 533 534 bsetprop(name, n_len, value, v_len + 1); 535 } 536 done: 537 if (fd >= 0) 538 BRD_CLOSE(bfs_ops, fd); 539 540 /* 541 * check to see if we have to override the default value of the console 542 */ 543 inputdev = line; 544 v_len = do_bsys_getproplen(NULL, "input-device"); 545 if (v_len > 0) 546 (void) do_bsys_getprop(NULL, "input-device", inputdev); 547 else 548 v_len = 0; 549 inputdev[v_len] = 0; 550 551 outputdev = inputdev + v_len + 1; 552 v_len = do_bsys_getproplen(NULL, "output-device"); 553 if (v_len > 0) 554 (void) do_bsys_getprop(NULL, "output-device", outputdev); 555 else 556 v_len = 0; 557 outputdev[v_len] = 0; 558 559 consoledev = outputdev + v_len + 1; 560 v_len = do_bsys_getproplen(NULL, "console"); 561 if (v_len > 0) 562 (void) do_bsys_getprop(NULL, "console", consoledev); 563 else 564 v_len = 0; 565 consoledev[v_len] = 0; 566 bcons_init2(inputdev, outputdev, consoledev); 567 568 if (strstr((char *)xbootp->bi_cmdline, "prom_debug") || kbm_debug) { 569 value = line; 570 bop_printf(NULL, "\nBoot properties:\n"); 571 name = ""; 572 while ((name = do_bsys_nextprop(NULL, name)) != NULL) { 573 bop_printf(NULL, "\t0x%p %s = ", (void *)name, name); 574 (void) do_bsys_getprop(NULL, name, value); 575 v_len = do_bsys_getproplen(NULL, name); 576 bop_printf(NULL, "len=%d ", v_len); 577 value[v_len] = 0; 578 bop_printf(NULL, "%s\n", value); 579 } 580 } 581 } 582 583 /* 584 * print formatted output 585 */ 586 /*PRINTFLIKE2*/ 587 /*ARGSUSED*/ 588 void 589 bop_printf(bootops_t *bop, char *fmt, ...) 590 { 591 va_list ap; 592 593 if (have_console == 0) 594 return; 595 596 va_start(ap, fmt); 597 (void) vsnprintf(buffer, BUFFERSIZE, fmt, ap); 598 va_end(ap); 599 PUT_STRING(buffer); 600 } 601 602 /* 603 * Another panic() variant; this one can be used even earlier during boot than 604 * prom_panic(). 605 */ 606 /*PRINTFLIKE1*/ 607 void 608 bop_panic(char *fmt, ...) 609 { 610 va_list ap; 611 612 va_start(ap, fmt); 613 bop_printf(NULL, fmt, ap); 614 va_end(ap); 615 616 bop_printf(NULL, "\nPress any key to reboot.\n"); 617 (void) bcons_getchar(); 618 bop_printf(NULL, "Resetting...\n"); 619 reset(); 620 } 621 622 /* 623 * Do a real mode interrupt BIOS call 624 */ 625 typedef struct bios_regs { 626 unsigned short ax, bx, cx, dx, si, di, bp, es, ds; 627 } bios_regs_t; 628 typedef int (*bios_func_t)(int, bios_regs_t *); 629 630 /*ARGSUSED*/ 631 static void 632 do_bsys_doint(bootops_t *bop, int intnum, struct bop_regs *rp) 633 { 634 static int firsttime = 1; 635 bios_func_t bios_func = (bios_func_t)(void *)(uintptr_t)0x5000; 636 bios_regs_t br; 637 638 /* 639 * The first time we do this, we have to copy the pre-packaged 640 * low memory bios call code image into place. 641 */ 642 if (firsttime) { 643 extern char bios_image[]; 644 extern uint32_t bios_size; 645 646 bcopy(bios_image, (void *)bios_func, bios_size); 647 firsttime = 0; 648 } 649 650 br.ax = rp->eax.word.ax; 651 br.bx = rp->ebx.word.bx; 652 br.cx = rp->ecx.word.cx; 653 br.dx = rp->edx.word.dx; 654 br.bp = rp->ebp.word.bp; 655 br.si = rp->esi.word.si; 656 br.di = rp->edi.word.di; 657 br.ds = rp->ds; 658 br.es = rp->es; 659 660 DBG_MSG("Doing BIOS call..."); 661 rp->eflags = bios_func(intnum, &br); 662 DBG_MSG("done\n"); 663 664 rp->eax.word.ax = br.ax; 665 rp->ebx.word.bx = br.bx; 666 rp->ecx.word.cx = br.cx; 667 rp->edx.word.dx = br.dx; 668 rp->ebp.word.bp = br.bp; 669 rp->esi.word.si = br.si; 670 rp->edi.word.di = br.di; 671 rp->ds = br.ds; 672 rp->es = br.es; 673 } 674 675 static struct boot_syscalls bop_sysp = { 676 bcons_getchar, 677 bcons_putchar, 678 bcons_ischar, 679 }; 680 681 static char *whoami; 682 683 #define BUFLEN 64 684 685 static void 686 setup_rarp_props(struct sol_netinfo *sip) 687 { 688 char buf[BUFLEN]; /* to hold ip/mac addrs */ 689 uint8_t *val; 690 691 val = (uint8_t *)&sip->sn_ciaddr; 692 (void) snprintf(buf, BUFLEN, "%d.%d.%d.%d", 693 val[0], val[1], val[2], val[3]); 694 bsetprops(BP_HOST_IP, buf); 695 696 val = (uint8_t *)&sip->sn_siaddr; 697 (void) snprintf(buf, BUFLEN, "%d.%d.%d.%d", 698 val[0], val[1], val[2], val[3]); 699 bsetprops(BP_SERVER_IP, buf); 700 701 if (sip->sn_giaddr != 0) { 702 val = (uint8_t *)&sip->sn_giaddr; 703 (void) snprintf(buf, BUFLEN, "%d.%d.%d.%d", 704 val[0], val[1], val[2], val[3]); 705 bsetprops(BP_ROUTER_IP, buf); 706 } 707 708 if (sip->sn_netmask != 0) { 709 val = (uint8_t *)&sip->sn_netmask; 710 (void) snprintf(buf, BUFLEN, "%d.%d.%d.%d", 711 val[0], val[1], val[2], val[3]); 712 bsetprops(BP_SUBNET_MASK, buf); 713 } 714 715 if (sip->sn_mactype != 4 || sip->sn_maclen != 6) { 716 bop_printf(NULL, "unsupported mac type %d, mac len %d\n", 717 sip->sn_mactype, sip->sn_maclen); 718 } else { 719 val = sip->sn_macaddr; 720 (void) snprintf(buf, BUFLEN, "%x:%x:%x:%x:%x:%x", 721 val[0], val[1], val[2], val[3], val[4], val[5]); 722 bsetprops(BP_BOOT_MAC, buf); 723 } 724 } 725 726 /* 727 * 1st pass at building the table of boot properties. This includes: 728 * - values set on the command line: -B a=x,b=y,c=z .... 729 * - known values we just compute (ie. from xbootp) 730 * - values from /boot/solaris/bootenv.rc (ie. eeprom(1m) values) 731 * 732 * the grub command line looked like: 733 * kernel boot-file [-B prop=value[,prop=value]...] [boot-args] 734 * 735 * whoami is the same as boot-file 736 */ 737 static void 738 build_boot_properties(void) 739 { 740 char *name; 741 int name_len; 742 char *value; 743 int value_len; 744 static int stdout_val = 0; 745 struct boot_modules *bm; 746 char *propbuf; 747 int quoted = 0; 748 int boot_arg_len; 749 uchar_t boot_device; 750 char str[3]; 751 multiboot_info_t *mbi; 752 int netboot; 753 struct sol_netinfo *sip; 754 755 /* 756 * These have to be done first, so that kobj_mount_root() works 757 */ 758 DBG_MSG("Building boot properties\n"); 759 propbuf = do_bsys_alloc(NULL, NULL, MMU_PAGESIZE, 0); 760 DBG((uintptr_t)propbuf); 761 if (xbootp->bi_module_cnt > 0) { 762 bm = xbootp->bi_modules; 763 bsetprop64("ramdisk_start", (uint64_t)(uintptr_t)bm->bm_addr); 764 bsetprop64("ramdisk_end", (uint64_t)(uintptr_t)bm->bm_addr + 765 bm->bm_size); 766 } 767 768 DBG_MSG("Parsing command line for boot properties\n"); 769 value = xbootp->bi_cmdline; 770 771 /* 772 * allocate memory to collect boot_args into 773 */ 774 boot_arg_len = strlen(xbootp->bi_cmdline) + 1; 775 boot_args = do_bsys_alloc(NULL, NULL, boot_arg_len, MMU_PAGESIZE); 776 boot_args[0] = 0; 777 boot_arg_len = 0; 778 779 while (ISSPACE(*value)) 780 ++value; 781 /* 782 * value now points at the boot-file 783 */ 784 value_len = 0; 785 while (value[value_len] && !ISSPACE(value[value_len])) 786 ++value_len; 787 if (value_len > 0) { 788 whoami = propbuf; 789 bcopy(value, whoami, value_len); 790 whoami[value_len] = 0; 791 bsetprops("boot-file", whoami); 792 /* 793 * strip leading path stuff from whoami, so running from 794 * PXE/miniroot makes sense. 795 */ 796 if (strstr(whoami, "/platform/") != NULL) 797 whoami = strstr(whoami, "/platform/"); 798 bsetprops("whoami", whoami); 799 } 800 801 /* 802 * Values forcibly set boot propertiex on the command line via -B. 803 * Allow use of quotes in values. Other stuff goes on kernel 804 * command line. 805 */ 806 name = value + value_len; 807 while (*name != 0) { 808 /* 809 * anything not " -B" is copied to the command line 810 */ 811 if (!ISSPACE(name[0]) || name[1] != '-' || name[2] != 'B') { 812 boot_args[boot_arg_len++] = *name; 813 boot_args[boot_arg_len] = 0; 814 ++name; 815 continue; 816 } 817 818 /* 819 * skip the " -B" and following white space 820 */ 821 name += 3; 822 while (ISSPACE(*name)) 823 ++name; 824 while (*name && !ISSPACE(*name)) { 825 value = strstr(name, "="); 826 if (value == NULL) 827 break; 828 name_len = value - name; 829 ++value; 830 value_len = 0; 831 quoted = 0; 832 for (; ; ++value_len) { 833 if (!value[value_len]) 834 break; 835 836 /* 837 * is this value quoted? 838 */ 839 if (value_len == 0 && 840 (value[0] == '\'' || value[0] == '"')) { 841 quoted = value[0]; 842 ++value_len; 843 } 844 845 /* 846 * In the quote accept any character, 847 * but look for ending quote. 848 */ 849 if (quoted) { 850 if (value[value_len] == quoted) 851 quoted = 0; 852 continue; 853 } 854 855 /* 856 * a comma or white space ends the value 857 */ 858 if (value[value_len] == ',' || 859 ISSPACE(value[value_len])) 860 break; 861 } 862 863 if (value_len == 0) { 864 bsetprop(name, name_len, "true", 5); 865 } else { 866 char *v = value; 867 int l = value_len; 868 if (v[0] == v[l - 1] && 869 (v[0] == '\'' || v[0] == '"')) { 870 ++v; 871 l -= 2; 872 } 873 bcopy(v, propbuf, l); 874 propbuf[l] = '\0'; 875 bsetprop(name, name_len, propbuf, 876 l + 1); 877 } 878 name = value + value_len; 879 while (*name == ',') 880 ++name; 881 } 882 } 883 884 /* 885 * set boot-args property 886 */ 887 bsetprops("boot-args", boot_args); 888 889 /* 890 * set the BIOS boot device from GRUB 891 */ 892 netboot = 0; 893 mbi = xbootp->bi_mb_info; 894 if (mbi != NULL && mbi->flags & 0x2) { 895 boot_device = mbi->boot_device >> 24; 896 if (boot_device == 0x20) 897 netboot++; 898 str[0] = (boot_device >> 4) + '0'; 899 str[1] = (boot_device & 0xf) + '0'; 900 str[2] = 0; 901 bsetprops("bios-boot-device", str); 902 } else { 903 netboot = 1; 904 } 905 906 /* 907 * In the netboot case, drives_info is overloaded with the dhcp ack. 908 * This is not multiboot compliant and requires special pxegrub! 909 */ 910 if (netboot && mbi->drives_length != 0) { 911 sip = (struct sol_netinfo *)(uintptr_t)mbi->drives_addr; 912 if (sip->sn_infotype == SN_TYPE_BOOTP) 913 bsetprop("bootp-response", sizeof ("bootp-response"), 914 (void *)(uintptr_t)mbi->drives_addr, 915 mbi->drives_length); 916 else if (sip->sn_infotype == SN_TYPE_BOOTP) 917 setup_rarp_props(sip); 918 } 919 bsetprop("stdout", strlen("stdout"), 920 &stdout_val, sizeof (stdout_val)); 921 922 /* 923 * more conjured up values for made up things.... 924 */ 925 bsetprops("mfg-name", "i86pc"); 926 bsetprops("impl-arch-name", "i86pc"); 927 928 /* 929 * Build firmware-provided system properties 930 */ 931 build_firmware_properties(); 932 933 /* 934 * Find out what these are: 935 * - cpuid_feature_ecx_include 936 * - cpuid_feature_ecx_exclude 937 * - cpuid_feature_edx_include 938 * - cpuid_feature_edx_exclude 939 * 940 * Find out what these are in multiboot: 941 * - bootp-response 942 * - netdev-path 943 * - fstype 944 */ 945 } 946 947 /* 948 * Install a temporary IDT that lets us catch errors in the boot time code. 949 * We shouldn't get any faults at all while this is installed, so we'll 950 * just generate a traceback and exit. 951 */ 952 #ifdef __amd64 953 static const int bcode_sel = B64CODE_SEL; 954 #else 955 static const int bcode_sel = B32CODE_SEL; 956 #endif 957 958 /* 959 * simple description of a stack frame (args are 32 bit only currently) 960 */ 961 typedef struct bop_frame { 962 struct bop_frame *old_frame; 963 pc_t retaddr; 964 long arg[1]; 965 } bop_frame_t; 966 967 void 968 bop_traceback(bop_frame_t *frame) 969 { 970 pc_t pc; 971 int cnt; 972 int a; 973 char *ksym; 974 ulong_t off; 975 976 bop_printf(NULL, "Stack traceback:\n"); 977 for (cnt = 0; cnt < 30; ++cnt) { /* up to 30 frames */ 978 pc = frame->retaddr; 979 if (pc == 0) 980 break; 981 ksym = kobj_getsymname(pc, &off); 982 if (ksym) 983 bop_printf(NULL, " %s+%lx", ksym, off); 984 else 985 bop_printf(NULL, " 0x%lx", pc); 986 987 frame = frame->old_frame; 988 if (frame == 0) { 989 bop_printf(NULL, "\n"); 990 break; 991 } 992 for (a = 0; a < 6; ++a) { /* try for 6 args */ 993 #if defined(__i386) 994 if ((void *)&frame->arg[a] == (void *)frame->old_frame) 995 break; 996 if (a == 0) 997 bop_printf(NULL, "("); 998 else 999 bop_printf(NULL, ","); 1000 bop_printf(NULL, "0x%lx", frame->arg[a]); 1001 #endif 1002 } 1003 bop_printf(NULL, ")\n"); 1004 } 1005 } 1006 1007 struct trapframe { 1008 ulong_t frame_ptr; /* %[er]bp pushed by our code */ 1009 ulong_t error_code; /* optional */ 1010 ulong_t inst_ptr; 1011 ulong_t code_seg; 1012 ulong_t flags_reg; 1013 #ifdef __amd64 1014 ulong_t stk_ptr; 1015 ulong_t stk_seg; 1016 #endif 1017 }; 1018 1019 void 1020 bop_trap(struct trapframe *tf) 1021 { 1022 bop_frame_t fakeframe; 1023 static int depth = 0; 1024 1025 /* 1026 * Check for an infinite loop of traps. Avoid bop_printf() here to 1027 * reduce code path and further possibility of failure. 1028 */ 1029 if (++depth > 2) { 1030 PUT_STRING("Nested trap, calling reset()\n"); 1031 reset(); 1032 } 1033 1034 /* 1035 * adjust the tf for optional error_code by detecting the code selector 1036 */ 1037 if (tf->code_seg != bcode_sel) 1038 tf = (struct trapframe *)((uintptr_t)tf - sizeof (ulong_t)); 1039 1040 bop_printf(NULL, "Unexpected trap\n"); 1041 bop_printf(NULL, "instruction pointer 0x%lx\n", tf->inst_ptr); 1042 bop_printf(NULL, "error code, optional 0x%lx\n", 1043 tf->error_code & 0xffffffff); 1044 bop_printf(NULL, "code segment 0x%lx\n", tf->code_seg & 0xffff); 1045 bop_printf(NULL, "flags register 0x%lx\n", tf->flags_reg); 1046 #ifdef __amd64 1047 bop_printf(NULL, "return %%rsp 0x%lx\n", tf->stk_ptr); 1048 bop_printf(NULL, "return %%ss 0x%lx\n", tf->stk_seg & 0xffff); 1049 #endif 1050 fakeframe.old_frame = (bop_frame_t *)tf->frame_ptr; 1051 fakeframe.retaddr = (pc_t)tf->inst_ptr; 1052 bop_printf(NULL, "Attempting stack backtrace:\n"); 1053 bop_traceback(&fakeframe); 1054 bop_panic("unexpected trap in early boot"); 1055 } 1056 1057 extern void bop_trap_handler(void); 1058 1059 static gate_desc_t bop_idt[NIDT]; 1060 1061 static desctbr_t bop_idt_info; 1062 1063 static void 1064 bop_idt_init(void) 1065 { 1066 int t; 1067 1068 bzero(&bop_idt, sizeof (bop_idt)); 1069 for (t = 0; t < NIDT; ++t) { 1070 set_gatesegd(&bop_idt[t], &bop_trap_handler, bcode_sel, 1071 SDT_SYSIGT, SEL_KPL); 1072 } 1073 bop_idt_info.dtr_limit = sizeof (bop_idt) - 1; 1074 bop_idt_info.dtr_base = (uintptr_t)&bop_idt; 1075 wr_idtr(&bop_idt_info); 1076 } 1077 1078 /* 1079 * This is where we enter the kernel. It dummies up the boot_ops and 1080 * boot_syscalls vectors and jumps off to _kobj_boot() 1081 */ 1082 void 1083 _start(struct xboot_info *xbp) 1084 { 1085 bootops_t *bops = &bootop; 1086 extern void _kobj_boot(); 1087 1088 /* 1089 * 1st off - initialize the console for any error messages 1090 */ 1091 xbootp = xbp; 1092 bcons_init((void *)xbootp->bi_cmdline); 1093 have_console = 1; 1094 1095 /* 1096 * enable debugging 1097 */ 1098 if (strstr((char *)xbootp->bi_cmdline, "kbm_debug")) 1099 kbm_debug = 1; 1100 1101 DBG_MSG("\n\n*** Entered Solaris in _start() cmdline is: "); 1102 DBG_MSG((char *)xbootp->bi_cmdline); 1103 DBG_MSG("\n\n\n"); 1104 1105 /* 1106 * Install an IDT to catch early pagefaults (shouldn't have any). 1107 * Also needed for kmdb. 1108 */ 1109 bop_idt_init(); 1110 1111 /* 1112 * only physinstalled is still used by startup. 1113 */ 1114 bm.physinstalled = xbp->bi_phys_install; 1115 bm.physavail = NULL; 1116 1117 /* 1118 * initialize the boot time allocator 1119 */ 1120 next_phys = xbootp->bi_next_paddr; 1121 DBG(next_phys); 1122 next_virt = (uintptr_t)xbootp->bi_next_vaddr; 1123 DBG(next_virt); 1124 DBG_MSG("Initializing boot time memory management..."); 1125 kbm_init(xbootp); 1126 DBG_MSG("done\n"); 1127 1128 /* 1129 * Fill in the bootops vector 1130 */ 1131 bops->bsys_version = BO_VERSION; 1132 bops->boot_mem = &bm; 1133 bops->bsys_alloc = do_bsys_alloc; 1134 bops->bsys_free = do_bsys_free; 1135 bops->bsys_getproplen = do_bsys_getproplen; 1136 bops->bsys_getprop = do_bsys_getprop; 1137 bops->bsys_nextprop = do_bsys_nextprop; 1138 bops->bsys_printf = bop_printf; 1139 bops->bsys_doint = do_bsys_doint; 1140 1141 /* 1142 * BOP_EALLOC() is no longer needed 1143 */ 1144 bops->bsys_ealloc = do_bsys_ealloc; 1145 1146 /* 1147 * 1148 */ 1149 DBG_MSG("Initializing boot properties:\n"); 1150 build_boot_properties(); 1151 1152 if (strstr((char *)xbootp->bi_cmdline, "prom_debug") || kbm_debug) { 1153 char *name; 1154 char *value; 1155 int len; 1156 1157 value = do_bsys_alloc(NULL, NULL, MMU_PAGESIZE, MMU_PAGESIZE); 1158 bop_printf(NULL, "\nBoot properties:\n"); 1159 name = ""; 1160 while ((name = do_bsys_nextprop(NULL, name)) != NULL) { 1161 bop_printf(NULL, "\t0x%p %s = ", (void *)name, name); 1162 (void) do_bsys_getprop(NULL, name, value); 1163 len = do_bsys_getproplen(NULL, name); 1164 bop_printf(NULL, "len=%d ", len); 1165 value[len] = 0; 1166 bop_printf(NULL, "%s\n", value); 1167 } 1168 } 1169 1170 /* 1171 * jump into krtld... 1172 */ 1173 _kobj_boot(&bop_sysp, NULL, bops, NULL); 1174 } 1175 1176 1177 /*ARGSUSED*/ 1178 static caddr_t 1179 no_more_alloc(bootops_t *bop, caddr_t virthint, size_t size, int align) 1180 { 1181 panic("Attempt to bsys_alloc() too late\n"); 1182 return (NULL); 1183 } 1184 1185 /*ARGSUSED*/ 1186 static void 1187 no_more_free(bootops_t *bop, caddr_t virt, size_t size) 1188 { 1189 panic("Attempt to bsys_free() too late\n"); 1190 } 1191 1192 void 1193 bop_no_more_mem(void) 1194 { 1195 DBG(total_bop_alloc_scratch); 1196 DBG(total_bop_alloc_kernel); 1197 bootops->bsys_alloc = no_more_alloc; 1198 bootops->bsys_free = no_more_free; 1199 } 1200 1201 1202 /* 1203 * Set ACPI firmware properties 1204 */ 1205 1206 static caddr_t 1207 vmap_phys(size_t length, paddr_t pa) 1208 { 1209 paddr_t start, end; 1210 caddr_t va; 1211 size_t len, page; 1212 1213 start = P2ALIGN(pa, MMU_PAGESIZE); 1214 end = P2ROUNDUP(pa + length, MMU_PAGESIZE); 1215 len = end - start; 1216 va = (caddr_t)alloc_vaddr(len, MMU_PAGESIZE); 1217 for (page = 0; page < len; page += MMU_PAGESIZE) 1218 kbm_map((uintptr_t)va + page, start + page, 0, 0); 1219 return (va + (pa & MMU_PAGEOFFSET)); 1220 } 1221 1222 static uint8_t 1223 checksum_table(uint8_t *tp, size_t len) 1224 { 1225 uint8_t sum = 0; 1226 1227 while (len-- > 0) 1228 sum += *tp++; 1229 1230 return (sum); 1231 } 1232 1233 static int 1234 valid_rsdp(struct rsdp *rp) 1235 { 1236 1237 /* validate the V1.x checksum */ 1238 if (checksum_table((uint8_t *)&rp->v1, sizeof (struct rsdp_v1)) != 0) 1239 return (0); 1240 1241 /* If pre-ACPI 2.0, this is a valid RSDP */ 1242 if (rp->v1.revision < 2) 1243 return (1); 1244 1245 /* validate the V2.x checksum */ 1246 if (checksum_table((uint8_t *)rp, sizeof (struct rsdp)) != 0) 1247 return (0); 1248 1249 return (1); 1250 } 1251 1252 /* 1253 * Scan memory range for an RSDP; 1254 * see ACPI 3.0 Spec, 5.2.5.1 1255 */ 1256 static struct rsdp * 1257 scan_rsdp(paddr_t start, paddr_t end) 1258 { 1259 size_t len = end - start + 1; 1260 caddr_t ptr; 1261 1262 ptr = vmap_phys(len, start); 1263 while (len > 0) { 1264 if (strncmp(ptr, ACPI_RSDP_SIG, ACPI_RSDP_SIG_LEN) == 0) 1265 if (valid_rsdp((struct rsdp *)ptr)) 1266 return ((struct rsdp *)ptr); 1267 ptr += 16; 1268 len -= 16; 1269 } 1270 1271 return (NULL); 1272 } 1273 1274 /* 1275 * Refer to ACPI 3.0 Spec, section 5.2.5.1 to understand this function 1276 */ 1277 static struct rsdp * 1278 find_rsdp() { 1279 struct rsdp *rsdp; 1280 uint16_t *ebda_seg; 1281 paddr_t ebda_addr; 1282 1283 /* 1284 * Get the EBDA segment and scan the first 1K 1285 */ 1286 ebda_seg = (uint16_t *)vmap_phys(sizeof (uint16_t), ACPI_EBDA_SEG_ADDR); 1287 ebda_addr = *ebda_seg << 4; 1288 rsdp = scan_rsdp(ebda_addr, ebda_addr + ACPI_EBDA_LEN - 1); 1289 if (rsdp == NULL) 1290 /* if EBDA doesn't contain RSDP, look in BIOS memory */ 1291 rsdp = scan_rsdp(0xe0000, 0xfffff); 1292 return (rsdp); 1293 } 1294 1295 static struct table_header * 1296 map_fw_table(paddr_t table_addr) 1297 { 1298 struct table_header *tp; 1299 size_t len = MAX(sizeof (struct table_header), MMU_PAGESIZE); 1300 1301 /* 1302 * Map at least a page; if the table is larger than this, remap it 1303 */ 1304 tp = (struct table_header *)vmap_phys(len, table_addr); 1305 if (tp->len > len) 1306 tp = (struct table_header *)vmap_phys(tp->len, table_addr); 1307 return (tp); 1308 } 1309 1310 static struct table_header * 1311 find_fw_table(char *signature) 1312 { 1313 static int revision = 0; 1314 static struct xsdt *xsdt; 1315 static int len; 1316 paddr_t xsdt_addr; 1317 struct rsdp *rsdp; 1318 struct table_header *tp; 1319 paddr_t table_addr; 1320 int n; 1321 1322 if (strlen(signature) != ACPI_TABLE_SIG_LEN) 1323 return (NULL); 1324 1325 /* 1326 * Reading the ACPI 3.0 Spec, section 5.2.5.3 will help 1327 * understand this code. If we haven't already found the RSDT/XSDT, 1328 * revision will be 0. Find the RSDP and check the revision 1329 * to find out whether to use the RSDT or XSDT. If revision is 1330 * 0 or 1, use the RSDT and set internal revision to 1; if it is 2, 1331 * use the XSDT. If the XSDT address is 0, though, fall back to 1332 * revision 1 and use the RSDT. 1333 */ 1334 if (revision == 0) { 1335 if ((rsdp = (struct rsdp *)find_rsdp()) != NULL) { 1336 revision = rsdp->v1.revision; 1337 switch (revision) { 1338 case 2: 1339 /* 1340 * Use the XSDT unless BIOS is buggy and 1341 * claims to be rev 2 but has a null XSDT 1342 * address 1343 */ 1344 xsdt_addr = rsdp->xsdt; 1345 if (xsdt_addr != 0) 1346 break; 1347 /* FALLTHROUGH */ 1348 case 0: 1349 /* treat RSDP rev 0 as revision 1 internally */ 1350 revision = 1; 1351 /* FALLTHROUGH */ 1352 case 1: 1353 /* use the RSDT for rev 0/1 */ 1354 xsdt_addr = rsdp->v1.rsdt; 1355 break; 1356 default: 1357 /* unknown revision */ 1358 revision = 0; 1359 break; 1360 } 1361 } 1362 if (revision == 0) 1363 return (NULL); 1364 1365 /* cache the XSDT info */ 1366 xsdt = (struct xsdt *)map_fw_table(xsdt_addr); 1367 len = (xsdt->hdr.len - sizeof (xsdt->hdr)) / 1368 ((revision == 1) ? sizeof (uint32_t) : sizeof (uint64_t)); 1369 } 1370 1371 /* 1372 * Scan the table headers looking for a signature match 1373 */ 1374 for (n = 0; n < len; n++) { 1375 table_addr = (revision == 1) ? xsdt->p.r[n] : xsdt->p.x[n]; 1376 if (table_addr == 0) 1377 continue; 1378 tp = map_fw_table(table_addr); 1379 if (strncmp(tp->sig, signature, ACPI_TABLE_SIG_LEN) == 0) { 1380 return (tp); 1381 } 1382 } 1383 return (NULL); 1384 } 1385 1386 static void 1387 process_madt(struct madt *tp) 1388 { 1389 struct madt_processor *cpu, *end; 1390 uint32_t cpu_count = 0; 1391 1392 /* 1393 * User-set boot-ncpus overrides firmware count 1394 */ 1395 if (do_bsys_getproplen(NULL, "boot-ncpus") >= 0) 1396 return; 1397 1398 if (tp != NULL) { 1399 end = (struct madt_processor *)(tp->hdr.len + (uintptr_t)tp); 1400 cpu = tp->list; 1401 while (cpu < end) { 1402 if (cpu->type == MADT_PROCESSOR) 1403 if (cpu->flags & 1) 1404 cpu_count++; 1405 1406 cpu = (struct madt_processor *) 1407 (cpu->len + (uintptr_t)cpu); 1408 } 1409 bsetpropsi("boot-ncpus", cpu_count); 1410 } 1411 1412 } 1413 1414 static void 1415 process_srat(struct srat *tp) 1416 { 1417 struct srat_item *item, *end; 1418 int i; 1419 int proc_num, mem_num; 1420 #pragma pack(1) 1421 struct { 1422 uint32_t domain; 1423 uint32_t apic_id; 1424 uint32_t sapic_id; 1425 } processor; 1426 struct { 1427 uint32_t domain; 1428 uint64_t addr; 1429 uint64_t length; 1430 uint32_t flags; 1431 } memory; 1432 #pragma pack() 1433 char prop_name[30]; 1434 1435 if (tp == NULL) 1436 return; 1437 1438 proc_num = mem_num = 0; 1439 end = (struct srat_item *)(tp->hdr.len + (uintptr_t)tp); 1440 item = tp->list; 1441 while (item < end) { 1442 switch (item->type) { 1443 case SRAT_PROCESSOR: 1444 if (!(item->i.p.flags & SRAT_ENABLED)) 1445 break; 1446 processor.domain = item->i.p.domain1; 1447 for (i = 0; i < 3; i++) 1448 processor.domain += 1449 item->i.p.domain2[i] << ((i + 1) * 8); 1450 processor.apic_id = item->i.p.apic_id; 1451 processor.sapic_id = item->i.p.local_sapic_eid; 1452 (void) snprintf(prop_name, 30, "acpi-srat-processor-%d", 1453 proc_num); 1454 bsetprop(prop_name, strlen(prop_name), &processor, 1455 sizeof (processor)); 1456 proc_num++; 1457 break; 1458 case SRAT_MEMORY: 1459 if (!(item->i.m.flags & SRAT_ENABLED)) 1460 break; 1461 memory.domain = item->i.m.domain; 1462 memory.addr = item->i.m.base_addr; 1463 memory.length = item->i.m.len; 1464 memory.flags = item->i.m.flags; 1465 (void) snprintf(prop_name, 30, "acpi-srat-memory-%d", 1466 mem_num); 1467 bsetprop(prop_name, strlen(prop_name), &memory, 1468 sizeof (memory)); 1469 mem_num++; 1470 break; 1471 } 1472 1473 item = (struct srat_item *) 1474 (item->len + (caddr_t)item); 1475 } 1476 } 1477 1478 static void 1479 build_firmware_properties(void) 1480 { 1481 struct table_header *tp; 1482 1483 if (tp = find_fw_table("APIC")) 1484 process_madt((struct madt *)tp); 1485 1486 if (tp = find_fw_table("SRAT")) 1487 process_srat((struct srat *)tp); 1488 } 1489 1490 /* 1491 * fake up a boot property for USB serial console early boot output 1492 */ 1493 void * 1494 usbser_init(size_t size) 1495 { 1496 static char *p = NULL; 1497 1498 p = do_bsys_alloc(NULL, NULL, size, MMU_PAGESIZE); 1499 *p = 0; 1500 bsetprop("usb-serial-buf", strlen("usb-serial-buf") + 1, 1501 &p, sizeof (p)); 1502 return (p); 1503 } 1504