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