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