xref: /freebsd/lib/libkvm/kvm_minidump_i386.c (revision a3d9bf49b57923118c339642594246ef73872ee8)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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,
135 	    sizeof(uint32_t)) == -1) {
136 		return (-1);
137 	}
138 	off += i386_round_page(vmst->hdr.bitmapsize);
139 
140 	if (_kvm_pmap_init(kd, vmst->hdr.ptesize, off) == -1) {
141 		return (-1);
142 	}
143 	off += i386_round_page(vmst->hdr.ptesize);
144 
145 	return (0);
146 }
147 
148 static int
149 _i386_minidump_vatop_pae(kvm_t *kd, kvaddr_t va, off_t *pa)
150 {
151 	struct vmstate *vm;
152 	i386_physaddr_pae_t offset;
153 	i386_pte_pae_t pte;
154 	kvaddr_t pteindex;
155 	i386_physaddr_pae_t a;
156 	off_t ofs;
157 
158 	vm = kd->vmst;
159 	offset = va & I386_PAGE_MASK;
160 
161 	if (va >= vm->hdr.kernbase) {
162 		pteindex = (va - vm->hdr.kernbase) >> I386_PAGE_SHIFT;
163 		if (pteindex >= vm->hdr.ptesize / sizeof(pte))
164 			goto invalid;
165 		pte = _i386_pte_pae_get(kd, pteindex);
166 		if ((pte & I386_PG_V) == 0) {
167 			_kvm_err(kd, kd->program,
168 			    "_i386_minidump_vatop_pae: pte not valid");
169 			goto invalid;
170 		}
171 		a = pte & I386_PG_FRAME_PAE;
172 		ofs = _kvm_pt_find(kd, a, I386_PAGE_SIZE);
173 		if (ofs == -1) {
174 			_kvm_err(kd, kd->program,
175 	    "_i386_minidump_vatop_pae: physical address 0x%jx not in minidump",
176 			    (uintmax_t)a);
177 			goto invalid;
178 		}
179 		*pa = ofs + offset;
180 		return (I386_PAGE_SIZE - offset);
181 	} else {
182 		_kvm_err(kd, kd->program,
183 	    "_i386_minidump_vatop_pae: virtual address 0x%jx not minidumped",
184 		    (uintmax_t)va);
185 		goto invalid;
186 	}
187 
188 invalid:
189 	_kvm_err(kd, 0, "invalid address (0x%jx)", (uintmax_t)va);
190 	return (0);
191 }
192 
193 static int
194 _i386_minidump_vatop(kvm_t *kd, kvaddr_t va, off_t *pa)
195 {
196 	struct vmstate *vm;
197 	i386_physaddr_t offset;
198 	i386_pte_t pte;
199 	kvaddr_t pteindex;
200 	i386_physaddr_t a;
201 	off_t ofs;
202 
203 	vm = kd->vmst;
204 	offset = va & I386_PAGE_MASK;
205 
206 	if (va >= vm->hdr.kernbase) {
207 		pteindex = (va - vm->hdr.kernbase) >> I386_PAGE_SHIFT;
208 		if (pteindex >= vm->hdr.ptesize / sizeof(pte))
209 			goto invalid;
210 		pte = _i386_pte_get(kd, pteindex);
211 		if ((pte & I386_PG_V) == 0) {
212 			_kvm_err(kd, kd->program,
213 			    "_i386_minidump_vatop: pte not valid");
214 			goto invalid;
215 		}
216 		a = pte & I386_PG_FRAME;
217 		ofs = _kvm_pt_find(kd, a, I386_PAGE_SIZE);
218 		if (ofs == -1) {
219 			_kvm_err(kd, kd->program,
220 	    "_i386_minidump_vatop: physical address 0x%jx not in minidump",
221 			    (uintmax_t)a);
222 			goto invalid;
223 		}
224 		*pa = ofs + offset;
225 		return (I386_PAGE_SIZE - offset);
226 	} else {
227 		_kvm_err(kd, kd->program,
228 	    "_i386_minidump_vatop: virtual address 0x%jx not minidumped",
229 		    (uintmax_t)va);
230 		goto invalid;
231 	}
232 
233 invalid:
234 	_kvm_err(kd, 0, "invalid address (0x%jx)", (uintmax_t)va);
235 	return (0);
236 }
237 
238 static int
239 _i386_minidump_kvatop(kvm_t *kd, kvaddr_t va, off_t *pa)
240 {
241 
242 	if (ISALIVE(kd)) {
243 		_kvm_err(kd, 0, "_i386_minidump_kvatop called in live kernel!");
244 		return (0);
245 	}
246 	if (kd->vmst->hdr.paemode)
247 		return (_i386_minidump_vatop_pae(kd, va, pa));
248 	else
249 		return (_i386_minidump_vatop(kd, va, pa));
250 }
251 
252 static vm_prot_t
253 _i386_entry_to_prot(uint64_t pte)
254 {
255 	vm_prot_t prot = VM_PROT_READ;
256 
257 	/* Source: i386/pmap.c:pmap_protect() */
258 	if (pte & I386_PG_RW)
259 		prot |= VM_PROT_WRITE;
260 	if ((pte & I386_PG_NX) == 0)
261 		prot |= VM_PROT_EXECUTE;
262 
263 	return prot;
264 }
265 
266 struct i386_iter {
267 	kvm_t *kd;
268 	u_long nptes;
269 	u_long pteindex;
270 };
271 
272 static void
273 _i386_iterator_init(struct i386_iter *it, kvm_t *kd)
274 {
275 	struct vmstate *vm = kd->vmst;
276 
277 	it->kd = kd;
278 	it->pteindex = 0;
279 	if (vm->hdr.paemode) {
280 		it->nptes = vm->hdr.ptesize / sizeof(i386_pte_pae_t);
281 	} else {
282 		it->nptes = vm->hdr.ptesize / sizeof(i386_pte_t);
283 	}
284 	return;
285 }
286 
287 static int
288 _i386_iterator_next(struct i386_iter *it, u_long *pa, u_long *va, u_long *dva,
289     vm_prot_t *prot)
290 {
291 	struct vmstate *vm = it->kd->vmst;
292 	i386_pte_t pte32;
293 	i386_pte_pae_t pte64;
294 	int found = 0;
295 
296 	*dva = 0;
297 	*pa = 0;
298 	*va = 0;
299 	*dva = 0;
300 	*prot = 0;
301 	for (; it->pteindex < it->nptes && found == 0; it->pteindex++) {
302 		if (vm->hdr.paemode) {
303 			pte64 = _i386_pte_pae_get(it->kd, it->pteindex);
304 			if ((pte64 & I386_PG_V) == 0)
305 				continue;
306 			*prot = _i386_entry_to_prot(pte64);
307 			*pa = pte64 & I386_PG_FRAME_PAE;
308 		} else {
309 			pte32 = _i386_pte_get(it->kd, it->pteindex);
310 			if ((pte32 & I386_PG_V) == 0)
311 				continue;
312 			*prot = _i386_entry_to_prot(pte32);
313 			*pa = pte32 & I386_PG_FRAME;
314 		}
315 		*va = vm->hdr.kernbase + (it->pteindex << I386_PAGE_SHIFT);
316 		found = 1;
317 	}
318 	return found;
319 }
320 
321 static int
322 _i386_minidump_walk_pages(kvm_t *kd, kvm_walk_pages_cb_t *cb, void *arg)
323 {
324 	struct i386_iter it;
325 	u_long dva, pa, va;
326 	vm_prot_t prot;
327 
328 	_i386_iterator_init(&it, kd);
329 	while (_i386_iterator_next(&it, &pa, &va, &dva, &prot)) {
330 		if (!_kvm_visit_cb(kd, cb, arg, pa, va, dva,
331 		    prot, I386_PAGE_SIZE, 0)) {
332 			return (0);
333 		}
334 	}
335 	return (1);
336 }
337 
338 static struct kvm_arch kvm_i386_minidump = {
339 	.ka_probe = _i386_minidump_probe,
340 	.ka_initvtop = _i386_minidump_initvtop,
341 	.ka_freevtop = _i386_minidump_freevtop,
342 	.ka_kvatop = _i386_minidump_kvatop,
343 	.ka_native = _i386_native,
344 	.ka_walk_pages = _i386_minidump_walk_pages,
345 };
346 
347 KVM_ARCH(kvm_i386_minidump);
348