1 /* $OpenBSD: kvm_i386.c,v 1.24 2013/11/01 15:57:56 deraadt 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 /* 38 * i386 machine dependent routines for kvm. Hopefully, the forthcoming 39 * vm code will one day obsolete this module. 40 */ 41 42 #include <sys/param.h> 43 #include <sys/proc.h> 44 #include <sys/stat.h> 45 #include <stdlib.h> 46 #include <unistd.h> 47 #include <nlist.h> 48 #include <kvm.h> 49 50 #include <uvm/uvm_extern.h> 51 #include <machine/vmparam.h> 52 #include <machine/pmap.h> 53 54 #include <limits.h> 55 #include <db.h> 56 57 #include "kvm_private.h" 58 59 #include <machine/pte.h> 60 61 struct vmstate { 62 pd_entry_t *PTD; 63 }; 64 65 void 66 _kvm_freevtop(kvm_t *kd) 67 { 68 if (kd->vmst != NULL) { 69 if (kd->vmst->PTD != NULL) 70 free(kd->vmst->PTD); 71 72 free(kd->vmst); 73 kd->vmst = NULL; 74 } 75 } 76 77 int 78 _kvm_initvtop(kvm_t *kd) 79 { 80 struct nlist nl[2]; 81 struct vmstate *vm; 82 u_long pa; 83 84 vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm)); 85 if (vm == NULL) 86 return (-1); 87 kd->vmst = vm; 88 89 vm->PTD = NULL; 90 91 nl[0].n_name = "_PTDpaddr"; 92 nl[1].n_name = NULL; 93 94 if (kvm_nlist(kd, nl) != 0) { 95 _kvm_err(kd, kd->program, "bad namelist"); 96 return (-1); 97 } 98 99 if (_kvm_pread(kd, kd->pmfd, &pa, sizeof pa, 100 (off_t)_kvm_pa2off(kd, nl[0].n_value - KERNBASE)) != sizeof pa) 101 goto invalid; 102 103 vm->PTD = (pd_entry_t *)_kvm_malloc(kd, kd->nbpg); 104 105 if (_kvm_pread(kd, kd->pmfd, vm->PTD, kd->nbpg, 106 (off_t)_kvm_pa2off(kd, pa)) != kd->nbpg) 107 goto invalid; 108 109 return (0); 110 111 invalid: 112 if (vm->PTD != NULL) { 113 free(vm->PTD); 114 vm->PTD = NULL; 115 } 116 return (-1); 117 } 118 119 /* 120 * Translate a kernel virtual address to a physical address. 121 */ 122 int 123 _kvm_kvatop(kvm_t *kd, u_long va, paddr_t *pa) 124 { 125 u_long offset, pte_pa; 126 struct vmstate *vm; 127 pt_entry_t pte; 128 129 if (!kd->vmst) { 130 _kvm_err(kd, 0, "vatop called before initvtop"); 131 return (0); 132 } 133 134 if (ISALIVE(kd)) { 135 _kvm_err(kd, 0, "vatop called in live kernel!"); 136 return (0); 137 } 138 139 vm = kd->vmst; 140 offset = va & (kd->nbpg - 1); 141 142 /* 143 * If we are initializing (kernel page table descriptor pointer 144 * not yet set) * then return pa == va to avoid infinite recursion. 145 */ 146 if (vm->PTD == NULL) { 147 *pa = va; 148 return (kd->nbpg - (int)offset); 149 } 150 if ((vm->PTD[pdei(va)] & PG_V) == 0) 151 goto invalid; 152 153 pte_pa = (vm->PTD[pdei(va)] & PG_FRAME) + 154 (ptei(va) * sizeof(pt_entry_t)); 155 156 /* XXX READ PHYSICAL XXX */ 157 if (_kvm_pread(kd, kd->pmfd, &pte, sizeof pte, 158 (off_t)_kvm_pa2off(kd, pte_pa)) != sizeof pte) 159 goto invalid; 160 161 *pa = (pte & PG_FRAME) + offset; 162 return (kd->nbpg - (int)offset); 163 164 invalid: 165 _kvm_err(kd, 0, "invalid address (%lx)", va); 166 return (0); 167 } 168 169 /* 170 * Translate a physical address to a file-offset in the crash-dump. 171 */ 172 off_t 173 _kvm_pa2off(kvm_t *kd, paddr_t pa) 174 { 175 return ((off_t)(kd->dump_off + pa)); 176 } 177