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