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 * 4. Neither the name of the University nor the names of its contributors
181573Srgrimes *    may be used to endorse or promote products derived from this software
191573Srgrimes *    without specific prior written permission.
201573Srgrimes *
211573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311573Srgrimes * SUCH DAMAGE.
321573Srgrimes */
331573Srgrimes
3483551Sdillon#include <sys/cdefs.h>
3583551Sdillon__FBSDID("$FreeBSD$");
3683551Sdillon
371573Srgrimes#if defined(LIBC_SCCS) && !defined(lint)
3855127Speter#if 0
391573Srgrimesstatic char sccsid[] = "@(#)kvm_sparc.c	8.1 (Berkeley) 6/4/93";
4055127Speter#endif
411573Srgrimes#endif /* LIBC_SCCS and not lint */
421573Srgrimes
431573Srgrimes/*
448870Srgrimes * Sparc machine dependent routines for kvm.  Hopefully, the forthcoming
451573Srgrimes * vm code will one day obsolete this module.
461573Srgrimes */
471573Srgrimes
481573Srgrimes#include <sys/param.h>
491573Srgrimes#include <sys/user.h>
501573Srgrimes#include <sys/proc.h>
511573Srgrimes#include <sys/stat.h>
521573Srgrimes#include <unistd.h>
531573Srgrimes#include <nlist.h>
541573Srgrimes#include <kvm.h>
551573Srgrimes
561573Srgrimes#include <vm/vm.h>
571573Srgrimes#include <vm/vm_param.h>
581573Srgrimes
591573Srgrimes#include <limits.h>
601573Srgrimes
611573Srgrimes#include "kvm_private.h"
621573Srgrimes
631573Srgrimes#define NPMEG 128
641573Srgrimes
651573Srgrimes/* XXX from sparc/pmap.c */
661573Srgrimes#define MAXMEM  (128 * 1024 * 1024)     /* no more than 128 MB phys mem */
671573Srgrimes#define NPGBANK 16                      /* 2^4 pages per bank (64K / bank) */
681573Srgrimes#define BSHIFT  4                       /* log2(NPGBANK) */
691573Srgrimes#define BOFFSET (NPGBANK - 1)
701573Srgrimes#define BTSIZE  (MAXMEM / NBPG / NPGBANK)
711573Srgrimes#define HWTOSW(pmap_stod, pg) (pmap_stod[(pg) >> BSHIFT] | ((pg) & BOFFSET))
721573Srgrimes
731573Srgrimesstruct vmstate {
741573Srgrimes	pmeg_t segmap[NKSEG];
751573Srgrimes	int pmeg[NPMEG][NPTESG];
761573Srgrimes	int pmap_stod[BTSIZE];              /* dense to sparse */
771573Srgrimes};
781573Srgrimes
791573Srgrimesvoid
801573Srgrimes_kvm_freevtop(kd)
811573Srgrimes	kvm_t *kd;
821573Srgrimes{
831573Srgrimes	if (kd->vmst != 0)
841573Srgrimes		free(kd->vmst);
851573Srgrimes}
861573Srgrimes
871573Srgrimesint
881573Srgrimes_kvm_initvtop(kd)
891573Srgrimes	kvm_t *kd;
901573Srgrimes{
9192913Sobrien	int i;
9292913Sobrien	int off;
9392913Sobrien	struct vmstate *vm;
941573Srgrimes	struct stat st;
951573Srgrimes	struct nlist nlist[2];
961573Srgrimes
971573Srgrimes	vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm));
981573Srgrimes	if (vm == 0)
991573Srgrimes		return (-1);
1001573Srgrimes
1011573Srgrimes	kd->vmst = vm;
1021573Srgrimes
1031573Srgrimes	if (fstat(kd->pmfd, &st) < 0)
1041573Srgrimes		return (-1);
1051573Srgrimes	/*
1061573Srgrimes	 * Read segment table.
1071573Srgrimes	 */
1081573Srgrimes	off = st.st_size - ctob(btoc(sizeof(vm->segmap)));
1091573Srgrimes	errno = 0;
1108870Srgrimes	if (lseek(kd->pmfd, (off_t)off, 0) == -1 && errno != 0 ||
1111573Srgrimes	    read(kd->pmfd, (char *)vm->segmap, sizeof(vm->segmap)) < 0) {
1121573Srgrimes		_kvm_err(kd, kd->program, "cannot read segment map");
1131573Srgrimes		return (-1);
1141573Srgrimes	}
1151573Srgrimes	/*
1161573Srgrimes	 * Read PMEGs.
1171573Srgrimes	 */
1181573Srgrimes	off = st.st_size - ctob(btoc(sizeof(vm->pmeg)) +
1191573Srgrimes	    btoc(sizeof(vm->segmap)));
1201573Srgrimes	errno = 0;
1218870Srgrimes	if (lseek(kd->pmfd, (off_t)off, 0) == -1 && errno != 0 ||
1221573Srgrimes	    read(kd->pmfd, (char *)vm->pmeg, sizeof(vm->pmeg)) < 0) {
1231573Srgrimes		_kvm_err(kd, kd->program, "cannot read PMEG table");
1241573Srgrimes		return (-1);
1251573Srgrimes	}
1261573Srgrimes	/*
1271573Srgrimes	 * Make pmap_stod be an identity map so we can bootstrap it in.
1281573Srgrimes	 * We assume it's in the first contiguous chunk of physical memory.
1291573Srgrimes	 */
1308870Srgrimes	for (i = 0; i < BTSIZE; ++i)
1311573Srgrimes		vm->pmap_stod[i] = i << 4;
1321573Srgrimes
1331573Srgrimes	/*
1341573Srgrimes	 * It's okay to do this nlist separately from the one kvm_getprocs()
1351573Srgrimes	 * does, since the only time we could gain anything by combining
1361573Srgrimes	 * them is if we do a kvm_getprocs() on a dead kernel, which is
1371573Srgrimes	 * not too common.
1381573Srgrimes	 */
1391573Srgrimes	nlist[0].n_name = "_pmap_stod";
1401573Srgrimes	nlist[1].n_name = 0;
1411573Srgrimes	if (kvm_nlist(kd, nlist) != 0) {
1421573Srgrimes		_kvm_err(kd, kd->program, "pmap_stod: no such symbol");
1431573Srgrimes		return (-1);
1441573Srgrimes	}
1458870Srgrimes	if (kvm_read(kd, (u_long)nlist[0].n_value,
1461573Srgrimes		     (char *)vm->pmap_stod, sizeof(vm->pmap_stod))
1471573Srgrimes	    != sizeof(vm->pmap_stod)) {
1481573Srgrimes		_kvm_err(kd, kd->program, "cannot read pmap_stod");
1491573Srgrimes		return (-1);
1501573Srgrimes	}
1511573Srgrimes	return (0);
1521573Srgrimes}
1531573Srgrimes
1541573Srgrimes#define VA_OFF(va) (va & (NBPG - 1))
1551573Srgrimes
1561573Srgrimes/*
1571573Srgrimes * Translate a user virtual address to a physical address.
1581573Srgrimes */
1591573Srgrimesint
1601573Srgrimes_kvm_uvatop(kd, p, va, pa)
1611573Srgrimes	kvm_t *kd;
1621573Srgrimes	const struct proc *p;
1631573Srgrimes	u_long va;
1641573Srgrimes	u_long *pa;
1651573Srgrimes{
1661573Srgrimes	int kva, pte;
16792913Sobrien	int off, frame;
16892913Sobrien	struct vmspace *vms = p->p_vmspace;
1691573Srgrimes
1701573Srgrimes	if ((u_long)vms < KERNBASE) {
1711573Srgrimes		_kvm_err(kd, kd->program, "_kvm_uvatop: corrupt proc");
1721573Srgrimes		return (0);
1731573Srgrimes	}
1741573Srgrimes	if (va >= KERNBASE)
1751573Srgrimes		return (0);
1761573Srgrimes	/*
1771573Srgrimes	 * Get the PTE.  This takes two steps.  We read the
1781573Srgrimes	 * base address of the table, then we index it.
1791573Srgrimes	 * Note that the index pte table is indexed by
1801573Srgrimes	 * virtual segment rather than physical segment.
1811573Srgrimes	 */
1821573Srgrimes	kva = (u_long)&vms->vm_pmap.pm_rpte[VA_VSEG(va)];
1831573Srgrimes	if (kvm_read(kd, kva, (char *)&kva, 4) != 4 || kva == 0)
1841573Srgrimes		goto invalid;
1851573Srgrimes	kva += sizeof(vms->vm_pmap.pm_rpte[0]) * VA_VPG(va);
1861573Srgrimes	if (kvm_read(kd, kva, (char *)&pte, 4) == 4 && (pte & PG_V)) {
1871573Srgrimes		off = VA_OFF(va);
1881573Srgrimes		/*
1891573Srgrimes		 * /dev/mem adheres to the hardware model of physical memory
1901573Srgrimes		 * (with holes in the address space), while crashdumps
1911573Srgrimes		 * adhere to the contiguous software model.
1921573Srgrimes		 */
1931573Srgrimes		if (ISALIVE(kd))
1941573Srgrimes			frame = pte & PG_PFNUM;
1951573Srgrimes		else
1961573Srgrimes			frame = HWTOSW(kd->vmst->pmap_stod, pte & PG_PFNUM);
1978870Srgrimes		*pa = (frame << PGSHIFT) | off;
1981573Srgrimes		return (NBPG - off);
1991573Srgrimes	}
2001573Srgrimesinvalid:
2011573Srgrimes	_kvm_err(kd, 0, "invalid address (%x)", va);
2021573Srgrimes	return (0);
2031573Srgrimes}
2041573Srgrimes
2051573Srgrimes/*
2061573Srgrimes * Translate a kernel virtual address to a physical address using the
2071573Srgrimes * mapping information in kd->vm.  Returns the result in pa, and returns
2088870Srgrimes * the number of bytes that are contiguously available from this
2091573Srgrimes * physical address.  This routine is used only for crashdumps.
2101573Srgrimes */
2111573Srgrimesint
2121573Srgrimes_kvm_kvatop(kd, va, pa)
2131573Srgrimes	kvm_t *kd;
2141573Srgrimes	u_long va;
215147672Speter	uint64_t *pa;
2161573Srgrimes{
21792913Sobrien	struct vmstate *vm;
21892913Sobrien	int s;
21992913Sobrien	int pte;
22092913Sobrien	int off;
2211573Srgrimes
2221573Srgrimes	if (va >= KERNBASE) {
2231573Srgrimes		vm = kd->vmst;
2241573Srgrimes		s = vm->segmap[VA_VSEG(va) - NUSEG];
2251573Srgrimes		pte = vm->pmeg[s][VA_VPG(va)];
2261573Srgrimes		if ((pte & PG_V) != 0) {
2271573Srgrimes			off = VA_OFF(va);
2281573Srgrimes			*pa = (HWTOSW(vm->pmap_stod, pte & PG_PFNUM)
2291573Srgrimes			       << PGSHIFT) | off;
2301573Srgrimes
2311573Srgrimes			return (NBPG - off);
2321573Srgrimes		}
2331573Srgrimes	}
2341573Srgrimes	_kvm_err(kd, 0, "invalid address (%x)", va);
2351573Srgrimes	return (0);
2361573Srgrimes}
237