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