xref: /illumos-gate/usr/src/uts/i86pc/dboot/dboot_startkern.c (revision 111b966ed3721c1cee4814c5314de6f3a9c8081d)
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 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright 2020 Joyent, Inc.
27  */
28 
29 
30 #include <sys/types.h>
31 #include <sys/machparam.h>
32 #include <sys/x86_archext.h>
33 #include <sys/systm.h>
34 #include <sys/mach_mmu.h>
35 #include <sys/multiboot.h>
36 #include <sys/multiboot2.h>
37 #include <sys/multiboot2_impl.h>
38 #include <sys/sysmacros.h>
39 #include <sys/framebuffer.h>
40 #include <sys/sha1.h>
41 #include <util/string.h>
42 #include <util/strtolctype.h>
43 #include <sys/efi.h>
44 
45 /*
46  * Compile time debug knob. We do not have any early mechanism to control it
47  * as the boot is the earliest mechanism we have, and we do not want to have
48  * it being switched on by default.
49  */
50 int dboot_debug = 0;
51 
52 #if defined(__xpv)
53 
54 #include <sys/hypervisor.h>
55 uintptr_t xen_virt_start;
56 pfn_t *mfn_to_pfn_mapping;
57 
58 #else /* !__xpv */
59 
60 extern multiboot_header_t mb_header;
61 extern uint32_t mb2_load_addr;
62 extern int have_cpuid(void);
63 
64 #endif /* !__xpv */
65 
66 #include <sys/inttypes.h>
67 #include <sys/bootinfo.h>
68 #include <sys/mach_mmu.h>
69 #include <sys/boot_console.h>
70 
71 #include "dboot_asm.h"
72 #include "dboot_printf.h"
73 #include "dboot_xboot.h"
74 #include "dboot_elfload.h"
75 
76 #define	SHA1_ASCII_LENGTH	(SHA1_DIGEST_LENGTH * 2)
77 
78 /*
79  * This file contains code that runs to transition us from either a multiboot
80  * compliant loader (32 bit non-paging) or a XPV domain loader to
81  * regular kernel execution. Its task is to setup the kernel memory image
82  * and page tables.
83  *
84  * The code executes as:
85  *	- 32 bits under GRUB (for 32 or 64 bit Solaris)
86  *	- a 32 bit program for the 32-bit PV hypervisor
87  *	- a 64 bit program for the 64-bit PV hypervisor (at least for now)
88  *
89  * Under the PV hypervisor, we must create mappings for any memory beyond the
90  * initial start of day allocation (such as the kernel itself).
91  *
92  * When on the metal, the mapping between maddr_t and paddr_t is 1:1.
93  * Since we are running in real mode, so all such memory is accessible.
94  */
95 
96 /*
97  * Standard bits used in PTE (page level) and PTP (internal levels)
98  */
99 x86pte_t ptp_bits = PT_VALID | PT_REF | PT_WRITABLE | PT_USER;
100 x86pte_t pte_bits = PT_VALID | PT_REF | PT_WRITABLE | PT_MOD | PT_NOCONSIST;
101 
102 /*
103  * This is the target addresses (physical) where the kernel text and data
104  * nucleus pages will be unpacked. On the hypervisor this is actually a
105  * virtual address.
106  */
107 paddr_t ktext_phys;
108 uint32_t ksize = 2 * FOUR_MEG;	/* kernel nucleus is 8Meg */
109 
110 static uint64_t target_kernel_text;	/* value to use for KERNEL_TEXT */
111 
112 /*
113  * The stack is setup in assembler before entering startup_kernel()
114  */
115 char stack_space[STACK_SIZE];
116 
117 /*
118  * Used to track physical memory allocation
119  */
120 static paddr_t next_avail_addr = 0;
121 
122 #if defined(__xpv)
123 /*
124  * Additional information needed for hypervisor memory allocation.
125  * Only memory up to scratch_end is mapped by page tables.
126  * mfn_base is the start of the hypervisor virtual image. It's ONE_GIG, so
127  * to derive a pfn from a pointer, you subtract mfn_base.
128  */
129 
130 static paddr_t scratch_end = 0;	/* we can't write all of mem here */
131 static paddr_t mfn_base;		/* addr corresponding to mfn_list[0] */
132 start_info_t *xen_info;
133 
134 #else	/* __xpv */
135 
136 /*
137  * If on the metal, then we have a multiboot loader.
138  */
139 uint32_t mb_magic;			/* magic from boot loader */
140 uint32_t mb_addr;			/* multiboot info package from loader */
141 int multiboot_version;
142 multiboot_info_t *mb_info;
143 multiboot2_info_header_t *mb2_info;
144 int num_entries;			/* mmap entry count */
145 boolean_t num_entries_set;		/* is mmap entry count set */
146 uintptr_t load_addr;
147 static boot_framebuffer_t framebuffer __aligned(16);
148 static boot_framebuffer_t *fb;
149 
150 /* can not be automatic variables because of alignment */
151 static efi_guid_t smbios3 = SMBIOS3_TABLE_GUID;
152 static efi_guid_t smbios = SMBIOS_TABLE_GUID;
153 static efi_guid_t acpi2 = EFI_ACPI_TABLE_GUID;
154 static efi_guid_t acpi1 = ACPI_10_TABLE_GUID;
155 #endif	/* __xpv */
156 
157 /*
158  * This contains information passed to the kernel
159  */
160 struct xboot_info boot_info __aligned(16);
161 struct xboot_info *bi;
162 
163 /*
164  * Page table and memory stuff.
165  */
166 static paddr_t max_mem;			/* maximum memory address */
167 
168 /*
169  * Information about processor MMU
170  */
171 int amd64_support = 0;
172 int largepage_support = 0;
173 int pae_support = 0;
174 int pge_support = 0;
175 int NX_support = 0;
176 int PAT_support = 0;
177 
178 /*
179  * Low 32 bits of kernel entry address passed back to assembler.
180  * When running a 64 bit kernel, the high 32 bits are 0xffffffff.
181  */
182 uint32_t entry_addr_low;
183 
184 /*
185  * Memlists for the kernel. We shouldn't need a lot of these.
186  */
187 #define	MAX_MEMLIST (50)
188 struct boot_memlist memlists[MAX_MEMLIST];
189 uint_t memlists_used = 0;
190 struct boot_memlist pcimemlists[MAX_MEMLIST];
191 uint_t pcimemlists_used = 0;
192 struct boot_memlist rsvdmemlists[MAX_MEMLIST];
193 uint_t rsvdmemlists_used = 0;
194 
195 /*
196  * This should match what's in the bootloader.  It's arbitrary, but GRUB
197  * in particular has limitations on how much space it can use before it
198  * stops working properly.  This should be enough.
199  */
200 struct boot_modules modules[MAX_BOOT_MODULES];
201 uint_t modules_used = 0;
202 
203 #ifdef __xpv
204 /*
205  * Xen strips the size field out of the mb_memory_map_t, see struct e820entry
206  * definition in Xen source.
207  */
208 typedef struct {
209 	uint32_t	base_addr_low;
210 	uint32_t	base_addr_high;
211 	uint32_t	length_low;
212 	uint32_t	length_high;
213 	uint32_t	type;
214 } mmap_t;
215 
216 /*
217  * There is 512KB of scratch area after the boot stack page.
218  * We'll use that for everything except the kernel nucleus pages which are too
219  * big to fit there and are allocated last anyway.
220  */
221 #define	MAXMAPS	100
222 static mmap_t map_buffer[MAXMAPS];
223 #else
224 typedef mb_memory_map_t mmap_t;
225 #endif
226 
227 /*
228  * Debugging macros
229  */
230 uint_t prom_debug = 0;
231 uint_t map_debug = 0;
232 
233 static char noname[2] = "-";
234 
235 /*
236  * Either hypervisor-specific or grub-specific code builds the initial
237  * memlists. This code does the sort/merge/link for final use.
238  */
239 static void
sort_physinstall(void)240 sort_physinstall(void)
241 {
242 	int i;
243 #if !defined(__xpv)
244 	int j;
245 	struct boot_memlist tmp;
246 
247 	/*
248 	 * Now sort the memlists, in case they weren't in order.
249 	 * Yeah, this is a bubble sort; small, simple and easy to get right.
250 	 */
251 	DBG_MSG("Sorting phys-installed list\n");
252 	for (j = memlists_used - 1; j > 0; --j) {
253 		for (i = 0; i < j; ++i) {
254 			if (memlists[i].addr < memlists[i + 1].addr)
255 				continue;
256 			tmp = memlists[i];
257 			memlists[i] = memlists[i + 1];
258 			memlists[i + 1] = tmp;
259 		}
260 	}
261 
262 	/*
263 	 * Merge any memlists that don't have holes between them.
264 	 */
265 	for (i = 0; i <= memlists_used - 1; ++i) {
266 		if (memlists[i].addr + memlists[i].size != memlists[i + 1].addr)
267 			continue;
268 
269 		if (prom_debug)
270 			dboot_printf(
271 			    "merging mem segs %" PRIx64 "...%" PRIx64
272 			    " w/ %" PRIx64 "...%" PRIx64 "\n",
273 			    memlists[i].addr,
274 			    memlists[i].addr + memlists[i].size,
275 			    memlists[i + 1].addr,
276 			    memlists[i + 1].addr + memlists[i + 1].size);
277 
278 		memlists[i].size += memlists[i + 1].size;
279 		for (j = i + 1; j < memlists_used - 1; ++j)
280 			memlists[j] = memlists[j + 1];
281 		--memlists_used;
282 		DBG(memlists_used);
283 		--i;	/* after merging we need to reexamine, so do this */
284 	}
285 #endif	/* __xpv */
286 
287 	if (prom_debug) {
288 		dboot_printf("\nFinal memlists:\n");
289 		for (i = 0; i < memlists_used; ++i) {
290 			dboot_printf("\t%d: addr=%" PRIx64 " size=%"
291 			    PRIx64 "\n", i, memlists[i].addr, memlists[i].size);
292 		}
293 	}
294 
295 	/*
296 	 * link together the memlists with native size pointers
297 	 */
298 	memlists[0].next = 0;
299 	memlists[0].prev = 0;
300 	for (i = 1; i < memlists_used; ++i) {
301 		memlists[i].prev = (native_ptr_t)(uintptr_t)(memlists + i - 1);
302 		memlists[i].next = 0;
303 		memlists[i - 1].next = (native_ptr_t)(uintptr_t)(memlists + i);
304 	}
305 	bi->bi_phys_install = (native_ptr_t)(uintptr_t)memlists;
306 	DBG(bi->bi_phys_install);
307 }
308 
309 /*
310  * build bios reserved memlists
311  */
312 static void
build_rsvdmemlists(void)313 build_rsvdmemlists(void)
314 {
315 	int i;
316 
317 	rsvdmemlists[0].next = 0;
318 	rsvdmemlists[0].prev = 0;
319 	for (i = 1; i < rsvdmemlists_used; ++i) {
320 		rsvdmemlists[i].prev =
321 		    (native_ptr_t)(uintptr_t)(rsvdmemlists + i - 1);
322 		rsvdmemlists[i].next = 0;
323 		rsvdmemlists[i - 1].next =
324 		    (native_ptr_t)(uintptr_t)(rsvdmemlists + i);
325 	}
326 	bi->bi_rsvdmem = (native_ptr_t)(uintptr_t)rsvdmemlists;
327 	DBG(bi->bi_rsvdmem);
328 }
329 
330 #if defined(__xpv)
331 
332 /*
333  * halt on the hypervisor after a delay to drain console output
334  */
335 __NORETURN void
dboot_halt(void)336 dboot_halt(void)
337 {
338 	uint_t i = 10000;
339 
340 	while (--i)
341 		(void) HYPERVISOR_yield();
342 	(void) HYPERVISOR_shutdown(SHUTDOWN_poweroff);
343 	/* never reached */
344 	for (;;)
345 		;
346 }
347 
348 /*
349  * From a machine address, find the corresponding pseudo-physical address.
350  * Pseudo-physical address are contiguous and run from mfn_base in each VM.
351  * Machine addresses are the real underlying hardware addresses.
352  * These are needed for page table entries. Note that this routine is
353  * poorly protected. A bad value of "ma" will cause a page fault.
354  */
355 paddr_t
ma_to_pa(maddr_t ma)356 ma_to_pa(maddr_t ma)
357 {
358 	ulong_t pgoff = ma & MMU_PAGEOFFSET;
359 	ulong_t pfn = mfn_to_pfn_mapping[mmu_btop(ma)];
360 	paddr_t pa;
361 
362 	if (pfn >= xen_info->nr_pages)
363 		return (-(paddr_t)1);
364 	pa = mfn_base + mmu_ptob((paddr_t)pfn) + pgoff;
365 #ifdef DEBUG
366 	if (ma != pa_to_ma(pa))
367 		dboot_printf("ma_to_pa(%" PRIx64 ") got %" PRIx64 ", "
368 		    "pa_to_ma() says %" PRIx64 "\n", ma, pa, pa_to_ma(pa));
369 #endif
370 	return (pa);
371 }
372 
373 /*
374  * From a pseudo-physical address, find the corresponding machine address.
375  */
376 maddr_t
pa_to_ma(paddr_t pa)377 pa_to_ma(paddr_t pa)
378 {
379 	pfn_t pfn;
380 	ulong_t mfn;
381 
382 	pfn = mmu_btop(pa - mfn_base);
383 	if (pa < mfn_base || pfn >= xen_info->nr_pages)
384 		dboot_panic("pa_to_ma(): illegal address 0x%lx", (ulong_t)pa);
385 	mfn = ((ulong_t *)xen_info->mfn_list)[pfn];
386 #ifdef DEBUG
387 	if (mfn_to_pfn_mapping[mfn] != pfn)
388 		dboot_printf("pa_to_ma(pfn=%lx) got %lx ma_to_pa() says %lx\n",
389 		    pfn, mfn, mfn_to_pfn_mapping[mfn]);
390 #endif
391 	return (mfn_to_ma(mfn) | (pa & MMU_PAGEOFFSET));
392 }
393 
394 #endif	/* __xpv */
395 
396 x86pte_t
get_pteval(paddr_t table,uint_t index)397 get_pteval(paddr_t table, uint_t index)
398 {
399 	if (pae_support)
400 		return (((x86pte_t *)(uintptr_t)table)[index]);
401 	return (((x86pte32_t *)(uintptr_t)table)[index]);
402 }
403 
404 /*ARGSUSED*/
405 void
set_pteval(paddr_t table,uint_t index,uint_t level,x86pte_t pteval)406 set_pteval(paddr_t table, uint_t index, uint_t level, x86pte_t pteval)
407 {
408 #ifdef __xpv
409 	mmu_update_t t;
410 	maddr_t mtable = pa_to_ma(table);
411 	int retcnt;
412 
413 	t.ptr = (mtable + index * pte_size) | MMU_NORMAL_PT_UPDATE;
414 	t.val = pteval;
415 	if (HYPERVISOR_mmu_update(&t, 1, &retcnt, DOMID_SELF) || retcnt != 1)
416 		dboot_panic("HYPERVISOR_mmu_update() failed");
417 #else /* __xpv */
418 	uintptr_t tab_addr = (uintptr_t)table;
419 
420 	if (pae_support)
421 		((x86pte_t *)tab_addr)[index] = pteval;
422 	else
423 		((x86pte32_t *)tab_addr)[index] = (x86pte32_t)pteval;
424 	if (level == top_level && level == 2)
425 		reload_cr3();
426 #endif /* __xpv */
427 }
428 
429 paddr_t
make_ptable(x86pte_t * pteval,uint_t level)430 make_ptable(x86pte_t *pteval, uint_t level)
431 {
432 	paddr_t new_table = (paddr_t)(uintptr_t)mem_alloc(MMU_PAGESIZE);
433 
434 	if (level == top_level && level == 2)
435 		*pteval = pa_to_ma((uintptr_t)new_table) | PT_VALID;
436 	else
437 		*pteval = pa_to_ma((uintptr_t)new_table) | ptp_bits;
438 
439 #ifdef __xpv
440 	/* Remove write permission to the new page table. */
441 	if (HYPERVISOR_update_va_mapping(new_table,
442 	    *pteval & ~(x86pte_t)PT_WRITABLE, UVMF_INVLPG | UVMF_LOCAL))
443 		dboot_panic("HYP_update_va_mapping error");
444 #endif
445 
446 	if (map_debug)
447 		dboot_printf("new page table lvl=%d paddr=0x%lx ptp=0x%"
448 		    PRIx64 "\n", level, (ulong_t)new_table, *pteval);
449 	return (new_table);
450 }
451 
452 x86pte_t *
map_pte(paddr_t table,uint_t index)453 map_pte(paddr_t table, uint_t index)
454 {
455 	return ((x86pte_t *)(uintptr_t)(table + index * pte_size));
456 }
457 
458 /*
459  * dump out the contents of page tables...
460  */
461 static void
dump_tables(void)462 dump_tables(void)
463 {
464 	uint_t save_index[4];	/* for recursion */
465 	char *save_table[4];	/* for recursion */
466 	uint_t	l;
467 	uint64_t va;
468 	uint64_t pgsize;
469 	int index;
470 	int i;
471 	x86pte_t pteval;
472 	char *table;
473 	static char *tablist = "\t\t\t";
474 	char *tabs = tablist + 3 - top_level;
475 	uint_t pa, pa1;
476 #if !defined(__xpv)
477 #define	maddr_t paddr_t
478 #endif /* !__xpv */
479 
480 	dboot_printf("Finished pagetables:\n");
481 	table = (char *)(uintptr_t)top_page_table;
482 	l = top_level;
483 	va = 0;
484 	for (index = 0; index < ptes_per_table; ++index) {
485 		pgsize = 1ull << shift_amt[l];
486 		if (pae_support)
487 			pteval = ((x86pte_t *)table)[index];
488 		else
489 			pteval = ((x86pte32_t *)table)[index];
490 		if (pteval == 0)
491 			goto next_entry;
492 
493 		dboot_printf("%s %p[0x%x] = %" PRIx64 ", va=%" PRIx64,
494 		    tabs + l, (void *)table, index, (uint64_t)pteval, va);
495 		pa = ma_to_pa(pteval & MMU_PAGEMASK);
496 		dboot_printf(" physaddr=%x\n", pa);
497 
498 		/*
499 		 * Don't try to walk hypervisor private pagetables
500 		 */
501 		if ((l > 1 || (l == 1 && (pteval & PT_PAGESIZE) == 0))) {
502 			save_table[l] = table;
503 			save_index[l] = index;
504 			--l;
505 			index = -1;
506 			table = (char *)(uintptr_t)
507 			    ma_to_pa(pteval & MMU_PAGEMASK);
508 			goto recursion;
509 		}
510 
511 		/*
512 		 * shorten dump for consecutive mappings
513 		 */
514 		for (i = 1; index + i < ptes_per_table; ++i) {
515 			if (pae_support)
516 				pteval = ((x86pte_t *)table)[index + i];
517 			else
518 				pteval = ((x86pte32_t *)table)[index + i];
519 			if (pteval == 0)
520 				break;
521 			pa1 = ma_to_pa(pteval & MMU_PAGEMASK);
522 			if (pa1 != pa + i * pgsize)
523 				break;
524 		}
525 		if (i > 2) {
526 			dboot_printf("%s...\n", tabs + l);
527 			va += pgsize * (i - 2);
528 			index += i - 2;
529 		}
530 next_entry:
531 		va += pgsize;
532 		if (l == 3 && index == 255)	/* VA hole */
533 			va = 0xffff800000000000ull;
534 recursion:
535 		;
536 	}
537 	if (l < top_level) {
538 		++l;
539 		index = save_index[l];
540 		table = save_table[l];
541 		goto recursion;
542 	}
543 }
544 
545 /*
546  * Add a mapping for the machine page at the given virtual address.
547  */
548 static void
map_ma_at_va(maddr_t ma,native_ptr_t va,uint_t level)549 map_ma_at_va(maddr_t ma, native_ptr_t va, uint_t level)
550 {
551 	x86pte_t *ptep;
552 	x86pte_t pteval;
553 
554 	pteval = ma | pte_bits;
555 	if (level > 0)
556 		pteval |= PT_PAGESIZE;
557 	if (va >= target_kernel_text && pge_support)
558 		pteval |= PT_GLOBAL;
559 
560 	if (map_debug && ma != va)
561 		dboot_printf("mapping ma=0x%" PRIx64 " va=0x%" PRIx64
562 		    " pte=0x%" PRIx64 " l=%d\n",
563 		    (uint64_t)ma, (uint64_t)va, pteval, level);
564 
565 #if defined(__xpv)
566 	/*
567 	 * see if we can avoid find_pte() on the hypervisor
568 	 */
569 	if (HYPERVISOR_update_va_mapping(va, pteval,
570 	    UVMF_INVLPG | UVMF_LOCAL) == 0)
571 		return;
572 #endif
573 
574 	/*
575 	 * Find the pte that will map this address. This creates any
576 	 * missing intermediate level page tables
577 	 */
578 	ptep = find_pte(va, NULL, level, 0);
579 
580 	/*
581 	 * When paravirtualized, we must use hypervisor calls to modify the
582 	 * PTE, since paging is active. On real hardware we just write to
583 	 * the pagetables which aren't in use yet.
584 	 */
585 #if defined(__xpv)
586 	ptep = ptep;	/* shut lint up */
587 	if (HYPERVISOR_update_va_mapping(va, pteval, UVMF_INVLPG | UVMF_LOCAL))
588 		dboot_panic("mmu_update failed-map_pa_at_va va=0x%" PRIx64
589 		    " l=%d ma=0x%" PRIx64 ", pte=0x%" PRIx64 "",
590 		    (uint64_t)va, level, (uint64_t)ma, pteval);
591 #else
592 	if (va < 1024 * 1024)
593 		pteval |= PT_NOCACHE;		/* for video RAM */
594 	if (pae_support)
595 		*ptep = pteval;
596 	else
597 		*((x86pte32_t *)ptep) = (x86pte32_t)pteval;
598 #endif
599 }
600 
601 /*
602  * Add a mapping for the physical page at the given virtual address.
603  */
604 static void
map_pa_at_va(paddr_t pa,native_ptr_t va,uint_t level)605 map_pa_at_va(paddr_t pa, native_ptr_t va, uint_t level)
606 {
607 	map_ma_at_va(pa_to_ma(pa), va, level);
608 }
609 
610 /*
611  * This is called to remove start..end from the
612  * possible range of PCI addresses.
613  */
614 const uint64_t pci_lo_limit = 0x00100000ul;
615 const uint64_t pci_hi_limit = 0xfff00000ul;
616 static void
exclude_from_pci(uint64_t start,uint64_t end)617 exclude_from_pci(uint64_t start, uint64_t end)
618 {
619 	int i;
620 	int j;
621 	struct boot_memlist *ml;
622 
623 	for (i = 0; i < pcimemlists_used; ++i) {
624 		ml = &pcimemlists[i];
625 
626 		/* delete the entire range? */
627 		if (start <= ml->addr && ml->addr + ml->size <= end) {
628 			--pcimemlists_used;
629 			for (j = i; j < pcimemlists_used; ++j)
630 				pcimemlists[j] = pcimemlists[j + 1];
631 			--i;	/* to revisit the new one at this index */
632 		}
633 
634 		/* split a range? */
635 		else if (ml->addr < start && end < ml->addr + ml->size) {
636 
637 			++pcimemlists_used;
638 			if (pcimemlists_used > MAX_MEMLIST)
639 				dboot_panic("too many pcimemlists");
640 
641 			for (j = pcimemlists_used - 1; j > i; --j)
642 				pcimemlists[j] = pcimemlists[j - 1];
643 			ml->size = start - ml->addr;
644 
645 			++ml;
646 			ml->size = (ml->addr + ml->size) - end;
647 			ml->addr = end;
648 			++i;	/* skip on to next one */
649 		}
650 
651 		/* cut memory off the start? */
652 		else if (ml->addr < end && end < ml->addr + ml->size) {
653 			ml->size -= end - ml->addr;
654 			ml->addr = end;
655 		}
656 
657 		/* cut memory off the end? */
658 		else if (ml->addr <= start && start < ml->addr + ml->size) {
659 			ml->size = start - ml->addr;
660 		}
661 	}
662 }
663 
664 /*
665  * During memory allocation, find the highest address not used yet.
666  */
667 static void
check_higher(paddr_t a)668 check_higher(paddr_t a)
669 {
670 	if (a < next_avail_addr)
671 		return;
672 	next_avail_addr = RNDUP(a + 1, MMU_PAGESIZE);
673 	DBG(next_avail_addr);
674 }
675 
676 static int
dboot_loader_mmap_entries(void)677 dboot_loader_mmap_entries(void)
678 {
679 #if !defined(__xpv)
680 	if (num_entries_set == B_TRUE)
681 		return (num_entries);
682 
683 	switch (multiboot_version) {
684 	case 1:
685 		DBG(mb_info->flags);
686 		if (mb_info->flags & 0x40) {
687 			mb_memory_map_t *mmap;
688 			caddr32_t mmap_addr;
689 
690 			DBG(mb_info->mmap_addr);
691 			DBG(mb_info->mmap_length);
692 			check_higher(mb_info->mmap_addr + mb_info->mmap_length);
693 
694 			for (mmap_addr = mb_info->mmap_addr;
695 			    mmap_addr < mb_info->mmap_addr +
696 			    mb_info->mmap_length;
697 			    mmap_addr += mmap->size + sizeof (mmap->size)) {
698 				mmap = (mb_memory_map_t *)(uintptr_t)mmap_addr;
699 				++num_entries;
700 			}
701 
702 			num_entries_set = B_TRUE;
703 		}
704 		break;
705 	case 2:
706 		num_entries = dboot_multiboot2_efi_mmap_nentries(mb2_info);
707 		if (num_entries == 0)
708 			num_entries = dboot_multiboot2_mmap_nentries(mb2_info);
709 		if (num_entries == 0)
710 			dboot_panic("No memory map?\n");
711 		num_entries_set = B_TRUE;
712 		break;
713 	default:
714 		dboot_panic("Unknown multiboot version: %d\n",
715 		    multiboot_version);
716 		break;
717 	}
718 	return (num_entries);
719 #else
720 	return (MAXMAPS);
721 #endif
722 }
723 
724 #if !defined(__xpv)
725 static uint32_t
dboot_efi_to_smap_type(int index,uint32_t type)726 dboot_efi_to_smap_type(int index, uint32_t type)
727 {
728 	uint64_t addr;
729 
730 	/*
731 	 * ACPI 6.1 tells the lower memory should be reported as
732 	 * normal memory, so we enforce page 0 type even as
733 	 * vmware maps it as acpi reclaimable.
734 	 */
735 	if (dboot_multiboot2_efi_mmap_get_base(mb2_info, index, &addr)) {
736 		if (addr == 0)
737 			return (1);
738 	}
739 
740 	/* translate UEFI memory types to SMAP types */
741 	switch (type) {
742 	case EfiLoaderCode:
743 	case EfiLoaderData:
744 	case EfiBootServicesCode:
745 	case EfiBootServicesData:
746 	case EfiConventionalMemory:
747 		return (1);
748 	case EfiReservedMemoryType:
749 	case EfiRuntimeServicesCode:
750 	case EfiRuntimeServicesData:
751 	case EfiMemoryMappedIO:
752 	case EfiMemoryMappedIOPortSpace:
753 	case EfiPalCode:
754 	case EfiUnusableMemory:
755 		return (2);
756 	case EfiACPIReclaimMemory:
757 		return (3);
758 	case EfiACPIMemoryNVS:
759 		return (4);
760 	}
761 
762 	return (2);
763 }
764 #endif
765 
766 static uint32_t
dboot_loader_mmap_get_type(int index)767 dboot_loader_mmap_get_type(int index)
768 {
769 #if !defined(__xpv)
770 	mb_memory_map_t *mp, *mpend;
771 	uint32_t type;
772 	int i;
773 
774 	switch (multiboot_version) {
775 	case 1:
776 		mp = (mb_memory_map_t *)(uintptr_t)mb_info->mmap_addr;
777 		mpend = (mb_memory_map_t *)(uintptr_t)
778 		    (mb_info->mmap_addr + mb_info->mmap_length);
779 
780 		for (i = 0; mp < mpend && i != index; i++)
781 			mp = (mb_memory_map_t *)((uintptr_t)mp + mp->size +
782 			    sizeof (mp->size));
783 		if (mp >= mpend) {
784 			dboot_panic("dboot_loader_mmap_get_type(): index "
785 			    "out of bounds: %d\n", index);
786 		}
787 		return (mp->type);
788 
789 	case 2:
790 		if (dboot_multiboot2_efi_mmap_get_type(mb2_info, index, &type))
791 			return (dboot_efi_to_smap_type(index, type));
792 
793 		if (dboot_multiboot2_mmap_get_type(mb2_info, index, &type))
794 			return (type);
795 
796 		dboot_panic("Can not get memory type for %d\n", index);
797 
798 	default:
799 		dboot_panic("Unknown multiboot version: %d\n",
800 		    multiboot_version);
801 		break;
802 	}
803 	return (0);
804 #else
805 	return (map_buffer[index].type);
806 #endif
807 }
808 
809 static uint64_t
dboot_loader_mmap_get_base(int index)810 dboot_loader_mmap_get_base(int index)
811 {
812 #if !defined(__xpv)
813 	mb_memory_map_t *mp, *mpend;
814 	uint64_t base;
815 	int i;
816 
817 	switch (multiboot_version) {
818 	case 1:
819 		mp = (mb_memory_map_t *)mb_info->mmap_addr;
820 		mpend = (mb_memory_map_t *)
821 		    (mb_info->mmap_addr + mb_info->mmap_length);
822 
823 		for (i = 0; mp < mpend && i != index; i++)
824 			mp = (mb_memory_map_t *)((uintptr_t)mp + mp->size +
825 			    sizeof (mp->size));
826 		if (mp >= mpend) {
827 			dboot_panic("dboot_loader_mmap_get_base(): index "
828 			    "out of bounds: %d\n", index);
829 		}
830 		return (((uint64_t)mp->base_addr_high << 32) +
831 		    (uint64_t)mp->base_addr_low);
832 
833 	case 2:
834 		if (dboot_multiboot2_efi_mmap_get_base(mb2_info, index, &base))
835 			return (base);
836 
837 		if (dboot_multiboot2_mmap_get_base(mb2_info, index, &base))
838 			return (base);
839 
840 		dboot_panic("Can not get memory address for %d\n", index);
841 
842 	default:
843 		dboot_panic("Unknown multiboot version: %d\n",
844 		    multiboot_version);
845 		break;
846 	}
847 	return (0);
848 #else
849 	return (((uint64_t)map_buffer[index].base_addr_high << 32) +
850 	    (uint64_t)map_buffer[index].base_addr_low);
851 #endif
852 }
853 
854 static uint64_t
dboot_loader_mmap_get_length(int index)855 dboot_loader_mmap_get_length(int index)
856 {
857 #if !defined(__xpv)
858 	mb_memory_map_t *mp, *mpend;
859 	uint64_t length;
860 	int i;
861 
862 	switch (multiboot_version) {
863 	case 1:
864 		mp = (mb_memory_map_t *)mb_info->mmap_addr;
865 		mpend = (mb_memory_map_t *)
866 		    (mb_info->mmap_addr + mb_info->mmap_length);
867 
868 		for (i = 0; mp < mpend && i != index; i++)
869 			mp = (mb_memory_map_t *)((uintptr_t)mp + mp->size +
870 			    sizeof (mp->size));
871 		if (mp >= mpend) {
872 			dboot_panic("dboot_loader_mmap_get_length(): index "
873 			    "out of bounds: %d\n", index);
874 		}
875 		return (((uint64_t)mp->length_high << 32) +
876 		    (uint64_t)mp->length_low);
877 
878 	case 2:
879 		if (dboot_multiboot2_efi_mmap_get_length(mb2_info,
880 		    index, &length))
881 			return (length);
882 
883 		if (dboot_multiboot2_mmap_get_length(mb2_info,
884 		    index, &length))
885 			return (length);
886 
887 		dboot_panic("Can not get memory length for %d\n", index);
888 
889 	default:
890 		dboot_panic("Unknown multiboot version: %d\n",
891 		    multiboot_version);
892 		break;
893 	}
894 	return (0);
895 #else
896 	return (((uint64_t)map_buffer[index].length_high << 32) +
897 	    (uint64_t)map_buffer[index].length_low);
898 #endif
899 }
900 
901 static void
build_pcimemlists(void)902 build_pcimemlists(void)
903 {
904 	uint64_t page_offset = MMU_PAGEOFFSET;	/* needs to be 64 bits */
905 	uint64_t start;
906 	uint64_t end;
907 	int i, num;
908 
909 	if (prom_debug)
910 		dboot_printf("building pcimemlists:\n");
911 	/*
912 	 * initialize
913 	 */
914 	pcimemlists[0].addr = pci_lo_limit;
915 	pcimemlists[0].size = pci_hi_limit - pci_lo_limit;
916 	pcimemlists_used = 1;
917 
918 	num = dboot_loader_mmap_entries();
919 	/*
920 	 * Fill in PCI memlists.
921 	 */
922 	for (i = 0; i < num; ++i) {
923 		start = dboot_loader_mmap_get_base(i);
924 		end = start + dboot_loader_mmap_get_length(i);
925 
926 		if (prom_debug)
927 			dboot_printf("\ttype: %d %" PRIx64 "..%"
928 			    PRIx64 "\n", dboot_loader_mmap_get_type(i),
929 			    start, end);
930 
931 		/*
932 		 * page align start and end
933 		 */
934 		start = (start + page_offset) & ~page_offset;
935 		end &= ~page_offset;
936 		if (end <= start)
937 			continue;
938 
939 		exclude_from_pci(start, end);
940 	}
941 
942 	/*
943 	 * Finish off the pcimemlist
944 	 */
945 	if (prom_debug) {
946 		for (i = 0; i < pcimemlists_used; ++i) {
947 			dboot_printf("pcimemlist entry 0x%" PRIx64 "..0x%"
948 			    PRIx64 "\n", pcimemlists[i].addr,
949 			    pcimemlists[i].addr + pcimemlists[i].size);
950 		}
951 	}
952 	pcimemlists[0].next = 0;
953 	pcimemlists[0].prev = 0;
954 	for (i = 1; i < pcimemlists_used; ++i) {
955 		pcimemlists[i].prev =
956 		    (native_ptr_t)(uintptr_t)(pcimemlists + i - 1);
957 		pcimemlists[i].next = 0;
958 		pcimemlists[i - 1].next =
959 		    (native_ptr_t)(uintptr_t)(pcimemlists + i);
960 	}
961 	bi->bi_pcimem = (native_ptr_t)(uintptr_t)pcimemlists;
962 	DBG(bi->bi_pcimem);
963 }
964 
965 #if defined(__xpv)
966 /*
967  * Initialize memory allocator stuff from hypervisor-supplied start info.
968  */
969 static void
init_mem_alloc(void)970 init_mem_alloc(void)
971 {
972 	int	local;	/* variables needed to find start region */
973 	paddr_t	scratch_start;
974 	xen_memory_map_t map;
975 
976 	DBG_MSG("Entered init_mem_alloc()\n");
977 
978 	/*
979 	 * Free memory follows the stack. There's at least 512KB of scratch
980 	 * space, rounded up to at least 2Mb alignment.  That should be enough
981 	 * for the page tables we'll need to build.  The nucleus memory is
982 	 * allocated last and will be outside the addressible range.  We'll
983 	 * switch to new page tables before we unpack the kernel
984 	 */
985 	scratch_start = RNDUP((paddr_t)(uintptr_t)&local, MMU_PAGESIZE);
986 	DBG(scratch_start);
987 	scratch_end = RNDUP((paddr_t)scratch_start + 512 * 1024, TWO_MEG);
988 	DBG(scratch_end);
989 
990 	/*
991 	 * For paranoia, leave some space between hypervisor data and ours.
992 	 * Use 500 instead of 512.
993 	 */
994 	next_avail_addr = scratch_end - 500 * 1024;
995 	DBG(next_avail_addr);
996 
997 	/*
998 	 * The domain builder gives us at most 1 module
999 	 */
1000 	DBG(xen_info->mod_len);
1001 	if (xen_info->mod_len > 0) {
1002 		DBG(xen_info->mod_start);
1003 		modules[0].bm_addr =
1004 		    (native_ptr_t)(uintptr_t)xen_info->mod_start;
1005 		modules[0].bm_size = xen_info->mod_len;
1006 		bi->bi_module_cnt = 1;
1007 		bi->bi_modules = (native_ptr_t)(uintptr_t)modules;
1008 	} else {
1009 		bi->bi_module_cnt = 0;
1010 		bi->bi_modules = (native_ptr_t)(uintptr_t)NULL;
1011 	}
1012 	DBG(bi->bi_module_cnt);
1013 	DBG(bi->bi_modules);
1014 
1015 	DBG(xen_info->mfn_list);
1016 	DBG(xen_info->nr_pages);
1017 	max_mem = (paddr_t)xen_info->nr_pages << MMU_PAGESHIFT;
1018 	DBG(max_mem);
1019 
1020 	/*
1021 	 * Using pseudo-physical addresses, so only 1 memlist element
1022 	 */
1023 	memlists[0].addr = 0;
1024 	DBG(memlists[0].addr);
1025 	memlists[0].size = max_mem;
1026 	DBG(memlists[0].size);
1027 	memlists_used = 1;
1028 	DBG(memlists_used);
1029 
1030 	/*
1031 	 * finish building physinstall list
1032 	 */
1033 	sort_physinstall();
1034 
1035 	/*
1036 	 * build bios reserved memlists
1037 	 */
1038 	build_rsvdmemlists();
1039 
1040 	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
1041 		/*
1042 		 * build PCI Memory list
1043 		 */
1044 		map.nr_entries = MAXMAPS;
1045 		/*LINTED: constant in conditional context*/
1046 		set_xen_guest_handle(map.buffer, map_buffer);
1047 		if (HYPERVISOR_memory_op(XENMEM_machine_memory_map, &map) != 0)
1048 			dboot_panic("getting XENMEM_machine_memory_map failed");
1049 		build_pcimemlists();
1050 	}
1051 }
1052 
1053 #else	/* !__xpv */
1054 
1055 static void
dboot_multiboot1_xboot_consinfo(void)1056 dboot_multiboot1_xboot_consinfo(void)
1057 {
1058 	fb->framebuffer = 0;
1059 }
1060 
1061 static void
dboot_multiboot2_xboot_consinfo(void)1062 dboot_multiboot2_xboot_consinfo(void)
1063 {
1064 	multiboot_tag_framebuffer_t *fbtag;
1065 	fbtag = dboot_multiboot2_find_tag(mb2_info,
1066 	    MULTIBOOT_TAG_TYPE_FRAMEBUFFER);
1067 	fb->framebuffer = (uint64_t)(uintptr_t)fbtag;
1068 }
1069 
1070 static int
dboot_multiboot_modcount(void)1071 dboot_multiboot_modcount(void)
1072 {
1073 	switch (multiboot_version) {
1074 	case 1:
1075 		return (mb_info->mods_count);
1076 
1077 	case 2:
1078 		return (dboot_multiboot2_modcount(mb2_info));
1079 
1080 	default:
1081 		dboot_panic("Unknown multiboot version: %d\n",
1082 		    multiboot_version);
1083 		break;
1084 	}
1085 	return (0);
1086 }
1087 
1088 static uint32_t
dboot_multiboot_modstart(int index)1089 dboot_multiboot_modstart(int index)
1090 {
1091 	switch (multiboot_version) {
1092 	case 1:
1093 		return (((mb_module_t *)mb_info->mods_addr)[index].mod_start);
1094 
1095 	case 2:
1096 		return (dboot_multiboot2_modstart(mb2_info, index));
1097 
1098 	default:
1099 		dboot_panic("Unknown multiboot version: %d\n",
1100 		    multiboot_version);
1101 		break;
1102 	}
1103 	return (0);
1104 }
1105 
1106 static uint32_t
dboot_multiboot_modend(int index)1107 dboot_multiboot_modend(int index)
1108 {
1109 	switch (multiboot_version) {
1110 	case 1:
1111 		return (((mb_module_t *)mb_info->mods_addr)[index].mod_end);
1112 
1113 	case 2:
1114 		return (dboot_multiboot2_modend(mb2_info, index));
1115 
1116 	default:
1117 		dboot_panic("Unknown multiboot version: %d\n",
1118 		    multiboot_version);
1119 		break;
1120 	}
1121 	return (0);
1122 }
1123 
1124 static char *
dboot_multiboot_modcmdline(int index)1125 dboot_multiboot_modcmdline(int index)
1126 {
1127 	switch (multiboot_version) {
1128 	case 1:
1129 		return ((char *)((mb_module_t *)
1130 		    mb_info->mods_addr)[index].mod_name);
1131 
1132 	case 2:
1133 		return (dboot_multiboot2_modcmdline(mb2_info, index));
1134 
1135 	default:
1136 		dboot_panic("Unknown multiboot version: %d\n",
1137 		    multiboot_version);
1138 		break;
1139 	}
1140 	return (0);
1141 }
1142 
1143 /*
1144  * Find the modules used by console setup.
1145  * Since we need the console to print early boot messages, the console is set up
1146  * before anything else and therefore we need to pick up the needed modules.
1147  *
1148  * Note, we just will search for and if found, will pass the modules
1149  * to console setup, the proper module list processing will happen later.
1150  * Currently used modules are boot environment and console font.
1151  */
1152 static void
dboot_find_console_modules(void)1153 dboot_find_console_modules(void)
1154 {
1155 	int i, modcount;
1156 	uint32_t mod_start, mod_end;
1157 	char *cmdline;
1158 
1159 	modcount = dboot_multiboot_modcount();
1160 	bi->bi_module_cnt = 0;
1161 	for (i = 0; i < modcount; ++i) {
1162 		cmdline = dboot_multiboot_modcmdline(i);
1163 		if (cmdline == NULL)
1164 			continue;
1165 
1166 		if (strstr(cmdline, "type=console-font") != NULL)
1167 			modules[bi->bi_module_cnt].bm_type = BMT_FONT;
1168 		else if (strstr(cmdline, "type=environment") != NULL)
1169 			modules[bi->bi_module_cnt].bm_type = BMT_ENV;
1170 		else
1171 			continue;
1172 
1173 		mod_start = dboot_multiboot_modstart(i);
1174 		mod_end = dboot_multiboot_modend(i);
1175 		modules[bi->bi_module_cnt].bm_addr =
1176 		    (native_ptr_t)(uintptr_t)mod_start;
1177 		modules[bi->bi_module_cnt].bm_size = mod_end - mod_start;
1178 		modules[bi->bi_module_cnt].bm_name =
1179 		    (native_ptr_t)(uintptr_t)NULL;
1180 		modules[bi->bi_module_cnt].bm_hash =
1181 		    (native_ptr_t)(uintptr_t)NULL;
1182 		bi->bi_module_cnt++;
1183 	}
1184 	if (bi->bi_module_cnt != 0)
1185 		bi->bi_modules = (native_ptr_t)(uintptr_t)modules;
1186 }
1187 
1188 static boolean_t
dboot_multiboot_basicmeminfo(uint32_t * lower,uint32_t * upper)1189 dboot_multiboot_basicmeminfo(uint32_t *lower, uint32_t *upper)
1190 {
1191 	boolean_t rv = B_FALSE;
1192 
1193 	switch (multiboot_version) {
1194 	case 1:
1195 		if (mb_info->flags & 0x01) {
1196 			*lower = mb_info->mem_lower;
1197 			*upper = mb_info->mem_upper;
1198 			rv = B_TRUE;
1199 		}
1200 		break;
1201 
1202 	case 2:
1203 		return (dboot_multiboot2_basicmeminfo(mb2_info, lower, upper));
1204 
1205 	default:
1206 		dboot_panic("Unknown multiboot version: %d\n",
1207 		    multiboot_version);
1208 		break;
1209 	}
1210 	return (rv);
1211 }
1212 
1213 static uint8_t
dboot_a2h(char v)1214 dboot_a2h(char v)
1215 {
1216 	if (v >= 'a')
1217 		return (v - 'a' + 0xa);
1218 	else if (v >= 'A')
1219 		return (v - 'A' + 0xa);
1220 	else if (v >= '0')
1221 		return (v - '0');
1222 	else
1223 		dboot_panic("bad ASCII hex character %c\n", v);
1224 
1225 	return (0);
1226 }
1227 
1228 static void
digest_a2h(const char * ascii,uint8_t * digest)1229 digest_a2h(const char *ascii, uint8_t *digest)
1230 {
1231 	unsigned int i;
1232 
1233 	for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
1234 		digest[i] = dboot_a2h(ascii[i * 2]) << 4;
1235 		digest[i] |= dboot_a2h(ascii[i * 2 + 1]);
1236 	}
1237 }
1238 
1239 /*
1240  * Generate a SHA-1 hash of the first len bytes of image, and compare it with
1241  * the ASCII-format hash found in the 40-byte buffer at ascii.  If they
1242  * match, return 0, otherwise -1.  This works only for images smaller than
1243  * 4 GB, which should not be a problem.
1244  */
1245 static int
check_image_hash(uint_t midx)1246 check_image_hash(uint_t midx)
1247 {
1248 	const char *ascii;
1249 	const void *image;
1250 	size_t len;
1251 	SHA1_CTX ctx;
1252 	uint8_t digest[SHA1_DIGEST_LENGTH];
1253 	uint8_t baseline[SHA1_DIGEST_LENGTH];
1254 	unsigned int i;
1255 
1256 	ascii = (const char *)(uintptr_t)modules[midx].bm_hash;
1257 	image = (const void *)(uintptr_t)modules[midx].bm_addr;
1258 	len = (size_t)modules[midx].bm_size;
1259 
1260 	digest_a2h(ascii, baseline);
1261 
1262 	SHA1Init(&ctx);
1263 	SHA1Update(&ctx, image, len);
1264 	SHA1Final(digest, &ctx);
1265 
1266 	for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
1267 		if (digest[i] != baseline[i])
1268 			return (-1);
1269 	}
1270 
1271 	return (0);
1272 }
1273 
1274 static const char *
type_to_str(boot_module_type_t type)1275 type_to_str(boot_module_type_t type)
1276 {
1277 	switch (type) {
1278 	case BMT_ROOTFS:
1279 		return ("rootfs");
1280 	case BMT_FILE:
1281 		return ("file");
1282 	case BMT_HASH:
1283 		return ("hash");
1284 	case BMT_ENV:
1285 		return ("environment");
1286 	case BMT_FONT:
1287 		return ("console-font");
1288 	default:
1289 		return ("unknown");
1290 	}
1291 }
1292 
1293 static void
check_images(void)1294 check_images(void)
1295 {
1296 	uint_t i;
1297 	char displayhash[SHA1_ASCII_LENGTH + 1];
1298 
1299 	for (i = 0; i < modules_used; i++) {
1300 		if (prom_debug) {
1301 			dboot_printf("module #%d: name %s type %s "
1302 			    "addr %lx size %lx\n",
1303 			    i, (char *)(uintptr_t)modules[i].bm_name,
1304 			    type_to_str(modules[i].bm_type),
1305 			    (ulong_t)modules[i].bm_addr,
1306 			    (ulong_t)modules[i].bm_size);
1307 		}
1308 
1309 		if (modules[i].bm_type == BMT_HASH ||
1310 		    modules[i].bm_hash == (native_ptr_t)(uintptr_t)NULL) {
1311 			DBG_MSG("module has no hash; skipping check\n");
1312 			continue;
1313 		}
1314 		(void) memcpy(displayhash,
1315 		    (void *)(uintptr_t)modules[i].bm_hash,
1316 		    SHA1_ASCII_LENGTH);
1317 		displayhash[SHA1_ASCII_LENGTH] = '\0';
1318 		if (prom_debug) {
1319 			dboot_printf("checking expected hash [%s]: ",
1320 			    displayhash);
1321 		}
1322 
1323 		if (check_image_hash(i) != 0)
1324 			dboot_panic("hash mismatch!\n");
1325 		else
1326 			DBG_MSG("OK\n");
1327 	}
1328 }
1329 
1330 /*
1331  * Determine the module's starting address, size, name, and type, and fill the
1332  * boot_modules structure.  This structure is used by the bop code, except for
1333  * hashes which are checked prior to transferring control to the kernel.
1334  */
1335 static void
process_module(int midx)1336 process_module(int midx)
1337 {
1338 	uint32_t mod_start = dboot_multiboot_modstart(midx);
1339 	uint32_t mod_end = dboot_multiboot_modend(midx);
1340 	char *cmdline = dboot_multiboot_modcmdline(midx);
1341 	char *p, *q;
1342 
1343 	check_higher(mod_end);
1344 	if (prom_debug) {
1345 		dboot_printf("\tmodule #%d: '%s' at 0x%lx, end 0x%lx\n",
1346 		    midx, cmdline, (ulong_t)mod_start, (ulong_t)mod_end);
1347 	}
1348 
1349 	if (mod_start > mod_end) {
1350 		dboot_panic("module #%d: module start address 0x%lx greater "
1351 		    "than end address 0x%lx", midx,
1352 		    (ulong_t)mod_start, (ulong_t)mod_end);
1353 	}
1354 
1355 	/*
1356 	 * A brief note on lengths and sizes: GRUB, for reasons unknown, passes
1357 	 * the address of the last valid byte in a module plus 1 as mod_end.
1358 	 * This is of course a bug; the multiboot specification simply states
1359 	 * that mod_start and mod_end "contain the start and end addresses of
1360 	 * the boot module itself" which is pretty obviously not what GRUB is
1361 	 * doing.  However, fixing it requires that not only this code be
1362 	 * changed but also that other code consuming this value and values
1363 	 * derived from it be fixed, and that the kernel and GRUB must either
1364 	 * both have the bug or neither.  While there are a lot of combinations
1365 	 * that will work, there are also some that won't, so for simplicity
1366 	 * we'll just cope with the bug.  That means we won't actually hash the
1367 	 * byte at mod_end, and we will expect that mod_end for the hash file
1368 	 * itself is one greater than some multiple of 41 (40 bytes of ASCII
1369 	 * hash plus a newline for each module).  We set bm_size to the true
1370 	 * correct number of bytes in each module, achieving exactly this.
1371 	 */
1372 
1373 	modules[midx].bm_addr = (native_ptr_t)(uintptr_t)mod_start;
1374 	modules[midx].bm_size = mod_end - mod_start;
1375 	modules[midx].bm_name = (native_ptr_t)(uintptr_t)cmdline;
1376 	modules[midx].bm_hash = (native_ptr_t)(uintptr_t)NULL;
1377 	modules[midx].bm_type = BMT_FILE;
1378 
1379 	if (cmdline == NULL) {
1380 		modules[midx].bm_name = (native_ptr_t)(uintptr_t)noname;
1381 		return;
1382 	}
1383 
1384 	p = cmdline;
1385 	modules[midx].bm_name =
1386 	    (native_ptr_t)(uintptr_t)strsep(&p, " \t\f\n\r");
1387 
1388 	while (p != NULL) {
1389 		q = strsep(&p, " \t\f\n\r");
1390 		if (strncmp(q, "name=", 5) == 0) {
1391 			if (q[5] != '\0' && !isspace(q[5])) {
1392 				modules[midx].bm_name =
1393 				    (native_ptr_t)(uintptr_t)(q + 5);
1394 			}
1395 			continue;
1396 		}
1397 
1398 		if (strncmp(q, "type=", 5) == 0) {
1399 			if (q[5] == '\0' || isspace(q[5]))
1400 				continue;
1401 			q += 5;
1402 			if (strcmp(q, "rootfs") == 0) {
1403 				modules[midx].bm_type = BMT_ROOTFS;
1404 			} else if (strcmp(q, "hash") == 0) {
1405 				modules[midx].bm_type = BMT_HASH;
1406 			} else if (strcmp(q, "environment") == 0) {
1407 				modules[midx].bm_type = BMT_ENV;
1408 			} else if (strcmp(q, "console-font") == 0) {
1409 				modules[midx].bm_type = BMT_FONT;
1410 			} else if (strcmp(q, "file") != 0) {
1411 				dboot_printf("\tmodule #%d: unknown module "
1412 				    "type '%s'; defaulting to 'file'\n",
1413 				    midx, q);
1414 			}
1415 			continue;
1416 		}
1417 
1418 		if (strncmp(q, "hash=", 5) == 0) {
1419 			if (q[5] != '\0' && !isspace(q[5])) {
1420 				modules[midx].bm_hash =
1421 				    (native_ptr_t)(uintptr_t)(q + 5);
1422 			}
1423 			continue;
1424 		}
1425 
1426 		dboot_printf("ignoring unknown option '%s'\n", q);
1427 	}
1428 }
1429 
1430 /*
1431  * Backward compatibility: if there are exactly one or two modules, both
1432  * of type 'file' and neither with an embedded hash value, we have been
1433  * given the legacy style modules.  In this case we need to treat the first
1434  * module as a rootfs and the second as a hash referencing that module.
1435  * Otherwise, even if the configuration is invalid, we assume that the
1436  * operator knows what he's doing or at least isn't being bitten by this
1437  * interface change.
1438  */
1439 static void
fixup_modules(void)1440 fixup_modules(void)
1441 {
1442 	if (modules_used == 0 || modules_used > 2)
1443 		return;
1444 
1445 	if (modules[0].bm_type != BMT_FILE ||
1446 	    (modules_used > 1 && modules[1].bm_type != BMT_FILE)) {
1447 		return;
1448 	}
1449 
1450 	if (modules[0].bm_hash != (native_ptr_t)(uintptr_t)NULL ||
1451 	    (modules_used > 1 &&
1452 	    modules[1].bm_hash != (native_ptr_t)(uintptr_t)NULL)) {
1453 		return;
1454 	}
1455 
1456 	modules[0].bm_type = BMT_ROOTFS;
1457 	if (modules_used > 1) {
1458 		modules[1].bm_type = BMT_HASH;
1459 		modules[1].bm_name = modules[0].bm_name;
1460 	}
1461 }
1462 
1463 /*
1464  * For modules that do not have assigned hashes but have a separate hash module,
1465  * find the assigned hash module and set the primary module's bm_hash to point
1466  * to the hash data from that module.  We will then ignore modules of type
1467  * BMT_HASH from this point forward.
1468  */
1469 static void
assign_module_hashes(void)1470 assign_module_hashes(void)
1471 {
1472 	uint_t i, j;
1473 
1474 	for (i = 0; i < modules_used; i++) {
1475 		if (modules[i].bm_type == BMT_HASH ||
1476 		    modules[i].bm_hash != (native_ptr_t)(uintptr_t)NULL) {
1477 			continue;
1478 		}
1479 
1480 		for (j = 0; j < modules_used; j++) {
1481 			if (modules[j].bm_type != BMT_HASH ||
1482 			    strcmp((char *)(uintptr_t)modules[j].bm_name,
1483 			    (char *)(uintptr_t)modules[i].bm_name) != 0) {
1484 				continue;
1485 			}
1486 
1487 			if (modules[j].bm_size < SHA1_ASCII_LENGTH) {
1488 				dboot_printf("Short hash module of length "
1489 				    "0x%lx bytes; ignoring\n",
1490 				    (ulong_t)modules[j].bm_size);
1491 			} else {
1492 				modules[i].bm_hash = modules[j].bm_addr;
1493 			}
1494 			break;
1495 		}
1496 	}
1497 }
1498 
1499 /*
1500  * Walk through the module information finding the last used address.
1501  * The first available address will become the top level page table.
1502  */
1503 static void
dboot_process_modules(void)1504 dboot_process_modules(void)
1505 {
1506 	int i, modcount;
1507 	extern char _end[];
1508 
1509 	DBG_MSG("\nFinding Modules\n");
1510 	modcount = dboot_multiboot_modcount();
1511 	if (modcount > MAX_BOOT_MODULES) {
1512 		dboot_panic("Too many modules (%d) -- the maximum is %d.",
1513 		    modcount, MAX_BOOT_MODULES);
1514 	}
1515 	/*
1516 	 * search the modules to find the last used address
1517 	 * we'll build the module list while we're walking through here
1518 	 */
1519 	check_higher((paddr_t)(uintptr_t)&_end);
1520 	for (i = 0; i < modcount; ++i) {
1521 		process_module(i);
1522 		modules_used++;
1523 	}
1524 	bi->bi_modules = (native_ptr_t)(uintptr_t)modules;
1525 	DBG(bi->bi_modules);
1526 	bi->bi_module_cnt = modcount;
1527 	DBG(bi->bi_module_cnt);
1528 
1529 	fixup_modules();
1530 	assign_module_hashes();
1531 	check_images();
1532 }
1533 
1534 /*
1535  * We then build the phys_install memlist from the multiboot information.
1536  */
1537 static void
dboot_process_mmap(void)1538 dboot_process_mmap(void)
1539 {
1540 	uint64_t start;
1541 	uint64_t end;
1542 	uint64_t page_offset = MMU_PAGEOFFSET;	/* needs to be 64 bits */
1543 	uint32_t lower, upper, type, t;
1544 	int i, mmap_entries;
1545 
1546 	/*
1547 	 * Walk through the memory map from multiboot and build our memlist
1548 	 * structures. Note these will have native format pointers.
1549 	 */
1550 	DBG_MSG("\nFinding Memory Map\n");
1551 	num_entries = 0;
1552 	num_entries_set = B_FALSE;
1553 	max_mem = 0;
1554 	t = 0;
1555 	if ((mmap_entries = dboot_loader_mmap_entries()) > 0) {
1556 		struct boot_memlist *mlist;
1557 		uint_t *indexp;
1558 
1559 		for (i = 0; i < mmap_entries; i++) {
1560 			start = dboot_loader_mmap_get_base(i);
1561 			end = start + dboot_loader_mmap_get_length(i);
1562 			type = dboot_loader_mmap_get_type(i);
1563 
1564 			if (prom_debug)
1565 				dboot_printf("\ttype: %u %" PRIx64 "..%"
1566 				    PRIx64 "\n", type, start, end);
1567 
1568 			/*
1569 			 * page align start and end
1570 			 */
1571 			start = (start + page_offset) & ~page_offset;
1572 			end &= ~page_offset;
1573 			if (end <= start)
1574 				continue;
1575 
1576 			/*
1577 			 * only type 1 is usable RAM
1578 			 */
1579 			switch (type) {
1580 			case 1:
1581 				if (end > max_mem)
1582 					max_mem = end;
1583 				mlist = memlists;
1584 				indexp = &memlists_used;
1585 				break;
1586 			case 2:
1587 				mlist = rsvdmemlists;
1588 				indexp = &rsvdmemlists_used;
1589 				break;
1590 			default:
1591 				continue;
1592 			}
1593 
1594 			if (memlists_used > MAX_MEMLIST)
1595 				dboot_panic("too many memlists");
1596 			if (rsvdmemlists_used > MAX_MEMLIST)
1597 				dboot_panic("too many rsvdmemlists");
1598 
1599 			if (mlist[*indexp].size != 0 &&
1600 			    type == t &&
1601 			    (mlist[*indexp].addr +
1602 			    mlist[*indexp].size) == start) {
1603 				mlist[*indexp].size =
1604 				    end - mlist[*indexp].addr;
1605 				continue;
1606 			}
1607 			/* do we need new entry? */
1608 			if (mlist[*indexp].size != 0) {
1609 				*indexp = *indexp + 1;
1610 				if (*indexp > MAX_MEMLIST)
1611 					continue;
1612 			}
1613 
1614 			t = type;
1615 			mlist[*indexp].addr = start;
1616 			mlist[*indexp].size = end - start;
1617 		}
1618 
1619 		if (memlists[memlists_used].size != 0) {
1620 			memlists_used++;
1621 		}
1622 		if (rsvdmemlists[rsvdmemlists_used].size != 0) {
1623 			rsvdmemlists_used++;
1624 		}
1625 
1626 		if (prom_debug) {
1627 			for (i = 0; i < memlists_used; i++) {
1628 				dboot_printf("memlists[%u] %"
1629 				    PRIx64 "..%" PRIx64 "\n",
1630 				    i,
1631 				    memlists[i].addr,
1632 				    memlists[i].size);
1633 			}
1634 			for (i = 0; i < rsvdmemlists_used; i++) {
1635 				dboot_printf("rsvdmemlists[%u] %"
1636 				    PRIx64 "..%" PRIx64 "\n",
1637 				    i,
1638 				    rsvdmemlists[i].addr,
1639 				    rsvdmemlists[i].size);
1640 			}
1641 		}
1642 
1643 		build_pcimemlists();
1644 	} else if (dboot_multiboot_basicmeminfo(&lower, &upper)) {
1645 		DBG(lower);
1646 		memlists[memlists_used].addr = 0;
1647 		memlists[memlists_used].size = lower * 1024;
1648 		++memlists_used;
1649 		DBG(upper);
1650 		memlists[memlists_used].addr = 1024 * 1024;
1651 		memlists[memlists_used].size = upper * 1024;
1652 		++memlists_used;
1653 
1654 		/*
1655 		 * Old platform - assume I/O space at the end of memory.
1656 		 */
1657 		pcimemlists[0].addr = (upper * 1024) + (1024 * 1024);
1658 		pcimemlists[0].size = pci_hi_limit - pcimemlists[0].addr;
1659 		pcimemlists[0].next = 0;
1660 		pcimemlists[0].prev = 0;
1661 		bi->bi_pcimem = (native_ptr_t)(uintptr_t)pcimemlists;
1662 		DBG(bi->bi_pcimem);
1663 	} else {
1664 		dboot_panic("No memory info from boot loader!!!");
1665 	}
1666 
1667 	/*
1668 	 * finish processing the physinstall list
1669 	 */
1670 	sort_physinstall();
1671 
1672 	/*
1673 	 * build bios reserved mem lists
1674 	 */
1675 	build_rsvdmemlists();
1676 }
1677 
1678 /*
1679  * The highest address is used as the starting point for dboot's simple
1680  * memory allocator.
1681  *
1682  * Finding the highest address in case of Multiboot 1 protocol is
1683  * quite painful in the sense that some information provided by
1684  * the multiboot info structure points to BIOS data, and some to RAM.
1685  *
1686  * The module list was processed and checked already by dboot_process_modules(),
1687  * so we will check the command line string and the memory map.
1688  *
1689  * This list of to be checked items is based on our current knowledge of
1690  * allocations made by grub1 and will need to be reviewed if there
1691  * are updates about the information provided by Multiboot 1.
1692  *
1693  * In the case of the Multiboot 2, our life is much simpler, as the MB2
1694  * information tag list is one contiguous chunk of memory.
1695  */
1696 static paddr_t
dboot_multiboot1_highest_addr(void)1697 dboot_multiboot1_highest_addr(void)
1698 {
1699 	paddr_t addr = (paddr_t)(uintptr_t)NULL;
1700 	char *cmdl = (char *)mb_info->cmdline;
1701 
1702 	if (mb_info->flags & MB_INFO_CMDLINE)
1703 		addr = ((paddr_t)((uintptr_t)cmdl + strlen(cmdl) + 1));
1704 
1705 	if (mb_info->flags & MB_INFO_MEM_MAP)
1706 		addr = MAX(addr,
1707 		    ((paddr_t)(mb_info->mmap_addr + mb_info->mmap_length)));
1708 	return (addr);
1709 }
1710 
1711 static void
dboot_multiboot_highest_addr(void)1712 dboot_multiboot_highest_addr(void)
1713 {
1714 	paddr_t addr;
1715 
1716 	switch (multiboot_version) {
1717 	case 1:
1718 		addr = dboot_multiboot1_highest_addr();
1719 		if (addr != (paddr_t)(uintptr_t)NULL)
1720 			check_higher(addr);
1721 		break;
1722 	case 2:
1723 		addr = dboot_multiboot2_highest_addr(mb2_info);
1724 		if (addr != (paddr_t)(uintptr_t)NULL)
1725 			check_higher(addr);
1726 		break;
1727 	default:
1728 		dboot_panic("Unknown multiboot version: %d\n",
1729 		    multiboot_version);
1730 		break;
1731 	}
1732 }
1733 
1734 /*
1735  * Walk the boot loader provided information and find the highest free address.
1736  */
1737 static void
init_mem_alloc(void)1738 init_mem_alloc(void)
1739 {
1740 	DBG_MSG("Entered init_mem_alloc()\n");
1741 	dboot_process_modules();
1742 	dboot_process_mmap();
1743 	dboot_multiboot_highest_addr();
1744 }
1745 
1746 static int
dboot_same_guids(efi_guid_t * g1,efi_guid_t * g2)1747 dboot_same_guids(efi_guid_t *g1, efi_guid_t *g2)
1748 {
1749 	int i;
1750 
1751 	if (g1->time_low != g2->time_low)
1752 		return (0);
1753 	if (g1->time_mid != g2->time_mid)
1754 		return (0);
1755 	if (g1->time_hi_and_version != g2->time_hi_and_version)
1756 		return (0);
1757 	if (g1->clock_seq_hi_and_reserved != g2->clock_seq_hi_and_reserved)
1758 		return (0);
1759 	if (g1->clock_seq_low != g2->clock_seq_low)
1760 		return (0);
1761 
1762 	for (i = 0; i < 6; i++) {
1763 		if (g1->node_addr[i] != g2->node_addr[i])
1764 			return (0);
1765 	}
1766 	return (1);
1767 }
1768 
1769 static void
process_efi32(EFI_SYSTEM_TABLE32 * efi)1770 process_efi32(EFI_SYSTEM_TABLE32 *efi)
1771 {
1772 	uint32_t entries;
1773 	EFI_CONFIGURATION_TABLE32 *config;
1774 	efi_guid_t VendorGuid;
1775 	int i;
1776 
1777 	entries = efi->NumberOfTableEntries;
1778 	config = (EFI_CONFIGURATION_TABLE32 *)(uintptr_t)
1779 	    efi->ConfigurationTable;
1780 
1781 	for (i = 0; i < entries; i++) {
1782 		(void) memcpy(&VendorGuid, &config[i].VendorGuid,
1783 		    sizeof (VendorGuid));
1784 		if (dboot_same_guids(&VendorGuid, &smbios3)) {
1785 			bi->bi_smbios = (native_ptr_t)(uintptr_t)
1786 			    config[i].VendorTable;
1787 		}
1788 		if (bi->bi_smbios == 0 &&
1789 		    dboot_same_guids(&VendorGuid, &smbios)) {
1790 			bi->bi_smbios = (native_ptr_t)(uintptr_t)
1791 			    config[i].VendorTable;
1792 		}
1793 		if (dboot_same_guids(&VendorGuid, &acpi2)) {
1794 			bi->bi_acpi_rsdp = (native_ptr_t)(uintptr_t)
1795 			    config[i].VendorTable;
1796 		}
1797 		if (bi->bi_acpi_rsdp == 0 &&
1798 		    dboot_same_guids(&VendorGuid, &acpi1)) {
1799 			bi->bi_acpi_rsdp = (native_ptr_t)(uintptr_t)
1800 			    config[i].VendorTable;
1801 		}
1802 	}
1803 }
1804 
1805 static void
process_efi64(EFI_SYSTEM_TABLE64 * efi)1806 process_efi64(EFI_SYSTEM_TABLE64 *efi)
1807 {
1808 	uint64_t entries;
1809 	EFI_CONFIGURATION_TABLE64 *config;
1810 	efi_guid_t VendorGuid;
1811 	int i;
1812 
1813 	entries = efi->NumberOfTableEntries;
1814 	config = (EFI_CONFIGURATION_TABLE64 *)(uintptr_t)
1815 	    efi->ConfigurationTable;
1816 
1817 	for (i = 0; i < entries; i++) {
1818 		(void) memcpy(&VendorGuid, &config[i].VendorGuid,
1819 		    sizeof (VendorGuid));
1820 		if (dboot_same_guids(&VendorGuid, &smbios3)) {
1821 			bi->bi_smbios = (native_ptr_t)(uintptr_t)
1822 			    config[i].VendorTable;
1823 		}
1824 		if (bi->bi_smbios == 0 &&
1825 		    dboot_same_guids(&VendorGuid, &smbios)) {
1826 			bi->bi_smbios = (native_ptr_t)(uintptr_t)
1827 			    config[i].VendorTable;
1828 		}
1829 		/* Prefer acpi v2+ over v1. */
1830 		if (dboot_same_guids(&VendorGuid, &acpi2)) {
1831 			bi->bi_acpi_rsdp = (native_ptr_t)(uintptr_t)
1832 			    config[i].VendorTable;
1833 		}
1834 		if (bi->bi_acpi_rsdp == 0 &&
1835 		    dboot_same_guids(&VendorGuid, &acpi1)) {
1836 			bi->bi_acpi_rsdp = (native_ptr_t)(uintptr_t)
1837 			    config[i].VendorTable;
1838 		}
1839 	}
1840 }
1841 
1842 static void
dboot_multiboot_get_fwtables(void)1843 dboot_multiboot_get_fwtables(void)
1844 {
1845 	multiboot_tag_new_acpi_t *nacpitagp;
1846 	multiboot_tag_old_acpi_t *oacpitagp;
1847 	multiboot_tag_efi64_t *efi64tagp = NULL;
1848 	multiboot_tag_efi32_t *efi32tagp = NULL;
1849 
1850 	/* no fw tables from multiboot 1 */
1851 	if (multiboot_version != 2)
1852 		return;
1853 
1854 	efi64tagp = (multiboot_tag_efi64_t *)
1855 	    dboot_multiboot2_find_tag(mb2_info, MULTIBOOT_TAG_TYPE_EFI64);
1856 	if (efi64tagp != NULL) {
1857 		bi->bi_uefi_arch = XBI_UEFI_ARCH_64;
1858 		bi->bi_uefi_systab = (native_ptr_t)(uintptr_t)
1859 		    efi64tagp->mb_pointer;
1860 		process_efi64((EFI_SYSTEM_TABLE64 *)(uintptr_t)
1861 		    efi64tagp->mb_pointer);
1862 	} else {
1863 		efi32tagp = (multiboot_tag_efi32_t *)
1864 		    dboot_multiboot2_find_tag(mb2_info,
1865 		    MULTIBOOT_TAG_TYPE_EFI32);
1866 		if (efi32tagp != NULL) {
1867 			bi->bi_uefi_arch = XBI_UEFI_ARCH_32;
1868 			bi->bi_uefi_systab = (native_ptr_t)(uintptr_t)
1869 			    efi32tagp->mb_pointer;
1870 			process_efi32((EFI_SYSTEM_TABLE32 *)(uintptr_t)
1871 			    efi32tagp->mb_pointer);
1872 		}
1873 	}
1874 
1875 	/*
1876 	 * The multiboot2 info contains a copy of the RSDP; stash a pointer to
1877 	 * it (see find_rsdp() in fakebop).
1878 	 */
1879 	nacpitagp = (multiboot_tag_new_acpi_t *)
1880 	    dboot_multiboot2_find_tag(mb2_info, MULTIBOOT_TAG_TYPE_ACPI_NEW);
1881 	oacpitagp = (multiboot_tag_old_acpi_t *)
1882 	    dboot_multiboot2_find_tag(mb2_info, MULTIBOOT_TAG_TYPE_ACPI_OLD);
1883 
1884 	if (nacpitagp != NULL) {
1885 		bi->bi_acpi_rsdp_copy = (native_ptr_t)(uintptr_t)
1886 		    &nacpitagp->mb_rsdp[0];
1887 	} else if (oacpitagp != NULL) {
1888 		bi->bi_acpi_rsdp_copy = (native_ptr_t)(uintptr_t)
1889 		    &oacpitagp->mb_rsdp[0];
1890 	}
1891 }
1892 
1893 /* print out EFI version string with newline */
1894 static void
dboot_print_efi_version(uint32_t ver)1895 dboot_print_efi_version(uint32_t ver)
1896 {
1897 	int rev;
1898 
1899 	dboot_printf("%d.", EFI_REV_MAJOR(ver));
1900 
1901 	rev = EFI_REV_MINOR(ver);
1902 	if ((rev % 10) != 0) {
1903 		dboot_printf("%d.%d\n", rev / 10, rev % 10);
1904 	} else {
1905 		dboot_printf("%d\n", rev / 10);
1906 	}
1907 }
1908 
1909 static void
print_efi32(EFI_SYSTEM_TABLE32 * efi)1910 print_efi32(EFI_SYSTEM_TABLE32 *efi)
1911 {
1912 	uint16_t *data;
1913 	EFI_CONFIGURATION_TABLE32 *conf;
1914 	int i;
1915 
1916 	dboot_printf("EFI32 signature: %llx\n",
1917 	    (unsigned long long)efi->Hdr.Signature);
1918 	dboot_printf("EFI system version: ");
1919 	dboot_print_efi_version(efi->Hdr.Revision);
1920 	dboot_printf("EFI system vendor: ");
1921 	data = (uint16_t *)(uintptr_t)efi->FirmwareVendor;
1922 	for (i = 0; data[i] != 0; i++)
1923 		dboot_printf("%c", (char)data[i]);
1924 	dboot_printf("\nEFI firmware revision: ");
1925 	dboot_print_efi_version(efi->FirmwareRevision);
1926 	dboot_printf("EFI system table number of entries: %d\n",
1927 	    efi->NumberOfTableEntries);
1928 	conf = (EFI_CONFIGURATION_TABLE32 *)(uintptr_t)
1929 	    efi->ConfigurationTable;
1930 	for (i = 0; i < (int)efi->NumberOfTableEntries; i++) {
1931 		dboot_printf("%d: 0x%x 0x%x 0x%x 0x%x 0x%x", i,
1932 		    conf[i].VendorGuid.time_low,
1933 		    conf[i].VendorGuid.time_mid,
1934 		    conf[i].VendorGuid.time_hi_and_version,
1935 		    conf[i].VendorGuid.clock_seq_hi_and_reserved,
1936 		    conf[i].VendorGuid.clock_seq_low);
1937 		dboot_printf(" 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
1938 		    conf[i].VendorGuid.node_addr[0],
1939 		    conf[i].VendorGuid.node_addr[1],
1940 		    conf[i].VendorGuid.node_addr[2],
1941 		    conf[i].VendorGuid.node_addr[3],
1942 		    conf[i].VendorGuid.node_addr[4],
1943 		    conf[i].VendorGuid.node_addr[5]);
1944 	}
1945 }
1946 
1947 static void
print_efi64(EFI_SYSTEM_TABLE64 * efi)1948 print_efi64(EFI_SYSTEM_TABLE64 *efi)
1949 {
1950 	uint16_t *data;
1951 	EFI_CONFIGURATION_TABLE64 *conf;
1952 	int i;
1953 
1954 	dboot_printf("EFI64 signature: %llx\n",
1955 	    (unsigned long long)efi->Hdr.Signature);
1956 	dboot_printf("EFI system version: ");
1957 	dboot_print_efi_version(efi->Hdr.Revision);
1958 	dboot_printf("EFI system vendor: ");
1959 	data = (uint16_t *)(uintptr_t)efi->FirmwareVendor;
1960 	for (i = 0; data[i] != 0; i++)
1961 		dboot_printf("%c", (char)data[i]);
1962 	dboot_printf("\nEFI firmware revision: ");
1963 	dboot_print_efi_version(efi->FirmwareRevision);
1964 	dboot_printf("EFI system table number of entries: %" PRIu64 "\n",
1965 	    efi->NumberOfTableEntries);
1966 	conf = (EFI_CONFIGURATION_TABLE64 *)(uintptr_t)
1967 	    efi->ConfigurationTable;
1968 	for (i = 0; i < (int)efi->NumberOfTableEntries; i++) {
1969 		dboot_printf("%d: 0x%x 0x%x 0x%x 0x%x 0x%x", i,
1970 		    conf[i].VendorGuid.time_low,
1971 		    conf[i].VendorGuid.time_mid,
1972 		    conf[i].VendorGuid.time_hi_and_version,
1973 		    conf[i].VendorGuid.clock_seq_hi_and_reserved,
1974 		    conf[i].VendorGuid.clock_seq_low);
1975 		dboot_printf(" 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
1976 		    conf[i].VendorGuid.node_addr[0],
1977 		    conf[i].VendorGuid.node_addr[1],
1978 		    conf[i].VendorGuid.node_addr[2],
1979 		    conf[i].VendorGuid.node_addr[3],
1980 		    conf[i].VendorGuid.node_addr[4],
1981 		    conf[i].VendorGuid.node_addr[5]);
1982 	}
1983 }
1984 #endif /* !__xpv */
1985 
1986 /*
1987  * Simple memory allocator, allocates aligned physical memory.
1988  * Note that startup_kernel() only allocates memory, never frees.
1989  * Memory usage just grows in an upward direction.
1990  */
1991 static void *
do_mem_alloc(uint32_t size,uint32_t align)1992 do_mem_alloc(uint32_t size, uint32_t align)
1993 {
1994 	uint_t i;
1995 	uint64_t best;
1996 	uint64_t start;
1997 	uint64_t end;
1998 
1999 	/*
2000 	 * make sure size is a multiple of pagesize
2001 	 */
2002 	size = RNDUP(size, MMU_PAGESIZE);
2003 	next_avail_addr = RNDUP(next_avail_addr, align);
2004 
2005 	/*
2006 	 * XXPV fixme joe
2007 	 *
2008 	 * a really large bootarchive that causes you to run out of memory
2009 	 * may cause this to blow up
2010 	 */
2011 	/* LINTED E_UNEXPECTED_UINT_PROMOTION */
2012 	best = (uint64_t)-size;
2013 	for (i = 0; i < memlists_used; ++i) {
2014 		start = memlists[i].addr;
2015 #if defined(__xpv)
2016 		start += mfn_base;
2017 #endif
2018 		end = start + memlists[i].size;
2019 
2020 		/*
2021 		 * did we find the desired address?
2022 		 */
2023 		if (start <= next_avail_addr && next_avail_addr + size <= end) {
2024 			best = next_avail_addr;
2025 			goto done;
2026 		}
2027 
2028 		/*
2029 		 * if not is this address the best so far?
2030 		 */
2031 		if (start > next_avail_addr && start < best &&
2032 		    RNDUP(start, align) + size <= end)
2033 			best = RNDUP(start, align);
2034 	}
2035 
2036 	/*
2037 	 * We didn't find exactly the address we wanted, due to going off the
2038 	 * end of a memory region. Return the best found memory address.
2039 	 */
2040 done:
2041 	next_avail_addr = best + size;
2042 #if defined(__xpv)
2043 	if (next_avail_addr > scratch_end)
2044 		dboot_panic("Out of mem next_avail: 0x%lx, scratch_end: "
2045 		    "0x%lx", (ulong_t)next_avail_addr,
2046 		    (ulong_t)scratch_end);
2047 #endif
2048 	(void) memset((void *)(uintptr_t)best, 0, size);
2049 	return ((void *)(uintptr_t)best);
2050 }
2051 
2052 void *
mem_alloc(uint32_t size)2053 mem_alloc(uint32_t size)
2054 {
2055 	return (do_mem_alloc(size, MMU_PAGESIZE));
2056 }
2057 
2058 
2059 /*
2060  * Build page tables to map all of memory used so far as well as the kernel.
2061  */
2062 static void
build_page_tables(void)2063 build_page_tables(void)
2064 {
2065 	uint32_t psize;
2066 	uint32_t level;
2067 	uint32_t off;
2068 	uint64_t start;
2069 #if !defined(__xpv)
2070 	uint32_t i;
2071 	uint64_t end;
2072 #endif	/* __xpv */
2073 
2074 	/*
2075 	 * If we're on metal, we need to create the top level pagetable.
2076 	 */
2077 #if defined(__xpv)
2078 	top_page_table = (paddr_t)(uintptr_t)xen_info->pt_base;
2079 #else /* __xpv */
2080 	top_page_table = (paddr_t)(uintptr_t)mem_alloc(MMU_PAGESIZE);
2081 #endif /* __xpv */
2082 	DBG((uintptr_t)top_page_table);
2083 
2084 	/*
2085 	 * Determine if we'll use large mappings for kernel, then map it.
2086 	 */
2087 	if (largepage_support) {
2088 		psize = lpagesize;
2089 		level = 1;
2090 	} else {
2091 		psize = MMU_PAGESIZE;
2092 		level = 0;
2093 	}
2094 
2095 	DBG_MSG("Mapping kernel\n");
2096 	DBG(ktext_phys);
2097 	DBG(target_kernel_text);
2098 	DBG(ksize);
2099 	DBG(psize);
2100 	for (off = 0; off < ksize; off += psize)
2101 		map_pa_at_va(ktext_phys + off, target_kernel_text + off, level);
2102 
2103 	/*
2104 	 * The kernel will need a 1 page window to work with page tables
2105 	 */
2106 	bi->bi_pt_window = (native_ptr_t)(uintptr_t)mem_alloc(MMU_PAGESIZE);
2107 	DBG(bi->bi_pt_window);
2108 	bi->bi_pte_to_pt_window =
2109 	    (native_ptr_t)(uintptr_t)find_pte(bi->bi_pt_window, NULL, 0, 0);
2110 	DBG(bi->bi_pte_to_pt_window);
2111 
2112 #if defined(__xpv)
2113 	if (!DOMAIN_IS_INITDOMAIN(xen_info)) {
2114 		/* If this is a domU we're done. */
2115 		DBG_MSG("\nPage tables constructed\n");
2116 		return;
2117 	}
2118 #endif /* __xpv */
2119 
2120 	/*
2121 	 * We need 1:1 mappings for the lower 1M of memory to access
2122 	 * BIOS tables used by a couple of drivers during boot.
2123 	 *
2124 	 * The following code works because our simple memory allocator
2125 	 * only grows usage in an upwards direction.
2126 	 *
2127 	 * Note that by this point in boot some mappings for low memory
2128 	 * may already exist because we've already accessed device in low
2129 	 * memory.  (Specifically the video frame buffer and keyboard
2130 	 * status ports.)  If we're booting on raw hardware then GRUB
2131 	 * created these mappings for us.  If we're booting under a
2132 	 * hypervisor then we went ahead and remapped these devices into
2133 	 * memory allocated within dboot itself.
2134 	 */
2135 	if (map_debug)
2136 		dboot_printf("1:1 map pa=0..1Meg\n");
2137 	for (start = 0; start < 1024 * 1024; start += MMU_PAGESIZE) {
2138 #if defined(__xpv)
2139 		map_ma_at_va(start, start, 0);
2140 #else /* __xpv */
2141 		map_pa_at_va(start, start, 0);
2142 #endif /* __xpv */
2143 	}
2144 
2145 #if !defined(__xpv)
2146 
2147 	for (i = 0; i < memlists_used; ++i) {
2148 		start = memlists[i].addr;
2149 		end = start + memlists[i].size;
2150 
2151 		if (map_debug)
2152 			dboot_printf("1:1 map pa=%" PRIx64 "..%" PRIx64 "\n",
2153 			    start, end);
2154 		while (start < end && start < next_avail_addr) {
2155 			map_pa_at_va(start, start, 0);
2156 			start += MMU_PAGESIZE;
2157 		}
2158 		if (start >= next_avail_addr)
2159 			break;
2160 	}
2161 
2162 	/*
2163 	 * Map framebuffer memory as PT_NOCACHE as this is memory from a
2164 	 * device and therefore must not be cached.
2165 	 */
2166 	if (fb != NULL && fb->framebuffer != 0) {
2167 		multiboot_tag_framebuffer_t *fb_tagp;
2168 		fb_tagp = (multiboot_tag_framebuffer_t *)(uintptr_t)
2169 		    fb->framebuffer;
2170 
2171 		start = fb_tagp->framebuffer_common.framebuffer_addr;
2172 		end = start + fb_tagp->framebuffer_common.framebuffer_height *
2173 		    fb_tagp->framebuffer_common.framebuffer_pitch;
2174 
2175 		if (map_debug)
2176 			dboot_printf("FB 1:1 map pa=%" PRIx64 "..%" PRIx64 "\n",
2177 			    start, end);
2178 		pte_bits |= PT_NOCACHE;
2179 		if (PAT_support != 0)
2180 			pte_bits |= PT_PAT_4K;
2181 
2182 		while (start < end) {
2183 			map_pa_at_va(start, start, 0);
2184 			start += MMU_PAGESIZE;
2185 		}
2186 		pte_bits &= ~PT_NOCACHE;
2187 		if (PAT_support != 0)
2188 			pte_bits &= ~PT_PAT_4K;
2189 	}
2190 #endif /* !__xpv */
2191 
2192 	DBG_MSG("\nPage tables constructed\n");
2193 }
2194 
2195 #define	NO_MULTIBOOT	\
2196 "multiboot is no longer used to boot the Solaris Operating System.\n\
2197 The grub entry should be changed to:\n\
2198 kernel$ /platform/i86pc/kernel/$ISADIR/unix\n\
2199 module$ /platform/i86pc/$ISADIR/boot_archive\n\
2200 See http://illumos.org/msg/SUNOS-8000-AK for details.\n"
2201 
2202 static void
dboot_init_xboot_consinfo(void)2203 dboot_init_xboot_consinfo(void)
2204 {
2205 	bi = &boot_info;
2206 
2207 #if !defined(__xpv)
2208 	fb = &framebuffer;
2209 	bi->bi_framebuffer = (native_ptr_t)(uintptr_t)fb;
2210 
2211 	switch (multiboot_version) {
2212 	case 1:
2213 		dboot_multiboot1_xboot_consinfo();
2214 		break;
2215 	case 2:
2216 		dboot_multiboot2_xboot_consinfo();
2217 		break;
2218 	default:
2219 		dboot_panic("Unknown multiboot version: %d\n",
2220 		    multiboot_version);
2221 		break;
2222 	}
2223 	dboot_find_console_modules();
2224 #endif
2225 }
2226 
2227 /*
2228  * Set up basic data from the boot loader.
2229  * The load_addr is part of AOUT kludge setup in dboot_grub.s, to support
2230  * 32-bit dboot code setup used to set up and start 64-bit kernel.
2231  * AOUT kludge does allow 32-bit boot loader, such as grub1, to load and
2232  * start 64-bit illumos kernel.
2233  */
2234 static void
dboot_loader_init(void)2235 dboot_loader_init(void)
2236 {
2237 #if !defined(__xpv)
2238 	mb_info = NULL;
2239 	mb2_info = NULL;
2240 
2241 	switch (mb_magic) {
2242 	case MB_BOOTLOADER_MAGIC:
2243 		multiboot_version = 1;
2244 		mb_info = (multiboot_info_t *)(uintptr_t)mb_addr;
2245 #if defined(_BOOT_TARGET_amd64)
2246 		load_addr = mb_header.load_addr;
2247 #endif
2248 		break;
2249 
2250 	case MULTIBOOT2_BOOTLOADER_MAGIC:
2251 		multiboot_version = 2;
2252 		mb2_info = (multiboot2_info_header_t *)(uintptr_t)mb_addr;
2253 #if defined(_BOOT_TARGET_amd64)
2254 		load_addr = mb2_load_addr;
2255 #endif
2256 		break;
2257 
2258 	default:
2259 		dboot_panic("Unknown bootloader magic: 0x%x\n", mb_magic);
2260 		break;
2261 	}
2262 #endif	/* !defined(__xpv) */
2263 }
2264 
2265 /* Extract the kernel command line from [multi]boot information. */
2266 static char *
dboot_loader_cmdline(void)2267 dboot_loader_cmdline(void)
2268 {
2269 	char *line = NULL;
2270 
2271 #if defined(__xpv)
2272 	line = (char *)xen_info->cmd_line;
2273 #else /* __xpv */
2274 
2275 	switch (multiboot_version) {
2276 	case 1:
2277 		if (mb_info->flags & MB_INFO_CMDLINE)
2278 			line = (char *)mb_info->cmdline;
2279 		break;
2280 
2281 	case 2:
2282 		line = dboot_multiboot2_cmdline(mb2_info);
2283 		break;
2284 
2285 	default:
2286 		dboot_panic("Unknown multiboot version: %d\n",
2287 		    multiboot_version);
2288 		break;
2289 	}
2290 
2291 #endif /* __xpv */
2292 
2293 	/*
2294 	 * Make sure we have valid pointer so the string operations
2295 	 * will not crash us.
2296 	 */
2297 	if (line == NULL)
2298 		line = "";
2299 
2300 	return (line);
2301 }
2302 
2303 static char *
dboot_loader_name(void)2304 dboot_loader_name(void)
2305 {
2306 #if defined(__xpv)
2307 	return (NULL);
2308 #else /* __xpv */
2309 	multiboot_tag_string_t *tag;
2310 
2311 	switch (multiboot_version) {
2312 	case 1:
2313 		return ((char *)(uintptr_t)mb_info->boot_loader_name);
2314 
2315 	case 2:
2316 		tag = dboot_multiboot2_find_tag(mb2_info,
2317 		    MULTIBOOT_TAG_TYPE_BOOT_LOADER_NAME);
2318 		return (tag->mb_string);
2319 	default:
2320 		dboot_panic("Unknown multiboot version: %d\n",
2321 		    multiboot_version);
2322 		break;
2323 	}
2324 
2325 	return (NULL);
2326 #endif /* __xpv */
2327 }
2328 
2329 /*
2330  * startup_kernel has a pretty simple job. It builds pagetables which reflect
2331  * 1:1 mappings for all memory in use. It then also adds mappings for
2332  * the kernel nucleus at virtual address of target_kernel_text using large page
2333  * mappings. The page table pages are also accessible at 1:1 mapped
2334  * virtual addresses.
2335  */
2336 /*ARGSUSED*/
2337 void
startup_kernel(void)2338 startup_kernel(void)
2339 {
2340 	char *cmdline;
2341 	char *bootloader;
2342 #if defined(__xpv)
2343 	physdev_set_iopl_t set_iopl;
2344 #endif /* __xpv */
2345 
2346 	if (dboot_debug == 1)
2347 		bcons_init(NULL);	/* Set very early console to ttya. */
2348 	dboot_loader_init();
2349 	/*
2350 	 * At this point we are executing in a 32 bit real mode.
2351 	 */
2352 
2353 	bootloader = dboot_loader_name();
2354 	cmdline = dboot_loader_cmdline();
2355 
2356 #if defined(__xpv)
2357 	/*
2358 	 * For dom0, before we initialize the console subsystem we'll
2359 	 * need to enable io operations, so set I/O priveldge level to 1.
2360 	 */
2361 	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
2362 		set_iopl.iopl = 1;
2363 		(void) HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
2364 	}
2365 #endif /* __xpv */
2366 
2367 	dboot_init_xboot_consinfo();
2368 	bi->bi_cmdline = (native_ptr_t)(uintptr_t)cmdline;
2369 	bcons_init(bi);		/* Now we can set the real console. */
2370 
2371 	prom_debug = (find_boot_prop("prom_debug") != NULL);
2372 	map_debug = (find_boot_prop("map_debug") != NULL);
2373 
2374 #if !defined(__xpv)
2375 	dboot_multiboot_get_fwtables();
2376 #endif
2377 	DBG_MSG("\n\nillumos prekernel set: ");
2378 	DBG_MSG(cmdline);
2379 	DBG_MSG("\n");
2380 
2381 	if (bootloader != NULL && prom_debug) {
2382 		dboot_printf("Kernel loaded by: %s\n", bootloader);
2383 #if !defined(__xpv)
2384 		dboot_printf("Using multiboot %d boot protocol.\n",
2385 		    multiboot_version);
2386 #endif
2387 	}
2388 
2389 	if (strstr(cmdline, "multiboot") != NULL) {
2390 		dboot_panic(NO_MULTIBOOT);
2391 	}
2392 
2393 	DBG((uintptr_t)bi);
2394 #if !defined(__xpv)
2395 	DBG((uintptr_t)mb_info);
2396 	DBG((uintptr_t)mb2_info);
2397 	if (mb2_info != NULL)
2398 		DBG(mb2_info->mbi_total_size);
2399 	DBG(bi->bi_acpi_rsdp);
2400 	DBG(bi->bi_acpi_rsdp_copy);
2401 	DBG(bi->bi_smbios);
2402 	DBG(bi->bi_uefi_arch);
2403 	DBG(bi->bi_uefi_systab);
2404 
2405 	if (bi->bi_uefi_systab && prom_debug) {
2406 		if (bi->bi_uefi_arch == XBI_UEFI_ARCH_64) {
2407 			print_efi64((EFI_SYSTEM_TABLE64 *)(uintptr_t)
2408 			    bi->bi_uefi_systab);
2409 		} else {
2410 			print_efi32((EFI_SYSTEM_TABLE32 *)(uintptr_t)
2411 			    bi->bi_uefi_systab);
2412 		}
2413 	}
2414 #endif
2415 
2416 	/*
2417 	 * Need correct target_kernel_text value
2418 	 */
2419 	target_kernel_text = KERNEL_TEXT;
2420 	DBG(target_kernel_text);
2421 
2422 #if defined(__xpv)
2423 
2424 	/*
2425 	 * XXPV	Derive this stuff from CPUID / what the hypervisor has enabled
2426 	 */
2427 
2428 #if defined(_BOOT_TARGET_amd64)
2429 	/*
2430 	 * 64-bit hypervisor.
2431 	 */
2432 	amd64_support = 1;
2433 	pae_support = 1;
2434 
2435 #else	/* _BOOT_TARGET_amd64 */
2436 
2437 	/*
2438 	 * See if we are running on a PAE Hypervisor
2439 	 */
2440 	{
2441 		xen_capabilities_info_t caps;
2442 
2443 		if (HYPERVISOR_xen_version(XENVER_capabilities, &caps) != 0)
2444 			dboot_panic("HYPERVISOR_xen_version(caps) failed");
2445 		caps[sizeof (caps) - 1] = 0;
2446 		if (prom_debug)
2447 			dboot_printf("xen capabilities %s\n", caps);
2448 		if (strstr(caps, "x86_32p") != NULL)
2449 			pae_support = 1;
2450 	}
2451 
2452 #endif	/* _BOOT_TARGET_amd64 */
2453 	{
2454 		xen_platform_parameters_t p;
2455 
2456 		if (HYPERVISOR_xen_version(XENVER_platform_parameters, &p) != 0)
2457 			dboot_panic("HYPERVISOR_xen_version(parms) failed");
2458 		DBG(p.virt_start);
2459 		mfn_to_pfn_mapping = (pfn_t *)(xen_virt_start = p.virt_start);
2460 	}
2461 
2462 	/*
2463 	 * The hypervisor loads stuff starting at 1Gig
2464 	 */
2465 	mfn_base = ONE_GIG;
2466 	DBG(mfn_base);
2467 
2468 	/*
2469 	 * enable writable page table mode for the hypervisor
2470 	 */
2471 	if (HYPERVISOR_vm_assist(VMASST_CMD_enable,
2472 	    VMASST_TYPE_writable_pagetables) < 0)
2473 		dboot_panic("HYPERVISOR_vm_assist(writable_pagetables) failed");
2474 
2475 	/*
2476 	 * check for NX support
2477 	 */
2478 	if (pae_support) {
2479 		uint32_t eax = 0x80000000;
2480 		uint32_t edx = get_cpuid_edx(&eax);
2481 
2482 		if (eax >= 0x80000001) {
2483 			eax = 0x80000001;
2484 			edx = get_cpuid_edx(&eax);
2485 			if (edx & CPUID_AMD_EDX_NX)
2486 				NX_support = 1;
2487 		}
2488 	}
2489 
2490 	/*
2491 	 * check for PAT support
2492 	 */
2493 	{
2494 		uint32_t eax = 1;
2495 		uint32_t edx = get_cpuid_edx(&eax);
2496 
2497 		if (edx & CPUID_INTC_EDX_PAT)
2498 			PAT_support = 1;
2499 	}
2500 #if !defined(_BOOT_TARGET_amd64)
2501 
2502 	/*
2503 	 * The 32-bit hypervisor uses segmentation to protect itself from
2504 	 * guests. This means when a guest attempts to install a flat 4GB
2505 	 * code or data descriptor the 32-bit hypervisor will protect itself
2506 	 * by silently shrinking the segment such that if the guest attempts
2507 	 * any access where the hypervisor lives a #gp fault is generated.
2508 	 * The problem is that some applications expect a full 4GB flat
2509 	 * segment for their current thread pointer and will use negative
2510 	 * offset segment wrap around to access data. TLS support in linux
2511 	 * brand is one example of this.
2512 	 *
2513 	 * The 32-bit hypervisor can catch the #gp fault in these cases
2514 	 * and emulate the access without passing the #gp fault to the guest
2515 	 * but only if VMASST_TYPE_4gb_segments is explicitly turned on.
2516 	 * Seems like this should have been the default.
2517 	 * Either way, we want the hypervisor -- and not Solaris -- to deal
2518 	 * to deal with emulating these accesses.
2519 	 */
2520 	if (HYPERVISOR_vm_assist(VMASST_CMD_enable,
2521 	    VMASST_TYPE_4gb_segments) < 0)
2522 		dboot_panic("HYPERVISOR_vm_assist(4gb_segments) failed");
2523 #endif	/* !_BOOT_TARGET_amd64 */
2524 
2525 #else	/* __xpv */
2526 
2527 	/*
2528 	 * use cpuid to enable MMU features
2529 	 */
2530 	if (have_cpuid()) {
2531 		uint32_t eax, edx;
2532 
2533 		eax = 1;
2534 		edx = get_cpuid_edx(&eax);
2535 		if (edx & CPUID_INTC_EDX_PSE)
2536 			largepage_support = 1;
2537 		if (edx & CPUID_INTC_EDX_PGE)
2538 			pge_support = 1;
2539 		if (edx & CPUID_INTC_EDX_PAE)
2540 			pae_support = 1;
2541 		if (edx & CPUID_INTC_EDX_PAT)
2542 			PAT_support = 1;
2543 
2544 		eax = 0x80000000;
2545 		edx = get_cpuid_edx(&eax);
2546 		if (eax >= 0x80000001) {
2547 			eax = 0x80000001;
2548 			edx = get_cpuid_edx(&eax);
2549 			if (edx & CPUID_AMD_EDX_LM)
2550 				amd64_support = 1;
2551 			if (edx & CPUID_AMD_EDX_NX)
2552 				NX_support = 1;
2553 		}
2554 	} else {
2555 		dboot_printf("cpuid not supported\n");
2556 	}
2557 #endif /* __xpv */
2558 
2559 
2560 #if defined(_BOOT_TARGET_amd64)
2561 	if (amd64_support == 0)
2562 		dboot_panic("long mode not supported, rebooting");
2563 	else if (pae_support == 0)
2564 		dboot_panic("long mode, but no PAE; rebooting");
2565 #else
2566 	/*
2567 	 * Allow the command line to over-ride use of PAE for 32 bit.
2568 	 */
2569 	if (strstr(cmdline, "disablePAE=true") != NULL) {
2570 		pae_support = 0;
2571 		NX_support = 0;
2572 		amd64_support = 0;
2573 	}
2574 #endif
2575 
2576 	/*
2577 	 * initialize the simple memory allocator
2578 	 */
2579 	init_mem_alloc();
2580 
2581 #if !defined(__xpv) && !defined(_BOOT_TARGET_amd64)
2582 	/*
2583 	 * disable PAE on 32 bit h/w w/o NX and < 4Gig of memory
2584 	 */
2585 	if (max_mem < FOUR_GIG && NX_support == 0)
2586 		pae_support = 0;
2587 #endif
2588 
2589 	/*
2590 	 * configure mmu information
2591 	 */
2592 	if (pae_support) {
2593 		shift_amt = shift_amt_pae;
2594 		ptes_per_table = 512;
2595 		pte_size = 8;
2596 		lpagesize = TWO_MEG;
2597 #if defined(_BOOT_TARGET_amd64)
2598 		top_level = 3;
2599 #else
2600 		top_level = 2;
2601 #endif
2602 	} else {
2603 		pae_support = 0;
2604 		NX_support = 0;
2605 		shift_amt = shift_amt_nopae;
2606 		ptes_per_table = 1024;
2607 		pte_size = 4;
2608 		lpagesize = FOUR_MEG;
2609 		top_level = 1;
2610 	}
2611 
2612 	DBG(PAT_support);
2613 	DBG(pge_support);
2614 	DBG(NX_support);
2615 	DBG(largepage_support);
2616 	DBG(amd64_support);
2617 	DBG(top_level);
2618 	DBG(pte_size);
2619 	DBG(ptes_per_table);
2620 	DBG(lpagesize);
2621 
2622 #if defined(__xpv)
2623 	ktext_phys = ONE_GIG;		/* from UNIX Mapfile */
2624 #else
2625 	ktext_phys = FOUR_MEG;		/* from UNIX Mapfile */
2626 #endif
2627 
2628 #if !defined(__xpv) && defined(_BOOT_TARGET_amd64)
2629 	/*
2630 	 * For grub, copy kernel bits from the ELF64 file to final place.
2631 	 */
2632 	DBG_MSG("\nAllocating nucleus pages.\n");
2633 	ktext_phys = (uintptr_t)do_mem_alloc(ksize, FOUR_MEG);
2634 
2635 	if (ktext_phys == 0)
2636 		dboot_panic("failed to allocate aligned kernel memory");
2637 	DBG(load_addr);
2638 	if (dboot_elfload64(load_addr) != 0)
2639 		dboot_panic("failed to parse kernel ELF image, rebooting");
2640 #endif
2641 
2642 	DBG(ktext_phys);
2643 
2644 	/*
2645 	 * Allocate page tables.
2646 	 */
2647 	build_page_tables();
2648 
2649 	/*
2650 	 * return to assembly code to switch to running kernel
2651 	 */
2652 	entry_addr_low = (uint32_t)target_kernel_text;
2653 	DBG(entry_addr_low);
2654 	bi->bi_use_largepage = largepage_support;
2655 	bi->bi_use_pae = pae_support;
2656 	bi->bi_use_pge = pge_support;
2657 	bi->bi_use_nx = NX_support;
2658 
2659 #if defined(__xpv)
2660 
2661 	bi->bi_next_paddr = next_avail_addr - mfn_base;
2662 	DBG(bi->bi_next_paddr);
2663 	bi->bi_next_vaddr = (native_ptr_t)(uintptr_t)next_avail_addr;
2664 	DBG(bi->bi_next_vaddr);
2665 
2666 	/*
2667 	 * unmap unused pages in start area to make them available for DMA
2668 	 */
2669 	while (next_avail_addr < scratch_end) {
2670 		(void) HYPERVISOR_update_va_mapping(next_avail_addr,
2671 		    0, UVMF_INVLPG | UVMF_LOCAL);
2672 		next_avail_addr += MMU_PAGESIZE;
2673 	}
2674 
2675 	bi->bi_xen_start_info = (native_ptr_t)(uintptr_t)xen_info;
2676 	DBG((uintptr_t)HYPERVISOR_shared_info);
2677 	bi->bi_shared_info = (native_ptr_t)HYPERVISOR_shared_info;
2678 	bi->bi_top_page_table = (uintptr_t)top_page_table - mfn_base;
2679 
2680 #else /* __xpv */
2681 
2682 	bi->bi_next_paddr = next_avail_addr;
2683 	DBG(bi->bi_next_paddr);
2684 	bi->bi_next_vaddr = (native_ptr_t)(uintptr_t)next_avail_addr;
2685 	DBG(bi->bi_next_vaddr);
2686 	bi->bi_mb_version = multiboot_version;
2687 
2688 	switch (multiboot_version) {
2689 	case 1:
2690 		bi->bi_mb_info = (native_ptr_t)(uintptr_t)mb_info;
2691 		break;
2692 	case 2:
2693 		bi->bi_mb_info = (native_ptr_t)(uintptr_t)mb2_info;
2694 		break;
2695 	default:
2696 		dboot_panic("Unknown multiboot version: %d\n",
2697 		    multiboot_version);
2698 		break;
2699 	}
2700 	bi->bi_top_page_table = (uintptr_t)top_page_table;
2701 
2702 #endif /* __xpv */
2703 
2704 	bi->bi_kseg_size = FOUR_MEG;
2705 	DBG(bi->bi_kseg_size);
2706 
2707 #ifndef __xpv
2708 	if (map_debug)
2709 		dump_tables();
2710 #endif
2711 
2712 	DBG_MSG("\n\n*** DBOOT DONE -- back to asm to jump to kernel\n\n");
2713 
2714 #ifndef __xpv
2715 	/* Update boot info with FB data */
2716 	fb->cursor.origin.x = fb_info.cursor.origin.x;
2717 	fb->cursor.origin.y = fb_info.cursor.origin.y;
2718 	fb->cursor.pos.x = fb_info.cursor.pos.x;
2719 	fb->cursor.pos.y = fb_info.cursor.pos.y;
2720 	fb->cursor.visible = fb_info.cursor.visible;
2721 #endif
2722 }
2723