1 /* $OpenBSD: kvm_i386.c,v 1.20 2007/02/20 21:15:00 tom 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.20 2007/02/20 21:15:00 tom 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(kvm_t *kd) 76 { 77 if (kd->vmst != NULL) { 78 if (kd->vmst->PTD != NULL) 79 free(kd->vmst->PTD); 80 81 free(kd->vmst); 82 kd->vmst = NULL; 83 } 84 } 85 86 int 87 _kvm_initvtop(kvm_t *kd) 88 { 89 struct nlist nl[2]; 90 struct vmstate *vm; 91 u_long pa; 92 93 vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm)); 94 if (vm == NULL) 95 return (-1); 96 kd->vmst = vm; 97 98 vm->PTD = NULL; 99 100 nl[0].n_name = "_PTDpaddr"; 101 nl[1].n_name = NULL; 102 103 if (kvm_nlist(kd, nl) != 0) { 104 _kvm_err(kd, kd->program, "bad namelist"); 105 return (-1); 106 } 107 108 if (_kvm_pread(kd, kd->pmfd, &pa, sizeof pa, 109 (off_t)_kvm_pa2off(kd, nl[0].n_value - KERNBASE)) != sizeof pa) 110 goto invalid; 111 112 vm->PTD = (pd_entry_t *)_kvm_malloc(kd, NBPG); 113 114 if (_kvm_pread(kd, kd->pmfd, vm->PTD, NBPG, 115 (off_t)_kvm_pa2off(kd, pa)) != NBPG) 116 goto invalid; 117 118 return (0); 119 120 invalid: 121 if (vm->PTD != NULL) { 122 free(vm->PTD); 123 vm->PTD = NULL; 124 } 125 return (-1); 126 } 127 128 /* 129 * Translate a kernel virtual address to a physical address. 130 */ 131 int 132 _kvm_kvatop(kvm_t *kd, u_long va, paddr_t *pa) 133 { 134 u_long offset, pte_pa; 135 struct vmstate *vm; 136 pt_entry_t pte; 137 138 if (!kd->vmst) { 139 _kvm_err(kd, 0, "vatop called before initvtop"); 140 return (0); 141 } 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 == NULL) { 156 *pa = va; 157 return (NBPG - (int)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 165 /* XXX READ PHYSICAL XXX */ 166 if (_kvm_pread(kd, kd->pmfd, &pte, sizeof pte, 167 (off_t)_kvm_pa2off(kd, pte_pa)) != sizeof pte) 168 goto invalid; 169 170 *pa = (pte & PG_FRAME) + offset; 171 return (NBPG - (int)offset); 172 173 invalid: 174 _kvm_err(kd, 0, "invalid address (%lx)", va); 175 return (0); 176 } 177 178 /* 179 * Translate a physical address to a file-offset in the crash-dump. 180 */ 181 off_t 182 _kvm_pa2off(kvm_t *kd, paddr_t pa) 183 { 184 return ((off_t)(kd->dump_off + pa)); 185 } 186