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