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 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by the University of 20 * California, Berkeley and its contributors. 21 * 4. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 */ 37 38 #include <sys/cdefs.h> 39 __FBSDID("$FreeBSD$"); 40 41 #if defined(LIBC_SCCS) && !defined(lint) 42 #if 0 43 static char sccsid[] = "@(#)kvm_hp300.c 8.1 (Berkeley) 6/4/93"; 44 #endif 45 #endif /* LIBC_SCCS and not lint */ 46 47 /* 48 * i386 machine dependent routines for kvm. Hopefully, the forthcoming 49 * vm code will one day obsolete this module. 50 */ 51 52 #include <sys/param.h> 53 #include <sys/user.h> 54 #include <sys/proc.h> 55 #include <sys/stat.h> 56 #include <sys/mman.h> 57 #include <stdlib.h> 58 #include <unistd.h> 59 #include <nlist.h> 60 #include <kvm.h> 61 62 #include <vm/vm.h> 63 #include <vm/vm_param.h> 64 65 #include <machine/elf.h> 66 67 #include <limits.h> 68 69 #include "kvm_private.h" 70 71 #ifndef btop 72 #define btop(x) (i386_btop(x)) 73 #define ptob(x) (i386_ptob(x)) 74 #endif 75 76 #define PG_FRAME_PAE (~((uint64_t)PAGE_MASK)) 77 #define PDRSHIFT_PAE 21 78 #define NPTEPG_PAE (PAGE_SIZE/sizeof(uint64_t)) 79 #define NBPDR_PAE (1<<PDRSHIFT_PAE) 80 81 /* minidump must be the first item! */ 82 struct vmstate { 83 int minidump; /* 1 = minidump mode */ 84 void *mmapbase; 85 size_t mmapsize; 86 void *PTD; 87 int pae; 88 }; 89 90 /* 91 * Map the ELF headers into the process' address space. We do this in two 92 * steps: first the ELF header itself and using that information the whole 93 * set of headers. (Taken from kvm_ia64.c) 94 */ 95 static int 96 _kvm_maphdrs(kvm_t *kd, size_t sz) 97 { 98 struct vmstate *vm = kd->vmst; 99 100 /* munmap() previous mmap(). */ 101 if (vm->mmapbase != NULL) { 102 munmap(vm->mmapbase, vm->mmapsize); 103 vm->mmapbase = NULL; 104 } 105 106 vm->mmapsize = sz; 107 vm->mmapbase = mmap(NULL, sz, PROT_READ, MAP_PRIVATE, kd->pmfd, 0); 108 if (vm->mmapbase == MAP_FAILED) { 109 _kvm_err(kd, kd->program, "cannot mmap corefile"); 110 return (-1); 111 } 112 return (0); 113 } 114 115 /* 116 * Translate a physical memory address to a file-offset in the crash-dump. 117 * (Taken from kvm_ia64.c) 118 */ 119 static size_t 120 _kvm_pa2off(kvm_t *kd, uint64_t pa, off_t *ofs) 121 { 122 Elf_Ehdr *e = kd->vmst->mmapbase; 123 Elf_Phdr *p = (Elf_Phdr*)((char*)e + e->e_phoff); 124 int n = e->e_phnum; 125 126 while (n && (pa < p->p_paddr || pa >= p->p_paddr + p->p_memsz)) 127 p++, n--; 128 if (n == 0) 129 return (0); 130 *ofs = (pa - p->p_paddr) + p->p_offset; 131 return (PAGE_SIZE - ((size_t)pa & PAGE_MASK)); 132 } 133 134 void 135 _kvm_freevtop(kvm_t *kd) 136 { 137 struct vmstate *vm = kd->vmst; 138 139 if (kd->vmst->minidump) 140 return (_kvm_minidump_freevtop(kd)); 141 if (vm->mmapbase != NULL) 142 munmap(vm->mmapbase, vm->mmapsize); 143 if (vm->PTD) 144 free(vm->PTD); 145 free(vm); 146 kd->vmst = NULL; 147 } 148 149 int 150 _kvm_initvtop(kvm_t *kd) 151 { 152 struct nlist nlist[2]; 153 u_long pa; 154 u_long kernbase; 155 char *PTD; 156 Elf_Ehdr *ehdr; 157 size_t hdrsz; 158 int i; 159 char minihdr[8]; 160 161 if (pread(kd->pmfd, &minihdr, 8, 0) == 8) 162 if (memcmp(&minihdr, "minidump", 8) == 0) 163 return (_kvm_minidump_initvtop(kd)); 164 165 kd->vmst = (struct vmstate *)_kvm_malloc(kd, sizeof(*kd->vmst)); 166 if (kd->vmst == 0) { 167 _kvm_err(kd, kd->program, "cannot allocate vm"); 168 return (-1); 169 } 170 kd->vmst->PTD = 0; 171 172 if (_kvm_maphdrs(kd, sizeof(Elf_Ehdr)) == -1) 173 return (-1); 174 175 ehdr = kd->vmst->mmapbase; 176 hdrsz = ehdr->e_phoff + ehdr->e_phentsize * ehdr->e_phnum; 177 if (_kvm_maphdrs(kd, hdrsz) == -1) 178 return (-1); 179 180 nlist[0].n_name = "kernbase"; 181 nlist[1].n_name = 0; 182 183 if (kvm_nlist(kd, nlist) != 0) 184 kernbase = KERNBASE; /* for old kernels */ 185 else 186 kernbase = nlist[0].n_value; 187 188 nlist[0].n_name = "IdlePDPT"; 189 nlist[1].n_name = 0; 190 191 if (kvm_nlist(kd, nlist) == 0) { 192 uint64_t pa64; 193 194 if (kvm_read(kd, (nlist[0].n_value - kernbase), &pa, 195 sizeof(pa)) != sizeof(pa)) { 196 _kvm_err(kd, kd->program, "cannot read IdlePDPT"); 197 return (-1); 198 } 199 PTD = _kvm_malloc(kd, 4 * PAGE_SIZE); 200 for (i = 0; i < 4; i++) { 201 if (kvm_read(kd, pa + (i * sizeof(pa64)), &pa64, 202 sizeof(pa64)) != sizeof(pa64)) { 203 _kvm_err(kd, kd->program, "Cannot read PDPT"); 204 free(PTD); 205 return (-1); 206 } 207 if (kvm_read(kd, pa64 & PG_FRAME_PAE, 208 PTD + (i * PAGE_SIZE), PAGE_SIZE) != (PAGE_SIZE)) { 209 _kvm_err(kd, kd->program, "cannot read PDPT"); 210 free(PTD); 211 return (-1); 212 } 213 } 214 kd->vmst->PTD = PTD; 215 kd->vmst->pae = 1; 216 } else { 217 nlist[0].n_name = "IdlePTD"; 218 nlist[1].n_name = 0; 219 220 if (kvm_nlist(kd, nlist) != 0) { 221 _kvm_err(kd, kd->program, "bad namelist"); 222 return (-1); 223 } 224 if (kvm_read(kd, (nlist[0].n_value - kernbase), &pa, 225 sizeof(pa)) != sizeof(pa)) { 226 _kvm_err(kd, kd->program, "cannot read IdlePTD"); 227 return (-1); 228 } 229 PTD = _kvm_malloc(kd, PAGE_SIZE); 230 if (kvm_read(kd, pa, PTD, PAGE_SIZE) != PAGE_SIZE) { 231 _kvm_err(kd, kd->program, "cannot read PTD"); 232 return (-1); 233 } 234 kd->vmst->PTD = PTD; 235 return (0); 236 kd->vmst->pae = 0; 237 } 238 return (0); 239 } 240 241 static int 242 _kvm_vatop(kvm_t *kd, u_long va, off_t *pa) 243 { 244 struct vmstate *vm; 245 u_long offset; 246 u_long pte_pa; 247 u_long pde_pa; 248 pd_entry_t pde; 249 pt_entry_t pte; 250 u_long pdeindex; 251 u_long pteindex; 252 size_t s; 253 u_long a; 254 off_t ofs; 255 uint32_t *PTD; 256 257 vm = kd->vmst; 258 PTD = (uint32_t *)vm->PTD; 259 offset = va & (PAGE_SIZE - 1); 260 261 /* 262 * If we are initializing (kernel page table descriptor pointer 263 * not yet set) then return pa == va to avoid infinite recursion. 264 */ 265 if (PTD == 0) { 266 s = _kvm_pa2off(kd, va, pa); 267 if (s == 0) { 268 _kvm_err(kd, kd->program, 269 "_kvm_vatop: bootstrap data not in dump"); 270 goto invalid; 271 } else 272 return (PAGE_SIZE - offset); 273 } 274 275 pdeindex = va >> PDRSHIFT; 276 pde = PTD[pdeindex]; 277 if (((u_long)pde & PG_V) == 0) { 278 _kvm_err(kd, kd->program, "_kvm_vatop: pde not valid"); 279 goto invalid; 280 } 281 282 if ((u_long)pde & PG_PS) { 283 /* 284 * No second-level page table; ptd describes one 4MB page. 285 * (We assume that the kernel wouldn't set PG_PS without enabling 286 * it cr0). 287 */ 288 #define PAGE4M_MASK (NBPDR - 1) 289 #define PG_FRAME4M (~PAGE4M_MASK) 290 pde_pa = ((u_long)pde & PG_FRAME4M) + (va & PAGE4M_MASK); 291 s = _kvm_pa2off(kd, pde_pa, &ofs); 292 if (s < sizeof pde) { 293 _kvm_syserr(kd, kd->program, 294 "_kvm_vatop: pde_pa not found"); 295 goto invalid; 296 } 297 *pa = ofs; 298 return (NBPDR - (va & PAGE4M_MASK)); 299 } 300 301 pteindex = (va >> PAGE_SHIFT) & (NPTEPG-1); 302 pte_pa = ((u_long)pde & PG_FRAME) + (pteindex * sizeof(pde)); 303 304 s = _kvm_pa2off(kd, pte_pa, &ofs); 305 if (s < sizeof pte) { 306 _kvm_err(kd, kd->program, "_kvm_vatop: pdpe_pa not found"); 307 goto invalid; 308 } 309 310 /* XXX This has to be a physical address read, kvm_read is virtual */ 311 if (lseek(kd->pmfd, ofs, 0) == -1) { 312 _kvm_syserr(kd, kd->program, "_kvm_vatop: lseek"); 313 goto invalid; 314 } 315 if (read(kd->pmfd, &pte, sizeof pte) != sizeof pte) { 316 _kvm_syserr(kd, kd->program, "_kvm_vatop: read"); 317 goto invalid; 318 } 319 if (((u_long)pte & PG_V) == 0) { 320 _kvm_err(kd, kd->program, "_kvm_kvatop: pte not valid"); 321 goto invalid; 322 } 323 324 a = ((u_long)pte & PG_FRAME) + offset; 325 s =_kvm_pa2off(kd, a, pa); 326 if (s == 0) { 327 _kvm_err(kd, kd->program, "_kvm_vatop: address not in dump"); 328 goto invalid; 329 } else 330 return (PAGE_SIZE - offset); 331 332 invalid: 333 _kvm_err(kd, 0, "invalid address (0x%lx)", va); 334 return (0); 335 } 336 337 static int 338 _kvm_vatop_pae(kvm_t *kd, u_long va, off_t *pa) 339 { 340 struct vmstate *vm; 341 uint64_t offset; 342 uint64_t pte_pa; 343 uint64_t pde_pa; 344 uint64_t pde; 345 uint64_t pte; 346 u_long pdeindex; 347 u_long pteindex; 348 size_t s; 349 uint64_t a; 350 off_t ofs; 351 uint64_t *PTD; 352 353 vm = kd->vmst; 354 PTD = (uint64_t *)vm->PTD; 355 offset = va & (PAGE_SIZE - 1); 356 357 /* 358 * If we are initializing (kernel page table descriptor pointer 359 * not yet set) then return pa == va to avoid infinite recursion. 360 */ 361 if (PTD == 0) { 362 s = _kvm_pa2off(kd, va, pa); 363 if (s == 0) { 364 _kvm_err(kd, kd->program, 365 "_kvm_vatop_pae: bootstrap data not in dump"); 366 goto invalid; 367 } else 368 return (PAGE_SIZE - offset); 369 } 370 371 pdeindex = va >> PDRSHIFT_PAE; 372 pde = PTD[pdeindex]; 373 if (((u_long)pde & PG_V) == 0) { 374 _kvm_err(kd, kd->program, "_kvm_kvatop_pae: pde not valid"); 375 goto invalid; 376 } 377 378 if ((u_long)pde & PG_PS) { 379 /* 380 * No second-level page table; ptd describes one 2MB page. 381 * (We assume that the kernel wouldn't set PG_PS without enabling 382 * it cr0). 383 */ 384 #define PAGE2M_MASK (NBPDR_PAE - 1) 385 #define PG_FRAME2M (~PAGE2M_MASK) 386 pde_pa = ((u_long)pde & PG_FRAME2M) + (va & PAGE2M_MASK); 387 s = _kvm_pa2off(kd, pde_pa, &ofs); 388 if (s < sizeof pde) { 389 _kvm_syserr(kd, kd->program, 390 "_kvm_vatop_pae: pde_pa not found"); 391 goto invalid; 392 } 393 *pa = ofs; 394 return (NBPDR_PAE - (va & PAGE2M_MASK)); 395 } 396 397 pteindex = (va >> PAGE_SHIFT) & (NPTEPG_PAE-1); 398 pte_pa = ((uint64_t)pde & PG_FRAME_PAE) + (pteindex * sizeof(pde)); 399 400 s = _kvm_pa2off(kd, pte_pa, &ofs); 401 if (s < sizeof pte) { 402 _kvm_err(kd, kd->program, "_kvm_vatop_pae: pdpe_pa not found"); 403 goto invalid; 404 } 405 406 /* XXX This has to be a physical address read, kvm_read is virtual */ 407 if (lseek(kd->pmfd, ofs, 0) == -1) { 408 _kvm_syserr(kd, kd->program, "_kvm_vatop_pae: lseek"); 409 goto invalid; 410 } 411 if (read(kd->pmfd, &pte, sizeof pte) != sizeof pte) { 412 _kvm_syserr(kd, kd->program, "_kvm_vatop_pae: read"); 413 goto invalid; 414 } 415 if (((uint64_t)pte & PG_V) == 0) { 416 _kvm_err(kd, kd->program, "_kvm_vatop_pae: pte not valid"); 417 goto invalid; 418 } 419 420 a = ((uint64_t)pte & PG_FRAME_PAE) + offset; 421 s =_kvm_pa2off(kd, a, pa); 422 if (s == 0) { 423 _kvm_err(kd, kd->program, 424 "_kvm_vatop_pae: address not in dump"); 425 goto invalid; 426 } else 427 return (PAGE_SIZE - offset); 428 429 invalid: 430 _kvm_err(kd, 0, "invalid address (0x%lx)", va); 431 return (0); 432 } 433 434 int 435 _kvm_kvatop(kvm_t *kd, u_long va, off_t *pa) 436 { 437 438 if (kd->vmst->minidump) 439 return (_kvm_minidump_kvatop(kd, va, pa)); 440 if (ISALIVE(kd)) { 441 _kvm_err(kd, 0, "vatop called in live kernel!"); 442 return (0); 443 } 444 if (kd->vmst->pae) 445 return (_kvm_vatop_pae(kd, va, pa)); 446 else 447 return (_kvm_vatop(kd, va, pa)); 448 } 449