xref: /freebsd/lib/libkvm/kvm_minidump_aarch64.c (revision dd41de95a84d979615a2ef11df6850622bf6184e)
1 /*-
2  * Copyright (c) 2006 Peter Wemm
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  * From: FreeBSD: src/lib/libkvm/kvm_minidump_amd64.c r261799
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 /*
32  * ARM64 (AArch64) machine dependent routines for kvm and minidumps.
33  */
34 
35 #include <sys/param.h>
36 #include <stdint.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <vm/vm.h>
41 #include <kvm.h>
42 
43 #include "../../sys/arm64/include/minidump.h"
44 
45 #include <limits.h>
46 
47 #include "kvm_private.h"
48 #include "kvm_aarch64.h"
49 
50 #define	aarch64_round_page(x)	roundup2((kvaddr_t)(x), AARCH64_PAGE_SIZE)
51 
52 struct vmstate {
53 	struct minidumphdr hdr;
54 };
55 
56 static aarch64_pte_t
57 _aarch64_pte_get(kvm_t *kd, u_long pteindex)
58 {
59 	aarch64_pte_t *pte = _kvm_pmap_get(kd, pteindex, sizeof(*pte));
60 
61 	return le64toh(*pte);
62 }
63 
64 static int
65 _aarch64_minidump_probe(kvm_t *kd)
66 {
67 
68 	return (_kvm_probe_elf_kernel(kd, ELFCLASS64, EM_AARCH64) &&
69 	    _kvm_is_minidump(kd));
70 }
71 
72 static void
73 _aarch64_minidump_freevtop(kvm_t *kd)
74 {
75 	struct vmstate *vm = kd->vmst;
76 
77 	free(vm);
78 	kd->vmst = NULL;
79 }
80 
81 static int
82 _aarch64_minidump_initvtop(kvm_t *kd)
83 {
84 	struct vmstate *vmst;
85 	off_t off, dump_avail_off, sparse_off;
86 
87 	vmst = _kvm_malloc(kd, sizeof(*vmst));
88 	if (vmst == NULL) {
89 		_kvm_err(kd, kd->program, "cannot allocate vm");
90 		return (-1);
91 	}
92 	kd->vmst = vmst;
93 	if (pread(kd->pmfd, &vmst->hdr, sizeof(vmst->hdr), 0) !=
94 	    sizeof(vmst->hdr)) {
95 		_kvm_err(kd, kd->program, "cannot read dump header");
96 		return (-1);
97 	}
98 	if (strncmp(MINIDUMP_MAGIC, vmst->hdr.magic,
99 	    sizeof(vmst->hdr.magic)) != 0) {
100 		_kvm_err(kd, kd->program, "not a minidump for this platform");
101 		return (-1);
102 	}
103 
104 	vmst->hdr.version = le32toh(vmst->hdr.version);
105 	if (vmst->hdr.version != MINIDUMP_VERSION && vmst->hdr.version != 1) {
106 		_kvm_err(kd, kd->program, "wrong minidump version. "
107 		    "Expected %d got %d", MINIDUMP_VERSION, vmst->hdr.version);
108 		return (-1);
109 	}
110 	vmst->hdr.msgbufsize = le32toh(vmst->hdr.msgbufsize);
111 	vmst->hdr.bitmapsize = le32toh(vmst->hdr.bitmapsize);
112 	vmst->hdr.pmapsize = le32toh(vmst->hdr.pmapsize);
113 	vmst->hdr.kernbase = le64toh(vmst->hdr.kernbase);
114 	vmst->hdr.dmapphys = le64toh(vmst->hdr.dmapphys);
115 	vmst->hdr.dmapbase = le64toh(vmst->hdr.dmapbase);
116 	vmst->hdr.dmapend = le64toh(vmst->hdr.dmapend);
117 	vmst->hdr.dumpavailsize = vmst->hdr.version == MINIDUMP_VERSION ?
118 	    le32toh(vmst->hdr.dumpavailsize) : 0;
119 
120 	/* Skip header and msgbuf */
121 	dump_avail_off = AARCH64_PAGE_SIZE + aarch64_round_page(vmst->hdr.msgbufsize);
122 
123 	/* Skip dump_avail */
124 	off = dump_avail_off + aarch64_round_page(vmst->hdr.dumpavailsize);
125 
126 	/* build physical address lookup table for sparse pages */
127 	sparse_off = off + aarch64_round_page(vmst->hdr.bitmapsize) +
128 	    aarch64_round_page(vmst->hdr.pmapsize);
129 	if (_kvm_pt_init(kd, vmst->hdr.dumpavailsize, dump_avail_off,
130 	    vmst->hdr.bitmapsize, off, sparse_off, AARCH64_PAGE_SIZE) == -1) {
131 		return (-1);
132 	}
133 	off += aarch64_round_page(vmst->hdr.bitmapsize);
134 
135 	if (_kvm_pmap_init(kd, vmst->hdr.pmapsize, off) == -1) {
136 		return (-1);
137 	}
138 	off += aarch64_round_page(vmst->hdr.pmapsize);
139 
140 	return (0);
141 }
142 
143 static int
144 _aarch64_minidump_vatop(kvm_t *kd, kvaddr_t va, off_t *pa)
145 {
146 	struct vmstate *vm;
147 	aarch64_physaddr_t offset;
148 	aarch64_pte_t l3;
149 	kvaddr_t l3_index;
150 	aarch64_physaddr_t a;
151 	off_t ofs;
152 
153 	vm = kd->vmst;
154 	offset = va & AARCH64_PAGE_MASK;
155 
156 	if (va >= vm->hdr.dmapbase && va < vm->hdr.dmapend) {
157 		a = (va - vm->hdr.dmapbase + vm->hdr.dmapphys) &
158 		    ~AARCH64_PAGE_MASK;
159 		ofs = _kvm_pt_find(kd, a, AARCH64_PAGE_SIZE);
160 		if (ofs == -1) {
161 			_kvm_err(kd, kd->program, "_aarch64_minidump_vatop: "
162 			    "direct map address 0x%jx not in minidump",
163 			    (uintmax_t)va);
164 			goto invalid;
165 		}
166 		*pa = ofs + offset;
167 		return (AARCH64_PAGE_SIZE - offset);
168 	} else if (va >= vm->hdr.kernbase) {
169 		l3_index = (va - vm->hdr.kernbase) >> AARCH64_L3_SHIFT;
170 		if (l3_index >= vm->hdr.pmapsize / sizeof(l3))
171 			goto invalid;
172 		l3 = _aarch64_pte_get(kd, l3_index);
173 		if ((l3 & AARCH64_ATTR_DESCR_MASK) != AARCH64_L3_PAGE) {
174 			_kvm_err(kd, kd->program,
175 			    "_aarch64_minidump_vatop: pde not valid");
176 			goto invalid;
177 		}
178 		a = l3 & ~AARCH64_ATTR_MASK;
179 		ofs = _kvm_pt_find(kd, a, AARCH64_PAGE_SIZE);
180 		if (ofs == -1) {
181 			_kvm_err(kd, kd->program, "_aarch64_minidump_vatop: "
182 			    "physical address 0x%jx not in minidump",
183 			    (uintmax_t)a);
184 			goto invalid;
185 		}
186 		*pa = ofs + offset;
187 		return (AARCH64_PAGE_SIZE - offset);
188 	} else {
189 		_kvm_err(kd, kd->program,
190 	    "_aarch64_minidump_vatop: virtual address 0x%jx not minidumped",
191 		    (uintmax_t)va);
192 		goto invalid;
193 	}
194 
195 invalid:
196 	_kvm_err(kd, 0, "invalid address (0x%jx)", (uintmax_t)va);
197 	return (0);
198 }
199 
200 static int
201 _aarch64_minidump_kvatop(kvm_t *kd, kvaddr_t va, off_t *pa)
202 {
203 
204 	if (ISALIVE(kd)) {
205 		_kvm_err(kd, 0,
206 		    "_aarch64_minidump_kvatop called in live kernel!");
207 		return (0);
208 	}
209 	return (_aarch64_minidump_vatop(kd, va, pa));
210 }
211 
212 static int
213 _aarch64_native(kvm_t *kd __unused)
214 {
215 
216 #ifdef __aarch64__
217 	return (1);
218 #else
219 	return (0);
220 #endif
221 }
222 
223 static vm_prot_t
224 _aarch64_entry_to_prot(aarch64_pte_t pte)
225 {
226 	vm_prot_t prot = VM_PROT_READ;
227 
228 	/* Source: arm64/arm64/pmap.c:pmap_protect() */
229 	if ((pte & AARCH64_ATTR_AP(AARCH64_ATTR_AP_RO)) == 0)
230 		prot |= VM_PROT_WRITE;
231 	if ((pte & AARCH64_ATTR_XN) == 0)
232 		prot |= VM_PROT_EXECUTE;
233 	return prot;
234 }
235 
236 static int
237 _aarch64_minidump_walk_pages(kvm_t *kd, kvm_walk_pages_cb_t *cb, void *arg)
238 {
239 	struct vmstate *vm = kd->vmst;
240 	u_long nptes = vm->hdr.pmapsize / sizeof(aarch64_pte_t);
241 	u_long bmindex, dva, pa, pteindex, va;
242 	struct kvm_bitmap bm;
243 	vm_prot_t prot;
244 	int ret = 0;
245 
246 	if (!_kvm_bitmap_init(&bm, vm->hdr.bitmapsize, &bmindex))
247 		return (0);
248 
249 	for (pteindex = 0; pteindex < nptes; pteindex++) {
250 		aarch64_pte_t pte = _aarch64_pte_get(kd, pteindex);
251 
252 		if ((pte & AARCH64_ATTR_DESCR_MASK) != AARCH64_L3_PAGE)
253 			continue;
254 
255 		va = vm->hdr.kernbase + (pteindex << AARCH64_L3_SHIFT);
256 		pa = pte & ~AARCH64_ATTR_MASK;
257 		dva = vm->hdr.dmapbase + pa;
258 		if (!_kvm_visit_cb(kd, cb, arg, pa, va, dva,
259 		    _aarch64_entry_to_prot(pte), AARCH64_PAGE_SIZE, 0)) {
260 			goto out;
261 		}
262 	}
263 
264 	while (_kvm_bitmap_next(&bm, &bmindex)) {
265 		pa = _kvm_bit_id_pa(kd, bmindex, AARCH64_PAGE_SIZE);
266 		if (pa == _KVM_PA_INVALID)
267 			break;
268 		dva = vm->hdr.dmapbase + pa;
269 		if (vm->hdr.dmapend < (dva + AARCH64_PAGE_SIZE))
270 			break;
271 		va = 0;
272 		prot = VM_PROT_READ | VM_PROT_WRITE;
273 		if (!_kvm_visit_cb(kd, cb, arg, pa, va, dva,
274 		    prot, AARCH64_PAGE_SIZE, 0)) {
275 			goto out;
276 		}
277 	}
278 	ret = 1;
279 
280 out:
281 	_kvm_bitmap_deinit(&bm);
282 	return (ret);
283 }
284 
285 static struct kvm_arch kvm_aarch64_minidump = {
286 	.ka_probe = _aarch64_minidump_probe,
287 	.ka_initvtop = _aarch64_minidump_initvtop,
288 	.ka_freevtop = _aarch64_minidump_freevtop,
289 	.ka_kvatop = _aarch64_minidump_kvatop,
290 	.ka_native = _aarch64_native,
291 	.ka_walk_pages = _aarch64_minidump_walk_pages,
292 };
293 
294 KVM_ARCH(kvm_aarch64_minidump);
295