xref: /freebsd/lib/libkvm/kvm_minidump_amd64.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  * 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, dump_avail_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; version 3 adds the dumpavailsize field
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 	vmst->hdr.dumpavailsize = vmst->hdr.version == MINIDUMP_VERSION ?
161 	    le32toh(vmst->hdr.dumpavailsize) : 0;
162 
163 	/* Skip header and msgbuf */
164 	dump_avail_off = AMD64_PAGE_SIZE + amd64_round_page(vmst->hdr.msgbufsize);
165 
166 	/* Skip dump_avail */
167 	off = dump_avail_off + amd64_round_page(vmst->hdr.dumpavailsize);
168 
169 	sparse_off = off + amd64_round_page(vmst->hdr.bitmapsize) +
170 	    amd64_round_page(vmst->hdr.pmapsize);
171 	if (_kvm_pt_init(kd, vmst->hdr.dumpavailsize, dump_avail_off,
172 	    vmst->hdr.bitmapsize, off, sparse_off, AMD64_PAGE_SIZE,
173 	    sizeof(uint64_t)) == -1) {
174 		return (-1);
175 	}
176 	off += amd64_round_page(vmst->hdr.bitmapsize);
177 
178 	if (_kvm_pmap_init(kd, vmst->hdr.pmapsize, off) == -1) {
179 		return (-1);
180 	}
181 	off += amd64_round_page(vmst->hdr.pmapsize);
182 
183 	return (0);
184 }
185 
186 static int
187 _amd64_minidump_vatop_v1(kvm_t *kd, kvaddr_t va, off_t *pa)
188 {
189 	struct vmstate *vm;
190 	amd64_physaddr_t offset;
191 	amd64_pte_t pte;
192 	kvaddr_t pteindex;
193 	amd64_physaddr_t a;
194 	off_t ofs;
195 
196 	vm = kd->vmst;
197 	offset = va & AMD64_PAGE_MASK;
198 
199 	if (va >= vm->hdr.kernbase) {
200 		pteindex = (va - vm->hdr.kernbase) >> AMD64_PAGE_SHIFT;
201 		if (pteindex >= vm->hdr.pmapsize / sizeof(pte))
202 			goto invalid;
203 		pte = _amd64_pte_get(kd, pteindex);
204 		if ((pte & AMD64_PG_V) == 0) {
205 			_kvm_err(kd, kd->program,
206 			    "_amd64_minidump_vatop_v1: pte not valid");
207 			goto invalid;
208 		}
209 		a = pte & AMD64_PG_FRAME;
210 		ofs = _kvm_pt_find(kd, a, AMD64_PAGE_SIZE);
211 		if (ofs == -1) {
212 			_kvm_err(kd, kd->program,
213 	    "_amd64_minidump_vatop_v1: physical address 0x%jx not in minidump",
214 			    (uintmax_t)a);
215 			goto invalid;
216 		}
217 		*pa = ofs + offset;
218 		return (AMD64_PAGE_SIZE - offset);
219 	} else if (va >= vm->hdr.dmapbase && va < vm->hdr.dmapend) {
220 		a = (va - vm->hdr.dmapbase) & ~AMD64_PAGE_MASK;
221 		ofs = _kvm_pt_find(kd, a, AMD64_PAGE_SIZE);
222 		if (ofs == -1) {
223 			_kvm_err(kd, kd->program,
224     "_amd64_minidump_vatop_v1: direct map address 0x%jx not in minidump",
225 			    (uintmax_t)va);
226 			goto invalid;
227 		}
228 		*pa = ofs + offset;
229 		return (AMD64_PAGE_SIZE - offset);
230 	} else {
231 		_kvm_err(kd, kd->program,
232 	    "_amd64_minidump_vatop_v1: virtual address 0x%jx not minidumped",
233 		    (uintmax_t)va);
234 		goto invalid;
235 	}
236 
237 invalid:
238 	_kvm_err(kd, 0, "invalid address (0x%jx)", (uintmax_t)va);
239 	return (0);
240 }
241 
242 static int
243 _amd64_minidump_vatop(kvm_t *kd, kvaddr_t va, off_t *pa)
244 {
245 	amd64_pte_t pt[AMD64_NPTEPG];
246 	struct vmstate *vm;
247 	amd64_physaddr_t offset;
248 	amd64_pde_t pde;
249 	amd64_pte_t pte;
250 	kvaddr_t pteindex;
251 	kvaddr_t pdeindex;
252 	amd64_physaddr_t a;
253 	off_t ofs;
254 
255 	vm = kd->vmst;
256 	offset = va & AMD64_PAGE_MASK;
257 
258 	if (va >= vm->hdr.kernbase) {
259 		pdeindex = (va - vm->hdr.kernbase) >> AMD64_PDRSHIFT;
260 		if (pdeindex >= vm->hdr.pmapsize / sizeof(pde))
261 			goto invalid;
262 		pde = _amd64_pde_get(kd, pdeindex);
263 		if ((pde & AMD64_PG_V) == 0) {
264 			_kvm_err(kd, kd->program,
265 			    "_amd64_minidump_vatop: pde not valid");
266 			goto invalid;
267 		}
268 		if ((pde & AMD64_PG_PS) == 0) {
269 			a = pde & AMD64_PG_FRAME;
270 			/* TODO: Just read the single PTE */
271 			ofs = _kvm_pt_find(kd, a, AMD64_PAGE_SIZE);
272 			if (ofs == -1) {
273 				_kvm_err(kd, kd->program,
274 				    "cannot find page table entry for %ju",
275 				    (uintmax_t)a);
276 				goto invalid;
277 			}
278 			if (pread(kd->pmfd, &pt, AMD64_PAGE_SIZE, ofs) !=
279 			    AMD64_PAGE_SIZE) {
280 				_kvm_err(kd, kd->program,
281 				    "cannot read page table entry for %ju",
282 				    (uintmax_t)a);
283 				goto invalid;
284 			}
285 			pteindex = (va >> AMD64_PAGE_SHIFT) &
286 			    (AMD64_NPTEPG - 1);
287 			pte = le64toh(pt[pteindex]);
288 			if ((pte & AMD64_PG_V) == 0) {
289 				_kvm_err(kd, kd->program,
290 				    "_amd64_minidump_vatop: pte not valid");
291 				goto invalid;
292 			}
293 			a = pte & AMD64_PG_FRAME;
294 		} else {
295 			a = pde & AMD64_PG_PS_FRAME;
296 			a += (va & AMD64_PDRMASK) ^ offset;
297 		}
298 		ofs = _kvm_pt_find(kd, a, AMD64_PAGE_SIZE);
299 		if (ofs == -1) {
300 			_kvm_err(kd, kd->program,
301 	    "_amd64_minidump_vatop: physical address 0x%jx not in minidump",
302 			    (uintmax_t)a);
303 			goto invalid;
304 		}
305 		*pa = ofs + offset;
306 		return (AMD64_PAGE_SIZE - offset);
307 	} else if (va >= vm->hdr.dmapbase && va < vm->hdr.dmapend) {
308 		a = (va - vm->hdr.dmapbase) & ~AMD64_PAGE_MASK;
309 		ofs = _kvm_pt_find(kd, a, AMD64_PAGE_SIZE);
310 		if (ofs == -1) {
311 			_kvm_err(kd, kd->program,
312 	    "_amd64_minidump_vatop: direct map address 0x%jx not in minidump",
313 			    (uintmax_t)va);
314 			goto invalid;
315 		}
316 		*pa = ofs + offset;
317 		return (AMD64_PAGE_SIZE - offset);
318 	} else {
319 		_kvm_err(kd, kd->program,
320 	    "_amd64_minidump_vatop: virtual address 0x%jx not minidumped",
321 		    (uintmax_t)va);
322 		goto invalid;
323 	}
324 
325 invalid:
326 	_kvm_err(kd, 0, "invalid address (0x%jx)", (uintmax_t)va);
327 	return (0);
328 }
329 
330 static int
331 _amd64_minidump_kvatop(kvm_t *kd, kvaddr_t va, off_t *pa)
332 {
333 
334 	if (ISALIVE(kd)) {
335 		_kvm_err(kd, 0,
336 		    "_amd64_minidump_kvatop called in live kernel!");
337 		return (0);
338 	}
339 	if (((struct vmstate *)kd->vmst)->hdr.version == 1)
340 		return (_amd64_minidump_vatop_v1(kd, va, pa));
341 	else
342 		return (_amd64_minidump_vatop(kd, va, pa));
343 }
344 
345 static int
346 _amd64_minidump_walk_pages(kvm_t *kd, kvm_walk_pages_cb_t *cb, void *arg)
347 {
348 	struct vmstate *vm = kd->vmst;
349 	u_long npdes = vm->hdr.pmapsize / sizeof(amd64_pde_t);
350 	u_long bmindex, dva, pa, pdeindex, va;
351 	struct kvm_bitmap bm;
352 	int ret = 0;
353 	vm_prot_t prot;
354 	unsigned int pgsz = AMD64_PAGE_SIZE;
355 
356 	if (vm->hdr.version < 2)
357 		return (0);
358 
359 	if (!_kvm_bitmap_init(&bm, vm->hdr.bitmapsize, &bmindex))
360 		return (0);
361 
362 	for (pdeindex = 0; pdeindex < npdes; pdeindex++) {
363 		amd64_pde_t pde = _amd64_pde_get(kd, pdeindex);
364 		amd64_pte_t *ptes;
365 		u_long i;
366 
367 		va = vm->hdr.kernbase + (pdeindex << AMD64_PDRSHIFT);
368 		if ((pde & AMD64_PG_V) == 0)
369 			continue;
370 
371 		if ((pde & AMD64_PG_PS) != 0) {
372 			/*
373 			 * Large page.  Iterate on each 4K page section
374 			 * within this page.  This differs from 4K pages in
375 			 * that every page here uses the same PDE to
376 			 * generate permissions.
377 			 */
378 			pa = (pde & AMD64_PG_PS_FRAME) +
379 			    ((va & AMD64_PDRMASK) ^ VA_OFF(vm, va));
380 			dva = vm->hdr.dmapbase + pa;
381 			_kvm_bitmap_set(&bm, _kvm_pa_bit_id(kd, pa, AMD64_PAGE_SIZE));
382 			if (!_kvm_visit_cb(kd, cb, arg, pa, va, dva,
383 			    _amd64_entry_to_prot(pde), AMD64_NBPDR, pgsz)) {
384 				goto out;
385 			}
386 			continue;
387 		}
388 
389 		/* 4K pages: pde references another page of entries. */
390 		ptes = _amd64_pde_first_pte(kd, pdeindex);
391 		/* Ignore page directory pages that were not dumped. */
392 		if (ptes == NULL)
393 			continue;
394 
395 		for (i = 0; i < AMD64_NPTEPG; i++) {
396 			amd64_pte_t pte = (u_long)ptes[i];
397 
398 			pa = pte & AMD64_PG_FRAME;
399 			dva = vm->hdr.dmapbase + pa;
400 			if ((pte & AMD64_PG_V) != 0) {
401 				_kvm_bitmap_set(&bm,
402 				    _kvm_pa_bit_id(kd, pa, AMD64_PAGE_SIZE));
403 				if (!_kvm_visit_cb(kd, cb, arg, pa, va, dva,
404 				    _amd64_entry_to_prot(pte), pgsz, 0)) {
405 					goto out;
406 				}
407 			}
408 			va += AMD64_PAGE_SIZE;
409 		}
410 	}
411 
412 	while (_kvm_bitmap_next(&bm, &bmindex)) {
413 		pa = _kvm_bit_id_pa(kd, bmindex, AMD64_PAGE_SIZE);
414 		if (pa == _KVM_PA_INVALID)
415 			break;
416 		dva = vm->hdr.dmapbase + pa;
417 		if (vm->hdr.dmapend < (dva + pgsz))
418 			break;
419 		va = 0;
420 		/* amd64/pmap.c: create_pagetables(): dmap always R|W. */
421 		prot = VM_PROT_READ | VM_PROT_WRITE;
422 		if (!_kvm_visit_cb(kd, cb, arg, pa, va, dva, prot, pgsz, 0)) {
423 			goto out;
424 		}
425 	}
426 
427 	ret = 1;
428 
429 out:
430 	_kvm_bitmap_deinit(&bm);
431 	return (ret);
432 }
433 
434 static struct kvm_arch kvm_amd64_minidump = {
435 	.ka_probe = _amd64_minidump_probe,
436 	.ka_initvtop = _amd64_minidump_initvtop,
437 	.ka_freevtop = _amd64_minidump_freevtop,
438 	.ka_kvatop = _amd64_minidump_kvatop,
439 	.ka_native = _amd64_native,
440 	.ka_walk_pages = _amd64_minidump_walk_pages,
441 };
442 
443 KVM_ARCH(kvm_amd64_minidump);
444