2 * Copyright (c) 1989, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
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.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #if defined(LIBC_SCCS) && !defined(lint)
39 static char sccsid[] = "@(#)kvm_hp300.c 8.1 (Berkeley) 6/4/93";
41 #endif /* LIBC_SCCS and not lint */
44 * i386 machine dependent routines for kvm. Hopefully, the forthcoming
45 * vm code will one day obsolete this module.
48 #include <sys/param.h>
60 #include <vm/vm_param.h>
62 #include <machine/elf.h>
66 #include "kvm_private.h"
69 #define btop(x) (i386_btop(x))
70 #define ptob(x) (i386_ptob(x))
73 #define PG_FRAME_PAE (~((uint64_t)PAGE_MASK))
74 #define PDRSHIFT_PAE 21
75 #define NPTEPG_PAE (PAGE_SIZE/sizeof(uint64_t))
76 #define NBPDR_PAE (1<<PDRSHIFT_PAE)
78 /* minidump must be the first item! */
80 int minidump; /* 1 = minidump mode */
88 * Map the ELF headers into the process' address space. We do this in two
89 * steps: first the ELF header itself and using that information the whole
90 * set of headers. (Taken from kvm_ia64.c)
93 _kvm_maphdrs(kvm_t *kd, size_t sz)
95 struct vmstate *vm = kd->vmst;
97 /* munmap() previous mmap(). */
98 if (vm->mmapbase != NULL) {
99 munmap(vm->mmapbase, vm->mmapsize);
104 vm->mmapbase = mmap(NULL, sz, PROT_READ, MAP_PRIVATE, kd->pmfd, 0);
105 if (vm->mmapbase == MAP_FAILED) {
106 _kvm_err(kd, kd->program, "cannot mmap corefile");
113 * Translate a physical memory address to a file-offset in the crash-dump.
114 * (Taken from kvm_ia64.c)
117 _kvm_pa2off(kvm_t *kd, uint64_t pa, off_t *ofs)
119 Elf_Ehdr *e = kd->vmst->mmapbase;
125 return (PAGE_SIZE - ((size_t)pa & PAGE_MASK));
128 p = (Elf_Phdr*)((char*)e + e->e_phoff);
130 while (n && (pa < p->p_paddr || pa >= p->p_paddr + p->p_memsz))
134 *ofs = (pa - p->p_paddr) + p->p_offset;
135 return (PAGE_SIZE - ((size_t)pa & PAGE_MASK));
139 _kvm_freevtop(kvm_t *kd)
141 struct vmstate *vm = kd->vmst;
143 if (kd->vmst->minidump)
144 return (_kvm_minidump_freevtop(kd));
145 if (vm->mmapbase != NULL)
146 munmap(vm->mmapbase, vm->mmapsize);
154 _kvm_initvtop(kvm_t *kd)
165 if (!kd->rawdump && pread(kd->pmfd, &minihdr, 8, 0) == 8)
166 if (memcmp(&minihdr, "minidump", 8) == 0)
167 return (_kvm_minidump_initvtop(kd));
169 kd->vmst = (struct vmstate *)_kvm_malloc(kd, sizeof(*kd->vmst));
171 _kvm_err(kd, kd->program, "cannot allocate vm");
176 if (kd->rawdump == 0) {
177 if (_kvm_maphdrs(kd, sizeof(Elf_Ehdr)) == -1)
180 ehdr = kd->vmst->mmapbase;
181 hdrsz = ehdr->e_phoff + ehdr->e_phentsize * ehdr->e_phnum;
182 if (_kvm_maphdrs(kd, hdrsz) == -1)
186 nl[0].n_name = "kernbase";
189 if (kvm_nlist(kd, nl) != 0)
190 kernbase = KERNBASE; /* for old kernels */
192 kernbase = nl[0].n_value;
194 nl[0].n_name = "IdlePDPT";
197 if (kvm_nlist(kd, nl) == 0) {
200 if (kvm_read(kd, (nl[0].n_value - kernbase), &pa,
201 sizeof(pa)) != sizeof(pa)) {
202 _kvm_err(kd, kd->program, "cannot read IdlePDPT");
205 PTD = _kvm_malloc(kd, 4 * PAGE_SIZE);
206 for (i = 0; i < 4; i++) {
207 if (kvm_read(kd, pa + (i * sizeof(pa64)), &pa64,
208 sizeof(pa64)) != sizeof(pa64)) {
209 _kvm_err(kd, kd->program, "Cannot read PDPT");
213 if (kvm_read(kd, pa64 & PG_FRAME_PAE,
214 PTD + (i * PAGE_SIZE), PAGE_SIZE) != (PAGE_SIZE)) {
215 _kvm_err(kd, kd->program, "cannot read PDPT");
223 nl[0].n_name = "IdlePTD";
226 if (kvm_nlist(kd, nl) != 0) {
227 _kvm_err(kd, kd->program, "bad namelist");
230 if (kvm_read(kd, (nl[0].n_value - kernbase), &pa,
231 sizeof(pa)) != sizeof(pa)) {
232 _kvm_err(kd, kd->program, "cannot read IdlePTD");
235 PTD = _kvm_malloc(kd, PAGE_SIZE);
236 if (kvm_read(kd, pa, PTD, PAGE_SIZE) != PAGE_SIZE) {
237 _kvm_err(kd, kd->program, "cannot read PTD");
248 _kvm_vatop(kvm_t *kd, u_long va, off_t *pa)
264 PTD = (uint32_t *)vm->PTD;
265 offset = va & (PAGE_SIZE - 1);
268 * If we are initializing (kernel page table descriptor pointer
269 * not yet set) then return pa == va to avoid infinite recursion.
272 s = _kvm_pa2off(kd, va, pa);
274 _kvm_err(kd, kd->program,
275 "_kvm_vatop: bootstrap data not in dump");
278 return (PAGE_SIZE - offset);
281 pdeindex = va >> PDRSHIFT;
283 if (((u_long)pde & PG_V) == 0) {
284 _kvm_err(kd, kd->program, "_kvm_vatop: pde not valid");
288 if ((u_long)pde & PG_PS) {
290 * No second-level page table; ptd describes one 4MB page.
291 * (We assume that the kernel wouldn't set PG_PS without enabling
294 #define PAGE4M_MASK (NBPDR - 1)
295 #define PG_FRAME4M (~PAGE4M_MASK)
296 pde_pa = ((u_long)pde & PG_FRAME4M) + (va & PAGE4M_MASK);
297 s = _kvm_pa2off(kd, pde_pa, &ofs);
299 _kvm_err(kd, kd->program,
300 "_kvm_vatop: 4MB page address not in dump");
304 return (NBPDR - (va & PAGE4M_MASK));
307 pteindex = (va >> PAGE_SHIFT) & (NPTEPG-1);
308 pte_pa = ((u_long)pde & PG_FRAME) + (pteindex * sizeof(pde));
310 s = _kvm_pa2off(kd, pte_pa, &ofs);
311 if (s < sizeof pte) {
312 _kvm_err(kd, kd->program, "_kvm_vatop: pdpe_pa not found");
316 /* XXX This has to be a physical address read, kvm_read is virtual */
317 if (lseek(kd->pmfd, ofs, 0) == -1) {
318 _kvm_syserr(kd, kd->program, "_kvm_vatop: lseek");
321 if (read(kd->pmfd, &pte, sizeof pte) != sizeof pte) {
322 _kvm_syserr(kd, kd->program, "_kvm_vatop: read");
325 if (((u_long)pte & PG_V) == 0) {
326 _kvm_err(kd, kd->program, "_kvm_kvatop: pte not valid");
330 a = ((u_long)pte & PG_FRAME) + offset;
331 s =_kvm_pa2off(kd, a, pa);
333 _kvm_err(kd, kd->program, "_kvm_vatop: address not in dump");
336 return (PAGE_SIZE - offset);
339 _kvm_err(kd, 0, "invalid address (0x%lx)", va);
344 _kvm_vatop_pae(kvm_t *kd, u_long va, off_t *pa)
360 PTD = (uint64_t *)vm->PTD;
361 offset = va & (PAGE_SIZE - 1);
364 * If we are initializing (kernel page table descriptor pointer
365 * not yet set) then return pa == va to avoid infinite recursion.
368 s = _kvm_pa2off(kd, va, pa);
370 _kvm_err(kd, kd->program,
371 "_kvm_vatop_pae: bootstrap data not in dump");
374 return (PAGE_SIZE - offset);
377 pdeindex = va >> PDRSHIFT_PAE;
379 if (((u_long)pde & PG_V) == 0) {
380 _kvm_err(kd, kd->program, "_kvm_kvatop_pae: pde not valid");
384 if ((u_long)pde & PG_PS) {
386 * No second-level page table; ptd describes one 2MB page.
387 * (We assume that the kernel wouldn't set PG_PS without enabling
390 #define PAGE2M_MASK (NBPDR_PAE - 1)
391 #define PG_FRAME2M (~PAGE2M_MASK)
392 pde_pa = ((u_long)pde & PG_FRAME2M) + (va & PAGE2M_MASK);
393 s = _kvm_pa2off(kd, pde_pa, &ofs);
395 _kvm_err(kd, kd->program,
396 "_kvm_vatop: 2MB page address not in dump");
400 return (NBPDR_PAE - (va & PAGE2M_MASK));
403 pteindex = (va >> PAGE_SHIFT) & (NPTEPG_PAE-1);
404 pte_pa = ((uint64_t)pde & PG_FRAME_PAE) + (pteindex * sizeof(pde));
406 s = _kvm_pa2off(kd, pte_pa, &ofs);
407 if (s < sizeof pte) {
408 _kvm_err(kd, kd->program, "_kvm_vatop_pae: pdpe_pa not found");
412 /* XXX This has to be a physical address read, kvm_read is virtual */
413 if (lseek(kd->pmfd, ofs, 0) == -1) {
414 _kvm_syserr(kd, kd->program, "_kvm_vatop_pae: lseek");
417 if (read(kd->pmfd, &pte, sizeof pte) != sizeof pte) {
418 _kvm_syserr(kd, kd->program, "_kvm_vatop_pae: read");
421 if (((uint64_t)pte & PG_V) == 0) {
422 _kvm_err(kd, kd->program, "_kvm_vatop_pae: pte not valid");
426 a = ((uint64_t)pte & PG_FRAME_PAE) + offset;
427 s =_kvm_pa2off(kd, a, pa);
429 _kvm_err(kd, kd->program,
430 "_kvm_vatop_pae: address not in dump");
433 return (PAGE_SIZE - offset);
436 _kvm_err(kd, 0, "invalid address (0x%lx)", va);
441 _kvm_kvatop(kvm_t *kd, u_long va, off_t *pa)
444 if (kd->vmst->minidump)
445 return (_kvm_minidump_kvatop(kd, va, pa));
447 _kvm_err(kd, 0, "vatop called in live kernel!");
451 return (_kvm_vatop_pae(kd, va, pa));
453 return (_kvm_vatop(kd, va, pa));