xref: /freebsd/lib/libkvm/kvm_arm.c (revision 39ee7a7a6bdd1557b1c3532abf60d139798ac88b)
1 /*-
2  * Copyright (c) 2005 Olivier Houchard
3  * Copyright (c) 1989, 1992, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * This code is derived from software developed by the Computer Systems
7  * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
8  * BG 91-66 and contributed to Berkeley.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * ARM machine dependent routines for kvm.
36  */
37 
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40 
41 #include <sys/param.h>
42 #include <sys/elf32.h>
43 #include <sys/mman.h>
44 
45 #ifndef CROSS_LIBKVM
46 #include <vm/vm.h>
47 #include <vm/vm_param.h>
48 #include <vm/pmap.h>
49 #include <machine/pmap.h>
50 #else
51 #include "../../sys/arm/include/pte.h"
52 #include "../../sys/arm/include/vmparam.h"
53 #endif
54 
55 #include <db.h>
56 #include <limits.h>
57 #include <kvm.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <unistd.h>
61 
62 #include "kvm_private.h"
63 
64 /* minidump must be the first item! */
65 struct vmstate {
66 	int minidump;		/* 1 = minidump mode */
67 	pd_entry_t *l1pt;
68 	void *mmapbase;
69 	size_t mmapsize;
70 };
71 
72 static int
73 _kvm_maphdrs(kvm_t *kd, size_t sz)
74 {
75 	struct vmstate *vm = kd->vmst;
76 
77 	/* munmap() previous mmap(). */
78 	if (vm->mmapbase != NULL) {
79 		munmap(vm->mmapbase, vm->mmapsize);
80 		vm->mmapbase = NULL;
81 	}
82 
83 	vm->mmapsize = sz;
84 	vm->mmapbase = mmap(NULL, sz, PROT_READ, MAP_PRIVATE, kd->pmfd, 0);
85 	if (vm->mmapbase == MAP_FAILED) {
86 		_kvm_err(kd, kd->program, "cannot mmap corefile");
87 		return (-1);
88 	}
89 
90 	return (0);
91 }
92 
93 /*
94  * Translate a physical memory address to a file-offset in the crash-dump.
95  */
96 static size_t
97 _kvm_pa2off(kvm_t *kd, uint64_t pa, off_t *ofs, size_t pgsz)
98 {
99 	Elf32_Ehdr *e = kd->vmst->mmapbase;
100 	Elf32_Phdr *p = (Elf32_Phdr*)((char*)e + e->e_phoff);
101 	int n = e->e_phnum;
102 
103 	while (n && (pa < p->p_paddr || pa >= p->p_paddr + p->p_memsz))
104 		p++, n--;
105 	if (n == 0)
106 		return (0);
107 
108 	*ofs = (pa - p->p_paddr) + p->p_offset;
109 	if (pgsz == 0)
110 		return (p->p_memsz - (pa - p->p_paddr));
111 	return (pgsz - ((size_t)pa & (pgsz - 1)));
112 }
113 
114 void
115 _kvm_freevtop(kvm_t *kd)
116 {
117 	if (kd->vmst != 0) {
118 		if (kd->vmst->minidump)
119 			return (_kvm_minidump_freevtop(kd));
120 		if (kd->vmst->mmapbase != NULL)
121 			munmap(kd->vmst->mmapbase, kd->vmst->mmapsize);
122 		free(kd->vmst);
123 		kd->vmst = NULL;
124 	}
125 }
126 
127 int
128 _kvm_initvtop(kvm_t *kd)
129 {
130 	struct vmstate *vm;
131 	struct nlist nl[2];
132 	u_long kernbase, physaddr, pa;
133 	pd_entry_t *l1pt;
134 	Elf32_Ehdr *ehdr;
135 	Elf32_Phdr *phdr;
136 	size_t hdrsz;
137 	char minihdr[8];
138 	int found, i;
139 
140 	if (!kd->rawdump) {
141 		if (pread(kd->pmfd, &minihdr, 8, 0) == 8) {
142 			if (memcmp(&minihdr, "minidump", 8) == 0)
143 				return (_kvm_minidump_initvtop(kd));
144 		} else {
145 			_kvm_err(kd, kd->program, "cannot read header");
146 			return (-1);
147 		}
148 	}
149 
150 	vm = _kvm_malloc(kd, sizeof(*vm));
151 	if (vm == 0) {
152 		_kvm_err(kd, kd->program, "cannot allocate vm");
153 		return (-1);
154 	}
155 	kd->vmst = vm;
156 	vm->l1pt = NULL;
157 	if (_kvm_maphdrs(kd, sizeof(Elf32_Ehdr)) == -1)
158 		return (-1);
159 	ehdr = kd->vmst->mmapbase;
160 	hdrsz = ehdr->e_phoff + ehdr->e_phentsize * ehdr->e_phnum;
161 	if (_kvm_maphdrs(kd, hdrsz) == -1)
162 		return (-1);
163 
164 	phdr = (Elf32_Phdr *)((uint8_t *)ehdr + ehdr->e_phoff);
165 	found = 0;
166 	for (i = 0; i < ehdr->e_phnum; i++) {
167 		if (phdr[i].p_type == PT_DUMP_DELTA) {
168 			kernbase = phdr[i].p_vaddr;
169 			physaddr = phdr[i].p_paddr;
170 			found = 1;
171 			break;
172 		}
173 	}
174 
175 	nl[1].n_name = NULL;
176 	if (!found) {
177 		nl[0].n_name = "kernbase";
178 		if (kvm_nlist(kd, nl) != 0)
179 			kernbase = KERNBASE;
180 		else
181 			kernbase = nl[0].n_value;
182 
183 		nl[0].n_name = "physaddr";
184 		if (kvm_nlist(kd, nl) != 0) {
185 			_kvm_err(kd, kd->program, "couldn't get phys addr");
186 			return (-1);
187 		}
188 		physaddr = nl[0].n_value;
189 	}
190 	nl[0].n_name = "kernel_l1pa";
191 	if (kvm_nlist(kd, nl) != 0) {
192 		_kvm_err(kd, kd->program, "bad namelist");
193 		return (-1);
194 	}
195 	if (kvm_read(kd, (nl[0].n_value - kernbase + physaddr), &pa,
196 	    sizeof(pa)) != sizeof(pa)) {
197 		_kvm_err(kd, kd->program, "cannot read kernel_l1pa");
198 		return (-1);
199 	}
200 	l1pt = _kvm_malloc(kd, L1_TABLE_SIZE);
201 	if (kvm_read(kd, pa, l1pt, L1_TABLE_SIZE) != L1_TABLE_SIZE) {
202 		_kvm_err(kd, kd->program, "cannot read l1pt");
203 		free(l1pt);
204 		return (-1);
205 	}
206 	vm->l1pt = l1pt;
207 	return 0;
208 }
209 
210 /* from arm/pmap.c */
211 #define	L1_IDX(va)		(((vm_offset_t)(va)) >> L1_S_SHIFT)
212 /* from arm/pmap.h */
213 #define	L1_TYPE_INV	0x00		/* Invalid (fault) */
214 #define	L1_TYPE_C	0x01		/* Coarse L2 */
215 #define	L1_TYPE_S	0x02		/* Section */
216 #define	L1_TYPE_F	0x03		/* Fine L2 */
217 #define	L1_TYPE_MASK	0x03		/* mask of type bits */
218 
219 #define	l1pte_section_p(pde)	(((pde) & L1_TYPE_MASK) == L1_TYPE_S)
220 #define	l1pte_valid(pde)	((pde) != 0)
221 #define	l2pte_valid(pte)	((pte) != 0)
222 #define l2pte_index(v)		(((v) & L2_ADDR_BITS) >> L2_S_SHIFT)
223 
224 
225 int
226 _kvm_kvatop(kvm_t *kd, u_long va, off_t *pa)
227 {
228 	struct vmstate *vm = kd->vmst;
229 	pd_entry_t pd;
230 	pt_entry_t pte;
231 	off_t pte_pa;
232 
233 	if (kd->vmst->minidump)
234 		return (_kvm_minidump_kvatop(kd, va, pa));
235 
236 	if (vm->l1pt == NULL)
237 		return (_kvm_pa2off(kd, va, pa, PAGE_SIZE));
238 	pd = vm->l1pt[L1_IDX(va)];
239 	if (!l1pte_valid(pd))
240 		goto invalid;
241 	if (l1pte_section_p(pd)) {
242 		/* 1MB section mapping. */
243 		*pa = ((u_long)pd & L1_S_ADDR_MASK) + (va & L1_S_OFFSET);
244 		return  (_kvm_pa2off(kd, *pa, pa, L1_S_SIZE));
245 	}
246 	pte_pa = (pd & L1_ADDR_MASK) + l2pte_index(va) * sizeof(pte);
247 	_kvm_pa2off(kd, pte_pa, &pte_pa, L1_S_SIZE);
248 	if (lseek(kd->pmfd, pte_pa, 0) == -1) {
249 		_kvm_syserr(kd, kd->program, "_kvm_kvatop: lseek");
250 		goto invalid;
251 	}
252 	if (read(kd->pmfd, &pte, sizeof(pte)) != sizeof (pte)) {
253 		_kvm_syserr(kd, kd->program, "_kvm_kvatop: read");
254 		goto invalid;
255 	}
256 	if (!l2pte_valid(pte)) {
257 		goto invalid;
258 	}
259 	if ((pte & L2_TYPE_MASK) == L2_TYPE_L) {
260 		*pa = (pte & L2_L_FRAME) | (va & L2_L_OFFSET);
261 		return (_kvm_pa2off(kd, *pa, pa, L2_L_SIZE));
262 	}
263 	*pa = (pte & L2_S_FRAME) | (va & L2_S_OFFSET);
264 	return (_kvm_pa2off(kd, *pa, pa, PAGE_SIZE));
265 invalid:
266 	_kvm_err(kd, 0, "Invalid address (%lx)", va);
267 	return 0;
268 }
269 
270 /*
271  * Machine-dependent initialization for ALL open kvm descriptors,
272  * not just those for a kernel crash dump.  Some architectures
273  * have to deal with these NOT being constants!  (i.e. m68k)
274  */
275 #ifdef FBSD_NOT_YET
276 int
277 _kvm_mdopen(kvm_t *kd)
278 {
279 
280 	kd->usrstack = USRSTACK;
281 	kd->min_uva = VM_MIN_ADDRESS;
282 	kd->max_uva = VM_MAXUSER_ADDRESS;
283 
284 	return (0);
285 }
286 #endif
287