Home | History | Annotate | Line # | Download | only in libkvm
kvm_i386.c revision 1.8
      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.8  mycroft static char *rcsid = "$Id: kvm_i386.c,v 1.8 1996/03/08 10:45:16 mycroft 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.6      cgd #include <stdlib.h>
     53  1.1      cgd #include <unistd.h>
     54  1.1      cgd #include <nlist.h>
     55  1.1      cgd #include <kvm.h>
     56  1.1      cgd 
     57  1.1      cgd #include <vm/vm.h>
     58  1.1      cgd #include <vm/vm_param.h>
     59  1.1      cgd 
     60  1.1      cgd #include <limits.h>
     61  1.1      cgd #include <db.h>
     62  1.1      cgd 
     63  1.1      cgd #include "kvm_private.h"
     64  1.1      cgd 
     65  1.1      cgd #include <machine/pte.h>
     66  1.1      cgd 
     67  1.1      cgd #ifndef btop
     68  1.1      cgd #define	btop(x)		(((unsigned)(x)) >> PGSHIFT)	/* XXX */
     69  1.1      cgd #define	ptob(x)		((caddr_t)((x) << PGSHIFT))	/* XXX */
     70  1.1      cgd #endif
     71  1.1      cgd 
     72  1.1      cgd struct vmstate {
     73  1.4  mycroft 	pd_entry_t *PTD;
     74  1.1      cgd };
     75  1.1      cgd 
     76  1.1      cgd void
     77  1.1      cgd _kvm_freevtop(kd)
     78  1.1      cgd 	kvm_t *kd;
     79  1.1      cgd {
     80  1.7      cgd 	if (kd->vmst != 0) {
     81  1.7      cgd 		if (kd->vmst->PTD != 0)
     82  1.7      cgd 			free(kd->vmst->PTD);
     83  1.1      cgd 
     84  1.1      cgd 		free(kd->vmst);
     85  1.7      cgd 	}
     86  1.1      cgd }
     87  1.1      cgd 
     88  1.1      cgd int
     89  1.1      cgd _kvm_initvtop(kd)
     90  1.1      cgd 	kvm_t *kd;
     91  1.1      cgd {
     92  1.1      cgd 	struct vmstate *vm;
     93  1.1      cgd 	struct nlist nlist[2];
     94  1.8  mycroft 	u_long pa;
     95  1.1      cgd 
     96  1.1      cgd 	vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm));
     97  1.1      cgd 	if (vm == 0)
     98  1.1      cgd 		return (-1);
     99  1.1      cgd 	kd->vmst = vm;
    100  1.1      cgd 
    101  1.6      cgd 	nlist[0].n_name = "_PTDpaddr";
    102  1.1      cgd 	nlist[1].n_name = 0;
    103  1.1      cgd 
    104  1.1      cgd 	if (kvm_nlist(kd, nlist) != 0) {
    105  1.1      cgd 		_kvm_err(kd, kd->program, "bad namelist");
    106  1.1      cgd 		return (-1);
    107  1.1      cgd 	}
    108  1.6      cgd 
    109  1.1      cgd 	vm->PTD = 0;
    110  1.8  mycroft 
    111  1.8  mycroft 	if (lseek(kd->pmfd, (off_t)(nlist[0].n_value - KERNBASE), 0) == -1 &&
    112  1.8  mycroft 	    errno != 0) {
    113  1.8  mycroft 		_kvm_syserr(kd, kd->program, "kvm_lseek");
    114  1.8  mycroft 		goto invalid;
    115  1.8  mycroft 	}
    116  1.8  mycroft 	if (read(kd->pmfd, &pa, sizeof pa) != sizeof pa) {
    117  1.8  mycroft 		_kvm_syserr(kd, kd->program, "kvm_read");
    118  1.8  mycroft 		goto invalid;
    119  1.1      cgd 	}
    120  1.6      cgd 
    121  1.8  mycroft 	vm->PTD = (pd_entry_t *)_kvm_malloc(kd, NBPG);
    122  1.8  mycroft 
    123  1.8  mycroft 	if (lseek(kd->pmfd, (off_t)pa, 0) == -1 && errno != 0) {
    124  1.8  mycroft 		_kvm_syserr(kd, kd->program, "kvm_lseek");
    125  1.8  mycroft 		goto invalid;
    126  1.8  mycroft 	}
    127  1.8  mycroft 	if (read(kd->pmfd, vm->PTD, NBPG) != NBPG) {
    128  1.8  mycroft 		_kvm_syserr(kd, kd->program, "kvm_read");
    129  1.8  mycroft 		goto invalid;
    130  1.1      cgd 	}
    131  1.8  mycroft 
    132  1.1      cgd 	return (0);
    133  1.8  mycroft 
    134  1.8  mycroft invalid:
    135  1.8  mycroft 	if (vm->PTD != 0)
    136  1.8  mycroft 		free(vm->PTD);
    137  1.8  mycroft 	return (-1);
    138  1.1      cgd }
    139  1.1      cgd 
    140  1.1      cgd /*
    141  1.1      cgd  * Translate a kernel virtual address to a physical address.
    142  1.1      cgd  */
    143  1.1      cgd int
    144  1.1      cgd _kvm_kvatop(kd, va, pa)
    145  1.1      cgd 	kvm_t *kd;
    146  1.1      cgd 	u_long va;
    147  1.1      cgd 	u_long *pa;
    148  1.1      cgd {
    149  1.1      cgd 	struct vmstate *vm;
    150  1.1      cgd 	u_long offset;
    151  1.6      cgd 	u_long pte_pa;
    152  1.6      cgd 	pt_entry_t pte;
    153  1.1      cgd 
    154  1.1      cgd 	if (ISALIVE(kd)) {
    155  1.1      cgd 		_kvm_err(kd, 0, "vatop called in live kernel!");
    156  1.1      cgd 		return(0);
    157  1.1      cgd 	}
    158  1.6      cgd 
    159  1.1      cgd 	vm = kd->vmst;
    160  1.1      cgd 	offset = va & PGOFSET;
    161  1.6      cgd 
    162  1.6      cgd         /*
    163  1.6      cgd          * If we are initializing (kernel page table descriptor pointer
    164  1.6      cgd 	 * not yet set) * then return pa == va to avoid infinite recursion.
    165  1.6      cgd          */
    166  1.6      cgd         if (vm->PTD == 0) {
    167  1.6      cgd                 *pa = va;
    168  1.6      cgd                 return (NBPG - offset);
    169  1.6      cgd         }
    170  1.6      cgd 	if ((vm->PTD[pdei(va)] & PG_V) == 0)
    171  1.6      cgd 		goto invalid;
    172  1.6      cgd 
    173  1.6      cgd 	pte_pa = (vm->PTD[pdei(va)] & PG_FRAME) +
    174  1.6      cgd 	    (ptei(va) * sizeof(pt_entry_t));
    175  1.6      cgd 	/* XXX READ PHYSICAL XXX */
    176  1.6      cgd 	{
    177  1.6      cgd 		if (lseek(kd->pmfd, (off_t)pte_pa, 0) == -1 && errno != 0) {
    178  1.8  mycroft 			_kvm_syserr(kd, kd->program, "kvm_lseek");
    179  1.6      cgd 			goto invalid;
    180  1.6      cgd 		}
    181  1.6      cgd 		if (read(kd->pmfd, &pte, sizeof pte) != sizeof pte) {
    182  1.6      cgd 			_kvm_syserr(kd, kd->program, "kvm_read");
    183  1.6      cgd 			goto invalid;
    184  1.6      cgd 		}
    185  1.6      cgd 	}
    186  1.6      cgd 
    187  1.6      cgd 	*pa = (pte & PG_FRAME) + offset;
    188  1.6      cgd 	return (NBPG - offset);
    189  1.1      cgd 
    190  1.1      cgd invalid:
    191  1.1      cgd 	_kvm_err(kd, 0, "invalid address (%x)", va);
    192  1.1      cgd 	return (0);
    193  1.1      cgd }
    194