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