kvm_sparc.c revision 83551
11573Srgrimes/*-
21573Srgrimes * Copyright (c) 1992, 1993
31573Srgrimes *	The Regents of the University of California.  All rights reserved.
41573Srgrimes *
51573Srgrimes * This code is derived from software developed by the Computer Systems
61573Srgrimes * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
71573Srgrimes * BG 91-66 and contributed to Berkeley.
81573Srgrimes *
91573Srgrimes * Redistribution and use in source and binary forms, with or without
101573Srgrimes * modification, are permitted provided that the following conditions
111573Srgrimes * are met:
121573Srgrimes * 1. Redistributions of source code must retain the above copyright
131573Srgrimes *    notice, this list of conditions and the following disclaimer.
141573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
151573Srgrimes *    notice, this list of conditions and the following disclaimer in the
161573Srgrimes *    documentation and/or other materials provided with the distribution.
171573Srgrimes * 3. All advertising materials mentioning features or use of this software
181573Srgrimes *    must display the following acknowledgement:
191573Srgrimes *	This product includes software developed by the University of
201573Srgrimes *	California, Berkeley and its contributors.
211573Srgrimes * 4. Neither the name of the University nor the names of its contributors
221573Srgrimes *    may be used to endorse or promote products derived from this software
231573Srgrimes *    without specific prior written permission.
241573Srgrimes *
251573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
261573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
271573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
281573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
291573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
301573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
311573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
321573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
331573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
341573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
351573Srgrimes * SUCH DAMAGE.
361573Srgrimes */
371573Srgrimes
3883551Sdillon#include <sys/cdefs.h>
3983551Sdillon__FBSDID("$FreeBSD: head/lib/libkvm/kvm_sparc.c 83551 2001-09-16 21:35:07Z dillon $");
4083551Sdillon
411573Srgrimes#if defined(LIBC_SCCS) && !defined(lint)
4255127Speter#if 0
431573Srgrimesstatic char sccsid[] = "@(#)kvm_sparc.c	8.1 (Berkeley) 6/4/93";
4455127Speter#endif
451573Srgrimes#endif /* LIBC_SCCS and not lint */
461573Srgrimes
471573Srgrimes/*
488870Srgrimes * Sparc machine dependent routines for kvm.  Hopefully, the forthcoming
491573Srgrimes * vm code will one day obsolete this module.
501573Srgrimes */
511573Srgrimes
521573Srgrimes#include <sys/param.h>
531573Srgrimes#include <sys/user.h>
541573Srgrimes#include <sys/proc.h>
551573Srgrimes#include <sys/stat.h>
561573Srgrimes#include <unistd.h>
571573Srgrimes#include <nlist.h>
581573Srgrimes#include <kvm.h>
591573Srgrimes
601573Srgrimes#include <vm/vm.h>
611573Srgrimes#include <vm/vm_param.h>
621573Srgrimes
631573Srgrimes#include <limits.h>
641573Srgrimes
651573Srgrimes#include "kvm_private.h"
661573Srgrimes
671573Srgrimes#define NPMEG 128
681573Srgrimes
691573Srgrimes/* XXX from sparc/pmap.c */
701573Srgrimes#define MAXMEM  (128 * 1024 * 1024)     /* no more than 128 MB phys mem */
711573Srgrimes#define NPGBANK 16                      /* 2^4 pages per bank (64K / bank) */
721573Srgrimes#define BSHIFT  4                       /* log2(NPGBANK) */
731573Srgrimes#define BOFFSET (NPGBANK - 1)
741573Srgrimes#define BTSIZE  (MAXMEM / NBPG / NPGBANK)
751573Srgrimes#define HWTOSW(pmap_stod, pg) (pmap_stod[(pg) >> BSHIFT] | ((pg) & BOFFSET))
761573Srgrimes
771573Srgrimesstruct vmstate {
781573Srgrimes	pmeg_t segmap[NKSEG];
791573Srgrimes	int pmeg[NPMEG][NPTESG];
801573Srgrimes	int pmap_stod[BTSIZE];              /* dense to sparse */
811573Srgrimes};
821573Srgrimes
831573Srgrimesvoid
841573Srgrimes_kvm_freevtop(kd)
851573Srgrimes	kvm_t *kd;
861573Srgrimes{
871573Srgrimes	if (kd->vmst != 0)
881573Srgrimes		free(kd->vmst);
891573Srgrimes}
901573Srgrimes
911573Srgrimesint
921573Srgrimes_kvm_initvtop(kd)
931573Srgrimes	kvm_t *kd;
941573Srgrimes{
951573Srgrimes	register int i;
961573Srgrimes	register int off;
971573Srgrimes	register struct vmstate *vm;
981573Srgrimes	struct stat st;
991573Srgrimes	struct nlist nlist[2];
1001573Srgrimes
1011573Srgrimes	vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm));
1021573Srgrimes	if (vm == 0)
1031573Srgrimes		return (-1);
1041573Srgrimes
1051573Srgrimes	kd->vmst = vm;
1061573Srgrimes
1071573Srgrimes	if (fstat(kd->pmfd, &st) < 0)
1081573Srgrimes		return (-1);
1091573Srgrimes	/*
1101573Srgrimes	 * Read segment table.
1111573Srgrimes	 */
1121573Srgrimes	off = st.st_size - ctob(btoc(sizeof(vm->segmap)));
1131573Srgrimes	errno = 0;
1148870Srgrimes	if (lseek(kd->pmfd, (off_t)off, 0) == -1 && errno != 0 ||
1151573Srgrimes	    read(kd->pmfd, (char *)vm->segmap, sizeof(vm->segmap)) < 0) {
1161573Srgrimes		_kvm_err(kd, kd->program, "cannot read segment map");
1171573Srgrimes		return (-1);
1181573Srgrimes	}
1191573Srgrimes	/*
1201573Srgrimes	 * Read PMEGs.
1211573Srgrimes	 */
1221573Srgrimes	off = st.st_size - ctob(btoc(sizeof(vm->pmeg)) +
1231573Srgrimes	    btoc(sizeof(vm->segmap)));
1241573Srgrimes	errno = 0;
1258870Srgrimes	if (lseek(kd->pmfd, (off_t)off, 0) == -1 && errno != 0 ||
1261573Srgrimes	    read(kd->pmfd, (char *)vm->pmeg, sizeof(vm->pmeg)) < 0) {
1271573Srgrimes		_kvm_err(kd, kd->program, "cannot read PMEG table");
1281573Srgrimes		return (-1);
1291573Srgrimes	}
1301573Srgrimes	/*
1311573Srgrimes	 * Make pmap_stod be an identity map so we can bootstrap it in.
1321573Srgrimes	 * We assume it's in the first contiguous chunk of physical memory.
1331573Srgrimes	 */
1348870Srgrimes	for (i = 0; i < BTSIZE; ++i)
1351573Srgrimes		vm->pmap_stod[i] = i << 4;
1361573Srgrimes
1371573Srgrimes	/*
1381573Srgrimes	 * It's okay to do this nlist separately from the one kvm_getprocs()
1391573Srgrimes	 * does, since the only time we could gain anything by combining
1401573Srgrimes	 * them is if we do a kvm_getprocs() on a dead kernel, which is
1411573Srgrimes	 * not too common.
1421573Srgrimes	 */
1431573Srgrimes	nlist[0].n_name = "_pmap_stod";
1441573Srgrimes	nlist[1].n_name = 0;
1451573Srgrimes	if (kvm_nlist(kd, nlist) != 0) {
1461573Srgrimes		_kvm_err(kd, kd->program, "pmap_stod: no such symbol");
1471573Srgrimes		return (-1);
1481573Srgrimes	}
1498870Srgrimes	if (kvm_read(kd, (u_long)nlist[0].n_value,
1501573Srgrimes		     (char *)vm->pmap_stod, sizeof(vm->pmap_stod))
1511573Srgrimes	    != sizeof(vm->pmap_stod)) {
1521573Srgrimes		_kvm_err(kd, kd->program, "cannot read pmap_stod");
1531573Srgrimes		return (-1);
1541573Srgrimes	}
1551573Srgrimes	return (0);
1561573Srgrimes}
1571573Srgrimes
1581573Srgrimes#define VA_OFF(va) (va & (NBPG - 1))
1591573Srgrimes
1601573Srgrimes/*
1611573Srgrimes * Translate a user virtual address to a physical address.
1621573Srgrimes */
1631573Srgrimesint
1641573Srgrimes_kvm_uvatop(kd, p, va, pa)
1651573Srgrimes	kvm_t *kd;
1661573Srgrimes	const struct proc *p;
1671573Srgrimes	u_long va;
1681573Srgrimes	u_long *pa;
1691573Srgrimes{
1701573Srgrimes	int kva, pte;
1711573Srgrimes	register int off, frame;
1721573Srgrimes	register struct vmspace *vms = p->p_vmspace;
1731573Srgrimes
1741573Srgrimes	if ((u_long)vms < KERNBASE) {
1751573Srgrimes		_kvm_err(kd, kd->program, "_kvm_uvatop: corrupt proc");
1761573Srgrimes		return (0);
1771573Srgrimes	}
1781573Srgrimes	if (va >= KERNBASE)
1791573Srgrimes		return (0);
1801573Srgrimes	/*
1811573Srgrimes	 * Get the PTE.  This takes two steps.  We read the
1821573Srgrimes	 * base address of the table, then we index it.
1831573Srgrimes	 * Note that the index pte table is indexed by
1841573Srgrimes	 * virtual segment rather than physical segment.
1851573Srgrimes	 */
1861573Srgrimes	kva = (u_long)&vms->vm_pmap.pm_rpte[VA_VSEG(va)];
1871573Srgrimes	if (kvm_read(kd, kva, (char *)&kva, 4) != 4 || kva == 0)
1881573Srgrimes		goto invalid;
1891573Srgrimes	kva += sizeof(vms->vm_pmap.pm_rpte[0]) * VA_VPG(va);
1901573Srgrimes	if (kvm_read(kd, kva, (char *)&pte, 4) == 4 && (pte & PG_V)) {
1911573Srgrimes		off = VA_OFF(va);
1921573Srgrimes		/*
1931573Srgrimes		 * /dev/mem adheres to the hardware model of physical memory
1941573Srgrimes		 * (with holes in the address space), while crashdumps
1951573Srgrimes		 * adhere to the contiguous software model.
1961573Srgrimes		 */
1971573Srgrimes		if (ISALIVE(kd))
1981573Srgrimes			frame = pte & PG_PFNUM;
1991573Srgrimes		else
2001573Srgrimes			frame = HWTOSW(kd->vmst->pmap_stod, pte & PG_PFNUM);
2018870Srgrimes		*pa = (frame << PGSHIFT) | off;
2021573Srgrimes		return (NBPG - off);
2031573Srgrimes	}
2041573Srgrimesinvalid:
2051573Srgrimes	_kvm_err(kd, 0, "invalid address (%x)", va);
2061573Srgrimes	return (0);
2071573Srgrimes}
2081573Srgrimes
2091573Srgrimes/*
2101573Srgrimes * Translate a kernel virtual address to a physical address using the
2111573Srgrimes * mapping information in kd->vm.  Returns the result in pa, and returns
2128870Srgrimes * the number of bytes that are contiguously available from this
2131573Srgrimes * physical address.  This routine is used only for crashdumps.
2141573Srgrimes */
2151573Srgrimesint
2161573Srgrimes_kvm_kvatop(kd, va, pa)
2171573Srgrimes	kvm_t *kd;
2181573Srgrimes	u_long va;
2191573Srgrimes	u_long *pa;
2201573Srgrimes{
2211573Srgrimes	register struct vmstate *vm;
2221573Srgrimes	register int s;
2231573Srgrimes	register int pte;
2241573Srgrimes	register int off;
2251573Srgrimes
2261573Srgrimes	if (va >= KERNBASE) {
2271573Srgrimes		vm = kd->vmst;
2281573Srgrimes		s = vm->segmap[VA_VSEG(va) - NUSEG];
2291573Srgrimes		pte = vm->pmeg[s][VA_VPG(va)];
2301573Srgrimes		if ((pte & PG_V) != 0) {
2311573Srgrimes			off = VA_OFF(va);
2321573Srgrimes			*pa = (HWTOSW(vm->pmap_stod, pte & PG_PFNUM)
2331573Srgrimes			       << PGSHIFT) | off;
2341573Srgrimes
2351573Srgrimes			return (NBPG - off);
2361573Srgrimes		}
2371573Srgrimes	}
2381573Srgrimes	_kvm_err(kd, 0, "invalid address (%x)", va);
2391573Srgrimes	return (0);
2401573Srgrimes}
241