xref: /freebsd/lib/libkvm/kvm_minidump_arm.c (revision 9978ade4980452e3699285c01a4b9c4b5adc68e4)
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) == -1) {
139 		return (-1);
140 	}
141 	off += arm_round_page(vmst->hdr.bitmapsize);
142 
143 	if (_kvm_pmap_init(kd, vmst->hdr.ptesize, off) == -1) {
144 		return (-1);
145 	}
146 	off += arm_round_page(vmst->hdr.ptesize);
147 
148 	return (0);
149 }
150 
151 static int
152 _arm_minidump_kvatop(kvm_t *kd, kvaddr_t va, off_t *pa)
153 {
154 	struct vmstate *vm;
155 	arm_pt_entry_t pte;
156 	arm_physaddr_t offset, a;
157 	kvaddr_t pteindex;
158 	off_t ofs;
159 
160 	if (ISALIVE(kd)) {
161 		_kvm_err(kd, 0, "_arm_minidump_kvatop called in live kernel!");
162 		return (0);
163 	}
164 
165 	vm = kd->vmst;
166 
167 	if (va >= vm->hdr.kernbase) {
168 		pteindex = (va - vm->hdr.kernbase) >> ARM_PAGE_SHIFT;
169 		if (pteindex >= vm->hdr.ptesize / sizeof(pte))
170 			goto invalid;
171 		pte = _arm_pte_get(kd, pteindex);
172 		if ((pte & ARM_L2_TYPE_MASK) == ARM_L2_TYPE_INV) {
173 			_kvm_err(kd, kd->program,
174 			    "_arm_minidump_kvatop: pte not valid");
175 			goto invalid;
176 		}
177 		if ((pte & ARM_L2_TYPE_MASK) == ARM_L2_TYPE_L) {
178 			/* 64K page -> convert to be like 4K page */
179 			offset = va & ARM_L2_S_OFFSET;
180 			a = (pte & ARM_L2_L_FRAME) +
181 			    (va & ARM_L2_L_OFFSET & ARM_L2_S_FRAME);
182 		} else {
183 			if (kd->vmst->hdr.mmuformat == MINIDUMP_MMU_FORMAT_V4 &&
184 			    (pte & ARM_L2_TYPE_MASK) == ARM_L2_TYPE_T) {
185 				_kvm_err(kd, kd->program,
186 				    "_arm_minidump_kvatop: pte not supported");
187 				goto invalid;
188 			}
189 			/* 4K page */
190 			offset = va & ARM_L2_S_OFFSET;
191 			a = pte & ARM_L2_S_FRAME;
192 		}
193 
194 		ofs = _kvm_pt_find(kd, a, ARM_PAGE_SIZE);
195 		if (ofs == -1) {
196 			_kvm_err(kd, kd->program, "_arm_minidump_kvatop: "
197 			    "physical address 0x%jx not in minidump",
198 			    (uintmax_t)a);
199 			goto invalid;
200 		}
201 
202 		*pa = ofs + offset;
203 		return (ARM_PAGE_SIZE - offset);
204 	} else
205 		_kvm_err(kd, kd->program, "_arm_minidump_kvatop: virtual "
206 		    "address 0x%jx not minidumped", (uintmax_t)va);
207 
208 invalid:
209 	_kvm_err(kd, 0, "invalid address (0x%jx)", (uintmax_t)va);
210 	return (0);
211 }
212 
213 static vm_prot_t
214 _arm_entry_to_prot(kvm_t *kd, arm_pt_entry_t pte)
215 {
216 	struct vmstate *vm = kd->vmst;
217 	vm_prot_t prot = VM_PROT_READ;
218 
219 	/* Source: arm/arm/pmap-v4.c:pmap_fault_fixup() */
220 	if (vm->hdr.mmuformat == MINIDUMP_MMU_FORMAT_V4) {
221 		if (pte & ARM_L2_S_PROT_W)
222 			prot |= VM_PROT_WRITE;
223 		return prot;
224 	}
225 
226 	/* Source: arm/arm/pmap-v6.c:pmap_protect() */
227 	if ((pte & ARM_PTE2_RO) == 0)
228 		prot |= VM_PROT_WRITE;
229 	if ((pte & ARM_PTE2_NX) == 0)
230 		prot |= VM_PROT_EXECUTE;
231 	return prot;
232 }
233 
234 static int
235 _arm_minidump_walk_pages(kvm_t *kd, kvm_walk_pages_cb_t *cb, void *arg)
236 {
237 	struct vmstate *vm = kd->vmst;
238 	u_long nptes = vm->hdr.ptesize / sizeof(arm_pt_entry_t);
239 	u_long dva, pa, pteindex, va;
240 
241 	for (pteindex = 0; pteindex < nptes; pteindex++) {
242 		arm_pt_entry_t pte = _arm_pte_get(kd, pteindex);
243 
244 		if ((pte & ARM_L2_TYPE_MASK) == ARM_L2_TYPE_INV)
245 			continue;
246 
247 		va = vm->hdr.kernbase + (pteindex << ARM_PAGE_SHIFT);
248 		if ((pte & ARM_L2_TYPE_MASK) == ARM_L2_TYPE_L) {
249 			/* 64K page */
250 			pa = (pte & ARM_L2_L_FRAME) +
251 			    (va & ARM_L2_L_OFFSET & ARM_L2_S_FRAME);
252 		} else {
253 			if (vm->hdr.mmuformat == MINIDUMP_MMU_FORMAT_V4 &&
254 			    (pte & ARM_L2_TYPE_MASK) == ARM_L2_TYPE_T) {
255 				continue;
256 			}
257 			/* 4K page */
258 			pa = pte & ARM_L2_S_FRAME;
259 		}
260 
261 		dva = 0; /* no direct map on this platform */
262 		if (!_kvm_visit_cb(kd, cb, arg, pa, va, dva,
263 		    _arm_entry_to_prot(kd, pte), ARM_PAGE_SIZE, 0))
264 			return (0);
265 	}
266 	return (1);
267 }
268 
269 static struct kvm_arch kvm_arm_minidump = {
270 	.ka_probe = _arm_minidump_probe,
271 	.ka_initvtop = _arm_minidump_initvtop,
272 	.ka_freevtop = _arm_minidump_freevtop,
273 	.ka_kvatop = _arm_minidump_kvatop,
274 	.ka_native = _arm_native,
275 	.ka_walk_pages = _arm_minidump_walk_pages,
276 };
277 
278 KVM_ARCH(kvm_arm_minidump);
279