kvm_sparc.c revision 147672
1/*- 2 * Copyright (c) 1992, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software developed by the Computer Systems 6 * Engineering group at Lawrence Berkeley Laboratory under DARPA contract 7 * BG 91-66 and contributed to Berkeley. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by the University of 20 * California, Berkeley and its contributors. 21 * 4. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 */ 37 38#include <sys/cdefs.h> 39__FBSDID("$FreeBSD: head/lib/libkvm/kvm_sparc.c 147672 2005-06-29 22:39:41Z peter $"); 40 41#if defined(LIBC_SCCS) && !defined(lint) 42#if 0 43static char sccsid[] = "@(#)kvm_sparc.c 8.1 (Berkeley) 6/4/93"; 44#endif 45#endif /* LIBC_SCCS and not lint */ 46 47/* 48 * Sparc machine dependent routines for kvm. Hopefully, the forthcoming 49 * vm code will one day obsolete this module. 50 */ 51 52#include <sys/param.h> 53#include <sys/user.h> 54#include <sys/proc.h> 55#include <sys/stat.h> 56#include <unistd.h> 57#include <nlist.h> 58#include <kvm.h> 59 60#include <vm/vm.h> 61#include <vm/vm_param.h> 62 63#include <limits.h> 64 65#include "kvm_private.h" 66 67#define NPMEG 128 68 69/* XXX from sparc/pmap.c */ 70#define MAXMEM (128 * 1024 * 1024) /* no more than 128 MB phys mem */ 71#define NPGBANK 16 /* 2^4 pages per bank (64K / bank) */ 72#define BSHIFT 4 /* log2(NPGBANK) */ 73#define BOFFSET (NPGBANK - 1) 74#define BTSIZE (MAXMEM / NBPG / NPGBANK) 75#define HWTOSW(pmap_stod, pg) (pmap_stod[(pg) >> BSHIFT] | ((pg) & BOFFSET)) 76 77struct vmstate { 78 pmeg_t segmap[NKSEG]; 79 int pmeg[NPMEG][NPTESG]; 80 int pmap_stod[BTSIZE]; /* dense to sparse */ 81}; 82 83void 84_kvm_freevtop(kd) 85 kvm_t *kd; 86{ 87 if (kd->vmst != 0) 88 free(kd->vmst); 89} 90 91int 92_kvm_initvtop(kd) 93 kvm_t *kd; 94{ 95 int i; 96 int off; 97 struct vmstate *vm; 98 struct stat st; 99 struct nlist nlist[2]; 100 101 vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm)); 102 if (vm == 0) 103 return (-1); 104 105 kd->vmst = vm; 106 107 if (fstat(kd->pmfd, &st) < 0) 108 return (-1); 109 /* 110 * Read segment table. 111 */ 112 off = st.st_size - ctob(btoc(sizeof(vm->segmap))); 113 errno = 0; 114 if (lseek(kd->pmfd, (off_t)off, 0) == -1 && errno != 0 || 115 read(kd->pmfd, (char *)vm->segmap, sizeof(vm->segmap)) < 0) { 116 _kvm_err(kd, kd->program, "cannot read segment map"); 117 return (-1); 118 } 119 /* 120 * Read PMEGs. 121 */ 122 off = st.st_size - ctob(btoc(sizeof(vm->pmeg)) + 123 btoc(sizeof(vm->segmap))); 124 errno = 0; 125 if (lseek(kd->pmfd, (off_t)off, 0) == -1 && errno != 0 || 126 read(kd->pmfd, (char *)vm->pmeg, sizeof(vm->pmeg)) < 0) { 127 _kvm_err(kd, kd->program, "cannot read PMEG table"); 128 return (-1); 129 } 130 /* 131 * Make pmap_stod be an identity map so we can bootstrap it in. 132 * We assume it's in the first contiguous chunk of physical memory. 133 */ 134 for (i = 0; i < BTSIZE; ++i) 135 vm->pmap_stod[i] = i << 4; 136 137 /* 138 * It's okay to do this nlist separately from the one kvm_getprocs() 139 * does, since the only time we could gain anything by combining 140 * them is if we do a kvm_getprocs() on a dead kernel, which is 141 * not too common. 142 */ 143 nlist[0].n_name = "_pmap_stod"; 144 nlist[1].n_name = 0; 145 if (kvm_nlist(kd, nlist) != 0) { 146 _kvm_err(kd, kd->program, "pmap_stod: no such symbol"); 147 return (-1); 148 } 149 if (kvm_read(kd, (u_long)nlist[0].n_value, 150 (char *)vm->pmap_stod, sizeof(vm->pmap_stod)) 151 != sizeof(vm->pmap_stod)) { 152 _kvm_err(kd, kd->program, "cannot read pmap_stod"); 153 return (-1); 154 } 155 return (0); 156} 157 158#define VA_OFF(va) (va & (NBPG - 1)) 159 160/* 161 * Translate a user virtual address to a physical address. 162 */ 163int 164_kvm_uvatop(kd, p, va, pa) 165 kvm_t *kd; 166 const struct proc *p; 167 u_long va; 168 u_long *pa; 169{ 170 int kva, pte; 171 int off, frame; 172 struct vmspace *vms = p->p_vmspace; 173 174 if ((u_long)vms < KERNBASE) { 175 _kvm_err(kd, kd->program, "_kvm_uvatop: corrupt proc"); 176 return (0); 177 } 178 if (va >= KERNBASE) 179 return (0); 180 /* 181 * Get the PTE. This takes two steps. We read the 182 * base address of the table, then we index it. 183 * Note that the index pte table is indexed by 184 * virtual segment rather than physical segment. 185 */ 186 kva = (u_long)&vms->vm_pmap.pm_rpte[VA_VSEG(va)]; 187 if (kvm_read(kd, kva, (char *)&kva, 4) != 4 || kva == 0) 188 goto invalid; 189 kva += sizeof(vms->vm_pmap.pm_rpte[0]) * VA_VPG(va); 190 if (kvm_read(kd, kva, (char *)&pte, 4) == 4 && (pte & PG_V)) { 191 off = VA_OFF(va); 192 /* 193 * /dev/mem adheres to the hardware model of physical memory 194 * (with holes in the address space), while crashdumps 195 * adhere to the contiguous software model. 196 */ 197 if (ISALIVE(kd)) 198 frame = pte & PG_PFNUM; 199 else 200 frame = HWTOSW(kd->vmst->pmap_stod, pte & PG_PFNUM); 201 *pa = (frame << PGSHIFT) | off; 202 return (NBPG - off); 203 } 204invalid: 205 _kvm_err(kd, 0, "invalid address (%x)", va); 206 return (0); 207} 208 209/* 210 * Translate a kernel virtual address to a physical address using the 211 * mapping information in kd->vm. Returns the result in pa, and returns 212 * the number of bytes that are contiguously available from this 213 * physical address. This routine is used only for crashdumps. 214 */ 215int 216_kvm_kvatop(kd, va, pa) 217 kvm_t *kd; 218 u_long va; 219 uint64_t *pa; 220{ 221 struct vmstate *vm; 222 int s; 223 int pte; 224 int off; 225 226 if (va >= KERNBASE) { 227 vm = kd->vmst; 228 s = vm->segmap[VA_VSEG(va) - NUSEG]; 229 pte = vm->pmeg[s][VA_VPG(va)]; 230 if ((pte & PG_V) != 0) { 231 off = VA_OFF(va); 232 *pa = (HWTOSW(vm->pmap_stod, pte & PG_PFNUM) 233 << PGSHIFT) | off; 234 235 return (NBPG - off); 236 } 237 } 238 _kvm_err(kd, 0, "invalid address (%x)", va); 239 return (0); 240} 241