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