Changeset View
Changeset View
Standalone View
Standalone View
head/lib/libkvm/kvm_minidump_aarch64.c
Show All 27 Lines | |||||
#include <sys/cdefs.h> | #include <sys/cdefs.h> | ||||
__FBSDID("$FreeBSD$"); | __FBSDID("$FreeBSD$"); | ||||
/* | /* | ||||
* ARM64 (AArch64) machine dependent routines for kvm and minidumps. | * ARM64 (AArch64) machine dependent routines for kvm and minidumps. | ||||
*/ | */ | ||||
#include <sys/param.h> | #include <sys/param.h> | ||||
#include <sys/user.h> | #include <stdint.h> | ||||
#include <sys/proc.h> | |||||
#include <sys/stat.h> | |||||
#include <sys/mman.h> | |||||
#include <sys/fnv_hash.h> | |||||
#include <stdlib.h> | #include <stdlib.h> | ||||
#include <string.h> | #include <string.h> | ||||
#include <unistd.h> | #include <unistd.h> | ||||
#include <nlist.h> | |||||
#include <kvm.h> | #include <kvm.h> | ||||
#include <vm/vm.h> | #include "../../sys/arm64/include/minidump.h" | ||||
#include <vm/vm_param.h> | |||||
#include <machine/elf.h> | |||||
#include <machine/cpufunc.h> | |||||
#include <machine/pte.h> | |||||
#include <machine/minidump.h> | |||||
#include <limits.h> | #include <limits.h> | ||||
#include <stdint.h> | |||||
#include "kvm_private.h" | #include "kvm_private.h" | ||||
#include "kvm_aarch64.h" | |||||
struct hpte { | #define aarch64_round_page(x) roundup2((kvaddr_t)(x), AARCH64_PAGE_SIZE) | ||||
struct hpte *next; | |||||
vm_paddr_t pa; | |||||
int64_t off; | |||||
}; | |||||
#define HPT_SIZE 1024 | |||||
/* minidump must be the first item! */ | |||||
struct vmstate { | struct vmstate { | ||||
int minidump; /* 1 = minidump mode */ | |||||
struct minidumphdr hdr; | struct minidumphdr hdr; | ||||
void *hpt_head[HPT_SIZE]; | struct hpt hpt; | ||||
uint64_t *bitmap; | |||||
uint64_t *page_map; | uint64_t *page_map; | ||||
}; | }; | ||||
static void | |||||
hpt_insert(kvm_t *kd, vm_paddr_t pa, int64_t off) | |||||
{ | |||||
struct hpte *hpte; | |||||
uint32_t fnv = FNV1_32_INIT; | |||||
fnv = fnv_32_buf(&pa, sizeof(pa), fnv); | |||||
fnv &= (HPT_SIZE - 1); | |||||
hpte = malloc(sizeof(*hpte)); | |||||
hpte->pa = pa; | |||||
hpte->off = off; | |||||
hpte->next = kd->vmst->hpt_head[fnv]; | |||||
kd->vmst->hpt_head[fnv] = hpte; | |||||
} | |||||
static int64_t | |||||
hpt_find(kvm_t *kd, vm_paddr_t pa) | |||||
{ | |||||
struct hpte *hpte; | |||||
uint32_t fnv = FNV1_32_INIT; | |||||
fnv = fnv_32_buf(&pa, sizeof(pa), fnv); | |||||
fnv &= (HPT_SIZE - 1); | |||||
for (hpte = kd->vmst->hpt_head[fnv]; hpte != NULL; hpte = hpte->next) { | |||||
if (pa == hpte->pa) | |||||
return (hpte->off); | |||||
} | |||||
return (-1); | |||||
} | |||||
static int | static int | ||||
inithash(kvm_t *kd, uint64_t *base, int len, off_t off) | _aarch64_minidump_probe(kvm_t *kd) | ||||
{ | { | ||||
uint64_t idx; | |||||
uint64_t bit, bits; | |||||
vm_paddr_t pa; | |||||
for (idx = 0; idx < len / sizeof(*base); idx++) { | return (_kvm_probe_elf_kernel(kd, ELFCLASS64, EM_AARCH64) && | ||||
bits = base[idx]; | _kvm_is_minidump(kd)); | ||||
while (bits) { | |||||
bit = ffsl(bits) - 1; | |||||
bits &= ~(1ul << bit); | |||||
pa = (idx * sizeof(*base) * NBBY + bit) * PAGE_SIZE; | |||||
hpt_insert(kd, pa, off); | |||||
off += PAGE_SIZE; | |||||
} | } | ||||
} | |||||
return (off); | |||||
} | |||||
void | static void | ||||
_kvm_minidump_freevtop(kvm_t *kd) | _aarch64_minidump_freevtop(kvm_t *kd) | ||||
{ | { | ||||
struct vmstate *vm = kd->vmst; | struct vmstate *vm = kd->vmst; | ||||
free(vm->bitmap); | _kvm_hpt_free(&vm->hpt); | ||||
free(vm->page_map); | free(vm->page_map); | ||||
free(vm); | free(vm); | ||||
kd->vmst = NULL; | kd->vmst = NULL; | ||||
} | } | ||||
int | static int | ||||
_kvm_minidump_initvtop(kvm_t *kd) | _aarch64_minidump_initvtop(kvm_t *kd) | ||||
{ | { | ||||
struct vmstate *vmst; | struct vmstate *vmst; | ||||
uint64_t *bitmap; | |||||
off_t off; | off_t off; | ||||
vmst = _kvm_malloc(kd, sizeof(*vmst)); | vmst = _kvm_malloc(kd, sizeof(*vmst)); | ||||
if (vmst == 0) { | if (vmst == 0) { | ||||
_kvm_err(kd, kd->program, "cannot allocate vm"); | _kvm_err(kd, kd->program, "cannot allocate vm"); | ||||
return (-1); | return (-1); | ||||
} | } | ||||
kd->vmst = vmst; | kd->vmst = vmst; | ||||
vmst->minidump = 1; | |||||
if (pread(kd->pmfd, &vmst->hdr, sizeof(vmst->hdr), 0) != | if (pread(kd->pmfd, &vmst->hdr, sizeof(vmst->hdr), 0) != | ||||
sizeof(vmst->hdr)) { | sizeof(vmst->hdr)) { | ||||
_kvm_err(kd, kd->program, "cannot read dump header"); | _kvm_err(kd, kd->program, "cannot read dump header"); | ||||
return (-1); | return (-1); | ||||
} | } | ||||
if (strncmp(MINIDUMP_MAGIC, vmst->hdr.magic, | if (strncmp(MINIDUMP_MAGIC, vmst->hdr.magic, | ||||
sizeof(vmst->hdr.magic)) != 0) { | sizeof(vmst->hdr.magic)) != 0) { | ||||
_kvm_err(kd, kd->program, "not a minidump for this platform"); | _kvm_err(kd, kd->program, "not a minidump for this platform"); | ||||
return (-1); | return (-1); | ||||
} | } | ||||
if (vmst->hdr.version != MINIDUMP_VERSION && vmst->hdr.version != 1) { | vmst->hdr.version = le32toh(vmst->hdr.version); | ||||
if (vmst->hdr.version != MINIDUMP_VERSION) { | |||||
_kvm_err(kd, kd->program, "wrong minidump version. " | _kvm_err(kd, kd->program, "wrong minidump version. " | ||||
"Expected %d got %d", MINIDUMP_VERSION, vmst->hdr.version); | "Expected %d got %d", MINIDUMP_VERSION, vmst->hdr.version); | ||||
return (-1); | return (-1); | ||||
} | } | ||||
vmst->hdr.msgbufsize = le32toh(vmst->hdr.msgbufsize); | |||||
vmst->hdr.bitmapsize = le32toh(vmst->hdr.bitmapsize); | |||||
vmst->hdr.pmapsize = le32toh(vmst->hdr.pmapsize); | |||||
vmst->hdr.kernbase = le64toh(vmst->hdr.kernbase); | |||||
vmst->hdr.dmapphys = le64toh(vmst->hdr.dmapphys); | |||||
vmst->hdr.dmapbase = le64toh(vmst->hdr.dmapbase); | |||||
vmst->hdr.dmapend = le64toh(vmst->hdr.dmapend); | |||||
/* Skip header and msgbuf */ | /* Skip header and msgbuf */ | ||||
off = PAGE_SIZE + round_page(vmst->hdr.msgbufsize); | off = AARCH64_PAGE_SIZE + aarch64_round_page(vmst->hdr.msgbufsize); | ||||
vmst->bitmap = _kvm_malloc(kd, vmst->hdr.bitmapsize); | bitmap = _kvm_malloc(kd, vmst->hdr.bitmapsize); | ||||
if (vmst->bitmap == NULL) { | if (bitmap == NULL) { | ||||
_kvm_err(kd, kd->program, | _kvm_err(kd, kd->program, | ||||
"cannot allocate %d bytes for bitmap", | "cannot allocate %d bytes for bitmap", | ||||
vmst->hdr.bitmapsize); | vmst->hdr.bitmapsize); | ||||
return (-1); | return (-1); | ||||
} | } | ||||
if (pread(kd->pmfd, vmst->bitmap, vmst->hdr.bitmapsize, off) != | if (pread(kd->pmfd, bitmap, vmst->hdr.bitmapsize, off) != | ||||
vmst->hdr.bitmapsize) { | (ssize_t)vmst->hdr.bitmapsize) { | ||||
_kvm_err(kd, kd->program, | _kvm_err(kd, kd->program, | ||||
"cannot read %d bytes for page bitmap", | "cannot read %d bytes for page bitmap", | ||||
vmst->hdr.bitmapsize); | vmst->hdr.bitmapsize); | ||||
free(bitmap); | |||||
return (-1); | return (-1); | ||||
} | } | ||||
off += round_page(vmst->hdr.bitmapsize); | off += aarch64_round_page(vmst->hdr.bitmapsize); | ||||
vmst->page_map = _kvm_malloc(kd, vmst->hdr.pmapsize); | vmst->page_map = _kvm_malloc(kd, vmst->hdr.pmapsize); | ||||
if (vmst->page_map == NULL) { | if (vmst->page_map == NULL) { | ||||
_kvm_err(kd, kd->program, | _kvm_err(kd, kd->program, | ||||
"cannot allocate %d bytes for page_map", | "cannot allocate %d bytes for page_map", | ||||
vmst->hdr.pmapsize); | vmst->hdr.pmapsize); | ||||
free(bitmap); | |||||
return (-1); | return (-1); | ||||
} | } | ||||
/* This is the end of the dump, savecore may have truncated it. */ | /* This is the end of the dump, savecore may have truncated it. */ | ||||
/* | |||||
* XXX: This doesn't make sense. The pmap is not at the end, | |||||
* and if it is truncated we don't have any actual data (it's | |||||
* all stored after the bitmap and pmap. -- jhb | |||||
*/ | |||||
if (pread(kd->pmfd, vmst->page_map, vmst->hdr.pmapsize, off) < | if (pread(kd->pmfd, vmst->page_map, vmst->hdr.pmapsize, off) < | ||||
PAGE_SIZE) { | AARCH64_PAGE_SIZE) { | ||||
_kvm_err(kd, kd->program, "cannot read %d bytes for page_map", | _kvm_err(kd, kd->program, "cannot read %d bytes for page_map", | ||||
vmst->hdr.pmapsize); | vmst->hdr.pmapsize); | ||||
free(bitmap); | |||||
return (-1); | |||||
} | } | ||||
off += vmst->hdr.pmapsize; | off += vmst->hdr.pmapsize; | ||||
/* build physical address hash table for sparse pages */ | /* build physical address hash table for sparse pages */ | ||||
inithash(kd, vmst->bitmap, vmst->hdr.bitmapsize, off); | _kvm_hpt_init(kd, &vmst->hpt, bitmap, vmst->hdr.bitmapsize, off, | ||||
AARCH64_PAGE_SIZE, sizeof(*bitmap)); | |||||
free(bitmap); | |||||
return (0); | return (0); | ||||
} | } | ||||
static int | static int | ||||
_kvm_minidump_vatop(kvm_t *kd, u_long va, off_t *pa) | _aarch64_minidump_vatop(kvm_t *kd, kvaddr_t va, off_t *pa) | ||||
{ | { | ||||
struct vmstate *vm; | struct vmstate *vm; | ||||
u_long offset; | aarch64_physaddr_t offset; | ||||
pt_entry_t l3; | aarch64_pte_t l3; | ||||
u_long l3_index; | kvaddr_t l3_index; | ||||
u_long a; | aarch64_physaddr_t a; | ||||
off_t ofs; | off_t ofs; | ||||
vm = kd->vmst; | vm = kd->vmst; | ||||
offset = va & PAGE_MASK; | offset = va & AARCH64_PAGE_MASK; | ||||
if (va >= vm->hdr.dmapbase && va < vm->hdr.dmapend) { | if (va >= vm->hdr.dmapbase && va < vm->hdr.dmapend) { | ||||
a = (va - vm->hdr.dmapbase + vm->hdr.dmapphys) & ~PAGE_MASK; | a = (va - vm->hdr.dmapbase + vm->hdr.dmapphys) & | ||||
ofs = hpt_find(kd, a); | ~AARCH64_PAGE_MASK; | ||||
ofs = _kvm_hpt_find(&vm->hpt, a); | |||||
if (ofs == -1) { | if (ofs == -1) { | ||||
_kvm_err(kd, kd->program, "_kvm_vatop: " | _kvm_err(kd, kd->program, "_aarch64_minidump_vatop: " | ||||
"direct map address 0x%lx not in minidump", va); | "direct map address 0x%jx not in minidump", | ||||
(uintmax_t)va); | |||||
goto invalid; | goto invalid; | ||||
} | } | ||||
*pa = ofs + offset; | *pa = ofs + offset; | ||||
return (PAGE_SIZE - offset); | return (AARCH64_PAGE_SIZE - offset); | ||||
} else if (va >= vm->hdr.kernbase) { | } else if (va >= vm->hdr.kernbase) { | ||||
l3_index = (va - vm->hdr.kernbase) >> L3_SHIFT; | l3_index = (va - vm->hdr.kernbase) >> AARCH64_L3_SHIFT; | ||||
if (l3_index >= vm->hdr.pmapsize / sizeof(*vm->page_map)) | if (l3_index >= vm->hdr.pmapsize / sizeof(*vm->page_map)) | ||||
goto invalid; | goto invalid; | ||||
l3 = vm->page_map[l3_index]; | l3 = le64toh(vm->page_map[l3_index]); | ||||
if ((l3 & ATTR_DESCR_MASK) != L3_PAGE) { | if ((l3 & AARCH64_ATTR_DESCR_MASK) != AARCH64_L3_PAGE) { | ||||
_kvm_err(kd, kd->program, "_kvm_vatop: pde not valid"); | _kvm_err(kd, kd->program, | ||||
"_aarch64_minidump_vatop: pde not valid"); | |||||
goto invalid; | goto invalid; | ||||
} | } | ||||
a = l3 & ~ATTR_MASK; | a = l3 & ~AARCH64_ATTR_MASK; | ||||
ofs = hpt_find(kd, a); | ofs = _kvm_hpt_find(&vm->hpt, a); | ||||
if (ofs == -1) { | if (ofs == -1) { | ||||
_kvm_err(kd, kd->program, "_kvm_vatop: " | _kvm_err(kd, kd->program, "_aarch64_minidump_vatop: " | ||||
"physical address 0x%lx not in minidump", a); | "physical address 0x%jx not in minidump", | ||||
(uintmax_t)a); | |||||
goto invalid; | goto invalid; | ||||
} | } | ||||
*pa = ofs + offset; | *pa = ofs + offset; | ||||
return (PAGE_SIZE - offset); | return (AARCH64_PAGE_SIZE - offset); | ||||
} else { | } else { | ||||
_kvm_err(kd, kd->program, | _kvm_err(kd, kd->program, | ||||
"_kvm_vatop: virtual address 0x%lx not minidumped", va); | "_aarch64_minidump_vatop: virtual address 0x%jx not minidumped", | ||||
(uintmax_t)va); | |||||
goto invalid; | goto invalid; | ||||
} | } | ||||
invalid: | invalid: | ||||
_kvm_err(kd, 0, "invalid address (0x%lx)", va); | _kvm_err(kd, 0, "invalid address (0x%jx)", (uintmax_t)va); | ||||
return (0); | return (0); | ||||
} | } | ||||
int | static int | ||||
_kvm_minidump_kvatop(kvm_t *kd, u_long va, off_t *pa) | _aarch64_minidump_kvatop(kvm_t *kd, kvaddr_t va, off_t *pa) | ||||
{ | { | ||||
if (ISALIVE(kd)) { | if (ISALIVE(kd)) { | ||||
_kvm_err(kd, 0, "kvm_kvatop called in live kernel!"); | _kvm_err(kd, 0, | ||||
"_aarch64_minidump_kvatop called in live kernel!"); | |||||
return (0); | return (0); | ||||
} | } | ||||
return (_kvm_minidump_vatop(kd, va, pa)); | return (_aarch64_minidump_vatop(kd, va, pa)); | ||||
} | } | ||||
static int | |||||
_aarch64_native(kvm_t *kd) | |||||
{ | |||||
#ifdef __aarch64__ | |||||
return (1); | |||||
#else | |||||
return (0); | |||||
#endif | |||||
} | |||||
struct kvm_arch kvm_aarch64_minidump = { | |||||
.ka_probe = _aarch64_minidump_probe, | |||||
.ka_initvtop = _aarch64_minidump_initvtop, | |||||
.ka_freevtop = _aarch64_minidump_freevtop, | |||||
.ka_kvatop = _aarch64_minidump_kvatop, | |||||
.ka_native = _aarch64_native, | |||||
}; | |||||
KVM_ARCH(kvm_aarch64_minidump); |