xref: /illumos-gate/usr/src/uts/i86pc/os/fastboot.c (revision 7a088f03b431bdffa96c3b2175964d4d38420caa)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * This file contains the functions for performing Fast Reboot -- a
29  * reboot which bypasses the firmware and bootloader, considerably
30  * reducing downtime.
31  *
32  * fastboot_load_kernel(): This function is invoked by mdpreboot() in the
33  * reboot path.  It loads the new kernel and boot archive into memory, builds
34  * the data structure containing sufficient information about the new
35  * kernel and boot archive to be passed to the fast reboot switcher
36  * (see fb_swtch_src.s for details).  When invoked the switcher relocates
37  * the new kernel and boot archive to physically contiguous low memory,
38  * similar to where the boot loader would have loaded them, and jumps to
39  * the new kernel.
40  *
41  * If fastreboot_onpanic is enabled, fastboot_load_kernel() is called
42  * by fastreboot_post_startup() to load the back up kernel in case of
43  * panic.
44  *
45  * The physical addresses of the memory allocated for the new kernel, boot
46  * archive and their page tables must be above where the boot archive ends
47  * after it has been relocated by the switcher, otherwise the new files
48  * and their page tables could be overridden during relocation.
49  *
50  * fast_reboot(): This function is invoked by mdboot() once it's determined
51  * that the system is capable of fast reboot.  It jumps to the fast reboot
52  * switcher with the data structure built by fastboot_load_kernel() as the
53  * argument.
54  */
55 
56 #include <sys/types.h>
57 #include <sys/param.h>
58 #include <sys/segments.h>
59 #include <sys/sysmacros.h>
60 #include <sys/vm.h>
61 
62 #include <sys/proc.h>
63 #include <sys/buf.h>
64 #include <sys/kmem.h>
65 
66 #include <sys/reboot.h>
67 #include <sys/uadmin.h>
68 
69 #include <sys/cred.h>
70 #include <sys/vnode.h>
71 #include <sys/file.h>
72 
73 #include <sys/cmn_err.h>
74 #include <sys/dumphdr.h>
75 #include <sys/bootconf.h>
76 #include <sys/ddidmareq.h>
77 #include <sys/varargs.h>
78 #include <sys/promif.h>
79 #include <sys/modctl.h>
80 
81 #include <vm/hat.h>
82 #include <vm/as.h>
83 #include <vm/page.h>
84 #include <vm/seg.h>
85 #include <vm/hat_i86.h>
86 #include <sys/vm_machparam.h>
87 #include <sys/archsystm.h>
88 #include <sys/machsystm.h>
89 #include <sys/mman.h>
90 #include <sys/x86_archext.h>
91 #include <sys/smp_impldefs.h>
92 #include <sys/spl.h>
93 
94 #include <sys/fastboot.h>
95 #include <sys/machelf.h>
96 #include <sys/kobj.h>
97 #include <sys/multiboot.h>
98 #include <sys/kobj_lex.h>
99 
100 /*
101  * Macro to determine how many pages are needed for PTEs to map a particular
102  * file.  Allocate one extra page table entry for terminating the list.
103  */
104 #define	FASTBOOT_PTE_LIST_SIZE(fsize)	\
105 	P2ROUNDUP((((fsize) >> PAGESHIFT) + 1) * sizeof (x86pte_t), PAGESIZE)
106 
107 /*
108  * Data structure containing necessary information for the fast reboot
109  * switcher to jump to the new kernel.
110  */
111 fastboot_info_t newkernel = { 0 };
112 char		fastboot_args[OBP_MAXPATHLEN];
113 
114 static char fastboot_filename[2][OBP_MAXPATHLEN] = { { 0 }, { 0 }};
115 static x86pte_t ptp_bits = PT_VALID | PT_REF | PT_USER | PT_WRITABLE;
116 static x86pte_t pte_bits =
117     PT_VALID | PT_REF | PT_MOD | PT_NOCONSIST | PT_WRITABLE;
118 static uint_t fastboot_shift_amt_pae[] = {12, 21, 30, 39};
119 
120 int fastboot_debug = 0;
121 int fastboot_contig = 0;
122 
123 /*
124  * Fake starting va for new kernel and boot archive.
125  */
126 static uintptr_t fake_va = FASTBOOT_FAKE_VA;
127 
128 /*
129  * Reserve memory below PA 1G in preparation of fast reboot.
130  *
131  * This variable is only checked when fastreboot_capable is set, but
132  * fastreboot_onpanic is not set.  The amount of memory reserved
133  * is negligible, but just in case we are really short of low memory,
134  * this variable will give us a backdoor to not consume memory at all.
135  */
136 int reserve_mem_enabled = 1;
137 
138 /*
139  * Amount of memory below PA 1G to reserve for constructing the multiboot
140  * data structure and the page tables as we tend to run out of those
141  * when more drivers are loaded.
142  */
143 static size_t fastboot_mbi_size = 0x2000;	/* 8K */
144 static size_t fastboot_pagetable_size = 0x5000;	/* 20K */
145 
146 /*
147  * Use below 1G for page tables as
148  *	1. we are only doing 1:1 mapping of the bottom 1G of physical memory.
149  *	2. we are using 2G as the fake virtual address for the new kernel and
150  *	boot archive.
151  */
152 static ddi_dma_attr_t fastboot_below_1G_dma_attr = {
153 	DMA_ATTR_V0,
154 	0x0000000008000000ULL,	/* dma_attr_addr_lo: 128MB */
155 	0x000000003FFFFFFFULL,	/* dma_attr_addr_hi: 1G */
156 	0x00000000FFFFFFFFULL,	/* dma_attr_count_max */
157 	0x0000000000001000ULL,	/* dma_attr_align: 4KB */
158 	1,			/* dma_attr_burstsize */
159 	1,			/* dma_attr_minxfer */
160 	0x00000000FFFFFFFFULL,	/* dma_attr_maxxfer */
161 	0x00000000FFFFFFFFULL,	/* dma_attr_seg */
162 	1,			/* dma_attr_sgllen */
163 	0x1000ULL,		/* dma_attr_granular */
164 	0,			/* dma_attr_flags */
165 };
166 
167 static ddi_dma_attr_t fastboot_dma_attr = {
168 	DMA_ATTR_V0,
169 	0x0000000008000000ULL,	/* dma_attr_addr_lo: 128MB */
170 #ifdef	__amd64
171 	0xFFFFFFFFFFFFFFFFULL,	/* dma_attr_addr_hi: 2^64B */
172 #else
173 	0x0000000FFFFFFFFFULL,	/* dma_attr_addr_hi: 64GB */
174 #endif	/* __amd64 */
175 	0x00000000FFFFFFFFULL,	/* dma_attr_count_max */
176 	0x0000000000001000ULL,	/* dma_attr_align: 4KB */
177 	1,			/* dma_attr_burstsize */
178 	1,			/* dma_attr_minxfer */
179 	0x00000000FFFFFFFFULL,	/* dma_attr_maxxfer */
180 	0x00000000FFFFFFFFULL,	/* dma_attr_seg */
181 	1,			/* dma_attr_sgllen */
182 	0x1000ULL,		/* dma_attr_granular */
183 	0,			/* dma_attr_flags */
184 };
185 
186 /*
187  * Various information saved from the previous boot to reconstruct
188  * multiboot_info.
189  */
190 extern multiboot_info_t saved_mbi;
191 extern mb_memory_map_t saved_mmap[FASTBOOT_SAVED_MMAP_COUNT];
192 extern struct sol_netinfo saved_drives[FASTBOOT_SAVED_DRIVES_COUNT];
193 extern char saved_cmdline[FASTBOOT_SAVED_CMDLINE_LEN];
194 extern int saved_cmdline_len;
195 extern size_t saved_file_size[];
196 
197 extern void* contig_alloc(size_t size, ddi_dma_attr_t *attr,
198     uintptr_t align, int cansleep);
199 extern void contig_free(void *addr, size_t size);
200 
201 
202 /* PRINTLIKE */
203 extern void vprintf(const char *, va_list);
204 
205 
206 /*
207  * Need to be able to get boot_archives from other places
208  */
209 #define	BOOTARCHIVE64	"/platform/i86pc/amd64/boot_archive"
210 #define	BOOTARCHIVE32	"/platform/i86pc/boot_archive"
211 #define	BOOTARCHIVE32_FAILSAFE	"/boot/x86.miniroot-safe"
212 #define	BOOTARCHIVE64_FAILSAFE	"/boot/amd64/x86.miniroot-safe"
213 #define	FAILSAFE_BOOTFILE32	"/boot/platform/i86pc/kernel/unix"
214 #define	FAILSAFE_BOOTFILE64	"/boot/platform/i86pc/kernel/amd64/unix"
215 
216 static uint_t fastboot_vatoindex(fastboot_info_t *, uintptr_t, int);
217 static void fastboot_map_with_size(fastboot_info_t *, uintptr_t,
218     paddr_t, size_t, int);
219 static void fastboot_build_pagetables(fastboot_info_t *);
220 static int fastboot_build_mbi(char *, fastboot_info_t *);
221 static void fastboot_free_file(fastboot_file_t *);
222 
223 static const char fastboot_enomem_msg[] = "Fastboot: Couldn't allocate 0x%"
224 	PRIx64" bytes below %s to do fast reboot";
225 
226 static void
227 dprintf(char *fmt, ...)
228 {
229 	va_list adx;
230 
231 	if (!fastboot_debug)
232 		return;
233 
234 	va_start(adx, fmt);
235 	vprintf(fmt, adx);
236 	va_end(adx);
237 }
238 
239 
240 /*
241  * Return the index corresponding to a virt address at a given page table level.
242  */
243 static uint_t
244 fastboot_vatoindex(fastboot_info_t *nk, uintptr_t va, int level)
245 {
246 	return ((va >> nk->fi_shift_amt[level]) & (nk->fi_ptes_per_table - 1));
247 }
248 
249 
250 /*
251  * Add mapping from vstart to pstart for the specified size.
252  * vstart, pstart and size should all have been aligned at 2M boundaries.
253  */
254 static void
255 fastboot_map_with_size(fastboot_info_t *nk, uintptr_t vstart, paddr_t pstart,
256     size_t size, int level)
257 {
258 	x86pte_t	pteval, *table;
259 	uintptr_t	vaddr;
260 	paddr_t		paddr;
261 	int		index, l;
262 
263 	table = (x86pte_t *)(nk->fi_pagetable_va);
264 
265 	for (l = nk->fi_top_level; l >= level; l--) {
266 
267 		index = fastboot_vatoindex(nk, vstart, l);
268 
269 		if (l == level) {
270 			/*
271 			 * Last level.  Program the page table entries.
272 			 */
273 			for (vaddr = vstart, paddr = pstart;
274 			    vaddr < vstart + size;
275 			    vaddr += (1ULL << nk->fi_shift_amt[l]),
276 			    paddr += (1ULL << nk->fi_shift_amt[l])) {
277 
278 				uint_t index = fastboot_vatoindex(nk, vaddr, l);
279 
280 				if (l > 0)
281 					pteval = paddr | pte_bits | PT_PAGESIZE;
282 				else
283 					pteval = paddr | pte_bits;
284 
285 				table[index] = pteval;
286 			}
287 		} else if (table[index] & PT_VALID) {
288 
289 			table = (x86pte_t *)
290 			    ((uintptr_t)(((paddr_t)table[index] & MMU_PAGEMASK)
291 			    - nk->fi_pagetable_pa) + nk->fi_pagetable_va);
292 		} else {
293 			/*
294 			 * Intermediate levels.
295 			 * Program with either valid bit or PTP bits.
296 			 */
297 			if (l == nk->fi_top_level) {
298 #ifdef	__amd64
299 				ASSERT(nk->fi_top_level == 3);
300 				table[index] = nk->fi_next_table_pa | ptp_bits;
301 #else
302 				table[index] = nk->fi_next_table_pa | PT_VALID;
303 #endif	/* __amd64 */
304 			} else {
305 				table[index] = nk->fi_next_table_pa | ptp_bits;
306 			}
307 			table = (x86pte_t *)(nk->fi_next_table_va);
308 			nk->fi_next_table_va += MMU_PAGESIZE;
309 			nk->fi_next_table_pa += MMU_PAGESIZE;
310 		}
311 	}
312 }
313 
314 /*
315  * Build page tables for the lower 1G of physical memory using 2M
316  * pages, and prepare page tables for mapping new kernel and boot
317  * archive pages using 4K pages.
318  */
319 static void
320 fastboot_build_pagetables(fastboot_info_t *nk)
321 {
322 	/*
323 	 * Map lower 1G physical memory.  Use large pages.
324 	 */
325 	fastboot_map_with_size(nk, 0, 0, ONE_GIG, 1);
326 
327 	/*
328 	 * Map one 4K page to get the middle page tables set up.
329 	 */
330 	fake_va = P2ALIGN_TYPED(fake_va, nk->fi_lpagesize, uintptr_t);
331 	fastboot_map_with_size(nk, fake_va,
332 	    nk->fi_files[0].fb_pte_list_va[0] & MMU_PAGEMASK, PAGESIZE, 0);
333 }
334 
335 
336 /*
337  * Sanity check.  Look for dboot offset.
338  */
339 static int
340 fastboot_elf64_find_dboot_load_offset(void *img, off_t imgsz, uint32_t *offp)
341 {
342 	Elf64_Ehdr	*ehdr = (Elf64_Ehdr *)img;
343 	Elf64_Phdr	*phdr;
344 	uint8_t		*phdrbase;
345 	int		i;
346 
347 	if ((ehdr->e_phoff + ehdr->e_phnum * ehdr->e_phentsize) >= imgsz)
348 		return (-1);
349 
350 	phdrbase = (uint8_t *)img + ehdr->e_phoff;
351 
352 	for (i = 0; i < ehdr->e_phnum; i++) {
353 		phdr = (Elf64_Phdr *)(phdrbase + ehdr->e_phentsize * i);
354 
355 		if (phdr->p_type == PT_LOAD) {
356 			if (phdr->p_vaddr == phdr->p_paddr &&
357 			    phdr->p_vaddr == DBOOT_ENTRY_ADDRESS) {
358 				ASSERT(phdr->p_offset <= UINT32_MAX);
359 				*offp = (uint32_t)phdr->p_offset;
360 				return (0);
361 			}
362 		}
363 	}
364 
365 	return (-1);
366 }
367 
368 
369 /*
370  * Initialize text and data section information for 32-bit kernel.
371  * sectcntp - is both input/output parameter.
372  * On entry, *sectcntp contains maximum allowable number of sections;
373  * on return, it contains the actual number of sections filled.
374  */
375 static int
376 fastboot_elf32_find_loadables(void *img, off_t imgsz, fastboot_section_t *sectp,
377     int *sectcntp, uint32_t *offp)
378 {
379 	Elf32_Ehdr	*ehdr = (Elf32_Ehdr *)img;
380 	Elf32_Phdr	*phdr;
381 	uint8_t		*phdrbase;
382 	int		i;
383 	int		used_sections = 0;
384 	const int	max_sectcnt = *sectcntp;
385 
386 	if ((ehdr->e_phoff + ehdr->e_phnum * ehdr->e_phentsize) >= imgsz)
387 		return (-1);
388 
389 	phdrbase = (uint8_t *)img + ehdr->e_phoff;
390 
391 	for (i = 0; i < ehdr->e_phnum; i++) {
392 		phdr = (Elf32_Phdr *)(phdrbase + ehdr->e_phentsize * i);
393 
394 		if (phdr->p_type == PT_INTERP)
395 			return (-1);
396 
397 		if (phdr->p_type != PT_LOAD)
398 			continue;
399 
400 		if (phdr->p_vaddr == phdr->p_paddr &&
401 		    phdr->p_paddr == DBOOT_ENTRY_ADDRESS) {
402 			*offp = (uint32_t)phdr->p_offset;
403 		} else {
404 			if (max_sectcnt <= used_sections)
405 				return (-1);
406 
407 			sectp[used_sections].fb_sec_offset = phdr->p_offset;
408 			sectp[used_sections].fb_sec_paddr = phdr->p_paddr;
409 			sectp[used_sections].fb_sec_size = phdr->p_filesz;
410 			sectp[used_sections].fb_sec_bss_size =
411 			    (phdr->p_filesz < phdr->p_memsz) ?
412 			    (phdr->p_memsz - phdr->p_filesz) : 0;
413 
414 			/* Extra sanity check for the input object file */
415 			if (sectp[used_sections].fb_sec_paddr +
416 			    sectp[used_sections].fb_sec_size +
417 			    sectp[used_sections].fb_sec_bss_size >=
418 			    DBOOT_ENTRY_ADDRESS)
419 				return (-1);
420 
421 			used_sections++;
422 		}
423 	}
424 
425 	*sectcntp = used_sections;
426 	return (0);
427 }
428 
429 /*
430  * Create multiboot info structure
431  */
432 static int
433 fastboot_build_mbi(char *mdep, fastboot_info_t *nk)
434 {
435 	mb_module_t	*mbp;
436 	uintptr_t	next_addr;
437 	uintptr_t	new_mbi_pa;
438 	size_t		arglen;
439 	char		bootargs[OBP_MAXPATHLEN];
440 	size_t		size;
441 
442 	bzero(bootargs, OBP_MAXPATHLEN);
443 
444 	if (mdep != NULL && strlen(mdep) != 0) {
445 		arglen = strlen(mdep) + 1;
446 	} else {
447 		arglen = saved_cmdline_len;
448 	}
449 
450 	size = PAGESIZE + P2ROUNDUP(arglen, PAGESIZE);
451 	if (nk->fi_mbi_size && nk->fi_mbi_size < size) {
452 		contig_free((void *)nk->fi_new_mbi_va, nk->fi_mbi_size);
453 		nk->fi_mbi_size = 0;
454 	}
455 
456 	if (nk->fi_mbi_size == 0) {
457 		if ((nk->fi_new_mbi_va =
458 		    (uintptr_t)contig_alloc(size, &fastboot_below_1G_dma_attr,
459 		    PAGESIZE, 0)) == NULL) {
460 			cmn_err(CE_WARN, fastboot_enomem_msg,
461 			    (uint64_t)size, "1G");
462 			return (-1);
463 		}
464 		/*
465 		 * fi_mbi_size must be set after the allocation succeeds
466 		 * as it's used to determine how much memory to free.
467 		 */
468 		nk->fi_mbi_size = size;
469 	}
470 
471 	bzero((void *)nk->fi_new_mbi_va, nk->fi_mbi_size);
472 
473 	new_mbi_pa = mmu_ptob((uint64_t)hat_getpfnum(kas.a_hat,
474 	    (caddr_t)nk->fi_new_mbi_va));
475 
476 	/*
477 	 * Map the address into both the current proc's address
478 	 * space and the kernel's address space in case the panic
479 	 * is forced by kmdb.
480 	 */
481 	AS_LOCK_ENTER(&kas, &kas.a_lock, RW_WRITER);
482 	hat_devload(kas.a_hat, (caddr_t)new_mbi_pa, nk->fi_mbi_size,
483 	    mmu_btop(new_mbi_pa), PROT_READ | PROT_WRITE,
484 	    HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK);
485 	AS_LOCK_EXIT(&kas, &kas.a_lock);
486 
487 	if (&kas != curproc->p_as) {
488 		struct as *asp = curproc->p_as;
489 		AS_LOCK_ENTER(asp, &asp->a_lock, RW_WRITER);
490 		hat_devload(asp->a_hat, (caddr_t)new_mbi_pa, nk->fi_mbi_size,
491 		    mmu_btop(new_mbi_pa), PROT_READ | PROT_WRITE,
492 		    HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK);
493 		AS_LOCK_EXIT(asp, &asp->a_lock);
494 	}
495 
496 	nk->fi_new_mbi_pa = (paddr_t)new_mbi_pa;
497 
498 	bcopy(&saved_mbi, (void *)new_mbi_pa, sizeof (multiboot_info_t));
499 
500 	next_addr = new_mbi_pa + sizeof (multiboot_info_t);
501 	((multiboot_info_t *)new_mbi_pa)->mods_addr = next_addr;
502 	mbp = (mb_module_t *)(uintptr_t)next_addr;
503 	mbp->mod_start = nk->fi_files[FASTBOOT_BOOTARCHIVE].fb_dest_pa;
504 	mbp->mod_end = nk->fi_files[FASTBOOT_BOOTARCHIVE].fb_next_pa;
505 
506 	next_addr += sizeof (mb_module_t);
507 	bcopy(fastboot_filename[FASTBOOT_NAME_BOOTARCHIVE], (void *)next_addr,
508 	    strlen(fastboot_filename[FASTBOOT_NAME_BOOTARCHIVE]));
509 
510 	mbp->mod_name = next_addr;
511 	mbp->reserved = 0;
512 	next_addr += strlen(fastboot_filename[FASTBOOT_NAME_BOOTARCHIVE]);
513 	*(char *)next_addr = '\0';
514 	next_addr++;
515 	next_addr = P2ROUNDUP_TYPED(next_addr, 16, uintptr_t);
516 
517 	((multiboot_info_t *)new_mbi_pa)->mmap_addr = next_addr;
518 	bcopy((void *)(uintptr_t)saved_mmap, (void *)next_addr,
519 	    saved_mbi.mmap_length);
520 	next_addr += saved_mbi.mmap_length;
521 
522 	((multiboot_info_t *)new_mbi_pa)->drives_addr = next_addr;
523 	bcopy((void *)(uintptr_t)saved_drives, (void *)next_addr,
524 	    saved_mbi.drives_length);
525 	next_addr += saved_mbi.drives_length;
526 
527 	((multiboot_info_t *)new_mbi_pa)->cmdline = next_addr;
528 
529 	if (mdep != NULL && strlen(mdep) != 0) {
530 		bcopy(mdep, (void *)(uintptr_t)
531 		    (((multiboot_info_t *)new_mbi_pa)->cmdline), (arglen - 1));
532 	} else {
533 		bcopy((void *)saved_cmdline, (void *)next_addr, (arglen - 1));
534 	}
535 	/* Terminate the string */
536 	((char *)(intptr_t)next_addr)[arglen - 1] = '\0';
537 
538 	return (0);
539 }
540 
541 /*
542  * Initialize HAT related fields
543  */
544 static void
545 fastboot_init_fields(fastboot_info_t *nk)
546 {
547 	if (x86_feature & X86_PAE) {
548 		nk->fi_has_pae = 1;
549 		nk->fi_shift_amt = fastboot_shift_amt_pae;
550 		nk->fi_ptes_per_table = 512;
551 		nk->fi_lpagesize = (2 << 20);	/* 2M */
552 #ifdef	__amd64
553 		nk->fi_top_level = 3;
554 #else
555 		nk->fi_top_level = 2;
556 #endif	/* __amd64 */
557 	}
558 }
559 
560 /*
561  * Process boot argument
562  */
563 static void
564 fastboot_parse_mdep(char *mdep, char *kern_bootpath, int *bootpath_len,
565     char *bootargs)
566 {
567 	int	i;
568 
569 	/*
570 	 * If mdep is not NULL, it comes in the format of
571 	 *	mountpoint unix args
572 	 */
573 	if (mdep != NULL && strlen(mdep) != 0) {
574 		if (mdep[0] != '-') {
575 			/* First get the root argument */
576 			i = 0;
577 			while (mdep[i] != '\0' && mdep[i] != ' ') {
578 				i++;
579 			}
580 
581 			if (i < 4 || strncmp(&mdep[i-4], "unix", 4) != 0) {
582 				/* mount point */
583 				bcopy(mdep, kern_bootpath, i);
584 				kern_bootpath[i] = '\0';
585 				*bootpath_len = i;
586 
587 				/*
588 				 * Get the next argument. It should be unix as
589 				 * we have validated in in halt.c.
590 				 */
591 				if (strlen(mdep) > i) {
592 					mdep += (i + 1);
593 					i = 0;
594 					while (mdep[i] != '\0' &&
595 					    mdep[i] != ' ') {
596 						i++;
597 					}
598 				}
599 
600 			}
601 			bcopy(mdep, kern_bootfile, i);
602 			kern_bootfile[i] = '\0';
603 			bcopy(mdep, bootargs, strlen(mdep));
604 		} else {
605 			int off = strlen(kern_bootfile);
606 			bcopy(kern_bootfile, bootargs, off);
607 			bcopy(" ", &bootargs[off++], 1);
608 			bcopy(mdep, &bootargs[off], strlen(mdep));
609 			off += strlen(mdep);
610 			bootargs[off] = '\0';
611 		}
612 	}
613 }
614 
615 /*
616  * Reserve memory under PA 1G for mapping the new kernel and boot archive.
617  * This function is only called if fastreboot_onpanic is *not* set.
618  */
619 static void
620 fastboot_reserve_mem(fastboot_info_t *nk)
621 {
622 	int i;
623 
624 	/*
625 	 * A valid kernel is in place.  No need to reserve any memory.
626 	 */
627 	if (nk->fi_valid)
628 		return;
629 
630 	/*
631 	 * Reserve memory under PA 1G for PTE lists.
632 	 */
633 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
634 		fastboot_file_t *fb = &nk->fi_files[i];
635 		size_t fsize_roundup, size;
636 
637 		fsize_roundup = P2ROUNDUP_TYPED(saved_file_size[i],
638 		    PAGESIZE, size_t);
639 		size = FASTBOOT_PTE_LIST_SIZE(fsize_roundup);
640 		if ((fb->fb_pte_list_va = contig_alloc(size,
641 		    &fastboot_below_1G_dma_attr, PAGESIZE, 0)) == NULL) {
642 			return;
643 		}
644 		fb->fb_pte_list_size = size;
645 	}
646 
647 	/*
648 	 * Reserve memory under PA 1G for page tables.
649 	 */
650 	if ((nk->fi_pagetable_va =
651 	    (uintptr_t)contig_alloc(fastboot_pagetable_size,
652 	    &fastboot_below_1G_dma_attr, PAGESIZE, 0)) == NULL) {
653 		return;
654 	}
655 	nk->fi_pagetable_size = fastboot_pagetable_size;
656 
657 	/*
658 	 * Reserve memory under PA 1G for multiboot structure.
659 	 */
660 	if ((nk->fi_new_mbi_va = (uintptr_t)contig_alloc(fastboot_mbi_size,
661 	    &fastboot_below_1G_dma_attr, PAGESIZE, 0)) == NULL) {
662 		return;
663 	}
664 	nk->fi_mbi_size = fastboot_mbi_size;
665 }
666 
667 /*
668  * Calculate MD5 digest for the given fastboot_file.
669  * Assumes that the file is allready loaded properly.
670  */
671 static void
672 fastboot_cksum_file(fastboot_file_t *fb, uchar_t *md5_hash)
673 {
674 	MD5_CTX md5_ctx;
675 
676 	MD5Init(&md5_ctx);
677 	MD5Update(&md5_ctx, (void *)fb->fb_va, fb->fb_size);
678 	MD5Final(md5_hash, &md5_ctx);
679 }
680 
681 /*
682  * Free up the memory we have allocated for a file
683  */
684 static void
685 fastboot_free_file(fastboot_file_t *fb)
686 {
687 	size_t	fsize_roundup;
688 
689 	fsize_roundup = P2ROUNDUP_TYPED(fb->fb_size, PAGESIZE, size_t);
690 	if (fsize_roundup) {
691 		contig_free((void *)fb->fb_va, fsize_roundup);
692 		fb->fb_va = NULL;
693 		fb->fb_size = 0;
694 	}
695 }
696 
697 /*
698  * Free up memory used by the PTEs for a file.
699  */
700 static void
701 fastboot_free_file_pte(fastboot_file_t *fb, uint64_t endaddr)
702 {
703 	if (fb->fb_pte_list_size && fb->fb_pte_list_pa < endaddr) {
704 		contig_free((void *)fb->fb_pte_list_va, fb->fb_pte_list_size);
705 		fb->fb_pte_list_va = 0;
706 		fb->fb_pte_list_pa = 0;
707 		fb->fb_pte_list_size = 0;
708 	}
709 }
710 
711 /*
712  * Free up all the memory used for representing a kernel with
713  * fastboot_info_t.
714  */
715 static void
716 fastboot_free_mem(fastboot_info_t *nk, uint64_t endaddr)
717 {
718 	int i;
719 
720 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
721 		fastboot_free_file(nk->fi_files + i);
722 		fastboot_free_file_pte(nk->fi_files + i, endaddr);
723 	}
724 
725 	if (nk->fi_pagetable_size && nk->fi_pagetable_pa < endaddr) {
726 		contig_free((void *)nk->fi_pagetable_va, nk->fi_pagetable_size);
727 		nk->fi_pagetable_va = 0;
728 		nk->fi_pagetable_pa = 0;
729 		nk->fi_pagetable_size = 0;
730 	}
731 
732 	if (nk->fi_mbi_size && nk->fi_new_mbi_pa < endaddr) {
733 		contig_free((void *)nk->fi_new_mbi_va, nk->fi_mbi_size);
734 		nk->fi_new_mbi_va = 0;
735 		nk->fi_new_mbi_pa = 0;
736 		nk->fi_mbi_size = 0;
737 	}
738 }
739 
740 /*
741  * Only free up the memory allocated for the kernel and boot archive,
742  * but not for the page tables.
743  */
744 void
745 fastboot_free_newkernel(fastboot_info_t *nk)
746 {
747 	int i;
748 
749 	nk->fi_valid = 0;
750 	/*
751 	 * Free the memory we have allocated
752 	 */
753 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
754 		fastboot_free_file(&(nk->fi_files[i]));
755 	}
756 }
757 
758 static void
759 fastboot_cksum_cdata(fastboot_info_t *nk, uchar_t *md5_hash)
760 {
761 	int i;
762 	MD5_CTX md5_ctx;
763 
764 	MD5Init(&md5_ctx);
765 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
766 		MD5Update(&md5_ctx, nk->fi_files[i].fb_pte_list_va,
767 		    nk->fi_files[i].fb_pte_list_size);
768 	}
769 	MD5Update(&md5_ctx, (void *)nk->fi_pagetable_va, nk->fi_pagetable_size);
770 	MD5Update(&md5_ctx, (void *)nk->fi_new_mbi_va, nk->fi_mbi_size);
771 
772 	MD5Final(md5_hash, &md5_ctx);
773 }
774 
775 /*
776  * Generate MD5 checksum of the given kernel.
777  */
778 static void
779 fastboot_cksum_generate(fastboot_info_t *nk)
780 {
781 	int i;
782 
783 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
784 		fastboot_cksum_file(nk->fi_files + i, nk->fi_md5_hash[i]);
785 	}
786 	fastboot_cksum_cdata(nk, nk->fi_md5_hash[i]);
787 }
788 
789 /*
790  * Calculate MD5 checksum of the given kernel and verify that
791  * it matches with what was calculated before.
792  */
793 int
794 fastboot_cksum_verify(fastboot_info_t *nk)
795 {
796 	int i;
797 	uchar_t md5_hash[MD5_DIGEST_LENGTH];
798 
799 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
800 		fastboot_cksum_file(nk->fi_files + i, md5_hash);
801 		if (bcmp(nk->fi_md5_hash[i], md5_hash,
802 		    sizeof (nk->fi_md5_hash[i])) != 0)
803 			return (i + 1);
804 	}
805 
806 	fastboot_cksum_cdata(nk, md5_hash);
807 	if (bcmp(nk->fi_md5_hash[i], md5_hash,
808 	    sizeof (nk->fi_md5_hash[i])) != 0)
809 		return (i + 1);
810 
811 	return (0);
812 }
813 
814 /*
815  * This function performs the following tasks:
816  * - Read the sizes of the new kernel and boot archive.
817  * - Allocate memory for the new kernel and boot archive.
818  * - Allocate memory for page tables necessary for mapping the memory
819  *   allocated for the files.
820  * - Read the new kernel and boot archive into memory.
821  * - Map in the fast reboot switcher.
822  * - Load the fast reboot switcher to FASTBOOT_SWTCH_PA.
823  * - Build the new multiboot_info structure
824  * - Build page tables for the low 1G of physical memory.
825  * - Mark the data structure as valid if all steps have succeeded.
826  */
827 void
828 fastboot_load_kernel(char *mdep)
829 {
830 	void		*buf = NULL;
831 	int		i;
832 	fastboot_file_t	*fb;
833 	uint32_t	dboot_start_offset;
834 	char		kern_bootpath[OBP_MAXPATHLEN];
835 	extern uintptr_t postbootkernelbase;
836 	uintptr_t	saved_kernelbase;
837 	int		bootpath_len = 0;
838 	int		is_failsafe = 0;
839 	int		is_retry = 0;
840 	uint64_t	end_addr;
841 
842 	ASSERT(fastreboot_capable);
843 
844 	if (newkernel.fi_valid)
845 		fastboot_free_newkernel(&newkernel);
846 
847 	saved_kernelbase = postbootkernelbase;
848 
849 	postbootkernelbase = 0;
850 
851 	/*
852 	 * Initialize various HAT related fields in the data structure
853 	 */
854 	fastboot_init_fields(&newkernel);
855 
856 	bzero(kern_bootpath, OBP_MAXPATHLEN);
857 
858 	/*
859 	 * Process the boot argument
860 	 */
861 	bzero(fastboot_args, OBP_MAXPATHLEN);
862 	fastboot_parse_mdep(mdep, kern_bootpath, &bootpath_len, fastboot_args);
863 
864 	/*
865 	 * Make sure we get the null character
866 	 */
867 	bcopy(kern_bootpath, fastboot_filename[FASTBOOT_NAME_UNIX],
868 	    bootpath_len);
869 	bcopy(kern_bootfile,
870 	    &fastboot_filename[FASTBOOT_NAME_UNIX][bootpath_len],
871 	    strlen(kern_bootfile) + 1);
872 
873 	bcopy(kern_bootpath, fastboot_filename[FASTBOOT_NAME_BOOTARCHIVE],
874 	    bootpath_len);
875 
876 	if (bcmp(kern_bootfile, FAILSAFE_BOOTFILE32,
877 	    (sizeof (FAILSAFE_BOOTFILE32) - 1)) == 0 ||
878 	    bcmp(kern_bootfile, FAILSAFE_BOOTFILE64,
879 	    (sizeof (FAILSAFE_BOOTFILE64) - 1)) == 0) {
880 		is_failsafe = 1;
881 	}
882 
883 load_kernel_retry:
884 	/*
885 	 * Read in unix and boot_archive
886 	 */
887 	end_addr = DBOOT_ENTRY_ADDRESS;
888 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
889 		struct _buf	*file;
890 		uintptr_t	va;
891 		uint64_t	fsize;
892 		size_t		fsize_roundup, pt_size;
893 		int		page_index;
894 		uintptr_t	offset;
895 		ddi_dma_attr_t dma_attr = fastboot_dma_attr;
896 
897 
898 		dprintf("fastboot_filename[%d] = %s\n",
899 		    i, fastboot_filename[i]);
900 
901 		if ((file = kobj_open_file(fastboot_filename[i])) ==
902 		    (struct _buf *)-1) {
903 			cmn_err(CE_WARN, "Fastboot: Couldn't open %s",
904 			    fastboot_filename[i]);
905 			goto err_out;
906 		}
907 
908 		if (kobj_get_filesize(file, &fsize) != 0) {
909 			cmn_err(CE_WARN,
910 			    "Fastboot: Couldn't get filesize for %s",
911 			    fastboot_filename[i]);
912 			goto err_out;
913 		}
914 
915 		fsize_roundup = P2ROUNDUP_TYPED(fsize, PAGESIZE, size_t);
916 
917 		/*
918 		 * Where the files end in physical memory after being
919 		 * relocated by the fast boot switcher.
920 		 */
921 		end_addr += fsize_roundup;
922 		if (end_addr > fastboot_below_1G_dma_attr.dma_attr_addr_hi) {
923 			cmn_err(CE_WARN, "Fastboot: boot archive is too big");
924 			goto err_out;
925 		}
926 
927 		/*
928 		 * Adjust dma_attr_addr_lo so that the new kernel and boot
929 		 * archive will not be overridden during relocation.
930 		 */
931 		if (end_addr > fastboot_dma_attr.dma_attr_addr_lo ||
932 		    end_addr > fastboot_below_1G_dma_attr.dma_attr_addr_lo) {
933 
934 			if (is_retry) {
935 				/*
936 				 * If we have already tried and didn't succeed,
937 				 * just give up.
938 				 */
939 				cmn_err(CE_WARN,
940 				    "Fastboot: boot archive is too big");
941 				goto err_out;
942 			} else {
943 				/* Set the flag so we don't keep retrying */
944 				is_retry++;
945 
946 				/* Adjust dma_attr_addr_lo */
947 				fastboot_dma_attr.dma_attr_addr_lo = end_addr;
948 				fastboot_below_1G_dma_attr.dma_attr_addr_lo =
949 				    end_addr;
950 
951 				/*
952 				 * Free the memory we have already allocated
953 				 * whose physical addresses might not fit
954 				 * the new lo and hi constraints.
955 				 */
956 				fastboot_free_mem(&newkernel, end_addr);
957 				goto load_kernel_retry;
958 			}
959 		}
960 
961 
962 		if (!fastboot_contig)
963 			dma_attr.dma_attr_sgllen = (fsize / PAGESIZE) +
964 			    (((fsize % PAGESIZE) == 0) ? 0 : 1);
965 
966 		if ((buf = contig_alloc(fsize, &dma_attr, PAGESIZE, 0))
967 		    == NULL) {
968 			cmn_err(CE_WARN, fastboot_enomem_msg, fsize, "64G");
969 			goto err_out;
970 		}
971 
972 		va = P2ROUNDUP_TYPED((uintptr_t)buf, PAGESIZE, uintptr_t);
973 
974 		if (kobj_read_file(file, (char *)va, fsize, 0) < 0) {
975 			cmn_err(CE_WARN, "Fastboot: Couldn't read %s",
976 			    fastboot_filename[i]);
977 			goto err_out;
978 		}
979 
980 		fb = &newkernel.fi_files[i];
981 		fb->fb_va = va;
982 		fb->fb_size = fsize;
983 		fb->fb_sectcnt = 0;
984 
985 		pt_size = FASTBOOT_PTE_LIST_SIZE(fsize_roundup);
986 
987 		/*
988 		 * If we have reserved memory but it not enough, free it.
989 		 */
990 		if (fb->fb_pte_list_size && fb->fb_pte_list_size < pt_size) {
991 			contig_free((void *)fb->fb_pte_list_va,
992 			    fb->fb_pte_list_size);
993 			fb->fb_pte_list_size = 0;
994 		}
995 
996 		if (fb->fb_pte_list_size == 0) {
997 			if ((fb->fb_pte_list_va =
998 			    (x86pte_t *)contig_alloc(pt_size,
999 			    &fastboot_below_1G_dma_attr, PAGESIZE, 0))
1000 			    == NULL) {
1001 				cmn_err(CE_WARN, fastboot_enomem_msg,
1002 				    (uint64_t)pt_size, "1G");
1003 				goto err_out;
1004 			}
1005 			/*
1006 			 * fb_pte_list_size must be set after the allocation
1007 			 * succeeds as it's used to determine how much memory to
1008 			 * free.
1009 			 */
1010 			fb->fb_pte_list_size = pt_size;
1011 		}
1012 
1013 		bzero((void *)(fb->fb_pte_list_va), fb->fb_pte_list_size);
1014 
1015 		fb->fb_pte_list_pa = mmu_ptob((uint64_t)hat_getpfnum(kas.a_hat,
1016 		    (caddr_t)fb->fb_pte_list_va));
1017 
1018 		for (page_index = 0, offset = 0; offset < fb->fb_size;
1019 		    offset += PAGESIZE) {
1020 			uint64_t paddr;
1021 
1022 			paddr = mmu_ptob((uint64_t)hat_getpfnum(kas.a_hat,
1023 			    (caddr_t)fb->fb_va + offset));
1024 
1025 			ASSERT(paddr >= fastboot_dma_attr.dma_attr_addr_lo);
1026 
1027 			/*
1028 			 * Include the pte_bits so we don't have to make
1029 			 * it in assembly.
1030 			 */
1031 			fb->fb_pte_list_va[page_index++] = (x86pte_t)
1032 			    (paddr | pte_bits);
1033 		}
1034 
1035 		fb->fb_pte_list_va[page_index] = FASTBOOT_TERMINATE;
1036 
1037 		if (i == FASTBOOT_UNIX) {
1038 			Ehdr	*ehdr = (Ehdr *)va;
1039 			int	j;
1040 
1041 			/*
1042 			 * Sanity checks:
1043 			 */
1044 			for (j = 0; j < SELFMAG; j++) {
1045 				if (ehdr->e_ident[j] != ELFMAG[j]) {
1046 					cmn_err(CE_WARN, "Fastboot: Bad ELF "
1047 					    "signature");
1048 					goto err_out;
1049 				}
1050 			}
1051 
1052 			if (ehdr->e_ident[EI_CLASS] == ELFCLASS32 &&
1053 			    ehdr->e_ident[EI_DATA] == ELFDATA2LSB &&
1054 			    ehdr->e_machine == EM_386) {
1055 
1056 				fb->fb_sectcnt = sizeof (fb->fb_sections) /
1057 				    sizeof (fb->fb_sections[0]);
1058 
1059 				if (fastboot_elf32_find_loadables((void *)va,
1060 				    fsize, &fb->fb_sections[0],
1061 				    &fb->fb_sectcnt, &dboot_start_offset) < 0) {
1062 					cmn_err(CE_WARN, "Fastboot: ELF32 "
1063 					    "program section failure");
1064 					goto err_out;
1065 				}
1066 
1067 				if (fb->fb_sectcnt == 0) {
1068 					cmn_err(CE_WARN, "Fastboot: No ELF32 "
1069 					    "program sections found");
1070 					goto err_out;
1071 				}
1072 
1073 				if (is_failsafe) {
1074 					/* Failsafe boot_archive */
1075 					bcopy(BOOTARCHIVE32_FAILSAFE,
1076 					    &fastboot_filename
1077 					    [FASTBOOT_NAME_BOOTARCHIVE]
1078 					    [bootpath_len],
1079 					    sizeof (BOOTARCHIVE32_FAILSAFE));
1080 				} else {
1081 					bcopy(BOOTARCHIVE32,
1082 					    &fastboot_filename
1083 					    [FASTBOOT_NAME_BOOTARCHIVE]
1084 					    [bootpath_len],
1085 					    sizeof (BOOTARCHIVE32));
1086 				}
1087 
1088 			} else if (ehdr->e_ident[EI_CLASS] == ELFCLASS64 &&
1089 			    ehdr->e_ident[EI_DATA] == ELFDATA2LSB &&
1090 			    ehdr->e_machine == EM_AMD64) {
1091 
1092 				if (fastboot_elf64_find_dboot_load_offset(
1093 				    (void *)va, fsize, &dboot_start_offset)
1094 				    != 0) {
1095 					cmn_err(CE_WARN, "Fastboot: Couldn't "
1096 					    "find ELF64 dboot entry offset");
1097 					goto err_out;
1098 				}
1099 
1100 				if ((x86_feature & X86_64) == 0 ||
1101 				    (x86_feature & X86_PAE) == 0) {
1102 					cmn_err(CE_WARN, "Fastboot: Cannot "
1103 					    "reboot to %s: "
1104 					    "not a 64-bit capable system",
1105 					    kern_bootfile);
1106 					goto err_out;
1107 				}
1108 
1109 				if (is_failsafe) {
1110 					/* Failsafe boot_archive */
1111 					bcopy(BOOTARCHIVE64_FAILSAFE,
1112 					    &fastboot_filename
1113 					    [FASTBOOT_NAME_BOOTARCHIVE]
1114 					    [bootpath_len],
1115 					    sizeof (BOOTARCHIVE64_FAILSAFE));
1116 				} else {
1117 					bcopy(BOOTARCHIVE64,
1118 					    &fastboot_filename
1119 					    [FASTBOOT_NAME_BOOTARCHIVE]
1120 					    [bootpath_len],
1121 					    sizeof (BOOTARCHIVE64));
1122 				}
1123 			} else {
1124 				cmn_err(CE_WARN, "Fastboot: Unknown ELF type");
1125 				goto err_out;
1126 			}
1127 
1128 			fb->fb_dest_pa = DBOOT_ENTRY_ADDRESS -
1129 			    dboot_start_offset;
1130 
1131 			fb->fb_next_pa = DBOOT_ENTRY_ADDRESS + fsize_roundup;
1132 		} else {
1133 			fb->fb_dest_pa = newkernel.fi_files[i - 1].fb_next_pa;
1134 			fb->fb_next_pa = fb->fb_dest_pa + fsize_roundup;
1135 		}
1136 
1137 		kobj_close_file(file);
1138 
1139 	}
1140 
1141 	/*
1142 	 * Add the function that will switch us to 32-bit protected mode
1143 	 */
1144 	fb = &newkernel.fi_files[FASTBOOT_SWTCH];
1145 	fb->fb_va = fb->fb_dest_pa = FASTBOOT_SWTCH_PA;
1146 	fb->fb_size = MMU_PAGESIZE;
1147 
1148 	hat_devload(kas.a_hat, (caddr_t)fb->fb_va,
1149 	    MMU_PAGESIZE, mmu_btop(fb->fb_dest_pa),
1150 	    PROT_READ | PROT_WRITE | PROT_EXEC,
1151 	    HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK);
1152 
1153 	/*
1154 	 * Build the new multiboot_info structure
1155 	 */
1156 	if (fastboot_build_mbi(fastboot_args, &newkernel) != 0) {
1157 		goto err_out;
1158 	}
1159 
1160 	/*
1161 	 * Build page table for low 1G physical memory. Use big pages.
1162 	 * Allocate 4 (5 for amd64) pages for the page tables.
1163 	 *    1 page for PML4 (amd64)
1164 	 *    1 page for Page-Directory-Pointer Table
1165 	 *    2 pages for Page Directory
1166 	 *    1 page for Page Table.
1167 	 * The page table entry will be rewritten to map the physical
1168 	 * address as we do the copying.
1169 	 */
1170 	if (newkernel.fi_has_pae) {
1171 #ifdef	__amd64
1172 		size_t size = MMU_PAGESIZE * 5;
1173 #else
1174 		size_t size = MMU_PAGESIZE * 4;
1175 #endif	/* __amd64 */
1176 
1177 		if (newkernel.fi_pagetable_size && newkernel.fi_pagetable_size
1178 		    < size) {
1179 			contig_free((void *)newkernel.fi_pagetable_va,
1180 			    newkernel.fi_pagetable_size);
1181 			newkernel.fi_pagetable_size = 0;
1182 		}
1183 
1184 		if (newkernel.fi_pagetable_size == 0) {
1185 			if ((newkernel.fi_pagetable_va = (uintptr_t)
1186 			    contig_alloc(size, &fastboot_below_1G_dma_attr,
1187 			    MMU_PAGESIZE, 0)) == NULL) {
1188 				cmn_err(CE_WARN, fastboot_enomem_msg,
1189 				    (uint64_t)size, "1G");
1190 				goto err_out;
1191 			}
1192 			/*
1193 			 * fi_pagetable_size must be set after the allocation
1194 			 * succeeds as it's used to determine how much memory to
1195 			 * free.
1196 			 */
1197 			newkernel.fi_pagetable_size = size;
1198 		}
1199 
1200 		bzero((void *)(newkernel.fi_pagetable_va), size);
1201 
1202 		newkernel.fi_pagetable_pa =
1203 		    mmu_ptob((uint64_t)hat_getpfnum(kas.a_hat,
1204 		    (caddr_t)newkernel.fi_pagetable_va));
1205 
1206 		newkernel.fi_last_table_pa = newkernel.fi_pagetable_pa +
1207 		    size - MMU_PAGESIZE;
1208 
1209 		newkernel.fi_next_table_va = newkernel.fi_pagetable_va +
1210 		    MMU_PAGESIZE;
1211 		newkernel.fi_next_table_pa = newkernel.fi_pagetable_pa +
1212 		    MMU_PAGESIZE;
1213 
1214 		fastboot_build_pagetables(&newkernel);
1215 	}
1216 
1217 
1218 	/* Generate MD5 checksums */
1219 	fastboot_cksum_generate(&newkernel);
1220 
1221 	/* Mark it as valid */
1222 	newkernel.fi_valid = 1;
1223 	newkernel.fi_magic = FASTBOOT_MAGIC;
1224 
1225 	postbootkernelbase = saved_kernelbase;
1226 	return;
1227 
1228 err_out:
1229 	postbootkernelbase = saved_kernelbase;
1230 	newkernel.fi_valid = 0;
1231 	fastboot_free_newkernel(&newkernel);
1232 }
1233 
1234 
1235 /* ARGSUSED */
1236 static int
1237 fastboot_xc_func(fastboot_info_t *nk, xc_arg_t unused2, xc_arg_t unused3)
1238 {
1239 	void (*fastboot_func)(fastboot_info_t *);
1240 	fastboot_file_t	*fb = &nk->fi_files[FASTBOOT_SWTCH];
1241 	fastboot_func = (void (*)())(fb->fb_va);
1242 	kthread_t *t_intr = curthread->t_intr;
1243 
1244 	if (&kas != curproc->p_as) {
1245 		hat_devload(curproc->p_as->a_hat, (caddr_t)fb->fb_va,
1246 		    MMU_PAGESIZE, mmu_btop(fb->fb_dest_pa),
1247 		    PROT_READ | PROT_WRITE | PROT_EXEC,
1248 		    HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK);
1249 	}
1250 
1251 	/*
1252 	 * If we have pinned a thread, make sure the address is mapped
1253 	 * in the address space of the pinned thread.
1254 	 */
1255 	if (t_intr && t_intr->t_procp->p_as->a_hat != curproc->p_as->a_hat &&
1256 	    t_intr->t_procp->p_as != &kas)
1257 		hat_devload(t_intr->t_procp->p_as->a_hat, (caddr_t)fb->fb_va,
1258 		    MMU_PAGESIZE, mmu_btop(fb->fb_dest_pa),
1259 		    PROT_READ | PROT_WRITE | PROT_EXEC,
1260 		    HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK);
1261 
1262 	(*psm_shutdownf)(A_SHUTDOWN, AD_FASTREBOOT);
1263 	(*fastboot_func)(nk);
1264 
1265 	/*NOTREACHED*/
1266 	return (0);
1267 }
1268 
1269 /*
1270  * Jump to the fast reboot switcher.  This function never returns.
1271  */
1272 void
1273 fast_reboot()
1274 {
1275 	processorid_t bootcpuid = 0;
1276 	extern uintptr_t postbootkernelbase;
1277 	extern char	fb_swtch_image[];
1278 	fastboot_file_t	*fb;
1279 	int i;
1280 
1281 	postbootkernelbase = 0;
1282 
1283 	fb = &newkernel.fi_files[FASTBOOT_SWTCH];
1284 
1285 	/*
1286 	 * Map the address into both the current proc's address
1287 	 * space and the kernel's address space in case the panic
1288 	 * is forced by kmdb.
1289 	 */
1290 	if (&kas != curproc->p_as) {
1291 		hat_devload(curproc->p_as->a_hat, (caddr_t)fb->fb_va,
1292 		    MMU_PAGESIZE, mmu_btop(fb->fb_dest_pa),
1293 		    PROT_READ | PROT_WRITE | PROT_EXEC,
1294 		    HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK);
1295 	}
1296 
1297 	bcopy((void *)fb_swtch_image, (void *)fb->fb_va, fb->fb_size);
1298 
1299 
1300 	/*
1301 	 * Set fb_va to fake_va
1302 	 */
1303 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
1304 		newkernel.fi_files[i].fb_va = fake_va;
1305 
1306 	}
1307 
1308 	if (panicstr && CPU->cpu_id != bootcpuid &&
1309 	    CPU_ACTIVE(cpu_get(bootcpuid))) {
1310 		cpuset_t cpuset;
1311 
1312 		CPUSET_ZERO(cpuset);
1313 		CPUSET_ADD(cpuset, bootcpuid);
1314 		xc_trycall((xc_arg_t)&newkernel, 0, 0, cpuset,
1315 		    (xc_func_t)fastboot_xc_func);
1316 
1317 		/* Do what panic_idle() does */
1318 		splx(ipltospl(CLOCK_LEVEL));
1319 		(void) setjmp(&curthread->t_pcb);
1320 		for (;;)
1321 			;
1322 	} else
1323 		(void) fastboot_xc_func(&newkernel, 0, 0);
1324 }
1325 
1326 
1327 /*
1328  * Get boot property value for fastreboot_onpanic.
1329  *
1330  * NOTE: If fastreboot_onpanic is set to non-zero in /etc/system,
1331  * new setting passed in via "-B fastreboot_onpanic" is ignored.
1332  * This order of precedence is to enable developers debugging panics
1333  * that occur early in boot to utilize Fast Reboot on panic.
1334  */
1335 static void
1336 fastboot_get_bootprop(void)
1337 {
1338 	int		val = 0xaa, len, ret;
1339 	dev_info_t	*devi;
1340 	char		*propstr = NULL;
1341 
1342 	devi = ddi_root_node();
1343 
1344 	ret = ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
1345 	    FASTREBOOT_ONPANIC, &propstr);
1346 
1347 	if (ret == DDI_PROP_SUCCESS) {
1348 		if (FASTREBOOT_ONPANIC_NOTSET(propstr))
1349 			val = 0;
1350 		else if (FASTREBOOT_ONPANIC_ISSET(propstr))
1351 			val = UA_FASTREBOOT_ONPANIC;
1352 
1353 		/*
1354 		 * Only set fastreboot_onpanic to the value passed in
1355 		 * if it's not already set to non-zero, and the value
1356 		 * has indeed been passed in via command line.
1357 		 */
1358 		if (!fastreboot_onpanic && val != 0xaa)
1359 			fastreboot_onpanic = val;
1360 		ddi_prop_free(propstr);
1361 	} else if (ret != DDI_PROP_NOT_FOUND && ret != DDI_PROP_UNDEFINED) {
1362 		cmn_err(CE_WARN, "%s value is invalid, will be ignored",
1363 		    FASTREBOOT_ONPANIC);
1364 	}
1365 
1366 	len = sizeof (fastreboot_onpanic_cmdline);
1367 	ret = ddi_getlongprop_buf(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
1368 	    FASTREBOOT_ONPANIC_CMDLINE, fastreboot_onpanic_cmdline, &len);
1369 
1370 	if (ret == DDI_PROP_BUF_TOO_SMALL)
1371 		cmn_err(CE_WARN, "%s value is too long, will be ignored",
1372 		    FASTREBOOT_ONPANIC_CMDLINE);
1373 }
1374 
1375 /*
1376  * This function is called by main() to either load the backup kernel for panic
1377  * fast reboot, or to reserve low physical memory for fast reboot.
1378  */
1379 void
1380 fastboot_post_startup()
1381 {
1382 	if (!fastreboot_capable)
1383 		return;
1384 
1385 	fastboot_get_bootprop();
1386 
1387 	if (fastreboot_onpanic)
1388 		fastboot_load_kernel(fastreboot_onpanic_cmdline);
1389 	else if (reserve_mem_enabled)
1390 		fastboot_reserve_mem(&newkernel);
1391 }
1392 
1393 /*
1394  * Update boot configuration settings.
1395  * If the new fastreboot_onpanic setting is false, and a kernel has
1396  * been preloaded, free the memory;
1397  * if the new fastreboot_onpanic setting is true and newkernel is
1398  * not valid, load the new kernel.
1399  */
1400 void
1401 fastboot_update_config(const char *mdep)
1402 {
1403 	uint8_t boot_config = (uint8_t)*mdep;
1404 	int cur_fastreboot_onpanic = fastreboot_onpanic;
1405 
1406 	if (!fastreboot_capable)
1407 		return;
1408 
1409 	fastreboot_onpanic = boot_config & UA_FASTREBOOT_ONPANIC;
1410 	if (fastreboot_onpanic && (!cur_fastreboot_onpanic ||
1411 	    !newkernel.fi_valid))
1412 		fastboot_load_kernel(fastreboot_onpanic_cmdline);
1413 	if (cur_fastreboot_onpanic && !fastreboot_onpanic)
1414 		fastboot_free_newkernel(&newkernel);
1415 }
1416