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)
143 struct vmstate *vmst;
146 vmst = _kvm_malloc(kd, sizeof(*vmst));
148 _kvm_err(kd, kd->program, "cannot allocate vm");
155 off = lseek(kd->pmfd, 0, SEEK_CUR);
156 if (pread(kd->pmfd, &vmst->hdr,
157 sizeof(vmst->hdr), 0) != sizeof(vmst->hdr)) {
158 _kvm_err(kd, kd->program, "cannot read dump header");
162 if (strncmp(MINIDUMP_MAGIC, vmst->hdr.magic,
163 sizeof(vmst->hdr.magic)) != 0) {
164 _kvm_err(kd, kd->program, "not a minidump for this platform");
167 if (vmst->hdr.version != MINIDUMP_VERSION) {
168 _kvm_err(kd, kd->program, "wrong minidump version. "
169 "Expected %d got %d", MINIDUMP_VERSION, vmst->hdr.version);
173 /* Skip header and msgbuf */
174 off = PAGE_SIZE + round_page(vmst->hdr.msgbufsize);
176 vmst->bitmap = _kvm_malloc(kd, vmst->hdr.bitmapsize);
177 if (vmst->bitmap == NULL) {
178 _kvm_err(kd, kd->program, "cannot allocate %d bytes for "
179 "bitmap", vmst->hdr.bitmapsize);
183 if (pread(kd->pmfd, vmst->bitmap, vmst->hdr.bitmapsize, off) !=
184 (ssize_t)vmst->hdr.bitmapsize) {
185 _kvm_err(kd, kd->program, "cannot read %d bytes for page bitmap",
186 vmst->hdr.bitmapsize);
189 off += round_page(vmst->hdr.bitmapsize);
191 vmst->ptemap = _kvm_malloc(kd, vmst->hdr.ptesize);
192 if (vmst->ptemap == NULL) {
193 _kvm_err(kd, kd->program, "cannot allocate %d bytes for "
194 "ptemap", vmst->hdr.ptesize);
198 if (pread(kd->pmfd, vmst->ptemap, vmst->hdr.ptesize, off) !=
199 (ssize_t)vmst->hdr.ptesize) {
200 _kvm_err(kd, kd->program, "cannot read %d bytes for ptemap",
205 off += vmst->hdr.ptesize;
207 /* Build physical address hash table for sparse pages */
208 inithash(kd, vmst->bitmap, vmst->hdr.bitmapsize, off);
214 _kvm_minidump_kvatop(kvm_t *kd, u_long va, off_t *pa)
218 u_long offset, pteindex, a;
223 _kvm_err(kd, 0, "kvm_kvatop called in live kernel!");
227 offset = va & PAGE_MASK;
228 /* Operate with page-aligned address */
234 #if defined(__mips_n64)
235 if (va >= MIPS_XKPHYS_START && va < MIPS_XKPHYS_END)
236 a = (MIPS_XKPHYS_TO_PHYS(va));
239 if (va >= (u_long)MIPS_KSEG0_START && va < (u_long)MIPS_KSEG0_END)
240 a = (MIPS_KSEG0_TO_PHYS(va));
241 else if (va >= (u_long)MIPS_KSEG1_START && va < (u_long)MIPS_KSEG1_END)
242 a = (MIPS_KSEG1_TO_PHYS(va));
243 else if (va >= vm->hdr.kernbase) {
244 pteindex = (va - vm->hdr.kernbase) >> PAGE_SHIFT;
245 pte = ptemap[pteindex];
247 _kvm_err(kd, kd->program, "_kvm_vatop: pte not valid");
251 a = TLBLO_PTE_TO_PA(pte);
254 _kvm_err(kd, kd->program, "_kvm_vatop: virtual address 0x%lx "
255 "not minidumped", va);
259 ofs = hpt_find(kd, a);
261 _kvm_err(kd, kd->program, "_kvm_vatop: physical "
262 "address 0x%lx not in minidump", a);
267 return (PAGE_SIZE - offset);
271 _kvm_err(kd, 0, "invalid address (0x%lx)", va);