Home | History | Annotate | Line # | Download | only in libkvm
kvm_i386.c revision 1.1
      1  1.1  cgd /*-
      2  1.1  cgd  * Copyright (c) 1989, 1992, 1993
      3  1.1  cgd  *	The Regents of the University of California.  All rights reserved.
      4  1.1  cgd  *
      5  1.1  cgd  * This code is derived from software developed by the Computer Systems
      6  1.1  cgd  * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
      7  1.1  cgd  * BG 91-66 and contributed to Berkeley.
      8  1.1  cgd  *
      9  1.1  cgd  * Redistribution and use in source and binary forms, with or without
     10  1.1  cgd  * modification, are permitted provided that the following conditions
     11  1.1  cgd  * are met:
     12  1.1  cgd  * 1. Redistributions of source code must retain the above copyright
     13  1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     14  1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  cgd  *    documentation and/or other materials provided with the distribution.
     17  1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     18  1.1  cgd  *    must display the following acknowledgement:
     19  1.1  cgd  *	This product includes software developed by the University of
     20  1.1  cgd  *	California, Berkeley and its contributors.
     21  1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     22  1.1  cgd  *    may be used to endorse or promote products derived from this software
     23  1.1  cgd  *    without specific prior written permission.
     24  1.1  cgd  *
     25  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26  1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29  1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  1.1  cgd  * SUCH DAMAGE.
     36  1.1  cgd  */
     37  1.1  cgd 
     38  1.1  cgd #if defined(LIBC_SCCS) && !defined(lint)
     39  1.1  cgd /* from: static char sccsid[] = "@(#)kvm_hp300.c	8.1 (Berkeley) 6/4/93"; */
     40  1.1  cgd static char *rcsid = "$Id: kvm_i386.c,v 1.1 1994/05/09 03:15:53 cgd Exp $";
     41  1.1  cgd #endif /* LIBC_SCCS and not lint */
     42  1.1  cgd 
     43  1.1  cgd /*
     44  1.1  cgd  * i386 machine dependent routines for kvm.  Hopefully, the forthcoming
     45  1.1  cgd  * vm code will one day obsolete this module.
     46  1.1  cgd  */
     47  1.1  cgd 
     48  1.1  cgd #include <sys/param.h>
     49  1.1  cgd #include <sys/user.h>
     50  1.1  cgd #include <sys/proc.h>
     51  1.1  cgd #include <sys/stat.h>
     52  1.1  cgd #include <unistd.h>
     53  1.1  cgd #include <nlist.h>
     54  1.1  cgd #include <kvm.h>
     55  1.1  cgd 
     56  1.1  cgd #include <vm/vm.h>
     57  1.1  cgd #include <vm/vm_param.h>
     58  1.1  cgd 
     59  1.1  cgd #include <limits.h>
     60  1.1  cgd #include <db.h>
     61  1.1  cgd 
     62  1.1  cgd #include "kvm_private.h"
     63  1.1  cgd 
     64  1.1  cgd #include <machine/pte.h>
     65  1.1  cgd 
     66  1.1  cgd #ifndef btop
     67  1.1  cgd #define	btop(x)		(((unsigned)(x)) >> PGSHIFT)	/* XXX */
     68  1.1  cgd #define	ptob(x)		((caddr_t)((x) << PGSHIFT))	/* XXX */
     69  1.1  cgd #endif
     70  1.1  cgd 
     71  1.1  cgd struct vmstate {
     72  1.1  cgd 	struct pde **IdlePTD;
     73  1.1  cgd 	struct pde *PTD;
     74  1.1  cgd };
     75  1.1  cgd 
     76  1.1  cgd #define KREAD(kd, addr, p)\
     77  1.1  cgd 	(kvm_read(kd, addr, (char *)(p), sizeof(*(p))) != sizeof(*(p)))
     78  1.1  cgd 
     79  1.1  cgd void
     80  1.1  cgd _kvm_freevtop(kd)
     81  1.1  cgd 	kvm_t *kd;
     82  1.1  cgd {
     83  1.1  cgd 	if (kd->vmst->PTD != 0)
     84  1.1  cgd 		free(kd->vmst->PTD);
     85  1.1  cgd 
     86  1.1  cgd 	if (kd->vmst != 0)
     87  1.1  cgd 		free(kd->vmst);
     88  1.1  cgd }
     89  1.1  cgd 
     90  1.1  cgd int
     91  1.1  cgd _kvm_initvtop(kd)
     92  1.1  cgd 	kvm_t *kd;
     93  1.1  cgd {
     94  1.1  cgd 	struct vmstate *vm;
     95  1.1  cgd 	struct nlist nlist[2];
     96  1.1  cgd 
     97  1.1  cgd 	vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm));
     98  1.1  cgd 	if (vm == 0)
     99  1.1  cgd 		return (-1);
    100  1.1  cgd 	kd->vmst = vm;
    101  1.1  cgd 
    102  1.1  cgd 	nlist[0].n_name = "_IdlePTD";
    103  1.1  cgd 	nlist[1].n_name = 0;
    104  1.1  cgd 
    105  1.1  cgd 	if (kvm_nlist(kd, nlist) != 0) {
    106  1.1  cgd 		_kvm_err(kd, kd->program, "bad namelist");
    107  1.1  cgd 		return (-1);
    108  1.1  cgd 	}
    109  1.1  cgd 	vm->IdlePTD = 0;
    110  1.1  cgd 	vm->PTD = 0;
    111  1.1  cgd 	if (KREAD(kd, (u_long)nlist[0].n_value, &vm->IdlePTD)) {
    112  1.1  cgd 		_kvm_err(kd, kd->program, "cannot read IdlePTD");
    113  1.1  cgd 		return (-1);
    114  1.1  cgd 	}
    115  1.1  cgd 	vm->PTD = (struct pde *)_kvm_malloc(kd, NBPG);
    116  1.1  cgd 	if ((kvm_read(kd, (u_long)vm->IdlePTD, &vm->PTD, NBPG)) != NBPG) {
    117  1.1  cgd 		_kvm_err(kd, kd->program, "cannot read PTD");
    118  1.1  cgd 		return (-1);
    119  1.1  cgd 	}
    120  1.1  cgd 	return (0);
    121  1.1  cgd }
    122  1.1  cgd 
    123  1.1  cgd /*
    124  1.1  cgd  * Translate a kernel virtual address to a physical address.
    125  1.1  cgd  */
    126  1.1  cgd int
    127  1.1  cgd _kvm_kvatop(kd, va, pa)
    128  1.1  cgd 	kvm_t *kd;
    129  1.1  cgd 	u_long va;
    130  1.1  cgd 	u_long *pa;
    131  1.1  cgd {
    132  1.1  cgd 	struct vmstate *vm;
    133  1.1  cgd 	u_long offset;
    134  1.1  cgd 
    135  1.1  cgd 	if (ISALIVE(kd)) {
    136  1.1  cgd 		_kvm_err(kd, 0, "vatop called in live kernel!");
    137  1.1  cgd 		return(0);
    138  1.1  cgd 	}
    139  1.1  cgd 	vm = kd->vmst;
    140  1.1  cgd 	offset = va & PGOFSET;
    141  1.1  cgd 
    142  1.1  cgd invalid:
    143  1.1  cgd 	_kvm_err(kd, 0, "invalid address (%x)", va);
    144  1.1  cgd 	return (0);
    145  1.1  cgd }
    146  1.1  cgd 
    147  1.1  cgd /*
    148  1.1  cgd  * Translate a user virtual address to a physical address.
    149  1.1  cgd  */
    150  1.1  cgd int
    151  1.1  cgd _kvm_uvatop(kd, p, va, pa)
    152  1.1  cgd 	kvm_t *kd;
    153  1.1  cgd 	const struct proc *p;
    154  1.1  cgd 	u_long va;
    155  1.1  cgd 	u_long *pa;
    156  1.1  cgd {
    157  1.1  cgd 	struct vmspace *vms = p->p_vmspace;
    158  1.1  cgd 	struct pde pde, *pdeloc;
    159  1.1  cgd 	struct pte pte, *pteloc;
    160  1.1  cgd 	u_long kva, offset;
    161  1.1  cgd 
    162  1.1  cgd 	if (va >= KERNBASE)
    163  1.1  cgd 		goto invalid;
    164  1.1  cgd 
    165  1.1  cgd 	kva = (u_long)vms->vm_pmap.pm_pdir;
    166  1.1  cgd 	if (kvm_read(kd, kva, (char *)&pdeloc, sizeof(pdeloc)) !=
    167  1.1  cgd 	    sizeof(pdeloc))
    168  1.1  cgd 		goto invalid;
    169  1.1  cgd 	if (pdeloc == 0)
    170  1.1  cgd 		goto invalid;
    171  1.1  cgd 
    172  1.1  cgd 	pdeloc += va >> PDSHIFT;
    173  1.1  cgd 	if (kvm_read(kd, (u_long)pdeloc, (char *)&pde, sizeof(pde)) !=
    174  1.1  cgd 	    sizeof(pde))
    175  1.1  cgd 		goto invalid;
    176  1.1  cgd 	if (pde.pd_v == 0)
    177  1.1  cgd 		goto invalid;
    178  1.1  cgd 
    179  1.1  cgd 	pteloc = ((struct pte *)pde.pd_pfnum) + btop(va & PT_MASK);
    180  1.1  cgd 	if (KREAD(kd, (u_long)pteloc, &pte))
    181  1.1  cgd 		goto invalid;
    182  1.1  cgd 	if (pte.pg_v == 0)
    183  1.1  cgd 		goto invalid;
    184  1.1  cgd 
    185  1.1  cgd 	offset = va & PGOFSET;
    186  1.1  cgd 	*pa = pte.pg_pfnum + offset;
    187  1.1  cgd 	return (NBPG - offset);
    188  1.1  cgd 
    189  1.1  cgd invalid:
    190  1.1  cgd 	_kvm_err(kd, 0, "invalid address (%x)", va);
    191  1.1  cgd 	return (0);
    192  1.1  cgd }
    193