2 * Copyright (c) 2006 Peter Wemm
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
30 * AMD64 machine dependent routines for kvm and minidumps.
33 #include <sys/param.h>
38 #include <sys/fnv_hash.h>
46 #include <vm/vm_param.h>
48 #include <machine/elf.h>
49 #include <machine/cpufunc.h>
50 #include <machine/minidump.h>
54 #include "kvm_private.h"
64 /* minidump must be the first item! */
66 int minidump; /* 1 = minidump mode */
67 struct minidumphdr hdr;
68 void *hpt_head[HPT_SIZE];
74 hpt_insert(kvm_t *kd, vm_paddr_t pa, int64_t off)
77 uint32_t fnv = FNV1_32_INIT;
79 fnv = fnv_32_buf(&pa, sizeof(pa), fnv);
80 fnv &= (HPT_SIZE - 1);
81 hpte = malloc(sizeof(*hpte));
84 hpte->next = kd->vmst->hpt_head[fnv];
85 kd->vmst->hpt_head[fnv] = hpte;
89 hpt_find(kvm_t *kd, vm_paddr_t pa)
92 uint32_t fnv = FNV1_32_INIT;
94 fnv = fnv_32_buf(&pa, sizeof(pa), fnv);
95 fnv &= (HPT_SIZE - 1);
96 for (hpte = kd->vmst->hpt_head[fnv]; hpte != NULL; hpte = hpte->next) {
104 inithash(kvm_t *kd, uint64_t *base, int len, off_t off)
110 for (idx = 0; idx < len / sizeof(*base); idx++) {
114 bits &= ~(1ul << bit);
115 pa = (idx * sizeof(*base) * NBBY + bit) * PAGE_SIZE;
116 hpt_insert(kd, pa, off);
124 _kvm_minidump_freevtop(kvm_t *kd)
126 struct vmstate *vm = kd->vmst;
137 _kvm_minidump_initvtop(kvm_t *kd)
139 struct vmstate *vmst;
142 vmst = _kvm_malloc(kd, sizeof(*vmst));
144 _kvm_err(kd, kd->program, "cannot allocate vm");
149 if (pread(kd->pmfd, &vmst->hdr, sizeof(vmst->hdr), 0) !=
151 _kvm_err(kd, kd->program, "cannot read dump header");
154 if (strncmp(MINIDUMP_MAGIC, vmst->hdr.magic, sizeof(vmst->hdr.magic)) != 0) {
155 _kvm_err(kd, kd->program, "not a minidump for this platform");
160 * NB: amd64 minidump header is binary compatible between version 1
161 * and version 2; this may not be the case for the future versions.
163 if (vmst->hdr.version != MINIDUMP_VERSION && vmst->hdr.version != 1) {
164 _kvm_err(kd, kd->program, "wrong minidump version. expected %d got %d",
165 MINIDUMP_VERSION, vmst->hdr.version);
169 /* Skip header and msgbuf */
170 off = PAGE_SIZE + round_page(vmst->hdr.msgbufsize);
172 vmst->bitmap = _kvm_malloc(kd, vmst->hdr.bitmapsize);
173 if (vmst->bitmap == NULL) {
174 _kvm_err(kd, kd->program, "cannot allocate %d bytes for bitmap", vmst->hdr.bitmapsize);
177 if (pread(kd->pmfd, vmst->bitmap, vmst->hdr.bitmapsize, off) !=
178 vmst->hdr.bitmapsize) {
179 _kvm_err(kd, kd->program, "cannot read %d bytes for page bitmap", vmst->hdr.bitmapsize);
182 off += round_page(vmst->hdr.bitmapsize);
184 vmst->page_map = _kvm_malloc(kd, vmst->hdr.pmapsize);
185 if (vmst->page_map == NULL) {
186 _kvm_err(kd, kd->program, "cannot allocate %d bytes for page_map", vmst->hdr.pmapsize);
189 if (pread(kd->pmfd, vmst->page_map, vmst->hdr.pmapsize, off) !=
190 vmst->hdr.pmapsize) {
191 _kvm_err(kd, kd->program, "cannot read %d bytes for page_map", vmst->hdr.pmapsize);
194 off += vmst->hdr.pmapsize;
196 /* build physical address hash table for sparse pages */
197 inithash(kd, vmst->bitmap, vmst->hdr.bitmapsize, off);
203 _kvm_minidump_vatop_v1(kvm_t *kd, u_long va, off_t *pa)
213 offset = va & (PAGE_SIZE - 1);
215 if (va >= vm->hdr.kernbase) {
216 pteindex = (va - vm->hdr.kernbase) >> PAGE_SHIFT;
217 pte = vm->page_map[pteindex];
218 if (((u_long)pte & PG_V) == 0) {
219 _kvm_err(kd, kd->program, "_kvm_vatop: pte not valid");
223 ofs = hpt_find(kd, a);
225 _kvm_err(kd, kd->program, "_kvm_vatop: physical address 0x%lx not in minidump", a);
229 return (PAGE_SIZE - offset);
230 } else if (va >= vm->hdr.dmapbase && va < vm->hdr.dmapend) {
231 a = (va - vm->hdr.dmapbase) & ~PAGE_MASK;
232 ofs = hpt_find(kd, a);
234 _kvm_err(kd, kd->program, "_kvm_vatop: direct map address 0x%lx not in minidump", va);
238 return (PAGE_SIZE - offset);
240 _kvm_err(kd, kd->program, "_kvm_vatop: virtual address 0x%lx not minidumped", va);
245 _kvm_err(kd, 0, "invalid address (0x%lx)", va);
250 _kvm_minidump_vatop(kvm_t *kd, u_long va, off_t *pa)
252 pt_entry_t pt[NPTEPG];
263 offset = va & PAGE_MASK;
265 if (va >= vm->hdr.kernbase) {
266 pdeindex = (va - vm->hdr.kernbase) >> PDRSHIFT;
267 pde = vm->page_map[pdeindex];
268 if (((u_long)pde & PG_V) == 0) {
269 _kvm_err(kd, kd->program, "_kvm_vatop: pde not valid");
272 if ((pde & PG_PS) == 0) {
274 ofs = hpt_find(kd, a);
276 _kvm_err(kd, kd->program, "_kvm_vatop: pt physical address 0x%lx not in minidump", a);
279 if (pread(kd->pmfd, &pt, PAGE_SIZE, ofs) != PAGE_SIZE) {
280 _kvm_err(kd, kd->program, "cannot read %d bytes for pt", PAGE_SIZE);
283 pteindex = (va >> PAGE_SHIFT) & ((1ul << NPTEPGSHIFT) - 1);
285 if (((u_long)pte & PG_V) == 0) {
286 _kvm_err(kd, kd->program, "_kvm_vatop: pte not valid");
291 a = pde & PG_PS_FRAME;
292 a += (va & PDRMASK) ^ offset;
294 ofs = hpt_find(kd, a);
296 _kvm_err(kd, kd->program, "_kvm_vatop: physical address 0x%lx not in minidump", a);
300 return (PAGE_SIZE - offset);
301 } else if (va >= vm->hdr.dmapbase && va < vm->hdr.dmapend) {
302 a = (va - vm->hdr.dmapbase) & ~PAGE_MASK;
303 ofs = hpt_find(kd, a);
305 _kvm_err(kd, kd->program, "_kvm_vatop: direct map address 0x%lx not in minidump", va);
309 return (PAGE_SIZE - offset);
311 _kvm_err(kd, kd->program, "_kvm_vatop: virtual address 0x%lx not minidumped", va);
316 _kvm_err(kd, 0, "invalid address (0x%lx)", va);
321 _kvm_minidump_kvatop(kvm_t *kd, u_long va, off_t *pa)
325 _kvm_err(kd, 0, "kvm_kvatop called in live kernel!");
328 if (((struct vmstate *)kd->vmst)->hdr.version == 1)
329 return (_kvm_minidump_vatop_v1(kd, va, pa));
331 return (_kvm_minidump_vatop(kd, va, pa));