]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libkvm/kvm_minidump_mips.c
MFV r315875:
[FreeBSD/FreeBSD.git] / lib / libkvm / kvm_minidump_mips.c
1 /*-
2  * Copyright (c) 2010 Oleksandr Tymoshenko
3  * Copyright (c) 2008 Semihalf, Grzegorz Bernacki
4  * Copyright (c) 2006 Peter Wemm
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  *
27  * From: FreeBSD: src/lib/libkvm/kvm_minidump_arm.c r214223
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 /*
34  * MIPS machine dependent routines for kvm and minidumps.
35  */
36
37 #include <sys/param.h>
38 #include <kvm.h>
39 #include <limits.h>
40 #include <stdint.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44
45 #include "../../sys/mips/include/cpuregs.h"
46 #include "../../sys/mips/include/minidump.h"
47
48 #include "kvm_private.h"
49 #include "kvm_mips.h"
50
51 #define mips_round_page(x)      roundup2((kvaddr_t)(x), MIPS_PAGE_SIZE)
52
53 struct vmstate {
54         struct          minidumphdr hdr;
55         void            *ptemap;
56         int             pte_size;
57 };
58
59 static int
60 _mips_minidump_probe(kvm_t *kd)
61 {
62
63         if (kd->nlehdr.e_ident[EI_CLASS] != ELFCLASS32 &&
64             kd->nlehdr.e_ident[EI_CLASS] != ELFCLASS64)
65                 return (0);
66         if (kd->nlehdr.e_machine != EM_MIPS)
67                 return (0);
68         return (_kvm_is_minidump(kd));
69 }
70
71 static void
72 _mips_minidump_freevtop(kvm_t *kd)
73 {
74         struct vmstate *vm = kd->vmst;
75
76         free(vm->ptemap);
77         free(vm);
78         kd->vmst = NULL;
79 }
80
81 static int
82 _mips_minidump_initvtop(kvm_t *kd)
83 {
84         struct vmstate *vmst;
85         off_t off, sparse_off;
86
87         vmst = _kvm_malloc(kd, sizeof(*vmst));
88         if (vmst == NULL) {
89                 _kvm_err(kd, kd->program, "cannot allocate vm");
90                 return (-1);
91         }
92
93         kd->vmst = vmst;
94
95         if (kd->nlehdr.e_ident[EI_CLASS] == ELFCLASS64 ||
96             kd->nlehdr.e_flags & EF_MIPS_ABI2)
97                 vmst->pte_size = 64;
98         else
99                 vmst->pte_size = 32;
100
101         if (pread(kd->pmfd, &vmst->hdr,
102             sizeof(vmst->hdr), 0) != sizeof(vmst->hdr)) {
103                 _kvm_err(kd, kd->program, "cannot read dump header");
104                 return (-1);
105         }
106
107         if (strncmp(MINIDUMP_MAGIC, vmst->hdr.magic,
108             sizeof(vmst->hdr.magic)) != 0) {
109                 _kvm_err(kd, kd->program, "not a minidump for this platform");
110                 return (-1);
111         }
112         vmst->hdr.version = _kvm32toh(kd, vmst->hdr.version);
113         if (vmst->hdr.version != MINIDUMP_VERSION) {
114                 _kvm_err(kd, kd->program, "wrong minidump version. "
115                     "Expected %d got %d", MINIDUMP_VERSION, vmst->hdr.version);
116                 return (-1);
117         }
118         vmst->hdr.msgbufsize = _kvm32toh(kd, vmst->hdr.msgbufsize);
119         vmst->hdr.bitmapsize = _kvm32toh(kd, vmst->hdr.bitmapsize);
120         vmst->hdr.ptesize = _kvm32toh(kd, vmst->hdr.ptesize);
121         vmst->hdr.kernbase = _kvm64toh(kd, vmst->hdr.kernbase);
122         vmst->hdr.dmapbase = _kvm64toh(kd, vmst->hdr.dmapbase);
123         vmst->hdr.dmapend = _kvm64toh(kd, vmst->hdr.dmapend);
124
125         /* Skip header and msgbuf */
126         off = MIPS_PAGE_SIZE + mips_round_page(vmst->hdr.msgbufsize);
127
128         sparse_off = off + mips_round_page(vmst->hdr.bitmapsize) +
129             mips_round_page(vmst->hdr.ptesize);
130         if (_kvm_pt_init(kd, vmst->hdr.bitmapsize, off, sparse_off,
131             MIPS_PAGE_SIZE, sizeof(uint32_t)) == -1) {
132                 _kvm_err(kd, kd->program, "cannot load core bitmap");
133                 return (-1);
134         }
135         off += mips_round_page(vmst->hdr.bitmapsize);
136
137         vmst->ptemap = _kvm_malloc(kd, vmst->hdr.ptesize);
138         if (vmst->ptemap == NULL) {
139                 _kvm_err(kd, kd->program, "cannot allocate %d bytes for "
140                     "ptemap", vmst->hdr.ptesize);
141                 return (-1);
142         }
143
144         if (pread(kd->pmfd, vmst->ptemap, vmst->hdr.ptesize, off) !=
145             (ssize_t)vmst->hdr.ptesize) {
146                 _kvm_err(kd, kd->program, "cannot read %d bytes for ptemap",
147                     vmst->hdr.ptesize);
148                 return (-1);
149         }
150         off += mips_round_page(vmst->hdr.ptesize);
151
152         return (0);
153 }
154
155 static int
156 _mips_minidump_kvatop(kvm_t *kd, kvaddr_t va, off_t *pa)
157 {
158         struct vmstate *vm;
159         uint64_t pte;
160         mips_physaddr_t offset, a;
161         kvaddr_t pteindex;
162         off_t ofs;
163         uint32_t *ptemap32;
164         uint64_t *ptemap64;
165
166         if (ISALIVE(kd)) {
167                 _kvm_err(kd, 0, "_mips_minidump_kvatop called in live kernel!");
168                 return (0);
169         }
170
171         offset = va & MIPS_PAGE_MASK;
172         /* Operate with page-aligned address */
173         va &= ~MIPS_PAGE_MASK;
174
175         vm = kd->vmst;
176         ptemap32 = vm->ptemap;
177         ptemap64 = vm->ptemap;
178
179         if (kd->nlehdr.e_ident[EI_CLASS] == ELFCLASS64) {
180                 if (va >= MIPS_XKPHYS_START && va < MIPS_XKPHYS_END) {
181                         a = va & MIPS_XKPHYS_PHYS_MASK;
182                         goto found;
183                 }
184                 if (va >= MIPS64_KSEG0_START && va < MIPS64_KSEG0_END) {
185                         a = va & MIPS_KSEG0_PHYS_MASK;
186                         goto found;
187                 }
188                 if (va >= MIPS64_KSEG1_START && va < MIPS64_KSEG1_END) {
189                         a = va & MIPS_KSEG0_PHYS_MASK;
190                         goto found;
191                 }
192         } else {
193                 if (va >= MIPS32_KSEG0_START && va < MIPS32_KSEG0_END) {
194                         a = va & MIPS_KSEG0_PHYS_MASK;
195                         goto found;
196                 }
197                 if (va >= MIPS32_KSEG1_START && va < MIPS32_KSEG1_END) {
198                         a = va & MIPS_KSEG0_PHYS_MASK;
199                         goto found;
200                 }
201         }
202         if (va >= vm->hdr.kernbase) {
203                 pteindex = (va - vm->hdr.kernbase) >> MIPS_PAGE_SHIFT;
204                 if (vm->pte_size == 64) {
205                         if (pteindex >= vm->hdr.ptesize / sizeof(*ptemap64))
206                                 goto invalid;
207                         pte = _kvm64toh(kd, ptemap64[pteindex]);
208                         a = MIPS64_PTE_TO_PA(pte);
209                 } else {
210                         if (pteindex >= vm->hdr.ptesize / sizeof(*ptemap32))
211                                 goto invalid;
212                         pte = _kvm32toh(kd, ptemap32[pteindex]);
213                         a = MIPS32_PTE_TO_PA(pte);
214                 }
215                 if (!pte) {
216                         _kvm_err(kd, kd->program, "_mips_minidump_kvatop: pte "
217                             "not valid");
218                         goto invalid;
219                 }
220         } else {
221                 _kvm_err(kd, kd->program, "_mips_minidump_kvatop: virtual "
222                     "address 0x%jx not minidumped", (uintmax_t)va);
223                 return (0);
224         }
225
226 found:
227         ofs = _kvm_pt_find(kd, a);
228         if (ofs == -1) {
229                 _kvm_err(kd, kd->program, "_mips_minidump_kvatop: physical "
230                     "address 0x%jx not in minidump", (uintmax_t)a);
231                 goto invalid;
232         }
233
234         *pa = ofs + offset;
235         return (MIPS_PAGE_SIZE - offset);
236
237
238 invalid:
239         _kvm_err(kd, 0, "invalid address (0x%jx)", (uintmax_t)va);
240         return (0);
241 }
242
243 static int
244 #ifdef __mips__
245 _mips_native(kvm_t *kd)
246 #else
247 _mips_native(kvm_t *kd __unused)
248 #endif
249 {
250
251 #ifdef __mips__
252 #ifdef __mips_n64
253         if (kd->nlehdr.e_ident[EI_CLASS] != ELFCLASS64)
254                 return (0);
255 #else
256         if (kd->nlehdr.e_ident[EI_CLASS] != ELFCLASS32)
257                 return (0);
258 #ifdef __mips_n32
259         if (!(kd->nlehdr.e_flags & EF_MIPS_ABI2))
260                 return (0);
261 #else
262         if (kd->nlehdr.e_flags & EF_MIPS_ABI2)
263                 return (0);
264 #endif
265 #endif
266 #if _BYTE_ORDER == _LITTLE_ENDIAN
267         return (kd->nlehdr.e_ident[EI_DATA] == ELFDATA2LSB);
268 #else
269         return (kd->nlehdr.e_ident[EI_DATA] == ELFDATA2MSB);
270 #endif
271 #else
272         return (0);
273 #endif
274 }
275
276 static struct kvm_arch kvm_mips_minidump = {
277         .ka_probe = _mips_minidump_probe,
278         .ka_initvtop = _mips_minidump_initvtop,
279         .ka_freevtop = _mips_minidump_freevtop,
280         .ka_kvatop = _mips_minidump_kvatop,
281         .ka_native = _mips_native,
282 };
283
284 KVM_ARCH(kvm_mips_minidump);