xref: /freebsd/lib/libkvm/kvm_i386.c (revision 3416500aef140042c64bc149cb1ec6620483bc44)
1 /*-
2  * Copyright (c) 1989, 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software developed by the Computer Systems
6  * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
7  * BG 91-66 and contributed to Berkeley.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #if defined(LIBC_SCCS) && !defined(lint)
38 #if 0
39 static char sccsid[] = "@(#)kvm_hp300.c	8.1 (Berkeley) 6/4/93";
40 #endif
41 #endif /* LIBC_SCCS and not lint */
42 
43 /*
44  * i386 machine dependent routines for kvm.  Hopefully, the forthcoming
45  * vm code will one day obsolete this module.
46  */
47 
48 #include <sys/param.h>
49 #include <sys/endian.h>
50 #include <stdint.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54 #include <kvm.h>
55 
56 #ifdef __i386__
57 #include <machine/vmparam.h>		/* For KERNBASE. */
58 #endif
59 
60 #include <limits.h>
61 
62 #include "kvm_private.h"
63 #include "kvm_i386.h"
64 
65 struct vmstate {
66 	void		*PTD;
67 	int		pae;
68 	size_t		phnum;
69 	GElf_Phdr	*phdr;
70 };
71 
72 /*
73  * Translate a physical memory address to a file-offset in the crash-dump.
74  */
75 static size_t
76 _kvm_pa2off(kvm_t *kd, uint64_t pa, off_t *ofs)
77 {
78 	struct vmstate *vm = kd->vmst;
79 	GElf_Phdr *p;
80 	size_t n;
81 
82 	if (kd->rawdump) {
83 		*ofs = pa;
84 		return (I386_PAGE_SIZE - (pa & I386_PAGE_MASK));
85 	}
86 
87 	p = vm->phdr;
88 	n = vm->phnum;
89 	while (n && (pa < p->p_paddr || pa >= p->p_paddr + p->p_memsz))
90 		p++, n--;
91 	if (n == 0)
92 		return (0);
93 	*ofs = (pa - p->p_paddr) + p->p_offset;
94 	return (I386_PAGE_SIZE - (pa & I386_PAGE_MASK));
95 }
96 
97 static void
98 _i386_freevtop(kvm_t *kd)
99 {
100 	struct vmstate *vm = kd->vmst;
101 
102 	if (vm->PTD)
103 		free(vm->PTD);
104 	free(vm->phdr);
105 	free(vm);
106 	kd->vmst = NULL;
107 }
108 
109 static int
110 _i386_probe(kvm_t *kd)
111 {
112 
113 	return (_kvm_probe_elf_kernel(kd, ELFCLASS32, EM_386) &&
114 	    !_kvm_is_minidump(kd));
115 }
116 
117 static int
118 _i386_initvtop(kvm_t *kd)
119 {
120 	struct kvm_nlist nl[2];
121 	i386_physaddr_t pa;
122 	kvaddr_t kernbase;
123 	char		*PTD;
124 	int		i;
125 
126 	kd->vmst = (struct vmstate *)_kvm_malloc(kd, sizeof(struct vmstate));
127 	if (kd->vmst == NULL) {
128 		_kvm_err(kd, kd->program, "cannot allocate vm");
129 		return (-1);
130 	}
131 	kd->vmst->PTD = 0;
132 
133 	if (kd->rawdump == 0) {
134 		if (_kvm_read_core_phdrs(kd, &kd->vmst->phnum,
135 		    &kd->vmst->phdr) == -1)
136 			return (-1);
137 	}
138 
139 	nl[0].n_name = "kernbase";
140 	nl[1].n_name = 0;
141 
142 	if (kvm_nlist2(kd, nl) != 0) {
143 #ifdef __i386__
144 		kernbase = KERNBASE;	/* for old kernels */
145 #else
146 		_kvm_err(kd, kd->program, "cannot resolve kernbase");
147 		return (-1);
148 #endif
149 	} else
150 		kernbase = nl[0].n_value;
151 
152 	nl[0].n_name = "IdlePDPT";
153 	nl[1].n_name = 0;
154 
155 	if (kvm_nlist2(kd, nl) == 0) {
156 		i386_physaddr_pae_t pa64;
157 
158 		if (kvm_read2(kd, (nl[0].n_value - kernbase), &pa,
159 		    sizeof(pa)) != sizeof(pa)) {
160 			_kvm_err(kd, kd->program, "cannot read IdlePDPT");
161 			return (-1);
162 		}
163 		pa = le32toh(pa);
164 		PTD = _kvm_malloc(kd, 4 * I386_PAGE_SIZE);
165 		if (PTD == NULL) {
166 			_kvm_err(kd, kd->program, "cannot allocate PTD");
167 			return (-1);
168 		}
169 		for (i = 0; i < 4; i++) {
170 			if (kvm_read2(kd, pa + (i * sizeof(pa64)), &pa64,
171 			    sizeof(pa64)) != sizeof(pa64)) {
172 				_kvm_err(kd, kd->program, "Cannot read PDPT");
173 				free(PTD);
174 				return (-1);
175 			}
176 			pa64 = le64toh(pa64);
177 			if (kvm_read2(kd, pa64 & I386_PG_FRAME_PAE,
178 			    PTD + (i * I386_PAGE_SIZE), I386_PAGE_SIZE) !=
179 			    I386_PAGE_SIZE) {
180 				_kvm_err(kd, kd->program, "cannot read PDPT");
181 				free(PTD);
182 				return (-1);
183 			}
184 		}
185 		kd->vmst->PTD = PTD;
186 		kd->vmst->pae = 1;
187 	} else {
188 		nl[0].n_name = "IdlePTD";
189 		nl[1].n_name = 0;
190 
191 		if (kvm_nlist2(kd, nl) != 0) {
192 			_kvm_err(kd, kd->program, "bad namelist");
193 			return (-1);
194 		}
195 		if (kvm_read2(kd, (nl[0].n_value - kernbase), &pa,
196 		    sizeof(pa)) != sizeof(pa)) {
197 			_kvm_err(kd, kd->program, "cannot read IdlePTD");
198 			return (-1);
199 		}
200 		pa = le32toh(pa);
201 		PTD = _kvm_malloc(kd, I386_PAGE_SIZE);
202 		if (PTD == NULL) {
203 			_kvm_err(kd, kd->program, "cannot allocate PTD");
204 			return (-1);
205 		}
206 		if (kvm_read2(kd, pa, PTD, I386_PAGE_SIZE) != I386_PAGE_SIZE) {
207 			_kvm_err(kd, kd->program, "cannot read PTD");
208 			return (-1);
209 		}
210 		kd->vmst->PTD = PTD;
211 		kd->vmst->pae = 0;
212 	}
213 	return (0);
214 }
215 
216 static int
217 _i386_vatop(kvm_t *kd, kvaddr_t va, off_t *pa)
218 {
219 	struct vmstate *vm;
220 	i386_physaddr_t offset;
221 	i386_physaddr_t pte_pa;
222 	i386_pde_t pde;
223 	i386_pte_t pte;
224 	kvaddr_t pdeindex;
225 	kvaddr_t pteindex;
226 	size_t s;
227 	i386_physaddr_t a;
228 	off_t ofs;
229 	i386_pde_t *PTD;
230 
231 	vm = kd->vmst;
232 	PTD = (i386_pde_t *)vm->PTD;
233 	offset = va & I386_PAGE_MASK;
234 
235 	/*
236 	 * If we are initializing (kernel page table descriptor pointer
237 	 * not yet set) then return pa == va to avoid infinite recursion.
238 	 */
239 	if (PTD == NULL) {
240 		s = _kvm_pa2off(kd, va, pa);
241 		if (s == 0) {
242 			_kvm_err(kd, kd->program,
243 			    "_i386_vatop: bootstrap data not in dump");
244 			goto invalid;
245 		} else
246 			return (I386_PAGE_SIZE - offset);
247 	}
248 
249 	pdeindex = va >> I386_PDRSHIFT;
250 	pde = le32toh(PTD[pdeindex]);
251 	if ((pde & I386_PG_V) == 0) {
252 		_kvm_err(kd, kd->program, "_i386_vatop: pde not valid");
253 		goto invalid;
254 	}
255 
256 	if (pde & I386_PG_PS) {
257 		/*
258 		 * No second-level page table; ptd describes one 4MB
259 		 * page.  (We assume that the kernel wouldn't set
260 		 * PG_PS without enabling it cr0).
261 		 */
262 		offset = va & I386_PAGE_PS_MASK;
263 		a = (pde & I386_PG_PS_FRAME) + offset;
264 		s = _kvm_pa2off(kd, a, pa);
265 		if (s == 0) {
266 			_kvm_err(kd, kd->program,
267 			    "_i386_vatop: 4MB page address not in dump");
268 			goto invalid;
269 		}
270 		return (I386_NBPDR - offset);
271 	}
272 
273 	pteindex = (va >> I386_PAGE_SHIFT) & (I386_NPTEPG - 1);
274 	pte_pa = (pde & I386_PG_FRAME) + (pteindex * sizeof(pte));
275 
276 	s = _kvm_pa2off(kd, pte_pa, &ofs);
277 	if (s < sizeof(pte)) {
278 		_kvm_err(kd, kd->program, "_i386_vatop: pte_pa not found");
279 		goto invalid;
280 	}
281 
282 	/* XXX This has to be a physical address read, kvm_read is virtual */
283 	if (pread(kd->pmfd, &pte, sizeof(pte), ofs) != sizeof(pte)) {
284 		_kvm_syserr(kd, kd->program, "_i386_vatop: pread");
285 		goto invalid;
286 	}
287 	pte = le32toh(pte);
288 	if ((pte & I386_PG_V) == 0) {
289 		_kvm_err(kd, kd->program, "_kvm_kvatop: pte not valid");
290 		goto invalid;
291 	}
292 
293 	a = (pte & I386_PG_FRAME) + offset;
294 	s = _kvm_pa2off(kd, a, pa);
295 	if (s == 0) {
296 		_kvm_err(kd, kd->program, "_i386_vatop: address not in dump");
297 		goto invalid;
298 	} else
299 		return (I386_PAGE_SIZE - offset);
300 
301 invalid:
302 	_kvm_err(kd, 0, "invalid address (0x%jx)", (uintmax_t)va);
303 	return (0);
304 }
305 
306 static int
307 _i386_vatop_pae(kvm_t *kd, kvaddr_t va, off_t *pa)
308 {
309 	struct vmstate *vm;
310 	i386_physaddr_pae_t offset;
311 	i386_physaddr_pae_t pte_pa;
312 	i386_pde_pae_t pde;
313 	i386_pte_pae_t pte;
314 	kvaddr_t pdeindex;
315 	kvaddr_t pteindex;
316 	size_t s;
317 	i386_physaddr_pae_t a;
318 	off_t ofs;
319 	i386_pde_pae_t *PTD;
320 
321 	vm = kd->vmst;
322 	PTD = (i386_pde_pae_t *)vm->PTD;
323 	offset = va & I386_PAGE_MASK;
324 
325 	/*
326 	 * If we are initializing (kernel page table descriptor pointer
327 	 * not yet set) then return pa == va to avoid infinite recursion.
328 	 */
329 	if (PTD == NULL) {
330 		s = _kvm_pa2off(kd, va, pa);
331 		if (s == 0) {
332 			_kvm_err(kd, kd->program,
333 			    "_i386_vatop_pae: bootstrap data not in dump");
334 			goto invalid;
335 		} else
336 			return (I386_PAGE_SIZE - offset);
337 	}
338 
339 	pdeindex = va >> I386_PDRSHIFT_PAE;
340 	pde = le64toh(PTD[pdeindex]);
341 	if ((pde & I386_PG_V) == 0) {
342 		_kvm_err(kd, kd->program, "_kvm_kvatop_pae: pde not valid");
343 		goto invalid;
344 	}
345 
346 	if (pde & I386_PG_PS) {
347 		/*
348 		 * No second-level page table; ptd describes one 2MB
349 		 * page.  (We assume that the kernel wouldn't set
350 		 * PG_PS without enabling it cr0).
351 		 */
352 		offset = va & I386_PAGE_PS_MASK_PAE;
353 		a = (pde & I386_PG_PS_FRAME_PAE) + offset;
354 		s = _kvm_pa2off(kd, a, pa);
355 		if (s == 0) {
356 			_kvm_err(kd, kd->program,
357 			    "_i386_vatop: 2MB page address not in dump");
358 			goto invalid;
359 		}
360 		return (I386_NBPDR_PAE - offset);
361 	}
362 
363 	pteindex = (va >> I386_PAGE_SHIFT) & (I386_NPTEPG_PAE - 1);
364 	pte_pa = (pde & I386_PG_FRAME_PAE) + (pteindex * sizeof(pde));
365 
366 	s = _kvm_pa2off(kd, pte_pa, &ofs);
367 	if (s < sizeof(pte)) {
368 		_kvm_err(kd, kd->program, "_i386_vatop_pae: pdpe_pa not found");
369 		goto invalid;
370 	}
371 
372 	/* XXX This has to be a physical address read, kvm_read is virtual */
373 	if (pread(kd->pmfd, &pte, sizeof(pte), ofs) != sizeof(pte)) {
374 		_kvm_syserr(kd, kd->program, "_i386_vatop_pae: read");
375 		goto invalid;
376 	}
377 	pte = le64toh(pte);
378 	if ((pte & I386_PG_V) == 0) {
379 		_kvm_err(kd, kd->program, "_i386_vatop_pae: pte not valid");
380 		goto invalid;
381 	}
382 
383 	a = (pte & I386_PG_FRAME_PAE) + offset;
384 	s = _kvm_pa2off(kd, a, pa);
385 	if (s == 0) {
386 		_kvm_err(kd, kd->program,
387 		    "_i386_vatop_pae: address not in dump");
388 		goto invalid;
389 	} else
390 		return (I386_PAGE_SIZE - offset);
391 
392 invalid:
393 	_kvm_err(kd, 0, "invalid address (0x%jx)", (uintmax_t)va);
394 	return (0);
395 }
396 
397 static int
398 _i386_kvatop(kvm_t *kd, kvaddr_t va, off_t *pa)
399 {
400 
401 	if (ISALIVE(kd)) {
402 		_kvm_err(kd, 0, "vatop called in live kernel!");
403 		return (0);
404 	}
405 	if (kd->vmst->pae)
406 		return (_i386_vatop_pae(kd, va, pa));
407 	else
408 		return (_i386_vatop(kd, va, pa));
409 }
410 
411 int
412 _i386_native(kvm_t *kd __unused)
413 {
414 
415 #ifdef __i386__
416 	return (1);
417 #else
418 	return (0);
419 #endif
420 }
421 
422 static struct kvm_arch kvm_i386 = {
423 	.ka_probe = _i386_probe,
424 	.ka_initvtop = _i386_initvtop,
425 	.ka_freevtop = _i386_freevtop,
426 	.ka_kvatop = _i386_kvatop,
427 	.ka_native = _i386_native,
428 };
429 
430 KVM_ARCH(kvm_i386);
431