xref: /titanic_50/usr/src/uts/i86pc/os/fastboot.c (revision 6a634c9dca3093f3922e4b7ab826d7bdf17bf78e)
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 /*
23*7417cfdeSKuriakose Kuruvilla  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
2419397407SSherry Moore  */
2519397407SSherry Moore 
266bc8bc6aSSherry Moore /*
276bc8bc6aSSherry Moore  * This file contains the functions for performing Fast Reboot -- a
286bc8bc6aSSherry Moore  * reboot which bypasses the firmware and bootloader, considerably
296bc8bc6aSSherry Moore  * reducing downtime.
306bc8bc6aSSherry Moore  *
31753a6d45SSherry Moore  * fastboot_load_kernel(): This function is invoked by mdpreboot() in the
32753a6d45SSherry Moore  * reboot path.  It loads the new kernel and boot archive into memory, builds
336bc8bc6aSSherry Moore  * the data structure containing sufficient information about the new
346bc8bc6aSSherry Moore  * kernel and boot archive to be passed to the fast reboot switcher
356bc8bc6aSSherry Moore  * (see fb_swtch_src.s for details).  When invoked the switcher relocates
366bc8bc6aSSherry Moore  * the new kernel and boot archive to physically contiguous low memory,
376bc8bc6aSSherry Moore  * similar to where the boot loader would have loaded them, and jumps to
386bc8bc6aSSherry Moore  * the new kernel.
396bc8bc6aSSherry Moore  *
40753a6d45SSherry Moore  * If fastreboot_onpanic is enabled, fastboot_load_kernel() is called
41753a6d45SSherry Moore  * by fastreboot_post_startup() to load the back up kernel in case of
42753a6d45SSherry Moore  * panic.
43753a6d45SSherry Moore  *
446bc8bc6aSSherry Moore  * The physical addresses of the memory allocated for the new kernel, boot
456bc8bc6aSSherry Moore  * archive and their page tables must be above where the boot archive ends
466bc8bc6aSSherry Moore  * after it has been relocated by the switcher, otherwise the new files
476bc8bc6aSSherry Moore  * and their page tables could be overridden during relocation.
486bc8bc6aSSherry Moore  *
496bc8bc6aSSherry Moore  * fast_reboot(): This function is invoked by mdboot() once it's determined
506bc8bc6aSSherry Moore  * that the system is capable of fast reboot.  It jumps to the fast reboot
51753a6d45SSherry Moore  * switcher with the data structure built by fastboot_load_kernel() as the
52753a6d45SSherry Moore  * argument.
536bc8bc6aSSherry Moore  */
5419397407SSherry Moore 
5519397407SSherry Moore #include <sys/types.h>
5619397407SSherry Moore #include <sys/param.h>
5719397407SSherry Moore #include <sys/segments.h>
5819397407SSherry Moore #include <sys/sysmacros.h>
5919397407SSherry Moore #include <sys/vm.h>
6019397407SSherry Moore 
6119397407SSherry Moore #include <sys/proc.h>
6219397407SSherry Moore #include <sys/buf.h>
6319397407SSherry Moore #include <sys/kmem.h>
6419397407SSherry Moore 
6519397407SSherry Moore #include <sys/reboot.h>
6619397407SSherry Moore #include <sys/uadmin.h>
6719397407SSherry Moore 
6819397407SSherry Moore #include <sys/cred.h>
6919397407SSherry Moore #include <sys/vnode.h>
7019397407SSherry Moore #include <sys/file.h>
7119397407SSherry Moore 
7219397407SSherry Moore #include <sys/cmn_err.h>
7319397407SSherry Moore #include <sys/dumphdr.h>
7419397407SSherry Moore #include <sys/bootconf.h>
7519397407SSherry Moore #include <sys/ddidmareq.h>
7619397407SSherry Moore #include <sys/varargs.h>
7719397407SSherry Moore #include <sys/promif.h>
7819397407SSherry Moore #include <sys/modctl.h>
7919397407SSherry Moore 
8019397407SSherry Moore #include <vm/hat.h>
8119397407SSherry Moore #include <vm/as.h>
8219397407SSherry Moore #include <vm/page.h>
8319397407SSherry Moore #include <vm/seg.h>
8419397407SSherry Moore #include <vm/hat_i86.h>
8519397407SSherry Moore #include <sys/vm_machparam.h>
8619397407SSherry Moore #include <sys/archsystm.h>
8719397407SSherry Moore #include <sys/machsystm.h>
8819397407SSherry Moore #include <sys/mman.h>
8919397407SSherry Moore #include <sys/x86_archext.h>
90753a6d45SSherry Moore #include <sys/smp_impldefs.h>
91753a6d45SSherry Moore #include <sys/spl.h>
9219397407SSherry Moore 
935ee8e422SKonstantin Ananyev #include <sys/fastboot_impl.h>
9419397407SSherry Moore #include <sys/machelf.h>
9519397407SSherry Moore #include <sys/kobj.h>
9619397407SSherry Moore #include <sys/multiboot.h>
97753a6d45SSherry Moore #include <sys/kobj_lex.h>
98753a6d45SSherry Moore 
99753a6d45SSherry Moore /*
100753a6d45SSherry Moore  * Macro to determine how many pages are needed for PTEs to map a particular
101753a6d45SSherry Moore  * file.  Allocate one extra page table entry for terminating the list.
102753a6d45SSherry Moore  */
103753a6d45SSherry Moore #define	FASTBOOT_PTE_LIST_SIZE(fsize)	\
104753a6d45SSherry Moore 	P2ROUNDUP((((fsize) >> PAGESHIFT) + 1) * sizeof (x86pte_t), PAGESIZE)
10519397407SSherry Moore 
1066bc8bc6aSSherry Moore /*
1076bc8bc6aSSherry Moore  * Data structure containing necessary information for the fast reboot
1086bc8bc6aSSherry Moore  * switcher to jump to the new kernel.
1096bc8bc6aSSherry Moore  */
11019397407SSherry Moore fastboot_info_t newkernel = { 0 };
111753a6d45SSherry Moore char		fastboot_args[OBP_MAXPATHLEN];
1126bc8bc6aSSherry Moore 
11319397407SSherry Moore static char fastboot_filename[2][OBP_MAXPATHLEN] = { { 0 }, { 0 }};
11419397407SSherry Moore static x86pte_t ptp_bits = PT_VALID | PT_REF | PT_USER | PT_WRITABLE;
11519397407SSherry Moore static x86pte_t pte_bits =
11619397407SSherry Moore     PT_VALID | PT_REF | PT_MOD | PT_NOCONSIST | PT_WRITABLE;
11719397407SSherry Moore static uint_t fastboot_shift_amt_pae[] = {12, 21, 30, 39};
11819397407SSherry Moore 
1195ee8e422SKonstantin Ananyev /* Index into Fast Reboot not supported message array */
1205ee8e422SKonstantin Ananyev static uint32_t fastreboot_nosup_id = FBNS_DEFAULT;
1215ee8e422SKonstantin Ananyev 
1225ee8e422SKonstantin Ananyev /* Fast Reboot not supported message array */
1235ee8e422SKonstantin Ananyev static const char * const fastreboot_nosup_desc[FBNS_END] = {
1245ee8e422SKonstantin Ananyev #define	fastboot_nosup_msg(id, str)	str,
1255ee8e422SKonstantin Ananyev #include <sys/fastboot_msg.h>
1265ee8e422SKonstantin Ananyev };
1275ee8e422SKonstantin Ananyev 
12819397407SSherry Moore int fastboot_debug = 0;
12919397407SSherry Moore int fastboot_contig = 0;
13019397407SSherry Moore 
13119397407SSherry Moore /*
13219397407SSherry Moore  * Fake starting va for new kernel and boot archive.
13319397407SSherry Moore  */
13419397407SSherry Moore static uintptr_t fake_va = FASTBOOT_FAKE_VA;
13519397407SSherry Moore 
13619397407SSherry Moore /*
137753a6d45SSherry Moore  * Reserve memory below PA 1G in preparation of fast reboot.
138753a6d45SSherry Moore  *
139753a6d45SSherry Moore  * This variable is only checked when fastreboot_capable is set, but
140753a6d45SSherry Moore  * fastreboot_onpanic is not set.  The amount of memory reserved
141753a6d45SSherry Moore  * is negligible, but just in case we are really short of low memory,
142753a6d45SSherry Moore  * this variable will give us a backdoor to not consume memory at all.
143753a6d45SSherry Moore  */
144753a6d45SSherry Moore int reserve_mem_enabled = 1;
145753a6d45SSherry Moore 
146753a6d45SSherry Moore /*
147c90a5fbeSSherry Moore  * Mutex to protect fastreboot_onpanic.
148c90a5fbeSSherry Moore  */
149c90a5fbeSSherry Moore kmutex_t fastreboot_config_mutex;
150c90a5fbeSSherry Moore 
151c90a5fbeSSherry Moore /*
152753a6d45SSherry Moore  * Amount of memory below PA 1G to reserve for constructing the multiboot
153753a6d45SSherry Moore  * data structure and the page tables as we tend to run out of those
154753a6d45SSherry Moore  * when more drivers are loaded.
155753a6d45SSherry Moore  */
156753a6d45SSherry Moore static size_t fastboot_mbi_size = 0x2000;	/* 8K */
157753a6d45SSherry Moore static size_t fastboot_pagetable_size = 0x5000;	/* 20K */
158753a6d45SSherry Moore 
159753a6d45SSherry Moore /*
160835b9930SSherry Moore  * Minimum system uptime in clock_t before Fast Reboot should be used
161835b9930SSherry Moore  * on panic.  Will be initialized in fastboot_post_startup().
162835b9930SSherry Moore  */
163835b9930SSherry Moore clock_t fastreboot_onpanic_uptime = LONG_MAX;
164835b9930SSherry Moore 
165835b9930SSherry Moore /*
166835b9930SSherry Moore  * lbolt value when the system booted.  This value will be used if the system
167835b9930SSherry Moore  * panics to calculate how long the system has been up.  If the uptime is less
168835b9930SSherry Moore  * than fastreboot_onpanic_uptime, a reboot through BIOS will be performed to
169835b9930SSherry Moore  * avoid a potential panic/reboot loop.
170835b9930SSherry Moore  */
171835b9930SSherry Moore clock_t lbolt_at_boot = LONG_MAX;
172835b9930SSherry Moore 
173835b9930SSherry Moore /*
174753a6d45SSherry Moore  * Use below 1G for page tables as
175753a6d45SSherry Moore  *	1. we are only doing 1:1 mapping of the bottom 1G of physical memory.
176753a6d45SSherry Moore  *	2. we are using 2G as the fake virtual address for the new kernel and
177753a6d45SSherry Moore  *	boot archive.
17819397407SSherry Moore  */
17919397407SSherry Moore static ddi_dma_attr_t fastboot_below_1G_dma_attr = {
18019397407SSherry Moore 	DMA_ATTR_V0,
18119397407SSherry Moore 	0x0000000008000000ULL,	/* dma_attr_addr_lo: 128MB */
18219397407SSherry Moore 	0x000000003FFFFFFFULL,	/* dma_attr_addr_hi: 1G */
18319397407SSherry Moore 	0x00000000FFFFFFFFULL,	/* dma_attr_count_max */
18419397407SSherry Moore 	0x0000000000001000ULL,	/* dma_attr_align: 4KB */
18519397407SSherry Moore 	1,			/* dma_attr_burstsize */
18619397407SSherry Moore 	1,			/* dma_attr_minxfer */
18719397407SSherry Moore 	0x00000000FFFFFFFFULL,	/* dma_attr_maxxfer */
18819397407SSherry Moore 	0x00000000FFFFFFFFULL,	/* dma_attr_seg */
18919397407SSherry Moore 	1,			/* dma_attr_sgllen */
19019397407SSherry Moore 	0x1000ULL,		/* dma_attr_granular */
19119397407SSherry Moore 	0,			/* dma_attr_flags */
19219397407SSherry Moore };
19319397407SSherry Moore 
19419397407SSherry Moore static ddi_dma_attr_t fastboot_dma_attr = {
19519397407SSherry Moore 	DMA_ATTR_V0,
19619397407SSherry Moore 	0x0000000008000000ULL,	/* dma_attr_addr_lo: 128MB */
197877400d3SKonstantin Ananyev #ifdef	__amd64
198877400d3SKonstantin Ananyev 	0xFFFFFFFFFFFFFFFFULL,	/* dma_attr_addr_hi: 2^64B */
199877400d3SKonstantin Ananyev #else
20019397407SSherry Moore 	0x0000000FFFFFFFFFULL,	/* dma_attr_addr_hi: 64GB */
201877400d3SKonstantin Ananyev #endif	/* __amd64 */
20219397407SSherry Moore 	0x00000000FFFFFFFFULL,	/* dma_attr_count_max */
20319397407SSherry Moore 	0x0000000000001000ULL,	/* dma_attr_align: 4KB */
20419397407SSherry Moore 	1,			/* dma_attr_burstsize */
20519397407SSherry Moore 	1,			/* dma_attr_minxfer */
20619397407SSherry Moore 	0x00000000FFFFFFFFULL,	/* dma_attr_maxxfer */
20719397407SSherry Moore 	0x00000000FFFFFFFFULL,	/* dma_attr_seg */
20819397407SSherry Moore 	1,			/* dma_attr_sgllen */
20919397407SSherry Moore 	0x1000ULL,		/* dma_attr_granular */
21019397407SSherry Moore 	0,			/* dma_attr_flags */
21119397407SSherry Moore };
21219397407SSherry Moore 
21319397407SSherry Moore /*
21419397407SSherry Moore  * Various information saved from the previous boot to reconstruct
21519397407SSherry Moore  * multiboot_info.
21619397407SSherry Moore  */
21719397407SSherry Moore extern multiboot_info_t saved_mbi;
21819397407SSherry Moore extern mb_memory_map_t saved_mmap[FASTBOOT_SAVED_MMAP_COUNT];
2196915124bSKonstantin Ananyev extern uint8_t saved_drives[FASTBOOT_SAVED_DRIVES_SIZE];
22019397407SSherry Moore extern char saved_cmdline[FASTBOOT_SAVED_CMDLINE_LEN];
22119397407SSherry Moore extern int saved_cmdline_len;
222753a6d45SSherry Moore extern size_t saved_file_size[];
22319397407SSherry Moore 
22419397407SSherry Moore extern void* contig_alloc(size_t size, ddi_dma_attr_t *attr,
22519397407SSherry Moore     uintptr_t align, int cansleep);
2266bc8bc6aSSherry Moore extern void contig_free(void *addr, size_t size);
2276bc8bc6aSSherry Moore 
22819397407SSherry Moore 
22919397407SSherry Moore /* PRINTLIKE */
23019397407SSherry Moore extern void vprintf(const char *, va_list);
23119397407SSherry Moore 
23219397407SSherry Moore 
23319397407SSherry Moore /*
23419397407SSherry Moore  * Need to be able to get boot_archives from other places
23519397407SSherry Moore  */
23619397407SSherry Moore #define	BOOTARCHIVE64	"/platform/i86pc/amd64/boot_archive"
23719397407SSherry Moore #define	BOOTARCHIVE32	"/platform/i86pc/boot_archive"
238753a6d45SSherry Moore #define	BOOTARCHIVE32_FAILSAFE	"/boot/x86.miniroot-safe"
239753a6d45SSherry Moore #define	BOOTARCHIVE64_FAILSAFE	"/boot/amd64/x86.miniroot-safe"
240753a6d45SSherry Moore #define	FAILSAFE_BOOTFILE32	"/boot/platform/i86pc/kernel/unix"
241753a6d45SSherry Moore #define	FAILSAFE_BOOTFILE64	"/boot/platform/i86pc/kernel/amd64/unix"
24219397407SSherry Moore 
24319397407SSherry Moore static uint_t fastboot_vatoindex(fastboot_info_t *, uintptr_t, int);
24419397407SSherry Moore static void fastboot_map_with_size(fastboot_info_t *, uintptr_t,
24519397407SSherry Moore     paddr_t, size_t, int);
24619397407SSherry Moore static void fastboot_build_pagetables(fastboot_info_t *);
24719397407SSherry Moore static int fastboot_build_mbi(char *, fastboot_info_t *);
248753a6d45SSherry Moore static void fastboot_free_file(fastboot_file_t *);
24919397407SSherry Moore 
250a2491ff4SSherry Moore static const char fastboot_enomem_msg[] = "!Fastboot: Couldn't allocate 0x%"
25119397407SSherry Moore 	PRIx64" bytes below %s to do fast reboot";
25219397407SSherry Moore 
25319397407SSherry Moore static void
dprintf(char * fmt,...)25419397407SSherry Moore dprintf(char *fmt, ...)
25519397407SSherry Moore {
25619397407SSherry Moore 	va_list adx;
25719397407SSherry Moore 
25819397407SSherry Moore 	if (!fastboot_debug)
25919397407SSherry Moore 		return;
26019397407SSherry Moore 
26119397407SSherry Moore 	va_start(adx, fmt);
26219397407SSherry Moore 	vprintf(fmt, adx);
26319397407SSherry Moore 	va_end(adx);
26419397407SSherry Moore }
26519397407SSherry Moore 
26619397407SSherry Moore 
26719397407SSherry Moore /*
26819397407SSherry Moore  * Return the index corresponding to a virt address at a given page table level.
26919397407SSherry Moore  */
27019397407SSherry Moore static uint_t
fastboot_vatoindex(fastboot_info_t * nk,uintptr_t va,int level)27119397407SSherry Moore fastboot_vatoindex(fastboot_info_t *nk, uintptr_t va, int level)
27219397407SSherry Moore {
27319397407SSherry Moore 	return ((va >> nk->fi_shift_amt[level]) & (nk->fi_ptes_per_table - 1));
27419397407SSherry Moore }
27519397407SSherry Moore 
27619397407SSherry Moore 
27719397407SSherry Moore /*
27819397407SSherry Moore  * Add mapping from vstart to pstart for the specified size.
279877400d3SKonstantin Ananyev  * vstart, pstart and size should all have been aligned at 2M boundaries.
28019397407SSherry Moore  */
28119397407SSherry Moore static void
fastboot_map_with_size(fastboot_info_t * nk,uintptr_t vstart,paddr_t pstart,size_t size,int level)28219397407SSherry Moore fastboot_map_with_size(fastboot_info_t *nk, uintptr_t vstart, paddr_t pstart,
28319397407SSherry Moore     size_t size, int level)
28419397407SSherry Moore {
28519397407SSherry Moore 	x86pte_t	pteval, *table;
28619397407SSherry Moore 	uintptr_t	vaddr;
28719397407SSherry Moore 	paddr_t		paddr;
28819397407SSherry Moore 	int		index, l;
28919397407SSherry Moore 
29019397407SSherry Moore 	table = (x86pte_t *)(nk->fi_pagetable_va);
29119397407SSherry Moore 
29219397407SSherry Moore 	for (l = nk->fi_top_level; l >= level; l--) {
29319397407SSherry Moore 
29419397407SSherry Moore 		index = fastboot_vatoindex(nk, vstart, l);
29519397407SSherry Moore 
29619397407SSherry Moore 		if (l == level) {
29719397407SSherry Moore 			/*
29819397407SSherry Moore 			 * Last level.  Program the page table entries.
29919397407SSherry Moore 			 */
30019397407SSherry Moore 			for (vaddr = vstart, paddr = pstart;
30119397407SSherry Moore 			    vaddr < vstart + size;
30219397407SSherry Moore 			    vaddr += (1ULL << nk->fi_shift_amt[l]),
30319397407SSherry Moore 			    paddr += (1ULL << nk->fi_shift_amt[l])) {
30419397407SSherry Moore 
30519397407SSherry Moore 				uint_t index = fastboot_vatoindex(nk, vaddr, l);
30619397407SSherry Moore 
30719397407SSherry Moore 				if (l > 0)
30819397407SSherry Moore 					pteval = paddr | pte_bits | PT_PAGESIZE;
30919397407SSherry Moore 				else
31019397407SSherry Moore 					pteval = paddr | pte_bits;
31119397407SSherry Moore 
31219397407SSherry Moore 				table[index] = pteval;
31319397407SSherry Moore 			}
31419397407SSherry Moore 		} else if (table[index] & PT_VALID) {
31519397407SSherry Moore 
31619397407SSherry Moore 			table = (x86pte_t *)
31719397407SSherry Moore 			    ((uintptr_t)(((paddr_t)table[index] & MMU_PAGEMASK)
31819397407SSherry Moore 			    - nk->fi_pagetable_pa) + nk->fi_pagetable_va);
31919397407SSherry Moore 		} else {
32019397407SSherry Moore 			/*
321877400d3SKonstantin Ananyev 			 * Intermediate levels.
322877400d3SKonstantin Ananyev 			 * Program with either valid bit or PTP bits.
32319397407SSherry Moore 			 */
32419397407SSherry Moore 			if (l == nk->fi_top_level) {
325877400d3SKonstantin Ananyev #ifdef	__amd64
326877400d3SKonstantin Ananyev 				ASSERT(nk->fi_top_level == 3);
327877400d3SKonstantin Ananyev 				table[index] = nk->fi_next_table_pa | ptp_bits;
328877400d3SKonstantin Ananyev #else
32919397407SSherry Moore 				table[index] = nk->fi_next_table_pa | PT_VALID;
330877400d3SKonstantin Ananyev #endif	/* __amd64 */
33119397407SSherry Moore 			} else {
33219397407SSherry Moore 				table[index] = nk->fi_next_table_pa | ptp_bits;
33319397407SSherry Moore 			}
33419397407SSherry Moore 			table = (x86pte_t *)(nk->fi_next_table_va);
33519397407SSherry Moore 			nk->fi_next_table_va += MMU_PAGESIZE;
33619397407SSherry Moore 			nk->fi_next_table_pa += MMU_PAGESIZE;
33719397407SSherry Moore 		}
33819397407SSherry Moore 	}
33919397407SSherry Moore }
34019397407SSherry Moore 
34119397407SSherry Moore /*
34219397407SSherry Moore  * Build page tables for the lower 1G of physical memory using 2M
34319397407SSherry Moore  * pages, and prepare page tables for mapping new kernel and boot
34419397407SSherry Moore  * archive pages using 4K pages.
34519397407SSherry Moore  */
34619397407SSherry Moore static void
fastboot_build_pagetables(fastboot_info_t * nk)34719397407SSherry Moore fastboot_build_pagetables(fastboot_info_t *nk)
34819397407SSherry Moore {
34919397407SSherry Moore 	/*
35019397407SSherry Moore 	 * Map lower 1G physical memory.  Use large pages.
35119397407SSherry Moore 	 */
35219397407SSherry Moore 	fastboot_map_with_size(nk, 0, 0, ONE_GIG, 1);
35319397407SSherry Moore 
35419397407SSherry Moore 	/*
35519397407SSherry Moore 	 * Map one 4K page to get the middle page tables set up.
35619397407SSherry Moore 	 */
35719397407SSherry Moore 	fake_va = P2ALIGN_TYPED(fake_va, nk->fi_lpagesize, uintptr_t);
35819397407SSherry Moore 	fastboot_map_with_size(nk, fake_va,
35919397407SSherry Moore 	    nk->fi_files[0].fb_pte_list_va[0] & MMU_PAGEMASK, PAGESIZE, 0);
36019397407SSherry Moore }
36119397407SSherry Moore 
36219397407SSherry Moore 
36319397407SSherry Moore /*
36419397407SSherry Moore  * Sanity check.  Look for dboot offset.
36519397407SSherry Moore  */
36619397407SSherry Moore static int
fastboot_elf64_find_dboot_load_offset(void * img,off_t imgsz,uint32_t * offp)36719397407SSherry Moore fastboot_elf64_find_dboot_load_offset(void *img, off_t imgsz, uint32_t *offp)
36819397407SSherry Moore {
36919397407SSherry Moore 	Elf64_Ehdr	*ehdr = (Elf64_Ehdr *)img;
37019397407SSherry Moore 	Elf64_Phdr	*phdr;
37119397407SSherry Moore 	uint8_t		*phdrbase;
37219397407SSherry Moore 	int		i;
37319397407SSherry Moore 
37419397407SSherry Moore 	if ((ehdr->e_phoff + ehdr->e_phnum * ehdr->e_phentsize) >= imgsz)
37519397407SSherry Moore 		return (-1);
37619397407SSherry Moore 
37719397407SSherry Moore 	phdrbase = (uint8_t *)img + ehdr->e_phoff;
37819397407SSherry Moore 
37919397407SSherry Moore 	for (i = 0; i < ehdr->e_phnum; i++) {
38019397407SSherry Moore 		phdr = (Elf64_Phdr *)(phdrbase + ehdr->e_phentsize * i);
38119397407SSherry Moore 
38219397407SSherry Moore 		if (phdr->p_type == PT_LOAD) {
38319397407SSherry Moore 			if (phdr->p_vaddr == phdr->p_paddr &&
38419397407SSherry Moore 			    phdr->p_vaddr == DBOOT_ENTRY_ADDRESS) {
38519397407SSherry Moore 				ASSERT(phdr->p_offset <= UINT32_MAX);
38619397407SSherry Moore 				*offp = (uint32_t)phdr->p_offset;
38719397407SSherry Moore 				return (0);
38819397407SSherry Moore 			}
38919397407SSherry Moore 		}
39019397407SSherry Moore 	}
39119397407SSherry Moore 
39219397407SSherry Moore 	return (-1);
39319397407SSherry Moore }
39419397407SSherry Moore 
39519397407SSherry Moore 
39619397407SSherry Moore /*
39719397407SSherry Moore  * Initialize text and data section information for 32-bit kernel.
398877400d3SKonstantin Ananyev  * sectcntp - is both input/output parameter.
399877400d3SKonstantin Ananyev  * On entry, *sectcntp contains maximum allowable number of sections;
400877400d3SKonstantin Ananyev  * on return, it contains the actual number of sections filled.
40119397407SSherry Moore  */
40219397407SSherry Moore static int
fastboot_elf32_find_loadables(void * img,off_t imgsz,fastboot_section_t * sectp,int * sectcntp,uint32_t * offp)40319397407SSherry Moore fastboot_elf32_find_loadables(void *img, off_t imgsz, fastboot_section_t *sectp,
40419397407SSherry Moore     int *sectcntp, uint32_t *offp)
40519397407SSherry Moore {
40619397407SSherry Moore 	Elf32_Ehdr	*ehdr = (Elf32_Ehdr *)img;
40719397407SSherry Moore 	Elf32_Phdr	*phdr;
40819397407SSherry Moore 	uint8_t		*phdrbase;
40919397407SSherry Moore 	int		i;
41019397407SSherry Moore 	int		used_sections = 0;
411877400d3SKonstantin Ananyev 	const int	max_sectcnt = *sectcntp;
41219397407SSherry Moore 
41319397407SSherry Moore 	if ((ehdr->e_phoff + ehdr->e_phnum * ehdr->e_phentsize) >= imgsz)
41419397407SSherry Moore 		return (-1);
41519397407SSherry Moore 
41619397407SSherry Moore 	phdrbase = (uint8_t *)img + ehdr->e_phoff;
41719397407SSherry Moore 
41819397407SSherry Moore 	for (i = 0; i < ehdr->e_phnum; i++) {
41919397407SSherry Moore 		phdr = (Elf32_Phdr *)(phdrbase + ehdr->e_phentsize * i);
42019397407SSherry Moore 
42119397407SSherry Moore 		if (phdr->p_type == PT_INTERP)
42219397407SSherry Moore 			return (-1);
42319397407SSherry Moore 
42419397407SSherry Moore 		if (phdr->p_type != PT_LOAD)
42519397407SSherry Moore 			continue;
42619397407SSherry Moore 
42719397407SSherry Moore 		if (phdr->p_vaddr == phdr->p_paddr &&
42819397407SSherry Moore 		    phdr->p_paddr == DBOOT_ENTRY_ADDRESS) {
42919397407SSherry Moore 			*offp = (uint32_t)phdr->p_offset;
43019397407SSherry Moore 		} else {
431877400d3SKonstantin Ananyev 			if (max_sectcnt <= used_sections)
432877400d3SKonstantin Ananyev 				return (-1);
433877400d3SKonstantin Ananyev 
43419397407SSherry Moore 			sectp[used_sections].fb_sec_offset = phdr->p_offset;
43519397407SSherry Moore 			sectp[used_sections].fb_sec_paddr = phdr->p_paddr;
43619397407SSherry Moore 			sectp[used_sections].fb_sec_size = phdr->p_filesz;
43719397407SSherry Moore 			sectp[used_sections].fb_sec_bss_size =
43819397407SSherry Moore 			    (phdr->p_filesz < phdr->p_memsz) ?
43919397407SSherry Moore 			    (phdr->p_memsz - phdr->p_filesz) : 0;
44019397407SSherry Moore 
441877400d3SKonstantin Ananyev 			/* Extra sanity check for the input object file */
442877400d3SKonstantin Ananyev 			if (sectp[used_sections].fb_sec_paddr +
443877400d3SKonstantin Ananyev 			    sectp[used_sections].fb_sec_size +
444877400d3SKonstantin Ananyev 			    sectp[used_sections].fb_sec_bss_size >=
445877400d3SKonstantin Ananyev 			    DBOOT_ENTRY_ADDRESS)
446877400d3SKonstantin Ananyev 				return (-1);
447877400d3SKonstantin Ananyev 
44819397407SSherry Moore 			used_sections++;
44919397407SSherry Moore 		}
45019397407SSherry Moore 	}
45119397407SSherry Moore 
45219397407SSherry Moore 	*sectcntp = used_sections;
45319397407SSherry Moore 	return (0);
45419397407SSherry Moore }
45519397407SSherry Moore 
45619397407SSherry Moore /*
4571a6f4459SKonstantin Ananyev  * Create multiboot info structure (mbi) base on the saved mbi.
4581a6f4459SKonstantin Ananyev  * Recalculate values of the pointer type fields in the data
4591a6f4459SKonstantin Ananyev  * structure based on the new starting physical address of the
4601a6f4459SKonstantin Ananyev  * data structure.
46119397407SSherry Moore  */
46219397407SSherry Moore static int
fastboot_build_mbi(char * mdep,fastboot_info_t * nk)46319397407SSherry Moore fastboot_build_mbi(char *mdep, fastboot_info_t *nk)
46419397407SSherry Moore {
46519397407SSherry Moore 	mb_module_t	*mbp;
4661a6f4459SKonstantin Ananyev 	multiboot_info_t	*mbi;	/* pointer to multiboot structure */
4671a6f4459SKonstantin Ananyev 	uintptr_t	start_addr_va;	/* starting VA of mbi */
4681a6f4459SKonstantin Ananyev 	uintptr_t	start_addr_pa;	/* starting PA of mbi */
4691a6f4459SKonstantin Ananyev 	size_t		offs = 0;	/* offset from the starting address */
4701a6f4459SKonstantin Ananyev 	size_t		arglen;		/* length of the command line arg */
4711a6f4459SKonstantin Ananyev 	size_t		size;	/* size of the memory reserved for mbi */
4721a6f4459SKonstantin Ananyev 	size_t		mdnsz;	/* length of the boot archive name */
47319397407SSherry Moore 
4741a6f4459SKonstantin Ananyev 	/*
4751a6f4459SKonstantin Ananyev 	 * If mdep is not NULL or empty, use the length of mdep + 1
4761a6f4459SKonstantin Ananyev 	 * (for NULL terminating) as the length of the new command
4771a6f4459SKonstantin Ananyev 	 * line; else use the saved command line length as the
4781a6f4459SKonstantin Ananyev 	 * length for the new command line.
4791a6f4459SKonstantin Ananyev 	 */
4806bc8bc6aSSherry Moore 	if (mdep != NULL && strlen(mdep) != 0) {
48119397407SSherry Moore 		arglen = strlen(mdep) + 1;
48219397407SSherry Moore 	} else {
48319397407SSherry Moore 		arglen = saved_cmdline_len;
48419397407SSherry Moore 	}
48519397407SSherry Moore 
4861a6f4459SKonstantin Ananyev 	/*
4871a6f4459SKonstantin Ananyev 	 * Allocate memory for the new multiboot info structure (mbi).
4881a6f4459SKonstantin Ananyev 	 * If we have reserved memory for mbi but it's not enough,
4891a6f4459SKonstantin Ananyev 	 * free it and reallocate.
4901a6f4459SKonstantin Ananyev 	 */
49119397407SSherry Moore 	size = PAGESIZE + P2ROUNDUP(arglen, PAGESIZE);
492753a6d45SSherry Moore 	if (nk->fi_mbi_size && nk->fi_mbi_size < size) {
493753a6d45SSherry Moore 		contig_free((void *)nk->fi_new_mbi_va, nk->fi_mbi_size);
494753a6d45SSherry Moore 		nk->fi_mbi_size = 0;
49519397407SSherry Moore 	}
49619397407SSherry Moore 
497753a6d45SSherry Moore 	if (nk->fi_mbi_size == 0) {
498753a6d45SSherry Moore 		if ((nk->fi_new_mbi_va =
499753a6d45SSherry Moore 		    (uintptr_t)contig_alloc(size, &fastboot_below_1G_dma_attr,
500753a6d45SSherry Moore 		    PAGESIZE, 0)) == NULL) {
501a2491ff4SSherry Moore 			cmn_err(CE_NOTE, fastboot_enomem_msg,
502753a6d45SSherry Moore 			    (uint64_t)size, "1G");
503753a6d45SSherry Moore 			return (-1);
504753a6d45SSherry Moore 		}
505753a6d45SSherry Moore 		/*
506753a6d45SSherry Moore 		 * fi_mbi_size must be set after the allocation succeeds
507753a6d45SSherry Moore 		 * as it's used to determine how much memory to free.
508753a6d45SSherry Moore 		 */
509753a6d45SSherry Moore 		nk->fi_mbi_size = size;
510753a6d45SSherry Moore 	}
51119397407SSherry Moore 
5121a6f4459SKonstantin Ananyev 	/*
5131a6f4459SKonstantin Ananyev 	 * Initalize memory
5141a6f4459SKonstantin Ananyev 	 */
515753a6d45SSherry Moore 	bzero((void *)nk->fi_new_mbi_va, nk->fi_mbi_size);
51619397407SSherry Moore 
5171a6f4459SKonstantin Ananyev 	/*
5181a6f4459SKonstantin Ananyev 	 * Get PA for the new mbi
5191a6f4459SKonstantin Ananyev 	 */
5201a6f4459SKonstantin Ananyev 	start_addr_va = nk->fi_new_mbi_va;
5211a6f4459SKonstantin Ananyev 	start_addr_pa = mmu_ptob((uint64_t)hat_getpfnum(kas.a_hat,
5221a6f4459SKonstantin Ananyev 	    (caddr_t)start_addr_va));
5231a6f4459SKonstantin Ananyev 	nk->fi_new_mbi_pa = (paddr_t)start_addr_pa;
524753a6d45SSherry Moore 
525753a6d45SSherry Moore 	/*
5261a6f4459SKonstantin Ananyev 	 * Populate the rest of the fields in the data structure
527753a6d45SSherry Moore 	 */
528753a6d45SSherry Moore 
5291a6f4459SKonstantin Ananyev 	/*
5301a6f4459SKonstantin Ananyev 	 * Copy from the saved mbi to preserve all non-pointer type fields.
5311a6f4459SKonstantin Ananyev 	 */
5321a6f4459SKonstantin Ananyev 	mbi = (multiboot_info_t *)start_addr_va;
5331a6f4459SKonstantin Ananyev 	bcopy(&saved_mbi, mbi, sizeof (*mbi));
53419397407SSherry Moore 
5351a6f4459SKonstantin Ananyev 	/*
5361a6f4459SKonstantin Ananyev 	 * Recalculate mods_addr.  Set mod_start and mod_end based on
5371a6f4459SKonstantin Ananyev 	 * the physical address of the new boot archive.  Set mod_name
5381a6f4459SKonstantin Ananyev 	 * to the name of the new boto archive.
5391a6f4459SKonstantin Ananyev 	 */
5401a6f4459SKonstantin Ananyev 	offs += sizeof (multiboot_info_t);
5411a6f4459SKonstantin Ananyev 	mbi->mods_addr = start_addr_pa + offs;
5421a6f4459SKonstantin Ananyev 	mbp = (mb_module_t *)(start_addr_va + offs);
543753a6d45SSherry Moore 	mbp->mod_start = nk->fi_files[FASTBOOT_BOOTARCHIVE].fb_dest_pa;
544753a6d45SSherry Moore 	mbp->mod_end = nk->fi_files[FASTBOOT_BOOTARCHIVE].fb_next_pa;
54519397407SSherry Moore 
5461a6f4459SKonstantin Ananyev 	offs += sizeof (mb_module_t);
5471a6f4459SKonstantin Ananyev 	mdnsz = strlen(fastboot_filename[FASTBOOT_NAME_BOOTARCHIVE]) + 1;
5481a6f4459SKonstantin Ananyev 	bcopy(fastboot_filename[FASTBOOT_NAME_BOOTARCHIVE],
5491a6f4459SKonstantin Ananyev 	    (void *)(start_addr_va + offs), mdnsz);
5501a6f4459SKonstantin Ananyev 	mbp->mod_name = start_addr_pa + offs;
55119397407SSherry Moore 	mbp->reserved = 0;
55219397407SSherry Moore 
5531a6f4459SKonstantin Ananyev 	/*
5541a6f4459SKonstantin Ananyev 	 * Make sure the offset is 16-byte aligned to avoid unaligned access.
5551a6f4459SKonstantin Ananyev 	 */
5561a6f4459SKonstantin Ananyev 	offs += mdnsz;
5571a6f4459SKonstantin Ananyev 	offs = P2ROUNDUP_TYPED(offs, 16, size_t);
5581a6f4459SKonstantin Ananyev 
5591a6f4459SKonstantin Ananyev 	/*
5601a6f4459SKonstantin Ananyev 	 * Recalculate mmap_addr
5611a6f4459SKonstantin Ananyev 	 */
5621a6f4459SKonstantin Ananyev 	mbi->mmap_addr = start_addr_pa + offs;
5631a6f4459SKonstantin Ananyev 	bcopy((void *)(uintptr_t)saved_mmap, (void *)(start_addr_va + offs),
56419397407SSherry Moore 	    saved_mbi.mmap_length);
5651a6f4459SKonstantin Ananyev 	offs += saved_mbi.mmap_length;
56619397407SSherry Moore 
5671a6f4459SKonstantin Ananyev 	/*
5681a6f4459SKonstantin Ananyev 	 * Recalculate drives_addr
5691a6f4459SKonstantin Ananyev 	 */
5701a6f4459SKonstantin Ananyev 	mbi->drives_addr = start_addr_pa + offs;
5711a6f4459SKonstantin Ananyev 	bcopy((void *)(uintptr_t)saved_drives, (void *)(start_addr_va + offs),
57219397407SSherry Moore 	    saved_mbi.drives_length);
5731a6f4459SKonstantin Ananyev 	offs += saved_mbi.drives_length;
57419397407SSherry Moore 
5751a6f4459SKonstantin Ananyev 	/*
5761a6f4459SKonstantin Ananyev 	 * Recalculate the address of cmdline.  Set cmdline to contain the
5771a6f4459SKonstantin Ananyev 	 * new boot argument.
5781a6f4459SKonstantin Ananyev 	 */
5791a6f4459SKonstantin Ananyev 	mbi->cmdline = start_addr_pa + offs;
58019397407SSherry Moore 
5816bc8bc6aSSherry Moore 	if (mdep != NULL && strlen(mdep) != 0) {
5821a6f4459SKonstantin Ananyev 		bcopy(mdep, (void *)(start_addr_va + offs), arglen);
58319397407SSherry Moore 	} else {
5841a6f4459SKonstantin Ananyev 		bcopy((void *)saved_cmdline, (void *)(start_addr_va + offs),
5851a6f4459SKonstantin Ananyev 		    arglen);
58619397407SSherry Moore 	}
58719397407SSherry Moore 
5886915124bSKonstantin Ananyev 	/* clear fields and flags that are not copied */
5896915124bSKonstantin Ananyev 	bzero(&mbi->config_table,
5906915124bSKonstantin Ananyev 	    sizeof (*mbi) - offsetof(multiboot_info_t, config_table));
5916915124bSKonstantin Ananyev 	mbi->flags &= ~(MB_INFO_CONFIG_TABLE | MB_INFO_BOOT_LOADER_NAME |
5926915124bSKonstantin Ananyev 	    MB_INFO_APM_TABLE | MB_INFO_VIDEO_INFO);
5936915124bSKonstantin Ananyev 
59419397407SSherry Moore 	return (0);
59519397407SSherry Moore }
59619397407SSherry Moore 
5976bc8bc6aSSherry Moore /*
5986bc8bc6aSSherry Moore  * Initialize HAT related fields
5996bc8bc6aSSherry Moore  */
6006bc8bc6aSSherry Moore static void
fastboot_init_fields(fastboot_info_t * nk)6016bc8bc6aSSherry Moore fastboot_init_fields(fastboot_info_t *nk)
60219397407SSherry Moore {
603*7417cfdeSKuriakose Kuruvilla 	if (is_x86_feature(x86_featureset, X86FSET_PAE)) {
6046bc8bc6aSSherry Moore 		nk->fi_has_pae = 1;
6056bc8bc6aSSherry Moore 		nk->fi_shift_amt = fastboot_shift_amt_pae;
6066bc8bc6aSSherry Moore 		nk->fi_ptes_per_table = 512;
6076bc8bc6aSSherry Moore 		nk->fi_lpagesize = (2 << 20);	/* 2M */
608877400d3SKonstantin Ananyev #ifdef	__amd64
609877400d3SKonstantin Ananyev 		nk->fi_top_level = 3;
610877400d3SKonstantin Ananyev #else
6116bc8bc6aSSherry Moore 		nk->fi_top_level = 2;
612877400d3SKonstantin Ananyev #endif	/* __amd64 */
6136bc8bc6aSSherry Moore 	}
61419397407SSherry Moore }
61519397407SSherry Moore 
6166bc8bc6aSSherry Moore /*
6176bc8bc6aSSherry Moore  * Process boot argument
6186bc8bc6aSSherry Moore  */
6196bc8bc6aSSherry Moore static void
fastboot_parse_mdep(char * mdep,char * kern_bootpath,int * bootpath_len,char * bootargs)6206bc8bc6aSSherry Moore fastboot_parse_mdep(char *mdep, char *kern_bootpath, int *bootpath_len,
6216bc8bc6aSSherry Moore     char *bootargs)
6226bc8bc6aSSherry Moore {
6236bc8bc6aSSherry Moore 	int	i;
62419397407SSherry Moore 
62519397407SSherry Moore 	/*
62619397407SSherry Moore 	 * If mdep is not NULL, it comes in the format of
62719397407SSherry Moore 	 *	mountpoint unix args
62819397407SSherry Moore 	 */
6296bc8bc6aSSherry Moore 	if (mdep != NULL && strlen(mdep) != 0) {
63019397407SSherry Moore 		if (mdep[0] != '-') {
63119397407SSherry Moore 			/* First get the root argument */
63219397407SSherry Moore 			i = 0;
63319397407SSherry Moore 			while (mdep[i] != '\0' && mdep[i] != ' ') {
63419397407SSherry Moore 				i++;
63519397407SSherry Moore 			}
63619397407SSherry Moore 
63719397407SSherry Moore 			if (i < 4 || strncmp(&mdep[i-4], "unix", 4) != 0) {
63819397407SSherry Moore 				/* mount point */
63919397407SSherry Moore 				bcopy(mdep, kern_bootpath, i);
64019397407SSherry Moore 				kern_bootpath[i] = '\0';
6416bc8bc6aSSherry Moore 				*bootpath_len = i;
64219397407SSherry Moore 
64319397407SSherry Moore 				/*
64419397407SSherry Moore 				 * Get the next argument. It should be unix as
64519397407SSherry Moore 				 * we have validated in in halt.c.
64619397407SSherry Moore 				 */
64719397407SSherry Moore 				if (strlen(mdep) > i) {
64819397407SSherry Moore 					mdep += (i + 1);
64919397407SSherry Moore 					i = 0;
65019397407SSherry Moore 					while (mdep[i] != '\0' &&
65119397407SSherry Moore 					    mdep[i] != ' ') {
65219397407SSherry Moore 						i++;
65319397407SSherry Moore 					}
65419397407SSherry Moore 				}
65519397407SSherry Moore 
65619397407SSherry Moore 			}
65719397407SSherry Moore 			bcopy(mdep, kern_bootfile, i);
65819397407SSherry Moore 			kern_bootfile[i] = '\0';
6596bc8bc6aSSherry Moore 			bcopy(mdep, bootargs, strlen(mdep));
66019397407SSherry Moore 		} else {
66119397407SSherry Moore 			int off = strlen(kern_bootfile);
66219397407SSherry Moore 			bcopy(kern_bootfile, bootargs, off);
66319397407SSherry Moore 			bcopy(" ", &bootargs[off++], 1);
66419397407SSherry Moore 			bcopy(mdep, &bootargs[off], strlen(mdep));
66519397407SSherry Moore 			off += strlen(mdep);
66619397407SSherry Moore 			bootargs[off] = '\0';
66719397407SSherry Moore 		}
66819397407SSherry Moore 	}
6696bc8bc6aSSherry Moore }
6706bc8bc6aSSherry Moore 
6716bc8bc6aSSherry Moore /*
672753a6d45SSherry Moore  * Reserve memory under PA 1G for mapping the new kernel and boot archive.
673753a6d45SSherry Moore  * This function is only called if fastreboot_onpanic is *not* set.
674753a6d45SSherry Moore  */
675753a6d45SSherry Moore static void
fastboot_reserve_mem(fastboot_info_t * nk)676753a6d45SSherry Moore fastboot_reserve_mem(fastboot_info_t *nk)
677753a6d45SSherry Moore {
678753a6d45SSherry Moore 	int i;
679753a6d45SSherry Moore 
680753a6d45SSherry Moore 	/*
681753a6d45SSherry Moore 	 * A valid kernel is in place.  No need to reserve any memory.
682753a6d45SSherry Moore 	 */
683753a6d45SSherry Moore 	if (nk->fi_valid)
684753a6d45SSherry Moore 		return;
685753a6d45SSherry Moore 
686753a6d45SSherry Moore 	/*
687753a6d45SSherry Moore 	 * Reserve memory under PA 1G for PTE lists.
688753a6d45SSherry Moore 	 */
689753a6d45SSherry Moore 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
690753a6d45SSherry Moore 		fastboot_file_t *fb = &nk->fi_files[i];
691753a6d45SSherry Moore 		size_t fsize_roundup, size;
692753a6d45SSherry Moore 
693753a6d45SSherry Moore 		fsize_roundup = P2ROUNDUP_TYPED(saved_file_size[i],
694753a6d45SSherry Moore 		    PAGESIZE, size_t);
695753a6d45SSherry Moore 		size = FASTBOOT_PTE_LIST_SIZE(fsize_roundup);
696753a6d45SSherry Moore 		if ((fb->fb_pte_list_va = contig_alloc(size,
697753a6d45SSherry Moore 		    &fastboot_below_1G_dma_attr, PAGESIZE, 0)) == NULL) {
698753a6d45SSherry Moore 			return;
699753a6d45SSherry Moore 		}
700753a6d45SSherry Moore 		fb->fb_pte_list_size = size;
701753a6d45SSherry Moore 	}
702753a6d45SSherry Moore 
703753a6d45SSherry Moore 	/*
704753a6d45SSherry Moore 	 * Reserve memory under PA 1G for page tables.
705753a6d45SSherry Moore 	 */
706753a6d45SSherry Moore 	if ((nk->fi_pagetable_va =
707753a6d45SSherry Moore 	    (uintptr_t)contig_alloc(fastboot_pagetable_size,
708753a6d45SSherry Moore 	    &fastboot_below_1G_dma_attr, PAGESIZE, 0)) == NULL) {
709753a6d45SSherry Moore 		return;
710753a6d45SSherry Moore 	}
711753a6d45SSherry Moore 	nk->fi_pagetable_size = fastboot_pagetable_size;
712753a6d45SSherry Moore 
713753a6d45SSherry Moore 	/*
714753a6d45SSherry Moore 	 * Reserve memory under PA 1G for multiboot structure.
715753a6d45SSherry Moore 	 */
716753a6d45SSherry Moore 	if ((nk->fi_new_mbi_va = (uintptr_t)contig_alloc(fastboot_mbi_size,
717753a6d45SSherry Moore 	    &fastboot_below_1G_dma_attr, PAGESIZE, 0)) == NULL) {
718753a6d45SSherry Moore 		return;
719753a6d45SSherry Moore 	}
720753a6d45SSherry Moore 	nk->fi_mbi_size = fastboot_mbi_size;
721753a6d45SSherry Moore }
722753a6d45SSherry Moore 
723753a6d45SSherry Moore /*
724753a6d45SSherry Moore  * Calculate MD5 digest for the given fastboot_file.
725753a6d45SSherry Moore  * Assumes that the file is allready loaded properly.
726753a6d45SSherry Moore  */
727753a6d45SSherry Moore static void
fastboot_cksum_file(fastboot_file_t * fb,uchar_t * md5_hash)728753a6d45SSherry Moore fastboot_cksum_file(fastboot_file_t *fb, uchar_t *md5_hash)
729753a6d45SSherry Moore {
730753a6d45SSherry Moore 	MD5_CTX md5_ctx;
731753a6d45SSherry Moore 
732753a6d45SSherry Moore 	MD5Init(&md5_ctx);
733753a6d45SSherry Moore 	MD5Update(&md5_ctx, (void *)fb->fb_va, fb->fb_size);
734753a6d45SSherry Moore 	MD5Final(md5_hash, &md5_ctx);
735753a6d45SSherry Moore }
736753a6d45SSherry Moore 
737753a6d45SSherry Moore /*
738753a6d45SSherry Moore  * Free up the memory we have allocated for a file
7396bc8bc6aSSherry Moore  */
7406bc8bc6aSSherry Moore static void
fastboot_free_file(fastboot_file_t * fb)7416bc8bc6aSSherry Moore fastboot_free_file(fastboot_file_t *fb)
7426bc8bc6aSSherry Moore {
743753a6d45SSherry Moore 	size_t	fsize_roundup;
7446bc8bc6aSSherry Moore 
7456bc8bc6aSSherry Moore 	fsize_roundup = P2ROUNDUP_TYPED(fb->fb_size, PAGESIZE, size_t);
746753a6d45SSherry Moore 	if (fsize_roundup) {
7476bc8bc6aSSherry Moore 		contig_free((void *)fb->fb_va, fsize_roundup);
748753a6d45SSherry Moore 		fb->fb_va = NULL;
749753a6d45SSherry Moore 		fb->fb_size = 0;
750753a6d45SSherry Moore 	}
751753a6d45SSherry Moore }
7526bc8bc6aSSherry Moore 
753753a6d45SSherry Moore /*
754753a6d45SSherry Moore  * Free up memory used by the PTEs for a file.
755753a6d45SSherry Moore  */
756753a6d45SSherry Moore static void
fastboot_free_file_pte(fastboot_file_t * fb,uint64_t endaddr)757753a6d45SSherry Moore fastboot_free_file_pte(fastboot_file_t *fb, uint64_t endaddr)
758753a6d45SSherry Moore {
759753a6d45SSherry Moore 	if (fb->fb_pte_list_size && fb->fb_pte_list_pa < endaddr) {
760753a6d45SSherry Moore 		contig_free((void *)fb->fb_pte_list_va, fb->fb_pte_list_size);
761753a6d45SSherry Moore 		fb->fb_pte_list_va = 0;
762753a6d45SSherry Moore 		fb->fb_pte_list_pa = 0;
763753a6d45SSherry Moore 		fb->fb_pte_list_size = 0;
764753a6d45SSherry Moore 	}
765753a6d45SSherry Moore }
766753a6d45SSherry Moore 
767753a6d45SSherry Moore /*
768753a6d45SSherry Moore  * Free up all the memory used for representing a kernel with
769753a6d45SSherry Moore  * fastboot_info_t.
770753a6d45SSherry Moore  */
771753a6d45SSherry Moore static void
fastboot_free_mem(fastboot_info_t * nk,uint64_t endaddr)772753a6d45SSherry Moore fastboot_free_mem(fastboot_info_t *nk, uint64_t endaddr)
773753a6d45SSherry Moore {
774753a6d45SSherry Moore 	int i;
775753a6d45SSherry Moore 
776753a6d45SSherry Moore 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
777753a6d45SSherry Moore 		fastboot_free_file(nk->fi_files + i);
778753a6d45SSherry Moore 		fastboot_free_file_pte(nk->fi_files + i, endaddr);
779753a6d45SSherry Moore 	}
780753a6d45SSherry Moore 
781753a6d45SSherry Moore 	if (nk->fi_pagetable_size && nk->fi_pagetable_pa < endaddr) {
782753a6d45SSherry Moore 		contig_free((void *)nk->fi_pagetable_va, nk->fi_pagetable_size);
783753a6d45SSherry Moore 		nk->fi_pagetable_va = 0;
784753a6d45SSherry Moore 		nk->fi_pagetable_pa = 0;
785753a6d45SSherry Moore 		nk->fi_pagetable_size = 0;
786753a6d45SSherry Moore 	}
787753a6d45SSherry Moore 
788753a6d45SSherry Moore 	if (nk->fi_mbi_size && nk->fi_new_mbi_pa < endaddr) {
789753a6d45SSherry Moore 		contig_free((void *)nk->fi_new_mbi_va, nk->fi_mbi_size);
790753a6d45SSherry Moore 		nk->fi_new_mbi_va = 0;
791753a6d45SSherry Moore 		nk->fi_new_mbi_pa = 0;
792753a6d45SSherry Moore 		nk->fi_mbi_size = 0;
793753a6d45SSherry Moore 	}
794753a6d45SSherry Moore }
795753a6d45SSherry Moore 
796753a6d45SSherry Moore /*
797753a6d45SSherry Moore  * Only free up the memory allocated for the kernel and boot archive,
798753a6d45SSherry Moore  * but not for the page tables.
799753a6d45SSherry Moore  */
800753a6d45SSherry Moore void
fastboot_free_newkernel(fastboot_info_t * nk)801753a6d45SSherry Moore fastboot_free_newkernel(fastboot_info_t *nk)
802753a6d45SSherry Moore {
803753a6d45SSherry Moore 	int i;
804753a6d45SSherry Moore 
805753a6d45SSherry Moore 	nk->fi_valid = 0;
806753a6d45SSherry Moore 	/*
807753a6d45SSherry Moore 	 * Free the memory we have allocated
808753a6d45SSherry Moore 	 */
809753a6d45SSherry Moore 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
810753a6d45SSherry Moore 		fastboot_free_file(&(nk->fi_files[i]));
811753a6d45SSherry Moore 	}
812753a6d45SSherry Moore }
813753a6d45SSherry Moore 
814753a6d45SSherry Moore static void
fastboot_cksum_cdata(fastboot_info_t * nk,uchar_t * md5_hash)815753a6d45SSherry Moore fastboot_cksum_cdata(fastboot_info_t *nk, uchar_t *md5_hash)
816753a6d45SSherry Moore {
817753a6d45SSherry Moore 	int i;
818753a6d45SSherry Moore 	MD5_CTX md5_ctx;
819753a6d45SSherry Moore 
820753a6d45SSherry Moore 	MD5Init(&md5_ctx);
821753a6d45SSherry Moore 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
822753a6d45SSherry Moore 		MD5Update(&md5_ctx, nk->fi_files[i].fb_pte_list_va,
823753a6d45SSherry Moore 		    nk->fi_files[i].fb_pte_list_size);
824753a6d45SSherry Moore 	}
825753a6d45SSherry Moore 	MD5Update(&md5_ctx, (void *)nk->fi_pagetable_va, nk->fi_pagetable_size);
826753a6d45SSherry Moore 	MD5Update(&md5_ctx, (void *)nk->fi_new_mbi_va, nk->fi_mbi_size);
827753a6d45SSherry Moore 
828753a6d45SSherry Moore 	MD5Final(md5_hash, &md5_ctx);
829753a6d45SSherry Moore }
830753a6d45SSherry Moore 
831753a6d45SSherry Moore /*
832753a6d45SSherry Moore  * Generate MD5 checksum of the given kernel.
833753a6d45SSherry Moore  */
834753a6d45SSherry Moore static void
fastboot_cksum_generate(fastboot_info_t * nk)835753a6d45SSherry Moore fastboot_cksum_generate(fastboot_info_t *nk)
836753a6d45SSherry Moore {
837753a6d45SSherry Moore 	int i;
838753a6d45SSherry Moore 
839753a6d45SSherry Moore 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
840753a6d45SSherry Moore 		fastboot_cksum_file(nk->fi_files + i, nk->fi_md5_hash[i]);
841753a6d45SSherry Moore 	}
842753a6d45SSherry Moore 	fastboot_cksum_cdata(nk, nk->fi_md5_hash[i]);
843753a6d45SSherry Moore }
844753a6d45SSherry Moore 
845753a6d45SSherry Moore /*
846753a6d45SSherry Moore  * Calculate MD5 checksum of the given kernel and verify that
847753a6d45SSherry Moore  * it matches with what was calculated before.
848753a6d45SSherry Moore  */
849753a6d45SSherry Moore int
fastboot_cksum_verify(fastboot_info_t * nk)850753a6d45SSherry Moore fastboot_cksum_verify(fastboot_info_t *nk)
851753a6d45SSherry Moore {
852753a6d45SSherry Moore 	int i;
853753a6d45SSherry Moore 	uchar_t md5_hash[MD5_DIGEST_LENGTH];
854753a6d45SSherry Moore 
855753a6d45SSherry Moore 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
856753a6d45SSherry Moore 		fastboot_cksum_file(nk->fi_files + i, md5_hash);
857753a6d45SSherry Moore 		if (bcmp(nk->fi_md5_hash[i], md5_hash,
858753a6d45SSherry Moore 		    sizeof (nk->fi_md5_hash[i])) != 0)
859753a6d45SSherry Moore 			return (i + 1);
860753a6d45SSherry Moore 	}
861753a6d45SSherry Moore 
862753a6d45SSherry Moore 	fastboot_cksum_cdata(nk, md5_hash);
863753a6d45SSherry Moore 	if (bcmp(nk->fi_md5_hash[i], md5_hash,
864753a6d45SSherry Moore 	    sizeof (nk->fi_md5_hash[i])) != 0)
865753a6d45SSherry Moore 		return (i + 1);
866753a6d45SSherry Moore 
867753a6d45SSherry Moore 	return (0);
8686bc8bc6aSSherry Moore }
8696bc8bc6aSSherry Moore 
8706bc8bc6aSSherry Moore /*
8716bc8bc6aSSherry Moore  * This function performs the following tasks:
8726bc8bc6aSSherry Moore  * - Read the sizes of the new kernel and boot archive.
8736bc8bc6aSSherry Moore  * - Allocate memory for the new kernel and boot archive.
8746bc8bc6aSSherry Moore  * - Allocate memory for page tables necessary for mapping the memory
8756bc8bc6aSSherry Moore  *   allocated for the files.
8766bc8bc6aSSherry Moore  * - Read the new kernel and boot archive into memory.
8776bc8bc6aSSherry Moore  * - Map in the fast reboot switcher.
8786bc8bc6aSSherry Moore  * - Load the fast reboot switcher to FASTBOOT_SWTCH_PA.
8796bc8bc6aSSherry Moore  * - Build the new multiboot_info structure
8806bc8bc6aSSherry Moore  * - Build page tables for the low 1G of physical memory.
8816bc8bc6aSSherry Moore  * - Mark the data structure as valid if all steps have succeeded.
8826bc8bc6aSSherry Moore  */
8836bc8bc6aSSherry Moore void
fastboot_load_kernel(char * mdep)884753a6d45SSherry Moore fastboot_load_kernel(char *mdep)
8856bc8bc6aSSherry Moore {
8866bc8bc6aSSherry Moore 	void		*buf = NULL;
8876bc8bc6aSSherry Moore 	int		i;
8886bc8bc6aSSherry Moore 	fastboot_file_t	*fb;
8896bc8bc6aSSherry Moore 	uint32_t	dboot_start_offset;
8906bc8bc6aSSherry Moore 	char		kern_bootpath[OBP_MAXPATHLEN];
8916bc8bc6aSSherry Moore 	extern uintptr_t postbootkernelbase;
892753a6d45SSherry Moore 	uintptr_t	saved_kernelbase;
8936bc8bc6aSSherry Moore 	int		bootpath_len = 0;
8946bc8bc6aSSherry Moore 	int		is_failsafe = 0;
8956bc8bc6aSSherry Moore 	int		is_retry = 0;
8966bc8bc6aSSherry Moore 	uint64_t	end_addr;
8976bc8bc6aSSherry Moore 
898c90a5fbeSSherry Moore 	if (!fastreboot_capable)
899c90a5fbeSSherry Moore 		return;
9006bc8bc6aSSherry Moore 
901753a6d45SSherry Moore 	if (newkernel.fi_valid)
902753a6d45SSherry Moore 		fastboot_free_newkernel(&newkernel);
903753a6d45SSherry Moore 
904753a6d45SSherry Moore 	saved_kernelbase = postbootkernelbase;
905753a6d45SSherry Moore 
9066bc8bc6aSSherry Moore 	postbootkernelbase = 0;
9076bc8bc6aSSherry Moore 
9086bc8bc6aSSherry Moore 	/*
9096bc8bc6aSSherry Moore 	 * Initialize various HAT related fields in the data structure
9106bc8bc6aSSherry Moore 	 */
9116bc8bc6aSSherry Moore 	fastboot_init_fields(&newkernel);
9126bc8bc6aSSherry Moore 
9136bc8bc6aSSherry Moore 	bzero(kern_bootpath, OBP_MAXPATHLEN);
9146bc8bc6aSSherry Moore 
9156bc8bc6aSSherry Moore 	/*
9166bc8bc6aSSherry Moore 	 * Process the boot argument
9176bc8bc6aSSherry Moore 	 */
918753a6d45SSherry Moore 	bzero(fastboot_args, OBP_MAXPATHLEN);
919753a6d45SSherry Moore 	fastboot_parse_mdep(mdep, kern_bootpath, &bootpath_len, fastboot_args);
92019397407SSherry Moore 
92119397407SSherry Moore 	/*
92219397407SSherry Moore 	 * Make sure we get the null character
92319397407SSherry Moore 	 */
92419397407SSherry Moore 	bcopy(kern_bootpath, fastboot_filename[FASTBOOT_NAME_UNIX],
92519397407SSherry Moore 	    bootpath_len);
92619397407SSherry Moore 	bcopy(kern_bootfile,
92719397407SSherry Moore 	    &fastboot_filename[FASTBOOT_NAME_UNIX][bootpath_len],
92819397407SSherry Moore 	    strlen(kern_bootfile) + 1);
92919397407SSherry Moore 
93019397407SSherry Moore 	bcopy(kern_bootpath, fastboot_filename[FASTBOOT_NAME_BOOTARCHIVE],
93119397407SSherry Moore 	    bootpath_len);
93219397407SSherry Moore 
933753a6d45SSherry Moore 	if (bcmp(kern_bootfile, FAILSAFE_BOOTFILE32,
934753a6d45SSherry Moore 	    (sizeof (FAILSAFE_BOOTFILE32) - 1)) == 0 ||
935753a6d45SSherry Moore 	    bcmp(kern_bootfile, FAILSAFE_BOOTFILE64,
936753a6d45SSherry Moore 	    (sizeof (FAILSAFE_BOOTFILE64) - 1)) == 0) {
93719397407SSherry Moore 		is_failsafe = 1;
93819397407SSherry Moore 	}
93919397407SSherry Moore 
9406bc8bc6aSSherry Moore load_kernel_retry:
94119397407SSherry Moore 	/*
94219397407SSherry Moore 	 * Read in unix and boot_archive
94319397407SSherry Moore 	 */
9446bc8bc6aSSherry Moore 	end_addr = DBOOT_ENTRY_ADDRESS;
94519397407SSherry Moore 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
9466bc8bc6aSSherry Moore 		struct _buf	*file;
9476bc8bc6aSSherry Moore 		uintptr_t	va;
94819397407SSherry Moore 		uint64_t	fsize;
94919397407SSherry Moore 		size_t		fsize_roundup, pt_size;
95019397407SSherry Moore 		int		page_index;
95119397407SSherry Moore 		uintptr_t	offset;
95219397407SSherry Moore 		ddi_dma_attr_t dma_attr = fastboot_dma_attr;
95319397407SSherry Moore 
9546bc8bc6aSSherry Moore 
95519397407SSherry Moore 		dprintf("fastboot_filename[%d] = %s\n",
95619397407SSherry Moore 		    i, fastboot_filename[i]);
95719397407SSherry Moore 
95819397407SSherry Moore 		if ((file = kobj_open_file(fastboot_filename[i])) ==
95919397407SSherry Moore 		    (struct _buf *)-1) {
960a2491ff4SSherry Moore 			cmn_err(CE_NOTE, "!Fastboot: Couldn't open %s",
96119397407SSherry Moore 			    fastboot_filename[i]);
96219397407SSherry Moore 			goto err_out;
96319397407SSherry Moore 		}
96419397407SSherry Moore 
96519397407SSherry Moore 		if (kobj_get_filesize(file, &fsize) != 0) {
966a2491ff4SSherry Moore 			cmn_err(CE_NOTE,
967a2491ff4SSherry Moore 			    "!Fastboot: Couldn't get filesize for %s",
96819397407SSherry Moore 			    fastboot_filename[i]);
96919397407SSherry Moore 			goto err_out;
97019397407SSherry Moore 		}
97119397407SSherry Moore 
9726bc8bc6aSSherry Moore 		fsize_roundup = P2ROUNDUP_TYPED(fsize, PAGESIZE, size_t);
9736bc8bc6aSSherry Moore 
9746bc8bc6aSSherry Moore 		/*
9756bc8bc6aSSherry Moore 		 * Where the files end in physical memory after being
9766bc8bc6aSSherry Moore 		 * relocated by the fast boot switcher.
9776bc8bc6aSSherry Moore 		 */
9786bc8bc6aSSherry Moore 		end_addr += fsize_roundup;
9796bc8bc6aSSherry Moore 		if (end_addr > fastboot_below_1G_dma_attr.dma_attr_addr_hi) {
980a2491ff4SSherry Moore 			cmn_err(CE_NOTE, "!Fastboot: boot archive is too big");
9816bc8bc6aSSherry Moore 			goto err_out;
98219397407SSherry Moore 		}
98319397407SSherry Moore 
9846bc8bc6aSSherry Moore 		/*
9856bc8bc6aSSherry Moore 		 * Adjust dma_attr_addr_lo so that the new kernel and boot
9866bc8bc6aSSherry Moore 		 * archive will not be overridden during relocation.
9876bc8bc6aSSherry Moore 		 */
9886bc8bc6aSSherry Moore 		if (end_addr > fastboot_dma_attr.dma_attr_addr_lo ||
9896bc8bc6aSSherry Moore 		    end_addr > fastboot_below_1G_dma_attr.dma_attr_addr_lo) {
9906bc8bc6aSSherry Moore 
9916bc8bc6aSSherry Moore 			if (is_retry) {
9926bc8bc6aSSherry Moore 				/*
9936bc8bc6aSSherry Moore 				 * If we have already tried and didn't succeed,
9946bc8bc6aSSherry Moore 				 * just give up.
9956bc8bc6aSSherry Moore 				 */
996a2491ff4SSherry Moore 				cmn_err(CE_NOTE,
997a2491ff4SSherry Moore 				    "!Fastboot: boot archive is too big");
9986bc8bc6aSSherry Moore 				goto err_out;
9996bc8bc6aSSherry Moore 			} else {
10006bc8bc6aSSherry Moore 				/* Set the flag so we don't keep retrying */
10016bc8bc6aSSherry Moore 				is_retry++;
10026bc8bc6aSSherry Moore 
10036bc8bc6aSSherry Moore 				/* Adjust dma_attr_addr_lo */
10046bc8bc6aSSherry Moore 				fastboot_dma_attr.dma_attr_addr_lo = end_addr;
10056bc8bc6aSSherry Moore 				fastboot_below_1G_dma_attr.dma_attr_addr_lo =
10066bc8bc6aSSherry Moore 				    end_addr;
10076bc8bc6aSSherry Moore 
10086bc8bc6aSSherry Moore 				/*
10096bc8bc6aSSherry Moore 				 * Free the memory we have already allocated
10106bc8bc6aSSherry Moore 				 * whose physical addresses might not fit
10116bc8bc6aSSherry Moore 				 * the new lo and hi constraints.
10126bc8bc6aSSherry Moore 				 */
1013753a6d45SSherry Moore 				fastboot_free_mem(&newkernel, end_addr);
10146bc8bc6aSSherry Moore 				goto load_kernel_retry;
10156bc8bc6aSSherry Moore 			}
10166bc8bc6aSSherry Moore 		}
10176bc8bc6aSSherry Moore 
10186bc8bc6aSSherry Moore 
101919397407SSherry Moore 		if (!fastboot_contig)
102019397407SSherry Moore 			dma_attr.dma_attr_sgllen = (fsize / PAGESIZE) +
102119397407SSherry Moore 			    (((fsize % PAGESIZE) == 0) ? 0 : 1);
102219397407SSherry Moore 
102319397407SSherry Moore 		if ((buf = contig_alloc(fsize, &dma_attr, PAGESIZE, 0))
102419397407SSherry Moore 		    == NULL) {
1025a2491ff4SSherry Moore 			cmn_err(CE_NOTE, fastboot_enomem_msg, fsize, "64G");
102619397407SSherry Moore 			goto err_out;
102719397407SSherry Moore 		}
102819397407SSherry Moore 
102919397407SSherry Moore 		va = P2ROUNDUP_TYPED((uintptr_t)buf, PAGESIZE, uintptr_t);
103019397407SSherry Moore 
103119397407SSherry Moore 		if (kobj_read_file(file, (char *)va, fsize, 0) < 0) {
1032a2491ff4SSherry Moore 			cmn_err(CE_NOTE, "!Fastboot: Couldn't read %s",
103319397407SSherry Moore 			    fastboot_filename[i]);
103419397407SSherry Moore 			goto err_out;
103519397407SSherry Moore 		}
103619397407SSherry Moore 
103719397407SSherry Moore 		fb = &newkernel.fi_files[i];
103819397407SSherry Moore 		fb->fb_va = va;
103919397407SSherry Moore 		fb->fb_size = fsize;
104019397407SSherry Moore 		fb->fb_sectcnt = 0;
104119397407SSherry Moore 
1042753a6d45SSherry Moore 		pt_size = FASTBOOT_PTE_LIST_SIZE(fsize_roundup);
104319397407SSherry Moore 
1044753a6d45SSherry Moore 		/*
1045753a6d45SSherry Moore 		 * If we have reserved memory but it not enough, free it.
1046753a6d45SSherry Moore 		 */
1047753a6d45SSherry Moore 		if (fb->fb_pte_list_size && fb->fb_pte_list_size < pt_size) {
1048753a6d45SSherry Moore 			contig_free((void *)fb->fb_pte_list_va,
1049753a6d45SSherry Moore 			    fb->fb_pte_list_size);
1050753a6d45SSherry Moore 			fb->fb_pte_list_size = 0;
1051753a6d45SSherry Moore 		}
1052753a6d45SSherry Moore 
1053753a6d45SSherry Moore 		if (fb->fb_pte_list_size == 0) {
105419397407SSherry Moore 			if ((fb->fb_pte_list_va =
105519397407SSherry Moore 			    (x86pte_t *)contig_alloc(pt_size,
1056753a6d45SSherry Moore 			    &fastboot_below_1G_dma_attr, PAGESIZE, 0))
1057753a6d45SSherry Moore 			    == NULL) {
1058a2491ff4SSherry Moore 				cmn_err(CE_NOTE, fastboot_enomem_msg,
105919397407SSherry Moore 				    (uint64_t)pt_size, "1G");
106019397407SSherry Moore 				goto err_out;
106119397407SSherry Moore 			}
1062753a6d45SSherry Moore 			/*
1063753a6d45SSherry Moore 			 * fb_pte_list_size must be set after the allocation
1064753a6d45SSherry Moore 			 * succeeds as it's used to determine how much memory to
1065753a6d45SSherry Moore 			 * free.
1066753a6d45SSherry Moore 			 */
1067753a6d45SSherry Moore 			fb->fb_pte_list_size = pt_size;
1068753a6d45SSherry Moore 		}
106919397407SSherry Moore 
1070753a6d45SSherry Moore 		bzero((void *)(fb->fb_pte_list_va), fb->fb_pte_list_size);
107119397407SSherry Moore 
107219397407SSherry Moore 		fb->fb_pte_list_pa = mmu_ptob((uint64_t)hat_getpfnum(kas.a_hat,
107319397407SSherry Moore 		    (caddr_t)fb->fb_pte_list_va));
107419397407SSherry Moore 
107519397407SSherry Moore 		for (page_index = 0, offset = 0; offset < fb->fb_size;
107619397407SSherry Moore 		    offset += PAGESIZE) {
107719397407SSherry Moore 			uint64_t paddr;
107819397407SSherry Moore 
107919397407SSherry Moore 			paddr = mmu_ptob((uint64_t)hat_getpfnum(kas.a_hat,
108019397407SSherry Moore 			    (caddr_t)fb->fb_va + offset));
108119397407SSherry Moore 
108219397407SSherry Moore 			ASSERT(paddr >= fastboot_dma_attr.dma_attr_addr_lo);
108319397407SSherry Moore 
108419397407SSherry Moore 			/*
108519397407SSherry Moore 			 * Include the pte_bits so we don't have to make
108619397407SSherry Moore 			 * it in assembly.
108719397407SSherry Moore 			 */
108819397407SSherry Moore 			fb->fb_pte_list_va[page_index++] = (x86pte_t)
108919397407SSherry Moore 			    (paddr | pte_bits);
109019397407SSherry Moore 		}
109119397407SSherry Moore 
109219397407SSherry Moore 		fb->fb_pte_list_va[page_index] = FASTBOOT_TERMINATE;
109319397407SSherry Moore 
109419397407SSherry Moore 		if (i == FASTBOOT_UNIX) {
10956bc8bc6aSSherry Moore 			Ehdr	*ehdr = (Ehdr *)va;
10966bc8bc6aSSherry Moore 			int	j;
109719397407SSherry Moore 
109819397407SSherry Moore 			/*
109919397407SSherry Moore 			 * Sanity checks:
110019397407SSherry Moore 			 */
110119397407SSherry Moore 			for (j = 0; j < SELFMAG; j++) {
110219397407SSherry Moore 				if (ehdr->e_ident[j] != ELFMAG[j]) {
1103a2491ff4SSherry Moore 					cmn_err(CE_NOTE, "!Fastboot: Bad ELF "
110419397407SSherry Moore 					    "signature");
110519397407SSherry Moore 					goto err_out;
110619397407SSherry Moore 				}
110719397407SSherry Moore 			}
110819397407SSherry Moore 
110919397407SSherry Moore 			if (ehdr->e_ident[EI_CLASS] == ELFCLASS32 &&
111019397407SSherry Moore 			    ehdr->e_ident[EI_DATA] == ELFDATA2LSB &&
111119397407SSherry Moore 			    ehdr->e_machine == EM_386) {
111219397407SSherry Moore 
1113877400d3SKonstantin Ananyev 				fb->fb_sectcnt = sizeof (fb->fb_sections) /
1114877400d3SKonstantin Ananyev 				    sizeof (fb->fb_sections[0]);
1115877400d3SKonstantin Ananyev 
111619397407SSherry Moore 				if (fastboot_elf32_find_loadables((void *)va,
111719397407SSherry Moore 				    fsize, &fb->fb_sections[0],
111819397407SSherry Moore 				    &fb->fb_sectcnt, &dboot_start_offset) < 0) {
1119a2491ff4SSherry Moore 					cmn_err(CE_NOTE, "!Fastboot: ELF32 "
112019397407SSherry Moore 					    "program section failure");
112119397407SSherry Moore 					goto err_out;
112219397407SSherry Moore 				}
112319397407SSherry Moore 
112419397407SSherry Moore 				if (fb->fb_sectcnt == 0) {
1125a2491ff4SSherry Moore 					cmn_err(CE_NOTE, "!Fastboot: No ELF32 "
112619397407SSherry Moore 					    "program sections found");
112719397407SSherry Moore 					goto err_out;
112819397407SSherry Moore 				}
112919397407SSherry Moore 
113019397407SSherry Moore 				if (is_failsafe) {
113119397407SSherry Moore 					/* Failsafe boot_archive */
1132753a6d45SSherry Moore 					bcopy(BOOTARCHIVE32_FAILSAFE,
113319397407SSherry Moore 					    &fastboot_filename
113419397407SSherry Moore 					    [FASTBOOT_NAME_BOOTARCHIVE]
113519397407SSherry Moore 					    [bootpath_len],
1136753a6d45SSherry Moore 					    sizeof (BOOTARCHIVE32_FAILSAFE));
113719397407SSherry Moore 				} else {
113819397407SSherry Moore 					bcopy(BOOTARCHIVE32,
113919397407SSherry Moore 					    &fastboot_filename
114019397407SSherry Moore 					    [FASTBOOT_NAME_BOOTARCHIVE]
114119397407SSherry Moore 					    [bootpath_len],
114219397407SSherry Moore 					    sizeof (BOOTARCHIVE32));
114319397407SSherry Moore 				}
114419397407SSherry Moore 
114519397407SSherry Moore 			} else if (ehdr->e_ident[EI_CLASS] == ELFCLASS64 &&
114619397407SSherry Moore 			    ehdr->e_ident[EI_DATA] == ELFDATA2LSB &&
114719397407SSherry Moore 			    ehdr->e_machine == EM_AMD64) {
114819397407SSherry Moore 
114919397407SSherry Moore 				if (fastboot_elf64_find_dboot_load_offset(
115019397407SSherry Moore 				    (void *)va, fsize, &dboot_start_offset)
115119397407SSherry Moore 				    != 0) {
1152a2491ff4SSherry Moore 					cmn_err(CE_NOTE, "!Fastboot: Couldn't "
115319397407SSherry Moore 					    "find ELF64 dboot entry offset");
115419397407SSherry Moore 					goto err_out;
115519397407SSherry Moore 				}
115619397407SSherry Moore 
1157*7417cfdeSKuriakose Kuruvilla 				if (!is_x86_feature(x86_featureset,
1158*7417cfdeSKuriakose Kuruvilla 				    X86FSET_64) ||
1159*7417cfdeSKuriakose Kuruvilla 				    !is_x86_feature(x86_featureset,
1160*7417cfdeSKuriakose Kuruvilla 				    X86FSET_PAE)) {
1161*7417cfdeSKuriakose Kuruvilla 					cmn_err(CE_NOTE, "Fastboot: Cannot "
116219397407SSherry Moore 					    "reboot to %s: "
116319397407SSherry Moore 					    "not a 64-bit capable system",
116419397407SSherry Moore 					    kern_bootfile);
116519397407SSherry Moore 					goto err_out;
116619397407SSherry Moore 				}
116719397407SSherry Moore 
1168753a6d45SSherry Moore 				if (is_failsafe) {
1169753a6d45SSherry Moore 					/* Failsafe boot_archive */
1170753a6d45SSherry Moore 					bcopy(BOOTARCHIVE64_FAILSAFE,
1171753a6d45SSherry Moore 					    &fastboot_filename
1172753a6d45SSherry Moore 					    [FASTBOOT_NAME_BOOTARCHIVE]
1173753a6d45SSherry Moore 					    [bootpath_len],
1174753a6d45SSherry Moore 					    sizeof (BOOTARCHIVE64_FAILSAFE));
1175753a6d45SSherry Moore 				} else {
117619397407SSherry Moore 					bcopy(BOOTARCHIVE64,
117719397407SSherry Moore 					    &fastboot_filename
1178753a6d45SSherry Moore 					    [FASTBOOT_NAME_BOOTARCHIVE]
1179753a6d45SSherry Moore 					    [bootpath_len],
118019397407SSherry Moore 					    sizeof (BOOTARCHIVE64));
1181753a6d45SSherry Moore 				}
118219397407SSherry Moore 			} else {
1183a2491ff4SSherry Moore 				cmn_err(CE_NOTE, "!Fastboot: Unknown ELF type");
118419397407SSherry Moore 				goto err_out;
118519397407SSherry Moore 			}
118619397407SSherry Moore 
118719397407SSherry Moore 			fb->fb_dest_pa = DBOOT_ENTRY_ADDRESS -
118819397407SSherry Moore 			    dboot_start_offset;
118919397407SSherry Moore 
119019397407SSherry Moore 			fb->fb_next_pa = DBOOT_ENTRY_ADDRESS + fsize_roundup;
119119397407SSherry Moore 		} else {
119219397407SSherry Moore 			fb->fb_dest_pa = newkernel.fi_files[i - 1].fb_next_pa;
119319397407SSherry Moore 			fb->fb_next_pa = fb->fb_dest_pa + fsize_roundup;
119419397407SSherry Moore 		}
119519397407SSherry Moore 
119619397407SSherry Moore 		kobj_close_file(file);
119719397407SSherry Moore 
11986bc8bc6aSSherry Moore 	}
11996bc8bc6aSSherry Moore 
120019397407SSherry Moore 	/*
120119397407SSherry Moore 	 * Add the function that will switch us to 32-bit protected mode
120219397407SSherry Moore 	 */
120319397407SSherry Moore 	fb = &newkernel.fi_files[FASTBOOT_SWTCH];
120419397407SSherry Moore 	fb->fb_va = fb->fb_dest_pa = FASTBOOT_SWTCH_PA;
1205877400d3SKonstantin Ananyev 	fb->fb_size = MMU_PAGESIZE;
120619397407SSherry Moore 
1207753a6d45SSherry Moore 	hat_devload(kas.a_hat, (caddr_t)fb->fb_va,
1208753a6d45SSherry Moore 	    MMU_PAGESIZE, mmu_btop(fb->fb_dest_pa),
1209753a6d45SSherry Moore 	    PROT_READ | PROT_WRITE | PROT_EXEC,
1210753a6d45SSherry Moore 	    HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK);
121119397407SSherry Moore 
121219397407SSherry Moore 	/*
121319397407SSherry Moore 	 * Build the new multiboot_info structure
121419397407SSherry Moore 	 */
1215753a6d45SSherry Moore 	if (fastboot_build_mbi(fastboot_args, &newkernel) != 0) {
121619397407SSherry Moore 		goto err_out;
121719397407SSherry Moore 	}
121819397407SSherry Moore 
121919397407SSherry Moore 	/*
122019397407SSherry Moore 	 * Build page table for low 1G physical memory. Use big pages.
1221877400d3SKonstantin Ananyev 	 * Allocate 4 (5 for amd64) pages for the page tables.
1222877400d3SKonstantin Ananyev 	 *    1 page for PML4 (amd64)
122319397407SSherry Moore 	 *    1 page for Page-Directory-Pointer Table
1224877400d3SKonstantin Ananyev 	 *    2 pages for Page Directory
122519397407SSherry Moore 	 *    1 page for Page Table.
122619397407SSherry Moore 	 * The page table entry will be rewritten to map the physical
122719397407SSherry Moore 	 * address as we do the copying.
122819397407SSherry Moore 	 */
122919397407SSherry Moore 	if (newkernel.fi_has_pae) {
1230877400d3SKonstantin Ananyev #ifdef	__amd64
1231877400d3SKonstantin Ananyev 		size_t size = MMU_PAGESIZE * 5;
1232877400d3SKonstantin Ananyev #else
123319397407SSherry Moore 		size_t size = MMU_PAGESIZE * 4;
1234877400d3SKonstantin Ananyev #endif	/* __amd64 */
123519397407SSherry Moore 
1236753a6d45SSherry Moore 		if (newkernel.fi_pagetable_size && newkernel.fi_pagetable_size
1237753a6d45SSherry Moore 		    < size) {
1238753a6d45SSherry Moore 			contig_free((void *)newkernel.fi_pagetable_va,
1239753a6d45SSherry Moore 			    newkernel.fi_pagetable_size);
1240753a6d45SSherry Moore 			newkernel.fi_pagetable_size = 0;
1241753a6d45SSherry Moore 		}
1242753a6d45SSherry Moore 
1243753a6d45SSherry Moore 		if (newkernel.fi_pagetable_size == 0) {
124419397407SSherry Moore 			if ((newkernel.fi_pagetable_va = (uintptr_t)
124519397407SSherry Moore 			    contig_alloc(size, &fastboot_below_1G_dma_attr,
1246877400d3SKonstantin Ananyev 			    MMU_PAGESIZE, 0)) == NULL) {
1247a2491ff4SSherry Moore 				cmn_err(CE_NOTE, fastboot_enomem_msg,
124819397407SSherry Moore 				    (uint64_t)size, "1G");
124919397407SSherry Moore 				goto err_out;
125019397407SSherry Moore 			}
1251753a6d45SSherry Moore 			/*
1252753a6d45SSherry Moore 			 * fi_pagetable_size must be set after the allocation
1253753a6d45SSherry Moore 			 * succeeds as it's used to determine how much memory to
1254753a6d45SSherry Moore 			 * free.
1255753a6d45SSherry Moore 			 */
1256753a6d45SSherry Moore 			newkernel.fi_pagetable_size = size;
1257753a6d45SSherry Moore 		}
125819397407SSherry Moore 
125919397407SSherry Moore 		bzero((void *)(newkernel.fi_pagetable_va), size);
126019397407SSherry Moore 
126119397407SSherry Moore 		newkernel.fi_pagetable_pa =
126219397407SSherry Moore 		    mmu_ptob((uint64_t)hat_getpfnum(kas.a_hat,
126319397407SSherry Moore 		    (caddr_t)newkernel.fi_pagetable_va));
126419397407SSherry Moore 
126519397407SSherry Moore 		newkernel.fi_last_table_pa = newkernel.fi_pagetable_pa +
1266877400d3SKonstantin Ananyev 		    size - MMU_PAGESIZE;
126719397407SSherry Moore 
126819397407SSherry Moore 		newkernel.fi_next_table_va = newkernel.fi_pagetable_va +
126919397407SSherry Moore 		    MMU_PAGESIZE;
127019397407SSherry Moore 		newkernel.fi_next_table_pa = newkernel.fi_pagetable_pa +
127119397407SSherry Moore 		    MMU_PAGESIZE;
127219397407SSherry Moore 
127319397407SSherry Moore 		fastboot_build_pagetables(&newkernel);
127419397407SSherry Moore 	}
127519397407SSherry Moore 
127619397407SSherry Moore 
1277753a6d45SSherry Moore 	/* Generate MD5 checksums */
1278753a6d45SSherry Moore 	fastboot_cksum_generate(&newkernel);
1279753a6d45SSherry Moore 
128019397407SSherry Moore 	/* Mark it as valid */
128119397407SSherry Moore 	newkernel.fi_valid = 1;
128219397407SSherry Moore 	newkernel.fi_magic = FASTBOOT_MAGIC;
128319397407SSherry Moore 
1284753a6d45SSherry Moore 	postbootkernelbase = saved_kernelbase;
128519397407SSherry Moore 	return;
128619397407SSherry Moore 
128719397407SSherry Moore err_out:
1288753a6d45SSherry Moore 	postbootkernelbase = saved_kernelbase;
128919397407SSherry Moore 	newkernel.fi_valid = 0;
1290753a6d45SSherry Moore 	fastboot_free_newkernel(&newkernel);
1291753a6d45SSherry Moore }
1292753a6d45SSherry Moore 
1293753a6d45SSherry Moore 
1294753a6d45SSherry Moore /* ARGSUSED */
1295753a6d45SSherry Moore static int
fastboot_xc_func(fastboot_info_t * nk,xc_arg_t unused2,xc_arg_t unused3)1296753a6d45SSherry Moore fastboot_xc_func(fastboot_info_t *nk, xc_arg_t unused2, xc_arg_t unused3)
1297753a6d45SSherry Moore {
1298753a6d45SSherry Moore 	void (*fastboot_func)(fastboot_info_t *);
1299753a6d45SSherry Moore 	fastboot_file_t	*fb = &nk->fi_files[FASTBOOT_SWTCH];
1300753a6d45SSherry Moore 	fastboot_func = (void (*)())(fb->fb_va);
1301753a6d45SSherry Moore 	kthread_t *t_intr = curthread->t_intr;
1302753a6d45SSherry Moore 
1303753a6d45SSherry Moore 	if (&kas != curproc->p_as) {
1304753a6d45SSherry Moore 		hat_devload(curproc->p_as->a_hat, (caddr_t)fb->fb_va,
1305753a6d45SSherry Moore 		    MMU_PAGESIZE, mmu_btop(fb->fb_dest_pa),
1306753a6d45SSherry Moore 		    PROT_READ | PROT_WRITE | PROT_EXEC,
1307753a6d45SSherry Moore 		    HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK);
1308753a6d45SSherry Moore 	}
1309753a6d45SSherry Moore 
1310753a6d45SSherry Moore 	/*
1311753a6d45SSherry Moore 	 * If we have pinned a thread, make sure the address is mapped
1312753a6d45SSherry Moore 	 * in the address space of the pinned thread.
1313753a6d45SSherry Moore 	 */
1314753a6d45SSherry Moore 	if (t_intr && t_intr->t_procp->p_as->a_hat != curproc->p_as->a_hat &&
1315753a6d45SSherry Moore 	    t_intr->t_procp->p_as != &kas)
1316753a6d45SSherry Moore 		hat_devload(t_intr->t_procp->p_as->a_hat, (caddr_t)fb->fb_va,
1317753a6d45SSherry Moore 		    MMU_PAGESIZE, mmu_btop(fb->fb_dest_pa),
1318753a6d45SSherry Moore 		    PROT_READ | PROT_WRITE | PROT_EXEC,
1319753a6d45SSherry Moore 		    HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK);
1320753a6d45SSherry Moore 
1321753a6d45SSherry Moore 	(*psm_shutdownf)(A_SHUTDOWN, AD_FASTREBOOT);
1322753a6d45SSherry Moore 	(*fastboot_func)(nk);
1323753a6d45SSherry Moore 
1324753a6d45SSherry Moore 	/*NOTREACHED*/
1325753a6d45SSherry Moore 	return (0);
132619397407SSherry Moore }
132719397407SSherry Moore 
13286bc8bc6aSSherry Moore /*
13296bc8bc6aSSherry Moore  * Jump to the fast reboot switcher.  This function never returns.
13306bc8bc6aSSherry Moore  */
133119397407SSherry Moore void
fast_reboot()133219397407SSherry Moore fast_reboot()
133319397407SSherry Moore {
1334753a6d45SSherry Moore 	processorid_t bootcpuid = 0;
1335753a6d45SSherry Moore 	extern uintptr_t postbootkernelbase;
1336753a6d45SSherry Moore 	extern char	fb_swtch_image[];
1337753a6d45SSherry Moore 	fastboot_file_t	*fb;
1338753a6d45SSherry Moore 	int i;
133919397407SSherry Moore 
1340753a6d45SSherry Moore 	postbootkernelbase = 0;
1341753a6d45SSherry Moore 
1342753a6d45SSherry Moore 	fb = &newkernel.fi_files[FASTBOOT_SWTCH];
1343753a6d45SSherry Moore 
1344753a6d45SSherry Moore 	/*
1345753a6d45SSherry Moore 	 * Map the address into both the current proc's address
1346753a6d45SSherry Moore 	 * space and the kernel's address space in case the panic
1347753a6d45SSherry Moore 	 * is forced by kmdb.
1348753a6d45SSherry Moore 	 */
1349753a6d45SSherry Moore 	if (&kas != curproc->p_as) {
1350753a6d45SSherry Moore 		hat_devload(curproc->p_as->a_hat, (caddr_t)fb->fb_va,
1351753a6d45SSherry Moore 		    MMU_PAGESIZE, mmu_btop(fb->fb_dest_pa),
1352753a6d45SSherry Moore 		    PROT_READ | PROT_WRITE | PROT_EXEC,
1353753a6d45SSherry Moore 		    HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK);
1354753a6d45SSherry Moore 	}
1355753a6d45SSherry Moore 
1356753a6d45SSherry Moore 	bcopy((void *)fb_swtch_image, (void *)fb->fb_va, fb->fb_size);
1357753a6d45SSherry Moore 
1358753a6d45SSherry Moore 
1359753a6d45SSherry Moore 	/*
1360753a6d45SSherry Moore 	 * Set fb_va to fake_va
1361753a6d45SSherry Moore 	 */
1362753a6d45SSherry Moore 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
1363753a6d45SSherry Moore 		newkernel.fi_files[i].fb_va = fake_va;
1364753a6d45SSherry Moore 
1365753a6d45SSherry Moore 	}
1366753a6d45SSherry Moore 
1367753a6d45SSherry Moore 	if (panicstr && CPU->cpu_id != bootcpuid &&
1368753a6d45SSherry Moore 	    CPU_ACTIVE(cpu_get(bootcpuid))) {
1369f34a7178SJoe Bonasera 		extern void panic_idle(void);
1370753a6d45SSherry Moore 		cpuset_t cpuset;
1371753a6d45SSherry Moore 
1372753a6d45SSherry Moore 		CPUSET_ZERO(cpuset);
1373753a6d45SSherry Moore 		CPUSET_ADD(cpuset, bootcpuid);
1374f34a7178SJoe Bonasera 		xc_priority((xc_arg_t)&newkernel, 0, 0, CPUSET2BV(cpuset),
1375753a6d45SSherry Moore 		    (xc_func_t)fastboot_xc_func);
1376753a6d45SSherry Moore 
1377f34a7178SJoe Bonasera 		panic_idle();
1378753a6d45SSherry Moore 	} else
1379753a6d45SSherry Moore 		(void) fastboot_xc_func(&newkernel, 0, 0);
1380753a6d45SSherry Moore }
1381753a6d45SSherry Moore 
1382753a6d45SSherry Moore 
1383753a6d45SSherry Moore /*
1384753a6d45SSherry Moore  * Get boot property value for fastreboot_onpanic.
1385753a6d45SSherry Moore  *
1386753a6d45SSherry Moore  * NOTE: If fastreboot_onpanic is set to non-zero in /etc/system,
1387753a6d45SSherry Moore  * new setting passed in via "-B fastreboot_onpanic" is ignored.
1388753a6d45SSherry Moore  * This order of precedence is to enable developers debugging panics
1389753a6d45SSherry Moore  * that occur early in boot to utilize Fast Reboot on panic.
1390753a6d45SSherry Moore  */
1391753a6d45SSherry Moore static void
fastboot_get_bootprop(void)1392753a6d45SSherry Moore fastboot_get_bootprop(void)
1393753a6d45SSherry Moore {
1394753a6d45SSherry Moore 	int		val = 0xaa, len, ret;
1395753a6d45SSherry Moore 	dev_info_t	*devi;
1396753a6d45SSherry Moore 	char		*propstr = NULL;
1397753a6d45SSherry Moore 
1398753a6d45SSherry Moore 	devi = ddi_root_node();
1399753a6d45SSherry Moore 
1400753a6d45SSherry Moore 	ret = ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
1401753a6d45SSherry Moore 	    FASTREBOOT_ONPANIC, &propstr);
1402753a6d45SSherry Moore 
1403753a6d45SSherry Moore 	if (ret == DDI_PROP_SUCCESS) {
1404753a6d45SSherry Moore 		if (FASTREBOOT_ONPANIC_NOTSET(propstr))
1405753a6d45SSherry Moore 			val = 0;
1406753a6d45SSherry Moore 		else if (FASTREBOOT_ONPANIC_ISSET(propstr))
1407753a6d45SSherry Moore 			val = UA_FASTREBOOT_ONPANIC;
1408753a6d45SSherry Moore 
1409753a6d45SSherry Moore 		/*
1410753a6d45SSherry Moore 		 * Only set fastreboot_onpanic to the value passed in
1411753a6d45SSherry Moore 		 * if it's not already set to non-zero, and the value
1412753a6d45SSherry Moore 		 * has indeed been passed in via command line.
1413753a6d45SSherry Moore 		 */
1414753a6d45SSherry Moore 		if (!fastreboot_onpanic && val != 0xaa)
1415753a6d45SSherry Moore 			fastreboot_onpanic = val;
1416753a6d45SSherry Moore 		ddi_prop_free(propstr);
1417753a6d45SSherry Moore 	} else if (ret != DDI_PROP_NOT_FOUND && ret != DDI_PROP_UNDEFINED) {
1418a2491ff4SSherry Moore 		cmn_err(CE_NOTE, "!%s value is invalid, will be ignored",
1419753a6d45SSherry Moore 		    FASTREBOOT_ONPANIC);
1420753a6d45SSherry Moore 	}
1421753a6d45SSherry Moore 
1422753a6d45SSherry Moore 	len = sizeof (fastreboot_onpanic_cmdline);
1423753a6d45SSherry Moore 	ret = ddi_getlongprop_buf(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
1424753a6d45SSherry Moore 	    FASTREBOOT_ONPANIC_CMDLINE, fastreboot_onpanic_cmdline, &len);
1425753a6d45SSherry Moore 
1426753a6d45SSherry Moore 	if (ret == DDI_PROP_BUF_TOO_SMALL)
1427a2491ff4SSherry Moore 		cmn_err(CE_NOTE, "!%s value is too long, will be ignored",
1428753a6d45SSherry Moore 		    FASTREBOOT_ONPANIC_CMDLINE);
1429753a6d45SSherry Moore }
1430753a6d45SSherry Moore 
1431753a6d45SSherry Moore /*
1432753a6d45SSherry Moore  * This function is called by main() to either load the backup kernel for panic
1433753a6d45SSherry Moore  * fast reboot, or to reserve low physical memory for fast reboot.
1434753a6d45SSherry Moore  */
1435753a6d45SSherry Moore void
fastboot_post_startup()1436753a6d45SSherry Moore fastboot_post_startup()
1437753a6d45SSherry Moore {
1438835b9930SSherry Moore 	lbolt_at_boot = ddi_get_lbolt();
1439835b9930SSherry Moore 
1440835b9930SSherry Moore 	/* Default to 10 minutes */
1441835b9930SSherry Moore 	if (fastreboot_onpanic_uptime == LONG_MAX)
1442835b9930SSherry Moore 		fastreboot_onpanic_uptime = SEC_TO_TICK(10 * 60);
1443835b9930SSherry Moore 
1444753a6d45SSherry Moore 	if (!fastreboot_capable)
1445753a6d45SSherry Moore 		return;
1446753a6d45SSherry Moore 
1447c90a5fbeSSherry Moore 	mutex_enter(&fastreboot_config_mutex);
1448c90a5fbeSSherry Moore 
1449753a6d45SSherry Moore 	fastboot_get_bootprop();
1450753a6d45SSherry Moore 
1451753a6d45SSherry Moore 	if (fastreboot_onpanic)
1452753a6d45SSherry Moore 		fastboot_load_kernel(fastreboot_onpanic_cmdline);
1453753a6d45SSherry Moore 	else if (reserve_mem_enabled)
1454753a6d45SSherry Moore 		fastboot_reserve_mem(&newkernel);
1455c90a5fbeSSherry Moore 
1456c90a5fbeSSherry Moore 	mutex_exit(&fastreboot_config_mutex);
1457753a6d45SSherry Moore }
1458753a6d45SSherry Moore 
1459753a6d45SSherry Moore /*
1460753a6d45SSherry Moore  * Update boot configuration settings.
1461753a6d45SSherry Moore  * If the new fastreboot_onpanic setting is false, and a kernel has
1462753a6d45SSherry Moore  * been preloaded, free the memory;
1463753a6d45SSherry Moore  * if the new fastreboot_onpanic setting is true and newkernel is
1464753a6d45SSherry Moore  * not valid, load the new kernel.
1465753a6d45SSherry Moore  */
1466753a6d45SSherry Moore void
fastboot_update_config(const char * mdep)1467753a6d45SSherry Moore fastboot_update_config(const char *mdep)
1468753a6d45SSherry Moore {
1469753a6d45SSherry Moore 	uint8_t boot_config = (uint8_t)*mdep;
1470c90a5fbeSSherry Moore 	int cur_fastreboot_onpanic;
1471753a6d45SSherry Moore 
1472753a6d45SSherry Moore 	if (!fastreboot_capable)
1473753a6d45SSherry Moore 		return;
1474753a6d45SSherry Moore 
1475c90a5fbeSSherry Moore 	mutex_enter(&fastreboot_config_mutex);
1476c90a5fbeSSherry Moore 
1477c90a5fbeSSherry Moore 	cur_fastreboot_onpanic = fastreboot_onpanic;
1478753a6d45SSherry Moore 	fastreboot_onpanic = boot_config & UA_FASTREBOOT_ONPANIC;
1479c90a5fbeSSherry Moore 
1480753a6d45SSherry Moore 	if (fastreboot_onpanic && (!cur_fastreboot_onpanic ||
1481753a6d45SSherry Moore 	    !newkernel.fi_valid))
1482753a6d45SSherry Moore 		fastboot_load_kernel(fastreboot_onpanic_cmdline);
1483753a6d45SSherry Moore 	if (cur_fastreboot_onpanic && !fastreboot_onpanic)
1484753a6d45SSherry Moore 		fastboot_free_newkernel(&newkernel);
1485c90a5fbeSSherry Moore 
1486c90a5fbeSSherry Moore 	mutex_exit(&fastreboot_config_mutex);
1487c90a5fbeSSherry Moore }
1488c90a5fbeSSherry Moore 
1489c90a5fbeSSherry Moore /*
14905ee8e422SKonstantin Ananyev  * This is an internal interface to disable Fast Reboot on Panic.
14915ee8e422SKonstantin Ananyev  * It frees up memory allocated for the backup kernel and sets
14925ee8e422SKonstantin Ananyev  * fastreboot_onpanic to zero.
1493c90a5fbeSSherry Moore  */
14945ee8e422SKonstantin Ananyev static void
fastreboot_onpanic_disable(void)14955ee8e422SKonstantin Ananyev fastreboot_onpanic_disable(void)
1496c90a5fbeSSherry Moore {
1497c90a5fbeSSherry Moore 	uint8_t boot_config = (uint8_t)(~UA_FASTREBOOT_ONPANIC);
1498c90a5fbeSSherry Moore 	fastboot_update_config((const char *)&boot_config);
1499c90a5fbeSSherry Moore }
1500c90a5fbeSSherry Moore 
1501c90a5fbeSSherry Moore /*
1502c90a5fbeSSherry Moore  * This is the interface to be called by fm_panic() in case FMA has diagnosed
1503c90a5fbeSSherry Moore  * a terminal machine check exception.  It does not free up memory allocated
1504c90a5fbeSSherry Moore  * for the backup kernel.  General disabling fastreboot_onpanic in a
15055ee8e422SKonstantin Ananyev  * non-panicking situation must go through fastboot_onpanic_disable().
1506c90a5fbeSSherry Moore  */
1507c90a5fbeSSherry Moore void
fastreboot_disable_highpil(void)15085ee8e422SKonstantin Ananyev fastreboot_disable_highpil(void)
1509c90a5fbeSSherry Moore {
1510c90a5fbeSSherry Moore 	fastreboot_onpanic = 0;
1511c90a5fbeSSherry Moore }
1512c90a5fbeSSherry Moore 
15135ee8e422SKonstantin Ananyev /*
15145ee8e422SKonstantin Ananyev  * This is an internal interface to disable Fast Reboot by Default.
15155ee8e422SKonstantin Ananyev  * It does not free up memory allocated for the backup kernel.
15165ee8e422SKonstantin Ananyev  */
15175ee8e422SKonstantin Ananyev static void
fastreboot_capable_disable(uint32_t msgid)15185ee8e422SKonstantin Ananyev fastreboot_capable_disable(uint32_t msgid)
15195ee8e422SKonstantin Ananyev {
15205ee8e422SKonstantin Ananyev 	if (fastreboot_capable != 0) {
15215ee8e422SKonstantin Ananyev 		fastreboot_capable = 0;
15225ee8e422SKonstantin Ananyev 		if (msgid < sizeof (fastreboot_nosup_desc) /
15235ee8e422SKonstantin Ananyev 		    sizeof (fastreboot_nosup_desc[0]))
15245ee8e422SKonstantin Ananyev 			fastreboot_nosup_id = msgid;
15255ee8e422SKonstantin Ananyev 		else
15265ee8e422SKonstantin Ananyev 			fastreboot_nosup_id = FBNS_DEFAULT;
15275ee8e422SKonstantin Ananyev 	}
15285ee8e422SKonstantin Ananyev }
15295ee8e422SKonstantin Ananyev 
15305ee8e422SKonstantin Ananyev /*
15315ee8e422SKonstantin Ananyev  * This is the kernel interface for disabling
15325ee8e422SKonstantin Ananyev  * Fast Reboot by Default and Fast Reboot on Panic.
15335ee8e422SKonstantin Ananyev  * Frees up memory allocated for the backup kernel.
15345ee8e422SKonstantin Ananyev  * General disabling of the Fast Reboot by Default feature should be done
15355ee8e422SKonstantin Ananyev  * via the userland interface scf_fastreboot_default_set_transient().
15365ee8e422SKonstantin Ananyev  */
15375ee8e422SKonstantin Ananyev void
fastreboot_disable(uint32_t msgid)15385ee8e422SKonstantin Ananyev fastreboot_disable(uint32_t msgid)
15395ee8e422SKonstantin Ananyev {
15405ee8e422SKonstantin Ananyev 	fastreboot_capable_disable(msgid);
15415ee8e422SKonstantin Ananyev 	fastreboot_onpanic_disable();
15425ee8e422SKonstantin Ananyev }
15435ee8e422SKonstantin Ananyev 
15445ee8e422SKonstantin Ananyev /*
15455ee8e422SKonstantin Ananyev  * Returns Fast Reboot not support message for fastreboot_nosup_id.
15465ee8e422SKonstantin Ananyev  * If fastreboot_nosup_id contains invalid index, default
15475ee8e422SKonstantin Ananyev  * Fast Reboot not support message is returned.
15485ee8e422SKonstantin Ananyev  */
15495ee8e422SKonstantin Ananyev const char *
fastreboot_nosup_message(void)15505ee8e422SKonstantin Ananyev fastreboot_nosup_message(void)
15515ee8e422SKonstantin Ananyev {
15525ee8e422SKonstantin Ananyev 	uint32_t msgid;
15535ee8e422SKonstantin Ananyev 
15545ee8e422SKonstantin Ananyev 	msgid = fastreboot_nosup_id;
15555ee8e422SKonstantin Ananyev 	if (msgid >= sizeof (fastreboot_nosup_desc) /
15565ee8e422SKonstantin Ananyev 	    sizeof (fastreboot_nosup_desc[0]))
15575ee8e422SKonstantin Ananyev 		msgid = FBNS_DEFAULT;
15585ee8e422SKonstantin Ananyev 
15595ee8e422SKonstantin Ananyev 	return (fastreboot_nosup_desc[msgid]);
15605ee8e422SKonstantin Ananyev }
1561c90a5fbeSSherry Moore 
1562c90a5fbeSSherry Moore /*
1563c90a5fbeSSherry Moore  * A simplified interface for uadmin to call to update the configuration
1564c90a5fbeSSherry Moore  * setting and load a new kernel if necessary.
1565c90a5fbeSSherry Moore  */
1566c90a5fbeSSherry Moore void
fastboot_update_and_load(int fcn,char * mdep)1567c90a5fbeSSherry Moore fastboot_update_and_load(int fcn, char *mdep)
1568c90a5fbeSSherry Moore {
1569c90a5fbeSSherry Moore 	if (fcn != AD_FASTREBOOT) {
1570c90a5fbeSSherry Moore 		/*
1571c90a5fbeSSherry Moore 		 * If user has explicitly requested reboot to prom,
1572c90a5fbeSSherry Moore 		 * or uadmin(1M) was invoked with other functions,
1573c90a5fbeSSherry Moore 		 * don't try to fast reboot after dumping.
1574c90a5fbeSSherry Moore 		 */
15755ee8e422SKonstantin Ananyev 		fastreboot_onpanic_disable();
1576c90a5fbeSSherry Moore 	}
1577c90a5fbeSSherry Moore 
1578c90a5fbeSSherry Moore 	mutex_enter(&fastreboot_config_mutex);
1579c90a5fbeSSherry Moore 
1580c90a5fbeSSherry Moore 	if (fastreboot_onpanic)
1581c90a5fbeSSherry Moore 		fastboot_load_kernel(mdep);
1582c90a5fbeSSherry Moore 
1583c90a5fbeSSherry Moore 	mutex_exit(&fastreboot_config_mutex);
158419397407SSherry Moore }
1585