xref: /freebsd/lib/libkvm/kvm_minidump_aarch64.c (revision 1f4bcc459a76b7aa664f3fd557684cd0ba6da352)
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 <kvm.h>
41 
42 #include "../../sys/arm64/include/minidump.h"
43 
44 #include <limits.h>
45 
46 #include "kvm_private.h"
47 #include "kvm_aarch64.h"
48 
49 #define	aarch64_round_page(x)	roundup2((kvaddr_t)(x), AARCH64_PAGE_SIZE)
50 
51 struct vmstate {
52 	struct minidumphdr hdr;
53 	struct hpt hpt;
54 	uint64_t *page_map;
55 };
56 
57 static int
58 _aarch64_minidump_probe(kvm_t *kd)
59 {
60 
61 	return (_kvm_probe_elf_kernel(kd, ELFCLASS64, EM_AARCH64) &&
62 	    _kvm_is_minidump(kd));
63 }
64 
65 static void
66 _aarch64_minidump_freevtop(kvm_t *kd)
67 {
68 	struct vmstate *vm = kd->vmst;
69 
70 	_kvm_hpt_free(&vm->hpt);
71 	free(vm->page_map);
72 	free(vm);
73 	kd->vmst = NULL;
74 }
75 
76 static int
77 _aarch64_minidump_initvtop(kvm_t *kd)
78 {
79 	struct vmstate *vmst;
80 	uint64_t *bitmap;
81 	off_t off;
82 
83 	vmst = _kvm_malloc(kd, sizeof(*vmst));
84 	if (vmst == 0) {
85 		_kvm_err(kd, kd->program, "cannot allocate vm");
86 		return (-1);
87 	}
88 	kd->vmst = vmst;
89 	if (pread(kd->pmfd, &vmst->hdr, sizeof(vmst->hdr), 0) !=
90 	    sizeof(vmst->hdr)) {
91 		_kvm_err(kd, kd->program, "cannot read dump header");
92 		return (-1);
93 	}
94 	if (strncmp(MINIDUMP_MAGIC, vmst->hdr.magic,
95 	    sizeof(vmst->hdr.magic)) != 0) {
96 		_kvm_err(kd, kd->program, "not a minidump for this platform");
97 		return (-1);
98 	}
99 
100 	vmst->hdr.version = le32toh(vmst->hdr.version);
101 	if (vmst->hdr.version != MINIDUMP_VERSION) {
102 		_kvm_err(kd, kd->program, "wrong minidump version. "
103 		    "Expected %d got %d", MINIDUMP_VERSION, vmst->hdr.version);
104 		return (-1);
105 	}
106 	vmst->hdr.msgbufsize = le32toh(vmst->hdr.msgbufsize);
107 	vmst->hdr.bitmapsize = le32toh(vmst->hdr.bitmapsize);
108 	vmst->hdr.pmapsize = le32toh(vmst->hdr.pmapsize);
109 	vmst->hdr.kernbase = le64toh(vmst->hdr.kernbase);
110 	vmst->hdr.dmapphys = le64toh(vmst->hdr.dmapphys);
111 	vmst->hdr.dmapbase = le64toh(vmst->hdr.dmapbase);
112 	vmst->hdr.dmapend = le64toh(vmst->hdr.dmapend);
113 
114 	/* Skip header and msgbuf */
115 	off = AARCH64_PAGE_SIZE + aarch64_round_page(vmst->hdr.msgbufsize);
116 
117 	bitmap = _kvm_malloc(kd, vmst->hdr.bitmapsize);
118 	if (bitmap == NULL) {
119 		_kvm_err(kd, kd->program,
120 		    "cannot allocate %d bytes for bitmap",
121 		    vmst->hdr.bitmapsize);
122 		return (-1);
123 	}
124 	if (pread(kd->pmfd, bitmap, vmst->hdr.bitmapsize, off) !=
125 	    (ssize_t)vmst->hdr.bitmapsize) {
126 		_kvm_err(kd, kd->program,
127 		    "cannot read %d bytes for page bitmap",
128 		    vmst->hdr.bitmapsize);
129 		free(bitmap);
130 		return (-1);
131 	}
132 	off += aarch64_round_page(vmst->hdr.bitmapsize);
133 
134 	vmst->page_map = _kvm_malloc(kd, vmst->hdr.pmapsize);
135 	if (vmst->page_map == NULL) {
136 		_kvm_err(kd, kd->program,
137 		    "cannot allocate %d bytes for page_map",
138 		    vmst->hdr.pmapsize);
139 		free(bitmap);
140 		return (-1);
141 	}
142 	/* This is the end of the dump, savecore may have truncated it. */
143 	/*
144 	 * XXX: This doesn't make sense.  The pmap is not at the end,
145 	 * and if it is truncated we don't have any actual data (it's
146 	 * all stored after the bitmap and pmap.  -- jhb
147 	 */
148 	if (pread(kd->pmfd, vmst->page_map, vmst->hdr.pmapsize, off) <
149 	    AARCH64_PAGE_SIZE) {
150 		_kvm_err(kd, kd->program, "cannot read %d bytes for page_map",
151 		    vmst->hdr.pmapsize);
152 		free(bitmap);
153 		return (-1);
154 	}
155 	off += vmst->hdr.pmapsize;
156 
157 	/* build physical address hash table for sparse pages */
158 	_kvm_hpt_init(kd, &vmst->hpt, bitmap, vmst->hdr.bitmapsize, off,
159 	    AARCH64_PAGE_SIZE, sizeof(*bitmap));
160 	free(bitmap);
161 
162 	return (0);
163 }
164 
165 static int
166 _aarch64_minidump_vatop(kvm_t *kd, kvaddr_t va, off_t *pa)
167 {
168 	struct vmstate *vm;
169 	aarch64_physaddr_t offset;
170 	aarch64_pte_t l3;
171 	kvaddr_t l3_index;
172 	aarch64_physaddr_t a;
173 	off_t ofs;
174 
175 	vm = kd->vmst;
176 	offset = va & AARCH64_PAGE_MASK;
177 
178 	if (va >= vm->hdr.dmapbase && va < vm->hdr.dmapend) {
179 		a = (va - vm->hdr.dmapbase + vm->hdr.dmapphys) &
180 		    ~AARCH64_PAGE_MASK;
181 		ofs = _kvm_hpt_find(&vm->hpt, a);
182 		if (ofs == -1) {
183 			_kvm_err(kd, kd->program, "_aarch64_minidump_vatop: "
184 			    "direct map address 0x%jx not in minidump",
185 			    (uintmax_t)va);
186 			goto invalid;
187 		}
188 		*pa = ofs + offset;
189 		return (AARCH64_PAGE_SIZE - offset);
190 	} else if (va >= vm->hdr.kernbase) {
191 		l3_index = (va - vm->hdr.kernbase) >> AARCH64_L3_SHIFT;
192 		if (l3_index >= vm->hdr.pmapsize / sizeof(*vm->page_map))
193 			goto invalid;
194 		l3 = le64toh(vm->page_map[l3_index]);
195 		if ((l3 & AARCH64_ATTR_DESCR_MASK) != AARCH64_L3_PAGE) {
196 			_kvm_err(kd, kd->program,
197 			    "_aarch64_minidump_vatop: pde not valid");
198 			goto invalid;
199 		}
200 		a = l3 & ~AARCH64_ATTR_MASK;
201 		ofs = _kvm_hpt_find(&vm->hpt, a);
202 		if (ofs == -1) {
203 			_kvm_err(kd, kd->program, "_aarch64_minidump_vatop: "
204 			    "physical address 0x%jx not in minidump",
205 			    (uintmax_t)a);
206 			goto invalid;
207 		}
208 		*pa = ofs + offset;
209 		return (AARCH64_PAGE_SIZE - offset);
210 	} else {
211 		_kvm_err(kd, kd->program,
212 	    "_aarch64_minidump_vatop: virtual address 0x%jx not minidumped",
213 		    (uintmax_t)va);
214 		goto invalid;
215 	}
216 
217 invalid:
218 	_kvm_err(kd, 0, "invalid address (0x%jx)", (uintmax_t)va);
219 	return (0);
220 }
221 
222 static int
223 _aarch64_minidump_kvatop(kvm_t *kd, kvaddr_t va, off_t *pa)
224 {
225 
226 	if (ISALIVE(kd)) {
227 		_kvm_err(kd, 0,
228 		    "_aarch64_minidump_kvatop called in live kernel!");
229 		return (0);
230 	}
231 	return (_aarch64_minidump_vatop(kd, va, pa));
232 }
233 
234 static int
235 _aarch64_native(kvm_t *kd)
236 {
237 
238 #ifdef __aarch64__
239 	return (1);
240 #else
241 	return (0);
242 #endif
243 }
244 
245 struct kvm_arch kvm_aarch64_minidump = {
246 	.ka_probe = _aarch64_minidump_probe,
247 	.ka_initvtop = _aarch64_minidump_initvtop,
248 	.ka_freevtop = _aarch64_minidump_freevtop,
249 	.ka_kvatop = _aarch64_minidump_kvatop,
250 	.ka_native = _aarch64_native,
251 };
252 
253 KVM_ARCH(kvm_aarch64_minidump);
254