]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libkvm/kvm_minidump_i386.c
libpfctl: handle allocation failure
[FreeBSD/FreeBSD.git] / lib / libkvm / kvm_minidump_i386.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
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
28 #include <sys/cdefs.h>
29 /*
30  * i386 machine dependent routines for kvm and minidumps.
31  */
32
33 #include <sys/param.h>
34 #include <sys/endian.h>
35 #include <stdint.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include <vm/vm.h>
40 #include <kvm.h>
41
42 #include "../../sys/i386/include/minidump.h"
43
44 #include <limits.h>
45
46 #include "kvm_private.h"
47 #include "kvm_i386.h"
48
49 #define i386_round_page(x)      roundup2((kvaddr_t)(x), I386_PAGE_SIZE)
50
51 struct vmstate {
52         struct minidumphdr hdr;
53 };
54
55 static i386_pte_pae_t
56 _i386_pte_pae_get(kvm_t *kd, u_long pteindex)
57 {
58         i386_pte_pae_t *pte = _kvm_pmap_get(kd, pteindex, sizeof(*pte));
59
60         return le64toh(*pte);
61 }
62
63 static i386_pte_t
64 _i386_pte_get(kvm_t *kd, u_long pteindex)
65 {
66         i386_pte_t *pte = _kvm_pmap_get(kd, pteindex, sizeof(*pte));
67
68         return le32toh(*pte);
69 }
70
71 static int
72 _i386_minidump_probe(kvm_t *kd)
73 {
74
75         return (_kvm_probe_elf_kernel(kd, ELFCLASS32, EM_386) &&
76             _kvm_is_minidump(kd));
77 }
78
79 static void
80 _i386_minidump_freevtop(kvm_t *kd)
81 {
82         struct vmstate *vm = kd->vmst;
83
84         free(vm);
85         kd->vmst = NULL;
86 }
87
88 static int
89 _i386_minidump_initvtop(kvm_t *kd)
90 {
91         struct vmstate *vmst;
92         off_t off, dump_avail_off, sparse_off;
93
94         vmst = _kvm_malloc(kd, sizeof(*vmst));
95         if (vmst == NULL) {
96                 _kvm_err(kd, kd->program, "cannot allocate vm");
97                 return (-1);
98         }
99         kd->vmst = vmst;
100         if (pread(kd->pmfd, &vmst->hdr, sizeof(vmst->hdr), 0) !=
101             sizeof(vmst->hdr)) {
102                 _kvm_err(kd, kd->program, "cannot read dump header");
103                 return (-1);
104         }
105         if (strncmp(MINIDUMP_MAGIC, vmst->hdr.magic, sizeof(vmst->hdr.magic)) != 0) {
106                 _kvm_err(kd, kd->program, "not a minidump for this platform");
107                 return (-1);
108         }
109         vmst->hdr.version = le32toh(vmst->hdr.version);
110         if (vmst->hdr.version != MINIDUMP_VERSION && vmst->hdr.version != 1) {
111                 _kvm_err(kd, kd->program, "wrong minidump version. expected %d got %d",
112                     MINIDUMP_VERSION, vmst->hdr.version);
113                 return (-1);
114         }
115         vmst->hdr.msgbufsize = le32toh(vmst->hdr.msgbufsize);
116         vmst->hdr.bitmapsize = le32toh(vmst->hdr.bitmapsize);
117         vmst->hdr.ptesize = le32toh(vmst->hdr.ptesize);
118         vmst->hdr.kernbase = le32toh(vmst->hdr.kernbase);
119         vmst->hdr.paemode = le32toh(vmst->hdr.paemode);
120         vmst->hdr.dumpavailsize = vmst->hdr.version == MINIDUMP_VERSION ?
121             le32toh(vmst->hdr.dumpavailsize) : 0;
122
123         /* Skip header and msgbuf */
124         dump_avail_off = I386_PAGE_SIZE + i386_round_page(vmst->hdr.msgbufsize);
125
126         /* Skip dump_avail */
127         off = dump_avail_off + i386_round_page(vmst->hdr.dumpavailsize);
128
129         sparse_off = off + i386_round_page(vmst->hdr.bitmapsize) +
130             i386_round_page(vmst->hdr.ptesize);
131         if (_kvm_pt_init(kd, vmst->hdr.dumpavailsize, dump_avail_off,
132             vmst->hdr.bitmapsize, off, sparse_off, I386_PAGE_SIZE) == -1) {
133                 return (-1);
134         }
135         off += i386_round_page(vmst->hdr.bitmapsize);
136
137         if (_kvm_pmap_init(kd, vmst->hdr.ptesize, off) == -1) {
138                 return (-1);
139         }
140         off += i386_round_page(vmst->hdr.ptesize);
141
142         return (0);
143 }
144
145 static int
146 _i386_minidump_vatop_pae(kvm_t *kd, kvaddr_t va, off_t *pa)
147 {
148         struct vmstate *vm;
149         i386_physaddr_pae_t offset;
150         i386_pte_pae_t pte;
151         kvaddr_t pteindex;
152         i386_physaddr_pae_t a;
153         off_t ofs;
154
155         vm = kd->vmst;
156         offset = va & I386_PAGE_MASK;
157
158         if (va >= vm->hdr.kernbase) {
159                 pteindex = (va - vm->hdr.kernbase) >> I386_PAGE_SHIFT;
160                 if (pteindex >= vm->hdr.ptesize / sizeof(pte))
161                         goto invalid;
162                 pte = _i386_pte_pae_get(kd, pteindex);
163                 if ((pte & I386_PG_V) == 0) {
164                         _kvm_err(kd, kd->program,
165                             "_i386_minidump_vatop_pae: pte not valid");
166                         goto invalid;
167                 }
168                 a = pte & I386_PG_FRAME_PAE;
169                 ofs = _kvm_pt_find(kd, a, I386_PAGE_SIZE);
170                 if (ofs == -1) {
171                         _kvm_err(kd, kd->program,
172             "_i386_minidump_vatop_pae: physical address 0x%jx not in minidump",
173                             (uintmax_t)a);
174                         goto invalid;
175                 }
176                 *pa = ofs + offset;
177                 return (I386_PAGE_SIZE - offset);
178         } else {
179                 _kvm_err(kd, kd->program,
180             "_i386_minidump_vatop_pae: virtual address 0x%jx not minidumped",
181                     (uintmax_t)va);
182                 goto invalid;
183         }
184
185 invalid:
186         _kvm_err(kd, 0, "invalid address (0x%jx)", (uintmax_t)va);
187         return (0);
188 }
189
190 static int
191 _i386_minidump_vatop(kvm_t *kd, kvaddr_t va, off_t *pa)
192 {
193         struct vmstate *vm;
194         i386_physaddr_t offset;
195         i386_pte_t pte;
196         kvaddr_t pteindex;
197         i386_physaddr_t a;
198         off_t ofs;
199
200         vm = kd->vmst;
201         offset = va & I386_PAGE_MASK;
202
203         if (va >= vm->hdr.kernbase) {
204                 pteindex = (va - vm->hdr.kernbase) >> I386_PAGE_SHIFT;
205                 if (pteindex >= vm->hdr.ptesize / sizeof(pte))
206                         goto invalid;
207                 pte = _i386_pte_get(kd, pteindex);
208                 if ((pte & I386_PG_V) == 0) {
209                         _kvm_err(kd, kd->program,
210                             "_i386_minidump_vatop: pte not valid");
211                         goto invalid;
212                 }
213                 a = pte & I386_PG_FRAME;
214                 ofs = _kvm_pt_find(kd, a, I386_PAGE_SIZE);
215                 if (ofs == -1) {
216                         _kvm_err(kd, kd->program,
217             "_i386_minidump_vatop: physical address 0x%jx not in minidump",
218                             (uintmax_t)a);
219                         goto invalid;
220                 }
221                 *pa = ofs + offset;
222                 return (I386_PAGE_SIZE - offset);
223         } else {
224                 _kvm_err(kd, kd->program,
225             "_i386_minidump_vatop: virtual address 0x%jx not minidumped",
226                     (uintmax_t)va);
227                 goto invalid;
228         }
229
230 invalid:
231         _kvm_err(kd, 0, "invalid address (0x%jx)", (uintmax_t)va);
232         return (0);
233 }
234
235 static int
236 _i386_minidump_kvatop(kvm_t *kd, kvaddr_t va, off_t *pa)
237 {
238
239         if (ISALIVE(kd)) {
240                 _kvm_err(kd, 0, "_i386_minidump_kvatop called in live kernel!");
241                 return (0);
242         }
243         if (kd->vmst->hdr.paemode)
244                 return (_i386_minidump_vatop_pae(kd, va, pa));
245         else
246                 return (_i386_minidump_vatop(kd, va, pa));
247 }
248
249 static vm_prot_t
250 _i386_entry_to_prot(uint64_t pte)
251 {
252         vm_prot_t prot = VM_PROT_READ;
253
254         /* Source: i386/pmap.c:pmap_protect() */
255         if (pte & I386_PG_RW)
256                 prot |= VM_PROT_WRITE;
257         if ((pte & I386_PG_NX) == 0)
258                 prot |= VM_PROT_EXECUTE;
259
260         return prot;
261 }
262
263 struct i386_iter {
264         kvm_t *kd;
265         u_long nptes;
266         u_long pteindex;
267 };
268
269 static void
270 _i386_iterator_init(struct i386_iter *it, kvm_t *kd)
271 {
272         struct vmstate *vm = kd->vmst;
273
274         it->kd = kd;
275         it->pteindex = 0;
276         if (vm->hdr.paemode) {
277                 it->nptes = vm->hdr.ptesize / sizeof(i386_pte_pae_t);
278         } else {
279                 it->nptes = vm->hdr.ptesize / sizeof(i386_pte_t);
280         }
281         return;
282 }
283
284 static int
285 _i386_iterator_next(struct i386_iter *it, u_long *pa, u_long *va, u_long *dva,
286     vm_prot_t *prot)
287 {
288         struct vmstate *vm = it->kd->vmst;
289         i386_pte_t pte32;
290         i386_pte_pae_t pte64;
291         int found = 0;
292
293         *dva = 0;
294         *pa = 0;
295         *va = 0;
296         *dva = 0;
297         *prot = 0;
298         for (; it->pteindex < it->nptes && found == 0; it->pteindex++) {
299                 if (vm->hdr.paemode) {
300                         pte64 = _i386_pte_pae_get(it->kd, it->pteindex);
301                         if ((pte64 & I386_PG_V) == 0)
302                                 continue;
303                         *prot = _i386_entry_to_prot(pte64);
304                         *pa = pte64 & I386_PG_FRAME_PAE;
305                 } else {
306                         pte32 = _i386_pte_get(it->kd, it->pteindex);
307                         if ((pte32 & I386_PG_V) == 0)
308                                 continue;
309                         *prot = _i386_entry_to_prot(pte32);
310                         *pa = pte32 & I386_PG_FRAME;
311                 }
312                 *va = vm->hdr.kernbase + (it->pteindex << I386_PAGE_SHIFT);
313                 found = 1;
314         }
315         return found;
316 }
317
318 static int
319 _i386_minidump_walk_pages(kvm_t *kd, kvm_walk_pages_cb_t *cb, void *arg)
320 {
321         struct i386_iter it;
322         u_long dva, pa, va;
323         vm_prot_t prot;
324
325         _i386_iterator_init(&it, kd);
326         while (_i386_iterator_next(&it, &pa, &va, &dva, &prot)) {
327                 if (!_kvm_visit_cb(kd, cb, arg, pa, va, dva,
328                     prot, I386_PAGE_SIZE, 0)) {
329                         return (0);
330                 }
331         }
332         return (1);
333 }
334
335 static struct kvm_arch kvm_i386_minidump = {
336         .ka_probe = _i386_minidump_probe,
337         .ka_initvtop = _i386_minidump_initvtop,
338         .ka_freevtop = _i386_minidump_freevtop,
339         .ka_kvatop = _i386_minidump_kvatop,
340         .ka_native = _i386_native,
341         .ka_walk_pages = _i386_minidump_walk_pages,
342 };
343
344 KVM_ARCH(kvm_i386_minidump);