xref: /titanic_50/usr/src/uts/i86pc/os/fastboot.c (revision 6bc8bc6a97518e9ec3d440de43fc1a30a7db825f)
119397407SSherry Moore /*
219397407SSherry Moore  * CDDL HEADER START
319397407SSherry Moore  *
419397407SSherry Moore  * The contents of this file are subject to the terms of the
519397407SSherry Moore  * Common Development and Distribution License (the "License").
619397407SSherry Moore  * You may not use this file except in compliance with the License.
719397407SSherry Moore  *
819397407SSherry Moore  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
919397407SSherry Moore  * or http://www.opensolaris.org/os/licensing.
1019397407SSherry Moore  * See the License for the specific language governing permissions
1119397407SSherry Moore  * and limitations under the License.
1219397407SSherry Moore  *
1319397407SSherry Moore  * When distributing Covered Code, include this CDDL HEADER in each
1419397407SSherry Moore  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1519397407SSherry Moore  * If applicable, add the following below this CDDL HEADER, with the
1619397407SSherry Moore  * fields enclosed by brackets "[]" replaced with your own identifying
1719397407SSherry Moore  * information: Portions Copyright [yyyy] [name of copyright owner]
1819397407SSherry Moore  *
1919397407SSherry Moore  * CDDL HEADER END
2019397407SSherry Moore  */
2119397407SSherry Moore 
2219397407SSherry Moore /*
2319397407SSherry Moore  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
2419397407SSherry Moore  * Use is subject to license terms.
2519397407SSherry Moore  */
2619397407SSherry Moore 
27*6bc8bc6aSSherry Moore /*
28*6bc8bc6aSSherry Moore  * This file contains the functions for performing Fast Reboot -- a
29*6bc8bc6aSSherry Moore  * reboot which bypasses the firmware and bootloader, considerably
30*6bc8bc6aSSherry Moore  * reducing downtime.
31*6bc8bc6aSSherry Moore  *
32*6bc8bc6aSSherry Moore  * load_kernel(): This function is invoked by mdpreboot() in the reboot
33*6bc8bc6aSSherry Moore  * path.  It loads the new kernel and boot archive into memory, builds
34*6bc8bc6aSSherry Moore  * the data structure containing sufficient information about the new
35*6bc8bc6aSSherry Moore  * kernel and boot archive to be passed to the fast reboot switcher
36*6bc8bc6aSSherry Moore  * (see fb_swtch_src.s for details).  When invoked the switcher relocates
37*6bc8bc6aSSherry Moore  * the new kernel and boot archive to physically contiguous low memory,
38*6bc8bc6aSSherry Moore  * similar to where the boot loader would have loaded them, and jumps to
39*6bc8bc6aSSherry Moore  * the new kernel.
40*6bc8bc6aSSherry Moore  *
41*6bc8bc6aSSherry Moore  * The physical addresses of the memory allocated for the new kernel, boot
42*6bc8bc6aSSherry Moore  * archive and their page tables must be above where the boot archive ends
43*6bc8bc6aSSherry Moore  * after it has been relocated by the switcher, otherwise the new files
44*6bc8bc6aSSherry Moore  * and their page tables could be overridden during relocation.
45*6bc8bc6aSSherry Moore  *
46*6bc8bc6aSSherry Moore  * fast_reboot(): This function is invoked by mdboot() once it's determined
47*6bc8bc6aSSherry Moore  * that the system is capable of fast reboot.  It jumps to the fast reboot
48*6bc8bc6aSSherry Moore  * switcher with the data structure built by load_kernel() as the argument.
49*6bc8bc6aSSherry Moore  */
5019397407SSherry Moore 
5119397407SSherry Moore #include <sys/types.h>
5219397407SSherry Moore #include <sys/param.h>
5319397407SSherry Moore #include <sys/segments.h>
5419397407SSherry Moore #include <sys/sysmacros.h>
5519397407SSherry Moore #include <sys/vm.h>
5619397407SSherry Moore 
5719397407SSherry Moore #include <sys/proc.h>
5819397407SSherry Moore #include <sys/buf.h>
5919397407SSherry Moore #include <sys/kmem.h>
6019397407SSherry Moore 
6119397407SSherry Moore #include <sys/reboot.h>
6219397407SSherry Moore #include <sys/uadmin.h>
6319397407SSherry Moore 
6419397407SSherry Moore #include <sys/cred.h>
6519397407SSherry Moore #include <sys/vnode.h>
6619397407SSherry Moore #include <sys/file.h>
6719397407SSherry Moore 
6819397407SSherry Moore #include <sys/cmn_err.h>
6919397407SSherry Moore #include <sys/dumphdr.h>
7019397407SSherry Moore #include <sys/bootconf.h>
7119397407SSherry Moore #include <sys/ddidmareq.h>
7219397407SSherry Moore #include <sys/varargs.h>
7319397407SSherry Moore #include <sys/promif.h>
7419397407SSherry Moore #include <sys/modctl.h>
7519397407SSherry Moore 
7619397407SSherry Moore #include <vm/hat.h>
7719397407SSherry Moore #include <vm/as.h>
7819397407SSherry Moore #include <vm/page.h>
7919397407SSherry Moore #include <vm/seg.h>
8019397407SSherry Moore #include <vm/hat_i86.h>
8119397407SSherry Moore #include <sys/vm_machparam.h>
8219397407SSherry Moore #include <sys/archsystm.h>
8319397407SSherry Moore #include <sys/machsystm.h>
8419397407SSherry Moore #include <sys/mman.h>
8519397407SSherry Moore #include <sys/x86_archext.h>
8619397407SSherry Moore 
8719397407SSherry Moore #include <sys/fastboot.h>
8819397407SSherry Moore #include <sys/machelf.h>
8919397407SSherry Moore #include <sys/kobj.h>
9019397407SSherry Moore #include <sys/multiboot.h>
9119397407SSherry Moore 
92*6bc8bc6aSSherry Moore /*
93*6bc8bc6aSSherry Moore  * Data structure containing necessary information for the fast reboot
94*6bc8bc6aSSherry Moore  * switcher to jump to the new kernel.
95*6bc8bc6aSSherry Moore  */
9619397407SSherry Moore fastboot_info_t newkernel = { 0 };
97*6bc8bc6aSSherry Moore 
9819397407SSherry Moore static char fastboot_filename[2][OBP_MAXPATHLEN] = { { 0 }, { 0 }};
9919397407SSherry Moore static x86pte_t ptp_bits = PT_VALID | PT_REF | PT_USER | PT_WRITABLE;
10019397407SSherry Moore static x86pte_t pte_bits =
10119397407SSherry Moore     PT_VALID | PT_REF | PT_MOD | PT_NOCONSIST | PT_WRITABLE;
10219397407SSherry Moore static uint_t fastboot_shift_amt_pae[] = {12, 21, 30, 39};
10319397407SSherry Moore 
10419397407SSherry Moore int fastboot_debug = 0;
10519397407SSherry Moore int fastboot_contig = 0;
10619397407SSherry Moore 
10719397407SSherry Moore /*
10819397407SSherry Moore  * Fake starting va for new kernel and boot archive.
10919397407SSherry Moore  */
11019397407SSherry Moore static uintptr_t fake_va = FASTBOOT_FAKE_VA;
11119397407SSherry Moore 
11219397407SSherry Moore /*
11319397407SSherry Moore  * Below 1G for page tables as we are using 2G as the fake virtual address for
11419397407SSherry Moore  * the new kernel and boot archive.
11519397407SSherry Moore  */
11619397407SSherry Moore static ddi_dma_attr_t fastboot_below_1G_dma_attr = {
11719397407SSherry Moore 	DMA_ATTR_V0,
11819397407SSherry Moore 	0x0000000008000000ULL,	/* dma_attr_addr_lo: 128MB */
11919397407SSherry Moore 	0x000000003FFFFFFFULL,	/* dma_attr_addr_hi: 1G */
12019397407SSherry Moore 	0x00000000FFFFFFFFULL,	/* dma_attr_count_max */
12119397407SSherry Moore 	0x0000000000001000ULL,	/* dma_attr_align: 4KB */
12219397407SSherry Moore 	1,			/* dma_attr_burstsize */
12319397407SSherry Moore 	1,			/* dma_attr_minxfer */
12419397407SSherry Moore 	0x00000000FFFFFFFFULL,	/* dma_attr_maxxfer */
12519397407SSherry Moore 	0x00000000FFFFFFFFULL,	/* dma_attr_seg */
12619397407SSherry Moore 	1,			/* dma_attr_sgllen */
12719397407SSherry Moore 	0x1000ULL,		/* dma_attr_granular */
12819397407SSherry Moore 	0,			/* dma_attr_flags */
12919397407SSherry Moore };
13019397407SSherry Moore 
13119397407SSherry Moore static ddi_dma_attr_t fastboot_dma_attr = {
13219397407SSherry Moore 	DMA_ATTR_V0,
13319397407SSherry Moore 	0x0000000008000000ULL,	/* dma_attr_addr_lo: 128MB */
13419397407SSherry Moore 	0x0000000FFFFFFFFFULL,	/* dma_attr_addr_hi: 64GB */
13519397407SSherry Moore 	0x00000000FFFFFFFFULL,	/* dma_attr_count_max */
13619397407SSherry Moore 	0x0000000000001000ULL,	/* dma_attr_align: 4KB */
13719397407SSherry Moore 	1,			/* dma_attr_burstsize */
13819397407SSherry Moore 	1,			/* dma_attr_minxfer */
13919397407SSherry Moore 	0x00000000FFFFFFFFULL,	/* dma_attr_maxxfer */
14019397407SSherry Moore 	0x00000000FFFFFFFFULL,	/* dma_attr_seg */
14119397407SSherry Moore 	1,			/* dma_attr_sgllen */
14219397407SSherry Moore 	0x1000ULL,		/* dma_attr_granular */
14319397407SSherry Moore 	0,			/* dma_attr_flags */
14419397407SSherry Moore };
14519397407SSherry Moore 
14619397407SSherry Moore /*
14719397407SSherry Moore  * Various information saved from the previous boot to reconstruct
14819397407SSherry Moore  * multiboot_info.
14919397407SSherry Moore  */
15019397407SSherry Moore extern multiboot_info_t saved_mbi;
15119397407SSherry Moore extern mb_memory_map_t saved_mmap[FASTBOOT_SAVED_MMAP_COUNT];
15219397407SSherry Moore extern struct sol_netinfo saved_drives[FASTBOOT_SAVED_DRIVES_COUNT];
15319397407SSherry Moore extern char saved_cmdline[FASTBOOT_SAVED_CMDLINE_LEN];
15419397407SSherry Moore extern int saved_cmdline_len;
15519397407SSherry Moore 
15619397407SSherry Moore extern void* contig_alloc(size_t size, ddi_dma_attr_t *attr,
15719397407SSherry Moore     uintptr_t align, int cansleep);
158*6bc8bc6aSSherry Moore extern void contig_free(void *addr, size_t size);
159*6bc8bc6aSSherry Moore 
16019397407SSherry Moore 
16119397407SSherry Moore /* PRINTLIKE */
16219397407SSherry Moore extern void vprintf(const char *, va_list);
16319397407SSherry Moore 
16419397407SSherry Moore 
16519397407SSherry Moore /*
16619397407SSherry Moore  * Need to be able to get boot_archives from other places
16719397407SSherry Moore  */
16819397407SSherry Moore #define	BOOTARCHIVE64	"/platform/i86pc/amd64/boot_archive"
16919397407SSherry Moore #define	BOOTARCHIVE32	"/platform/i86pc/boot_archive"
17019397407SSherry Moore #define	BOOTARCHIVE_FAILSAFE	"/boot/x86.miniroot-safe"
17119397407SSherry Moore #define	FAILSAFE_BOOTFILE	"/boot/platform/i86pc/kernel/unix"
17219397407SSherry Moore 
17319397407SSherry Moore static uint_t fastboot_vatoindex(fastboot_info_t *, uintptr_t, int);
17419397407SSherry Moore static void fastboot_map_with_size(fastboot_info_t *, uintptr_t,
17519397407SSherry Moore     paddr_t, size_t, int);
17619397407SSherry Moore static void fastboot_build_pagetables(fastboot_info_t *);
17719397407SSherry Moore static int fastboot_build_mbi(char *, fastboot_info_t *);
17819397407SSherry Moore 
17919397407SSherry Moore static const char fastboot_enomem_msg[] = "Fastboot: Couldn't allocate 0x%"
18019397407SSherry Moore 	PRIx64" bytes below %s to do fast reboot";
18119397407SSherry Moore 
18219397407SSherry Moore static void
18319397407SSherry Moore dprintf(char *fmt, ...)
18419397407SSherry Moore {
18519397407SSherry Moore 	va_list adx;
18619397407SSherry Moore 
18719397407SSherry Moore 	if (!fastboot_debug)
18819397407SSherry Moore 		return;
18919397407SSherry Moore 
19019397407SSherry Moore 	va_start(adx, fmt);
19119397407SSherry Moore 	vprintf(fmt, adx);
19219397407SSherry Moore 	va_end(adx);
19319397407SSherry Moore }
19419397407SSherry Moore 
19519397407SSherry Moore 
19619397407SSherry Moore /*
19719397407SSherry Moore  * Return the index corresponding to a virt address at a given page table level.
19819397407SSherry Moore  */
19919397407SSherry Moore static uint_t
20019397407SSherry Moore fastboot_vatoindex(fastboot_info_t *nk, uintptr_t va, int level)
20119397407SSherry Moore {
20219397407SSherry Moore 	return ((va >> nk->fi_shift_amt[level]) & (nk->fi_ptes_per_table - 1));
20319397407SSherry Moore }
20419397407SSherry Moore 
20519397407SSherry Moore 
20619397407SSherry Moore /*
20719397407SSherry Moore  * Add mapping from vstart to pstart for the specified size.
20819397407SSherry Moore  * Only handles 2 level.  Must use 2M pages.  vstart, pstart
20919397407SSherry Moore  * and size should all have been aligned at 2M boundaries.
21019397407SSherry Moore  */
21119397407SSherry Moore static void
21219397407SSherry Moore fastboot_map_with_size(fastboot_info_t *nk, uintptr_t vstart, paddr_t pstart,
21319397407SSherry Moore     size_t size, int level)
21419397407SSherry Moore {
21519397407SSherry Moore 	x86pte_t	pteval, *table;
21619397407SSherry Moore 	uintptr_t	vaddr;
21719397407SSherry Moore 	paddr_t		paddr;
21819397407SSherry Moore 	int		index, l;
21919397407SSherry Moore 
22019397407SSherry Moore 	table = (x86pte_t *)(nk->fi_pagetable_va);
22119397407SSherry Moore 
22219397407SSherry Moore 	for (l = nk->fi_top_level; l >= level; l--) {
22319397407SSherry Moore 
22419397407SSherry Moore 		index = fastboot_vatoindex(nk, vstart, l);
22519397407SSherry Moore 
22619397407SSherry Moore 		if (l == level) {
22719397407SSherry Moore 			/*
22819397407SSherry Moore 			 * Last level.  Program the page table entries.
22919397407SSherry Moore 			 */
23019397407SSherry Moore 			for (vaddr = vstart, paddr = pstart;
23119397407SSherry Moore 			    vaddr < vstart + size;
23219397407SSherry Moore 			    vaddr += (1ULL << nk->fi_shift_amt[l]),
23319397407SSherry Moore 			    paddr += (1ULL << nk->fi_shift_amt[l])) {
23419397407SSherry Moore 
23519397407SSherry Moore 				uint_t index = fastboot_vatoindex(nk, vaddr, l);
23619397407SSherry Moore 
23719397407SSherry Moore 				if (l > 0)
23819397407SSherry Moore 					pteval = paddr | pte_bits | PT_PAGESIZE;
23919397407SSherry Moore 				else
24019397407SSherry Moore 					pteval = paddr | pte_bits;
24119397407SSherry Moore 
24219397407SSherry Moore 				table[index] = pteval;
24319397407SSherry Moore 			}
24419397407SSherry Moore 		} else if (table[index] & PT_VALID) {
24519397407SSherry Moore 			if (l == level)
24619397407SSherry Moore 				break;
24719397407SSherry Moore 
24819397407SSherry Moore 			table = (x86pte_t *)
24919397407SSherry Moore 			    ((uintptr_t)(((paddr_t)table[index] & MMU_PAGEMASK)
25019397407SSherry Moore 			    - nk->fi_pagetable_pa) + nk->fi_pagetable_va);
25119397407SSherry Moore 		} else {
25219397407SSherry Moore 			/*
25319397407SSherry Moore 			 * Intermediate levels.  Program with either valid
25419397407SSherry Moore 			 * bit or PTP bits.
25519397407SSherry Moore 			 */
25619397407SSherry Moore 			if (l == nk->fi_top_level) {
25719397407SSherry Moore 				table[index] = nk->fi_next_table_pa | PT_VALID;
25819397407SSherry Moore 			} else {
25919397407SSherry Moore 				table[index] = nk->fi_next_table_pa | ptp_bits;
26019397407SSherry Moore 			}
26119397407SSherry Moore 			table = (x86pte_t *)(nk->fi_next_table_va);
26219397407SSherry Moore 			nk->fi_next_table_va += MMU_PAGESIZE;
26319397407SSherry Moore 			nk->fi_next_table_pa += MMU_PAGESIZE;
26419397407SSherry Moore 		}
26519397407SSherry Moore 	}
26619397407SSherry Moore }
26719397407SSherry Moore 
26819397407SSherry Moore /*
26919397407SSherry Moore  * Build page tables for the lower 1G of physical memory using 2M
27019397407SSherry Moore  * pages, and prepare page tables for mapping new kernel and boot
27119397407SSherry Moore  * archive pages using 4K pages.
27219397407SSherry Moore  */
27319397407SSherry Moore static void
27419397407SSherry Moore fastboot_build_pagetables(fastboot_info_t *nk)
27519397407SSherry Moore {
27619397407SSherry Moore 	/*
27719397407SSherry Moore 	 * Map lower 1G physical memory.  Use large pages.
27819397407SSherry Moore 	 */
27919397407SSherry Moore 	fastboot_map_with_size(nk, 0, 0, ONE_GIG, 1);
28019397407SSherry Moore 
28119397407SSherry Moore 	/*
28219397407SSherry Moore 	 * Map one 4K page to get the middle page tables set up.
28319397407SSherry Moore 	 */
28419397407SSherry Moore 	fake_va = P2ALIGN_TYPED(fake_va, nk->fi_lpagesize, uintptr_t);
28519397407SSherry Moore 	fastboot_map_with_size(nk, fake_va,
28619397407SSherry Moore 	    nk->fi_files[0].fb_pte_list_va[0] & MMU_PAGEMASK, PAGESIZE, 0);
28719397407SSherry Moore }
28819397407SSherry Moore 
28919397407SSherry Moore 
29019397407SSherry Moore /*
29119397407SSherry Moore  * Sanity check.  Look for dboot offset.
29219397407SSherry Moore  */
29319397407SSherry Moore static int
29419397407SSherry Moore fastboot_elf64_find_dboot_load_offset(void *img, off_t imgsz, uint32_t *offp)
29519397407SSherry Moore {
29619397407SSherry Moore 	Elf64_Ehdr	*ehdr = (Elf64_Ehdr *)img;
29719397407SSherry Moore 	Elf64_Phdr	*phdr;
29819397407SSherry Moore 	uint8_t		*phdrbase;
29919397407SSherry Moore 	int		i;
30019397407SSherry Moore 
30119397407SSherry Moore 	if ((ehdr->e_phoff + ehdr->e_phnum * ehdr->e_phentsize) >= imgsz)
30219397407SSherry Moore 		return (-1);
30319397407SSherry Moore 
30419397407SSherry Moore 	phdrbase = (uint8_t *)img + ehdr->e_phoff;
30519397407SSherry Moore 
30619397407SSherry Moore 	for (i = 0; i < ehdr->e_phnum; i++) {
30719397407SSherry Moore 		phdr = (Elf64_Phdr *)(phdrbase + ehdr->e_phentsize * i);
30819397407SSherry Moore 
30919397407SSherry Moore 		if (phdr->p_type == PT_LOAD) {
31019397407SSherry Moore 			if (phdr->p_vaddr == phdr->p_paddr &&
31119397407SSherry Moore 			    phdr->p_vaddr == DBOOT_ENTRY_ADDRESS) {
31219397407SSherry Moore 				ASSERT(phdr->p_offset <= UINT32_MAX);
31319397407SSherry Moore 				*offp = (uint32_t)phdr->p_offset;
31419397407SSherry Moore 				return (0);
31519397407SSherry Moore 			}
31619397407SSherry Moore 		}
31719397407SSherry Moore 	}
31819397407SSherry Moore 
31919397407SSherry Moore 	return (-1);
32019397407SSherry Moore }
32119397407SSherry Moore 
32219397407SSherry Moore 
32319397407SSherry Moore /*
32419397407SSherry Moore  * Initialize text and data section information for 32-bit kernel.
32519397407SSherry Moore  */
32619397407SSherry Moore static int
32719397407SSherry Moore fastboot_elf32_find_loadables(void *img, off_t imgsz, fastboot_section_t *sectp,
32819397407SSherry Moore     int *sectcntp, uint32_t *offp)
32919397407SSherry Moore {
33019397407SSherry Moore 	Elf32_Ehdr	*ehdr = (Elf32_Ehdr *)img;
33119397407SSherry Moore 	Elf32_Phdr	*phdr;
33219397407SSherry Moore 	uint8_t		*phdrbase;
33319397407SSherry Moore 	int		i;
33419397407SSherry Moore 	int		used_sections = 0;
33519397407SSherry Moore 
33619397407SSherry Moore 
33719397407SSherry Moore 	if ((ehdr->e_phoff + ehdr->e_phnum * ehdr->e_phentsize) >= imgsz)
33819397407SSherry Moore 		return (-1);
33919397407SSherry Moore 
34019397407SSherry Moore 	phdrbase = (uint8_t *)img + ehdr->e_phoff;
34119397407SSherry Moore 
34219397407SSherry Moore 	for (i = 0; i < ehdr->e_phnum; i++) {
34319397407SSherry Moore 		phdr = (Elf32_Phdr *)(phdrbase + ehdr->e_phentsize * i);
34419397407SSherry Moore 
34519397407SSherry Moore 		if (phdr->p_type == PT_INTERP)
34619397407SSherry Moore 			return (-1);
34719397407SSherry Moore 
34819397407SSherry Moore 		if (phdr->p_type != PT_LOAD)
34919397407SSherry Moore 			continue;
35019397407SSherry Moore 
35119397407SSherry Moore 		if (phdr->p_vaddr == phdr->p_paddr &&
35219397407SSherry Moore 		    phdr->p_paddr == DBOOT_ENTRY_ADDRESS) {
35319397407SSherry Moore 			*offp = (uint32_t)phdr->p_offset;
35419397407SSherry Moore 		} else {
35519397407SSherry Moore 			sectp[used_sections].fb_sec_offset = phdr->p_offset;
35619397407SSherry Moore 			sectp[used_sections].fb_sec_paddr = phdr->p_paddr;
35719397407SSherry Moore 			sectp[used_sections].fb_sec_size = phdr->p_filesz;
35819397407SSherry Moore 			sectp[used_sections].fb_sec_bss_size =
35919397407SSherry Moore 			    (phdr->p_filesz < phdr->p_memsz) ?
36019397407SSherry Moore 			    (phdr->p_memsz - phdr->p_filesz) : 0;
36119397407SSherry Moore 
36219397407SSherry Moore 			used_sections++;
36319397407SSherry Moore 		}
36419397407SSherry Moore 
36519397407SSherry Moore 	}
36619397407SSherry Moore 
36719397407SSherry Moore 	*sectcntp = used_sections;
36819397407SSherry Moore 	return (0);
36919397407SSherry Moore }
37019397407SSherry Moore 
37119397407SSherry Moore /*
37219397407SSherry Moore  * Create multiboot info structure
37319397407SSherry Moore  */
37419397407SSherry Moore static int
37519397407SSherry Moore fastboot_build_mbi(char *mdep, fastboot_info_t *nk)
37619397407SSherry Moore {
37719397407SSherry Moore 	mb_module_t	*mbp;
37819397407SSherry Moore 	uintptr_t	next_addr;
37919397407SSherry Moore 	uintptr_t	new_mbi_pa;
38019397407SSherry Moore 	size_t		size;
38119397407SSherry Moore 	void		*buf = NULL;
38219397407SSherry Moore 	size_t		arglen;
38319397407SSherry Moore 	char		bootargs[OBP_MAXPATHLEN];
38419397407SSherry Moore 
38519397407SSherry Moore 	bzero(bootargs, OBP_MAXPATHLEN);
38619397407SSherry Moore 
387*6bc8bc6aSSherry Moore 	if (mdep != NULL && strlen(mdep) != 0) {
38819397407SSherry Moore 		arglen = strlen(mdep) + 1;
38919397407SSherry Moore 	} else {
39019397407SSherry Moore 		arglen = saved_cmdline_len;
39119397407SSherry Moore 	}
39219397407SSherry Moore 
39319397407SSherry Moore 	size = PAGESIZE + P2ROUNDUP(arglen, PAGESIZE);
39419397407SSherry Moore 	buf = contig_alloc(size, &fastboot_below_1G_dma_attr, PAGESIZE, 0);
39519397407SSherry Moore 	if (buf == NULL) {
39619397407SSherry Moore 		cmn_err(CE_WARN, fastboot_enomem_msg, (uint64_t)size, "1G");
39719397407SSherry Moore 		return (-1);
39819397407SSherry Moore 	}
39919397407SSherry Moore 
40019397407SSherry Moore 	bzero(buf, size);
40119397407SSherry Moore 
40219397407SSherry Moore 	new_mbi_pa = mmu_ptob((uint64_t)hat_getpfnum(kas.a_hat, (caddr_t)buf));
40319397407SSherry Moore 
40419397407SSherry Moore 	hat_devload(kas.a_hat, (caddr_t)new_mbi_pa, size,
40519397407SSherry Moore 	    mmu_btop(new_mbi_pa), PROT_READ | PROT_WRITE, HAT_LOAD_NOCONSIST);
40619397407SSherry Moore 
40719397407SSherry Moore 	nk->fi_new_mbi_pa = (paddr_t)new_mbi_pa;
40819397407SSherry Moore 
40919397407SSherry Moore 	bcopy(&saved_mbi, (void *)new_mbi_pa, sizeof (multiboot_info_t));
41019397407SSherry Moore 
41119397407SSherry Moore 	next_addr = new_mbi_pa + sizeof (multiboot_info_t);
41219397407SSherry Moore 	((multiboot_info_t *)new_mbi_pa)->mods_addr = next_addr;
41319397407SSherry Moore 	mbp = (mb_module_t *)(uintptr_t)next_addr;
41419397407SSherry Moore 	mbp->mod_start = newkernel.fi_files[FASTBOOT_BOOTARCHIVE].fb_dest_pa;
41519397407SSherry Moore 	mbp->mod_end = newkernel.fi_files[FASTBOOT_BOOTARCHIVE].fb_next_pa;
41619397407SSherry Moore 
41719397407SSherry Moore 	next_addr += sizeof (mb_module_t);
41819397407SSherry Moore 	bcopy(fastboot_filename[FASTBOOT_NAME_BOOTARCHIVE], (void *)next_addr,
41919397407SSherry Moore 	    strlen(fastboot_filename[FASTBOOT_NAME_BOOTARCHIVE]));
42019397407SSherry Moore 
42119397407SSherry Moore 	mbp->mod_name = next_addr;
42219397407SSherry Moore 	mbp->reserved = 0;
42319397407SSherry Moore 	next_addr += strlen(fastboot_filename[FASTBOOT_NAME_BOOTARCHIVE]);
42419397407SSherry Moore 	*(char *)next_addr = '\0';
42519397407SSherry Moore 	next_addr++;
42619397407SSherry Moore 	next_addr = P2ROUNDUP_TYPED(next_addr, 16, uintptr_t);
42719397407SSherry Moore 
42819397407SSherry Moore 	((multiboot_info_t *)new_mbi_pa)->mmap_addr = next_addr;
42919397407SSherry Moore 	bcopy((void *)(uintptr_t)saved_mmap, (void *)next_addr,
43019397407SSherry Moore 	    saved_mbi.mmap_length);
43119397407SSherry Moore 	next_addr += saved_mbi.mmap_length;
43219397407SSherry Moore 
43319397407SSherry Moore 	((multiboot_info_t *)new_mbi_pa)->drives_addr = next_addr;
43419397407SSherry Moore 	bcopy((void *)(uintptr_t)saved_drives, (void *)next_addr,
43519397407SSherry Moore 	    saved_mbi.drives_length);
43619397407SSherry Moore 	next_addr += saved_mbi.drives_length;
43719397407SSherry Moore 
43819397407SSherry Moore 	((multiboot_info_t *)new_mbi_pa)->cmdline = next_addr;
43919397407SSherry Moore 
440*6bc8bc6aSSherry Moore 	if (mdep != NULL && strlen(mdep) != 0) {
44119397407SSherry Moore 		bcopy(mdep, (void *)(uintptr_t)
44219397407SSherry Moore 		    (((multiboot_info_t *)new_mbi_pa)->cmdline), (arglen - 1));
44319397407SSherry Moore 	} else {
44419397407SSherry Moore 		bcopy((void *)saved_cmdline, (void *)next_addr, (arglen - 1));
44519397407SSherry Moore 	}
44619397407SSherry Moore 	/* Terminate the string */
44719397407SSherry Moore 	((char *)(intptr_t)next_addr)[arglen - 1] = '\0';
44819397407SSherry Moore 
44919397407SSherry Moore 	return (0);
45019397407SSherry Moore }
45119397407SSherry Moore 
452*6bc8bc6aSSherry Moore /*
453*6bc8bc6aSSherry Moore  * Initialize HAT related fields
454*6bc8bc6aSSherry Moore  */
455*6bc8bc6aSSherry Moore static void
456*6bc8bc6aSSherry Moore fastboot_init_fields(fastboot_info_t *nk)
45719397407SSherry Moore {
45819397407SSherry Moore 	if (x86_feature & X86_PAE) {
459*6bc8bc6aSSherry Moore 		nk->fi_has_pae = 1;
460*6bc8bc6aSSherry Moore 		nk->fi_shift_amt = fastboot_shift_amt_pae;
461*6bc8bc6aSSherry Moore 		nk->fi_ptes_per_table = 512;
462*6bc8bc6aSSherry Moore 		nk->fi_lpagesize = (2 << 20);	/* 2M */
463*6bc8bc6aSSherry Moore 		nk->fi_top_level = 2;
464*6bc8bc6aSSherry Moore 	}
46519397407SSherry Moore }
46619397407SSherry Moore 
467*6bc8bc6aSSherry Moore /*
468*6bc8bc6aSSherry Moore  * Process boot argument
469*6bc8bc6aSSherry Moore  */
470*6bc8bc6aSSherry Moore static void
471*6bc8bc6aSSherry Moore fastboot_parse_mdep(char *mdep, char *kern_bootpath, int *bootpath_len,
472*6bc8bc6aSSherry Moore     char *bootargs)
473*6bc8bc6aSSherry Moore {
474*6bc8bc6aSSherry Moore 	int	i;
47519397407SSherry Moore 
47619397407SSherry Moore 	/*
47719397407SSherry Moore 	 * If mdep is not NULL, it comes in the format of
47819397407SSherry Moore 	 *	mountpoint unix args
47919397407SSherry Moore 	 */
480*6bc8bc6aSSherry Moore 	if (mdep != NULL && strlen(mdep) != 0) {
48119397407SSherry Moore 		if (mdep[0] != '-') {
48219397407SSherry Moore 			/* First get the root argument */
48319397407SSherry Moore 			i = 0;
48419397407SSherry Moore 			while (mdep[i] != '\0' && mdep[i] != ' ') {
48519397407SSherry Moore 				i++;
48619397407SSherry Moore 			}
48719397407SSherry Moore 
48819397407SSherry Moore 			if (i < 4 || strncmp(&mdep[i-4], "unix", 4) != 0) {
48919397407SSherry Moore 				/* mount point */
49019397407SSherry Moore 				bcopy(mdep, kern_bootpath, i);
49119397407SSherry Moore 				kern_bootpath[i] = '\0';
492*6bc8bc6aSSherry Moore 				*bootpath_len = i;
49319397407SSherry Moore 
49419397407SSherry Moore 				/*
49519397407SSherry Moore 				 * Get the next argument. It should be unix as
49619397407SSherry Moore 				 * we have validated in in halt.c.
49719397407SSherry Moore 				 */
49819397407SSherry Moore 				if (strlen(mdep) > i) {
49919397407SSherry Moore 					mdep += (i + 1);
50019397407SSherry Moore 					i = 0;
50119397407SSherry Moore 					while (mdep[i] != '\0' &&
50219397407SSherry Moore 					    mdep[i] != ' ') {
50319397407SSherry Moore 						i++;
50419397407SSherry Moore 					}
50519397407SSherry Moore 				}
50619397407SSherry Moore 
50719397407SSherry Moore 			}
50819397407SSherry Moore 			bcopy(mdep, kern_bootfile, i);
50919397407SSherry Moore 			kern_bootfile[i] = '\0';
510*6bc8bc6aSSherry Moore 			bcopy(mdep, bootargs, strlen(mdep));
51119397407SSherry Moore 		} else {
51219397407SSherry Moore 			int off = strlen(kern_bootfile);
51319397407SSherry Moore 			bcopy(kern_bootfile, bootargs, off);
51419397407SSherry Moore 			bcopy(" ", &bootargs[off++], 1);
51519397407SSherry Moore 			bcopy(mdep, &bootargs[off], strlen(mdep));
51619397407SSherry Moore 			off += strlen(mdep);
51719397407SSherry Moore 			bootargs[off] = '\0';
51819397407SSherry Moore 		}
51919397407SSherry Moore 	}
520*6bc8bc6aSSherry Moore }
521*6bc8bc6aSSherry Moore 
522*6bc8bc6aSSherry Moore /*
523*6bc8bc6aSSherry Moore  * Free up the memory we have allocated for this file
524*6bc8bc6aSSherry Moore  */
525*6bc8bc6aSSherry Moore static void
526*6bc8bc6aSSherry Moore fastboot_free_file(fastboot_file_t *fb)
527*6bc8bc6aSSherry Moore {
528*6bc8bc6aSSherry Moore 	size_t	fsize_roundup, pt_size;
529*6bc8bc6aSSherry Moore 	int	pt_entry_count;
530*6bc8bc6aSSherry Moore 
531*6bc8bc6aSSherry Moore 	fsize_roundup = P2ROUNDUP_TYPED(fb->fb_size, PAGESIZE, size_t);
532*6bc8bc6aSSherry Moore 	contig_free((void *)fb->fb_va, fsize_roundup);
533*6bc8bc6aSSherry Moore 
534*6bc8bc6aSSherry Moore 	pt_entry_count = (fsize_roundup >> PAGESHIFT) + 1;
535*6bc8bc6aSSherry Moore 	pt_size = P2ROUNDUP(pt_entry_count * 8, PAGESIZE);
536*6bc8bc6aSSherry Moore 	contig_free((void *)fb->fb_pte_list_va, pt_size);
537*6bc8bc6aSSherry Moore }
538*6bc8bc6aSSherry Moore 
539*6bc8bc6aSSherry Moore /*
540*6bc8bc6aSSherry Moore  * This function performs the following tasks:
541*6bc8bc6aSSherry Moore  * - Read the sizes of the new kernel and boot archive.
542*6bc8bc6aSSherry Moore  * - Allocate memory for the new kernel and boot archive.
543*6bc8bc6aSSherry Moore  * - Allocate memory for page tables necessary for mapping the memory
544*6bc8bc6aSSherry Moore  *   allocated for the files.
545*6bc8bc6aSSherry Moore  * - Read the new kernel and boot archive into memory.
546*6bc8bc6aSSherry Moore  * - Map in the fast reboot switcher.
547*6bc8bc6aSSherry Moore  * - Load the fast reboot switcher to FASTBOOT_SWTCH_PA.
548*6bc8bc6aSSherry Moore  * - Build the new multiboot_info structure
549*6bc8bc6aSSherry Moore  * - Build page tables for the low 1G of physical memory.
550*6bc8bc6aSSherry Moore  * - Mark the data structure as valid if all steps have succeeded.
551*6bc8bc6aSSherry Moore  */
552*6bc8bc6aSSherry Moore void
553*6bc8bc6aSSherry Moore load_kernel(char *mdep)
554*6bc8bc6aSSherry Moore {
555*6bc8bc6aSSherry Moore 	void		*buf = NULL;
556*6bc8bc6aSSherry Moore 	int		i;
557*6bc8bc6aSSherry Moore 	fastboot_file_t	*fb;
558*6bc8bc6aSSherry Moore 	uint32_t	dboot_start_offset;
559*6bc8bc6aSSherry Moore 	char		kern_bootpath[OBP_MAXPATHLEN];
560*6bc8bc6aSSherry Moore 	char		bootargs[OBP_MAXPATHLEN];
561*6bc8bc6aSSherry Moore 	extern uintptr_t postbootkernelbase;
562*6bc8bc6aSSherry Moore 	extern char	fb_swtch_image[];
563*6bc8bc6aSSherry Moore 	int		bootpath_len = 0;
564*6bc8bc6aSSherry Moore 	int		is_failsafe = 0;
565*6bc8bc6aSSherry Moore 	int		is_retry = 0;
566*6bc8bc6aSSherry Moore 	uint64_t	end_addr;
567*6bc8bc6aSSherry Moore 
568*6bc8bc6aSSherry Moore 	ASSERT(fastreboot_capable);
569*6bc8bc6aSSherry Moore 
570*6bc8bc6aSSherry Moore 	postbootkernelbase = 0;
571*6bc8bc6aSSherry Moore 
572*6bc8bc6aSSherry Moore 	/*
573*6bc8bc6aSSherry Moore 	 * Initialize various HAT related fields in the data structure
574*6bc8bc6aSSherry Moore 	 */
575*6bc8bc6aSSherry Moore 	fastboot_init_fields(&newkernel);
576*6bc8bc6aSSherry Moore 
577*6bc8bc6aSSherry Moore 	bzero(kern_bootpath, OBP_MAXPATHLEN);
578*6bc8bc6aSSherry Moore 
579*6bc8bc6aSSherry Moore 	/*
580*6bc8bc6aSSherry Moore 	 * Process the boot argument
581*6bc8bc6aSSherry Moore 	 */
582*6bc8bc6aSSherry Moore 	bzero(bootargs, OBP_MAXPATHLEN);
583*6bc8bc6aSSherry Moore 	fastboot_parse_mdep(mdep, kern_bootpath, &bootpath_len, bootargs);
58419397407SSherry Moore 
58519397407SSherry Moore 	/*
58619397407SSherry Moore 	 * Make sure we get the null character
58719397407SSherry Moore 	 */
58819397407SSherry Moore 	bcopy(kern_bootpath, fastboot_filename[FASTBOOT_NAME_UNIX],
58919397407SSherry Moore 	    bootpath_len);
59019397407SSherry Moore 	bcopy(kern_bootfile,
59119397407SSherry Moore 	    &fastboot_filename[FASTBOOT_NAME_UNIX][bootpath_len],
59219397407SSherry Moore 	    strlen(kern_bootfile) + 1);
59319397407SSherry Moore 
59419397407SSherry Moore 	bcopy(kern_bootpath, fastboot_filename[FASTBOOT_NAME_BOOTARCHIVE],
59519397407SSherry Moore 	    bootpath_len);
59619397407SSherry Moore 
59719397407SSherry Moore 	if (bcmp(kern_bootfile, FAILSAFE_BOOTFILE,
59819397407SSherry Moore 	    (sizeof (FAILSAFE_BOOTFILE) - 1)) == 0) {
59919397407SSherry Moore 		is_failsafe = 1;
60019397407SSherry Moore 	}
60119397407SSherry Moore 
602*6bc8bc6aSSherry Moore load_kernel_retry:
60319397407SSherry Moore 	/*
60419397407SSherry Moore 	 * Read in unix and boot_archive
60519397407SSherry Moore 	 */
606*6bc8bc6aSSherry Moore 	end_addr = DBOOT_ENTRY_ADDRESS;
60719397407SSherry Moore 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
608*6bc8bc6aSSherry Moore 		struct _buf	*file;
609*6bc8bc6aSSherry Moore 		uintptr_t	va;
61019397407SSherry Moore 		uint64_t	fsize;
61119397407SSherry Moore 		size_t		fsize_roundup, pt_size;
61219397407SSherry Moore 		int		page_index;
61319397407SSherry Moore 		uintptr_t	offset;
61419397407SSherry Moore 		int		pt_entry_count;
61519397407SSherry Moore 		ddi_dma_attr_t dma_attr = fastboot_dma_attr;
61619397407SSherry Moore 
617*6bc8bc6aSSherry Moore 
61819397407SSherry Moore 		dprintf("fastboot_filename[%d] = %s\n",
61919397407SSherry Moore 		    i, fastboot_filename[i]);
62019397407SSherry Moore 
62119397407SSherry Moore 		if ((file = kobj_open_file(fastboot_filename[i])) ==
62219397407SSherry Moore 		    (struct _buf *)-1) {
62319397407SSherry Moore 			cmn_err(CE_WARN, "Fastboot: Couldn't open %s",
62419397407SSherry Moore 			    fastboot_filename[i]);
62519397407SSherry Moore 			goto err_out;
62619397407SSherry Moore 		}
62719397407SSherry Moore 
62819397407SSherry Moore 		if (kobj_get_filesize(file, &fsize) != 0) {
62919397407SSherry Moore 			cmn_err(CE_WARN,
63019397407SSherry Moore 			    "Fastboot: Couldn't get filesize for %s",
63119397407SSherry Moore 			    fastboot_filename[i]);
63219397407SSherry Moore 			goto err_out;
63319397407SSherry Moore 		}
63419397407SSherry Moore 
635*6bc8bc6aSSherry Moore 		fsize_roundup = P2ROUNDUP_TYPED(fsize, PAGESIZE, size_t);
636*6bc8bc6aSSherry Moore 
637*6bc8bc6aSSherry Moore 		/*
638*6bc8bc6aSSherry Moore 		 * Where the files end in physical memory after being
639*6bc8bc6aSSherry Moore 		 * relocated by the fast boot switcher.
640*6bc8bc6aSSherry Moore 		 */
641*6bc8bc6aSSherry Moore 		end_addr += fsize_roundup;
642*6bc8bc6aSSherry Moore 		if (end_addr > fastboot_below_1G_dma_attr.dma_attr_addr_hi) {
643*6bc8bc6aSSherry Moore 			cmn_err(CE_WARN, "Fastboot: boot archive is too big");
644*6bc8bc6aSSherry Moore 			goto err_out;
64519397407SSherry Moore 		}
64619397407SSherry Moore 
647*6bc8bc6aSSherry Moore 		/*
648*6bc8bc6aSSherry Moore 		 * Adjust dma_attr_addr_lo so that the new kernel and boot
649*6bc8bc6aSSherry Moore 		 * archive will not be overridden during relocation.
650*6bc8bc6aSSherry Moore 		 */
651*6bc8bc6aSSherry Moore 		if (end_addr > fastboot_dma_attr.dma_attr_addr_lo ||
652*6bc8bc6aSSherry Moore 		    end_addr > fastboot_below_1G_dma_attr.dma_attr_addr_lo) {
653*6bc8bc6aSSherry Moore 
654*6bc8bc6aSSherry Moore 			if (is_retry) {
655*6bc8bc6aSSherry Moore 				/*
656*6bc8bc6aSSherry Moore 				 * If we have already tried and didn't succeed,
657*6bc8bc6aSSherry Moore 				 * just give up.
658*6bc8bc6aSSherry Moore 				 */
659*6bc8bc6aSSherry Moore 				cmn_err(CE_WARN,
660*6bc8bc6aSSherry Moore 				    "Fastboot: boot archive is too big");
661*6bc8bc6aSSherry Moore 				goto err_out;
662*6bc8bc6aSSherry Moore 			} else {
663*6bc8bc6aSSherry Moore 				int j;
664*6bc8bc6aSSherry Moore 
665*6bc8bc6aSSherry Moore 				/* Set the flag so we don't keep retrying */
666*6bc8bc6aSSherry Moore 				is_retry++;
667*6bc8bc6aSSherry Moore 
668*6bc8bc6aSSherry Moore 				/* Adjust dma_attr_addr_lo */
669*6bc8bc6aSSherry Moore 				fastboot_dma_attr.dma_attr_addr_lo = end_addr;
670*6bc8bc6aSSherry Moore 				fastboot_below_1G_dma_attr.dma_attr_addr_lo =
671*6bc8bc6aSSherry Moore 				    end_addr;
672*6bc8bc6aSSherry Moore 
673*6bc8bc6aSSherry Moore 				/*
674*6bc8bc6aSSherry Moore 				 * Free the memory we have already allocated
675*6bc8bc6aSSherry Moore 				 * whose physical addresses might not fit
676*6bc8bc6aSSherry Moore 				 * the new lo and hi constraints.
677*6bc8bc6aSSherry Moore 				 */
678*6bc8bc6aSSherry Moore 				for (j = 0; j < i; j++)
679*6bc8bc6aSSherry Moore 					fastboot_free_file(
680*6bc8bc6aSSherry Moore 					    &newkernel.fi_files[j]);
681*6bc8bc6aSSherry Moore 				goto load_kernel_retry;
682*6bc8bc6aSSherry Moore 			}
683*6bc8bc6aSSherry Moore 		}
684*6bc8bc6aSSherry Moore 
685*6bc8bc6aSSherry Moore 
68619397407SSherry Moore 		if (!fastboot_contig)
68719397407SSherry Moore 			dma_attr.dma_attr_sgllen = (fsize / PAGESIZE) +
68819397407SSherry Moore 			    (((fsize % PAGESIZE) == 0) ? 0 : 1);
68919397407SSherry Moore 
69019397407SSherry Moore 		if ((buf = contig_alloc(fsize, &dma_attr, PAGESIZE, 0))
69119397407SSherry Moore 		    == NULL) {
692*6bc8bc6aSSherry Moore 			cmn_err(CE_WARN, fastboot_enomem_msg, fsize, "64G");
69319397407SSherry Moore 			goto err_out;
69419397407SSherry Moore 		}
69519397407SSherry Moore 
69619397407SSherry Moore 		va = P2ROUNDUP_TYPED((uintptr_t)buf, PAGESIZE, uintptr_t);
69719397407SSherry Moore 
69819397407SSherry Moore 		if (kobj_read_file(file, (char *)va, fsize, 0) < 0) {
69919397407SSherry Moore 			cmn_err(CE_WARN, "Fastboot: Couldn't read %s",
70019397407SSherry Moore 			    fastboot_filename[i]);
70119397407SSherry Moore 			goto err_out;
70219397407SSherry Moore 		}
70319397407SSherry Moore 
70419397407SSherry Moore 		fb = &newkernel.fi_files[i];
70519397407SSherry Moore 		fb->fb_va = va;
70619397407SSherry Moore 		fb->fb_size = fsize;
70719397407SSherry Moore 		fb->fb_sectcnt = 0;
70819397407SSherry Moore 
70919397407SSherry Moore 		/*
71019397407SSherry Moore 		 * Allocate one extra page table entry for terminating
71119397407SSherry Moore 		 * the list.
71219397407SSherry Moore 		 */
71319397407SSherry Moore 		pt_entry_count = (fsize_roundup >> PAGESHIFT) + 1;
71419397407SSherry Moore 		pt_size = P2ROUNDUP(pt_entry_count * 8, PAGESIZE);
71519397407SSherry Moore 
71619397407SSherry Moore 		if ((fb->fb_pte_list_va =
71719397407SSherry Moore 		    (x86pte_t *)contig_alloc(pt_size,
71819397407SSherry Moore 		    &fastboot_below_1G_dma_attr, PAGESIZE, 0)) == NULL) {
71919397407SSherry Moore 			cmn_err(CE_WARN, fastboot_enomem_msg,
72019397407SSherry Moore 			    (uint64_t)pt_size, "1G");
72119397407SSherry Moore 			goto err_out;
72219397407SSherry Moore 		}
72319397407SSherry Moore 
72419397407SSherry Moore 		bzero((void *)(fb->fb_pte_list_va), pt_size);
72519397407SSherry Moore 
72619397407SSherry Moore 		fb->fb_pte_list_pa = mmu_ptob((uint64_t)hat_getpfnum(kas.a_hat,
72719397407SSherry Moore 		    (caddr_t)fb->fb_pte_list_va));
72819397407SSherry Moore 
72919397407SSherry Moore 		for (page_index = 0, offset = 0; offset < fb->fb_size;
73019397407SSherry Moore 		    offset += PAGESIZE) {
73119397407SSherry Moore 			uint64_t paddr;
73219397407SSherry Moore 
73319397407SSherry Moore 			paddr = mmu_ptob((uint64_t)hat_getpfnum(kas.a_hat,
73419397407SSherry Moore 			    (caddr_t)fb->fb_va + offset));
73519397407SSherry Moore 
73619397407SSherry Moore 			ASSERT(paddr >= fastboot_dma_attr.dma_attr_addr_lo);
73719397407SSherry Moore 
73819397407SSherry Moore 			/*
73919397407SSherry Moore 			 * Include the pte_bits so we don't have to make
74019397407SSherry Moore 			 * it in assembly.
74119397407SSherry Moore 			 */
74219397407SSherry Moore 			fb->fb_pte_list_va[page_index++] = (x86pte_t)
74319397407SSherry Moore 			    (paddr | pte_bits);
74419397407SSherry Moore 		}
74519397407SSherry Moore 
74619397407SSherry Moore 		fb->fb_pte_list_va[page_index] = FASTBOOT_TERMINATE;
74719397407SSherry Moore 
74819397407SSherry Moore 		if (i == FASTBOOT_UNIX) {
749*6bc8bc6aSSherry Moore 			Ehdr	*ehdr = (Ehdr *)va;
750*6bc8bc6aSSherry Moore 			int	j;
75119397407SSherry Moore 
75219397407SSherry Moore 			/*
75319397407SSherry Moore 			 * Sanity checks:
75419397407SSherry Moore 			 */
75519397407SSherry Moore 			for (j = 0; j < SELFMAG; j++) {
75619397407SSherry Moore 				if (ehdr->e_ident[j] != ELFMAG[j]) {
75719397407SSherry Moore 					cmn_err(CE_WARN, "Fastboot: Bad ELF "
75819397407SSherry Moore 					    "signature");
75919397407SSherry Moore 					goto err_out;
76019397407SSherry Moore 				}
76119397407SSherry Moore 			}
76219397407SSherry Moore 
76319397407SSherry Moore 			if (ehdr->e_ident[EI_CLASS] == ELFCLASS32 &&
76419397407SSherry Moore 			    ehdr->e_ident[EI_DATA] == ELFDATA2LSB &&
76519397407SSherry Moore 			    ehdr->e_machine == EM_386) {
76619397407SSherry Moore 
76719397407SSherry Moore 				if (fastboot_elf32_find_loadables((void *)va,
76819397407SSherry Moore 				    fsize, &fb->fb_sections[0],
76919397407SSherry Moore 				    &fb->fb_sectcnt, &dboot_start_offset) < 0) {
77019397407SSherry Moore 					cmn_err(CE_WARN, "Fastboot: ELF32 "
77119397407SSherry Moore 					    "program section failure");
77219397407SSherry Moore 					goto err_out;
77319397407SSherry Moore 				}
77419397407SSherry Moore 
77519397407SSherry Moore 				if (fb->fb_sectcnt == 0) {
77619397407SSherry Moore 					cmn_err(CE_WARN, "Fastboot: No ELF32 "
77719397407SSherry Moore 					    "program sections found");
77819397407SSherry Moore 					goto err_out;
77919397407SSherry Moore 				}
78019397407SSherry Moore 
78119397407SSherry Moore 				if (is_failsafe) {
78219397407SSherry Moore 					/* Failsafe boot_archive */
78319397407SSherry Moore 					bcopy(BOOTARCHIVE_FAILSAFE,
78419397407SSherry Moore 					    &fastboot_filename
78519397407SSherry Moore 					    [FASTBOOT_NAME_BOOTARCHIVE]
78619397407SSherry Moore 					    [bootpath_len],
78719397407SSherry Moore 					    sizeof (BOOTARCHIVE_FAILSAFE));
78819397407SSherry Moore 				} else {
78919397407SSherry Moore 					bcopy(BOOTARCHIVE32,
79019397407SSherry Moore 					    &fastboot_filename
79119397407SSherry Moore 					    [FASTBOOT_NAME_BOOTARCHIVE]
79219397407SSherry Moore 					    [bootpath_len],
79319397407SSherry Moore 					    sizeof (BOOTARCHIVE32));
79419397407SSherry Moore 				}
79519397407SSherry Moore 
79619397407SSherry Moore 			} else if (ehdr->e_ident[EI_CLASS] == ELFCLASS64 &&
79719397407SSherry Moore 			    ehdr->e_ident[EI_DATA] == ELFDATA2LSB &&
79819397407SSherry Moore 			    ehdr->e_machine == EM_AMD64) {
79919397407SSherry Moore 
80019397407SSherry Moore 				if (fastboot_elf64_find_dboot_load_offset(
80119397407SSherry Moore 				    (void *)va, fsize, &dboot_start_offset)
80219397407SSherry Moore 				    != 0) {
80319397407SSherry Moore 					cmn_err(CE_WARN, "Fastboot: Couldn't "
80419397407SSherry Moore 					    "find ELF64 dboot entry offset");
80519397407SSherry Moore 					goto err_out;
80619397407SSherry Moore 				}
80719397407SSherry Moore 
80819397407SSherry Moore 				if ((x86_feature & X86_64) == 0 ||
80919397407SSherry Moore 				    newkernel.fi_has_pae == 0) {
81019397407SSherry Moore 					cmn_err(CE_WARN, "Fastboot: Cannot "
81119397407SSherry Moore 					    "reboot to %s: "
81219397407SSherry Moore 					    "not a 64-bit capable system",
81319397407SSherry Moore 					    kern_bootfile);
81419397407SSherry Moore 					goto err_out;
81519397407SSherry Moore 				}
81619397407SSherry Moore 
81719397407SSherry Moore 				bcopy(BOOTARCHIVE64,
81819397407SSherry Moore 				    &fastboot_filename
81919397407SSherry Moore 				    [FASTBOOT_NAME_BOOTARCHIVE][bootpath_len],
82019397407SSherry Moore 				    sizeof (BOOTARCHIVE64));
82119397407SSherry Moore 			} else {
82219397407SSherry Moore 				cmn_err(CE_WARN, "Fastboot: Unknown ELF type");
82319397407SSherry Moore 				goto err_out;
82419397407SSherry Moore 			}
82519397407SSherry Moore 
82619397407SSherry Moore 			fb->fb_dest_pa = DBOOT_ENTRY_ADDRESS -
82719397407SSherry Moore 			    dboot_start_offset;
82819397407SSherry Moore 
82919397407SSherry Moore 			fb->fb_next_pa = DBOOT_ENTRY_ADDRESS + fsize_roundup;
83019397407SSherry Moore 		} else {
83119397407SSherry Moore 			fb->fb_dest_pa = newkernel.fi_files[i - 1].fb_next_pa;
83219397407SSherry Moore 			fb->fb_next_pa = fb->fb_dest_pa + fsize_roundup;
83319397407SSherry Moore 		}
83419397407SSherry Moore 
83519397407SSherry Moore 		kobj_close_file(file);
83619397407SSherry Moore 
837*6bc8bc6aSSherry Moore 	}
838*6bc8bc6aSSherry Moore 
83919397407SSherry Moore 	/*
84019397407SSherry Moore 	 * Set fb_va to fake_va
84119397407SSherry Moore 	 */
842*6bc8bc6aSSherry Moore 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
843*6bc8bc6aSSherry Moore 		newkernel.fi_files[i].fb_va = fake_va;
84419397407SSherry Moore 
845*6bc8bc6aSSherry Moore 	}
84619397407SSherry Moore 
84719397407SSherry Moore 	/*
84819397407SSherry Moore 	 * Add the function that will switch us to 32-bit protected mode
84919397407SSherry Moore 	 */
85019397407SSherry Moore 	fb = &newkernel.fi_files[FASTBOOT_SWTCH];
85119397407SSherry Moore 	fb->fb_va = fb->fb_dest_pa = FASTBOOT_SWTCH_PA;
85219397407SSherry Moore 	fb->fb_size = PAGESIZE;
85319397407SSherry Moore 
85419397407SSherry Moore 	/*
85519397407SSherry Moore 	 * Map in FASTBOOT_SWTCH_PA
85619397407SSherry Moore 	 */
85719397407SSherry Moore 	hat_devload(kas.a_hat, (caddr_t)fb->fb_va, MMU_PAGESIZE,
85819397407SSherry Moore 	    mmu_btop(fb->fb_dest_pa),
85919397407SSherry Moore 	    PROT_READ | PROT_WRITE | PROT_EXEC, HAT_LOAD_NOCONSIST);
86019397407SSherry Moore 
86119397407SSherry Moore 	bcopy((void *)fb_swtch_image, (void *)fb->fb_va, fb->fb_size);
86219397407SSherry Moore 
86319397407SSherry Moore 	/*
86419397407SSherry Moore 	 * Build the new multiboot_info structure
86519397407SSherry Moore 	 */
866*6bc8bc6aSSherry Moore 	if (fastboot_build_mbi(bootargs, &newkernel) != 0) {
86719397407SSherry Moore 		goto err_out;
86819397407SSherry Moore 	}
86919397407SSherry Moore 
87019397407SSherry Moore 	/*
87119397407SSherry Moore 	 * Build page table for low 1G physical memory. Use big pages.
87219397407SSherry Moore 	 * Allocate 4 pages for the page tables.
87319397407SSherry Moore 	 *    1 page for Page-Directory-Pointer Table
87419397407SSherry Moore 	 *    2 page for Page Directory
87519397407SSherry Moore 	 *    1 page for Page Table.
87619397407SSherry Moore 	 * The page table entry will be rewritten to map the physical
87719397407SSherry Moore 	 * address as we do the copying.
87819397407SSherry Moore 	 */
87919397407SSherry Moore 	if (newkernel.fi_has_pae) {
88019397407SSherry Moore 		size_t size = MMU_PAGESIZE * 4;
88119397407SSherry Moore 
88219397407SSherry Moore 		if ((newkernel.fi_pagetable_va = (uintptr_t)
88319397407SSherry Moore 		    contig_alloc(size, &fastboot_below_1G_dma_attr,
88419397407SSherry Moore 		    PAGESIZE, 0)) == NULL) {
88519397407SSherry Moore 			cmn_err(CE_WARN, fastboot_enomem_msg,
88619397407SSherry Moore 			    (uint64_t)size, "1G");
88719397407SSherry Moore 			goto err_out;
88819397407SSherry Moore 		}
88919397407SSherry Moore 
89019397407SSherry Moore 		bzero((void *)(newkernel.fi_pagetable_va), size);
89119397407SSherry Moore 
89219397407SSherry Moore 		newkernel.fi_pagetable_pa =
89319397407SSherry Moore 		    mmu_ptob((uint64_t)hat_getpfnum(kas.a_hat,
89419397407SSherry Moore 		    (caddr_t)newkernel.fi_pagetable_va));
89519397407SSherry Moore 
89619397407SSherry Moore 		newkernel.fi_last_table_pa = newkernel.fi_pagetable_pa +
89719397407SSherry Moore 		    MMU_PAGESIZE * 3;
89819397407SSherry Moore 
89919397407SSherry Moore 		newkernel.fi_next_table_va = newkernel.fi_pagetable_va +
90019397407SSherry Moore 		    MMU_PAGESIZE;
90119397407SSherry Moore 		newkernel.fi_next_table_pa = newkernel.fi_pagetable_pa +
90219397407SSherry Moore 		    MMU_PAGESIZE;
90319397407SSherry Moore 
90419397407SSherry Moore 		fastboot_build_pagetables(&newkernel);
90519397407SSherry Moore 	}
90619397407SSherry Moore 
90719397407SSherry Moore 
90819397407SSherry Moore 	/* Mark it as valid */
90919397407SSherry Moore 	newkernel.fi_valid = 1;
91019397407SSherry Moore 	newkernel.fi_magic = FASTBOOT_MAGIC;
91119397407SSherry Moore 
91219397407SSherry Moore 	return;
91319397407SSherry Moore 
91419397407SSherry Moore err_out:
91519397407SSherry Moore 	newkernel.fi_valid = 0;
91619397407SSherry Moore }
91719397407SSherry Moore 
918*6bc8bc6aSSherry Moore /*
919*6bc8bc6aSSherry Moore  * Jump to the fast reboot switcher.  This function never returns.
920*6bc8bc6aSSherry Moore  */
92119397407SSherry Moore void
92219397407SSherry Moore fast_reboot()
92319397407SSherry Moore {
92419397407SSherry Moore 	void (*fastboot_func)(fastboot_info_t *);
92519397407SSherry Moore 
92619397407SSherry Moore 	fastboot_func = (void (*)())(newkernel.fi_files[FASTBOOT_SWTCH].fb_va);
92719397407SSherry Moore 	(*fastboot_func)(&newkernel);
92819397407SSherry Moore }
929