2015-08-20 11:07:51 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2006 Peter Wemm
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* From: FreeBSD: src/lib/libkvm/kvm_minidump_amd64.c r261799
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
/*
|
2015-11-26 19:42:10 +00:00
|
|
|
* ARM64 (AArch64) machine dependent routines for kvm and minidumps.
|
2015-08-20 11:07:51 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
2015-11-27 18:58:26 +00:00
|
|
|
#include <stdint.h>
|
2015-08-20 11:07:51 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2017-11-12 01:36:48 +00:00
|
|
|
#include <vm/vm.h>
|
2015-08-20 11:07:51 +00:00
|
|
|
#include <kvm.h>
|
|
|
|
|
2015-11-27 18:58:26 +00:00
|
|
|
#include "../../sys/arm64/include/minidump.h"
|
2015-08-20 11:07:51 +00:00
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
#include "kvm_private.h"
|
2015-11-27 18:58:26 +00:00
|
|
|
#include "kvm_aarch64.h"
|
2015-08-20 11:07:51 +00:00
|
|
|
|
2015-11-27 18:58:26 +00:00
|
|
|
#define aarch64_round_page(x) roundup2((kvaddr_t)(x), AARCH64_PAGE_SIZE)
|
2015-08-20 11:07:51 +00:00
|
|
|
|
|
|
|
struct vmstate {
|
|
|
|
struct minidumphdr hdr;
|
|
|
|
};
|
|
|
|
|
2017-11-11 23:30:58 +00:00
|
|
|
static aarch64_pte_t
|
|
|
|
_aarch64_pte_get(kvm_t *kd, u_long pteindex)
|
|
|
|
{
|
|
|
|
aarch64_pte_t *pte = _kvm_pmap_get(kd, pteindex, sizeof(*pte));
|
|
|
|
|
|
|
|
return le64toh(*pte);
|
|
|
|
}
|
|
|
|
|
2015-08-20 11:07:51 +00:00
|
|
|
static int
|
2015-11-27 18:58:26 +00:00
|
|
|
_aarch64_minidump_probe(kvm_t *kd)
|
2015-08-20 11:07:51 +00:00
|
|
|
{
|
|
|
|
|
2015-11-27 18:58:26 +00:00
|
|
|
return (_kvm_probe_elf_kernel(kd, ELFCLASS64, EM_AARCH64) &&
|
|
|
|
_kvm_is_minidump(kd));
|
2015-08-20 11:07:51 +00:00
|
|
|
}
|
|
|
|
|
2015-11-27 18:58:26 +00:00
|
|
|
static void
|
|
|
|
_aarch64_minidump_freevtop(kvm_t *kd)
|
2015-08-20 11:07:51 +00:00
|
|
|
{
|
|
|
|
struct vmstate *vm = kd->vmst;
|
|
|
|
|
|
|
|
free(vm);
|
|
|
|
kd->vmst = NULL;
|
|
|
|
}
|
|
|
|
|
2015-11-27 18:58:26 +00:00
|
|
|
static int
|
|
|
|
_aarch64_minidump_initvtop(kvm_t *kd)
|
2015-08-20 11:07:51 +00:00
|
|
|
{
|
|
|
|
struct vmstate *vmst;
|
2016-07-18 01:55:25 +00:00
|
|
|
off_t off, sparse_off;
|
2015-08-20 11:07:51 +00:00
|
|
|
|
|
|
|
vmst = _kvm_malloc(kd, sizeof(*vmst));
|
2016-04-22 18:05:34 +00:00
|
|
|
if (vmst == NULL) {
|
2015-08-20 11:07:51 +00:00
|
|
|
_kvm_err(kd, kd->program, "cannot allocate vm");
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
kd->vmst = vmst;
|
|
|
|
if (pread(kd->pmfd, &vmst->hdr, sizeof(vmst->hdr), 0) !=
|
|
|
|
sizeof(vmst->hdr)) {
|
|
|
|
_kvm_err(kd, kd->program, "cannot read dump header");
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
if (strncmp(MINIDUMP_MAGIC, vmst->hdr.magic,
|
|
|
|
sizeof(vmst->hdr.magic)) != 0) {
|
|
|
|
_kvm_err(kd, kd->program, "not a minidump for this platform");
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2015-11-27 18:58:26 +00:00
|
|
|
vmst->hdr.version = le32toh(vmst->hdr.version);
|
|
|
|
if (vmst->hdr.version != MINIDUMP_VERSION) {
|
2015-08-20 11:07:51 +00:00
|
|
|
_kvm_err(kd, kd->program, "wrong minidump version. "
|
|
|
|
"Expected %d got %d", MINIDUMP_VERSION, vmst->hdr.version);
|
|
|
|
return (-1);
|
|
|
|
}
|
2015-11-27 18:58:26 +00:00
|
|
|
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);
|
2015-08-20 11:07:51 +00:00
|
|
|
|
|
|
|
/* Skip header and msgbuf */
|
2015-11-27 18:58:26 +00:00
|
|
|
off = AARCH64_PAGE_SIZE + aarch64_round_page(vmst->hdr.msgbufsize);
|
2015-08-20 11:07:51 +00:00
|
|
|
|
2016-07-18 01:55:25 +00:00
|
|
|
/* build physical address lookup table for sparse pages */
|
|
|
|
sparse_off = off + aarch64_round_page(vmst->hdr.bitmapsize) +
|
|
|
|
aarch64_round_page(vmst->hdr.pmapsize);
|
|
|
|
if (_kvm_pt_init(kd, vmst->hdr.bitmapsize, off, sparse_off,
|
|
|
|
AARCH64_PAGE_SIZE, sizeof(uint64_t)) == -1) {
|
2015-08-20 11:07:51 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
2015-11-27 18:58:26 +00:00
|
|
|
off += aarch64_round_page(vmst->hdr.bitmapsize);
|
2015-08-20 11:07:51 +00:00
|
|
|
|
2017-11-11 23:30:58 +00:00
|
|
|
if (_kvm_pmap_init(kd, vmst->hdr.pmapsize, off) == -1) {
|
2015-11-27 18:58:26 +00:00
|
|
|
return (-1);
|
2015-08-20 11:07:51 +00:00
|
|
|
}
|
2016-07-18 01:55:25 +00:00
|
|
|
off += aarch64_round_page(vmst->hdr.pmapsize);
|
2015-08-20 11:07:51 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-11-27 18:58:26 +00:00
|
|
|
_aarch64_minidump_vatop(kvm_t *kd, kvaddr_t va, off_t *pa)
|
2015-08-20 11:07:51 +00:00
|
|
|
{
|
|
|
|
struct vmstate *vm;
|
2015-11-27 18:58:26 +00:00
|
|
|
aarch64_physaddr_t offset;
|
|
|
|
aarch64_pte_t l3;
|
|
|
|
kvaddr_t l3_index;
|
|
|
|
aarch64_physaddr_t a;
|
2015-08-20 11:07:51 +00:00
|
|
|
off_t ofs;
|
|
|
|
|
|
|
|
vm = kd->vmst;
|
2015-11-27 18:58:26 +00:00
|
|
|
offset = va & AARCH64_PAGE_MASK;
|
2015-08-20 11:07:51 +00:00
|
|
|
|
|
|
|
if (va >= vm->hdr.dmapbase && va < vm->hdr.dmapend) {
|
2015-11-27 18:58:26 +00:00
|
|
|
a = (va - vm->hdr.dmapbase + vm->hdr.dmapphys) &
|
|
|
|
~AARCH64_PAGE_MASK;
|
2017-11-11 23:30:58 +00:00
|
|
|
ofs = _kvm_pt_find(kd, a, AARCH64_PAGE_SIZE);
|
2015-08-20 11:07:51 +00:00
|
|
|
if (ofs == -1) {
|
2015-11-27 18:58:26 +00:00
|
|
|
_kvm_err(kd, kd->program, "_aarch64_minidump_vatop: "
|
|
|
|
"direct map address 0x%jx not in minidump",
|
|
|
|
(uintmax_t)va);
|
2015-08-20 11:07:51 +00:00
|
|
|
goto invalid;
|
|
|
|
}
|
|
|
|
*pa = ofs + offset;
|
2015-11-27 18:58:26 +00:00
|
|
|
return (AARCH64_PAGE_SIZE - offset);
|
2015-08-20 11:07:51 +00:00
|
|
|
} else if (va >= vm->hdr.kernbase) {
|
2015-11-27 18:58:26 +00:00
|
|
|
l3_index = (va - vm->hdr.kernbase) >> AARCH64_L3_SHIFT;
|
2017-11-11 23:30:58 +00:00
|
|
|
if (l3_index >= vm->hdr.pmapsize / sizeof(l3))
|
2015-08-20 11:07:51 +00:00
|
|
|
goto invalid;
|
2017-11-11 23:30:58 +00:00
|
|
|
l3 = _aarch64_pte_get(kd, l3_index);
|
2015-11-27 18:58:26 +00:00
|
|
|
if ((l3 & AARCH64_ATTR_DESCR_MASK) != AARCH64_L3_PAGE) {
|
|
|
|
_kvm_err(kd, kd->program,
|
|
|
|
"_aarch64_minidump_vatop: pde not valid");
|
2015-08-20 11:07:51 +00:00
|
|
|
goto invalid;
|
|
|
|
}
|
2015-11-27 18:58:26 +00:00
|
|
|
a = l3 & ~AARCH64_ATTR_MASK;
|
2017-11-11 23:30:58 +00:00
|
|
|
ofs = _kvm_pt_find(kd, a, AARCH64_PAGE_SIZE);
|
2015-08-20 11:07:51 +00:00
|
|
|
if (ofs == -1) {
|
2015-11-27 18:58:26 +00:00
|
|
|
_kvm_err(kd, kd->program, "_aarch64_minidump_vatop: "
|
|
|
|
"physical address 0x%jx not in minidump",
|
|
|
|
(uintmax_t)a);
|
2015-08-20 11:07:51 +00:00
|
|
|
goto invalid;
|
|
|
|
}
|
|
|
|
*pa = ofs + offset;
|
2015-11-27 18:58:26 +00:00
|
|
|
return (AARCH64_PAGE_SIZE - offset);
|
2015-08-20 11:07:51 +00:00
|
|
|
} else {
|
|
|
|
_kvm_err(kd, kd->program,
|
2015-11-27 18:58:26 +00:00
|
|
|
"_aarch64_minidump_vatop: virtual address 0x%jx not minidumped",
|
|
|
|
(uintmax_t)va);
|
2015-08-20 11:07:51 +00:00
|
|
|
goto invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
invalid:
|
2015-11-27 18:58:26 +00:00
|
|
|
_kvm_err(kd, 0, "invalid address (0x%jx)", (uintmax_t)va);
|
2015-08-20 11:07:51 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2015-11-27 18:58:26 +00:00
|
|
|
static int
|
|
|
|
_aarch64_minidump_kvatop(kvm_t *kd, kvaddr_t va, off_t *pa)
|
2015-08-20 11:07:51 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
if (ISALIVE(kd)) {
|
2015-11-27 18:58:26 +00:00
|
|
|
_kvm_err(kd, 0,
|
|
|
|
"_aarch64_minidump_kvatop called in live kernel!");
|
2015-08-20 11:07:51 +00:00
|
|
|
return (0);
|
|
|
|
}
|
2015-11-27 18:58:26 +00:00
|
|
|
return (_aarch64_minidump_vatop(kd, va, pa));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2017-03-22 02:42:00 +00:00
|
|
|
_aarch64_native(kvm_t *kd __unused)
|
2015-11-27 18:58:26 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
#ifdef __aarch64__
|
|
|
|
return (1);
|
|
|
|
#else
|
|
|
|
return (0);
|
|
|
|
#endif
|
2015-08-20 11:07:51 +00:00
|
|
|
}
|
2015-11-27 18:58:26 +00:00
|
|
|
|
2017-11-11 23:30:58 +00:00
|
|
|
static vm_prot_t
|
|
|
|
_aarch64_entry_to_prot(aarch64_pte_t pte)
|
|
|
|
{
|
|
|
|
vm_prot_t prot = VM_PROT_READ;
|
|
|
|
|
|
|
|
/* Source: arm64/arm64/pmap.c:pmap_protect() */
|
|
|
|
if ((pte & AARCH64_ATTR_AP(AARCH64_ATTR_AP_RO)) == 0)
|
|
|
|
prot |= VM_PROT_WRITE;
|
|
|
|
if ((pte & AARCH64_ATTR_XN) == 0)
|
|
|
|
prot |= VM_PROT_EXECUTE;
|
|
|
|
return prot;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
_aarch64_minidump_walk_pages(kvm_t *kd, kvm_walk_pages_cb_t *cb, void *arg)
|
|
|
|
{
|
|
|
|
struct vmstate *vm = kd->vmst;
|
|
|
|
u_long nptes = vm->hdr.pmapsize / sizeof(aarch64_pte_t);
|
|
|
|
u_long bmindex, dva, pa, pteindex, va;
|
|
|
|
struct kvm_bitmap bm;
|
|
|
|
vm_prot_t prot;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (!_kvm_bitmap_init(&bm, vm->hdr.bitmapsize, &bmindex))
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
for (pteindex = 0; pteindex < nptes; pteindex++) {
|
|
|
|
aarch64_pte_t pte = _aarch64_pte_get(kd, pteindex);
|
|
|
|
|
|
|
|
if ((pte & AARCH64_ATTR_DESCR_MASK) != AARCH64_L3_PAGE)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
va = vm->hdr.kernbase + (pteindex << AARCH64_L3_SHIFT);
|
|
|
|
pa = pte & ~AARCH64_ATTR_MASK;
|
|
|
|
dva = vm->hdr.dmapbase + pa;
|
|
|
|
if (!_kvm_visit_cb(kd, cb, arg, pa, va, dva,
|
|
|
|
_aarch64_entry_to_prot(pte), AARCH64_PAGE_SIZE, 0)) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (_kvm_bitmap_next(&bm, &bmindex)) {
|
|
|
|
pa = bmindex * AARCH64_PAGE_SIZE;
|
|
|
|
dva = vm->hdr.dmapbase + pa;
|
|
|
|
if (vm->hdr.dmapend < (dva + AARCH64_PAGE_SIZE))
|
|
|
|
break;
|
|
|
|
va = 0;
|
|
|
|
prot = VM_PROT_READ | VM_PROT_WRITE;
|
|
|
|
if (!_kvm_visit_cb(kd, cb, arg, pa, va, dva,
|
|
|
|
prot, AARCH64_PAGE_SIZE, 0)) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
out:
|
|
|
|
_kvm_bitmap_deinit(&bm);
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2017-03-22 02:42:00 +00:00
|
|
|
static struct kvm_arch kvm_aarch64_minidump = {
|
2015-11-27 18:58:26 +00:00
|
|
|
.ka_probe = _aarch64_minidump_probe,
|
|
|
|
.ka_initvtop = _aarch64_minidump_initvtop,
|
|
|
|
.ka_freevtop = _aarch64_minidump_freevtop,
|
|
|
|
.ka_kvatop = _aarch64_minidump_kvatop,
|
|
|
|
.ka_native = _aarch64_native,
|
2017-11-11 23:30:58 +00:00
|
|
|
.ka_walk_pages = _aarch64_minidump_walk_pages,
|
2015-11-27 18:58:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
KVM_ARCH(kvm_aarch64_minidump);
|