xref: /freebsd/lib/libkvm/kvm_minidump_arm.c (revision 1e4896b176ff664dc9c2fce5426bf2fdf8017a7d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2008 Semihalf, Grzegorz Bernacki
5  * Copyright (c) 2006 Peter Wemm
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * From: FreeBSD: src/lib/libkvm/kvm_minidump_i386.c,v 1.2 2006/06/05 08:51:14
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 /*
35  * ARM machine dependent routines for kvm and minidumps.
36  */
37 
38 #include <sys/endian.h>
39 #include <sys/param.h>
40 #include <vm/vm.h>
41 #include <kvm.h>
42 #include <limits.h>
43 #include <stdint.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47 
48 #include "../../sys/arm/include/minidump.h"
49 
50 #include "kvm_private.h"
51 #include "kvm_arm.h"
52 
53 #define	arm_round_page(x)	roundup2((kvaddr_t)(x), ARM_PAGE_SIZE)
54 
55 struct vmstate {
56 	struct		minidumphdr hdr;
57 	unsigned char	ei_data;
58 };
59 
60 static arm_pt_entry_t
61 _arm_pte_get(kvm_t *kd, u_long pteindex)
62 {
63 	arm_pt_entry_t *pte = _kvm_pmap_get(kd, pteindex, sizeof(*pte));
64 
65 	return _kvm32toh(kd, *pte);
66 }
67 
68 static int
69 _arm_minidump_probe(kvm_t *kd)
70 {
71 
72 	return (_kvm_probe_elf_kernel(kd, ELFCLASS32, EM_ARM) &&
73 	    _kvm_is_minidump(kd));
74 }
75 
76 static void
77 _arm_minidump_freevtop(kvm_t *kd)
78 {
79 	struct vmstate *vm = kd->vmst;
80 
81 	free(vm);
82 	kd->vmst = NULL;
83 }
84 
85 static int
86 _arm_minidump_initvtop(kvm_t *kd)
87 {
88 	struct vmstate *vmst;
89 	off_t off, dump_avail_off, sparse_off;
90 
91 	vmst = _kvm_malloc(kd, sizeof(*vmst));
92 	if (vmst == NULL) {
93 		_kvm_err(kd, kd->program, "cannot allocate vm");
94 		return (-1);
95 	}
96 
97 	kd->vmst = vmst;
98 
99 	if (pread(kd->pmfd, &vmst->hdr,
100 	    sizeof(vmst->hdr), 0) != sizeof(vmst->hdr)) {
101 		_kvm_err(kd, kd->program, "cannot read dump header");
102 		return (-1);
103 	}
104 
105 	if (strncmp(MINIDUMP_MAGIC, vmst->hdr.magic,
106 	    sizeof(vmst->hdr.magic)) != 0) {
107 		_kvm_err(kd, kd->program, "not a minidump for this platform");
108 		return (-1);
109 	}
110 	vmst->hdr.version = _kvm32toh(kd, vmst->hdr.version);
111 	if (vmst->hdr.version != MINIDUMP_VERSION && vmst->hdr.version != 1) {
112 		_kvm_err(kd, kd->program, "wrong minidump version. "
113 		    "Expected %d got %d", MINIDUMP_VERSION, vmst->hdr.version);
114 		return (-1);
115 	}
116 	vmst->hdr.msgbufsize = _kvm32toh(kd, vmst->hdr.msgbufsize);
117 	vmst->hdr.bitmapsize = _kvm32toh(kd, vmst->hdr.bitmapsize);
118 	vmst->hdr.ptesize = _kvm32toh(kd, vmst->hdr.ptesize);
119 	vmst->hdr.kernbase = _kvm32toh(kd, vmst->hdr.kernbase);
120 	vmst->hdr.arch = _kvm32toh(kd, vmst->hdr.arch);
121 	vmst->hdr.mmuformat = _kvm32toh(kd, vmst->hdr.mmuformat);
122 	if (vmst->hdr.mmuformat == MINIDUMP_MMU_FORMAT_UNKNOWN) {
123 		/* This is a safe default as 1K pages are not used. */
124 		vmst->hdr.mmuformat = MINIDUMP_MMU_FORMAT_V6;
125 	}
126 	vmst->hdr.dumpavailsize = vmst->hdr.version == MINIDUMP_VERSION ?
127 	    _kvm32toh(kd, vmst->hdr.dumpavailsize) : 0;
128 
129 	/* Skip header and msgbuf */
130 	dump_avail_off = ARM_PAGE_SIZE + arm_round_page(vmst->hdr.msgbufsize);
131 
132 	/* Skip dump_avail */
133 	off = dump_avail_off + arm_round_page(vmst->hdr.dumpavailsize);
134 
135 	sparse_off = off + arm_round_page(vmst->hdr.bitmapsize) +
136 	    arm_round_page(vmst->hdr.ptesize);
137 	if (_kvm_pt_init(kd, vmst->hdr.dumpavailsize, dump_avail_off,
138 	    vmst->hdr.bitmapsize, off, sparse_off, ARM_PAGE_SIZE,
139 	    sizeof(uint32_t)) == -1) {
140 		return (-1);
141 	}
142 	off += arm_round_page(vmst->hdr.bitmapsize);
143 
144 	if (_kvm_pmap_init(kd, vmst->hdr.ptesize, off) == -1) {
145 		return (-1);
146 	}
147 	off += arm_round_page(vmst->hdr.ptesize);
148 
149 	return (0);
150 }
151 
152 static int
153 _arm_minidump_kvatop(kvm_t *kd, kvaddr_t va, off_t *pa)
154 {
155 	struct vmstate *vm;
156 	arm_pt_entry_t pte;
157 	arm_physaddr_t offset, a;
158 	kvaddr_t pteindex;
159 	off_t ofs;
160 
161 	if (ISALIVE(kd)) {
162 		_kvm_err(kd, 0, "_arm_minidump_kvatop called in live kernel!");
163 		return (0);
164 	}
165 
166 	vm = kd->vmst;
167 
168 	if (va >= vm->hdr.kernbase) {
169 		pteindex = (va - vm->hdr.kernbase) >> ARM_PAGE_SHIFT;
170 		if (pteindex >= vm->hdr.ptesize / sizeof(pte))
171 			goto invalid;
172 		pte = _arm_pte_get(kd, pteindex);
173 		if ((pte & ARM_L2_TYPE_MASK) == ARM_L2_TYPE_INV) {
174 			_kvm_err(kd, kd->program,
175 			    "_arm_minidump_kvatop: pte not valid");
176 			goto invalid;
177 		}
178 		if ((pte & ARM_L2_TYPE_MASK) == ARM_L2_TYPE_L) {
179 			/* 64K page -> convert to be like 4K page */
180 			offset = va & ARM_L2_S_OFFSET;
181 			a = (pte & ARM_L2_L_FRAME) +
182 			    (va & ARM_L2_L_OFFSET & ARM_L2_S_FRAME);
183 		} else {
184 			if (kd->vmst->hdr.mmuformat == MINIDUMP_MMU_FORMAT_V4 &&
185 			    (pte & ARM_L2_TYPE_MASK) == ARM_L2_TYPE_T) {
186 				_kvm_err(kd, kd->program,
187 				    "_arm_minidump_kvatop: pte not supported");
188 				goto invalid;
189 			}
190 			/* 4K page */
191 			offset = va & ARM_L2_S_OFFSET;
192 			a = pte & ARM_L2_S_FRAME;
193 		}
194 
195 		ofs = _kvm_pt_find(kd, a, ARM_PAGE_SIZE);
196 		if (ofs == -1) {
197 			_kvm_err(kd, kd->program, "_arm_minidump_kvatop: "
198 			    "physical address 0x%jx not in minidump",
199 			    (uintmax_t)a);
200 			goto invalid;
201 		}
202 
203 		*pa = ofs + offset;
204 		return (ARM_PAGE_SIZE - offset);
205 	} else
206 		_kvm_err(kd, kd->program, "_arm_minidump_kvatop: virtual "
207 		    "address 0x%jx not minidumped", (uintmax_t)va);
208 
209 invalid:
210 	_kvm_err(kd, 0, "invalid address (0x%jx)", (uintmax_t)va);
211 	return (0);
212 }
213 
214 static vm_prot_t
215 _arm_entry_to_prot(kvm_t *kd, arm_pt_entry_t pte)
216 {
217 	struct vmstate *vm = kd->vmst;
218 	vm_prot_t prot = VM_PROT_READ;
219 
220 	/* Source: arm/arm/pmap-v4.c:pmap_fault_fixup() */
221 	if (vm->hdr.mmuformat == MINIDUMP_MMU_FORMAT_V4) {
222 		if (pte & ARM_L2_S_PROT_W)
223 			prot |= VM_PROT_WRITE;
224 		return prot;
225 	}
226 
227 	/* Source: arm/arm/pmap-v6.c:pmap_protect() */
228 	if ((pte & ARM_PTE2_RO) == 0)
229 		prot |= VM_PROT_WRITE;
230 	if ((pte & ARM_PTE2_NX) == 0)
231 		prot |= VM_PROT_EXECUTE;
232 	return prot;
233 }
234 
235 static int
236 _arm_minidump_walk_pages(kvm_t *kd, kvm_walk_pages_cb_t *cb, void *arg)
237 {
238 	struct vmstate *vm = kd->vmst;
239 	u_long nptes = vm->hdr.ptesize / sizeof(arm_pt_entry_t);
240 	u_long dva, pa, pteindex, va;
241 
242 	for (pteindex = 0; pteindex < nptes; pteindex++) {
243 		arm_pt_entry_t pte = _arm_pte_get(kd, pteindex);
244 
245 		if ((pte & ARM_L2_TYPE_MASK) == ARM_L2_TYPE_INV)
246 			continue;
247 
248 		va = vm->hdr.kernbase + (pteindex << ARM_PAGE_SHIFT);
249 		if ((pte & ARM_L2_TYPE_MASK) == ARM_L2_TYPE_L) {
250 			/* 64K page */
251 			pa = (pte & ARM_L2_L_FRAME) +
252 			    (va & ARM_L2_L_OFFSET & ARM_L2_S_FRAME);
253 		} else {
254 			if (vm->hdr.mmuformat == MINIDUMP_MMU_FORMAT_V4 &&
255 			    (pte & ARM_L2_TYPE_MASK) == ARM_L2_TYPE_T) {
256 				continue;
257 			}
258 			/* 4K page */
259 			pa = pte & ARM_L2_S_FRAME;
260 		}
261 
262 		dva = 0; /* no direct map on this platform */
263 		if (!_kvm_visit_cb(kd, cb, arg, pa, va, dva,
264 		    _arm_entry_to_prot(kd, pte), ARM_PAGE_SIZE, 0))
265 			return (0);
266 	}
267 	return (1);
268 }
269 
270 static struct kvm_arch kvm_arm_minidump = {
271 	.ka_probe = _arm_minidump_probe,
272 	.ka_initvtop = _arm_minidump_initvtop,
273 	.ka_freevtop = _arm_minidump_freevtop,
274 	.ka_kvatop = _arm_minidump_kvatop,
275 	.ka_native = _arm_native,
276 	.ka_walk_pages = _arm_minidump_walk_pages,
277 };
278 
279 KVM_ARCH(kvm_arm_minidump);
280