2 * Copyright (c) 2008 Semihalf, Grzegorz Bernacki
3 * Copyright (c) 2006 Peter Wemm
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * From: FreeBSD: src/lib/libkvm/kvm_minidump_i386.c,v 1.2 2006/06/05 08:51:14
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 * ARM machine dependent routines for kvm and minidumps.
36 #include <sys/param.h>
41 #include <sys/fnv_hash.h>
49 #include <vm/vm_param.h>
51 #include <machine/elf.h>
52 #include <machine/cpufunc.h>
53 #include <machine/minidump.h>
57 #include "kvm_private.h"
67 /* minidump must be the first field */
69 int minidump; /* 1 = minidump mode */
70 struct minidumphdr hdr;
71 void *hpt_head[HPT_SIZE];
77 hpt_insert(kvm_t *kd, uint64_t pa, int64_t off)
80 uint32_t fnv = FNV1_32_INIT;
82 fnv = fnv_32_buf(&pa, sizeof(pa), fnv);
83 fnv &= (HPT_SIZE - 1);
84 hpte = malloc(sizeof(*hpte));
87 hpte->next = kd->vmst->hpt_head[fnv];
88 kd->vmst->hpt_head[fnv] = hpte;
92 hpt_find(kvm_t *kd, uint64_t pa)
95 uint32_t fnv = FNV1_32_INIT;
97 fnv = fnv_32_buf(&pa, sizeof(pa), fnv);
98 fnv &= (HPT_SIZE - 1);
99 for (hpte = kd->vmst->hpt_head[fnv]; hpte != NULL; hpte = hpte->next)
107 inithash(kvm_t *kd, uint32_t *base, int len, off_t off)
112 for (idx = 0; idx < len / sizeof(*base); idx++) {
116 bits &= ~(1ul << bit);
117 pa = (idx * sizeof(*base) * NBBY + bit) * PAGE_SIZE;
118 hpt_insert(kd, pa, off);
126 _kvm_minidump_freevtop(kvm_t *kd)
128 struct vmstate *vm = kd->vmst;
139 _kvm_minidump_initvtop(kvm_t *kd)
142 struct vmstate *vmst;
145 vmst = _kvm_malloc(kd, sizeof(*vmst));
147 _kvm_err(kd, kd->program, "cannot allocate vm");
154 if (pread(kd->pmfd, &vmst->hdr,
155 sizeof(vmst->hdr), 0) != sizeof(vmst->hdr)) {
156 _kvm_err(kd, kd->program, "cannot read dump header");
160 if (strncmp(MINIDUMP_MAGIC, vmst->hdr.magic,
161 sizeof(vmst->hdr.magic)) != 0) {
162 _kvm_err(kd, kd->program, "not a minidump for this platform");
165 if (vmst->hdr.version != MINIDUMP_VERSION) {
166 _kvm_err(kd, kd->program, "wrong minidump version. "
167 "Expected %d got %d", MINIDUMP_VERSION, vmst->hdr.version);
171 /* Skip header and msgbuf */
172 off = PAGE_SIZE + round_page(vmst->hdr.msgbufsize);
174 vmst->bitmap = _kvm_malloc(kd, vmst->hdr.bitmapsize);
175 if (vmst->bitmap == NULL) {
176 _kvm_err(kd, kd->program, "cannot allocate %d bytes for "
177 "bitmap", vmst->hdr.bitmapsize);
181 if (pread(kd->pmfd, vmst->bitmap, vmst->hdr.bitmapsize, off) !=
182 vmst->hdr.bitmapsize) {
183 _kvm_err(kd, kd->program, "cannot read %d bytes for page bitmap",
184 vmst->hdr.bitmapsize);
187 off += round_page(vmst->hdr.bitmapsize);
189 vmst->ptemap = _kvm_malloc(kd, vmst->hdr.ptesize);
190 if (vmst->ptemap == NULL) {
191 _kvm_err(kd, kd->program, "cannot allocate %d bytes for "
192 "ptemap", vmst->hdr.ptesize);
196 if (pread(kd->pmfd, vmst->ptemap, vmst->hdr.ptesize, off) !=
198 _kvm_err(kd, kd->program, "cannot read %d bytes for ptemap",
203 off += vmst->hdr.ptesize;
205 /* Build physical address hash table for sparse pages */
206 inithash(kd, vmst->bitmap, vmst->hdr.bitmapsize, off);
212 _kvm_minidump_kvatop(kvm_t *kd, u_long va, off_t *pa)
216 u_long offset, pteindex, a;
222 _kvm_err(kd, 0, "kvm_kvatop called in live kernel!");
229 if (va >= vm->hdr.kernbase) {
230 pteindex = (va - vm->hdr.kernbase) >> PAGE_SHIFT;
231 pte = ptemap[pteindex];
233 _kvm_err(kd, kd->program, "_kvm_vatop: pte not valid");
236 if ((pte & L2_TYPE_MASK) == L2_TYPE_L) {
237 offset = va & L2_L_OFFSET;
238 a = pte & L2_L_FRAME;
239 } else if ((pte & L2_TYPE_MASK) == L2_TYPE_S) {
240 offset = va & L2_S_OFFSET;
241 a = pte & L2_S_FRAME;
245 ofs = hpt_find(kd, a);
247 _kvm_err(kd, kd->program, "_kvm_vatop: physical "
248 "address 0x%lx not in minidump", a);
253 return (PAGE_SIZE - offset);
256 _kvm_err(kd, kd->program, "_kvm_vatop: virtual address 0x%lx "
257 "not minidumped", va);
260 _kvm_err(kd, 0, "invalid address (0x%lx)", va);