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>
45 #include "../../sys/mips/include/cpuregs.h"
46 #include "../../sys/mips/include/minidump.h"
48 #include "kvm_private.h"
51 #define mips_round_page(x) roundup2((kvaddr_t)(x), MIPS_PAGE_SIZE)
54 struct minidumphdr hdr;
59 _mips_minidump_probe(kvm_t *kd)
62 if (kd->nlehdr.e_ident[EI_CLASS] != ELFCLASS32 &&
63 kd->nlehdr.e_ident[EI_CLASS] != ELFCLASS64)
65 if (kd->nlehdr.e_machine != EM_MIPS)
67 return (_kvm_is_minidump(kd));
71 _mips_minidump_freevtop(kvm_t *kd)
73 struct vmstate *vm = kd->vmst;
80 _mips_minidump_initvtop(kvm_t *kd)
83 off_t off, sparse_off;
85 vmst = _kvm_malloc(kd, sizeof(*vmst));
87 _kvm_err(kd, kd->program, "cannot allocate vm");
93 if (kd->nlehdr.e_ident[EI_CLASS] == ELFCLASS64 ||
94 kd->nlehdr.e_flags & EF_MIPS_ABI2)
99 if (pread(kd->pmfd, &vmst->hdr,
100 sizeof(vmst->hdr), 0) != sizeof(vmst->hdr)) {
101 _kvm_err(kd, kd->program, "cannot read dump header");
105 if (strncmp(MINIDUMP_MAGIC, vmst->hdr.magic,
106 sizeof(vmst->hdr.magic)) != 0) {
107 _kvm_err(kd, kd->program, "not a minidump for this platform");
110 vmst->hdr.version = _kvm32toh(kd, vmst->hdr.version);
111 if (vmst->hdr.version != MINIDUMP_VERSION) {
112 _kvm_err(kd, kd->program, "wrong minidump version. "
113 "Expected %d got %d", MINIDUMP_VERSION, vmst->hdr.version);
116 vmst->hdr.msgbufsize = _kvm32toh(kd, vmst->hdr.msgbufsize);
117 vmst->hdr.bitmapsize = _kvm32toh(kd, vmst->hdr.bitmapsize);
118 vmst->hdr.ptesize = _kvm32toh(kd, vmst->hdr.ptesize);
119 vmst->hdr.kernbase = _kvm64toh(kd, vmst->hdr.kernbase);
120 vmst->hdr.dmapbase = _kvm64toh(kd, vmst->hdr.dmapbase);
121 vmst->hdr.dmapend = _kvm64toh(kd, vmst->hdr.dmapend);
123 /* Skip header and msgbuf */
124 off = MIPS_PAGE_SIZE + mips_round_page(vmst->hdr.msgbufsize);
126 sparse_off = off + mips_round_page(vmst->hdr.bitmapsize) +
127 mips_round_page(vmst->hdr.ptesize);
128 if (_kvm_pt_init(kd, vmst->hdr.bitmapsize, off, sparse_off,
129 MIPS_PAGE_SIZE, sizeof(uint32_t)) == -1) {
132 off += mips_round_page(vmst->hdr.bitmapsize);
134 if (_kvm_pmap_init(kd, vmst->hdr.ptesize, off) == -1) {
137 off += mips_round_page(vmst->hdr.ptesize);
143 _mips_minidump_kvatop(kvm_t *kd, kvaddr_t va, off_t *pa)
146 mips_physaddr_t offset, a;
154 _kvm_err(kd, 0, "_mips_minidump_kvatop called in live kernel!");
158 offset = va & MIPS_PAGE_MASK;
159 /* Operate with page-aligned address */
160 va &= ~MIPS_PAGE_MASK;
163 if (kd->nlehdr.e_ident[EI_CLASS] == ELFCLASS64) {
164 if (va >= MIPS_XKPHYS_START && va < MIPS_XKPHYS_END) {
165 a = va & MIPS_XKPHYS_PHYS_MASK;
168 if (va >= MIPS64_KSEG0_START && va < MIPS64_KSEG0_END) {
169 a = va & MIPS_KSEG0_PHYS_MASK;
172 if (va >= MIPS64_KSEG1_START && va < MIPS64_KSEG1_END) {
173 a = va & MIPS_KSEG0_PHYS_MASK;
177 if (va >= MIPS32_KSEG0_START && va < MIPS32_KSEG0_END) {
178 a = va & MIPS_KSEG0_PHYS_MASK;
181 if (va >= MIPS32_KSEG1_START && va < MIPS32_KSEG1_END) {
182 a = va & MIPS_KSEG0_PHYS_MASK;
186 if (va >= vm->hdr.kernbase) {
187 pteindex = (va - vm->hdr.kernbase) >> MIPS_PAGE_SHIFT;
188 if (vm->pte_size == 64) {
189 valid = pteindex < vm->hdr.ptesize / sizeof(pte64);
190 if (pteindex >= vm->hdr.ptesize / sizeof(pte64))
192 pte64 = _mips64_pte_get(kd, pteindex);
193 valid = pte64 & MIPS_PTE_V;
195 a = MIPS64_PTE_TO_PA(pte64);
197 if (pteindex >= vm->hdr.ptesize / sizeof(pte32))
199 pte32 = _mips32_pte_get(kd, pteindex);
200 valid = pte32 & MIPS_PTE_V;
202 a = MIPS32_PTE_TO_PA(pte32);
205 _kvm_err(kd, kd->program, "_mips_minidump_kvatop: pte "
210 _kvm_err(kd, kd->program, "_mips_minidump_kvatop: virtual "
211 "address 0x%jx not minidumped", (uintmax_t)va);
216 ofs = _kvm_pt_find(kd, a, MIPS_PAGE_SIZE);
218 _kvm_err(kd, kd->program, "_mips_minidump_kvatop: physical "
219 "address 0x%jx not in minidump", (uintmax_t)a);
224 return (MIPS_PAGE_SIZE - offset);
228 _kvm_err(kd, 0, "invalid address (0x%jx)", (uintmax_t)va);
234 _mips_native(kvm_t *kd)
236 _mips_native(kvm_t *kd __unused)
242 if (kd->nlehdr.e_ident[EI_CLASS] != ELFCLASS64)
245 if (kd->nlehdr.e_ident[EI_CLASS] != ELFCLASS32)
248 if (!(kd->nlehdr.e_flags & EF_MIPS_ABI2))
251 if (kd->nlehdr.e_flags & EF_MIPS_ABI2)
255 #if _BYTE_ORDER == _LITTLE_ENDIAN
256 return (kd->nlehdr.e_ident[EI_DATA] == ELFDATA2LSB);
258 return (kd->nlehdr.e_ident[EI_DATA] == ELFDATA2MSB);
272 _mips_iterator_init(struct mips_iter *it, kvm_t *kd)
274 struct vmstate *vm = kd->vmst;
278 if (vm->pte_size == 64)
279 it->nptes = vm->hdr.ptesize / sizeof(mips64_pte_t);
281 it->nptes = vm->hdr.ptesize / sizeof(mips32_pte_t);
286 _mips_iterator_next(struct mips_iter *it, u_long *pa, u_long *va, u_long *dva,
289 struct vmstate *vm = it->kd->vmst;
295 * mips/mips/pmap.c: init_pte_prot / pmap_protect indicate that all
296 * pages are R|X at least.
298 *prot = VM_PROT_READ | VM_PROT_EXECUTE;
300 for (;it->pteindex < it->nptes && found == 0; it->pteindex++) {
301 if (vm->pte_size == 64) {
302 pte64 = _mips64_pte_get(it->kd, it->pteindex);
303 if ((pte64 & MIPS_PTE_V) == 0)
305 if ((pte64 & MIPS64_PTE_RO) == 0)
306 *prot |= VM_PROT_WRITE;
307 *pa = MIPS64_PTE_TO_PA(pte64);
309 pte32 = _mips32_pte_get(it->kd, it->pteindex);
310 if ((pte32 & MIPS_PTE_V) == 0)
312 if ((pte32 & MIPS32_PTE_RO) == 0)
313 *prot |= VM_PROT_WRITE;
314 *pa = MIPS32_PTE_TO_PA(pte32);
316 *va = vm->hdr.kernbase + (it->pteindex << MIPS_PAGE_SHIFT);
318 /* advance pteindex regardless */
325 _mips_minidump_walk_pages(kvm_t *kd, kvm_walk_pages_cb_t *cb, void *arg)
331 /* Generate direct mapped entries; need page entries for prot etc? */
332 if (kd->nlehdr.e_ident[EI_CLASS] == ELFCLASS64) {
333 /* MIPS_XKPHYS_START..MIPS_XKPHYS_END */
334 /* MIPS64_KSEG0_START..MIPS64_KSEG0_END */
335 /* MIPS64_KSEG1_START..MIPS64_KSEG1_START */
337 /* MIPS32_KSEG0_START..MIPS32_KSEG0_END */
338 /* MIPS32_KSEG1_START..MIPS32_KSEG1_END */
341 _mips_iterator_init(&it, kd);
342 while (_mips_iterator_next(&it, &pa, &va, &dva, &prot)) {
343 if (!_kvm_visit_cb(kd, cb, arg, pa, va, dva,
344 prot, MIPS_PAGE_SIZE, 0)) {
351 static struct kvm_arch kvm_mips_minidump = {
352 .ka_probe = _mips_minidump_probe,
353 .ka_initvtop = _mips_minidump_initvtop,
354 .ka_freevtop = _mips_minidump_freevtop,
355 .ka_kvatop = _mips_minidump_kvatop,
356 .ka_native = _mips_native,
357 .ka_walk_pages = _mips_minidump_walk_pages,
360 KVM_ARCH(kvm_mips_minidump);