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 #if defined(LIBC_SCCS) && !defined(lint) 39 static char sccsid[] = "@(#)kvm_sparc.c 8.1 (Berkeley) 6/4/93"; 40 #endif /* LIBC_SCCS and not lint */ 41 42 /* 43 * Sparc machine dependent routines for kvm. Hopefully, the forthcoming 44 * vm code will one day obsolete this module. 45 */ 46 47 #include <sys/param.h> 48 #include <sys/user.h> 49 #include <sys/proc.h> 50 #include <sys/stat.h> 51 #include <unistd.h> 52 #include <nlist.h> 53 #include <kvm.h> 54 55 #include <vm/vm.h> 56 #include <vm/vm_param.h> 57 58 #include <limits.h> 59 #include <db.h> 60 61 #include "kvm_private.h" 62 63 #define NPMEG 128 64 65 /* XXX from sparc/pmap.c */ 66 #define MAXMEM (128 * 1024 * 1024) /* no more than 128 MB phys mem */ 67 #define NPGBANK 16 /* 2^4 pages per bank (64K / bank) */ 68 #define BSHIFT 4 /* log2(NPGBANK) */ 69 #define BOFFSET (NPGBANK - 1) 70 #define BTSIZE (MAXMEM / NBPG / NPGBANK) 71 #define HWTOSW(pmap_stod, pg) (pmap_stod[(pg) >> BSHIFT] | ((pg) & BOFFSET)) 72 73 struct vmstate { 74 pmeg_t segmap[NKSEG]; 75 int pmeg[NPMEG][NPTESG]; 76 int pmap_stod[BTSIZE]; /* dense to sparse */ 77 }; 78 79 void 80 _kvm_freevtop(kd) 81 kvm_t *kd; 82 { 83 if (kd->vmst != 0) 84 free(kd->vmst); 85 } 86 87 int 88 _kvm_initvtop(kd) 89 kvm_t *kd; 90 { 91 register int i; 92 register int off; 93 register struct vmstate *vm; 94 struct stat st; 95 struct nlist nlist[2]; 96 97 vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm)); 98 if (vm == 0) 99 return (-1); 100 101 kd->vmst = vm; 102 103 if (fstat(kd->pmfd, &st) < 0) 104 return (-1); 105 /* 106 * Read segment table. 107 */ 108 off = st.st_size - ctob(btoc(sizeof(vm->segmap))); 109 errno = 0; 110 if (lseek(kd->pmfd, (off_t)off, 0) == -1 && errno != 0 || 111 read(kd->pmfd, (char *)vm->segmap, sizeof(vm->segmap)) < 0) { 112 _kvm_err(kd, kd->program, "cannot read segment map"); 113 return (-1); 114 } 115 /* 116 * Read PMEGs. 117 */ 118 off = st.st_size - ctob(btoc(sizeof(vm->pmeg)) + 119 btoc(sizeof(vm->segmap))); 120 errno = 0; 121 if (lseek(kd->pmfd, (off_t)off, 0) == -1 && errno != 0 || 122 read(kd->pmfd, (char *)vm->pmeg, sizeof(vm->pmeg)) < 0) { 123 _kvm_err(kd, kd->program, "cannot read PMEG table"); 124 return (-1); 125 } 126 /* 127 * Make pmap_stod be an identity map so we can bootstrap it in. 128 * We assume it's in the first contiguous chunk of physical memory. 129 */ 130 for (i = 0; i < BTSIZE; ++i) 131 vm->pmap_stod[i] = i << 4; 132 133 /* 134 * It's okay to do this nlist separately from the one kvm_getprocs() 135 * does, since the only time we could gain anything by combining 136 * them is if we do a kvm_getprocs() on a dead kernel, which is 137 * not too common. 138 */ 139 nlist[0].n_name = "_pmap_stod"; 140 nlist[1].n_name = 0; 141 if (kvm_nlist(kd, nlist) != 0) { 142 _kvm_err(kd, kd->program, "pmap_stod: no such symbol"); 143 return (-1); 144 } 145 if (kvm_read(kd, (u_long)nlist[0].n_value, 146 (char *)vm->pmap_stod, sizeof(vm->pmap_stod)) 147 != sizeof(vm->pmap_stod)) { 148 _kvm_err(kd, kd->program, "cannot read pmap_stod"); 149 return (-1); 150 } 151 return (0); 152 } 153 154 #define VA_OFF(va) (va & (NBPG - 1)) 155 156 /* 157 * Translate a user virtual address to a physical address. 158 */ 159 int 160 _kvm_uvatop(kd, p, va, pa) 161 kvm_t *kd; 162 const struct proc *p; 163 u_long va; 164 u_long *pa; 165 { 166 int kva, pte; 167 register int off, frame; 168 register struct vmspace *vms = p->p_vmspace; 169 struct usegmap *usp; 170 171 if ((u_long)vms < KERNBASE) { 172 _kvm_err(kd, kd->program, "_kvm_uvatop: corrupt proc"); 173 return (0); 174 } 175 if (va >= KERNBASE) 176 return (0); 177 /* 178 * Get the PTE. This takes two steps. We read the 179 * base address of the table, then we index it. 180 * Note that the index pte table is indexed by 181 * virtual segment rather than physical segment. 182 */ 183 kva = (u_long)&vms->vm_pmap.pm_segstore; 184 if (kvm_read(kd, kva, (char *)&usp, 4) != 4) 185 goto invalid; 186 kva = (u_long)&usp->us_pte[VA_VSEG(va)]; 187 if (kvm_read(kd, kva, (char *)&kva, 4) != 4 || kva == 0) 188 goto invalid; 189 kva += sizeof(usp->us_pte[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 } 204 invalid: 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 */ 215 int 216 _kvm_kvatop(kd, va, pa) 217 kvm_t *kd; 218 u_long va; 219 u_long *pa; 220 { 221 register struct vmstate *vm; 222 register int s; 223 register int pte; 224 register 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