Home | History | Annotate | Line # | Download | only in libkvm
kvm_mips.c revision 1.7
      1  1.7     mikel /*	$NetBSD: kvm_mips.c,v 1.7 1997/08/15 02:22:00 mikel Exp $	*/
      2  1.3   thorpej 
      3  1.1       cgd /*-
      4  1.1       cgd  * Copyright (c) 1989, 1992, 1993
      5  1.1       cgd  *	The Regents of the University of California.  All rights reserved.
      6  1.1       cgd  *
      7  1.1       cgd  * This code is derived from software developed by the Computer Systems
      8  1.1       cgd  * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
      9  1.1       cgd  * BG 91-66 and contributed to Berkeley. Modified for MIPS by Ralph Campbell.
     10  1.1       cgd  *
     11  1.1       cgd  * Redistribution and use in source and binary forms, with or without
     12  1.1       cgd  * modification, are permitted provided that the following conditions
     13  1.1       cgd  * are met:
     14  1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     15  1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     16  1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     18  1.1       cgd  *    documentation and/or other materials provided with the distribution.
     19  1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     20  1.1       cgd  *    must display the following acknowledgement:
     21  1.1       cgd  *	This product includes software developed by the University of
     22  1.1       cgd  *	California, Berkeley and its contributors.
     23  1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     24  1.1       cgd  *    may be used to endorse or promote products derived from this software
     25  1.1       cgd  *    without specific prior written permission.
     26  1.1       cgd  *
     27  1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28  1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29  1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30  1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31  1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32  1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33  1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34  1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35  1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36  1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37  1.1       cgd  * SUCH DAMAGE.
     38  1.1       cgd  */
     39  1.1       cgd 
     40  1.7     mikel #include <sys/cdefs.h>
     41  1.1       cgd #if defined(LIBC_SCCS) && !defined(lint)
     42  1.3   thorpej #if 0
     43  1.1       cgd static char sccsid[] = "@(#)kvm_mips.c	8.1 (Berkeley) 6/4/93";
     44  1.3   thorpej #else
     45  1.7     mikel __RCSID("$NetBSD: kvm_mips.c,v 1.7 1997/08/15 02:22:00 mikel Exp $");
     46  1.3   thorpej #endif
     47  1.1       cgd #endif /* LIBC_SCCS and not lint */
     48  1.3   thorpej 
     49  1.1       cgd /*
     50  1.1       cgd  * MIPS machine dependent routines for kvm.  Hopefully, the forthcoming
     51  1.1       cgd  * vm code will one day obsolete this module.
     52  1.1       cgd  */
     53  1.1       cgd 
     54  1.1       cgd #include <sys/param.h>
     55  1.1       cgd #include <sys/user.h>
     56  1.1       cgd #include <sys/proc.h>
     57  1.1       cgd #include <sys/stat.h>
     58  1.1       cgd #include <unistd.h>
     59  1.1       cgd #include <nlist.h>
     60  1.1       cgd #include <kvm.h>
     61  1.1       cgd 
     62  1.1       cgd #include <vm/vm.h>
     63  1.1       cgd #include <vm/vm_param.h>
     64  1.1       cgd 
     65  1.1       cgd #include <limits.h>
     66  1.1       cgd #include <db.h>
     67  1.1       cgd 
     68  1.1       cgd #include "kvm_private.h"
     69  1.1       cgd 
     70  1.4  jonathan #include <mips/cpuregs.h>
     71  1.4  jonathan #include <mips/mips1_pte.h>
     72  1.4  jonathan #include <mips/mips3_pte.h>
     73  1.4  jonathan #include <mips/pmap.h>
     74  1.1       cgd 
     75  1.1       cgd struct vmstate {
     76  1.4  jonathan 	/*pt_entry_t*/u_int	*Sysmap;
     77  1.1       cgd 	u_int		Sysmapsize;
     78  1.4  jonathan 	u_int		cpu_arch;
     79  1.1       cgd };
     80  1.1       cgd 
     81  1.1       cgd #define KREAD(kd, addr, p)\
     82  1.1       cgd 	(kvm_read(kd, addr, (char *)(p), sizeof(*(p))) != sizeof(*(p)))
     83  1.1       cgd 
     84  1.1       cgd void
     85  1.1       cgd _kvm_freevtop(kd)
     86  1.1       cgd 	kvm_t *kd;
     87  1.1       cgd {
     88  1.1       cgd 	if (kd->vmst != 0)
     89  1.1       cgd 		free(kd->vmst);
     90  1.1       cgd }
     91  1.1       cgd 
     92  1.1       cgd int
     93  1.1       cgd _kvm_initvtop(kd)
     94  1.1       cgd 	kvm_t *kd;
     95  1.1       cgd {
     96  1.1       cgd 	struct vmstate *vm;
     97  1.4  jonathan 	struct nlist nlist[4];
     98  1.1       cgd 
     99  1.1       cgd 	vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm));
    100  1.1       cgd 	if (vm == 0)
    101  1.1       cgd 		return (-1);
    102  1.1       cgd 	kd->vmst = vm;
    103  1.1       cgd 
    104  1.4  jonathan 	nlist[0].n_name = "cpu_arch";
    105  1.4  jonathan 	nlist[1].n_name = "Sysmap";
    106  1.4  jonathan 	nlist[2].n_name = "Sysmapsize";
    107  1.4  jonathan 	nlist[3].n_name = 0;
    108  1.1       cgd 
    109  1.1       cgd 	if (kvm_nlist(kd, nlist) != 0) {
    110  1.1       cgd 		_kvm_err(kd, kd->program, "bad namelist");
    111  1.1       cgd 		return (-1);
    112  1.1       cgd 	}
    113  1.4  jonathan 	if (KREAD(kd, (u_long)nlist[0].n_value, &vm->cpu_arch)) {
    114  1.4  jonathan 		_kvm_err(kd, kd->program, "cannot read cpu_arch");
    115  1.4  jonathan 		return (-1);
    116  1.4  jonathan 	}
    117  1.4  jonathan 	if (KREAD(kd, (u_long)nlist[1].n_value, &vm->Sysmap)) {
    118  1.1       cgd 		_kvm_err(kd, kd->program, "cannot read Sysmap");
    119  1.1       cgd 		return (-1);
    120  1.1       cgd 	}
    121  1.4  jonathan 	if (KREAD(kd, (u_long)nlist[2].n_value, &vm->Sysmapsize)) {
    122  1.1       cgd 		_kvm_err(kd, kd->program, "cannot read mmutype");
    123  1.1       cgd 		return (-1);
    124  1.1       cgd 	}
    125  1.1       cgd 	return (0);
    126  1.1       cgd }
    127  1.1       cgd 
    128  1.1       cgd /*
    129  1.1       cgd  * Translate a kernel virtual address to a physical address.
    130  1.1       cgd  */
    131  1.1       cgd int
    132  1.1       cgd _kvm_kvatop(kd, va, pa)
    133  1.1       cgd 	kvm_t *kd;
    134  1.1       cgd 	u_long va;
    135  1.1       cgd 	u_long *pa;
    136  1.1       cgd {
    137  1.1       cgd 	register struct vmstate *vm;
    138  1.1       cgd 	u_long pte, addr, offset;
    139  1.1       cgd 
    140  1.4  jonathan 	u_int pgshift, pg_v, pg_frame;
    141  1.4  jonathan 
    142  1.1       cgd 	if (ISALIVE(kd)) {
    143  1.1       cgd 		_kvm_err(kd, 0, "vatop called in live kernel!");
    144  1.1       cgd 		return((off_t)0);
    145  1.1       cgd 	}
    146  1.4  jonathan 
    147  1.4  jonathan 	/* Compute TLB offsets for the level of mips cpu we're running on */
    148  1.4  jonathan 	switch (vm->cpu_arch) {
    149  1.4  jonathan 	case 1:
    150  1.4  jonathan 		pgshift = MIPS1_PG_SHIFT;
    151  1.4  jonathan 		pg_v = MIPS1_PG_V;
    152  1.4  jonathan 		pg_frame = MIPS1_PG_FRAME;
    153  1.4  jonathan 		break;
    154  1.4  jonathan 
    155  1.4  jonathan 	case 3:
    156  1.4  jonathan 		pgshift = MIPS3_PG_SHIFT;
    157  1.4  jonathan 		pg_v = MIPS3_PG_V;
    158  1.4  jonathan 		pg_frame = MIPS3_PG_FRAME;
    159  1.4  jonathan 		break;
    160  1.4  jonathan 
    161  1.4  jonathan 	default:
    162  1.4  jonathan 		_kvm_err(kd, 0, "unknown or unsupported mips CPU family!");
    163  1.4  jonathan 		return((off_t)0);
    164  1.4  jonathan 	}
    165  1.4  jonathan 
    166  1.1       cgd 	vm = kd->vmst;
    167  1.1       cgd 	offset = va & PGOFSET;
    168  1.1       cgd 	/*
    169  1.1       cgd 	 * If we are initializing (kernel segment table pointer not yet set)
    170  1.1       cgd 	 * then return pa == va to avoid infinite recursion.
    171  1.1       cgd 	 */
    172  1.1       cgd 	if (vm->Sysmap == 0) {
    173  1.1       cgd 		*pa = va;
    174  1.1       cgd 		return (NBPG - offset);
    175  1.1       cgd 	}
    176  1.1       cgd 	if (va < KERNBASE ||
    177  1.1       cgd 	    va >= VM_MIN_KERNEL_ADDRESS + vm->Sysmapsize * NBPG)
    178  1.1       cgd 		goto invalid;
    179  1.1       cgd 	if (va < VM_MIN_KERNEL_ADDRESS) {
    180  1.5  jonathan 		*pa = MIPS_KSEG0_TO_PHYS(va);
    181  1.1       cgd 		return (NBPG - offset);
    182  1.1       cgd 	}
    183  1.4  jonathan 	addr = (u_long)(vm->Sysmap +
    184  1.4  jonathan 	    ((va - VM_MIN_KERNEL_ADDRESS) >> pgshift));
    185  1.4  jonathan 
    186  1.1       cgd 	/*
    187  1.1       cgd 	 * Can't use KREAD to read kernel segment table entries.
    188  1.1       cgd 	 * Fortunately it is 1-to-1 mapped so we don't have to.
    189  1.1       cgd 	 */
    190  1.1       cgd 	if (lseek(kd->pmfd, (off_t)addr, 0) < 0 ||
    191  1.1       cgd 	    read(kd->pmfd, (char *)&pte, sizeof(pte)) < 0)
    192  1.1       cgd 		goto invalid;
    193  1.4  jonathan 	if (!(pte & pg_v))
    194  1.1       cgd 		goto invalid;
    195  1.4  jonathan 	*pa = (pte & pg_frame) | offset;
    196  1.1       cgd 	return (NBPG - offset);
    197  1.1       cgd 
    198  1.1       cgd invalid:
    199  1.1       cgd 	_kvm_err(kd, 0, "invalid address (%x)", va);
    200  1.6       gwr 	return (0);
    201  1.6       gwr }
    202  1.6       gwr 
    203  1.6       gwr /*
    204  1.6       gwr  * Machine-dependent initialization for ALL open kvm descriptors,
    205  1.6       gwr  * not just those for a kernel crash dump.  Some architectures
    206  1.6       gwr  * have to deal with these NOT being constants!  (i.e. m68k)
    207  1.6       gwr  */
    208  1.6       gwr int
    209  1.6       gwr _kvm_mdopen(kd)
    210  1.6       gwr 	kvm_t	*kd;
    211  1.6       gwr {
    212  1.6       gwr 
    213  1.6       gwr 	kd->usrstack = USRSTACK;
    214  1.6       gwr 	kd->min_uva = VM_MIN_ADDRESS;
    215  1.6       gwr 	kd->max_uva = VM_MAXUSER_ADDRESS;
    216  1.6       gwr 
    217  1.1       cgd 	return (0);
    218  1.1       cgd }
    219