xref: /titanic_51/usr/src/uts/i86pc/os/fastboot.c (revision 877400d375b54199aef7c18828cb32f991339ff7)
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 
276bc8bc6aSSherry Moore /*
286bc8bc6aSSherry Moore  * This file contains the functions for performing Fast Reboot -- a
296bc8bc6aSSherry Moore  * reboot which bypasses the firmware and bootloader, considerably
306bc8bc6aSSherry Moore  * reducing downtime.
316bc8bc6aSSherry Moore  *
326bc8bc6aSSherry Moore  * load_kernel(): This function is invoked by mdpreboot() in the reboot
336bc8bc6aSSherry Moore  * path.  It loads the new kernel and boot archive into memory, builds
346bc8bc6aSSherry Moore  * the data structure containing sufficient information about the new
356bc8bc6aSSherry Moore  * kernel and boot archive to be passed to the fast reboot switcher
366bc8bc6aSSherry Moore  * (see fb_swtch_src.s for details).  When invoked the switcher relocates
376bc8bc6aSSherry Moore  * the new kernel and boot archive to physically contiguous low memory,
386bc8bc6aSSherry Moore  * similar to where the boot loader would have loaded them, and jumps to
396bc8bc6aSSherry Moore  * the new kernel.
406bc8bc6aSSherry Moore  *
416bc8bc6aSSherry Moore  * The physical addresses of the memory allocated for the new kernel, boot
426bc8bc6aSSherry Moore  * archive and their page tables must be above where the boot archive ends
436bc8bc6aSSherry Moore  * after it has been relocated by the switcher, otherwise the new files
446bc8bc6aSSherry Moore  * and their page tables could be overridden during relocation.
456bc8bc6aSSherry Moore  *
466bc8bc6aSSherry Moore  * fast_reboot(): This function is invoked by mdboot() once it's determined
476bc8bc6aSSherry Moore  * that the system is capable of fast reboot.  It jumps to the fast reboot
486bc8bc6aSSherry Moore  * switcher with the data structure built by load_kernel() as the argument.
496bc8bc6aSSherry 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 
926bc8bc6aSSherry Moore /*
936bc8bc6aSSherry Moore  * Data structure containing necessary information for the fast reboot
946bc8bc6aSSherry Moore  * switcher to jump to the new kernel.
956bc8bc6aSSherry Moore  */
9619397407SSherry Moore fastboot_info_t newkernel = { 0 };
976bc8bc6aSSherry 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 */
134*877400d3SKonstantin Ananyev #ifdef	__amd64
135*877400d3SKonstantin Ananyev 	0xFFFFFFFFFFFFFFFFULL,	/* dma_attr_addr_hi: 2^64B */
136*877400d3SKonstantin Ananyev #else
13719397407SSherry Moore 	0x0000000FFFFFFFFFULL,	/* dma_attr_addr_hi: 64GB */
138*877400d3SKonstantin Ananyev #endif	/* __amd64 */
13919397407SSherry Moore 	0x00000000FFFFFFFFULL,	/* dma_attr_count_max */
14019397407SSherry Moore 	0x0000000000001000ULL,	/* dma_attr_align: 4KB */
14119397407SSherry Moore 	1,			/* dma_attr_burstsize */
14219397407SSherry Moore 	1,			/* dma_attr_minxfer */
14319397407SSherry Moore 	0x00000000FFFFFFFFULL,	/* dma_attr_maxxfer */
14419397407SSherry Moore 	0x00000000FFFFFFFFULL,	/* dma_attr_seg */
14519397407SSherry Moore 	1,			/* dma_attr_sgllen */
14619397407SSherry Moore 	0x1000ULL,		/* dma_attr_granular */
14719397407SSherry Moore 	0,			/* dma_attr_flags */
14819397407SSherry Moore };
14919397407SSherry Moore 
15019397407SSherry Moore /*
15119397407SSherry Moore  * Various information saved from the previous boot to reconstruct
15219397407SSherry Moore  * multiboot_info.
15319397407SSherry Moore  */
15419397407SSherry Moore extern multiboot_info_t saved_mbi;
15519397407SSherry Moore extern mb_memory_map_t saved_mmap[FASTBOOT_SAVED_MMAP_COUNT];
15619397407SSherry Moore extern struct sol_netinfo saved_drives[FASTBOOT_SAVED_DRIVES_COUNT];
15719397407SSherry Moore extern char saved_cmdline[FASTBOOT_SAVED_CMDLINE_LEN];
15819397407SSherry Moore extern int saved_cmdline_len;
15919397407SSherry Moore 
16019397407SSherry Moore extern void* contig_alloc(size_t size, ddi_dma_attr_t *attr,
16119397407SSherry Moore     uintptr_t align, int cansleep);
1626bc8bc6aSSherry Moore extern void contig_free(void *addr, size_t size);
1636bc8bc6aSSherry Moore 
16419397407SSherry Moore 
16519397407SSherry Moore /* PRINTLIKE */
16619397407SSherry Moore extern void vprintf(const char *, va_list);
16719397407SSherry Moore 
16819397407SSherry Moore 
16919397407SSherry Moore /*
17019397407SSherry Moore  * Need to be able to get boot_archives from other places
17119397407SSherry Moore  */
17219397407SSherry Moore #define	BOOTARCHIVE64	"/platform/i86pc/amd64/boot_archive"
17319397407SSherry Moore #define	BOOTARCHIVE32	"/platform/i86pc/boot_archive"
17419397407SSherry Moore #define	BOOTARCHIVE_FAILSAFE	"/boot/x86.miniroot-safe"
17519397407SSherry Moore #define	FAILSAFE_BOOTFILE	"/boot/platform/i86pc/kernel/unix"
17619397407SSherry Moore 
17719397407SSherry Moore static uint_t fastboot_vatoindex(fastboot_info_t *, uintptr_t, int);
17819397407SSherry Moore static void fastboot_map_with_size(fastboot_info_t *, uintptr_t,
17919397407SSherry Moore     paddr_t, size_t, int);
18019397407SSherry Moore static void fastboot_build_pagetables(fastboot_info_t *);
18119397407SSherry Moore static int fastboot_build_mbi(char *, fastboot_info_t *);
18219397407SSherry Moore 
18319397407SSherry Moore static const char fastboot_enomem_msg[] = "Fastboot: Couldn't allocate 0x%"
18419397407SSherry Moore 	PRIx64" bytes below %s to do fast reboot";
18519397407SSherry Moore 
18619397407SSherry Moore static void
18719397407SSherry Moore dprintf(char *fmt, ...)
18819397407SSherry Moore {
18919397407SSherry Moore 	va_list adx;
19019397407SSherry Moore 
19119397407SSherry Moore 	if (!fastboot_debug)
19219397407SSherry Moore 		return;
19319397407SSherry Moore 
19419397407SSherry Moore 	va_start(adx, fmt);
19519397407SSherry Moore 	vprintf(fmt, adx);
19619397407SSherry Moore 	va_end(adx);
19719397407SSherry Moore }
19819397407SSherry Moore 
19919397407SSherry Moore 
20019397407SSherry Moore /*
20119397407SSherry Moore  * Return the index corresponding to a virt address at a given page table level.
20219397407SSherry Moore  */
20319397407SSherry Moore static uint_t
20419397407SSherry Moore fastboot_vatoindex(fastboot_info_t *nk, uintptr_t va, int level)
20519397407SSherry Moore {
20619397407SSherry Moore 	return ((va >> nk->fi_shift_amt[level]) & (nk->fi_ptes_per_table - 1));
20719397407SSherry Moore }
20819397407SSherry Moore 
20919397407SSherry Moore 
21019397407SSherry Moore /*
21119397407SSherry Moore  * Add mapping from vstart to pstart for the specified size.
212*877400d3SKonstantin Ananyev  * vstart, pstart and size should all have been aligned at 2M boundaries.
21319397407SSherry Moore  */
21419397407SSherry Moore static void
21519397407SSherry Moore fastboot_map_with_size(fastboot_info_t *nk, uintptr_t vstart, paddr_t pstart,
21619397407SSherry Moore     size_t size, int level)
21719397407SSherry Moore {
21819397407SSherry Moore 	x86pte_t	pteval, *table;
21919397407SSherry Moore 	uintptr_t	vaddr;
22019397407SSherry Moore 	paddr_t		paddr;
22119397407SSherry Moore 	int		index, l;
22219397407SSherry Moore 
22319397407SSherry Moore 	table = (x86pte_t *)(nk->fi_pagetable_va);
22419397407SSherry Moore 
22519397407SSherry Moore 	for (l = nk->fi_top_level; l >= level; l--) {
22619397407SSherry Moore 
22719397407SSherry Moore 		index = fastboot_vatoindex(nk, vstart, l);
22819397407SSherry Moore 
22919397407SSherry Moore 		if (l == level) {
23019397407SSherry Moore 			/*
23119397407SSherry Moore 			 * Last level.  Program the page table entries.
23219397407SSherry Moore 			 */
23319397407SSherry Moore 			for (vaddr = vstart, paddr = pstart;
23419397407SSherry Moore 			    vaddr < vstart + size;
23519397407SSherry Moore 			    vaddr += (1ULL << nk->fi_shift_amt[l]),
23619397407SSherry Moore 			    paddr += (1ULL << nk->fi_shift_amt[l])) {
23719397407SSherry Moore 
23819397407SSherry Moore 				uint_t index = fastboot_vatoindex(nk, vaddr, l);
23919397407SSherry Moore 
24019397407SSherry Moore 				if (l > 0)
24119397407SSherry Moore 					pteval = paddr | pte_bits | PT_PAGESIZE;
24219397407SSherry Moore 				else
24319397407SSherry Moore 					pteval = paddr | pte_bits;
24419397407SSherry Moore 
24519397407SSherry Moore 				table[index] = pteval;
24619397407SSherry Moore 			}
24719397407SSherry Moore 		} else if (table[index] & PT_VALID) {
24819397407SSherry Moore 
24919397407SSherry Moore 			table = (x86pte_t *)
25019397407SSherry Moore 			    ((uintptr_t)(((paddr_t)table[index] & MMU_PAGEMASK)
25119397407SSherry Moore 			    - nk->fi_pagetable_pa) + nk->fi_pagetable_va);
25219397407SSherry Moore 		} else {
25319397407SSherry Moore 			/*
254*877400d3SKonstantin Ananyev 			 * Intermediate levels.
255*877400d3SKonstantin Ananyev 			 * Program with either valid bit or PTP bits.
25619397407SSherry Moore 			 */
25719397407SSherry Moore 			if (l == nk->fi_top_level) {
258*877400d3SKonstantin Ananyev #ifdef	__amd64
259*877400d3SKonstantin Ananyev 				ASSERT(nk->fi_top_level == 3);
260*877400d3SKonstantin Ananyev 				table[index] = nk->fi_next_table_pa | ptp_bits;
261*877400d3SKonstantin Ananyev #else
26219397407SSherry Moore 				table[index] = nk->fi_next_table_pa | PT_VALID;
263*877400d3SKonstantin Ananyev #endif	/* __amd64 */
26419397407SSherry Moore 			} else {
26519397407SSherry Moore 				table[index] = nk->fi_next_table_pa | ptp_bits;
26619397407SSherry Moore 			}
26719397407SSherry Moore 			table = (x86pte_t *)(nk->fi_next_table_va);
26819397407SSherry Moore 			nk->fi_next_table_va += MMU_PAGESIZE;
26919397407SSherry Moore 			nk->fi_next_table_pa += MMU_PAGESIZE;
27019397407SSherry Moore 		}
27119397407SSherry Moore 	}
27219397407SSherry Moore }
27319397407SSherry Moore 
27419397407SSherry Moore /*
27519397407SSherry Moore  * Build page tables for the lower 1G of physical memory using 2M
27619397407SSherry Moore  * pages, and prepare page tables for mapping new kernel and boot
27719397407SSherry Moore  * archive pages using 4K pages.
27819397407SSherry Moore  */
27919397407SSherry Moore static void
28019397407SSherry Moore fastboot_build_pagetables(fastboot_info_t *nk)
28119397407SSherry Moore {
28219397407SSherry Moore 	/*
28319397407SSherry Moore 	 * Map lower 1G physical memory.  Use large pages.
28419397407SSherry Moore 	 */
28519397407SSherry Moore 	fastboot_map_with_size(nk, 0, 0, ONE_GIG, 1);
28619397407SSherry Moore 
28719397407SSherry Moore 	/*
28819397407SSherry Moore 	 * Map one 4K page to get the middle page tables set up.
28919397407SSherry Moore 	 */
29019397407SSherry Moore 	fake_va = P2ALIGN_TYPED(fake_va, nk->fi_lpagesize, uintptr_t);
29119397407SSherry Moore 	fastboot_map_with_size(nk, fake_va,
29219397407SSherry Moore 	    nk->fi_files[0].fb_pte_list_va[0] & MMU_PAGEMASK, PAGESIZE, 0);
29319397407SSherry Moore }
29419397407SSherry Moore 
29519397407SSherry Moore 
29619397407SSherry Moore /*
29719397407SSherry Moore  * Sanity check.  Look for dboot offset.
29819397407SSherry Moore  */
29919397407SSherry Moore static int
30019397407SSherry Moore fastboot_elf64_find_dboot_load_offset(void *img, off_t imgsz, uint32_t *offp)
30119397407SSherry Moore {
30219397407SSherry Moore 	Elf64_Ehdr	*ehdr = (Elf64_Ehdr *)img;
30319397407SSherry Moore 	Elf64_Phdr	*phdr;
30419397407SSherry Moore 	uint8_t		*phdrbase;
30519397407SSherry Moore 	int		i;
30619397407SSherry Moore 
30719397407SSherry Moore 	if ((ehdr->e_phoff + ehdr->e_phnum * ehdr->e_phentsize) >= imgsz)
30819397407SSherry Moore 		return (-1);
30919397407SSherry Moore 
31019397407SSherry Moore 	phdrbase = (uint8_t *)img + ehdr->e_phoff;
31119397407SSherry Moore 
31219397407SSherry Moore 	for (i = 0; i < ehdr->e_phnum; i++) {
31319397407SSherry Moore 		phdr = (Elf64_Phdr *)(phdrbase + ehdr->e_phentsize * i);
31419397407SSherry Moore 
31519397407SSherry Moore 		if (phdr->p_type == PT_LOAD) {
31619397407SSherry Moore 			if (phdr->p_vaddr == phdr->p_paddr &&
31719397407SSherry Moore 			    phdr->p_vaddr == DBOOT_ENTRY_ADDRESS) {
31819397407SSherry Moore 				ASSERT(phdr->p_offset <= UINT32_MAX);
31919397407SSherry Moore 				*offp = (uint32_t)phdr->p_offset;
32019397407SSherry Moore 				return (0);
32119397407SSherry Moore 			}
32219397407SSherry Moore 		}
32319397407SSherry Moore 	}
32419397407SSherry Moore 
32519397407SSherry Moore 	return (-1);
32619397407SSherry Moore }
32719397407SSherry Moore 
32819397407SSherry Moore 
32919397407SSherry Moore /*
33019397407SSherry Moore  * Initialize text and data section information for 32-bit kernel.
331*877400d3SKonstantin Ananyev  * sectcntp - is both input/output parameter.
332*877400d3SKonstantin Ananyev  * On entry, *sectcntp contains maximum allowable number of sections;
333*877400d3SKonstantin Ananyev  * on return, it contains the actual number of sections filled.
33419397407SSherry Moore  */
33519397407SSherry Moore static int
33619397407SSherry Moore fastboot_elf32_find_loadables(void *img, off_t imgsz, fastboot_section_t *sectp,
33719397407SSherry Moore     int *sectcntp, uint32_t *offp)
33819397407SSherry Moore {
33919397407SSherry Moore 	Elf32_Ehdr	*ehdr = (Elf32_Ehdr *)img;
34019397407SSherry Moore 	Elf32_Phdr	*phdr;
34119397407SSherry Moore 	uint8_t		*phdrbase;
34219397407SSherry Moore 	int		i;
34319397407SSherry Moore 	int		used_sections = 0;
344*877400d3SKonstantin Ananyev 	const int	max_sectcnt = *sectcntp;
34519397407SSherry Moore 
34619397407SSherry Moore 	if ((ehdr->e_phoff + ehdr->e_phnum * ehdr->e_phentsize) >= imgsz)
34719397407SSherry Moore 		return (-1);
34819397407SSherry Moore 
34919397407SSherry Moore 	phdrbase = (uint8_t *)img + ehdr->e_phoff;
35019397407SSherry Moore 
35119397407SSherry Moore 	for (i = 0; i < ehdr->e_phnum; i++) {
35219397407SSherry Moore 		phdr = (Elf32_Phdr *)(phdrbase + ehdr->e_phentsize * i);
35319397407SSherry Moore 
35419397407SSherry Moore 		if (phdr->p_type == PT_INTERP)
35519397407SSherry Moore 			return (-1);
35619397407SSherry Moore 
35719397407SSherry Moore 		if (phdr->p_type != PT_LOAD)
35819397407SSherry Moore 			continue;
35919397407SSherry Moore 
36019397407SSherry Moore 		if (phdr->p_vaddr == phdr->p_paddr &&
36119397407SSherry Moore 		    phdr->p_paddr == DBOOT_ENTRY_ADDRESS) {
36219397407SSherry Moore 			*offp = (uint32_t)phdr->p_offset;
36319397407SSherry Moore 		} else {
364*877400d3SKonstantin Ananyev 			if (max_sectcnt <= used_sections)
365*877400d3SKonstantin Ananyev 				return (-1);
366*877400d3SKonstantin Ananyev 
36719397407SSherry Moore 			sectp[used_sections].fb_sec_offset = phdr->p_offset;
36819397407SSherry Moore 			sectp[used_sections].fb_sec_paddr = phdr->p_paddr;
36919397407SSherry Moore 			sectp[used_sections].fb_sec_size = phdr->p_filesz;
37019397407SSherry Moore 			sectp[used_sections].fb_sec_bss_size =
37119397407SSherry Moore 			    (phdr->p_filesz < phdr->p_memsz) ?
37219397407SSherry Moore 			    (phdr->p_memsz - phdr->p_filesz) : 0;
37319397407SSherry Moore 
374*877400d3SKonstantin Ananyev 			/* Extra sanity check for the input object file */
375*877400d3SKonstantin Ananyev 			if (sectp[used_sections].fb_sec_paddr +
376*877400d3SKonstantin Ananyev 			    sectp[used_sections].fb_sec_size +
377*877400d3SKonstantin Ananyev 			    sectp[used_sections].fb_sec_bss_size >=
378*877400d3SKonstantin Ananyev 			    DBOOT_ENTRY_ADDRESS)
379*877400d3SKonstantin Ananyev 				return (-1);
380*877400d3SKonstantin Ananyev 
38119397407SSherry Moore 			used_sections++;
38219397407SSherry Moore 		}
38319397407SSherry Moore 	}
38419397407SSherry Moore 
38519397407SSherry Moore 	*sectcntp = used_sections;
38619397407SSherry Moore 	return (0);
38719397407SSherry Moore }
38819397407SSherry Moore 
38919397407SSherry Moore /*
39019397407SSherry Moore  * Create multiboot info structure
39119397407SSherry Moore  */
39219397407SSherry Moore static int
39319397407SSherry Moore fastboot_build_mbi(char *mdep, fastboot_info_t *nk)
39419397407SSherry Moore {
39519397407SSherry Moore 	mb_module_t	*mbp;
39619397407SSherry Moore 	uintptr_t	next_addr;
39719397407SSherry Moore 	uintptr_t	new_mbi_pa;
39819397407SSherry Moore 	size_t		size;
39919397407SSherry Moore 	void		*buf = NULL;
40019397407SSherry Moore 	size_t		arglen;
40119397407SSherry Moore 	char		bootargs[OBP_MAXPATHLEN];
40219397407SSherry Moore 
40319397407SSherry Moore 	bzero(bootargs, OBP_MAXPATHLEN);
40419397407SSherry Moore 
4056bc8bc6aSSherry Moore 	if (mdep != NULL && strlen(mdep) != 0) {
40619397407SSherry Moore 		arglen = strlen(mdep) + 1;
40719397407SSherry Moore 	} else {
40819397407SSherry Moore 		arglen = saved_cmdline_len;
40919397407SSherry Moore 	}
41019397407SSherry Moore 
41119397407SSherry Moore 	size = PAGESIZE + P2ROUNDUP(arglen, PAGESIZE);
41219397407SSherry Moore 	buf = contig_alloc(size, &fastboot_below_1G_dma_attr, PAGESIZE, 0);
41319397407SSherry Moore 	if (buf == NULL) {
41419397407SSherry Moore 		cmn_err(CE_WARN, fastboot_enomem_msg, (uint64_t)size, "1G");
41519397407SSherry Moore 		return (-1);
41619397407SSherry Moore 	}
41719397407SSherry Moore 
41819397407SSherry Moore 	bzero(buf, size);
41919397407SSherry Moore 
42019397407SSherry Moore 	new_mbi_pa = mmu_ptob((uint64_t)hat_getpfnum(kas.a_hat, (caddr_t)buf));
42119397407SSherry Moore 
42219397407SSherry Moore 	hat_devload(kas.a_hat, (caddr_t)new_mbi_pa, size,
42319397407SSherry Moore 	    mmu_btop(new_mbi_pa), PROT_READ | PROT_WRITE, HAT_LOAD_NOCONSIST);
42419397407SSherry Moore 
42519397407SSherry Moore 	nk->fi_new_mbi_pa = (paddr_t)new_mbi_pa;
42619397407SSherry Moore 
42719397407SSherry Moore 	bcopy(&saved_mbi, (void *)new_mbi_pa, sizeof (multiboot_info_t));
42819397407SSherry Moore 
42919397407SSherry Moore 	next_addr = new_mbi_pa + sizeof (multiboot_info_t);
43019397407SSherry Moore 	((multiboot_info_t *)new_mbi_pa)->mods_addr = next_addr;
43119397407SSherry Moore 	mbp = (mb_module_t *)(uintptr_t)next_addr;
43219397407SSherry Moore 	mbp->mod_start = newkernel.fi_files[FASTBOOT_BOOTARCHIVE].fb_dest_pa;
43319397407SSherry Moore 	mbp->mod_end = newkernel.fi_files[FASTBOOT_BOOTARCHIVE].fb_next_pa;
43419397407SSherry Moore 
43519397407SSherry Moore 	next_addr += sizeof (mb_module_t);
43619397407SSherry Moore 	bcopy(fastboot_filename[FASTBOOT_NAME_BOOTARCHIVE], (void *)next_addr,
43719397407SSherry Moore 	    strlen(fastboot_filename[FASTBOOT_NAME_BOOTARCHIVE]));
43819397407SSherry Moore 
43919397407SSherry Moore 	mbp->mod_name = next_addr;
44019397407SSherry Moore 	mbp->reserved = 0;
44119397407SSherry Moore 	next_addr += strlen(fastboot_filename[FASTBOOT_NAME_BOOTARCHIVE]);
44219397407SSherry Moore 	*(char *)next_addr = '\0';
44319397407SSherry Moore 	next_addr++;
44419397407SSherry Moore 	next_addr = P2ROUNDUP_TYPED(next_addr, 16, uintptr_t);
44519397407SSherry Moore 
44619397407SSherry Moore 	((multiboot_info_t *)new_mbi_pa)->mmap_addr = next_addr;
44719397407SSherry Moore 	bcopy((void *)(uintptr_t)saved_mmap, (void *)next_addr,
44819397407SSherry Moore 	    saved_mbi.mmap_length);
44919397407SSherry Moore 	next_addr += saved_mbi.mmap_length;
45019397407SSherry Moore 
45119397407SSherry Moore 	((multiboot_info_t *)new_mbi_pa)->drives_addr = next_addr;
45219397407SSherry Moore 	bcopy((void *)(uintptr_t)saved_drives, (void *)next_addr,
45319397407SSherry Moore 	    saved_mbi.drives_length);
45419397407SSherry Moore 	next_addr += saved_mbi.drives_length;
45519397407SSherry Moore 
45619397407SSherry Moore 	((multiboot_info_t *)new_mbi_pa)->cmdline = next_addr;
45719397407SSherry Moore 
4586bc8bc6aSSherry Moore 	if (mdep != NULL && strlen(mdep) != 0) {
45919397407SSherry Moore 		bcopy(mdep, (void *)(uintptr_t)
46019397407SSherry Moore 		    (((multiboot_info_t *)new_mbi_pa)->cmdline), (arglen - 1));
46119397407SSherry Moore 	} else {
46219397407SSherry Moore 		bcopy((void *)saved_cmdline, (void *)next_addr, (arglen - 1));
46319397407SSherry Moore 	}
46419397407SSherry Moore 	/* Terminate the string */
46519397407SSherry Moore 	((char *)(intptr_t)next_addr)[arglen - 1] = '\0';
46619397407SSherry Moore 
46719397407SSherry Moore 	return (0);
46819397407SSherry Moore }
46919397407SSherry Moore 
4706bc8bc6aSSherry Moore /*
4716bc8bc6aSSherry Moore  * Initialize HAT related fields
4726bc8bc6aSSherry Moore  */
4736bc8bc6aSSherry Moore static void
4746bc8bc6aSSherry Moore fastboot_init_fields(fastboot_info_t *nk)
47519397407SSherry Moore {
47619397407SSherry Moore 	if (x86_feature & X86_PAE) {
4776bc8bc6aSSherry Moore 		nk->fi_has_pae = 1;
4786bc8bc6aSSherry Moore 		nk->fi_shift_amt = fastboot_shift_amt_pae;
4796bc8bc6aSSherry Moore 		nk->fi_ptes_per_table = 512;
4806bc8bc6aSSherry Moore 		nk->fi_lpagesize = (2 << 20);	/* 2M */
481*877400d3SKonstantin Ananyev #ifdef	__amd64
482*877400d3SKonstantin Ananyev 		nk->fi_top_level = 3;
483*877400d3SKonstantin Ananyev #else
4846bc8bc6aSSherry Moore 		nk->fi_top_level = 2;
485*877400d3SKonstantin Ananyev #endif	/* __amd64 */
4866bc8bc6aSSherry Moore 	}
48719397407SSherry Moore }
48819397407SSherry Moore 
4896bc8bc6aSSherry Moore /*
4906bc8bc6aSSherry Moore  * Process boot argument
4916bc8bc6aSSherry Moore  */
4926bc8bc6aSSherry Moore static void
4936bc8bc6aSSherry Moore fastboot_parse_mdep(char *mdep, char *kern_bootpath, int *bootpath_len,
4946bc8bc6aSSherry Moore     char *bootargs)
4956bc8bc6aSSherry Moore {
4966bc8bc6aSSherry Moore 	int	i;
49719397407SSherry Moore 
49819397407SSherry Moore 	/*
49919397407SSherry Moore 	 * If mdep is not NULL, it comes in the format of
50019397407SSherry Moore 	 *	mountpoint unix args
50119397407SSherry Moore 	 */
5026bc8bc6aSSherry Moore 	if (mdep != NULL && strlen(mdep) != 0) {
50319397407SSherry Moore 		if (mdep[0] != '-') {
50419397407SSherry Moore 			/* First get the root argument */
50519397407SSherry Moore 			i = 0;
50619397407SSherry Moore 			while (mdep[i] != '\0' && mdep[i] != ' ') {
50719397407SSherry Moore 				i++;
50819397407SSherry Moore 			}
50919397407SSherry Moore 
51019397407SSherry Moore 			if (i < 4 || strncmp(&mdep[i-4], "unix", 4) != 0) {
51119397407SSherry Moore 				/* mount point */
51219397407SSherry Moore 				bcopy(mdep, kern_bootpath, i);
51319397407SSherry Moore 				kern_bootpath[i] = '\0';
5146bc8bc6aSSherry Moore 				*bootpath_len = i;
51519397407SSherry Moore 
51619397407SSherry Moore 				/*
51719397407SSherry Moore 				 * Get the next argument. It should be unix as
51819397407SSherry Moore 				 * we have validated in in halt.c.
51919397407SSherry Moore 				 */
52019397407SSherry Moore 				if (strlen(mdep) > i) {
52119397407SSherry Moore 					mdep += (i + 1);
52219397407SSherry Moore 					i = 0;
52319397407SSherry Moore 					while (mdep[i] != '\0' &&
52419397407SSherry Moore 					    mdep[i] != ' ') {
52519397407SSherry Moore 						i++;
52619397407SSherry Moore 					}
52719397407SSherry Moore 				}
52819397407SSherry Moore 
52919397407SSherry Moore 			}
53019397407SSherry Moore 			bcopy(mdep, kern_bootfile, i);
53119397407SSherry Moore 			kern_bootfile[i] = '\0';
5326bc8bc6aSSherry Moore 			bcopy(mdep, bootargs, strlen(mdep));
53319397407SSherry Moore 		} else {
53419397407SSherry Moore 			int off = strlen(kern_bootfile);
53519397407SSherry Moore 			bcopy(kern_bootfile, bootargs, off);
53619397407SSherry Moore 			bcopy(" ", &bootargs[off++], 1);
53719397407SSherry Moore 			bcopy(mdep, &bootargs[off], strlen(mdep));
53819397407SSherry Moore 			off += strlen(mdep);
53919397407SSherry Moore 			bootargs[off] = '\0';
54019397407SSherry Moore 		}
54119397407SSherry Moore 	}
5426bc8bc6aSSherry Moore }
5436bc8bc6aSSherry Moore 
5446bc8bc6aSSherry Moore /*
5456bc8bc6aSSherry Moore  * Free up the memory we have allocated for this file
5466bc8bc6aSSherry Moore  */
5476bc8bc6aSSherry Moore static void
5486bc8bc6aSSherry Moore fastboot_free_file(fastboot_file_t *fb)
5496bc8bc6aSSherry Moore {
5506bc8bc6aSSherry Moore 	size_t	fsize_roundup, pt_size;
5516bc8bc6aSSherry Moore 	int	pt_entry_count;
5526bc8bc6aSSherry Moore 
5536bc8bc6aSSherry Moore 	fsize_roundup = P2ROUNDUP_TYPED(fb->fb_size, PAGESIZE, size_t);
5546bc8bc6aSSherry Moore 	contig_free((void *)fb->fb_va, fsize_roundup);
5556bc8bc6aSSherry Moore 
5566bc8bc6aSSherry Moore 	pt_entry_count = (fsize_roundup >> PAGESHIFT) + 1;
5576bc8bc6aSSherry Moore 	pt_size = P2ROUNDUP(pt_entry_count * 8, PAGESIZE);
5586bc8bc6aSSherry Moore 	contig_free((void *)fb->fb_pte_list_va, pt_size);
5596bc8bc6aSSherry Moore }
5606bc8bc6aSSherry Moore 
5616bc8bc6aSSherry Moore /*
5626bc8bc6aSSherry Moore  * This function performs the following tasks:
5636bc8bc6aSSherry Moore  * - Read the sizes of the new kernel and boot archive.
5646bc8bc6aSSherry Moore  * - Allocate memory for the new kernel and boot archive.
5656bc8bc6aSSherry Moore  * - Allocate memory for page tables necessary for mapping the memory
5666bc8bc6aSSherry Moore  *   allocated for the files.
5676bc8bc6aSSherry Moore  * - Read the new kernel and boot archive into memory.
5686bc8bc6aSSherry Moore  * - Map in the fast reboot switcher.
5696bc8bc6aSSherry Moore  * - Load the fast reboot switcher to FASTBOOT_SWTCH_PA.
5706bc8bc6aSSherry Moore  * - Build the new multiboot_info structure
5716bc8bc6aSSherry Moore  * - Build page tables for the low 1G of physical memory.
5726bc8bc6aSSherry Moore  * - Mark the data structure as valid if all steps have succeeded.
5736bc8bc6aSSherry Moore  */
5746bc8bc6aSSherry Moore void
5756bc8bc6aSSherry Moore load_kernel(char *mdep)
5766bc8bc6aSSherry Moore {
5776bc8bc6aSSherry Moore 	void		*buf = NULL;
5786bc8bc6aSSherry Moore 	int		i;
5796bc8bc6aSSherry Moore 	fastboot_file_t	*fb;
5806bc8bc6aSSherry Moore 	uint32_t	dboot_start_offset;
5816bc8bc6aSSherry Moore 	char		kern_bootpath[OBP_MAXPATHLEN];
5826bc8bc6aSSherry Moore 	char		bootargs[OBP_MAXPATHLEN];
5836bc8bc6aSSherry Moore 	extern uintptr_t postbootkernelbase;
5846bc8bc6aSSherry Moore 	extern char	fb_swtch_image[];
5856bc8bc6aSSherry Moore 	int		bootpath_len = 0;
5866bc8bc6aSSherry Moore 	int		is_failsafe = 0;
5876bc8bc6aSSherry Moore 	int		is_retry = 0;
5886bc8bc6aSSherry Moore 	uint64_t	end_addr;
5896bc8bc6aSSherry Moore 
5906bc8bc6aSSherry Moore 	ASSERT(fastreboot_capable);
5916bc8bc6aSSherry Moore 
5926bc8bc6aSSherry Moore 	postbootkernelbase = 0;
5936bc8bc6aSSherry Moore 
5946bc8bc6aSSherry Moore 	/*
5956bc8bc6aSSherry Moore 	 * Initialize various HAT related fields in the data structure
5966bc8bc6aSSherry Moore 	 */
5976bc8bc6aSSherry Moore 	fastboot_init_fields(&newkernel);
5986bc8bc6aSSherry Moore 
5996bc8bc6aSSherry Moore 	bzero(kern_bootpath, OBP_MAXPATHLEN);
6006bc8bc6aSSherry Moore 
6016bc8bc6aSSherry Moore 	/*
6026bc8bc6aSSherry Moore 	 * Process the boot argument
6036bc8bc6aSSherry Moore 	 */
6046bc8bc6aSSherry Moore 	bzero(bootargs, OBP_MAXPATHLEN);
6056bc8bc6aSSherry Moore 	fastboot_parse_mdep(mdep, kern_bootpath, &bootpath_len, bootargs);
60619397407SSherry Moore 
60719397407SSherry Moore 	/*
60819397407SSherry Moore 	 * Make sure we get the null character
60919397407SSherry Moore 	 */
61019397407SSherry Moore 	bcopy(kern_bootpath, fastboot_filename[FASTBOOT_NAME_UNIX],
61119397407SSherry Moore 	    bootpath_len);
61219397407SSherry Moore 	bcopy(kern_bootfile,
61319397407SSherry Moore 	    &fastboot_filename[FASTBOOT_NAME_UNIX][bootpath_len],
61419397407SSherry Moore 	    strlen(kern_bootfile) + 1);
61519397407SSherry Moore 
61619397407SSherry Moore 	bcopy(kern_bootpath, fastboot_filename[FASTBOOT_NAME_BOOTARCHIVE],
61719397407SSherry Moore 	    bootpath_len);
61819397407SSherry Moore 
61919397407SSherry Moore 	if (bcmp(kern_bootfile, FAILSAFE_BOOTFILE,
62019397407SSherry Moore 	    (sizeof (FAILSAFE_BOOTFILE) - 1)) == 0) {
62119397407SSherry Moore 		is_failsafe = 1;
62219397407SSherry Moore 	}
62319397407SSherry Moore 
6246bc8bc6aSSherry Moore load_kernel_retry:
62519397407SSherry Moore 	/*
62619397407SSherry Moore 	 * Read in unix and boot_archive
62719397407SSherry Moore 	 */
6286bc8bc6aSSherry Moore 	end_addr = DBOOT_ENTRY_ADDRESS;
62919397407SSherry Moore 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
6306bc8bc6aSSherry Moore 		struct _buf	*file;
6316bc8bc6aSSherry Moore 		uintptr_t	va;
63219397407SSherry Moore 		uint64_t	fsize;
63319397407SSherry Moore 		size_t		fsize_roundup, pt_size;
63419397407SSherry Moore 		int		page_index;
63519397407SSherry Moore 		uintptr_t	offset;
63619397407SSherry Moore 		int		pt_entry_count;
63719397407SSherry Moore 		ddi_dma_attr_t dma_attr = fastboot_dma_attr;
63819397407SSherry Moore 
6396bc8bc6aSSherry Moore 
64019397407SSherry Moore 		dprintf("fastboot_filename[%d] = %s\n",
64119397407SSherry Moore 		    i, fastboot_filename[i]);
64219397407SSherry Moore 
64319397407SSherry Moore 		if ((file = kobj_open_file(fastboot_filename[i])) ==
64419397407SSherry Moore 		    (struct _buf *)-1) {
64519397407SSherry Moore 			cmn_err(CE_WARN, "Fastboot: Couldn't open %s",
64619397407SSherry Moore 			    fastboot_filename[i]);
64719397407SSherry Moore 			goto err_out;
64819397407SSherry Moore 		}
64919397407SSherry Moore 
65019397407SSherry Moore 		if (kobj_get_filesize(file, &fsize) != 0) {
65119397407SSherry Moore 			cmn_err(CE_WARN,
65219397407SSherry Moore 			    "Fastboot: Couldn't get filesize for %s",
65319397407SSherry Moore 			    fastboot_filename[i]);
65419397407SSherry Moore 			goto err_out;
65519397407SSherry Moore 		}
65619397407SSherry Moore 
6576bc8bc6aSSherry Moore 		fsize_roundup = P2ROUNDUP_TYPED(fsize, PAGESIZE, size_t);
6586bc8bc6aSSherry Moore 
6596bc8bc6aSSherry Moore 		/*
6606bc8bc6aSSherry Moore 		 * Where the files end in physical memory after being
6616bc8bc6aSSherry Moore 		 * relocated by the fast boot switcher.
6626bc8bc6aSSherry Moore 		 */
6636bc8bc6aSSherry Moore 		end_addr += fsize_roundup;
6646bc8bc6aSSherry Moore 		if (end_addr > fastboot_below_1G_dma_attr.dma_attr_addr_hi) {
6656bc8bc6aSSherry Moore 			cmn_err(CE_WARN, "Fastboot: boot archive is too big");
6666bc8bc6aSSherry Moore 			goto err_out;
66719397407SSherry Moore 		}
66819397407SSherry Moore 
6696bc8bc6aSSherry Moore 		/*
6706bc8bc6aSSherry Moore 		 * Adjust dma_attr_addr_lo so that the new kernel and boot
6716bc8bc6aSSherry Moore 		 * archive will not be overridden during relocation.
6726bc8bc6aSSherry Moore 		 */
6736bc8bc6aSSherry Moore 		if (end_addr > fastboot_dma_attr.dma_attr_addr_lo ||
6746bc8bc6aSSherry Moore 		    end_addr > fastboot_below_1G_dma_attr.dma_attr_addr_lo) {
6756bc8bc6aSSherry Moore 
6766bc8bc6aSSherry Moore 			if (is_retry) {
6776bc8bc6aSSherry Moore 				/*
6786bc8bc6aSSherry Moore 				 * If we have already tried and didn't succeed,
6796bc8bc6aSSherry Moore 				 * just give up.
6806bc8bc6aSSherry Moore 				 */
6816bc8bc6aSSherry Moore 				cmn_err(CE_WARN,
6826bc8bc6aSSherry Moore 				    "Fastboot: boot archive is too big");
6836bc8bc6aSSherry Moore 				goto err_out;
6846bc8bc6aSSherry Moore 			} else {
6856bc8bc6aSSherry Moore 				int j;
6866bc8bc6aSSherry Moore 
6876bc8bc6aSSherry Moore 				/* Set the flag so we don't keep retrying */
6886bc8bc6aSSherry Moore 				is_retry++;
6896bc8bc6aSSherry Moore 
6906bc8bc6aSSherry Moore 				/* Adjust dma_attr_addr_lo */
6916bc8bc6aSSherry Moore 				fastboot_dma_attr.dma_attr_addr_lo = end_addr;
6926bc8bc6aSSherry Moore 				fastboot_below_1G_dma_attr.dma_attr_addr_lo =
6936bc8bc6aSSherry Moore 				    end_addr;
6946bc8bc6aSSherry Moore 
6956bc8bc6aSSherry Moore 				/*
6966bc8bc6aSSherry Moore 				 * Free the memory we have already allocated
6976bc8bc6aSSherry Moore 				 * whose physical addresses might not fit
6986bc8bc6aSSherry Moore 				 * the new lo and hi constraints.
6996bc8bc6aSSherry Moore 				 */
7006bc8bc6aSSherry Moore 				for (j = 0; j < i; j++)
7016bc8bc6aSSherry Moore 					fastboot_free_file(
7026bc8bc6aSSherry Moore 					    &newkernel.fi_files[j]);
7036bc8bc6aSSherry Moore 				goto load_kernel_retry;
7046bc8bc6aSSherry Moore 			}
7056bc8bc6aSSherry Moore 		}
7066bc8bc6aSSherry Moore 
7076bc8bc6aSSherry Moore 
70819397407SSherry Moore 		if (!fastboot_contig)
70919397407SSherry Moore 			dma_attr.dma_attr_sgllen = (fsize / PAGESIZE) +
71019397407SSherry Moore 			    (((fsize % PAGESIZE) == 0) ? 0 : 1);
71119397407SSherry Moore 
71219397407SSherry Moore 		if ((buf = contig_alloc(fsize, &dma_attr, PAGESIZE, 0))
71319397407SSherry Moore 		    == NULL) {
7146bc8bc6aSSherry Moore 			cmn_err(CE_WARN, fastboot_enomem_msg, fsize, "64G");
71519397407SSherry Moore 			goto err_out;
71619397407SSherry Moore 		}
71719397407SSherry Moore 
71819397407SSherry Moore 		va = P2ROUNDUP_TYPED((uintptr_t)buf, PAGESIZE, uintptr_t);
71919397407SSherry Moore 
72019397407SSherry Moore 		if (kobj_read_file(file, (char *)va, fsize, 0) < 0) {
72119397407SSherry Moore 			cmn_err(CE_WARN, "Fastboot: Couldn't read %s",
72219397407SSherry Moore 			    fastboot_filename[i]);
72319397407SSherry Moore 			goto err_out;
72419397407SSherry Moore 		}
72519397407SSherry Moore 
72619397407SSherry Moore 		fb = &newkernel.fi_files[i];
72719397407SSherry Moore 		fb->fb_va = va;
72819397407SSherry Moore 		fb->fb_size = fsize;
72919397407SSherry Moore 		fb->fb_sectcnt = 0;
73019397407SSherry Moore 
73119397407SSherry Moore 		/*
73219397407SSherry Moore 		 * Allocate one extra page table entry for terminating
73319397407SSherry Moore 		 * the list.
73419397407SSherry Moore 		 */
73519397407SSherry Moore 		pt_entry_count = (fsize_roundup >> PAGESHIFT) + 1;
73619397407SSherry Moore 		pt_size = P2ROUNDUP(pt_entry_count * 8, PAGESIZE);
73719397407SSherry Moore 
73819397407SSherry Moore 		if ((fb->fb_pte_list_va =
73919397407SSherry Moore 		    (x86pte_t *)contig_alloc(pt_size,
74019397407SSherry Moore 		    &fastboot_below_1G_dma_attr, PAGESIZE, 0)) == NULL) {
74119397407SSherry Moore 			cmn_err(CE_WARN, fastboot_enomem_msg,
74219397407SSherry Moore 			    (uint64_t)pt_size, "1G");
74319397407SSherry Moore 			goto err_out;
74419397407SSherry Moore 		}
74519397407SSherry Moore 
74619397407SSherry Moore 		bzero((void *)(fb->fb_pte_list_va), pt_size);
74719397407SSherry Moore 
74819397407SSherry Moore 		fb->fb_pte_list_pa = mmu_ptob((uint64_t)hat_getpfnum(kas.a_hat,
74919397407SSherry Moore 		    (caddr_t)fb->fb_pte_list_va));
75019397407SSherry Moore 
75119397407SSherry Moore 		for (page_index = 0, offset = 0; offset < fb->fb_size;
75219397407SSherry Moore 		    offset += PAGESIZE) {
75319397407SSherry Moore 			uint64_t paddr;
75419397407SSherry Moore 
75519397407SSherry Moore 			paddr = mmu_ptob((uint64_t)hat_getpfnum(kas.a_hat,
75619397407SSherry Moore 			    (caddr_t)fb->fb_va + offset));
75719397407SSherry Moore 
75819397407SSherry Moore 			ASSERT(paddr >= fastboot_dma_attr.dma_attr_addr_lo);
75919397407SSherry Moore 
76019397407SSherry Moore 			/*
76119397407SSherry Moore 			 * Include the pte_bits so we don't have to make
76219397407SSherry Moore 			 * it in assembly.
76319397407SSherry Moore 			 */
76419397407SSherry Moore 			fb->fb_pte_list_va[page_index++] = (x86pte_t)
76519397407SSherry Moore 			    (paddr | pte_bits);
76619397407SSherry Moore 		}
76719397407SSherry Moore 
76819397407SSherry Moore 		fb->fb_pte_list_va[page_index] = FASTBOOT_TERMINATE;
76919397407SSherry Moore 
77019397407SSherry Moore 		if (i == FASTBOOT_UNIX) {
7716bc8bc6aSSherry Moore 			Ehdr	*ehdr = (Ehdr *)va;
7726bc8bc6aSSherry Moore 			int	j;
77319397407SSherry Moore 
77419397407SSherry Moore 			/*
77519397407SSherry Moore 			 * Sanity checks:
77619397407SSherry Moore 			 */
77719397407SSherry Moore 			for (j = 0; j < SELFMAG; j++) {
77819397407SSherry Moore 				if (ehdr->e_ident[j] != ELFMAG[j]) {
77919397407SSherry Moore 					cmn_err(CE_WARN, "Fastboot: Bad ELF "
78019397407SSherry Moore 					    "signature");
78119397407SSherry Moore 					goto err_out;
78219397407SSherry Moore 				}
78319397407SSherry Moore 			}
78419397407SSherry Moore 
78519397407SSherry Moore 			if (ehdr->e_ident[EI_CLASS] == ELFCLASS32 &&
78619397407SSherry Moore 			    ehdr->e_ident[EI_DATA] == ELFDATA2LSB &&
78719397407SSherry Moore 			    ehdr->e_machine == EM_386) {
78819397407SSherry Moore 
789*877400d3SKonstantin Ananyev 				fb->fb_sectcnt = sizeof (fb->fb_sections) /
790*877400d3SKonstantin Ananyev 				    sizeof (fb->fb_sections[0]);
791*877400d3SKonstantin Ananyev 
79219397407SSherry Moore 				if (fastboot_elf32_find_loadables((void *)va,
79319397407SSherry Moore 				    fsize, &fb->fb_sections[0],
79419397407SSherry Moore 				    &fb->fb_sectcnt, &dboot_start_offset) < 0) {
79519397407SSherry Moore 					cmn_err(CE_WARN, "Fastboot: ELF32 "
79619397407SSherry Moore 					    "program section failure");
79719397407SSherry Moore 					goto err_out;
79819397407SSherry Moore 				}
79919397407SSherry Moore 
80019397407SSherry Moore 				if (fb->fb_sectcnt == 0) {
80119397407SSherry Moore 					cmn_err(CE_WARN, "Fastboot: No ELF32 "
80219397407SSherry Moore 					    "program sections found");
80319397407SSherry Moore 					goto err_out;
80419397407SSherry Moore 				}
80519397407SSherry Moore 
80619397407SSherry Moore 				if (is_failsafe) {
80719397407SSherry Moore 					/* Failsafe boot_archive */
80819397407SSherry Moore 					bcopy(BOOTARCHIVE_FAILSAFE,
80919397407SSherry Moore 					    &fastboot_filename
81019397407SSherry Moore 					    [FASTBOOT_NAME_BOOTARCHIVE]
81119397407SSherry Moore 					    [bootpath_len],
81219397407SSherry Moore 					    sizeof (BOOTARCHIVE_FAILSAFE));
81319397407SSherry Moore 				} else {
81419397407SSherry Moore 					bcopy(BOOTARCHIVE32,
81519397407SSherry Moore 					    &fastboot_filename
81619397407SSherry Moore 					    [FASTBOOT_NAME_BOOTARCHIVE]
81719397407SSherry Moore 					    [bootpath_len],
81819397407SSherry Moore 					    sizeof (BOOTARCHIVE32));
81919397407SSherry Moore 				}
82019397407SSherry Moore 
82119397407SSherry Moore 			} else if (ehdr->e_ident[EI_CLASS] == ELFCLASS64 &&
82219397407SSherry Moore 			    ehdr->e_ident[EI_DATA] == ELFDATA2LSB &&
82319397407SSherry Moore 			    ehdr->e_machine == EM_AMD64) {
82419397407SSherry Moore 
82519397407SSherry Moore 				if (fastboot_elf64_find_dboot_load_offset(
82619397407SSherry Moore 				    (void *)va, fsize, &dboot_start_offset)
82719397407SSherry Moore 				    != 0) {
82819397407SSherry Moore 					cmn_err(CE_WARN, "Fastboot: Couldn't "
82919397407SSherry Moore 					    "find ELF64 dboot entry offset");
83019397407SSherry Moore 					goto err_out;
83119397407SSherry Moore 				}
83219397407SSherry Moore 
83319397407SSherry Moore 				if ((x86_feature & X86_64) == 0 ||
834*877400d3SKonstantin Ananyev 				    (x86_feature & X86_PAE) == 0) {
83519397407SSherry Moore 					cmn_err(CE_WARN, "Fastboot: Cannot "
83619397407SSherry Moore 					    "reboot to %s: "
83719397407SSherry Moore 					    "not a 64-bit capable system",
83819397407SSherry Moore 					    kern_bootfile);
83919397407SSherry Moore 					goto err_out;
84019397407SSherry Moore 				}
84119397407SSherry Moore 
84219397407SSherry Moore 				bcopy(BOOTARCHIVE64,
84319397407SSherry Moore 				    &fastboot_filename
84419397407SSherry Moore 				    [FASTBOOT_NAME_BOOTARCHIVE][bootpath_len],
84519397407SSherry Moore 				    sizeof (BOOTARCHIVE64));
84619397407SSherry Moore 			} else {
84719397407SSherry Moore 				cmn_err(CE_WARN, "Fastboot: Unknown ELF type");
84819397407SSherry Moore 				goto err_out;
84919397407SSherry Moore 			}
85019397407SSherry Moore 
85119397407SSherry Moore 			fb->fb_dest_pa = DBOOT_ENTRY_ADDRESS -
85219397407SSherry Moore 			    dboot_start_offset;
85319397407SSherry Moore 
85419397407SSherry Moore 			fb->fb_next_pa = DBOOT_ENTRY_ADDRESS + fsize_roundup;
85519397407SSherry Moore 		} else {
85619397407SSherry Moore 			fb->fb_dest_pa = newkernel.fi_files[i - 1].fb_next_pa;
85719397407SSherry Moore 			fb->fb_next_pa = fb->fb_dest_pa + fsize_roundup;
85819397407SSherry Moore 		}
85919397407SSherry Moore 
86019397407SSherry Moore 		kobj_close_file(file);
86119397407SSherry Moore 
8626bc8bc6aSSherry Moore 	}
8636bc8bc6aSSherry Moore 
86419397407SSherry Moore 	/*
86519397407SSherry Moore 	 * Set fb_va to fake_va
86619397407SSherry Moore 	 */
8676bc8bc6aSSherry Moore 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
8686bc8bc6aSSherry Moore 		newkernel.fi_files[i].fb_va = fake_va;
86919397407SSherry Moore 
8706bc8bc6aSSherry Moore 	}
87119397407SSherry Moore 
87219397407SSherry Moore 	/*
87319397407SSherry Moore 	 * Add the function that will switch us to 32-bit protected mode
87419397407SSherry Moore 	 */
87519397407SSherry Moore 	fb = &newkernel.fi_files[FASTBOOT_SWTCH];
87619397407SSherry Moore 	fb->fb_va = fb->fb_dest_pa = FASTBOOT_SWTCH_PA;
877*877400d3SKonstantin Ananyev 	fb->fb_size = MMU_PAGESIZE;
87819397407SSherry Moore 
87919397407SSherry Moore 	/*
88019397407SSherry Moore 	 * Map in FASTBOOT_SWTCH_PA
88119397407SSherry Moore 	 */
88219397407SSherry Moore 	hat_devload(kas.a_hat, (caddr_t)fb->fb_va, MMU_PAGESIZE,
88319397407SSherry Moore 	    mmu_btop(fb->fb_dest_pa),
88419397407SSherry Moore 	    PROT_READ | PROT_WRITE | PROT_EXEC, HAT_LOAD_NOCONSIST);
88519397407SSherry Moore 
88619397407SSherry Moore 	bcopy((void *)fb_swtch_image, (void *)fb->fb_va, fb->fb_size);
88719397407SSherry Moore 
88819397407SSherry Moore 	/*
88919397407SSherry Moore 	 * Build the new multiboot_info structure
89019397407SSherry Moore 	 */
8916bc8bc6aSSherry Moore 	if (fastboot_build_mbi(bootargs, &newkernel) != 0) {
89219397407SSherry Moore 		goto err_out;
89319397407SSherry Moore 	}
89419397407SSherry Moore 
89519397407SSherry Moore 	/*
89619397407SSherry Moore 	 * Build page table for low 1G physical memory. Use big pages.
897*877400d3SKonstantin Ananyev 	 * Allocate 4 (5 for amd64) pages for the page tables.
898*877400d3SKonstantin Ananyev 	 *    1 page for PML4 (amd64)
89919397407SSherry Moore 	 *    1 page for Page-Directory-Pointer Table
900*877400d3SKonstantin Ananyev 	 *    2 pages for Page Directory
90119397407SSherry Moore 	 *    1 page for Page Table.
90219397407SSherry Moore 	 * The page table entry will be rewritten to map the physical
90319397407SSherry Moore 	 * address as we do the copying.
90419397407SSherry Moore 	 */
90519397407SSherry Moore 	if (newkernel.fi_has_pae) {
906*877400d3SKonstantin Ananyev #ifdef	__amd64
907*877400d3SKonstantin Ananyev 		size_t size = MMU_PAGESIZE * 5;
908*877400d3SKonstantin Ananyev #else
90919397407SSherry Moore 		size_t size = MMU_PAGESIZE * 4;
910*877400d3SKonstantin Ananyev #endif	/* __amd64 */
91119397407SSherry Moore 
91219397407SSherry Moore 		if ((newkernel.fi_pagetable_va = (uintptr_t)
91319397407SSherry Moore 		    contig_alloc(size, &fastboot_below_1G_dma_attr,
914*877400d3SKonstantin Ananyev 		    MMU_PAGESIZE, 0)) == NULL) {
91519397407SSherry Moore 			cmn_err(CE_WARN, fastboot_enomem_msg,
91619397407SSherry Moore 			    (uint64_t)size, "1G");
91719397407SSherry Moore 			goto err_out;
91819397407SSherry Moore 		}
91919397407SSherry Moore 
92019397407SSherry Moore 		bzero((void *)(newkernel.fi_pagetable_va), size);
92119397407SSherry Moore 
92219397407SSherry Moore 		newkernel.fi_pagetable_pa =
92319397407SSherry Moore 		    mmu_ptob((uint64_t)hat_getpfnum(kas.a_hat,
92419397407SSherry Moore 		    (caddr_t)newkernel.fi_pagetable_va));
92519397407SSherry Moore 
92619397407SSherry Moore 		newkernel.fi_last_table_pa = newkernel.fi_pagetable_pa +
927*877400d3SKonstantin Ananyev 		    size - MMU_PAGESIZE;
92819397407SSherry Moore 
92919397407SSherry Moore 		newkernel.fi_next_table_va = newkernel.fi_pagetable_va +
93019397407SSherry Moore 		    MMU_PAGESIZE;
93119397407SSherry Moore 		newkernel.fi_next_table_pa = newkernel.fi_pagetable_pa +
93219397407SSherry Moore 		    MMU_PAGESIZE;
93319397407SSherry Moore 
93419397407SSherry Moore 		fastboot_build_pagetables(&newkernel);
93519397407SSherry Moore 	}
93619397407SSherry Moore 
93719397407SSherry Moore 
93819397407SSherry Moore 	/* Mark it as valid */
93919397407SSherry Moore 	newkernel.fi_valid = 1;
94019397407SSherry Moore 	newkernel.fi_magic = FASTBOOT_MAGIC;
94119397407SSherry Moore 
94219397407SSherry Moore 	return;
94319397407SSherry Moore 
94419397407SSherry Moore err_out:
94519397407SSherry Moore 	newkernel.fi_valid = 0;
94619397407SSherry Moore }
94719397407SSherry Moore 
9486bc8bc6aSSherry Moore /*
9496bc8bc6aSSherry Moore  * Jump to the fast reboot switcher.  This function never returns.
9506bc8bc6aSSherry Moore  */
95119397407SSherry Moore void
95219397407SSherry Moore fast_reboot()
95319397407SSherry Moore {
95419397407SSherry Moore 	void (*fastboot_func)(fastboot_info_t *);
95519397407SSherry Moore 
95619397407SSherry Moore 	fastboot_func = (void (*)())(newkernel.fi_files[FASTBOOT_SWTCH].fb_va);
95719397407SSherry Moore 	(*fastboot_func)(&newkernel);
95819397407SSherry Moore }
959