1 /* $OpenBSD: kvm_i386.c,v 1.19 2006/06/09 09:46:04 mickey 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.19 2006/06/09 09:46:04 mickey 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 void *PTD; 72 paddr_t pg_frame; 73 paddr_t pt_mask; 74 int size; 75 int pte_size; 76 }; 77 78 #define pdei(vm,v) ((v) >> ((vm)->size == NBPG ? 22 : 21)) 79 #define ptei(vm,v) (((v) & (vm)->pt_mask) >> PGSHIFT) 80 #define PDE(vm,v) \ 81 ((vm)->size == NBPG ? ((u_int32_t *)(vm)->PTD)[pdei(vm,v)] : \ 82 ((u_int64_t *)(vm)->PTD)[pdei(vm,v)]) 83 #define PG_FRAME(vm) ((vm)->pg_frame) 84 #define pte_size(vm) ((vm)->pte_size) 85 86 void 87 _kvm_freevtop(kvm_t *kd) 88 { 89 if (kd->vmst != NULL) { 90 if (kd->vmst->PTD != NULL) 91 free(kd->vmst->PTD); 92 93 free(kd->vmst); 94 kd->vmst = NULL; 95 } 96 } 97 98 int 99 _kvm_initvtop(kvm_t *kd) 100 { 101 struct nlist nl[3]; 102 struct vmstate *vm; 103 paddr_t pa; 104 int ps; 105 106 vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm)); 107 if (vm == NULL) 108 return (-1); 109 kd->vmst = vm; 110 111 vm->PTD = NULL; 112 113 nl[0].n_name = "_PTDpaddr"; 114 nl[1].n_name = "_PTDsize"; 115 nl[2].n_name = NULL; 116 117 if (kvm_nlist(kd, nl) != 0) { 118 _kvm_err(kd, kd->program, "bad namelist"); 119 return (-1); 120 } 121 122 if (_kvm_pread(kd, kd->pmfd, &ps, sizeof ps, 123 _kvm_pa2off(kd, nl[1].n_value - KERNBASE)) != sizeof ps) 124 return (-1); 125 126 pa = 0; 127 if (ps == NBPG) { 128 vm->pg_frame = 0xfffff000; 129 vm->pt_mask = 0x003ff000; 130 vm->pte_size = 4; 131 } else if (ps == NBPG * 4) { 132 vm->pg_frame = 0xffffff000ULL; 133 vm->pt_mask = 0x001ff000; 134 vm->pte_size = 8; 135 } else { 136 _kvm_err(kd, 0, "PTDsize is invalid"); 137 return (-1); 138 } 139 140 if (_kvm_pread(kd, kd->pmfd, &pa, vm->pte_size, 141 _kvm_pa2off(kd, nl[0].n_value - KERNBASE)) != vm->pte_size) 142 return (-1); 143 144 vm->PTD = _kvm_malloc(kd, ps); 145 if (vm->PTD == NULL) 146 return (-1); 147 148 if (_kvm_pread(kd, kd->pmfd, vm->PTD, ps, _kvm_pa2off(kd, pa)) != ps) { 149 free(vm->PTD); 150 vm->PTD = NULL; 151 return (-1); 152 } 153 154 vm->size = ps; 155 return (0); 156 } 157 158 /* 159 * Translate a kernel virtual address to a physical address. 160 */ 161 int 162 _kvm_kvatop(kvm_t *kd, u_long va, paddr_t *pa) 163 { 164 u_long offset; 165 struct vmstate *vm; 166 paddr_t pte, pte_pa; 167 168 if (!kd->vmst) { 169 _kvm_err(kd, 0, "vatop called before initvtop"); 170 return (0); 171 } 172 173 if (ISALIVE(kd)) { 174 _kvm_err(kd, 0, "vatop called in live kernel!"); 175 return (0); 176 } 177 178 vm = kd->vmst; 179 offset = va & PGOFSET; 180 181 /* 182 * If we are initializing (kernel page table descriptor pointer 183 * not yet set) * then return pa == va to avoid infinite recursion. 184 */ 185 if (vm->PTD == NULL) { 186 *pa = va; 187 return (NBPG - (int)offset); 188 } 189 if ((PDE(vm, va) & PG_V) == 0) 190 goto invalid; 191 192 pte_pa = (PDE(vm, va) & PG_FRAME(vm)) + 193 (ptei(vm, va) * pte_size(vm)); 194 195 /* XXX READ PHYSICAL XXX */ 196 pte = 0; 197 if (_kvm_pread(kd, kd->pmfd, &pte, pte_size(vm), 198 _kvm_pa2off(kd, pte_pa)) != pte_size(vm)) 199 goto invalid; 200 201 *pa = (pte & PG_FRAME(vm)) + offset; 202 return (NBPG - (int)offset); 203 204 invalid: 205 _kvm_err(kd, 0, "invalid address (%lx)", va); 206 return (0); 207 } 208 209 /* 210 * Translate a physical address to a file-offset in the crash-dump. 211 */ 212 off_t 213 _kvm_pa2off(kvm_t *kd, paddr_t pa) 214 { 215 return ((off_t)(kd->dump_off + pa)); 216 } 217