Home | History | Annotate | Line # | Download | only in libkvm
kvm_i386.c revision 1.6
      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.
      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 /* from: static char sccsid[] = "@(#)kvm_hp300.c	8.1 (Berkeley) 6/4/93"; */
     40 static char *rcsid = "$Id: kvm_i386.c,v 1.6 1995/06/26 13:19:27 cgd Exp $";
     41 #endif /* LIBC_SCCS and not lint */
     42 
     43 /*
     44  * i386 machine dependent routines for kvm.  Hopefully, the forthcoming
     45  * vm code will one day obsolete this module.
     46  */
     47 
     48 #include <sys/param.h>
     49 #include <sys/user.h>
     50 #include <sys/proc.h>
     51 #include <sys/stat.h>
     52 #include <stdlib.h>
     53 #include <unistd.h>
     54 #include <nlist.h>
     55 #include <kvm.h>
     56 
     57 #include <vm/vm.h>
     58 #include <vm/vm_param.h>
     59 
     60 #include <limits.h>
     61 #include <db.h>
     62 
     63 #include "kvm_private.h"
     64 
     65 #include <machine/pte.h>
     66 
     67 #ifndef btop
     68 #define	btop(x)		(((unsigned)(x)) >> PGSHIFT)	/* XXX */
     69 #define	ptob(x)		((caddr_t)((x) << PGSHIFT))	/* XXX */
     70 #endif
     71 
     72 struct vmstate {
     73 	pd_entry_t **PTDpaddr;
     74 	pd_entry_t *PTD;
     75 };
     76 
     77 #define KREAD(kd, addr, p)\
     78 	(kvm_read(kd, addr, (char *)(p), sizeof(*(p))) != sizeof(*(p)))
     79 
     80 void
     81 _kvm_freevtop(kd)
     82 	kvm_t *kd;
     83 {
     84 	if (kd->vmst->PTD != 0)
     85 		free(kd->vmst->PTD);
     86 
     87 	if (kd->vmst != 0)
     88 		free(kd->vmst);
     89 }
     90 
     91 int
     92 _kvm_initvtop(kd)
     93 	kvm_t *kd;
     94 {
     95 	struct vmstate *vm;
     96 	struct nlist nlist[2];
     97 	pt_entry_t *tmpPTD;
     98 
     99 	vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm));
    100 	if (vm == 0)
    101 		return (-1);
    102 	kd->vmst = vm;
    103 
    104 	nlist[0].n_name = "_PTDpaddr";
    105 	nlist[1].n_name = 0;
    106 
    107 	if (kvm_nlist(kd, nlist) != 0) {
    108 		_kvm_err(kd, kd->program, "bad namelist");
    109 		return (-1);
    110 	}
    111 
    112 	vm->PTDpaddr = 0;
    113 	vm->PTD = 0;
    114 	if (KREAD(kd, (u_long)nlist[0].n_value - KERNBASE, &vm->PTDpaddr)) {
    115 		_kvm_err(kd, kd->program, "cannot read PTDpaddr");
    116 		return (-1);
    117 	}
    118 
    119 	tmpPTD = (pd_entry_t *)_kvm_malloc(kd, NBPG);
    120 	if ((kvm_read(kd, (u_long)vm->PTDpaddr, tmpPTD, NBPG)) != NBPG) {
    121 		free(tmpPTD);
    122 		_kvm_err(kd, kd->program, "cannot read PTD");
    123 		return (-1);
    124 	}
    125 	vm->PTD = tmpPTD;
    126 	return (0);
    127 }
    128 
    129 /*
    130  * Translate a kernel virtual address to a physical address.
    131  */
    132 int
    133 _kvm_kvatop(kd, va, pa)
    134 	kvm_t *kd;
    135 	u_long va;
    136 	u_long *pa;
    137 {
    138 	struct vmstate *vm;
    139 	u_long offset;
    140 	u_long pte_pa;
    141 	pt_entry_t pte;
    142 
    143 	if (ISALIVE(kd)) {
    144 		_kvm_err(kd, 0, "vatop called in live kernel!");
    145 		return(0);
    146 	}
    147 
    148 	vm = kd->vmst;
    149 	offset = va & PGOFSET;
    150 
    151         /*
    152          * If we are initializing (kernel page table descriptor pointer
    153 	 * not yet set) * then return pa == va to avoid infinite recursion.
    154          */
    155         if (vm->PTD == 0) {
    156                 *pa = va;
    157                 return (NBPG - offset);
    158         }
    159 	if ((vm->PTD[pdei(va)] & PG_V) == 0)
    160 		goto invalid;
    161 
    162 	pte_pa = (vm->PTD[pdei(va)] & PG_FRAME) +
    163 	    (ptei(va) * sizeof(pt_entry_t));
    164 	/* XXX READ PHYSICAL XXX */
    165 	{
    166 		if (lseek(kd->pmfd, (off_t)pte_pa, 0) == -1 && errno != 0) {
    167 			_kvm_syserr(kd, 0, "kvm_lseek");
    168 			goto invalid;
    169 		}
    170 		if (read(kd->pmfd, &pte, sizeof pte) != sizeof pte) {
    171 			_kvm_syserr(kd, kd->program, "kvm_read");
    172 			goto invalid;
    173 		}
    174 	}
    175 
    176 	*pa = (pte & PG_FRAME) + offset;
    177 	return (NBPG - offset);
    178 
    179 invalid:
    180 	_kvm_err(kd, 0, "invalid address (%x)", va);
    181 	return (0);
    182 }
    183