xref: /freebsd/lib/libkvm/kvm_minidump_amd64.c (revision b9f654b163bce26de79705e77b872427c9f2afa1)
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  * AMD64 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/amd64/include/minidump.h"
45 
46 #include <limits.h>
47 
48 #include "kvm_private.h"
49 #include "kvm_amd64.h"
50 
51 #define	amd64_round_page(x)	roundup2((kvaddr_t)(x), AMD64_PAGE_SIZE)
52 #define	VM_IS_V1(vm)		(vm->hdr.version == 1)
53 #define	VA_OFF(vm, va)		\
54 	(VM_IS_V1(vm) ? ((va) & (AMD64_PAGE_SIZE - 1)) : ((va) & AMD64_PAGE_MASK))
55 
56 struct vmstate {
57 	struct minidumphdr hdr;
58 };
59 
60 static vm_prot_t
61 _amd64_entry_to_prot(uint64_t entry)
62 {
63 	vm_prot_t prot = VM_PROT_READ;
64 
65 	if ((entry & AMD64_PG_RW) != 0)
66 		prot |= VM_PROT_WRITE;
67 	if ((entry & AMD64_PG_NX) == 0)
68 		prot |= VM_PROT_EXECUTE;
69 	return prot;
70 }
71 
72 /*
73  * Version 2 minidumps use page directory entries, while version 1 use page
74  * table entries.
75  */
76 
77 static amd64_pde_t
78 _amd64_pde_get(kvm_t *kd, u_long pdeindex)
79 {
80 	amd64_pde_t *pde = _kvm_pmap_get(kd, pdeindex, sizeof(*pde));
81 
82 	return le64toh(*pde);
83 }
84 
85 static amd64_pte_t
86 _amd64_pte_get(kvm_t *kd, u_long pteindex)
87 {
88 	amd64_pte_t *pte = _kvm_pmap_get(kd, pteindex, sizeof(*pte));
89 
90 	return le64toh(*pte);
91 }
92 
93 /* Get the first page table entry for a given page directory index. */
94 static amd64_pte_t *
95 _amd64_pde_first_pte(kvm_t *kd, u_long pdeindex)
96 {
97 	u_long *pa;
98 
99 	pa = _kvm_pmap_get(kd, pdeindex, sizeof(amd64_pde_t));
100 	if (pa == NULL)
101 		return NULL;
102 	return _kvm_map_get(kd, *pa & AMD64_PG_FRAME, AMD64_PAGE_SIZE);
103 }
104 
105 static int
106 _amd64_minidump_probe(kvm_t *kd)
107 {
108 
109 	return (_kvm_probe_elf_kernel(kd, ELFCLASS64, EM_X86_64) &&
110 	    _kvm_is_minidump(kd));
111 }
112 
113 static void
114 _amd64_minidump_freevtop(kvm_t *kd)
115 {
116 	struct vmstate *vm = kd->vmst;
117 
118 	free(vm);
119 	kd->vmst = NULL;
120 }
121 
122 static int
123 _amd64_minidump_initvtop(kvm_t *kd)
124 {
125 	struct vmstate *vmst;
126 	off_t off, sparse_off;
127 
128 	vmst = _kvm_malloc(kd, sizeof(*vmst));
129 	if (vmst == NULL) {
130 		_kvm_err(kd, kd->program, "cannot allocate vm");
131 		return (-1);
132 	}
133 	kd->vmst = vmst;
134 	if (pread(kd->pmfd, &vmst->hdr, sizeof(vmst->hdr), 0) !=
135 	    sizeof(vmst->hdr)) {
136 		_kvm_err(kd, kd->program, "cannot read dump header");
137 		return (-1);
138 	}
139 	if (strncmp(MINIDUMP_MAGIC, vmst->hdr.magic, sizeof(vmst->hdr.magic)) != 0) {
140 		_kvm_err(kd, kd->program, "not a minidump for this platform");
141 		return (-1);
142 	}
143 
144 	/*
145 	 * NB: amd64 minidump header is binary compatible between version 1
146 	 * and version 2; this may not be the case for the future versions.
147 	 */
148 	vmst->hdr.version = le32toh(vmst->hdr.version);
149 	if (vmst->hdr.version != MINIDUMP_VERSION && vmst->hdr.version != 1) {
150 		_kvm_err(kd, kd->program, "wrong minidump version. expected %d got %d",
151 		    MINIDUMP_VERSION, vmst->hdr.version);
152 		return (-1);
153 	}
154 	vmst->hdr.msgbufsize = le32toh(vmst->hdr.msgbufsize);
155 	vmst->hdr.bitmapsize = le32toh(vmst->hdr.bitmapsize);
156 	vmst->hdr.pmapsize = le32toh(vmst->hdr.pmapsize);
157 	vmst->hdr.kernbase = le64toh(vmst->hdr.kernbase);
158 	vmst->hdr.dmapbase = le64toh(vmst->hdr.dmapbase);
159 	vmst->hdr.dmapend = le64toh(vmst->hdr.dmapend);
160 
161 	/* Skip header and msgbuf */
162 	off = AMD64_PAGE_SIZE + amd64_round_page(vmst->hdr.msgbufsize);
163 
164 	sparse_off = off + amd64_round_page(vmst->hdr.bitmapsize) +
165 	    amd64_round_page(vmst->hdr.pmapsize);
166 	if (_kvm_pt_init(kd, vmst->hdr.bitmapsize, off, sparse_off,
167 	    AMD64_PAGE_SIZE, sizeof(uint64_t)) == -1) {
168 		return (-1);
169 	}
170 	off += amd64_round_page(vmst->hdr.bitmapsize);
171 
172 	if (_kvm_pmap_init(kd, vmst->hdr.pmapsize, off) == -1) {
173 		return (-1);
174 	}
175 	off += amd64_round_page(vmst->hdr.pmapsize);
176 
177 	return (0);
178 }
179 
180 static int
181 _amd64_minidump_vatop_v1(kvm_t *kd, kvaddr_t va, off_t *pa)
182 {
183 	struct vmstate *vm;
184 	amd64_physaddr_t offset;
185 	amd64_pte_t pte;
186 	kvaddr_t pteindex;
187 	amd64_physaddr_t a;
188 	off_t ofs;
189 
190 	vm = kd->vmst;
191 	offset = va & AMD64_PAGE_MASK;
192 
193 	if (va >= vm->hdr.kernbase) {
194 		pteindex = (va - vm->hdr.kernbase) >> AMD64_PAGE_SHIFT;
195 		if (pteindex >= vm->hdr.pmapsize / sizeof(pte))
196 			goto invalid;
197 		pte = _amd64_pte_get(kd, pteindex);
198 		if ((pte & AMD64_PG_V) == 0) {
199 			_kvm_err(kd, kd->program,
200 			    "_amd64_minidump_vatop_v1: pte not valid");
201 			goto invalid;
202 		}
203 		a = pte & AMD64_PG_FRAME;
204 		ofs = _kvm_pt_find(kd, a, AMD64_PAGE_SIZE);
205 		if (ofs == -1) {
206 			_kvm_err(kd, kd->program,
207 	    "_amd64_minidump_vatop_v1: physical address 0x%jx not in minidump",
208 			    (uintmax_t)a);
209 			goto invalid;
210 		}
211 		*pa = ofs + offset;
212 		return (AMD64_PAGE_SIZE - offset);
213 	} else if (va >= vm->hdr.dmapbase && va < vm->hdr.dmapend) {
214 		a = (va - vm->hdr.dmapbase) & ~AMD64_PAGE_MASK;
215 		ofs = _kvm_pt_find(kd, a, AMD64_PAGE_SIZE);
216 		if (ofs == -1) {
217 			_kvm_err(kd, kd->program,
218     "_amd64_minidump_vatop_v1: direct map address 0x%jx not in minidump",
219 			    (uintmax_t)va);
220 			goto invalid;
221 		}
222 		*pa = ofs + offset;
223 		return (AMD64_PAGE_SIZE - offset);
224 	} else {
225 		_kvm_err(kd, kd->program,
226 	    "_amd64_minidump_vatop_v1: virtual address 0x%jx not minidumped",
227 		    (uintmax_t)va);
228 		goto invalid;
229 	}
230 
231 invalid:
232 	_kvm_err(kd, 0, "invalid address (0x%jx)", (uintmax_t)va);
233 	return (0);
234 }
235 
236 static int
237 _amd64_minidump_vatop(kvm_t *kd, kvaddr_t va, off_t *pa)
238 {
239 	amd64_pte_t pt[AMD64_NPTEPG];
240 	struct vmstate *vm;
241 	amd64_physaddr_t offset;
242 	amd64_pde_t pde;
243 	amd64_pte_t pte;
244 	kvaddr_t pteindex;
245 	kvaddr_t pdeindex;
246 	amd64_physaddr_t a;
247 	off_t ofs;
248 
249 	vm = kd->vmst;
250 	offset = va & AMD64_PAGE_MASK;
251 
252 	if (va >= vm->hdr.kernbase) {
253 		pdeindex = (va - vm->hdr.kernbase) >> AMD64_PDRSHIFT;
254 		if (pdeindex >= vm->hdr.pmapsize / sizeof(pde))
255 			goto invalid;
256 		pde = _amd64_pde_get(kd, pdeindex);
257 		if ((pde & AMD64_PG_V) == 0) {
258 			_kvm_err(kd, kd->program,
259 			    "_amd64_minidump_vatop: pde not valid");
260 			goto invalid;
261 		}
262 		if ((pde & AMD64_PG_PS) == 0) {
263 			a = pde & AMD64_PG_FRAME;
264 			/* TODO: Just read the single PTE */
265 			ofs = _kvm_pt_find(kd, a, AMD64_PAGE_SIZE);
266 			if (ofs == -1) {
267 				_kvm_err(kd, kd->program,
268 				    "cannot find page table entry for %ju",
269 				    (uintmax_t)a);
270 				goto invalid;
271 			}
272 			if (pread(kd->pmfd, &pt, AMD64_PAGE_SIZE, ofs) !=
273 			    AMD64_PAGE_SIZE) {
274 				_kvm_err(kd, kd->program,
275 				    "cannot read page table entry for %ju",
276 				    (uintmax_t)a);
277 				goto invalid;
278 			}
279 			pteindex = (va >> AMD64_PAGE_SHIFT) &
280 			    (AMD64_NPTEPG - 1);
281 			pte = le64toh(pt[pteindex]);
282 			if ((pte & AMD64_PG_V) == 0) {
283 				_kvm_err(kd, kd->program,
284 				    "_amd64_minidump_vatop: pte not valid");
285 				goto invalid;
286 			}
287 			a = pte & AMD64_PG_FRAME;
288 		} else {
289 			a = pde & AMD64_PG_PS_FRAME;
290 			a += (va & AMD64_PDRMASK) ^ offset;
291 		}
292 		ofs = _kvm_pt_find(kd, a, AMD64_PAGE_SIZE);
293 		if (ofs == -1) {
294 			_kvm_err(kd, kd->program,
295 	    "_amd64_minidump_vatop: physical address 0x%jx not in minidump",
296 			    (uintmax_t)a);
297 			goto invalid;
298 		}
299 		*pa = ofs + offset;
300 		return (AMD64_PAGE_SIZE - offset);
301 	} else if (va >= vm->hdr.dmapbase && va < vm->hdr.dmapend) {
302 		a = (va - vm->hdr.dmapbase) & ~AMD64_PAGE_MASK;
303 		ofs = _kvm_pt_find(kd, a, AMD64_PAGE_SIZE);
304 		if (ofs == -1) {
305 			_kvm_err(kd, kd->program,
306 	    "_amd64_minidump_vatop: direct map address 0x%jx not in minidump",
307 			    (uintmax_t)va);
308 			goto invalid;
309 		}
310 		*pa = ofs + offset;
311 		return (AMD64_PAGE_SIZE - offset);
312 	} else {
313 		_kvm_err(kd, kd->program,
314 	    "_amd64_minidump_vatop: virtual address 0x%jx not minidumped",
315 		    (uintmax_t)va);
316 		goto invalid;
317 	}
318 
319 invalid:
320 	_kvm_err(kd, 0, "invalid address (0x%jx)", (uintmax_t)va);
321 	return (0);
322 }
323 
324 static int
325 _amd64_minidump_kvatop(kvm_t *kd, kvaddr_t va, off_t *pa)
326 {
327 
328 	if (ISALIVE(kd)) {
329 		_kvm_err(kd, 0,
330 		    "_amd64_minidump_kvatop called in live kernel!");
331 		return (0);
332 	}
333 	if (((struct vmstate *)kd->vmst)->hdr.version == 1)
334 		return (_amd64_minidump_vatop_v1(kd, va, pa));
335 	else
336 		return (_amd64_minidump_vatop(kd, va, pa));
337 }
338 
339 static int
340 _amd64_minidump_walk_pages(kvm_t *kd, kvm_walk_pages_cb_t *cb, void *arg)
341 {
342 	struct vmstate *vm = kd->vmst;
343 	u_long npdes = vm->hdr.pmapsize / sizeof(amd64_pde_t);
344 	u_long bmindex, dva, pa, pdeindex, va;
345 	struct kvm_bitmap bm;
346 	int ret = 0;
347 	vm_prot_t prot;
348 	unsigned int pgsz = AMD64_PAGE_SIZE;
349 
350 	if (vm->hdr.version < 2)
351 		return (0);
352 
353 	if (!_kvm_bitmap_init(&bm, vm->hdr.bitmapsize, &bmindex))
354 		return (0);
355 
356 	for (pdeindex = 0; pdeindex < npdes; pdeindex++) {
357 		amd64_pde_t pde = _amd64_pde_get(kd, pdeindex);
358 		amd64_pte_t *ptes;
359 		u_long i;
360 
361 		va = vm->hdr.kernbase + (pdeindex << AMD64_PDRSHIFT);
362 		if ((pde & AMD64_PG_V) == 0)
363 			continue;
364 
365 		if ((pde & AMD64_PG_PS) != 0) {
366 			/*
367 			 * Large page.  Iterate on each 4K page section
368 			 * within this page.  This differs from 4K pages in
369 			 * that every page here uses the same PDE to
370 			 * generate permissions.
371 			 */
372 			pa = (pde & AMD64_PG_PS_FRAME) +
373 			    ((va & AMD64_PDRMASK) ^ VA_OFF(vm, va));
374 			dva = vm->hdr.dmapbase + pa;
375 			_kvm_bitmap_set(&bm, pa, AMD64_PAGE_SIZE);
376 			if (!_kvm_visit_cb(kd, cb, arg, pa, va, dva,
377 			    _amd64_entry_to_prot(pde), AMD64_NBPDR, pgsz)) {
378 				goto out;
379 			}
380 			continue;
381 		}
382 
383 		/* 4K pages: pde references another page of entries. */
384 		ptes = _amd64_pde_first_pte(kd, pdeindex);
385 		/* Ignore page directory pages that were not dumped. */
386 		if (ptes == NULL)
387 			continue;
388 
389 		for (i = 0; i < AMD64_NPTEPG; i++) {
390 			amd64_pte_t pte = (u_long)ptes[i];
391 
392 			pa = pte & AMD64_PG_FRAME;
393 			dva = vm->hdr.dmapbase + pa;
394 			if ((pte & AMD64_PG_V) != 0) {
395 				_kvm_bitmap_set(&bm, pa, AMD64_PAGE_SIZE);
396 				if (!_kvm_visit_cb(kd, cb, arg, pa, va, dva,
397 				    _amd64_entry_to_prot(pte), pgsz, 0)) {
398 					goto out;
399 				}
400 			}
401 			va += AMD64_PAGE_SIZE;
402 		}
403 	}
404 
405 	while (_kvm_bitmap_next(&bm, &bmindex)) {
406 		pa = bmindex * AMD64_PAGE_SIZE;
407 		dva = vm->hdr.dmapbase + pa;
408 		if (vm->hdr.dmapend < (dva + pgsz))
409 			break;
410 		va = 0;
411 		/* amd64/pmap.c: create_pagetables(): dmap always R|W. */
412 		prot = VM_PROT_READ | VM_PROT_WRITE;
413 		if (!_kvm_visit_cb(kd, cb, arg, pa, va, dva, prot, pgsz, 0)) {
414 			goto out;
415 		}
416 	}
417 
418 	ret = 1;
419 
420 out:
421 	_kvm_bitmap_deinit(&bm);
422 	return (ret);
423 }
424 
425 static struct kvm_arch kvm_amd64_minidump = {
426 	.ka_probe = _amd64_minidump_probe,
427 	.ka_initvtop = _amd64_minidump_initvtop,
428 	.ka_freevtop = _amd64_minidump_freevtop,
429 	.ka_kvatop = _amd64_minidump_kvatop,
430 	.ka_native = _amd64_native,
431 	.ka_walk_pages = _amd64_minidump_walk_pages,
432 };
433 
434 KVM_ARCH(kvm_amd64_minidump);
435