xref: /illumos-gate/usr/src/uts/i86pc/os/fastboot.c (revision d0f40dc6a997c84bacf5f9ba83d57a95495c399b)
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 2010 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_impl.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 /* Index into Fast Reboot not supported message array */
121 static uint32_t fastreboot_nosup_id = FBNS_DEFAULT;
122 
123 /* Fast Reboot not supported message array */
124 static const char * const fastreboot_nosup_desc[FBNS_END] = {
125 #define	fastboot_nosup_msg(id, str)	str,
126 #include <sys/fastboot_msg.h>
127 };
128 
129 int fastboot_debug = 0;
130 int fastboot_contig = 0;
131 
132 /*
133  * Fake starting va for new kernel and boot archive.
134  */
135 static uintptr_t fake_va = FASTBOOT_FAKE_VA;
136 
137 /*
138  * Reserve memory below PA 1G in preparation of fast reboot.
139  *
140  * This variable is only checked when fastreboot_capable is set, but
141  * fastreboot_onpanic is not set.  The amount of memory reserved
142  * is negligible, but just in case we are really short of low memory,
143  * this variable will give us a backdoor to not consume memory at all.
144  */
145 int reserve_mem_enabled = 1;
146 
147 /*
148  * Mutex to protect fastreboot_onpanic.
149  */
150 kmutex_t fastreboot_config_mutex;
151 
152 /*
153  * Amount of memory below PA 1G to reserve for constructing the multiboot
154  * data structure and the page tables as we tend to run out of those
155  * when more drivers are loaded.
156  */
157 static size_t fastboot_mbi_size = 0x2000;	/* 8K */
158 static size_t fastboot_pagetable_size = 0x5000;	/* 20K */
159 
160 /*
161  * Minimum system uptime in clock_t before Fast Reboot should be used
162  * on panic.  Will be initialized in fastboot_post_startup().
163  */
164 clock_t fastreboot_onpanic_uptime = LONG_MAX;
165 
166 /*
167  * lbolt value when the system booted.  This value will be used if the system
168  * panics to calculate how long the system has been up.  If the uptime is less
169  * than fastreboot_onpanic_uptime, a reboot through BIOS will be performed to
170  * avoid a potential panic/reboot loop.
171  */
172 clock_t lbolt_at_boot = LONG_MAX;
173 
174 /*
175  * Use below 1G for page tables as
176  *	1. we are only doing 1:1 mapping of the bottom 1G of physical memory.
177  *	2. we are using 2G as the fake virtual address for the new kernel and
178  *	boot archive.
179  */
180 static ddi_dma_attr_t fastboot_below_1G_dma_attr = {
181 	DMA_ATTR_V0,
182 	0x0000000008000000ULL,	/* dma_attr_addr_lo: 128MB */
183 	0x000000003FFFFFFFULL,	/* dma_attr_addr_hi: 1G */
184 	0x00000000FFFFFFFFULL,	/* dma_attr_count_max */
185 	0x0000000000001000ULL,	/* dma_attr_align: 4KB */
186 	1,			/* dma_attr_burstsize */
187 	1,			/* dma_attr_minxfer */
188 	0x00000000FFFFFFFFULL,	/* dma_attr_maxxfer */
189 	0x00000000FFFFFFFFULL,	/* dma_attr_seg */
190 	1,			/* dma_attr_sgllen */
191 	0x1000ULL,		/* dma_attr_granular */
192 	0,			/* dma_attr_flags */
193 };
194 
195 static ddi_dma_attr_t fastboot_dma_attr = {
196 	DMA_ATTR_V0,
197 	0x0000000008000000ULL,	/* dma_attr_addr_lo: 128MB */
198 #ifdef	__amd64
199 	0xFFFFFFFFFFFFFFFFULL,	/* dma_attr_addr_hi: 2^64B */
200 #else
201 	0x0000000FFFFFFFFFULL,	/* dma_attr_addr_hi: 64GB */
202 #endif	/* __amd64 */
203 	0x00000000FFFFFFFFULL,	/* dma_attr_count_max */
204 	0x0000000000001000ULL,	/* dma_attr_align: 4KB */
205 	1,			/* dma_attr_burstsize */
206 	1,			/* dma_attr_minxfer */
207 	0x00000000FFFFFFFFULL,	/* dma_attr_maxxfer */
208 	0x00000000FFFFFFFFULL,	/* dma_attr_seg */
209 	1,			/* dma_attr_sgllen */
210 	0x1000ULL,		/* dma_attr_granular */
211 	0,			/* dma_attr_flags */
212 };
213 
214 /*
215  * Various information saved from the previous boot to reconstruct
216  * multiboot_info.
217  */
218 extern multiboot_info_t saved_mbi;
219 extern mb_memory_map_t saved_mmap[FASTBOOT_SAVED_MMAP_COUNT];
220 extern uint8_t saved_drives[FASTBOOT_SAVED_DRIVES_SIZE];
221 extern char saved_cmdline[FASTBOOT_SAVED_CMDLINE_LEN];
222 extern int saved_cmdline_len;
223 extern size_t saved_file_size[];
224 
225 extern void* contig_alloc(size_t size, ddi_dma_attr_t *attr,
226     uintptr_t align, int cansleep);
227 extern void contig_free(void *addr, size_t size);
228 
229 
230 /* PRINTLIKE */
231 extern void vprintf(const char *, va_list);
232 
233 
234 /*
235  * Need to be able to get boot_archives from other places
236  */
237 #define	BOOTARCHIVE64	"/platform/i86pc/amd64/boot_archive"
238 #define	BOOTARCHIVE32	"/platform/i86pc/boot_archive"
239 #define	BOOTARCHIVE32_FAILSAFE	"/boot/x86.miniroot-safe"
240 #define	BOOTARCHIVE64_FAILSAFE	"/boot/amd64/x86.miniroot-safe"
241 #define	FAILSAFE_BOOTFILE32	"/boot/platform/i86pc/kernel/unix"
242 #define	FAILSAFE_BOOTFILE64	"/boot/platform/i86pc/kernel/amd64/unix"
243 
244 static uint_t fastboot_vatoindex(fastboot_info_t *, uintptr_t, int);
245 static void fastboot_map_with_size(fastboot_info_t *, uintptr_t,
246     paddr_t, size_t, int);
247 static void fastboot_build_pagetables(fastboot_info_t *);
248 static int fastboot_build_mbi(char *, fastboot_info_t *);
249 static void fastboot_free_file(fastboot_file_t *);
250 
251 static const char fastboot_enomem_msg[] = "!Fastboot: Couldn't allocate 0x%"
252 	PRIx64" bytes below %s to do fast reboot";
253 
254 static void
255 dprintf(char *fmt, ...)
256 {
257 	va_list adx;
258 
259 	if (!fastboot_debug)
260 		return;
261 
262 	va_start(adx, fmt);
263 	vprintf(fmt, adx);
264 	va_end(adx);
265 }
266 
267 
268 /*
269  * Return the index corresponding to a virt address at a given page table level.
270  */
271 static uint_t
272 fastboot_vatoindex(fastboot_info_t *nk, uintptr_t va, int level)
273 {
274 	return ((va >> nk->fi_shift_amt[level]) & (nk->fi_ptes_per_table - 1));
275 }
276 
277 
278 /*
279  * Add mapping from vstart to pstart for the specified size.
280  * vstart, pstart and size should all have been aligned at 2M boundaries.
281  */
282 static void
283 fastboot_map_with_size(fastboot_info_t *nk, uintptr_t vstart, paddr_t pstart,
284     size_t size, int level)
285 {
286 	x86pte_t	pteval, *table;
287 	uintptr_t	vaddr;
288 	paddr_t		paddr;
289 	int		index, l;
290 
291 	table = (x86pte_t *)(nk->fi_pagetable_va);
292 
293 	for (l = nk->fi_top_level; l >= level; l--) {
294 
295 		index = fastboot_vatoindex(nk, vstart, l);
296 
297 		if (l == level) {
298 			/*
299 			 * Last level.  Program the page table entries.
300 			 */
301 			for (vaddr = vstart, paddr = pstart;
302 			    vaddr < vstart + size;
303 			    vaddr += (1ULL << nk->fi_shift_amt[l]),
304 			    paddr += (1ULL << nk->fi_shift_amt[l])) {
305 
306 				uint_t index = fastboot_vatoindex(nk, vaddr, l);
307 
308 				if (l > 0)
309 					pteval = paddr | pte_bits | PT_PAGESIZE;
310 				else
311 					pteval = paddr | pte_bits;
312 
313 				table[index] = pteval;
314 			}
315 		} else if (table[index] & PT_VALID) {
316 
317 			table = (x86pte_t *)
318 			    ((uintptr_t)(((paddr_t)table[index] & MMU_PAGEMASK)
319 			    - nk->fi_pagetable_pa) + nk->fi_pagetable_va);
320 		} else {
321 			/*
322 			 * Intermediate levels.
323 			 * Program with either valid bit or PTP bits.
324 			 */
325 			if (l == nk->fi_top_level) {
326 #ifdef	__amd64
327 				ASSERT(nk->fi_top_level == 3);
328 				table[index] = nk->fi_next_table_pa | ptp_bits;
329 #else
330 				table[index] = nk->fi_next_table_pa | PT_VALID;
331 #endif	/* __amd64 */
332 			} else {
333 				table[index] = nk->fi_next_table_pa | ptp_bits;
334 			}
335 			table = (x86pte_t *)(nk->fi_next_table_va);
336 			nk->fi_next_table_va += MMU_PAGESIZE;
337 			nk->fi_next_table_pa += MMU_PAGESIZE;
338 		}
339 	}
340 }
341 
342 /*
343  * Build page tables for the lower 1G of physical memory using 2M
344  * pages, and prepare page tables for mapping new kernel and boot
345  * archive pages using 4K pages.
346  */
347 static void
348 fastboot_build_pagetables(fastboot_info_t *nk)
349 {
350 	/*
351 	 * Map lower 1G physical memory.  Use large pages.
352 	 */
353 	fastboot_map_with_size(nk, 0, 0, ONE_GIG, 1);
354 
355 	/*
356 	 * Map one 4K page to get the middle page tables set up.
357 	 */
358 	fake_va = P2ALIGN_TYPED(fake_va, nk->fi_lpagesize, uintptr_t);
359 	fastboot_map_with_size(nk, fake_va,
360 	    nk->fi_files[0].fb_pte_list_va[0] & MMU_PAGEMASK, PAGESIZE, 0);
361 }
362 
363 
364 /*
365  * Sanity check.  Look for dboot offset.
366  */
367 static int
368 fastboot_elf64_find_dboot_load_offset(void *img, off_t imgsz, uint32_t *offp)
369 {
370 	Elf64_Ehdr	*ehdr = (Elf64_Ehdr *)img;
371 	Elf64_Phdr	*phdr;
372 	uint8_t		*phdrbase;
373 	int		i;
374 
375 	if ((ehdr->e_phoff + ehdr->e_phnum * ehdr->e_phentsize) >= imgsz)
376 		return (-1);
377 
378 	phdrbase = (uint8_t *)img + ehdr->e_phoff;
379 
380 	for (i = 0; i < ehdr->e_phnum; i++) {
381 		phdr = (Elf64_Phdr *)(phdrbase + ehdr->e_phentsize * i);
382 
383 		if (phdr->p_type == PT_LOAD) {
384 			if (phdr->p_vaddr == phdr->p_paddr &&
385 			    phdr->p_vaddr == DBOOT_ENTRY_ADDRESS) {
386 				ASSERT(phdr->p_offset <= UINT32_MAX);
387 				*offp = (uint32_t)phdr->p_offset;
388 				return (0);
389 			}
390 		}
391 	}
392 
393 	return (-1);
394 }
395 
396 
397 /*
398  * Initialize text and data section information for 32-bit kernel.
399  * sectcntp - is both input/output parameter.
400  * On entry, *sectcntp contains maximum allowable number of sections;
401  * on return, it contains the actual number of sections filled.
402  */
403 static int
404 fastboot_elf32_find_loadables(void *img, off_t imgsz, fastboot_section_t *sectp,
405     int *sectcntp, uint32_t *offp)
406 {
407 	Elf32_Ehdr	*ehdr = (Elf32_Ehdr *)img;
408 	Elf32_Phdr	*phdr;
409 	uint8_t		*phdrbase;
410 	int		i;
411 	int		used_sections = 0;
412 	const int	max_sectcnt = *sectcntp;
413 
414 	if ((ehdr->e_phoff + ehdr->e_phnum * ehdr->e_phentsize) >= imgsz)
415 		return (-1);
416 
417 	phdrbase = (uint8_t *)img + ehdr->e_phoff;
418 
419 	for (i = 0; i < ehdr->e_phnum; i++) {
420 		phdr = (Elf32_Phdr *)(phdrbase + ehdr->e_phentsize * i);
421 
422 		if (phdr->p_type == PT_INTERP)
423 			return (-1);
424 
425 		if (phdr->p_type != PT_LOAD)
426 			continue;
427 
428 		if (phdr->p_vaddr == phdr->p_paddr &&
429 		    phdr->p_paddr == DBOOT_ENTRY_ADDRESS) {
430 			*offp = (uint32_t)phdr->p_offset;
431 		} else {
432 			if (max_sectcnt <= used_sections)
433 				return (-1);
434 
435 			sectp[used_sections].fb_sec_offset = phdr->p_offset;
436 			sectp[used_sections].fb_sec_paddr = phdr->p_paddr;
437 			sectp[used_sections].fb_sec_size = phdr->p_filesz;
438 			sectp[used_sections].fb_sec_bss_size =
439 			    (phdr->p_filesz < phdr->p_memsz) ?
440 			    (phdr->p_memsz - phdr->p_filesz) : 0;
441 
442 			/* Extra sanity check for the input object file */
443 			if (sectp[used_sections].fb_sec_paddr +
444 			    sectp[used_sections].fb_sec_size +
445 			    sectp[used_sections].fb_sec_bss_size >=
446 			    DBOOT_ENTRY_ADDRESS)
447 				return (-1);
448 
449 			used_sections++;
450 		}
451 	}
452 
453 	*sectcntp = used_sections;
454 	return (0);
455 }
456 
457 /*
458  * Create multiboot info structure (mbi) base on the saved mbi.
459  * Recalculate values of the pointer type fields in the data
460  * structure based on the new starting physical address of the
461  * data structure.
462  */
463 static int
464 fastboot_build_mbi(char *mdep, fastboot_info_t *nk)
465 {
466 	mb_module_t	*mbp;
467 	multiboot_info_t	*mbi;	/* pointer to multiboot structure */
468 	uintptr_t	start_addr_va;	/* starting VA of mbi */
469 	uintptr_t	start_addr_pa;	/* starting PA of mbi */
470 	size_t		offs = 0;	/* offset from the starting address */
471 	size_t		arglen;		/* length of the command line arg */
472 	size_t		size;	/* size of the memory reserved for mbi */
473 	size_t		mdnsz;	/* length of the boot archive name */
474 
475 	/*
476 	 * If mdep is not NULL or empty, use the length of mdep + 1
477 	 * (for NULL terminating) as the length of the new command
478 	 * line; else use the saved command line length as the
479 	 * length for the new command line.
480 	 */
481 	if (mdep != NULL && strlen(mdep) != 0) {
482 		arglen = strlen(mdep) + 1;
483 	} else {
484 		arglen = saved_cmdline_len;
485 	}
486 
487 	/*
488 	 * Allocate memory for the new multiboot info structure (mbi).
489 	 * If we have reserved memory for mbi but it's not enough,
490 	 * free it and reallocate.
491 	 */
492 	size = PAGESIZE + P2ROUNDUP(arglen, PAGESIZE);
493 	if (nk->fi_mbi_size && nk->fi_mbi_size < size) {
494 		contig_free((void *)nk->fi_new_mbi_va, nk->fi_mbi_size);
495 		nk->fi_mbi_size = 0;
496 	}
497 
498 	if (nk->fi_mbi_size == 0) {
499 		if ((nk->fi_new_mbi_va =
500 		    (uintptr_t)contig_alloc(size, &fastboot_below_1G_dma_attr,
501 		    PAGESIZE, 0)) == NULL) {
502 			cmn_err(CE_NOTE, fastboot_enomem_msg,
503 			    (uint64_t)size, "1G");
504 			return (-1);
505 		}
506 		/*
507 		 * fi_mbi_size must be set after the allocation succeeds
508 		 * as it's used to determine how much memory to free.
509 		 */
510 		nk->fi_mbi_size = size;
511 	}
512 
513 	/*
514 	 * Initalize memory
515 	 */
516 	bzero((void *)nk->fi_new_mbi_va, nk->fi_mbi_size);
517 
518 	/*
519 	 * Get PA for the new mbi
520 	 */
521 	start_addr_va = nk->fi_new_mbi_va;
522 	start_addr_pa = mmu_ptob((uint64_t)hat_getpfnum(kas.a_hat,
523 	    (caddr_t)start_addr_va));
524 	nk->fi_new_mbi_pa = (paddr_t)start_addr_pa;
525 
526 	/*
527 	 * Populate the rest of the fields in the data structure
528 	 */
529 
530 	/*
531 	 * Copy from the saved mbi to preserve all non-pointer type fields.
532 	 */
533 	mbi = (multiboot_info_t *)start_addr_va;
534 	bcopy(&saved_mbi, mbi, sizeof (*mbi));
535 
536 	/*
537 	 * Recalculate mods_addr.  Set mod_start and mod_end based on
538 	 * the physical address of the new boot archive.  Set mod_name
539 	 * to the name of the new boto archive.
540 	 */
541 	offs += sizeof (multiboot_info_t);
542 	mbi->mods_addr = start_addr_pa + offs;
543 	mbp = (mb_module_t *)(start_addr_va + offs);
544 	mbp->mod_start = nk->fi_files[FASTBOOT_BOOTARCHIVE].fb_dest_pa;
545 	mbp->mod_end = nk->fi_files[FASTBOOT_BOOTARCHIVE].fb_next_pa;
546 
547 	offs += sizeof (mb_module_t);
548 	mdnsz = strlen(fastboot_filename[FASTBOOT_NAME_BOOTARCHIVE]) + 1;
549 	bcopy(fastboot_filename[FASTBOOT_NAME_BOOTARCHIVE],
550 	    (void *)(start_addr_va + offs), mdnsz);
551 	mbp->mod_name = start_addr_pa + offs;
552 	mbp->reserved = 0;
553 
554 	/*
555 	 * Make sure the offset is 16-byte aligned to avoid unaligned access.
556 	 */
557 	offs += mdnsz;
558 	offs = P2ROUNDUP_TYPED(offs, 16, size_t);
559 
560 	/*
561 	 * Recalculate mmap_addr
562 	 */
563 	mbi->mmap_addr = start_addr_pa + offs;
564 	bcopy((void *)(uintptr_t)saved_mmap, (void *)(start_addr_va + offs),
565 	    saved_mbi.mmap_length);
566 	offs += saved_mbi.mmap_length;
567 
568 	/*
569 	 * Recalculate drives_addr
570 	 */
571 	mbi->drives_addr = start_addr_pa + offs;
572 	bcopy((void *)(uintptr_t)saved_drives, (void *)(start_addr_va + offs),
573 	    saved_mbi.drives_length);
574 	offs += saved_mbi.drives_length;
575 
576 	/*
577 	 * Recalculate the address of cmdline.  Set cmdline to contain the
578 	 * new boot argument.
579 	 */
580 	mbi->cmdline = start_addr_pa + offs;
581 
582 	if (mdep != NULL && strlen(mdep) != 0) {
583 		bcopy(mdep, (void *)(start_addr_va + offs), arglen);
584 	} else {
585 		bcopy((void *)saved_cmdline, (void *)(start_addr_va + offs),
586 		    arglen);
587 	}
588 
589 	/* clear fields and flags that are not copied */
590 	bzero(&mbi->config_table,
591 	    sizeof (*mbi) - offsetof(multiboot_info_t, config_table));
592 	mbi->flags &= ~(MB_INFO_CONFIG_TABLE | MB_INFO_BOOT_LOADER_NAME |
593 	    MB_INFO_APM_TABLE | MB_INFO_VIDEO_INFO);
594 
595 	return (0);
596 }
597 
598 /*
599  * Initialize HAT related fields
600  */
601 static void
602 fastboot_init_fields(fastboot_info_t *nk)
603 {
604 	if (x86_feature & X86_PAE) {
605 		nk->fi_has_pae = 1;
606 		nk->fi_shift_amt = fastboot_shift_amt_pae;
607 		nk->fi_ptes_per_table = 512;
608 		nk->fi_lpagesize = (2 << 20);	/* 2M */
609 #ifdef	__amd64
610 		nk->fi_top_level = 3;
611 #else
612 		nk->fi_top_level = 2;
613 #endif	/* __amd64 */
614 	}
615 }
616 
617 /*
618  * Process boot argument
619  */
620 static void
621 fastboot_parse_mdep(char *mdep, char *kern_bootpath, int *bootpath_len,
622     char *bootargs)
623 {
624 	int	i;
625 
626 	/*
627 	 * If mdep is not NULL, it comes in the format of
628 	 *	mountpoint unix args
629 	 */
630 	if (mdep != NULL && strlen(mdep) != 0) {
631 		if (mdep[0] != '-') {
632 			/* First get the root argument */
633 			i = 0;
634 			while (mdep[i] != '\0' && mdep[i] != ' ') {
635 				i++;
636 			}
637 
638 			if (i < 4 || strncmp(&mdep[i-4], "unix", 4) != 0) {
639 				/* mount point */
640 				bcopy(mdep, kern_bootpath, i);
641 				kern_bootpath[i] = '\0';
642 				*bootpath_len = i;
643 
644 				/*
645 				 * Get the next argument. It should be unix as
646 				 * we have validated in in halt.c.
647 				 */
648 				if (strlen(mdep) > i) {
649 					mdep += (i + 1);
650 					i = 0;
651 					while (mdep[i] != '\0' &&
652 					    mdep[i] != ' ') {
653 						i++;
654 					}
655 				}
656 
657 			}
658 			bcopy(mdep, kern_bootfile, i);
659 			kern_bootfile[i] = '\0';
660 			bcopy(mdep, bootargs, strlen(mdep));
661 		} else {
662 			int off = strlen(kern_bootfile);
663 			bcopy(kern_bootfile, bootargs, off);
664 			bcopy(" ", &bootargs[off++], 1);
665 			bcopy(mdep, &bootargs[off], strlen(mdep));
666 			off += strlen(mdep);
667 			bootargs[off] = '\0';
668 		}
669 	}
670 }
671 
672 /*
673  * Reserve memory under PA 1G for mapping the new kernel and boot archive.
674  * This function is only called if fastreboot_onpanic is *not* set.
675  */
676 static void
677 fastboot_reserve_mem(fastboot_info_t *nk)
678 {
679 	int i;
680 
681 	/*
682 	 * A valid kernel is in place.  No need to reserve any memory.
683 	 */
684 	if (nk->fi_valid)
685 		return;
686 
687 	/*
688 	 * Reserve memory under PA 1G for PTE lists.
689 	 */
690 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
691 		fastboot_file_t *fb = &nk->fi_files[i];
692 		size_t fsize_roundup, size;
693 
694 		fsize_roundup = P2ROUNDUP_TYPED(saved_file_size[i],
695 		    PAGESIZE, size_t);
696 		size = FASTBOOT_PTE_LIST_SIZE(fsize_roundup);
697 		if ((fb->fb_pte_list_va = contig_alloc(size,
698 		    &fastboot_below_1G_dma_attr, PAGESIZE, 0)) == NULL) {
699 			return;
700 		}
701 		fb->fb_pte_list_size = size;
702 	}
703 
704 	/*
705 	 * Reserve memory under PA 1G for page tables.
706 	 */
707 	if ((nk->fi_pagetable_va =
708 	    (uintptr_t)contig_alloc(fastboot_pagetable_size,
709 	    &fastboot_below_1G_dma_attr, PAGESIZE, 0)) == NULL) {
710 		return;
711 	}
712 	nk->fi_pagetable_size = fastboot_pagetable_size;
713 
714 	/*
715 	 * Reserve memory under PA 1G for multiboot structure.
716 	 */
717 	if ((nk->fi_new_mbi_va = (uintptr_t)contig_alloc(fastboot_mbi_size,
718 	    &fastboot_below_1G_dma_attr, PAGESIZE, 0)) == NULL) {
719 		return;
720 	}
721 	nk->fi_mbi_size = fastboot_mbi_size;
722 }
723 
724 /*
725  * Calculate MD5 digest for the given fastboot_file.
726  * Assumes that the file is allready loaded properly.
727  */
728 static void
729 fastboot_cksum_file(fastboot_file_t *fb, uchar_t *md5_hash)
730 {
731 	MD5_CTX md5_ctx;
732 
733 	MD5Init(&md5_ctx);
734 	MD5Update(&md5_ctx, (void *)fb->fb_va, fb->fb_size);
735 	MD5Final(md5_hash, &md5_ctx);
736 }
737 
738 /*
739  * Free up the memory we have allocated for a file
740  */
741 static void
742 fastboot_free_file(fastboot_file_t *fb)
743 {
744 	size_t	fsize_roundup;
745 
746 	fsize_roundup = P2ROUNDUP_TYPED(fb->fb_size, PAGESIZE, size_t);
747 	if (fsize_roundup) {
748 		contig_free((void *)fb->fb_va, fsize_roundup);
749 		fb->fb_va = NULL;
750 		fb->fb_size = 0;
751 	}
752 }
753 
754 /*
755  * Free up memory used by the PTEs for a file.
756  */
757 static void
758 fastboot_free_file_pte(fastboot_file_t *fb, uint64_t endaddr)
759 {
760 	if (fb->fb_pte_list_size && fb->fb_pte_list_pa < endaddr) {
761 		contig_free((void *)fb->fb_pte_list_va, fb->fb_pte_list_size);
762 		fb->fb_pte_list_va = 0;
763 		fb->fb_pte_list_pa = 0;
764 		fb->fb_pte_list_size = 0;
765 	}
766 }
767 
768 /*
769  * Free up all the memory used for representing a kernel with
770  * fastboot_info_t.
771  */
772 static void
773 fastboot_free_mem(fastboot_info_t *nk, uint64_t endaddr)
774 {
775 	int i;
776 
777 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
778 		fastboot_free_file(nk->fi_files + i);
779 		fastboot_free_file_pte(nk->fi_files + i, endaddr);
780 	}
781 
782 	if (nk->fi_pagetable_size && nk->fi_pagetable_pa < endaddr) {
783 		contig_free((void *)nk->fi_pagetable_va, nk->fi_pagetable_size);
784 		nk->fi_pagetable_va = 0;
785 		nk->fi_pagetable_pa = 0;
786 		nk->fi_pagetable_size = 0;
787 	}
788 
789 	if (nk->fi_mbi_size && nk->fi_new_mbi_pa < endaddr) {
790 		contig_free((void *)nk->fi_new_mbi_va, nk->fi_mbi_size);
791 		nk->fi_new_mbi_va = 0;
792 		nk->fi_new_mbi_pa = 0;
793 		nk->fi_mbi_size = 0;
794 	}
795 }
796 
797 /*
798  * Only free up the memory allocated for the kernel and boot archive,
799  * but not for the page tables.
800  */
801 void
802 fastboot_free_newkernel(fastboot_info_t *nk)
803 {
804 	int i;
805 
806 	nk->fi_valid = 0;
807 	/*
808 	 * Free the memory we have allocated
809 	 */
810 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
811 		fastboot_free_file(&(nk->fi_files[i]));
812 	}
813 }
814 
815 static void
816 fastboot_cksum_cdata(fastboot_info_t *nk, uchar_t *md5_hash)
817 {
818 	int i;
819 	MD5_CTX md5_ctx;
820 
821 	MD5Init(&md5_ctx);
822 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
823 		MD5Update(&md5_ctx, nk->fi_files[i].fb_pte_list_va,
824 		    nk->fi_files[i].fb_pte_list_size);
825 	}
826 	MD5Update(&md5_ctx, (void *)nk->fi_pagetable_va, nk->fi_pagetable_size);
827 	MD5Update(&md5_ctx, (void *)nk->fi_new_mbi_va, nk->fi_mbi_size);
828 
829 	MD5Final(md5_hash, &md5_ctx);
830 }
831 
832 /*
833  * Generate MD5 checksum of the given kernel.
834  */
835 static void
836 fastboot_cksum_generate(fastboot_info_t *nk)
837 {
838 	int i;
839 
840 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
841 		fastboot_cksum_file(nk->fi_files + i, nk->fi_md5_hash[i]);
842 	}
843 	fastboot_cksum_cdata(nk, nk->fi_md5_hash[i]);
844 }
845 
846 /*
847  * Calculate MD5 checksum of the given kernel and verify that
848  * it matches with what was calculated before.
849  */
850 int
851 fastboot_cksum_verify(fastboot_info_t *nk)
852 {
853 	int i;
854 	uchar_t md5_hash[MD5_DIGEST_LENGTH];
855 
856 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
857 		fastboot_cksum_file(nk->fi_files + i, md5_hash);
858 		if (bcmp(nk->fi_md5_hash[i], md5_hash,
859 		    sizeof (nk->fi_md5_hash[i])) != 0)
860 			return (i + 1);
861 	}
862 
863 	fastboot_cksum_cdata(nk, md5_hash);
864 	if (bcmp(nk->fi_md5_hash[i], md5_hash,
865 	    sizeof (nk->fi_md5_hash[i])) != 0)
866 		return (i + 1);
867 
868 	return (0);
869 }
870 
871 /*
872  * This function performs the following tasks:
873  * - Read the sizes of the new kernel and boot archive.
874  * - Allocate memory for the new kernel and boot archive.
875  * - Allocate memory for page tables necessary for mapping the memory
876  *   allocated for the files.
877  * - Read the new kernel and boot archive into memory.
878  * - Map in the fast reboot switcher.
879  * - Load the fast reboot switcher to FASTBOOT_SWTCH_PA.
880  * - Build the new multiboot_info structure
881  * - Build page tables for the low 1G of physical memory.
882  * - Mark the data structure as valid if all steps have succeeded.
883  */
884 void
885 fastboot_load_kernel(char *mdep)
886 {
887 	void		*buf = NULL;
888 	int		i;
889 	fastboot_file_t	*fb;
890 	uint32_t	dboot_start_offset;
891 	char		kern_bootpath[OBP_MAXPATHLEN];
892 	extern uintptr_t postbootkernelbase;
893 	uintptr_t	saved_kernelbase;
894 	int		bootpath_len = 0;
895 	int		is_failsafe = 0;
896 	int		is_retry = 0;
897 	uint64_t	end_addr;
898 
899 	if (!fastreboot_capable)
900 		return;
901 
902 	if (newkernel.fi_valid)
903 		fastboot_free_newkernel(&newkernel);
904 
905 	saved_kernelbase = postbootkernelbase;
906 
907 	postbootkernelbase = 0;
908 
909 	/*
910 	 * Initialize various HAT related fields in the data structure
911 	 */
912 	fastboot_init_fields(&newkernel);
913 
914 	bzero(kern_bootpath, OBP_MAXPATHLEN);
915 
916 	/*
917 	 * Process the boot argument
918 	 */
919 	bzero(fastboot_args, OBP_MAXPATHLEN);
920 	fastboot_parse_mdep(mdep, kern_bootpath, &bootpath_len, fastboot_args);
921 
922 	/*
923 	 * Make sure we get the null character
924 	 */
925 	bcopy(kern_bootpath, fastboot_filename[FASTBOOT_NAME_UNIX],
926 	    bootpath_len);
927 	bcopy(kern_bootfile,
928 	    &fastboot_filename[FASTBOOT_NAME_UNIX][bootpath_len],
929 	    strlen(kern_bootfile) + 1);
930 
931 	bcopy(kern_bootpath, fastboot_filename[FASTBOOT_NAME_BOOTARCHIVE],
932 	    bootpath_len);
933 
934 	if (bcmp(kern_bootfile, FAILSAFE_BOOTFILE32,
935 	    (sizeof (FAILSAFE_BOOTFILE32) - 1)) == 0 ||
936 	    bcmp(kern_bootfile, FAILSAFE_BOOTFILE64,
937 	    (sizeof (FAILSAFE_BOOTFILE64) - 1)) == 0) {
938 		is_failsafe = 1;
939 	}
940 
941 load_kernel_retry:
942 	/*
943 	 * Read in unix and boot_archive
944 	 */
945 	end_addr = DBOOT_ENTRY_ADDRESS;
946 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
947 		struct _buf	*file;
948 		uintptr_t	va;
949 		uint64_t	fsize;
950 		size_t		fsize_roundup, pt_size;
951 		int		page_index;
952 		uintptr_t	offset;
953 		ddi_dma_attr_t dma_attr = fastboot_dma_attr;
954 
955 
956 		dprintf("fastboot_filename[%d] = %s\n",
957 		    i, fastboot_filename[i]);
958 
959 		if ((file = kobj_open_file(fastboot_filename[i])) ==
960 		    (struct _buf *)-1) {
961 			cmn_err(CE_NOTE, "!Fastboot: Couldn't open %s",
962 			    fastboot_filename[i]);
963 			goto err_out;
964 		}
965 
966 		if (kobj_get_filesize(file, &fsize) != 0) {
967 			cmn_err(CE_NOTE,
968 			    "!Fastboot: Couldn't get filesize for %s",
969 			    fastboot_filename[i]);
970 			goto err_out;
971 		}
972 
973 		fsize_roundup = P2ROUNDUP_TYPED(fsize, PAGESIZE, size_t);
974 
975 		/*
976 		 * Where the files end in physical memory after being
977 		 * relocated by the fast boot switcher.
978 		 */
979 		end_addr += fsize_roundup;
980 		if (end_addr > fastboot_below_1G_dma_attr.dma_attr_addr_hi) {
981 			cmn_err(CE_NOTE, "!Fastboot: boot archive is too big");
982 			goto err_out;
983 		}
984 
985 		/*
986 		 * Adjust dma_attr_addr_lo so that the new kernel and boot
987 		 * archive will not be overridden during relocation.
988 		 */
989 		if (end_addr > fastboot_dma_attr.dma_attr_addr_lo ||
990 		    end_addr > fastboot_below_1G_dma_attr.dma_attr_addr_lo) {
991 
992 			if (is_retry) {
993 				/*
994 				 * If we have already tried and didn't succeed,
995 				 * just give up.
996 				 */
997 				cmn_err(CE_NOTE,
998 				    "!Fastboot: boot archive is too big");
999 				goto err_out;
1000 			} else {
1001 				/* Set the flag so we don't keep retrying */
1002 				is_retry++;
1003 
1004 				/* Adjust dma_attr_addr_lo */
1005 				fastboot_dma_attr.dma_attr_addr_lo = end_addr;
1006 				fastboot_below_1G_dma_attr.dma_attr_addr_lo =
1007 				    end_addr;
1008 
1009 				/*
1010 				 * Free the memory we have already allocated
1011 				 * whose physical addresses might not fit
1012 				 * the new lo and hi constraints.
1013 				 */
1014 				fastboot_free_mem(&newkernel, end_addr);
1015 				goto load_kernel_retry;
1016 			}
1017 		}
1018 
1019 
1020 		if (!fastboot_contig)
1021 			dma_attr.dma_attr_sgllen = (fsize / PAGESIZE) +
1022 			    (((fsize % PAGESIZE) == 0) ? 0 : 1);
1023 
1024 		if ((buf = contig_alloc(fsize, &dma_attr, PAGESIZE, 0))
1025 		    == NULL) {
1026 			cmn_err(CE_NOTE, fastboot_enomem_msg, fsize, "64G");
1027 			goto err_out;
1028 		}
1029 
1030 		va = P2ROUNDUP_TYPED((uintptr_t)buf, PAGESIZE, uintptr_t);
1031 
1032 		if (kobj_read_file(file, (char *)va, fsize, 0) < 0) {
1033 			cmn_err(CE_NOTE, "!Fastboot: Couldn't read %s",
1034 			    fastboot_filename[i]);
1035 			goto err_out;
1036 		}
1037 
1038 		fb = &newkernel.fi_files[i];
1039 		fb->fb_va = va;
1040 		fb->fb_size = fsize;
1041 		fb->fb_sectcnt = 0;
1042 
1043 		pt_size = FASTBOOT_PTE_LIST_SIZE(fsize_roundup);
1044 
1045 		/*
1046 		 * If we have reserved memory but it not enough, free it.
1047 		 */
1048 		if (fb->fb_pte_list_size && fb->fb_pte_list_size < pt_size) {
1049 			contig_free((void *)fb->fb_pte_list_va,
1050 			    fb->fb_pte_list_size);
1051 			fb->fb_pte_list_size = 0;
1052 		}
1053 
1054 		if (fb->fb_pte_list_size == 0) {
1055 			if ((fb->fb_pte_list_va =
1056 			    (x86pte_t *)contig_alloc(pt_size,
1057 			    &fastboot_below_1G_dma_attr, PAGESIZE, 0))
1058 			    == NULL) {
1059 				cmn_err(CE_NOTE, fastboot_enomem_msg,
1060 				    (uint64_t)pt_size, "1G");
1061 				goto err_out;
1062 			}
1063 			/*
1064 			 * fb_pte_list_size must be set after the allocation
1065 			 * succeeds as it's used to determine how much memory to
1066 			 * free.
1067 			 */
1068 			fb->fb_pte_list_size = pt_size;
1069 		}
1070 
1071 		bzero((void *)(fb->fb_pte_list_va), fb->fb_pte_list_size);
1072 
1073 		fb->fb_pte_list_pa = mmu_ptob((uint64_t)hat_getpfnum(kas.a_hat,
1074 		    (caddr_t)fb->fb_pte_list_va));
1075 
1076 		for (page_index = 0, offset = 0; offset < fb->fb_size;
1077 		    offset += PAGESIZE) {
1078 			uint64_t paddr;
1079 
1080 			paddr = mmu_ptob((uint64_t)hat_getpfnum(kas.a_hat,
1081 			    (caddr_t)fb->fb_va + offset));
1082 
1083 			ASSERT(paddr >= fastboot_dma_attr.dma_attr_addr_lo);
1084 
1085 			/*
1086 			 * Include the pte_bits so we don't have to make
1087 			 * it in assembly.
1088 			 */
1089 			fb->fb_pte_list_va[page_index++] = (x86pte_t)
1090 			    (paddr | pte_bits);
1091 		}
1092 
1093 		fb->fb_pte_list_va[page_index] = FASTBOOT_TERMINATE;
1094 
1095 		if (i == FASTBOOT_UNIX) {
1096 			Ehdr	*ehdr = (Ehdr *)va;
1097 			int	j;
1098 
1099 			/*
1100 			 * Sanity checks:
1101 			 */
1102 			for (j = 0; j < SELFMAG; j++) {
1103 				if (ehdr->e_ident[j] != ELFMAG[j]) {
1104 					cmn_err(CE_NOTE, "!Fastboot: Bad ELF "
1105 					    "signature");
1106 					goto err_out;
1107 				}
1108 			}
1109 
1110 			if (ehdr->e_ident[EI_CLASS] == ELFCLASS32 &&
1111 			    ehdr->e_ident[EI_DATA] == ELFDATA2LSB &&
1112 			    ehdr->e_machine == EM_386) {
1113 
1114 				fb->fb_sectcnt = sizeof (fb->fb_sections) /
1115 				    sizeof (fb->fb_sections[0]);
1116 
1117 				if (fastboot_elf32_find_loadables((void *)va,
1118 				    fsize, &fb->fb_sections[0],
1119 				    &fb->fb_sectcnt, &dboot_start_offset) < 0) {
1120 					cmn_err(CE_NOTE, "!Fastboot: ELF32 "
1121 					    "program section failure");
1122 					goto err_out;
1123 				}
1124 
1125 				if (fb->fb_sectcnt == 0) {
1126 					cmn_err(CE_NOTE, "!Fastboot: No ELF32 "
1127 					    "program sections found");
1128 					goto err_out;
1129 				}
1130 
1131 				if (is_failsafe) {
1132 					/* Failsafe boot_archive */
1133 					bcopy(BOOTARCHIVE32_FAILSAFE,
1134 					    &fastboot_filename
1135 					    [FASTBOOT_NAME_BOOTARCHIVE]
1136 					    [bootpath_len],
1137 					    sizeof (BOOTARCHIVE32_FAILSAFE));
1138 				} else {
1139 					bcopy(BOOTARCHIVE32,
1140 					    &fastboot_filename
1141 					    [FASTBOOT_NAME_BOOTARCHIVE]
1142 					    [bootpath_len],
1143 					    sizeof (BOOTARCHIVE32));
1144 				}
1145 
1146 			} else if (ehdr->e_ident[EI_CLASS] == ELFCLASS64 &&
1147 			    ehdr->e_ident[EI_DATA] == ELFDATA2LSB &&
1148 			    ehdr->e_machine == EM_AMD64) {
1149 
1150 				if (fastboot_elf64_find_dboot_load_offset(
1151 				    (void *)va, fsize, &dboot_start_offset)
1152 				    != 0) {
1153 					cmn_err(CE_NOTE, "!Fastboot: Couldn't "
1154 					    "find ELF64 dboot entry offset");
1155 					goto err_out;
1156 				}
1157 
1158 				if ((x86_feature & X86_64) == 0 ||
1159 				    (x86_feature & X86_PAE) == 0) {
1160 					cmn_err(CE_NOTE, "!Fastboot: Cannot "
1161 					    "reboot to %s: "
1162 					    "not a 64-bit capable system",
1163 					    kern_bootfile);
1164 					goto err_out;
1165 				}
1166 
1167 				if (is_failsafe) {
1168 					/* Failsafe boot_archive */
1169 					bcopy(BOOTARCHIVE64_FAILSAFE,
1170 					    &fastboot_filename
1171 					    [FASTBOOT_NAME_BOOTARCHIVE]
1172 					    [bootpath_len],
1173 					    sizeof (BOOTARCHIVE64_FAILSAFE));
1174 				} else {
1175 					bcopy(BOOTARCHIVE64,
1176 					    &fastboot_filename
1177 					    [FASTBOOT_NAME_BOOTARCHIVE]
1178 					    [bootpath_len],
1179 					    sizeof (BOOTARCHIVE64));
1180 				}
1181 			} else {
1182 				cmn_err(CE_NOTE, "!Fastboot: Unknown ELF type");
1183 				goto err_out;
1184 			}
1185 
1186 			fb->fb_dest_pa = DBOOT_ENTRY_ADDRESS -
1187 			    dboot_start_offset;
1188 
1189 			fb->fb_next_pa = DBOOT_ENTRY_ADDRESS + fsize_roundup;
1190 		} else {
1191 			fb->fb_dest_pa = newkernel.fi_files[i - 1].fb_next_pa;
1192 			fb->fb_next_pa = fb->fb_dest_pa + fsize_roundup;
1193 		}
1194 
1195 		kobj_close_file(file);
1196 
1197 	}
1198 
1199 	/*
1200 	 * Add the function that will switch us to 32-bit protected mode
1201 	 */
1202 	fb = &newkernel.fi_files[FASTBOOT_SWTCH];
1203 	fb->fb_va = fb->fb_dest_pa = FASTBOOT_SWTCH_PA;
1204 	fb->fb_size = MMU_PAGESIZE;
1205 
1206 	hat_devload(kas.a_hat, (caddr_t)fb->fb_va,
1207 	    MMU_PAGESIZE, mmu_btop(fb->fb_dest_pa),
1208 	    PROT_READ | PROT_WRITE | PROT_EXEC,
1209 	    HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK);
1210 
1211 	/*
1212 	 * Build the new multiboot_info structure
1213 	 */
1214 	if (fastboot_build_mbi(fastboot_args, &newkernel) != 0) {
1215 		goto err_out;
1216 	}
1217 
1218 	/*
1219 	 * Build page table for low 1G physical memory. Use big pages.
1220 	 * Allocate 4 (5 for amd64) pages for the page tables.
1221 	 *    1 page for PML4 (amd64)
1222 	 *    1 page for Page-Directory-Pointer Table
1223 	 *    2 pages for Page Directory
1224 	 *    1 page for Page Table.
1225 	 * The page table entry will be rewritten to map the physical
1226 	 * address as we do the copying.
1227 	 */
1228 	if (newkernel.fi_has_pae) {
1229 #ifdef	__amd64
1230 		size_t size = MMU_PAGESIZE * 5;
1231 #else
1232 		size_t size = MMU_PAGESIZE * 4;
1233 #endif	/* __amd64 */
1234 
1235 		if (newkernel.fi_pagetable_size && newkernel.fi_pagetable_size
1236 		    < size) {
1237 			contig_free((void *)newkernel.fi_pagetable_va,
1238 			    newkernel.fi_pagetable_size);
1239 			newkernel.fi_pagetable_size = 0;
1240 		}
1241 
1242 		if (newkernel.fi_pagetable_size == 0) {
1243 			if ((newkernel.fi_pagetable_va = (uintptr_t)
1244 			    contig_alloc(size, &fastboot_below_1G_dma_attr,
1245 			    MMU_PAGESIZE, 0)) == NULL) {
1246 				cmn_err(CE_NOTE, fastboot_enomem_msg,
1247 				    (uint64_t)size, "1G");
1248 				goto err_out;
1249 			}
1250 			/*
1251 			 * fi_pagetable_size must be set after the allocation
1252 			 * succeeds as it's used to determine how much memory to
1253 			 * free.
1254 			 */
1255 			newkernel.fi_pagetable_size = size;
1256 		}
1257 
1258 		bzero((void *)(newkernel.fi_pagetable_va), size);
1259 
1260 		newkernel.fi_pagetable_pa =
1261 		    mmu_ptob((uint64_t)hat_getpfnum(kas.a_hat,
1262 		    (caddr_t)newkernel.fi_pagetable_va));
1263 
1264 		newkernel.fi_last_table_pa = newkernel.fi_pagetable_pa +
1265 		    size - MMU_PAGESIZE;
1266 
1267 		newkernel.fi_next_table_va = newkernel.fi_pagetable_va +
1268 		    MMU_PAGESIZE;
1269 		newkernel.fi_next_table_pa = newkernel.fi_pagetable_pa +
1270 		    MMU_PAGESIZE;
1271 
1272 		fastboot_build_pagetables(&newkernel);
1273 	}
1274 
1275 
1276 	/* Generate MD5 checksums */
1277 	fastboot_cksum_generate(&newkernel);
1278 
1279 	/* Mark it as valid */
1280 	newkernel.fi_valid = 1;
1281 	newkernel.fi_magic = FASTBOOT_MAGIC;
1282 
1283 	postbootkernelbase = saved_kernelbase;
1284 	return;
1285 
1286 err_out:
1287 	postbootkernelbase = saved_kernelbase;
1288 	newkernel.fi_valid = 0;
1289 	fastboot_free_newkernel(&newkernel);
1290 }
1291 
1292 
1293 /* ARGSUSED */
1294 static int
1295 fastboot_xc_func(fastboot_info_t *nk, xc_arg_t unused2, xc_arg_t unused3)
1296 {
1297 	void (*fastboot_func)(fastboot_info_t *);
1298 	fastboot_file_t	*fb = &nk->fi_files[FASTBOOT_SWTCH];
1299 	fastboot_func = (void (*)())(fb->fb_va);
1300 	kthread_t *t_intr = curthread->t_intr;
1301 
1302 	if (&kas != curproc->p_as) {
1303 		hat_devload(curproc->p_as->a_hat, (caddr_t)fb->fb_va,
1304 		    MMU_PAGESIZE, mmu_btop(fb->fb_dest_pa),
1305 		    PROT_READ | PROT_WRITE | PROT_EXEC,
1306 		    HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK);
1307 	}
1308 
1309 	/*
1310 	 * If we have pinned a thread, make sure the address is mapped
1311 	 * in the address space of the pinned thread.
1312 	 */
1313 	if (t_intr && t_intr->t_procp->p_as->a_hat != curproc->p_as->a_hat &&
1314 	    t_intr->t_procp->p_as != &kas)
1315 		hat_devload(t_intr->t_procp->p_as->a_hat, (caddr_t)fb->fb_va,
1316 		    MMU_PAGESIZE, mmu_btop(fb->fb_dest_pa),
1317 		    PROT_READ | PROT_WRITE | PROT_EXEC,
1318 		    HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK);
1319 
1320 	(*psm_shutdownf)(A_SHUTDOWN, AD_FASTREBOOT);
1321 	(*fastboot_func)(nk);
1322 
1323 	/*NOTREACHED*/
1324 	return (0);
1325 }
1326 
1327 /*
1328  * Jump to the fast reboot switcher.  This function never returns.
1329  */
1330 void
1331 fast_reboot()
1332 {
1333 	processorid_t bootcpuid = 0;
1334 	extern uintptr_t postbootkernelbase;
1335 	extern char	fb_swtch_image[];
1336 	fastboot_file_t	*fb;
1337 	int i;
1338 
1339 	postbootkernelbase = 0;
1340 
1341 	fb = &newkernel.fi_files[FASTBOOT_SWTCH];
1342 
1343 	/*
1344 	 * Map the address into both the current proc's address
1345 	 * space and the kernel's address space in case the panic
1346 	 * is forced by kmdb.
1347 	 */
1348 	if (&kas != curproc->p_as) {
1349 		hat_devload(curproc->p_as->a_hat, (caddr_t)fb->fb_va,
1350 		    MMU_PAGESIZE, mmu_btop(fb->fb_dest_pa),
1351 		    PROT_READ | PROT_WRITE | PROT_EXEC,
1352 		    HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK);
1353 	}
1354 
1355 	bcopy((void *)fb_swtch_image, (void *)fb->fb_va, fb->fb_size);
1356 
1357 
1358 	/*
1359 	 * Set fb_va to fake_va
1360 	 */
1361 	for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
1362 		newkernel.fi_files[i].fb_va = fake_va;
1363 
1364 	}
1365 
1366 	if (panicstr && CPU->cpu_id != bootcpuid &&
1367 	    CPU_ACTIVE(cpu_get(bootcpuid))) {
1368 		extern void panic_idle(void);
1369 		cpuset_t cpuset;
1370 
1371 		CPUSET_ZERO(cpuset);
1372 		CPUSET_ADD(cpuset, bootcpuid);
1373 		xc_priority((xc_arg_t)&newkernel, 0, 0, CPUSET2BV(cpuset),
1374 		    (xc_func_t)fastboot_xc_func);
1375 
1376 		panic_idle();
1377 	} else
1378 		(void) fastboot_xc_func(&newkernel, 0, 0);
1379 }
1380 
1381 
1382 /*
1383  * Get boot property value for fastreboot_onpanic.
1384  *
1385  * NOTE: If fastreboot_onpanic is set to non-zero in /etc/system,
1386  * new setting passed in via "-B fastreboot_onpanic" is ignored.
1387  * This order of precedence is to enable developers debugging panics
1388  * that occur early in boot to utilize Fast Reboot on panic.
1389  */
1390 static void
1391 fastboot_get_bootprop(void)
1392 {
1393 	int		val = 0xaa, len, ret;
1394 	dev_info_t	*devi;
1395 	char		*propstr = NULL;
1396 
1397 	devi = ddi_root_node();
1398 
1399 	ret = ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
1400 	    FASTREBOOT_ONPANIC, &propstr);
1401 
1402 	if (ret == DDI_PROP_SUCCESS) {
1403 		if (FASTREBOOT_ONPANIC_NOTSET(propstr))
1404 			val = 0;
1405 		else if (FASTREBOOT_ONPANIC_ISSET(propstr))
1406 			val = UA_FASTREBOOT_ONPANIC;
1407 
1408 		/*
1409 		 * Only set fastreboot_onpanic to the value passed in
1410 		 * if it's not already set to non-zero, and the value
1411 		 * has indeed been passed in via command line.
1412 		 */
1413 		if (!fastreboot_onpanic && val != 0xaa)
1414 			fastreboot_onpanic = val;
1415 		ddi_prop_free(propstr);
1416 	} else if (ret != DDI_PROP_NOT_FOUND && ret != DDI_PROP_UNDEFINED) {
1417 		cmn_err(CE_NOTE, "!%s value is invalid, will be ignored",
1418 		    FASTREBOOT_ONPANIC);
1419 	}
1420 
1421 	len = sizeof (fastreboot_onpanic_cmdline);
1422 	ret = ddi_getlongprop_buf(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
1423 	    FASTREBOOT_ONPANIC_CMDLINE, fastreboot_onpanic_cmdline, &len);
1424 
1425 	if (ret == DDI_PROP_BUF_TOO_SMALL)
1426 		cmn_err(CE_NOTE, "!%s value is too long, will be ignored",
1427 		    FASTREBOOT_ONPANIC_CMDLINE);
1428 }
1429 
1430 /*
1431  * This function is called by main() to either load the backup kernel for panic
1432  * fast reboot, or to reserve low physical memory for fast reboot.
1433  */
1434 void
1435 fastboot_post_startup()
1436 {
1437 	lbolt_at_boot = ddi_get_lbolt();
1438 
1439 	/* Default to 10 minutes */
1440 	if (fastreboot_onpanic_uptime == LONG_MAX)
1441 		fastreboot_onpanic_uptime = SEC_TO_TICK(10 * 60);
1442 
1443 	if (!fastreboot_capable)
1444 		return;
1445 
1446 	mutex_enter(&fastreboot_config_mutex);
1447 
1448 	fastboot_get_bootprop();
1449 
1450 	if (fastreboot_onpanic)
1451 		fastboot_load_kernel(fastreboot_onpanic_cmdline);
1452 	else if (reserve_mem_enabled)
1453 		fastboot_reserve_mem(&newkernel);
1454 
1455 	mutex_exit(&fastreboot_config_mutex);
1456 }
1457 
1458 /*
1459  * Update boot configuration settings.
1460  * If the new fastreboot_onpanic setting is false, and a kernel has
1461  * been preloaded, free the memory;
1462  * if the new fastreboot_onpanic setting is true and newkernel is
1463  * not valid, load the new kernel.
1464  */
1465 void
1466 fastboot_update_config(const char *mdep)
1467 {
1468 	uint8_t boot_config = (uint8_t)*mdep;
1469 	int cur_fastreboot_onpanic;
1470 
1471 	if (!fastreboot_capable)
1472 		return;
1473 
1474 	mutex_enter(&fastreboot_config_mutex);
1475 
1476 	cur_fastreboot_onpanic = fastreboot_onpanic;
1477 	fastreboot_onpanic = boot_config & UA_FASTREBOOT_ONPANIC;
1478 
1479 	if (fastreboot_onpanic && (!cur_fastreboot_onpanic ||
1480 	    !newkernel.fi_valid))
1481 		fastboot_load_kernel(fastreboot_onpanic_cmdline);
1482 	if (cur_fastreboot_onpanic && !fastreboot_onpanic)
1483 		fastboot_free_newkernel(&newkernel);
1484 
1485 	mutex_exit(&fastreboot_config_mutex);
1486 }
1487 
1488 /*
1489  * This is an internal interface to disable Fast Reboot on Panic.
1490  * It frees up memory allocated for the backup kernel and sets
1491  * fastreboot_onpanic to zero.
1492  */
1493 static void
1494 fastreboot_onpanic_disable(void)
1495 {
1496 	uint8_t boot_config = (uint8_t)(~UA_FASTREBOOT_ONPANIC);
1497 	fastboot_update_config((const char *)&boot_config);
1498 }
1499 
1500 /*
1501  * This is the interface to be called by fm_panic() in case FMA has diagnosed
1502  * a terminal machine check exception.  It does not free up memory allocated
1503  * for the backup kernel.  General disabling fastreboot_onpanic in a
1504  * non-panicking situation must go through fastboot_onpanic_disable().
1505  */
1506 void
1507 fastreboot_disable_highpil(void)
1508 {
1509 	fastreboot_onpanic = 0;
1510 }
1511 
1512 /*
1513  * This is an internal interface to disable Fast Reboot by Default.
1514  * It does not free up memory allocated for the backup kernel.
1515  */
1516 static void
1517 fastreboot_capable_disable(uint32_t msgid)
1518 {
1519 	if (fastreboot_capable != 0) {
1520 		fastreboot_capable = 0;
1521 		if (msgid < sizeof (fastreboot_nosup_desc) /
1522 		    sizeof (fastreboot_nosup_desc[0]))
1523 			fastreboot_nosup_id = msgid;
1524 		else
1525 			fastreboot_nosup_id = FBNS_DEFAULT;
1526 	}
1527 }
1528 
1529 /*
1530  * This is the kernel interface for disabling
1531  * Fast Reboot by Default and Fast Reboot on Panic.
1532  * Frees up memory allocated for the backup kernel.
1533  * General disabling of the Fast Reboot by Default feature should be done
1534  * via the userland interface scf_fastreboot_default_set_transient().
1535  */
1536 void
1537 fastreboot_disable(uint32_t msgid)
1538 {
1539 	fastreboot_capable_disable(msgid);
1540 	fastreboot_onpanic_disable();
1541 }
1542 
1543 /*
1544  * Returns Fast Reboot not support message for fastreboot_nosup_id.
1545  * If fastreboot_nosup_id contains invalid index, default
1546  * Fast Reboot not support message is returned.
1547  */
1548 const char *
1549 fastreboot_nosup_message(void)
1550 {
1551 	uint32_t msgid;
1552 
1553 	msgid = fastreboot_nosup_id;
1554 	if (msgid >= sizeof (fastreboot_nosup_desc) /
1555 	    sizeof (fastreboot_nosup_desc[0]))
1556 		msgid = FBNS_DEFAULT;
1557 
1558 	return (fastreboot_nosup_desc[msgid]);
1559 }
1560 
1561 /*
1562  * A simplified interface for uadmin to call to update the configuration
1563  * setting and load a new kernel if necessary.
1564  */
1565 void
1566 fastboot_update_and_load(int fcn, char *mdep)
1567 {
1568 	if (fcn != AD_FASTREBOOT) {
1569 		/*
1570 		 * If user has explicitly requested reboot to prom,
1571 		 * or uadmin(1M) was invoked with other functions,
1572 		 * don't try to fast reboot after dumping.
1573 		 */
1574 		fastreboot_onpanic_disable();
1575 	}
1576 
1577 	mutex_enter(&fastreboot_config_mutex);
1578 
1579 	if (fastreboot_onpanic)
1580 		fastboot_load_kernel(mdep);
1581 
1582 	mutex_exit(&fastreboot_config_mutex);
1583 }
1584