Home | History | Annotate | Line # | Download | only in libkvm
kvm_sparc.c revision 1.4
      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 <sys/sysctl.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 #include <db.h>
     61 
     62 #include "kvm_private.h"
     63 
     64 #define NPMEG 128
     65 
     66 /* XXX from sparc/pmap.c */
     67 #define MAXMEM  (128 * 1024 * 1024)     /* no more than 128 MB phys mem */
     68 #define NPGBANK 16                      /* 2^4 pages per bank (64K / bank) */
     69 #define BSHIFT  4                       /* log2(NPGBANK) */
     70 #define BOFFSET (NPGBANK - 1)
     71 #define BTSIZE  (MAXMEM / 4096 / NPGBANK)
     72 #define HWTOSW(pmap_stod, pg) (pmap_stod[(pg) >> BSHIFT] | ((pg) & BOFFSET))
     73 
     74 struct vmstate {
     75 	pmeg_t segmap[NKSEG];
     76 	int *pmeg;
     77 	int pmap_stod[BTSIZE];              /* dense to sparse */
     78 };
     79 
     80 static int cputyp = -1;
     81 
     82 static int pgshift, nptesg;
     83 
     84 #define VA_VPG(va)	(cputyp==CPU_SUN4C ? VA_SUN4C_VPG(va) : VA_SUN4_VPG(va))
     85 
     86 static void
     87 _kvm_mustinit(kd)
     88 	kvm_t *kd;
     89 {
     90 	if (cputyp != -1)
     91 		return;
     92 	for (pgshift = 12; (1 << pgshift) != kd->nbpg; pgshift++)
     93 		;
     94 	nptesg = NBPSG / kd->nbpg;
     95 
     96 #if 1
     97 	if (cputyp == -1) {
     98 		if (kd->nbpg == 8192)
     99 			cputyp = CPU_SUN4;
    100 		else
    101 			cputyp = CPU_SUN4C;
    102 	}
    103 #endif
    104 }
    105 
    106 void
    107 _kvm_freevtop(kd)
    108 	kvm_t *kd;
    109 {
    110 	if (kd->vmst != 0) {
    111 		if (kd->vmst->pmeg != 0)
    112 			free(kd->vmst->pmeg);
    113 		free(kd->vmst);
    114 		kd->vmst  = 0;
    115 	}
    116 }
    117 
    118 int
    119 _kvm_initvtop(kd)
    120 	kvm_t *kd;
    121 {
    122 	register int i;
    123 	register int off;
    124 	register struct vmstate *vm;
    125 	struct stat st;
    126 	struct nlist nlist[2];
    127 
    128 	_kvm_mustinit(kd);
    129 
    130 	if (kd->vmst == 0) {
    131 		kd->vmst = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm));
    132 		if (kd->vmst == 0)
    133 			return (-1);
    134 		kd->vmst->pmeg = (int *)_kvm_malloc(kd,
    135 		    NPMEG * nptesg * sizeof(int));
    136 		if (kd->vmst->pmeg == 0) {
    137 			free(kd->vmst);
    138 			kd->vmst = 0;
    139 			return (-1);
    140 		}
    141 	}
    142 
    143 	if (fstat(kd->pmfd, &st) < 0)
    144 		return (-1);
    145 	/*
    146 	 * Read segment table.
    147 	 */
    148 	off = st.st_size - roundup(sizeof(vm->segmap), kd->nbpg);
    149 	errno = 0;
    150 	if (lseek(kd->pmfd, (off_t)off, 0) == -1 && errno != 0 ||
    151 	    read(kd->pmfd, (char *)vm->segmap, sizeof(vm->segmap)) < 0) {
    152 		_kvm_err(kd, kd->program, "cannot read segment map");
    153 		return (-1);
    154 	}
    155 	/*
    156 	 * Read PMEGs.
    157 	 */
    158 	off = st.st_size - roundup(NPMEG * nptesg * sizeof(int), kd->nbpg) +
    159 	    ((sizeof(vm->segmap) + kd->nbpg - 1) >> pgshift);
    160 	errno = 0;
    161 	if (lseek(kd->pmfd, (off_t)off, 0) == -1 && errno != 0 ||
    162 	    read(kd->pmfd, (char *)vm->pmeg, NPMEG * nptesg * sizeof(int)) < 0) {
    163 		_kvm_err(kd, kd->program, "cannot read PMEG table");
    164 		return (-1);
    165 	}
    166 	/*
    167 	 * Make pmap_stod be an identity map so we can bootstrap it in.
    168 	 * We assume it's in the first contiguous chunk of physical memory.
    169 	 */
    170 	for (i = 0; i < BTSIZE; ++i)
    171 		vm->pmap_stod[i] = i << 4;
    172 
    173 	/*
    174 	 * It's okay to do this nlist separately from the one kvm_getprocs()
    175 	 * does, since the only time we could gain anything by combining
    176 	 * them is if we do a kvm_getprocs() on a dead kernel, which is
    177 	 * not too common.
    178 	 */
    179 	nlist[0].n_name = "_pmap_stod";
    180 	nlist[1].n_name = 0;
    181 	(void)kvm_nlist(kd, nlist);
    182 
    183 	/*
    184 	 * a kernel compiled only for the sun4 will not contain the symbol
    185 	 * pmap_stod. Instead, we are happy to use the identity map
    186 	 * initialized earlier.
    187 	 * If we are not a sun4, the lack of this symbol is fatal.
    188 	 */
    189 	if (nlist[0].n_value != 0) {
    190 		if (kvm_read(kd, (u_long)nlist[0].n_value,
    191 		    (char *)vm->pmap_stod, sizeof(vm->pmap_stod))
    192 		    != sizeof(vm->pmap_stod)) {
    193 			_kvm_err(kd, kd->program, "cannot read pmap_stod");
    194 			return (-1);
    195 		}
    196 	} else {
    197 		if (cputyp != CPU_SUN4) {
    198 			_kvm_err(kd, kd->program, "pmap_stod: no such symbol");
    199 			return (-1);
    200 		}
    201 	}
    202 
    203 	return (0);
    204 }
    205 
    206 #define VA_OFF(va) (va & (kd->nbpg - 1))
    207 
    208 /*
    209  * Translate a user virtual address to a physical address.
    210  */
    211 int
    212 _kvm_uvatop(kd, p, va, pa)
    213 	kvm_t *kd;
    214 	const struct proc *p;
    215 	u_long va;
    216 	u_long *pa;
    217 {
    218 	int kva, pte;
    219 	register int off, frame;
    220 	register struct vmspace *vms = p->p_vmspace;
    221 	struct usegmap *usp;
    222 
    223 	_kvm_mustinit(kd);
    224 
    225 	if ((u_long)vms < KERNBASE) {
    226 		_kvm_err(kd, kd->program, "_kvm_uvatop: corrupt proc");
    227 		return (0);
    228 	}
    229 	if (va >= KERNBASE)
    230 		return (0);
    231 	/*
    232 	 * Get the PTE.  This takes two steps.  We read the
    233 	 * base address of the table, then we index it.
    234 	 * Note that the index pte table is indexed by
    235 	 * virtual segment rather than physical segment.
    236 	 */
    237 	kva = (u_long)&vms->vm_pmap.pm_segstore;
    238 	if (kvm_read(kd, kva, (char *)&usp, 4) != 4)
    239 		goto invalid;
    240 	kva = (u_long)&usp->us_pte[VA_VSEG(va)];
    241 	if (kvm_read(kd, kva, (char *)&kva, 4) != 4 || kva == 0)
    242 		goto invalid;
    243 	kva += sizeof(usp->us_pte[0]) * VA_VPG(va);
    244 	if (kvm_read(kd, kva, (char *)&pte, 4) == 4 && (pte & PG_V)) {
    245 		off = VA_OFF(va);
    246 		/*
    247 		 * /dev/mem adheres to the hardware model of physical memory
    248 		 * (with holes in the address space), while crashdumps
    249 		 * adhere to the contiguous software model.
    250 		 */
    251 		if (ISALIVE(kd))
    252 			frame = pte & PG_PFNUM;
    253 		else
    254 			frame = HWTOSW(kd->vmst->pmap_stod, pte & PG_PFNUM);
    255 		*pa = (frame << pgshift) | off;
    256 		return (kd->nbpg - off);
    257 	}
    258 invalid:
    259 	_kvm_err(kd, 0, "invalid address (%x)", va);
    260 	return (0);
    261 }
    262 
    263 /*
    264  * Translate a kernel virtual address to a physical address using the
    265  * mapping information in kd->vm.  Returns the result in pa, and returns
    266  * the number of bytes that are contiguously available from this
    267  * physical address.  This routine is used only for crashdumps.
    268  */
    269 int
    270 _kvm_kvatop(kd, va, pa)
    271 	kvm_t *kd;
    272 	u_long va;
    273 	u_long *pa;
    274 {
    275 	register struct vmstate *vm;
    276 	register int s;
    277 	register int pte;
    278 	register int off;
    279 
    280 	_kvm_mustinit(kd);
    281 
    282 	if (va >= KERNBASE) {
    283 		vm = kd->vmst;
    284 		s = vm->segmap[VA_VSEG(va) - NUSEG];
    285 		pte = vm->pmeg[VA_VPG(va) + nptesg * s];
    286 		if ((pte & PG_V) != 0) {
    287 			off = VA_OFF(va);
    288 			*pa = (HWTOSW(vm->pmap_stod, pte & PG_PFNUM)
    289 			       << pgshift) | off;
    290 
    291 			return (kd->nbpg - off);
    292 		}
    293 	}
    294 	_kvm_err(kd, 0, "invalid address (%x)", va);
    295 	return (0);
    296 }
    297 
    298 #if 0
    299 static int
    300 getcputyp()
    301 {
    302 	int mib[2];
    303 	size_t size;
    304 
    305 	mib[0] = CTL_HW;
    306 	mib[1] = HW_CLASS;
    307 	size = sizeof cputyp;
    308 	if (sysctl(mib, 2, &cputyp, &size, NULL, 0) == -1)
    309 		return (-1);
    310 }
    311 #endif
    312