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