Merge commit '5e2cca1843c61ee0ef1bb95c5dddc9b450b790c6'
[unleashed.git] / arch / x86 / kernel / os / fakebop.c
blob845eba85f45d93ff517707de205fd709e984c7d7
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 2018 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 #include <vm/kboot_mmu.h>
64 #include <vm/hat_pte.h>
65 #include <sys/kobj.h>
66 #include <sys/kobj_lex.h>
67 #include <sys/pci_cfgspace_impl.h>
68 #include <sys/fastboot_impl.h>
69 #include <sys/acpi/acconfig.h>
70 #include <sys/acpi/acpi.h>
71 #include <sys/ddipropdefs.h> /* For DDI prop types */
73 static int have_console = 0; /* set once primitive console is initialized */
74 static char *boot_args = "";
77 * Debugging macros
79 static uint_t kbm_debug = 0;
80 #define DBG_MSG(s) { if (kbm_debug) bop_printf(NULL, "%s", s); }
81 #define DBG(x) { if (kbm_debug) \
82 bop_printf(NULL, "%s is %" PRIx64 "\n", #x, (uint64_t)(x)); \
85 #define PUT_STRING(s) { \
86 char *cp; \
87 for (cp = (s); *cp; ++cp) \
88 bcons_putchar(*cp); \
91 bootops_t bootop; /* simple bootops we'll pass on to kernel */
92 struct bsys_mem bm;
95 * Boot info from "glue" code in low memory. xbootp is used by:
96 * do_bop_phys_alloc(), do_bsys_alloc() and boot_prop_finish().
98 static struct xboot_info *xbootp;
99 static uintptr_t next_virt; /* next available virtual address */
100 static paddr_t next_phys; /* next available physical address from dboot */
101 static paddr_t high_phys = -(paddr_t)1; /* last used physical address */
104 * buffer for vsnprintf for console I/O
106 #define BUFFERSIZE 512
107 static char buffer[BUFFERSIZE];
110 * stuff to store/report/manipulate boot property settings.
112 typedef struct bootprop {
113 struct bootprop *bp_next;
114 char *bp_name;
115 int bp_flags; /* DDI prop type */
116 uint_t bp_vlen; /* 0 for boolean */
117 char *bp_value;
118 } bootprop_t;
120 static bootprop_t *bprops = NULL;
121 static char *curr_page = NULL; /* ptr to avail bprop memory */
122 static int curr_space = 0; /* amount of memory at curr_page */
126 * some allocator statistics
128 static ulong_t total_bop_alloc_scratch = 0;
129 static ulong_t total_bop_alloc_kernel = 0;
131 static void build_firmware_properties(struct xboot_info *);
133 static int early_allocation = 1;
135 int force_fastreboot = 0;
136 volatile int fastreboot_onpanic = 0;
137 int post_fastreboot = 0;
138 volatile int fastreboot_capable = 1;
141 * Information saved from current boot for fast reboot.
142 * If the information size exceeds what we have allocated, fast reboot
143 * will not be supported.
145 multiboot_info_t saved_mbi;
146 mb_memory_map_t saved_mmap[FASTBOOT_SAVED_MMAP_COUNT];
147 uint8_t saved_drives[FASTBOOT_SAVED_DRIVES_SIZE];
148 char saved_cmdline[FASTBOOT_SAVED_CMDLINE_LEN];
149 int saved_cmdline_len = 0;
150 size_t saved_file_size[FASTBOOT_MAX_FILES_MAP];
153 * Turn off fastreboot_onpanic to avoid panic loop.
155 char fastreboot_onpanic_cmdline[FASTBOOT_SAVED_CMDLINE_LEN];
156 static const char fastreboot_onpanic_args[] = " -B fastreboot_onpanic=0";
159 * Pointers to where System Resource Affinity Table (SRAT), System Locality
160 * Information Table (SLIT) and Maximum System Capability Table (MSCT)
161 * are mapped into virtual memory
163 ACPI_TABLE_SRAT *srat_ptr = NULL;
164 ACPI_TABLE_SLIT *slit_ptr = NULL;
165 ACPI_TABLE_MSCT *msct_ptr = NULL;
168 * Arbitrary limit on number of localities we handle; if
169 * this limit is raised to more than UINT16_MAX, make sure
170 * process_slit() knows how to handle it.
172 #define SLIT_LOCALITIES_MAX (4096)
174 #define SLIT_NUM_PROPNAME "acpi-slit-localities"
175 #define SLIT_PROPNAME "acpi-slit"
178 * Allocate aligned physical memory at boot time. This allocator allocates
179 * from the highest possible addresses. This avoids exhausting memory that
180 * would be useful for DMA buffers.
182 paddr_t
183 do_bop_phys_alloc(uint64_t size, uint64_t align)
185 paddr_t pa = 0;
186 paddr_t start;
187 paddr_t end;
188 struct memlist *ml = (struct memlist *)xbootp->bi_phys_install;
191 * Be careful if high memory usage is limited in startup.c
192 * Since there are holes in the low part of the physical address
193 * space we can treat physmem as a pfn (not just a pgcnt) and
194 * get a conservative upper limit.
196 if (physmem != 0 && high_phys > pfn_to_pa(physmem))
197 high_phys = pfn_to_pa(physmem);
200 * find the highest available memory in physinstalled
202 size = P2ROUNDUP(size, align);
203 for (; ml; ml = ml->ml_next) {
204 start = P2ROUNDUP(ml->ml_address, align);
205 end = P2ALIGN(ml->ml_address + ml->ml_size, align);
206 if (start < next_phys)
207 start = P2ROUNDUP(next_phys, align);
208 if (end > high_phys)
209 end = P2ALIGN(high_phys, align);
211 if (end <= start)
212 continue;
213 if (end - start < size)
214 continue;
217 * Early allocations need to use low memory, since
218 * physmem might be further limited by bootenv.rc
220 if (early_allocation) {
221 if (pa == 0 || start < pa)
222 pa = start;
223 } else {
224 if (end - size > pa)
225 pa = end - size;
228 if (pa != 0) {
229 if (early_allocation)
230 next_phys = pa + size;
231 else
232 high_phys = pa;
233 return (pa);
235 bop_panic("do_bop_phys_alloc(0x%" PRIx64 ", 0x%" PRIx64
236 ") Out of memory\n", size, align);
237 /*NOTREACHED*/
240 uintptr_t
241 alloc_vaddr(size_t size, paddr_t align)
243 uintptr_t rv;
245 next_virt = P2ROUNDUP(next_virt, (uintptr_t)align);
246 rv = (uintptr_t)next_virt;
247 next_virt += size;
248 return (rv);
252 * Allocate virtual memory. The size is always rounded up to a multiple
253 * of base pagesize.
256 /*ARGSUSED*/
257 static caddr_t
258 do_bsys_alloc(bootops_t *bop, caddr_t virthint, size_t size, int align)
260 paddr_t a = align; /* same type as pa for masking */
261 uint_t pgsize;
262 paddr_t pa;
263 uintptr_t va;
264 ssize_t s; /* the aligned size */
265 uint_t level;
266 uint_t is_kernel = (virthint != 0);
268 if (a < MMU_PAGESIZE)
269 a = MMU_PAGESIZE;
270 else if (!ISP2(a))
271 prom_panic("do_bsys_alloc() incorrect alignment");
272 size = P2ROUNDUP(size, MMU_PAGESIZE);
275 * Use the next aligned virtual address if we weren't given one.
277 if (virthint == NULL) {
278 virthint = (caddr_t)alloc_vaddr(size, a);
279 total_bop_alloc_scratch += size;
280 } else {
281 total_bop_alloc_kernel += size;
285 * allocate the physical memory
287 pa = do_bop_phys_alloc(size, a);
290 * Add the mappings to the page tables, try large pages first.
292 va = (uintptr_t)virthint;
293 s = size;
294 level = 1;
295 pgsize = xbootp->bi_use_pae ? TWO_MEG : FOUR_MEG;
296 if (xbootp->bi_use_largepage && a == pgsize) {
297 while (IS_P2ALIGNED(pa, pgsize) && IS_P2ALIGNED(va, pgsize) &&
298 s >= pgsize) {
299 kbm_map(va, pa, level, is_kernel);
300 va += pgsize;
301 pa += pgsize;
302 s -= pgsize;
307 * Map remaining pages use small mappings
309 level = 0;
310 pgsize = MMU_PAGESIZE;
311 while (s > 0) {
312 kbm_map(va, pa, level, is_kernel);
313 va += pgsize;
314 pa += pgsize;
315 s -= pgsize;
317 return (virthint);
321 * Free virtual memory - we'll just ignore these.
323 /*ARGSUSED*/
324 static void
325 do_bsys_free(bootops_t *bop, caddr_t virt, size_t size)
327 bop_printf(NULL, "do_bsys_free(virt=0x%p, size=0x%lx) ignored\n",
328 (void *)virt, size);
332 * Old interface
334 /*ARGSUSED*/
335 static caddr_t
336 do_bsys_ealloc(bootops_t *bop, caddr_t virthint, size_t size,
337 int align, int flags)
339 prom_panic("unsupported call to BOP_EALLOC()\n");
340 return (0);
344 static void
345 bsetprop(int flags, char *name, int nlen, void *value, int vlen)
347 uint_t size;
348 uint_t need_size;
349 bootprop_t *b;
352 * align the size to 16 byte boundary
354 size = sizeof (bootprop_t) + nlen + 1 + vlen;
355 size = (size + 0xf) & ~0xf;
356 if (size > curr_space) {
357 need_size = (size + (MMU_PAGEOFFSET)) & MMU_PAGEMASK;
358 curr_page = do_bsys_alloc(NULL, 0, need_size, MMU_PAGESIZE);
359 curr_space = need_size;
363 * use a bootprop_t at curr_page and link into list
365 b = (bootprop_t *)curr_page;
366 curr_page += sizeof (bootprop_t);
367 curr_space -= sizeof (bootprop_t);
368 b->bp_next = bprops;
369 bprops = b;
372 * follow by name and ending zero byte
374 b->bp_name = curr_page;
375 bcopy(name, curr_page, nlen);
376 curr_page += nlen;
377 *curr_page++ = 0;
378 curr_space -= nlen + 1;
381 * set the property type
383 b->bp_flags = flags & DDI_PROP_TYPE_MASK;
386 * copy in value, but no ending zero byte
388 b->bp_value = curr_page;
389 b->bp_vlen = vlen;
390 if (vlen > 0) {
391 bcopy(value, curr_page, vlen);
392 curr_page += vlen;
393 curr_space -= vlen;
397 * align new values of curr_page, curr_space
399 while (curr_space & 0xf) {
400 ++curr_page;
401 --curr_space;
405 static void
406 bsetprops(char *name, char *value)
408 bsetprop(DDI_PROP_TYPE_STRING, name, strlen(name),
409 value, strlen(value) + 1);
412 static void
413 bsetprop32(char *name, uint32_t value)
415 bsetprop(DDI_PROP_TYPE_INT, name, strlen(name),
416 (void *)&value, sizeof (value));
419 static void
420 bsetprop64(char *name, uint64_t value)
422 bsetprop(DDI_PROP_TYPE_INT64, name, strlen(name),
423 (void *)&value, sizeof (value));
426 static void
427 bsetpropsi(char *name, int value)
429 char prop_val[32];
431 (void) snprintf(prop_val, sizeof (prop_val), "%d", value);
432 bsetprops(name, prop_val);
436 * to find the type of the value associated with this name
438 /*ARGSUSED*/
440 do_bsys_getproptype(bootops_t *bop, const char *name)
442 bootprop_t *b;
444 for (b = bprops; b != NULL; b = b->bp_next) {
445 if (strcmp(name, b->bp_name) != 0)
446 continue;
447 return (b->bp_flags);
449 return (-1);
453 * to find the size of the buffer to allocate
455 /*ARGSUSED*/
457 do_bsys_getproplen(bootops_t *bop, const char *name)
459 bootprop_t *b;
461 for (b = bprops; b; b = b->bp_next) {
462 if (strcmp(name, b->bp_name) != 0)
463 continue;
464 return (b->bp_vlen);
466 return (-1);
470 * get the value associated with this name
472 /*ARGSUSED*/
474 do_bsys_getprop(bootops_t *bop, const char *name, void *value)
476 bootprop_t *b;
478 for (b = bprops; b; b = b->bp_next) {
479 if (strcmp(name, b->bp_name) != 0)
480 continue;
481 bcopy(b->bp_value, value, b->bp_vlen);
482 return (0);
484 return (-1);
488 * get the name of the next property in succession from the standalone
490 /*ARGSUSED*/
491 static char *
492 do_bsys_nextprop(bootops_t *bop, char *name)
494 bootprop_t *b;
497 * A null name is a special signal for the 1st boot property
499 if (name == NULL || strlen(name) == 0) {
500 if (bprops == NULL)
501 return (NULL);
502 return (bprops->bp_name);
505 for (b = bprops; b; b = b->bp_next) {
506 if (name != b->bp_name)
507 continue;
508 b = b->bp_next;
509 if (b == NULL)
510 return (NULL);
511 return (b->bp_name);
513 return (NULL);
517 * Parse numeric value from a string. Understands decimal, hex, octal, - and ~
519 static int
520 parse_value(char *p, uint64_t *retval)
522 int adjust = 0;
523 uint64_t tmp = 0;
524 int digit;
525 int radix = 10;
527 *retval = 0;
528 if (*p == '-' || *p == '~')
529 adjust = *p++;
531 if (*p == '0') {
532 ++p;
533 if (*p == 0)
534 return (0);
535 if (*p == 'x' || *p == 'X') {
536 radix = 16;
537 ++p;
538 } else {
539 radix = 8;
540 ++p;
543 while (*p) {
544 if ('0' <= *p && *p <= '9')
545 digit = *p - '0';
546 else if ('a' <= *p && *p <= 'f')
547 digit = 10 + *p - 'a';
548 else if ('A' <= *p && *p <= 'F')
549 digit = 10 + *p - 'A';
550 else
551 return (-1);
552 if (digit >= radix)
553 return (-1);
554 tmp = tmp * radix + digit;
555 ++p;
557 if (adjust == '-')
558 tmp = -tmp;
559 else if (adjust == '~')
560 tmp = ~tmp;
561 *retval = tmp;
562 return (0);
565 static boolean_t
566 unprintable(char *value, int size)
568 int i;
570 if (size <= 0 || value[0] == '\0')
571 return (B_TRUE);
573 for (i = 0; i < size; i++) {
574 if (value[i] == '\0')
575 return (i != (size - 1));
577 if (!isprint(value[i]))
578 return (B_TRUE);
580 return (B_FALSE);
584 * Print out information about all boot properties.
585 * buffer is pointer to pre-allocated space to be used as temporary
586 * space for property values.
588 static void
589 boot_prop_display(char *buffer)
591 char *name = "";
592 int i, len, flags, *buf32;
593 int64_t *buf64;
595 bop_printf(NULL, "\nBoot properties:\n");
597 while ((name = do_bsys_nextprop(NULL, name)) != NULL) {
598 bop_printf(NULL, "\t0x%p %s = ", (void *)name, name);
599 (void) do_bsys_getprop(NULL, name, buffer);
600 len = do_bsys_getproplen(NULL, name);
601 flags = do_bsys_getproptype(NULL, name);
602 bop_printf(NULL, "len=%d ", len);
604 switch (flags) {
605 case DDI_PROP_TYPE_INT:
606 len = len / sizeof (int);
607 buf32 = (int *)buffer;
608 for (i = 0; i < len; i++) {
609 bop_printf(NULL, "%08x", buf32[i]);
610 if (i < len - 1)
611 bop_printf(NULL, ".");
613 break;
614 case DDI_PROP_TYPE_STRING:
615 bop_printf(NULL, "%s", buffer);
616 break;
617 case DDI_PROP_TYPE_INT64:
618 len = len / sizeof (int64_t);
619 buf64 = (int64_t *)buffer;
620 for (i = 0; i < len; i++) {
621 bop_printf(NULL, "%016" PRIx64, buf64[i]);
622 if (i < len - 1)
623 bop_printf(NULL, ".");
625 break;
626 default:
627 if (!unprintable(buffer, len)) {
628 buffer[len] = 0;
629 bop_printf(NULL, "%s", buffer);
630 break;
632 for (i = 0; i < len; i++) {
633 bop_printf(NULL, "%02x", buffer[i] & 0xff);
634 if (i < len - 1)
635 bop_printf(NULL, ".");
637 break;
639 bop_printf(NULL, "\n");
644 * 2nd part of building the table of boot properties. This includes:
645 * - values from /boot/solaris/bootenv.rc (ie. eeprom(1m) values)
647 * lines look like one of:
648 * ^$
649 * ^# comment till end of line
650 * setprop name 'value'
651 * setprop name value
652 * setprop name "value"
654 * we do single character I/O since this is really just looking at memory
656 void
657 boot_prop_finish(void)
659 int fd;
660 char *line;
661 int c;
662 int bytes_read;
663 char *name;
664 int n_len;
665 char *value;
666 int v_len;
667 char *inputdev; /* these override the command line if serial ports */
668 char *outputdev;
669 char *consoledev;
670 uint64_t lvalue;
671 int use_xencons = 0;
672 extern int bootrd_debug;
675 DBG_MSG("Opening /boot/solaris/bootenv.rc\n");
676 fd = BRD_OPEN(bfs_ops, "/boot/solaris/bootenv.rc", 0);
677 DBG(fd);
679 line = do_bsys_alloc(NULL, NULL, MMU_PAGESIZE, MMU_PAGESIZE);
680 while (fd >= 0) {
683 * get a line
685 for (c = 0; ; ++c) {
686 bytes_read = BRD_READ(bfs_ops, fd, line + c, 1);
687 if (bytes_read == 0) {
688 if (c == 0)
689 goto done;
690 break;
692 if (line[c] == '\n')
693 break;
695 line[c] = 0;
698 * ignore comment lines
700 c = 0;
701 while (ISSPACE(line[c]))
702 ++c;
703 if (line[c] == '#' || line[c] == 0)
704 continue;
707 * must have "setprop " or "setprop\t"
709 if (strncmp(line + c, "setprop ", 8) != 0 &&
710 strncmp(line + c, "setprop\t", 8) != 0)
711 continue;
712 c += 8;
713 while (ISSPACE(line[c]))
714 ++c;
715 if (line[c] == 0)
716 continue;
719 * gather up the property name
721 name = line + c;
722 n_len = 0;
723 while (line[c] && !ISSPACE(line[c]))
724 ++n_len, ++c;
727 * gather up the value, if any
729 value = "";
730 v_len = 0;
731 while (ISSPACE(line[c]))
732 ++c;
733 if (line[c] != 0) {
734 value = line + c;
735 while (line[c] && !ISSPACE(line[c]))
736 ++v_len, ++c;
739 if (v_len >= 2 && value[0] == value[v_len - 1] &&
740 (value[0] == '\'' || value[0] == '"')) {
741 ++value;
742 v_len -= 2;
744 name[n_len] = 0;
745 if (v_len > 0)
746 value[v_len] = 0;
747 else
748 continue;
751 * ignore "boot-file" property, it's now meaningless
753 if (strcmp(name, "boot-file") == 0)
754 continue;
755 if (strcmp(name, "boot-args") == 0 &&
756 strlen(boot_args) > 0)
757 continue;
760 * If a property was explicitly set on the command line
761 * it will override a setting in bootenv.rc
763 if (do_bsys_getproplen(NULL, name) >= 0)
764 continue;
766 bsetprops(name, value);
768 done:
769 if (fd >= 0)
770 (void) BRD_CLOSE(bfs_ops, fd);
773 * Check if we have to limit the boot time allocator
775 if (do_bsys_getproplen(NULL, "physmem") != -1 &&
776 do_bsys_getprop(NULL, "physmem", line) >= 0 &&
777 parse_value(line, &lvalue) != -1) {
778 if (0 < lvalue && (lvalue < physmem || physmem == 0)) {
779 physmem = (pgcnt_t)lvalue;
780 DBG(physmem);
783 early_allocation = 0;
786 * Check for bootrd_debug.
788 if (find_boot_prop("bootrd_debug"))
789 bootrd_debug = 1;
792 * check to see if we have to override the default value of the console
794 if (!use_xencons) {
795 inputdev = line;
796 v_len = do_bsys_getproplen(NULL, "input-device");
797 if (v_len > 0)
798 (void) do_bsys_getprop(NULL, "input-device", inputdev);
799 else
800 v_len = 0;
801 inputdev[v_len] = 0;
803 outputdev = inputdev + v_len + 1;
804 v_len = do_bsys_getproplen(NULL, "output-device");
805 if (v_len > 0)
806 (void) do_bsys_getprop(NULL, "output-device",
807 outputdev);
808 else
809 v_len = 0;
810 outputdev[v_len] = 0;
812 consoledev = outputdev + v_len + 1;
813 v_len = do_bsys_getproplen(NULL, "console");
814 if (v_len > 0) {
815 (void) do_bsys_getprop(NULL, "console", consoledev);
816 if (post_fastreboot &&
817 strcmp(consoledev, "graphics") == 0) {
818 bsetprops("console", "text");
819 v_len = strlen("text");
820 bcopy("text", consoledev, v_len);
822 } else {
823 v_len = 0;
825 consoledev[v_len] = 0;
826 bcons_init2(inputdev, outputdev, consoledev);
827 } else {
829 * Ensure console property exists
830 * If not create it as "hypervisor"
832 v_len = do_bsys_getproplen(NULL, "console");
833 if (v_len < 0)
834 bsetprops("console", "hypervisor");
835 inputdev = outputdev = consoledev = "hypervisor";
836 bcons_init2(inputdev, outputdev, consoledev);
839 if (find_boot_prop("prom_debug") || kbm_debug)
840 boot_prop_display(line);
844 * print formatted output
846 /*PRINTFLIKE2*/
847 /*ARGSUSED*/
848 void
849 bop_printf(bootops_t *bop, const char *fmt, ...)
851 va_list ap;
853 if (have_console == 0)
854 return;
856 va_start(ap, fmt);
857 (void) vsnprintf(buffer, BUFFERSIZE, fmt, ap);
858 va_end(ap);
859 PUT_STRING(buffer);
863 * Another panic() variant; this one can be used even earlier during boot than
864 * prom_panic().
866 /*PRINTFLIKE1*/
867 void
868 bop_panic(const char *fmt, ...)
870 va_list ap;
872 va_start(ap, fmt);
873 bop_printf(NULL, fmt, ap);
874 va_end(ap);
876 bop_printf(NULL, "\nPress any key to reboot.\n");
877 (void) bcons_getchar();
878 bop_printf(NULL, "Resetting...\n");
879 pc_reset();
883 * Do a real mode interrupt BIOS call
885 typedef struct bios_regs {
886 unsigned short ax, bx, cx, dx, si, di, bp, es, ds;
887 } bios_regs_t;
888 typedef int (*bios_func_t)(int, bios_regs_t *);
890 /*ARGSUSED*/
891 static void
892 do_bsys_doint(bootops_t *bop, int intnum, struct bop_regs *rp)
894 static int firsttime = 1;
895 bios_func_t bios_func = (bios_func_t)(void *)(uintptr_t)0x5000;
896 bios_regs_t br;
899 * We're about to disable paging; we shouldn't be PCID enabled.
901 if (getcr4() & CR4_PCIDE)
902 prom_panic("do_bsys_doint() with PCID enabled\n");
905 * The first time we do this, we have to copy the pre-packaged
906 * low memory bios call code image into place.
908 if (firsttime) {
909 extern char bios_image[];
910 extern uint32_t bios_size;
912 bcopy(bios_image, (void *)bios_func, bios_size);
913 firsttime = 0;
916 br.ax = rp->eax.word.ax;
917 br.bx = rp->ebx.word.bx;
918 br.cx = rp->ecx.word.cx;
919 br.dx = rp->edx.word.dx;
920 br.bp = rp->ebp.word.bp;
921 br.si = rp->esi.word.si;
922 br.di = rp->edi.word.di;
923 br.ds = rp->ds;
924 br.es = rp->es;
926 DBG_MSG("Doing BIOS call...");
927 DBG(br.ax);
928 DBG(br.bx);
929 DBG(br.dx);
930 rp->eflags = bios_func(intnum, &br);
931 DBG_MSG("done\n");
933 rp->eax.word.ax = br.ax;
934 rp->ebx.word.bx = br.bx;
935 rp->ecx.word.cx = br.cx;
936 rp->edx.word.dx = br.dx;
937 rp->ebp.word.bp = br.bp;
938 rp->esi.word.si = br.si;
939 rp->edi.word.di = br.di;
940 rp->ds = br.ds;
941 rp->es = br.es;
944 static struct boot_syscalls bop_sysp = {
945 bcons_getchar,
946 bcons_putchar,
947 bcons_ischar,
950 static char *whoami;
952 #define BUFLEN 64
955 static void
956 setup_rarp_props(struct sol_netinfo *sip)
958 char buf[BUFLEN]; /* to hold ip/mac addrs */
959 uint8_t *val;
961 val = (uint8_t *)&sip->sn_ciaddr;
962 (void) snprintf(buf, BUFLEN, "%d.%d.%d.%d",
963 val[0], val[1], val[2], val[3]);
964 bsetprops(BP_HOST_IP, buf);
966 val = (uint8_t *)&sip->sn_siaddr;
967 (void) snprintf(buf, BUFLEN, "%d.%d.%d.%d",
968 val[0], val[1], val[2], val[3]);
969 bsetprops(BP_SERVER_IP, buf);
971 if (sip->sn_giaddr != 0) {
972 val = (uint8_t *)&sip->sn_giaddr;
973 (void) snprintf(buf, BUFLEN, "%d.%d.%d.%d",
974 val[0], val[1], val[2], val[3]);
975 bsetprops(BP_ROUTER_IP, buf);
978 if (sip->sn_netmask != 0) {
979 val = (uint8_t *)&sip->sn_netmask;
980 (void) snprintf(buf, BUFLEN, "%d.%d.%d.%d",
981 val[0], val[1], val[2], val[3]);
982 bsetprops(BP_SUBNET_MASK, buf);
985 if (sip->sn_mactype != 4 || sip->sn_maclen != 6) {
986 bop_printf(NULL, "unsupported mac type %d, mac len %d\n",
987 sip->sn_mactype, sip->sn_maclen);
988 } else {
989 val = sip->sn_macaddr;
990 (void) snprintf(buf, BUFLEN, "%x:%x:%x:%x:%x:%x",
991 val[0], val[1], val[2], val[3], val[4], val[5]);
992 bsetprops(BP_BOOT_MAC, buf);
997 static void
998 build_panic_cmdline(const char *cmd, int cmdlen)
1000 int proplen;
1001 size_t arglen;
1003 arglen = sizeof (fastreboot_onpanic_args);
1005 * If we allready have fastreboot-onpanic set to zero,
1006 * don't add them again.
1008 if ((proplen = do_bsys_getproplen(NULL, FASTREBOOT_ONPANIC)) > 0 &&
1009 proplen <= sizeof (fastreboot_onpanic_cmdline)) {
1010 (void) do_bsys_getprop(NULL, FASTREBOOT_ONPANIC,
1011 fastreboot_onpanic_cmdline);
1012 if (FASTREBOOT_ONPANIC_NOTSET(fastreboot_onpanic_cmdline))
1013 arglen = 1;
1017 * construct fastreboot_onpanic_cmdline
1019 if (cmdlen + arglen > sizeof (fastreboot_onpanic_cmdline)) {
1020 DBG_MSG("Command line too long: clearing "
1021 FASTREBOOT_ONPANIC "\n");
1022 fastreboot_onpanic = 0;
1023 } else {
1024 bcopy(cmd, fastreboot_onpanic_cmdline, cmdlen);
1025 if (arglen != 1)
1026 bcopy(fastreboot_onpanic_args,
1027 fastreboot_onpanic_cmdline + cmdlen, arglen);
1028 else
1029 fastreboot_onpanic_cmdline[cmdlen] = 0;
1035 * Construct boot command line for Fast Reboot. The saved_cmdline
1036 * is also reported by "eeprom bootcmd".
1038 static void
1039 build_fastboot_cmdline(struct xboot_info *xbp)
1041 saved_cmdline_len = strlen(xbp->bi_cmdline) + 1;
1042 if (saved_cmdline_len > FASTBOOT_SAVED_CMDLINE_LEN) {
1043 DBG(saved_cmdline_len);
1044 DBG_MSG("Command line too long: clearing fastreboot_capable\n");
1045 fastreboot_capable = 0;
1046 } else {
1047 bcopy((void *)(xbp->bi_cmdline), (void *)saved_cmdline,
1048 saved_cmdline_len);
1049 saved_cmdline[saved_cmdline_len - 1] = '\0';
1050 build_panic_cmdline(saved_cmdline, saved_cmdline_len - 1);
1055 * Save memory layout, disk drive information, unix and boot archive sizes for
1056 * Fast Reboot.
1058 static void
1059 save_boot_info(struct xboot_info *xbi)
1061 multiboot_info_t *mbi = xbi->bi_mb_info;
1062 struct boot_modules *modp;
1063 int i;
1065 bcopy(mbi, &saved_mbi, sizeof (multiboot_info_t));
1066 if (mbi->mmap_length > sizeof (saved_mmap)) {
1067 DBG_MSG("mbi->mmap_length too big: clearing "
1068 "fastreboot_capable\n");
1069 fastreboot_capable = 0;
1070 } else {
1071 bcopy((void *)(uintptr_t)mbi->mmap_addr, (void *)saved_mmap,
1072 mbi->mmap_length);
1075 if ((mbi->flags & MB_INFO_DRIVE_INFO) != 0) {
1076 if (mbi->drives_length > sizeof (saved_drives)) {
1077 DBG(mbi->drives_length);
1078 DBG_MSG("mbi->drives_length too big: clearing "
1079 "fastreboot_capable\n");
1080 fastreboot_capable = 0;
1081 } else {
1082 bcopy((void *)(uintptr_t)mbi->drives_addr,
1083 (void *)saved_drives, mbi->drives_length);
1085 } else {
1086 saved_mbi.drives_length = 0;
1087 saved_mbi.drives_addr = (uintptr_t)NULL;
1091 * Current file sizes. Used by fastboot.c to figure out how much
1092 * memory to reserve for panic reboot.
1093 * Use the module list from the dboot-constructed xboot_info
1094 * instead of the list referenced by the multiboot structure
1095 * because that structure may not be addressable now.
1097 saved_file_size[FASTBOOT_NAME_UNIX] = FOUR_MEG - PAGESIZE;
1098 for (i = 0, modp = (struct boot_modules *)(uintptr_t)xbi->bi_modules;
1099 i < xbi->bi_module_cnt; i++, modp++) {
1100 saved_file_size[FASTBOOT_NAME_BOOTARCHIVE] += modp->bm_size;
1105 * Import boot environment module variables as properties, applying
1106 * blacklist filter for variables we know we will not use.
1108 * Since the environment can be relatively large, containing many variables
1109 * used only for boot loader purposes, we will use a blacklist based filter.
1110 * To keep the blacklist from growing too large, we use prefix based filtering.
1111 * This is possible because in many cases, the loader variable names are
1112 * using a structured layout.
1114 * We will not overwrite already set properties.
1116 static struct bop_blacklist {
1117 const char *bl_name;
1118 int bl_name_len;
1119 } bop_prop_blacklist[] = {
1120 { "ISADIR", sizeof ("ISADIR") },
1121 { "acpi", sizeof ("acpi") },
1122 { "autoboot_delay", sizeof ("autoboot_delay") },
1123 { "autoboot_delay", sizeof ("autoboot_delay") },
1124 { "beansi_", sizeof ("beansi_") },
1125 { "beastie", sizeof ("beastie") },
1126 { "bemenu", sizeof ("bemenu") },
1127 { "boot.", sizeof ("boot.") },
1128 { "bootenv", sizeof ("bootenv") },
1129 { "currdev", sizeof ("currdev") },
1130 { "dhcp.", sizeof ("dhcp.") },
1131 { "interpret", sizeof ("interpret") },
1132 { "kernel", sizeof ("kernel") },
1133 { "loaddev", sizeof ("loaddev") },
1134 { "loader_", sizeof ("loader_") },
1135 { "module_path", sizeof ("module_path") },
1136 { "nfs.", sizeof ("nfs.") },
1137 { "pcibios", sizeof ("pcibios") },
1138 { "prompt", sizeof ("prompt") },
1139 { "smbios", sizeof ("smbios") },
1140 { "tem", sizeof ("tem") },
1141 { "twiddle_divisor", sizeof ("twiddle_divisor") },
1142 { "zfs_be", sizeof ("zfs_be") },
1146 * Match the name against prefixes in above blacklist. If the match was
1147 * found, this name is blacklisted.
1149 static boolean_t
1150 name_is_blacklisted(const char *name)
1152 int i, n;
1154 n = sizeof (bop_prop_blacklist) / sizeof (bop_prop_blacklist[0]);
1155 for (i = 0; i < n; i++) {
1156 if (strncmp(bop_prop_blacklist[i].bl_name, name,
1157 bop_prop_blacklist[i].bl_name_len - 1) == 0) {
1158 return (B_TRUE);
1161 return (B_FALSE);
1164 static void
1165 process_boot_environment(struct boot_modules *benv)
1167 char *env, *ptr, *name, *value;
1168 uint32_t size, name_len, value_len;
1170 if (benv == NULL || benv->bm_type != BMT_ENV)
1171 return;
1172 ptr = env = benv->bm_addr;
1173 size = benv->bm_size;
1174 do {
1175 name = ptr;
1176 /* find '=' */
1177 while (*ptr != '=') {
1178 ptr++;
1179 if (ptr > env + size) /* Something is very wrong. */
1180 return;
1182 name_len = ptr - name;
1183 if (sizeof (buffer) <= name_len)
1184 continue;
1186 (void) strncpy(buffer, name, sizeof (buffer));
1187 buffer[name_len] = '\0';
1188 name = buffer;
1190 value_len = 0;
1191 value = ++ptr;
1192 while ((uintptr_t)ptr - (uintptr_t)env < size) {
1193 if (*ptr == '\0') {
1194 ptr++;
1195 value_len = (uintptr_t)ptr - (uintptr_t)env;
1196 break;
1198 ptr++;
1201 /* Did we reach the end of the module? */
1202 if (value_len == 0)
1203 return;
1205 if (*value == '\0')
1206 continue;
1208 /* Is this property already set? */
1209 if (do_bsys_getproplen(NULL, name) >= 0)
1210 continue;
1212 /* Translate netboot variables */
1213 if (strcmp(name, "boot.netif.gateway") == 0) {
1214 bsetprops(BP_ROUTER_IP, value);
1215 continue;
1217 if (strcmp(name, "boot.netif.hwaddr") == 0) {
1218 bsetprops(BP_BOOT_MAC, value);
1219 continue;
1221 if (strcmp(name, "boot.netif.ip") == 0) {
1222 bsetprops(BP_HOST_IP, value);
1223 continue;
1225 if (strcmp(name, "boot.netif.netmask") == 0) {
1226 bsetprops(BP_SUBNET_MASK, value);
1227 continue;
1229 if (strcmp(name, "boot.netif.server") == 0) {
1230 bsetprops(BP_SERVER_IP, value);
1231 continue;
1233 if (strcmp(name, "boot.netif.server") == 0) {
1234 if (do_bsys_getproplen(NULL, BP_SERVER_IP) < 0)
1235 bsetprops(BP_SERVER_IP, value);
1236 continue;
1238 if (strcmp(name, "boot.nfsroot.server") == 0) {
1239 if (do_bsys_getproplen(NULL, BP_SERVER_IP) < 0)
1240 bsetprops(BP_SERVER_IP, value);
1241 continue;
1243 if (strcmp(name, "boot.nfsroot.path") == 0) {
1244 bsetprops(BP_SERVER_PATH, value);
1245 continue;
1248 if (name_is_blacklisted(name) == B_TRUE)
1249 continue;
1251 /* Create new property. */
1252 bsetprops(name, value);
1254 /* Avoid reading past the module end. */
1255 if (size <= (uintptr_t)ptr - (uintptr_t)env)
1256 return;
1257 } while (*ptr != '\0');
1261 * 1st pass at building the table of boot properties. This includes:
1262 * - values set on the command line: -B a=x,b=y,c=z ....
1263 * - known values we just compute (ie. from xbp)
1264 * - values from /boot/solaris/bootenv.rc (ie. eeprom(1m) values)
1266 * the grub command line looked like:
1267 * kernel boot-file [-B prop=value[,prop=value]...] [boot-args]
1269 * whoami is the same as boot-file
1271 static void
1272 build_boot_properties(struct xboot_info *xbp)
1274 char *name;
1275 int name_len;
1276 char *value;
1277 int value_len;
1278 struct boot_modules *bm, *rdbm, *benv = NULL;
1279 char *propbuf;
1280 int quoted = 0;
1281 int boot_arg_len;
1282 uint_t i, midx;
1283 char modid[32];
1284 static int stdout_val = 0;
1285 uchar_t boot_device;
1286 char str[3];
1289 * These have to be done first, so that kobj_mount_root() works
1291 DBG_MSG("Building boot properties\n");
1292 propbuf = do_bsys_alloc(NULL, NULL, MMU_PAGESIZE, 0);
1293 DBG((uintptr_t)propbuf);
1294 if (xbp->bi_module_cnt > 0) {
1295 bm = xbp->bi_modules;
1296 rdbm = NULL;
1297 for (midx = i = 0; i < xbp->bi_module_cnt; i++) {
1298 if (bm[i].bm_type == BMT_ROOTFS) {
1299 rdbm = &bm[i];
1300 continue;
1302 if (bm[i].bm_type == BMT_HASH || bm[i].bm_name == NULL)
1303 continue;
1305 if (bm[i].bm_type == BMT_ENV) {
1306 if (benv == NULL)
1307 benv = &bm[i];
1308 else
1309 continue;
1312 (void) snprintf(modid, sizeof (modid),
1313 "module-name-%u", midx);
1314 bsetprops(modid, (char *)bm[i].bm_name);
1315 (void) snprintf(modid, sizeof (modid),
1316 "module-addr-%u", midx);
1317 bsetprop64(modid, (uint64_t)(uintptr_t)bm[i].bm_addr);
1318 (void) snprintf(modid, sizeof (modid),
1319 "module-size-%u", midx);
1320 bsetprop64(modid, (uint64_t)bm[i].bm_size);
1321 ++midx;
1323 if (rdbm != NULL) {
1324 bsetprop64("ramdisk_start",
1325 (uint64_t)(uintptr_t)rdbm->bm_addr);
1326 bsetprop64("ramdisk_end",
1327 (uint64_t)(uintptr_t)rdbm->bm_addr + rdbm->bm_size);
1332 * If there are any boot time modules or hashes present, then disable
1333 * fast reboot.
1335 if (xbp->bi_module_cnt > 1) {
1336 fastreboot_disable(FBNS_BOOTMOD);
1340 * Disable fast reboot if we're using the Multiboot 2 boot protocol,
1341 * since we don't currently support MB2 info and module relocation.
1342 * Note that fast reboot will have already been disabled if multiple
1343 * modules are present, since the current implementation assumes that
1344 * we only have a single module, the boot_archive.
1346 if (xbp->bi_mb_version != 1) {
1347 fastreboot_disable(FBNS_MULTIBOOT2);
1350 DBG_MSG("Parsing command line for boot properties\n");
1351 value = xbp->bi_cmdline;
1354 * allocate memory to collect boot_args into
1356 boot_arg_len = strlen(xbp->bi_cmdline) + 1;
1357 boot_args = do_bsys_alloc(NULL, NULL, boot_arg_len, MMU_PAGESIZE);
1358 boot_args[0] = 0;
1359 boot_arg_len = 0;
1362 while (ISSPACE(*value))
1363 ++value;
1365 * value now points at the boot-file
1367 value_len = 0;
1368 while (value[value_len] && !ISSPACE(value[value_len]))
1369 ++value_len;
1370 if (value_len > 0) {
1371 whoami = propbuf;
1372 bcopy(value, whoami, value_len);
1373 whoami[value_len] = 0;
1374 bsetprops("boot-file", whoami);
1376 * strip leading path stuff from whoami, so running from
1377 * PXE/miniroot makes sense.
1379 if (strstr(whoami, "/platform/") != NULL)
1380 whoami = strstr(whoami, "/platform/");
1381 bsetprops("whoami", whoami);
1385 * Values forcibly set boot properties on the command line via -B.
1386 * Allow use of quotes in values. Other stuff goes on kernel
1387 * command line.
1389 name = value + value_len;
1390 while (*name != 0) {
1392 * anything not " -B" is copied to the command line
1394 if (!ISSPACE(name[0]) || name[1] != '-' || name[2] != 'B') {
1395 boot_args[boot_arg_len++] = *name;
1396 boot_args[boot_arg_len] = 0;
1397 ++name;
1398 continue;
1402 * skip the " -B" and following white space
1404 name += 3;
1405 while (ISSPACE(*name))
1406 ++name;
1407 while (*name && !ISSPACE(*name)) {
1408 value = strstr(name, "=");
1409 if (value == NULL)
1410 break;
1411 name_len = value - name;
1412 ++value;
1413 value_len = 0;
1414 quoted = 0;
1415 for (; ; ++value_len) {
1416 if (!value[value_len])
1417 break;
1420 * is this value quoted?
1422 if (value_len == 0 &&
1423 (value[0] == '\'' || value[0] == '"')) {
1424 quoted = value[0];
1425 ++value_len;
1429 * In the quote accept any character,
1430 * but look for ending quote.
1432 if (quoted) {
1433 if (value[value_len] == quoted)
1434 quoted = 0;
1435 continue;
1439 * a comma or white space ends the value
1441 if (value[value_len] == ',' ||
1442 ISSPACE(value[value_len]))
1443 break;
1446 if (value_len == 0) {
1447 bsetprop(DDI_PROP_TYPE_ANY, name, name_len,
1448 NULL, 0);
1449 } else {
1450 char *v = value;
1451 int l = value_len;
1452 if (v[0] == v[l - 1] &&
1453 (v[0] == '\'' || v[0] == '"')) {
1454 ++v;
1455 l -= 2;
1457 bcopy(v, propbuf, l);
1458 propbuf[l] = '\0';
1459 bsetprop(DDI_PROP_TYPE_STRING, name, name_len,
1460 propbuf, l + 1);
1462 name = value + value_len;
1463 while (*name == ',')
1464 ++name;
1469 * set boot-args property
1470 * 1275 name is bootargs, so set
1471 * that too
1473 bsetprops("boot-args", boot_args);
1474 bsetprops("bootargs", boot_args);
1476 process_boot_environment(benv);
1479 * Build boot command line for Fast Reboot
1481 build_fastboot_cmdline(xbp);
1483 if (xbp->bi_mb_version == 1) {
1484 multiboot_info_t *mbi = xbp->bi_mb_info;
1485 int netboot;
1486 struct sol_netinfo *sip;
1489 * set the BIOS boot device from GRUB
1491 netboot = 0;
1494 * Save various boot information for Fast Reboot
1496 save_boot_info(xbp);
1498 if (mbi != NULL && mbi->flags & MB_INFO_BOOTDEV) {
1499 boot_device = mbi->boot_device >> 24;
1500 if (boot_device == 0x20)
1501 netboot++;
1502 str[0] = (boot_device >> 4) + '0';
1503 str[1] = (boot_device & 0xf) + '0';
1504 str[2] = 0;
1505 bsetprops("bios-boot-device", str);
1506 } else {
1507 netboot = 1;
1511 * In the netboot case, drives_info is overloaded with the
1512 * dhcp ack. This is not multiboot compliant and requires
1513 * special pxegrub!
1515 if (netboot && mbi->drives_length != 0) {
1516 sip = (struct sol_netinfo *)(uintptr_t)mbi->drives_addr;
1517 if (sip->sn_infotype == SN_TYPE_BOOTP)
1518 bsetprop(DDI_PROP_TYPE_BYTE,
1519 "bootp-response",
1520 sizeof ("bootp-response"),
1521 (void *)(uintptr_t)mbi->drives_addr,
1522 mbi->drives_length);
1523 else if (sip->sn_infotype == SN_TYPE_RARP)
1524 setup_rarp_props(sip);
1526 } else {
1527 multiboot2_info_header_t *mbi = xbp->bi_mb_info;
1528 multiboot_tag_bootdev_t *bootdev = NULL;
1529 multiboot_tag_network_t *netdev = NULL;
1531 if (mbi != NULL) {
1532 bootdev = dboot_multiboot2_find_tag(mbi,
1533 MULTIBOOT_TAG_TYPE_BOOTDEV);
1534 netdev = dboot_multiboot2_find_tag(mbi,
1535 MULTIBOOT_TAG_TYPE_NETWORK);
1537 if (bootdev != NULL) {
1538 DBG(bootdev->mb_biosdev);
1539 boot_device = bootdev->mb_biosdev;
1540 str[0] = (boot_device >> 4) + '0';
1541 str[1] = (boot_device & 0xf) + '0';
1542 str[2] = 0;
1543 bsetprops("bios-boot-device", str);
1545 if (netdev != NULL) {
1546 bsetprop(DDI_PROP_TYPE_BYTE,
1547 "bootp-response", sizeof ("bootp-response"),
1548 (void *)(uintptr_t)netdev->mb_dhcpack,
1549 netdev->mb_size -
1550 sizeof (multiboot_tag_network_t));
1554 bsetprop32("stdout", stdout_val);
1557 * more conjured up values for made up things....
1559 bsetprops("mfg-name", "i86pc");
1560 bsetprops("impl-arch-name", "i86pc");
1563 * Build firmware-provided system properties
1565 build_firmware_properties(xbp);
1568 * XXPV
1570 * Find out what these are:
1571 * - cpuid_feature_ecx_include
1572 * - cpuid_feature_ecx_exclude
1573 * - cpuid_feature_edx_include
1574 * - cpuid_feature_edx_exclude
1576 * Find out what these are in multiboot:
1577 * - netdev-path
1578 * - fstype
1584 * simple description of a stack frame (args are 32 bit only currently)
1586 typedef struct bop_frame {
1587 struct bop_frame *old_frame;
1588 pc_t retaddr;
1589 long arg[1];
1590 } bop_frame_t;
1592 void
1593 bop_traceback(bop_frame_t *frame)
1595 pc_t pc;
1596 int cnt;
1597 char *ksym;
1598 ulong_t off;
1600 bop_printf(NULL, "Stack traceback:\n");
1601 for (cnt = 0; cnt < 30; ++cnt) { /* up to 30 frames */
1602 pc = frame->retaddr;
1603 if (pc == 0)
1604 break;
1605 ksym = kobj_getsymname(pc, &off);
1606 if (ksym)
1607 bop_printf(NULL, " %s+%lx", ksym, off);
1608 else
1609 bop_printf(NULL, " 0x%lx", pc);
1611 frame = frame->old_frame;
1612 if (frame == 0) {
1613 bop_printf(NULL, "\n");
1614 break;
1616 bop_printf(NULL, "\n");
1620 struct trapframe {
1621 ulong_t error_code; /* optional */
1622 ulong_t inst_ptr;
1623 ulong_t code_seg;
1624 ulong_t flags_reg;
1625 ulong_t stk_ptr;
1626 ulong_t stk_seg;
1629 void
1630 bop_trap(ulong_t *tfp)
1632 struct trapframe *tf = (struct trapframe *)tfp;
1633 bop_frame_t fakeframe;
1634 static int depth = 0;
1637 * Check for an infinite loop of traps.
1639 if (++depth > 2)
1640 bop_panic("Nested trap");
1642 bop_printf(NULL, "Unexpected trap\n");
1645 * adjust the tf for optional error_code by detecting the code selector
1647 if (tf->code_seg != B64CODE_SEL)
1648 tf = (struct trapframe *)(tfp - 1);
1649 else
1650 bop_printf(NULL, "error code 0x%lx\n",
1651 tf->error_code & 0xffffffff);
1653 bop_printf(NULL, "instruction pointer 0x%lx\n", tf->inst_ptr);
1654 bop_printf(NULL, "code segment 0x%lx\n", tf->code_seg & 0xffff);
1655 bop_printf(NULL, "flags register 0x%lx\n", tf->flags_reg);
1656 bop_printf(NULL, "return %%rsp 0x%lx\n", tf->stk_ptr);
1657 bop_printf(NULL, "return %%ss 0x%lx\n", tf->stk_seg & 0xffff);
1659 /* grab %[er]bp pushed by our code from the stack */
1660 fakeframe.old_frame = (bop_frame_t *)*(tfp - 3);
1661 fakeframe.retaddr = (pc_t)tf->inst_ptr;
1662 bop_printf(NULL, "Attempting stack backtrace:\n");
1663 bop_traceback(&fakeframe);
1664 bop_panic("unexpected trap in early boot");
1667 extern void bop_trap_handler(void);
1669 static gate_desc_t *bop_idt;
1671 static desctbr_t bop_idt_info;
1674 * Install a temporary IDT that lets us catch errors in the boot time code.
1675 * We shouldn't get any faults at all while this is installed, so we'll
1676 * just generate a traceback and exit.
1678 static void
1679 bop_idt_init(void)
1681 int t;
1683 bop_idt = (gate_desc_t *)
1684 do_bsys_alloc(NULL, NULL, MMU_PAGESIZE, MMU_PAGESIZE);
1685 bzero(bop_idt, MMU_PAGESIZE);
1686 for (t = 0; t < NIDT; ++t) {
1688 * Note that since boot runs without a TSS, the
1689 * double fault handler cannot use an alternate stack (64-bit).
1691 set_gatesegd(&bop_idt[t], &bop_trap_handler, B64CODE_SEL,
1692 SDT_SYSIGT, TRP_KPL, 0);
1694 bop_idt_info.dtr_limit = (NIDT * sizeof (gate_desc_t)) - 1;
1695 bop_idt_info.dtr_base = (uintptr_t)bop_idt;
1696 wr_idtr(&bop_idt_info);
1700 * This is where we enter the kernel. It dummies up the boot_ops and
1701 * boot_syscalls vectors and jumps off to _kobj_boot()
1703 void
1704 _start(struct xboot_info *xbp)
1706 bootops_t *bops = &bootop;
1707 extern void _kobj_boot();
1710 * 1st off - initialize the console for any error messages
1712 xbootp = xbp;
1714 if (*((uint32_t *)(FASTBOOT_SWTCH_PA + FASTBOOT_STACK_OFFSET)) ==
1715 FASTBOOT_MAGIC) {
1716 post_fastreboot = 1;
1717 *((uint32_t *)(FASTBOOT_SWTCH_PA + FASTBOOT_STACK_OFFSET)) = 0;
1720 bcons_init(xbp);
1721 have_console = 1;
1724 * enable debugging
1726 if (find_boot_prop("kbm_debug") != NULL)
1727 kbm_debug = 1;
1729 DBG_MSG("\n\n*** Entered Solaris in _start() cmdline is: ");
1730 DBG_MSG((char *)xbp->bi_cmdline);
1731 DBG_MSG("\n\n\n");
1734 * physavail is no longer used by startup
1736 bm.physinstalled = xbp->bi_phys_install;
1737 bm.pcimem = xbp->bi_pcimem;
1738 bm.rsvdmem = xbp->bi_rsvdmem;
1739 bm.physavail = NULL;
1742 * initialize the boot time allocator
1744 next_phys = xbp->bi_next_paddr;
1745 DBG(next_phys);
1746 next_virt = (uintptr_t)xbp->bi_next_vaddr;
1747 DBG(next_virt);
1748 DBG_MSG("Initializing boot time memory management...");
1749 kbm_init(xbp);
1750 DBG_MSG("done\n");
1753 * Fill in the bootops vector
1755 bops->bsys_version = BO_VERSION;
1756 bops->boot_mem = &bm;
1757 bops->bsys_alloc = do_bsys_alloc;
1758 bops->bsys_free = do_bsys_free;
1759 bops->bsys_getproplen = do_bsys_getproplen;
1760 bops->bsys_getprop = do_bsys_getprop;
1761 bops->bsys_nextprop = do_bsys_nextprop;
1762 bops->bsys_printf = bop_printf;
1763 bops->bsys_doint = do_bsys_doint;
1766 * BOP_EALLOC() is no longer needed
1768 bops->bsys_ealloc = do_bsys_ealloc;
1772 * Install an IDT to catch early pagefaults (shouldn't have any).
1773 * Also needed for kmdb.
1775 bop_idt_init();
1778 * Start building the boot properties from the command line
1780 DBG_MSG("Initializing boot properties:\n");
1781 build_boot_properties(xbp);
1783 if (find_boot_prop("prom_debug") || kbm_debug) {
1784 char *value;
1786 value = do_bsys_alloc(NULL, NULL, MMU_PAGESIZE, MMU_PAGESIZE);
1787 boot_prop_display(value);
1791 * jump into krtld...
1793 _kobj_boot(&bop_sysp, NULL, bops, NULL);
1797 /*ARGSUSED*/
1798 static caddr_t
1799 no_more_alloc(bootops_t *bop, caddr_t virthint, size_t size, int align)
1801 panic("Attempt to bsys_alloc() too late\n");
1802 return (NULL);
1805 /*ARGSUSED*/
1806 static void
1807 no_more_free(bootops_t *bop, caddr_t virt, size_t size)
1809 panic("Attempt to bsys_free() too late\n");
1812 void
1813 bop_no_more_mem(void)
1815 DBG(total_bop_alloc_scratch);
1816 DBG(total_bop_alloc_kernel);
1817 bootops->bsys_alloc = no_more_alloc;
1818 bootops->bsys_free = no_more_free;
1823 * Set ACPI firmware properties
1826 static caddr_t
1827 vmap_phys(size_t length, paddr_t pa)
1829 paddr_t start, end;
1830 caddr_t va;
1831 size_t len, page;
1833 start = P2ALIGN(pa, MMU_PAGESIZE);
1834 end = P2ROUNDUP(pa + length, MMU_PAGESIZE);
1835 len = end - start;
1836 va = (caddr_t)alloc_vaddr(len, MMU_PAGESIZE);
1837 for (page = 0; page < len; page += MMU_PAGESIZE)
1838 kbm_map((uintptr_t)va + page, start + page, 0, 0);
1839 return (va + (pa & MMU_PAGEOFFSET));
1842 static uint8_t
1843 checksum_table(uint8_t *tp, size_t len)
1845 uint8_t sum = 0;
1847 while (len-- > 0)
1848 sum += *tp++;
1850 return (sum);
1853 static int
1854 valid_rsdp(ACPI_TABLE_RSDP *rp)
1857 /* validate the V1.x checksum */
1858 if (checksum_table((uint8_t *)rp, ACPI_RSDP_CHECKSUM_LENGTH) != 0)
1859 return (0);
1861 /* If pre-ACPI 2.0, this is a valid RSDP */
1862 if (rp->Revision < 2)
1863 return (1);
1865 /* validate the V2.x checksum */
1866 if (checksum_table((uint8_t *)rp, ACPI_RSDP_XCHECKSUM_LENGTH) != 0)
1867 return (0);
1869 return (1);
1873 * Scan memory range for an RSDP;
1874 * see ACPI 3.0 Spec, 5.2.5.1
1876 static ACPI_TABLE_RSDP *
1877 scan_rsdp(paddr_t start, paddr_t end)
1879 ssize_t len = end - start;
1880 caddr_t ptr;
1882 ptr = vmap_phys(len, start);
1883 while (len > 0) {
1884 if (strncmp(ptr, ACPI_SIG_RSDP, strlen(ACPI_SIG_RSDP)) == 0 &&
1885 valid_rsdp((ACPI_TABLE_RSDP *)ptr))
1886 return ((ACPI_TABLE_RSDP *)ptr);
1888 ptr += ACPI_RSDP_SCAN_STEP;
1889 len -= ACPI_RSDP_SCAN_STEP;
1892 return (NULL);
1896 * Refer to ACPI 3.0 Spec, section 5.2.5.1 to understand this function
1898 static ACPI_TABLE_RSDP *
1899 find_rsdp()
1901 ACPI_TABLE_RSDP *rsdp;
1902 uint64_t rsdp_val = 0;
1903 uint16_t *ebda_seg;
1904 paddr_t ebda_addr;
1906 /* check for "acpi-root-tab" property */
1907 if (do_bsys_getproplen(NULL, "acpi-root-tab") == sizeof (uint64_t)) {
1908 (void) do_bsys_getprop(NULL, "acpi-root-tab", &rsdp_val);
1909 if (rsdp_val != 0) {
1910 rsdp = scan_rsdp(rsdp_val, rsdp_val + sizeof (*rsdp));
1911 if (rsdp != NULL) {
1912 if (kbm_debug) {
1913 bop_printf(NULL,
1914 "Using RSDP from bootloader: "
1915 "0x%p\n", (void *)rsdp);
1917 return (rsdp);
1923 * Get the EBDA segment and scan the first 1K
1925 ebda_seg = (uint16_t *)vmap_phys(sizeof (uint16_t),
1926 ACPI_EBDA_PTR_LOCATION);
1927 ebda_addr = *ebda_seg << 4;
1928 rsdp = scan_rsdp(ebda_addr, ebda_addr + ACPI_EBDA_WINDOW_SIZE);
1929 if (rsdp == NULL)
1930 /* if EBDA doesn't contain RSDP, look in BIOS memory */
1931 rsdp = scan_rsdp(ACPI_HI_RSDP_WINDOW_BASE,
1932 ACPI_HI_RSDP_WINDOW_BASE + ACPI_HI_RSDP_WINDOW_SIZE);
1933 return (rsdp);
1936 static ACPI_TABLE_HEADER *
1937 map_fw_table(paddr_t table_addr)
1939 ACPI_TABLE_HEADER *tp;
1940 size_t len = MAX(sizeof (*tp), MMU_PAGESIZE);
1943 * Map at least a page; if the table is larger than this, remap it
1945 tp = (ACPI_TABLE_HEADER *)vmap_phys(len, table_addr);
1946 if (tp->Length > len)
1947 tp = (ACPI_TABLE_HEADER *)vmap_phys(tp->Length, table_addr);
1948 return (tp);
1951 static ACPI_TABLE_HEADER *
1952 find_fw_table(char *signature)
1954 static int revision = 0;
1955 static ACPI_TABLE_XSDT *xsdt;
1956 static int len;
1957 paddr_t xsdt_addr;
1958 ACPI_TABLE_RSDP *rsdp;
1959 ACPI_TABLE_HEADER *tp;
1960 paddr_t table_addr;
1961 int n;
1963 if (strlen(signature) != ACPI_NAME_SIZE)
1964 return (NULL);
1967 * Reading the ACPI 3.0 Spec, section 5.2.5.3 will help
1968 * understand this code. If we haven't already found the RSDT/XSDT,
1969 * revision will be 0. Find the RSDP and check the revision
1970 * to find out whether to use the RSDT or XSDT. If revision is
1971 * 0 or 1, use the RSDT and set internal revision to 1; if it is 2,
1972 * use the XSDT. If the XSDT address is 0, though, fall back to
1973 * revision 1 and use the RSDT.
1975 if (revision == 0) {
1976 if ((rsdp = find_rsdp()) != NULL) {
1977 revision = rsdp->Revision;
1979 * ACPI 6.0 states that current revision is 2
1980 * from acpi_table_rsdp definition:
1981 * Must be (0) for ACPI 1.0 or (2) for ACPI 2.0+
1983 if (revision > 2)
1984 revision = 2;
1985 switch (revision) {
1986 case 2:
1988 * Use the XSDT unless BIOS is buggy and
1989 * claims to be rev 2 but has a null XSDT
1990 * address
1992 xsdt_addr = rsdp->XsdtPhysicalAddress;
1993 if (xsdt_addr != 0)
1994 break;
1995 /* FALLTHROUGH */
1996 case 0:
1997 /* treat RSDP rev 0 as revision 1 internally */
1998 revision = 1;
1999 /* FALLTHROUGH */
2000 case 1:
2001 /* use the RSDT for rev 0/1 */
2002 xsdt_addr = rsdp->RsdtPhysicalAddress;
2003 break;
2004 default:
2005 /* unknown revision */
2006 revision = 0;
2007 break;
2010 if (revision == 0)
2011 return (NULL);
2013 /* cache the XSDT info */
2014 xsdt = (ACPI_TABLE_XSDT *)map_fw_table(xsdt_addr);
2015 len = (xsdt->Header.Length - sizeof (xsdt->Header)) /
2016 ((revision == 1) ? sizeof (uint32_t) : sizeof (uint64_t));
2020 * Scan the table headers looking for a signature match
2022 for (n = 0; n < len; n++) {
2023 ACPI_TABLE_RSDT *rsdt = (ACPI_TABLE_RSDT *)xsdt;
2024 table_addr = (revision == 1) ? rsdt->TableOffsetEntry[n] :
2025 xsdt->TableOffsetEntry[n];
2027 if (table_addr == 0)
2028 continue;
2029 tp = map_fw_table(table_addr);
2030 if (strncmp(tp->Signature, signature, ACPI_NAME_SIZE) == 0) {
2031 return (tp);
2034 return (NULL);
2037 static void
2038 process_mcfg(ACPI_TABLE_MCFG *tp)
2040 ACPI_MCFG_ALLOCATION *cfg_baap;
2041 char *cfg_baa_endp;
2042 int64_t ecfginfo[4];
2044 cfg_baap = (ACPI_MCFG_ALLOCATION *)((uintptr_t)tp + sizeof (*tp));
2045 cfg_baa_endp = ((char *)tp) + tp->Header.Length;
2046 while ((char *)cfg_baap < cfg_baa_endp) {
2047 if (cfg_baap->Address != 0 && cfg_baap->PciSegment == 0) {
2048 ecfginfo[0] = cfg_baap->Address;
2049 ecfginfo[1] = cfg_baap->PciSegment;
2050 ecfginfo[2] = cfg_baap->StartBusNumber;
2051 ecfginfo[3] = cfg_baap->EndBusNumber;
2052 bsetprop(DDI_PROP_TYPE_INT64,
2053 MCFG_PROPNAME, strlen(MCFG_PROPNAME),
2054 ecfginfo, sizeof (ecfginfo));
2055 break;
2057 cfg_baap++;
2061 static void
2062 process_madt_entries(ACPI_TABLE_MADT *tp, uint32_t *cpu_countp,
2063 uint32_t *cpu_possible_countp, uint32_t *cpu_apicid_array)
2065 ACPI_SUBTABLE_HEADER *item, *end;
2066 uint32_t cpu_count = 0;
2067 uint32_t cpu_possible_count = 0;
2070 * Determine number of CPUs and keep track of "final" APIC ID
2071 * for each CPU by walking through ACPI MADT processor list
2073 end = (ACPI_SUBTABLE_HEADER *)(tp->Header.Length + (uintptr_t)tp);
2074 item = (ACPI_SUBTABLE_HEADER *)((uintptr_t)tp + sizeof (*tp));
2076 while (item < end) {
2077 switch (item->Type) {
2078 case ACPI_MADT_TYPE_LOCAL_APIC: {
2079 ACPI_MADT_LOCAL_APIC *cpu =
2080 (ACPI_MADT_LOCAL_APIC *) item;
2082 if (cpu->LapicFlags & ACPI_MADT_ENABLED) {
2083 if (cpu_apicid_array != NULL)
2084 cpu_apicid_array[cpu_count] = cpu->Id;
2085 cpu_count++;
2087 cpu_possible_count++;
2088 break;
2090 case ACPI_MADT_TYPE_LOCAL_X2APIC: {
2091 ACPI_MADT_LOCAL_X2APIC *cpu =
2092 (ACPI_MADT_LOCAL_X2APIC *) item;
2094 if (cpu->LapicFlags & ACPI_MADT_ENABLED) {
2095 if (cpu_apicid_array != NULL)
2096 cpu_apicid_array[cpu_count] =
2097 cpu->LocalApicId;
2098 cpu_count++;
2100 cpu_possible_count++;
2101 break;
2103 default:
2104 if (kbm_debug)
2105 bop_printf(NULL, "MADT type %d\n", item->Type);
2106 break;
2109 item = (ACPI_SUBTABLE_HEADER *)((uintptr_t)item + item->Length);
2111 if (cpu_countp)
2112 *cpu_countp = cpu_count;
2113 if (cpu_possible_countp)
2114 *cpu_possible_countp = cpu_possible_count;
2117 static void
2118 process_madt(ACPI_TABLE_MADT *tp)
2120 uint32_t cpu_count = 0;
2121 uint32_t cpu_possible_count = 0;
2122 uint32_t *cpu_apicid_array; /* x2APIC ID is 32bit! */
2124 if (tp != NULL) {
2125 /* count cpu's */
2126 process_madt_entries(tp, &cpu_count, &cpu_possible_count, NULL);
2128 cpu_apicid_array = (uint32_t *)do_bsys_alloc(NULL, NULL,
2129 cpu_count * sizeof (*cpu_apicid_array), MMU_PAGESIZE);
2130 if (cpu_apicid_array == NULL)
2131 bop_panic("Not enough memory for APIC ID array");
2133 /* copy IDs */
2134 process_madt_entries(tp, NULL, NULL, cpu_apicid_array);
2137 * Make boot property for array of "final" APIC IDs for each
2138 * CPU
2140 bsetprop(DDI_PROP_TYPE_INT,
2141 BP_CPU_APICID_ARRAY, strlen(BP_CPU_APICID_ARRAY),
2142 cpu_apicid_array, cpu_count * sizeof (*cpu_apicid_array));
2146 * Check whether property plat-max-ncpus is already set.
2148 if (do_bsys_getproplen(NULL, PLAT_MAX_NCPUS_NAME) < 0) {
2150 * Set plat-max-ncpus to number of maximum possible CPUs given
2151 * in MADT if it hasn't been set.
2152 * There's no formal way to detect max possible CPUs supported
2153 * by platform according to ACPI spec3.0b. So current CPU
2154 * hotplug implementation expects that all possible CPUs will
2155 * have an entry in MADT table and set plat-max-ncpus to number
2156 * of entries in MADT.
2157 * With introducing of ACPI4.0, Maximum System Capability Table
2158 * (MSCT) provides maximum number of CPUs supported by platform.
2159 * If MSCT is unavailable, fall back to old way.
2161 if (tp != NULL)
2162 bsetpropsi(PLAT_MAX_NCPUS_NAME, cpu_possible_count);
2166 * Set boot property boot-max-ncpus to number of CPUs existing at
2167 * boot time. boot-max-ncpus is mainly used for optimization.
2169 if (tp != NULL)
2170 bsetpropsi(BOOT_MAX_NCPUS_NAME, cpu_count);
2173 * User-set boot-ncpus overrides firmware count
2175 if (do_bsys_getproplen(NULL, BOOT_NCPUS_NAME) >= 0)
2176 return;
2179 * Set boot property boot-ncpus to number of active CPUs given in MADT
2180 * if it hasn't been set yet.
2182 if (tp != NULL)
2183 bsetpropsi(BOOT_NCPUS_NAME, cpu_count);
2186 static void
2187 process_srat(ACPI_TABLE_SRAT *tp)
2189 ACPI_SUBTABLE_HEADER *item, *end;
2190 int i;
2191 int proc_num, mem_num;
2192 #pragma pack(1)
2193 struct {
2194 uint32_t domain;
2195 uint32_t apic_id;
2196 uint32_t sapic_id;
2197 } processor;
2198 struct {
2199 uint32_t domain;
2200 uint32_t x2apic_id;
2201 } x2apic;
2202 struct {
2203 uint32_t domain;
2204 uint64_t addr;
2205 uint64_t length;
2206 uint32_t flags;
2207 } memory;
2208 #pragma pack()
2209 char prop_name[30];
2210 uint64_t maxmem = 0;
2212 if (tp == NULL)
2213 return;
2215 proc_num = mem_num = 0;
2216 end = (ACPI_SUBTABLE_HEADER *)(tp->Header.Length + (uintptr_t)tp);
2217 item = (ACPI_SUBTABLE_HEADER *)((uintptr_t)tp + sizeof (*tp));
2218 while (item < end) {
2219 switch (item->Type) {
2220 case ACPI_SRAT_TYPE_CPU_AFFINITY: {
2221 ACPI_SRAT_CPU_AFFINITY *cpu =
2222 (ACPI_SRAT_CPU_AFFINITY *) item;
2224 if (!(cpu->Flags & ACPI_SRAT_CPU_ENABLED))
2225 break;
2226 processor.domain = cpu->ProximityDomainLo;
2227 for (i = 0; i < 3; i++)
2228 processor.domain +=
2229 cpu->ProximityDomainHi[i] << ((i + 1) * 8);
2230 processor.apic_id = cpu->ApicId;
2231 processor.sapic_id = cpu->LocalSapicEid;
2232 (void) snprintf(prop_name, 30, "acpi-srat-processor-%d",
2233 proc_num);
2234 bsetprop(DDI_PROP_TYPE_INT,
2235 prop_name, strlen(prop_name), &processor,
2236 sizeof (processor));
2237 proc_num++;
2238 break;
2240 case ACPI_SRAT_TYPE_MEMORY_AFFINITY: {
2241 ACPI_SRAT_MEM_AFFINITY *mem =
2242 (ACPI_SRAT_MEM_AFFINITY *)item;
2244 if (!(mem->Flags & ACPI_SRAT_MEM_ENABLED))
2245 break;
2246 memory.domain = mem->ProximityDomain;
2247 memory.addr = mem->BaseAddress;
2248 memory.length = mem->Length;
2249 memory.flags = mem->Flags;
2250 (void) snprintf(prop_name, 30, "acpi-srat-memory-%d",
2251 mem_num);
2252 bsetprop(DDI_PROP_TYPE_INT,
2253 prop_name, strlen(prop_name), &memory,
2254 sizeof (memory));
2255 if ((mem->Flags & ACPI_SRAT_MEM_HOT_PLUGGABLE) &&
2256 (memory.addr + memory.length > maxmem)) {
2257 maxmem = memory.addr + memory.length;
2259 mem_num++;
2260 break;
2262 case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY: {
2263 ACPI_SRAT_X2APIC_CPU_AFFINITY *x2cpu =
2264 (ACPI_SRAT_X2APIC_CPU_AFFINITY *) item;
2266 if (!(x2cpu->Flags & ACPI_SRAT_CPU_ENABLED))
2267 break;
2268 x2apic.domain = x2cpu->ProximityDomain;
2269 x2apic.x2apic_id = x2cpu->ApicId;
2270 (void) snprintf(prop_name, 30, "acpi-srat-processor-%d",
2271 proc_num);
2272 bsetprop(DDI_PROP_TYPE_INT,
2273 prop_name, strlen(prop_name), &x2apic,
2274 sizeof (x2apic));
2275 proc_num++;
2276 break;
2278 default:
2279 if (kbm_debug)
2280 bop_printf(NULL, "SRAT type %d\n", item->Type);
2281 break;
2284 item = (ACPI_SUBTABLE_HEADER *)
2285 (item->Length + (uintptr_t)item);
2289 * The maximum physical address calculated from the SRAT table is more
2290 * accurate than that calculated from the MSCT table.
2292 if (maxmem != 0) {
2293 plat_dr_physmax = btop(maxmem);
2297 static void
2298 process_slit(ACPI_TABLE_SLIT *tp)
2302 * Check the number of localities; if it's too huge, we just
2303 * return and locality enumeration code will handle this later,
2304 * if possible.
2306 * Note that the size of the table is the square of the
2307 * number of localities; if the number of localities exceeds
2308 * UINT16_MAX, the table size may overflow an int when being
2309 * passed to bsetprop() below.
2311 if (tp->LocalityCount >= SLIT_LOCALITIES_MAX)
2312 return;
2314 bsetprop64(SLIT_NUM_PROPNAME, tp->LocalityCount);
2315 bsetprop(DDI_PROP_TYPE_BYTE,
2316 SLIT_PROPNAME, strlen(SLIT_PROPNAME), &tp->Entry,
2317 tp->LocalityCount * tp->LocalityCount);
2320 static ACPI_TABLE_MSCT *
2321 process_msct(ACPI_TABLE_MSCT *tp)
2323 int last_seen = 0;
2324 int proc_num = 0;
2325 ACPI_MSCT_PROXIMITY *item, *end;
2326 extern uint64_t plat_dr_options;
2328 ASSERT(tp != NULL);
2330 end = (ACPI_MSCT_PROXIMITY *)(tp->Header.Length + (uintptr_t)tp);
2331 for (item = (void *)((uintptr_t)tp + tp->ProximityOffset);
2332 item < end;
2333 item = (void *)(item->Length + (uintptr_t)item)) {
2335 * Sanity check according to section 5.2.19.1 of ACPI 4.0.
2336 * Revision 1
2337 * Length 22
2339 if (item->Revision != 1 || item->Length != 22) {
2340 cmn_err(CE_CONT,
2341 "?boot: unknown proximity domain structure in MSCT "
2342 "with Revision(%d), Length(%d).\n",
2343 (int)item->Revision, (int)item->Length);
2344 return (NULL);
2345 } else if (item->RangeStart > item->RangeEnd) {
2346 cmn_err(CE_CONT,
2347 "?boot: invalid proximity domain structure in MSCT "
2348 "with RangeStart(%u), RangeEnd(%u).\n",
2349 item->RangeStart, item->RangeEnd);
2350 return (NULL);
2351 } else if (item->RangeStart != last_seen) {
2353 * Items must be organized in ascending order of the
2354 * proximity domain enumerations.
2356 cmn_err(CE_CONT,
2357 "?boot: invalid proximity domain structure in MSCT,"
2358 " items are not orginized in ascending order.\n");
2359 return (NULL);
2363 * If ProcessorCapacity is 0 then there would be no CPUs in this
2364 * domain.
2366 if (item->ProcessorCapacity != 0) {
2367 proc_num += (item->RangeEnd - item->RangeStart + 1) *
2368 item->ProcessorCapacity;
2371 last_seen = item->RangeEnd - item->RangeStart + 1;
2373 * Break out if all proximity domains have been processed.
2374 * Some BIOSes may have unused items at the end of MSCT table.
2376 if (last_seen > tp->MaxProximityDomains) {
2377 break;
2380 if (last_seen != tp->MaxProximityDomains + 1) {
2381 cmn_err(CE_CONT,
2382 "?boot: invalid proximity domain structure in MSCT, "
2383 "proximity domain count doesn't match.\n");
2384 return (NULL);
2388 * Set plat-max-ncpus property if it hasn't been set yet.
2390 if (do_bsys_getproplen(NULL, PLAT_MAX_NCPUS_NAME) < 0) {
2391 if (proc_num != 0) {
2392 bsetpropsi(PLAT_MAX_NCPUS_NAME, proc_num);
2397 * Use Maximum Physical Address from the MSCT table as upper limit for
2398 * memory hot-adding by default. It may be overridden by value from
2399 * the SRAT table or the "plat-dr-physmax" boot option.
2401 plat_dr_physmax = btop(tp->MaxAddress + 1);
2404 * Existence of MSCT implies CPU/memory hotplug-capability for the
2405 * platform.
2407 plat_dr_options |= PLAT_DR_FEATURE_CPU;
2408 plat_dr_options |= PLAT_DR_FEATURE_MEMORY;
2410 return (tp);
2414 /*ARGSUSED*/
2415 static void
2416 build_firmware_properties(struct xboot_info *xbp)
2418 ACPI_TABLE_HEADER *tp = NULL;
2420 if (xbp->bi_uefi_arch == XBI_UEFI_ARCH_64) {
2421 bsetprops("efi-systype", "64");
2422 bsetprop64("efi-systab",
2423 (uint64_t)(uintptr_t)xbp->bi_uefi_systab);
2424 if (kbm_debug)
2425 bop_printf(NULL, "64-bit UEFI detected.\n");
2426 } else if (xbp->bi_uefi_arch == XBI_UEFI_ARCH_32) {
2427 bsetprops("efi-systype", "32");
2428 bsetprop64("efi-systab",
2429 (uint64_t)(uintptr_t)xbp->bi_uefi_systab);
2430 if (kbm_debug)
2431 bop_printf(NULL, "32-bit UEFI detected.\n");
2434 if (xbp->bi_acpi_rsdp != NULL) {
2435 bsetprop64("acpi-root-tab",
2436 (uint64_t)(uintptr_t)xbp->bi_acpi_rsdp);
2439 if (xbp->bi_smbios != NULL) {
2440 bsetprop64("smbios-address",
2441 (uint64_t)(uintptr_t)xbp->bi_smbios);
2444 if ((tp = find_fw_table(ACPI_SIG_MSCT)) != NULL)
2445 msct_ptr = process_msct((ACPI_TABLE_MSCT *)tp);
2446 else
2447 msct_ptr = NULL;
2449 if ((tp = find_fw_table(ACPI_SIG_MADT)) != NULL)
2450 process_madt((ACPI_TABLE_MADT *)tp);
2452 if ((srat_ptr = (ACPI_TABLE_SRAT *)
2453 find_fw_table(ACPI_SIG_SRAT)) != NULL)
2454 process_srat(srat_ptr);
2456 if (slit_ptr = (ACPI_TABLE_SLIT *)find_fw_table(ACPI_SIG_SLIT))
2457 process_slit(slit_ptr);
2459 tp = find_fw_table(ACPI_SIG_MCFG);
2460 if (tp != NULL)
2461 process_mcfg((ACPI_TABLE_MCFG *)tp);
2465 * fake up a boot property for deferred early console output
2466 * this is used by both graphical boot and the (developer only)
2467 * USB serial console
2469 void *
2470 defcons_init(size_t size)
2472 static char *p = NULL;
2474 p = do_bsys_alloc(NULL, NULL, size, MMU_PAGESIZE);
2475 *p = 0;
2476 bsetprop32("deferred-console-buf", (uint32_t)((uintptr_t)&p));
2477 return (p);
2480 /*ARGSUSED*/
2482 boot_compinfo(int fd, struct compinfo *cbp)
2484 cbp->iscmp = 0;
2485 cbp->blksize = MAXBSIZE;
2486 return (0);
2489 #define BP_MAX_STRLEN 32
2492 * Get value for given boot property
2495 bootprop_getval(const char *prop_name, u_longlong_t *prop_value)
2497 int boot_prop_len;
2498 char str[BP_MAX_STRLEN];
2499 u_longlong_t value;
2501 boot_prop_len = BOP_GETPROPLEN(bootops, prop_name);
2502 if (boot_prop_len < 0 || boot_prop_len > sizeof (str) ||
2503 BOP_GETPROP(bootops, prop_name, str) < 0 ||
2504 kobj_getvalue(str, &value) == -1)
2505 return (-1);
2507 if (prop_value)
2508 *prop_value = value;
2510 return (0);