2 * Copyright (c) 2010 Oleksandr Tymoshenko
3 * Copyright (c) 2008 Semihalf, Grzegorz Bernacki
4 * Copyright (c) 2006 Peter Wemm
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * From: FreeBSD: src/lib/libkvm/kvm_minidump_arm.c r214223
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 * MIPS machine dependent routines for kvm and minidumps.
37 #include <sys/param.h>
42 #include <sys/fnv_hash.h>
50 #include <vm/vm_param.h>
52 #include <machine/elf.h>
53 #include <machine/cpufunc.h>
54 #include <machine/minidump.h>
58 #include "kvm_private.h"
68 /* minidump must be the first field */
70 int minidump; /* 1 = minidump mode */
71 struct minidumphdr hdr;
72 void *hpt_head[HPT_SIZE];
78 hpt_insert(kvm_t *kd, uint64_t pa, int64_t off)
81 uint32_t fnv = FNV1_32_INIT;
83 fnv = fnv_32_buf(&pa, sizeof(pa), fnv);
84 fnv &= (HPT_SIZE - 1);
85 hpte = malloc(sizeof(*hpte));
88 hpte->next = kd->vmst->hpt_head[fnv];
89 kd->vmst->hpt_head[fnv] = hpte;
93 hpt_find(kvm_t *kd, uint64_t pa)
96 uint32_t fnv = FNV1_32_INIT;
98 fnv = fnv_32_buf(&pa, sizeof(pa), fnv);
99 fnv &= (HPT_SIZE - 1);
100 for (hpte = kd->vmst->hpt_head[fnv]; hpte != NULL; hpte = hpte->next)
108 inithash(kvm_t *kd, uint32_t *base, int len, off_t off)
113 for (idx = 0; idx < len / sizeof(*base); idx++) {
117 bits &= ~(1ul << bit);
118 pa = (idx * sizeof(*base) * NBBY + bit) * PAGE_SIZE;
119 hpt_insert(kd, pa, off);
128 _kvm_minidump_freevtop(kvm_t *kd)
130 struct vmstate *vm = kd->vmst;
141 _kvm_minidump_initvtop(kvm_t *kd)
144 struct vmstate *vmst;
147 vmst = _kvm_malloc(kd, sizeof(*vmst));
149 _kvm_err(kd, kd->program, "cannot allocate vm");
156 off = lseek(kd->pmfd, 0, SEEK_CUR);
157 if (pread(kd->pmfd, &vmst->hdr,
158 sizeof(vmst->hdr), 0) != sizeof(vmst->hdr)) {
159 _kvm_err(kd, kd->program, "cannot read dump header");
163 if (strncmp(MINIDUMP_MAGIC, vmst->hdr.magic,
164 sizeof(vmst->hdr.magic)) != 0) {
165 _kvm_err(kd, kd->program, "not a minidump for this platform");
168 if (vmst->hdr.version != MINIDUMP_VERSION) {
169 _kvm_err(kd, kd->program, "wrong minidump version. "
170 "Expected %d got %d", MINIDUMP_VERSION, vmst->hdr.version);
174 /* Skip header and msgbuf */
175 off = PAGE_SIZE + round_page(vmst->hdr.msgbufsize);
177 vmst->bitmap = _kvm_malloc(kd, vmst->hdr.bitmapsize);
178 if (vmst->bitmap == NULL) {
179 _kvm_err(kd, kd->program, "cannot allocate %d bytes for "
180 "bitmap", vmst->hdr.bitmapsize);
184 if (pread(kd->pmfd, vmst->bitmap, vmst->hdr.bitmapsize, off) !=
185 vmst->hdr.bitmapsize) {
186 _kvm_err(kd, kd->program, "cannot read %d bytes for page bitmap",
187 vmst->hdr.bitmapsize);
190 off += round_page(vmst->hdr.bitmapsize);
192 vmst->ptemap = _kvm_malloc(kd, vmst->hdr.ptesize);
193 if (vmst->ptemap == NULL) {
194 _kvm_err(kd, kd->program, "cannot allocate %d bytes for "
195 "ptemap", vmst->hdr.ptesize);
199 if (pread(kd->pmfd, vmst->ptemap, vmst->hdr.ptesize, off) !=
201 _kvm_err(kd, kd->program, "cannot read %d bytes for ptemap",
206 off += vmst->hdr.ptesize;
208 /* Build physical address hash table for sparse pages */
209 inithash(kd, vmst->bitmap, vmst->hdr.bitmapsize, off);
215 _kvm_minidump_kvatop(kvm_t *kd, u_long va, off_t *pa)
219 u_long offset, pteindex, a;
225 _kvm_err(kd, 0, "kvm_kvatop called in live kernel!");
229 offset = va & PAGE_MASK;
230 /* Operate with page-aligned address */
236 #if defined(__mips_n64)
237 if (va >= MIPS_XKPHYS_START && va < MIPS_XKPHYS_END)
238 a = (MIPS_XKPHYS_TO_PHYS(va));
241 if (va >= MIPS_KSEG0_START && va < MIPS_KSEG0_END)
242 a = (MIPS_KSEG0_TO_PHYS(va));
243 else if (va >= MIPS_KSEG1_START && va < MIPS_KSEG1_END)
244 a = (MIPS_KSEG1_TO_PHYS(va));
245 else if (va >= vm->hdr.kernbase) {
246 pteindex = (va - vm->hdr.kernbase) >> PAGE_SHIFT;
247 pte = ptemap[pteindex];
249 _kvm_err(kd, kd->program, "_kvm_vatop: pte not valid");
253 a = TLBLO_PTE_TO_PA(pte);
256 _kvm_err(kd, kd->program, "_kvm_vatop: virtual address 0x%lx "
257 "not minidumped", va);
261 ofs = hpt_find(kd, a);
263 _kvm_err(kd, kd->program, "_kvm_vatop: physical "
264 "address 0x%lx not in minidump", a);
269 return (PAGE_SIZE - offset);
273 _kvm_err(kd, 0, "invalid address (0x%lx)", va);