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