xref: /freebsd/lib/libkvm/kvm_amd64.c (revision 308659acbff34c51e57693e4dbfa43cbbc910fa1)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software developed by the Computer Systems
8  * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
9  * BG 91-66 and contributed to Berkeley.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 
37 /*
38  * AMD64 machine dependent routines for kvm.  Hopefully, the forthcoming
39  * vm code will one day obsolete this module.
40  */
41 
42 #include <sys/param.h>
43 #include <sys/endian.h>
44 #include <stdint.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include <vm/vm.h>
49 #include <kvm.h>
50 
51 #include <limits.h>
52 
53 #include "kvm_private.h"
54 #include "kvm_amd64.h"
55 
56 struct vmstate {
57 	size_t		phnum;
58 	GElf_Phdr	*phdr;
59 	amd64_pml4e_t	*PML4;
60 };
61 
62 /*
63  * Translate a physical memory address to a file-offset in the crash-dump.
64  */
65 static size_t
_kvm_pa2off(kvm_t * kd,uint64_t pa,off_t * ofs)66 _kvm_pa2off(kvm_t *kd, uint64_t pa, off_t *ofs)
67 {
68 	struct vmstate *vm = kd->vmst;
69 	GElf_Phdr *p;
70 	size_t n;
71 
72 	if (kd->rawdump) {
73 		*ofs = pa;
74 		return (AMD64_PAGE_SIZE - (pa & AMD64_PAGE_MASK));
75 	}
76 
77 	p = vm->phdr;
78 	n = vm->phnum;
79 	while (n && (pa < p->p_paddr || pa >= p->p_paddr + p->p_memsz))
80 		p++, n--;
81 	if (n == 0)
82 		return (0);
83 	*ofs = (pa - p->p_paddr) + p->p_offset;
84 	return (AMD64_PAGE_SIZE - (pa & AMD64_PAGE_MASK));
85 }
86 
87 static void
_amd64_freevtop(kvm_t * kd)88 _amd64_freevtop(kvm_t *kd)
89 {
90 	struct vmstate *vm = kd->vmst;
91 
92 	if (vm->PML4)
93 		free(vm->PML4);
94 	free(vm->phdr);
95 	free(vm);
96 	kd->vmst = NULL;
97 }
98 
99 static int
_amd64_probe(kvm_t * kd)100 _amd64_probe(kvm_t *kd)
101 {
102 
103 	return (_kvm_probe_elf_kernel(kd, ELFCLASS64, EM_X86_64) &&
104 	    !_kvm_is_minidump(kd));
105 }
106 
107 static int
_amd64_initvtop(kvm_t * kd)108 _amd64_initvtop(kvm_t *kd)
109 {
110 	struct kvm_nlist nl[2];
111 	amd64_physaddr_t pa;
112 	kvaddr_t kernbase, kernphys;
113 	amd64_pml4e_t *PML4;
114 	int found = 0;
115 
116 	kd->vmst = (struct vmstate *)_kvm_malloc(kd, sizeof(*kd->vmst));
117 	if (kd->vmst == NULL) {
118 		_kvm_err(kd, kd->program, "cannot allocate vm");
119 		return (-1);
120 	}
121 	kd->vmst->PML4 = 0;
122 
123 	if (kd->rawdump == 0) {
124 		if (_kvm_read_core_phdrs(kd, &kd->vmst->phnum,
125 		    &kd->vmst->phdr) == -1)
126 			return (-1);
127 
128 		for (size_t i = 0; i < kd->vmst->phnum; i++) {
129 			if (kd->vmst->phdr[i].p_type == PT_DUMP_DELTA) {
130 				/* Account for the 2M hole at KERNBASE. */
131 				kernphys = kd->vmst->phdr[i].p_paddr -
132 				    kd->vmst->phdr[i].p_align;
133 				kernbase = kd->vmst->phdr[i].p_vaddr;
134 
135 				found = 1;
136 				break;
137 			}
138 		}
139 	}
140 
141 	if (found == 0) {
142 		nl[0].n_name = "kernbase";
143 		nl[1].n_name = 0;
144 
145 		if (kvm_nlist2(kd, nl) != 0) {
146 			_kvm_err(kd, kd->program, "bad namelist - no kernbase");
147 			return (-1);
148 		}
149 
150 		nl[0].n_name = "kernphys";
151 		nl[1].n_name = 0;
152 
153 		/* XXX
154 		 * Relocatable kernels can still be loaded at 2M.
155 		 */
156 		if (kvm_nlist2(kd, nl) != 1) {
157 			_kvm_err(kd, kd->program, "cannot determine kernphys");
158 			return (-1);
159 		}
160 
161 		kernphys = 0;
162 		kernbase = nl[0].n_value;
163 	}
164 
165 	nl[0].n_name = "KPML4phys";
166 	nl[1].n_name = 0;
167 
168 	if (kvm_nlist2(kd, nl) != 0) {
169 		_kvm_err(kd, kd->program, "bad namelist - no KPML4phys");
170 		return (-1);
171 	}
172 	if (kvm_read2(kd, (nl[0].n_value - kernbase + kernphys), &pa,
173 	    sizeof(pa)) != sizeof(pa)) {
174 		_kvm_err(kd, kd->program, "cannot read KPML4phys");
175 		return (-1);
176 	}
177 	pa = le64toh(pa);
178 	PML4 = _kvm_malloc(kd, AMD64_PAGE_SIZE);
179 	if (PML4 == NULL) {
180 		_kvm_err(kd, kd->program, "cannot allocate PML4");
181 		return (-1);
182 	}
183 	if (kvm_read2(kd, pa, PML4, AMD64_PAGE_SIZE) != AMD64_PAGE_SIZE) {
184 		_kvm_err(kd, kd->program, "cannot read KPML4phys");
185 		free(PML4);
186 		return (-1);
187 	}
188 	kd->vmst->PML4 = PML4;
189 	return (0);
190 }
191 
192 static int
_amd64_vatop(kvm_t * kd,kvaddr_t va,off_t * pa)193 _amd64_vatop(kvm_t *kd, kvaddr_t va, off_t *pa)
194 {
195 	struct vmstate *vm;
196 	amd64_physaddr_t offset;
197 	amd64_physaddr_t pdpe_pa;
198 	amd64_physaddr_t pde_pa;
199 	amd64_physaddr_t pte_pa;
200 	amd64_pml4e_t pml4e;
201 	amd64_pdpe_t pdpe;
202 	amd64_pde_t pde;
203 	amd64_pte_t pte;
204 	kvaddr_t pml4eindex;
205 	kvaddr_t pdpeindex;
206 	kvaddr_t pdeindex;
207 	kvaddr_t pteindex;
208 	amd64_physaddr_t a;
209 	off_t ofs;
210 	size_t s;
211 
212 	vm = kd->vmst;
213 	offset = va & AMD64_PAGE_MASK;
214 
215 	/*
216 	 * If we are initializing (kernel page table descriptor pointer
217 	 * not yet set) then return pa == va to avoid infinite recursion.
218 	 */
219 	if (vm->PML4 == NULL) {
220 		s = _kvm_pa2off(kd, va, pa);
221 		if (s == 0) {
222 			_kvm_err(kd, kd->program,
223 			    "_amd64_vatop: bootstrap data not in dump");
224 			goto invalid;
225 		} else
226 			return (AMD64_PAGE_SIZE - offset);
227 	}
228 
229 	pml4eindex = (va >> AMD64_PML4SHIFT) & (AMD64_NPML4EPG - 1);
230 	pml4e = le64toh(vm->PML4[pml4eindex]);
231 	if ((pml4e & AMD64_PG_V) == 0) {
232 		_kvm_err(kd, kd->program, "_amd64_vatop: pml4e not valid");
233 		goto invalid;
234 	}
235 
236 	pdpeindex = (va >> AMD64_PDPSHIFT) & (AMD64_NPDPEPG - 1);
237 	pdpe_pa = (pml4e & AMD64_PG_FRAME) + (pdpeindex * sizeof(amd64_pdpe_t));
238 
239 	s = _kvm_pa2off(kd, pdpe_pa, &ofs);
240 	if (s < sizeof(pdpe)) {
241 		_kvm_err(kd, kd->program, "_amd64_vatop: pdpe_pa not found");
242 		goto invalid;
243 	}
244 	if (pread(kd->pmfd, &pdpe, sizeof(pdpe), ofs) != sizeof(pdpe)) {
245 		_kvm_syserr(kd, kd->program, "_amd64_vatop: read pdpe");
246 		goto invalid;
247 	}
248 	pdpe = le64toh(pdpe);
249 	if ((pdpe & AMD64_PG_V) == 0) {
250 		_kvm_err(kd, kd->program, "_amd64_vatop: pdpe not valid");
251 		goto invalid;
252 	}
253 
254 	if (pdpe & AMD64_PG_PS) {
255 		/*
256 		 * No next-level page table; pdpe describes one 1GB page.
257 		 */
258 		a = (pdpe & AMD64_PG_1GB_FRAME) + (va & AMD64_PDPMASK);
259 		s = _kvm_pa2off(kd, a, pa);
260 		if (s == 0) {
261 			_kvm_err(kd, kd->program,
262 			    "_amd64_vatop: 1GB page address not in dump");
263 			goto invalid;
264 		} else
265 			return (AMD64_NBPDP - (va & AMD64_PDPMASK));
266 	}
267 
268 	pdeindex = (va >> AMD64_PDRSHIFT) & (AMD64_NPDEPG - 1);
269 	pde_pa = (pdpe & AMD64_PG_FRAME) + (pdeindex * sizeof(amd64_pde_t));
270 
271 	s = _kvm_pa2off(kd, pde_pa, &ofs);
272 	if (s < sizeof(pde)) {
273 		_kvm_syserr(kd, kd->program, "_amd64_vatop: pde_pa not found");
274 		goto invalid;
275 	}
276 	if (pread(kd->pmfd, &pde, sizeof(pde), ofs) != sizeof(pde)) {
277 		_kvm_syserr(kd, kd->program, "_amd64_vatop: read pde");
278 		goto invalid;
279 	}
280 	pde = le64toh(pde);
281 	if ((pde & AMD64_PG_V) == 0) {
282 		_kvm_err(kd, kd->program, "_amd64_vatop: pde not valid");
283 		goto invalid;
284 	}
285 
286 	if (pde & AMD64_PG_PS) {
287 		/*
288 		 * No final-level page table; pde describes one 2MB page.
289 		 */
290 		a = (pde & AMD64_PG_PS_FRAME) + (va & AMD64_PDRMASK);
291 		s = _kvm_pa2off(kd, a, pa);
292 		if (s == 0) {
293 			_kvm_err(kd, kd->program,
294 			    "_amd64_vatop: 2MB page address not in dump");
295 			goto invalid;
296 		} else
297 			return (AMD64_NBPDR - (va & AMD64_PDRMASK));
298 	}
299 
300 	pteindex = (va >> AMD64_PAGE_SHIFT) & (AMD64_NPTEPG - 1);
301 	pte_pa = (pde & AMD64_PG_FRAME) + (pteindex * sizeof(amd64_pte_t));
302 
303 	s = _kvm_pa2off(kd, pte_pa, &ofs);
304 	if (s < sizeof(pte)) {
305 		_kvm_err(kd, kd->program, "_amd64_vatop: pte_pa not found");
306 		goto invalid;
307 	}
308 	if (pread(kd->pmfd, &pte, sizeof(pte), ofs) != sizeof(pte)) {
309 		_kvm_syserr(kd, kd->program, "_amd64_vatop: read");
310 		goto invalid;
311 	}
312 	if ((pte & AMD64_PG_V) == 0) {
313 		_kvm_err(kd, kd->program, "_amd64_vatop: pte not valid");
314 		goto invalid;
315 	}
316 
317 	a = (pte & AMD64_PG_FRAME) + offset;
318 	s = _kvm_pa2off(kd, a, pa);
319 	if (s == 0) {
320 		_kvm_err(kd, kd->program, "_amd64_vatop: address not in dump");
321 		goto invalid;
322 	} else
323 		return (AMD64_PAGE_SIZE - offset);
324 
325 invalid:
326 	_kvm_err(kd, 0, "invalid address (0x%jx)", (uintmax_t)va);
327 	return (0);
328 }
329 
330 static int
_amd64_kvatop(kvm_t * kd,kvaddr_t va,off_t * pa)331 _amd64_kvatop(kvm_t *kd, kvaddr_t va, off_t *pa)
332 {
333 
334 	if (ISALIVE(kd)) {
335 		_kvm_err(kd, 0, "kvm_kvatop called in live kernel!");
336 		return (0);
337 	}
338 	return (_amd64_vatop(kd, va, pa));
339 }
340 
341 int
_amd64_native(kvm_t * kd __unused)342 _amd64_native(kvm_t *kd __unused)
343 {
344 
345 #ifdef __amd64__
346 	return (1);
347 #else
348 	return (0);
349 #endif
350 }
351 
352 static struct kvm_arch kvm_amd64 = {
353 	.ka_probe = _amd64_probe,
354 	.ka_initvtop = _amd64_initvtop,
355 	.ka_freevtop = _amd64_freevtop,
356 	.ka_kvatop = _amd64_kvatop,
357 	.ka_native = _amd64_native,
358 };
359 
360 KVM_ARCH(kvm_amd64);
361