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