xref: /onnv-gate/usr/src/uts/i86pc/os/fastboot.c (revision 11686:ee97d7ce1deb)
17656SSherry.Moore@Sun.COM /*
27656SSherry.Moore@Sun.COM  * CDDL HEADER START
37656SSherry.Moore@Sun.COM  *
47656SSherry.Moore@Sun.COM  * The contents of this file are subject to the terms of the
57656SSherry.Moore@Sun.COM  * Common Development and Distribution License (the "License").
67656SSherry.Moore@Sun.COM  * You may not use this file except in compliance with the License.
77656SSherry.Moore@Sun.COM  *
87656SSherry.Moore@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97656SSherry.Moore@Sun.COM  * or http://www.opensolaris.org/os/licensing.
107656SSherry.Moore@Sun.COM  * See the License for the specific language governing permissions
117656SSherry.Moore@Sun.COM  * and limitations under the License.
127656SSherry.Moore@Sun.COM  *
137656SSherry.Moore@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
147656SSherry.Moore@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157656SSherry.Moore@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
167656SSherry.Moore@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
177656SSherry.Moore@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
187656SSherry.Moore@Sun.COM  *
197656SSherry.Moore@Sun.COM  * CDDL HEADER END
207656SSherry.Moore@Sun.COM  */
217656SSherry.Moore@Sun.COM 
227656SSherry.Moore@Sun.COM /*
23*11686SKonstantin.Ananyev@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
247656SSherry.Moore@Sun.COM  * Use is subject to license terms.
257656SSherry.Moore@Sun.COM  */
267656SSherry.Moore@Sun.COM 
277750SSherry.Moore@Sun.COM /*
287750SSherry.Moore@Sun.COM  * This file contains the functions for performing Fast Reboot -- a
297750SSherry.Moore@Sun.COM  * reboot which bypasses the firmware and bootloader, considerably
307750SSherry.Moore@Sun.COM  * reducing downtime.
317750SSherry.Moore@Sun.COM  *
329160SSherry.Moore@Sun.COM  * fastboot_load_kernel(): This function is invoked by mdpreboot() in the
339160SSherry.Moore@Sun.COM  * reboot path.  It loads the new kernel and boot archive into memory, builds
347750SSherry.Moore@Sun.COM  * the data structure containing sufficient information about the new
357750SSherry.Moore@Sun.COM  * kernel and boot archive to be passed to the fast reboot switcher
367750SSherry.Moore@Sun.COM  * (see fb_swtch_src.s for details).  When invoked the switcher relocates
377750SSherry.Moore@Sun.COM  * the new kernel and boot archive to physically contiguous low memory,
387750SSherry.Moore@Sun.COM  * similar to where the boot loader would have loaded them, and jumps to
397750SSherry.Moore@Sun.COM  * the new kernel.
407750SSherry.Moore@Sun.COM  *
419160SSherry.Moore@Sun.COM  * If fastreboot_onpanic is enabled, fastboot_load_kernel() is called
429160SSherry.Moore@Sun.COM  * by fastreboot_post_startup() to load the back up kernel in case of
439160SSherry.Moore@Sun.COM  * panic.
449160SSherry.Moore@Sun.COM  *
457750SSherry.Moore@Sun.COM  * The physical addresses of the memory allocated for the new kernel, boot
467750SSherry.Moore@Sun.COM  * archive and their page tables must be above where the boot archive ends
477750SSherry.Moore@Sun.COM  * after it has been relocated by the switcher, otherwise the new files
487750SSherry.Moore@Sun.COM  * and their page tables could be overridden during relocation.
497750SSherry.Moore@Sun.COM  *
507750SSherry.Moore@Sun.COM  * fast_reboot(): This function is invoked by mdboot() once it's determined
517750SSherry.Moore@Sun.COM  * that the system is capable of fast reboot.  It jumps to the fast reboot
529160SSherry.Moore@Sun.COM  * switcher with the data structure built by fastboot_load_kernel() as the
539160SSherry.Moore@Sun.COM  * argument.
547750SSherry.Moore@Sun.COM  */
557656SSherry.Moore@Sun.COM 
567656SSherry.Moore@Sun.COM #include <sys/types.h>
577656SSherry.Moore@Sun.COM #include <sys/param.h>
587656SSherry.Moore@Sun.COM #include <sys/segments.h>
597656SSherry.Moore@Sun.COM #include <sys/sysmacros.h>
607656SSherry.Moore@Sun.COM #include <sys/vm.h>
617656SSherry.Moore@Sun.COM 
627656SSherry.Moore@Sun.COM #include <sys/proc.h>
637656SSherry.Moore@Sun.COM #include <sys/buf.h>
647656SSherry.Moore@Sun.COM #include <sys/kmem.h>
657656SSherry.Moore@Sun.COM 
667656SSherry.Moore@Sun.COM #include <sys/reboot.h>
677656SSherry.Moore@Sun.COM #include <sys/uadmin.h>
687656SSherry.Moore@Sun.COM 
697656SSherry.Moore@Sun.COM #include <sys/cred.h>
707656SSherry.Moore@Sun.COM #include <sys/vnode.h>
717656SSherry.Moore@Sun.COM #include <sys/file.h>
727656SSherry.Moore@Sun.COM 
737656SSherry.Moore@Sun.COM #include <sys/cmn_err.h>
747656SSherry.Moore@Sun.COM #include <sys/dumphdr.h>
757656SSherry.Moore@Sun.COM #include <sys/bootconf.h>
767656SSherry.Moore@Sun.COM #include <sys/ddidmareq.h>
777656SSherry.Moore@Sun.COM #include <sys/varargs.h>
787656SSherry.Moore@Sun.COM #include <sys/promif.h>
797656SSherry.Moore@Sun.COM #include <sys/modctl.h>
807656SSherry.Moore@Sun.COM 
817656SSherry.Moore@Sun.COM #include <vm/hat.h>
827656SSherry.Moore@Sun.COM #include <vm/as.h>
837656SSherry.Moore@Sun.COM #include <vm/page.h>
847656SSherry.Moore@Sun.COM #include <vm/seg.h>
857656SSherry.Moore@Sun.COM #include <vm/hat_i86.h>
867656SSherry.Moore@Sun.COM #include <sys/vm_machparam.h>
877656SSherry.Moore@Sun.COM #include <sys/archsystm.h>
887656SSherry.Moore@Sun.COM #include <sys/machsystm.h>
897656SSherry.Moore@Sun.COM #include <sys/mman.h>
907656SSherry.Moore@Sun.COM #include <sys/x86_archext.h>
919160SSherry.Moore@Sun.COM #include <sys/smp_impldefs.h>
929160SSherry.Moore@Sun.COM #include <sys/spl.h>
937656SSherry.Moore@Sun.COM 
94*11686SKonstantin.Ananyev@Sun.COM #include <sys/fastboot_impl.h>
957656SSherry.Moore@Sun.COM #include <sys/machelf.h>
967656SSherry.Moore@Sun.COM #include <sys/kobj.h>
977656SSherry.Moore@Sun.COM #include <sys/multiboot.h>
989160SSherry.Moore@Sun.COM #include <sys/kobj_lex.h>
999160SSherry.Moore@Sun.COM 
1009160SSherry.Moore@Sun.COM /*
1019160SSherry.Moore@Sun.COM  * Macro to determine how many pages are needed for PTEs to map a particular
1029160SSherry.Moore@Sun.COM  * file.  Allocate one extra page table entry for terminating the list.
1039160SSherry.Moore@Sun.COM  */
1049160SSherry.Moore@Sun.COM #define	FASTBOOT_PTE_LIST_SIZE(fsize)	\
1059160SSherry.Moore@Sun.COM 	P2ROUNDUP((((fsize) >> PAGESHIFT) + 1) * sizeof (x86pte_t), PAGESIZE)
1067656SSherry.Moore@Sun.COM 
1077750SSherry.Moore@Sun.COM /*
1087750SSherry.Moore@Sun.COM  * Data structure containing necessary information for the fast reboot
1097750SSherry.Moore@Sun.COM  * switcher to jump to the new kernel.
1107750SSherry.Moore@Sun.COM  */
1117656SSherry.Moore@Sun.COM fastboot_info_t newkernel = { 0 };
1129160SSherry.Moore@Sun.COM char		fastboot_args[OBP_MAXPATHLEN];
1137750SSherry.Moore@Sun.COM 
1147656SSherry.Moore@Sun.COM static char fastboot_filename[2][OBP_MAXPATHLEN] = { { 0 }, { 0 }};
1157656SSherry.Moore@Sun.COM static x86pte_t ptp_bits = PT_VALID | PT_REF | PT_USER | PT_WRITABLE;
1167656SSherry.Moore@Sun.COM static x86pte_t pte_bits =
1177656SSherry.Moore@Sun.COM     PT_VALID | PT_REF | PT_MOD | PT_NOCONSIST | PT_WRITABLE;
1187656SSherry.Moore@Sun.COM static uint_t fastboot_shift_amt_pae[] = {12, 21, 30, 39};
1197656SSherry.Moore@Sun.COM 
120*11686SKonstantin.Ananyev@Sun.COM /* Index into Fast Reboot not supported message array */
121*11686SKonstantin.Ananyev@Sun.COM static uint32_t fastreboot_nosup_id = FBNS_DEFAULT;
122*11686SKonstantin.Ananyev@Sun.COM 
123*11686SKonstantin.Ananyev@Sun.COM /* Fast Reboot not supported message array */
124*11686SKonstantin.Ananyev@Sun.COM static const char * const fastreboot_nosup_desc[FBNS_END] = {
125*11686SKonstantin.Ananyev@Sun.COM #define	fastboot_nosup_msg(id, str)	str,
126*11686SKonstantin.Ananyev@Sun.COM #include <sys/fastboot_msg.h>
127*11686SKonstantin.Ananyev@Sun.COM };
128*11686SKonstantin.Ananyev@Sun.COM 
1297656SSherry.Moore@Sun.COM int fastboot_debug = 0;
1307656SSherry.Moore@Sun.COM int fastboot_contig = 0;
1317656SSherry.Moore@Sun.COM 
1327656SSherry.Moore@Sun.COM /*
1337656SSherry.Moore@Sun.COM  * Fake starting va for new kernel and boot archive.
1347656SSherry.Moore@Sun.COM  */
1357656SSherry.Moore@Sun.COM static uintptr_t fake_va = FASTBOOT_FAKE_VA;
1367656SSherry.Moore@Sun.COM 
1377656SSherry.Moore@Sun.COM /*
1389160SSherry.Moore@Sun.COM  * Reserve memory below PA 1G in preparation of fast reboot.
1399160SSherry.Moore@Sun.COM  *
1409160SSherry.Moore@Sun.COM  * This variable is only checked when fastreboot_capable is set, but
1419160SSherry.Moore@Sun.COM  * fastreboot_onpanic is not set.  The amount of memory reserved
1429160SSherry.Moore@Sun.COM  * is negligible, but just in case we are really short of low memory,
1439160SSherry.Moore@Sun.COM  * this variable will give us a backdoor to not consume memory at all.
1449160SSherry.Moore@Sun.COM  */
1459160SSherry.Moore@Sun.COM int reserve_mem_enabled = 1;
1469160SSherry.Moore@Sun.COM 
1479160SSherry.Moore@Sun.COM /*
14810559SSherry.Moore@Sun.COM  * Mutex to protect fastreboot_onpanic.
14910559SSherry.Moore@Sun.COM  */
15010559SSherry.Moore@Sun.COM kmutex_t fastreboot_config_mutex;
15110559SSherry.Moore@Sun.COM 
15210559SSherry.Moore@Sun.COM /*
1539160SSherry.Moore@Sun.COM  * Amount of memory below PA 1G to reserve for constructing the multiboot
1549160SSherry.Moore@Sun.COM  * data structure and the page tables as we tend to run out of those
1559160SSherry.Moore@Sun.COM  * when more drivers are loaded.
1569160SSherry.Moore@Sun.COM  */
1579160SSherry.Moore@Sun.COM static size_t fastboot_mbi_size = 0x2000;	/* 8K */
1589160SSherry.Moore@Sun.COM static size_t fastboot_pagetable_size = 0x5000;	/* 20K */
1599160SSherry.Moore@Sun.COM 
1609160SSherry.Moore@Sun.COM /*
16110916SSherry.Moore@Sun.COM  * Minimum system uptime in clock_t before Fast Reboot should be used
16210916SSherry.Moore@Sun.COM  * on panic.  Will be initialized in fastboot_post_startup().
16310916SSherry.Moore@Sun.COM  */
16410916SSherry.Moore@Sun.COM clock_t fastreboot_onpanic_uptime = LONG_MAX;
16510916SSherry.Moore@Sun.COM 
16610916SSherry.Moore@Sun.COM /*
16710916SSherry.Moore@Sun.COM  * lbolt value when the system booted.  This value will be used if the system
16810916SSherry.Moore@Sun.COM  * panics to calculate how long the system has been up.  If the uptime is less
16910916SSherry.Moore@Sun.COM  * than fastreboot_onpanic_uptime, a reboot through BIOS will be performed to
17010916SSherry.Moore@Sun.COM  * avoid a potential panic/reboot loop.
17110916SSherry.Moore@Sun.COM  */
17210916SSherry.Moore@Sun.COM clock_t lbolt_at_boot = LONG_MAX;
17310916SSherry.Moore@Sun.COM 
17410916SSherry.Moore@Sun.COM /*
1759160SSherry.Moore@Sun.COM  * Use below 1G for page tables as
1769160SSherry.Moore@Sun.COM  *	1. we are only doing 1:1 mapping of the bottom 1G of physical memory.
1779160SSherry.Moore@Sun.COM  *	2. we are using 2G as the fake virtual address for the new kernel and
1789160SSherry.Moore@Sun.COM  *	boot archive.
1797656SSherry.Moore@Sun.COM  */
1807656SSherry.Moore@Sun.COM static ddi_dma_attr_t fastboot_below_1G_dma_attr = {
1817656SSherry.Moore@Sun.COM 	DMA_ATTR_V0,
1827656SSherry.Moore@Sun.COM 	0x0000000008000000ULL,	/* dma_attr_addr_lo: 128MB */
1837656SSherry.Moore@Sun.COM 	0x000000003FFFFFFFULL,	/* dma_attr_addr_hi: 1G */
1847656SSherry.Moore@Sun.COM 	0x00000000FFFFFFFFULL,	/* dma_attr_count_max */
1857656SSherry.Moore@Sun.COM 	0x0000000000001000ULL,	/* dma_attr_align: 4KB */
1867656SSherry.Moore@Sun.COM 	1,			/* dma_attr_burstsize */
1877656SSherry.Moore@Sun.COM 	1,			/* dma_attr_minxfer */
1887656SSherry.Moore@Sun.COM 	0x00000000FFFFFFFFULL,	/* dma_attr_maxxfer */
1897656SSherry.Moore@Sun.COM 	0x00000000FFFFFFFFULL,	/* dma_attr_seg */
1907656SSherry.Moore@Sun.COM 	1,			/* dma_attr_sgllen */
1917656SSherry.Moore@Sun.COM 	0x1000ULL,		/* dma_attr_granular */
1927656SSherry.Moore@Sun.COM 	0,			/* dma_attr_flags */
1937656SSherry.Moore@Sun.COM };
1947656SSherry.Moore@Sun.COM 
1957656SSherry.Moore@Sun.COM static ddi_dma_attr_t fastboot_dma_attr = {
1967656SSherry.Moore@Sun.COM 	DMA_ATTR_V0,
1977656SSherry.Moore@Sun.COM 	0x0000000008000000ULL,	/* dma_attr_addr_lo: 128MB */
1988151SKonstantin.Ananyev@Sun.COM #ifdef	__amd64
1998151SKonstantin.Ananyev@Sun.COM 	0xFFFFFFFFFFFFFFFFULL,	/* dma_attr_addr_hi: 2^64B */
2008151SKonstantin.Ananyev@Sun.COM #else
2017656SSherry.Moore@Sun.COM 	0x0000000FFFFFFFFFULL,	/* dma_attr_addr_hi: 64GB */
2028151SKonstantin.Ananyev@Sun.COM #endif	/* __amd64 */
2037656SSherry.Moore@Sun.COM 	0x00000000FFFFFFFFULL,	/* dma_attr_count_max */
2047656SSherry.Moore@Sun.COM 	0x0000000000001000ULL,	/* dma_attr_align: 4KB */
2057656SSherry.Moore@Sun.COM 	1,			/* dma_attr_burstsize */
2067656SSherry.Moore@Sun.COM 	1,			/* dma_attr_minxfer */
2077656SSherry.Moore@Sun.COM 	0x00000000FFFFFFFFULL,	/* dma_attr_maxxfer */
2087656SSherry.Moore@Sun.COM 	0x00000000FFFFFFFFULL,	/* dma_attr_seg */
2097656SSherry.Moore@Sun.COM 	1,			/* dma_attr_sgllen */
2107656SSherry.Moore@Sun.COM 	0x1000ULL,		/* dma_attr_granular */
2117656SSherry.Moore@Sun.COM 	0,			/* dma_attr_flags */
2127656SSherry.Moore@Sun.COM };
2137656SSherry.Moore@Sun.COM 
2147656SSherry.Moore@Sun.COM /*
2157656SSherry.Moore@Sun.COM  * Various information saved from the previous boot to reconstruct
2167656SSherry.Moore@Sun.COM  * multiboot_info.
2177656SSherry.Moore@Sun.COM  */
2187656SSherry.Moore@Sun.COM extern multiboot_info_t saved_mbi;
2197656SSherry.Moore@Sun.COM extern mb_memory_map_t saved_mmap[FASTBOOT_SAVED_MMAP_COUNT];
22010525SKonstantin.Ananyev@Sun.COM extern uint8_t saved_drives[FASTBOOT_SAVED_DRIVES_SIZE];
2217656SSherry.Moore@Sun.COM extern char saved_cmdline[FASTBOOT_SAVED_CMDLINE_LEN];
2227656SSherry.Moore@Sun.COM extern int saved_cmdline_len;
2239160SSherry.Moore@Sun.COM extern size_t saved_file_size[];
2247656SSherry.Moore@Sun.COM 
2257656SSherry.Moore@Sun.COM extern void* contig_alloc(size_t size, ddi_dma_attr_t *attr,
2267656SSherry.Moore@Sun.COM     uintptr_t align, int cansleep);
2277750SSherry.Moore@Sun.COM extern void contig_free(void *addr, size_t size);
2287750SSherry.Moore@Sun.COM 
2297656SSherry.Moore@Sun.COM 
2307656SSherry.Moore@Sun.COM /* PRINTLIKE */
2317656SSherry.Moore@Sun.COM extern void vprintf(const char *, va_list);
2327656SSherry.Moore@Sun.COM 
2337656SSherry.Moore@Sun.COM 
2347656SSherry.Moore@Sun.COM /*
2357656SSherry.Moore@Sun.COM  * Need to be able to get boot_archives from other places
2367656SSherry.Moore@Sun.COM  */
2377656SSherry.Moore@Sun.COM #define	BOOTARCHIVE64	"/platform/i86pc/amd64/boot_archive"
2387656SSherry.Moore@Sun.COM #define	BOOTARCHIVE32	"/platform/i86pc/boot_archive"
2399160SSherry.Moore@Sun.COM #define	BOOTARCHIVE32_FAILSAFE	"/boot/x86.miniroot-safe"
2409160SSherry.Moore@Sun.COM #define	BOOTARCHIVE64_FAILSAFE	"/boot/amd64/x86.miniroot-safe"
2419160SSherry.Moore@Sun.COM #define	FAILSAFE_BOOTFILE32	"/boot/platform/i86pc/kernel/unix"
2429160SSherry.Moore@Sun.COM #define	FAILSAFE_BOOTFILE64	"/boot/platform/i86pc/kernel/amd64/unix"
2437656SSherry.Moore@Sun.COM 
2447656SSherry.Moore@Sun.COM static uint_t fastboot_vatoindex(fastboot_info_t *, uintptr_t, int);
2457656SSherry.Moore@Sun.COM static void fastboot_map_with_size(fastboot_info_t *, uintptr_t,
2467656SSherry.Moore@Sun.COM     paddr_t, size_t, int);
2477656SSherry.Moore@Sun.COM static void fastboot_build_pagetables(fastboot_info_t *);
2487656SSherry.Moore@Sun.COM static int fastboot_build_mbi(char *, fastboot_info_t *);
2499160SSherry.Moore@Sun.COM static void fastboot_free_file(fastboot_file_t *);
2507656SSherry.Moore@Sun.COM 
25110970SSherry.Moore@Sun.COM static const char fastboot_enomem_msg[] = "!Fastboot: Couldn't allocate 0x%"
2527656SSherry.Moore@Sun.COM 	PRIx64" bytes below %s to do fast reboot";
2537656SSherry.Moore@Sun.COM 
2547656SSherry.Moore@Sun.COM static void
2557656SSherry.Moore@Sun.COM dprintf(char *fmt, ...)
2567656SSherry.Moore@Sun.COM {
2577656SSherry.Moore@Sun.COM 	va_list adx;
2587656SSherry.Moore@Sun.COM 
2597656SSherry.Moore@Sun.COM 	if (!fastboot_debug)
2607656SSherry.Moore@Sun.COM 		return;
2617656SSherry.Moore@Sun.COM 
2627656SSherry.Moore@Sun.COM 	va_start(adx, fmt);
2637656SSherry.Moore@Sun.COM 	vprintf(fmt, adx);
2647656SSherry.Moore@Sun.COM 	va_end(adx);
2657656SSherry.Moore@Sun.COM }
2667656SSherry.Moore@Sun.COM 
2677656SSherry.Moore@Sun.COM 
2687656SSherry.Moore@Sun.COM /*
2697656SSherry.Moore@Sun.COM  * Return the index corresponding to a virt address at a given page table level.
2707656SSherry.Moore@Sun.COM  */
2717656SSherry.Moore@Sun.COM static uint_t
2727656SSherry.Moore@Sun.COM fastboot_vatoindex(fastboot_info_t *nk, uintptr_t va, int level)
2737656SSherry.Moore@Sun.COM {
2747656SSherry.Moore@Sun.COM 	return ((va >> nk->fi_shift_amt[level]) & (nk->fi_ptes_per_table - 1));
2757656SSherry.Moore@Sun.COM }
2767656SSherry.Moore@Sun.COM 
2777656SSherry.Moore@Sun.COM 
2787656SSherry.Moore@Sun.COM /*
2797656SSherry.Moore@Sun.COM  * Add mapping from vstart to pstart for the specified size.
2808151SKonstantin.Ananyev@Sun.COM  * vstart, pstart and size should all have been aligned at 2M boundaries.
2817656SSherry.Moore@Sun.COM  */
2827656SSherry.Moore@Sun.COM static void
2837656SSherry.Moore@Sun.COM fastboot_map_with_size(fastboot_info_t *nk, uintptr_t vstart, paddr_t pstart,
2847656SSherry.Moore@Sun.COM     size_t size, int level)
2857656SSherry.Moore@Sun.COM {
2867656SSherry.Moore@Sun.COM 	x86pte_t	pteval, *table;
2877656SSherry.Moore@Sun.COM 	uintptr_t	vaddr;
2887656SSherry.Moore@Sun.COM 	paddr_t		paddr;
2897656SSherry.Moore@Sun.COM 	int		index, l;
2907656SSherry.Moore@Sun.COM 
2917656SSherry.Moore@Sun.COM 	table = (x86pte_t *)(nk->fi_pagetable_va);
2927656SSherry.Moore@Sun.COM 
2937656SSherry.Moore@Sun.COM 	for (l = nk->fi_top_level; l >= level; l--) {
2947656SSherry.Moore@Sun.COM 
2957656SSherry.Moore@Sun.COM 		index = fastboot_vatoindex(nk, vstart, l);
2967656SSherry.Moore@Sun.COM 
2977656SSherry.Moore@Sun.COM 		if (l == level) {
2987656SSherry.Moore@Sun.COM 			/*
2997656SSherry.Moore@Sun.COM 			 * Last level.  Program the page table entries.
3007656SSherry.Moore@Sun.COM 			 */
3017656SSherry.Moore@Sun.COM 			for (vaddr = vstart, paddr = pstart;
3027656SSherry.Moore@Sun.COM 			    vaddr < vstart + size;
3037656SSherry.Moore@Sun.COM 			    vaddr += (1ULL << nk->fi_shift_amt[l]),
3047656SSherry.Moore@Sun.COM 			    paddr += (1ULL << nk->fi_shift_amt[l])) {
3057656SSherry.Moore@Sun.COM 
3067656SSherry.Moore@Sun.COM 				uint_t index = fastboot_vatoindex(nk, vaddr, l);
3077656SSherry.Moore@Sun.COM 
3087656SSherry.Moore@Sun.COM 				if (l > 0)
3097656SSherry.Moore@Sun.COM 					pteval = paddr | pte_bits | PT_PAGESIZE;
3107656SSherry.Moore@Sun.COM 				else
3117656SSherry.Moore@Sun.COM 					pteval = paddr | pte_bits;
3127656SSherry.Moore@Sun.COM 
3137656SSherry.Moore@Sun.COM 				table[index] = pteval;
3147656SSherry.Moore@Sun.COM 			}
3157656SSherry.Moore@Sun.COM 		} else if (table[index] & PT_VALID) {
3167656SSherry.Moore@Sun.COM 
3177656SSherry.Moore@Sun.COM 			table = (x86pte_t *)
3187656SSherry.Moore@Sun.COM 			    ((uintptr_t)(((paddr_t)table[index] & MMU_PAGEMASK)
3197656SSherry.Moore@Sun.COM 			    - nk->fi_pagetable_pa) + nk->fi_pagetable_va);
3207656SSherry.Moore@Sun.COM 		} else {
3217656SSherry.Moore@Sun.COM 			/*
3228151SKonstantin.Ananyev@Sun.COM 			 * Intermediate levels.
3238151SKonstantin.Ananyev@Sun.COM 			 * Program with either valid bit or PTP bits.
3247656SSherry.Moore@Sun.COM 			 */
3257656SSherry.Moore@Sun.COM 			if (l == nk->fi_top_level) {
3268151SKonstantin.Ananyev@Sun.COM #ifdef	__amd64
3278151SKonstantin.Ananyev@Sun.COM 				ASSERT(nk->fi_top_level == 3);
3288151SKonstantin.Ananyev@Sun.COM 				table[index] = nk->fi_next_table_pa | ptp_bits;
3298151SKonstantin.Ananyev@Sun.COM #else
3307656SSherry.Moore@Sun.COM 				table[index] = nk->fi_next_table_pa | PT_VALID;
3318151SKonstantin.Ananyev@Sun.COM #endif	/* __amd64 */
3327656SSherry.Moore@Sun.COM 			} else {
3337656SSherry.Moore@Sun.COM 				table[index] = nk->fi_next_table_pa | ptp_bits;
3347656SSherry.Moore@Sun.COM 			}
3357656SSherry.Moore@Sun.COM 			table = (x86pte_t *)(nk->fi_next_table_va);
3367656SSherry.Moore@Sun.COM 			nk->fi_next_table_va += MMU_PAGESIZE;
3377656SSherry.Moore@Sun.COM 			nk->fi_next_table_pa += MMU_PAGESIZE;
3387656SSherry.Moore@Sun.COM 		}
3397656SSherry.Moore@Sun.COM 	}
3407656SSherry.Moore@Sun.COM }
3417656SSherry.Moore@Sun.COM 
3427656SSherry.Moore@Sun.COM /*
3437656SSherry.Moore@Sun.COM  * Build page tables for the lower 1G of physical memory using 2M
3447656SSherry.Moore@Sun.COM  * pages, and prepare page tables for mapping new kernel and boot
3457656SSherry.Moore@Sun.COM  * archive pages using 4K pages.
3467656SSherry.Moore@Sun.COM  */
3477656SSherry.Moore@Sun.COM static void
3487656SSherry.Moore@Sun.COM fastboot_build_pagetables(fastboot_info_t *nk)
3497656SSherry.Moore@Sun.COM {
3507656SSherry.Moore@Sun.COM 	/*
3517656SSherry.Moore@Sun.COM 	 * Map lower 1G physical memory.  Use large pages.
3527656SSherry.Moore@Sun.COM 	 */
3537656SSherry.Moore@Sun.COM 	fastboot_map_with_size(nk, 0, 0, ONE_GIG, 1);
3547656SSherry.Moore@Sun.COM 
3557656SSherry.Moore@Sun.COM 	/*
3567656SSherry.Moore@Sun.COM 	 * Map one 4K page to get the middle page tables set up.
3577656SSherry.Moore@Sun.COM 	 */
3587656SSherry.Moore@Sun.COM 	fake_va = P2ALIGN_TYPED(fake_va, nk->fi_lpagesize, uintptr_t);
3597656SSherry.Moore@Sun.COM 	fastboot_map_with_size(nk, fake_va,
3607656SSherry.Moore@Sun.COM 	    nk->fi_files[0].fb_pte_list_va[0] & MMU_PAGEMASK, PAGESIZE, 0);
3617656SSherry.Moore@Sun.COM }
3627656SSherry.Moore@Sun.COM 
3637656SSherry.Moore@Sun.COM 
3647656SSherry.Moore@Sun.COM /*
3657656SSherry.Moore@Sun.COM  * Sanity check.  Look for dboot offset.
3667656SSherry.Moore@Sun.COM  */
3677656SSherry.Moore@Sun.COM static int
3687656SSherry.Moore@Sun.COM fastboot_elf64_find_dboot_load_offset(void *img, off_t imgsz, uint32_t *offp)
3697656SSherry.Moore@Sun.COM {
3707656SSherry.Moore@Sun.COM 	Elf64_Ehdr	*ehdr = (Elf64_Ehdr *)img;
3717656SSherry.Moore@Sun.COM 	Elf64_Phdr	*phdr;
3727656SSherry.Moore@Sun.COM 	uint8_t		*phdrbase;
3737656SSherry.Moore@Sun.COM 	int		i;
3747656SSherry.Moore@Sun.COM 
3757656SSherry.Moore@Sun.COM 	if ((ehdr->e_phoff + ehdr->e_phnum * ehdr->e_phentsize) >= imgsz)
3767656SSherry.Moore@Sun.COM 		return (-1);
3777656SSherry.Moore@Sun.COM 
3787656SSherry.Moore@Sun.COM 	phdrbase = (uint8_t *)img + ehdr->e_phoff;
3797656SSherry.Moore@Sun.COM 
3807656SSherry.Moore@Sun.COM 	for (i = 0; i < ehdr->e_phnum; i++) {
3817656SSherry.Moore@Sun.COM 		phdr = (Elf64_Phdr *)(phdrbase + ehdr->e_phentsize * i);
3827656SSherry.Moore@Sun.COM 
3837656SSherry.Moore@Sun.COM 		if (phdr->p_type == PT_LOAD) {
3847656SSherry.Moore@Sun.COM 			if (phdr->p_vaddr == phdr->p_paddr &&
3857656SSherry.Moore@Sun.COM 			    phdr->p_vaddr == DBOOT_ENTRY_ADDRESS) {
3867656SSherry.Moore@Sun.COM 				ASSERT(phdr->p_offset <= UINT32_MAX);
3877656SSherry.Moore@Sun.COM 				*offp = (uint32_t)phdr->p_offset;
3887656SSherry.Moore@Sun.COM 				return (0);
3897656SSherry.Moore@Sun.COM 			}
3907656SSherry.Moore@Sun.COM 		}
3917656SSherry.Moore@Sun.COM 	}
3927656SSherry.Moore@Sun.COM 
3937656SSherry.Moore@Sun.COM 	return (-1);
3947656SSherry.Moore@Sun.COM }
3957656SSherry.Moore@Sun.COM 
3967656SSherry.Moore@Sun.COM 
3977656SSherry.Moore@Sun.COM /*
3987656SSherry.Moore@Sun.COM  * Initialize text and data section information for 32-bit kernel.
3998151SKonstantin.Ananyev@Sun.COM  * sectcntp - is both input/output parameter.
4008151SKonstantin.Ananyev@Sun.COM  * On entry, *sectcntp contains maximum allowable number of sections;
4018151SKonstantin.Ananyev@Sun.COM  * on return, it contains the actual number of sections filled.
4027656SSherry.Moore@Sun.COM  */
4037656SSherry.Moore@Sun.COM static int
4047656SSherry.Moore@Sun.COM fastboot_elf32_find_loadables(void *img, off_t imgsz, fastboot_section_t *sectp,
4057656SSherry.Moore@Sun.COM     int *sectcntp, uint32_t *offp)
4067656SSherry.Moore@Sun.COM {
4077656SSherry.Moore@Sun.COM 	Elf32_Ehdr	*ehdr = (Elf32_Ehdr *)img;
4087656SSherry.Moore@Sun.COM 	Elf32_Phdr	*phdr;
4097656SSherry.Moore@Sun.COM 	uint8_t		*phdrbase;
4107656SSherry.Moore@Sun.COM 	int		i;
4117656SSherry.Moore@Sun.COM 	int		used_sections = 0;
4128151SKonstantin.Ananyev@Sun.COM 	const int	max_sectcnt = *sectcntp;
4137656SSherry.Moore@Sun.COM 
4147656SSherry.Moore@Sun.COM 	if ((ehdr->e_phoff + ehdr->e_phnum * ehdr->e_phentsize) >= imgsz)
4157656SSherry.Moore@Sun.COM 		return (-1);
4167656SSherry.Moore@Sun.COM 
4177656SSherry.Moore@Sun.COM 	phdrbase = (uint8_t *)img + ehdr->e_phoff;
4187656SSherry.Moore@Sun.COM 
4197656SSherry.Moore@Sun.COM 	for (i = 0; i < ehdr->e_phnum; i++) {
4207656SSherry.Moore@Sun.COM 		phdr = (Elf32_Phdr *)(phdrbase + ehdr->e_phentsize * i);
4217656SSherry.Moore@Sun.COM 
4227656SSherry.Moore@Sun.COM 		if (phdr->p_type == PT_INTERP)
4237656SSherry.Moore@Sun.COM 			return (-1);
4247656SSherry.Moore@Sun.COM 
4257656SSherry.Moore@Sun.COM 		if (phdr->p_type != PT_LOAD)
4267656SSherry.Moore@Sun.COM 			continue;
4277656SSherry.Moore@Sun.COM 
4287656SSherry.Moore@Sun.COM 		if (phdr->p_vaddr == phdr->p_paddr &&
4297656SSherry.Moore@Sun.COM 		    phdr->p_paddr == DBOOT_ENTRY_ADDRESS) {
4307656SSherry.Moore@Sun.COM 			*offp = (uint32_t)phdr->p_offset;
4317656SSherry.Moore@Sun.COM 		} else {
4328151SKonstantin.Ananyev@Sun.COM 			if (max_sectcnt <= used_sections)
4338151SKonstantin.Ananyev@Sun.COM 				return (-1);
4348151SKonstantin.Ananyev@Sun.COM 
4357656SSherry.Moore@Sun.COM 			sectp[used_sections].fb_sec_offset = phdr->p_offset;
4367656SSherry.Moore@Sun.COM 			sectp[used_sections].fb_sec_paddr = phdr->p_paddr;
4377656SSherry.Moore@Sun.COM 			sectp[used_sections].fb_sec_size = phdr->p_filesz;
4387656SSherry.Moore@Sun.COM 			sectp[used_sections].fb_sec_bss_size =
4397656SSherry.Moore@Sun.COM 			    (phdr->p_filesz < phdr->p_memsz) ?
4407656SSherry.Moore@Sun.COM 			    (phdr->p_memsz - phdr->p_filesz) : 0;
4417656SSherry.Moore@Sun.COM 
4428151SKonstantin.Ananyev@Sun.COM 			/* Extra sanity check for the input object file */
4438151SKonstantin.Ananyev@Sun.COM 			if (sectp[used_sections].fb_sec_paddr +
4448151SKonstantin.Ananyev@Sun.COM 			    sectp[used_sections].fb_sec_size +
4458151SKonstantin.Ananyev@Sun.COM 			    sectp[used_sections].fb_sec_bss_size >=
4468151SKonstantin.Ananyev@Sun.COM 			    DBOOT_ENTRY_ADDRESS)
4478151SKonstantin.Ananyev@Sun.COM 				return (-1);
4488151SKonstantin.Ananyev@Sun.COM 
4497656SSherry.Moore@Sun.COM 			used_sections++;
4507656SSherry.Moore@Sun.COM 		}
4517656SSherry.Moore@Sun.COM 	}
4527656SSherry.Moore@Sun.COM 
4537656SSherry.Moore@Sun.COM 	*sectcntp = used_sections;
4547656SSherry.Moore@Sun.COM 	return (0);
4557656SSherry.Moore@Sun.COM }
4567656SSherry.Moore@Sun.COM 
4577656SSherry.Moore@Sun.COM /*
4589219SKonstantin.Ananyev@Sun.COM  * Create multiboot info structure (mbi) base on the saved mbi.
4599219SKonstantin.Ananyev@Sun.COM  * Recalculate values of the pointer type fields in the data
4609219SKonstantin.Ananyev@Sun.COM  * structure based on the new starting physical address of the
4619219SKonstantin.Ananyev@Sun.COM  * data structure.
4627656SSherry.Moore@Sun.COM  */
4637656SSherry.Moore@Sun.COM static int
4647656SSherry.Moore@Sun.COM fastboot_build_mbi(char *mdep, fastboot_info_t *nk)
4657656SSherry.Moore@Sun.COM {
4667656SSherry.Moore@Sun.COM 	mb_module_t	*mbp;
4679219SKonstantin.Ananyev@Sun.COM 	multiboot_info_t	*mbi;	/* pointer to multiboot structure */
4689219SKonstantin.Ananyev@Sun.COM 	uintptr_t	start_addr_va;	/* starting VA of mbi */
4699219SKonstantin.Ananyev@Sun.COM 	uintptr_t	start_addr_pa;	/* starting PA of mbi */
4709219SKonstantin.Ananyev@Sun.COM 	size_t		offs = 0;	/* offset from the starting address */
4719219SKonstantin.Ananyev@Sun.COM 	size_t		arglen;		/* length of the command line arg */
4729219SKonstantin.Ananyev@Sun.COM 	size_t		size;	/* size of the memory reserved for mbi */
4739219SKonstantin.Ananyev@Sun.COM 	size_t		mdnsz;	/* length of the boot archive name */
4747656SSherry.Moore@Sun.COM 
4759219SKonstantin.Ananyev@Sun.COM 	/*
4769219SKonstantin.Ananyev@Sun.COM 	 * If mdep is not NULL or empty, use the length of mdep + 1
4779219SKonstantin.Ananyev@Sun.COM 	 * (for NULL terminating) as the length of the new command
4789219SKonstantin.Ananyev@Sun.COM 	 * line; else use the saved command line length as the
4799219SKonstantin.Ananyev@Sun.COM 	 * length for the new command line.
4809219SKonstantin.Ananyev@Sun.COM 	 */
4817750SSherry.Moore@Sun.COM 	if (mdep != NULL && strlen(mdep) != 0) {
4827656SSherry.Moore@Sun.COM 		arglen = strlen(mdep) + 1;
4837656SSherry.Moore@Sun.COM 	} else {
4847656SSherry.Moore@Sun.COM 		arglen = saved_cmdline_len;
4857656SSherry.Moore@Sun.COM 	}
4867656SSherry.Moore@Sun.COM 
4879219SKonstantin.Ananyev@Sun.COM 	/*
4889219SKonstantin.Ananyev@Sun.COM 	 * Allocate memory for the new multiboot info structure (mbi).
4899219SKonstantin.Ananyev@Sun.COM 	 * If we have reserved memory for mbi but it's not enough,
4909219SKonstantin.Ananyev@Sun.COM 	 * free it and reallocate.
4919219SKonstantin.Ananyev@Sun.COM 	 */
4927656SSherry.Moore@Sun.COM 	size = PAGESIZE + P2ROUNDUP(arglen, PAGESIZE);
4939160SSherry.Moore@Sun.COM 	if (nk->fi_mbi_size && nk->fi_mbi_size < size) {
4949160SSherry.Moore@Sun.COM 		contig_free((void *)nk->fi_new_mbi_va, nk->fi_mbi_size);
4959160SSherry.Moore@Sun.COM 		nk->fi_mbi_size = 0;
4969160SSherry.Moore@Sun.COM 	}
4979160SSherry.Moore@Sun.COM 
4989160SSherry.Moore@Sun.COM 	if (nk->fi_mbi_size == 0) {
4999160SSherry.Moore@Sun.COM 		if ((nk->fi_new_mbi_va =
5009160SSherry.Moore@Sun.COM 		    (uintptr_t)contig_alloc(size, &fastboot_below_1G_dma_attr,
5019160SSherry.Moore@Sun.COM 		    PAGESIZE, 0)) == NULL) {
50210970SSherry.Moore@Sun.COM 			cmn_err(CE_NOTE, fastboot_enomem_msg,
5039160SSherry.Moore@Sun.COM 			    (uint64_t)size, "1G");
5049160SSherry.Moore@Sun.COM 			return (-1);
5059160SSherry.Moore@Sun.COM 		}
5069160SSherry.Moore@Sun.COM 		/*
5079160SSherry.Moore@Sun.COM 		 * fi_mbi_size must be set after the allocation succeeds
5089160SSherry.Moore@Sun.COM 		 * as it's used to determine how much memory to free.
5099160SSherry.Moore@Sun.COM 		 */
5109160SSherry.Moore@Sun.COM 		nk->fi_mbi_size = size;
5117656SSherry.Moore@Sun.COM 	}
5127656SSherry.Moore@Sun.COM 
5139219SKonstantin.Ananyev@Sun.COM 	/*
5149219SKonstantin.Ananyev@Sun.COM 	 * Initalize memory
5159219SKonstantin.Ananyev@Sun.COM 	 */
5169160SSherry.Moore@Sun.COM 	bzero((void *)nk->fi_new_mbi_va, nk->fi_mbi_size);
5179160SSherry.Moore@Sun.COM 
5189219SKonstantin.Ananyev@Sun.COM 	/*
5199219SKonstantin.Ananyev@Sun.COM 	 * Get PA for the new mbi
5209219SKonstantin.Ananyev@Sun.COM 	 */
5219219SKonstantin.Ananyev@Sun.COM 	start_addr_va = nk->fi_new_mbi_va;
5229219SKonstantin.Ananyev@Sun.COM 	start_addr_pa = mmu_ptob((uint64_t)hat_getpfnum(kas.a_hat,
5239219SKonstantin.Ananyev@Sun.COM 	    (caddr_t)start_addr_va));
5249219SKonstantin.Ananyev@Sun.COM 	nk->fi_new_mbi_pa = (paddr_t)start_addr_pa;
5257656SSherry.Moore@Sun.COM 
5269160SSherry.Moore@Sun.COM 	/*
5279219SKonstantin.Ananyev@Sun.COM 	 * Populate the rest of the fields in the data structure
5289160SSherry.Moore@Sun.COM 	 */
5299219SKonstantin.Ananyev@Sun.COM 
5309219SKonstantin.Ananyev@Sun.COM 	/*
5319219SKonstantin.Ananyev@Sun.COM 	 * Copy from the saved mbi to preserve all non-pointer type fields.
5329219SKonstantin.Ananyev@Sun.COM 	 */
5339219SKonstantin.Ananyev@Sun.COM 	mbi = (multiboot_info_t *)start_addr_va;
5349219SKonstantin.Ananyev@Sun.COM 	bcopy(&saved_mbi, mbi, sizeof (*mbi));
5357656SSherry.Moore@Sun.COM 
5369219SKonstantin.Ananyev@Sun.COM 	/*
5379219SKonstantin.Ananyev@Sun.COM 	 * Recalculate mods_addr.  Set mod_start and mod_end based on
5389219SKonstantin.Ananyev@Sun.COM 	 * the physical address of the new boot archive.  Set mod_name
5399219SKonstantin.Ananyev@Sun.COM 	 * to the name of the new boto archive.
5409219SKonstantin.Ananyev@Sun.COM 	 */
5419219SKonstantin.Ananyev@Sun.COM 	offs += sizeof (multiboot_info_t);
5429219SKonstantin.Ananyev@Sun.COM 	mbi->mods_addr = start_addr_pa + offs;
5439219SKonstantin.Ananyev@Sun.COM 	mbp = (mb_module_t *)(start_addr_va + offs);
5449160SSherry.Moore@Sun.COM 	mbp->mod_start = nk->fi_files[FASTBOOT_BOOTARCHIVE].fb_dest_pa;
5459160SSherry.Moore@Sun.COM 	mbp->mod_end = nk->fi_files[FASTBOOT_BOOTARCHIVE].fb_next_pa;
5467656SSherry.Moore@Sun.COM 
5479219SKonstantin.Ananyev@Sun.COM 	offs += sizeof (mb_module_t);
5489219SKonstantin.Ananyev@Sun.COM 	mdnsz = strlen(fastboot_filename[FASTBOOT_NAME_BOOTARCHIVE]) + 1;
5499219SKonstantin.Ananyev@Sun.COM 	bcopy(fastboot_filename[FASTBOOT_NAME_BOOTARCHIVE],
5509219SKonstantin.Ananyev@Sun.COM 	    (void *)(start_addr_va + offs), mdnsz);
5519219SKonstantin.Ananyev@Sun.COM 	mbp->mod_name = start_addr_pa + offs;
5529219SKonstantin.Ananyev@Sun.COM 	mbp->reserved = 0;
5537656SSherry.Moore@Sun.COM 
5549219SKonstantin.Ananyev@Sun.COM 	/*
5559219SKonstantin.Ananyev@Sun.COM 	 * Make sure the offset is 16-byte aligned to avoid unaligned access.
5569219SKonstantin.Ananyev@Sun.COM 	 */
5579219SKonstantin.Ananyev@Sun.COM 	offs += mdnsz;
5589219SKonstantin.Ananyev@Sun.COM 	offs = P2ROUNDUP_TYPED(offs, 16, size_t);
5597656SSherry.Moore@Sun.COM 
5609219SKonstantin.Ananyev@Sun.COM 	/*
5619219SKonstantin.Ananyev@Sun.COM 	 * Recalculate mmap_addr
5629219SKonstantin.Ananyev@Sun.COM 	 */
5639219SKonstantin.Ananyev@Sun.COM 	mbi->mmap_addr = start_addr_pa + offs;
5649219SKonstantin.Ananyev@Sun.COM 	bcopy((void *)(uintptr_t)saved_mmap, (void *)(start_addr_va + offs),
5657656SSherry.Moore@Sun.COM 	    saved_mbi.mmap_length);
5669219SKonstantin.Ananyev@Sun.COM 	offs += saved_mbi.mmap_length;
5677656SSherry.Moore@Sun.COM 
5689219SKonstantin.Ananyev@Sun.COM 	/*
5699219SKonstantin.Ananyev@Sun.COM 	 * Recalculate drives_addr
5709219SKonstantin.Ananyev@Sun.COM 	 */
5719219SKonstantin.Ananyev@Sun.COM 	mbi->drives_addr = start_addr_pa + offs;
5729219SKonstantin.Ananyev@Sun.COM 	bcopy((void *)(uintptr_t)saved_drives, (void *)(start_addr_va + offs),
5737656SSherry.Moore@Sun.COM 	    saved_mbi.drives_length);
5749219SKonstantin.Ananyev@Sun.COM 	offs += saved_mbi.drives_length;
5757656SSherry.Moore@Sun.COM 
5769219SKonstantin.Ananyev@Sun.COM 	/*
5779219SKonstantin.Ananyev@Sun.COM 	 * Recalculate the address of cmdline.  Set cmdline to contain the
5789219SKonstantin.Ananyev@Sun.COM 	 * new boot argument.
5799219SKonstantin.Ananyev@Sun.COM 	 */
5809219SKonstantin.Ananyev@Sun.COM 	mbi->cmdline = start_addr_pa + offs;
5817656SSherry.Moore@Sun.COM 
5827750SSherry.Moore@Sun.COM 	if (mdep != NULL && strlen(mdep) != 0) {
5839219SKonstantin.Ananyev@Sun.COM 		bcopy(mdep, (void *)(start_addr_va + offs), arglen);
5847656SSherry.Moore@Sun.COM 	} else {
5859219SKonstantin.Ananyev@Sun.COM 		bcopy((void *)saved_cmdline, (void *)(start_addr_va + offs),
5869219SKonstantin.Ananyev@Sun.COM 		    arglen);
5877656SSherry.Moore@Sun.COM 	}
5887656SSherry.Moore@Sun.COM 
58910525SKonstantin.Ananyev@Sun.COM 	/* clear fields and flags that are not copied */
59010525SKonstantin.Ananyev@Sun.COM 	bzero(&mbi->config_table,
59110525SKonstantin.Ananyev@Sun.COM 	    sizeof (*mbi) - offsetof(multiboot_info_t, config_table));
59210525SKonstantin.Ananyev@Sun.COM 	mbi->flags &= ~(MB_INFO_CONFIG_TABLE | MB_INFO_BOOT_LOADER_NAME |
59310525SKonstantin.Ananyev@Sun.COM 	    MB_INFO_APM_TABLE | MB_INFO_VIDEO_INFO);
59410525SKonstantin.Ananyev@Sun.COM 
5957656SSherry.Moore@Sun.COM 	return (0);
5967656SSherry.Moore@Sun.COM }
5977656SSherry.Moore@Sun.COM 
5987750SSherry.Moore@Sun.COM /*
5997750SSherry.Moore@Sun.COM  * Initialize HAT related fields
6007750SSherry.Moore@Sun.COM  */
6017750SSherry.Moore@Sun.COM static void
6027750SSherry.Moore@Sun.COM fastboot_init_fields(fastboot_info_t *nk)
6037656SSherry.Moore@Sun.COM {
6047750SSherry.Moore@Sun.COM 	if (x86_feature & X86_PAE) {
6057750SSherry.Moore@Sun.COM 		nk->fi_has_pae = 1;
6067750SSherry.Moore@Sun.COM 		nk->fi_shift_amt = fastboot_shift_amt_pae;
6077750SSherry.Moore@Sun.COM 		nk->fi_ptes_per_table = 512;
6087750SSherry.Moore@Sun.COM 		nk->fi_lpagesize = (2 << 20);	/* 2M */
6098151SKonstantin.Ananyev@Sun.COM #ifdef	__amd64
6108151SKonstantin.Ananyev@Sun.COM 		nk->fi_top_level = 3;
6118151SKonstantin.Ananyev@Sun.COM #else
6127750SSherry.Moore@Sun.COM 		nk->fi_top_level = 2;
6138151SKonstantin.Ananyev@Sun.COM #endif	/* __amd64 */
6147750SSherry.Moore@Sun.COM 	}
6157750SSherry.Moore@Sun.COM }
6167656SSherry.Moore@Sun.COM 
6177750SSherry.Moore@Sun.COM /*
6187750SSherry.Moore@Sun.COM  * Process boot argument
6197750SSherry.Moore@Sun.COM  */
6207750SSherry.Moore@Sun.COM static void
6217750SSherry.Moore@Sun.COM fastboot_parse_mdep(char *mdep, char *kern_bootpath, int *bootpath_len,
6227750SSherry.Moore@Sun.COM     char *bootargs)
6237750SSherry.Moore@Sun.COM {
6247750SSherry.Moore@Sun.COM 	int	i;
6257656SSherry.Moore@Sun.COM 
6267656SSherry.Moore@Sun.COM 	/*
6277656SSherry.Moore@Sun.COM 	 * If mdep is not NULL, it comes in the format of
6287656SSherry.Moore@Sun.COM 	 *	mountpoint unix args
6297656SSherry.Moore@Sun.COM 	 */
6307750SSherry.Moore@Sun.COM 	if (mdep != NULL && strlen(mdep) != 0) {
6317656SSherry.Moore@Sun.COM 		if (mdep[0] != '-') {
6327656SSherry.Moore@Sun.COM 			/* First get the root argument */
6337656SSherry.Moore@Sun.COM 			i = 0;
6347656SSherry.Moore@Sun.COM 			while (mdep[i] != '\0' && mdep[i] != ' ') {
6357656SSherry.Moore@Sun.COM 				i++;
6367656SSherry.Moore@Sun.COM 			}
6377656SSherry.Moore@Sun.COM 
6387656SSherry.Moore@Sun.COM 			if (i < 4 || strncmp(&mdep[i-4], "unix", 4) != 0) {
6397656SSherry.Moore@Sun.COM 				/* mount point */
6407656SSherry.Moore@Sun.COM 				bcopy(mdep, kern_bootpath, i);
6417656SSherry.Moore@Sun.COM 				kern_bootpath[i] = '\0';
6427750SSherry.Moore@Sun.COM 				*bootpath_len = i;
6437656SSherry.Moore@Sun.COM 
6447656SSherry.Moore@Sun.COM 				/*
6457656SSherry.Moore@Sun.COM 				 * Get the next argument. It should be unix as
6467656SSherry.Moore@Sun.COM 				 * we have validated in in halt.c.
6477656SSherry.Moore@Sun.COM 				 */
6487656SSherry.Moore@Sun.COM 				if (strlen(mdep) > i) {
6497656SSherry.Moore@Sun.COM 					mdep += (i + 1);
6507656SSherry.Moore@Sun.COM 					i = 0;
6517656SSherry.Moore@Sun.COM 					while (mdep[i] != '\0' &&
6527656SSherry.Moore@Sun.COM 					    mdep[i] != ' ') {
6537656SSherry.Moore@Sun.COM 						i++;
6547656SSherry.Moore@Sun.COM 					}
6557656SSherry.Moore@Sun.COM 				}
6567656SSherry.Moore@Sun.COM 
6577656SSherry.Moore@Sun.COM 			}
6587656SSherry.Moore@Sun.COM 			bcopy(mdep, kern_bootfile, i);
6597656SSherry.Moore@Sun.COM 			kern_bootfile[i] = '\0';
6607750SSherry.Moore@Sun.COM 			bcopy(mdep, bootargs, strlen(mdep));
6617656SSherry.Moore@Sun.COM 		} else {
6627656SSherry.Moore@Sun.COM 			int off = strlen(kern_bootfile);
6637656SSherry.Moore@Sun.COM 			bcopy(kern_bootfile, bootargs, off);
6647656SSherry.Moore@Sun.COM 			bcopy(" ", &bootargs[off++], 1);
6657656SSherry.Moore@Sun.COM 			bcopy(mdep, &bootargs[off], strlen(mdep));
6667656SSherry.Moore@Sun.COM 			off += strlen(mdep);
6677656SSherry.Moore@Sun.COM 			bootargs[off] = '\0';
6687656SSherry.Moore@Sun.COM 		}
6697656SSherry.Moore@Sun.COM 	}
6707750SSherry.Moore@Sun.COM }
6717750SSherry.Moore@Sun.COM 
6727750SSherry.Moore@Sun.COM /*
6739160SSherry.Moore@Sun.COM  * Reserve memory under PA 1G for mapping the new kernel and boot archive.
6749160SSherry.Moore@Sun.COM  * This function is only called if fastreboot_onpanic is *not* set.
6759160SSherry.Moore@Sun.COM  */
6769160SSherry.Moore@Sun.COM static void
6779160SSherry.Moore@Sun.COM fastboot_reserve_mem(fastboot_info_t *nk)
6789160SSherry.Moore@Sun.COM {
6799160SSherry.Moore@Sun.COM 	int i;
6809160SSherry.Moore@Sun.COM 
6819160SSherry.Moore@Sun.COM 	/*
6829160SSherry.Moore@Sun.COM 	 * A valid kernel is in place.  No need to reserve any memory.
6839160SSherry.Moore@Sun.COM 	 */
6849160SSherry.Moore@Sun.COM 	if (nk->fi_valid)
6859160SSherry.Moore@Sun.COM 		return;
6869160SSherry.Moore@Sun.COM 
6879160SSherry.Moore@Sun.COM 	/*
6889160SSherry.Moore@Sun.COM 	 * Reserve memory under PA 1G for PTE lists.
6899160SSherry.Moore@Sun.COM 	 */
6909160SSherry.Moore@Sun.COM 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
6919160SSherry.Moore@Sun.COM 		fastboot_file_t *fb = &nk->fi_files[i];
6929160SSherry.Moore@Sun.COM 		size_t fsize_roundup, size;
6939160SSherry.Moore@Sun.COM 
6949160SSherry.Moore@Sun.COM 		fsize_roundup = P2ROUNDUP_TYPED(saved_file_size[i],
6959160SSherry.Moore@Sun.COM 		    PAGESIZE, size_t);
6969160SSherry.Moore@Sun.COM 		size = FASTBOOT_PTE_LIST_SIZE(fsize_roundup);
6979160SSherry.Moore@Sun.COM 		if ((fb->fb_pte_list_va = contig_alloc(size,
6989160SSherry.Moore@Sun.COM 		    &fastboot_below_1G_dma_attr, PAGESIZE, 0)) == NULL) {
6999160SSherry.Moore@Sun.COM 			return;
7009160SSherry.Moore@Sun.COM 		}
7019160SSherry.Moore@Sun.COM 		fb->fb_pte_list_size = size;
7029160SSherry.Moore@Sun.COM 	}
7039160SSherry.Moore@Sun.COM 
7049160SSherry.Moore@Sun.COM 	/*
7059160SSherry.Moore@Sun.COM 	 * Reserve memory under PA 1G for page tables.
7069160SSherry.Moore@Sun.COM 	 */
7079160SSherry.Moore@Sun.COM 	if ((nk->fi_pagetable_va =
7089160SSherry.Moore@Sun.COM 	    (uintptr_t)contig_alloc(fastboot_pagetable_size,
7099160SSherry.Moore@Sun.COM 	    &fastboot_below_1G_dma_attr, PAGESIZE, 0)) == NULL) {
7109160SSherry.Moore@Sun.COM 		return;
7119160SSherry.Moore@Sun.COM 	}
7129160SSherry.Moore@Sun.COM 	nk->fi_pagetable_size = fastboot_pagetable_size;
7139160SSherry.Moore@Sun.COM 
7149160SSherry.Moore@Sun.COM 	/*
7159160SSherry.Moore@Sun.COM 	 * Reserve memory under PA 1G for multiboot structure.
7169160SSherry.Moore@Sun.COM 	 */
7179160SSherry.Moore@Sun.COM 	if ((nk->fi_new_mbi_va = (uintptr_t)contig_alloc(fastboot_mbi_size,
7189160SSherry.Moore@Sun.COM 	    &fastboot_below_1G_dma_attr, PAGESIZE, 0)) == NULL) {
7199160SSherry.Moore@Sun.COM 		return;
7209160SSherry.Moore@Sun.COM 	}
7219160SSherry.Moore@Sun.COM 	nk->fi_mbi_size = fastboot_mbi_size;
7229160SSherry.Moore@Sun.COM }
7239160SSherry.Moore@Sun.COM 
7249160SSherry.Moore@Sun.COM /*
7259160SSherry.Moore@Sun.COM  * Calculate MD5 digest for the given fastboot_file.
7269160SSherry.Moore@Sun.COM  * Assumes that the file is allready loaded properly.
7279160SSherry.Moore@Sun.COM  */
7289160SSherry.Moore@Sun.COM static void
7299160SSherry.Moore@Sun.COM fastboot_cksum_file(fastboot_file_t *fb, uchar_t *md5_hash)
7309160SSherry.Moore@Sun.COM {
7319160SSherry.Moore@Sun.COM 	MD5_CTX md5_ctx;
7329160SSherry.Moore@Sun.COM 
7339160SSherry.Moore@Sun.COM 	MD5Init(&md5_ctx);
7349160SSherry.Moore@Sun.COM 	MD5Update(&md5_ctx, (void *)fb->fb_va, fb->fb_size);
7359160SSherry.Moore@Sun.COM 	MD5Final(md5_hash, &md5_ctx);
7369160SSherry.Moore@Sun.COM }
7379160SSherry.Moore@Sun.COM 
7389160SSherry.Moore@Sun.COM /*
7399160SSherry.Moore@Sun.COM  * Free up the memory we have allocated for a file
7407750SSherry.Moore@Sun.COM  */
7417750SSherry.Moore@Sun.COM static void
7427750SSherry.Moore@Sun.COM fastboot_free_file(fastboot_file_t *fb)
7437750SSherry.Moore@Sun.COM {
7449160SSherry.Moore@Sun.COM 	size_t	fsize_roundup;
7457750SSherry.Moore@Sun.COM 
7467750SSherry.Moore@Sun.COM 	fsize_roundup = P2ROUNDUP_TYPED(fb->fb_size, PAGESIZE, size_t);
7479160SSherry.Moore@Sun.COM 	if (fsize_roundup) {
7489160SSherry.Moore@Sun.COM 		contig_free((void *)fb->fb_va, fsize_roundup);
7499160SSherry.Moore@Sun.COM 		fb->fb_va = NULL;
7509160SSherry.Moore@Sun.COM 		fb->fb_size = 0;
7519160SSherry.Moore@Sun.COM 	}
7529160SSherry.Moore@Sun.COM }
7539160SSherry.Moore@Sun.COM 
7549160SSherry.Moore@Sun.COM /*
7559160SSherry.Moore@Sun.COM  * Free up memory used by the PTEs for a file.
7569160SSherry.Moore@Sun.COM  */
7579160SSherry.Moore@Sun.COM static void
7589160SSherry.Moore@Sun.COM fastboot_free_file_pte(fastboot_file_t *fb, uint64_t endaddr)
7599160SSherry.Moore@Sun.COM {
7609160SSherry.Moore@Sun.COM 	if (fb->fb_pte_list_size && fb->fb_pte_list_pa < endaddr) {
7619160SSherry.Moore@Sun.COM 		contig_free((void *)fb->fb_pte_list_va, fb->fb_pte_list_size);
7629160SSherry.Moore@Sun.COM 		fb->fb_pte_list_va = 0;
7639160SSherry.Moore@Sun.COM 		fb->fb_pte_list_pa = 0;
7649160SSherry.Moore@Sun.COM 		fb->fb_pte_list_size = 0;
7659160SSherry.Moore@Sun.COM 	}
7669160SSherry.Moore@Sun.COM }
7679160SSherry.Moore@Sun.COM 
7689160SSherry.Moore@Sun.COM /*
7699160SSherry.Moore@Sun.COM  * Free up all the memory used for representing a kernel with
7709160SSherry.Moore@Sun.COM  * fastboot_info_t.
7719160SSherry.Moore@Sun.COM  */
7729160SSherry.Moore@Sun.COM static void
7739160SSherry.Moore@Sun.COM fastboot_free_mem(fastboot_info_t *nk, uint64_t endaddr)
7749160SSherry.Moore@Sun.COM {
7759160SSherry.Moore@Sun.COM 	int i;
7769160SSherry.Moore@Sun.COM 
7779160SSherry.Moore@Sun.COM 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
7789160SSherry.Moore@Sun.COM 		fastboot_free_file(nk->fi_files + i);
7799160SSherry.Moore@Sun.COM 		fastboot_free_file_pte(nk->fi_files + i, endaddr);
7809160SSherry.Moore@Sun.COM 	}
7819160SSherry.Moore@Sun.COM 
7829160SSherry.Moore@Sun.COM 	if (nk->fi_pagetable_size && nk->fi_pagetable_pa < endaddr) {
7839160SSherry.Moore@Sun.COM 		contig_free((void *)nk->fi_pagetable_va, nk->fi_pagetable_size);
7849160SSherry.Moore@Sun.COM 		nk->fi_pagetable_va = 0;
7859160SSherry.Moore@Sun.COM 		nk->fi_pagetable_pa = 0;
7869160SSherry.Moore@Sun.COM 		nk->fi_pagetable_size = 0;
7879160SSherry.Moore@Sun.COM 	}
7889160SSherry.Moore@Sun.COM 
7899160SSherry.Moore@Sun.COM 	if (nk->fi_mbi_size && nk->fi_new_mbi_pa < endaddr) {
7909160SSherry.Moore@Sun.COM 		contig_free((void *)nk->fi_new_mbi_va, nk->fi_mbi_size);
7919160SSherry.Moore@Sun.COM 		nk->fi_new_mbi_va = 0;
7929160SSherry.Moore@Sun.COM 		nk->fi_new_mbi_pa = 0;
7939160SSherry.Moore@Sun.COM 		nk->fi_mbi_size = 0;
7949160SSherry.Moore@Sun.COM 	}
7959160SSherry.Moore@Sun.COM }
7969160SSherry.Moore@Sun.COM 
7979160SSherry.Moore@Sun.COM /*
7989160SSherry.Moore@Sun.COM  * Only free up the memory allocated for the kernel and boot archive,
7999160SSherry.Moore@Sun.COM  * but not for the page tables.
8009160SSherry.Moore@Sun.COM  */
8019160SSherry.Moore@Sun.COM void
8029160SSherry.Moore@Sun.COM fastboot_free_newkernel(fastboot_info_t *nk)
8039160SSherry.Moore@Sun.COM {
8049160SSherry.Moore@Sun.COM 	int i;
8057750SSherry.Moore@Sun.COM 
8069160SSherry.Moore@Sun.COM 	nk->fi_valid = 0;
8079160SSherry.Moore@Sun.COM 	/*
8089160SSherry.Moore@Sun.COM 	 * Free the memory we have allocated
8099160SSherry.Moore@Sun.COM 	 */
8109160SSherry.Moore@Sun.COM 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
8119160SSherry.Moore@Sun.COM 		fastboot_free_file(&(nk->fi_files[i]));
8129160SSherry.Moore@Sun.COM 	}
8139160SSherry.Moore@Sun.COM }
8149160SSherry.Moore@Sun.COM 
8159160SSherry.Moore@Sun.COM static void
8169160SSherry.Moore@Sun.COM fastboot_cksum_cdata(fastboot_info_t *nk, uchar_t *md5_hash)
8179160SSherry.Moore@Sun.COM {
8189160SSherry.Moore@Sun.COM 	int i;
8199160SSherry.Moore@Sun.COM 	MD5_CTX md5_ctx;
8209160SSherry.Moore@Sun.COM 
8219160SSherry.Moore@Sun.COM 	MD5Init(&md5_ctx);
8229160SSherry.Moore@Sun.COM 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
8239160SSherry.Moore@Sun.COM 		MD5Update(&md5_ctx, nk->fi_files[i].fb_pte_list_va,
8249160SSherry.Moore@Sun.COM 		    nk->fi_files[i].fb_pte_list_size);
8259160SSherry.Moore@Sun.COM 	}
8269160SSherry.Moore@Sun.COM 	MD5Update(&md5_ctx, (void *)nk->fi_pagetable_va, nk->fi_pagetable_size);
8279160SSherry.Moore@Sun.COM 	MD5Update(&md5_ctx, (void *)nk->fi_new_mbi_va, nk->fi_mbi_size);
8289160SSherry.Moore@Sun.COM 
8299160SSherry.Moore@Sun.COM 	MD5Final(md5_hash, &md5_ctx);
8309160SSherry.Moore@Sun.COM }
8319160SSherry.Moore@Sun.COM 
8329160SSherry.Moore@Sun.COM /*
8339160SSherry.Moore@Sun.COM  * Generate MD5 checksum of the given kernel.
8349160SSherry.Moore@Sun.COM  */
8359160SSherry.Moore@Sun.COM static void
8369160SSherry.Moore@Sun.COM fastboot_cksum_generate(fastboot_info_t *nk)
8379160SSherry.Moore@Sun.COM {
8389160SSherry.Moore@Sun.COM 	int i;
8399160SSherry.Moore@Sun.COM 
8409160SSherry.Moore@Sun.COM 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
8419160SSherry.Moore@Sun.COM 		fastboot_cksum_file(nk->fi_files + i, nk->fi_md5_hash[i]);
8429160SSherry.Moore@Sun.COM 	}
8439160SSherry.Moore@Sun.COM 	fastboot_cksum_cdata(nk, nk->fi_md5_hash[i]);
8449160SSherry.Moore@Sun.COM }
8459160SSherry.Moore@Sun.COM 
8469160SSherry.Moore@Sun.COM /*
8479160SSherry.Moore@Sun.COM  * Calculate MD5 checksum of the given kernel and verify that
8489160SSherry.Moore@Sun.COM  * it matches with what was calculated before.
8499160SSherry.Moore@Sun.COM  */
8509160SSherry.Moore@Sun.COM int
8519160SSherry.Moore@Sun.COM fastboot_cksum_verify(fastboot_info_t *nk)
8529160SSherry.Moore@Sun.COM {
8539160SSherry.Moore@Sun.COM 	int i;
8549160SSherry.Moore@Sun.COM 	uchar_t md5_hash[MD5_DIGEST_LENGTH];
8559160SSherry.Moore@Sun.COM 
8569160SSherry.Moore@Sun.COM 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
8579160SSherry.Moore@Sun.COM 		fastboot_cksum_file(nk->fi_files + i, md5_hash);
8589160SSherry.Moore@Sun.COM 		if (bcmp(nk->fi_md5_hash[i], md5_hash,
8599160SSherry.Moore@Sun.COM 		    sizeof (nk->fi_md5_hash[i])) != 0)
8609160SSherry.Moore@Sun.COM 			return (i + 1);
8619160SSherry.Moore@Sun.COM 	}
8629160SSherry.Moore@Sun.COM 
8639160SSherry.Moore@Sun.COM 	fastboot_cksum_cdata(nk, md5_hash);
8649160SSherry.Moore@Sun.COM 	if (bcmp(nk->fi_md5_hash[i], md5_hash,
8659160SSherry.Moore@Sun.COM 	    sizeof (nk->fi_md5_hash[i])) != 0)
8669160SSherry.Moore@Sun.COM 		return (i + 1);
8679160SSherry.Moore@Sun.COM 
8689160SSherry.Moore@Sun.COM 	return (0);
8697750SSherry.Moore@Sun.COM }
8707750SSherry.Moore@Sun.COM 
8717750SSherry.Moore@Sun.COM /*
8727750SSherry.Moore@Sun.COM  * This function performs the following tasks:
8737750SSherry.Moore@Sun.COM  * - Read the sizes of the new kernel and boot archive.
8747750SSherry.Moore@Sun.COM  * - Allocate memory for the new kernel and boot archive.
8757750SSherry.Moore@Sun.COM  * - Allocate memory for page tables necessary for mapping the memory
8767750SSherry.Moore@Sun.COM  *   allocated for the files.
8777750SSherry.Moore@Sun.COM  * - Read the new kernel and boot archive into memory.
8787750SSherry.Moore@Sun.COM  * - Map in the fast reboot switcher.
8797750SSherry.Moore@Sun.COM  * - Load the fast reboot switcher to FASTBOOT_SWTCH_PA.
8807750SSherry.Moore@Sun.COM  * - Build the new multiboot_info structure
8817750SSherry.Moore@Sun.COM  * - Build page tables for the low 1G of physical memory.
8827750SSherry.Moore@Sun.COM  * - Mark the data structure as valid if all steps have succeeded.
8837750SSherry.Moore@Sun.COM  */
8847750SSherry.Moore@Sun.COM void
8859160SSherry.Moore@Sun.COM fastboot_load_kernel(char *mdep)
8867750SSherry.Moore@Sun.COM {
8877750SSherry.Moore@Sun.COM 	void		*buf = NULL;
8887750SSherry.Moore@Sun.COM 	int		i;
8897750SSherry.Moore@Sun.COM 	fastboot_file_t	*fb;
8907750SSherry.Moore@Sun.COM 	uint32_t	dboot_start_offset;
8917750SSherry.Moore@Sun.COM 	char		kern_bootpath[OBP_MAXPATHLEN];
8927750SSherry.Moore@Sun.COM 	extern uintptr_t postbootkernelbase;
8939160SSherry.Moore@Sun.COM 	uintptr_t	saved_kernelbase;
8947750SSherry.Moore@Sun.COM 	int		bootpath_len = 0;
8957750SSherry.Moore@Sun.COM 	int		is_failsafe = 0;
8967750SSherry.Moore@Sun.COM 	int		is_retry = 0;
8977750SSherry.Moore@Sun.COM 	uint64_t	end_addr;
8987750SSherry.Moore@Sun.COM 
89910559SSherry.Moore@Sun.COM 	if (!fastreboot_capable)
90010559SSherry.Moore@Sun.COM 		return;
9017750SSherry.Moore@Sun.COM 
9029160SSherry.Moore@Sun.COM 	if (newkernel.fi_valid)
9039160SSherry.Moore@Sun.COM 		fastboot_free_newkernel(&newkernel);
9049160SSherry.Moore@Sun.COM 
9059160SSherry.Moore@Sun.COM 	saved_kernelbase = postbootkernelbase;
9069160SSherry.Moore@Sun.COM 
9077750SSherry.Moore@Sun.COM 	postbootkernelbase = 0;
9087750SSherry.Moore@Sun.COM 
9097750SSherry.Moore@Sun.COM 	/*
9107750SSherry.Moore@Sun.COM 	 * Initialize various HAT related fields in the data structure
9117750SSherry.Moore@Sun.COM 	 */
9127750SSherry.Moore@Sun.COM 	fastboot_init_fields(&newkernel);
9137750SSherry.Moore@Sun.COM 
9147750SSherry.Moore@Sun.COM 	bzero(kern_bootpath, OBP_MAXPATHLEN);
9157750SSherry.Moore@Sun.COM 
9167750SSherry.Moore@Sun.COM 	/*
9177750SSherry.Moore@Sun.COM 	 * Process the boot argument
9187750SSherry.Moore@Sun.COM 	 */
9199160SSherry.Moore@Sun.COM 	bzero(fastboot_args, OBP_MAXPATHLEN);
9209160SSherry.Moore@Sun.COM 	fastboot_parse_mdep(mdep, kern_bootpath, &bootpath_len, fastboot_args);
9217656SSherry.Moore@Sun.COM 
9227656SSherry.Moore@Sun.COM 	/*
9237656SSherry.Moore@Sun.COM 	 * Make sure we get the null character
9247656SSherry.Moore@Sun.COM 	 */
9257656SSherry.Moore@Sun.COM 	bcopy(kern_bootpath, fastboot_filename[FASTBOOT_NAME_UNIX],
9267656SSherry.Moore@Sun.COM 	    bootpath_len);
9277656SSherry.Moore@Sun.COM 	bcopy(kern_bootfile,
9287656SSherry.Moore@Sun.COM 	    &fastboot_filename[FASTBOOT_NAME_UNIX][bootpath_len],
9297656SSherry.Moore@Sun.COM 	    strlen(kern_bootfile) + 1);
9307656SSherry.Moore@Sun.COM 
9317656SSherry.Moore@Sun.COM 	bcopy(kern_bootpath, fastboot_filename[FASTBOOT_NAME_BOOTARCHIVE],
9327656SSherry.Moore@Sun.COM 	    bootpath_len);
9337656SSherry.Moore@Sun.COM 
9349160SSherry.Moore@Sun.COM 	if (bcmp(kern_bootfile, FAILSAFE_BOOTFILE32,
9359160SSherry.Moore@Sun.COM 	    (sizeof (FAILSAFE_BOOTFILE32) - 1)) == 0 ||
9369160SSherry.Moore@Sun.COM 	    bcmp(kern_bootfile, FAILSAFE_BOOTFILE64,
9379160SSherry.Moore@Sun.COM 	    (sizeof (FAILSAFE_BOOTFILE64) - 1)) == 0) {
9387656SSherry.Moore@Sun.COM 		is_failsafe = 1;
9397656SSherry.Moore@Sun.COM 	}
9407656SSherry.Moore@Sun.COM 
9417750SSherry.Moore@Sun.COM load_kernel_retry:
9427656SSherry.Moore@Sun.COM 	/*
9437656SSherry.Moore@Sun.COM 	 * Read in unix and boot_archive
9447656SSherry.Moore@Sun.COM 	 */
9457750SSherry.Moore@Sun.COM 	end_addr = DBOOT_ENTRY_ADDRESS;
9467656SSherry.Moore@Sun.COM 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
9477750SSherry.Moore@Sun.COM 		struct _buf	*file;
9487750SSherry.Moore@Sun.COM 		uintptr_t	va;
9497750SSherry.Moore@Sun.COM 		uint64_t	fsize;
9507750SSherry.Moore@Sun.COM 		size_t		fsize_roundup, pt_size;
9517750SSherry.Moore@Sun.COM 		int		page_index;
9527750SSherry.Moore@Sun.COM 		uintptr_t	offset;
9537656SSherry.Moore@Sun.COM 		ddi_dma_attr_t dma_attr = fastboot_dma_attr;
9547656SSherry.Moore@Sun.COM 
9557750SSherry.Moore@Sun.COM 
9567656SSherry.Moore@Sun.COM 		dprintf("fastboot_filename[%d] = %s\n",
9577656SSherry.Moore@Sun.COM 		    i, fastboot_filename[i]);
9587656SSherry.Moore@Sun.COM 
9597656SSherry.Moore@Sun.COM 		if ((file = kobj_open_file(fastboot_filename[i])) ==
9607656SSherry.Moore@Sun.COM 		    (struct _buf *)-1) {
96110970SSherry.Moore@Sun.COM 			cmn_err(CE_NOTE, "!Fastboot: Couldn't open %s",
9627656SSherry.Moore@Sun.COM 			    fastboot_filename[i]);
9637656SSherry.Moore@Sun.COM 			goto err_out;
9647656SSherry.Moore@Sun.COM 		}
9657656SSherry.Moore@Sun.COM 
9667656SSherry.Moore@Sun.COM 		if (kobj_get_filesize(file, &fsize) != 0) {
96710970SSherry.Moore@Sun.COM 			cmn_err(CE_NOTE,
96810970SSherry.Moore@Sun.COM 			    "!Fastboot: Couldn't get filesize for %s",
9697656SSherry.Moore@Sun.COM 			    fastboot_filename[i]);
9707656SSherry.Moore@Sun.COM 			goto err_out;
9717656SSherry.Moore@Sun.COM 		}
9727656SSherry.Moore@Sun.COM 
9737750SSherry.Moore@Sun.COM 		fsize_roundup = P2ROUNDUP_TYPED(fsize, PAGESIZE, size_t);
9747750SSherry.Moore@Sun.COM 
9757750SSherry.Moore@Sun.COM 		/*
9767750SSherry.Moore@Sun.COM 		 * Where the files end in physical memory after being
9777750SSherry.Moore@Sun.COM 		 * relocated by the fast boot switcher.
9787750SSherry.Moore@Sun.COM 		 */
9797750SSherry.Moore@Sun.COM 		end_addr += fsize_roundup;
9807750SSherry.Moore@Sun.COM 		if (end_addr > fastboot_below_1G_dma_attr.dma_attr_addr_hi) {
98110970SSherry.Moore@Sun.COM 			cmn_err(CE_NOTE, "!Fastboot: boot archive is too big");
9827750SSherry.Moore@Sun.COM 			goto err_out;
9837656SSherry.Moore@Sun.COM 		}
9847656SSherry.Moore@Sun.COM 
9857750SSherry.Moore@Sun.COM 		/*
9867750SSherry.Moore@Sun.COM 		 * Adjust dma_attr_addr_lo so that the new kernel and boot
9877750SSherry.Moore@Sun.COM 		 * archive will not be overridden during relocation.
9887750SSherry.Moore@Sun.COM 		 */
9897750SSherry.Moore@Sun.COM 		if (end_addr > fastboot_dma_attr.dma_attr_addr_lo ||
9907750SSherry.Moore@Sun.COM 		    end_addr > fastboot_below_1G_dma_attr.dma_attr_addr_lo) {
9917750SSherry.Moore@Sun.COM 
9927750SSherry.Moore@Sun.COM 			if (is_retry) {
9937750SSherry.Moore@Sun.COM 				/*
9947750SSherry.Moore@Sun.COM 				 * If we have already tried and didn't succeed,
9957750SSherry.Moore@Sun.COM 				 * just give up.
9967750SSherry.Moore@Sun.COM 				 */
99710970SSherry.Moore@Sun.COM 				cmn_err(CE_NOTE,
99810970SSherry.Moore@Sun.COM 				    "!Fastboot: boot archive is too big");
9997750SSherry.Moore@Sun.COM 				goto err_out;
10007750SSherry.Moore@Sun.COM 			} else {
10017750SSherry.Moore@Sun.COM 				/* Set the flag so we don't keep retrying */
10027750SSherry.Moore@Sun.COM 				is_retry++;
10037750SSherry.Moore@Sun.COM 
10047750SSherry.Moore@Sun.COM 				/* Adjust dma_attr_addr_lo */
10057750SSherry.Moore@Sun.COM 				fastboot_dma_attr.dma_attr_addr_lo = end_addr;
10067750SSherry.Moore@Sun.COM 				fastboot_below_1G_dma_attr.dma_attr_addr_lo =
10077750SSherry.Moore@Sun.COM 				    end_addr;
10087750SSherry.Moore@Sun.COM 
10097750SSherry.Moore@Sun.COM 				/*
10107750SSherry.Moore@Sun.COM 				 * Free the memory we have already allocated
10117750SSherry.Moore@Sun.COM 				 * whose physical addresses might not fit
10127750SSherry.Moore@Sun.COM 				 * the new lo and hi constraints.
10137750SSherry.Moore@Sun.COM 				 */
10149160SSherry.Moore@Sun.COM 				fastboot_free_mem(&newkernel, end_addr);
10157750SSherry.Moore@Sun.COM 				goto load_kernel_retry;
10167750SSherry.Moore@Sun.COM 			}
10177750SSherry.Moore@Sun.COM 		}
10187750SSherry.Moore@Sun.COM 
10197750SSherry.Moore@Sun.COM 
10207656SSherry.Moore@Sun.COM 		if (!fastboot_contig)
10217656SSherry.Moore@Sun.COM 			dma_attr.dma_attr_sgllen = (fsize / PAGESIZE) +
10227656SSherry.Moore@Sun.COM 			    (((fsize % PAGESIZE) == 0) ? 0 : 1);
10237656SSherry.Moore@Sun.COM 
10247656SSherry.Moore@Sun.COM 		if ((buf = contig_alloc(fsize, &dma_attr, PAGESIZE, 0))
10257656SSherry.Moore@Sun.COM 		    == NULL) {
102610970SSherry.Moore@Sun.COM 			cmn_err(CE_NOTE, fastboot_enomem_msg, fsize, "64G");
10277656SSherry.Moore@Sun.COM 			goto err_out;
10287656SSherry.Moore@Sun.COM 		}
10297656SSherry.Moore@Sun.COM 
10307656SSherry.Moore@Sun.COM 		va = P2ROUNDUP_TYPED((uintptr_t)buf, PAGESIZE, uintptr_t);
10317656SSherry.Moore@Sun.COM 
10327656SSherry.Moore@Sun.COM 		if (kobj_read_file(file, (char *)va, fsize, 0) < 0) {
103310970SSherry.Moore@Sun.COM 			cmn_err(CE_NOTE, "!Fastboot: Couldn't read %s",
10347656SSherry.Moore@Sun.COM 			    fastboot_filename[i]);
10357656SSherry.Moore@Sun.COM 			goto err_out;
10367656SSherry.Moore@Sun.COM 		}
10377656SSherry.Moore@Sun.COM 
10387656SSherry.Moore@Sun.COM 		fb = &newkernel.fi_files[i];
10397656SSherry.Moore@Sun.COM 		fb->fb_va = va;
10407656SSherry.Moore@Sun.COM 		fb->fb_size = fsize;
10417656SSherry.Moore@Sun.COM 		fb->fb_sectcnt = 0;
10427656SSherry.Moore@Sun.COM 
10439160SSherry.Moore@Sun.COM 		pt_size = FASTBOOT_PTE_LIST_SIZE(fsize_roundup);
10447656SSherry.Moore@Sun.COM 
10459160SSherry.Moore@Sun.COM 		/*
10469160SSherry.Moore@Sun.COM 		 * If we have reserved memory but it not enough, free it.
10479160SSherry.Moore@Sun.COM 		 */
10489160SSherry.Moore@Sun.COM 		if (fb->fb_pte_list_size && fb->fb_pte_list_size < pt_size) {
10499160SSherry.Moore@Sun.COM 			contig_free((void *)fb->fb_pte_list_va,
10509160SSherry.Moore@Sun.COM 			    fb->fb_pte_list_size);
10519160SSherry.Moore@Sun.COM 			fb->fb_pte_list_size = 0;
10527656SSherry.Moore@Sun.COM 		}
10537656SSherry.Moore@Sun.COM 
10549160SSherry.Moore@Sun.COM 		if (fb->fb_pte_list_size == 0) {
10559160SSherry.Moore@Sun.COM 			if ((fb->fb_pte_list_va =
10569160SSherry.Moore@Sun.COM 			    (x86pte_t *)contig_alloc(pt_size,
10579160SSherry.Moore@Sun.COM 			    &fastboot_below_1G_dma_attr, PAGESIZE, 0))
10589160SSherry.Moore@Sun.COM 			    == NULL) {
105910970SSherry.Moore@Sun.COM 				cmn_err(CE_NOTE, fastboot_enomem_msg,
10609160SSherry.Moore@Sun.COM 				    (uint64_t)pt_size, "1G");
10619160SSherry.Moore@Sun.COM 				goto err_out;
10629160SSherry.Moore@Sun.COM 			}
10639160SSherry.Moore@Sun.COM 			/*
10649160SSherry.Moore@Sun.COM 			 * fb_pte_list_size must be set after the allocation
10659160SSherry.Moore@Sun.COM 			 * succeeds as it's used to determine how much memory to
10669160SSherry.Moore@Sun.COM 			 * free.
10679160SSherry.Moore@Sun.COM 			 */
10689160SSherry.Moore@Sun.COM 			fb->fb_pte_list_size = pt_size;
10699160SSherry.Moore@Sun.COM 		}
10709160SSherry.Moore@Sun.COM 
10719160SSherry.Moore@Sun.COM 		bzero((void *)(fb->fb_pte_list_va), fb->fb_pte_list_size);
10727656SSherry.Moore@Sun.COM 
10737656SSherry.Moore@Sun.COM 		fb->fb_pte_list_pa = mmu_ptob((uint64_t)hat_getpfnum(kas.a_hat,
10747656SSherry.Moore@Sun.COM 		    (caddr_t)fb->fb_pte_list_va));
10757656SSherry.Moore@Sun.COM 
10767656SSherry.Moore@Sun.COM 		for (page_index = 0, offset = 0; offset < fb->fb_size;
10777656SSherry.Moore@Sun.COM 		    offset += PAGESIZE) {
10787656SSherry.Moore@Sun.COM 			uint64_t paddr;
10797656SSherry.Moore@Sun.COM 
10807656SSherry.Moore@Sun.COM 			paddr = mmu_ptob((uint64_t)hat_getpfnum(kas.a_hat,
10817656SSherry.Moore@Sun.COM 			    (caddr_t)fb->fb_va + offset));
10827656SSherry.Moore@Sun.COM 
10837656SSherry.Moore@Sun.COM 			ASSERT(paddr >= fastboot_dma_attr.dma_attr_addr_lo);
10847656SSherry.Moore@Sun.COM 
10857656SSherry.Moore@Sun.COM 			/*
10867656SSherry.Moore@Sun.COM 			 * Include the pte_bits so we don't have to make
10877656SSherry.Moore@Sun.COM 			 * it in assembly.
10887656SSherry.Moore@Sun.COM 			 */
10897656SSherry.Moore@Sun.COM 			fb->fb_pte_list_va[page_index++] = (x86pte_t)
10907656SSherry.Moore@Sun.COM 			    (paddr | pte_bits);
10917656SSherry.Moore@Sun.COM 		}
10927656SSherry.Moore@Sun.COM 
10937656SSherry.Moore@Sun.COM 		fb->fb_pte_list_va[page_index] = FASTBOOT_TERMINATE;
10947656SSherry.Moore@Sun.COM 
10957656SSherry.Moore@Sun.COM 		if (i == FASTBOOT_UNIX) {
10967750SSherry.Moore@Sun.COM 			Ehdr	*ehdr = (Ehdr *)va;
10977750SSherry.Moore@Sun.COM 			int	j;
10987656SSherry.Moore@Sun.COM 
10997656SSherry.Moore@Sun.COM 			/*
11007656SSherry.Moore@Sun.COM 			 * Sanity checks:
11017656SSherry.Moore@Sun.COM 			 */
11027656SSherry.Moore@Sun.COM 			for (j = 0; j < SELFMAG; j++) {
11037656SSherry.Moore@Sun.COM 				if (ehdr->e_ident[j] != ELFMAG[j]) {
110410970SSherry.Moore@Sun.COM 					cmn_err(CE_NOTE, "!Fastboot: Bad ELF "
11057656SSherry.Moore@Sun.COM 					    "signature");
11067656SSherry.Moore@Sun.COM 					goto err_out;
11077656SSherry.Moore@Sun.COM 				}
11087656SSherry.Moore@Sun.COM 			}
11097656SSherry.Moore@Sun.COM 
11107656SSherry.Moore@Sun.COM 			if (ehdr->e_ident[EI_CLASS] == ELFCLASS32 &&
11117656SSherry.Moore@Sun.COM 			    ehdr->e_ident[EI_DATA] == ELFDATA2LSB &&
11127656SSherry.Moore@Sun.COM 			    ehdr->e_machine == EM_386) {
11137656SSherry.Moore@Sun.COM 
11148151SKonstantin.Ananyev@Sun.COM 				fb->fb_sectcnt = sizeof (fb->fb_sections) /
11158151SKonstantin.Ananyev@Sun.COM 				    sizeof (fb->fb_sections[0]);
11168151SKonstantin.Ananyev@Sun.COM 
11177656SSherry.Moore@Sun.COM 				if (fastboot_elf32_find_loadables((void *)va,
11187656SSherry.Moore@Sun.COM 				    fsize, &fb->fb_sections[0],
11197656SSherry.Moore@Sun.COM 				    &fb->fb_sectcnt, &dboot_start_offset) < 0) {
112010970SSherry.Moore@Sun.COM 					cmn_err(CE_NOTE, "!Fastboot: ELF32 "
11217656SSherry.Moore@Sun.COM 					    "program section failure");
11227656SSherry.Moore@Sun.COM 					goto err_out;
11237656SSherry.Moore@Sun.COM 				}
11247656SSherry.Moore@Sun.COM 
11257656SSherry.Moore@Sun.COM 				if (fb->fb_sectcnt == 0) {
112610970SSherry.Moore@Sun.COM 					cmn_err(CE_NOTE, "!Fastboot: No ELF32 "
11277656SSherry.Moore@Sun.COM 					    "program sections found");
11287656SSherry.Moore@Sun.COM 					goto err_out;
11297656SSherry.Moore@Sun.COM 				}
11307656SSherry.Moore@Sun.COM 
11317656SSherry.Moore@Sun.COM 				if (is_failsafe) {
11327656SSherry.Moore@Sun.COM 					/* Failsafe boot_archive */
11339160SSherry.Moore@Sun.COM 					bcopy(BOOTARCHIVE32_FAILSAFE,
11347656SSherry.Moore@Sun.COM 					    &fastboot_filename
11357656SSherry.Moore@Sun.COM 					    [FASTBOOT_NAME_BOOTARCHIVE]
11367656SSherry.Moore@Sun.COM 					    [bootpath_len],
11379160SSherry.Moore@Sun.COM 					    sizeof (BOOTARCHIVE32_FAILSAFE));
11387656SSherry.Moore@Sun.COM 				} else {
11397656SSherry.Moore@Sun.COM 					bcopy(BOOTARCHIVE32,
11407656SSherry.Moore@Sun.COM 					    &fastboot_filename
11417656SSherry.Moore@Sun.COM 					    [FASTBOOT_NAME_BOOTARCHIVE]
11427656SSherry.Moore@Sun.COM 					    [bootpath_len],
11437656SSherry.Moore@Sun.COM 					    sizeof (BOOTARCHIVE32));
11447656SSherry.Moore@Sun.COM 				}
11457656SSherry.Moore@Sun.COM 
11467656SSherry.Moore@Sun.COM 			} else if (ehdr->e_ident[EI_CLASS] == ELFCLASS64 &&
11477656SSherry.Moore@Sun.COM 			    ehdr->e_ident[EI_DATA] == ELFDATA2LSB &&
11487656SSherry.Moore@Sun.COM 			    ehdr->e_machine == EM_AMD64) {
11497656SSherry.Moore@Sun.COM 
11507656SSherry.Moore@Sun.COM 				if (fastboot_elf64_find_dboot_load_offset(
11517656SSherry.Moore@Sun.COM 				    (void *)va, fsize, &dboot_start_offset)
11527656SSherry.Moore@Sun.COM 				    != 0) {
115310970SSherry.Moore@Sun.COM 					cmn_err(CE_NOTE, "!Fastboot: Couldn't "
11547656SSherry.Moore@Sun.COM 					    "find ELF64 dboot entry offset");
11557656SSherry.Moore@Sun.COM 					goto err_out;
11567656SSherry.Moore@Sun.COM 				}
11577656SSherry.Moore@Sun.COM 
11587656SSherry.Moore@Sun.COM 				if ((x86_feature & X86_64) == 0 ||
11598151SKonstantin.Ananyev@Sun.COM 				    (x86_feature & X86_PAE) == 0) {
116010970SSherry.Moore@Sun.COM 					cmn_err(CE_NOTE, "!Fastboot: Cannot "
11617656SSherry.Moore@Sun.COM 					    "reboot to %s: "
11627656SSherry.Moore@Sun.COM 					    "not a 64-bit capable system",
11637656SSherry.Moore@Sun.COM 					    kern_bootfile);
11647656SSherry.Moore@Sun.COM 					goto err_out;
11657656SSherry.Moore@Sun.COM 				}
11667656SSherry.Moore@Sun.COM 
11679160SSherry.Moore@Sun.COM 				if (is_failsafe) {
11689160SSherry.Moore@Sun.COM 					/* Failsafe boot_archive */
11699160SSherry.Moore@Sun.COM 					bcopy(BOOTARCHIVE64_FAILSAFE,
11709160SSherry.Moore@Sun.COM 					    &fastboot_filename
11719160SSherry.Moore@Sun.COM 					    [FASTBOOT_NAME_BOOTARCHIVE]
11729160SSherry.Moore@Sun.COM 					    [bootpath_len],
11739160SSherry.Moore@Sun.COM 					    sizeof (BOOTARCHIVE64_FAILSAFE));
11749160SSherry.Moore@Sun.COM 				} else {
11759160SSherry.Moore@Sun.COM 					bcopy(BOOTARCHIVE64,
11769160SSherry.Moore@Sun.COM 					    &fastboot_filename
11779160SSherry.Moore@Sun.COM 					    [FASTBOOT_NAME_BOOTARCHIVE]
11789160SSherry.Moore@Sun.COM 					    [bootpath_len],
11799160SSherry.Moore@Sun.COM 					    sizeof (BOOTARCHIVE64));
11809160SSherry.Moore@Sun.COM 				}
11817656SSherry.Moore@Sun.COM 			} else {
118210970SSherry.Moore@Sun.COM 				cmn_err(CE_NOTE, "!Fastboot: Unknown ELF type");
11837656SSherry.Moore@Sun.COM 				goto err_out;
11847656SSherry.Moore@Sun.COM 			}
11857656SSherry.Moore@Sun.COM 
11867656SSherry.Moore@Sun.COM 			fb->fb_dest_pa = DBOOT_ENTRY_ADDRESS -
11877656SSherry.Moore@Sun.COM 			    dboot_start_offset;
11887656SSherry.Moore@Sun.COM 
11897656SSherry.Moore@Sun.COM 			fb->fb_next_pa = DBOOT_ENTRY_ADDRESS + fsize_roundup;
11907656SSherry.Moore@Sun.COM 		} else {
11917656SSherry.Moore@Sun.COM 			fb->fb_dest_pa = newkernel.fi_files[i - 1].fb_next_pa;
11927656SSherry.Moore@Sun.COM 			fb->fb_next_pa = fb->fb_dest_pa + fsize_roundup;
11937656SSherry.Moore@Sun.COM 		}
11947656SSherry.Moore@Sun.COM 
11957656SSherry.Moore@Sun.COM 		kobj_close_file(file);
11967656SSherry.Moore@Sun.COM 
11977656SSherry.Moore@Sun.COM 	}
11987656SSherry.Moore@Sun.COM 
11997750SSherry.Moore@Sun.COM 	/*
12007656SSherry.Moore@Sun.COM 	 * Add the function that will switch us to 32-bit protected mode
12017656SSherry.Moore@Sun.COM 	 */
12027656SSherry.Moore@Sun.COM 	fb = &newkernel.fi_files[FASTBOOT_SWTCH];
12037656SSherry.Moore@Sun.COM 	fb->fb_va = fb->fb_dest_pa = FASTBOOT_SWTCH_PA;
12048151SKonstantin.Ananyev@Sun.COM 	fb->fb_size = MMU_PAGESIZE;
12057656SSherry.Moore@Sun.COM 
12069160SSherry.Moore@Sun.COM 	hat_devload(kas.a_hat, (caddr_t)fb->fb_va,
12079160SSherry.Moore@Sun.COM 	    MMU_PAGESIZE, mmu_btop(fb->fb_dest_pa),
12089160SSherry.Moore@Sun.COM 	    PROT_READ | PROT_WRITE | PROT_EXEC,
12099160SSherry.Moore@Sun.COM 	    HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK);
12107656SSherry.Moore@Sun.COM 
12117656SSherry.Moore@Sun.COM 	/*
12127656SSherry.Moore@Sun.COM 	 * Build the new multiboot_info structure
12137656SSherry.Moore@Sun.COM 	 */
12149160SSherry.Moore@Sun.COM 	if (fastboot_build_mbi(fastboot_args, &newkernel) != 0) {
12157656SSherry.Moore@Sun.COM 		goto err_out;
12167656SSherry.Moore@Sun.COM 	}
12177656SSherry.Moore@Sun.COM 
12187656SSherry.Moore@Sun.COM 	/*
12197656SSherry.Moore@Sun.COM 	 * Build page table for low 1G physical memory. Use big pages.
12208151SKonstantin.Ananyev@Sun.COM 	 * Allocate 4 (5 for amd64) pages for the page tables.
12218151SKonstantin.Ananyev@Sun.COM 	 *    1 page for PML4 (amd64)
12227656SSherry.Moore@Sun.COM 	 *    1 page for Page-Directory-Pointer Table
12238151SKonstantin.Ananyev@Sun.COM 	 *    2 pages for Page Directory
12247656SSherry.Moore@Sun.COM 	 *    1 page for Page Table.
12257656SSherry.Moore@Sun.COM 	 * The page table entry will be rewritten to map the physical
12267656SSherry.Moore@Sun.COM 	 * address as we do the copying.
12277656SSherry.Moore@Sun.COM 	 */
12287656SSherry.Moore@Sun.COM 	if (newkernel.fi_has_pae) {
12298151SKonstantin.Ananyev@Sun.COM #ifdef	__amd64
12308151SKonstantin.Ananyev@Sun.COM 		size_t size = MMU_PAGESIZE * 5;
12318151SKonstantin.Ananyev@Sun.COM #else
12327656SSherry.Moore@Sun.COM 		size_t size = MMU_PAGESIZE * 4;
12338151SKonstantin.Ananyev@Sun.COM #endif	/* __amd64 */
12347656SSherry.Moore@Sun.COM 
12359160SSherry.Moore@Sun.COM 		if (newkernel.fi_pagetable_size && newkernel.fi_pagetable_size
12369160SSherry.Moore@Sun.COM 		    < size) {
12379160SSherry.Moore@Sun.COM 			contig_free((void *)newkernel.fi_pagetable_va,
12389160SSherry.Moore@Sun.COM 			    newkernel.fi_pagetable_size);
12399160SSherry.Moore@Sun.COM 			newkernel.fi_pagetable_size = 0;
12409160SSherry.Moore@Sun.COM 		}
12419160SSherry.Moore@Sun.COM 
12429160SSherry.Moore@Sun.COM 		if (newkernel.fi_pagetable_size == 0) {
12439160SSherry.Moore@Sun.COM 			if ((newkernel.fi_pagetable_va = (uintptr_t)
12449160SSherry.Moore@Sun.COM 			    contig_alloc(size, &fastboot_below_1G_dma_attr,
12459160SSherry.Moore@Sun.COM 			    MMU_PAGESIZE, 0)) == NULL) {
124610970SSherry.Moore@Sun.COM 				cmn_err(CE_NOTE, fastboot_enomem_msg,
12479160SSherry.Moore@Sun.COM 				    (uint64_t)size, "1G");
12489160SSherry.Moore@Sun.COM 				goto err_out;
12499160SSherry.Moore@Sun.COM 			}
12509160SSherry.Moore@Sun.COM 			/*
12519160SSherry.Moore@Sun.COM 			 * fi_pagetable_size must be set after the allocation
12529160SSherry.Moore@Sun.COM 			 * succeeds as it's used to determine how much memory to
12539160SSherry.Moore@Sun.COM 			 * free.
12549160SSherry.Moore@Sun.COM 			 */
12559160SSherry.Moore@Sun.COM 			newkernel.fi_pagetable_size = size;
12567656SSherry.Moore@Sun.COM 		}
12577656SSherry.Moore@Sun.COM 
12587656SSherry.Moore@Sun.COM 		bzero((void *)(newkernel.fi_pagetable_va), size);
12597656SSherry.Moore@Sun.COM 
12607656SSherry.Moore@Sun.COM 		newkernel.fi_pagetable_pa =
12617656SSherry.Moore@Sun.COM 		    mmu_ptob((uint64_t)hat_getpfnum(kas.a_hat,
12627656SSherry.Moore@Sun.COM 		    (caddr_t)newkernel.fi_pagetable_va));
12637656SSherry.Moore@Sun.COM 
12647656SSherry.Moore@Sun.COM 		newkernel.fi_last_table_pa = newkernel.fi_pagetable_pa +
12658151SKonstantin.Ananyev@Sun.COM 		    size - MMU_PAGESIZE;
12667656SSherry.Moore@Sun.COM 
12677656SSherry.Moore@Sun.COM 		newkernel.fi_next_table_va = newkernel.fi_pagetable_va +
12687656SSherry.Moore@Sun.COM 		    MMU_PAGESIZE;
12697656SSherry.Moore@Sun.COM 		newkernel.fi_next_table_pa = newkernel.fi_pagetable_pa +
12707656SSherry.Moore@Sun.COM 		    MMU_PAGESIZE;
12717656SSherry.Moore@Sun.COM 
12727656SSherry.Moore@Sun.COM 		fastboot_build_pagetables(&newkernel);
12737656SSherry.Moore@Sun.COM 	}
12747656SSherry.Moore@Sun.COM 
12757656SSherry.Moore@Sun.COM 
12769160SSherry.Moore@Sun.COM 	/* Generate MD5 checksums */
12779160SSherry.Moore@Sun.COM 	fastboot_cksum_generate(&newkernel);
12789160SSherry.Moore@Sun.COM 
12797656SSherry.Moore@Sun.COM 	/* Mark it as valid */
12807656SSherry.Moore@Sun.COM 	newkernel.fi_valid = 1;
12817656SSherry.Moore@Sun.COM 	newkernel.fi_magic = FASTBOOT_MAGIC;
12827656SSherry.Moore@Sun.COM 
12839160SSherry.Moore@Sun.COM 	postbootkernelbase = saved_kernelbase;
12847656SSherry.Moore@Sun.COM 	return;
12857656SSherry.Moore@Sun.COM 
12867656SSherry.Moore@Sun.COM err_out:
12879160SSherry.Moore@Sun.COM 	postbootkernelbase = saved_kernelbase;
12887656SSherry.Moore@Sun.COM 	newkernel.fi_valid = 0;
12899160SSherry.Moore@Sun.COM 	fastboot_free_newkernel(&newkernel);
12909160SSherry.Moore@Sun.COM }
12919160SSherry.Moore@Sun.COM 
12929160SSherry.Moore@Sun.COM 
12939160SSherry.Moore@Sun.COM /* ARGSUSED */
12949160SSherry.Moore@Sun.COM static int
12959160SSherry.Moore@Sun.COM fastboot_xc_func(fastboot_info_t *nk, xc_arg_t unused2, xc_arg_t unused3)
12969160SSherry.Moore@Sun.COM {
12979160SSherry.Moore@Sun.COM 	void (*fastboot_func)(fastboot_info_t *);
12989160SSherry.Moore@Sun.COM 	fastboot_file_t	*fb = &nk->fi_files[FASTBOOT_SWTCH];
12999160SSherry.Moore@Sun.COM 	fastboot_func = (void (*)())(fb->fb_va);
13009160SSherry.Moore@Sun.COM 	kthread_t *t_intr = curthread->t_intr;
13019160SSherry.Moore@Sun.COM 
13029160SSherry.Moore@Sun.COM 	if (&kas != curproc->p_as) {
13039160SSherry.Moore@Sun.COM 		hat_devload(curproc->p_as->a_hat, (caddr_t)fb->fb_va,
13049160SSherry.Moore@Sun.COM 		    MMU_PAGESIZE, mmu_btop(fb->fb_dest_pa),
13059160SSherry.Moore@Sun.COM 		    PROT_READ | PROT_WRITE | PROT_EXEC,
13069160SSherry.Moore@Sun.COM 		    HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK);
13079160SSherry.Moore@Sun.COM 	}
13089160SSherry.Moore@Sun.COM 
13099160SSherry.Moore@Sun.COM 	/*
13109160SSherry.Moore@Sun.COM 	 * If we have pinned a thread, make sure the address is mapped
13119160SSherry.Moore@Sun.COM 	 * in the address space of the pinned thread.
13129160SSherry.Moore@Sun.COM 	 */
13139160SSherry.Moore@Sun.COM 	if (t_intr && t_intr->t_procp->p_as->a_hat != curproc->p_as->a_hat &&
13149160SSherry.Moore@Sun.COM 	    t_intr->t_procp->p_as != &kas)
13159160SSherry.Moore@Sun.COM 		hat_devload(t_intr->t_procp->p_as->a_hat, (caddr_t)fb->fb_va,
13169160SSherry.Moore@Sun.COM 		    MMU_PAGESIZE, mmu_btop(fb->fb_dest_pa),
13179160SSherry.Moore@Sun.COM 		    PROT_READ | PROT_WRITE | PROT_EXEC,
13189160SSherry.Moore@Sun.COM 		    HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK);
13199160SSherry.Moore@Sun.COM 
13209160SSherry.Moore@Sun.COM 	(*psm_shutdownf)(A_SHUTDOWN, AD_FASTREBOOT);
13219160SSherry.Moore@Sun.COM 	(*fastboot_func)(nk);
13229160SSherry.Moore@Sun.COM 
13239160SSherry.Moore@Sun.COM 	/*NOTREACHED*/
13249160SSherry.Moore@Sun.COM 	return (0);
13257656SSherry.Moore@Sun.COM }
13267656SSherry.Moore@Sun.COM 
13277750SSherry.Moore@Sun.COM /*
13287750SSherry.Moore@Sun.COM  * Jump to the fast reboot switcher.  This function never returns.
13297750SSherry.Moore@Sun.COM  */
13307656SSherry.Moore@Sun.COM void
13317656SSherry.Moore@Sun.COM fast_reboot()
13327656SSherry.Moore@Sun.COM {
13339160SSherry.Moore@Sun.COM 	processorid_t bootcpuid = 0;
13349160SSherry.Moore@Sun.COM 	extern uintptr_t postbootkernelbase;
13359160SSherry.Moore@Sun.COM 	extern char	fb_swtch_image[];
13369160SSherry.Moore@Sun.COM 	fastboot_file_t	*fb;
13379160SSherry.Moore@Sun.COM 	int i;
13389160SSherry.Moore@Sun.COM 
13399160SSherry.Moore@Sun.COM 	postbootkernelbase = 0;
13409160SSherry.Moore@Sun.COM 
13419160SSherry.Moore@Sun.COM 	fb = &newkernel.fi_files[FASTBOOT_SWTCH];
13429160SSherry.Moore@Sun.COM 
13439160SSherry.Moore@Sun.COM 	/*
13449160SSherry.Moore@Sun.COM 	 * Map the address into both the current proc's address
13459160SSherry.Moore@Sun.COM 	 * space and the kernel's address space in case the panic
13469160SSherry.Moore@Sun.COM 	 * is forced by kmdb.
13479160SSherry.Moore@Sun.COM 	 */
13489160SSherry.Moore@Sun.COM 	if (&kas != curproc->p_as) {
13499160SSherry.Moore@Sun.COM 		hat_devload(curproc->p_as->a_hat, (caddr_t)fb->fb_va,
13509160SSherry.Moore@Sun.COM 		    MMU_PAGESIZE, mmu_btop(fb->fb_dest_pa),
13519160SSherry.Moore@Sun.COM 		    PROT_READ | PROT_WRITE | PROT_EXEC,
13529160SSherry.Moore@Sun.COM 		    HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK);
13539160SSherry.Moore@Sun.COM 	}
13549160SSherry.Moore@Sun.COM 
13559160SSherry.Moore@Sun.COM 	bcopy((void *)fb_swtch_image, (void *)fb->fb_va, fb->fb_size);
13569160SSherry.Moore@Sun.COM 
13579160SSherry.Moore@Sun.COM 
13589160SSherry.Moore@Sun.COM 	/*
13599160SSherry.Moore@Sun.COM 	 * Set fb_va to fake_va
13609160SSherry.Moore@Sun.COM 	 */
13619160SSherry.Moore@Sun.COM 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
13629160SSherry.Moore@Sun.COM 		newkernel.fi_files[i].fb_va = fake_va;
13639160SSherry.Moore@Sun.COM 
13649160SSherry.Moore@Sun.COM 	}
13659160SSherry.Moore@Sun.COM 
13669160SSherry.Moore@Sun.COM 	if (panicstr && CPU->cpu_id != bootcpuid &&
13679160SSherry.Moore@Sun.COM 	    CPU_ACTIVE(cpu_get(bootcpuid))) {
13689489SJoe.Bonasera@sun.com 		extern void panic_idle(void);
13699160SSherry.Moore@Sun.COM 		cpuset_t cpuset;
13709160SSherry.Moore@Sun.COM 
13719160SSherry.Moore@Sun.COM 		CPUSET_ZERO(cpuset);
13729160SSherry.Moore@Sun.COM 		CPUSET_ADD(cpuset, bootcpuid);
13739489SJoe.Bonasera@sun.com 		xc_priority((xc_arg_t)&newkernel, 0, 0, CPUSET2BV(cpuset),
13749160SSherry.Moore@Sun.COM 		    (xc_func_t)fastboot_xc_func);
13759160SSherry.Moore@Sun.COM 
13769489SJoe.Bonasera@sun.com 		panic_idle();
13779160SSherry.Moore@Sun.COM 	} else
13789160SSherry.Moore@Sun.COM 		(void) fastboot_xc_func(&newkernel, 0, 0);
13799160SSherry.Moore@Sun.COM }
13809160SSherry.Moore@Sun.COM 
13819160SSherry.Moore@Sun.COM 
13829160SSherry.Moore@Sun.COM /*
13839160SSherry.Moore@Sun.COM  * Get boot property value for fastreboot_onpanic.
13849160SSherry.Moore@Sun.COM  *
13859160SSherry.Moore@Sun.COM  * NOTE: If fastreboot_onpanic is set to non-zero in /etc/system,
13869160SSherry.Moore@Sun.COM  * new setting passed in via "-B fastreboot_onpanic" is ignored.
13879160SSherry.Moore@Sun.COM  * This order of precedence is to enable developers debugging panics
13889160SSherry.Moore@Sun.COM  * that occur early in boot to utilize Fast Reboot on panic.
13899160SSherry.Moore@Sun.COM  */
13909160SSherry.Moore@Sun.COM static void
13919160SSherry.Moore@Sun.COM fastboot_get_bootprop(void)
13929160SSherry.Moore@Sun.COM {
13939160SSherry.Moore@Sun.COM 	int		val = 0xaa, len, ret;
13949160SSherry.Moore@Sun.COM 	dev_info_t	*devi;
13959160SSherry.Moore@Sun.COM 	char		*propstr = NULL;
13969160SSherry.Moore@Sun.COM 
13979160SSherry.Moore@Sun.COM 	devi = ddi_root_node();
13987656SSherry.Moore@Sun.COM 
13999160SSherry.Moore@Sun.COM 	ret = ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
14009160SSherry.Moore@Sun.COM 	    FASTREBOOT_ONPANIC, &propstr);
14019160SSherry.Moore@Sun.COM 
14029160SSherry.Moore@Sun.COM 	if (ret == DDI_PROP_SUCCESS) {
14039160SSherry.Moore@Sun.COM 		if (FASTREBOOT_ONPANIC_NOTSET(propstr))
14049160SSherry.Moore@Sun.COM 			val = 0;
14059160SSherry.Moore@Sun.COM 		else if (FASTREBOOT_ONPANIC_ISSET(propstr))
14069160SSherry.Moore@Sun.COM 			val = UA_FASTREBOOT_ONPANIC;
14079160SSherry.Moore@Sun.COM 
14089160SSherry.Moore@Sun.COM 		/*
14099160SSherry.Moore@Sun.COM 		 * Only set fastreboot_onpanic to the value passed in
14109160SSherry.Moore@Sun.COM 		 * if it's not already set to non-zero, and the value
14119160SSherry.Moore@Sun.COM 		 * has indeed been passed in via command line.
14129160SSherry.Moore@Sun.COM 		 */
14139160SSherry.Moore@Sun.COM 		if (!fastreboot_onpanic && val != 0xaa)
14149160SSherry.Moore@Sun.COM 			fastreboot_onpanic = val;
14159160SSherry.Moore@Sun.COM 		ddi_prop_free(propstr);
14169160SSherry.Moore@Sun.COM 	} else if (ret != DDI_PROP_NOT_FOUND && ret != DDI_PROP_UNDEFINED) {
141710970SSherry.Moore@Sun.COM 		cmn_err(CE_NOTE, "!%s value is invalid, will be ignored",
14189160SSherry.Moore@Sun.COM 		    FASTREBOOT_ONPANIC);
14199160SSherry.Moore@Sun.COM 	}
14209160SSherry.Moore@Sun.COM 
14219160SSherry.Moore@Sun.COM 	len = sizeof (fastreboot_onpanic_cmdline);
14229160SSherry.Moore@Sun.COM 	ret = ddi_getlongprop_buf(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
14239160SSherry.Moore@Sun.COM 	    FASTREBOOT_ONPANIC_CMDLINE, fastreboot_onpanic_cmdline, &len);
14249160SSherry.Moore@Sun.COM 
14259160SSherry.Moore@Sun.COM 	if (ret == DDI_PROP_BUF_TOO_SMALL)
142610970SSherry.Moore@Sun.COM 		cmn_err(CE_NOTE, "!%s value is too long, will be ignored",
14279160SSherry.Moore@Sun.COM 		    FASTREBOOT_ONPANIC_CMDLINE);
14287656SSherry.Moore@Sun.COM }
14299160SSherry.Moore@Sun.COM 
14309160SSherry.Moore@Sun.COM /*
14319160SSherry.Moore@Sun.COM  * This function is called by main() to either load the backup kernel for panic
14329160SSherry.Moore@Sun.COM  * fast reboot, or to reserve low physical memory for fast reboot.
14339160SSherry.Moore@Sun.COM  */
14349160SSherry.Moore@Sun.COM void
14359160SSherry.Moore@Sun.COM fastboot_post_startup()
14369160SSherry.Moore@Sun.COM {
143710916SSherry.Moore@Sun.COM 	lbolt_at_boot = ddi_get_lbolt();
143810916SSherry.Moore@Sun.COM 
143910916SSherry.Moore@Sun.COM 	/* Default to 10 minutes */
144010916SSherry.Moore@Sun.COM 	if (fastreboot_onpanic_uptime == LONG_MAX)
144110916SSherry.Moore@Sun.COM 		fastreboot_onpanic_uptime = SEC_TO_TICK(10 * 60);
144210916SSherry.Moore@Sun.COM 
14439160SSherry.Moore@Sun.COM 	if (!fastreboot_capable)
14449160SSherry.Moore@Sun.COM 		return;
14459160SSherry.Moore@Sun.COM 
144610559SSherry.Moore@Sun.COM 	mutex_enter(&fastreboot_config_mutex);
144710559SSherry.Moore@Sun.COM 
14489160SSherry.Moore@Sun.COM 	fastboot_get_bootprop();
14499160SSherry.Moore@Sun.COM 
14509160SSherry.Moore@Sun.COM 	if (fastreboot_onpanic)
14519160SSherry.Moore@Sun.COM 		fastboot_load_kernel(fastreboot_onpanic_cmdline);
14529160SSherry.Moore@Sun.COM 	else if (reserve_mem_enabled)
14539160SSherry.Moore@Sun.COM 		fastboot_reserve_mem(&newkernel);
145410559SSherry.Moore@Sun.COM 
145510559SSherry.Moore@Sun.COM 	mutex_exit(&fastreboot_config_mutex);
14569160SSherry.Moore@Sun.COM }
14579160SSherry.Moore@Sun.COM 
14589160SSherry.Moore@Sun.COM /*
14599160SSherry.Moore@Sun.COM  * Update boot configuration settings.
14609160SSherry.Moore@Sun.COM  * If the new fastreboot_onpanic setting is false, and a kernel has
14619160SSherry.Moore@Sun.COM  * been preloaded, free the memory;
14629160SSherry.Moore@Sun.COM  * if the new fastreboot_onpanic setting is true and newkernel is
14639160SSherry.Moore@Sun.COM  * not valid, load the new kernel.
14649160SSherry.Moore@Sun.COM  */
14659160SSherry.Moore@Sun.COM void
14669160SSherry.Moore@Sun.COM fastboot_update_config(const char *mdep)
14679160SSherry.Moore@Sun.COM {
14689160SSherry.Moore@Sun.COM 	uint8_t boot_config = (uint8_t)*mdep;
146910559SSherry.Moore@Sun.COM 	int cur_fastreboot_onpanic;
14709160SSherry.Moore@Sun.COM 
14719160SSherry.Moore@Sun.COM 	if (!fastreboot_capable)
14729160SSherry.Moore@Sun.COM 		return;
14739160SSherry.Moore@Sun.COM 
147410559SSherry.Moore@Sun.COM 	mutex_enter(&fastreboot_config_mutex);
147510559SSherry.Moore@Sun.COM 
147610559SSherry.Moore@Sun.COM 	cur_fastreboot_onpanic = fastreboot_onpanic;
14779160SSherry.Moore@Sun.COM 	fastreboot_onpanic = boot_config & UA_FASTREBOOT_ONPANIC;
147810559SSherry.Moore@Sun.COM 
14799160SSherry.Moore@Sun.COM 	if (fastreboot_onpanic && (!cur_fastreboot_onpanic ||
14809160SSherry.Moore@Sun.COM 	    !newkernel.fi_valid))
14819160SSherry.Moore@Sun.COM 		fastboot_load_kernel(fastreboot_onpanic_cmdline);
14829160SSherry.Moore@Sun.COM 	if (cur_fastreboot_onpanic && !fastreboot_onpanic)
14839160SSherry.Moore@Sun.COM 		fastboot_free_newkernel(&newkernel);
148410559SSherry.Moore@Sun.COM 
148510559SSherry.Moore@Sun.COM 	mutex_exit(&fastreboot_config_mutex);
14869160SSherry.Moore@Sun.COM }
148710559SSherry.Moore@Sun.COM 
148810559SSherry.Moore@Sun.COM /*
1489*11686SKonstantin.Ananyev@Sun.COM  * This is an internal interface to disable Fast Reboot on Panic.
1490*11686SKonstantin.Ananyev@Sun.COM  * It frees up memory allocated for the backup kernel and sets
1491*11686SKonstantin.Ananyev@Sun.COM  * fastreboot_onpanic to zero.
149210559SSherry.Moore@Sun.COM  */
1493*11686SKonstantin.Ananyev@Sun.COM static void
1494*11686SKonstantin.Ananyev@Sun.COM fastreboot_onpanic_disable(void)
149510559SSherry.Moore@Sun.COM {
149610559SSherry.Moore@Sun.COM 	uint8_t boot_config = (uint8_t)(~UA_FASTREBOOT_ONPANIC);
149710559SSherry.Moore@Sun.COM 	fastboot_update_config((const char *)&boot_config);
149810559SSherry.Moore@Sun.COM }
149910559SSherry.Moore@Sun.COM 
150010559SSherry.Moore@Sun.COM /*
150110559SSherry.Moore@Sun.COM  * This is the interface to be called by fm_panic() in case FMA has diagnosed
150210559SSherry.Moore@Sun.COM  * a terminal machine check exception.  It does not free up memory allocated
150310559SSherry.Moore@Sun.COM  * for the backup kernel.  General disabling fastreboot_onpanic in a
1504*11686SKonstantin.Ananyev@Sun.COM  * non-panicking situation must go through fastboot_onpanic_disable().
150510559SSherry.Moore@Sun.COM  */
150610559SSherry.Moore@Sun.COM void
1507*11686SKonstantin.Ananyev@Sun.COM fastreboot_disable_highpil(void)
150810559SSherry.Moore@Sun.COM {
150910559SSherry.Moore@Sun.COM 	fastreboot_onpanic = 0;
151010559SSherry.Moore@Sun.COM }
151110559SSherry.Moore@Sun.COM 
1512*11686SKonstantin.Ananyev@Sun.COM /*
1513*11686SKonstantin.Ananyev@Sun.COM  * This is an internal interface to disable Fast Reboot by Default.
1514*11686SKonstantin.Ananyev@Sun.COM  * It does not free up memory allocated for the backup kernel.
1515*11686SKonstantin.Ananyev@Sun.COM  */
1516*11686SKonstantin.Ananyev@Sun.COM static void
1517*11686SKonstantin.Ananyev@Sun.COM fastreboot_capable_disable(uint32_t msgid)
1518*11686SKonstantin.Ananyev@Sun.COM {
1519*11686SKonstantin.Ananyev@Sun.COM 	if (fastreboot_capable != 0) {
1520*11686SKonstantin.Ananyev@Sun.COM 		fastreboot_capable = 0;
1521*11686SKonstantin.Ananyev@Sun.COM 		if (msgid < sizeof (fastreboot_nosup_desc) /
1522*11686SKonstantin.Ananyev@Sun.COM 		    sizeof (fastreboot_nosup_desc[0]))
1523*11686SKonstantin.Ananyev@Sun.COM 			fastreboot_nosup_id = msgid;
1524*11686SKonstantin.Ananyev@Sun.COM 		else
1525*11686SKonstantin.Ananyev@Sun.COM 			fastreboot_nosup_id = FBNS_DEFAULT;
1526*11686SKonstantin.Ananyev@Sun.COM 	}
1527*11686SKonstantin.Ananyev@Sun.COM }
1528*11686SKonstantin.Ananyev@Sun.COM 
1529*11686SKonstantin.Ananyev@Sun.COM /*
1530*11686SKonstantin.Ananyev@Sun.COM  * This is the kernel interface for disabling
1531*11686SKonstantin.Ananyev@Sun.COM  * Fast Reboot by Default and Fast Reboot on Panic.
1532*11686SKonstantin.Ananyev@Sun.COM  * Frees up memory allocated for the backup kernel.
1533*11686SKonstantin.Ananyev@Sun.COM  * General disabling of the Fast Reboot by Default feature should be done
1534*11686SKonstantin.Ananyev@Sun.COM  * via the userland interface scf_fastreboot_default_set_transient().
1535*11686SKonstantin.Ananyev@Sun.COM  */
1536*11686SKonstantin.Ananyev@Sun.COM void
1537*11686SKonstantin.Ananyev@Sun.COM fastreboot_disable(uint32_t msgid)
1538*11686SKonstantin.Ananyev@Sun.COM {
1539*11686SKonstantin.Ananyev@Sun.COM 	fastreboot_capable_disable(msgid);
1540*11686SKonstantin.Ananyev@Sun.COM 	fastreboot_onpanic_disable();
1541*11686SKonstantin.Ananyev@Sun.COM }
1542*11686SKonstantin.Ananyev@Sun.COM 
1543*11686SKonstantin.Ananyev@Sun.COM /*
1544*11686SKonstantin.Ananyev@Sun.COM  * Returns Fast Reboot not support message for fastreboot_nosup_id.
1545*11686SKonstantin.Ananyev@Sun.COM  * If fastreboot_nosup_id contains invalid index, default
1546*11686SKonstantin.Ananyev@Sun.COM  * Fast Reboot not support message is returned.
1547*11686SKonstantin.Ananyev@Sun.COM  */
1548*11686SKonstantin.Ananyev@Sun.COM const char *
1549*11686SKonstantin.Ananyev@Sun.COM fastreboot_nosup_message(void)
1550*11686SKonstantin.Ananyev@Sun.COM {
1551*11686SKonstantin.Ananyev@Sun.COM 	uint32_t msgid;
1552*11686SKonstantin.Ananyev@Sun.COM 
1553*11686SKonstantin.Ananyev@Sun.COM 	msgid = fastreboot_nosup_id;
1554*11686SKonstantin.Ananyev@Sun.COM 	if (msgid >= sizeof (fastreboot_nosup_desc) /
1555*11686SKonstantin.Ananyev@Sun.COM 	    sizeof (fastreboot_nosup_desc[0]))
1556*11686SKonstantin.Ananyev@Sun.COM 		msgid = FBNS_DEFAULT;
1557*11686SKonstantin.Ananyev@Sun.COM 
1558*11686SKonstantin.Ananyev@Sun.COM 	return (fastreboot_nosup_desc[msgid]);
1559*11686SKonstantin.Ananyev@Sun.COM }
156010559SSherry.Moore@Sun.COM 
156110559SSherry.Moore@Sun.COM /*
156210559SSherry.Moore@Sun.COM  * A simplified interface for uadmin to call to update the configuration
156310559SSherry.Moore@Sun.COM  * setting and load a new kernel if necessary.
156410559SSherry.Moore@Sun.COM  */
156510559SSherry.Moore@Sun.COM void
156610559SSherry.Moore@Sun.COM fastboot_update_and_load(int fcn, char *mdep)
156710559SSherry.Moore@Sun.COM {
156810559SSherry.Moore@Sun.COM 	if (fcn != AD_FASTREBOOT) {
156910559SSherry.Moore@Sun.COM 		/*
157010559SSherry.Moore@Sun.COM 		 * If user has explicitly requested reboot to prom,
157110559SSherry.Moore@Sun.COM 		 * or uadmin(1M) was invoked with other functions,
157210559SSherry.Moore@Sun.COM 		 * don't try to fast reboot after dumping.
157310559SSherry.Moore@Sun.COM 		 */
1574*11686SKonstantin.Ananyev@Sun.COM 		fastreboot_onpanic_disable();
157510559SSherry.Moore@Sun.COM 	}
157610559SSherry.Moore@Sun.COM 
157710559SSherry.Moore@Sun.COM 	mutex_enter(&fastreboot_config_mutex);
157810559SSherry.Moore@Sun.COM 
157910559SSherry.Moore@Sun.COM 	if (fastreboot_onpanic)
158010559SSherry.Moore@Sun.COM 		fastboot_load_kernel(mdep);
158110559SSherry.Moore@Sun.COM 
158210559SSherry.Moore@Sun.COM 	mutex_exit(&fastreboot_config_mutex);
158310559SSherry.Moore@Sun.COM }
1584