/* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License (the "License"). * You may not use this file except in compliance with the License. * * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at usr/src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright 2008 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include fastboot_info_t newkernel = { 0 }; static char fastboot_filename[2][OBP_MAXPATHLEN] = { { 0 }, { 0 }}; static x86pte_t ptp_bits = PT_VALID | PT_REF | PT_USER | PT_WRITABLE; static x86pte_t pte_bits = PT_VALID | PT_REF | PT_MOD | PT_NOCONSIST | PT_WRITABLE; static uint_t fastboot_shift_amt_pae[] = {12, 21, 30, 39}; int fastboot_debug = 0; int fastboot_contig = 0; /* * Fake starting va for new kernel and boot archive. */ static uintptr_t fake_va = FASTBOOT_FAKE_VA; /* * Below 1G for page tables as we are using 2G as the fake virtual address for * the new kernel and boot archive. */ static ddi_dma_attr_t fastboot_below_1G_dma_attr = { DMA_ATTR_V0, 0x0000000008000000ULL, /* dma_attr_addr_lo: 128MB */ 0x000000003FFFFFFFULL, /* dma_attr_addr_hi: 1G */ 0x00000000FFFFFFFFULL, /* dma_attr_count_max */ 0x0000000000001000ULL, /* dma_attr_align: 4KB */ 1, /* dma_attr_burstsize */ 1, /* dma_attr_minxfer */ 0x00000000FFFFFFFFULL, /* dma_attr_maxxfer */ 0x00000000FFFFFFFFULL, /* dma_attr_seg */ 1, /* dma_attr_sgllen */ 0x1000ULL, /* dma_attr_granular */ 0, /* dma_attr_flags */ }; static ddi_dma_attr_t fastboot_dma_attr = { DMA_ATTR_V0, 0x0000000008000000ULL, /* dma_attr_addr_lo: 128MB */ 0x0000000FFFFFFFFFULL, /* dma_attr_addr_hi: 64GB */ 0x00000000FFFFFFFFULL, /* dma_attr_count_max */ 0x0000000000001000ULL, /* dma_attr_align: 4KB */ 1, /* dma_attr_burstsize */ 1, /* dma_attr_minxfer */ 0x00000000FFFFFFFFULL, /* dma_attr_maxxfer */ 0x00000000FFFFFFFFULL, /* dma_attr_seg */ 1, /* dma_attr_sgllen */ 0x1000ULL, /* dma_attr_granular */ 0, /* dma_attr_flags */ }; /* * Various information saved from the previous boot to reconstruct * multiboot_info. */ extern multiboot_info_t saved_mbi; extern mb_memory_map_t saved_mmap[FASTBOOT_SAVED_MMAP_COUNT]; extern struct sol_netinfo saved_drives[FASTBOOT_SAVED_DRIVES_COUNT]; extern char saved_cmdline[FASTBOOT_SAVED_CMDLINE_LEN]; extern int saved_cmdline_len; extern void* contig_alloc(size_t size, ddi_dma_attr_t *attr, uintptr_t align, int cansleep); /* PRINTLIKE */ extern void vprintf(const char *, va_list); /* * Need to be able to get boot_archives from other places */ #define BOOTARCHIVE64 "/platform/i86pc/amd64/boot_archive" #define BOOTARCHIVE32 "/platform/i86pc/boot_archive" #define BOOTARCHIVE_FAILSAFE "/boot/x86.miniroot-safe" #define FAILSAFE_BOOTFILE "/boot/platform/i86pc/kernel/unix" static uint_t fastboot_vatoindex(fastboot_info_t *, uintptr_t, int); static void fastboot_map_with_size(fastboot_info_t *, uintptr_t, paddr_t, size_t, int); static void fastboot_build_pagetables(fastboot_info_t *); static int fastboot_build_mbi(char *, fastboot_info_t *); static const char fastboot_enomem_msg[] = "Fastboot: Couldn't allocate 0x%" PRIx64" bytes below %s to do fast reboot"; static void dprintf(char *fmt, ...) { va_list adx; if (!fastboot_debug) return; va_start(adx, fmt); vprintf(fmt, adx); va_end(adx); } /* * Return the index corresponding to a virt address at a given page table level. */ static uint_t fastboot_vatoindex(fastboot_info_t *nk, uintptr_t va, int level) { return ((va >> nk->fi_shift_amt[level]) & (nk->fi_ptes_per_table - 1)); } /* * Add mapping from vstart to pstart for the specified size. * Only handles 2 level. Must use 2M pages. vstart, pstart * and size should all have been aligned at 2M boundaries. */ static void fastboot_map_with_size(fastboot_info_t *nk, uintptr_t vstart, paddr_t pstart, size_t size, int level) { x86pte_t pteval, *table; uintptr_t vaddr; paddr_t paddr; int index, l; table = (x86pte_t *)(nk->fi_pagetable_va); for (l = nk->fi_top_level; l >= level; l--) { index = fastboot_vatoindex(nk, vstart, l); if (l == level) { /* * Last level. Program the page table entries. */ for (vaddr = vstart, paddr = pstart; vaddr < vstart + size; vaddr += (1ULL << nk->fi_shift_amt[l]), paddr += (1ULL << nk->fi_shift_amt[l])) { uint_t index = fastboot_vatoindex(nk, vaddr, l); if (l > 0) pteval = paddr | pte_bits | PT_PAGESIZE; else pteval = paddr | pte_bits; table[index] = pteval; } } else if (table[index] & PT_VALID) { if (l == level) break; table = (x86pte_t *) ((uintptr_t)(((paddr_t)table[index] & MMU_PAGEMASK) - nk->fi_pagetable_pa) + nk->fi_pagetable_va); } else { /* * Intermediate levels. Program with either valid * bit or PTP bits. */ if (l == nk->fi_top_level) { table[index] = nk->fi_next_table_pa | PT_VALID; } else { table[index] = nk->fi_next_table_pa | ptp_bits; } table = (x86pte_t *)(nk->fi_next_table_va); nk->fi_next_table_va += MMU_PAGESIZE; nk->fi_next_table_pa += MMU_PAGESIZE; } } } /* * Build page tables for the lower 1G of physical memory using 2M * pages, and prepare page tables for mapping new kernel and boot * archive pages using 4K pages. */ static void fastboot_build_pagetables(fastboot_info_t *nk) { /* * Map lower 1G physical memory. Use large pages. */ fastboot_map_with_size(nk, 0, 0, ONE_GIG, 1); /* * Map one 4K page to get the middle page tables set up. */ fake_va = P2ALIGN_TYPED(fake_va, nk->fi_lpagesize, uintptr_t); fastboot_map_with_size(nk, fake_va, nk->fi_files[0].fb_pte_list_va[0] & MMU_PAGEMASK, PAGESIZE, 0); } /* * Sanity check. Look for dboot offset. */ static int fastboot_elf64_find_dboot_load_offset(void *img, off_t imgsz, uint32_t *offp) { Elf64_Ehdr *ehdr = (Elf64_Ehdr *)img; Elf64_Phdr *phdr; uint8_t *phdrbase; int i; if ((ehdr->e_phoff + ehdr->e_phnum * ehdr->e_phentsize) >= imgsz) return (-1); phdrbase = (uint8_t *)img + ehdr->e_phoff; for (i = 0; i < ehdr->e_phnum; i++) { phdr = (Elf64_Phdr *)(phdrbase + ehdr->e_phentsize * i); if (phdr->p_type == PT_LOAD) { if (phdr->p_vaddr == phdr->p_paddr && phdr->p_vaddr == DBOOT_ENTRY_ADDRESS) { ASSERT(phdr->p_offset <= UINT32_MAX); *offp = (uint32_t)phdr->p_offset; return (0); } } } return (-1); } /* * Initialize text and data section information for 32-bit kernel. */ static int fastboot_elf32_find_loadables(void *img, off_t imgsz, fastboot_section_t *sectp, int *sectcntp, uint32_t *offp) { Elf32_Ehdr *ehdr = (Elf32_Ehdr *)img; Elf32_Phdr *phdr; uint8_t *phdrbase; int i; int used_sections = 0; if ((ehdr->e_phoff + ehdr->e_phnum * ehdr->e_phentsize) >= imgsz) return (-1); phdrbase = (uint8_t *)img + ehdr->e_phoff; for (i = 0; i < ehdr->e_phnum; i++) { phdr = (Elf32_Phdr *)(phdrbase + ehdr->e_phentsize * i); if (phdr->p_type == PT_INTERP) return (-1); if (phdr->p_type != PT_LOAD) continue; if (phdr->p_vaddr == phdr->p_paddr && phdr->p_paddr == DBOOT_ENTRY_ADDRESS) { *offp = (uint32_t)phdr->p_offset; } else { sectp[used_sections].fb_sec_offset = phdr->p_offset; sectp[used_sections].fb_sec_paddr = phdr->p_paddr; sectp[used_sections].fb_sec_size = phdr->p_filesz; sectp[used_sections].fb_sec_bss_size = (phdr->p_filesz < phdr->p_memsz) ? (phdr->p_memsz - phdr->p_filesz) : 0; used_sections++; } } *sectcntp = used_sections; return (0); } /* * Create multiboot info structure */ static int fastboot_build_mbi(char *mdep, fastboot_info_t *nk) { mb_module_t *mbp; uintptr_t next_addr; uintptr_t new_mbi_pa; size_t size; void *buf = NULL; size_t arglen; char bootargs[OBP_MAXPATHLEN]; bzero(bootargs, OBP_MAXPATHLEN); if (mdep != NULL) { arglen = strlen(mdep) + 1; } else { arglen = saved_cmdline_len; } size = PAGESIZE + P2ROUNDUP(arglen, PAGESIZE); buf = contig_alloc(size, &fastboot_below_1G_dma_attr, PAGESIZE, 0); if (buf == NULL) { cmn_err(CE_WARN, fastboot_enomem_msg, (uint64_t)size, "1G"); return (-1); } bzero(buf, size); new_mbi_pa = mmu_ptob((uint64_t)hat_getpfnum(kas.a_hat, (caddr_t)buf)); hat_devload(kas.a_hat, (caddr_t)new_mbi_pa, size, mmu_btop(new_mbi_pa), PROT_READ | PROT_WRITE, HAT_LOAD_NOCONSIST); nk->fi_new_mbi_pa = (paddr_t)new_mbi_pa; bcopy(&saved_mbi, (void *)new_mbi_pa, sizeof (multiboot_info_t)); next_addr = new_mbi_pa + sizeof (multiboot_info_t); ((multiboot_info_t *)new_mbi_pa)->mods_addr = next_addr; mbp = (mb_module_t *)(uintptr_t)next_addr; mbp->mod_start = newkernel.fi_files[FASTBOOT_BOOTARCHIVE].fb_dest_pa; mbp->mod_end = newkernel.fi_files[FASTBOOT_BOOTARCHIVE].fb_next_pa; next_addr += sizeof (mb_module_t); bcopy(fastboot_filename[FASTBOOT_NAME_BOOTARCHIVE], (void *)next_addr, strlen(fastboot_filename[FASTBOOT_NAME_BOOTARCHIVE])); mbp->mod_name = next_addr; mbp->reserved = 0; next_addr += strlen(fastboot_filename[FASTBOOT_NAME_BOOTARCHIVE]); *(char *)next_addr = '\0'; next_addr++; next_addr = P2ROUNDUP_TYPED(next_addr, 16, uintptr_t); ((multiboot_info_t *)new_mbi_pa)->mmap_addr = next_addr; bcopy((void *)(uintptr_t)saved_mmap, (void *)next_addr, saved_mbi.mmap_length); next_addr += saved_mbi.mmap_length; ((multiboot_info_t *)new_mbi_pa)->drives_addr = next_addr; bcopy((void *)(uintptr_t)saved_drives, (void *)next_addr, saved_mbi.drives_length); next_addr += saved_mbi.drives_length; ((multiboot_info_t *)new_mbi_pa)->cmdline = next_addr; if (mdep != NULL) { bcopy(mdep, (void *)(uintptr_t) (((multiboot_info_t *)new_mbi_pa)->cmdline), (arglen - 1)); } else { bcopy((void *)saved_cmdline, (void *)next_addr, (arglen - 1)); } /* Terminate the string */ ((char *)(intptr_t)next_addr)[arglen - 1] = '\0'; return (0); } void load_kernel(char *mdep) { struct _buf *file; void *buf = NULL; uintptr_t va; int i, j; fastboot_file_t *fb; uint32_t dboot_start_offset; Ehdr *ehdr; char kern_bootpath[OBP_MAXPATHLEN]; char bootargs[OBP_MAXPATHLEN]; extern uintptr_t postbootkernelbase; extern char fb_swtch_image[]; int bootpath_len = 0; int is_failsafe = 0; uintptr_t next_pa = 0; /* next available physical addr */ ASSERT(fastreboot_capable); postbootkernelbase = 0; if (x86_feature & X86_PAE) { newkernel.fi_has_pae = 1; newkernel.fi_shift_amt = fastboot_shift_amt_pae; newkernel.fi_ptes_per_table = 512; newkernel.fi_lpagesize = (2 << 20); /* 2M */ newkernel.fi_top_level = 2; } bzero(kern_bootpath, OBP_MAXPATHLEN); bzero(bootargs, OBP_MAXPATHLEN); /* * If mdep is not NULL, it comes in the format of * mountpoint unix args */ if (mdep != NULL) { if (mdep[0] != '-') { /* First get the root argument */ i = 0; while (mdep[i] != '\0' && mdep[i] != ' ') { i++; } if (i < 4 || strncmp(&mdep[i-4], "unix", 4) != 0) { /* mount point */ bcopy(mdep, kern_bootpath, i); kern_bootpath[i] = '\0'; bootpath_len = i; /* * Get the next argument. It should be unix as * we have validated in in halt.c. */ if (strlen(mdep) > i) { mdep += (i + 1); i = 0; while (mdep[i] != '\0' && mdep[i] != ' ') { i++; } } } bcopy(mdep, kern_bootfile, i); kern_bootfile[i] = '\0'; } else { int off = strlen(kern_bootfile); bcopy(kern_bootfile, bootargs, off); bcopy(" ", &bootargs[off++], 1); bcopy(mdep, &bootargs[off], strlen(mdep)); off += strlen(mdep); bootargs[off] = '\0'; mdep = bootargs; } } /* * Make sure we get the null character */ bcopy(kern_bootpath, fastboot_filename[FASTBOOT_NAME_UNIX], bootpath_len); bcopy(kern_bootfile, &fastboot_filename[FASTBOOT_NAME_UNIX][bootpath_len], strlen(kern_bootfile) + 1); bcopy(kern_bootpath, fastboot_filename[FASTBOOT_NAME_BOOTARCHIVE], bootpath_len); if (bcmp(kern_bootfile, FAILSAFE_BOOTFILE, (sizeof (FAILSAFE_BOOTFILE) - 1)) == 0) { is_failsafe = 1; } /* * Read in unix and boot_archive */ for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) { uint64_t fsize; size_t fsize_roundup, pt_size; int page_index; uintptr_t offset; int pt_entry_count; ddi_dma_attr_t dma_attr = fastboot_dma_attr; dprintf("fastboot_filename[%d] = %s\n", i, fastboot_filename[i]); if ((file = kobj_open_file(fastboot_filename[i])) == (struct _buf *)-1) { cmn_err(CE_WARN, "Fastboot: Couldn't open %s", fastboot_filename[i]); goto err_out; } if (kobj_get_filesize(file, &fsize) != 0) { cmn_err(CE_WARN, "Fastboot: Couldn't get filesize for %s", fastboot_filename[i]); goto err_out; } if (i == FASTBOOT_BOOTARCHIVE && is_failsafe) { /* Adjust low memory for failsafe mode */ fastboot_below_1G_dma_attr.dma_attr_addr_lo = dma_attr.dma_attr_addr_lo = P2ROUNDUP_TYPED(fsize, PAGESIZE, uint64_t) + next_pa; } if (!fastboot_contig) dma_attr.dma_attr_sgllen = (fsize / PAGESIZE) + (((fsize % PAGESIZE) == 0) ? 0 : 1); if ((buf = contig_alloc(fsize, &dma_attr, PAGESIZE, 0)) == NULL) { cmn_err(CE_WARN, fastboot_enomem_msg, fsize, "64G"); goto err_out; } va = P2ROUNDUP_TYPED((uintptr_t)buf, PAGESIZE, uintptr_t); if (kobj_read_file(file, (char *)va, fsize, 0) < 0) { cmn_err(CE_WARN, "Fastboot: Couldn't read %s", fastboot_filename[i]); goto err_out; } fb = &newkernel.fi_files[i]; fb->fb_va = va; fb->fb_size = fsize; fb->fb_sectcnt = 0; fsize_roundup = P2ROUNDUP_TYPED(fb->fb_size, PAGESIZE, size_t); /* * Allocate one extra page table entry for terminating * the list. */ pt_entry_count = (fsize_roundup >> PAGESHIFT) + 1; pt_size = P2ROUNDUP(pt_entry_count * 8, PAGESIZE); if ((fb->fb_pte_list_va = (x86pte_t *)contig_alloc(pt_size, &fastboot_below_1G_dma_attr, PAGESIZE, 0)) == NULL) { cmn_err(CE_WARN, fastboot_enomem_msg, (uint64_t)pt_size, "1G"); goto err_out; } bzero((void *)(fb->fb_pte_list_va), pt_size); fb->fb_pte_list_pa = mmu_ptob((uint64_t)hat_getpfnum(kas.a_hat, (caddr_t)fb->fb_pte_list_va)); for (page_index = 0, offset = 0; offset < fb->fb_size; offset += PAGESIZE) { uint64_t paddr; paddr = mmu_ptob((uint64_t)hat_getpfnum(kas.a_hat, (caddr_t)fb->fb_va + offset)); ASSERT(paddr >= fastboot_dma_attr.dma_attr_addr_lo); /* * Include the pte_bits so we don't have to make * it in assembly. */ fb->fb_pte_list_va[page_index++] = (x86pte_t) (paddr | pte_bits); } fb->fb_pte_list_va[page_index] = FASTBOOT_TERMINATE; if (i == FASTBOOT_UNIX) { ehdr = (Ehdr *)va; /* * Sanity checks: */ for (j = 0; j < SELFMAG; j++) { if (ehdr->e_ident[j] != ELFMAG[j]) { cmn_err(CE_WARN, "Fastboot: Bad ELF " "signature"); goto err_out; } } if (ehdr->e_ident[EI_CLASS] == ELFCLASS32 && ehdr->e_ident[EI_DATA] == ELFDATA2LSB && ehdr->e_machine == EM_386) { if (fastboot_elf32_find_loadables((void *)va, fsize, &fb->fb_sections[0], &fb->fb_sectcnt, &dboot_start_offset) < 0) { cmn_err(CE_WARN, "Fastboot: ELF32 " "program section failure"); goto err_out; } if (fb->fb_sectcnt == 0) { cmn_err(CE_WARN, "Fastboot: No ELF32 " "program sections found"); goto err_out; } if (is_failsafe) { /* Failsafe boot_archive */ bcopy(BOOTARCHIVE_FAILSAFE, &fastboot_filename [FASTBOOT_NAME_BOOTARCHIVE] [bootpath_len], sizeof (BOOTARCHIVE_FAILSAFE)); } else { bcopy(BOOTARCHIVE32, &fastboot_filename [FASTBOOT_NAME_BOOTARCHIVE] [bootpath_len], sizeof (BOOTARCHIVE32)); } } else if (ehdr->e_ident[EI_CLASS] == ELFCLASS64 && ehdr->e_ident[EI_DATA] == ELFDATA2LSB && ehdr->e_machine == EM_AMD64) { if (fastboot_elf64_find_dboot_load_offset( (void *)va, fsize, &dboot_start_offset) != 0) { cmn_err(CE_WARN, "Fastboot: Couldn't " "find ELF64 dboot entry offset"); goto err_out; } if ((x86_feature & X86_64) == 0 || newkernel.fi_has_pae == 0) { cmn_err(CE_WARN, "Fastboot: Cannot " "reboot to %s: " "not a 64-bit capable system", kern_bootfile); goto err_out; } bcopy(BOOTARCHIVE64, &fastboot_filename [FASTBOOT_NAME_BOOTARCHIVE][bootpath_len], sizeof (BOOTARCHIVE64)); } else { cmn_err(CE_WARN, "Fastboot: Unknown ELF type"); goto err_out; } fb->fb_dest_pa = DBOOT_ENTRY_ADDRESS - dboot_start_offset; fb->fb_next_pa = DBOOT_ENTRY_ADDRESS + fsize_roundup; } else { fb->fb_dest_pa = newkernel.fi_files[i - 1].fb_next_pa; fb->fb_next_pa = fb->fb_dest_pa + fsize_roundup; } next_pa = fb->fb_next_pa; kobj_close_file(file); /* * Set fb_va to fake_va */ fb->fb_va = fake_va; } /* * Add the function that will switch us to 32-bit protected mode */ fb = &newkernel.fi_files[FASTBOOT_SWTCH]; fb->fb_va = fb->fb_dest_pa = FASTBOOT_SWTCH_PA; fb->fb_size = PAGESIZE; /* * Map in FASTBOOT_SWTCH_PA */ hat_devload(kas.a_hat, (caddr_t)fb->fb_va, MMU_PAGESIZE, mmu_btop(fb->fb_dest_pa), PROT_READ | PROT_WRITE | PROT_EXEC, HAT_LOAD_NOCONSIST); bcopy((void *)fb_swtch_image, (void *)fb->fb_va, fb->fb_size); /* * Build the new multiboot_info structure */ if (fastboot_build_mbi(mdep, &newkernel) != 0) { goto err_out; } /* * Build page table for low 1G physical memory. Use big pages. * Allocate 4 pages for the page tables. * 1 page for Page-Directory-Pointer Table * 2 page for Page Directory * 1 page for Page Table. * The page table entry will be rewritten to map the physical * address as we do the copying. */ if (newkernel.fi_has_pae) { size_t size = MMU_PAGESIZE * 4; if ((newkernel.fi_pagetable_va = (uintptr_t) contig_alloc(size, &fastboot_below_1G_dma_attr, PAGESIZE, 0)) == NULL) { cmn_err(CE_WARN, fastboot_enomem_msg, (uint64_t)size, "1G"); goto err_out; } bzero((void *)(newkernel.fi_pagetable_va), size); newkernel.fi_pagetable_pa = mmu_ptob((uint64_t)hat_getpfnum(kas.a_hat, (caddr_t)newkernel.fi_pagetable_va)); newkernel.fi_last_table_pa = newkernel.fi_pagetable_pa + MMU_PAGESIZE * 3; newkernel.fi_next_table_va = newkernel.fi_pagetable_va + MMU_PAGESIZE; newkernel.fi_next_table_pa = newkernel.fi_pagetable_pa + MMU_PAGESIZE; fastboot_build_pagetables(&newkernel); } /* Mark it as valid */ newkernel.fi_valid = 1; newkernel.fi_magic = FASTBOOT_MAGIC; return; err_out: /* XXX Do we need to free up the memory we allocated? */ newkernel.fi_valid = 0; } void fast_reboot() { void (*fastboot_func)(fastboot_info_t *); fastboot_func = (void (*)())(newkernel.fi_files[FASTBOOT_SWTCH].fb_va); (*fastboot_func)(&newkernel); }