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)
141 struct vmstate *vmst;
144 vmst = _kvm_malloc(kd, sizeof(*vmst));
146 _kvm_err(kd, kd->program, "cannot allocate vm");
153 if (pread(kd->pmfd, &vmst->hdr,
154 sizeof(vmst->hdr), 0) != sizeof(vmst->hdr)) {
155 _kvm_err(kd, kd->program, "cannot read dump header");
159 if (strncmp(MINIDUMP_MAGIC, vmst->hdr.magic,
160 sizeof(vmst->hdr.magic)) != 0) {
161 _kvm_err(kd, kd->program, "not a minidump for this platform");
164 if (vmst->hdr.version != MINIDUMP_VERSION) {
165 _kvm_err(kd, kd->program, "wrong minidump version. "
166 "Expected %d got %d", MINIDUMP_VERSION, vmst->hdr.version);
170 /* Skip header and msgbuf */
171 off = PAGE_SIZE + round_page(vmst->hdr.msgbufsize);
173 vmst->bitmap = _kvm_malloc(kd, vmst->hdr.bitmapsize);
174 if (vmst->bitmap == NULL) {
175 _kvm_err(kd, kd->program, "cannot allocate %d bytes for "
176 "bitmap", vmst->hdr.bitmapsize);
180 if (pread(kd->pmfd, vmst->bitmap, vmst->hdr.bitmapsize, off) !=
181 (ssize_t)vmst->hdr.bitmapsize) {
182 _kvm_err(kd, kd->program, "cannot read %d bytes for page bitmap",
183 vmst->hdr.bitmapsize);
186 off += round_page(vmst->hdr.bitmapsize);
188 vmst->ptemap = _kvm_malloc(kd, vmst->hdr.ptesize);
189 if (vmst->ptemap == NULL) {
190 _kvm_err(kd, kd->program, "cannot allocate %d bytes for "
191 "ptemap", vmst->hdr.ptesize);
195 if (pread(kd->pmfd, vmst->ptemap, vmst->hdr.ptesize, off) !=
196 (ssize_t)vmst->hdr.ptesize) {
197 _kvm_err(kd, kd->program, "cannot read %d bytes for ptemap",
202 off += vmst->hdr.ptesize;
204 /* Build physical address hash table for sparse pages */
205 inithash(kd, vmst->bitmap, vmst->hdr.bitmapsize, off);
211 _kvm_minidump_kvatop(kvm_t *kd, u_long va, off_t *pa)
215 u_long offset, pteindex, a;
220 _kvm_err(kd, 0, "kvm_kvatop called in live kernel!");
227 if (va >= vm->hdr.kernbase) {
228 pteindex = (va - vm->hdr.kernbase) >> PAGE_SHIFT;
229 pte = ptemap[pteindex];
231 _kvm_err(kd, kd->program, "_kvm_vatop: pte not valid");
234 if ((pte & L2_TYPE_MASK) == L2_TYPE_L) {
235 offset = va & L2_L_OFFSET;
236 a = pte & L2_L_FRAME;
237 } else if ((pte & L2_TYPE_MASK) == L2_TYPE_S) {
238 offset = va & L2_S_OFFSET;
239 a = pte & L2_S_FRAME;
243 ofs = hpt_find(kd, a);
245 _kvm_err(kd, kd->program, "_kvm_vatop: physical "
246 "address 0x%lx not in minidump", a);
251 return (PAGE_SIZE - offset);
254 _kvm_err(kd, kd->program, "_kvm_vatop: virtual address 0x%lx "
255 "not minidumped", va);
258 _kvm_err(kd, 0, "invalid address (0x%lx)", va);