1 /*- 2 * Copyright (c) 1989, 1992, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software developed by the Computer Systems 6 * Engineering group at Lawrence Berkeley Laboratory under DARPA contract 7 * BG 91-66 and contributed to Berkeley. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #include <sys/cdefs.h> 35 __FBSDID("$FreeBSD$"); 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 #endif 41 #endif /* LIBC_SCCS and not lint */ 42 43 /* 44 * i386 machine dependent routines for kvm. Hopefully, the forthcoming 45 * vm code will one day obsolete this module. 46 */ 47 48 #include <sys/param.h> 49 #include <sys/endian.h> 50 #include <stdint.h> 51 #include <stdlib.h> 52 #include <string.h> 53 #include <unistd.h> 54 #include <kvm.h> 55 56 #ifdef __i386__ 57 #include <machine/vmparam.h> /* For KERNBASE. */ 58 #endif 59 60 #include <limits.h> 61 62 #include "kvm_private.h" 63 #include "kvm_i386.h" 64 65 struct vmstate { 66 void *PTD; 67 int pae; 68 size_t phnum; 69 GElf_Phdr *phdr; 70 }; 71 72 /* 73 * Translate a physical memory address to a file-offset in the crash-dump. 74 */ 75 static size_t 76 _kvm_pa2off(kvm_t *kd, uint64_t pa, off_t *ofs) 77 { 78 struct vmstate *vm = kd->vmst; 79 GElf_Phdr *p; 80 size_t n; 81 82 if (kd->rawdump) { 83 *ofs = pa; 84 return (I386_PAGE_SIZE - (pa & I386_PAGE_MASK)); 85 } 86 87 p = vm->phdr; 88 n = vm->phnum; 89 while (n && (pa < p->p_paddr || pa >= p->p_paddr + p->p_memsz)) 90 p++, n--; 91 if (n == 0) 92 return (0); 93 *ofs = (pa - p->p_paddr) + p->p_offset; 94 return (I386_PAGE_SIZE - (pa & I386_PAGE_MASK)); 95 } 96 97 static void 98 _i386_freevtop(kvm_t *kd) 99 { 100 struct vmstate *vm = kd->vmst; 101 102 if (vm->PTD) 103 free(vm->PTD); 104 free(vm->phdr); 105 free(vm); 106 kd->vmst = NULL; 107 } 108 109 static int 110 _i386_probe(kvm_t *kd) 111 { 112 113 return (_kvm_probe_elf_kernel(kd, ELFCLASS32, EM_386) && 114 !_kvm_is_minidump(kd)); 115 } 116 117 static int 118 _i386_initvtop(kvm_t *kd) 119 { 120 struct kvm_nlist nl[2]; 121 i386_physaddr_t pa; 122 kvaddr_t kernbase; 123 char *PTD; 124 int i; 125 126 kd->vmst = (struct vmstate *)_kvm_malloc(kd, sizeof(struct vmstate)); 127 if (kd->vmst == NULL) { 128 _kvm_err(kd, kd->program, "cannot allocate vm"); 129 return (-1); 130 } 131 kd->vmst->PTD = 0; 132 133 if (kd->rawdump == 0) { 134 if (_kvm_read_core_phdrs(kd, &kd->vmst->phnum, 135 &kd->vmst->phdr) == -1) 136 return (-1); 137 } 138 139 nl[0].n_name = "kernbase"; 140 nl[1].n_name = 0; 141 142 if (kvm_nlist2(kd, nl) != 0) { 143 #ifdef __i386__ 144 kernbase = KERNBASE; /* for old kernels */ 145 #else 146 _kvm_err(kd, kd->program, "cannot resolve kernbase"); 147 return (-1); 148 #endif 149 } else 150 kernbase = nl[0].n_value; 151 152 nl[0].n_name = "IdlePDPT"; 153 nl[1].n_name = 0; 154 155 if (kvm_nlist2(kd, nl) == 0) { 156 i386_physaddr_pae_t pa64; 157 158 if (kvm_read2(kd, (nl[0].n_value - kernbase), &pa, 159 sizeof(pa)) != sizeof(pa)) { 160 _kvm_err(kd, kd->program, "cannot read IdlePDPT"); 161 return (-1); 162 } 163 pa = le32toh(pa); 164 PTD = _kvm_malloc(kd, 4 * I386_PAGE_SIZE); 165 for (i = 0; i < 4; i++) { 166 if (kvm_read2(kd, pa + (i * sizeof(pa64)), &pa64, 167 sizeof(pa64)) != sizeof(pa64)) { 168 _kvm_err(kd, kd->program, "Cannot read PDPT"); 169 free(PTD); 170 return (-1); 171 } 172 pa64 = le64toh(pa64); 173 if (kvm_read2(kd, pa64 & I386_PG_FRAME_PAE, 174 PTD + (i * I386_PAGE_SIZE), I386_PAGE_SIZE) != 175 I386_PAGE_SIZE) { 176 _kvm_err(kd, kd->program, "cannot read PDPT"); 177 free(PTD); 178 return (-1); 179 } 180 } 181 kd->vmst->PTD = PTD; 182 kd->vmst->pae = 1; 183 } else { 184 nl[0].n_name = "IdlePTD"; 185 nl[1].n_name = 0; 186 187 if (kvm_nlist2(kd, nl) != 0) { 188 _kvm_err(kd, kd->program, "bad namelist"); 189 return (-1); 190 } 191 if (kvm_read2(kd, (nl[0].n_value - kernbase), &pa, 192 sizeof(pa)) != sizeof(pa)) { 193 _kvm_err(kd, kd->program, "cannot read IdlePTD"); 194 return (-1); 195 } 196 pa = le32toh(pa); 197 PTD = _kvm_malloc(kd, I386_PAGE_SIZE); 198 if (kvm_read2(kd, pa, PTD, I386_PAGE_SIZE) != I386_PAGE_SIZE) { 199 _kvm_err(kd, kd->program, "cannot read PTD"); 200 return (-1); 201 } 202 kd->vmst->PTD = PTD; 203 kd->vmst->pae = 0; 204 } 205 return (0); 206 } 207 208 static int 209 _i386_vatop(kvm_t *kd, kvaddr_t va, off_t *pa) 210 { 211 struct vmstate *vm; 212 i386_physaddr_t offset; 213 i386_physaddr_t pte_pa; 214 i386_pde_t pde; 215 i386_pte_t pte; 216 kvaddr_t pdeindex; 217 kvaddr_t pteindex; 218 size_t s; 219 i386_physaddr_t a; 220 off_t ofs; 221 i386_pde_t *PTD; 222 223 vm = kd->vmst; 224 PTD = (i386_pde_t *)vm->PTD; 225 offset = va & I386_PAGE_MASK; 226 227 /* 228 * If we are initializing (kernel page table descriptor pointer 229 * not yet set) then return pa == va to avoid infinite recursion. 230 */ 231 if (PTD == 0) { 232 s = _kvm_pa2off(kd, va, pa); 233 if (s == 0) { 234 _kvm_err(kd, kd->program, 235 "_i386_vatop: bootstrap data not in dump"); 236 goto invalid; 237 } else 238 return (I386_PAGE_SIZE - offset); 239 } 240 241 pdeindex = va >> I386_PDRSHIFT; 242 pde = le32toh(PTD[pdeindex]); 243 if ((pde & I386_PG_V) == 0) { 244 _kvm_err(kd, kd->program, "_i386_vatop: pde not valid"); 245 goto invalid; 246 } 247 248 if (pde & I386_PG_PS) { 249 /* 250 * No second-level page table; ptd describes one 4MB 251 * page. (We assume that the kernel wouldn't set 252 * PG_PS without enabling it cr0). 253 */ 254 offset = va & I386_PAGE_PS_MASK; 255 a = (pde & I386_PG_PS_FRAME) + offset; 256 s = _kvm_pa2off(kd, a, pa); 257 if (s == 0) { 258 _kvm_err(kd, kd->program, 259 "_i386_vatop: 4MB page address not in dump"); 260 goto invalid; 261 } 262 return (I386_NBPDR - offset); 263 } 264 265 pteindex = (va >> I386_PAGE_SHIFT) & (I386_NPTEPG - 1); 266 pte_pa = (pde & I386_PG_FRAME) + (pteindex * sizeof(pte)); 267 268 s = _kvm_pa2off(kd, pte_pa, &ofs); 269 if (s < sizeof(pte)) { 270 _kvm_err(kd, kd->program, "_i386_vatop: pte_pa not found"); 271 goto invalid; 272 } 273 274 /* XXX This has to be a physical address read, kvm_read is virtual */ 275 if (pread(kd->pmfd, &pte, sizeof(pte), ofs) != sizeof(pte)) { 276 _kvm_syserr(kd, kd->program, "_i386_vatop: pread"); 277 goto invalid; 278 } 279 pte = le32toh(pte); 280 if ((pte & I386_PG_V) == 0) { 281 _kvm_err(kd, kd->program, "_kvm_kvatop: pte not valid"); 282 goto invalid; 283 } 284 285 a = (pte & I386_PG_FRAME) + offset; 286 s = _kvm_pa2off(kd, a, pa); 287 if (s == 0) { 288 _kvm_err(kd, kd->program, "_i386_vatop: address not in dump"); 289 goto invalid; 290 } else 291 return (I386_PAGE_SIZE - offset); 292 293 invalid: 294 _kvm_err(kd, 0, "invalid address (0x%jx)", (uintmax_t)va); 295 return (0); 296 } 297 298 static int 299 _i386_vatop_pae(kvm_t *kd, kvaddr_t va, off_t *pa) 300 { 301 struct vmstate *vm; 302 i386_physaddr_pae_t offset; 303 i386_physaddr_pae_t pte_pa; 304 i386_pde_pae_t pde; 305 i386_pte_pae_t pte; 306 kvaddr_t pdeindex; 307 kvaddr_t pteindex; 308 size_t s; 309 i386_physaddr_pae_t a; 310 off_t ofs; 311 i386_pde_pae_t *PTD; 312 313 vm = kd->vmst; 314 PTD = (i386_pde_pae_t *)vm->PTD; 315 offset = va & I386_PAGE_MASK; 316 317 /* 318 * If we are initializing (kernel page table descriptor pointer 319 * not yet set) then return pa == va to avoid infinite recursion. 320 */ 321 if (PTD == 0) { 322 s = _kvm_pa2off(kd, va, pa); 323 if (s == 0) { 324 _kvm_err(kd, kd->program, 325 "_i386_vatop_pae: bootstrap data not in dump"); 326 goto invalid; 327 } else 328 return (I386_PAGE_SIZE - offset); 329 } 330 331 pdeindex = va >> I386_PDRSHIFT_PAE; 332 pde = le64toh(PTD[pdeindex]); 333 if ((pde & I386_PG_V) == 0) { 334 _kvm_err(kd, kd->program, "_kvm_kvatop_pae: pde not valid"); 335 goto invalid; 336 } 337 338 if (pde & I386_PG_PS) { 339 /* 340 * No second-level page table; ptd describes one 2MB 341 * page. (We assume that the kernel wouldn't set 342 * PG_PS without enabling it cr0). 343 */ 344 offset = va & I386_PAGE_PS_MASK_PAE; 345 a = (pde & I386_PG_PS_FRAME_PAE) + offset; 346 s = _kvm_pa2off(kd, a, pa); 347 if (s == 0) { 348 _kvm_err(kd, kd->program, 349 "_i386_vatop: 2MB page address not in dump"); 350 goto invalid; 351 } 352 return (I386_NBPDR_PAE - offset); 353 } 354 355 pteindex = (va >> I386_PAGE_SHIFT) & (I386_NPTEPG_PAE - 1); 356 pte_pa = (pde & I386_PG_FRAME_PAE) + (pteindex * sizeof(pde)); 357 358 s = _kvm_pa2off(kd, pte_pa, &ofs); 359 if (s < sizeof(pte)) { 360 _kvm_err(kd, kd->program, "_i386_vatop_pae: pdpe_pa not found"); 361 goto invalid; 362 } 363 364 /* XXX This has to be a physical address read, kvm_read is virtual */ 365 if (pread(kd->pmfd, &pte, sizeof(pte), ofs) != sizeof(pte)) { 366 _kvm_syserr(kd, kd->program, "_i386_vatop_pae: read"); 367 goto invalid; 368 } 369 pte = le64toh(pte); 370 if ((pte & I386_PG_V) == 0) { 371 _kvm_err(kd, kd->program, "_i386_vatop_pae: pte not valid"); 372 goto invalid; 373 } 374 375 a = (pte & I386_PG_FRAME_PAE) + offset; 376 s = _kvm_pa2off(kd, a, pa); 377 if (s == 0) { 378 _kvm_err(kd, kd->program, 379 "_i386_vatop_pae: address not in dump"); 380 goto invalid; 381 } else 382 return (I386_PAGE_SIZE - offset); 383 384 invalid: 385 _kvm_err(kd, 0, "invalid address (0x%jx)", (uintmax_t)va); 386 return (0); 387 } 388 389 static int 390 _i386_kvatop(kvm_t *kd, kvaddr_t va, off_t *pa) 391 { 392 393 if (ISALIVE(kd)) { 394 _kvm_err(kd, 0, "vatop called in live kernel!"); 395 return (0); 396 } 397 if (kd->vmst->pae) 398 return (_i386_vatop_pae(kd, va, pa)); 399 else 400 return (_i386_vatop(kd, va, pa)); 401 } 402 403 int 404 _i386_native(kvm_t *kd) 405 { 406 407 #ifdef __i386__ 408 return (1); 409 #else 410 return (0); 411 #endif 412 } 413 414 struct kvm_arch kvm_i386 = { 415 .ka_probe = _i386_probe, 416 .ka_initvtop = _i386_initvtop, 417 .ka_freevtop = _i386_freevtop, 418 .ka_kvatop = _i386_kvatop, 419 .ka_native = _i386_native, 420 }; 421 422 KVM_ARCH(kvm_i386); 423