summaryrefslogtreecommitdiff
path: root/lib/libkvm
diff options
context:
space:
mode:
authorMiod Vallat <miod@cvs.openbsd.org>2007-03-03 21:37:28 +0000
committerMiod Vallat <miod@cvs.openbsd.org>2007-03-03 21:37:28 +0000
commit301ce3789c0dded4c40ebb4c1197dee00cf0fc3e (patch)
tree44351c56ccd077c95b601f112ad6c7c4d156911e /lib/libkvm
parent8f58ce59b66d35d5d484c5cfb9f5967571339ac9 (diff)
Kernel crash dumps and associated libkvm bits for landisk.
Diffstat (limited to 'lib/libkvm')
-rw-r--r--lib/libkvm/kvm_sh.c144
1 files changed, 115 insertions, 29 deletions
diff --git a/lib/libkvm/kvm_sh.c b/lib/libkvm/kvm_sh.c
index 4940acbb7d3..52326a2eec4 100644
--- a/lib/libkvm/kvm_sh.c
+++ b/lib/libkvm/kvm_sh.c
@@ -1,47 +1,43 @@
-/* $OpenBSD: kvm_sh.c,v 1.1 2006/10/10 22:07:10 miod Exp $ */
+/* $OpenBSD: kvm_sh.c,v 1.2 2007/03/03 21:37:27 miod Exp $ */
/*
- * Copyright (c) 2002, Miodrag Vallat.
- * All rights reserved.
+ * Copyright (c) 2007 Miodrag Vallat.
*
- * 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.
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice, this permission notice, and the disclaimer below
+ * appear in all copies.
*
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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.
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/param.h>
+#include <sys/core.h>
+#include <sys/kcore.h>
#include <unistd.h>
#include <stdlib.h>
+#include <nlist.h>
#include <kvm.h>
#include <db.h>
#include "kvm_private.h"
+#include <machine/cpu.h>
+#include <machine/kcore.h>
+#include <machine/pte.h>
+#include <machine/vmparam.h>
+
void
_kvm_freevtop(kvm_t *kd)
{
- if (kd->vmst != NULL) {
- free(kd->vmst);
- kd->vmst = NULL;
- }
}
int
@@ -50,17 +46,97 @@ _kvm_initvtop(kvm_t *kd)
return (0);
}
+/*
+ * Translate a kernel virtual address to a physical address by walking
+ * the kernels page table.
+ */
+
+/* Stolen from sys/arch/sh/include/pmap.h we can't really include */
+#define __PMAP_PTP_N 512 /* # of page table page maps 2GB. */
+/* Stolen from sys/arch/sh/sh/pmap.c */
+#define __PMAP_PTP_SHIFT 22
+#define __PMAP_PTP_PG_N (PAGE_SIZE / sizeof(pt_entry_t))
+#define __PMAP_PTP_INDEX(va) (((va) >> __PMAP_PTP_SHIFT) & (__PMAP_PTP_N - 1))
+#define __PMAP_PTP_OFSET(va) ((va >> PGSHIFT) & (__PMAP_PTP_PG_N - 1))
+
int
_kvm_kvatop(kvm_t *kd, u_long va, paddr_t *pa)
{
- int offset;
+ cpu_kcore_hdr_t *h = kd->cpu_data;
+ u_int l1idx, l2idx;
+ vaddr_t l2va;
+ pt_entry_t l1pte, l2pte;
+ off_t pteoffset;
if (ISALIVE(kd)) {
_kvm_err(kd, 0, "vatop called in live kernel!");
return (0);
}
- /* TODO */
+ /*
+ * P1 and P2 segments addresses are trivial.
+ */
+ if (va >= SH3_P1SEG_BASE && va <= SH3_P1SEG_END) {
+ *pa = SH3_P1SEG_TO_PHYS(va);
+ return (int)((vaddr_t)SH3_P1SEG_END + 1 - va);
+ }
+ if (va >= SH3_P2SEG_BASE && va <= SH3_P2SEG_END) {
+ *pa = SH3_P2SEG_TO_PHYS(va);
+ return (int)((vaddr_t)SH3_P2SEG_END + 1 - va);
+ }
+
+ /*
+ * P3 segment addresses need kernel page table walk.
+ */
+ if (va >= SH3_P3SEG_BASE && va < SH3_P3SEG_END) {
+ l1idx = __PMAP_PTP_INDEX(va - VM_MIN_KERNEL_ADDRESS);
+ l2idx = __PMAP_PTP_OFSET(va);
+
+ /* read level 1 pte */
+ pteoffset = h->kcore_kptp + sizeof(pt_entry_t) * l1idx;
+ if (_kvm_pread(kd, kd->pmfd, (char *)&l1pte, sizeof(l1pte),
+ _kvm_pa2off(kd, pteoffset)) != sizeof(l1pte)) {
+ _kvm_syserr(kd, 0, "could not read level 1 pte");
+ goto bad;
+ }
+
+ /* check pte for validity */
+ if ((l1pte & PG_V) == 0) {
+ _kvm_err(kd, 0, "invalid level 1 pte: no valid bit");
+ goto bad;
+ }
+
+ l2va = l1pte & PG_PPN;
+ if (l2va < SH3_P1SEG_BASE || l2va > SH3_P1SEG_END) {
+ _kvm_err(kd, 0, "invalid level 1 pte: out of P1");
+ goto bad;
+ }
+
+ /* read level 2 pte */
+ pteoffset = SH3_P1SEG_TO_PHYS(l2va) +
+ sizeof(pt_entry_t) * l2idx;
+ if (_kvm_pread(kd, kd->pmfd, (char *)&l2pte, sizeof(l2pte),
+ _kvm_pa2off(kd, pteoffset)) != sizeof(l2pte)) {
+ _kvm_syserr(kd, 0, "could not read level 2 pte");
+ goto bad;
+ }
+
+ /* check pte for validity */
+ if ((l2pte & PG_V) == 0) {
+ _kvm_err(kd, 0, "invalid level 2 pte: no valid bit");
+ goto bad;
+ }
+
+ *pa = (l2pte & PG_PPN) | (va & PAGE_MASK);
+ return (PAGE_SIZE - (va & PAGE_MASK));
+ }
+
+ /*
+ * All other addresses are incorrect.
+ */
+ _kvm_err(kd, 0, "not a kernel virtual address");
+bad:
+ *pa = (paddr_t)-1;
return (0);
}
@@ -70,7 +146,17 @@ _kvm_kvatop(kvm_t *kd, u_long va, paddr_t *pa)
off_t
_kvm_pa2off(kvm_t *kd, paddr_t pa)
{
- /* TODO */
+ cpu_kcore_hdr_t *h = kd->cpu_data;
+ phys_ram_seg_t *seg = h->kcore_segs;
+ off_t off = kd->dump_off;
+ u_int i;
+
+ for (i = h->kcore_nsegs; i != 0; i--) {
+ if (pa >= seg->start && pa < seg->start + seg->size)
+ return (off + (pa - seg->start));
+ off += seg->size;
+ }
+
+ _kvm_err(kd, 0, "physical address out of the image (%lx)", pa);
return (0);
}
-