Home | History | Annotate | Line # | Download | only in libkvm
kvm_mips.c revision 1.1.1.1
      1 /*-
      2  * Copyright (c) 1989, 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. Modified for MIPS by Ralph Campbell.
      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_mips.c	8.1 (Berkeley) 6/4/93";
     40 #endif /* LIBC_SCCS and not lint */
     41 /*
     42  * MIPS machine dependent routines for kvm.  Hopefully, the forthcoming
     43  * vm code will one day obsolete this module.
     44  */
     45 
     46 #include <sys/param.h>
     47 #include <sys/user.h>
     48 #include <sys/proc.h>
     49 #include <sys/stat.h>
     50 #include <unistd.h>
     51 #include <nlist.h>
     52 #include <kvm.h>
     53 
     54 #include <vm/vm.h>
     55 #include <vm/vm_param.h>
     56 
     57 #include <limits.h>
     58 #include <db.h>
     59 
     60 #include "kvm_private.h"
     61 
     62 #include <machine/machConst.h>
     63 #include <machine/pte.h>
     64 #include <machine/pmap.h>
     65 
     66 struct vmstate {
     67 	pt_entry_t	*Sysmap;
     68 	u_int		Sysmapsize;
     69 };
     70 
     71 #define KREAD(kd, addr, p)\
     72 	(kvm_read(kd, addr, (char *)(p), sizeof(*(p))) != sizeof(*(p)))
     73 
     74 void
     75 _kvm_freevtop(kd)
     76 	kvm_t *kd;
     77 {
     78 	if (kd->vmst != 0)
     79 		free(kd->vmst);
     80 }
     81 
     82 int
     83 _kvm_initvtop(kd)
     84 	kvm_t *kd;
     85 {
     86 	struct vmstate *vm;
     87 	struct nlist nlist[3];
     88 
     89 	vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm));
     90 	if (vm == 0)
     91 		return (-1);
     92 	kd->vmst = vm;
     93 
     94 	nlist[0].n_name = "Sysmap";
     95 	nlist[1].n_name = "Sysmapsize";
     96 	nlist[2].n_name = 0;
     97 
     98 	if (kvm_nlist(kd, nlist) != 0) {
     99 		_kvm_err(kd, kd->program, "bad namelist");
    100 		return (-1);
    101 	}
    102 	if (KREAD(kd, (u_long)nlist[0].n_value, &vm->Sysmap)) {
    103 		_kvm_err(kd, kd->program, "cannot read Sysmap");
    104 		return (-1);
    105 	}
    106 	if (KREAD(kd, (u_long)nlist[1].n_value, &vm->Sysmapsize)) {
    107 		_kvm_err(kd, kd->program, "cannot read mmutype");
    108 		return (-1);
    109 	}
    110 	return (0);
    111 }
    112 
    113 /*
    114  * Translate a kernel virtual address to a physical address.
    115  */
    116 int
    117 _kvm_kvatop(kd, va, pa)
    118 	kvm_t *kd;
    119 	u_long va;
    120 	u_long *pa;
    121 {
    122 	register struct vmstate *vm;
    123 	u_long pte, addr, offset;
    124 
    125 	if (ISALIVE(kd)) {
    126 		_kvm_err(kd, 0, "vatop called in live kernel!");
    127 		return((off_t)0);
    128 	}
    129 	vm = kd->vmst;
    130 	offset = va & PGOFSET;
    131 	/*
    132 	 * If we are initializing (kernel segment table pointer not yet set)
    133 	 * then return pa == va to avoid infinite recursion.
    134 	 */
    135 	if (vm->Sysmap == 0) {
    136 		*pa = va;
    137 		return (NBPG - offset);
    138 	}
    139 	if (va < KERNBASE ||
    140 	    va >= VM_MIN_KERNEL_ADDRESS + vm->Sysmapsize * NBPG)
    141 		goto invalid;
    142 	if (va < VM_MIN_KERNEL_ADDRESS) {
    143 		*pa = MACH_CACHED_TO_PHYS(va);
    144 		return (NBPG - offset);
    145 	}
    146 	addr = (u_long)(vm->Sysmap + ((va - VM_MIN_KERNEL_ADDRESS) >> PGSHIFT));
    147 	/*
    148 	 * Can't use KREAD to read kernel segment table entries.
    149 	 * Fortunately it is 1-to-1 mapped so we don't have to.
    150 	 */
    151 	if (lseek(kd->pmfd, (off_t)addr, 0) < 0 ||
    152 	    read(kd->pmfd, (char *)&pte, sizeof(pte)) < 0)
    153 		goto invalid;
    154 	if (!(pte & PG_V))
    155 		goto invalid;
    156 	*pa = (pte & PG_FRAME) | offset;
    157 	return (NBPG - offset);
    158 
    159 invalid:
    160 	_kvm_err(kd, 0, "invalid address (%x)", va);
    161 	return (0);
    162 }
    163 
    164 /*
    165  * Translate a user virtual address to a physical address.
    166  */
    167 int
    168 _kvm_uvatop(kd, p, va, pa)
    169 	kvm_t *kd;
    170 	const struct proc *p;
    171 	u_long va;
    172 	u_long *pa;
    173 {
    174 	register struct vmspace *vms = p->p_vmspace;
    175 	u_long kva, offset;
    176 
    177 	if (va >= KERNBASE)
    178 		goto invalid;
    179 
    180 	/* read the address of the first level table */
    181 	kva = (u_long)&vms->vm_pmap.pm_segtab;
    182 	if (kvm_read(kd, kva, (char *)&kva, sizeof(kva)) != sizeof(kva))
    183 		goto invalid;
    184 	if (kva == 0)
    185 		goto invalid;
    186 
    187 	/* read the address of the second level table */
    188 	kva += (va >> SEGSHIFT) * sizeof(caddr_t);
    189 	if (kvm_read(kd, kva, (char *)&kva, sizeof(kva)) != sizeof(kva))
    190 		goto invalid;
    191 	if (kva == 0)
    192 		goto invalid;
    193 
    194 	/* read the pte from the second level table */
    195 	kva += (va >> PGSHIFT) & (NPTEPG - 1);
    196 	if (kvm_read(kd, kva, (char *)&kva, sizeof(kva)) != sizeof(kva))
    197 		goto invalid;
    198 	if (!(kva & PG_V))
    199 		goto invalid;
    200 	offset = va & PGOFSET;
    201 	*pa = (kva & PG_FRAME) | offset;
    202 	return (NBPG - offset);
    203 
    204 invalid:
    205 	_kvm_err(kd, 0, "invalid address (%x)", va);
    206 	return (0);
    207 }
    208