xref: /titanic_44/usr/src/uts/i86pc/os/fakebop.c (revision a1e4879402b1a0a7c410577cff81f1ce31ace7af)
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 static 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 static 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 console was explicitly set on the command line it will
591 		 * override a setting in bootenv.rc
592 		 */
593 		if (strcmp(name, "console") == 0 &&
594 		    do_bsys_getproplen(NULL, "console") > 0)
595 			continue;
596 
597 		bsetprop(name, n_len, value, v_len + 1);
598 	}
599 done:
600 	if (fd >= 0)
601 		BRD_CLOSE(bfs_ops, fd);
602 
603 	/*
604 	 * Check if we have to limit the boot time allocator
605 	 */
606 	if (do_bsys_getproplen(NULL, "physmem") != -1 &&
607 	    do_bsys_getprop(NULL, "physmem", line) >= 0 &&
608 	    parse_value(line, &lvalue) != -1) {
609 		if (0 < lvalue && (lvalue < physmem || physmem == 0)) {
610 			physmem = (pgcnt_t)lvalue;
611 			DBG(physmem);
612 		}
613 	}
614 	early_allocation = 0;
615 
616 	/*
617 	 * check to see if we have to override the default value of the console
618 	 */
619 	inputdev = line;
620 	v_len = do_bsys_getproplen(NULL, "input-device");
621 	if (v_len > 0)
622 		(void) do_bsys_getprop(NULL, "input-device", inputdev);
623 	else
624 		v_len = 0;
625 	inputdev[v_len] = 0;
626 
627 	outputdev = inputdev + v_len + 1;
628 	v_len = do_bsys_getproplen(NULL, "output-device");
629 	if (v_len > 0)
630 		(void) do_bsys_getprop(NULL, "output-device", outputdev);
631 	else
632 		v_len = 0;
633 	outputdev[v_len] = 0;
634 
635 	consoledev = outputdev + v_len + 1;
636 	v_len = do_bsys_getproplen(NULL, "console");
637 	if (v_len > 0)
638 		(void) do_bsys_getprop(NULL, "console", consoledev);
639 	else
640 		v_len = 0;
641 	consoledev[v_len] = 0;
642 	bcons_init2(inputdev, outputdev, consoledev);
643 
644 	if (strstr((char *)xbootp->bi_cmdline, "prom_debug") || kbm_debug) {
645 		value = line;
646 		bop_printf(NULL, "\nBoot properties:\n");
647 		name = "";
648 		while ((name = do_bsys_nextprop(NULL, name)) != NULL) {
649 			bop_printf(NULL, "\t0x%p %s = ", (void *)name, name);
650 			(void) do_bsys_getprop(NULL, name, value);
651 			v_len = do_bsys_getproplen(NULL, name);
652 			bop_printf(NULL, "len=%d ", v_len);
653 			value[v_len] = 0;
654 			bop_printf(NULL, "%s\n", value);
655 		}
656 	}
657 }
658 
659 /*
660  * print formatted output
661  */
662 /*PRINTFLIKE2*/
663 /*ARGSUSED*/
664 void
665 bop_printf(bootops_t *bop, char *fmt, ...)
666 {
667 	va_list	ap;
668 
669 	if (have_console == 0)
670 		return;
671 
672 	va_start(ap, fmt);
673 	(void) vsnprintf(buffer, BUFFERSIZE, fmt, ap);
674 	va_end(ap);
675 	PUT_STRING(buffer);
676 }
677 
678 /*
679  * Another panic() variant; this one can be used even earlier during boot than
680  * prom_panic().
681  */
682 /*PRINTFLIKE1*/
683 void
684 bop_panic(char *fmt, ...)
685 {
686 	va_list ap;
687 
688 	va_start(ap, fmt);
689 	bop_printf(NULL, fmt, ap);
690 	va_end(ap);
691 
692 	bop_printf(NULL, "\nPress any key to reboot.\n");
693 	(void) bcons_getchar();
694 	bop_printf(NULL, "Resetting...\n");
695 	reset();
696 }
697 
698 /*
699  * Do a real mode interrupt BIOS call
700  */
701 typedef struct bios_regs {
702 	unsigned short ax, bx, cx, dx, si, di, bp, es, ds;
703 } bios_regs_t;
704 typedef int (*bios_func_t)(int, bios_regs_t *);
705 
706 /*ARGSUSED*/
707 static void
708 do_bsys_doint(bootops_t *bop, int intnum, struct bop_regs *rp)
709 {
710 	static int firsttime = 1;
711 	bios_func_t bios_func = (bios_func_t)(void *)(uintptr_t)0x5000;
712 	bios_regs_t br;
713 
714 	/*
715 	 * The first time we do this, we have to copy the pre-packaged
716 	 * low memory bios call code image into place.
717 	 */
718 	if (firsttime) {
719 		extern char bios_image[];
720 		extern uint32_t bios_size;
721 
722 		bcopy(bios_image, (void *)bios_func, bios_size);
723 		firsttime = 0;
724 	}
725 
726 	br.ax = rp->eax.word.ax;
727 	br.bx = rp->ebx.word.bx;
728 	br.cx = rp->ecx.word.cx;
729 	br.dx = rp->edx.word.dx;
730 	br.bp = rp->ebp.word.bp;
731 	br.si = rp->esi.word.si;
732 	br.di = rp->edi.word.di;
733 	br.ds = rp->ds;
734 	br.es = rp->es;
735 
736 	DBG_MSG("Doing BIOS call...");
737 	rp->eflags = bios_func(intnum, &br);
738 	DBG_MSG("done\n");
739 
740 	rp->eax.word.ax = br.ax;
741 	rp->ebx.word.bx = br.bx;
742 	rp->ecx.word.cx = br.cx;
743 	rp->edx.word.dx = br.dx;
744 	rp->ebp.word.bp = br.bp;
745 	rp->esi.word.si = br.si;
746 	rp->edi.word.di = br.di;
747 	rp->ds = br.ds;
748 	rp->es = br.es;
749 }
750 
751 static struct boot_syscalls bop_sysp = {
752 	bcons_getchar,
753 	bcons_putchar,
754 	bcons_ischar,
755 };
756 
757 static char *whoami;
758 
759 #define	BUFLEN	64
760 
761 static void
762 setup_rarp_props(struct sol_netinfo *sip)
763 {
764 	char buf[BUFLEN];	/* to hold ip/mac addrs */
765 	uint8_t *val;
766 
767 	val = (uint8_t *)&sip->sn_ciaddr;
768 	(void) snprintf(buf, BUFLEN, "%d.%d.%d.%d",
769 	    val[0], val[1], val[2], val[3]);
770 	bsetprops(BP_HOST_IP, buf);
771 
772 	val = (uint8_t *)&sip->sn_siaddr;
773 	(void) snprintf(buf, BUFLEN, "%d.%d.%d.%d",
774 	    val[0], val[1], val[2], val[3]);
775 	bsetprops(BP_SERVER_IP, buf);
776 
777 	if (sip->sn_giaddr != 0) {
778 		val = (uint8_t *)&sip->sn_giaddr;
779 		(void) snprintf(buf, BUFLEN, "%d.%d.%d.%d",
780 		    val[0], val[1], val[2], val[3]);
781 		bsetprops(BP_ROUTER_IP, buf);
782 	}
783 
784 	if (sip->sn_netmask != 0) {
785 		val = (uint8_t *)&sip->sn_netmask;
786 		(void) snprintf(buf, BUFLEN, "%d.%d.%d.%d",
787 		    val[0], val[1], val[2], val[3]);
788 		bsetprops(BP_SUBNET_MASK, buf);
789 	}
790 
791 	if (sip->sn_mactype != 4 || sip->sn_maclen != 6) {
792 		bop_printf(NULL, "unsupported mac type %d, mac len %d\n",
793 		    sip->sn_mactype, sip->sn_maclen);
794 	} else {
795 		val = sip->sn_macaddr;
796 		(void) snprintf(buf, BUFLEN, "%x:%x:%x:%x:%x:%x",
797 		    val[0], val[1], val[2], val[3], val[4], val[5]);
798 		bsetprops(BP_BOOT_MAC, buf);
799 	}
800 }
801 
802 /*
803  * 1st pass at building the table of boot properties. This includes:
804  * - values set on the command line: -B a=x,b=y,c=z ....
805  * - known values we just compute (ie. from xbootp)
806  * - values from /boot/solaris/bootenv.rc (ie. eeprom(1m) values)
807  *
808  * the grub command line looked like:
809  * kernel boot-file [-B prop=value[,prop=value]...] [boot-args]
810  *
811  * whoami is the same as boot-file
812  */
813 static void
814 build_boot_properties(void)
815 {
816 	char *name;
817 	int name_len;
818 	char *value;
819 	int value_len;
820 	static int stdout_val = 0;
821 	struct boot_modules *bm;
822 	char *propbuf;
823 	int quoted = 0;
824 	int boot_arg_len;
825 	uchar_t boot_device;
826 	char str[3];
827 	multiboot_info_t *mbi;
828 	int netboot;
829 	struct sol_netinfo *sip;
830 
831 	/*
832 	 * These have to be done first, so that kobj_mount_root() works
833 	 */
834 	DBG_MSG("Building boot properties\n");
835 	propbuf = do_bsys_alloc(NULL, NULL, MMU_PAGESIZE, 0);
836 	DBG((uintptr_t)propbuf);
837 	if (xbootp->bi_module_cnt > 0) {
838 		bm = xbootp->bi_modules;
839 		bsetprop64("ramdisk_start", (uint64_t)(uintptr_t)bm->bm_addr);
840 		bsetprop64("ramdisk_end", (uint64_t)(uintptr_t)bm->bm_addr +
841 		    bm->bm_size);
842 	}
843 
844 	DBG_MSG("Parsing command line for boot properties\n");
845 	value = xbootp->bi_cmdline;
846 
847 	/*
848 	 * allocate memory to collect boot_args into
849 	 */
850 	boot_arg_len = strlen(xbootp->bi_cmdline) + 1;
851 	boot_args = do_bsys_alloc(NULL, NULL, boot_arg_len, MMU_PAGESIZE);
852 	boot_args[0] = 0;
853 	boot_arg_len = 0;
854 
855 	while (ISSPACE(*value))
856 		++value;
857 	/*
858 	 * value now points at the boot-file
859 	 */
860 	value_len = 0;
861 	while (value[value_len] && !ISSPACE(value[value_len]))
862 		++value_len;
863 	if (value_len > 0) {
864 		whoami = propbuf;
865 		bcopy(value, whoami, value_len);
866 		whoami[value_len] = 0;
867 		bsetprops("boot-file", whoami);
868 		/*
869 		 * strip leading path stuff from whoami, so running from
870 		 * PXE/miniroot makes sense.
871 		 */
872 		if (strstr(whoami, "/platform/") != NULL)
873 			whoami = strstr(whoami, "/platform/");
874 		bsetprops("whoami", whoami);
875 	}
876 
877 	/*
878 	 * Values forcibly set boot properties on the command line via -B.
879 	 * Allow use of quotes in values. Other stuff goes on kernel
880 	 * command line.
881 	 */
882 	name = value + value_len;
883 	while (*name != 0) {
884 		/*
885 		 * anything not " -B" is copied to the command line
886 		 */
887 		if (!ISSPACE(name[0]) || name[1] != '-' || name[2] != 'B') {
888 			boot_args[boot_arg_len++] = *name;
889 			boot_args[boot_arg_len] = 0;
890 			++name;
891 			continue;
892 		}
893 
894 		/*
895 		 * skip the " -B" and following white space
896 		 */
897 		name += 3;
898 		while (ISSPACE(*name))
899 			++name;
900 		while (*name && !ISSPACE(*name)) {
901 			value = strstr(name, "=");
902 			if (value == NULL)
903 				break;
904 			name_len = value - name;
905 			++value;
906 			value_len = 0;
907 			quoted = 0;
908 			for (; ; ++value_len) {
909 				if (!value[value_len])
910 					break;
911 
912 				/*
913 				 * is this value quoted?
914 				 */
915 				if (value_len == 0 &&
916 				    (value[0] == '\'' || value[0] == '"')) {
917 					quoted = value[0];
918 					++value_len;
919 				}
920 
921 				/*
922 				 * In the quote accept any character,
923 				 * but look for ending quote.
924 				 */
925 				if (quoted) {
926 					if (value[value_len] == quoted)
927 						quoted = 0;
928 					continue;
929 				}
930 
931 				/*
932 				 * a comma or white space ends the value
933 				 */
934 				if (value[value_len] == ',' ||
935 				    ISSPACE(value[value_len]))
936 					break;
937 			}
938 
939 			if (value_len == 0) {
940 				bsetprop(name, name_len, "true", 5);
941 			} else {
942 				char *v = value;
943 				int l = value_len;
944 				if (v[0] == v[l - 1] &&
945 				    (v[0] == '\'' || v[0] == '"')) {
946 					++v;
947 					l -= 2;
948 				}
949 				bcopy(v, propbuf, l);
950 				propbuf[l] = '\0';
951 				bsetprop(name, name_len, propbuf,
952 				    l + 1);
953 			}
954 			name = value + value_len;
955 			while (*name == ',')
956 				++name;
957 		}
958 	}
959 
960 	/*
961 	 * set boot-args property
962 	 */
963 	bsetprops("boot-args", boot_args);
964 
965 	/*
966 	 * set the BIOS boot device from GRUB
967 	 */
968 	netboot = 0;
969 	mbi = xbootp->bi_mb_info;
970 	if (mbi != NULL && mbi->flags & 0x2) {
971 		boot_device = mbi->boot_device >> 24;
972 		if (boot_device == 0x20)
973 			netboot++;
974 		str[0] = (boot_device >> 4) + '0';
975 		str[1] = (boot_device & 0xf) + '0';
976 		str[2] = 0;
977 		bsetprops("bios-boot-device", str);
978 	} else {
979 		netboot = 1;
980 	}
981 
982 	/*
983 	 * In the netboot case, drives_info is overloaded with the dhcp ack.
984 	 * This is not multiboot compliant and requires special pxegrub!
985 	 */
986 	if (netboot && mbi->drives_length != 0) {
987 		sip = (struct sol_netinfo *)(uintptr_t)mbi->drives_addr;
988 		if (sip->sn_infotype == SN_TYPE_BOOTP)
989 			bsetprop("bootp-response", sizeof ("bootp-response"),
990 			    (void *)(uintptr_t)mbi->drives_addr,
991 			    mbi->drives_length);
992 		else if (sip->sn_infotype == SN_TYPE_BOOTP)
993 			setup_rarp_props(sip);
994 	}
995 	bsetprop("stdout", strlen("stdout"),
996 	    &stdout_val, sizeof (stdout_val));
997 
998 	/*
999 	 * more conjured up values for made up things....
1000 	 */
1001 	bsetprops("mfg-name", "i86pc");
1002 	bsetprops("impl-arch-name", "i86pc");
1003 
1004 	/*
1005 	 * Build firmware-provided system properties
1006 	 */
1007 	build_firmware_properties();
1008 
1009 	/*
1010 	 * Find out what these are:
1011 	 * - cpuid_feature_ecx_include
1012 	 * - cpuid_feature_ecx_exclude
1013 	 * - cpuid_feature_edx_include
1014 	 * - cpuid_feature_edx_exclude
1015 	 *
1016 	 * Find out what these are in multiboot:
1017 	 * - bootp-response
1018 	 * - netdev-path
1019 	 * - fstype
1020 	 */
1021 }
1022 
1023 /*
1024  * Install a temporary IDT that lets us catch errors in the boot time code.
1025  * We shouldn't get any faults at all while this is installed, so we'll
1026  * just generate a traceback and exit.
1027  */
1028 #ifdef __amd64
1029 static const int bcode_sel = B64CODE_SEL;
1030 #else
1031 static const int bcode_sel = B32CODE_SEL;
1032 #endif
1033 
1034 /*
1035  * simple description of a stack frame (args are 32 bit only currently)
1036  */
1037 typedef struct bop_frame {
1038 	struct bop_frame *old_frame;
1039 	pc_t retaddr;
1040 	long arg[1];
1041 } bop_frame_t;
1042 
1043 void
1044 bop_traceback(bop_frame_t *frame)
1045 {
1046 	pc_t pc;
1047 	int cnt;
1048 	int a;
1049 	char *ksym;
1050 	ulong_t off;
1051 
1052 	bop_printf(NULL, "Stack traceback:\n");
1053 	for (cnt = 0; cnt < 30; ++cnt) {	/* up to 30 frames */
1054 		pc = frame->retaddr;
1055 		if (pc == 0)
1056 			break;
1057 		ksym = kobj_getsymname(pc, &off);
1058 		if (ksym)
1059 			bop_printf(NULL, "  %s+%lx", ksym, off);
1060 		else
1061 			bop_printf(NULL, "  0x%lx", pc);
1062 
1063 		frame = frame->old_frame;
1064 		if (frame == 0) {
1065 			bop_printf(NULL, "\n");
1066 			break;
1067 		}
1068 		for (a = 0; a < 6; ++a) {	/* try for 6 args */
1069 #if defined(__i386)
1070 			if ((void *)&frame->arg[a] == (void *)frame->old_frame)
1071 				break;
1072 			if (a == 0)
1073 				bop_printf(NULL, "(");
1074 			else
1075 				bop_printf(NULL, ",");
1076 			bop_printf(NULL, "0x%lx", frame->arg[a]);
1077 #endif
1078 		}
1079 		bop_printf(NULL, ")\n");
1080 	}
1081 }
1082 
1083 struct trapframe {
1084 	ulong_t frame_ptr;	/* %[er]bp pushed by our code */
1085 	ulong_t error_code;	/* optional */
1086 	ulong_t inst_ptr;
1087 	ulong_t code_seg;
1088 	ulong_t flags_reg;
1089 #ifdef __amd64
1090 	ulong_t stk_ptr;
1091 	ulong_t stk_seg;
1092 #endif
1093 };
1094 
1095 void
1096 bop_trap(struct trapframe *tf)
1097 {
1098 	bop_frame_t fakeframe;
1099 	static int depth = 0;
1100 
1101 	/*
1102 	 * Check for an infinite loop of traps. Avoid bop_printf() here to
1103 	 * reduce code path and further possibility of failure.
1104 	 */
1105 	if (++depth > 2) {
1106 		PUT_STRING("Nested trap, calling reset()\n");
1107 		reset();
1108 	}
1109 
1110 	/*
1111 	 * adjust the tf for optional error_code by detecting the code selector
1112 	 */
1113 	if (tf->code_seg != bcode_sel)
1114 		tf = (struct trapframe *)((uintptr_t)tf - sizeof (ulong_t));
1115 
1116 	bop_printf(NULL, "Unexpected trap\n");
1117 	bop_printf(NULL, "instruction pointer  0x%lx\n", tf->inst_ptr);
1118 	bop_printf(NULL, "error code, optional 0x%lx\n",
1119 	    tf->error_code & 0xffffffff);
1120 	bop_printf(NULL, "code segment         0x%lx\n", tf->code_seg & 0xffff);
1121 	bop_printf(NULL, "flags register       0x%lx\n", tf->flags_reg);
1122 #ifdef __amd64
1123 	bop_printf(NULL, "return %%rsp         0x%lx\n", tf->stk_ptr);
1124 	bop_printf(NULL, "return %%ss          0x%lx\n", tf->stk_seg & 0xffff);
1125 #endif
1126 	fakeframe.old_frame = (bop_frame_t *)tf->frame_ptr;
1127 	fakeframe.retaddr = (pc_t)tf->inst_ptr;
1128 	bop_printf(NULL, "Attempting stack backtrace:\n");
1129 	bop_traceback(&fakeframe);
1130 	bop_panic("unexpected trap in early boot");
1131 }
1132 
1133 extern void bop_trap_handler(void);
1134 
1135 static gate_desc_t bop_idt[NIDT];
1136 
1137 static desctbr_t bop_idt_info;
1138 
1139 static void
1140 bop_idt_init(void)
1141 {
1142 	int t;
1143 
1144 	bzero(&bop_idt, sizeof (bop_idt));
1145 	for (t = 0; t < NIDT; ++t) {
1146 		set_gatesegd(&bop_idt[t], &bop_trap_handler, bcode_sel,
1147 		    SDT_SYSIGT, SEL_KPL);
1148 	}
1149 	bop_idt_info.dtr_limit = sizeof (bop_idt) - 1;
1150 	bop_idt_info.dtr_base = (uintptr_t)&bop_idt;
1151 	wr_idtr(&bop_idt_info);
1152 }
1153 
1154 /*
1155  * This is where we enter the kernel. It dummies up the boot_ops and
1156  * boot_syscalls vectors and jumps off to _kobj_boot()
1157  */
1158 void
1159 _start(struct xboot_info *xbp)
1160 {
1161 	bootops_t *bops = &bootop;
1162 	extern void _kobj_boot();
1163 
1164 	/*
1165 	 * 1st off - initialize the console for any error messages
1166 	 */
1167 	xbootp = xbp;
1168 	bcons_init((void *)xbootp->bi_cmdline);
1169 	have_console = 1;
1170 
1171 	/*
1172 	 * enable debugging
1173 	 */
1174 	if (strstr((char *)xbootp->bi_cmdline, "kbm_debug"))
1175 		kbm_debug = 1;
1176 
1177 	DBG_MSG("\n\n*** Entered Solaris in _start() cmdline is: ");
1178 	DBG_MSG((char *)xbootp->bi_cmdline);
1179 	DBG_MSG("\n\n\n");
1180 
1181 	/*
1182 	 * Install an IDT to catch early pagefaults (shouldn't have any).
1183 	 * Also needed for kmdb.
1184 	 */
1185 	bop_idt_init();
1186 
1187 	/*
1188 	 * physavail is no longer used by startup
1189 	 */
1190 	bm.physinstalled = xbp->bi_phys_install;
1191 	bm.pcimem = xbp->bi_pcimem;
1192 	bm.physavail = NULL;
1193 
1194 	/*
1195 	 * initialize the boot time allocator
1196 	 */
1197 	next_phys = xbootp->bi_next_paddr;
1198 	DBG(next_phys);
1199 	next_virt = (uintptr_t)xbootp->bi_next_vaddr;
1200 	DBG(next_virt);
1201 	DBG_MSG("Initializing boot time memory management...");
1202 	kbm_init(xbootp);
1203 	DBG_MSG("done\n");
1204 
1205 	/*
1206 	 * Fill in the bootops vector
1207 	 */
1208 	bops->bsys_version = BO_VERSION;
1209 	bops->boot_mem = &bm;
1210 	bops->bsys_alloc = do_bsys_alloc;
1211 	bops->bsys_free = do_bsys_free;
1212 	bops->bsys_getproplen = do_bsys_getproplen;
1213 	bops->bsys_getprop = do_bsys_getprop;
1214 	bops->bsys_nextprop = do_bsys_nextprop;
1215 	bops->bsys_printf = bop_printf;
1216 	bops->bsys_doint = do_bsys_doint;
1217 
1218 	/*
1219 	 * BOP_EALLOC() is no longer needed
1220 	 */
1221 	bops->bsys_ealloc = do_bsys_ealloc;
1222 
1223 	/*
1224 	 *
1225 	 */
1226 	DBG_MSG("Initializing boot properties:\n");
1227 	build_boot_properties();
1228 
1229 	if (strstr((char *)xbootp->bi_cmdline, "prom_debug") || kbm_debug) {
1230 		char *name;
1231 		char *value;
1232 		int len;
1233 
1234 		value = do_bsys_alloc(NULL, NULL, MMU_PAGESIZE, MMU_PAGESIZE);
1235 		bop_printf(NULL, "\nBoot properties:\n");
1236 		name = "";
1237 		while ((name = do_bsys_nextprop(NULL, name)) != NULL) {
1238 			bop_printf(NULL, "\t0x%p %s = ", (void *)name, name);
1239 			(void) do_bsys_getprop(NULL, name, value);
1240 			len = do_bsys_getproplen(NULL, name);
1241 			bop_printf(NULL, "len=%d ", len);
1242 			value[len] = 0;
1243 			bop_printf(NULL, "%s\n", value);
1244 		}
1245 	}
1246 
1247 	/*
1248 	 * jump into krtld...
1249 	 */
1250 	_kobj_boot(&bop_sysp, NULL, bops, NULL);
1251 }
1252 
1253 
1254 /*ARGSUSED*/
1255 static caddr_t
1256 no_more_alloc(bootops_t *bop, caddr_t virthint, size_t size, int align)
1257 {
1258 	panic("Attempt to bsys_alloc() too late\n");
1259 	return (NULL);
1260 }
1261 
1262 /*ARGSUSED*/
1263 static void
1264 no_more_free(bootops_t *bop, caddr_t virt, size_t size)
1265 {
1266 	panic("Attempt to bsys_free() too late\n");
1267 }
1268 
1269 void
1270 bop_no_more_mem(void)
1271 {
1272 	DBG(total_bop_alloc_scratch);
1273 	DBG(total_bop_alloc_kernel);
1274 	bootops->bsys_alloc = no_more_alloc;
1275 	bootops->bsys_free = no_more_free;
1276 }
1277 
1278 
1279 /*
1280  * Set ACPI firmware properties
1281  */
1282 
1283 static caddr_t
1284 vmap_phys(size_t length, paddr_t pa)
1285 {
1286 	paddr_t	start, end;
1287 	caddr_t	va;
1288 	size_t	len, page;
1289 
1290 	start = P2ALIGN(pa, MMU_PAGESIZE);
1291 	end = P2ROUNDUP(pa + length, MMU_PAGESIZE);
1292 	len = end - start;
1293 	va = (caddr_t)alloc_vaddr(len, MMU_PAGESIZE);
1294 	for (page = 0; page < len; page += MMU_PAGESIZE)
1295 		kbm_map((uintptr_t)va + page, start + page, 0, 0);
1296 	return (va + (pa & MMU_PAGEOFFSET));
1297 }
1298 
1299 static uint8_t
1300 checksum_table(uint8_t *tp, size_t len)
1301 {
1302 	uint8_t sum = 0;
1303 
1304 	while (len-- > 0)
1305 		sum += *tp++;
1306 
1307 	return (sum);
1308 }
1309 
1310 static int
1311 valid_rsdp(struct rsdp *rp)
1312 {
1313 
1314 	/* validate the V1.x checksum */
1315 	if (checksum_table((uint8_t *)&rp->v1, sizeof (struct rsdp_v1)) != 0)
1316 		return (0);
1317 
1318 	/* If pre-ACPI 2.0, this is a valid RSDP */
1319 	if (rp->v1.revision < 2)
1320 		return (1);
1321 
1322 	/* validate the V2.x checksum */
1323 	if (checksum_table((uint8_t *)rp, sizeof (struct rsdp)) != 0)
1324 		return (0);
1325 
1326 	return (1);
1327 }
1328 
1329 /*
1330  * Scan memory range for an RSDP;
1331  * see ACPI 3.0 Spec, 5.2.5.1
1332  */
1333 static struct rsdp *
1334 scan_rsdp(paddr_t start, paddr_t end)
1335 {
1336 	size_t len  = end - start + 1;
1337 	caddr_t ptr;
1338 
1339 	ptr = vmap_phys(len, start);
1340 	while (len > 0) {
1341 		if (strncmp(ptr, ACPI_RSDP_SIG, ACPI_RSDP_SIG_LEN) == 0)
1342 			if (valid_rsdp((struct rsdp *)ptr))
1343 				return ((struct rsdp *)ptr);
1344 		ptr += 16;
1345 		len -= 16;
1346 	}
1347 
1348 	return (NULL);
1349 }
1350 
1351 /*
1352  * Refer to ACPI 3.0 Spec, section 5.2.5.1 to understand this function
1353  */
1354 static struct rsdp *
1355 find_rsdp() {
1356 	struct rsdp *rsdp;
1357 	uint16_t *ebda_seg;
1358 	paddr_t  ebda_addr;
1359 
1360 	/*
1361 	 * Get the EBDA segment and scan the first 1K
1362 	 */
1363 	ebda_seg = (uint16_t *)vmap_phys(sizeof (uint16_t), ACPI_EBDA_SEG_ADDR);
1364 	ebda_addr = *ebda_seg << 4;
1365 	rsdp = scan_rsdp(ebda_addr, ebda_addr + ACPI_EBDA_LEN - 1);
1366 	if (rsdp == NULL)
1367 		/* if EBDA doesn't contain RSDP, look in BIOS memory */
1368 		rsdp = scan_rsdp(0xe0000, 0xfffff);
1369 	return (rsdp);
1370 }
1371 
1372 static struct table_header *
1373 map_fw_table(paddr_t table_addr)
1374 {
1375 	struct table_header *tp;
1376 	size_t len = MAX(sizeof (struct table_header), MMU_PAGESIZE);
1377 
1378 	/*
1379 	 * Map at least a page; if the table is larger than this, remap it
1380 	 */
1381 	tp = (struct table_header *)vmap_phys(len, table_addr);
1382 	if (tp->len > len)
1383 		tp = (struct table_header *)vmap_phys(tp->len, table_addr);
1384 	return (tp);
1385 }
1386 
1387 static struct table_header *
1388 find_fw_table(char *signature)
1389 {
1390 	static int revision = 0;
1391 	static struct xsdt *xsdt;
1392 	static int len;
1393 	paddr_t xsdt_addr;
1394 	struct rsdp *rsdp;
1395 	struct table_header *tp;
1396 	paddr_t table_addr;
1397 	int	n;
1398 
1399 	if (strlen(signature) != ACPI_TABLE_SIG_LEN)
1400 		return (NULL);
1401 
1402 	/*
1403 	 * Reading the ACPI 3.0 Spec, section 5.2.5.3 will help
1404 	 * understand this code.  If we haven't already found the RSDT/XSDT,
1405 	 * revision will be 0. Find the RSDP and check the revision
1406 	 * to find out whether to use the RSDT or XSDT.  If revision is
1407 	 * 0 or 1, use the RSDT and set internal revision to 1; if it is 2,
1408 	 * use the XSDT.  If the XSDT address is 0, though, fall back to
1409 	 * revision 1 and use the RSDT.
1410 	 */
1411 	if (revision == 0) {
1412 		if ((rsdp = (struct rsdp *)find_rsdp()) != NULL) {
1413 			revision = rsdp->v1.revision;
1414 			switch (revision) {
1415 			case 2:
1416 				/*
1417 				 * Use the XSDT unless BIOS is buggy and
1418 				 * claims to be rev 2 but has a null XSDT
1419 				 * address
1420 				 */
1421 				xsdt_addr = rsdp->xsdt;
1422 				if (xsdt_addr != 0)
1423 					break;
1424 				/* FALLTHROUGH */
1425 			case 0:
1426 				/* treat RSDP rev 0 as revision 1 internally */
1427 				revision = 1;
1428 				/* FALLTHROUGH */
1429 			case 1:
1430 				/* use the RSDT for rev 0/1 */
1431 				xsdt_addr = rsdp->v1.rsdt;
1432 				break;
1433 			default:
1434 				/* unknown revision */
1435 				revision = 0;
1436 				break;
1437 			}
1438 		}
1439 		if (revision == 0)
1440 			return (NULL);
1441 
1442 		/* cache the XSDT info */
1443 		xsdt = (struct xsdt *)map_fw_table(xsdt_addr);
1444 		len = (xsdt->hdr.len - sizeof (xsdt->hdr)) /
1445 		    ((revision == 1) ? sizeof (uint32_t) : sizeof (uint64_t));
1446 	}
1447 
1448 	/*
1449 	 * Scan the table headers looking for a signature match
1450 	 */
1451 	for (n = 0; n < len; n++) {
1452 		table_addr = (revision == 1) ? xsdt->p.r[n] : xsdt->p.x[n];
1453 		if (table_addr == 0)
1454 			continue;
1455 		tp = map_fw_table(table_addr);
1456 		if (strncmp(tp->sig, signature, ACPI_TABLE_SIG_LEN) == 0) {
1457 			return (tp);
1458 		}
1459 	}
1460 	return (NULL);
1461 }
1462 
1463 static void
1464 process_madt(struct madt *tp)
1465 {
1466 	struct madt_processor *cpu, *end;
1467 	uint32_t cpu_count = 0;
1468 
1469 	/*
1470 	 * User-set boot-ncpus overrides firmware count
1471 	 */
1472 	if (do_bsys_getproplen(NULL, "boot-ncpus") >= 0)
1473 		return;
1474 
1475 	if (tp != NULL) {
1476 		end = (struct madt_processor *)(tp->hdr.len + (uintptr_t)tp);
1477 		cpu = tp->list;
1478 		while (cpu < end) {
1479 			if (cpu->type == MADT_PROCESSOR)
1480 				if (cpu->flags & 1)
1481 					cpu_count++;
1482 
1483 			cpu = (struct madt_processor *)
1484 			    (cpu->len + (uintptr_t)cpu);
1485 		}
1486 		bsetpropsi("boot-ncpus", cpu_count);
1487 	}
1488 
1489 }
1490 
1491 static void
1492 process_srat(struct srat *tp)
1493 {
1494 	struct srat_item *item, *end;
1495 	int i;
1496 	int proc_num, mem_num;
1497 #pragma pack(1)
1498 	struct {
1499 		uint32_t domain;
1500 		uint32_t apic_id;
1501 		uint32_t sapic_id;
1502 	} processor;
1503 	struct {
1504 		uint32_t domain;
1505 		uint64_t addr;
1506 		uint64_t length;
1507 		uint32_t flags;
1508 	} memory;
1509 #pragma pack()
1510 	char prop_name[30];
1511 
1512 	if (tp == NULL)
1513 		return;
1514 
1515 	proc_num = mem_num = 0;
1516 	end = (struct srat_item *)(tp->hdr.len + (uintptr_t)tp);
1517 	item = tp->list;
1518 	while (item < end) {
1519 		switch (item->type) {
1520 		case SRAT_PROCESSOR:
1521 			if (!(item->i.p.flags & SRAT_ENABLED))
1522 				break;
1523 			processor.domain = item->i.p.domain1;
1524 			for (i = 0; i < 3; i++)
1525 				processor.domain +=
1526 				    item->i.p.domain2[i] << ((i + 1) * 8);
1527 			processor.apic_id = item->i.p.apic_id;
1528 			processor.sapic_id = item->i.p.local_sapic_eid;
1529 			(void) snprintf(prop_name, 30, "acpi-srat-processor-%d",
1530 			    proc_num);
1531 			bsetprop(prop_name, strlen(prop_name), &processor,
1532 			    sizeof (processor));
1533 			proc_num++;
1534 			break;
1535 		case SRAT_MEMORY:
1536 			if (!(item->i.m.flags & SRAT_ENABLED))
1537 				break;
1538 			memory.domain = item->i.m.domain;
1539 			memory.addr = item->i.m.base_addr;
1540 			memory.length = item->i.m.len;
1541 			memory.flags = item->i.m.flags;
1542 			(void) snprintf(prop_name, 30, "acpi-srat-memory-%d",
1543 			    mem_num);
1544 			bsetprop(prop_name, strlen(prop_name), &memory,
1545 			    sizeof (memory));
1546 			mem_num++;
1547 			break;
1548 		}
1549 
1550 		item = (struct srat_item *)
1551 		    (item->len + (caddr_t)item);
1552 	}
1553 }
1554 
1555 static void
1556 process_slit(struct slit *tp)
1557 {
1558 
1559 	/*
1560 	 * Check the number of localities; if it's too huge, we just
1561 	 * return and locality enumeration code will handle this later,
1562 	 * if possible.
1563 	 *
1564 	 * Note that the size of the table is the square of the
1565 	 * number of localities; if the number of localities exceeds
1566 	 * UINT16_MAX, the table size may overflow an int when being
1567 	 * passed to bsetprop() below.
1568 	 */
1569 	if (tp->number >= SLIT_LOCALITIES_MAX)
1570 		return;
1571 
1572 	bsetprop(SLIT_NUM_PROPNAME, strlen(SLIT_NUM_PROPNAME), &tp->number,
1573 	    sizeof (tp->number));
1574 	bsetprop(SLIT_PROPNAME, strlen(SLIT_PROPNAME), &tp->entry,
1575 	    tp->number * tp->number);
1576 }
1577 
1578 static void
1579 build_firmware_properties(void)
1580 {
1581 	struct table_header *tp;
1582 
1583 	if (tp = find_fw_table("APIC"))
1584 		process_madt((struct madt *)tp);
1585 
1586 	if (tp = find_fw_table("SRAT"))
1587 		process_srat((struct srat *)tp);
1588 
1589 	if (tp = find_fw_table("SLIT"))
1590 		process_slit((struct slit *)tp);
1591 }
1592 
1593 /*
1594  * fake up a boot property for USB serial console early boot output
1595  */
1596 void *
1597 usbser_init(size_t size)
1598 {
1599 	static char *p = NULL;
1600 
1601 	p = do_bsys_alloc(NULL, NULL, size, MMU_PAGESIZE);
1602 	*p = 0;
1603 	bsetprop("usb-serial-buf", strlen("usb-serial-buf") + 1,
1604 	    &p, sizeof (p));
1605 	return (p);
1606 }
1607