xref: /titanic_41/usr/src/uts/i86pc/os/fastboot.c (revision c25309d42d46e04be84e0dbadaf3c9ab3369ad05)
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 (mbi) base on the saved mbi.
431  * Recalculate values of the pointer type fields in the data
432  * structure based on the new starting physical address of the
433  * data structure.
434  */
435 static int
436 fastboot_build_mbi(char *mdep, fastboot_info_t *nk)
437 {
438 	mb_module_t	*mbp;
439 	multiboot_info_t	*mbi;	/* pointer to multiboot structure */
440 	uintptr_t	start_addr_va;	/* starting VA of mbi */
441 	uintptr_t	start_addr_pa;	/* starting PA of mbi */
442 	size_t		offs = 0;	/* offset from the starting address */
443 	size_t		arglen;		/* length of the command line arg */
444 	size_t		size;	/* size of the memory reserved for mbi */
445 	size_t		mdnsz;	/* length of the boot archive name */
446 
447 	/*
448 	 * If mdep is not NULL or empty, use the length of mdep + 1
449 	 * (for NULL terminating) as the length of the new command
450 	 * line; else use the saved command line length as the
451 	 * length for the new command line.
452 	 */
453 	if (mdep != NULL && strlen(mdep) != 0) {
454 		arglen = strlen(mdep) + 1;
455 	} else {
456 		arglen = saved_cmdline_len;
457 	}
458 
459 	/*
460 	 * Allocate memory for the new multiboot info structure (mbi).
461 	 * If we have reserved memory for mbi but it's not enough,
462 	 * free it and reallocate.
463 	 */
464 	size = PAGESIZE + P2ROUNDUP(arglen, PAGESIZE);
465 	if (nk->fi_mbi_size && nk->fi_mbi_size < size) {
466 		contig_free((void *)nk->fi_new_mbi_va, nk->fi_mbi_size);
467 		nk->fi_mbi_size = 0;
468 	}
469 
470 	if (nk->fi_mbi_size == 0) {
471 		if ((nk->fi_new_mbi_va =
472 		    (uintptr_t)contig_alloc(size, &fastboot_below_1G_dma_attr,
473 		    PAGESIZE, 0)) == NULL) {
474 			cmn_err(CE_WARN, fastboot_enomem_msg,
475 			    (uint64_t)size, "1G");
476 			return (-1);
477 		}
478 		/*
479 		 * fi_mbi_size must be set after the allocation succeeds
480 		 * as it's used to determine how much memory to free.
481 		 */
482 		nk->fi_mbi_size = size;
483 	}
484 
485 	/*
486 	 * Initalize memory
487 	 */
488 	bzero((void *)nk->fi_new_mbi_va, nk->fi_mbi_size);
489 
490 	/*
491 	 * Get PA for the new mbi
492 	 */
493 	start_addr_va = nk->fi_new_mbi_va;
494 	start_addr_pa = mmu_ptob((uint64_t)hat_getpfnum(kas.a_hat,
495 	    (caddr_t)start_addr_va));
496 	nk->fi_new_mbi_pa = (paddr_t)start_addr_pa;
497 
498 	/*
499 	 * Populate the rest of the fields in the data structure
500 	 */
501 
502 	/*
503 	 * Copy from the saved mbi to preserve all non-pointer type fields.
504 	 */
505 	mbi = (multiboot_info_t *)start_addr_va;
506 	bcopy(&saved_mbi, mbi, sizeof (*mbi));
507 
508 	/*
509 	 * Recalculate mods_addr.  Set mod_start and mod_end based on
510 	 * the physical address of the new boot archive.  Set mod_name
511 	 * to the name of the new boto archive.
512 	 */
513 	offs += sizeof (multiboot_info_t);
514 	mbi->mods_addr = start_addr_pa + offs;
515 	mbp = (mb_module_t *)(start_addr_va + offs);
516 	mbp->mod_start = nk->fi_files[FASTBOOT_BOOTARCHIVE].fb_dest_pa;
517 	mbp->mod_end = nk->fi_files[FASTBOOT_BOOTARCHIVE].fb_next_pa;
518 
519 	offs += sizeof (mb_module_t);
520 	mdnsz = strlen(fastboot_filename[FASTBOOT_NAME_BOOTARCHIVE]) + 1;
521 	bcopy(fastboot_filename[FASTBOOT_NAME_BOOTARCHIVE],
522 	    (void *)(start_addr_va + offs), mdnsz);
523 	mbp->mod_name = start_addr_pa + offs;
524 	mbp->reserved = 0;
525 
526 	/*
527 	 * Make sure the offset is 16-byte aligned to avoid unaligned access.
528 	 */
529 	offs += mdnsz;
530 	offs = P2ROUNDUP_TYPED(offs, 16, size_t);
531 
532 	/*
533 	 * Recalculate mmap_addr
534 	 */
535 	mbi->mmap_addr = start_addr_pa + offs;
536 	bcopy((void *)(uintptr_t)saved_mmap, (void *)(start_addr_va + offs),
537 	    saved_mbi.mmap_length);
538 	offs += saved_mbi.mmap_length;
539 
540 	/*
541 	 * Recalculate drives_addr
542 	 */
543 	mbi->drives_addr = start_addr_pa + offs;
544 	bcopy((void *)(uintptr_t)saved_drives, (void *)(start_addr_va + offs),
545 	    saved_mbi.drives_length);
546 	offs += saved_mbi.drives_length;
547 
548 	/*
549 	 * Recalculate the address of cmdline.  Set cmdline to contain the
550 	 * new boot argument.
551 	 */
552 	mbi->cmdline = start_addr_pa + offs;
553 
554 	if (mdep != NULL && strlen(mdep) != 0) {
555 		bcopy(mdep, (void *)(start_addr_va + offs), arglen);
556 	} else {
557 		bcopy((void *)saved_cmdline, (void *)(start_addr_va + offs),
558 		    arglen);
559 	}
560 
561 	return (0);
562 }
563 
564 /*
565  * Initialize HAT related fields
566  */
567 static void
568 fastboot_init_fields(fastboot_info_t *nk)
569 {
570 	if (x86_feature & X86_PAE) {
571 		nk->fi_has_pae = 1;
572 		nk->fi_shift_amt = fastboot_shift_amt_pae;
573 		nk->fi_ptes_per_table = 512;
574 		nk->fi_lpagesize = (2 << 20);	/* 2M */
575 #ifdef	__amd64
576 		nk->fi_top_level = 3;
577 #else
578 		nk->fi_top_level = 2;
579 #endif	/* __amd64 */
580 	}
581 }
582 
583 /*
584  * Process boot argument
585  */
586 static void
587 fastboot_parse_mdep(char *mdep, char *kern_bootpath, int *bootpath_len,
588     char *bootargs)
589 {
590 	int	i;
591 
592 	/*
593 	 * If mdep is not NULL, it comes in the format of
594 	 *	mountpoint unix args
595 	 */
596 	if (mdep != NULL && strlen(mdep) != 0) {
597 		if (mdep[0] != '-') {
598 			/* First get the root argument */
599 			i = 0;
600 			while (mdep[i] != '\0' && mdep[i] != ' ') {
601 				i++;
602 			}
603 
604 			if (i < 4 || strncmp(&mdep[i-4], "unix", 4) != 0) {
605 				/* mount point */
606 				bcopy(mdep, kern_bootpath, i);
607 				kern_bootpath[i] = '\0';
608 				*bootpath_len = i;
609 
610 				/*
611 				 * Get the next argument. It should be unix as
612 				 * we have validated in in halt.c.
613 				 */
614 				if (strlen(mdep) > i) {
615 					mdep += (i + 1);
616 					i = 0;
617 					while (mdep[i] != '\0' &&
618 					    mdep[i] != ' ') {
619 						i++;
620 					}
621 				}
622 
623 			}
624 			bcopy(mdep, kern_bootfile, i);
625 			kern_bootfile[i] = '\0';
626 			bcopy(mdep, bootargs, strlen(mdep));
627 		} else {
628 			int off = strlen(kern_bootfile);
629 			bcopy(kern_bootfile, bootargs, off);
630 			bcopy(" ", &bootargs[off++], 1);
631 			bcopy(mdep, &bootargs[off], strlen(mdep));
632 			off += strlen(mdep);
633 			bootargs[off] = '\0';
634 		}
635 	}
636 }
637 
638 /*
639  * Reserve memory under PA 1G for mapping the new kernel and boot archive.
640  * This function is only called if fastreboot_onpanic is *not* set.
641  */
642 static void
643 fastboot_reserve_mem(fastboot_info_t *nk)
644 {
645 	int i;
646 
647 	/*
648 	 * A valid kernel is in place.  No need to reserve any memory.
649 	 */
650 	if (nk->fi_valid)
651 		return;
652 
653 	/*
654 	 * Reserve memory under PA 1G for PTE lists.
655 	 */
656 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
657 		fastboot_file_t *fb = &nk->fi_files[i];
658 		size_t fsize_roundup, size;
659 
660 		fsize_roundup = P2ROUNDUP_TYPED(saved_file_size[i],
661 		    PAGESIZE, size_t);
662 		size = FASTBOOT_PTE_LIST_SIZE(fsize_roundup);
663 		if ((fb->fb_pte_list_va = contig_alloc(size,
664 		    &fastboot_below_1G_dma_attr, PAGESIZE, 0)) == NULL) {
665 			return;
666 		}
667 		fb->fb_pte_list_size = size;
668 	}
669 
670 	/*
671 	 * Reserve memory under PA 1G for page tables.
672 	 */
673 	if ((nk->fi_pagetable_va =
674 	    (uintptr_t)contig_alloc(fastboot_pagetable_size,
675 	    &fastboot_below_1G_dma_attr, PAGESIZE, 0)) == NULL) {
676 		return;
677 	}
678 	nk->fi_pagetable_size = fastboot_pagetable_size;
679 
680 	/*
681 	 * Reserve memory under PA 1G for multiboot structure.
682 	 */
683 	if ((nk->fi_new_mbi_va = (uintptr_t)contig_alloc(fastboot_mbi_size,
684 	    &fastboot_below_1G_dma_attr, PAGESIZE, 0)) == NULL) {
685 		return;
686 	}
687 	nk->fi_mbi_size = fastboot_mbi_size;
688 }
689 
690 /*
691  * Calculate MD5 digest for the given fastboot_file.
692  * Assumes that the file is allready loaded properly.
693  */
694 static void
695 fastboot_cksum_file(fastboot_file_t *fb, uchar_t *md5_hash)
696 {
697 	MD5_CTX md5_ctx;
698 
699 	MD5Init(&md5_ctx);
700 	MD5Update(&md5_ctx, (void *)fb->fb_va, fb->fb_size);
701 	MD5Final(md5_hash, &md5_ctx);
702 }
703 
704 /*
705  * Free up the memory we have allocated for a file
706  */
707 static void
708 fastboot_free_file(fastboot_file_t *fb)
709 {
710 	size_t	fsize_roundup;
711 
712 	fsize_roundup = P2ROUNDUP_TYPED(fb->fb_size, PAGESIZE, size_t);
713 	if (fsize_roundup) {
714 		contig_free((void *)fb->fb_va, fsize_roundup);
715 		fb->fb_va = NULL;
716 		fb->fb_size = 0;
717 	}
718 }
719 
720 /*
721  * Free up memory used by the PTEs for a file.
722  */
723 static void
724 fastboot_free_file_pte(fastboot_file_t *fb, uint64_t endaddr)
725 {
726 	if (fb->fb_pte_list_size && fb->fb_pte_list_pa < endaddr) {
727 		contig_free((void *)fb->fb_pte_list_va, fb->fb_pte_list_size);
728 		fb->fb_pte_list_va = 0;
729 		fb->fb_pte_list_pa = 0;
730 		fb->fb_pte_list_size = 0;
731 	}
732 }
733 
734 /*
735  * Free up all the memory used for representing a kernel with
736  * fastboot_info_t.
737  */
738 static void
739 fastboot_free_mem(fastboot_info_t *nk, uint64_t endaddr)
740 {
741 	int i;
742 
743 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
744 		fastboot_free_file(nk->fi_files + i);
745 		fastboot_free_file_pte(nk->fi_files + i, endaddr);
746 	}
747 
748 	if (nk->fi_pagetable_size && nk->fi_pagetable_pa < endaddr) {
749 		contig_free((void *)nk->fi_pagetable_va, nk->fi_pagetable_size);
750 		nk->fi_pagetable_va = 0;
751 		nk->fi_pagetable_pa = 0;
752 		nk->fi_pagetable_size = 0;
753 	}
754 
755 	if (nk->fi_mbi_size && nk->fi_new_mbi_pa < endaddr) {
756 		contig_free((void *)nk->fi_new_mbi_va, nk->fi_mbi_size);
757 		nk->fi_new_mbi_va = 0;
758 		nk->fi_new_mbi_pa = 0;
759 		nk->fi_mbi_size = 0;
760 	}
761 }
762 
763 /*
764  * Only free up the memory allocated for the kernel and boot archive,
765  * but not for the page tables.
766  */
767 void
768 fastboot_free_newkernel(fastboot_info_t *nk)
769 {
770 	int i;
771 
772 	nk->fi_valid = 0;
773 	/*
774 	 * Free the memory we have allocated
775 	 */
776 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
777 		fastboot_free_file(&(nk->fi_files[i]));
778 	}
779 }
780 
781 static void
782 fastboot_cksum_cdata(fastboot_info_t *nk, uchar_t *md5_hash)
783 {
784 	int i;
785 	MD5_CTX md5_ctx;
786 
787 	MD5Init(&md5_ctx);
788 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
789 		MD5Update(&md5_ctx, nk->fi_files[i].fb_pte_list_va,
790 		    nk->fi_files[i].fb_pte_list_size);
791 	}
792 	MD5Update(&md5_ctx, (void *)nk->fi_pagetable_va, nk->fi_pagetable_size);
793 	MD5Update(&md5_ctx, (void *)nk->fi_new_mbi_va, nk->fi_mbi_size);
794 
795 	MD5Final(md5_hash, &md5_ctx);
796 }
797 
798 /*
799  * Generate MD5 checksum of the given kernel.
800  */
801 static void
802 fastboot_cksum_generate(fastboot_info_t *nk)
803 {
804 	int i;
805 
806 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
807 		fastboot_cksum_file(nk->fi_files + i, nk->fi_md5_hash[i]);
808 	}
809 	fastboot_cksum_cdata(nk, nk->fi_md5_hash[i]);
810 }
811 
812 /*
813  * Calculate MD5 checksum of the given kernel and verify that
814  * it matches with what was calculated before.
815  */
816 int
817 fastboot_cksum_verify(fastboot_info_t *nk)
818 {
819 	int i;
820 	uchar_t md5_hash[MD5_DIGEST_LENGTH];
821 
822 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
823 		fastboot_cksum_file(nk->fi_files + i, md5_hash);
824 		if (bcmp(nk->fi_md5_hash[i], md5_hash,
825 		    sizeof (nk->fi_md5_hash[i])) != 0)
826 			return (i + 1);
827 	}
828 
829 	fastboot_cksum_cdata(nk, md5_hash);
830 	if (bcmp(nk->fi_md5_hash[i], md5_hash,
831 	    sizeof (nk->fi_md5_hash[i])) != 0)
832 		return (i + 1);
833 
834 	return (0);
835 }
836 
837 /*
838  * This function performs the following tasks:
839  * - Read the sizes of the new kernel and boot archive.
840  * - Allocate memory for the new kernel and boot archive.
841  * - Allocate memory for page tables necessary for mapping the memory
842  *   allocated for the files.
843  * - Read the new kernel and boot archive into memory.
844  * - Map in the fast reboot switcher.
845  * - Load the fast reboot switcher to FASTBOOT_SWTCH_PA.
846  * - Build the new multiboot_info structure
847  * - Build page tables for the low 1G of physical memory.
848  * - Mark the data structure as valid if all steps have succeeded.
849  */
850 void
851 fastboot_load_kernel(char *mdep)
852 {
853 	void		*buf = NULL;
854 	int		i;
855 	fastboot_file_t	*fb;
856 	uint32_t	dboot_start_offset;
857 	char		kern_bootpath[OBP_MAXPATHLEN];
858 	extern uintptr_t postbootkernelbase;
859 	uintptr_t	saved_kernelbase;
860 	int		bootpath_len = 0;
861 	int		is_failsafe = 0;
862 	int		is_retry = 0;
863 	uint64_t	end_addr;
864 
865 	ASSERT(fastreboot_capable);
866 
867 	if (newkernel.fi_valid)
868 		fastboot_free_newkernel(&newkernel);
869 
870 	saved_kernelbase = postbootkernelbase;
871 
872 	postbootkernelbase = 0;
873 
874 	/*
875 	 * Initialize various HAT related fields in the data structure
876 	 */
877 	fastboot_init_fields(&newkernel);
878 
879 	bzero(kern_bootpath, OBP_MAXPATHLEN);
880 
881 	/*
882 	 * Process the boot argument
883 	 */
884 	bzero(fastboot_args, OBP_MAXPATHLEN);
885 	fastboot_parse_mdep(mdep, kern_bootpath, &bootpath_len, fastboot_args);
886 
887 	/*
888 	 * Make sure we get the null character
889 	 */
890 	bcopy(kern_bootpath, fastboot_filename[FASTBOOT_NAME_UNIX],
891 	    bootpath_len);
892 	bcopy(kern_bootfile,
893 	    &fastboot_filename[FASTBOOT_NAME_UNIX][bootpath_len],
894 	    strlen(kern_bootfile) + 1);
895 
896 	bcopy(kern_bootpath, fastboot_filename[FASTBOOT_NAME_BOOTARCHIVE],
897 	    bootpath_len);
898 
899 	if (bcmp(kern_bootfile, FAILSAFE_BOOTFILE32,
900 	    (sizeof (FAILSAFE_BOOTFILE32) - 1)) == 0 ||
901 	    bcmp(kern_bootfile, FAILSAFE_BOOTFILE64,
902 	    (sizeof (FAILSAFE_BOOTFILE64) - 1)) == 0) {
903 		is_failsafe = 1;
904 	}
905 
906 load_kernel_retry:
907 	/*
908 	 * Read in unix and boot_archive
909 	 */
910 	end_addr = DBOOT_ENTRY_ADDRESS;
911 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
912 		struct _buf	*file;
913 		uintptr_t	va;
914 		uint64_t	fsize;
915 		size_t		fsize_roundup, pt_size;
916 		int		page_index;
917 		uintptr_t	offset;
918 		ddi_dma_attr_t dma_attr = fastboot_dma_attr;
919 
920 
921 		dprintf("fastboot_filename[%d] = %s\n",
922 		    i, fastboot_filename[i]);
923 
924 		if ((file = kobj_open_file(fastboot_filename[i])) ==
925 		    (struct _buf *)-1) {
926 			cmn_err(CE_WARN, "Fastboot: Couldn't open %s",
927 			    fastboot_filename[i]);
928 			goto err_out;
929 		}
930 
931 		if (kobj_get_filesize(file, &fsize) != 0) {
932 			cmn_err(CE_WARN,
933 			    "Fastboot: Couldn't get filesize for %s",
934 			    fastboot_filename[i]);
935 			goto err_out;
936 		}
937 
938 		fsize_roundup = P2ROUNDUP_TYPED(fsize, PAGESIZE, size_t);
939 
940 		/*
941 		 * Where the files end in physical memory after being
942 		 * relocated by the fast boot switcher.
943 		 */
944 		end_addr += fsize_roundup;
945 		if (end_addr > fastboot_below_1G_dma_attr.dma_attr_addr_hi) {
946 			cmn_err(CE_WARN, "Fastboot: boot archive is too big");
947 			goto err_out;
948 		}
949 
950 		/*
951 		 * Adjust dma_attr_addr_lo so that the new kernel and boot
952 		 * archive will not be overridden during relocation.
953 		 */
954 		if (end_addr > fastboot_dma_attr.dma_attr_addr_lo ||
955 		    end_addr > fastboot_below_1G_dma_attr.dma_attr_addr_lo) {
956 
957 			if (is_retry) {
958 				/*
959 				 * If we have already tried and didn't succeed,
960 				 * just give up.
961 				 */
962 				cmn_err(CE_WARN,
963 				    "Fastboot: boot archive is too big");
964 				goto err_out;
965 			} else {
966 				/* Set the flag so we don't keep retrying */
967 				is_retry++;
968 
969 				/* Adjust dma_attr_addr_lo */
970 				fastboot_dma_attr.dma_attr_addr_lo = end_addr;
971 				fastboot_below_1G_dma_attr.dma_attr_addr_lo =
972 				    end_addr;
973 
974 				/*
975 				 * Free the memory we have already allocated
976 				 * whose physical addresses might not fit
977 				 * the new lo and hi constraints.
978 				 */
979 				fastboot_free_mem(&newkernel, end_addr);
980 				goto load_kernel_retry;
981 			}
982 		}
983 
984 
985 		if (!fastboot_contig)
986 			dma_attr.dma_attr_sgllen = (fsize / PAGESIZE) +
987 			    (((fsize % PAGESIZE) == 0) ? 0 : 1);
988 
989 		if ((buf = contig_alloc(fsize, &dma_attr, PAGESIZE, 0))
990 		    == NULL) {
991 			cmn_err(CE_WARN, fastboot_enomem_msg, fsize, "64G");
992 			goto err_out;
993 		}
994 
995 		va = P2ROUNDUP_TYPED((uintptr_t)buf, PAGESIZE, uintptr_t);
996 
997 		if (kobj_read_file(file, (char *)va, fsize, 0) < 0) {
998 			cmn_err(CE_WARN, "Fastboot: Couldn't read %s",
999 			    fastboot_filename[i]);
1000 			goto err_out;
1001 		}
1002 
1003 		fb = &newkernel.fi_files[i];
1004 		fb->fb_va = va;
1005 		fb->fb_size = fsize;
1006 		fb->fb_sectcnt = 0;
1007 
1008 		pt_size = FASTBOOT_PTE_LIST_SIZE(fsize_roundup);
1009 
1010 		/*
1011 		 * If we have reserved memory but it not enough, free it.
1012 		 */
1013 		if (fb->fb_pte_list_size && fb->fb_pte_list_size < pt_size) {
1014 			contig_free((void *)fb->fb_pte_list_va,
1015 			    fb->fb_pte_list_size);
1016 			fb->fb_pte_list_size = 0;
1017 		}
1018 
1019 		if (fb->fb_pte_list_size == 0) {
1020 			if ((fb->fb_pte_list_va =
1021 			    (x86pte_t *)contig_alloc(pt_size,
1022 			    &fastboot_below_1G_dma_attr, PAGESIZE, 0))
1023 			    == NULL) {
1024 				cmn_err(CE_WARN, fastboot_enomem_msg,
1025 				    (uint64_t)pt_size, "1G");
1026 				goto err_out;
1027 			}
1028 			/*
1029 			 * fb_pte_list_size must be set after the allocation
1030 			 * succeeds as it's used to determine how much memory to
1031 			 * free.
1032 			 */
1033 			fb->fb_pte_list_size = pt_size;
1034 		}
1035 
1036 		bzero((void *)(fb->fb_pte_list_va), fb->fb_pte_list_size);
1037 
1038 		fb->fb_pte_list_pa = mmu_ptob((uint64_t)hat_getpfnum(kas.a_hat,
1039 		    (caddr_t)fb->fb_pte_list_va));
1040 
1041 		for (page_index = 0, offset = 0; offset < fb->fb_size;
1042 		    offset += PAGESIZE) {
1043 			uint64_t paddr;
1044 
1045 			paddr = mmu_ptob((uint64_t)hat_getpfnum(kas.a_hat,
1046 			    (caddr_t)fb->fb_va + offset));
1047 
1048 			ASSERT(paddr >= fastboot_dma_attr.dma_attr_addr_lo);
1049 
1050 			/*
1051 			 * Include the pte_bits so we don't have to make
1052 			 * it in assembly.
1053 			 */
1054 			fb->fb_pte_list_va[page_index++] = (x86pte_t)
1055 			    (paddr | pte_bits);
1056 		}
1057 
1058 		fb->fb_pte_list_va[page_index] = FASTBOOT_TERMINATE;
1059 
1060 		if (i == FASTBOOT_UNIX) {
1061 			Ehdr	*ehdr = (Ehdr *)va;
1062 			int	j;
1063 
1064 			/*
1065 			 * Sanity checks:
1066 			 */
1067 			for (j = 0; j < SELFMAG; j++) {
1068 				if (ehdr->e_ident[j] != ELFMAG[j]) {
1069 					cmn_err(CE_WARN, "Fastboot: Bad ELF "
1070 					    "signature");
1071 					goto err_out;
1072 				}
1073 			}
1074 
1075 			if (ehdr->e_ident[EI_CLASS] == ELFCLASS32 &&
1076 			    ehdr->e_ident[EI_DATA] == ELFDATA2LSB &&
1077 			    ehdr->e_machine == EM_386) {
1078 
1079 				fb->fb_sectcnt = sizeof (fb->fb_sections) /
1080 				    sizeof (fb->fb_sections[0]);
1081 
1082 				if (fastboot_elf32_find_loadables((void *)va,
1083 				    fsize, &fb->fb_sections[0],
1084 				    &fb->fb_sectcnt, &dboot_start_offset) < 0) {
1085 					cmn_err(CE_WARN, "Fastboot: ELF32 "
1086 					    "program section failure");
1087 					goto err_out;
1088 				}
1089 
1090 				if (fb->fb_sectcnt == 0) {
1091 					cmn_err(CE_WARN, "Fastboot: No ELF32 "
1092 					    "program sections found");
1093 					goto err_out;
1094 				}
1095 
1096 				if (is_failsafe) {
1097 					/* Failsafe boot_archive */
1098 					bcopy(BOOTARCHIVE32_FAILSAFE,
1099 					    &fastboot_filename
1100 					    [FASTBOOT_NAME_BOOTARCHIVE]
1101 					    [bootpath_len],
1102 					    sizeof (BOOTARCHIVE32_FAILSAFE));
1103 				} else {
1104 					bcopy(BOOTARCHIVE32,
1105 					    &fastboot_filename
1106 					    [FASTBOOT_NAME_BOOTARCHIVE]
1107 					    [bootpath_len],
1108 					    sizeof (BOOTARCHIVE32));
1109 				}
1110 
1111 			} else if (ehdr->e_ident[EI_CLASS] == ELFCLASS64 &&
1112 			    ehdr->e_ident[EI_DATA] == ELFDATA2LSB &&
1113 			    ehdr->e_machine == EM_AMD64) {
1114 
1115 				if (fastboot_elf64_find_dboot_load_offset(
1116 				    (void *)va, fsize, &dboot_start_offset)
1117 				    != 0) {
1118 					cmn_err(CE_WARN, "Fastboot: Couldn't "
1119 					    "find ELF64 dboot entry offset");
1120 					goto err_out;
1121 				}
1122 
1123 				if ((x86_feature & X86_64) == 0 ||
1124 				    (x86_feature & X86_PAE) == 0) {
1125 					cmn_err(CE_WARN, "Fastboot: Cannot "
1126 					    "reboot to %s: "
1127 					    "not a 64-bit capable system",
1128 					    kern_bootfile);
1129 					goto err_out;
1130 				}
1131 
1132 				if (is_failsafe) {
1133 					/* Failsafe boot_archive */
1134 					bcopy(BOOTARCHIVE64_FAILSAFE,
1135 					    &fastboot_filename
1136 					    [FASTBOOT_NAME_BOOTARCHIVE]
1137 					    [bootpath_len],
1138 					    sizeof (BOOTARCHIVE64_FAILSAFE));
1139 				} else {
1140 					bcopy(BOOTARCHIVE64,
1141 					    &fastboot_filename
1142 					    [FASTBOOT_NAME_BOOTARCHIVE]
1143 					    [bootpath_len],
1144 					    sizeof (BOOTARCHIVE64));
1145 				}
1146 			} else {
1147 				cmn_err(CE_WARN, "Fastboot: Unknown ELF type");
1148 				goto err_out;
1149 			}
1150 
1151 			fb->fb_dest_pa = DBOOT_ENTRY_ADDRESS -
1152 			    dboot_start_offset;
1153 
1154 			fb->fb_next_pa = DBOOT_ENTRY_ADDRESS + fsize_roundup;
1155 		} else {
1156 			fb->fb_dest_pa = newkernel.fi_files[i - 1].fb_next_pa;
1157 			fb->fb_next_pa = fb->fb_dest_pa + fsize_roundup;
1158 		}
1159 
1160 		kobj_close_file(file);
1161 
1162 	}
1163 
1164 	/*
1165 	 * Add the function that will switch us to 32-bit protected mode
1166 	 */
1167 	fb = &newkernel.fi_files[FASTBOOT_SWTCH];
1168 	fb->fb_va = fb->fb_dest_pa = FASTBOOT_SWTCH_PA;
1169 	fb->fb_size = MMU_PAGESIZE;
1170 
1171 	hat_devload(kas.a_hat, (caddr_t)fb->fb_va,
1172 	    MMU_PAGESIZE, mmu_btop(fb->fb_dest_pa),
1173 	    PROT_READ | PROT_WRITE | PROT_EXEC,
1174 	    HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK);
1175 
1176 	/*
1177 	 * Build the new multiboot_info structure
1178 	 */
1179 	if (fastboot_build_mbi(fastboot_args, &newkernel) != 0) {
1180 		goto err_out;
1181 	}
1182 
1183 	/*
1184 	 * Build page table for low 1G physical memory. Use big pages.
1185 	 * Allocate 4 (5 for amd64) pages for the page tables.
1186 	 *    1 page for PML4 (amd64)
1187 	 *    1 page for Page-Directory-Pointer Table
1188 	 *    2 pages for Page Directory
1189 	 *    1 page for Page Table.
1190 	 * The page table entry will be rewritten to map the physical
1191 	 * address as we do the copying.
1192 	 */
1193 	if (newkernel.fi_has_pae) {
1194 #ifdef	__amd64
1195 		size_t size = MMU_PAGESIZE * 5;
1196 #else
1197 		size_t size = MMU_PAGESIZE * 4;
1198 #endif	/* __amd64 */
1199 
1200 		if (newkernel.fi_pagetable_size && newkernel.fi_pagetable_size
1201 		    < size) {
1202 			contig_free((void *)newkernel.fi_pagetable_va,
1203 			    newkernel.fi_pagetable_size);
1204 			newkernel.fi_pagetable_size = 0;
1205 		}
1206 
1207 		if (newkernel.fi_pagetable_size == 0) {
1208 			if ((newkernel.fi_pagetable_va = (uintptr_t)
1209 			    contig_alloc(size, &fastboot_below_1G_dma_attr,
1210 			    MMU_PAGESIZE, 0)) == NULL) {
1211 				cmn_err(CE_WARN, fastboot_enomem_msg,
1212 				    (uint64_t)size, "1G");
1213 				goto err_out;
1214 			}
1215 			/*
1216 			 * fi_pagetable_size must be set after the allocation
1217 			 * succeeds as it's used to determine how much memory to
1218 			 * free.
1219 			 */
1220 			newkernel.fi_pagetable_size = size;
1221 		}
1222 
1223 		bzero((void *)(newkernel.fi_pagetable_va), size);
1224 
1225 		newkernel.fi_pagetable_pa =
1226 		    mmu_ptob((uint64_t)hat_getpfnum(kas.a_hat,
1227 		    (caddr_t)newkernel.fi_pagetable_va));
1228 
1229 		newkernel.fi_last_table_pa = newkernel.fi_pagetable_pa +
1230 		    size - MMU_PAGESIZE;
1231 
1232 		newkernel.fi_next_table_va = newkernel.fi_pagetable_va +
1233 		    MMU_PAGESIZE;
1234 		newkernel.fi_next_table_pa = newkernel.fi_pagetable_pa +
1235 		    MMU_PAGESIZE;
1236 
1237 		fastboot_build_pagetables(&newkernel);
1238 	}
1239 
1240 
1241 	/* Generate MD5 checksums */
1242 	fastboot_cksum_generate(&newkernel);
1243 
1244 	/* Mark it as valid */
1245 	newkernel.fi_valid = 1;
1246 	newkernel.fi_magic = FASTBOOT_MAGIC;
1247 
1248 	postbootkernelbase = saved_kernelbase;
1249 	return;
1250 
1251 err_out:
1252 	postbootkernelbase = saved_kernelbase;
1253 	newkernel.fi_valid = 0;
1254 	fastboot_free_newkernel(&newkernel);
1255 }
1256 
1257 
1258 /* ARGSUSED */
1259 static int
1260 fastboot_xc_func(fastboot_info_t *nk, xc_arg_t unused2, xc_arg_t unused3)
1261 {
1262 	void (*fastboot_func)(fastboot_info_t *);
1263 	fastboot_file_t	*fb = &nk->fi_files[FASTBOOT_SWTCH];
1264 	fastboot_func = (void (*)())(fb->fb_va);
1265 	kthread_t *t_intr = curthread->t_intr;
1266 
1267 	if (&kas != curproc->p_as) {
1268 		hat_devload(curproc->p_as->a_hat, (caddr_t)fb->fb_va,
1269 		    MMU_PAGESIZE, mmu_btop(fb->fb_dest_pa),
1270 		    PROT_READ | PROT_WRITE | PROT_EXEC,
1271 		    HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK);
1272 	}
1273 
1274 	/*
1275 	 * If we have pinned a thread, make sure the address is mapped
1276 	 * in the address space of the pinned thread.
1277 	 */
1278 	if (t_intr && t_intr->t_procp->p_as->a_hat != curproc->p_as->a_hat &&
1279 	    t_intr->t_procp->p_as != &kas)
1280 		hat_devload(t_intr->t_procp->p_as->a_hat, (caddr_t)fb->fb_va,
1281 		    MMU_PAGESIZE, mmu_btop(fb->fb_dest_pa),
1282 		    PROT_READ | PROT_WRITE | PROT_EXEC,
1283 		    HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK);
1284 
1285 	(*psm_shutdownf)(A_SHUTDOWN, AD_FASTREBOOT);
1286 	(*fastboot_func)(nk);
1287 
1288 	/*NOTREACHED*/
1289 	return (0);
1290 }
1291 
1292 /*
1293  * Jump to the fast reboot switcher.  This function never returns.
1294  */
1295 void
1296 fast_reboot()
1297 {
1298 	processorid_t bootcpuid = 0;
1299 	extern uintptr_t postbootkernelbase;
1300 	extern char	fb_swtch_image[];
1301 	fastboot_file_t	*fb;
1302 	int i;
1303 
1304 	postbootkernelbase = 0;
1305 
1306 	fb = &newkernel.fi_files[FASTBOOT_SWTCH];
1307 
1308 	/*
1309 	 * Map the address into both the current proc's address
1310 	 * space and the kernel's address space in case the panic
1311 	 * is forced by kmdb.
1312 	 */
1313 	if (&kas != curproc->p_as) {
1314 		hat_devload(curproc->p_as->a_hat, (caddr_t)fb->fb_va,
1315 		    MMU_PAGESIZE, mmu_btop(fb->fb_dest_pa),
1316 		    PROT_READ | PROT_WRITE | PROT_EXEC,
1317 		    HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK);
1318 	}
1319 
1320 	bcopy((void *)fb_swtch_image, (void *)fb->fb_va, fb->fb_size);
1321 
1322 
1323 	/*
1324 	 * Set fb_va to fake_va
1325 	 */
1326 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
1327 		newkernel.fi_files[i].fb_va = fake_va;
1328 
1329 	}
1330 
1331 	if (panicstr && CPU->cpu_id != bootcpuid &&
1332 	    CPU_ACTIVE(cpu_get(bootcpuid))) {
1333 		extern void panic_idle(void);
1334 		cpuset_t cpuset;
1335 
1336 		CPUSET_ZERO(cpuset);
1337 		CPUSET_ADD(cpuset, bootcpuid);
1338 		xc_priority((xc_arg_t)&newkernel, 0, 0, CPUSET2BV(cpuset),
1339 		    (xc_func_t)fastboot_xc_func);
1340 
1341 		panic_idle();
1342 	} else
1343 		(void) fastboot_xc_func(&newkernel, 0, 0);
1344 }
1345 
1346 
1347 /*
1348  * Get boot property value for fastreboot_onpanic.
1349  *
1350  * NOTE: If fastreboot_onpanic is set to non-zero in /etc/system,
1351  * new setting passed in via "-B fastreboot_onpanic" is ignored.
1352  * This order of precedence is to enable developers debugging panics
1353  * that occur early in boot to utilize Fast Reboot on panic.
1354  */
1355 static void
1356 fastboot_get_bootprop(void)
1357 {
1358 	int		val = 0xaa, len, ret;
1359 	dev_info_t	*devi;
1360 	char		*propstr = NULL;
1361 
1362 	devi = ddi_root_node();
1363 
1364 	ret = ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
1365 	    FASTREBOOT_ONPANIC, &propstr);
1366 
1367 	if (ret == DDI_PROP_SUCCESS) {
1368 		if (FASTREBOOT_ONPANIC_NOTSET(propstr))
1369 			val = 0;
1370 		else if (FASTREBOOT_ONPANIC_ISSET(propstr))
1371 			val = UA_FASTREBOOT_ONPANIC;
1372 
1373 		/*
1374 		 * Only set fastreboot_onpanic to the value passed in
1375 		 * if it's not already set to non-zero, and the value
1376 		 * has indeed been passed in via command line.
1377 		 */
1378 		if (!fastreboot_onpanic && val != 0xaa)
1379 			fastreboot_onpanic = val;
1380 		ddi_prop_free(propstr);
1381 	} else if (ret != DDI_PROP_NOT_FOUND && ret != DDI_PROP_UNDEFINED) {
1382 		cmn_err(CE_WARN, "%s value is invalid, will be ignored",
1383 		    FASTREBOOT_ONPANIC);
1384 	}
1385 
1386 	len = sizeof (fastreboot_onpanic_cmdline);
1387 	ret = ddi_getlongprop_buf(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
1388 	    FASTREBOOT_ONPANIC_CMDLINE, fastreboot_onpanic_cmdline, &len);
1389 
1390 	if (ret == DDI_PROP_BUF_TOO_SMALL)
1391 		cmn_err(CE_WARN, "%s value is too long, will be ignored",
1392 		    FASTREBOOT_ONPANIC_CMDLINE);
1393 }
1394 
1395 /*
1396  * This function is called by main() to either load the backup kernel for panic
1397  * fast reboot, or to reserve low physical memory for fast reboot.
1398  */
1399 void
1400 fastboot_post_startup()
1401 {
1402 	if (!fastreboot_capable)
1403 		return;
1404 
1405 	fastboot_get_bootprop();
1406 
1407 	if (fastreboot_onpanic)
1408 		fastboot_load_kernel(fastreboot_onpanic_cmdline);
1409 	else if (reserve_mem_enabled)
1410 		fastboot_reserve_mem(&newkernel);
1411 }
1412 
1413 /*
1414  * Update boot configuration settings.
1415  * If the new fastreboot_onpanic setting is false, and a kernel has
1416  * been preloaded, free the memory;
1417  * if the new fastreboot_onpanic setting is true and newkernel is
1418  * not valid, load the new kernel.
1419  */
1420 void
1421 fastboot_update_config(const char *mdep)
1422 {
1423 	uint8_t boot_config = (uint8_t)*mdep;
1424 	int cur_fastreboot_onpanic = fastreboot_onpanic;
1425 
1426 	if (!fastreboot_capable)
1427 		return;
1428 
1429 	fastreboot_onpanic = boot_config & UA_FASTREBOOT_ONPANIC;
1430 	if (fastreboot_onpanic && (!cur_fastreboot_onpanic ||
1431 	    !newkernel.fi_valid))
1432 		fastboot_load_kernel(fastreboot_onpanic_cmdline);
1433 	if (cur_fastreboot_onpanic && !fastreboot_onpanic)
1434 		fastboot_free_newkernel(&newkernel);
1435 }
1436