xref: /freebsd/lib/libkvm/kvm_minidump_aarch64.c (revision 39ee7a7a6bdd1557b1c3532abf60d139798ac88b)
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 <sys/user.h>
37 #include <sys/proc.h>
38 #include <sys/stat.h>
39 #include <sys/mman.h>
40 #include <sys/fnv_hash.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include <nlist.h>
45 #include <kvm.h>
46 
47 #include <vm/vm.h>
48 #include <vm/vm_param.h>
49 
50 #include <machine/elf.h>
51 #include <machine/cpufunc.h>
52 #include <machine/pte.h>
53 #include <machine/minidump.h>
54 
55 #include <limits.h>
56 #include <stdint.h>
57 
58 #include "kvm_private.h"
59 
60 struct hpte {
61 	struct hpte *next;
62 	vm_paddr_t pa;
63 	int64_t off;
64 };
65 
66 #define HPT_SIZE 1024
67 
68 /* minidump must be the first item! */
69 struct vmstate {
70 	int minidump;		/* 1 = minidump mode */
71 	struct minidumphdr hdr;
72 	void *hpt_head[HPT_SIZE];
73 	uint64_t *bitmap;
74 	uint64_t *page_map;
75 };
76 
77 static void
78 hpt_insert(kvm_t *kd, vm_paddr_t pa, int64_t off)
79 {
80 	struct hpte *hpte;
81 	uint32_t fnv = FNV1_32_INIT;
82 
83 	fnv = fnv_32_buf(&pa, sizeof(pa), fnv);
84 	fnv &= (HPT_SIZE - 1);
85 	hpte = malloc(sizeof(*hpte));
86 	hpte->pa = pa;
87 	hpte->off = off;
88 	hpte->next = kd->vmst->hpt_head[fnv];
89 	kd->vmst->hpt_head[fnv] = hpte;
90 }
91 
92 static int64_t
93 hpt_find(kvm_t *kd, vm_paddr_t pa)
94 {
95 	struct hpte *hpte;
96 	uint32_t fnv = FNV1_32_INIT;
97 
98 	fnv = fnv_32_buf(&pa, sizeof(pa), fnv);
99 	fnv &= (HPT_SIZE - 1);
100 	for (hpte = kd->vmst->hpt_head[fnv]; hpte != NULL; hpte = hpte->next) {
101 		if (pa == hpte->pa)
102 			return (hpte->off);
103 	}
104 	return (-1);
105 }
106 
107 static int
108 inithash(kvm_t *kd, uint64_t *base, int len, off_t off)
109 {
110 	uint64_t idx;
111 	uint64_t bit, bits;
112 	vm_paddr_t pa;
113 
114 	for (idx = 0; idx < len / sizeof(*base); idx++) {
115 		bits = base[idx];
116 		while (bits) {
117 			bit = ffsl(bits) - 1;
118 			bits &= ~(1ul << bit);
119 			pa = (idx * sizeof(*base) * NBBY + bit) * PAGE_SIZE;
120 			hpt_insert(kd, pa, off);
121 			off += PAGE_SIZE;
122 		}
123 	}
124 	return (off);
125 }
126 
127 void
128 _kvm_minidump_freevtop(kvm_t *kd)
129 {
130 	struct vmstate *vm = kd->vmst;
131 
132 	free(vm->bitmap);
133 	free(vm->page_map);
134 	free(vm);
135 	kd->vmst = NULL;
136 }
137 
138 int
139 _kvm_minidump_initvtop(kvm_t *kd)
140 {
141 	struct vmstate *vmst;
142 	off_t off;
143 
144 	vmst = _kvm_malloc(kd, sizeof(*vmst));
145 	if (vmst == 0) {
146 		_kvm_err(kd, kd->program, "cannot allocate vm");
147 		return (-1);
148 	}
149 	kd->vmst = vmst;
150 	vmst->minidump = 1;
151 	if (pread(kd->pmfd, &vmst->hdr, sizeof(vmst->hdr), 0) !=
152 	    sizeof(vmst->hdr)) {
153 		_kvm_err(kd, kd->program, "cannot read dump header");
154 		return (-1);
155 	}
156 	if (strncmp(MINIDUMP_MAGIC, vmst->hdr.magic,
157 	    sizeof(vmst->hdr.magic)) != 0) {
158 		_kvm_err(kd, kd->program, "not a minidump for this platform");
159 		return (-1);
160 	}
161 
162 	if (vmst->hdr.version != MINIDUMP_VERSION && vmst->hdr.version != 1) {
163 		_kvm_err(kd, kd->program, "wrong minidump version. "
164 		    "Expected %d got %d", MINIDUMP_VERSION, vmst->hdr.version);
165 		return (-1);
166 	}
167 
168 	/* Skip header and msgbuf */
169 	off = PAGE_SIZE + round_page(vmst->hdr.msgbufsize);
170 
171 	vmst->bitmap = _kvm_malloc(kd, vmst->hdr.bitmapsize);
172 	if (vmst->bitmap == NULL) {
173 		_kvm_err(kd, kd->program,
174 		    "cannot allocate %d bytes for bitmap",
175 		    vmst->hdr.bitmapsize);
176 		return (-1);
177 	}
178 	if (pread(kd->pmfd, vmst->bitmap, vmst->hdr.bitmapsize, off) !=
179 	    vmst->hdr.bitmapsize) {
180 		_kvm_err(kd, kd->program,
181 		    "cannot read %d bytes for page bitmap",
182 		    vmst->hdr.bitmapsize);
183 		return (-1);
184 	}
185 	off += round_page(vmst->hdr.bitmapsize);
186 
187 	vmst->page_map = _kvm_malloc(kd, vmst->hdr.pmapsize);
188 	if (vmst->page_map == NULL) {
189 		_kvm_err(kd, kd->program,
190 		    "cannot allocate %d bytes for page_map",
191 		    vmst->hdr.pmapsize);
192 		return (-1);
193 	}
194 	/* This is the end of the dump, savecore may have truncated it. */
195 	if (pread(kd->pmfd, vmst->page_map, vmst->hdr.pmapsize, off) <
196 	    PAGE_SIZE) {
197 		_kvm_err(kd, kd->program, "cannot read %d bytes for page_map",
198 		    vmst->hdr.pmapsize);
199 	}
200 	off += vmst->hdr.pmapsize;
201 
202 	/* build physical address hash table for sparse pages */
203 	inithash(kd, vmst->bitmap, vmst->hdr.bitmapsize, off);
204 
205 	return (0);
206 }
207 
208 static int
209 _kvm_minidump_vatop(kvm_t *kd, u_long va, off_t *pa)
210 {
211 	struct vmstate *vm;
212 	u_long offset;
213 	pt_entry_t l3;
214 	u_long l3_index;
215 	u_long a;
216 	off_t ofs;
217 
218 	vm = kd->vmst;
219 	offset = va & PAGE_MASK;
220 
221 	if (va >= vm->hdr.dmapbase && va < vm->hdr.dmapend) {
222 		a = (va - vm->hdr.dmapbase + vm->hdr.dmapphys) & ~PAGE_MASK;
223 		ofs = hpt_find(kd, a);
224 		if (ofs == -1) {
225 			_kvm_err(kd, kd->program, "_kvm_vatop: "
226 			    "direct map address 0x%lx not in minidump", va);
227 			goto invalid;
228 		}
229 		*pa = ofs + offset;
230 		return (PAGE_SIZE - offset);
231 	} else if (va >= vm->hdr.kernbase) {
232 		l3_index = (va - vm->hdr.kernbase) >> L3_SHIFT;
233 		if (l3_index >= vm->hdr.pmapsize / sizeof(*vm->page_map))
234 			goto invalid;
235 		l3 = vm->page_map[l3_index];
236 		if ((l3 & ATTR_DESCR_MASK) != L3_PAGE) {
237 			_kvm_err(kd, kd->program, "_kvm_vatop: pde not valid");
238 			goto invalid;
239 		}
240 		a = l3 & ~ATTR_MASK;
241 		ofs = hpt_find(kd, a);
242 		if (ofs == -1) {
243 			_kvm_err(kd, kd->program, "_kvm_vatop: "
244 			    "physical address 0x%lx not in minidump", a);
245 			goto invalid;
246 		}
247 		*pa = ofs + offset;
248 		return (PAGE_SIZE - offset);
249 	} else {
250 		_kvm_err(kd, kd->program,
251 		    "_kvm_vatop: virtual address 0x%lx not minidumped", va);
252 		goto invalid;
253 	}
254 
255 invalid:
256 	_kvm_err(kd, 0, "invalid address (0x%lx)", va);
257 	return (0);
258 }
259 
260 int
261 _kvm_minidump_kvatop(kvm_t *kd, u_long va, off_t *pa)
262 {
263 
264 	if (ISALIVE(kd)) {
265 		_kvm_err(kd, 0, "kvm_kvatop called in live kernel!");
266 		return (0);
267 	}
268 	return (_kvm_minidump_vatop(kd, va, pa));
269 }
270