1 /* $NetBSD: kvm_sun3x.c,v 1.9 2008/04/28 20:23:01 martin Exp $ */ 2 3 /*- 4 * Copyright (c) 1997 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Gordon W. Ross. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #if defined(LIBC_SCCS) && !defined(lint) 34 #if 0 35 static char sccsid[] = "@(#)kvm_sparc.c 8.1 (Berkeley) 6/4/93"; 36 #else 37 __RCSID("$NetBSD: kvm_sun3x.c,v 1.9 2008/04/28 20:23:01 martin Exp $"); 38 #endif 39 #endif /* LIBC_SCCS and not lint */ 40 41 /* 42 * Sun3x machine dependent routines for kvm. 43 * 44 * Note: This file has to build on ALL m68k machines, 45 * so do NOT include any <machine / *.h> files here. 46 */ 47 48 #include <sys/types.h> 49 #include <sys/kcore.h> 50 51 #include <unistd.h> 52 #include <limits.h> 53 #include <nlist.h> 54 #include <kvm.h> 55 #include <db.h> 56 57 #include <m68k/kcore.h> 58 59 #include "kvm_private.h" 60 #include "kvm_m68k.h" 61 62 int _kvm_sun3x_initvtop __P((kvm_t *)); 63 void _kvm_sun3x_freevtop __P((kvm_t *)); 64 int _kvm_sun3x_kvatop __P((kvm_t *, u_long, u_long *)); 65 off_t _kvm_sun3x_pa2off __P((kvm_t *, u_long)); 66 67 struct kvm_ops _kvm_ops_sun3x = { 68 _kvm_sun3x_initvtop, 69 _kvm_sun3x_freevtop, 70 _kvm_sun3x_kvatop, 71 _kvm_sun3x_pa2off }; 72 73 #define _kvm_kvas_size(h) \ 74 (-((h)->kernbase)) 75 #define _kvm_nkptes(h, v) \ 76 (_kvm_kvas_size((h)) >> (v)->pgshift) 77 #define _kvm_pg_pa(pte, h) \ 78 ((pte) & (h)->pg_frame) 79 80 /* 81 * Prepare for translation of kernel virtual addresses into offsets 82 * into crash dump files. Nothing to do here. 83 */ 84 int 85 _kvm_sun3x_initvtop(kd) 86 kvm_t *kd; 87 { 88 return (0); 89 } 90 91 void 92 _kvm_sun3x_freevtop(kd) 93 kvm_t *kd; 94 { 95 } 96 97 /* 98 * Translate a kernel virtual address to a physical address using the 99 * mapping information in kd->vm. Returns the result in pa, and returns 100 * the number of bytes that are contiguously available from this 101 * physical address. This routine is used only for crash dumps. 102 */ 103 int 104 _kvm_sun3x_kvatop(kd, va, pap) 105 kvm_t *kd; 106 u_long va; 107 u_long *pap; 108 { 109 cpu_kcore_hdr_t *h = kd->cpu_data; 110 struct sun3x_kcore_hdr *s = &h->un._sun3x; 111 struct vmstate *v = kd->vmst; 112 int idx, len, offset, pte; 113 u_long pteva, pa; 114 115 if (ISALIVE(kd)) { 116 _kvm_err(kd, 0, "vatop called in live kernel!"); 117 return(0); 118 } 119 120 if (va < h->kernbase) { 121 _kvm_err(kd, 0, "not a kernel address"); 122 return(0); 123 } 124 125 /* 126 * If this VA is in the contiguous range, short-cut. 127 * Note that this ends our recursion when we call 128 * kvm_read to access the kernel page table, which 129 * is guaranteed to be in the contiguous range. 130 */ 131 if (va < s->contig_end) { 132 len = s->contig_end - va; 133 pa = va - h->kernbase; 134 goto done; 135 } 136 137 /* 138 * The KVA is beyond the contiguous range, so we must 139 * read the PTE for this KVA from the page table. 140 */ 141 idx = ((va - h->kernbase) >> v->pgshift); 142 pteva = s->kernCbase + (idx * 4); 143 if (kvm_read(kd, pteva, &pte, 4) != 4) { 144 _kvm_err(kd, 0, "can not read PTE!"); 145 return (0); 146 } 147 if ((pte & s->pg_valid) == 0) { 148 _kvm_err(kd, 0, "page not valid (VA=0x%lx)", va); 149 return (0); 150 } 151 offset = va & v->pgofset; 152 len = (h->page_size - offset); 153 pa = _kvm_pg_pa(pte, s) + offset; 154 155 done: 156 *pap = pa; 157 return (len); 158 } 159 160 /* 161 * Translate a physical address to a file-offset in the crash dump. 162 */ 163 off_t 164 _kvm_sun3x_pa2off(kd, pa) 165 kvm_t *kd; 166 u_long pa; 167 { 168 off_t off; 169 phys_ram_seg_t *rsp; 170 cpu_kcore_hdr_t *h = kd->cpu_data; 171 struct sun3x_kcore_hdr *s = &h->un._sun3x; 172 173 off = 0; 174 for (rsp = s->ram_segs; rsp->size; rsp++) { 175 if (pa >= rsp->start && pa < rsp->start + rsp->size) { 176 pa -= rsp->start; 177 break; 178 } 179 off += rsp->size; 180 } 181 return (kd->dump_off + off + pa); 182 } 183 184