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