xref: /illumos-gate/usr/src/uts/i86pc/os/fakebop.c (revision 508de9f313d4260d23954085ed866e727901964c)
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 /*ARGSUSED*/
863 void
864 vbop_printf(void *ptr, const char *fmt, va_list ap)
865 {
866 	if (have_console == 0)
867 		return;
868 
869 	(void) vsnprintf(buffer, BUFFERSIZE, fmt, ap);
870 	PUT_STRING(buffer);
871 }
872 
873 /*PRINTFLIKE2*/
874 void
875 bop_printf(void *bop, const char *fmt, ...)
876 {
877 	va_list	ap;
878 
879 	va_start(ap, fmt);
880 	vbop_printf(bop, fmt, ap);
881 	va_end(ap);
882 }
883 
884 /*
885  * Another panic() variant; this one can be used even earlier during boot than
886  * prom_panic().
887  */
888 /*PRINTFLIKE1*/
889 void
890 bop_panic(const char *fmt, ...)
891 {
892 	va_list ap;
893 
894 	va_start(ap, fmt);
895 	bop_printf(NULL, fmt, ap);
896 	va_end(ap);
897 
898 	bop_printf(NULL, "\nPress any key to reboot.\n");
899 	(void) bcons_getchar();
900 	bop_printf(NULL, "Resetting...\n");
901 	pc_reset();
902 }
903 
904 /*
905  * Do a real mode interrupt BIOS call
906  */
907 typedef struct bios_regs {
908 	unsigned short ax, bx, cx, dx, si, di, bp, es, ds;
909 } bios_regs_t;
910 typedef int (*bios_func_t)(int, bios_regs_t *);
911 
912 /*ARGSUSED*/
913 static void
914 do_bsys_doint(bootops_t *bop, int intnum, struct bop_regs *rp)
915 {
916 #if defined(__xpv)
917 	prom_panic("unsupported call to BOP_DOINT()\n");
918 #else	/* __xpv */
919 	static int firsttime = 1;
920 	bios_func_t bios_func = (bios_func_t)(void *)(uintptr_t)0x5000;
921 	bios_regs_t br;
922 
923 	/*
924 	 * We're about to disable paging; we shouldn't be PCID enabled.
925 	 */
926 	if (getcr4() & CR4_PCIDE)
927 		prom_panic("do_bsys_doint() with PCID enabled\n");
928 
929 	/*
930 	 * The first time we do this, we have to copy the pre-packaged
931 	 * low memory bios call code image into place.
932 	 */
933 	if (firsttime) {
934 		extern char bios_image[];
935 		extern uint32_t bios_size;
936 
937 		bcopy(bios_image, (void *)bios_func, bios_size);
938 		firsttime = 0;
939 	}
940 
941 	br.ax = rp->eax.word.ax;
942 	br.bx = rp->ebx.word.bx;
943 	br.cx = rp->ecx.word.cx;
944 	br.dx = rp->edx.word.dx;
945 	br.bp = rp->ebp.word.bp;
946 	br.si = rp->esi.word.si;
947 	br.di = rp->edi.word.di;
948 	br.ds = rp->ds;
949 	br.es = rp->es;
950 
951 	DBG_MSG("Doing BIOS call...");
952 	DBG(br.ax);
953 	DBG(br.bx);
954 	DBG(br.dx);
955 	rp->eflags = bios_func(intnum, &br);
956 	DBG_MSG("done\n");
957 
958 	rp->eax.word.ax = br.ax;
959 	rp->ebx.word.bx = br.bx;
960 	rp->ecx.word.cx = br.cx;
961 	rp->edx.word.dx = br.dx;
962 	rp->ebp.word.bp = br.bp;
963 	rp->esi.word.si = br.si;
964 	rp->edi.word.di = br.di;
965 	rp->ds = br.ds;
966 	rp->es = br.es;
967 #endif /* __xpv */
968 }
969 
970 static struct boot_syscalls bop_sysp = {
971 	bcons_getchar,
972 	bcons_putchar,
973 	bcons_ischar,
974 };
975 
976 static char *whoami;
977 
978 #define	BUFLEN	64
979 
980 #if defined(__xpv)
981 
982 static char namebuf[32];
983 
984 static void
985 xen_parse_props(char *s, char *prop_map[], int n_prop)
986 {
987 	char **prop_name = prop_map;
988 	char *cp = s, *scp;
989 
990 	do {
991 		scp = cp;
992 		while ((*cp != NULL) && (*cp != ':'))
993 			cp++;
994 
995 		if ((scp != cp) && (*prop_name != NULL)) {
996 			*cp = NULL;
997 			bsetprops(*prop_name, scp);
998 		}
999 
1000 		cp++;
1001 		prop_name++;
1002 		n_prop--;
1003 	} while (n_prop > 0);
1004 }
1005 
1006 #define	VBDPATHLEN	64
1007 
1008 /*
1009  * parse the 'xpv-root' property to create properties used by
1010  * ufs_mountroot.
1011  */
1012 static void
1013 xen_vbdroot_props(char *s)
1014 {
1015 	char vbdpath[VBDPATHLEN] = "/xpvd/xdf@";
1016 	const char lnamefix[] = "/dev/dsk/c0d";
1017 	char *pnp;
1018 	char *prop_p;
1019 	char mi;
1020 	short minor;
1021 	long addr = 0;
1022 
1023 	pnp = vbdpath + strlen(vbdpath);
1024 	prop_p = s + strlen(lnamefix);
1025 	while ((*prop_p != '\0') && (*prop_p != 's') && (*prop_p != 'p'))
1026 		addr = addr * 10 + *prop_p++ - '0';
1027 	(void) snprintf(pnp, VBDPATHLEN, "%lx", addr);
1028 	pnp = vbdpath + strlen(vbdpath);
1029 	if (*prop_p == 's')
1030 		mi = 'a';
1031 	else if (*prop_p == 'p')
1032 		mi = 'q';
1033 	else
1034 		ASSERT(0); /* shouldn't be here */
1035 	prop_p++;
1036 	ASSERT(*prop_p != '\0');
1037 	if (ISDIGIT(*prop_p)) {
1038 		minor = *prop_p - '0';
1039 		prop_p++;
1040 		if (ISDIGIT(*prop_p)) {
1041 			minor = minor * 10 + *prop_p - '0';
1042 		}
1043 	} else {
1044 		/* malformed root path, use 0 as default */
1045 		minor = 0;
1046 	}
1047 	ASSERT(minor < 16); /* at most 16 partitions */
1048 	mi += minor;
1049 	*pnp++ = ':';
1050 	*pnp++ = mi;
1051 	*pnp++ = '\0';
1052 	bsetprops("fstype", "ufs");
1053 	bsetprops("bootpath", vbdpath);
1054 
1055 	DBG_MSG("VBD bootpath set to ");
1056 	DBG_MSG(vbdpath);
1057 	DBG_MSG("\n");
1058 }
1059 
1060 /*
1061  * parse the xpv-nfsroot property to create properties used by
1062  * nfs_mountroot.
1063  */
1064 static void
1065 xen_nfsroot_props(char *s)
1066 {
1067 	char *prop_map[] = {
1068 		BP_SERVER_IP,	/* server IP address */
1069 		BP_SERVER_NAME,	/* server hostname */
1070 		BP_SERVER_PATH,	/* root path */
1071 	};
1072 	int n_prop = sizeof (prop_map) / sizeof (prop_map[0]);
1073 
1074 	bsetprops("fstype", "nfs");
1075 
1076 	xen_parse_props(s, prop_map, n_prop);
1077 
1078 	/*
1079 	 * If a server name wasn't specified, use a default.
1080 	 */
1081 	if (do_bsys_getproplen(NULL, BP_SERVER_NAME) == -1)
1082 		bsetprops(BP_SERVER_NAME, "unknown");
1083 }
1084 
1085 /*
1086  * Extract our IP address, etc. from the "xpv-ip" property.
1087  */
1088 static void
1089 xen_ip_props(char *s)
1090 {
1091 	char *prop_map[] = {
1092 		BP_HOST_IP,		/* IP address */
1093 		NULL,			/* NFS server IP address (ignored in */
1094 					/* favour of xpv-nfsroot) */
1095 		BP_ROUTER_IP,		/* IP gateway */
1096 		BP_SUBNET_MASK,		/* IP subnet mask */
1097 		"xpv-hostname",		/* hostname (ignored) */
1098 		BP_NETWORK_INTERFACE,	/* interface name */
1099 		"xpv-hcp",		/* host configuration protocol */
1100 	};
1101 	int n_prop = sizeof (prop_map) / sizeof (prop_map[0]);
1102 	char ifname[IFNAMSIZ];
1103 
1104 	xen_parse_props(s, prop_map, n_prop);
1105 
1106 	/*
1107 	 * A Linux dom0 administrator expects all interfaces to be
1108 	 * called "ethX", which is not the case here.
1109 	 *
1110 	 * If the interface name specified is "eth0", presume that
1111 	 * this is really intended to be "xnf0" (the first domU ->
1112 	 * dom0 interface for this domain).
1113 	 */
1114 	if ((do_bsys_getprop(NULL, BP_NETWORK_INTERFACE, ifname) == 0) &&
1115 	    (strcmp("eth0", ifname) == 0)) {
1116 		bsetprops(BP_NETWORK_INTERFACE, "xnf0");
1117 		bop_printf(NULL,
1118 		    "network interface name 'eth0' replaced with 'xnf0'\n");
1119 	}
1120 }
1121 
1122 #else	/* __xpv */
1123 
1124 static void
1125 setup_rarp_props(struct sol_netinfo *sip)
1126 {
1127 	char buf[BUFLEN];	/* to hold ip/mac addrs */
1128 	uint8_t *val;
1129 
1130 	val = (uint8_t *)&sip->sn_ciaddr;
1131 	(void) snprintf(buf, BUFLEN, "%d.%d.%d.%d",
1132 	    val[0], val[1], val[2], val[3]);
1133 	bsetprops(BP_HOST_IP, buf);
1134 
1135 	val = (uint8_t *)&sip->sn_siaddr;
1136 	(void) snprintf(buf, BUFLEN, "%d.%d.%d.%d",
1137 	    val[0], val[1], val[2], val[3]);
1138 	bsetprops(BP_SERVER_IP, buf);
1139 
1140 	if (sip->sn_giaddr != 0) {
1141 		val = (uint8_t *)&sip->sn_giaddr;
1142 		(void) snprintf(buf, BUFLEN, "%d.%d.%d.%d",
1143 		    val[0], val[1], val[2], val[3]);
1144 		bsetprops(BP_ROUTER_IP, buf);
1145 	}
1146 
1147 	if (sip->sn_netmask != 0) {
1148 		val = (uint8_t *)&sip->sn_netmask;
1149 		(void) snprintf(buf, BUFLEN, "%d.%d.%d.%d",
1150 		    val[0], val[1], val[2], val[3]);
1151 		bsetprops(BP_SUBNET_MASK, buf);
1152 	}
1153 
1154 	if (sip->sn_mactype != 4 || sip->sn_maclen != 6) {
1155 		bop_printf(NULL, "unsupported mac type %d, mac len %d\n",
1156 		    sip->sn_mactype, sip->sn_maclen);
1157 	} else {
1158 		val = sip->sn_macaddr;
1159 		(void) snprintf(buf, BUFLEN, "%x:%x:%x:%x:%x:%x",
1160 		    val[0], val[1], val[2], val[3], val[4], val[5]);
1161 		bsetprops(BP_BOOT_MAC, buf);
1162 	}
1163 }
1164 
1165 #endif	/* __xpv */
1166 
1167 static void
1168 build_panic_cmdline(const char *cmd, int cmdlen)
1169 {
1170 	int proplen;
1171 	size_t arglen;
1172 
1173 	arglen = sizeof (fastreboot_onpanic_args);
1174 	/*
1175 	 * If we allready have fastreboot-onpanic set to zero,
1176 	 * don't add them again.
1177 	 */
1178 	if ((proplen = do_bsys_getproplen(NULL, FASTREBOOT_ONPANIC)) > 0 &&
1179 	    proplen <=  sizeof (fastreboot_onpanic_cmdline)) {
1180 		(void) do_bsys_getprop(NULL, FASTREBOOT_ONPANIC,
1181 		    fastreboot_onpanic_cmdline);
1182 		if (FASTREBOOT_ONPANIC_NOTSET(fastreboot_onpanic_cmdline))
1183 			arglen = 1;
1184 	}
1185 
1186 	/*
1187 	 * construct fastreboot_onpanic_cmdline
1188 	 */
1189 	if (cmdlen + arglen > sizeof (fastreboot_onpanic_cmdline)) {
1190 		DBG_MSG("Command line too long: clearing "
1191 		    FASTREBOOT_ONPANIC "\n");
1192 		fastreboot_onpanic = 0;
1193 	} else {
1194 		bcopy(cmd, fastreboot_onpanic_cmdline, cmdlen);
1195 		if (arglen != 1)
1196 			bcopy(fastreboot_onpanic_args,
1197 			    fastreboot_onpanic_cmdline + cmdlen, arglen);
1198 		else
1199 			fastreboot_onpanic_cmdline[cmdlen] = 0;
1200 	}
1201 }
1202 
1203 
1204 #ifndef	__xpv
1205 /*
1206  * Construct boot command line for Fast Reboot. The saved_cmdline
1207  * is also reported by "eeprom bootcmd".
1208  */
1209 static void
1210 build_fastboot_cmdline(struct xboot_info *xbp)
1211 {
1212 	saved_cmdline_len =  strlen(xbp->bi_cmdline) + 1;
1213 	if (saved_cmdline_len > FASTBOOT_SAVED_CMDLINE_LEN) {
1214 		DBG(saved_cmdline_len);
1215 		DBG_MSG("Command line too long: clearing fastreboot_capable\n");
1216 		fastreboot_capable = 0;
1217 	} else {
1218 		bcopy((void *)(xbp->bi_cmdline), (void *)saved_cmdline,
1219 		    saved_cmdline_len);
1220 		saved_cmdline[saved_cmdline_len - 1] = '\0';
1221 		build_panic_cmdline(saved_cmdline, saved_cmdline_len - 1);
1222 	}
1223 }
1224 
1225 /*
1226  * Save memory layout, disk drive information, unix and boot archive sizes for
1227  * Fast Reboot.
1228  */
1229 static void
1230 save_boot_info(struct xboot_info *xbi)
1231 {
1232 	multiboot_info_t *mbi = xbi->bi_mb_info;
1233 	struct boot_modules *modp;
1234 	int i;
1235 
1236 	bcopy(mbi, &saved_mbi, sizeof (multiboot_info_t));
1237 	if (mbi->mmap_length > sizeof (saved_mmap)) {
1238 		DBG_MSG("mbi->mmap_length too big: clearing "
1239 		    "fastreboot_capable\n");
1240 		fastreboot_capable = 0;
1241 	} else {
1242 		bcopy((void *)(uintptr_t)mbi->mmap_addr, (void *)saved_mmap,
1243 		    mbi->mmap_length);
1244 	}
1245 
1246 	if ((mbi->flags & MB_INFO_DRIVE_INFO) != 0) {
1247 		if (mbi->drives_length > sizeof (saved_drives)) {
1248 			DBG(mbi->drives_length);
1249 			DBG_MSG("mbi->drives_length too big: clearing "
1250 			    "fastreboot_capable\n");
1251 			fastreboot_capable = 0;
1252 		} else {
1253 			bcopy((void *)(uintptr_t)mbi->drives_addr,
1254 			    (void *)saved_drives, mbi->drives_length);
1255 		}
1256 	} else {
1257 		saved_mbi.drives_length = 0;
1258 		saved_mbi.drives_addr = NULL;
1259 	}
1260 
1261 	/*
1262 	 * Current file sizes.  Used by fastboot.c to figure out how much
1263 	 * memory to reserve for panic reboot.
1264 	 * Use the module list from the dboot-constructed xboot_info
1265 	 * instead of the list referenced by the multiboot structure
1266 	 * because that structure may not be addressable now.
1267 	 */
1268 	saved_file_size[FASTBOOT_NAME_UNIX] = FOUR_MEG - PAGESIZE;
1269 	for (i = 0, modp = (struct boot_modules *)(uintptr_t)xbi->bi_modules;
1270 	    i < xbi->bi_module_cnt; i++, modp++) {
1271 		saved_file_size[FASTBOOT_NAME_BOOTARCHIVE] += modp->bm_size;
1272 	}
1273 }
1274 #endif	/* __xpv */
1275 
1276 /*
1277  * Import boot environment module variables as properties, applying
1278  * blacklist filter for variables we know we will not use.
1279  *
1280  * Since the environment can be relatively large, containing many variables
1281  * used only for boot loader purposes, we will use a blacklist based filter.
1282  * To keep the blacklist from growing too large, we use prefix based filtering.
1283  * This is possible because in many cases, the loader variable names are
1284  * using a structured layout.
1285  *
1286  * We will not overwrite already set properties.
1287  */
1288 static struct bop_blacklist {
1289 	const char *bl_name;
1290 	int bl_name_len;
1291 } bop_prop_blacklist[] = {
1292 	{ "ISADIR", sizeof ("ISADIR") },
1293 	{ "acpi", sizeof ("acpi") },
1294 	{ "autoboot_delay", sizeof ("autoboot_delay") },
1295 	{ "autoboot_delay", sizeof ("autoboot_delay") },
1296 	{ "beansi_", sizeof ("beansi_") },
1297 	{ "beastie", sizeof ("beastie") },
1298 	{ "bemenu", sizeof ("bemenu") },
1299 	{ "boot.", sizeof ("boot.") },
1300 	{ "bootenv", sizeof ("bootenv") },
1301 	{ "currdev", sizeof ("currdev") },
1302 	{ "dhcp.", sizeof ("dhcp.") },
1303 	{ "interpret", sizeof ("interpret") },
1304 	{ "kernel", sizeof ("kernel") },
1305 	{ "loaddev", sizeof ("loaddev") },
1306 	{ "loader_", sizeof ("loader_") },
1307 	{ "module_path", sizeof ("module_path") },
1308 	{ "nfs.", sizeof ("nfs.") },
1309 	{ "pcibios", sizeof ("pcibios") },
1310 	{ "prompt", sizeof ("prompt") },
1311 	{ "smbios", sizeof ("smbios") },
1312 	{ "tem", sizeof ("tem") },
1313 	{ "twiddle_divisor", sizeof ("twiddle_divisor") },
1314 	{ "zfs_be", sizeof ("zfs_be") },
1315 };
1316 
1317 /*
1318  * Match the name against prefixes in above blacklist. If the match was
1319  * found, this name is blacklisted.
1320  */
1321 static boolean_t
1322 name_is_blacklisted(const char *name)
1323 {
1324 	int i, n;
1325 
1326 	n = sizeof (bop_prop_blacklist) / sizeof (bop_prop_blacklist[0]);
1327 	for (i = 0; i < n; i++) {
1328 		if (strncmp(bop_prop_blacklist[i].bl_name, name,
1329 		    bop_prop_blacklist[i].bl_name_len - 1) == 0) {
1330 			return (B_TRUE);
1331 		}
1332 	}
1333 	return (B_FALSE);
1334 }
1335 
1336 static void
1337 process_boot_environment(struct boot_modules *benv)
1338 {
1339 	char *env, *ptr, *name, *value;
1340 	uint32_t size, name_len, value_len;
1341 
1342 	if (benv == NULL || benv->bm_type != BMT_ENV)
1343 		return;
1344 	ptr = env = benv->bm_addr;
1345 	size = benv->bm_size;
1346 	do {
1347 		name = ptr;
1348 		/* find '=' */
1349 		while (*ptr != '=') {
1350 			ptr++;
1351 			if (ptr > env + size) /* Something is very wrong. */
1352 				return;
1353 		}
1354 		name_len = ptr - name;
1355 		if (sizeof (buffer) <= name_len)
1356 			continue;
1357 
1358 		(void) strncpy(buffer, name, sizeof (buffer));
1359 		buffer[name_len] = '\0';
1360 		name = buffer;
1361 
1362 		value_len = 0;
1363 		value = ++ptr;
1364 		while ((uintptr_t)ptr - (uintptr_t)env < size) {
1365 			if (*ptr == '\0') {
1366 				ptr++;
1367 				value_len = (uintptr_t)ptr - (uintptr_t)env;
1368 				break;
1369 			}
1370 			ptr++;
1371 		}
1372 
1373 		/* Did we reach the end of the module? */
1374 		if (value_len == 0)
1375 			return;
1376 
1377 		if (*value == '\0')
1378 			continue;
1379 
1380 		/* Is this property already set? */
1381 		if (do_bsys_getproplen(NULL, name) >= 0)
1382 			continue;
1383 
1384 		/* Translate netboot variables */
1385 		if (strcmp(name, "boot.netif.gateway") == 0) {
1386 			bsetprops(BP_ROUTER_IP, value);
1387 			continue;
1388 		}
1389 		if (strcmp(name, "boot.netif.hwaddr") == 0) {
1390 			bsetprops(BP_BOOT_MAC, value);
1391 			continue;
1392 		}
1393 		if (strcmp(name, "boot.netif.ip") == 0) {
1394 			bsetprops(BP_HOST_IP, value);
1395 			continue;
1396 		}
1397 		if (strcmp(name, "boot.netif.netmask") == 0) {
1398 			bsetprops(BP_SUBNET_MASK, value);
1399 			continue;
1400 		}
1401 		if (strcmp(name, "boot.netif.server") == 0) {
1402 			bsetprops(BP_SERVER_IP, value);
1403 			continue;
1404 		}
1405 		if (strcmp(name, "boot.netif.server") == 0) {
1406 			if (do_bsys_getproplen(NULL, BP_SERVER_IP) < 0)
1407 				bsetprops(BP_SERVER_IP, value);
1408 			continue;
1409 		}
1410 		if (strcmp(name, "boot.nfsroot.server") == 0) {
1411 			if (do_bsys_getproplen(NULL, BP_SERVER_IP) < 0)
1412 				bsetprops(BP_SERVER_IP, value);
1413 			continue;
1414 		}
1415 		if (strcmp(name, "boot.nfsroot.path") == 0) {
1416 			bsetprops(BP_SERVER_PATH, value);
1417 			continue;
1418 		}
1419 
1420 		if (name_is_blacklisted(name) == B_TRUE)
1421 			continue;
1422 
1423 		/* Create new property. */
1424 		bsetprops(name, value);
1425 
1426 		/* Avoid reading past the module end. */
1427 		if (size <= (uintptr_t)ptr - (uintptr_t)env)
1428 			return;
1429 	} while (*ptr != '\0');
1430 }
1431 
1432 /*
1433  * 1st pass at building the table of boot properties. This includes:
1434  * - values set on the command line: -B a=x,b=y,c=z ....
1435  * - known values we just compute (ie. from xbp)
1436  * - values from /boot/solaris/bootenv.rc (ie. eeprom(1m) values)
1437  *
1438  * the grub command line looked like:
1439  * kernel boot-file [-B prop=value[,prop=value]...] [boot-args]
1440  *
1441  * whoami is the same as boot-file
1442  */
1443 static void
1444 build_boot_properties(struct xboot_info *xbp)
1445 {
1446 	char *name;
1447 	int name_len;
1448 	char *value;
1449 	int value_len;
1450 	struct boot_modules *bm, *rdbm, *benv = NULL;
1451 	char *propbuf;
1452 	int quoted = 0;
1453 	int boot_arg_len;
1454 	uint_t i, midx;
1455 	char modid[32];
1456 #ifndef __xpv
1457 	static int stdout_val = 0;
1458 	uchar_t boot_device;
1459 	char str[3];
1460 #endif
1461 
1462 	/*
1463 	 * These have to be done first, so that kobj_mount_root() works
1464 	 */
1465 	DBG_MSG("Building boot properties\n");
1466 	propbuf = do_bsys_alloc(NULL, NULL, MMU_PAGESIZE, 0);
1467 	DBG((uintptr_t)propbuf);
1468 	if (xbp->bi_module_cnt > 0) {
1469 		bm = xbp->bi_modules;
1470 		rdbm = NULL;
1471 		for (midx = i = 0; i < xbp->bi_module_cnt; i++) {
1472 			if (bm[i].bm_type == BMT_ROOTFS) {
1473 				rdbm = &bm[i];
1474 				continue;
1475 			}
1476 			if (bm[i].bm_type == BMT_HASH || bm[i].bm_name == NULL)
1477 				continue;
1478 
1479 			if (bm[i].bm_type == BMT_ENV) {
1480 				if (benv == NULL)
1481 					benv = &bm[i];
1482 				else
1483 					continue;
1484 			}
1485 
1486 			(void) snprintf(modid, sizeof (modid),
1487 			    "module-name-%u", midx);
1488 			bsetprops(modid, (char *)bm[i].bm_name);
1489 			(void) snprintf(modid, sizeof (modid),
1490 			    "module-addr-%u", midx);
1491 			bsetprop64(modid, (uint64_t)(uintptr_t)bm[i].bm_addr);
1492 			(void) snprintf(modid, sizeof (modid),
1493 			    "module-size-%u", midx);
1494 			bsetprop64(modid, (uint64_t)bm[i].bm_size);
1495 			++midx;
1496 		}
1497 		if (rdbm != NULL) {
1498 			bsetprop64("ramdisk_start",
1499 			    (uint64_t)(uintptr_t)rdbm->bm_addr);
1500 			bsetprop64("ramdisk_end",
1501 			    (uint64_t)(uintptr_t)rdbm->bm_addr + rdbm->bm_size);
1502 		}
1503 	}
1504 
1505 	/*
1506 	 * If there are any boot time modules or hashes present, then disable
1507 	 * fast reboot.
1508 	 */
1509 	if (xbp->bi_module_cnt > 1) {
1510 		fastreboot_disable(FBNS_BOOTMOD);
1511 	}
1512 
1513 #ifndef __xpv
1514 	/*
1515 	 * Disable fast reboot if we're using the Multiboot 2 boot protocol,
1516 	 * since we don't currently support MB2 info and module relocation.
1517 	 * Note that fast reboot will have already been disabled if multiple
1518 	 * modules are present, since the current implementation assumes that
1519 	 * we only have a single module, the boot_archive.
1520 	 */
1521 	if (xbp->bi_mb_version != 1) {
1522 		fastreboot_disable(FBNS_MULTIBOOT2);
1523 	}
1524 #endif
1525 
1526 	DBG_MSG("Parsing command line for boot properties\n");
1527 	value = xbp->bi_cmdline;
1528 
1529 	/*
1530 	 * allocate memory to collect boot_args into
1531 	 */
1532 	boot_arg_len = strlen(xbp->bi_cmdline) + 1;
1533 	boot_args = do_bsys_alloc(NULL, NULL, boot_arg_len, MMU_PAGESIZE);
1534 	boot_args[0] = 0;
1535 	boot_arg_len = 0;
1536 
1537 #ifdef __xpv
1538 	/*
1539 	 * Xen puts a lot of device information in front of the kernel name
1540 	 * let's grab them and make them boot properties.  The first
1541 	 * string w/o an "=" in it will be the boot-file property.
1542 	 */
1543 	(void) strcpy(namebuf, "xpv-");
1544 	for (;;) {
1545 		/*
1546 		 * get to next property
1547 		 */
1548 		while (ISSPACE(*value))
1549 			++value;
1550 		name = value;
1551 		/*
1552 		 * look for an "="
1553 		 */
1554 		while (*value && !ISSPACE(*value) && *value != '=') {
1555 			value++;
1556 		}
1557 		if (*value != '=') { /* no "=" in the property */
1558 			value = name;
1559 			break;
1560 		}
1561 		name_len = value - name;
1562 		value_len = 0;
1563 		/*
1564 		 * skip over the "="
1565 		 */
1566 		value++;
1567 		while (value[value_len] && !ISSPACE(value[value_len])) {
1568 			++value_len;
1569 		}
1570 		/*
1571 		 * build property name with "xpv-" prefix
1572 		 */
1573 		if (name_len + 4 > 32) { /* skip if name too long */
1574 			value += value_len;
1575 			continue;
1576 		}
1577 		bcopy(name, &namebuf[4], name_len);
1578 		name_len += 4;
1579 		namebuf[name_len] = 0;
1580 		bcopy(value, propbuf, value_len);
1581 		propbuf[value_len] = 0;
1582 		bsetprops(namebuf, propbuf);
1583 
1584 		/*
1585 		 * xpv-root is set to the logical disk name of the xen
1586 		 * VBD when booting from a disk-based filesystem.
1587 		 */
1588 		if (strcmp(namebuf, "xpv-root") == 0)
1589 			xen_vbdroot_props(propbuf);
1590 		/*
1591 		 * While we're here, if we have a "xpv-nfsroot" property
1592 		 * then we need to set "fstype" to "nfs" so we mount
1593 		 * our root from the nfs server.  Also parse the xpv-nfsroot
1594 		 * property to create the properties that nfs_mountroot will
1595 		 * need to find the root and mount it.
1596 		 */
1597 		if (strcmp(namebuf, "xpv-nfsroot") == 0)
1598 			xen_nfsroot_props(propbuf);
1599 
1600 		if (strcmp(namebuf, "xpv-ip") == 0)
1601 			xen_ip_props(propbuf);
1602 		value += value_len;
1603 	}
1604 #endif
1605 
1606 	while (ISSPACE(*value))
1607 		++value;
1608 	/*
1609 	 * value now points at the boot-file
1610 	 */
1611 	value_len = 0;
1612 	while (value[value_len] && !ISSPACE(value[value_len]))
1613 		++value_len;
1614 	if (value_len > 0) {
1615 		whoami = propbuf;
1616 		bcopy(value, whoami, value_len);
1617 		whoami[value_len] = 0;
1618 		bsetprops("boot-file", whoami);
1619 		/*
1620 		 * strip leading path stuff from whoami, so running from
1621 		 * PXE/miniroot makes sense.
1622 		 */
1623 		if (strstr(whoami, "/platform/") != NULL)
1624 			whoami = strstr(whoami, "/platform/");
1625 		bsetprops("whoami", whoami);
1626 	}
1627 
1628 	/*
1629 	 * Values forcibly set boot properties on the command line via -B.
1630 	 * Allow use of quotes in values. Other stuff goes on kernel
1631 	 * command line.
1632 	 */
1633 	name = value + value_len;
1634 	while (*name != 0) {
1635 		/*
1636 		 * anything not " -B" is copied to the command line
1637 		 */
1638 		if (!ISSPACE(name[0]) || name[1] != '-' || name[2] != 'B') {
1639 			boot_args[boot_arg_len++] = *name;
1640 			boot_args[boot_arg_len] = 0;
1641 			++name;
1642 			continue;
1643 		}
1644 
1645 		/*
1646 		 * skip the " -B" and following white space
1647 		 */
1648 		name += 3;
1649 		while (ISSPACE(*name))
1650 			++name;
1651 		while (*name && !ISSPACE(*name)) {
1652 			value = strstr(name, "=");
1653 			if (value == NULL)
1654 				break;
1655 			name_len = value - name;
1656 			++value;
1657 			value_len = 0;
1658 			quoted = 0;
1659 			for (; ; ++value_len) {
1660 				if (!value[value_len])
1661 					break;
1662 
1663 				/*
1664 				 * is this value quoted?
1665 				 */
1666 				if (value_len == 0 &&
1667 				    (value[0] == '\'' || value[0] == '"')) {
1668 					quoted = value[0];
1669 					++value_len;
1670 				}
1671 
1672 				/*
1673 				 * In the quote accept any character,
1674 				 * but look for ending quote.
1675 				 */
1676 				if (quoted) {
1677 					if (value[value_len] == quoted)
1678 						quoted = 0;
1679 					continue;
1680 				}
1681 
1682 				/*
1683 				 * a comma or white space ends the value
1684 				 */
1685 				if (value[value_len] == ',' ||
1686 				    ISSPACE(value[value_len]))
1687 					break;
1688 			}
1689 
1690 			if (value_len == 0) {
1691 				bsetprop(DDI_PROP_TYPE_ANY, name, name_len,
1692 				    NULL, 0);
1693 			} else {
1694 				char *v = value;
1695 				int l = value_len;
1696 				if (v[0] == v[l - 1] &&
1697 				    (v[0] == '\'' || v[0] == '"')) {
1698 					++v;
1699 					l -= 2;
1700 				}
1701 				bcopy(v, propbuf, l);
1702 				propbuf[l] = '\0';
1703 				bsetprop(DDI_PROP_TYPE_STRING, name, name_len,
1704 				    propbuf, l + 1);
1705 			}
1706 			name = value + value_len;
1707 			while (*name == ',')
1708 				++name;
1709 		}
1710 	}
1711 
1712 	/*
1713 	 * set boot-args property
1714 	 * 1275 name is bootargs, so set
1715 	 * that too
1716 	 */
1717 	bsetprops("boot-args", boot_args);
1718 	bsetprops("bootargs", boot_args);
1719 
1720 	process_boot_environment(benv);
1721 
1722 #ifndef __xpv
1723 	/*
1724 	 * Build boot command line for Fast Reboot
1725 	 */
1726 	build_fastboot_cmdline(xbp);
1727 
1728 	if (xbp->bi_mb_version == 1) {
1729 		multiboot_info_t *mbi = xbp->bi_mb_info;
1730 		int netboot;
1731 		struct sol_netinfo *sip;
1732 
1733 		/*
1734 		 * set the BIOS boot device from GRUB
1735 		 */
1736 		netboot = 0;
1737 
1738 		/*
1739 		 * Save various boot information for Fast Reboot
1740 		 */
1741 		save_boot_info(xbp);
1742 
1743 		if (mbi != NULL && mbi->flags & MB_INFO_BOOTDEV) {
1744 			boot_device = mbi->boot_device >> 24;
1745 			if (boot_device == 0x20)
1746 				netboot++;
1747 			str[0] = (boot_device >> 4) + '0';
1748 			str[1] = (boot_device & 0xf) + '0';
1749 			str[2] = 0;
1750 			bsetprops("bios-boot-device", str);
1751 		} else {
1752 			netboot = 1;
1753 		}
1754 
1755 		/*
1756 		 * In the netboot case, drives_info is overloaded with the
1757 		 * dhcp ack. This is not multiboot compliant and requires
1758 		 * special pxegrub!
1759 		 */
1760 		if (netboot && mbi->drives_length != 0) {
1761 			sip = (struct sol_netinfo *)(uintptr_t)mbi->drives_addr;
1762 			if (sip->sn_infotype == SN_TYPE_BOOTP)
1763 				bsetprop(DDI_PROP_TYPE_BYTE,
1764 				    "bootp-response",
1765 				    sizeof ("bootp-response"),
1766 				    (void *)(uintptr_t)mbi->drives_addr,
1767 				    mbi->drives_length);
1768 			else if (sip->sn_infotype == SN_TYPE_RARP)
1769 				setup_rarp_props(sip);
1770 		}
1771 	} else {
1772 		multiboot2_info_header_t *mbi = xbp->bi_mb_info;
1773 		multiboot_tag_bootdev_t *bootdev = NULL;
1774 		multiboot_tag_network_t *netdev = NULL;
1775 
1776 		if (mbi != NULL) {
1777 			bootdev = dboot_multiboot2_find_tag(mbi,
1778 			    MULTIBOOT_TAG_TYPE_BOOTDEV);
1779 			netdev = dboot_multiboot2_find_tag(mbi,
1780 			    MULTIBOOT_TAG_TYPE_NETWORK);
1781 		}
1782 		if (bootdev != NULL) {
1783 			DBG(bootdev->mb_biosdev);
1784 			boot_device = bootdev->mb_biosdev;
1785 			str[0] = (boot_device >> 4) + '0';
1786 			str[1] = (boot_device & 0xf) + '0';
1787 			str[2] = 0;
1788 			bsetprops("bios-boot-device", str);
1789 		}
1790 		if (netdev != NULL) {
1791 			bsetprop(DDI_PROP_TYPE_BYTE,
1792 			    "bootp-response", sizeof ("bootp-response"),
1793 			    (void *)(uintptr_t)netdev->mb_dhcpack,
1794 			    netdev->mb_size -
1795 			    sizeof (multiboot_tag_network_t));
1796 		}
1797 	}
1798 
1799 	bsetprop32("stdout", stdout_val);
1800 #endif /* __xpv */
1801 
1802 	/*
1803 	 * more conjured up values for made up things....
1804 	 */
1805 #if defined(__xpv)
1806 	bsetprops("mfg-name", "i86xpv");
1807 	bsetprops("impl-arch-name", "i86xpv");
1808 #else
1809 	bsetprops("mfg-name", "i86pc");
1810 	bsetprops("impl-arch-name", "i86pc");
1811 #endif
1812 
1813 	/*
1814 	 * Build firmware-provided system properties
1815 	 */
1816 	build_firmware_properties(xbp);
1817 
1818 	/*
1819 	 * XXPV
1820 	 *
1821 	 * Find out what these are:
1822 	 * - cpuid_feature_ecx_include
1823 	 * - cpuid_feature_ecx_exclude
1824 	 * - cpuid_feature_edx_include
1825 	 * - cpuid_feature_edx_exclude
1826 	 *
1827 	 * Find out what these are in multiboot:
1828 	 * - netdev-path
1829 	 * - fstype
1830 	 */
1831 }
1832 
1833 #ifdef __xpv
1834 /*
1835  * Under the Hypervisor, memory usable for DMA may be scarce. One
1836  * very likely large pool of DMA friendly memory is occupied by
1837  * the boot_archive, as it was loaded by grub into low MFNs.
1838  *
1839  * Here we free up that memory by copying the boot archive to what are
1840  * likely higher MFN pages and then swapping the mfn/pfn mappings.
1841  */
1842 #define	PFN_2GIG	0x80000
1843 static void
1844 relocate_boot_archive(struct xboot_info *xbp)
1845 {
1846 	mfn_t max_mfn = HYPERVISOR_memory_op(XENMEM_maximum_ram_page, NULL);
1847 	struct boot_modules *bm = xbp->bi_modules;
1848 	uintptr_t va;
1849 	pfn_t va_pfn;
1850 	mfn_t va_mfn;
1851 	caddr_t copy;
1852 	pfn_t copy_pfn;
1853 	mfn_t copy_mfn;
1854 	size_t	len;
1855 	int slop;
1856 	int total = 0;
1857 	int relocated = 0;
1858 	int mmu_update_return;
1859 	mmu_update_t t[2];
1860 	x86pte_t pte;
1861 
1862 	/*
1863 	 * If all MFN's are below 2Gig, don't bother doing this.
1864 	 */
1865 	if (max_mfn < PFN_2GIG)
1866 		return;
1867 	if (xbp->bi_module_cnt < 1) {
1868 		DBG_MSG("no boot_archive!");
1869 		return;
1870 	}
1871 
1872 	DBG_MSG("moving boot_archive to high MFN memory\n");
1873 	va = (uintptr_t)bm->bm_addr;
1874 	len = bm->bm_size;
1875 	slop = va & MMU_PAGEOFFSET;
1876 	if (slop) {
1877 		va += MMU_PAGESIZE - slop;
1878 		len -= MMU_PAGESIZE - slop;
1879 	}
1880 	len = P2ALIGN(len, MMU_PAGESIZE);
1881 
1882 	/*
1883 	 * Go through all boot_archive pages, swapping any low MFN pages
1884 	 * with memory at next_phys.
1885 	 */
1886 	while (len != 0) {
1887 		++total;
1888 		va_pfn = mmu_btop(va - ONE_GIG);
1889 		va_mfn = mfn_list[va_pfn];
1890 		if (mfn_list[va_pfn] < PFN_2GIG) {
1891 			copy = kbm_remap_window(next_phys, 1);
1892 			bcopy((void *)va, copy, MMU_PAGESIZE);
1893 			copy_pfn = mmu_btop(next_phys);
1894 			copy_mfn = mfn_list[copy_pfn];
1895 
1896 			pte = mfn_to_ma(copy_mfn) | PT_NOCONSIST | PT_VALID;
1897 			if (HYPERVISOR_update_va_mapping(va, pte,
1898 			    UVMF_INVLPG | UVMF_LOCAL))
1899 				bop_panic("relocate_boot_archive():  "
1900 				    "HYPERVISOR_update_va_mapping() failed");
1901 
1902 			mfn_list[va_pfn] = copy_mfn;
1903 			mfn_list[copy_pfn] = va_mfn;
1904 
1905 			t[0].ptr = mfn_to_ma(copy_mfn) | MMU_MACHPHYS_UPDATE;
1906 			t[0].val = va_pfn;
1907 			t[1].ptr = mfn_to_ma(va_mfn) | MMU_MACHPHYS_UPDATE;
1908 			t[1].val = copy_pfn;
1909 			if (HYPERVISOR_mmu_update(t, 2, &mmu_update_return,
1910 			    DOMID_SELF) != 0 || mmu_update_return != 2)
1911 				bop_panic("relocate_boot_archive():  "
1912 				    "HYPERVISOR_mmu_update() failed");
1913 
1914 			next_phys += MMU_PAGESIZE;
1915 			++relocated;
1916 		}
1917 		len -= MMU_PAGESIZE;
1918 		va += MMU_PAGESIZE;
1919 	}
1920 	DBG_MSG("Relocated pages:\n");
1921 	DBG(relocated);
1922 	DBG_MSG("Out of total pages:\n");
1923 	DBG(total);
1924 }
1925 #endif /* __xpv */
1926 
1927 #if !defined(__xpv)
1928 /*
1929  * simple description of a stack frame (args are 32 bit only currently)
1930  */
1931 typedef struct bop_frame {
1932 	struct bop_frame *old_frame;
1933 	pc_t retaddr;
1934 	long arg[1];
1935 } bop_frame_t;
1936 
1937 void
1938 bop_traceback(bop_frame_t *frame)
1939 {
1940 	pc_t pc;
1941 	int cnt;
1942 	char *ksym;
1943 	ulong_t off;
1944 
1945 	bop_printf(NULL, "Stack traceback:\n");
1946 	for (cnt = 0; cnt < 30; ++cnt) {	/* up to 30 frames */
1947 		pc = frame->retaddr;
1948 		if (pc == 0)
1949 			break;
1950 		ksym = kobj_getsymname(pc, &off);
1951 		if (ksym)
1952 			bop_printf(NULL, "  %s+%lx", ksym, off);
1953 		else
1954 			bop_printf(NULL, "  0x%lx", pc);
1955 
1956 		frame = frame->old_frame;
1957 		if (frame == 0) {
1958 			bop_printf(NULL, "\n");
1959 			break;
1960 		}
1961 		bop_printf(NULL, "\n");
1962 	}
1963 }
1964 
1965 struct trapframe {
1966 	ulong_t error_code;	/* optional */
1967 	ulong_t inst_ptr;
1968 	ulong_t code_seg;
1969 	ulong_t flags_reg;
1970 	ulong_t stk_ptr;
1971 	ulong_t stk_seg;
1972 };
1973 
1974 void
1975 bop_trap(ulong_t *tfp)
1976 {
1977 	struct trapframe *tf = (struct trapframe *)tfp;
1978 	bop_frame_t fakeframe;
1979 	static int depth = 0;
1980 
1981 	/*
1982 	 * Check for an infinite loop of traps.
1983 	 */
1984 	if (++depth > 2)
1985 		bop_panic("Nested trap");
1986 
1987 	bop_printf(NULL, "Unexpected trap\n");
1988 
1989 	/*
1990 	 * adjust the tf for optional error_code by detecting the code selector
1991 	 */
1992 	if (tf->code_seg != B64CODE_SEL)
1993 		tf = (struct trapframe *)(tfp - 1);
1994 	else
1995 		bop_printf(NULL, "error code           0x%lx\n",
1996 		    tf->error_code & 0xffffffff);
1997 
1998 	bop_printf(NULL, "instruction pointer  0x%lx\n", tf->inst_ptr);
1999 	bop_printf(NULL, "code segment         0x%lx\n", tf->code_seg & 0xffff);
2000 	bop_printf(NULL, "flags register       0x%lx\n", tf->flags_reg);
2001 	bop_printf(NULL, "return %%rsp          0x%lx\n", tf->stk_ptr);
2002 	bop_printf(NULL, "return %%ss           0x%lx\n", tf->stk_seg & 0xffff);
2003 
2004 	/* grab %[er]bp pushed by our code from the stack */
2005 	fakeframe.old_frame = (bop_frame_t *)*(tfp - 3);
2006 	fakeframe.retaddr = (pc_t)tf->inst_ptr;
2007 	bop_printf(NULL, "Attempting stack backtrace:\n");
2008 	bop_traceback(&fakeframe);
2009 	bop_panic("unexpected trap in early boot");
2010 }
2011 
2012 extern void bop_trap_handler(void);
2013 
2014 static gate_desc_t *bop_idt;
2015 
2016 static desctbr_t bop_idt_info;
2017 
2018 /*
2019  * Install a temporary IDT that lets us catch errors in the boot time code.
2020  * We shouldn't get any faults at all while this is installed, so we'll
2021  * just generate a traceback and exit.
2022  */
2023 static void
2024 bop_idt_init(void)
2025 {
2026 	int t;
2027 
2028 	bop_idt = (gate_desc_t *)
2029 	    do_bsys_alloc(NULL, NULL, MMU_PAGESIZE, MMU_PAGESIZE);
2030 	bzero(bop_idt, MMU_PAGESIZE);
2031 	for (t = 0; t < NIDT; ++t) {
2032 		/*
2033 		 * Note that since boot runs without a TSS, the
2034 		 * double fault handler cannot use an alternate stack (64-bit).
2035 		 */
2036 		set_gatesegd(&bop_idt[t], &bop_trap_handler, B64CODE_SEL,
2037 		    SDT_SYSIGT, TRP_KPL, 0);
2038 	}
2039 	bop_idt_info.dtr_limit = (NIDT * sizeof (gate_desc_t)) - 1;
2040 	bop_idt_info.dtr_base = (uintptr_t)bop_idt;
2041 	wr_idtr(&bop_idt_info);
2042 }
2043 #endif	/* !defined(__xpv) */
2044 
2045 /*
2046  * This is where we enter the kernel. It dummies up the boot_ops and
2047  * boot_syscalls vectors and jumps off to _kobj_boot()
2048  */
2049 void
2050 _start(struct xboot_info *xbp)
2051 {
2052 	bootops_t *bops = &bootop;
2053 	extern void _kobj_boot();
2054 
2055 	/*
2056 	 * 1st off - initialize the console for any error messages
2057 	 */
2058 	xbootp = xbp;
2059 #ifdef __xpv
2060 	HYPERVISOR_shared_info = (void *)xbp->bi_shared_info;
2061 	xen_info = xbp->bi_xen_start_info;
2062 #endif
2063 
2064 #ifndef __xpv
2065 	if (*((uint32_t *)(FASTBOOT_SWTCH_PA + FASTBOOT_STACK_OFFSET)) ==
2066 	    FASTBOOT_MAGIC) {
2067 		post_fastreboot = 1;
2068 		*((uint32_t *)(FASTBOOT_SWTCH_PA + FASTBOOT_STACK_OFFSET)) = 0;
2069 	}
2070 #endif
2071 
2072 	bcons_init(xbp);
2073 	have_console = 1;
2074 
2075 	/*
2076 	 * enable debugging
2077 	 */
2078 	if (find_boot_prop("kbm_debug") != NULL)
2079 		kbm_debug = 1;
2080 
2081 	DBG_MSG("\n\n*** Entered Solaris in _start() cmdline is: ");
2082 	DBG_MSG((char *)xbp->bi_cmdline);
2083 	DBG_MSG("\n\n\n");
2084 
2085 	/*
2086 	 * physavail is no longer used by startup
2087 	 */
2088 	bm.physinstalled = xbp->bi_phys_install;
2089 	bm.pcimem = xbp->bi_pcimem;
2090 	bm.rsvdmem = xbp->bi_rsvdmem;
2091 	bm.physavail = NULL;
2092 
2093 	/*
2094 	 * initialize the boot time allocator
2095 	 */
2096 	next_phys = xbp->bi_next_paddr;
2097 	DBG(next_phys);
2098 	next_virt = (uintptr_t)xbp->bi_next_vaddr;
2099 	DBG(next_virt);
2100 	DBG_MSG("Initializing boot time memory management...");
2101 #ifdef __xpv
2102 	{
2103 		xen_platform_parameters_t p;
2104 
2105 		/* This call shouldn't fail, dboot already did it once. */
2106 		(void) HYPERVISOR_xen_version(XENVER_platform_parameters, &p);
2107 		mfn_to_pfn_mapping = (pfn_t *)(xen_virt_start = p.virt_start);
2108 		DBG(xen_virt_start);
2109 	}
2110 #endif
2111 	kbm_init(xbp);
2112 	DBG_MSG("done\n");
2113 
2114 	/*
2115 	 * Fill in the bootops vector
2116 	 */
2117 	bops->bsys_version = BO_VERSION;
2118 	bops->boot_mem = &bm;
2119 	bops->bsys_alloc = do_bsys_alloc;
2120 	bops->bsys_free = do_bsys_free;
2121 	bops->bsys_getproplen = do_bsys_getproplen;
2122 	bops->bsys_getprop = do_bsys_getprop;
2123 	bops->bsys_nextprop = do_bsys_nextprop;
2124 	bops->bsys_printf = bop_printf;
2125 	bops->bsys_doint = do_bsys_doint;
2126 
2127 	/*
2128 	 * BOP_EALLOC() is no longer needed
2129 	 */
2130 	bops->bsys_ealloc = do_bsys_ealloc;
2131 
2132 #ifdef __xpv
2133 	/*
2134 	 * On domain 0 we need to free up some physical memory that is
2135 	 * usable for DMA. Since GRUB loaded the boot_archive, it is
2136 	 * sitting in low MFN memory. We'll relocated the boot archive
2137 	 * pages to high PFN memory.
2138 	 */
2139 	if (DOMAIN_IS_INITDOMAIN(xen_info))
2140 		relocate_boot_archive(xbp);
2141 #endif
2142 
2143 #ifndef __xpv
2144 	/*
2145 	 * Install an IDT to catch early pagefaults (shouldn't have any).
2146 	 * Also needed for kmdb.
2147 	 */
2148 	bop_idt_init();
2149 #endif
2150 
2151 	/*
2152 	 * Start building the boot properties from the command line
2153 	 */
2154 	DBG_MSG("Initializing boot properties:\n");
2155 	build_boot_properties(xbp);
2156 
2157 	if (find_boot_prop("prom_debug") || kbm_debug) {
2158 		char *value;
2159 
2160 		value = do_bsys_alloc(NULL, NULL, MMU_PAGESIZE, MMU_PAGESIZE);
2161 		boot_prop_display(value);
2162 	}
2163 
2164 	/*
2165 	 * jump into krtld...
2166 	 */
2167 	_kobj_boot(&bop_sysp, NULL, bops, NULL);
2168 }
2169 
2170 
2171 /*ARGSUSED*/
2172 static caddr_t
2173 no_more_alloc(bootops_t *bop, caddr_t virthint, size_t size, int align)
2174 {
2175 	panic("Attempt to bsys_alloc() too late\n");
2176 	return (NULL);
2177 }
2178 
2179 /*ARGSUSED*/
2180 static void
2181 no_more_free(bootops_t *bop, caddr_t virt, size_t size)
2182 {
2183 	panic("Attempt to bsys_free() too late\n");
2184 }
2185 
2186 void
2187 bop_no_more_mem(void)
2188 {
2189 	DBG(total_bop_alloc_scratch);
2190 	DBG(total_bop_alloc_kernel);
2191 	bootops->bsys_alloc = no_more_alloc;
2192 	bootops->bsys_free = no_more_free;
2193 }
2194 
2195 
2196 /*
2197  * Set ACPI firmware properties
2198  */
2199 
2200 static caddr_t
2201 vmap_phys(size_t length, paddr_t pa)
2202 {
2203 	paddr_t	start, end;
2204 	caddr_t	va;
2205 	size_t	len, page;
2206 
2207 #ifdef __xpv
2208 	pa = pfn_to_pa(xen_assign_pfn(mmu_btop(pa))) | (pa & MMU_PAGEOFFSET);
2209 #endif
2210 	start = P2ALIGN(pa, MMU_PAGESIZE);
2211 	end = P2ROUNDUP(pa + length, MMU_PAGESIZE);
2212 	len = end - start;
2213 	va = (caddr_t)alloc_vaddr(len, MMU_PAGESIZE);
2214 	for (page = 0; page < len; page += MMU_PAGESIZE)
2215 		kbm_map((uintptr_t)va + page, start + page, 0, 0);
2216 	return (va + (pa & MMU_PAGEOFFSET));
2217 }
2218 
2219 static uint8_t
2220 checksum_table(uint8_t *tp, size_t len)
2221 {
2222 	uint8_t sum = 0;
2223 
2224 	while (len-- > 0)
2225 		sum += *tp++;
2226 
2227 	return (sum);
2228 }
2229 
2230 static int
2231 valid_rsdp(ACPI_TABLE_RSDP *rp)
2232 {
2233 
2234 	/* validate the V1.x checksum */
2235 	if (checksum_table((uint8_t *)rp, ACPI_RSDP_CHECKSUM_LENGTH) != 0)
2236 		return (0);
2237 
2238 	/* If pre-ACPI 2.0, this is a valid RSDP */
2239 	if (rp->Revision < 2)
2240 		return (1);
2241 
2242 	/* validate the V2.x checksum */
2243 	if (checksum_table((uint8_t *)rp, ACPI_RSDP_XCHECKSUM_LENGTH) != 0)
2244 		return (0);
2245 
2246 	return (1);
2247 }
2248 
2249 /*
2250  * Scan memory range for an RSDP;
2251  * see ACPI 3.0 Spec, 5.2.5.1
2252  */
2253 static ACPI_TABLE_RSDP *
2254 scan_rsdp(paddr_t start, paddr_t end)
2255 {
2256 	ssize_t len  = end - start;
2257 	caddr_t ptr;
2258 
2259 	ptr = vmap_phys(len, start);
2260 	while (len > 0) {
2261 		if (strncmp(ptr, ACPI_SIG_RSDP, strlen(ACPI_SIG_RSDP)) == 0 &&
2262 		    valid_rsdp((ACPI_TABLE_RSDP *)ptr))
2263 			return ((ACPI_TABLE_RSDP *)ptr);
2264 
2265 		ptr += ACPI_RSDP_SCAN_STEP;
2266 		len -= ACPI_RSDP_SCAN_STEP;
2267 	}
2268 
2269 	return (NULL);
2270 }
2271 
2272 /*
2273  * Refer to ACPI 3.0 Spec, section 5.2.5.1 to understand this function
2274  */
2275 static ACPI_TABLE_RSDP *
2276 find_rsdp()
2277 {
2278 	ACPI_TABLE_RSDP *rsdp;
2279 	uint64_t rsdp_val = 0;
2280 	uint16_t *ebda_seg;
2281 	paddr_t  ebda_addr;
2282 
2283 	/* check for "acpi-root-tab" property */
2284 	if (do_bsys_getproplen(NULL, "acpi-root-tab") == sizeof (uint64_t)) {
2285 		(void) do_bsys_getprop(NULL, "acpi-root-tab", &rsdp_val);
2286 		if (rsdp_val != 0) {
2287 			rsdp = scan_rsdp(rsdp_val, rsdp_val + sizeof (*rsdp));
2288 			if (rsdp != NULL) {
2289 				if (kbm_debug) {
2290 					bop_printf(NULL,
2291 					    "Using RSDP from bootloader: "
2292 					    "0x%p\n", (void *)rsdp);
2293 				}
2294 				return (rsdp);
2295 			}
2296 		}
2297 	}
2298 
2299 	/*
2300 	 * Get the EBDA segment and scan the first 1K
2301 	 */
2302 	ebda_seg = (uint16_t *)vmap_phys(sizeof (uint16_t),
2303 	    ACPI_EBDA_PTR_LOCATION);
2304 	ebda_addr = *ebda_seg << 4;
2305 	rsdp = scan_rsdp(ebda_addr, ebda_addr + ACPI_EBDA_WINDOW_SIZE);
2306 	if (rsdp == NULL)
2307 		/* if EBDA doesn't contain RSDP, look in BIOS memory */
2308 		rsdp = scan_rsdp(ACPI_HI_RSDP_WINDOW_BASE,
2309 		    ACPI_HI_RSDP_WINDOW_BASE + ACPI_HI_RSDP_WINDOW_SIZE);
2310 	return (rsdp);
2311 }
2312 
2313 static ACPI_TABLE_HEADER *
2314 map_fw_table(paddr_t table_addr)
2315 {
2316 	ACPI_TABLE_HEADER *tp;
2317 	size_t len = MAX(sizeof (*tp), MMU_PAGESIZE);
2318 
2319 	/*
2320 	 * Map at least a page; if the table is larger than this, remap it
2321 	 */
2322 	tp = (ACPI_TABLE_HEADER *)vmap_phys(len, table_addr);
2323 	if (tp->Length > len)
2324 		tp = (ACPI_TABLE_HEADER *)vmap_phys(tp->Length, table_addr);
2325 	return (tp);
2326 }
2327 
2328 static ACPI_TABLE_HEADER *
2329 find_fw_table(char *signature)
2330 {
2331 	static int revision = 0;
2332 	static ACPI_TABLE_XSDT *xsdt;
2333 	static int len;
2334 	paddr_t xsdt_addr;
2335 	ACPI_TABLE_RSDP *rsdp;
2336 	ACPI_TABLE_HEADER *tp;
2337 	paddr_t table_addr;
2338 	int	n;
2339 
2340 	if (strlen(signature) != ACPI_NAME_SIZE)
2341 		return (NULL);
2342 
2343 	/*
2344 	 * Reading the ACPI 3.0 Spec, section 5.2.5.3 will help
2345 	 * understand this code.  If we haven't already found the RSDT/XSDT,
2346 	 * revision will be 0. Find the RSDP and check the revision
2347 	 * to find out whether to use the RSDT or XSDT.  If revision is
2348 	 * 0 or 1, use the RSDT and set internal revision to 1; if it is 2,
2349 	 * use the XSDT.  If the XSDT address is 0, though, fall back to
2350 	 * revision 1 and use the RSDT.
2351 	 */
2352 	if (revision == 0) {
2353 		if ((rsdp = find_rsdp()) != NULL) {
2354 			revision = rsdp->Revision;
2355 			/*
2356 			 * ACPI 6.0 states that current revision is 2
2357 			 * from acpi_table_rsdp definition:
2358 			 * Must be (0) for ACPI 1.0 or (2) for ACPI 2.0+
2359 			 */
2360 			if (revision > 2)
2361 				revision = 2;
2362 			switch (revision) {
2363 			case 2:
2364 				/*
2365 				 * Use the XSDT unless BIOS is buggy and
2366 				 * claims to be rev 2 but has a null XSDT
2367 				 * address
2368 				 */
2369 				xsdt_addr = rsdp->XsdtPhysicalAddress;
2370 				if (xsdt_addr != 0)
2371 					break;
2372 				/* FALLTHROUGH */
2373 			case 0:
2374 				/* treat RSDP rev 0 as revision 1 internally */
2375 				revision = 1;
2376 				/* FALLTHROUGH */
2377 			case 1:
2378 				/* use the RSDT for rev 0/1 */
2379 				xsdt_addr = rsdp->RsdtPhysicalAddress;
2380 				break;
2381 			default:
2382 				/* unknown revision */
2383 				revision = 0;
2384 				break;
2385 			}
2386 		}
2387 		if (revision == 0)
2388 			return (NULL);
2389 
2390 		/* cache the XSDT info */
2391 		xsdt = (ACPI_TABLE_XSDT *)map_fw_table(xsdt_addr);
2392 		len = (xsdt->Header.Length - sizeof (xsdt->Header)) /
2393 		    ((revision == 1) ? sizeof (uint32_t) : sizeof (uint64_t));
2394 	}
2395 
2396 	/*
2397 	 * Scan the table headers looking for a signature match
2398 	 */
2399 	for (n = 0; n < len; n++) {
2400 		ACPI_TABLE_RSDT *rsdt = (ACPI_TABLE_RSDT *)xsdt;
2401 		table_addr = (revision == 1) ? rsdt->TableOffsetEntry[n] :
2402 		    xsdt->TableOffsetEntry[n];
2403 
2404 		if (table_addr == 0)
2405 			continue;
2406 		tp = map_fw_table(table_addr);
2407 		if (strncmp(tp->Signature, signature, ACPI_NAME_SIZE) == 0) {
2408 			return (tp);
2409 		}
2410 	}
2411 	return (NULL);
2412 }
2413 
2414 static void
2415 process_mcfg(ACPI_TABLE_MCFG *tp)
2416 {
2417 	ACPI_MCFG_ALLOCATION *cfg_baap;
2418 	char *cfg_baa_endp;
2419 	int64_t ecfginfo[4];
2420 
2421 	cfg_baap = (ACPI_MCFG_ALLOCATION *)((uintptr_t)tp + sizeof (*tp));
2422 	cfg_baa_endp = ((char *)tp) + tp->Header.Length;
2423 	while ((char *)cfg_baap < cfg_baa_endp) {
2424 		if (cfg_baap->Address != 0 && cfg_baap->PciSegment == 0) {
2425 			ecfginfo[0] = cfg_baap->Address;
2426 			ecfginfo[1] = cfg_baap->PciSegment;
2427 			ecfginfo[2] = cfg_baap->StartBusNumber;
2428 			ecfginfo[3] = cfg_baap->EndBusNumber;
2429 			bsetprop(DDI_PROP_TYPE_INT64,
2430 			    MCFG_PROPNAME, strlen(MCFG_PROPNAME),
2431 			    ecfginfo, sizeof (ecfginfo));
2432 			break;
2433 		}
2434 		cfg_baap++;
2435 	}
2436 }
2437 
2438 #ifndef __xpv
2439 static void
2440 process_madt_entries(ACPI_TABLE_MADT *tp, uint32_t *cpu_countp,
2441     uint32_t *cpu_possible_countp, uint32_t *cpu_apicid_array)
2442 {
2443 	ACPI_SUBTABLE_HEADER *item, *end;
2444 	uint32_t cpu_count = 0;
2445 	uint32_t cpu_possible_count = 0;
2446 
2447 	/*
2448 	 * Determine number of CPUs and keep track of "final" APIC ID
2449 	 * for each CPU by walking through ACPI MADT processor list
2450 	 */
2451 	end = (ACPI_SUBTABLE_HEADER *)(tp->Header.Length + (uintptr_t)tp);
2452 	item = (ACPI_SUBTABLE_HEADER *)((uintptr_t)tp + sizeof (*tp));
2453 
2454 	while (item < end) {
2455 		switch (item->Type) {
2456 		case ACPI_MADT_TYPE_LOCAL_APIC: {
2457 			ACPI_MADT_LOCAL_APIC *cpu =
2458 			    (ACPI_MADT_LOCAL_APIC *) item;
2459 
2460 			if (cpu->LapicFlags & ACPI_MADT_ENABLED) {
2461 				if (cpu_apicid_array != NULL)
2462 					cpu_apicid_array[cpu_count] = cpu->Id;
2463 				cpu_count++;
2464 			}
2465 			cpu_possible_count++;
2466 			break;
2467 		}
2468 		case ACPI_MADT_TYPE_LOCAL_X2APIC: {
2469 			ACPI_MADT_LOCAL_X2APIC *cpu =
2470 			    (ACPI_MADT_LOCAL_X2APIC *) item;
2471 
2472 			if (cpu->LapicFlags & ACPI_MADT_ENABLED) {
2473 				if (cpu_apicid_array != NULL)
2474 					cpu_apicid_array[cpu_count] =
2475 					    cpu->LocalApicId;
2476 				cpu_count++;
2477 			}
2478 			cpu_possible_count++;
2479 			break;
2480 		}
2481 		default:
2482 			if (kbm_debug)
2483 				bop_printf(NULL, "MADT type %d\n", item->Type);
2484 			break;
2485 		}
2486 
2487 		item = (ACPI_SUBTABLE_HEADER *)((uintptr_t)item + item->Length);
2488 	}
2489 	if (cpu_countp)
2490 		*cpu_countp = cpu_count;
2491 	if (cpu_possible_countp)
2492 		*cpu_possible_countp = cpu_possible_count;
2493 }
2494 
2495 static void
2496 process_madt(ACPI_TABLE_MADT *tp)
2497 {
2498 	uint32_t cpu_count = 0;
2499 	uint32_t cpu_possible_count = 0;
2500 	uint32_t *cpu_apicid_array; /* x2APIC ID is 32bit! */
2501 
2502 	if (tp != NULL) {
2503 		/* count cpu's */
2504 		process_madt_entries(tp, &cpu_count, &cpu_possible_count, NULL);
2505 
2506 		cpu_apicid_array = (uint32_t *)do_bsys_alloc(NULL, NULL,
2507 		    cpu_count * sizeof (*cpu_apicid_array), MMU_PAGESIZE);
2508 		if (cpu_apicid_array == NULL)
2509 			bop_panic("Not enough memory for APIC ID array");
2510 
2511 		/* copy IDs */
2512 		process_madt_entries(tp, NULL, NULL, cpu_apicid_array);
2513 
2514 		/*
2515 		 * Make boot property for array of "final" APIC IDs for each
2516 		 * CPU
2517 		 */
2518 		bsetprop(DDI_PROP_TYPE_INT,
2519 		    BP_CPU_APICID_ARRAY, strlen(BP_CPU_APICID_ARRAY),
2520 		    cpu_apicid_array, cpu_count * sizeof (*cpu_apicid_array));
2521 	}
2522 
2523 	/*
2524 	 * Check whether property plat-max-ncpus is already set.
2525 	 */
2526 	if (do_bsys_getproplen(NULL, PLAT_MAX_NCPUS_NAME) < 0) {
2527 		/*
2528 		 * Set plat-max-ncpus to number of maximum possible CPUs given
2529 		 * in MADT if it hasn't been set.
2530 		 * There's no formal way to detect max possible CPUs supported
2531 		 * by platform according to ACPI spec3.0b. So current CPU
2532 		 * hotplug implementation expects that all possible CPUs will
2533 		 * have an entry in MADT table and set plat-max-ncpus to number
2534 		 * of entries in MADT.
2535 		 * With introducing of ACPI4.0, Maximum System Capability Table
2536 		 * (MSCT) provides maximum number of CPUs supported by platform.
2537 		 * If MSCT is unavailable, fall back to old way.
2538 		 */
2539 		if (tp != NULL)
2540 			bsetpropsi(PLAT_MAX_NCPUS_NAME, cpu_possible_count);
2541 	}
2542 
2543 	/*
2544 	 * Set boot property boot-max-ncpus to number of CPUs existing at
2545 	 * boot time. boot-max-ncpus is mainly used for optimization.
2546 	 */
2547 	if (tp != NULL)
2548 		bsetpropsi(BOOT_MAX_NCPUS_NAME, cpu_count);
2549 
2550 	/*
2551 	 * User-set boot-ncpus overrides firmware count
2552 	 */
2553 	if (do_bsys_getproplen(NULL, BOOT_NCPUS_NAME) >= 0)
2554 		return;
2555 
2556 	/*
2557 	 * Set boot property boot-ncpus to number of active CPUs given in MADT
2558 	 * if it hasn't been set yet.
2559 	 */
2560 	if (tp != NULL)
2561 		bsetpropsi(BOOT_NCPUS_NAME, cpu_count);
2562 }
2563 
2564 static void
2565 process_srat(ACPI_TABLE_SRAT *tp)
2566 {
2567 	ACPI_SUBTABLE_HEADER *item, *end;
2568 	int i;
2569 	int proc_num, mem_num;
2570 #pragma pack(1)
2571 	struct {
2572 		uint32_t domain;
2573 		uint32_t apic_id;
2574 		uint32_t sapic_id;
2575 	} processor;
2576 	struct {
2577 		uint32_t domain;
2578 		uint32_t x2apic_id;
2579 	} x2apic;
2580 	struct {
2581 		uint32_t domain;
2582 		uint64_t addr;
2583 		uint64_t length;
2584 		uint32_t flags;
2585 	} memory;
2586 #pragma pack()
2587 	char prop_name[30];
2588 	uint64_t maxmem = 0;
2589 
2590 	if (tp == NULL)
2591 		return;
2592 
2593 	proc_num = mem_num = 0;
2594 	end = (ACPI_SUBTABLE_HEADER *)(tp->Header.Length + (uintptr_t)tp);
2595 	item = (ACPI_SUBTABLE_HEADER *)((uintptr_t)tp + sizeof (*tp));
2596 	while (item < end) {
2597 		switch (item->Type) {
2598 		case ACPI_SRAT_TYPE_CPU_AFFINITY: {
2599 			ACPI_SRAT_CPU_AFFINITY *cpu =
2600 			    (ACPI_SRAT_CPU_AFFINITY *) item;
2601 
2602 			if (!(cpu->Flags & ACPI_SRAT_CPU_ENABLED))
2603 				break;
2604 			processor.domain = cpu->ProximityDomainLo;
2605 			for (i = 0; i < 3; i++)
2606 				processor.domain +=
2607 				    cpu->ProximityDomainHi[i] << ((i + 1) * 8);
2608 			processor.apic_id = cpu->ApicId;
2609 			processor.sapic_id = cpu->LocalSapicEid;
2610 			(void) snprintf(prop_name, 30, "acpi-srat-processor-%d",
2611 			    proc_num);
2612 			bsetprop(DDI_PROP_TYPE_INT,
2613 			    prop_name, strlen(prop_name), &processor,
2614 			    sizeof (processor));
2615 			proc_num++;
2616 			break;
2617 		}
2618 		case ACPI_SRAT_TYPE_MEMORY_AFFINITY: {
2619 			ACPI_SRAT_MEM_AFFINITY *mem =
2620 			    (ACPI_SRAT_MEM_AFFINITY *)item;
2621 
2622 			if (!(mem->Flags & ACPI_SRAT_MEM_ENABLED))
2623 				break;
2624 			memory.domain = mem->ProximityDomain;
2625 			memory.addr = mem->BaseAddress;
2626 			memory.length = mem->Length;
2627 			memory.flags = mem->Flags;
2628 			(void) snprintf(prop_name, 30, "acpi-srat-memory-%d",
2629 			    mem_num);
2630 			bsetprop(DDI_PROP_TYPE_INT,
2631 			    prop_name, strlen(prop_name), &memory,
2632 			    sizeof (memory));
2633 			if ((mem->Flags & ACPI_SRAT_MEM_HOT_PLUGGABLE) &&
2634 			    (memory.addr + memory.length > maxmem)) {
2635 				maxmem = memory.addr + memory.length;
2636 			}
2637 			mem_num++;
2638 			break;
2639 		}
2640 		case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY: {
2641 			ACPI_SRAT_X2APIC_CPU_AFFINITY *x2cpu =
2642 			    (ACPI_SRAT_X2APIC_CPU_AFFINITY *) item;
2643 
2644 			if (!(x2cpu->Flags & ACPI_SRAT_CPU_ENABLED))
2645 				break;
2646 			x2apic.domain = x2cpu->ProximityDomain;
2647 			x2apic.x2apic_id = x2cpu->ApicId;
2648 			(void) snprintf(prop_name, 30, "acpi-srat-processor-%d",
2649 			    proc_num);
2650 			bsetprop(DDI_PROP_TYPE_INT,
2651 			    prop_name, strlen(prop_name), &x2apic,
2652 			    sizeof (x2apic));
2653 			proc_num++;
2654 			break;
2655 		}
2656 		default:
2657 			if (kbm_debug)
2658 				bop_printf(NULL, "SRAT type %d\n", item->Type);
2659 			break;
2660 		}
2661 
2662 		item = (ACPI_SUBTABLE_HEADER *)
2663 		    (item->Length + (uintptr_t)item);
2664 	}
2665 
2666 	/*
2667 	 * The maximum physical address calculated from the SRAT table is more
2668 	 * accurate than that calculated from the MSCT table.
2669 	 */
2670 	if (maxmem != 0) {
2671 		plat_dr_physmax = btop(maxmem);
2672 	}
2673 }
2674 
2675 static void
2676 process_slit(ACPI_TABLE_SLIT *tp)
2677 {
2678 
2679 	/*
2680 	 * Check the number of localities; if it's too huge, we just
2681 	 * return and locality enumeration code will handle this later,
2682 	 * if possible.
2683 	 *
2684 	 * Note that the size of the table is the square of the
2685 	 * number of localities; if the number of localities exceeds
2686 	 * UINT16_MAX, the table size may overflow an int when being
2687 	 * passed to bsetprop() below.
2688 	 */
2689 	if (tp->LocalityCount >= SLIT_LOCALITIES_MAX)
2690 		return;
2691 
2692 	bsetprop64(SLIT_NUM_PROPNAME, tp->LocalityCount);
2693 	bsetprop(DDI_PROP_TYPE_BYTE,
2694 	    SLIT_PROPNAME, strlen(SLIT_PROPNAME), &tp->Entry,
2695 	    tp->LocalityCount * tp->LocalityCount);
2696 }
2697 
2698 static ACPI_TABLE_MSCT *
2699 process_msct(ACPI_TABLE_MSCT *tp)
2700 {
2701 	int last_seen = 0;
2702 	int proc_num = 0;
2703 	ACPI_MSCT_PROXIMITY *item, *end;
2704 	extern uint64_t plat_dr_options;
2705 
2706 	ASSERT(tp != NULL);
2707 
2708 	end = (ACPI_MSCT_PROXIMITY *)(tp->Header.Length + (uintptr_t)tp);
2709 	for (item = (void *)((uintptr_t)tp + tp->ProximityOffset);
2710 	    item < end;
2711 	    item = (void *)(item->Length + (uintptr_t)item)) {
2712 		/*
2713 		 * Sanity check according to section 5.2.19.1 of ACPI 4.0.
2714 		 * Revision	1
2715 		 * Length	22
2716 		 */
2717 		if (item->Revision != 1 || item->Length != 22) {
2718 			cmn_err(CE_CONT,
2719 			    "?boot: unknown proximity domain structure in MSCT "
2720 			    "with Revision(%d), Length(%d).\n",
2721 			    (int)item->Revision, (int)item->Length);
2722 			return (NULL);
2723 		} else if (item->RangeStart > item->RangeEnd) {
2724 			cmn_err(CE_CONT,
2725 			    "?boot: invalid proximity domain structure in MSCT "
2726 			    "with RangeStart(%u), RangeEnd(%u).\n",
2727 			    item->RangeStart, item->RangeEnd);
2728 			return (NULL);
2729 		} else if (item->RangeStart != last_seen) {
2730 			/*
2731 			 * Items must be organized in ascending order of the
2732 			 * proximity domain enumerations.
2733 			 */
2734 			cmn_err(CE_CONT,
2735 			    "?boot: invalid proximity domain structure in MSCT,"
2736 			    " items are not orginized in ascending order.\n");
2737 			return (NULL);
2738 		}
2739 
2740 		/*
2741 		 * If ProcessorCapacity is 0 then there would be no CPUs in this
2742 		 * domain.
2743 		 */
2744 		if (item->ProcessorCapacity != 0) {
2745 			proc_num += (item->RangeEnd - item->RangeStart + 1) *
2746 			    item->ProcessorCapacity;
2747 		}
2748 
2749 		last_seen = item->RangeEnd - item->RangeStart + 1;
2750 		/*
2751 		 * Break out if all proximity domains have been processed.
2752 		 * Some BIOSes may have unused items at the end of MSCT table.
2753 		 */
2754 		if (last_seen > tp->MaxProximityDomains) {
2755 			break;
2756 		}
2757 	}
2758 	if (last_seen != tp->MaxProximityDomains + 1) {
2759 		cmn_err(CE_CONT,
2760 		    "?boot: invalid proximity domain structure in MSCT, "
2761 		    "proximity domain count doesn't match.\n");
2762 		return (NULL);
2763 	}
2764 
2765 	/*
2766 	 * Set plat-max-ncpus property if it hasn't been set yet.
2767 	 */
2768 	if (do_bsys_getproplen(NULL, PLAT_MAX_NCPUS_NAME) < 0) {
2769 		if (proc_num != 0) {
2770 			bsetpropsi(PLAT_MAX_NCPUS_NAME, proc_num);
2771 		}
2772 	}
2773 
2774 	/*
2775 	 * Use Maximum Physical Address from the MSCT table as upper limit for
2776 	 * memory hot-adding by default. It may be overridden by value from
2777 	 * the SRAT table or the "plat-dr-physmax" boot option.
2778 	 */
2779 	plat_dr_physmax = btop(tp->MaxAddress + 1);
2780 
2781 	/*
2782 	 * Existence of MSCT implies CPU/memory hotplug-capability for the
2783 	 * platform.
2784 	 */
2785 	plat_dr_options |= PLAT_DR_FEATURE_CPU;
2786 	plat_dr_options |= PLAT_DR_FEATURE_MEMORY;
2787 
2788 	return (tp);
2789 }
2790 
2791 #else /* __xpv */
2792 static void
2793 enumerate_xen_cpus()
2794 {
2795 	processorid_t	id, max_id;
2796 
2797 	/*
2798 	 * User-set boot-ncpus overrides enumeration
2799 	 */
2800 	if (do_bsys_getproplen(NULL, BOOT_NCPUS_NAME) >= 0)
2801 		return;
2802 
2803 	/*
2804 	 * Probe every possible virtual CPU id and remember the
2805 	 * highest id present; the count of CPUs is one greater
2806 	 * than this.  This tacitly assumes at least cpu 0 is present.
2807 	 */
2808 	max_id = 0;
2809 	for (id = 0; id < MAX_VIRT_CPUS; id++)
2810 		if (HYPERVISOR_vcpu_op(VCPUOP_is_up, id, NULL) == 0)
2811 			max_id = id;
2812 
2813 	bsetpropsi(BOOT_NCPUS_NAME, max_id+1);
2814 
2815 }
2816 #endif /* __xpv */
2817 
2818 /*ARGSUSED*/
2819 static void
2820 build_firmware_properties(struct xboot_info *xbp)
2821 {
2822 	ACPI_TABLE_HEADER *tp = NULL;
2823 
2824 #ifndef __xpv
2825 	if (xbp->bi_uefi_arch == XBI_UEFI_ARCH_64) {
2826 		bsetprops("efi-systype", "64");
2827 		bsetprop64("efi-systab",
2828 		    (uint64_t)(uintptr_t)xbp->bi_uefi_systab);
2829 		if (kbm_debug)
2830 			bop_printf(NULL, "64-bit UEFI detected.\n");
2831 	} else if (xbp->bi_uefi_arch == XBI_UEFI_ARCH_32) {
2832 		bsetprops("efi-systype", "32");
2833 		bsetprop64("efi-systab",
2834 		    (uint64_t)(uintptr_t)xbp->bi_uefi_systab);
2835 		if (kbm_debug)
2836 			bop_printf(NULL, "32-bit UEFI detected.\n");
2837 	}
2838 
2839 	if (xbp->bi_acpi_rsdp != NULL) {
2840 		bsetprop64("acpi-root-tab",
2841 		    (uint64_t)(uintptr_t)xbp->bi_acpi_rsdp);
2842 	}
2843 
2844 	if (xbp->bi_smbios != NULL) {
2845 		bsetprop64("smbios-address",
2846 		    (uint64_t)(uintptr_t)xbp->bi_smbios);
2847 	}
2848 
2849 	if ((tp = find_fw_table(ACPI_SIG_MSCT)) != NULL)
2850 		msct_ptr = process_msct((ACPI_TABLE_MSCT *)tp);
2851 	else
2852 		msct_ptr = NULL;
2853 
2854 	if ((tp = find_fw_table(ACPI_SIG_MADT)) != NULL)
2855 		process_madt((ACPI_TABLE_MADT *)tp);
2856 
2857 	if ((srat_ptr = (ACPI_TABLE_SRAT *)
2858 	    find_fw_table(ACPI_SIG_SRAT)) != NULL)
2859 		process_srat(srat_ptr);
2860 
2861 	if (slit_ptr = (ACPI_TABLE_SLIT *)find_fw_table(ACPI_SIG_SLIT))
2862 		process_slit(slit_ptr);
2863 
2864 	tp = find_fw_table(ACPI_SIG_MCFG);
2865 #else /* __xpv */
2866 	enumerate_xen_cpus();
2867 	if (DOMAIN_IS_INITDOMAIN(xen_info))
2868 		tp = find_fw_table(ACPI_SIG_MCFG);
2869 #endif /* __xpv */
2870 	if (tp != NULL)
2871 		process_mcfg((ACPI_TABLE_MCFG *)tp);
2872 }
2873 
2874 /*
2875  * fake up a boot property for deferred early console output
2876  * this is used by both graphical boot and the (developer only)
2877  * USB serial console
2878  */
2879 void *
2880 defcons_init(size_t size)
2881 {
2882 	static char *p = NULL;
2883 
2884 	p = do_bsys_alloc(NULL, NULL, size, MMU_PAGESIZE);
2885 	*p = 0;
2886 	bsetprop32("deferred-console-buf", (uint32_t)((uintptr_t)&p));
2887 	return (p);
2888 }
2889 
2890 /*ARGSUSED*/
2891 int
2892 boot_compinfo(int fd, struct compinfo *cbp)
2893 {
2894 	cbp->iscmp = 0;
2895 	cbp->blksize = MAXBSIZE;
2896 	return (0);
2897 }
2898 
2899 #define	BP_MAX_STRLEN	32
2900 
2901 /*
2902  * Get value for given boot property
2903  */
2904 int
2905 bootprop_getval(const char *prop_name, u_longlong_t *prop_value)
2906 {
2907 	int		boot_prop_len;
2908 	char		str[BP_MAX_STRLEN];
2909 	u_longlong_t	value;
2910 
2911 	boot_prop_len = BOP_GETPROPLEN(bootops, prop_name);
2912 	if (boot_prop_len < 0 || boot_prop_len > sizeof (str) ||
2913 	    BOP_GETPROP(bootops, prop_name, str) < 0 ||
2914 	    kobj_getvalue(str, &value) == -1)
2915 		return (-1);
2916 
2917 	if (prop_value)
2918 		*prop_value = value;
2919 
2920 	return (0);
2921 }
2922