2006-04-21 04:32: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
/*
|
2015-11-27 18:58:26 +00:00
|
|
|
* i386 machine dependent routines for kvm and minidumps.
|
2006-04-21 04:32:51 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
2015-11-27 18:58:26 +00:00
|
|
|
#include <sys/endian.h>
|
|
|
|
#include <stdint.h>
|
2006-04-21 04:32:51 +00:00
|
|
|
#include <stdlib.h>
|
2009-06-14 12:42:06 +00:00
|
|
|
#include <string.h>
|
2006-04-21 04:32:51 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <kvm.h>
|
|
|
|
|
2015-11-27 18:58:26 +00:00
|
|
|
#include "../../sys/i386/include/minidump.h"
|
2006-04-21 04:32:51 +00:00
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
#include "kvm_private.h"
|
2015-11-27 18:58:26 +00:00
|
|
|
#include "kvm_i386.h"
|
2006-04-21 04:32:51 +00:00
|
|
|
|
2015-11-27 18:58:26 +00:00
|
|
|
#define i386_round_page(x) roundup2((kvaddr_t)(x), I386_PAGE_SIZE)
|
2006-04-21 04:32:51 +00:00
|
|
|
|
|
|
|
struct vmstate {
|
|
|
|
struct minidumphdr hdr;
|
|
|
|
};
|
|
|
|
|
2017-11-11 23:30:58 +00:00
|
|
|
static i386_pte_pae_t
|
|
|
|
_i386_pte_pae_get(kvm_t *kd, u_long pteindex)
|
|
|
|
{
|
|
|
|
i386_pte_pae_t *pte = _kvm_pmap_get(kd, pteindex, sizeof(*pte));
|
|
|
|
|
|
|
|
return le64toh(*pte);
|
|
|
|
}
|
|
|
|
|
|
|
|
static i386_pte_t
|
|
|
|
_i386_pte_get(kvm_t *kd, u_long pteindex)
|
|
|
|
{
|
|
|
|
i386_pte_t *pte = _kvm_pmap_get(kd, pteindex, sizeof(*pte));
|
|
|
|
|
|
|
|
return le32toh(*pte);
|
|
|
|
}
|
|
|
|
|
2006-04-21 04:32:51 +00:00
|
|
|
static int
|
2015-11-27 18:58:26 +00:00
|
|
|
_i386_minidump_probe(kvm_t *kd)
|
2006-04-21 04:32:51 +00:00
|
|
|
{
|
|
|
|
|
2015-11-27 18:58:26 +00:00
|
|
|
return (_kvm_probe_elf_kernel(kd, ELFCLASS32, EM_386) &&
|
|
|
|
_kvm_is_minidump(kd));
|
2006-04-21 04:32:51 +00:00
|
|
|
}
|
|
|
|
|
2015-11-27 18:58:26 +00:00
|
|
|
static void
|
|
|
|
_i386_minidump_freevtop(kvm_t *kd)
|
2006-04-21 04:32:51 +00:00
|
|
|
{
|
|
|
|
struct vmstate *vm = kd->vmst;
|
|
|
|
|
|
|
|
free(vm);
|
|
|
|
kd->vmst = NULL;
|
|
|
|
}
|
|
|
|
|
2015-11-27 18:58:26 +00:00
|
|
|
static int
|
|
|
|
_i386_minidump_initvtop(kvm_t *kd)
|
2006-04-21 04:32:51 +00:00
|
|
|
{
|
|
|
|
struct vmstate *vmst;
|
2016-07-18 01:55:25 +00:00
|
|
|
off_t off, sparse_off;
|
2006-04-21 04:32:51 +00:00
|
|
|
|
|
|
|
vmst = _kvm_malloc(kd, sizeof(*vmst));
|
2016-04-22 18:05:34 +00:00
|
|
|
if (vmst == NULL) {
|
2006-04-21 04:32: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);
|
2006-04-21 04:32:51 +00:00
|
|
|
if (vmst->hdr.version != MINIDUMP_VERSION) {
|
|
|
|
_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.ptesize = le32toh(vmst->hdr.ptesize);
|
|
|
|
vmst->hdr.kernbase = le32toh(vmst->hdr.kernbase);
|
|
|
|
vmst->hdr.paemode = le32toh(vmst->hdr.paemode);
|
2006-04-21 04:32:51 +00:00
|
|
|
|
|
|
|
/* Skip header and msgbuf */
|
2015-11-27 18:58:26 +00:00
|
|
|
off = I386_PAGE_SIZE + i386_round_page(vmst->hdr.msgbufsize);
|
2006-04-21 04:32:51 +00:00
|
|
|
|
2016-07-18 01:55:25 +00:00
|
|
|
sparse_off = off + i386_round_page(vmst->hdr.bitmapsize) +
|
|
|
|
i386_round_page(vmst->hdr.ptesize);
|
|
|
|
if (_kvm_pt_init(kd, vmst->hdr.bitmapsize, off, sparse_off,
|
|
|
|
I386_PAGE_SIZE, sizeof(uint32_t)) == -1) {
|
2006-04-21 04:32:51 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
2015-11-27 18:58:26 +00:00
|
|
|
off += i386_round_page(vmst->hdr.bitmapsize);
|
2006-04-21 04:32:51 +00:00
|
|
|
|
2017-11-11 23:30:58 +00:00
|
|
|
if (_kvm_pmap_init(kd, vmst->hdr.ptesize, off) == -1) {
|
2006-04-21 04:32:51 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
2016-07-18 01:55:25 +00:00
|
|
|
off += i386_round_page(vmst->hdr.ptesize);
|
2006-04-21 04:32:51 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-11-27 18:58:26 +00:00
|
|
|
_i386_minidump_vatop_pae(kvm_t *kd, kvaddr_t va, off_t *pa)
|
2006-04-21 04:32:51 +00:00
|
|
|
{
|
|
|
|
struct vmstate *vm;
|
2015-11-27 18:58:26 +00:00
|
|
|
i386_physaddr_pae_t offset;
|
|
|
|
i386_pte_pae_t pte;
|
|
|
|
kvaddr_t pteindex;
|
|
|
|
i386_physaddr_pae_t a;
|
2006-04-21 04:32:51 +00:00
|
|
|
off_t ofs;
|
|
|
|
|
|
|
|
vm = kd->vmst;
|
2015-11-27 18:58:26 +00:00
|
|
|
offset = va & I386_PAGE_MASK;
|
2006-04-21 04:32:51 +00:00
|
|
|
|
|
|
|
if (va >= vm->hdr.kernbase) {
|
2015-11-27 18:58:26 +00:00
|
|
|
pteindex = (va - vm->hdr.kernbase) >> I386_PAGE_SHIFT;
|
2017-11-11 23:30:58 +00:00
|
|
|
if (pteindex >= vm->hdr.ptesize / sizeof(pte))
|
2016-07-18 01:03:39 +00:00
|
|
|
goto invalid;
|
2017-11-11 23:30:58 +00:00
|
|
|
pte = _i386_pte_pae_get(kd, pteindex);
|
2015-11-27 18:58:26 +00:00
|
|
|
if ((pte & I386_PG_V) == 0) {
|
|
|
|
_kvm_err(kd, kd->program,
|
|
|
|
"_i386_minidump_vatop_pae: pte not valid");
|
2006-04-21 04:32:51 +00:00
|
|
|
goto invalid;
|
|
|
|
}
|
2015-11-27 18:58:26 +00:00
|
|
|
a = pte & I386_PG_FRAME_PAE;
|
2017-11-11 23:30:58 +00:00
|
|
|
ofs = _kvm_pt_find(kd, a, I386_PAGE_SIZE);
|
2006-04-21 04:32:51 +00:00
|
|
|
if (ofs == -1) {
|
2015-11-27 18:58:26 +00:00
|
|
|
_kvm_err(kd, kd->program,
|
|
|
|
"_i386_minidump_vatop_pae: physical address 0x%jx not in minidump",
|
|
|
|
(uintmax_t)a);
|
2006-04-21 04:32:51 +00:00
|
|
|
goto invalid;
|
|
|
|
}
|
|
|
|
*pa = ofs + offset;
|
2015-11-27 18:58:26 +00:00
|
|
|
return (I386_PAGE_SIZE - offset);
|
2006-04-21 04:32:51 +00:00
|
|
|
} else {
|
2015-11-27 18:58:26 +00:00
|
|
|
_kvm_err(kd, kd->program,
|
|
|
|
"_i386_minidump_vatop_pae: virtual address 0x%jx not minidumped",
|
|
|
|
(uintmax_t)va);
|
2006-04-21 04:32: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);
|
2006-04-21 04:32:51 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-11-27 18:58:26 +00:00
|
|
|
_i386_minidump_vatop(kvm_t *kd, kvaddr_t va, off_t *pa)
|
2006-04-21 04:32:51 +00:00
|
|
|
{
|
|
|
|
struct vmstate *vm;
|
2015-11-27 18:58:26 +00:00
|
|
|
i386_physaddr_t offset;
|
|
|
|
i386_pte_t pte;
|
|
|
|
kvaddr_t pteindex;
|
|
|
|
i386_physaddr_t a;
|
2006-04-21 04:32:51 +00:00
|
|
|
off_t ofs;
|
|
|
|
|
|
|
|
vm = kd->vmst;
|
2015-11-27 18:58:26 +00:00
|
|
|
offset = va & I386_PAGE_MASK;
|
2006-04-21 04:32:51 +00:00
|
|
|
|
|
|
|
if (va >= vm->hdr.kernbase) {
|
2015-11-27 18:58:26 +00:00
|
|
|
pteindex = (va - vm->hdr.kernbase) >> I386_PAGE_SHIFT;
|
2017-11-11 23:30:58 +00:00
|
|
|
if (pteindex >= vm->hdr.ptesize / sizeof(pte))
|
2016-07-18 01:03:39 +00:00
|
|
|
goto invalid;
|
2017-11-11 23:30:58 +00:00
|
|
|
pte = _i386_pte_get(kd, pteindex);
|
2015-11-27 18:58:26 +00:00
|
|
|
if ((pte & I386_PG_V) == 0) {
|
|
|
|
_kvm_err(kd, kd->program,
|
|
|
|
"_i386_minidump_vatop: pte not valid");
|
2006-04-21 04:32:51 +00:00
|
|
|
goto invalid;
|
|
|
|
}
|
2015-11-27 18:58:26 +00:00
|
|
|
a = pte & I386_PG_FRAME;
|
2017-11-11 23:30:58 +00:00
|
|
|
ofs = _kvm_pt_find(kd, a, I386_PAGE_SIZE);
|
2006-04-21 04:32:51 +00:00
|
|
|
if (ofs == -1) {
|
2015-11-27 18:58:26 +00:00
|
|
|
_kvm_err(kd, kd->program,
|
|
|
|
"_i386_minidump_vatop: physical address 0x%jx not in minidump",
|
|
|
|
(uintmax_t)a);
|
2006-04-21 04:32:51 +00:00
|
|
|
goto invalid;
|
|
|
|
}
|
|
|
|
*pa = ofs + offset;
|
2015-11-27 18:58:26 +00:00
|
|
|
return (I386_PAGE_SIZE - offset);
|
2006-04-21 04:32:51 +00:00
|
|
|
} else {
|
2015-11-27 18:58:26 +00:00
|
|
|
_kvm_err(kd, kd->program,
|
|
|
|
"_i386_minidump_vatop: virtual address 0x%jx not minidumped",
|
|
|
|
(uintmax_t)va);
|
2006-04-21 04:32: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);
|
2006-04-21 04:32:51 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2015-11-27 18:58:26 +00:00
|
|
|
static int
|
|
|
|
_i386_minidump_kvatop(kvm_t *kd, kvaddr_t va, off_t *pa)
|
2006-04-21 04:32:51 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
if (ISALIVE(kd)) {
|
2015-11-27 18:58:26 +00:00
|
|
|
_kvm_err(kd, 0, "_i386_minidump_kvatop called in live kernel!");
|
2006-04-21 04:32:51 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (kd->vmst->hdr.paemode)
|
2015-11-27 18:58:26 +00:00
|
|
|
return (_i386_minidump_vatop_pae(kd, va, pa));
|
2015-11-26 19:42:10 +00:00
|
|
|
else
|
2015-11-27 18:58:26 +00:00
|
|
|
return (_i386_minidump_vatop(kd, va, pa));
|
2006-04-21 04:32:51 +00:00
|
|
|
}
|
2015-11-27 18:58:26 +00:00
|
|
|
|
2017-11-11 23:30:58 +00:00
|
|
|
static vm_prot_t
|
|
|
|
_i386_entry_to_prot(uint64_t pte)
|
|
|
|
{
|
|
|
|
vm_prot_t prot = VM_PROT_READ;
|
|
|
|
|
|
|
|
/* Source: i386/pmap.c:pmap_protect() */
|
|
|
|
if (pte & I386_PG_RW)
|
|
|
|
prot |= VM_PROT_WRITE;
|
|
|
|
if ((pte & I386_PG_NX) == 0)
|
|
|
|
prot |= VM_PROT_EXECUTE;
|
|
|
|
|
|
|
|
return prot;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct i386_iter {
|
|
|
|
kvm_t *kd;
|
|
|
|
u_long nptes;
|
|
|
|
u_long pteindex;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
_i386_iterator_init(struct i386_iter *it, kvm_t *kd)
|
|
|
|
{
|
|
|
|
struct vmstate *vm = kd->vmst;
|
|
|
|
|
|
|
|
it->kd = kd;
|
|
|
|
it->pteindex = 0;
|
|
|
|
if (vm->hdr.paemode) {
|
|
|
|
it->nptes = vm->hdr.ptesize / sizeof(i386_pte_pae_t);
|
|
|
|
} else {
|
|
|
|
it->nptes = vm->hdr.ptesize / sizeof(i386_pte_t);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
_i386_iterator_next(struct i386_iter *it, u_long *pa, u_long *va, u_long *dva,
|
|
|
|
vm_prot_t *prot)
|
|
|
|
{
|
|
|
|
struct vmstate *vm = it->kd->vmst;
|
|
|
|
i386_pte_t pte32;
|
|
|
|
i386_pte_pae_t pte64;
|
|
|
|
int found = 0;
|
|
|
|
|
|
|
|
*dva = 0;
|
|
|
|
for (; it->pteindex < it->nptes && found == 0; it->pteindex++) {
|
|
|
|
if (vm->hdr.paemode) {
|
|
|
|
pte64 = _i386_pte_pae_get(it->kd, it->pteindex);
|
|
|
|
if ((pte64 & I386_PG_V) == 0)
|
|
|
|
continue;
|
|
|
|
*prot = _i386_entry_to_prot(pte64);
|
|
|
|
*pa = pte64 & I386_PG_FRAME_PAE;
|
|
|
|
} else {
|
|
|
|
pte32 = _i386_pte_get(it->kd, it->pteindex);
|
|
|
|
if ((pte32 & I386_PG_V) == 0)
|
|
|
|
continue;
|
|
|
|
*prot = _i386_entry_to_prot(pte32);
|
|
|
|
*pa = pte32 & I386_PG_FRAME;
|
|
|
|
}
|
|
|
|
*va = vm->hdr.kernbase + (it->pteindex << I386_PAGE_SHIFT);
|
|
|
|
found = 1;
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
_i386_minidump_walk_pages(kvm_t *kd, kvm_walk_pages_cb_t *cb, void *arg)
|
|
|
|
{
|
|
|
|
struct i386_iter it;
|
|
|
|
u_long dva, pa, va;
|
|
|
|
vm_prot_t prot;
|
|
|
|
|
|
|
|
_i386_iterator_init(&it, kd);
|
|
|
|
while (_i386_iterator_next(&it, &pa, &va, &dva, &prot)) {
|
|
|
|
if (!_kvm_visit_cb(kd, cb, arg, pa, va, dva,
|
|
|
|
prot, I386_PAGE_SIZE, 0)) {
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
2017-03-22 02:42:00 +00:00
|
|
|
static struct kvm_arch kvm_i386_minidump = {
|
2015-11-27 18:58:26 +00:00
|
|
|
.ka_probe = _i386_minidump_probe,
|
|
|
|
.ka_initvtop = _i386_minidump_initvtop,
|
|
|
|
.ka_freevtop = _i386_minidump_freevtop,
|
|
|
|
.ka_kvatop = _i386_minidump_kvatop,
|
|
|
|
.ka_native = _i386_native,
|
2017-11-11 23:30:58 +00:00
|
|
|
.ka_walk_pages = _i386_minidump_walk_pages,
|
2015-11-27 18:58:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
KVM_ARCH(kvm_i386_minidump);
|