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 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34#include <sys/cdefs.h> 35__FBSDID("$FreeBSD$"); 36 37#if defined(LIBC_SCCS) && !defined(lint) 38#if 0 39static char sccsid[] = "@(#)kvm_sparc.c 8.1 (Berkeley) 6/4/93"; 40#endif 41#endif /* LIBC_SCCS and not lint */ 42 43/* 44 * Sparc machine dependent routines for kvm. Hopefully, the forthcoming 45 * vm code will one day obsolete this module. 46 */ 47 48#include <sys/param.h> 49#include <sys/user.h> 50#include <sys/proc.h> 51#include <sys/stat.h> 52#include <unistd.h> 53#include <nlist.h> 54#include <kvm.h> 55 56#include <vm/vm.h> 57#include <vm/vm_param.h> 58 59#include <limits.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 73struct vmstate { 74 pmeg_t segmap[NKSEG]; 75 int pmeg[NPMEG][NPTESG]; 76 int pmap_stod[BTSIZE]; /* dense to sparse */ 77}; 78 79void 80_kvm_freevtop(kd) 81 kvm_t *kd; 82{ 83 if (kd->vmst != 0) 84 free(kd->vmst); 85} 86 87int 88_kvm_initvtop(kd) 89 kvm_t *kd; 90{ 91 int i; 92 int off; 93 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 */ 159int 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 int off, frame; 168 struct vmspace *vms = p->p_vmspace; 169 170 if ((u_long)vms < KERNBASE) { 171 _kvm_err(kd, kd->program, "_kvm_uvatop: corrupt proc"); 172 return (0); 173 } 174 if (va >= KERNBASE) 175 return (0); 176 /* 177 * Get the PTE. This takes two steps. We read the 178 * base address of the table, then we index it. 179 * Note that the index pte table is indexed by 180 * virtual segment rather than physical segment. 181 */ 182 kva = (u_long)&vms->vm_pmap.pm_rpte[VA_VSEG(va)]; 183 if (kvm_read(kd, kva, (char *)&kva, 4) != 4 || kva == 0) 184 goto invalid; 185 kva += sizeof(vms->vm_pmap.pm_rpte[0]) * VA_VPG(va); 186 if (kvm_read(kd, kva, (char *)&pte, 4) == 4 && (pte & PG_V)) { 187 off = VA_OFF(va); 188 /* 189 * /dev/mem adheres to the hardware model of physical memory 190 * (with holes in the address space), while crashdumps 191 * adhere to the contiguous software model. 192 */ 193 if (ISALIVE(kd)) 194 frame = pte & PG_PFNUM; 195 else 196 frame = HWTOSW(kd->vmst->pmap_stod, pte & PG_PFNUM); 197 *pa = (frame << PGSHIFT) | off; 198 return (NBPG - off); 199 } 200invalid: 201 _kvm_err(kd, 0, "invalid address (%x)", va); 202 return (0); 203} 204 205/* 206 * Translate a kernel virtual address to a physical address using the 207 * mapping information in kd->vm. Returns the result in pa, and returns 208 * the number of bytes that are contiguously available from this 209 * physical address. This routine is used only for crashdumps. 210 */ 211int 212_kvm_kvatop(kd, va, pa) 213 kvm_t *kd; 214 u_long va; 215 uint64_t *pa; 216{ 217 struct vmstate *vm; 218 int s; 219 int pte; 220 int off; 221 222 if (va >= KERNBASE) { 223 vm = kd->vmst; 224 s = vm->segmap[VA_VSEG(va) - NUSEG]; 225 pte = vm->pmeg[s][VA_VPG(va)]; 226 if ((pte & PG_V) != 0) { 227 off = VA_OFF(va); 228 *pa = (HWTOSW(vm->pmap_stod, pte & PG_PFNUM) 229 << PGSHIFT) | off; 230 231 return (NBPG - off); 232 } 233 } 234 _kvm_err(kd, 0, "invalid address (%x)", va); 235 return (0); 236} 237