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