#include <sys/types.h>
#include <sys/param.h>
#include <sys/segments.h>
#include <sys/sysmacros.h>
#include <sys/vm.h>
#include <sys/proc.h>
#include <sys/buf.h>
#include <sys/kmem.h>
#include <sys/reboot.h>
#include <sys/uadmin.h>
#include <sys/cred.h>
#include <sys/vnode.h>
#include <sys/file.h>
#include <sys/cmn_err.h>
#include <sys/dumphdr.h>
#include <sys/bootconf.h>
#include <sys/ddidmareq.h>
#include <sys/varargs.h>
#include <sys/promif.h>
#include <sys/modctl.h>
#include <vm/hat.h>
#include <vm/as.h>
#include <vm/page.h>
#include <vm/seg.h>
#include <vm/hat_i86.h>
#include <sys/vm_machparam.h>
#include <sys/archsystm.h>
#include <sys/machsystm.h>
#include <sys/mman.h>
#include <sys/x86_archext.h>
#include <sys/smp_impldefs.h>
#include <sys/spl.h>
#include <sys/fastboot_impl.h>
#include <sys/machelf.h>
#include <sys/kobj.h>
#include <sys/multiboot.h>
#include <sys/kobj_lex.h>
#define FASTBOOT_PTE_LIST_SIZE(fsize) \
P2ROUNDUP((((fsize) >> PAGESHIFT) + 1) * sizeof (x86pte_t), PAGESIZE)
fastboot_info_t newkernel = { 0 };
char fastboot_args[OBP_MAXPATHLEN];
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};
static uint32_t fastreboot_nosup_id = FBNS_DEFAULT;
static const char * const fastreboot_nosup_desc[FBNS_END] = {
#define fastboot_nosup_msg(id, str) str,
#include <sys/fastboot_msg.h>
};
int fastboot_debug = 0;
int fastboot_contig = 0;
static uintptr_t fake_va = FASTBOOT_FAKE_VA;
int reserve_mem_enabled = 1;
kmutex_t fastreboot_config_mutex;
static size_t fastboot_mbi_size = 0x2000;
static size_t fastboot_pagetable_size = 0x5000;
clock_t fastreboot_onpanic_uptime = LONG_MAX;
clock_t lbolt_at_boot = LONG_MAX;
static ddi_dma_attr_t fastboot_below_1G_dma_attr = {
DMA_ATTR_V0,
0x0000000008000000ULL,
0x000000003FFFFFFFULL,
0x00000000FFFFFFFFULL,
0x0000000000001000ULL,
1,
1,
0x00000000FFFFFFFFULL,
0x00000000FFFFFFFFULL,
1,
0x1000ULL,
0,
};
static ddi_dma_attr_t fastboot_dma_attr = {
DMA_ATTR_V0,
0x0000000008000000ULL,
0xFFFFFFFFFFFFFFFFULL,
0x00000000FFFFFFFFULL,
0x0000000000001000ULL,
1,
1,
0x00000000FFFFFFFFULL,
0x00000000FFFFFFFFULL,
1,
0x1000ULL,
0,
};
extern multiboot_info_t saved_mbi;
extern mb_memory_map_t saved_mmap[FASTBOOT_SAVED_MMAP_COUNT];
extern uint8_t saved_drives[FASTBOOT_SAVED_DRIVES_SIZE];
extern char saved_cmdline[FASTBOOT_SAVED_CMDLINE_LEN];
extern int saved_cmdline_len;
extern size_t saved_file_size[];
extern void* contig_alloc(size_t size, ddi_dma_attr_t *attr,
uintptr_t align, int cansleep);
extern void contig_free(void *addr, size_t size);
extern void vprintf(const char *, va_list);
#define BOOTARCHIVE64 "/platform/i86pc/amd64/boot_archive"
#define BOOTARCHIVE32 "/platform/i86pc/boot_archive"
#define BOOTARCHIVE32_FAILSAFE "/boot/x86.miniroot-safe"
#define BOOTARCHIVE64_FAILSAFE "/boot/amd64/x86.miniroot-safe"
#define FAILSAFE_BOOTFILE32 "/boot/platform/i86pc/kernel/unix"
#define FAILSAFE_BOOTFILE64 "/boot/platform/i86pc/kernel/amd64/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 void fastboot_free_file(fastboot_file_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);
}
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));
}
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) {
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) {
table = (x86pte_t *)
((uintptr_t)(((paddr_t)table[index] & MMU_PAGEMASK)
- nk->fi_pagetable_pa) + nk->fi_pagetable_va);
} else {
if (l == nk->fi_top_level) {
ASSERT(nk->fi_top_level == 3);
table[index] = nk->fi_next_table_pa | ptp_bits;
} 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;
}
}
}
static void
fastboot_build_pagetables(fastboot_info_t *nk)
{
fastboot_map_with_size(nk, 0, 0, ONE_GIG, 1);
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);
}
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);
}
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;
const int max_sectcnt = *sectcntp;
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 {
if (max_sectcnt <= used_sections)
return (-1);
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;
if (sectp[used_sections].fb_sec_paddr +
sectp[used_sections].fb_sec_size +
sectp[used_sections].fb_sec_bss_size >=
DBOOT_ENTRY_ADDRESS)
return (-1);
used_sections++;
}
}
*sectcntp = used_sections;
return (0);
}
static int
fastboot_build_mbi(char *mdep, fastboot_info_t *nk)
{
mb_module_t *mbp;
multiboot_info_t *mbi;
uintptr_t start_addr_va;
uintptr_t start_addr_pa;
size_t offs = 0;
size_t arglen;
size_t size;
size_t mdnsz;
if (mdep != NULL && strlen(mdep) != 0) {
arglen = strlen(mdep) + 1;
} else {
arglen = saved_cmdline_len;
}
size = PAGESIZE + P2ROUNDUP(arglen, PAGESIZE);
if (nk->fi_mbi_size && nk->fi_mbi_size < size) {
contig_free((void *)nk->fi_new_mbi_va, nk->fi_mbi_size);
nk->fi_mbi_size = 0;
}
if (nk->fi_mbi_size == 0) {
if ((nk->fi_new_mbi_va =
(uintptr_t)contig_alloc(size, &fastboot_below_1G_dma_attr,
PAGESIZE, 0)) == 0) {
cmn_err(CE_NOTE, fastboot_enomem_msg,
(uint64_t)size, "1G");
return (-1);
}
nk->fi_mbi_size = size;
}
bzero((void *)nk->fi_new_mbi_va, nk->fi_mbi_size);
start_addr_va = nk->fi_new_mbi_va;
start_addr_pa = mmu_ptob((uint64_t)hat_getpfnum(kas.a_hat,
(caddr_t)start_addr_va));
nk->fi_new_mbi_pa = (paddr_t)start_addr_pa;
mbi = (multiboot_info_t *)start_addr_va;
bcopy(&saved_mbi, mbi, sizeof (*mbi));
offs += sizeof (multiboot_info_t);
mbi->mods_addr = start_addr_pa + offs;
mbp = (mb_module_t *)(start_addr_va + offs);
mbp->mod_start = nk->fi_files[FASTBOOT_BOOTARCHIVE].fb_dest_pa;
mbp->mod_end = nk->fi_files[FASTBOOT_BOOTARCHIVE].fb_next_pa;
offs += sizeof (mb_module_t);
mdnsz = strlen(fastboot_filename[FASTBOOT_NAME_BOOTARCHIVE]) + 1;
bcopy(fastboot_filename[FASTBOOT_NAME_BOOTARCHIVE],
(void *)(start_addr_va + offs), mdnsz);
mbp->mod_name = start_addr_pa + offs;
mbp->reserved = 0;
offs += mdnsz;
offs = P2ROUNDUP_TYPED(offs, 16, size_t);
mbi->mmap_addr = start_addr_pa + offs;
bcopy((void *)(uintptr_t)saved_mmap, (void *)(start_addr_va + offs),
saved_mbi.mmap_length);
offs += saved_mbi.mmap_length;
mbi->drives_addr = start_addr_pa + offs;
bcopy((void *)(uintptr_t)saved_drives, (void *)(start_addr_va + offs),
saved_mbi.drives_length);
offs += saved_mbi.drives_length;
mbi->cmdline = start_addr_pa + offs;
if (mdep != NULL && strlen(mdep) != 0) {
bcopy(mdep, (void *)(start_addr_va + offs), arglen);
} else {
bcopy((void *)saved_cmdline, (void *)(start_addr_va + offs),
arglen);
}
bzero(&mbi->config_table,
sizeof (*mbi) - offsetof(multiboot_info_t, config_table));
mbi->flags &= ~(MB_INFO_CONFIG_TABLE | MB_INFO_BOOT_LOADER_NAME |
MB_INFO_APM_TABLE | MB_INFO_VIDEO_INFO);
return (0);
}
static void
fastboot_init_fields(fastboot_info_t *nk)
{
if (is_x86_feature(x86_featureset, X86FSET_PAE)) {
nk->fi_has_pae = 1;
nk->fi_shift_amt = fastboot_shift_amt_pae;
nk->fi_ptes_per_table = 512;
nk->fi_lpagesize = (2 << 20);
nk->fi_top_level = 3;
}
}
static void
fastboot_parse_mdep(char *mdep, char *kern_bootpath, int *bootpath_len,
char *bootargs)
{
int i;
if (mdep != NULL && strlen(mdep) != 0) {
if (mdep[0] != '-') {
i = 0;
while (mdep[i] != '\0' && mdep[i] != ' ') {
i++;
}
if (i < 4 || strncmp(&mdep[i-4], "unix", 4) != 0) {
bcopy(mdep, kern_bootpath, i);
kern_bootpath[i] = '\0';
*bootpath_len = i;
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';
bcopy(mdep, bootargs, strlen(mdep));
} 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';
}
}
}
static void
fastboot_reserve_mem(fastboot_info_t *nk)
{
int i;
if (nk->fi_valid)
return;
for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
fastboot_file_t *fb = &nk->fi_files[i];
size_t fsize_roundup, size;
fsize_roundup = P2ROUNDUP_TYPED(saved_file_size[i],
PAGESIZE, size_t);
size = FASTBOOT_PTE_LIST_SIZE(fsize_roundup);
if ((fb->fb_pte_list_va = contig_alloc(size,
&fastboot_below_1G_dma_attr, PAGESIZE, 0)) == NULL) {
return;
}
fb->fb_pte_list_size = size;
}
if ((nk->fi_pagetable_va =
(uintptr_t)contig_alloc(fastboot_pagetable_size,
&fastboot_below_1G_dma_attr, PAGESIZE, 0)) == 0) {
return;
}
nk->fi_pagetable_size = fastboot_pagetable_size;
if ((nk->fi_new_mbi_va = (uintptr_t)contig_alloc(fastboot_mbi_size,
&fastboot_below_1G_dma_attr, PAGESIZE, 0)) == 0) {
return;
}
nk->fi_mbi_size = fastboot_mbi_size;
}
static void
fastboot_cksum_file(fastboot_file_t *fb, uchar_t *md5_hash)
{
MD5_CTX md5_ctx;
MD5Init(&md5_ctx);
MD5Update(&md5_ctx, (void *)fb->fb_va, fb->fb_size);
MD5Final(md5_hash, &md5_ctx);
}
static void
fastboot_free_file(fastboot_file_t *fb)
{
size_t fsize_roundup;
fsize_roundup = P2ROUNDUP_TYPED(fb->fb_size, PAGESIZE, size_t);
if (fsize_roundup) {
contig_free((void *)fb->fb_va, fsize_roundup);
fb->fb_va = 0;
fb->fb_size = 0;
}
}
static void
fastboot_free_file_pte(fastboot_file_t *fb, uint64_t endaddr)
{
if (fb->fb_pte_list_size && fb->fb_pte_list_pa < endaddr) {
contig_free((void *)fb->fb_pte_list_va, fb->fb_pte_list_size);
fb->fb_pte_list_va = 0;
fb->fb_pte_list_pa = 0;
fb->fb_pte_list_size = 0;
}
}
static void
fastboot_free_mem(fastboot_info_t *nk, uint64_t endaddr)
{
int i;
for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
fastboot_free_file(nk->fi_files + i);
fastboot_free_file_pte(nk->fi_files + i, endaddr);
}
if (nk->fi_pagetable_size && nk->fi_pagetable_pa < endaddr) {
contig_free((void *)nk->fi_pagetable_va, nk->fi_pagetable_size);
nk->fi_pagetable_va = 0;
nk->fi_pagetable_pa = 0;
nk->fi_pagetable_size = 0;
}
if (nk->fi_mbi_size && nk->fi_new_mbi_pa < endaddr) {
contig_free((void *)nk->fi_new_mbi_va, nk->fi_mbi_size);
nk->fi_new_mbi_va = 0;
nk->fi_new_mbi_pa = 0;
nk->fi_mbi_size = 0;
}
}
void
fastboot_free_newkernel(fastboot_info_t *nk)
{
int i;
nk->fi_valid = 0;
for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
fastboot_free_file(&(nk->fi_files[i]));
}
}
static void
fastboot_cksum_cdata(fastboot_info_t *nk, uchar_t *md5_hash)
{
int i;
MD5_CTX md5_ctx;
MD5Init(&md5_ctx);
for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
MD5Update(&md5_ctx, nk->fi_files[i].fb_pte_list_va,
nk->fi_files[i].fb_pte_list_size);
}
MD5Update(&md5_ctx, (void *)nk->fi_pagetable_va, nk->fi_pagetable_size);
MD5Update(&md5_ctx, (void *)nk->fi_new_mbi_va, nk->fi_mbi_size);
MD5Final(md5_hash, &md5_ctx);
}
static void
fastboot_cksum_generate(fastboot_info_t *nk)
{
int i;
for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
fastboot_cksum_file(nk->fi_files + i, nk->fi_md5_hash[i]);
}
fastboot_cksum_cdata(nk, nk->fi_md5_hash[i]);
}
int
fastboot_cksum_verify(fastboot_info_t *nk)
{
int i;
uchar_t md5_hash[MD5_DIGEST_LENGTH];
for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
fastboot_cksum_file(nk->fi_files + i, md5_hash);
if (bcmp(nk->fi_md5_hash[i], md5_hash,
sizeof (nk->fi_md5_hash[i])) != 0)
return (i + 1);
}
fastboot_cksum_cdata(nk, md5_hash);
if (bcmp(nk->fi_md5_hash[i], md5_hash,
sizeof (nk->fi_md5_hash[i])) != 0)
return (i + 1);
return (0);
}
void
fastboot_load_kernel(char *mdep)
{
void *buf = NULL;
int i;
fastboot_file_t *fb;
uint32_t dboot_start_offset;
char kern_bootpath[OBP_MAXPATHLEN];
extern uintptr_t postbootkernelbase;
uintptr_t saved_kernelbase;
int bootpath_len = 0;
int is_failsafe = 0;
int is_retry = 0;
uint64_t end_addr;
if (!fastreboot_capable)
return;
if (newkernel.fi_valid)
fastboot_free_newkernel(&newkernel);
saved_kernelbase = postbootkernelbase;
postbootkernelbase = 0;
fastboot_init_fields(&newkernel);
bzero(kern_bootpath, OBP_MAXPATHLEN);
bzero(fastboot_args, OBP_MAXPATHLEN);
fastboot_parse_mdep(mdep, kern_bootpath, &bootpath_len, fastboot_args);
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_BOOTFILE32,
(sizeof (FAILSAFE_BOOTFILE32) - 1)) == 0 ||
bcmp(kern_bootfile, FAILSAFE_BOOTFILE64,
(sizeof (FAILSAFE_BOOTFILE64) - 1)) == 0) {
is_failsafe = 1;
}
load_kernel_retry:
end_addr = DBOOT_ENTRY_ADDRESS;
for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
struct _buf *file;
uintptr_t va;
uint64_t fsize;
size_t fsize_roundup, pt_size;
int page_index;
uintptr_t offset;
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_NOTE, "!Fastboot: Couldn't open %s",
fastboot_filename[i]);
goto err_out;
}
if (kobj_get_filesize(file, &fsize) != 0) {
cmn_err(CE_NOTE,
"!Fastboot: Couldn't get filesize for %s",
fastboot_filename[i]);
goto err_out;
}
fsize_roundup = P2ROUNDUP_TYPED(fsize, PAGESIZE, size_t);
end_addr += fsize_roundup;
if (end_addr > fastboot_below_1G_dma_attr.dma_attr_addr_hi) {
cmn_err(CE_NOTE, "!Fastboot: boot archive is too big");
goto err_out;
}
if (end_addr > fastboot_dma_attr.dma_attr_addr_lo ||
end_addr > fastboot_below_1G_dma_attr.dma_attr_addr_lo) {
if (is_retry) {
cmn_err(CE_NOTE,
"!Fastboot: boot archive is too big");
goto err_out;
} else {
is_retry++;
fastboot_dma_attr.dma_attr_addr_lo = end_addr;
fastboot_below_1G_dma_attr.dma_attr_addr_lo =
end_addr;
fastboot_free_mem(&newkernel, end_addr);
goto load_kernel_retry;
}
}
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_NOTE, 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_NOTE, "!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;
pt_size = FASTBOOT_PTE_LIST_SIZE(fsize_roundup);
if (fb->fb_pte_list_size && fb->fb_pte_list_size < pt_size) {
contig_free((void *)fb->fb_pte_list_va,
fb->fb_pte_list_size);
fb->fb_pte_list_size = 0;
}
if (fb->fb_pte_list_size == 0) {
if ((fb->fb_pte_list_va =
(x86pte_t *)contig_alloc(pt_size,
&fastboot_below_1G_dma_attr, PAGESIZE, 0))
== NULL) {
cmn_err(CE_NOTE, fastboot_enomem_msg,
(uint64_t)pt_size, "1G");
goto err_out;
}
fb->fb_pte_list_size = pt_size;
}
bzero((void *)(fb->fb_pte_list_va), fb->fb_pte_list_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);
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 = (Ehdr *)va;
int j;
for (j = 0; j < SELFMAG; j++) {
if (ehdr->e_ident[j] != ELFMAG[j]) {
cmn_err(CE_NOTE, "!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) {
fb->fb_sectcnt = sizeof (fb->fb_sections) /
sizeof (fb->fb_sections[0]);
if (fastboot_elf32_find_loadables((void *)va,
fsize, &fb->fb_sections[0],
&fb->fb_sectcnt, &dboot_start_offset) < 0) {
cmn_err(CE_NOTE, "!Fastboot: ELF32 "
"program section failure");
goto err_out;
}
if (fb->fb_sectcnt == 0) {
cmn_err(CE_NOTE, "!Fastboot: No ELF32 "
"program sections found");
goto err_out;
}
if (is_failsafe) {
bcopy(BOOTARCHIVE32_FAILSAFE,
&fastboot_filename
[FASTBOOT_NAME_BOOTARCHIVE]
[bootpath_len],
sizeof (BOOTARCHIVE32_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_NOTE, "!Fastboot: Couldn't "
"find ELF64 dboot entry offset");
goto err_out;
}
if (!is_x86_feature(x86_featureset,
X86FSET_64) ||
!is_x86_feature(x86_featureset,
X86FSET_PAE)) {
cmn_err(CE_NOTE, "Fastboot: Cannot "
"reboot to %s: "
"not a 64-bit capable system",
kern_bootfile);
goto err_out;
}
if (is_failsafe) {
bcopy(BOOTARCHIVE64_FAILSAFE,
&fastboot_filename
[FASTBOOT_NAME_BOOTARCHIVE]
[bootpath_len],
sizeof (BOOTARCHIVE64_FAILSAFE));
} else {
bcopy(BOOTARCHIVE64,
&fastboot_filename
[FASTBOOT_NAME_BOOTARCHIVE]
[bootpath_len],
sizeof (BOOTARCHIVE64));
}
} else {
cmn_err(CE_NOTE, "!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;
}
kobj_close_file(file);
}
fb = &newkernel.fi_files[FASTBOOT_SWTCH];
fb->fb_va = fb->fb_dest_pa = FASTBOOT_SWTCH_PA;
fb->fb_size = MMU_PAGESIZE;
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 | HAT_LOAD_LOCK);
if (fastboot_build_mbi(fastboot_args, &newkernel) != 0) {
goto err_out;
}
if (newkernel.fi_has_pae) {
size_t size = MMU_PAGESIZE * 5;
if (newkernel.fi_pagetable_size && newkernel.fi_pagetable_size
< size) {
contig_free((void *)newkernel.fi_pagetable_va,
newkernel.fi_pagetable_size);
newkernel.fi_pagetable_size = 0;
}
if (newkernel.fi_pagetable_size == 0) {
if ((newkernel.fi_pagetable_va = (uintptr_t)
contig_alloc(size, &fastboot_below_1G_dma_attr,
MMU_PAGESIZE, 0)) == 0) {
cmn_err(CE_NOTE, fastboot_enomem_msg,
(uint64_t)size, "1G");
goto err_out;
}
newkernel.fi_pagetable_size = size;
}
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 +
size - MMU_PAGESIZE;
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);
}
fastboot_cksum_generate(&newkernel);
newkernel.fi_valid = 1;
newkernel.fi_magic = FASTBOOT_MAGIC;
postbootkernelbase = saved_kernelbase;
return;
err_out:
postbootkernelbase = saved_kernelbase;
newkernel.fi_valid = 0;
fastboot_free_newkernel(&newkernel);
}
static int
fastboot_xc_func(xc_arg_t arg1, xc_arg_t arg2 __unused, xc_arg_t arg3 __unused)
{
fastboot_info_t *nk = (fastboot_info_t *)arg1;
void (*fastboot_func)(fastboot_info_t *);
fastboot_file_t *fb = &nk->fi_files[FASTBOOT_SWTCH];
fastboot_func = (void (*)())(fb->fb_va);
kthread_t *t_intr = curthread->t_intr;
if (&kas != curproc->p_as) {
hat_devload(curproc->p_as->a_hat, (caddr_t)fb->fb_va,
MMU_PAGESIZE, mmu_btop(fb->fb_dest_pa),
PROT_READ | PROT_WRITE | PROT_EXEC,
HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK);
}
if (t_intr && t_intr->t_procp->p_as->a_hat != curproc->p_as->a_hat &&
t_intr->t_procp->p_as != &kas)
hat_devload(t_intr->t_procp->p_as->a_hat, (caddr_t)fb->fb_va,
MMU_PAGESIZE, mmu_btop(fb->fb_dest_pa),
PROT_READ | PROT_WRITE | PROT_EXEC,
HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK);
(*psm_shutdownf)(A_SHUTDOWN, AD_FASTREBOOT);
(*fastboot_func)(nk);
return (0);
}
void
fast_reboot()
{
processorid_t bootcpuid = 0;
extern uintptr_t postbootkernelbase;
extern char fb_swtch_image[];
fastboot_file_t *fb;
int i;
postbootkernelbase = 0;
fb = &newkernel.fi_files[FASTBOOT_SWTCH];
if (&kas != curproc->p_as) {
hat_devload(curproc->p_as->a_hat, (caddr_t)fb->fb_va,
MMU_PAGESIZE, mmu_btop(fb->fb_dest_pa),
PROT_READ | PROT_WRITE | PROT_EXEC,
HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK);
}
bcopy((void *)fb_swtch_image, (void *)fb->fb_va, fb->fb_size);
for (i = 0; i < FASTBOOT_MAX_FILES_MAP; i++) {
newkernel.fi_files[i].fb_va = fake_va;
}
if (panicstr && CPU->cpu_id != bootcpuid &&
CPU_ACTIVE(cpu_get(bootcpuid))) {
extern void panic_idle(void);
cpuset_t cpuset;
CPUSET_ZERO(cpuset);
CPUSET_ADD(cpuset, bootcpuid);
xc_priority((xc_arg_t)&newkernel, 0, 0, CPUSET2BV(cpuset),
fastboot_xc_func);
panic_idle();
} else
(void) fastboot_xc_func((xc_arg_t)&newkernel, 0, 0);
}
static void
fastboot_get_bootprop(void)
{
int val = 0xaa, len, ret;
dev_info_t *devi;
char *propstr = NULL;
devi = ddi_root_node();
ret = ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
FASTREBOOT_ONPANIC, &propstr);
if (ret == DDI_PROP_SUCCESS) {
if (FASTREBOOT_ONPANIC_NOTSET(propstr))
val = 0;
else if (FASTREBOOT_ONPANIC_ISSET(propstr))
val = UA_FASTREBOOT_ONPANIC;
if (!fastreboot_onpanic && val != 0xaa)
fastreboot_onpanic = val;
ddi_prop_free(propstr);
} else if (ret != DDI_PROP_NOT_FOUND && ret != DDI_PROP_UNDEFINED) {
cmn_err(CE_NOTE, "!%s value is invalid, will be ignored",
FASTREBOOT_ONPANIC);
}
len = sizeof (fastreboot_onpanic_cmdline);
ret = ddi_getlongprop_buf(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
FASTREBOOT_ONPANIC_CMDLINE, fastreboot_onpanic_cmdline, &len);
if (ret == DDI_PROP_BUF_TOO_SMALL)
cmn_err(CE_NOTE, "!%s value is too long, will be ignored",
FASTREBOOT_ONPANIC_CMDLINE);
}
void
fastboot_post_startup()
{
lbolt_at_boot = ddi_get_lbolt();
if (fastreboot_onpanic_uptime == LONG_MAX)
fastreboot_onpanic_uptime = SEC_TO_TICK(10 * 60);
if (!fastreboot_capable)
return;
mutex_enter(&fastreboot_config_mutex);
fastboot_get_bootprop();
if (fastreboot_onpanic)
fastboot_load_kernel(fastreboot_onpanic_cmdline);
else if (reserve_mem_enabled)
fastboot_reserve_mem(&newkernel);
mutex_exit(&fastreboot_config_mutex);
}
void
fastboot_update_config(const char *mdep)
{
uint8_t boot_config = (uint8_t)*mdep;
int cur_fastreboot_onpanic;
if (!fastreboot_capable)
return;
mutex_enter(&fastreboot_config_mutex);
cur_fastreboot_onpanic = fastreboot_onpanic;
fastreboot_onpanic = boot_config & UA_FASTREBOOT_ONPANIC;
if (fastreboot_onpanic && (!cur_fastreboot_onpanic ||
!newkernel.fi_valid))
fastboot_load_kernel(fastreboot_onpanic_cmdline);
if (cur_fastreboot_onpanic && !fastreboot_onpanic)
fastboot_free_newkernel(&newkernel);
mutex_exit(&fastreboot_config_mutex);
}
static void
fastreboot_onpanic_disable(void)
{
uint8_t boot_config = (uint8_t)(~UA_FASTREBOOT_ONPANIC);
fastboot_update_config((const char *)&boot_config);
}
void
fastreboot_disable_highpil(void)
{
fastreboot_onpanic = 0;
}
static void
fastreboot_capable_disable(uint32_t msgid)
{
if (fastreboot_capable != 0) {
fastreboot_capable = 0;
if (msgid < sizeof (fastreboot_nosup_desc) /
sizeof (fastreboot_nosup_desc[0]))
fastreboot_nosup_id = msgid;
else
fastreboot_nosup_id = FBNS_DEFAULT;
}
}
void
fastreboot_disable(uint32_t msgid)
{
fastreboot_capable_disable(msgid);
fastreboot_onpanic_disable();
}
const char *
fastreboot_nosup_message(void)
{
uint32_t msgid;
msgid = fastreboot_nosup_id;
if (msgid >= sizeof (fastreboot_nosup_desc) /
sizeof (fastreboot_nosup_desc[0]))
msgid = FBNS_DEFAULT;
return (fastreboot_nosup_desc[msgid]);
}
void
fastboot_update_and_load(int fcn, char *mdep)
{
if (fcn != AD_FASTREBOOT) {
fastreboot_onpanic_disable();
}
mutex_enter(&fastreboot_config_mutex);
if (fastreboot_onpanic)
fastboot_load_kernel(mdep);
mutex_exit(&fastreboot_config_mutex);
}