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 2010 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  *
  26  * Copyright (c) 2010, Intel Corporation.
  27  * All rights reserved.
  28  *
  29  * Copyright 2013 Joyent, Inc.  All rights reserved.
  30  */
  31 
  32 /*
  33  * This file contains the functionality that mimics the boot operations
  34  * on SPARC systems or the old boot.bin/multiboot programs on x86 systems.
  35  * The x86 kernel now does everything on its own.
  36  */
  37 
  38 #include <sys/types.h>
  39 #include <sys/bootconf.h>
  40 #include <sys/bootsvcs.h>
  41 #include <sys/bootinfo.h>
  42 #include <sys/multiboot.h>
  43 #include <sys/multiboot2.h>
  44 #include <sys/multiboot2_impl.h>
  45 #include <sys/bootvfs.h>
  46 #include <sys/bootprops.h>
  47 #include <sys/varargs.h>
  48 #include <sys/param.h>
  49 #include <sys/machparam.h>
  50 #include <sys/machsystm.h>
  51 #include <sys/archsystm.h>
  52 #include <sys/boot_console.h>
  53 #include <sys/framebuffer.h>
  54 #include <sys/cmn_err.h>
  55 #include <sys/systm.h>
  56 #include <sys/promif.h>
  57 #include <sys/archsystm.h>
  58 #include <sys/x86_archext.h>
  59 #include <sys/kobj.h>
  60 #include <sys/privregs.h>
  61 #include <sys/sysmacros.h>
  62 #include <sys/ctype.h>
  63 #include <sys/fastboot.h>
  64 #ifdef __xpv
  65 #include <sys/hypervisor.h>
  66 #include <net/if.h>
  67 #endif
  68 #include <vm/kboot_mmu.h>
  69 #include <vm/hat_pte.h>
  70 #include <sys/kobj.h>
  71 #include <sys/kobj_lex.h>
  72 #include <sys/pci_cfgspace_impl.h>
  73 #include <sys/fastboot_impl.h>
  74 #include <sys/acpi/acconfig.h>
  75 #include <sys/acpi/acpi.h>
  76 #include <sys/ddipropdefs.h>      /* For DDI prop types */
  77 
  78 static int have_console = 0;    /* set once primitive console is initialized */
  79 static char *boot_args = "";
  80 
  81 /*
  82  * Debugging macros
  83  */
  84 static uint_t kbm_debug = 0;
  85 #define DBG_MSG(s)      { if (kbm_debug) bop_printf(NULL, "%s", s); }
  86 #define DBG(x)          { if (kbm_debug)                        \
  87         bop_printf(NULL, "%s is %" PRIx64 "\n", #x, (uint64_t)(x));     \
  88         }
  89 
  90 #define PUT_STRING(s) {                         \
  91         char *cp;                               \
  92         for (cp = (s); *cp; ++cp)               \
  93                 bcons_putchar(*cp);             \
  94         }
  95 
  96 /* callback to boot_fb to set shadow frame buffer */
  97 extern void boot_fb_shadow_init(bootops_t *);
  98 
  99 bootops_t bootop;       /* simple bootops we'll pass on to kernel */
 100 struct bsys_mem bm;
 101 
 102 /*
 103  * Boot info from "glue" code in low memory. xbootp is used by:
 104  *      do_bop_phys_alloc(), do_bsys_alloc() and boot_prop_finish().
 105  */
 106 static struct xboot_info *xbootp;
 107 static uintptr_t next_virt;     /* next available virtual address */
 108 static paddr_t next_phys;       /* next available physical address from dboot */
 109 static paddr_t high_phys = -(paddr_t)1; /* last used physical address */
 110 
 111 /*
 112  * buffer for vsnprintf for console I/O
 113  */
 114 #define BUFFERSIZE      512
 115 static char buffer[BUFFERSIZE];
 116 
 117 /*
 118  * stuff to store/report/manipulate boot property settings.
 119  */
 120 typedef struct bootprop {
 121         struct bootprop *bp_next;
 122         char *bp_name;
 123         int bp_flags;                   /* DDI prop type */
 124         uint_t bp_vlen;                 /* 0 for boolean */
 125         char *bp_value;
 126 } bootprop_t;
 127 
 128 static bootprop_t *bprops = NULL;
 129 static char *curr_page = NULL;          /* ptr to avail bprop memory */
 130 static int curr_space = 0;              /* amount of memory at curr_page */
 131 
 132 #ifdef __xpv
 133 start_info_t *xen_info;
 134 shared_info_t *HYPERVISOR_shared_info;
 135 #endif
 136 
 137 /*
 138  * some allocator statistics
 139  */
 140 static ulong_t total_bop_alloc_scratch = 0;
 141 static ulong_t total_bop_alloc_kernel = 0;
 142 
 143 static void build_firmware_properties(struct xboot_info *);
 144 
 145 static int early_allocation = 1;
 146 
 147 int force_fastreboot = 0;
 148 volatile int fastreboot_onpanic = 0;
 149 int post_fastreboot = 0;
 150 #ifdef  __xpv
 151 volatile int fastreboot_capable = 0;
 152 #else
 153 volatile int fastreboot_capable = 1;
 154 #endif
 155 
 156 /*
 157  * Information saved from current boot for fast reboot.
 158  * If the information size exceeds what we have allocated, fast reboot
 159  * will not be supported.
 160  */
 161 multiboot_info_t saved_mbi;
 162 mb_memory_map_t saved_mmap[FASTBOOT_SAVED_MMAP_COUNT];
 163 uint8_t saved_drives[FASTBOOT_SAVED_DRIVES_SIZE];
 164 char saved_cmdline[FASTBOOT_SAVED_CMDLINE_LEN];
 165 int saved_cmdline_len = 0;
 166 size_t saved_file_size[FASTBOOT_MAX_FILES_MAP];
 167 
 168 /*
 169  * Turn off fastreboot_onpanic to avoid panic loop.
 170  */
 171 char fastreboot_onpanic_cmdline[FASTBOOT_SAVED_CMDLINE_LEN];
 172 static const char fastreboot_onpanic_args[] = " -B fastreboot_onpanic=0";
 173 
 174 /*
 175  * Pointers to where System Resource Affinity Table (SRAT), System Locality
 176  * Information Table (SLIT) and Maximum System Capability Table (MSCT)
 177  * are mapped into virtual memory
 178  */
 179 ACPI_TABLE_SRAT *srat_ptr = NULL;
 180 ACPI_TABLE_SLIT *slit_ptr = NULL;
 181 ACPI_TABLE_MSCT *msct_ptr = NULL;
 182 
 183 /*
 184  * Arbitrary limit on number of localities we handle; if
 185  * this limit is raised to more than UINT16_MAX, make sure
 186  * process_slit() knows how to handle it.
 187  */
 188 #define SLIT_LOCALITIES_MAX     (4096)
 189 
 190 #define SLIT_NUM_PROPNAME       "acpi-slit-localities"
 191 #define SLIT_PROPNAME           "acpi-slit"
 192 
 193 /*
 194  * Allocate aligned physical memory at boot time. This allocator allocates
 195  * from the highest possible addresses. This avoids exhausting memory that
 196  * would be useful for DMA buffers.
 197  */
 198 paddr_t
 199 do_bop_phys_alloc(uint64_t size, uint64_t align)
 200 {
 201         paddr_t pa = 0;
 202         paddr_t start;
 203         paddr_t end;
 204         struct memlist  *ml = (struct memlist *)xbootp->bi_phys_install;
 205 
 206         /*
 207          * Be careful if high memory usage is limited in startup.c
 208          * Since there are holes in the low part of the physical address
 209          * space we can treat physmem as a pfn (not just a pgcnt) and
 210          * get a conservative upper limit.
 211          */
 212         if (physmem != 0 && high_phys > pfn_to_pa(physmem))
 213                 high_phys = pfn_to_pa(physmem);
 214 
 215         /*
 216          * find the highest available memory in physinstalled
 217          */
 218         size = P2ROUNDUP(size, align);
 219         for (; ml; ml = ml->ml_next) {
 220                 start = P2ROUNDUP(ml->ml_address, align);
 221                 end = P2ALIGN(ml->ml_address + ml->ml_size, align);
 222                 if (start < next_phys)
 223                         start = P2ROUNDUP(next_phys, align);
 224                 if (end > high_phys)
 225                         end = P2ALIGN(high_phys, align);
 226 
 227                 if (end <= start)
 228                         continue;
 229                 if (end - start < size)
 230                         continue;
 231 
 232                 /*
 233                  * Early allocations need to use low memory, since
 234                  * physmem might be further limited by bootenv.rc
 235                  */
 236                 if (early_allocation) {
 237                         if (pa == 0 || start < pa)
 238                                 pa = start;
 239                 } else {
 240                         if (end - size > pa)
 241                                 pa = end - size;
 242                 }
 243         }
 244         if (pa != 0) {
 245                 if (early_allocation)
 246                         next_phys = pa + size;
 247                 else
 248                         high_phys = pa;
 249                 return (pa);
 250         }
 251         bop_panic("do_bop_phys_alloc(0x%" PRIx64 ", 0x%" PRIx64
 252             ") Out of memory\n", size, align);
 253         /*NOTREACHED*/
 254 }
 255 
 256 uintptr_t
 257 alloc_vaddr(size_t size, paddr_t align)
 258 {
 259         uintptr_t rv;
 260 
 261         next_virt = P2ROUNDUP(next_virt, (uintptr_t)align);
 262         rv = (uintptr_t)next_virt;
 263         next_virt += size;
 264         return (rv);
 265 }
 266 
 267 /*
 268  * Allocate virtual memory. The size is always rounded up to a multiple
 269  * of base pagesize.
 270  */
 271 
 272 /*ARGSUSED*/
 273 static caddr_t
 274 do_bsys_alloc(bootops_t *bop, caddr_t virthint, size_t size, int align)
 275 {
 276         paddr_t a = align;      /* same type as pa for masking */
 277         uint_t pgsize;
 278         paddr_t pa;
 279         uintptr_t va;
 280         ssize_t s;              /* the aligned size */
 281         uint_t level;
 282         uint_t is_kernel = (virthint != 0);
 283 
 284         if (a < MMU_PAGESIZE)
 285                 a = MMU_PAGESIZE;
 286         else if (!ISP2(a))
 287                 prom_panic("do_bsys_alloc() incorrect alignment");
 288         size = P2ROUNDUP(size, MMU_PAGESIZE);
 289 
 290         /*
 291          * Use the next aligned virtual address if we weren't given one.
 292          */
 293         if (virthint == NULL) {
 294                 virthint = (caddr_t)alloc_vaddr(size, a);
 295                 total_bop_alloc_scratch += size;
 296         } else {
 297                 total_bop_alloc_kernel += size;
 298         }
 299 
 300         /*
 301          * allocate the physical memory
 302          */
 303         pa = do_bop_phys_alloc(size, a);
 304 
 305         /*
 306          * Add the mappings to the page tables, try large pages first.
 307          */
 308         va = (uintptr_t)virthint;
 309         s = size;
 310         level = 1;
 311         pgsize = xbootp->bi_use_pae ? TWO_MEG : FOUR_MEG;
 312         if (xbootp->bi_use_largepage && a == pgsize) {
 313                 while (IS_P2ALIGNED(pa, pgsize) && IS_P2ALIGNED(va, pgsize) &&
 314                     s >= pgsize) {
 315                         kbm_map(va, pa, level, is_kernel);
 316                         va += pgsize;
 317                         pa += pgsize;
 318                         s -= pgsize;
 319                 }
 320         }
 321 
 322         /*
 323          * Map remaining pages use small mappings
 324          */
 325         level = 0;
 326         pgsize = MMU_PAGESIZE;
 327         while (s > 0) {
 328                 kbm_map(va, pa, level, is_kernel);
 329                 va += pgsize;
 330                 pa += pgsize;
 331                 s -= pgsize;
 332         }
 333         return (virthint);
 334 }
 335 
 336 /*
 337  * Free virtual memory - we'll just ignore these.
 338  */
 339 /*ARGSUSED*/
 340 static void
 341 do_bsys_free(bootops_t *bop, caddr_t virt, size_t size)
 342 {
 343         bop_printf(NULL, "do_bsys_free(virt=0x%p, size=0x%lx) ignored\n",
 344             (void *)virt, size);
 345 }
 346 
 347 /*
 348  * Old interface
 349  */
 350 /*ARGSUSED*/
 351 static caddr_t
 352 do_bsys_ealloc(bootops_t *bop, caddr_t virthint, size_t size,
 353     int align, int flags)
 354 {
 355         prom_panic("unsupported call to BOP_EALLOC()\n");
 356         return (0);
 357 }
 358 
 359 
 360 static void
 361 bsetprop(int flags, char *name, int nlen, void *value, int vlen)
 362 {
 363         uint_t size;
 364         uint_t need_size;
 365         bootprop_t *b;
 366 
 367         /*
 368          * align the size to 16 byte boundary
 369          */
 370         size = sizeof (bootprop_t) + nlen + 1 + vlen;
 371         size = (size + 0xf) & ~0xf;
 372         if (size > curr_space) {
 373                 need_size = (size + (MMU_PAGEOFFSET)) & MMU_PAGEMASK;
 374                 curr_page = do_bsys_alloc(NULL, 0, need_size, MMU_PAGESIZE);
 375                 curr_space = need_size;
 376         }
 377 
 378         /*
 379          * use a bootprop_t at curr_page and link into list
 380          */
 381         b = (bootprop_t *)curr_page;
 382         curr_page += sizeof (bootprop_t);
 383         curr_space -=  sizeof (bootprop_t);
 384         b->bp_next = bprops;
 385         bprops = b;
 386 
 387         /*
 388          * follow by name and ending zero byte
 389          */
 390         b->bp_name = curr_page;
 391         bcopy(name, curr_page, nlen);
 392         curr_page += nlen;
 393         *curr_page++ = 0;
 394         curr_space -= nlen + 1;
 395 
 396         /*
 397          * set the property type
 398          */
 399         b->bp_flags = flags & DDI_PROP_TYPE_MASK;
 400 
 401         /*
 402          * copy in value, but no ending zero byte
 403          */
 404         b->bp_value = curr_page;
 405         b->bp_vlen = vlen;
 406         if (vlen > 0) {
 407                 bcopy(value, curr_page, vlen);
 408                 curr_page += vlen;
 409                 curr_space -= vlen;
 410         }
 411 
 412         /*
 413          * align new values of curr_page, curr_space
 414          */
 415         while (curr_space & 0xf) {
 416                 ++curr_page;
 417                 --curr_space;
 418         }
 419 }
 420 
 421 static void
 422 bsetprops(char *name, char *value)
 423 {
 424         bsetprop(DDI_PROP_TYPE_STRING, name, strlen(name),
 425             value, strlen(value) + 1);
 426 }
 427 
 428 static void
 429 bsetprop32(char *name, uint32_t value)
 430 {
 431         bsetprop(DDI_PROP_TYPE_INT, name, strlen(name),
 432             (void *)&value, sizeof (value));
 433 }
 434 
 435 static void
 436 bsetprop64(char *name, uint64_t value)
 437 {
 438         bsetprop(DDI_PROP_TYPE_INT64, name, strlen(name),
 439             (void *)&value, sizeof (value));
 440 }
 441 
 442 static void
 443 bsetpropsi(char *name, int value)
 444 {
 445         char prop_val[32];
 446 
 447         (void) snprintf(prop_val, sizeof (prop_val), "%d", value);
 448         bsetprops(name, prop_val);
 449 }
 450 
 451 /*
 452  * to find the type of the value associated with this name
 453  */
 454 /*ARGSUSED*/
 455 int
 456 do_bsys_getproptype(bootops_t *bop, const char *name)
 457 {
 458         bootprop_t *b;
 459 
 460         for (b = bprops; b; b = b->bp_next) {
 461                 if (strcmp(name, b->bp_name) != 0)
 462                         continue;
 463                 return (b->bp_flags);
 464         }
 465         return (-1);
 466 }
 467 
 468 /*
 469  * to find the size of the buffer to allocate
 470  */
 471 /*ARGSUSED*/
 472 int
 473 do_bsys_getproplen(bootops_t *bop, const char *name)
 474 {
 475         bootprop_t *b;
 476 
 477         for (b = bprops; b; b = b->bp_next) {
 478                 if (strcmp(name, b->bp_name) != 0)
 479                         continue;
 480                 return (b->bp_vlen);
 481         }
 482         return (-1);
 483 }
 484 
 485 /*
 486  * get the value associated with this name
 487  */
 488 /*ARGSUSED*/
 489 int
 490 do_bsys_getprop(bootops_t *bop, const char *name, void *value)
 491 {
 492         bootprop_t *b;
 493 
 494         for (b = bprops; b; b = b->bp_next) {
 495                 if (strcmp(name, b->bp_name) != 0)
 496                         continue;
 497                 bcopy(b->bp_value, value, b->bp_vlen);
 498                 return (0);
 499         }
 500         return (-1);
 501 }
 502 
 503 /*
 504  * get the name of the next property in succession from the standalone
 505  */
 506 /*ARGSUSED*/
 507 static char *
 508 do_bsys_nextprop(bootops_t *bop, char *name)
 509 {
 510         bootprop_t *b;
 511 
 512         /*
 513          * A null name is a special signal for the 1st boot property
 514          */
 515         if (name == NULL || strlen(name) == 0) {
 516                 if (bprops == NULL)
 517                         return (NULL);
 518                 return (bprops->bp_name);
 519         }
 520 
 521         for (b = bprops; b; b = b->bp_next) {
 522                 if (name != b->bp_name)
 523                         continue;
 524                 b = b->bp_next;
 525                 if (b == NULL)
 526                         return (NULL);
 527                 return (b->bp_name);
 528         }
 529         return (NULL);
 530 }
 531 
 532 /*
 533  * Parse numeric value from a string. Understands decimal, hex, octal, - and ~
 534  */
 535 static int
 536 parse_value(char *p, uint64_t *retval)
 537 {
 538         int adjust = 0;
 539         uint64_t tmp = 0;
 540         int digit;
 541         int radix = 10;
 542 
 543         *retval = 0;
 544         if (*p == '-' || *p == '~')
 545                 adjust = *p++;
 546 
 547         if (*p == '0') {
 548                 ++p;
 549                 if (*p == 0)
 550                         return (0);
 551                 if (*p == 'x' || *p == 'X') {
 552                         radix = 16;
 553                         ++p;
 554                 } else {
 555                         radix = 8;
 556                         ++p;
 557                 }
 558         }
 559         while (*p) {
 560                 if ('0' <= *p && *p <= '9')
 561                         digit = *p - '0';
 562                 else if ('a' <= *p && *p <= 'f')
 563                         digit = 10 + *p - 'a';
 564                 else if ('A' <= *p && *p <= 'F')
 565                         digit = 10 + *p - 'A';
 566                 else
 567                         return (-1);
 568                 if (digit >= radix)
 569                         return (-1);
 570                 tmp = tmp * radix + digit;
 571                 ++p;
 572         }
 573         if (adjust == '-')
 574                 tmp = -tmp;
 575         else if (adjust == '~')
 576                 tmp = ~tmp;
 577         *retval = tmp;
 578         return (0);
 579 }
 580 
 581 static boolean_t
 582 unprintable(char *value, int size)
 583 {
 584         int i;
 585 
 586         if (size <= 0 || value[0] == '\0')
 587                 return (B_TRUE);
 588 
 589         for (i = 0; i < size; i++) {
 590                 if (value[i] == '\0')
 591                         return (i != (size - 1));
 592 
 593                 if (!isprint(value[i]))
 594                         return (B_TRUE);
 595         }
 596         return (B_FALSE);
 597 }
 598 
 599 /*
 600  * Print out information about all boot properties.
 601  * buffer is pointer to pre-allocated space to be used as temporary
 602  * space for property values.
 603  */
 604 static void
 605 boot_prop_display(char *buffer)
 606 {
 607         char *name = "";
 608         int i, len, flags, *buf32;
 609         uint64_t *buf64;
 610 
 611         bop_printf(NULL, "\nBoot properties:\n");
 612 
 613         while ((name = do_bsys_nextprop(NULL, name)) != NULL) {
 614                 bop_printf(NULL, "\t0x%p %s = ", (void *)name, name);
 615                 (void) do_bsys_getprop(NULL, name, buffer);
 616                 len = do_bsys_getproplen(NULL, name);
 617                 flags = do_bsys_getproptype(NULL, name);
 618                 bop_printf(NULL, "len=%d ", len);
 619 
 620                 switch (flags) {
 621                 case DDI_PROP_TYPE_INT:
 622                         len = len / sizeof (int);
 623                         buf32 = (int *)buffer;
 624                         for (i = 0; i < len; i++) {
 625                                 bop_printf(NULL, "%08x", buf32[i]);
 626                                 if (i < len - 1)
 627                                         bop_printf(NULL, ".");
 628                         }
 629                         break;
 630                 case DDI_PROP_TYPE_STRING:
 631                         bop_printf(NULL, buffer);
 632                         break;
 633                 case DDI_PROP_TYPE_INT64:
 634                         len = len / sizeof (uint64_t);
 635                         buf64 = (uint64_t *)buffer;
 636                         for (i = 0; i < len; i++) {
 637                                 bop_printf(NULL, "%016" PRIx64, buf64[i]);
 638                                 if (i < len - 1)
 639                                         bop_printf(NULL, ".");
 640                         }
 641                         break;
 642                 default:
 643                         if (!unprintable(buffer, len)) {
 644                                 buffer[len] = 0;
 645                                 bop_printf(NULL, "%s", buffer);
 646                                 break;
 647                         }
 648                         for (i = 0; i < len; i++) {
 649                                 bop_printf(NULL, "%02x", buffer[i] & 0xff);
 650                                 if (i < len - 1)
 651                                         bop_printf(NULL, ".");
 652                         }
 653                         break;
 654                 }
 655                 bop_printf(NULL, "\n");
 656         }
 657 }
 658 
 659 /*
 660  * 2nd part of building the table of boot properties. This includes:
 661  * - values from /boot/solaris/bootenv.rc (ie. eeprom(1m) values)
 662  *
 663  * lines look like one of:
 664  * ^$
 665  * ^# comment till end of line
 666  * setprop name 'value'
 667  * setprop name value
 668  * setprop name "value"
 669  *
 670  * we do single character I/O since this is really just looking at memory
 671  */
 672 void
 673 boot_prop_finish(void)
 674 {
 675         int fd;
 676         char *line;
 677         int c;
 678         int bytes_read;
 679         char *name;
 680         int n_len;
 681         char *value;
 682         int v_len;
 683         char *inputdev; /* these override the command line if serial ports */
 684         char *outputdev;
 685         char *consoledev;
 686         uint64_t lvalue;
 687         int use_xencons = 0;
 688 
 689 #ifdef __xpv
 690         if (!DOMAIN_IS_INITDOMAIN(xen_info))
 691                 use_xencons = 1;
 692 #endif /* __xpv */
 693 
 694         DBG_MSG("Opening /boot/solaris/bootenv.rc\n");
 695         fd = BRD_OPEN(bfs_ops, "/boot/solaris/bootenv.rc", 0);
 696         DBG(fd);
 697 
 698         line = do_bsys_alloc(NULL, NULL, MMU_PAGESIZE, MMU_PAGESIZE);
 699         while (fd >= 0) {
 700 
 701                 /*
 702                  * get a line
 703                  */
 704                 for (c = 0; ; ++c) {
 705                         bytes_read = BRD_READ(bfs_ops, fd, line + c, 1);
 706                         if (bytes_read == 0) {
 707                                 if (c == 0)
 708                                         goto done;
 709                                 break;
 710                         }
 711                         if (line[c] == '\n')
 712                                 break;
 713                 }
 714                 line[c] = 0;
 715 
 716                 /*
 717                  * ignore comment lines
 718                  */
 719                 c = 0;
 720                 while (ISSPACE(line[c]))
 721                         ++c;
 722                 if (line[c] == '#' || line[c] == 0)
 723                         continue;
 724 
 725                 /*
 726                  * must have "setprop " or "setprop\t"
 727                  */
 728                 if (strncmp(line + c, "setprop ", 8) != 0 &&
 729                     strncmp(line + c, "setprop\t", 8) != 0)
 730                         continue;
 731                 c += 8;
 732                 while (ISSPACE(line[c]))
 733                         ++c;
 734                 if (line[c] == 0)
 735                         continue;
 736 
 737                 /*
 738                  * gather up the property name
 739                  */
 740                 name = line + c;
 741                 n_len = 0;
 742                 while (line[c] && !ISSPACE(line[c]))
 743                         ++n_len, ++c;
 744 
 745                 /*
 746                  * gather up the value, if any
 747                  */
 748                 value = "";
 749                 v_len = 0;
 750                 while (ISSPACE(line[c]))
 751                         ++c;
 752                 if (line[c] != 0) {
 753                         value = line + c;
 754                         while (line[c] && !ISSPACE(line[c]))
 755                                 ++v_len, ++c;
 756                 }
 757 
 758                 if (v_len >= 2 && value[0] == value[v_len - 1] &&
 759                     (value[0] == '\'' || value[0] == '"')) {
 760                         ++value;
 761                         v_len -= 2;
 762                 }
 763                 name[n_len] = 0;
 764                 if (v_len > 0)
 765                         value[v_len] = 0;
 766                 else
 767                         continue;
 768 
 769                 /*
 770                  * ignore "boot-file" property, it's now meaningless
 771                  */
 772                 if (strcmp(name, "boot-file") == 0)
 773                         continue;
 774                 if (strcmp(name, "boot-args") == 0 &&
 775                     strlen(boot_args) > 0)
 776                         continue;
 777 
 778                 /*
 779                  * If a property was explicitly set on the command line
 780                  * it will override a setting in bootenv.rc
 781                  */
 782                 if (do_bsys_getproplen(NULL, name) >= 0)
 783                         continue;
 784 
 785                 bsetprops(name, value);
 786         }
 787 done:
 788         if (fd >= 0)
 789                 (void) BRD_CLOSE(bfs_ops, fd);
 790 
 791         /*
 792          * Check if we have to limit the boot time allocator
 793          */
 794         if (do_bsys_getproplen(NULL, "physmem") != -1 &&
 795             do_bsys_getprop(NULL, "physmem", line) >= 0 &&
 796             parse_value(line, &lvalue) != -1) {
 797                 if (0 < lvalue && (lvalue < physmem || physmem == 0)) {
 798                         physmem = (pgcnt_t)lvalue;
 799                         DBG(physmem);
 800                 }
 801         }
 802         early_allocation = 0;
 803 
 804         /*
 805          * check to see if we have to override the default value of the console
 806          */
 807         if (!use_xencons) {
 808                 inputdev = line;
 809                 v_len = do_bsys_getproplen(NULL, "input-device");
 810                 if (v_len > 0)
 811                         (void) do_bsys_getprop(NULL, "input-device", inputdev);
 812                 else
 813                         v_len = 0;
 814                 inputdev[v_len] = 0;
 815 
 816                 outputdev = inputdev + v_len + 1;
 817                 v_len = do_bsys_getproplen(NULL, "output-device");
 818                 if (v_len > 0)
 819                         (void) do_bsys_getprop(NULL, "output-device",
 820                             outputdev);
 821                 else
 822                         v_len = 0;
 823                 outputdev[v_len] = 0;
 824 
 825                 consoledev = outputdev + v_len + 1;
 826                 v_len = do_bsys_getproplen(NULL, "console");
 827                 if (v_len > 0) {
 828                         (void) do_bsys_getprop(NULL, "console", consoledev);
 829                         if (post_fastreboot &&
 830                             strcmp(consoledev, "graphics") == 0) {
 831                                 bsetprops("console", "text");
 832                                 v_len = strlen("text");
 833                                 bcopy("text", consoledev, v_len);
 834                         }
 835                 } else {
 836                         v_len = 0;
 837                 }
 838                 consoledev[v_len] = 0;
 839                 bcons_init2(inputdev, outputdev, consoledev);
 840         } else {
 841                 /*
 842                  * Ensure console property exists
 843                  * If not create it as "hypervisor"
 844                  */
 845                 v_len = do_bsys_getproplen(NULL, "console");
 846                 if (v_len < 0)
 847                         bsetprops("console", "hypervisor");
 848                 inputdev = outputdev = consoledev = "hypervisor";
 849                 bcons_init2(inputdev, outputdev, consoledev);
 850         }
 851 
 852         if (find_boot_prop("prom_debug") || kbm_debug)
 853                 boot_prop_display(line);
 854 }
 855 
 856 /*
 857  * print formatted output
 858  */
 859 /*PRINTFLIKE2*/
 860 /*ARGSUSED*/
 861 void
 862 bop_printf(bootops_t *bop, const char *fmt, ...)
 863 {
 864         va_list ap;
 865 
 866         if (have_console == 0)
 867                 return;
 868 
 869         va_start(ap, fmt);
 870         (void) vsnprintf(buffer, BUFFERSIZE, fmt, ap);
 871         va_end(ap);
 872         PUT_STRING(buffer);
 873 }
 874 
 875 /*
 876  * Another panic() variant; this one can be used even earlier during boot than
 877  * prom_panic().
 878  */
 879 /*PRINTFLIKE1*/
 880 void
 881 bop_panic(const char *fmt, ...)
 882 {
 883         va_list ap;
 884 
 885         va_start(ap, fmt);
 886         bop_printf(NULL, fmt, ap);
 887         va_end(ap);
 888 
 889         bop_printf(NULL, "\nPress any key to reboot.\n");
 890         (void) bcons_getchar();
 891         bop_printf(NULL, "Resetting...\n");
 892         pc_reset();
 893 }
 894 
 895 /*
 896  * Do a real mode interrupt BIOS call
 897  */
 898 typedef struct bios_regs {
 899         unsigned short ax, bx, cx, dx, si, di, bp, es, ds;
 900 } bios_regs_t;
 901 typedef int (*bios_func_t)(int, bios_regs_t *);
 902 
 903 /*ARGSUSED*/
 904 static void
 905 do_bsys_doint(bootops_t *bop, int intnum, struct bop_regs *rp)
 906 {
 907 #if defined(__xpv)
 908         prom_panic("unsupported call to BOP_DOINT()\n");
 909 #else   /* __xpv */
 910         static int firsttime = 1;
 911         bios_func_t bios_func = (bios_func_t)(void *)(uintptr_t)0x5000;
 912         bios_regs_t br;
 913 
 914         /*
 915          * The first time we do this, we have to copy the pre-packaged
 916          * low memory bios call code image into place.
 917          */
 918         if (firsttime) {
 919                 extern char bios_image[];
 920                 extern uint32_t bios_size;
 921 
 922                 bcopy(bios_image, (void *)bios_func, bios_size);
 923                 firsttime = 0;
 924         }
 925 
 926         br.ax = rp->eax.word.ax;
 927         br.bx = rp->ebx.word.bx;
 928         br.cx = rp->ecx.word.cx;
 929         br.dx = rp->edx.word.dx;
 930         br.bp = rp->ebp.word.bp;
 931         br.si = rp->esi.word.si;
 932         br.di = rp->edi.word.di;
 933         br.ds = rp->ds;
 934         br.es = rp->es;
 935 
 936         DBG_MSG("Doing BIOS call...");
 937         DBG(br.ax);
 938         DBG(br.bx);
 939         DBG(br.dx);
 940         rp->eflags = bios_func(intnum, &br);
 941         DBG_MSG("done\n");
 942 
 943         rp->eax.word.ax = br.ax;
 944         rp->ebx.word.bx = br.bx;
 945         rp->ecx.word.cx = br.cx;
 946         rp->edx.word.dx = br.dx;
 947         rp->ebp.word.bp = br.bp;
 948         rp->esi.word.si = br.si;
 949         rp->edi.word.di = br.di;
 950         rp->ds = br.ds;
 951         rp->es = br.es;
 952 #endif /* __xpv */
 953 }
 954 
 955 static struct boot_syscalls bop_sysp = {
 956         bcons_getchar,
 957         bcons_putchar,
 958         bcons_ischar,
 959 };
 960 
 961 static char *whoami;
 962 
 963 #define BUFLEN  64
 964 
 965 #if defined(__xpv)
 966 
 967 static char namebuf[32];
 968 
 969 static void
 970 xen_parse_props(char *s, char *prop_map[], int n_prop)
 971 {
 972         char **prop_name = prop_map;
 973         char *cp = s, *scp;
 974 
 975         do {
 976                 scp = cp;
 977                 while ((*cp != NULL) && (*cp != ':'))
 978                         cp++;
 979 
 980                 if ((scp != cp) && (*prop_name != NULL)) {
 981                         *cp = NULL;
 982                         bsetprops(*prop_name, scp);
 983                 }
 984 
 985                 cp++;
 986                 prop_name++;
 987                 n_prop--;
 988         } while (n_prop > 0);
 989 }
 990 
 991 #define VBDPATHLEN      64
 992 
 993 /*
 994  * parse the 'xpv-root' property to create properties used by
 995  * ufs_mountroot.
 996  */
 997 static void
 998 xen_vbdroot_props(char *s)
 999 {
1000         char vbdpath[VBDPATHLEN] = "/xpvd/xdf@";
1001         const char lnamefix[] = "/dev/dsk/c0d";
1002         char *pnp;
1003         char *prop_p;
1004         char mi;
1005         short minor;
1006         long addr = 0;
1007 
1008         pnp = vbdpath + strlen(vbdpath);
1009         prop_p = s + strlen(lnamefix);
1010         while ((*prop_p != '\0') && (*prop_p != 's') && (*prop_p != 'p'))
1011                 addr = addr * 10 + *prop_p++ - '0';
1012         (void) snprintf(pnp, VBDPATHLEN, "%lx", addr);
1013         pnp = vbdpath + strlen(vbdpath);
1014         if (*prop_p == 's')
1015                 mi = 'a';
1016         else if (*prop_p == 'p')
1017                 mi = 'q';
1018         else
1019                 ASSERT(0); /* shouldn't be here */
1020         prop_p++;
1021         ASSERT(*prop_p != '\0');
1022         if (ISDIGIT(*prop_p)) {
1023                 minor = *prop_p - '0';
1024                 prop_p++;
1025                 if (ISDIGIT(*prop_p)) {
1026                         minor = minor * 10 + *prop_p - '0';
1027                 }
1028         } else {
1029                 /* malformed root path, use 0 as default */
1030                 minor = 0;
1031         }
1032         ASSERT(minor < 16); /* at most 16 partitions */
1033         mi += minor;
1034         *pnp++ = ':';
1035         *pnp++ = mi;
1036         *pnp++ = '\0';
1037         bsetprops("fstype", "ufs");
1038         bsetprops("bootpath", vbdpath);
1039 
1040         DBG_MSG("VBD bootpath set to ");
1041         DBG_MSG(vbdpath);
1042         DBG_MSG("\n");
1043 }
1044 
1045 /*
1046  * parse the xpv-nfsroot property to create properties used by
1047  * nfs_mountroot.
1048  */
1049 static void
1050 xen_nfsroot_props(char *s)
1051 {
1052         char *prop_map[] = {
1053                 BP_SERVER_IP,   /* server IP address */
1054                 BP_SERVER_NAME, /* server hostname */
1055                 BP_SERVER_PATH, /* root path */
1056         };
1057         int n_prop = sizeof (prop_map) / sizeof (prop_map[0]);
1058 
1059         bsetprops("fstype", "nfs");
1060 
1061         xen_parse_props(s, prop_map, n_prop);
1062 
1063         /*
1064          * If a server name wasn't specified, use a default.
1065          */
1066         if (do_bsys_getproplen(NULL, BP_SERVER_NAME) == -1)
1067                 bsetprops(BP_SERVER_NAME, "unknown");
1068 }
1069 
1070 /*
1071  * Extract our IP address, etc. from the "xpv-ip" property.
1072  */
1073 static void
1074 xen_ip_props(char *s)
1075 {
1076         char *prop_map[] = {
1077                 BP_HOST_IP,             /* IP address */
1078                 NULL,                   /* NFS server IP address (ignored in */
1079                                         /* favour of xpv-nfsroot) */
1080                 BP_ROUTER_IP,           /* IP gateway */
1081                 BP_SUBNET_MASK,         /* IP subnet mask */
1082                 "xpv-hostname",         /* hostname (ignored) */
1083                 BP_NETWORK_INTERFACE,   /* interface name */
1084                 "xpv-hcp",              /* host configuration protocol */
1085         };
1086         int n_prop = sizeof (prop_map) / sizeof (prop_map[0]);
1087         char ifname[IFNAMSIZ];
1088 
1089         xen_parse_props(s, prop_map, n_prop);
1090 
1091         /*
1092          * A Linux dom0 administrator expects all interfaces to be
1093          * called "ethX", which is not the case here.
1094          *
1095          * If the interface name specified is "eth0", presume that
1096          * this is really intended to be "xnf0" (the first domU ->
1097          * dom0 interface for this domain).
1098          */
1099         if ((do_bsys_getprop(NULL, BP_NETWORK_INTERFACE, ifname) == 0) &&
1100             (strcmp("eth0", ifname) == 0)) {
1101                 bsetprops(BP_NETWORK_INTERFACE, "xnf0");
1102                 bop_printf(NULL,
1103                     "network interface name 'eth0' replaced with 'xnf0'\n");
1104         }
1105 }
1106 
1107 #else   /* __xpv */
1108 
1109 static void
1110 setup_rarp_props(struct sol_netinfo *sip)
1111 {
1112         char buf[BUFLEN];       /* to hold ip/mac addrs */
1113         uint8_t *val;
1114 
1115         val = (uint8_t *)&sip->sn_ciaddr;
1116         (void) snprintf(buf, BUFLEN, "%d.%d.%d.%d",
1117             val[0], val[1], val[2], val[3]);
1118         bsetprops(BP_HOST_IP, buf);
1119 
1120         val = (uint8_t *)&sip->sn_siaddr;
1121         (void) snprintf(buf, BUFLEN, "%d.%d.%d.%d",
1122             val[0], val[1], val[2], val[3]);
1123         bsetprops(BP_SERVER_IP, buf);
1124 
1125         if (sip->sn_giaddr != 0) {
1126                 val = (uint8_t *)&sip->sn_giaddr;
1127                 (void) snprintf(buf, BUFLEN, "%d.%d.%d.%d",
1128                     val[0], val[1], val[2], val[3]);
1129                 bsetprops(BP_ROUTER_IP, buf);
1130         }
1131 
1132         if (sip->sn_netmask != 0) {
1133                 val = (uint8_t *)&sip->sn_netmask;
1134                 (void) snprintf(buf, BUFLEN, "%d.%d.%d.%d",
1135                     val[0], val[1], val[2], val[3]);
1136                 bsetprops(BP_SUBNET_MASK, buf);
1137         }
1138 
1139         if (sip->sn_mactype != 4 || sip->sn_maclen != 6) {
1140                 bop_printf(NULL, "unsupported mac type %d, mac len %d\n",
1141                     sip->sn_mactype, sip->sn_maclen);
1142         } else {
1143                 val = sip->sn_macaddr;
1144                 (void) snprintf(buf, BUFLEN, "%x:%x:%x:%x:%x:%x",
1145                     val[0], val[1], val[2], val[3], val[4], val[5]);
1146                 bsetprops(BP_BOOT_MAC, buf);
1147         }
1148 }
1149 
1150 #endif  /* __xpv */
1151 
1152 static void
1153 build_panic_cmdline(const char *cmd, int cmdlen)
1154 {
1155         int proplen;
1156         size_t arglen;
1157 
1158         arglen = sizeof (fastreboot_onpanic_args);
1159         /*
1160          * If we allready have fastreboot-onpanic set to zero,
1161          * don't add them again.
1162          */
1163         if ((proplen = do_bsys_getproplen(NULL, FASTREBOOT_ONPANIC)) > 0 &&
1164             proplen <=  sizeof (fastreboot_onpanic_cmdline)) {
1165                 (void) do_bsys_getprop(NULL, FASTREBOOT_ONPANIC,
1166                     fastreboot_onpanic_cmdline);
1167                 if (FASTREBOOT_ONPANIC_NOTSET(fastreboot_onpanic_cmdline))
1168                         arglen = 1;
1169         }
1170 
1171         /*
1172          * construct fastreboot_onpanic_cmdline
1173          */
1174         if (cmdlen + arglen > sizeof (fastreboot_onpanic_cmdline)) {
1175                 DBG_MSG("Command line too long: clearing "
1176                     FASTREBOOT_ONPANIC "\n");
1177                 fastreboot_onpanic = 0;
1178         } else {
1179                 bcopy(cmd, fastreboot_onpanic_cmdline, cmdlen);
1180                 if (arglen != 1)
1181                         bcopy(fastreboot_onpanic_args,
1182                             fastreboot_onpanic_cmdline + cmdlen, arglen);
1183                 else
1184                         fastreboot_onpanic_cmdline[cmdlen] = 0;
1185         }
1186 }
1187 
1188 
1189 #ifndef __xpv
1190 /*
1191  * Construct boot command line for Fast Reboot. The saved_cmdline
1192  * is also reported by "eeprom bootcmd".
1193  */
1194 static void
1195 build_fastboot_cmdline(struct xboot_info *xbp)
1196 {
1197         saved_cmdline_len =  strlen(xbp->bi_cmdline) + 1;
1198         if (saved_cmdline_len > FASTBOOT_SAVED_CMDLINE_LEN) {
1199                 DBG(saved_cmdline_len);
1200                 DBG_MSG("Command line too long: clearing fastreboot_capable\n");
1201                 fastreboot_capable = 0;
1202         } else {
1203                 bcopy((void *)(xbp->bi_cmdline), (void *)saved_cmdline,
1204                     saved_cmdline_len);
1205                 saved_cmdline[saved_cmdline_len - 1] = '\0';
1206                 build_panic_cmdline(saved_cmdline, saved_cmdline_len - 1);
1207         }
1208 }
1209 
1210 /*
1211  * Save memory layout, disk drive information, unix and boot archive sizes for
1212  * Fast Reboot.
1213  */
1214 static void
1215 save_boot_info(struct xboot_info *xbi)
1216 {
1217         multiboot_info_t *mbi = xbi->bi_mb_info;
1218         struct boot_modules *modp;
1219         int i;
1220 
1221         bcopy(mbi, &saved_mbi, sizeof (multiboot_info_t));
1222         if (mbi->mmap_length > sizeof (saved_mmap)) {
1223                 DBG_MSG("mbi->mmap_length too big: clearing "
1224                     "fastreboot_capable\n");
1225                 fastreboot_capable = 0;
1226         } else {
1227                 bcopy((void *)(uintptr_t)mbi->mmap_addr, (void *)saved_mmap,
1228                     mbi->mmap_length);
1229         }
1230 
1231         if ((mbi->flags & MB_INFO_DRIVE_INFO) != 0) {
1232                 if (mbi->drives_length > sizeof (saved_drives)) {
1233                         DBG(mbi->drives_length);
1234                         DBG_MSG("mbi->drives_length too big: clearing "
1235                             "fastreboot_capable\n");
1236                         fastreboot_capable = 0;
1237                 } else {
1238                         bcopy((void *)(uintptr_t)mbi->drives_addr,
1239                             (void *)saved_drives, mbi->drives_length);
1240                 }
1241         } else {
1242                 saved_mbi.drives_length = 0;
1243                 saved_mbi.drives_addr = NULL;
1244         }
1245 
1246         /*
1247          * Current file sizes.  Used by fastboot.c to figure out how much
1248          * memory to reserve for panic reboot.
1249          * Use the module list from the dboot-constructed xboot_info
1250          * instead of the list referenced by the multiboot structure
1251          * because that structure may not be addressable now.
1252          */
1253         saved_file_size[FASTBOOT_NAME_UNIX] = FOUR_MEG - PAGESIZE;
1254         for (i = 0, modp = (struct boot_modules *)(uintptr_t)xbi->bi_modules;
1255             i < xbi->bi_module_cnt; i++, modp++) {
1256                 saved_file_size[FASTBOOT_NAME_BOOTARCHIVE] += modp->bm_size;
1257         }
1258 }
1259 #endif  /* __xpv */
1260 
1261 /*
1262  * Import boot environment module variables as properties, applying
1263  * blacklist filter for variables we know we will not use.
1264  *
1265  * Since the environment can be relatively large, containing many variables
1266  * used only for boot loader purposes, we will use a blacklist based filter.
1267  * To keep the blacklist from growing too large, we use prefix based filtering.
1268  * This is possible because in many cases, the loader variable names are
1269  * using a structured layout.
1270  *
1271  * We will not overwrite already set properties.
1272  */
1273 static struct bop_blacklist {
1274         const char *bl_name;
1275         int bl_name_len;
1276 } bop_prop_blacklist[] = {
1277         { "ISADIR", sizeof ("ISADIR") },
1278         { "acpi", sizeof ("acpi") },
1279         { "autoboot_delay", sizeof ("autoboot_delay") },
1280         { "autoboot_delay", sizeof ("autoboot_delay") },
1281         { "beansi_", sizeof ("beansi_") },
1282         { "beastie", sizeof ("beastie") },
1283         { "bemenu", sizeof ("bemenu") },
1284         { "boot.", sizeof ("boot.") },
1285         { "bootenv", sizeof ("bootenv") },
1286         { "currdev", sizeof ("currdev") },
1287         { "dhcp.", sizeof ("dhcp.") },
1288         { "interpret", sizeof ("interpret") },
1289         { "kernel", sizeof ("kernel") },
1290         { "loaddev", sizeof ("loaddev") },
1291         { "loader_", sizeof ("loader_") },
1292         { "module_path", sizeof ("module_path") },
1293         { "nfs.", sizeof ("nfs.") },
1294         { "pcibios", sizeof ("pcibios") },
1295         { "prompt", sizeof ("prompt") },
1296         { "smbios", sizeof ("smbios") },
1297         { "tem", sizeof ("tem") },
1298         { "twiddle_divisor", sizeof ("twiddle_divisor") },
1299         { "zfs_be", sizeof ("zfs_be") },
1300 };
1301 
1302 /*
1303  * Match the name against prefixes in above blacklist. If the match was
1304  * found, this name is blacklisted.
1305  */
1306 static boolean_t
1307 name_is_blacklisted(const char *name)
1308 {
1309         int i, n;
1310 
1311         n = sizeof (bop_prop_blacklist) / sizeof (bop_prop_blacklist[0]);
1312         for (i = 0; i < n; i++) {
1313                 if (strncmp(bop_prop_blacklist[i].bl_name, name,
1314                     bop_prop_blacklist[i].bl_name_len - 1) == 0) {
1315                         return (B_TRUE);
1316                 }
1317         }
1318         return (B_FALSE);
1319 }
1320 
1321 static void
1322 process_boot_environment(struct boot_modules *benv)
1323 {
1324         char *env, *ptr, *name, *value;
1325         uint32_t size, name_len, value_len;
1326 
1327         if (benv == NULL || benv->bm_type != BMT_ENV)
1328                 return;
1329         ptr = env = benv->bm_addr;
1330         size = benv->bm_size;
1331         do {
1332                 name = ptr;
1333                 /* find '=' */
1334                 while (*ptr != '=') {
1335                         ptr++;
1336                         if (ptr > env + size) /* Something is very wrong. */
1337                                 return;
1338                 }
1339                 name_len = ptr - name;
1340                 if (sizeof (buffer) <= name_len)
1341                         continue;
1342 
1343                 (void) strncpy(buffer, name, sizeof (buffer));
1344                 buffer[name_len] = '\0';
1345                 name = buffer;
1346 
1347                 value_len = 0;
1348                 value = ++ptr;
1349                 while ((uintptr_t)ptr - (uintptr_t)env < size) {
1350                         if (*ptr == '\0') {
1351                                 ptr++;
1352                                 value_len = (uintptr_t)ptr - (uintptr_t)env;
1353                                 break;
1354                         }
1355                         ptr++;
1356                 }
1357 
1358                 /* Did we reach the end of the module? */
1359                 if (value_len == 0)
1360                         return;
1361 
1362                 if (*value == '\0')
1363                         continue;
1364 
1365                 /* Is this property already set? */
1366                 if (do_bsys_getproplen(NULL, name) >= 0)
1367                         continue;
1368 
1369                 if (name_is_blacklisted(name) == B_TRUE)
1370                         continue;
1371 
1372                 /* Create new property. */
1373                 bsetprops(name, value);
1374 
1375                 /* Avoid reading past the module end. */
1376                 if (size <= (uintptr_t)ptr - (uintptr_t)env)
1377                         return;
1378         } while (*ptr != '\0');
1379 }
1380 
1381 /*
1382  * 1st pass at building the table of boot properties. This includes:
1383  * - values set on the command line: -B a=x,b=y,c=z ....
1384  * - known values we just compute (ie. from xbp)
1385  * - values from /boot/solaris/bootenv.rc (ie. eeprom(1m) values)
1386  *
1387  * the grub command line looked like:
1388  * kernel boot-file [-B prop=value[,prop=value]...] [boot-args]
1389  *
1390  * whoami is the same as boot-file
1391  */
1392 static void
1393 build_boot_properties(struct xboot_info *xbp)
1394 {
1395         char *name;
1396         int name_len;
1397         char *value;
1398         int value_len;
1399         struct boot_modules *bm, *rdbm, *benv = NULL;
1400         char *propbuf;
1401         int quoted = 0;
1402         int boot_arg_len;
1403         uint_t i, midx;
1404         char modid[32];
1405 #ifndef __xpv
1406         static int stdout_val = 0;
1407         uchar_t boot_device;
1408         char str[3];
1409 #endif
1410 
1411         /*
1412          * These have to be done first, so that kobj_mount_root() works
1413          */
1414         DBG_MSG("Building boot properties\n");
1415         propbuf = do_bsys_alloc(NULL, NULL, MMU_PAGESIZE, 0);
1416         DBG((uintptr_t)propbuf);
1417         if (xbp->bi_module_cnt > 0) {
1418                 bm = xbp->bi_modules;
1419                 rdbm = NULL;
1420                 for (midx = i = 0; i < xbp->bi_module_cnt; i++) {
1421                         if (bm[i].bm_type == BMT_ROOTFS) {
1422                                 rdbm = &bm[i];
1423                                 continue;
1424                         }
1425                         if (bm[i].bm_type == BMT_HASH ||
1426                             bm[i].bm_type == BMT_FONT ||
1427                             bm[i].bm_name == NULL)
1428                                 continue;
1429 
1430                         if (bm[i].bm_type == BMT_ENV) {
1431                                 if (benv == NULL)
1432                                         benv = &bm[i];
1433                                 else
1434                                         continue;
1435                         }
1436 
1437                         (void) snprintf(modid, sizeof (modid),
1438                             "module-name-%u", midx);
1439                         bsetprops(modid, (char *)bm[i].bm_name);
1440                         (void) snprintf(modid, sizeof (modid),
1441                             "module-addr-%u", midx);
1442                         bsetprop64(modid, (uint64_t)(uintptr_t)bm[i].bm_addr);
1443                         (void) snprintf(modid, sizeof (modid),
1444                             "module-size-%u", midx);
1445                         bsetprop64(modid, (uint64_t)bm[i].bm_size);
1446                         ++midx;
1447                 }
1448                 if (rdbm != NULL) {
1449                         bsetprop64("ramdisk_start",
1450                             (uint64_t)(uintptr_t)rdbm->bm_addr);
1451                         bsetprop64("ramdisk_end",
1452                             (uint64_t)(uintptr_t)rdbm->bm_addr + rdbm->bm_size);
1453                 }
1454         }
1455 
1456         /*
1457          * If there are any boot time modules or hashes present, then disable
1458          * fast reboot.
1459          */
1460         if (xbp->bi_module_cnt > 1) {
1461                 fastreboot_disable(FBNS_BOOTMOD);
1462         }
1463 
1464 #ifndef __xpv
1465         /*
1466          * Disable fast reboot if we're using the Multiboot 2 boot protocol,
1467          * since we don't currently support MB2 info and module relocation.
1468          * Note that fast reboot will have already been disabled if multiple
1469          * modules are present, since the current implementation assumes that
1470          * we only have a single module, the boot_archive.
1471          */
1472         if (xbp->bi_mb_version != 1) {
1473                 fastreboot_disable(FBNS_MULTIBOOT2);
1474         }
1475 #endif
1476 
1477         DBG_MSG("Parsing command line for boot properties\n");
1478         value = xbp->bi_cmdline;
1479 
1480         /*
1481          * allocate memory to collect boot_args into
1482          */
1483         boot_arg_len = strlen(xbp->bi_cmdline) + 1;
1484         boot_args = do_bsys_alloc(NULL, NULL, boot_arg_len, MMU_PAGESIZE);
1485         boot_args[0] = 0;
1486         boot_arg_len = 0;
1487 
1488 #ifdef __xpv
1489         /*
1490          * Xen puts a lot of device information in front of the kernel name
1491          * let's grab them and make them boot properties.  The first
1492          * string w/o an "=" in it will be the boot-file property.
1493          */
1494         (void) strcpy(namebuf, "xpv-");
1495         for (;;) {
1496                 /*
1497                  * get to next property
1498                  */
1499                 while (ISSPACE(*value))
1500                         ++value;
1501                 name = value;
1502                 /*
1503                  * look for an "="
1504                  */
1505                 while (*value && !ISSPACE(*value) && *value != '=') {
1506                         value++;
1507                 }
1508                 if (*value != '=') { /* no "=" in the property */
1509                         value = name;
1510                         break;
1511                 }
1512                 name_len = value - name;
1513                 value_len = 0;
1514                 /*
1515                  * skip over the "="
1516                  */
1517                 value++;
1518                 while (value[value_len] && !ISSPACE(value[value_len])) {
1519                         ++value_len;
1520                 }
1521                 /*
1522                  * build property name with "xpv-" prefix
1523                  */
1524                 if (name_len + 4 > 32) { /* skip if name too long */
1525                         value += value_len;
1526                         continue;
1527                 }
1528                 bcopy(name, &namebuf[4], name_len);
1529                 name_len += 4;
1530                 namebuf[name_len] = 0;
1531                 bcopy(value, propbuf, value_len);
1532                 propbuf[value_len] = 0;
1533                 bsetprops(namebuf, propbuf);
1534 
1535                 /*
1536                  * xpv-root is set to the logical disk name of the xen
1537                  * VBD when booting from a disk-based filesystem.
1538                  */
1539                 if (strcmp(namebuf, "xpv-root") == 0)
1540                         xen_vbdroot_props(propbuf);
1541                 /*
1542                  * While we're here, if we have a "xpv-nfsroot" property
1543                  * then we need to set "fstype" to "nfs" so we mount
1544                  * our root from the nfs server.  Also parse the xpv-nfsroot
1545                  * property to create the properties that nfs_mountroot will
1546                  * need to find the root and mount it.
1547                  */
1548                 if (strcmp(namebuf, "xpv-nfsroot") == 0)
1549                         xen_nfsroot_props(propbuf);
1550 
1551                 if (strcmp(namebuf, "xpv-ip") == 0)
1552                         xen_ip_props(propbuf);
1553                 value += value_len;
1554         }
1555 #endif
1556 
1557         while (ISSPACE(*value))
1558                 ++value;
1559         /*
1560          * value now points at the boot-file
1561          */
1562         value_len = 0;
1563         while (value[value_len] && !ISSPACE(value[value_len]))
1564                 ++value_len;
1565         if (value_len > 0) {
1566                 whoami = propbuf;
1567                 bcopy(value, whoami, value_len);
1568                 whoami[value_len] = 0;
1569                 bsetprops("boot-file", whoami);
1570                 /*
1571                  * strip leading path stuff from whoami, so running from
1572                  * PXE/miniroot makes sense.
1573                  */
1574                 if (strstr(whoami, "/platform/") != NULL)
1575                         whoami = strstr(whoami, "/platform/");
1576                 bsetprops("whoami", whoami);
1577         }
1578 
1579         /*
1580          * Values forcibly set boot properties on the command line via -B.
1581          * Allow use of quotes in values. Other stuff goes on kernel
1582          * command line.
1583          */
1584         name = value + value_len;
1585         while (*name != 0) {
1586                 /*
1587                  * anything not " -B" is copied to the command line
1588                  */
1589                 if (!ISSPACE(name[0]) || name[1] != '-' || name[2] != 'B') {
1590                         boot_args[boot_arg_len++] = *name;
1591                         boot_args[boot_arg_len] = 0;
1592                         ++name;
1593                         continue;
1594                 }
1595 
1596                 /*
1597                  * skip the " -B" and following white space
1598                  */
1599                 name += 3;
1600                 while (ISSPACE(*name))
1601                         ++name;
1602                 while (*name && !ISSPACE(*name)) {
1603                         value = strstr(name, "=");
1604                         if (value == NULL)
1605                                 break;
1606                         name_len = value - name;
1607                         ++value;
1608                         value_len = 0;
1609                         quoted = 0;
1610                         for (; ; ++value_len) {
1611                                 if (!value[value_len])
1612                                         break;
1613 
1614                                 /*
1615                                  * is this value quoted?
1616                                  */
1617                                 if (value_len == 0 &&
1618                                     (value[0] == '\'' || value[0] == '"')) {
1619                                         quoted = value[0];
1620                                         ++value_len;
1621                                 }
1622 
1623                                 /*
1624                                  * In the quote accept any character,
1625                                  * but look for ending quote.
1626                                  */
1627                                 if (quoted) {
1628                                         if (value[value_len] == quoted)
1629                                                 quoted = 0;
1630                                         continue;
1631                                 }
1632 
1633                                 /*
1634                                  * a comma or white space ends the value
1635                                  */
1636                                 if (value[value_len] == ',' ||
1637                                     ISSPACE(value[value_len]))
1638                                         break;
1639                         }
1640 
1641                         if (value_len == 0) {
1642                                 bsetprop(DDI_PROP_TYPE_ANY, name, name_len,
1643                                     NULL, 0);
1644                         } else {
1645                                 char *v = value;
1646                                 int l = value_len;
1647                                 if (v[0] == v[l - 1] &&
1648                                     (v[0] == '\'' || v[0] == '"')) {
1649                                         ++v;
1650                                         l -= 2;
1651                                 }
1652                                 bcopy(v, propbuf, l);
1653                                 propbuf[l] = '\0';
1654                                 bsetprop(DDI_PROP_TYPE_STRING, name, name_len,
1655                                     propbuf, l + 1);
1656                         }
1657                         name = value + value_len;
1658                         while (*name == ',')
1659                                 ++name;
1660                 }
1661         }
1662 
1663         /*
1664          * set boot-args property
1665          * 1275 name is bootargs, so set
1666          * that too
1667          */
1668         bsetprops("boot-args", boot_args);
1669         bsetprops("bootargs", boot_args);
1670 
1671         process_boot_environment(benv);
1672 
1673 #ifndef __xpv
1674         /*
1675          * Build boot command line for Fast Reboot
1676          */
1677         build_fastboot_cmdline(xbp);
1678 
1679         if (xbp->bi_mb_version == 1) {
1680                 multiboot_info_t *mbi = xbp->bi_mb_info;
1681                 int netboot;
1682                 struct sol_netinfo *sip;
1683 
1684                 /*
1685                  * set the BIOS boot device from GRUB
1686                  */
1687                 netboot = 0;
1688 
1689                 /*
1690                  * Save various boot information for Fast Reboot
1691                  */
1692                 save_boot_info(xbp);
1693 
1694                 if (mbi != NULL && mbi->flags & MB_INFO_BOOTDEV) {
1695                         boot_device = mbi->boot_device >> 24;
1696                         if (boot_device == 0x20)
1697                                 netboot++;
1698                         str[0] = (boot_device >> 4) + '0';
1699                         str[1] = (boot_device & 0xf) + '0';
1700                         str[2] = 0;
1701                         bsetprops("bios-boot-device", str);
1702                 } else {
1703                         netboot = 1;
1704                 }
1705 
1706                 /*
1707                  * In the netboot case, drives_info is overloaded with the
1708                  * dhcp ack. This is not multiboot compliant and requires
1709                  * special pxegrub!
1710                  */
1711                 if (netboot && mbi->drives_length != 0) {
1712                         sip = (struct sol_netinfo *)(uintptr_t)mbi->drives_addr;
1713                         if (sip->sn_infotype == SN_TYPE_BOOTP)
1714                                 bsetprop(DDI_PROP_TYPE_BYTE,
1715                                     "bootp-response",
1716                                     sizeof ("bootp-response"),
1717                                     (void *)(uintptr_t)mbi->drives_addr,
1718                                     mbi->drives_length);
1719                         else if (sip->sn_infotype == SN_TYPE_RARP)
1720                                 setup_rarp_props(sip);
1721                 }
1722         } else {
1723                 multiboot2_info_header_t *mbi = xbp->bi_mb_info;
1724                 multiboot_tag_bootdev_t *bootdev = NULL;
1725                 multiboot_tag_network_t *netdev = NULL;
1726 
1727                 if (mbi != NULL) {
1728                         bootdev = dboot_multiboot2_find_tag(mbi,
1729                             MULTIBOOT_TAG_TYPE_BOOTDEV);
1730                         netdev = dboot_multiboot2_find_tag(mbi,
1731                             MULTIBOOT_TAG_TYPE_NETWORK);
1732                 }
1733                 if (bootdev != NULL) {
1734                         DBG(bootdev->mb_biosdev);
1735                         boot_device = bootdev->mb_biosdev;
1736                         str[0] = (boot_device >> 4) + '0';
1737                         str[1] = (boot_device & 0xf) + '0';
1738                         str[2] = 0;
1739                         bsetprops("bios-boot-device", str);
1740                 }
1741                 if (netdev != NULL) {
1742                         bsetprop(DDI_PROP_TYPE_BYTE,
1743                             "bootp-response", sizeof ("bootp-response"),
1744                             (void *)(uintptr_t)netdev->mb_dhcpack,
1745                             netdev->mb_size -
1746                             sizeof (multiboot_tag_network_t));
1747                 }
1748         }
1749 
1750         bsetprop32("stdout", stdout_val);
1751 #endif /* __xpv */
1752 
1753         /*
1754          * more conjured up values for made up things....
1755          */
1756 #if defined(__xpv)
1757         bsetprops("mfg-name", "i86xpv");
1758         bsetprops("impl-arch-name", "i86xpv");
1759 #else
1760         bsetprops("mfg-name", "i86pc");
1761         bsetprops("impl-arch-name", "i86pc");
1762 #endif
1763 
1764         /*
1765          * Build firmware-provided system properties
1766          */
1767         build_firmware_properties(xbp);
1768 
1769         /*
1770          * XXPV
1771          *
1772          * Find out what these are:
1773          * - cpuid_feature_ecx_include
1774          * - cpuid_feature_ecx_exclude
1775          * - cpuid_feature_edx_include
1776          * - cpuid_feature_edx_exclude
1777          *
1778          * Find out what these are in multiboot:
1779          * - netdev-path
1780          * - fstype
1781          */
1782 }
1783 
1784 #ifdef __xpv
1785 /*
1786  * Under the Hypervisor, memory usable for DMA may be scarce. One
1787  * very likely large pool of DMA friendly memory is occupied by
1788  * the boot_archive, as it was loaded by grub into low MFNs.
1789  *
1790  * Here we free up that memory by copying the boot archive to what are
1791  * likely higher MFN pages and then swapping the mfn/pfn mappings.
1792  */
1793 #define PFN_2GIG        0x80000
1794 static void
1795 relocate_boot_archive(struct xboot_info *xbp)
1796 {
1797         mfn_t max_mfn = HYPERVISOR_memory_op(XENMEM_maximum_ram_page, NULL);
1798         struct boot_modules *bm = xbp->bi_modules;
1799         uintptr_t va;
1800         pfn_t va_pfn;
1801         mfn_t va_mfn;
1802         caddr_t copy;
1803         pfn_t copy_pfn;
1804         mfn_t copy_mfn;
1805         size_t  len;
1806         int slop;
1807         int total = 0;
1808         int relocated = 0;
1809         int mmu_update_return;
1810         mmu_update_t t[2];
1811         x86pte_t pte;
1812 
1813         /*
1814          * If all MFN's are below 2Gig, don't bother doing this.
1815          */
1816         if (max_mfn < PFN_2GIG)
1817                 return;
1818         if (xbp->bi_module_cnt < 1) {
1819                 DBG_MSG("no boot_archive!");
1820                 return;
1821         }
1822 
1823         DBG_MSG("moving boot_archive to high MFN memory\n");
1824         va = (uintptr_t)bm->bm_addr;
1825         len = bm->bm_size;
1826         slop = va & MMU_PAGEOFFSET;
1827         if (slop) {
1828                 va += MMU_PAGESIZE - slop;
1829                 len -= MMU_PAGESIZE - slop;
1830         }
1831         len = P2ALIGN(len, MMU_PAGESIZE);
1832 
1833         /*
1834          * Go through all boot_archive pages, swapping any low MFN pages
1835          * with memory at next_phys.
1836          */
1837         while (len != 0) {
1838                 ++total;
1839                 va_pfn = mmu_btop(va - ONE_GIG);
1840                 va_mfn = mfn_list[va_pfn];
1841                 if (mfn_list[va_pfn] < PFN_2GIG) {
1842                         copy = kbm_remap_window(next_phys, 1);
1843                         bcopy((void *)va, copy, MMU_PAGESIZE);
1844                         copy_pfn = mmu_btop(next_phys);
1845                         copy_mfn = mfn_list[copy_pfn];
1846 
1847                         pte = mfn_to_ma(copy_mfn) | PT_NOCONSIST | PT_VALID;
1848                         if (HYPERVISOR_update_va_mapping(va, pte,
1849                             UVMF_INVLPG | UVMF_LOCAL))
1850                                 bop_panic("relocate_boot_archive():  "
1851                                     "HYPERVISOR_update_va_mapping() failed");
1852 
1853                         mfn_list[va_pfn] = copy_mfn;
1854                         mfn_list[copy_pfn] = va_mfn;
1855 
1856                         t[0].ptr = mfn_to_ma(copy_mfn) | MMU_MACHPHYS_UPDATE;
1857                         t[0].val = va_pfn;
1858                         t[1].ptr = mfn_to_ma(va_mfn) | MMU_MACHPHYS_UPDATE;
1859                         t[1].val = copy_pfn;
1860                         if (HYPERVISOR_mmu_update(t, 2, &mmu_update_return,
1861                             DOMID_SELF) != 0 || mmu_update_return != 2)
1862                                 bop_panic("relocate_boot_archive():  "
1863                                     "HYPERVISOR_mmu_update() failed");
1864 
1865                         next_phys += MMU_PAGESIZE;
1866                         ++relocated;
1867                 }
1868                 len -= MMU_PAGESIZE;
1869                 va += MMU_PAGESIZE;
1870         }
1871         DBG_MSG("Relocated pages:\n");
1872         DBG(relocated);
1873         DBG_MSG("Out of total pages:\n");
1874         DBG(total);
1875 }
1876 #endif /* __xpv */
1877 
1878 #if !defined(__xpv)
1879 /*
1880  * simple description of a stack frame (args are 32 bit only currently)
1881  */
1882 typedef struct bop_frame {
1883         struct bop_frame *old_frame;
1884         pc_t retaddr;
1885         long arg[1];
1886 } bop_frame_t;
1887 
1888 void
1889 bop_traceback(bop_frame_t *frame)
1890 {
1891         pc_t pc;
1892         int cnt;
1893         char *ksym;
1894         ulong_t off;
1895 
1896         bop_printf(NULL, "Stack traceback:\n");
1897         for (cnt = 0; cnt < 30; ++cnt) {     /* up to 30 frames */
1898                 pc = frame->retaddr;
1899                 if (pc == 0)
1900                         break;
1901                 ksym = kobj_getsymname(pc, &off);
1902                 if (ksym)
1903                         bop_printf(NULL, "  %s+%lx", ksym, off);
1904                 else
1905                         bop_printf(NULL, "  0x%lx", pc);
1906 
1907                 frame = frame->old_frame;
1908                 if (frame == 0) {
1909                         bop_printf(NULL, "\n");
1910                         break;
1911                 }
1912                 bop_printf(NULL, "\n");
1913         }
1914 }
1915 
1916 struct trapframe {
1917         ulong_t error_code;     /* optional */
1918         ulong_t inst_ptr;
1919         ulong_t code_seg;
1920         ulong_t flags_reg;
1921         ulong_t stk_ptr;
1922         ulong_t stk_seg;
1923 };
1924 
1925 void
1926 bop_trap(ulong_t *tfp)
1927 {
1928         struct trapframe *tf = (struct trapframe *)tfp;
1929         bop_frame_t fakeframe;
1930         static int depth = 0;
1931 
1932         /*
1933          * Check for an infinite loop of traps.
1934          */
1935         if (++depth > 2)
1936                 bop_panic("Nested trap");
1937 
1938         bop_printf(NULL, "Unexpected trap\n");
1939 
1940         /*
1941          * adjust the tf for optional error_code by detecting the code selector
1942          */
1943         if (tf->code_seg != B64CODE_SEL)
1944                 tf = (struct trapframe *)(tfp - 1);
1945         else
1946                 bop_printf(NULL, "error code           0x%lx\n",
1947                     tf->error_code & 0xffffffff);
1948 
1949         bop_printf(NULL, "instruction pointer  0x%lx\n", tf->inst_ptr);
1950         bop_printf(NULL, "code segment         0x%lx\n", tf->code_seg & 0xffff);
1951         bop_printf(NULL, "flags register       0x%lx\n", tf->flags_reg);
1952         bop_printf(NULL, "return %%rsp          0x%lx\n", tf->stk_ptr);
1953         bop_printf(NULL, "return %%ss           0x%lx\n", tf->stk_seg & 0xffff);
1954 
1955         /* grab %[er]bp pushed by our code from the stack */
1956         fakeframe.old_frame = (bop_frame_t *)*(tfp - 3);
1957         fakeframe.retaddr = (pc_t)tf->inst_ptr;
1958         bop_printf(NULL, "Attempting stack backtrace:\n");
1959         bop_traceback(&fakeframe);
1960         bop_panic("unexpected trap in early boot");
1961 }
1962 
1963 extern void bop_trap_handler(void);
1964 
1965 static gate_desc_t *bop_idt;
1966 
1967 static desctbr_t bop_idt_info;
1968 
1969 /*
1970  * Install a temporary IDT that lets us catch errors in the boot time code.
1971  * We shouldn't get any faults at all while this is installed, so we'll
1972  * just generate a traceback and exit.
1973  */
1974 static void
1975 bop_idt_init(void)
1976 {
1977         int t;
1978 
1979         bop_idt = (gate_desc_t *)
1980             do_bsys_alloc(NULL, NULL, MMU_PAGESIZE, MMU_PAGESIZE);
1981         bzero(bop_idt, MMU_PAGESIZE);
1982         for (t = 0; t < NIDT; ++t) {
1983                 /*
1984                  * Note that since boot runs without a TSS, the
1985                  * double fault handler cannot use an alternate stack (64-bit).
1986                  */
1987                 set_gatesegd(&bop_idt[t], &bop_trap_handler, B64CODE_SEL,
1988                     SDT_SYSIGT, TRP_KPL, 0);
1989         }
1990         bop_idt_info.dtr_limit = (NIDT * sizeof (gate_desc_t)) - 1;
1991         bop_idt_info.dtr_base = (uintptr_t)bop_idt;
1992         wr_idtr(&bop_idt_info);
1993 }
1994 #endif  /* !defined(__xpv) */
1995 
1996 /*
1997  * This is where we enter the kernel. It dummies up the boot_ops and
1998  * boot_syscalls vectors and jumps off to _kobj_boot()
1999  */
2000 void
2001 _start(struct xboot_info *xbp)
2002 {
2003         bootops_t *bops = &bootop;
2004         extern void _kobj_boot();
2005 
2006         /*
2007          * 1st off - initialize the console for any error messages
2008          */
2009         xbootp = xbp;
2010 #ifdef __xpv
2011         HYPERVISOR_shared_info = (void *)xbp->bi_shared_info;
2012         xen_info = xbp->bi_xen_start_info;
2013 #endif
2014 
2015 #ifndef __xpv
2016         if (*((uint32_t *)(FASTBOOT_SWTCH_PA + FASTBOOT_STACK_OFFSET)) ==
2017             FASTBOOT_MAGIC) {
2018                 post_fastreboot = 1;
2019                 *((uint32_t *)(FASTBOOT_SWTCH_PA + FASTBOOT_STACK_OFFSET)) = 0;
2020         }
2021 #endif
2022 
2023         bcons_init(xbp);
2024         have_console = 1;
2025 
2026         /*
2027          * enable debugging
2028          */
2029         if (find_boot_prop("kbm_debug") != NULL)
2030                 kbm_debug = 1;
2031 
2032         DBG_MSG("\n\n*** Entered Solaris in _start() cmdline is: ");
2033         DBG_MSG((char *)xbp->bi_cmdline);
2034         DBG_MSG("\n\n\n");
2035 
2036         /*
2037          * physavail is no longer used by startup
2038          */
2039         bm.physinstalled = xbp->bi_phys_install;
2040         bm.pcimem = xbp->bi_pcimem;
2041         bm.rsvdmem = xbp->bi_rsvdmem;
2042         bm.physavail = NULL;
2043 
2044         /*
2045          * initialize the boot time allocator
2046          */
2047         next_phys = xbp->bi_next_paddr;
2048         DBG(next_phys);
2049         next_virt = (uintptr_t)xbp->bi_next_vaddr;
2050         DBG(next_virt);
2051         DBG_MSG("Initializing boot time memory management...");
2052 #ifdef __xpv
2053         {
2054                 xen_platform_parameters_t p;
2055 
2056                 /* This call shouldn't fail, dboot already did it once. */
2057                 (void) HYPERVISOR_xen_version(XENVER_platform_parameters, &p);
2058                 mfn_to_pfn_mapping = (pfn_t *)(xen_virt_start = p.virt_start);
2059                 DBG(xen_virt_start);
2060         }
2061 #endif
2062         kbm_init(xbp);
2063         DBG_MSG("done\n");
2064 
2065         /*
2066          * Fill in the bootops vector
2067          */
2068         bops->bsys_version = BO_VERSION;
2069         bops->boot_mem = &bm;
2070         bops->bsys_alloc = do_bsys_alloc;
2071         bops->bsys_free = do_bsys_free;
2072         bops->bsys_getproplen = do_bsys_getproplen;
2073         bops->bsys_getprop = do_bsys_getprop;
2074         bops->bsys_nextprop = do_bsys_nextprop;
2075         bops->bsys_printf = bop_printf;
2076         bops->bsys_doint = do_bsys_doint;
2077 
2078         /*
2079          * BOP_EALLOC() is no longer needed
2080          */
2081         bops->bsys_ealloc = do_bsys_ealloc;
2082 
2083 #ifdef __xpv
2084         /*
2085          * On domain 0 we need to free up some physical memory that is
2086          * usable for DMA. Since GRUB loaded the boot_archive, it is
2087          * sitting in low MFN memory. We'll relocated the boot archive
2088          * pages to high PFN memory.
2089          */
2090         if (DOMAIN_IS_INITDOMAIN(xen_info))
2091                 relocate_boot_archive(xbp);
2092 #endif
2093 
2094 #ifndef __xpv
2095         /*
2096          * Install an IDT to catch early pagefaults (shouldn't have any).
2097          * Also needed for kmdb.
2098          */
2099         bop_idt_init();
2100 #endif
2101         /* Set up the shadow fb for framebuffer console */
2102         boot_fb_shadow_init(bops);
2103 
2104         /*
2105          * Start building the boot properties from the command line
2106          */
2107         DBG_MSG("Initializing boot properties:\n");
2108         build_boot_properties(xbp);
2109 
2110         if (find_boot_prop("prom_debug") || kbm_debug) {
2111                 char *value;
2112 
2113                 value = do_bsys_alloc(NULL, NULL, MMU_PAGESIZE, MMU_PAGESIZE);
2114                 boot_prop_display(value);
2115         }
2116 
2117         /*
2118          * jump into krtld...
2119          */
2120         _kobj_boot(&bop_sysp, NULL, bops, NULL);
2121 }
2122 
2123 
2124 /*ARGSUSED*/
2125 static caddr_t
2126 no_more_alloc(bootops_t *bop, caddr_t virthint, size_t size, int align)
2127 {
2128         panic("Attempt to bsys_alloc() too late\n");
2129         return (NULL);
2130 }
2131 
2132 /*ARGSUSED*/
2133 static void
2134 no_more_free(bootops_t *bop, caddr_t virt, size_t size)
2135 {
2136         panic("Attempt to bsys_free() too late\n");
2137 }
2138 
2139 void
2140 bop_no_more_mem(void)
2141 {
2142         DBG(total_bop_alloc_scratch);
2143         DBG(total_bop_alloc_kernel);
2144         bootops->bsys_alloc = no_more_alloc;
2145         bootops->bsys_free = no_more_free;
2146 }
2147 
2148 
2149 /*
2150  * Set ACPI firmware properties
2151  */
2152 
2153 static caddr_t
2154 vmap_phys(size_t length, paddr_t pa)
2155 {
2156         paddr_t start, end;
2157         caddr_t va;
2158         size_t  len, page;
2159 
2160 #ifdef __xpv
2161         pa = pfn_to_pa(xen_assign_pfn(mmu_btop(pa))) | (pa & MMU_PAGEOFFSET);
2162 #endif
2163         start = P2ALIGN(pa, MMU_PAGESIZE);
2164         end = P2ROUNDUP(pa + length, MMU_PAGESIZE);
2165         len = end - start;
2166         va = (caddr_t)alloc_vaddr(len, MMU_PAGESIZE);
2167         for (page = 0; page < len; page += MMU_PAGESIZE)
2168                 kbm_map((uintptr_t)va + page, start + page, 0, 0);
2169         return (va + (pa & MMU_PAGEOFFSET));
2170 }
2171 
2172 static uint8_t
2173 checksum_table(uint8_t *tp, size_t len)
2174 {
2175         uint8_t sum = 0;
2176 
2177         while (len-- > 0)
2178                 sum += *tp++;
2179 
2180         return (sum);
2181 }
2182 
2183 static int
2184 valid_rsdp(ACPI_TABLE_RSDP *rp)
2185 {
2186 
2187         /* validate the V1.x checksum */
2188         if (checksum_table((uint8_t *)rp, ACPI_RSDP_CHECKSUM_LENGTH) != 0)
2189                 return (0);
2190 
2191         /* If pre-ACPI 2.0, this is a valid RSDP */
2192         if (rp->Revision < 2)
2193                 return (1);
2194 
2195         /* validate the V2.x checksum */
2196         if (checksum_table((uint8_t *)rp, ACPI_RSDP_XCHECKSUM_LENGTH) != 0)
2197                 return (0);
2198 
2199         return (1);
2200 }
2201 
2202 /*
2203  * Scan memory range for an RSDP;
2204  * see ACPI 3.0 Spec, 5.2.5.1
2205  */
2206 static ACPI_TABLE_RSDP *
2207 scan_rsdp(paddr_t start, paddr_t end)
2208 {
2209         ssize_t len  = end - start;
2210         caddr_t ptr;
2211 
2212         ptr = vmap_phys(len, start);
2213         while (len > 0) {
2214                 if (strncmp(ptr, ACPI_SIG_RSDP, strlen(ACPI_SIG_RSDP)) == 0 &&
2215                     valid_rsdp((ACPI_TABLE_RSDP *)ptr))
2216                         return ((ACPI_TABLE_RSDP *)ptr);
2217 
2218                 ptr += ACPI_RSDP_SCAN_STEP;
2219                 len -= ACPI_RSDP_SCAN_STEP;
2220         }
2221 
2222         return (NULL);
2223 }
2224 
2225 /*
2226  * Refer to ACPI 3.0 Spec, section 5.2.5.1 to understand this function
2227  */
2228 static ACPI_TABLE_RSDP *
2229 find_rsdp()
2230 {
2231         ACPI_TABLE_RSDP *rsdp;
2232         uint64_t rsdp_val = 0;
2233         uint16_t *ebda_seg;
2234         paddr_t  ebda_addr;
2235 
2236         /* check for "acpi-root-tab" property */
2237         if (do_bsys_getproplen(NULL, "acpi-root-tab") == sizeof (uint64_t)) {
2238                 (void) do_bsys_getprop(NULL, "acpi-root-tab", &rsdp_val);
2239                 if (rsdp_val != 0) {
2240                         rsdp = scan_rsdp(rsdp_val, rsdp_val + sizeof (*rsdp));
2241                         if (rsdp != NULL) {
2242                                 if (kbm_debug) {
2243                                         bop_printf(NULL,
2244                                             "Using RSDP from bootloader: "
2245                                             "0x%p\n", (void *)rsdp);
2246                                 }
2247                                 return (rsdp);
2248                         }
2249                 }
2250         }
2251 
2252         /*
2253          * Get the EBDA segment and scan the first 1K
2254          */
2255         ebda_seg = (uint16_t *)vmap_phys(sizeof (uint16_t),
2256             ACPI_EBDA_PTR_LOCATION);
2257         ebda_addr = *ebda_seg << 4;
2258         rsdp = scan_rsdp(ebda_addr, ebda_addr + ACPI_EBDA_WINDOW_SIZE);
2259         if (rsdp == NULL)
2260                 /* if EBDA doesn't contain RSDP, look in BIOS memory */
2261                 rsdp = scan_rsdp(ACPI_HI_RSDP_WINDOW_BASE,
2262                     ACPI_HI_RSDP_WINDOW_BASE + ACPI_HI_RSDP_WINDOW_SIZE);
2263         return (rsdp);
2264 }
2265 
2266 static ACPI_TABLE_HEADER *
2267 map_fw_table(paddr_t table_addr)
2268 {
2269         ACPI_TABLE_HEADER *tp;
2270         size_t len = MAX(sizeof (*tp), MMU_PAGESIZE);
2271 
2272         /*
2273          * Map at least a page; if the table is larger than this, remap it
2274          */
2275         tp = (ACPI_TABLE_HEADER *)vmap_phys(len, table_addr);
2276         if (tp->Length > len)
2277                 tp = (ACPI_TABLE_HEADER *)vmap_phys(tp->Length, table_addr);
2278         return (tp);
2279 }
2280 
2281 static ACPI_TABLE_HEADER *
2282 find_fw_table(char *signature)
2283 {
2284         static int revision = 0;
2285         static ACPI_TABLE_XSDT *xsdt;
2286         static int len;
2287         paddr_t xsdt_addr;
2288         ACPI_TABLE_RSDP *rsdp;
2289         ACPI_TABLE_HEADER *tp;
2290         paddr_t table_addr;
2291         int     n;
2292 
2293         if (strlen(signature) != ACPI_NAME_SIZE)
2294                 return (NULL);
2295 
2296         /*
2297          * Reading the ACPI 3.0 Spec, section 5.2.5.3 will help
2298          * understand this code.  If we haven't already found the RSDT/XSDT,
2299          * revision will be 0. Find the RSDP and check the revision
2300          * to find out whether to use the RSDT or XSDT.  If revision is
2301          * 0 or 1, use the RSDT and set internal revision to 1; if it is 2,
2302          * use the XSDT.  If the XSDT address is 0, though, fall back to
2303          * revision 1 and use the RSDT.
2304          */
2305         if (revision == 0) {
2306                 if ((rsdp = find_rsdp()) != NULL) {
2307                         revision = rsdp->Revision;
2308                         /*
2309                          * ACPI 6.0 states that current revision is 2
2310                          * from acpi_table_rsdp definition:
2311                          * Must be (0) for ACPI 1.0 or (2) for ACPI 2.0+
2312                          */
2313                         if (revision > 2)
2314                                 revision = 2;
2315                         switch (revision) {
2316                         case 2:
2317                                 /*
2318                                  * Use the XSDT unless BIOS is buggy and
2319                                  * claims to be rev 2 but has a null XSDT
2320                                  * address
2321                                  */
2322                                 xsdt_addr = rsdp->XsdtPhysicalAddress;
2323                                 if (xsdt_addr != 0)
2324                                         break;
2325                                 /* FALLTHROUGH */
2326                         case 0:
2327                                 /* treat RSDP rev 0 as revision 1 internally */
2328                                 revision = 1;
2329                                 /* FALLTHROUGH */
2330                         case 1:
2331                                 /* use the RSDT for rev 0/1 */
2332                                 xsdt_addr = rsdp->RsdtPhysicalAddress;
2333                                 break;
2334                         default:
2335                                 /* unknown revision */
2336                                 revision = 0;
2337                                 break;
2338                         }
2339                 }
2340                 if (revision == 0)
2341                         return (NULL);
2342 
2343                 /* cache the XSDT info */
2344                 xsdt = (ACPI_TABLE_XSDT *)map_fw_table(xsdt_addr);
2345                 len = (xsdt->Header.Length - sizeof (xsdt->Header)) /
2346                     ((revision == 1) ? sizeof (uint32_t) : sizeof (uint64_t));
2347         }
2348 
2349         /*
2350          * Scan the table headers looking for a signature match
2351          */
2352         for (n = 0; n < len; n++) {
2353                 ACPI_TABLE_RSDT *rsdt = (ACPI_TABLE_RSDT *)xsdt;
2354                 table_addr = (revision == 1) ? rsdt->TableOffsetEntry[n] :
2355                     xsdt->TableOffsetEntry[n];
2356 
2357                 if (table_addr == 0)
2358                         continue;
2359                 tp = map_fw_table(table_addr);
2360                 if (strncmp(tp->Signature, signature, ACPI_NAME_SIZE) == 0) {
2361                         return (tp);
2362                 }
2363         }
2364         return (NULL);
2365 }
2366 
2367 static void
2368 process_mcfg(ACPI_TABLE_MCFG *tp)
2369 {
2370         ACPI_MCFG_ALLOCATION *cfg_baap;
2371         char *cfg_baa_endp;
2372         int64_t ecfginfo[4];
2373 
2374         cfg_baap = (ACPI_MCFG_ALLOCATION *)((uintptr_t)tp + sizeof (*tp));
2375         cfg_baa_endp = ((char *)tp) + tp->Header.Length;
2376         while ((char *)cfg_baap < cfg_baa_endp) {
2377                 if (cfg_baap->Address != 0 && cfg_baap->PciSegment == 0) {
2378                         ecfginfo[0] = cfg_baap->Address;
2379                         ecfginfo[1] = cfg_baap->PciSegment;
2380                         ecfginfo[2] = cfg_baap->StartBusNumber;
2381                         ecfginfo[3] = cfg_baap->EndBusNumber;
2382                         bsetprop(DDI_PROP_TYPE_INT64,
2383                             MCFG_PROPNAME, strlen(MCFG_PROPNAME),
2384                             ecfginfo, sizeof (ecfginfo));
2385                         break;
2386                 }
2387                 cfg_baap++;
2388         }
2389 }
2390 
2391 #ifndef __xpv
2392 static void
2393 process_madt_entries(ACPI_TABLE_MADT *tp, uint32_t *cpu_countp,
2394     uint32_t *cpu_possible_countp, uint32_t *cpu_apicid_array)
2395 {
2396         ACPI_SUBTABLE_HEADER *item, *end;
2397         uint32_t cpu_count = 0;
2398         uint32_t cpu_possible_count = 0;
2399 
2400         /*
2401          * Determine number of CPUs and keep track of "final" APIC ID
2402          * for each CPU by walking through ACPI MADT processor list
2403          */
2404         end = (ACPI_SUBTABLE_HEADER *)(tp->Header.Length + (uintptr_t)tp);
2405         item = (ACPI_SUBTABLE_HEADER *)((uintptr_t)tp + sizeof (*tp));
2406 
2407         while (item < end) {
2408                 switch (item->Type) {
2409                 case ACPI_MADT_TYPE_LOCAL_APIC: {
2410                         ACPI_MADT_LOCAL_APIC *cpu =
2411                             (ACPI_MADT_LOCAL_APIC *) item;
2412 
2413                         if (cpu->LapicFlags & ACPI_MADT_ENABLED) {
2414                                 if (cpu_apicid_array != NULL)
2415                                         cpu_apicid_array[cpu_count] = cpu->Id;
2416                                 cpu_count++;
2417                         }
2418                         cpu_possible_count++;
2419                         break;
2420                 }
2421                 case ACPI_MADT_TYPE_LOCAL_X2APIC: {
2422                         ACPI_MADT_LOCAL_X2APIC *cpu =
2423                             (ACPI_MADT_LOCAL_X2APIC *) item;
2424 
2425                         if (cpu->LapicFlags & ACPI_MADT_ENABLED) {
2426                                 if (cpu_apicid_array != NULL)
2427                                         cpu_apicid_array[cpu_count] =
2428                                             cpu->LocalApicId;
2429                                 cpu_count++;
2430                         }
2431                         cpu_possible_count++;
2432                         break;
2433                 }
2434                 default:
2435                         if (kbm_debug)
2436                                 bop_printf(NULL, "MADT type %d\n", item->Type);
2437                         break;
2438                 }
2439 
2440                 item = (ACPI_SUBTABLE_HEADER *)((uintptr_t)item + item->Length);
2441         }
2442         if (cpu_countp)
2443                 *cpu_countp = cpu_count;
2444         if (cpu_possible_countp)
2445                 *cpu_possible_countp = cpu_possible_count;
2446 }
2447 
2448 static void
2449 process_madt(ACPI_TABLE_MADT *tp)
2450 {
2451         uint32_t cpu_count = 0;
2452         uint32_t cpu_possible_count = 0;
2453         uint32_t *cpu_apicid_array; /* x2APIC ID is 32bit! */
2454 
2455         if (tp != NULL) {
2456                 /* count cpu's */
2457                 process_madt_entries(tp, &cpu_count, &cpu_possible_count, NULL);
2458 
2459                 cpu_apicid_array = (uint32_t *)do_bsys_alloc(NULL, NULL,
2460                     cpu_count * sizeof (*cpu_apicid_array), MMU_PAGESIZE);
2461                 if (cpu_apicid_array == NULL)
2462                         bop_panic("Not enough memory for APIC ID array");
2463 
2464                 /* copy IDs */
2465                 process_madt_entries(tp, NULL, NULL, cpu_apicid_array);
2466 
2467                 /*
2468                  * Make boot property for array of "final" APIC IDs for each
2469                  * CPU
2470                  */
2471                 bsetprop(DDI_PROP_TYPE_INT,
2472                     BP_CPU_APICID_ARRAY, strlen(BP_CPU_APICID_ARRAY),
2473                     cpu_apicid_array, cpu_count * sizeof (*cpu_apicid_array));
2474         }
2475 
2476         /*
2477          * Check whether property plat-max-ncpus is already set.
2478          */
2479         if (do_bsys_getproplen(NULL, PLAT_MAX_NCPUS_NAME) < 0) {
2480                 /*
2481                  * Set plat-max-ncpus to number of maximum possible CPUs given
2482                  * in MADT if it hasn't been set.
2483                  * There's no formal way to detect max possible CPUs supported
2484                  * by platform according to ACPI spec3.0b. So current CPU
2485                  * hotplug implementation expects that all possible CPUs will
2486                  * have an entry in MADT table and set plat-max-ncpus to number
2487                  * of entries in MADT.
2488                  * With introducing of ACPI4.0, Maximum System Capability Table
2489                  * (MSCT) provides maximum number of CPUs supported by platform.
2490                  * If MSCT is unavailable, fall back to old way.
2491                  */
2492                 if (tp != NULL)
2493                         bsetpropsi(PLAT_MAX_NCPUS_NAME, cpu_possible_count);
2494         }
2495 
2496         /*
2497          * Set boot property boot-max-ncpus to number of CPUs existing at
2498          * boot time. boot-max-ncpus is mainly used for optimization.
2499          */
2500         if (tp != NULL)
2501                 bsetpropsi(BOOT_MAX_NCPUS_NAME, cpu_count);
2502 
2503         /*
2504          * User-set boot-ncpus overrides firmware count
2505          */
2506         if (do_bsys_getproplen(NULL, BOOT_NCPUS_NAME) >= 0)
2507                 return;
2508 
2509         /*
2510          * Set boot property boot-ncpus to number of active CPUs given in MADT
2511          * if it hasn't been set yet.
2512          */
2513         if (tp != NULL)
2514                 bsetpropsi(BOOT_NCPUS_NAME, cpu_count);
2515 }
2516 
2517 static void
2518 process_srat(ACPI_TABLE_SRAT *tp)
2519 {
2520         ACPI_SUBTABLE_HEADER *item, *end;
2521         int i;
2522         int proc_num, mem_num;
2523 #pragma pack(1)
2524         struct {
2525                 uint32_t domain;
2526                 uint32_t apic_id;
2527                 uint32_t sapic_id;
2528         } processor;
2529         struct {
2530                 uint32_t domain;
2531                 uint32_t x2apic_id;
2532         } x2apic;
2533         struct {
2534                 uint32_t domain;
2535                 uint64_t addr;
2536                 uint64_t length;
2537                 uint32_t flags;
2538         } memory;
2539 #pragma pack()
2540         char prop_name[30];
2541         uint64_t maxmem = 0;
2542 
2543         if (tp == NULL)
2544                 return;
2545 
2546         proc_num = mem_num = 0;
2547         end = (ACPI_SUBTABLE_HEADER *)(tp->Header.Length + (uintptr_t)tp);
2548         item = (ACPI_SUBTABLE_HEADER *)((uintptr_t)tp + sizeof (*tp));
2549         while (item < end) {
2550                 switch (item->Type) {
2551                 case ACPI_SRAT_TYPE_CPU_AFFINITY: {
2552                         ACPI_SRAT_CPU_AFFINITY *cpu =
2553                             (ACPI_SRAT_CPU_AFFINITY *) item;
2554 
2555                         if (!(cpu->Flags & ACPI_SRAT_CPU_ENABLED))
2556                                 break;
2557                         processor.domain = cpu->ProximityDomainLo;
2558                         for (i = 0; i < 3; i++)
2559                                 processor.domain +=
2560                                     cpu->ProximityDomainHi[i] << ((i + 1) * 8);
2561                         processor.apic_id = cpu->ApicId;
2562                         processor.sapic_id = cpu->LocalSapicEid;
2563                         (void) snprintf(prop_name, 30, "acpi-srat-processor-%d",
2564                             proc_num);
2565                         bsetprop(DDI_PROP_TYPE_INT,
2566                             prop_name, strlen(prop_name), &processor,
2567                             sizeof (processor));
2568                         proc_num++;
2569                         break;
2570                 }
2571                 case ACPI_SRAT_TYPE_MEMORY_AFFINITY: {
2572                         ACPI_SRAT_MEM_AFFINITY *mem =
2573                             (ACPI_SRAT_MEM_AFFINITY *)item;
2574 
2575                         if (!(mem->Flags & ACPI_SRAT_MEM_ENABLED))
2576                                 break;
2577                         memory.domain = mem->ProximityDomain;
2578                         memory.addr = mem->BaseAddress;
2579                         memory.length = mem->Length;
2580                         memory.flags = mem->Flags;
2581                         (void) snprintf(prop_name, 30, "acpi-srat-memory-%d",
2582                             mem_num);
2583                         bsetprop(DDI_PROP_TYPE_INT,
2584                             prop_name, strlen(prop_name), &memory,
2585                             sizeof (memory));
2586                         if ((mem->Flags & ACPI_SRAT_MEM_HOT_PLUGGABLE) &&
2587                             (memory.addr + memory.length > maxmem)) {
2588                                 maxmem = memory.addr + memory.length;
2589                         }
2590                         mem_num++;
2591                         break;
2592                 }
2593                 case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY: {
2594                         ACPI_SRAT_X2APIC_CPU_AFFINITY *x2cpu =
2595                             (ACPI_SRAT_X2APIC_CPU_AFFINITY *) item;
2596 
2597                         if (!(x2cpu->Flags & ACPI_SRAT_CPU_ENABLED))
2598                                 break;
2599                         x2apic.domain = x2cpu->ProximityDomain;
2600                         x2apic.x2apic_id = x2cpu->ApicId;
2601                         (void) snprintf(prop_name, 30, "acpi-srat-processor-%d",
2602                             proc_num);
2603                         bsetprop(DDI_PROP_TYPE_INT,
2604                             prop_name, strlen(prop_name), &x2apic,
2605                             sizeof (x2apic));
2606                         proc_num++;
2607                         break;
2608                 }
2609                 default:
2610                         if (kbm_debug)
2611                                 bop_printf(NULL, "SRAT type %d\n", item->Type);
2612                         break;
2613                 }
2614 
2615                 item = (ACPI_SUBTABLE_HEADER *)
2616                     (item->Length + (uintptr_t)item);
2617         }
2618 
2619         /*
2620          * The maximum physical address calculated from the SRAT table is more
2621          * accurate than that calculated from the MSCT table.
2622          */
2623         if (maxmem != 0) {
2624                 plat_dr_physmax = btop(maxmem);
2625         }
2626 }
2627 
2628 static void
2629 process_slit(ACPI_TABLE_SLIT *tp)
2630 {
2631 
2632         /*
2633          * Check the number of localities; if it's too huge, we just
2634          * return and locality enumeration code will handle this later,
2635          * if possible.
2636          *
2637          * Note that the size of the table is the square of the
2638          * number of localities; if the number of localities exceeds
2639          * UINT16_MAX, the table size may overflow an int when being
2640          * passed to bsetprop() below.
2641          */
2642         if (tp->LocalityCount >= SLIT_LOCALITIES_MAX)
2643                 return;
2644 
2645         bsetprop64(SLIT_NUM_PROPNAME, tp->LocalityCount);
2646         bsetprop(DDI_PROP_TYPE_BYTE,
2647             SLIT_PROPNAME, strlen(SLIT_PROPNAME), &tp->Entry,
2648             tp->LocalityCount * tp->LocalityCount);
2649 }
2650 
2651 static ACPI_TABLE_MSCT *
2652 process_msct(ACPI_TABLE_MSCT *tp)
2653 {
2654         int last_seen = 0;
2655         int proc_num = 0;
2656         ACPI_MSCT_PROXIMITY *item, *end;
2657         extern uint64_t plat_dr_options;
2658 
2659         ASSERT(tp != NULL);
2660 
2661         end = (ACPI_MSCT_PROXIMITY *)(tp->Header.Length + (uintptr_t)tp);
2662         for (item = (void *)((uintptr_t)tp + tp->ProximityOffset);
2663             item < end;
2664             item = (void *)(item->Length + (uintptr_t)item)) {
2665                 /*
2666                  * Sanity check according to section 5.2.19.1 of ACPI 4.0.
2667                  * Revision     1
2668                  * Length       22
2669                  */
2670                 if (item->Revision != 1 || item->Length != 22) {
2671                         cmn_err(CE_CONT,
2672                             "?boot: unknown proximity domain structure in MSCT "
2673                             "with Revision(%d), Length(%d).\n",
2674                             (int)item->Revision, (int)item->Length);
2675                         return (NULL);
2676                 } else if (item->RangeStart > item->RangeEnd) {
2677                         cmn_err(CE_CONT,
2678                             "?boot: invalid proximity domain structure in MSCT "
2679                             "with RangeStart(%u), RangeEnd(%u).\n",
2680                             item->RangeStart, item->RangeEnd);
2681                         return (NULL);
2682                 } else if (item->RangeStart != last_seen) {
2683                         /*
2684                          * Items must be organized in ascending order of the
2685                          * proximity domain enumerations.
2686                          */
2687                         cmn_err(CE_CONT,
2688                             "?boot: invalid proximity domain structure in MSCT,"
2689                             " items are not orginized in ascending order.\n");
2690                         return (NULL);
2691                 }
2692 
2693                 /*
2694                  * If ProcessorCapacity is 0 then there would be no CPUs in this
2695                  * domain.
2696                  */
2697                 if (item->ProcessorCapacity != 0) {
2698                         proc_num += (item->RangeEnd - item->RangeStart + 1) *
2699                             item->ProcessorCapacity;
2700                 }
2701 
2702                 last_seen = item->RangeEnd - item->RangeStart + 1;
2703                 /*
2704                  * Break out if all proximity domains have been processed.
2705                  * Some BIOSes may have unused items at the end of MSCT table.
2706                  */
2707                 if (last_seen > tp->MaxProximityDomains) {
2708                         break;
2709                 }
2710         }
2711         if (last_seen != tp->MaxProximityDomains + 1) {
2712                 cmn_err(CE_CONT,
2713                     "?boot: invalid proximity domain structure in MSCT, "
2714                     "proximity domain count doesn't match.\n");
2715                 return (NULL);
2716         }
2717 
2718         /*
2719          * Set plat-max-ncpus property if it hasn't been set yet.
2720          */
2721         if (do_bsys_getproplen(NULL, PLAT_MAX_NCPUS_NAME) < 0) {
2722                 if (proc_num != 0) {
2723                         bsetpropsi(PLAT_MAX_NCPUS_NAME, proc_num);
2724                 }
2725         }
2726 
2727         /*
2728          * Use Maximum Physical Address from the MSCT table as upper limit for
2729          * memory hot-adding by default. It may be overridden by value from
2730          * the SRAT table or the "plat-dr-physmax" boot option.
2731          */
2732         plat_dr_physmax = btop(tp->MaxAddress + 1);
2733 
2734         /*
2735          * Existence of MSCT implies CPU/memory hotplug-capability for the
2736          * platform.
2737          */
2738         plat_dr_options |= PLAT_DR_FEATURE_CPU;
2739         plat_dr_options |= PLAT_DR_FEATURE_MEMORY;
2740 
2741         return (tp);
2742 }
2743 
2744 #else /* __xpv */
2745 static void
2746 enumerate_xen_cpus()
2747 {
2748         processorid_t   id, max_id;
2749 
2750         /*
2751          * User-set boot-ncpus overrides enumeration
2752          */
2753         if (do_bsys_getproplen(NULL, BOOT_NCPUS_NAME) >= 0)
2754                 return;
2755 
2756         /*
2757          * Probe every possible virtual CPU id and remember the
2758          * highest id present; the count of CPUs is one greater
2759          * than this.  This tacitly assumes at least cpu 0 is present.
2760          */
2761         max_id = 0;
2762         for (id = 0; id < MAX_VIRT_CPUS; id++)
2763                 if (HYPERVISOR_vcpu_op(VCPUOP_is_up, id, NULL) == 0)
2764                         max_id = id;
2765 
2766         bsetpropsi(BOOT_NCPUS_NAME, max_id+1);
2767 
2768 }
2769 #endif /* __xpv */
2770 
2771 /*ARGSUSED*/
2772 static void
2773 build_firmware_properties(struct xboot_info *xbp)
2774 {
2775         ACPI_TABLE_HEADER *tp = NULL;
2776 
2777 #ifndef __xpv
2778         if (xbp->bi_uefi_arch == XBI_UEFI_ARCH_64) {
2779                 bsetprops("efi-systype", "64");
2780                 bsetprop64("efi-systab",
2781                     (uint64_t)(uintptr_t)xbp->bi_uefi_systab);
2782                 if (kbm_debug)
2783                         bop_printf(NULL, "64-bit UEFI detected.\n");
2784         } else if (xbp->bi_uefi_arch == XBI_UEFI_ARCH_32) {
2785                 bsetprops("efi-systype", "32");
2786                 bsetprop64("efi-systab",
2787                     (uint64_t)(uintptr_t)xbp->bi_uefi_systab);
2788                 if (kbm_debug)
2789                         bop_printf(NULL, "32-bit UEFI detected.\n");
2790         }
2791 
2792         if (xbp->bi_acpi_rsdp != NULL) {
2793                 bsetprop64("acpi-root-tab",
2794                     (uint64_t)(uintptr_t)xbp->bi_acpi_rsdp);
2795         }
2796 
2797         if (xbp->bi_smbios != NULL) {
2798                 bsetprop64("smbios-address",
2799                     (uint64_t)(uintptr_t)xbp->bi_smbios);
2800         }
2801 
2802         if ((tp = find_fw_table(ACPI_SIG_MSCT)) != NULL)
2803                 msct_ptr = process_msct((ACPI_TABLE_MSCT *)tp);
2804         else
2805                 msct_ptr = NULL;
2806 
2807         if ((tp = find_fw_table(ACPI_SIG_MADT)) != NULL)
2808                 process_madt((ACPI_TABLE_MADT *)tp);
2809 
2810         if ((srat_ptr = (ACPI_TABLE_SRAT *)
2811             find_fw_table(ACPI_SIG_SRAT)) != NULL)
2812                 process_srat(srat_ptr);
2813 
2814         if (slit_ptr = (ACPI_TABLE_SLIT *)find_fw_table(ACPI_SIG_SLIT))
2815                 process_slit(slit_ptr);
2816 
2817         tp = find_fw_table(ACPI_SIG_MCFG);
2818 #else /* __xpv */
2819         enumerate_xen_cpus();
2820         if (DOMAIN_IS_INITDOMAIN(xen_info))
2821                 tp = find_fw_table(ACPI_SIG_MCFG);
2822 #endif /* __xpv */
2823         if (tp != NULL)
2824                 process_mcfg((ACPI_TABLE_MCFG *)tp);
2825 }
2826 
2827 /*
2828  * fake up a boot property for deferred early console output
2829  * this is used by both graphical boot and the (developer only)
2830  * USB serial console
2831  */
2832 void *
2833 defcons_init(size_t size)
2834 {
2835         static char *p = NULL;
2836 
2837         p = do_bsys_alloc(NULL, NULL, size, MMU_PAGESIZE);
2838         *p = 0;
2839         bsetprop32("deferred-console-buf", (uint32_t)((uintptr_t)&p));
2840         return (p);
2841 }
2842 
2843 /*ARGSUSED*/
2844 int
2845 boot_compinfo(int fd, struct compinfo *cbp)
2846 {
2847         cbp->iscmp = 0;
2848         cbp->blksize = MAXBSIZE;
2849         return (0);
2850 }
2851 
2852 #define BP_MAX_STRLEN   32
2853 
2854 /*
2855  * Get value for given boot property
2856  */
2857 int
2858 bootprop_getval(const char *prop_name, u_longlong_t *prop_value)
2859 {
2860         int             boot_prop_len;
2861         char            str[BP_MAX_STRLEN];
2862         u_longlong_t    value;
2863 
2864         boot_prop_len = BOP_GETPROPLEN(bootops, prop_name);
2865         if (boot_prop_len < 0 || boot_prop_len > sizeof (str) ||
2866             BOP_GETPROP(bootops, prop_name, str) < 0 ||
2867             kobj_getvalue(str, &value) == -1)
2868                 return (-1);
2869 
2870         if (prop_value)
2871                 *prop_value = value;
2872 
2873         return (0);
2874 }