9000 unix: cleanup 32-bit specific code in fakebop
[unleashed.git] / usr / src / uts / i86pc / os / fakebop.c
blob9379072264f57d78a468f2bea19d5a20ca0fc84f
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2010, Intel Corporation.
27 * All rights reserved.
29 * Copyright 2013 Joyent, Inc. All rights reserved.
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.
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/cmn_err.h>
54 #include <sys/systm.h>
55 #include <sys/promif.h>
56 #include <sys/archsystm.h>
57 #include <sys/x86_archext.h>
58 #include <sys/kobj.h>
59 #include <sys/privregs.h>
60 #include <sys/sysmacros.h>
61 #include <sys/ctype.h>
62 #include <sys/fastboot.h>
63 #ifdef __xpv
64 #include <sys/hypervisor.h>
65 #include <net/if.h>
66 #endif
67 #include <vm/kboot_mmu.h>
68 #include <vm/hat_pte.h>
69 #include <sys/kobj.h>
70 #include <sys/kobj_lex.h>
71 #include <sys/pci_cfgspace_impl.h>
72 #include <sys/fastboot_impl.h>
73 #include <sys/acpi/acconfig.h>
74 #include <sys/acpi/acpi.h>
76 static int have_console = 0; /* set once primitive console is initialized */
77 static char *boot_args = "";
80 * Debugging macros
82 static uint_t kbm_debug = 0;
83 #define DBG_MSG(s) { if (kbm_debug) bop_printf(NULL, "%s", s); }
84 #define DBG(x) { if (kbm_debug) \
85 bop_printf(NULL, "%s is %" PRIx64 "\n", #x, (uint64_t)(x)); \
88 #define PUT_STRING(s) { \
89 char *cp; \
90 for (cp = (s); *cp; ++cp) \
91 bcons_putchar(*cp); \
94 bootops_t bootop; /* simple bootops we'll pass on to kernel */
95 struct bsys_mem bm;
98 * Boot info from "glue" code in low memory. xbootp is used by:
99 * do_bop_phys_alloc(), do_bsys_alloc() and boot_prop_finish().
101 static struct xboot_info *xbootp;
102 static uintptr_t next_virt; /* next available virtual address */
103 static paddr_t next_phys; /* next available physical address from dboot */
104 static paddr_t high_phys = -(paddr_t)1; /* last used physical address */
107 * buffer for vsnprintf for console I/O
109 #define BUFFERSIZE 512
110 static char buffer[BUFFERSIZE];
113 * stuff to store/report/manipulate boot property settings.
115 typedef struct bootprop {
116 struct bootprop *bp_next;
117 char *bp_name;
118 uint_t bp_vlen;
119 char *bp_value;
120 } bootprop_t;
122 static bootprop_t *bprops = NULL;
123 static char *curr_page = NULL; /* ptr to avail bprop memory */
124 static int curr_space = 0; /* amount of memory at curr_page */
126 #ifdef __xpv
127 start_info_t *xen_info;
128 shared_info_t *HYPERVISOR_shared_info;
129 #endif
132 * some allocator statistics
134 static ulong_t total_bop_alloc_scratch = 0;
135 static ulong_t total_bop_alloc_kernel = 0;
137 static void build_firmware_properties(struct xboot_info *);
139 static int early_allocation = 1;
141 int force_fastreboot = 0;
142 volatile int fastreboot_onpanic = 0;
143 int post_fastreboot = 0;
144 #ifdef __xpv
145 volatile int fastreboot_capable = 0;
146 #else
147 volatile int fastreboot_capable = 1;
148 #endif
151 * Information saved from current boot for fast reboot.
152 * If the information size exceeds what we have allocated, fast reboot
153 * will not be supported.
155 multiboot_info_t saved_mbi;
156 mb_memory_map_t saved_mmap[FASTBOOT_SAVED_MMAP_COUNT];
157 uint8_t saved_drives[FASTBOOT_SAVED_DRIVES_SIZE];
158 char saved_cmdline[FASTBOOT_SAVED_CMDLINE_LEN];
159 int saved_cmdline_len = 0;
160 size_t saved_file_size[FASTBOOT_MAX_FILES_MAP];
163 * Turn off fastreboot_onpanic to avoid panic loop.
165 char fastreboot_onpanic_cmdline[FASTBOOT_SAVED_CMDLINE_LEN];
166 static const char fastreboot_onpanic_args[] = " -B fastreboot_onpanic=0";
169 * Pointers to where System Resource Affinity Table (SRAT), System Locality
170 * Information Table (SLIT) and Maximum System Capability Table (MSCT)
171 * are mapped into virtual memory
173 ACPI_TABLE_SRAT *srat_ptr = NULL;
174 ACPI_TABLE_SLIT *slit_ptr = NULL;
175 ACPI_TABLE_MSCT *msct_ptr = NULL;
178 * Arbitrary limit on number of localities we handle; if
179 * this limit is raised to more than UINT16_MAX, make sure
180 * process_slit() knows how to handle it.
182 #define SLIT_LOCALITIES_MAX (4096)
184 #define SLIT_NUM_PROPNAME "acpi-slit-localities"
185 #define SLIT_PROPNAME "acpi-slit"
188 * Allocate aligned physical memory at boot time. This allocator allocates
189 * from the highest possible addresses. This avoids exhausting memory that
190 * would be useful for DMA buffers.
192 paddr_t
193 do_bop_phys_alloc(uint64_t size, uint64_t align)
195 paddr_t pa = 0;
196 paddr_t start;
197 paddr_t end;
198 struct memlist *ml = (struct memlist *)xbootp->bi_phys_install;
201 * Be careful if high memory usage is limited in startup.c
202 * Since there are holes in the low part of the physical address
203 * space we can treat physmem as a pfn (not just a pgcnt) and
204 * get a conservative upper limit.
206 if (physmem != 0 && high_phys > pfn_to_pa(physmem))
207 high_phys = pfn_to_pa(physmem);
210 * find the highest available memory in physinstalled
212 size = P2ROUNDUP(size, align);
213 for (; ml; ml = ml->ml_next) {
214 start = P2ROUNDUP(ml->ml_address, align);
215 end = P2ALIGN(ml->ml_address + ml->ml_size, align);
216 if (start < next_phys)
217 start = P2ROUNDUP(next_phys, align);
218 if (end > high_phys)
219 end = P2ALIGN(high_phys, align);
221 if (end <= start)
222 continue;
223 if (end - start < size)
224 continue;
227 * Early allocations need to use low memory, since
228 * physmem might be further limited by bootenv.rc
230 if (early_allocation) {
231 if (pa == 0 || start < pa)
232 pa = start;
233 } else {
234 if (end - size > pa)
235 pa = end - size;
238 if (pa != 0) {
239 if (early_allocation)
240 next_phys = pa + size;
241 else
242 high_phys = pa;
243 return (pa);
245 bop_panic("do_bop_phys_alloc(0x%" PRIx64 ", 0x%" PRIx64
246 ") Out of memory\n", size, align);
247 /*NOTREACHED*/
250 uintptr_t
251 alloc_vaddr(size_t size, paddr_t align)
253 uintptr_t rv;
255 next_virt = P2ROUNDUP(next_virt, (uintptr_t)align);
256 rv = (uintptr_t)next_virt;
257 next_virt += size;
258 return (rv);
262 * Allocate virtual memory. The size is always rounded up to a multiple
263 * of base pagesize.
266 /*ARGSUSED*/
267 static caddr_t
268 do_bsys_alloc(bootops_t *bop, caddr_t virthint, size_t size, int align)
270 paddr_t a = align; /* same type as pa for masking */
271 uint_t pgsize;
272 paddr_t pa;
273 uintptr_t va;
274 ssize_t s; /* the aligned size */
275 uint_t level;
276 uint_t is_kernel = (virthint != 0);
278 if (a < MMU_PAGESIZE)
279 a = MMU_PAGESIZE;
280 else if (!ISP2(a))
281 prom_panic("do_bsys_alloc() incorrect alignment");
282 size = P2ROUNDUP(size, MMU_PAGESIZE);
285 * Use the next aligned virtual address if we weren't given one.
287 if (virthint == NULL) {
288 virthint = (caddr_t)alloc_vaddr(size, a);
289 total_bop_alloc_scratch += size;
290 } else {
291 total_bop_alloc_kernel += size;
295 * allocate the physical memory
297 pa = do_bop_phys_alloc(size, a);
300 * Add the mappings to the page tables, try large pages first.
302 va = (uintptr_t)virthint;
303 s = size;
304 level = 1;
305 pgsize = xbootp->bi_use_pae ? TWO_MEG : FOUR_MEG;
306 if (xbootp->bi_use_largepage && a == pgsize) {
307 while (IS_P2ALIGNED(pa, pgsize) && IS_P2ALIGNED(va, pgsize) &&
308 s >= pgsize) {
309 kbm_map(va, pa, level, is_kernel);
310 va += pgsize;
311 pa += pgsize;
312 s -= pgsize;
317 * Map remaining pages use small mappings
319 level = 0;
320 pgsize = MMU_PAGESIZE;
321 while (s > 0) {
322 kbm_map(va, pa, level, is_kernel);
323 va += pgsize;
324 pa += pgsize;
325 s -= pgsize;
327 return (virthint);
331 * Free virtual memory - we'll just ignore these.
333 /*ARGSUSED*/
334 static void
335 do_bsys_free(bootops_t *bop, caddr_t virt, size_t size)
337 bop_printf(NULL, "do_bsys_free(virt=0x%p, size=0x%lx) ignored\n",
338 (void *)virt, size);
342 * Old interface
344 /*ARGSUSED*/
345 static caddr_t
346 do_bsys_ealloc(bootops_t *bop, caddr_t virthint, size_t size,
347 int align, int flags)
349 prom_panic("unsupported call to BOP_EALLOC()\n");
350 return (0);
354 static void
355 bsetprop(char *name, int nlen, void *value, int vlen)
357 uint_t size;
358 uint_t need_size;
359 bootprop_t *b;
362 * align the size to 16 byte boundary
364 size = sizeof (bootprop_t) + nlen + 1 + vlen;
365 size = (size + 0xf) & ~0xf;
366 if (size > curr_space) {
367 need_size = (size + (MMU_PAGEOFFSET)) & MMU_PAGEMASK;
368 curr_page = do_bsys_alloc(NULL, 0, need_size, MMU_PAGESIZE);
369 curr_space = need_size;
373 * use a bootprop_t at curr_page and link into list
375 b = (bootprop_t *)curr_page;
376 curr_page += sizeof (bootprop_t);
377 curr_space -= sizeof (bootprop_t);
378 b->bp_next = bprops;
379 bprops = b;
382 * follow by name and ending zero byte
384 b->bp_name = curr_page;
385 bcopy(name, curr_page, nlen);
386 curr_page += nlen;
387 *curr_page++ = 0;
388 curr_space -= nlen + 1;
391 * copy in value, but no ending zero byte
393 b->bp_value = curr_page;
394 b->bp_vlen = vlen;
395 if (vlen > 0) {
396 bcopy(value, curr_page, vlen);
397 curr_page += vlen;
398 curr_space -= vlen;
402 * align new values of curr_page, curr_space
404 while (curr_space & 0xf) {
405 ++curr_page;
406 --curr_space;
410 static void
411 bsetprops(char *name, char *value)
413 bsetprop(name, strlen(name), value, strlen(value) + 1);
416 static void
417 bsetprop64(char *name, uint64_t value)
419 bsetprop(name, strlen(name), (void *)&value, sizeof (value));
422 static void
423 bsetpropsi(char *name, int value)
425 char prop_val[32];
427 (void) snprintf(prop_val, sizeof (prop_val), "%d", value);
428 bsetprops(name, prop_val);
432 * to find the size of the buffer to allocate
434 /*ARGSUSED*/
436 do_bsys_getproplen(bootops_t *bop, const char *name)
438 bootprop_t *b;
440 for (b = bprops; b; b = b->bp_next) {
441 if (strcmp(name, b->bp_name) != 0)
442 continue;
443 return (b->bp_vlen);
445 return (-1);
449 * get the value associated with this name
451 /*ARGSUSED*/
453 do_bsys_getprop(bootops_t *bop, const char *name, void *value)
455 bootprop_t *b;
457 for (b = bprops; b; b = b->bp_next) {
458 if (strcmp(name, b->bp_name) != 0)
459 continue;
460 bcopy(b->bp_value, value, b->bp_vlen);
461 return (0);
463 return (-1);
467 * get the name of the next property in succession from the standalone
469 /*ARGSUSED*/
470 static char *
471 do_bsys_nextprop(bootops_t *bop, char *name)
473 bootprop_t *b;
476 * A null name is a special signal for the 1st boot property
478 if (name == NULL || strlen(name) == 0) {
479 if (bprops == NULL)
480 return (NULL);
481 return (bprops->bp_name);
484 for (b = bprops; b; b = b->bp_next) {
485 if (name != b->bp_name)
486 continue;
487 b = b->bp_next;
488 if (b == NULL)
489 return (NULL);
490 return (b->bp_name);
492 return (NULL);
496 * Parse numeric value from a string. Understands decimal, hex, octal, - and ~
498 static int
499 parse_value(char *p, uint64_t *retval)
501 int adjust = 0;
502 uint64_t tmp = 0;
503 int digit;
504 int radix = 10;
506 *retval = 0;
507 if (*p == '-' || *p == '~')
508 adjust = *p++;
510 if (*p == '0') {
511 ++p;
512 if (*p == 0)
513 return (0);
514 if (*p == 'x' || *p == 'X') {
515 radix = 16;
516 ++p;
517 } else {
518 radix = 8;
519 ++p;
522 while (*p) {
523 if ('0' <= *p && *p <= '9')
524 digit = *p - '0';
525 else if ('a' <= *p && *p <= 'f')
526 digit = 10 + *p - 'a';
527 else if ('A' <= *p && *p <= 'F')
528 digit = 10 + *p - 'A';
529 else
530 return (-1);
531 if (digit >= radix)
532 return (-1);
533 tmp = tmp * radix + digit;
534 ++p;
536 if (adjust == '-')
537 tmp = -tmp;
538 else if (adjust == '~')
539 tmp = ~tmp;
540 *retval = tmp;
541 return (0);
544 static boolean_t
545 unprintable(char *value, int size)
547 int i;
549 if (size <= 0 || value[0] == '\0')
550 return (B_TRUE);
552 for (i = 0; i < size; i++) {
553 if (value[i] == '\0')
554 return (i != (size - 1));
556 if (!isprint(value[i]))
557 return (B_TRUE);
559 return (B_FALSE);
563 * Print out information about all boot properties.
564 * buffer is pointer to pre-allocated space to be used as temporary
565 * space for property values.
567 static void
568 boot_prop_display(char *buffer)
570 char *name = "";
571 int i, len;
573 bop_printf(NULL, "\nBoot properties:\n");
575 while ((name = do_bsys_nextprop(NULL, name)) != NULL) {
576 bop_printf(NULL, "\t0x%p %s = ", (void *)name, name);
577 (void) do_bsys_getprop(NULL, name, buffer);
578 len = do_bsys_getproplen(NULL, name);
579 bop_printf(NULL, "len=%d ", len);
580 if (!unprintable(buffer, len)) {
581 buffer[len] = 0;
582 bop_printf(NULL, "%s\n", buffer);
583 continue;
585 for (i = 0; i < len; i++) {
586 bop_printf(NULL, "%02x", buffer[i] & 0xff);
587 if (i < len - 1)
588 bop_printf(NULL, ".");
590 bop_printf(NULL, "\n");
595 * 2nd part of building the table of boot properties. This includes:
596 * - values from /boot/solaris/bootenv.rc (ie. eeprom(1m) values)
598 * lines look like one of:
599 * ^$
600 * ^# comment till end of line
601 * setprop name 'value'
602 * setprop name value
603 * setprop name "value"
605 * we do single character I/O since this is really just looking at memory
607 void
608 boot_prop_finish(void)
610 int fd;
611 char *line;
612 int c;
613 int bytes_read;
614 char *name;
615 int n_len;
616 char *value;
617 int v_len;
618 char *inputdev; /* these override the command line if serial ports */
619 char *outputdev;
620 char *consoledev;
621 uint64_t lvalue;
622 int use_xencons = 0;
624 #ifdef __xpv
625 if (!DOMAIN_IS_INITDOMAIN(xen_info))
626 use_xencons = 1;
627 #endif /* __xpv */
629 DBG_MSG("Opening /boot/solaris/bootenv.rc\n");
630 fd = BRD_OPEN(bfs_ops, "/boot/solaris/bootenv.rc", 0);
631 DBG(fd);
633 line = do_bsys_alloc(NULL, NULL, MMU_PAGESIZE, MMU_PAGESIZE);
634 while (fd >= 0) {
637 * get a line
639 for (c = 0; ; ++c) {
640 bytes_read = BRD_READ(bfs_ops, fd, line + c, 1);
641 if (bytes_read == 0) {
642 if (c == 0)
643 goto done;
644 break;
646 if (line[c] == '\n')
647 break;
649 line[c] = 0;
652 * ignore comment lines
654 c = 0;
655 while (ISSPACE(line[c]))
656 ++c;
657 if (line[c] == '#' || line[c] == 0)
658 continue;
661 * must have "setprop " or "setprop\t"
663 if (strncmp(line + c, "setprop ", 8) != 0 &&
664 strncmp(line + c, "setprop\t", 8) != 0)
665 continue;
666 c += 8;
667 while (ISSPACE(line[c]))
668 ++c;
669 if (line[c] == 0)
670 continue;
673 * gather up the property name
675 name = line + c;
676 n_len = 0;
677 while (line[c] && !ISSPACE(line[c]))
678 ++n_len, ++c;
681 * gather up the value, if any
683 value = "";
684 v_len = 0;
685 while (ISSPACE(line[c]))
686 ++c;
687 if (line[c] != 0) {
688 value = line + c;
689 while (line[c] && !ISSPACE(line[c]))
690 ++v_len, ++c;
693 if (v_len >= 2 && value[0] == value[v_len - 1] &&
694 (value[0] == '\'' || value[0] == '"')) {
695 ++value;
696 v_len -= 2;
698 name[n_len] = 0;
699 if (v_len > 0)
700 value[v_len] = 0;
701 else
702 continue;
705 * ignore "boot-file" property, it's now meaningless
707 if (strcmp(name, "boot-file") == 0)
708 continue;
709 if (strcmp(name, "boot-args") == 0 &&
710 strlen(boot_args) > 0)
711 continue;
714 * If a property was explicitly set on the command line
715 * it will override a setting in bootenv.rc
717 if (do_bsys_getproplen(NULL, name) > 0)
718 continue;
720 bsetprop(name, n_len, value, v_len + 1);
722 done:
723 if (fd >= 0)
724 (void) BRD_CLOSE(bfs_ops, fd);
727 * Check if we have to limit the boot time allocator
729 if (do_bsys_getproplen(NULL, "physmem") != -1 &&
730 do_bsys_getprop(NULL, "physmem", line) >= 0 &&
731 parse_value(line, &lvalue) != -1) {
732 if (0 < lvalue && (lvalue < physmem || physmem == 0)) {
733 physmem = (pgcnt_t)lvalue;
734 DBG(physmem);
737 early_allocation = 0;
740 * check to see if we have to override the default value of the console
742 if (!use_xencons) {
743 inputdev = line;
744 v_len = do_bsys_getproplen(NULL, "input-device");
745 if (v_len > 0)
746 (void) do_bsys_getprop(NULL, "input-device", inputdev);
747 else
748 v_len = 0;
749 inputdev[v_len] = 0;
751 outputdev = inputdev + v_len + 1;
752 v_len = do_bsys_getproplen(NULL, "output-device");
753 if (v_len > 0)
754 (void) do_bsys_getprop(NULL, "output-device",
755 outputdev);
756 else
757 v_len = 0;
758 outputdev[v_len] = 0;
760 consoledev = outputdev + v_len + 1;
761 v_len = do_bsys_getproplen(NULL, "console");
762 if (v_len > 0) {
763 (void) do_bsys_getprop(NULL, "console", consoledev);
764 if (post_fastreboot &&
765 strcmp(consoledev, "graphics") == 0) {
766 bsetprops("console", "text");
767 v_len = strlen("text");
768 bcopy("text", consoledev, v_len);
770 } else {
771 v_len = 0;
773 consoledev[v_len] = 0;
774 bcons_init2(inputdev, outputdev, consoledev);
775 } else {
777 * Ensure console property exists
778 * If not create it as "hypervisor"
780 v_len = do_bsys_getproplen(NULL, "console");
781 if (v_len < 0)
782 bsetprops("console", "hypervisor");
783 inputdev = outputdev = consoledev = "hypervisor";
784 bcons_init2(inputdev, outputdev, consoledev);
787 if (find_boot_prop("prom_debug") || kbm_debug)
788 boot_prop_display(line);
792 * print formatted output
794 /*PRINTFLIKE2*/
795 /*ARGSUSED*/
796 void
797 bop_printf(bootops_t *bop, const char *fmt, ...)
799 va_list ap;
801 if (have_console == 0)
802 return;
804 va_start(ap, fmt);
805 (void) vsnprintf(buffer, BUFFERSIZE, fmt, ap);
806 va_end(ap);
807 PUT_STRING(buffer);
811 * Another panic() variant; this one can be used even earlier during boot than
812 * prom_panic().
814 /*PRINTFLIKE1*/
815 void
816 bop_panic(const char *fmt, ...)
818 va_list ap;
820 va_start(ap, fmt);
821 bop_printf(NULL, fmt, ap);
822 va_end(ap);
824 bop_printf(NULL, "\nPress any key to reboot.\n");
825 (void) bcons_getchar();
826 bop_printf(NULL, "Resetting...\n");
827 pc_reset();
831 * Do a real mode interrupt BIOS call
833 typedef struct bios_regs {
834 unsigned short ax, bx, cx, dx, si, di, bp, es, ds;
835 } bios_regs_t;
836 typedef int (*bios_func_t)(int, bios_regs_t *);
838 /*ARGSUSED*/
839 static void
840 do_bsys_doint(bootops_t *bop, int intnum, struct bop_regs *rp)
842 #if defined(__xpv)
843 prom_panic("unsupported call to BOP_DOINT()\n");
844 #else /* __xpv */
845 static int firsttime = 1;
846 bios_func_t bios_func = (bios_func_t)(void *)(uintptr_t)0x5000;
847 bios_regs_t br;
850 * The first time we do this, we have to copy the pre-packaged
851 * low memory bios call code image into place.
853 if (firsttime) {
854 extern char bios_image[];
855 extern uint32_t bios_size;
857 bcopy(bios_image, (void *)bios_func, bios_size);
858 firsttime = 0;
861 br.ax = rp->eax.word.ax;
862 br.bx = rp->ebx.word.bx;
863 br.cx = rp->ecx.word.cx;
864 br.dx = rp->edx.word.dx;
865 br.bp = rp->ebp.word.bp;
866 br.si = rp->esi.word.si;
867 br.di = rp->edi.word.di;
868 br.ds = rp->ds;
869 br.es = rp->es;
871 DBG_MSG("Doing BIOS call...");
872 DBG(br.ax);
873 DBG(br.bx);
874 DBG(br.dx);
875 rp->eflags = bios_func(intnum, &br);
876 DBG_MSG("done\n");
878 rp->eax.word.ax = br.ax;
879 rp->ebx.word.bx = br.bx;
880 rp->ecx.word.cx = br.cx;
881 rp->edx.word.dx = br.dx;
882 rp->ebp.word.bp = br.bp;
883 rp->esi.word.si = br.si;
884 rp->edi.word.di = br.di;
885 rp->ds = br.ds;
886 rp->es = br.es;
887 #endif /* __xpv */
890 static struct boot_syscalls bop_sysp = {
891 bcons_getchar,
892 bcons_putchar,
893 bcons_ischar,
896 static char *whoami;
898 #define BUFLEN 64
900 #if defined(__xpv)
902 static char namebuf[32];
904 static void
905 xen_parse_props(char *s, char *prop_map[], int n_prop)
907 char **prop_name = prop_map;
908 char *cp = s, *scp;
910 do {
911 scp = cp;
912 while ((*cp != NULL) && (*cp != ':'))
913 cp++;
915 if ((scp != cp) && (*prop_name != NULL)) {
916 *cp = NULL;
917 bsetprops(*prop_name, scp);
920 cp++;
921 prop_name++;
922 n_prop--;
923 } while (n_prop > 0);
926 #define VBDPATHLEN 64
929 * parse the 'xpv-root' property to create properties used by
930 * ufs_mountroot.
932 static void
933 xen_vbdroot_props(char *s)
935 char vbdpath[VBDPATHLEN] = "/xpvd/xdf@";
936 const char lnamefix[] = "/dev/dsk/c0d";
937 char *pnp;
938 char *prop_p;
939 char mi;
940 short minor;
941 long addr = 0;
943 pnp = vbdpath + strlen(vbdpath);
944 prop_p = s + strlen(lnamefix);
945 while ((*prop_p != '\0') && (*prop_p != 's') && (*prop_p != 'p'))
946 addr = addr * 10 + *prop_p++ - '0';
947 (void) snprintf(pnp, VBDPATHLEN, "%lx", addr);
948 pnp = vbdpath + strlen(vbdpath);
949 if (*prop_p == 's')
950 mi = 'a';
951 else if (*prop_p == 'p')
952 mi = 'q';
953 else
954 ASSERT(0); /* shouldn't be here */
955 prop_p++;
956 ASSERT(*prop_p != '\0');
957 if (ISDIGIT(*prop_p)) {
958 minor = *prop_p - '0';
959 prop_p++;
960 if (ISDIGIT(*prop_p)) {
961 minor = minor * 10 + *prop_p - '0';
963 } else {
964 /* malformed root path, use 0 as default */
965 minor = 0;
967 ASSERT(minor < 16); /* at most 16 partitions */
968 mi += minor;
969 *pnp++ = ':';
970 *pnp++ = mi;
971 *pnp++ = '\0';
972 bsetprops("fstype", "ufs");
973 bsetprops("bootpath", vbdpath);
975 DBG_MSG("VBD bootpath set to ");
976 DBG_MSG(vbdpath);
977 DBG_MSG("\n");
981 * parse the xpv-nfsroot property to create properties used by
982 * nfs_mountroot.
984 static void
985 xen_nfsroot_props(char *s)
987 char *prop_map[] = {
988 BP_SERVER_IP, /* server IP address */
989 BP_SERVER_NAME, /* server hostname */
990 BP_SERVER_PATH, /* root path */
992 int n_prop = sizeof (prop_map) / sizeof (prop_map[0]);
994 bsetprop("fstype", 6, "nfs", 4);
996 xen_parse_props(s, prop_map, n_prop);
999 * If a server name wasn't specified, use a default.
1001 if (do_bsys_getproplen(NULL, BP_SERVER_NAME) == -1)
1002 bsetprops(BP_SERVER_NAME, "unknown");
1006 * Extract our IP address, etc. from the "xpv-ip" property.
1008 static void
1009 xen_ip_props(char *s)
1011 char *prop_map[] = {
1012 BP_HOST_IP, /* IP address */
1013 NULL, /* NFS server IP address (ignored in */
1014 /* favour of xpv-nfsroot) */
1015 BP_ROUTER_IP, /* IP gateway */
1016 BP_SUBNET_MASK, /* IP subnet mask */
1017 "xpv-hostname", /* hostname (ignored) */
1018 BP_NETWORK_INTERFACE, /* interface name */
1019 "xpv-hcp", /* host configuration protocol */
1021 int n_prop = sizeof (prop_map) / sizeof (prop_map[0]);
1022 char ifname[IFNAMSIZ];
1024 xen_parse_props(s, prop_map, n_prop);
1027 * A Linux dom0 administrator expects all interfaces to be
1028 * called "ethX", which is not the case here.
1030 * If the interface name specified is "eth0", presume that
1031 * this is really intended to be "xnf0" (the first domU ->
1032 * dom0 interface for this domain).
1034 if ((do_bsys_getprop(NULL, BP_NETWORK_INTERFACE, ifname) == 0) &&
1035 (strcmp("eth0", ifname) == 0)) {
1036 bsetprops(BP_NETWORK_INTERFACE, "xnf0");
1037 bop_printf(NULL,
1038 "network interface name 'eth0' replaced with 'xnf0'\n");
1042 #else /* __xpv */
1044 static void
1045 setup_rarp_props(struct sol_netinfo *sip)
1047 char buf[BUFLEN]; /* to hold ip/mac addrs */
1048 uint8_t *val;
1050 val = (uint8_t *)&sip->sn_ciaddr;
1051 (void) snprintf(buf, BUFLEN, "%d.%d.%d.%d",
1052 val[0], val[1], val[2], val[3]);
1053 bsetprops(BP_HOST_IP, buf);
1055 val = (uint8_t *)&sip->sn_siaddr;
1056 (void) snprintf(buf, BUFLEN, "%d.%d.%d.%d",
1057 val[0], val[1], val[2], val[3]);
1058 bsetprops(BP_SERVER_IP, buf);
1060 if (sip->sn_giaddr != 0) {
1061 val = (uint8_t *)&sip->sn_giaddr;
1062 (void) snprintf(buf, BUFLEN, "%d.%d.%d.%d",
1063 val[0], val[1], val[2], val[3]);
1064 bsetprops(BP_ROUTER_IP, buf);
1067 if (sip->sn_netmask != 0) {
1068 val = (uint8_t *)&sip->sn_netmask;
1069 (void) snprintf(buf, BUFLEN, "%d.%d.%d.%d",
1070 val[0], val[1], val[2], val[3]);
1071 bsetprops(BP_SUBNET_MASK, buf);
1074 if (sip->sn_mactype != 4 || sip->sn_maclen != 6) {
1075 bop_printf(NULL, "unsupported mac type %d, mac len %d\n",
1076 sip->sn_mactype, sip->sn_maclen);
1077 } else {
1078 val = sip->sn_macaddr;
1079 (void) snprintf(buf, BUFLEN, "%x:%x:%x:%x:%x:%x",
1080 val[0], val[1], val[2], val[3], val[4], val[5]);
1081 bsetprops(BP_BOOT_MAC, buf);
1085 #endif /* __xpv */
1087 static void
1088 build_panic_cmdline(const char *cmd, int cmdlen)
1090 int proplen;
1091 size_t arglen;
1093 arglen = sizeof (fastreboot_onpanic_args);
1095 * If we allready have fastreboot-onpanic set to zero,
1096 * don't add them again.
1098 if ((proplen = do_bsys_getproplen(NULL, FASTREBOOT_ONPANIC)) > 0 &&
1099 proplen <= sizeof (fastreboot_onpanic_cmdline)) {
1100 (void) do_bsys_getprop(NULL, FASTREBOOT_ONPANIC,
1101 fastreboot_onpanic_cmdline);
1102 if (FASTREBOOT_ONPANIC_NOTSET(fastreboot_onpanic_cmdline))
1103 arglen = 1;
1107 * construct fastreboot_onpanic_cmdline
1109 if (cmdlen + arglen > sizeof (fastreboot_onpanic_cmdline)) {
1110 DBG_MSG("Command line too long: clearing "
1111 FASTREBOOT_ONPANIC "\n");
1112 fastreboot_onpanic = 0;
1113 } else {
1114 bcopy(cmd, fastreboot_onpanic_cmdline, cmdlen);
1115 if (arglen != 1)
1116 bcopy(fastreboot_onpanic_args,
1117 fastreboot_onpanic_cmdline + cmdlen, arglen);
1118 else
1119 fastreboot_onpanic_cmdline[cmdlen] = 0;
1124 #ifndef __xpv
1126 * Construct boot command line for Fast Reboot. The saved_cmdline
1127 * is also reported by "eeprom bootcmd".
1129 static void
1130 build_fastboot_cmdline(struct xboot_info *xbp)
1132 saved_cmdline_len = strlen(xbp->bi_cmdline) + 1;
1133 if (saved_cmdline_len > FASTBOOT_SAVED_CMDLINE_LEN) {
1134 DBG(saved_cmdline_len);
1135 DBG_MSG("Command line too long: clearing fastreboot_capable\n");
1136 fastreboot_capable = 0;
1137 } else {
1138 bcopy((void *)(xbp->bi_cmdline), (void *)saved_cmdline,
1139 saved_cmdline_len);
1140 saved_cmdline[saved_cmdline_len - 1] = '\0';
1141 build_panic_cmdline(saved_cmdline, saved_cmdline_len - 1);
1146 * Save memory layout, disk drive information, unix and boot archive sizes for
1147 * Fast Reboot.
1149 static void
1150 save_boot_info(struct xboot_info *xbi)
1152 multiboot_info_t *mbi = xbi->bi_mb_info;
1153 struct boot_modules *modp;
1154 int i;
1156 bcopy(mbi, &saved_mbi, sizeof (multiboot_info_t));
1157 if (mbi->mmap_length > sizeof (saved_mmap)) {
1158 DBG_MSG("mbi->mmap_length too big: clearing "
1159 "fastreboot_capable\n");
1160 fastreboot_capable = 0;
1161 } else {
1162 bcopy((void *)(uintptr_t)mbi->mmap_addr, (void *)saved_mmap,
1163 mbi->mmap_length);
1166 if ((mbi->flags & MB_INFO_DRIVE_INFO) != 0) {
1167 if (mbi->drives_length > sizeof (saved_drives)) {
1168 DBG(mbi->drives_length);
1169 DBG_MSG("mbi->drives_length too big: clearing "
1170 "fastreboot_capable\n");
1171 fastreboot_capable = 0;
1172 } else {
1173 bcopy((void *)(uintptr_t)mbi->drives_addr,
1174 (void *)saved_drives, mbi->drives_length);
1176 } else {
1177 saved_mbi.drives_length = 0;
1178 saved_mbi.drives_addr = NULL;
1182 * Current file sizes. Used by fastboot.c to figure out how much
1183 * memory to reserve for panic reboot.
1184 * Use the module list from the dboot-constructed xboot_info
1185 * instead of the list referenced by the multiboot structure
1186 * because that structure may not be addressable now.
1188 saved_file_size[FASTBOOT_NAME_UNIX] = FOUR_MEG - PAGESIZE;
1189 for (i = 0, modp = (struct boot_modules *)(uintptr_t)xbi->bi_modules;
1190 i < xbi->bi_module_cnt; i++, modp++) {
1191 saved_file_size[FASTBOOT_NAME_BOOTARCHIVE] += modp->bm_size;
1194 #endif /* __xpv */
1197 * Import boot environment module variables as properties, applying
1198 * blacklist filter for variables we know we will not use.
1200 * Since the environment can be relatively large, containing many variables
1201 * used only for boot loader purposes, we will use a blacklist based filter.
1202 * To keep the blacklist from growing too large, we use prefix based filtering.
1203 * This is possible because in many cases, the loader variable names are
1204 * using a structured layout.
1206 * We will not overwrite already set properties.
1208 static struct bop_blacklist {
1209 const char *bl_name;
1210 int bl_name_len;
1211 } bop_prop_blacklist[] = {
1212 { "ISADIR", sizeof ("ISADIR") },
1213 { "acpi", sizeof ("acpi") },
1214 { "autoboot_delay", sizeof ("autoboot_delay") },
1215 { "autoboot_delay", sizeof ("autoboot_delay") },
1216 { "beansi_", sizeof ("beansi_") },
1217 { "beastie", sizeof ("beastie") },
1218 { "bemenu", sizeof ("bemenu") },
1219 { "boot.", sizeof ("boot.") },
1220 { "bootenv", sizeof ("bootenv") },
1221 { "currdev", sizeof ("currdev") },
1222 { "dhcp.", sizeof ("dhcp.") },
1223 { "interpret", sizeof ("interpret") },
1224 { "kernel", sizeof ("kernel") },
1225 { "loaddev", sizeof ("loaddev") },
1226 { "loader_", sizeof ("loader_") },
1227 { "module_path", sizeof ("module_path") },
1228 { "nfs.", sizeof ("nfs.") },
1229 { "pcibios", sizeof ("pcibios") },
1230 { "prompt", sizeof ("prompt") },
1231 { "smbios", sizeof ("smbios") },
1232 { "tem", sizeof ("tem") },
1233 { "twiddle_divisor", sizeof ("twiddle_divisor") },
1234 { "zfs_be", sizeof ("zfs_be") },
1238 * Match the name against prefixes in above blacklist. If the match was
1239 * found, this name is blacklisted.
1241 static boolean_t
1242 name_is_blacklisted(const char *name)
1244 int i, n;
1246 n = sizeof (bop_prop_blacklist) / sizeof (bop_prop_blacklist[0]);
1247 for (i = 0; i < n; i++) {
1248 if (strncmp(bop_prop_blacklist[i].bl_name, name,
1249 bop_prop_blacklist[i].bl_name_len - 1) == 0) {
1250 return (B_TRUE);
1253 return (B_FALSE);
1256 static void
1257 process_boot_environment(struct boot_modules *benv)
1259 char *env, *ptr, *name, *value;
1260 uint32_t size, name_len, value_len;
1262 if (benv == NULL || benv->bm_type != BMT_ENV)
1263 return;
1264 ptr = env = benv->bm_addr;
1265 size = benv->bm_size;
1266 do {
1267 name = ptr;
1268 /* find '=' */
1269 while (*ptr != '=') {
1270 ptr++;
1271 if (ptr > env + size) /* Something is very wrong. */
1272 return;
1274 name_len = ptr - name;
1275 if (sizeof (buffer) <= name_len)
1276 continue;
1278 (void) strncpy(buffer, name, sizeof (buffer));
1279 buffer[name_len] = '\0';
1280 name = buffer;
1282 value_len = 0;
1283 value = ++ptr;
1284 while ((uintptr_t)ptr - (uintptr_t)env < size) {
1285 if (*ptr == '\0') {
1286 ptr++;
1287 value_len = (uintptr_t)ptr - (uintptr_t)env;
1288 break;
1290 ptr++;
1293 /* Did we reach the end of the module? */
1294 if (value_len == 0)
1295 return;
1297 if (*value == '\0')
1298 continue;
1300 /* Is this property already set? */
1301 if (do_bsys_getproplen(NULL, name) >= 0)
1302 continue;
1304 if (name_is_blacklisted(name) == B_TRUE)
1305 continue;
1307 /* Create new property. */
1308 bsetprops(name, value);
1310 /* Avoid reading past the module end. */
1311 if (size <= (uintptr_t)ptr - (uintptr_t)env)
1312 return;
1313 } while (*ptr != '\0');
1317 * 1st pass at building the table of boot properties. This includes:
1318 * - values set on the command line: -B a=x,b=y,c=z ....
1319 * - known values we just compute (ie. from xbp)
1320 * - values from /boot/solaris/bootenv.rc (ie. eeprom(1m) values)
1322 * the grub command line looked like:
1323 * kernel boot-file [-B prop=value[,prop=value]...] [boot-args]
1325 * whoami is the same as boot-file
1327 static void
1328 build_boot_properties(struct xboot_info *xbp)
1330 char *name;
1331 int name_len;
1332 char *value;
1333 int value_len;
1334 struct boot_modules *bm, *rdbm, *benv = NULL;
1335 char *propbuf;
1336 int quoted = 0;
1337 int boot_arg_len;
1338 uint_t i, midx;
1339 char modid[32];
1340 #ifndef __xpv
1341 static int stdout_val = 0;
1342 uchar_t boot_device;
1343 char str[3];
1344 #endif
1347 * These have to be done first, so that kobj_mount_root() works
1349 DBG_MSG("Building boot properties\n");
1350 propbuf = do_bsys_alloc(NULL, NULL, MMU_PAGESIZE, 0);
1351 DBG((uintptr_t)propbuf);
1352 if (xbp->bi_module_cnt > 0) {
1353 bm = xbp->bi_modules;
1354 rdbm = NULL;
1355 for (midx = i = 0; i < xbp->bi_module_cnt; i++) {
1356 if (bm[i].bm_type == BMT_ROOTFS) {
1357 rdbm = &bm[i];
1358 continue;
1360 if (bm[i].bm_type == BMT_HASH || bm[i].bm_name == NULL)
1361 continue;
1363 if (bm[i].bm_type == BMT_ENV) {
1364 if (benv == NULL)
1365 benv = &bm[i];
1366 else
1367 continue;
1370 (void) snprintf(modid, sizeof (modid),
1371 "module-name-%u", midx);
1372 bsetprops(modid, (char *)bm[i].bm_name);
1373 (void) snprintf(modid, sizeof (modid),
1374 "module-addr-%u", midx);
1375 bsetprop64(modid, (uint64_t)(uintptr_t)bm[i].bm_addr);
1376 (void) snprintf(modid, sizeof (modid),
1377 "module-size-%u", midx);
1378 bsetprop64(modid, (uint64_t)bm[i].bm_size);
1379 ++midx;
1381 if (rdbm != NULL) {
1382 bsetprop64("ramdisk_start",
1383 (uint64_t)(uintptr_t)rdbm->bm_addr);
1384 bsetprop64("ramdisk_end",
1385 (uint64_t)(uintptr_t)rdbm->bm_addr + rdbm->bm_size);
1390 * If there are any boot time modules or hashes present, then disable
1391 * fast reboot.
1393 if (xbp->bi_module_cnt > 1) {
1394 fastreboot_disable(FBNS_BOOTMOD);
1397 #ifndef __xpv
1399 * Disable fast reboot if we're using the Multiboot 2 boot protocol,
1400 * since we don't currently support MB2 info and module relocation.
1401 * Note that fast reboot will have already been disabled if multiple
1402 * modules are present, since the current implementation assumes that
1403 * we only have a single module, the boot_archive.
1405 if (xbp->bi_mb_version != 1) {
1406 fastreboot_disable(FBNS_MULTIBOOT2);
1408 #endif
1410 DBG_MSG("Parsing command line for boot properties\n");
1411 value = xbp->bi_cmdline;
1414 * allocate memory to collect boot_args into
1416 boot_arg_len = strlen(xbp->bi_cmdline) + 1;
1417 boot_args = do_bsys_alloc(NULL, NULL, boot_arg_len, MMU_PAGESIZE);
1418 boot_args[0] = 0;
1419 boot_arg_len = 0;
1421 #ifdef __xpv
1423 * Xen puts a lot of device information in front of the kernel name
1424 * let's grab them and make them boot properties. The first
1425 * string w/o an "=" in it will be the boot-file property.
1427 (void) strcpy(namebuf, "xpv-");
1428 for (;;) {
1430 * get to next property
1432 while (ISSPACE(*value))
1433 ++value;
1434 name = value;
1436 * look for an "="
1438 while (*value && !ISSPACE(*value) && *value != '=') {
1439 value++;
1441 if (*value != '=') { /* no "=" in the property */
1442 value = name;
1443 break;
1445 name_len = value - name;
1446 value_len = 0;
1448 * skip over the "="
1450 value++;
1451 while (value[value_len] && !ISSPACE(value[value_len])) {
1452 ++value_len;
1455 * build property name with "xpv-" prefix
1457 if (name_len + 4 > 32) { /* skip if name too long */
1458 value += value_len;
1459 continue;
1461 bcopy(name, &namebuf[4], name_len);
1462 name_len += 4;
1463 namebuf[name_len] = 0;
1464 bcopy(value, propbuf, value_len);
1465 propbuf[value_len] = 0;
1466 bsetprops(namebuf, propbuf);
1469 * xpv-root is set to the logical disk name of the xen
1470 * VBD when booting from a disk-based filesystem.
1472 if (strcmp(namebuf, "xpv-root") == 0)
1473 xen_vbdroot_props(propbuf);
1475 * While we're here, if we have a "xpv-nfsroot" property
1476 * then we need to set "fstype" to "nfs" so we mount
1477 * our root from the nfs server. Also parse the xpv-nfsroot
1478 * property to create the properties that nfs_mountroot will
1479 * need to find the root and mount it.
1481 if (strcmp(namebuf, "xpv-nfsroot") == 0)
1482 xen_nfsroot_props(propbuf);
1484 if (strcmp(namebuf, "xpv-ip") == 0)
1485 xen_ip_props(propbuf);
1486 value += value_len;
1488 #endif
1490 while (ISSPACE(*value))
1491 ++value;
1493 * value now points at the boot-file
1495 value_len = 0;
1496 while (value[value_len] && !ISSPACE(value[value_len]))
1497 ++value_len;
1498 if (value_len > 0) {
1499 whoami = propbuf;
1500 bcopy(value, whoami, value_len);
1501 whoami[value_len] = 0;
1502 bsetprops("boot-file", whoami);
1504 * strip leading path stuff from whoami, so running from
1505 * PXE/miniroot makes sense.
1507 if (strstr(whoami, "/platform/") != NULL)
1508 whoami = strstr(whoami, "/platform/");
1509 bsetprops("whoami", whoami);
1513 * Values forcibly set boot properties on the command line via -B.
1514 * Allow use of quotes in values. Other stuff goes on kernel
1515 * command line.
1517 name = value + value_len;
1518 while (*name != 0) {
1520 * anything not " -B" is copied to the command line
1522 if (!ISSPACE(name[0]) || name[1] != '-' || name[2] != 'B') {
1523 boot_args[boot_arg_len++] = *name;
1524 boot_args[boot_arg_len] = 0;
1525 ++name;
1526 continue;
1530 * skip the " -B" and following white space
1532 name += 3;
1533 while (ISSPACE(*name))
1534 ++name;
1535 while (*name && !ISSPACE(*name)) {
1536 value = strstr(name, "=");
1537 if (value == NULL)
1538 break;
1539 name_len = value - name;
1540 ++value;
1541 value_len = 0;
1542 quoted = 0;
1543 for (; ; ++value_len) {
1544 if (!value[value_len])
1545 break;
1548 * is this value quoted?
1550 if (value_len == 0 &&
1551 (value[0] == '\'' || value[0] == '"')) {
1552 quoted = value[0];
1553 ++value_len;
1557 * In the quote accept any character,
1558 * but look for ending quote.
1560 if (quoted) {
1561 if (value[value_len] == quoted)
1562 quoted = 0;
1563 continue;
1567 * a comma or white space ends the value
1569 if (value[value_len] == ',' ||
1570 ISSPACE(value[value_len]))
1571 break;
1574 if (value_len == 0) {
1575 bsetprop(name, name_len, "true", 5);
1576 } else {
1577 char *v = value;
1578 int l = value_len;
1579 if (v[0] == v[l - 1] &&
1580 (v[0] == '\'' || v[0] == '"')) {
1581 ++v;
1582 l -= 2;
1584 bcopy(v, propbuf, l);
1585 propbuf[l] = '\0';
1586 bsetprop(name, name_len, propbuf,
1587 l + 1);
1589 name = value + value_len;
1590 while (*name == ',')
1591 ++name;
1596 * set boot-args property
1597 * 1275 name is bootargs, so set
1598 * that too
1600 bsetprops("boot-args", boot_args);
1601 bsetprops("bootargs", boot_args);
1603 process_boot_environment(benv);
1605 #ifndef __xpv
1607 * Build boot command line for Fast Reboot
1609 build_fastboot_cmdline(xbp);
1611 if (xbp->bi_mb_version == 1) {
1612 multiboot_info_t *mbi = xbp->bi_mb_info;
1613 int netboot;
1614 struct sol_netinfo *sip;
1617 * set the BIOS boot device from GRUB
1619 netboot = 0;
1622 * Save various boot information for Fast Reboot
1624 save_boot_info(xbp);
1626 if (mbi != NULL && mbi->flags & MB_INFO_BOOTDEV) {
1627 boot_device = mbi->boot_device >> 24;
1628 if (boot_device == 0x20)
1629 netboot++;
1630 str[0] = (boot_device >> 4) + '0';
1631 str[1] = (boot_device & 0xf) + '0';
1632 str[2] = 0;
1633 bsetprops("bios-boot-device", str);
1634 } else {
1635 netboot = 1;
1639 * In the netboot case, drives_info is overloaded with the
1640 * dhcp ack. This is not multiboot compliant and requires
1641 * special pxegrub!
1643 if (netboot && mbi->drives_length != 0) {
1644 sip = (struct sol_netinfo *)(uintptr_t)mbi->drives_addr;
1645 if (sip->sn_infotype == SN_TYPE_BOOTP)
1646 bsetprop("bootp-response",
1647 sizeof ("bootp-response"),
1648 (void *)(uintptr_t)mbi->drives_addr,
1649 mbi->drives_length);
1650 else if (sip->sn_infotype == SN_TYPE_RARP)
1651 setup_rarp_props(sip);
1653 } else {
1654 multiboot2_info_header_t *mbi = xbp->bi_mb_info;
1655 multiboot_tag_bootdev_t *bootdev = NULL;
1656 multiboot_tag_network_t *netdev = NULL;
1658 if (mbi != NULL) {
1659 bootdev = dboot_multiboot2_find_tag(mbi,
1660 MULTIBOOT_TAG_TYPE_BOOTDEV);
1661 netdev = dboot_multiboot2_find_tag(mbi,
1662 MULTIBOOT_TAG_TYPE_NETWORK);
1664 if (bootdev != NULL) {
1665 DBG(bootdev->mb_biosdev);
1666 boot_device = bootdev->mb_biosdev;
1667 str[0] = (boot_device >> 4) + '0';
1668 str[1] = (boot_device & 0xf) + '0';
1669 str[2] = 0;
1670 bsetprops("bios-boot-device", str);
1672 if (netdev != NULL) {
1673 bsetprop("bootp-response", sizeof ("bootp-response"),
1674 (void *)(uintptr_t)netdev->mb_dhcpack,
1675 netdev->mb_size -
1676 sizeof (multiboot_tag_network_t));
1680 bsetprop("stdout", strlen("stdout"),
1681 &stdout_val, sizeof (stdout_val));
1682 #endif /* __xpv */
1685 * more conjured up values for made up things....
1687 #if defined(__xpv)
1688 bsetprops("mfg-name", "i86xpv");
1689 bsetprops("impl-arch-name", "i86xpv");
1690 #else
1691 bsetprops("mfg-name", "i86pc");
1692 bsetprops("impl-arch-name", "i86pc");
1693 #endif
1696 * Build firmware-provided system properties
1698 build_firmware_properties(xbp);
1701 * XXPV
1703 * Find out what these are:
1704 * - cpuid_feature_ecx_include
1705 * - cpuid_feature_ecx_exclude
1706 * - cpuid_feature_edx_include
1707 * - cpuid_feature_edx_exclude
1709 * Find out what these are in multiboot:
1710 * - netdev-path
1711 * - fstype
1715 #ifdef __xpv
1717 * Under the Hypervisor, memory usable for DMA may be scarce. One
1718 * very likely large pool of DMA friendly memory is occupied by
1719 * the boot_archive, as it was loaded by grub into low MFNs.
1721 * Here we free up that memory by copying the boot archive to what are
1722 * likely higher MFN pages and then swapping the mfn/pfn mappings.
1724 #define PFN_2GIG 0x80000
1725 static void
1726 relocate_boot_archive(struct xboot_info *xbp)
1728 mfn_t max_mfn = HYPERVISOR_memory_op(XENMEM_maximum_ram_page, NULL);
1729 struct boot_modules *bm = xbp->bi_modules;
1730 uintptr_t va;
1731 pfn_t va_pfn;
1732 mfn_t va_mfn;
1733 caddr_t copy;
1734 pfn_t copy_pfn;
1735 mfn_t copy_mfn;
1736 size_t len;
1737 int slop;
1738 int total = 0;
1739 int relocated = 0;
1740 int mmu_update_return;
1741 mmu_update_t t[2];
1742 x86pte_t pte;
1745 * If all MFN's are below 2Gig, don't bother doing this.
1747 if (max_mfn < PFN_2GIG)
1748 return;
1749 if (xbp->bi_module_cnt < 1) {
1750 DBG_MSG("no boot_archive!");
1751 return;
1754 DBG_MSG("moving boot_archive to high MFN memory\n");
1755 va = (uintptr_t)bm->bm_addr;
1756 len = bm->bm_size;
1757 slop = va & MMU_PAGEOFFSET;
1758 if (slop) {
1759 va += MMU_PAGESIZE - slop;
1760 len -= MMU_PAGESIZE - slop;
1762 len = P2ALIGN(len, MMU_PAGESIZE);
1765 * Go through all boot_archive pages, swapping any low MFN pages
1766 * with memory at next_phys.
1768 while (len != 0) {
1769 ++total;
1770 va_pfn = mmu_btop(va - ONE_GIG);
1771 va_mfn = mfn_list[va_pfn];
1772 if (mfn_list[va_pfn] < PFN_2GIG) {
1773 copy = kbm_remap_window(next_phys, 1);
1774 bcopy((void *)va, copy, MMU_PAGESIZE);
1775 copy_pfn = mmu_btop(next_phys);
1776 copy_mfn = mfn_list[copy_pfn];
1778 pte = mfn_to_ma(copy_mfn) | PT_NOCONSIST | PT_VALID;
1779 if (HYPERVISOR_update_va_mapping(va, pte,
1780 UVMF_INVLPG | UVMF_LOCAL))
1781 bop_panic("relocate_boot_archive(): "
1782 "HYPERVISOR_update_va_mapping() failed");
1784 mfn_list[va_pfn] = copy_mfn;
1785 mfn_list[copy_pfn] = va_mfn;
1787 t[0].ptr = mfn_to_ma(copy_mfn) | MMU_MACHPHYS_UPDATE;
1788 t[0].val = va_pfn;
1789 t[1].ptr = mfn_to_ma(va_mfn) | MMU_MACHPHYS_UPDATE;
1790 t[1].val = copy_pfn;
1791 if (HYPERVISOR_mmu_update(t, 2, &mmu_update_return,
1792 DOMID_SELF) != 0 || mmu_update_return != 2)
1793 bop_panic("relocate_boot_archive(): "
1794 "HYPERVISOR_mmu_update() failed");
1796 next_phys += MMU_PAGESIZE;
1797 ++relocated;
1799 len -= MMU_PAGESIZE;
1800 va += MMU_PAGESIZE;
1802 DBG_MSG("Relocated pages:\n");
1803 DBG(relocated);
1804 DBG_MSG("Out of total pages:\n");
1805 DBG(total);
1807 #endif /* __xpv */
1809 #if !defined(__xpv)
1811 * simple description of a stack frame (args are 32 bit only currently)
1813 typedef struct bop_frame {
1814 struct bop_frame *old_frame;
1815 pc_t retaddr;
1816 long arg[1];
1817 } bop_frame_t;
1819 void
1820 bop_traceback(bop_frame_t *frame)
1822 pc_t pc;
1823 int cnt;
1824 char *ksym;
1825 ulong_t off;
1827 bop_printf(NULL, "Stack traceback:\n");
1828 for (cnt = 0; cnt < 30; ++cnt) { /* up to 30 frames */
1829 pc = frame->retaddr;
1830 if (pc == 0)
1831 break;
1832 ksym = kobj_getsymname(pc, &off);
1833 if (ksym)
1834 bop_printf(NULL, " %s+%lx", ksym, off);
1835 else
1836 bop_printf(NULL, " 0x%lx", pc);
1838 frame = frame->old_frame;
1839 if (frame == 0) {
1840 bop_printf(NULL, "\n");
1841 break;
1843 bop_printf(NULL, "\n");
1847 struct trapframe {
1848 ulong_t error_code; /* optional */
1849 ulong_t inst_ptr;
1850 ulong_t code_seg;
1851 ulong_t flags_reg;
1852 ulong_t stk_ptr;
1853 ulong_t stk_seg;
1856 void
1857 bop_trap(ulong_t *tfp)
1859 struct trapframe *tf = (struct trapframe *)tfp;
1860 bop_frame_t fakeframe;
1861 static int depth = 0;
1864 * Check for an infinite loop of traps.
1866 if (++depth > 2)
1867 bop_panic("Nested trap");
1869 bop_printf(NULL, "Unexpected trap\n");
1872 * adjust the tf for optional error_code by detecting the code selector
1874 if (tf->code_seg != B64CODE_SEL)
1875 tf = (struct trapframe *)(tfp - 1);
1876 else
1877 bop_printf(NULL, "error code 0x%lx\n",
1878 tf->error_code & 0xffffffff);
1880 bop_printf(NULL, "instruction pointer 0x%lx\n", tf->inst_ptr);
1881 bop_printf(NULL, "code segment 0x%lx\n", tf->code_seg & 0xffff);
1882 bop_printf(NULL, "flags register 0x%lx\n", tf->flags_reg);
1883 bop_printf(NULL, "return %%rsp 0x%lx\n", tf->stk_ptr);
1884 bop_printf(NULL, "return %%ss 0x%lx\n", tf->stk_seg & 0xffff);
1886 /* grab %[er]bp pushed by our code from the stack */
1887 fakeframe.old_frame = (bop_frame_t *)*(tfp - 3);
1888 fakeframe.retaddr = (pc_t)tf->inst_ptr;
1889 bop_printf(NULL, "Attempting stack backtrace:\n");
1890 bop_traceback(&fakeframe);
1891 bop_panic("unexpected trap in early boot");
1894 extern void bop_trap_handler(void);
1896 static gate_desc_t *bop_idt;
1898 static desctbr_t bop_idt_info;
1901 * Install a temporary IDT that lets us catch errors in the boot time code.
1902 * We shouldn't get any faults at all while this is installed, so we'll
1903 * just generate a traceback and exit.
1905 static void
1906 bop_idt_init(void)
1908 int t;
1910 bop_idt = (gate_desc_t *)
1911 do_bsys_alloc(NULL, NULL, MMU_PAGESIZE, MMU_PAGESIZE);
1912 bzero(bop_idt, MMU_PAGESIZE);
1913 for (t = 0; t < NIDT; ++t) {
1915 * Note that since boot runs without a TSS, the
1916 * double fault handler cannot use an alternate stack (64-bit).
1918 set_gatesegd(&bop_idt[t], &bop_trap_handler, B64CODE_SEL,
1919 SDT_SYSIGT, TRP_KPL, 0);
1921 bop_idt_info.dtr_limit = (NIDT * sizeof (gate_desc_t)) - 1;
1922 bop_idt_info.dtr_base = (uintptr_t)bop_idt;
1923 wr_idtr(&bop_idt_info);
1925 #endif /* !defined(__xpv) */
1928 * This is where we enter the kernel. It dummies up the boot_ops and
1929 * boot_syscalls vectors and jumps off to _kobj_boot()
1931 void
1932 _start(struct xboot_info *xbp)
1934 bootops_t *bops = &bootop;
1935 extern void _kobj_boot();
1938 * 1st off - initialize the console for any error messages
1940 xbootp = xbp;
1941 #ifdef __xpv
1942 HYPERVISOR_shared_info = (void *)xbp->bi_shared_info;
1943 xen_info = xbp->bi_xen_start_info;
1944 #endif
1946 #ifndef __xpv
1947 if (*((uint32_t *)(FASTBOOT_SWTCH_PA + FASTBOOT_STACK_OFFSET)) ==
1948 FASTBOOT_MAGIC) {
1949 post_fastreboot = 1;
1950 *((uint32_t *)(FASTBOOT_SWTCH_PA + FASTBOOT_STACK_OFFSET)) = 0;
1952 #endif
1954 bcons_init(xbp);
1955 have_console = 1;
1958 * enable debugging
1960 if (find_boot_prop("kbm_debug") != NULL)
1961 kbm_debug = 1;
1963 DBG_MSG("\n\n*** Entered Solaris in _start() cmdline is: ");
1964 DBG_MSG((char *)xbp->bi_cmdline);
1965 DBG_MSG("\n\n\n");
1968 * physavail is no longer used by startup
1970 bm.physinstalled = xbp->bi_phys_install;
1971 bm.pcimem = xbp->bi_pcimem;
1972 bm.rsvdmem = xbp->bi_rsvdmem;
1973 bm.physavail = NULL;
1976 * initialize the boot time allocator
1978 next_phys = xbp->bi_next_paddr;
1979 DBG(next_phys);
1980 next_virt = (uintptr_t)xbp->bi_next_vaddr;
1981 DBG(next_virt);
1982 DBG_MSG("Initializing boot time memory management...");
1983 #ifdef __xpv
1985 xen_platform_parameters_t p;
1987 /* This call shouldn't fail, dboot already did it once. */
1988 (void) HYPERVISOR_xen_version(XENVER_platform_parameters, &p);
1989 mfn_to_pfn_mapping = (pfn_t *)(xen_virt_start = p.virt_start);
1990 DBG(xen_virt_start);
1992 #endif
1993 kbm_init(xbp);
1994 DBG_MSG("done\n");
1997 * Fill in the bootops vector
1999 bops->bsys_version = BO_VERSION;
2000 bops->boot_mem = &bm;
2001 bops->bsys_alloc = do_bsys_alloc;
2002 bops->bsys_free = do_bsys_free;
2003 bops->bsys_getproplen = do_bsys_getproplen;
2004 bops->bsys_getprop = do_bsys_getprop;
2005 bops->bsys_nextprop = do_bsys_nextprop;
2006 bops->bsys_printf = bop_printf;
2007 bops->bsys_doint = do_bsys_doint;
2010 * BOP_EALLOC() is no longer needed
2012 bops->bsys_ealloc = do_bsys_ealloc;
2014 #ifdef __xpv
2016 * On domain 0 we need to free up some physical memory that is
2017 * usable for DMA. Since GRUB loaded the boot_archive, it is
2018 * sitting in low MFN memory. We'll relocated the boot archive
2019 * pages to high PFN memory.
2021 if (DOMAIN_IS_INITDOMAIN(xen_info))
2022 relocate_boot_archive(xbp);
2023 #endif
2025 #ifndef __xpv
2027 * Install an IDT to catch early pagefaults (shouldn't have any).
2028 * Also needed for kmdb.
2030 bop_idt_init();
2031 #endif
2034 * Start building the boot properties from the command line
2036 DBG_MSG("Initializing boot properties:\n");
2037 build_boot_properties(xbp);
2039 if (find_boot_prop("prom_debug") || kbm_debug) {
2040 char *value;
2042 value = do_bsys_alloc(NULL, NULL, MMU_PAGESIZE, MMU_PAGESIZE);
2043 boot_prop_display(value);
2047 * jump into krtld...
2049 _kobj_boot(&bop_sysp, NULL, bops, NULL);
2053 /*ARGSUSED*/
2054 static caddr_t
2055 no_more_alloc(bootops_t *bop, caddr_t virthint, size_t size, int align)
2057 panic("Attempt to bsys_alloc() too late\n");
2058 return (NULL);
2061 /*ARGSUSED*/
2062 static void
2063 no_more_free(bootops_t *bop, caddr_t virt, size_t size)
2065 panic("Attempt to bsys_free() too late\n");
2068 void
2069 bop_no_more_mem(void)
2071 DBG(total_bop_alloc_scratch);
2072 DBG(total_bop_alloc_kernel);
2073 bootops->bsys_alloc = no_more_alloc;
2074 bootops->bsys_free = no_more_free;
2079 * Set ACPI firmware properties
2082 static caddr_t
2083 vmap_phys(size_t length, paddr_t pa)
2085 paddr_t start, end;
2086 caddr_t va;
2087 size_t len, page;
2089 #ifdef __xpv
2090 pa = pfn_to_pa(xen_assign_pfn(mmu_btop(pa))) | (pa & MMU_PAGEOFFSET);
2091 #endif
2092 start = P2ALIGN(pa, MMU_PAGESIZE);
2093 end = P2ROUNDUP(pa + length, MMU_PAGESIZE);
2094 len = end - start;
2095 va = (caddr_t)alloc_vaddr(len, MMU_PAGESIZE);
2096 for (page = 0; page < len; page += MMU_PAGESIZE)
2097 kbm_map((uintptr_t)va + page, start + page, 0, 0);
2098 return (va + (pa & MMU_PAGEOFFSET));
2101 static uint8_t
2102 checksum_table(uint8_t *tp, size_t len)
2104 uint8_t sum = 0;
2106 while (len-- > 0)
2107 sum += *tp++;
2109 return (sum);
2112 static int
2113 valid_rsdp(ACPI_TABLE_RSDP *rp)
2116 /* validate the V1.x checksum */
2117 if (checksum_table((uint8_t *)rp, ACPI_RSDP_CHECKSUM_LENGTH) != 0)
2118 return (0);
2120 /* If pre-ACPI 2.0, this is a valid RSDP */
2121 if (rp->Revision < 2)
2122 return (1);
2124 /* validate the V2.x checksum */
2125 if (checksum_table((uint8_t *)rp, ACPI_RSDP_XCHECKSUM_LENGTH) != 0)
2126 return (0);
2128 return (1);
2132 * Scan memory range for an RSDP;
2133 * see ACPI 3.0 Spec, 5.2.5.1
2135 static ACPI_TABLE_RSDP *
2136 scan_rsdp(paddr_t start, paddr_t end)
2138 ssize_t len = end - start;
2139 caddr_t ptr;
2141 ptr = vmap_phys(len, start);
2142 while (len > 0) {
2143 if (strncmp(ptr, ACPI_SIG_RSDP, strlen(ACPI_SIG_RSDP)) == 0 &&
2144 valid_rsdp((ACPI_TABLE_RSDP *)ptr))
2145 return ((ACPI_TABLE_RSDP *)ptr);
2147 ptr += ACPI_RSDP_SCAN_STEP;
2148 len -= ACPI_RSDP_SCAN_STEP;
2151 return (NULL);
2155 * Refer to ACPI 3.0 Spec, section 5.2.5.1 to understand this function
2157 static ACPI_TABLE_RSDP *
2158 find_rsdp()
2160 ACPI_TABLE_RSDP *rsdp;
2161 uint64_t rsdp_val = 0;
2162 uint16_t *ebda_seg;
2163 paddr_t ebda_addr;
2165 /* check for "acpi-root-tab" property */
2166 if (do_bsys_getproplen(NULL, "acpi-root-tab") == sizeof (uint64_t)) {
2167 (void) do_bsys_getprop(NULL, "acpi-root-tab", &rsdp_val);
2168 if (rsdp_val != 0) {
2169 rsdp = scan_rsdp(rsdp_val, rsdp_val + sizeof (*rsdp));
2170 if (rsdp != NULL) {
2171 if (kbm_debug) {
2172 bop_printf(NULL,
2173 "Using RSDP from bootloader: "
2174 "0x%p\n", (void *)rsdp);
2176 return (rsdp);
2182 * Get the EBDA segment and scan the first 1K
2184 ebda_seg = (uint16_t *)vmap_phys(sizeof (uint16_t),
2185 ACPI_EBDA_PTR_LOCATION);
2186 ebda_addr = *ebda_seg << 4;
2187 rsdp = scan_rsdp(ebda_addr, ebda_addr + ACPI_EBDA_WINDOW_SIZE);
2188 if (rsdp == NULL)
2189 /* if EBDA doesn't contain RSDP, look in BIOS memory */
2190 rsdp = scan_rsdp(ACPI_HI_RSDP_WINDOW_BASE,
2191 ACPI_HI_RSDP_WINDOW_BASE + ACPI_HI_RSDP_WINDOW_SIZE);
2192 return (rsdp);
2195 static ACPI_TABLE_HEADER *
2196 map_fw_table(paddr_t table_addr)
2198 ACPI_TABLE_HEADER *tp;
2199 size_t len = MAX(sizeof (*tp), MMU_PAGESIZE);
2202 * Map at least a page; if the table is larger than this, remap it
2204 tp = (ACPI_TABLE_HEADER *)vmap_phys(len, table_addr);
2205 if (tp->Length > len)
2206 tp = (ACPI_TABLE_HEADER *)vmap_phys(tp->Length, table_addr);
2207 return (tp);
2210 static ACPI_TABLE_HEADER *
2211 find_fw_table(char *signature)
2213 static int revision = 0;
2214 static ACPI_TABLE_XSDT *xsdt;
2215 static int len;
2216 paddr_t xsdt_addr;
2217 ACPI_TABLE_RSDP *rsdp;
2218 ACPI_TABLE_HEADER *tp;
2219 paddr_t table_addr;
2220 int n;
2222 if (strlen(signature) != ACPI_NAME_SIZE)
2223 return (NULL);
2226 * Reading the ACPI 3.0 Spec, section 5.2.5.3 will help
2227 * understand this code. If we haven't already found the RSDT/XSDT,
2228 * revision will be 0. Find the RSDP and check the revision
2229 * to find out whether to use the RSDT or XSDT. If revision is
2230 * 0 or 1, use the RSDT and set internal revision to 1; if it is 2,
2231 * use the XSDT. If the XSDT address is 0, though, fall back to
2232 * revision 1 and use the RSDT.
2234 if (revision == 0) {
2235 if ((rsdp = find_rsdp()) != NULL) {
2236 revision = rsdp->Revision;
2238 * ACPI 6.0 states that current revision is 2
2239 * from acpi_table_rsdp definition:
2240 * Must be (0) for ACPI 1.0 or (2) for ACPI 2.0+
2242 if (revision > 2)
2243 revision = 2;
2244 switch (revision) {
2245 case 2:
2247 * Use the XSDT unless BIOS is buggy and
2248 * claims to be rev 2 but has a null XSDT
2249 * address
2251 xsdt_addr = rsdp->XsdtPhysicalAddress;
2252 if (xsdt_addr != 0)
2253 break;
2254 /* FALLTHROUGH */
2255 case 0:
2256 /* treat RSDP rev 0 as revision 1 internally */
2257 revision = 1;
2258 /* FALLTHROUGH */
2259 case 1:
2260 /* use the RSDT for rev 0/1 */
2261 xsdt_addr = rsdp->RsdtPhysicalAddress;
2262 break;
2263 default:
2264 /* unknown revision */
2265 revision = 0;
2266 break;
2269 if (revision == 0)
2270 return (NULL);
2272 /* cache the XSDT info */
2273 xsdt = (ACPI_TABLE_XSDT *)map_fw_table(xsdt_addr);
2274 len = (xsdt->Header.Length - sizeof (xsdt->Header)) /
2275 ((revision == 1) ? sizeof (uint32_t) : sizeof (uint64_t));
2279 * Scan the table headers looking for a signature match
2281 for (n = 0; n < len; n++) {
2282 ACPI_TABLE_RSDT *rsdt = (ACPI_TABLE_RSDT *)xsdt;
2283 table_addr = (revision == 1) ? rsdt->TableOffsetEntry[n] :
2284 xsdt->TableOffsetEntry[n];
2286 if (table_addr == 0)
2287 continue;
2288 tp = map_fw_table(table_addr);
2289 if (strncmp(tp->Signature, signature, ACPI_NAME_SIZE) == 0) {
2290 return (tp);
2293 return (NULL);
2296 static void
2297 process_mcfg(ACPI_TABLE_MCFG *tp)
2299 ACPI_MCFG_ALLOCATION *cfg_baap;
2300 char *cfg_baa_endp;
2301 int64_t ecfginfo[4];
2303 cfg_baap = (ACPI_MCFG_ALLOCATION *)((uintptr_t)tp + sizeof (*tp));
2304 cfg_baa_endp = ((char *)tp) + tp->Header.Length;
2305 while ((char *)cfg_baap < cfg_baa_endp) {
2306 if (cfg_baap->Address != 0 && cfg_baap->PciSegment == 0) {
2307 ecfginfo[0] = cfg_baap->Address;
2308 ecfginfo[1] = cfg_baap->PciSegment;
2309 ecfginfo[2] = cfg_baap->StartBusNumber;
2310 ecfginfo[3] = cfg_baap->EndBusNumber;
2311 bsetprop(MCFG_PROPNAME, strlen(MCFG_PROPNAME),
2312 ecfginfo, sizeof (ecfginfo));
2313 break;
2315 cfg_baap++;
2319 #ifndef __xpv
2320 static void
2321 process_madt_entries(ACPI_TABLE_MADT *tp, uint32_t *cpu_countp,
2322 uint32_t *cpu_possible_countp, uint32_t *cpu_apicid_array)
2324 ACPI_SUBTABLE_HEADER *item, *end;
2325 uint32_t cpu_count = 0;
2326 uint32_t cpu_possible_count = 0;
2329 * Determine number of CPUs and keep track of "final" APIC ID
2330 * for each CPU by walking through ACPI MADT processor list
2332 end = (ACPI_SUBTABLE_HEADER *)(tp->Header.Length + (uintptr_t)tp);
2333 item = (ACPI_SUBTABLE_HEADER *)((uintptr_t)tp + sizeof (*tp));
2335 while (item < end) {
2336 switch (item->Type) {
2337 case ACPI_MADT_TYPE_LOCAL_APIC: {
2338 ACPI_MADT_LOCAL_APIC *cpu =
2339 (ACPI_MADT_LOCAL_APIC *) item;
2341 if (cpu->LapicFlags & ACPI_MADT_ENABLED) {
2342 if (cpu_apicid_array != NULL)
2343 cpu_apicid_array[cpu_count] = cpu->Id;
2344 cpu_count++;
2346 cpu_possible_count++;
2347 break;
2349 case ACPI_MADT_TYPE_LOCAL_X2APIC: {
2350 ACPI_MADT_LOCAL_X2APIC *cpu =
2351 (ACPI_MADT_LOCAL_X2APIC *) item;
2353 if (cpu->LapicFlags & ACPI_MADT_ENABLED) {
2354 if (cpu_apicid_array != NULL)
2355 cpu_apicid_array[cpu_count] =
2356 cpu->LocalApicId;
2357 cpu_count++;
2359 cpu_possible_count++;
2360 break;
2362 default:
2363 if (kbm_debug)
2364 bop_printf(NULL, "MADT type %d\n", item->Type);
2365 break;
2368 item = (ACPI_SUBTABLE_HEADER *)((uintptr_t)item + item->Length);
2370 if (cpu_countp)
2371 *cpu_countp = cpu_count;
2372 if (cpu_possible_countp)
2373 *cpu_possible_countp = cpu_possible_count;
2376 static void
2377 process_madt(ACPI_TABLE_MADT *tp)
2379 uint32_t cpu_count = 0;
2380 uint32_t cpu_possible_count = 0;
2381 uint32_t *cpu_apicid_array; /* x2APIC ID is 32bit! */
2383 if (tp != NULL) {
2384 /* count cpu's */
2385 process_madt_entries(tp, &cpu_count, &cpu_possible_count, NULL);
2387 cpu_apicid_array = (uint32_t *)do_bsys_alloc(NULL, NULL,
2388 cpu_count * sizeof (*cpu_apicid_array), MMU_PAGESIZE);
2389 if (cpu_apicid_array == NULL)
2390 bop_panic("Not enough memory for APIC ID array");
2392 /* copy IDs */
2393 process_madt_entries(tp, NULL, NULL, cpu_apicid_array);
2396 * Make boot property for array of "final" APIC IDs for each
2397 * CPU
2399 bsetprop(BP_CPU_APICID_ARRAY, strlen(BP_CPU_APICID_ARRAY),
2400 cpu_apicid_array, cpu_count * sizeof (*cpu_apicid_array));
2404 * Check whether property plat-max-ncpus is already set.
2406 if (do_bsys_getproplen(NULL, PLAT_MAX_NCPUS_NAME) < 0) {
2408 * Set plat-max-ncpus to number of maximum possible CPUs given
2409 * in MADT if it hasn't been set.
2410 * There's no formal way to detect max possible CPUs supported
2411 * by platform according to ACPI spec3.0b. So current CPU
2412 * hotplug implementation expects that all possible CPUs will
2413 * have an entry in MADT table and set plat-max-ncpus to number
2414 * of entries in MADT.
2415 * With introducing of ACPI4.0, Maximum System Capability Table
2416 * (MSCT) provides maximum number of CPUs supported by platform.
2417 * If MSCT is unavailable, fall back to old way.
2419 if (tp != NULL)
2420 bsetpropsi(PLAT_MAX_NCPUS_NAME, cpu_possible_count);
2424 * Set boot property boot-max-ncpus to number of CPUs existing at
2425 * boot time. boot-max-ncpus is mainly used for optimization.
2427 if (tp != NULL)
2428 bsetpropsi(BOOT_MAX_NCPUS_NAME, cpu_count);
2431 * User-set boot-ncpus overrides firmware count
2433 if (do_bsys_getproplen(NULL, BOOT_NCPUS_NAME) >= 0)
2434 return;
2437 * Set boot property boot-ncpus to number of active CPUs given in MADT
2438 * if it hasn't been set yet.
2440 if (tp != NULL)
2441 bsetpropsi(BOOT_NCPUS_NAME, cpu_count);
2444 static void
2445 process_srat(ACPI_TABLE_SRAT *tp)
2447 ACPI_SUBTABLE_HEADER *item, *end;
2448 int i;
2449 int proc_num, mem_num;
2450 #pragma pack(1)
2451 struct {
2452 uint32_t domain;
2453 uint32_t apic_id;
2454 uint32_t sapic_id;
2455 } processor;
2456 struct {
2457 uint32_t domain;
2458 uint32_t x2apic_id;
2459 } x2apic;
2460 struct {
2461 uint32_t domain;
2462 uint64_t addr;
2463 uint64_t length;
2464 uint32_t flags;
2465 } memory;
2466 #pragma pack()
2467 char prop_name[30];
2468 uint64_t maxmem = 0;
2470 if (tp == NULL)
2471 return;
2473 proc_num = mem_num = 0;
2474 end = (ACPI_SUBTABLE_HEADER *)(tp->Header.Length + (uintptr_t)tp);
2475 item = (ACPI_SUBTABLE_HEADER *)((uintptr_t)tp + sizeof (*tp));
2476 while (item < end) {
2477 switch (item->Type) {
2478 case ACPI_SRAT_TYPE_CPU_AFFINITY: {
2479 ACPI_SRAT_CPU_AFFINITY *cpu =
2480 (ACPI_SRAT_CPU_AFFINITY *) item;
2482 if (!(cpu->Flags & ACPI_SRAT_CPU_ENABLED))
2483 break;
2484 processor.domain = cpu->ProximityDomainLo;
2485 for (i = 0; i < 3; i++)
2486 processor.domain +=
2487 cpu->ProximityDomainHi[i] << ((i + 1) * 8);
2488 processor.apic_id = cpu->ApicId;
2489 processor.sapic_id = cpu->LocalSapicEid;
2490 (void) snprintf(prop_name, 30, "acpi-srat-processor-%d",
2491 proc_num);
2492 bsetprop(prop_name, strlen(prop_name), &processor,
2493 sizeof (processor));
2494 proc_num++;
2495 break;
2497 case ACPI_SRAT_TYPE_MEMORY_AFFINITY: {
2498 ACPI_SRAT_MEM_AFFINITY *mem =
2499 (ACPI_SRAT_MEM_AFFINITY *)item;
2501 if (!(mem->Flags & ACPI_SRAT_MEM_ENABLED))
2502 break;
2503 memory.domain = mem->ProximityDomain;
2504 memory.addr = mem->BaseAddress;
2505 memory.length = mem->Length;
2506 memory.flags = mem->Flags;
2507 (void) snprintf(prop_name, 30, "acpi-srat-memory-%d",
2508 mem_num);
2509 bsetprop(prop_name, strlen(prop_name), &memory,
2510 sizeof (memory));
2511 if ((mem->Flags & ACPI_SRAT_MEM_HOT_PLUGGABLE) &&
2512 (memory.addr + memory.length > maxmem)) {
2513 maxmem = memory.addr + memory.length;
2515 mem_num++;
2516 break;
2518 case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY: {
2519 ACPI_SRAT_X2APIC_CPU_AFFINITY *x2cpu =
2520 (ACPI_SRAT_X2APIC_CPU_AFFINITY *) item;
2522 if (!(x2cpu->Flags & ACPI_SRAT_CPU_ENABLED))
2523 break;
2524 x2apic.domain = x2cpu->ProximityDomain;
2525 x2apic.x2apic_id = x2cpu->ApicId;
2526 (void) snprintf(prop_name, 30, "acpi-srat-processor-%d",
2527 proc_num);
2528 bsetprop(prop_name, strlen(prop_name), &x2apic,
2529 sizeof (x2apic));
2530 proc_num++;
2531 break;
2533 default:
2534 if (kbm_debug)
2535 bop_printf(NULL, "SRAT type %d\n", item->Type);
2536 break;
2539 item = (ACPI_SUBTABLE_HEADER *)
2540 (item->Length + (uintptr_t)item);
2544 * The maximum physical address calculated from the SRAT table is more
2545 * accurate than that calculated from the MSCT table.
2547 if (maxmem != 0) {
2548 plat_dr_physmax = btop(maxmem);
2552 static void
2553 process_slit(ACPI_TABLE_SLIT *tp)
2557 * Check the number of localities; if it's too huge, we just
2558 * return and locality enumeration code will handle this later,
2559 * if possible.
2561 * Note that the size of the table is the square of the
2562 * number of localities; if the number of localities exceeds
2563 * UINT16_MAX, the table size may overflow an int when being
2564 * passed to bsetprop() below.
2566 if (tp->LocalityCount >= SLIT_LOCALITIES_MAX)
2567 return;
2569 bsetprop(SLIT_NUM_PROPNAME, strlen(SLIT_NUM_PROPNAME),
2570 &tp->LocalityCount, sizeof (tp->LocalityCount));
2571 bsetprop(SLIT_PROPNAME, strlen(SLIT_PROPNAME), &tp->Entry,
2572 tp->LocalityCount * tp->LocalityCount);
2575 static ACPI_TABLE_MSCT *
2576 process_msct(ACPI_TABLE_MSCT *tp)
2578 int last_seen = 0;
2579 int proc_num = 0;
2580 ACPI_MSCT_PROXIMITY *item, *end;
2581 extern uint64_t plat_dr_options;
2583 ASSERT(tp != NULL);
2585 end = (ACPI_MSCT_PROXIMITY *)(tp->Header.Length + (uintptr_t)tp);
2586 for (item = (void *)((uintptr_t)tp + tp->ProximityOffset);
2587 item < end;
2588 item = (void *)(item->Length + (uintptr_t)item)) {
2590 * Sanity check according to section 5.2.19.1 of ACPI 4.0.
2591 * Revision 1
2592 * Length 22
2594 if (item->Revision != 1 || item->Length != 22) {
2595 cmn_err(CE_CONT,
2596 "?boot: unknown proximity domain structure in MSCT "
2597 "with Revision(%d), Length(%d).\n",
2598 (int)item->Revision, (int)item->Length);
2599 return (NULL);
2600 } else if (item->RangeStart > item->RangeEnd) {
2601 cmn_err(CE_CONT,
2602 "?boot: invalid proximity domain structure in MSCT "
2603 "with RangeStart(%u), RangeEnd(%u).\n",
2604 item->RangeStart, item->RangeEnd);
2605 return (NULL);
2606 } else if (item->RangeStart != last_seen) {
2608 * Items must be organized in ascending order of the
2609 * proximity domain enumerations.
2611 cmn_err(CE_CONT,
2612 "?boot: invalid proximity domain structure in MSCT,"
2613 " items are not orginized in ascending order.\n");
2614 return (NULL);
2618 * If ProcessorCapacity is 0 then there would be no CPUs in this
2619 * domain.
2621 if (item->ProcessorCapacity != 0) {
2622 proc_num += (item->RangeEnd - item->RangeStart + 1) *
2623 item->ProcessorCapacity;
2626 last_seen = item->RangeEnd - item->RangeStart + 1;
2628 * Break out if all proximity domains have been processed.
2629 * Some BIOSes may have unused items at the end of MSCT table.
2631 if (last_seen > tp->MaxProximityDomains) {
2632 break;
2635 if (last_seen != tp->MaxProximityDomains + 1) {
2636 cmn_err(CE_CONT,
2637 "?boot: invalid proximity domain structure in MSCT, "
2638 "proximity domain count doesn't match.\n");
2639 return (NULL);
2643 * Set plat-max-ncpus property if it hasn't been set yet.
2645 if (do_bsys_getproplen(NULL, PLAT_MAX_NCPUS_NAME) < 0) {
2646 if (proc_num != 0) {
2647 bsetpropsi(PLAT_MAX_NCPUS_NAME, proc_num);
2652 * Use Maximum Physical Address from the MSCT table as upper limit for
2653 * memory hot-adding by default. It may be overridden by value from
2654 * the SRAT table or the "plat-dr-physmax" boot option.
2656 plat_dr_physmax = btop(tp->MaxAddress + 1);
2659 * Existence of MSCT implies CPU/memory hotplug-capability for the
2660 * platform.
2662 plat_dr_options |= PLAT_DR_FEATURE_CPU;
2663 plat_dr_options |= PLAT_DR_FEATURE_MEMORY;
2665 return (tp);
2668 #else /* __xpv */
2669 static void
2670 enumerate_xen_cpus()
2672 processorid_t id, max_id;
2675 * User-set boot-ncpus overrides enumeration
2677 if (do_bsys_getproplen(NULL, BOOT_NCPUS_NAME) >= 0)
2678 return;
2681 * Probe every possible virtual CPU id and remember the
2682 * highest id present; the count of CPUs is one greater
2683 * than this. This tacitly assumes at least cpu 0 is present.
2685 max_id = 0;
2686 for (id = 0; id < MAX_VIRT_CPUS; id++)
2687 if (HYPERVISOR_vcpu_op(VCPUOP_is_up, id, NULL) == 0)
2688 max_id = id;
2690 bsetpropsi(BOOT_NCPUS_NAME, max_id+1);
2693 #endif /* __xpv */
2695 /*ARGSUSED*/
2696 static void
2697 build_firmware_properties(struct xboot_info *xbp)
2699 ACPI_TABLE_HEADER *tp = NULL;
2701 #ifndef __xpv
2702 if (xbp->bi_uefi_arch == XBI_UEFI_ARCH_64) {
2703 bsetprops("efi-systype", "64");
2704 bsetprop64("efi-systab",
2705 (uint64_t)(uintptr_t)xbp->bi_uefi_systab);
2706 if (kbm_debug)
2707 bop_printf(NULL, "64-bit UEFI detected.\n");
2708 } else if (xbp->bi_uefi_arch == XBI_UEFI_ARCH_32) {
2709 bsetprops("efi-systype", "32");
2710 bsetprop64("efi-systab",
2711 (uint64_t)(uintptr_t)xbp->bi_uefi_systab);
2712 if (kbm_debug)
2713 bop_printf(NULL, "32-bit UEFI detected.\n");
2716 if (xbp->bi_acpi_rsdp != NULL) {
2717 bsetprop64("acpi-root-tab",
2718 (uint64_t)(uintptr_t)xbp->bi_acpi_rsdp);
2721 if (xbp->bi_smbios != NULL) {
2722 bsetprop64("smbios-address",
2723 (uint64_t)(uintptr_t)xbp->bi_smbios);
2726 if ((tp = find_fw_table(ACPI_SIG_MSCT)) != NULL)
2727 msct_ptr = process_msct((ACPI_TABLE_MSCT *)tp);
2728 else
2729 msct_ptr = NULL;
2731 if ((tp = find_fw_table(ACPI_SIG_MADT)) != NULL)
2732 process_madt((ACPI_TABLE_MADT *)tp);
2734 if ((srat_ptr = (ACPI_TABLE_SRAT *)
2735 find_fw_table(ACPI_SIG_SRAT)) != NULL)
2736 process_srat(srat_ptr);
2738 if (slit_ptr = (ACPI_TABLE_SLIT *)find_fw_table(ACPI_SIG_SLIT))
2739 process_slit(slit_ptr);
2741 tp = find_fw_table(ACPI_SIG_MCFG);
2742 #else /* __xpv */
2743 enumerate_xen_cpus();
2744 if (DOMAIN_IS_INITDOMAIN(xen_info))
2745 tp = find_fw_table(ACPI_SIG_MCFG);
2746 #endif /* __xpv */
2747 if (tp != NULL)
2748 process_mcfg((ACPI_TABLE_MCFG *)tp);
2752 * fake up a boot property for deferred early console output
2753 * this is used by both graphical boot and the (developer only)
2754 * USB serial console
2756 void *
2757 defcons_init(size_t size)
2759 static char *p = NULL;
2761 p = do_bsys_alloc(NULL, NULL, size, MMU_PAGESIZE);
2762 *p = 0;
2763 bsetprop("deferred-console-buf", strlen("deferred-console-buf") + 1,
2764 &p, sizeof (p));
2765 return (p);
2768 /*ARGSUSED*/
2770 boot_compinfo(int fd, struct compinfo *cbp)
2772 cbp->iscmp = 0;
2773 cbp->blksize = MAXBSIZE;
2774 return (0);
2777 #define BP_MAX_STRLEN 32
2780 * Get value for given boot property
2783 bootprop_getval(const char *prop_name, u_longlong_t *prop_value)
2785 int boot_prop_len;
2786 char str[BP_MAX_STRLEN];
2787 u_longlong_t value;
2789 boot_prop_len = BOP_GETPROPLEN(bootops, prop_name);
2790 if (boot_prop_len < 0 || boot_prop_len > sizeof (str) ||
2791 BOP_GETPROP(bootops, prop_name, str) < 0 ||
2792 kobj_getvalue(str, &value) == -1)
2793 return (-1);
2795 if (prop_value)
2796 *prop_value = value;
2798 return (0);