1 /* $NetBSD: kvm_sparc.c,v 1.15 1997/10/10 13:11:50 mrg Exp $ */ 2 3 /*- 4 * Copyright (c) 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software developed by the Computer Systems 8 * Engineering group at Lawrence Berkeley Laboratory under DARPA contract 9 * BG 91-66 and contributed to Berkeley. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the University of 22 * California, Berkeley and its contributors. 23 * 4. Neither the name of the University nor the names of its contributors 24 * may be used to endorse or promote products derived from this software 25 * without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 */ 39 40 #include <sys/cdefs.h> 41 #if defined(LIBC_SCCS) && !defined(lint) 42 #if 0 43 static char sccsid[] = "@(#)kvm_sparc.c 8.1 (Berkeley) 6/4/93"; 44 #else 45 __RCSID("$NetBSD: kvm_sparc.c,v 1.15 1997/10/10 13:11:50 mrg Exp $"); 46 #endif 47 #endif /* LIBC_SCCS and not lint */ 48 49 /* 50 * Sparc machine dependent routines for kvm. Hopefully, the forthcoming 51 * vm code will one day obsolete this module. 52 */ 53 54 #include <sys/param.h> 55 #include <sys/exec.h> 56 #include <sys/user.h> 57 #include <sys/proc.h> 58 #include <sys/stat.h> 59 #include <sys/core.h> 60 #include <sys/kcore.h> 61 #include <unistd.h> 62 #include <nlist.h> 63 #include <kvm.h> 64 65 #include <vm/vm.h> 66 #include <vm/vm_param.h> 67 #include <machine/autoconf.h> 68 #include <machine/kcore.h> 69 70 #include <limits.h> 71 #include <db.h> 72 73 #include "kvm_private.h" 74 75 76 static int cputyp = -1; 77 static int pgshift; 78 static int nptesg; /* [sun4/sun4c] only */ 79 80 #define VA_VPG(va) ((cputyp == CPU_SUN4C || cputyp == CPU_SUN4M) \ 81 ? VA_SUN4C_VPG(va) \ 82 : VA_SUN4_VPG(va)) 83 84 #define VA_OFF(va) (va & (kd->nbpg - 1)) 85 86 int _kvm_kvatop44c __P((kvm_t *, u_long, u_long *)); 87 int _kvm_kvatop4m __P((kvm_t *, u_long, u_long *)); 88 89 void 90 _kvm_freevtop(kd) 91 kvm_t *kd; 92 { 93 if (kd->vmst != 0) { 94 _kvm_err(kd, kd->program, "_kvm_freevtop: internal error"); 95 kd->vmst = 0; 96 } 97 } 98 99 /* 100 * Prepare for translation of kernel virtual addresses into offsets 101 * into crash dump files. We use the MMU specific goop written at the 102 * front of the crash dump by pmap_dumpmmu(). 103 */ 104 int 105 _kvm_initvtop(kd) 106 kvm_t *kd; 107 { 108 cpu_kcore_hdr_t *cpup = kd->cpu_data; 109 110 switch (cputyp = cpup->cputype) { 111 case CPU_SUN4: 112 kd->nbpg = 8196; 113 pgshift = 13; 114 break; 115 case CPU_SUN4C: 116 case CPU_SUN4M: 117 kd->nbpg = 4096; 118 pgshift = 12; 119 break; 120 default: 121 _kvm_err(kd, kd->program, "Unsupported CPU type"); 122 return (-1); 123 } 124 nptesg = NBPSG / kd->nbpg; 125 return (0); 126 } 127 128 /* 129 * Translate a kernel virtual address to a physical address using the 130 * mapping information in kd->vm. Returns the result in pa, and returns 131 * the number of bytes that are contiguously available from this 132 * physical address. This routine is used only for crashdumps. 133 */ 134 int 135 _kvm_kvatop(kd, va, pa) 136 kvm_t *kd; 137 u_long va; 138 u_long *pa; 139 { 140 if (cputyp == -1) 141 if (_kvm_initvtop(kd) != 0) 142 return (-1); 143 144 return ((cputyp == CPU_SUN4M) 145 ? _kvm_kvatop4m(kd, va, pa) 146 : _kvm_kvatop44c(kd, va, pa)); 147 } 148 149 /* 150 * (note: sun4 3-level MMU not yet supported) 151 */ 152 int 153 _kvm_kvatop44c(kd, va, pa) 154 kvm_t *kd; 155 u_long va; 156 u_long *pa; 157 { 158 register int vr, vs, pte; 159 cpu_kcore_hdr_t *cpup = kd->cpu_data; 160 struct segmap *sp, *segmaps; 161 int *ptes; 162 int nkreg, nureg; 163 u_long kernbase = cpup->kernbase; 164 165 if (va < kernbase) 166 goto err; 167 168 /* 169 * Layout of CPU segment: 170 * cpu_kcore_hdr_t; 171 * [alignment] 172 * phys_ram_seg_t[cpup->nmemseg]; 173 * segmap[cpup->nsegmap]; 174 * ptes[cpup->npmegs]; 175 */ 176 segmaps = (struct segmap *)((long)kd->cpu_data + cpup->segmapoffset); 177 ptes = (int *)((int)kd->cpu_data + cpup->pmegoffset); 178 nkreg = ((int)((-(unsigned)kernbase) / NBPRG)); 179 nureg = 256 - nkreg; 180 181 vr = VA_VREG(va); 182 vs = VA_VSEG(va); 183 184 sp = &segmaps[(vr-nureg)*NSEGRG + vs]; 185 if (sp->sg_npte == 0) 186 goto err; 187 if (sp->sg_pmeg == cpup->npmeg - 1) /* =seginval */ 188 goto err; 189 pte = ptes[sp->sg_pmeg * nptesg + VA_VPG(va)]; 190 if ((pte & PG_V) != 0) { 191 register long p, off = VA_OFF(va); 192 193 p = (pte & PG_PFNUM) << pgshift; 194 *pa = p + off; 195 return (kd->nbpg - off); 196 } 197 err: 198 _kvm_err(kd, 0, "invalid address (%x)", va); 199 return (0); 200 } 201 202 int 203 _kvm_kvatop4m(kd, va, pa) 204 kvm_t *kd; 205 u_long va; 206 u_long *pa; 207 { 208 cpu_kcore_hdr_t *cpup = kd->cpu_data; 209 register int vr, vs; 210 int pte; 211 off_t foff; 212 struct segmap *sp, *segmaps; 213 int nkreg, nureg; 214 u_long kernbase = cpup->kernbase; 215 216 if (va < kernbase) 217 goto err; 218 219 /* 220 * Layout of CPU segment: 221 * cpu_kcore_hdr_t; 222 * [alignment] 223 * phys_ram_seg_t[cpup->nmemseg]; 224 * segmap[cpup->nsegmap]; 225 */ 226 segmaps = (struct segmap *)((long)kd->cpu_data + cpup->segmapoffset); 227 nkreg = ((int)((-(unsigned)kernbase) / NBPRG)); 228 nureg = 256 - nkreg; 229 230 vr = VA_VREG(va); 231 vs = VA_VSEG(va); 232 233 sp = &segmaps[(vr-nureg)*NSEGRG + vs]; 234 if (sp->sg_npte == 0) 235 goto err; 236 237 /* XXX - assume page tables in initial kernel DATA or BSS. */ 238 foff = _kvm_pa2off(kd, (u_long)&sp->sg_pte[VA_VPG(va)] - kernbase); 239 if (foff == (off_t)-1) 240 return (0); 241 242 if (lseek(kd->pmfd, foff, 0) == -1 || 243 read(kd->pmfd, (void *)&pte, sizeof(pte)) < 0) { 244 _kvm_err(kd, kd->program, "cannot read pte for %x", va); 245 return (0); 246 } 247 248 if ((pte & SRMMU_TETYPE) == SRMMU_TEPTE) { 249 register long p, off = VA_OFF(va); 250 251 p = (pte & SRMMU_PPNMASK) << SRMMU_PPNPASHIFT; 252 *pa = p + off; 253 return (kd->nbpg - off); 254 } 255 err: 256 _kvm_err(kd, 0, "invalid address (%x)", va); 257 return (0); 258 } 259 260 /* 261 * Translate a physical address to a file-offset in the crash-dump. 262 */ 263 off_t 264 _kvm_pa2off(kd, pa) 265 kvm_t *kd; 266 u_long pa; 267 { 268 cpu_kcore_hdr_t *cpup = kd->cpu_data; 269 phys_ram_seg_t *mp; 270 off_t off; 271 int nmem; 272 273 /* 274 * Layout of CPU segment: 275 * cpu_kcore_hdr_t; 276 * [alignment] 277 * phys_ram_seg_t[cpup->nmemseg]; 278 */ 279 mp = (phys_ram_seg_t *)((int)kd->cpu_data + cpup->memsegoffset); 280 off = 0; 281 282 /* Translate (sparse) pfnum to (packed) dump offset */ 283 for (nmem = cpup->nmemseg; --nmem >= 0; mp++) { 284 if (mp->start <= pa && pa < mp->start + mp->size) 285 break; 286 off += mp->size; 287 } 288 if (nmem < 0) { 289 _kvm_err(kd, 0, "invalid address (%x)", pa); 290 return (-1); 291 } 292 293 return (kd->dump_off + off + pa - mp->start); 294 } 295 296 /* 297 * Machine-dependent initialization for ALL open kvm descriptors, 298 * not just those for a kernel crash dump. Some architectures 299 * have to deal with these NOT being constants! (i.e. m68k) 300 */ 301 int 302 _kvm_mdopen(kd) 303 kvm_t *kd; 304 { 305 u_long max_uva; 306 extern struct ps_strings *__ps_strings; 307 308 max_uva = (u_long) (__ps_strings + 1); 309 kd->usrstack = max_uva; 310 kd->max_uva = max_uva; 311 kd->min_uva = 0; 312 313 return (0); 314 } 315