xref: /freebsd/lib/libkvm/kvm_arm.c (revision 732a02b4e77866604a120a275c082bb6221bd2ff)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2005 Olivier Houchard
5  * Copyright (c) 1989, 1992, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software developed by the Computer Systems
9  * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
10  * BG 91-66 and contributed to Berkeley.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /*
37  * ARM machine dependent routines for kvm.
38  */
39 
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42 
43 #include <sys/param.h>
44 #include <sys/endian.h>
45 #include <kvm.h>
46 #include <limits.h>
47 #include <stdint.h>
48 #include <stdlib.h>
49 #include <unistd.h>
50 
51 #ifdef __arm__
52 #include <machine/vmparam.h>
53 #endif
54 
55 #include "kvm_private.h"
56 #include "kvm_arm.h"
57 
58 struct vmstate {
59 	arm_pd_entry_t *l1pt;
60 	size_t phnum;
61 	GElf_Phdr *phdr;
62 };
63 
64 /*
65  * Translate a physical memory address to a file-offset in the crash-dump.
66  */
67 static size_t
68 _kvm_pa2off(kvm_t *kd, uint64_t pa, off_t *ofs, size_t pgsz)
69 {
70 	struct vmstate *vm = kd->vmst;
71 	GElf_Phdr *p;
72 	size_t n;
73 
74 	p = vm->phdr;
75 	n = vm->phnum;
76 	while (n && (pa < p->p_paddr || pa >= p->p_paddr + p->p_memsz))
77 		p++, n--;
78 	if (n == 0)
79 		return (0);
80 
81 	*ofs = (pa - p->p_paddr) + p->p_offset;
82 	if (pgsz == 0)
83 		return (p->p_memsz - (pa - p->p_paddr));
84 	return (pgsz - ((size_t)pa & (pgsz - 1)));
85 }
86 
87 static void
88 _arm_freevtop(kvm_t *kd)
89 {
90 	struct vmstate *vm = kd->vmst;
91 
92 	free(vm->phdr);
93 	free(vm);
94 	kd->vmst = NULL;
95 }
96 
97 static int
98 _arm_probe(kvm_t *kd)
99 {
100 
101 	return (_kvm_probe_elf_kernel(kd, ELFCLASS32, EM_ARM) &&
102 	    !_kvm_is_minidump(kd));
103 }
104 
105 static int
106 _arm_initvtop(kvm_t *kd)
107 {
108 	struct vmstate *vm;
109 	struct kvm_nlist nl[2];
110 	kvaddr_t kernbase;
111 	arm_physaddr_t physaddr, pa;
112 	arm_pd_entry_t *l1pt;
113 	size_t i;
114 	int found;
115 
116 	if (kd->rawdump) {
117 		_kvm_err(kd, kd->program, "raw dumps not supported on arm");
118 		return (-1);
119 	}
120 
121 	vm = _kvm_malloc(kd, sizeof(*vm));
122 	if (vm == NULL) {
123 		_kvm_err(kd, kd->program, "cannot allocate vm");
124 		return (-1);
125 	}
126 	kd->vmst = vm;
127 	vm->l1pt = NULL;
128 
129 	if (_kvm_read_core_phdrs(kd, &vm->phnum, &vm->phdr) == -1)
130 		return (-1);
131 
132 	found = 0;
133 	for (i = 0; i < vm->phnum; i++) {
134 		if (vm->phdr[i].p_type == PT_DUMP_DELTA) {
135 			kernbase = vm->phdr[i].p_vaddr;
136 			physaddr = vm->phdr[i].p_paddr;
137 			found = 1;
138 			break;
139 		}
140 	}
141 
142 	nl[1].n_name = NULL;
143 	if (!found) {
144 		nl[0].n_name = "kernbase";
145 		if (kvm_nlist2(kd, nl) != 0) {
146 #ifdef __arm__
147 			kernbase = KERNBASE;
148 #else
149 		_kvm_err(kd, kd->program, "cannot resolve kernbase");
150 		return (-1);
151 #endif
152 		} else
153 			kernbase = nl[0].n_value;
154 
155 		nl[0].n_name = "physaddr";
156 		if (kvm_nlist2(kd, nl) != 0) {
157 			_kvm_err(kd, kd->program, "couldn't get phys addr");
158 			return (-1);
159 		}
160 		physaddr = nl[0].n_value;
161 	}
162 	nl[0].n_name = "kernel_l1pa";
163 	if (kvm_nlist2(kd, nl) != 0) {
164 		_kvm_err(kd, kd->program, "bad namelist");
165 		return (-1);
166 	}
167 	if (kvm_read2(kd, (nl[0].n_value - kernbase + physaddr), &pa,
168 	    sizeof(pa)) != sizeof(pa)) {
169 		_kvm_err(kd, kd->program, "cannot read kernel_l1pa");
170 		return (-1);
171 	}
172 	l1pt = _kvm_malloc(kd, ARM_L1_TABLE_SIZE);
173 	if (l1pt == NULL) {
174 		_kvm_err(kd, kd->program, "cannot allocate l1pt");
175 		return (-1);
176 	}
177 	if (kvm_read2(kd, pa, l1pt, ARM_L1_TABLE_SIZE) != ARM_L1_TABLE_SIZE) {
178 		_kvm_err(kd, kd->program, "cannot read l1pt");
179 		free(l1pt);
180 		return (-1);
181 	}
182 	vm->l1pt = l1pt;
183 	return 0;
184 }
185 
186 /* from arm/pmap.c */
187 #define	ARM_L1_IDX(va)		((va) >> ARM_L1_S_SHIFT)
188 
189 #define	l1pte_section_p(pde)	(((pde) & ARM_L1_TYPE_MASK) == ARM_L1_TYPE_S)
190 #define	l1pte_valid(pde)	((pde) != 0)
191 #define	l2pte_valid(pte)	((pte) != 0)
192 #define l2pte_index(v)		(((v) & ARM_L1_S_OFFSET) >> ARM_L2_S_SHIFT)
193 
194 
195 static int
196 _arm_kvatop(kvm_t *kd, kvaddr_t va, off_t *pa)
197 {
198 	struct vmstate *vm = kd->vmst;
199 	arm_pd_entry_t pd;
200 	arm_pt_entry_t pte;
201 	arm_physaddr_t pte_pa;
202 	off_t pte_off;
203 
204 	if (vm->l1pt == NULL)
205 		return (_kvm_pa2off(kd, va, pa, ARM_PAGE_SIZE));
206 	pd = _kvm32toh(kd, vm->l1pt[ARM_L1_IDX(va)]);
207 	if (!l1pte_valid(pd))
208 		goto invalid;
209 	if (l1pte_section_p(pd)) {
210 		/* 1MB section mapping. */
211 		*pa = (pd & ARM_L1_S_ADDR_MASK) + (va & ARM_L1_S_OFFSET);
212 		return  (_kvm_pa2off(kd, *pa, pa, ARM_L1_S_SIZE));
213 	}
214 	pte_pa = (pd & ARM_L1_C_ADDR_MASK) + l2pte_index(va) * sizeof(pte);
215 	_kvm_pa2off(kd, pte_pa, &pte_off, ARM_L1_S_SIZE);
216 	if (pread(kd->pmfd, &pte, sizeof(pte), pte_off) != sizeof(pte)) {
217 		_kvm_syserr(kd, kd->program, "_arm_kvatop: pread");
218 		goto invalid;
219 	}
220 	pte = _kvm32toh(kd, pte);
221 	if (!l2pte_valid(pte)) {
222 		goto invalid;
223 	}
224 	if ((pte & ARM_L2_TYPE_MASK) == ARM_L2_TYPE_L) {
225 		*pa = (pte & ARM_L2_L_FRAME) | (va & ARM_L2_L_OFFSET);
226 		return (_kvm_pa2off(kd, *pa, pa, ARM_L2_L_SIZE));
227 	}
228 	*pa = (pte & ARM_L2_S_FRAME) | (va & ARM_L2_S_OFFSET);
229 	return (_kvm_pa2off(kd, *pa, pa, ARM_PAGE_SIZE));
230 invalid:
231 	_kvm_err(kd, 0, "Invalid address (%jx)", (uintmax_t)va);
232 	return 0;
233 }
234 
235 /*
236  * Machine-dependent initialization for ALL open kvm descriptors,
237  * not just those for a kernel crash dump.  Some architectures
238  * have to deal with these NOT being constants!  (i.e. m68k)
239  */
240 #ifdef FBSD_NOT_YET
241 int
242 _kvm_mdopen(kvm_t *kd)
243 {
244 
245 	kd->usrstack = USRSTACK;
246 	kd->min_uva = VM_MIN_ADDRESS;
247 	kd->max_uva = VM_MAXUSER_ADDRESS;
248 
249 	return (0);
250 }
251 #endif
252 
253 int
254 #ifdef __arm__
255 _arm_native(kvm_t *kd)
256 #else
257 _arm_native(kvm_t *kd __unused)
258 #endif
259 {
260 
261 #ifdef __arm__
262 #if _BYTE_ORDER == _LITTLE_ENDIAN
263 	return (kd->nlehdr.e_ident[EI_DATA] == ELFDATA2LSB);
264 #else
265 	return (kd->nlehdr.e_ident[EI_DATA] == ELFDATA2MSB);
266 #endif
267 #else
268 	return (0);
269 #endif
270 }
271 
272 static struct kvm_arch kvm_arm = {
273 	.ka_probe = _arm_probe,
274 	.ka_initvtop = _arm_initvtop,
275 	.ka_freevtop = _arm_freevtop,
276 	.ka_kvatop = _arm_kvatop,
277 	.ka_native = _arm_native,
278 };
279 
280 KVM_ARCH(kvm_arm);
281