xref: /freebsd/lib/libkvm/kvm_minidump_i386.c (revision ca987d4641cdcd7f27e153db17c5bf064934faf5)
1 /*-
2  * Copyright (c) 2006 Peter Wemm
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28 
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, 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) {
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 
121 	/* Skip header and msgbuf */
122 	off = I386_PAGE_SIZE + i386_round_page(vmst->hdr.msgbufsize);
123 
124 	sparse_off = off + i386_round_page(vmst->hdr.bitmapsize) +
125 	    i386_round_page(vmst->hdr.ptesize);
126 	if (_kvm_pt_init(kd, vmst->hdr.bitmapsize, off, sparse_off,
127 	    I386_PAGE_SIZE, sizeof(uint32_t)) == -1) {
128 		return (-1);
129 	}
130 	off += i386_round_page(vmst->hdr.bitmapsize);
131 
132 	if (_kvm_pmap_init(kd, vmst->hdr.ptesize, off) == -1) {
133 		return (-1);
134 	}
135 	off += i386_round_page(vmst->hdr.ptesize);
136 
137 	return (0);
138 }
139 
140 static int
141 _i386_minidump_vatop_pae(kvm_t *kd, kvaddr_t va, off_t *pa)
142 {
143 	struct vmstate *vm;
144 	i386_physaddr_pae_t offset;
145 	i386_pte_pae_t pte;
146 	kvaddr_t pteindex;
147 	i386_physaddr_pae_t a;
148 	off_t ofs;
149 
150 	vm = kd->vmst;
151 	offset = va & I386_PAGE_MASK;
152 
153 	if (va >= vm->hdr.kernbase) {
154 		pteindex = (va - vm->hdr.kernbase) >> I386_PAGE_SHIFT;
155 		if (pteindex >= vm->hdr.ptesize / sizeof(pte))
156 			goto invalid;
157 		pte = _i386_pte_pae_get(kd, pteindex);
158 		if ((pte & I386_PG_V) == 0) {
159 			_kvm_err(kd, kd->program,
160 			    "_i386_minidump_vatop_pae: pte not valid");
161 			goto invalid;
162 		}
163 		a = pte & I386_PG_FRAME_PAE;
164 		ofs = _kvm_pt_find(kd, a, I386_PAGE_SIZE);
165 		if (ofs == -1) {
166 			_kvm_err(kd, kd->program,
167 	    "_i386_minidump_vatop_pae: physical address 0x%jx not in minidump",
168 			    (uintmax_t)a);
169 			goto invalid;
170 		}
171 		*pa = ofs + offset;
172 		return (I386_PAGE_SIZE - offset);
173 	} else {
174 		_kvm_err(kd, kd->program,
175 	    "_i386_minidump_vatop_pae: virtual address 0x%jx not minidumped",
176 		    (uintmax_t)va);
177 		goto invalid;
178 	}
179 
180 invalid:
181 	_kvm_err(kd, 0, "invalid address (0x%jx)", (uintmax_t)va);
182 	return (0);
183 }
184 
185 static int
186 _i386_minidump_vatop(kvm_t *kd, kvaddr_t va, off_t *pa)
187 {
188 	struct vmstate *vm;
189 	i386_physaddr_t offset;
190 	i386_pte_t pte;
191 	kvaddr_t pteindex;
192 	i386_physaddr_t a;
193 	off_t ofs;
194 
195 	vm = kd->vmst;
196 	offset = va & I386_PAGE_MASK;
197 
198 	if (va >= vm->hdr.kernbase) {
199 		pteindex = (va - vm->hdr.kernbase) >> I386_PAGE_SHIFT;
200 		if (pteindex >= vm->hdr.ptesize / sizeof(pte))
201 			goto invalid;
202 		pte = _i386_pte_get(kd, pteindex);
203 		if ((pte & I386_PG_V) == 0) {
204 			_kvm_err(kd, kd->program,
205 			    "_i386_minidump_vatop: pte not valid");
206 			goto invalid;
207 		}
208 		a = pte & I386_PG_FRAME;
209 		ofs = _kvm_pt_find(kd, a, I386_PAGE_SIZE);
210 		if (ofs == -1) {
211 			_kvm_err(kd, kd->program,
212 	    "_i386_minidump_vatop: physical address 0x%jx not in minidump",
213 			    (uintmax_t)a);
214 			goto invalid;
215 		}
216 		*pa = ofs + offset;
217 		return (I386_PAGE_SIZE - offset);
218 	} else {
219 		_kvm_err(kd, kd->program,
220 	    "_i386_minidump_vatop: virtual address 0x%jx not minidumped",
221 		    (uintmax_t)va);
222 		goto invalid;
223 	}
224 
225 invalid:
226 	_kvm_err(kd, 0, "invalid address (0x%jx)", (uintmax_t)va);
227 	return (0);
228 }
229 
230 static int
231 _i386_minidump_kvatop(kvm_t *kd, kvaddr_t va, off_t *pa)
232 {
233 
234 	if (ISALIVE(kd)) {
235 		_kvm_err(kd, 0, "_i386_minidump_kvatop called in live kernel!");
236 		return (0);
237 	}
238 	if (kd->vmst->hdr.paemode)
239 		return (_i386_minidump_vatop_pae(kd, va, pa));
240 	else
241 		return (_i386_minidump_vatop(kd, va, pa));
242 }
243 
244 static vm_prot_t
245 _i386_entry_to_prot(uint64_t pte)
246 {
247 	vm_prot_t prot = VM_PROT_READ;
248 
249 	/* Source: i386/pmap.c:pmap_protect() */
250 	if (pte & I386_PG_RW)
251 		prot |= VM_PROT_WRITE;
252 	if ((pte & I386_PG_NX) == 0)
253 		prot |= VM_PROT_EXECUTE;
254 
255 	return prot;
256 }
257 
258 struct i386_iter {
259 	kvm_t *kd;
260 	u_long nptes;
261 	u_long pteindex;
262 };
263 
264 static void
265 _i386_iterator_init(struct i386_iter *it, kvm_t *kd)
266 {
267 	struct vmstate *vm = kd->vmst;
268 
269 	it->kd = kd;
270 	it->pteindex = 0;
271 	if (vm->hdr.paemode) {
272 		it->nptes = vm->hdr.ptesize / sizeof(i386_pte_pae_t);
273 	} else {
274 		it->nptes = vm->hdr.ptesize / sizeof(i386_pte_t);
275 	}
276 	return;
277 }
278 
279 static int
280 _i386_iterator_next(struct i386_iter *it, u_long *pa, u_long *va, u_long *dva,
281     vm_prot_t *prot)
282 {
283 	struct vmstate *vm = it->kd->vmst;
284 	i386_pte_t pte32;
285 	i386_pte_pae_t pte64;
286 	int found = 0;
287 
288 	*dva = 0;
289 	*pa = 0;
290 	*va = 0;
291 	*dva = 0;
292 	*prot = 0;
293 	for (; it->pteindex < it->nptes && found == 0; it->pteindex++) {
294 		if (vm->hdr.paemode) {
295 			pte64 = _i386_pte_pae_get(it->kd, it->pteindex);
296 			if ((pte64 & I386_PG_V) == 0)
297 				continue;
298 			*prot = _i386_entry_to_prot(pte64);
299 			*pa = pte64 & I386_PG_FRAME_PAE;
300 		} else {
301 			pte32 = _i386_pte_get(it->kd, it->pteindex);
302 			if ((pte32 & I386_PG_V) == 0)
303 				continue;
304 			*prot = _i386_entry_to_prot(pte32);
305 			*pa = pte32 & I386_PG_FRAME;
306 		}
307 		*va = vm->hdr.kernbase + (it->pteindex << I386_PAGE_SHIFT);
308 		found = 1;
309 	}
310 	return found;
311 }
312 
313 static int
314 _i386_minidump_walk_pages(kvm_t *kd, kvm_walk_pages_cb_t *cb, void *arg)
315 {
316 	struct i386_iter it;
317 	u_long dva, pa, va;
318 	vm_prot_t prot;
319 
320 	_i386_iterator_init(&it, kd);
321 	while (_i386_iterator_next(&it, &pa, &va, &dva, &prot)) {
322 		if (!_kvm_visit_cb(kd, cb, arg, pa, va, dva,
323 		    prot, I386_PAGE_SIZE, 0)) {
324 			return (0);
325 		}
326 	}
327 	return (1);
328 }
329 
330 static struct kvm_arch kvm_i386_minidump = {
331 	.ka_probe = _i386_minidump_probe,
332 	.ka_initvtop = _i386_minidump_initvtop,
333 	.ka_freevtop = _i386_minidump_freevtop,
334 	.ka_kvatop = _i386_minidump_kvatop,
335 	.ka_native = _i386_native,
336 	.ka_walk_pages = _i386_minidump_walk_pages,
337 };
338 
339 KVM_ARCH(kvm_i386_minidump);
340