xref: /freebsd/lib/libkvm/kvm_i386.c (revision 1670a1c2a47d10ecccd001970b859caf93cd3b6e)
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  * 4. 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/user.h>
50 #include <sys/proc.h>
51 #include <sys/stat.h>
52 #include <sys/mman.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56 #include <nlist.h>
57 #include <kvm.h>
58 
59 #include <vm/vm.h>
60 #include <vm/vm_param.h>
61 
62 #include <machine/elf.h>
63 
64 #include <limits.h>
65 
66 #include "kvm_private.h"
67 
68 #ifndef btop
69 #define	btop(x)		(i386_btop(x))
70 #define	ptob(x)		(i386_ptob(x))
71 #endif
72 
73 #define	PG_FRAME_PAE	(~((uint64_t)PAGE_MASK))
74 #define	PDRSHIFT_PAE	21
75 #define	NPTEPG_PAE	(PAGE_SIZE/sizeof(uint64_t))
76 #define	NBPDR_PAE	(1<<PDRSHIFT_PAE)
77 
78 /* minidump must be the first item! */
79 struct vmstate {
80 	int		minidump;	/* 1 = minidump mode */
81 	void		*mmapbase;
82 	size_t		mmapsize;
83 	void		*PTD;
84 	int		pae;
85 };
86 
87 /*
88  * Map the ELF headers into the process' address space. We do this in two
89  * steps: first the ELF header itself and using that information the whole
90  * set of headers. (Taken from kvm_ia64.c)
91  */
92 static int
93 _kvm_maphdrs(kvm_t *kd, size_t sz)
94 {
95 	struct vmstate *vm = kd->vmst;
96 
97 	/* munmap() previous mmap(). */
98 	if (vm->mmapbase != NULL) {
99 		munmap(vm->mmapbase, vm->mmapsize);
100 		vm->mmapbase = NULL;
101 	}
102 
103 	vm->mmapsize = sz;
104 	vm->mmapbase = mmap(NULL, sz, PROT_READ, MAP_PRIVATE, kd->pmfd, 0);
105 	if (vm->mmapbase == MAP_FAILED) {
106 		_kvm_err(kd, kd->program, "cannot mmap corefile");
107 		return (-1);
108 	}
109 	return (0);
110 }
111 
112 /*
113  * Translate a physical memory address to a file-offset in the crash-dump.
114  * (Taken from kvm_ia64.c)
115  */
116 static size_t
117 _kvm_pa2off(kvm_t *kd, uint64_t pa, off_t *ofs)
118 {
119 	Elf_Ehdr *e = kd->vmst->mmapbase;
120 	Elf_Phdr *p;
121 	int n;
122 
123 	if (kd->rawdump) {
124 		*ofs = pa;
125 		return (PAGE_SIZE - ((size_t)pa & PAGE_MASK));
126 	}
127 
128 	p = (Elf_Phdr*)((char*)e + e->e_phoff);
129 	n = e->e_phnum;
130 	while (n && (pa < p->p_paddr || pa >= p->p_paddr + p->p_memsz))
131 		p++, n--;
132 	if (n == 0)
133 		return (0);
134 	*ofs = (pa - p->p_paddr) + p->p_offset;
135 	return (PAGE_SIZE - ((size_t)pa & PAGE_MASK));
136 }
137 
138 void
139 _kvm_freevtop(kvm_t *kd)
140 {
141 	struct vmstate *vm = kd->vmst;
142 
143 	if (kd->vmst->minidump)
144 		return (_kvm_minidump_freevtop(kd));
145 	if (vm->mmapbase != NULL)
146 		munmap(vm->mmapbase, vm->mmapsize);
147 	if (vm->PTD)
148 		free(vm->PTD);
149 	free(vm);
150 	kd->vmst = NULL;
151 }
152 
153 int
154 _kvm_initvtop(kvm_t *kd)
155 {
156 	struct nlist nlist[2];
157 	u_long pa;
158 	u_long kernbase;
159 	char		*PTD;
160 	Elf_Ehdr	*ehdr;
161 	size_t		hdrsz;
162 	int		i;
163 	char		minihdr[8];
164 
165 	if (!kd->rawdump && pread(kd->pmfd, &minihdr, 8, 0) == 8)
166 		if (memcmp(&minihdr, "minidump", 8) == 0)
167 			return (_kvm_minidump_initvtop(kd));
168 
169 	kd->vmst = (struct vmstate *)_kvm_malloc(kd, sizeof(*kd->vmst));
170 	if (kd->vmst == 0) {
171 		_kvm_err(kd, kd->program, "cannot allocate vm");
172 		return (-1);
173 	}
174 	kd->vmst->PTD = 0;
175 
176 	if (kd->rawdump == 0) {
177 		if (_kvm_maphdrs(kd, sizeof(Elf_Ehdr)) == -1)
178 			return (-1);
179 
180 		ehdr = kd->vmst->mmapbase;
181 		hdrsz = ehdr->e_phoff + ehdr->e_phentsize * ehdr->e_phnum;
182 		if (_kvm_maphdrs(kd, hdrsz) == -1)
183 			return (-1);
184 	}
185 
186 	nlist[0].n_name = "kernbase";
187 	nlist[1].n_name = 0;
188 
189 	if (kvm_nlist(kd, nlist) != 0)
190 		kernbase = KERNBASE;	/* for old kernels */
191 	else
192 		kernbase = nlist[0].n_value;
193 
194 	nlist[0].n_name = "IdlePDPT";
195 	nlist[1].n_name = 0;
196 
197 	if (kvm_nlist(kd, nlist) == 0) {
198 		uint64_t pa64;
199 
200 		if (kvm_read(kd, (nlist[0].n_value - kernbase), &pa,
201 		    sizeof(pa)) != sizeof(pa)) {
202 			_kvm_err(kd, kd->program, "cannot read IdlePDPT");
203 			return (-1);
204 		}
205 		PTD = _kvm_malloc(kd, 4 * PAGE_SIZE);
206 		for (i = 0; i < 4; i++) {
207 			if (kvm_read(kd, pa + (i * sizeof(pa64)), &pa64,
208 			    sizeof(pa64)) != sizeof(pa64)) {
209 				_kvm_err(kd, kd->program, "Cannot read PDPT");
210 				free(PTD);
211 				return (-1);
212 			}
213 			if (kvm_read(kd, pa64 & PG_FRAME_PAE,
214 			    PTD + (i * PAGE_SIZE), PAGE_SIZE) != (PAGE_SIZE)) {
215 				_kvm_err(kd, kd->program, "cannot read PDPT");
216 				free(PTD);
217 				return (-1);
218 			}
219 		}
220 		kd->vmst->PTD = PTD;
221 		kd->vmst->pae = 1;
222 	} else {
223 		nlist[0].n_name = "IdlePTD";
224 		nlist[1].n_name = 0;
225 
226 		if (kvm_nlist(kd, nlist) != 0) {
227 			_kvm_err(kd, kd->program, "bad namelist");
228 			return (-1);
229 		}
230 		if (kvm_read(kd, (nlist[0].n_value - kernbase), &pa,
231 		    sizeof(pa)) != sizeof(pa)) {
232 			_kvm_err(kd, kd->program, "cannot read IdlePTD");
233 			return (-1);
234 		}
235 		PTD = _kvm_malloc(kd, PAGE_SIZE);
236 		if (kvm_read(kd, pa, PTD, PAGE_SIZE) != PAGE_SIZE) {
237 			_kvm_err(kd, kd->program, "cannot read PTD");
238 			return (-1);
239 		}
240 		kd->vmst->PTD = PTD;
241 		return (0);
242 		kd->vmst->pae = 0;
243 	}
244 	return (0);
245 }
246 
247 static int
248 _kvm_vatop(kvm_t *kd, u_long va, off_t *pa)
249 {
250 	struct vmstate *vm;
251 	u_long offset;
252 	u_long pte_pa;
253 	u_long pde_pa;
254 	pd_entry_t pde;
255 	pt_entry_t pte;
256 	u_long pdeindex;
257 	u_long pteindex;
258 	size_t s;
259 	u_long a;
260 	off_t ofs;
261 	uint32_t *PTD;
262 
263 	vm = kd->vmst;
264 	PTD = (uint32_t *)vm->PTD;
265 	offset = va & (PAGE_SIZE - 1);
266 
267 	/*
268 	 * If we are initializing (kernel page table descriptor pointer
269 	 * not yet set) then return pa == va to avoid infinite recursion.
270 	 */
271 	if (PTD == 0) {
272 		s = _kvm_pa2off(kd, va, pa);
273 		if (s == 0) {
274 			_kvm_err(kd, kd->program,
275 			    "_kvm_vatop: bootstrap data not in dump");
276 			goto invalid;
277 		} else
278 			return (PAGE_SIZE - offset);
279 	}
280 
281 	pdeindex = va >> PDRSHIFT;
282 	pde = PTD[pdeindex];
283 	if (((u_long)pde & PG_V) == 0) {
284 		_kvm_err(kd, kd->program, "_kvm_vatop: pde not valid");
285 		goto invalid;
286 	}
287 
288 	if ((u_long)pde & PG_PS) {
289 	      /*
290 	       * No second-level page table; ptd describes one 4MB page.
291 	       * (We assume that the kernel wouldn't set PG_PS without enabling
292 	       * it cr0).
293 	       */
294 #define	PAGE4M_MASK	(NBPDR - 1)
295 #define	PG_FRAME4M	(~PAGE4M_MASK)
296 		pde_pa = ((u_long)pde & PG_FRAME4M) + (va & PAGE4M_MASK);
297 		s = _kvm_pa2off(kd, pde_pa, &ofs);
298 		if (s == 0) {
299 			_kvm_err(kd, kd->program,
300 			    "_kvm_vatop: 4MB page address not in dump");
301 			goto invalid;
302 		}
303 		*pa = ofs;
304 		return (NBPDR - (va & PAGE4M_MASK));
305 	}
306 
307 	pteindex = (va >> PAGE_SHIFT) & (NPTEPG-1);
308 	pte_pa = ((u_long)pde & PG_FRAME) + (pteindex * sizeof(pde));
309 
310 	s = _kvm_pa2off(kd, pte_pa, &ofs);
311 	if (s < sizeof pte) {
312 		_kvm_err(kd, kd->program, "_kvm_vatop: pdpe_pa not found");
313 		goto invalid;
314 	}
315 
316 	/* XXX This has to be a physical address read, kvm_read is virtual */
317 	if (lseek(kd->pmfd, ofs, 0) == -1) {
318 		_kvm_syserr(kd, kd->program, "_kvm_vatop: lseek");
319 		goto invalid;
320 	}
321 	if (read(kd->pmfd, &pte, sizeof pte) != sizeof pte) {
322 		_kvm_syserr(kd, kd->program, "_kvm_vatop: read");
323 		goto invalid;
324 	}
325 	if (((u_long)pte & PG_V) == 0) {
326 		_kvm_err(kd, kd->program, "_kvm_kvatop: pte not valid");
327 		goto invalid;
328 	}
329 
330 	a = ((u_long)pte & PG_FRAME) + offset;
331 	s =_kvm_pa2off(kd, a, pa);
332 	if (s == 0) {
333 		_kvm_err(kd, kd->program, "_kvm_vatop: address not in dump");
334 		goto invalid;
335 	} else
336 		return (PAGE_SIZE - offset);
337 
338 invalid:
339 	_kvm_err(kd, 0, "invalid address (0x%lx)", va);
340 	return (0);
341 }
342 
343 static int
344 _kvm_vatop_pae(kvm_t *kd, u_long va, off_t *pa)
345 {
346 	struct vmstate *vm;
347 	uint64_t offset;
348 	uint64_t pte_pa;
349 	uint64_t pde_pa;
350 	uint64_t pde;
351 	uint64_t pte;
352 	u_long pdeindex;
353 	u_long pteindex;
354 	size_t s;
355 	uint64_t a;
356 	off_t ofs;
357 	uint64_t *PTD;
358 
359 	vm = kd->vmst;
360 	PTD = (uint64_t *)vm->PTD;
361 	offset = va & (PAGE_SIZE - 1);
362 
363 	/*
364 	 * If we are initializing (kernel page table descriptor pointer
365 	 * not yet set) then return pa == va to avoid infinite recursion.
366 	 */
367 	if (PTD == 0) {
368 		s = _kvm_pa2off(kd, va, pa);
369 		if (s == 0) {
370 			_kvm_err(kd, kd->program,
371 			    "_kvm_vatop_pae: bootstrap data not in dump");
372 			goto invalid;
373 		} else
374 			return (PAGE_SIZE - offset);
375 	}
376 
377 	pdeindex = va >> PDRSHIFT_PAE;
378 	pde = PTD[pdeindex];
379 	if (((u_long)pde & PG_V) == 0) {
380 		_kvm_err(kd, kd->program, "_kvm_kvatop_pae: pde not valid");
381 		goto invalid;
382 	}
383 
384 	if ((u_long)pde & PG_PS) {
385 	      /*
386 	       * No second-level page table; ptd describes one 2MB page.
387 	       * (We assume that the kernel wouldn't set PG_PS without enabling
388 	       * it cr0).
389 	       */
390 #define	PAGE2M_MASK	(NBPDR_PAE - 1)
391 #define	PG_FRAME2M	(~PAGE2M_MASK)
392 		pde_pa = ((u_long)pde & PG_FRAME2M) + (va & PAGE2M_MASK);
393 		s = _kvm_pa2off(kd, pde_pa, &ofs);
394 		if (s == 0) {
395 			_kvm_err(kd, kd->program,
396 			    "_kvm_vatop: 2MB page address not in dump");
397 			goto invalid;
398 		}
399 		*pa = ofs;
400 		return (NBPDR_PAE - (va & PAGE2M_MASK));
401 	}
402 
403 	pteindex = (va >> PAGE_SHIFT) & (NPTEPG_PAE-1);
404 	pte_pa = ((uint64_t)pde & PG_FRAME_PAE) + (pteindex * sizeof(pde));
405 
406 	s = _kvm_pa2off(kd, pte_pa, &ofs);
407 	if (s < sizeof pte) {
408 		_kvm_err(kd, kd->program, "_kvm_vatop_pae: pdpe_pa not found");
409 		goto invalid;
410 	}
411 
412 	/* XXX This has to be a physical address read, kvm_read is virtual */
413 	if (lseek(kd->pmfd, ofs, 0) == -1) {
414 		_kvm_syserr(kd, kd->program, "_kvm_vatop_pae: lseek");
415 		goto invalid;
416 	}
417 	if (read(kd->pmfd, &pte, sizeof pte) != sizeof pte) {
418 		_kvm_syserr(kd, kd->program, "_kvm_vatop_pae: read");
419 		goto invalid;
420 	}
421 	if (((uint64_t)pte & PG_V) == 0) {
422 		_kvm_err(kd, kd->program, "_kvm_vatop_pae: pte not valid");
423 		goto invalid;
424 	}
425 
426 	a = ((uint64_t)pte & PG_FRAME_PAE) + offset;
427 	s =_kvm_pa2off(kd, a, pa);
428 	if (s == 0) {
429 		_kvm_err(kd, kd->program,
430 		    "_kvm_vatop_pae: address not in dump");
431 		goto invalid;
432 	} else
433 		return (PAGE_SIZE - offset);
434 
435 invalid:
436 	_kvm_err(kd, 0, "invalid address (0x%lx)", va);
437 	return (0);
438 }
439 
440 int
441 _kvm_kvatop(kvm_t *kd, u_long va, off_t *pa)
442 {
443 
444 	if (kd->vmst->minidump)
445 		return (_kvm_minidump_kvatop(kd, va, pa));
446 	if (ISALIVE(kd)) {
447 		_kvm_err(kd, 0, "vatop called in live kernel!");
448 		return (0);
449 	}
450 	if (kd->vmst->pae)
451 		return (_kvm_vatop_pae(kd, va, pa));
452 	else
453 		return (_kvm_vatop(kd, va, pa));
454 }
455