xref: /NextBSD/sys/arm64/arm64/minidump_machdep.c (revision c21ffb8d6aca32c9584cfa072f309a5890a21aea)
1 /*-
2  * Copyright (c) 2006 Peter Wemm
3  * Copyright (c) 2015 The FreeBSD Foundation
4  * All rights reserved.
5  *
6  * This software was developed by Andrew Turner under
7  * sponsorship from the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "opt_watchdog.h"
36 
37 #include "opt_watchdog.h"
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/conf.h>
42 #include <sys/cons.h>
43 #include <sys/kernel.h>
44 #include <sys/kerneldump.h>
45 #include <sys/msgbuf.h>
46 #include <sys/watchdog.h>
47 
48 #include <vm/vm.h>
49 #include <vm/vm_param.h>
50 #include <vm/vm_page.h>
51 #include <vm/vm_phys.h>
52 #include <vm/pmap.h>
53 
54 #include <machine/md_var.h>
55 #include <machine/pmap.h>
56 #include <machine/pte.h>
57 #include <machine/vmparam.h>
58 #include <machine/minidump.h>
59 
60 CTASSERT(sizeof(struct kerneldumpheader) == 512);
61 
62 /*
63  * Don't touch the first SIZEOF_METADATA bytes on the dump device. This
64  * is to protect us from metadata and to protect metadata from us.
65  */
66 #define	SIZEOF_METADATA		(64*1024)
67 
68 uint64_t *vm_page_dump;
69 int vm_page_dump_size;
70 
71 static struct kerneldumpheader kdh;
72 static off_t dumplo;
73 
74 /* Handle chunked writes. */
75 static size_t fragsz;
76 static void *dump_va;
77 static size_t counter, progress, dumpsize;
78 
79 static uint64_t tmpbuffer[PAGE_SIZE / sizeof(uint64_t)];
80 
81 CTASSERT(sizeof(*vm_page_dump) == 8);
82 
83 static int
is_dumpable(vm_paddr_t pa)84 is_dumpable(vm_paddr_t pa)
85 {
86 	vm_page_t m;
87 	int i;
88 
89 	if ((m = vm_phys_paddr_to_vm_page(pa)) != NULL)
90 		return ((m->flags & PG_NODUMP) == 0);
91 	for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
92 		if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
93 			return (1);
94 	}
95 	return (0);
96 }
97 
98 static int
blk_flush(struct dumperinfo * di)99 blk_flush(struct dumperinfo *di)
100 {
101 	int error;
102 
103 	if (fragsz == 0)
104 		return (0);
105 
106 	error = dump_write(di, dump_va, 0, dumplo, fragsz);
107 	dumplo += fragsz;
108 	fragsz = 0;
109 	return (error);
110 }
111 
112 static struct {
113 	int min_per;
114 	int max_per;
115 	int visited;
116 } progress_track[10] = {
117 	{  0,  10, 0},
118 	{ 10,  20, 0},
119 	{ 20,  30, 0},
120 	{ 30,  40, 0},
121 	{ 40,  50, 0},
122 	{ 50,  60, 0},
123 	{ 60,  70, 0},
124 	{ 70,  80, 0},
125 	{ 80,  90, 0},
126 	{ 90, 100, 0}
127 };
128 
129 static void
report_progress(size_t progress,size_t dumpsize)130 report_progress(size_t progress, size_t dumpsize)
131 {
132 	int sofar, i;
133 
134 	sofar = 100 - ((progress * 100) / dumpsize);
135 	for (i = 0; i < nitems(progress_track); i++) {
136 		if (sofar < progress_track[i].min_per ||
137 		    sofar > progress_track[i].max_per)
138 			continue;
139 		if (progress_track[i].visited)
140 			return;
141 		progress_track[i].visited = 1;
142 		printf("..%d%%", sofar);
143 		return;
144 	}
145 }
146 
147 static int
blk_write(struct dumperinfo * di,char * ptr,vm_paddr_t pa,size_t sz)148 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
149 {
150 	size_t len;
151 	int error, c;
152 	u_int maxdumpsz;
153 
154 	maxdumpsz = min(di->maxiosize, MAXDUMPPGS * PAGE_SIZE);
155 	if (maxdumpsz == 0)	/* seatbelt */
156 		maxdumpsz = PAGE_SIZE;
157 	error = 0;
158 	if ((sz % PAGE_SIZE) != 0) {
159 		printf("size not page aligned\n");
160 		return (EINVAL);
161 	}
162 	if (ptr != NULL && pa != 0) {
163 		printf("cant have both va and pa!\n");
164 		return (EINVAL);
165 	}
166 	if ((((uintptr_t)pa) % PAGE_SIZE) != 0) {
167 		printf("address not page aligned %p\n", ptr);
168 		return (EINVAL);
169 	}
170 	if (ptr != NULL) {
171 		/*
172 		 * If we're doing a virtual dump, flush any
173 		 * pre-existing pa pages.
174 		 */
175 		error = blk_flush(di);
176 		if (error)
177 			return (error);
178 	}
179 	while (sz) {
180 		len = maxdumpsz - fragsz;
181 		if (len > sz)
182 			len = sz;
183 		counter += len;
184 		progress -= len;
185 		if (counter >> 22) {
186 			report_progress(progress, dumpsize);
187 			counter &= (1 << 22) - 1;
188 		}
189 
190 		wdog_kern_pat(WD_LASTVAL);
191 
192 		if (ptr) {
193 			error = dump_write(di, ptr, 0, dumplo, len);
194 			if (error)
195 				return (error);
196 			dumplo += len;
197 			ptr += len;
198 			sz -= len;
199 		} else {
200 			dump_va = (void *)PHYS_TO_DMAP(pa);
201 			fragsz += len;
202 			pa += len;
203 			sz -= len;
204 			error = blk_flush(di);
205 			if (error)
206 				return (error);
207 		}
208 
209 		/* Check for user abort. */
210 		c = cncheckc();
211 		if (c == 0x03)
212 			return (ECANCELED);
213 		if (c != -1)
214 			printf(" (CTRL-C to abort) ");
215 	}
216 
217 	return (0);
218 }
219 
220 int
minidumpsys(struct dumperinfo * di)221 minidumpsys(struct dumperinfo *di)
222 {
223 	pd_entry_t *l1, *l2;
224 	pt_entry_t *l3;
225 	uint32_t pmapsize;
226 	vm_offset_t va;
227 	vm_paddr_t pa;
228 	int error;
229 	uint64_t bits;
230 	int i, bit;
231 	int retry_count;
232 	struct minidumphdr mdhdr;
233 
234 	retry_count = 0;
235  retry:
236 	retry_count++;
237 	error = 0;
238 	pmapsize = 0;
239 	for (va = VM_MIN_KERNEL_ADDRESS; va < kernel_vm_end; va += L2_SIZE) {
240 		pmapsize += PAGE_SIZE;
241 		if (!pmap_get_tables(pmap_kernel(), va, &l1, &l2, &l3))
242 			continue;
243 
244 		/* We should always be using the l2 table for kvm */
245 		if (l2 == NULL)
246 			continue;
247 
248 		if ((*l2 & ATTR_DESCR_MASK) == L2_BLOCK) {
249 			pa = *l2 & ~ATTR_MASK;
250 			for (i = 0; i < Ln_ENTRIES; i++, pa += PAGE_SIZE) {
251 				if (is_dumpable(pa))
252 					dump_add_page(pa);
253 			}
254 		} else if ((*l2 & ATTR_DESCR_MASK) == L2_TABLE) {
255 			for (i = 0; i < Ln_ENTRIES; i++) {
256 				if ((l3[i] & ATTR_DESCR_MASK) != L3_PAGE)
257 					continue;
258 				pa = l3[i] & ~ATTR_MASK;
259 				if (is_dumpable(pa))
260 					dump_add_page(pa);
261 			}
262 		}
263 	}
264 
265 	/* Calculate dump size. */
266 	dumpsize = pmapsize;
267 	dumpsize += round_page(msgbufp->msg_size);
268 	dumpsize += round_page(vm_page_dump_size);
269 	for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
270 		bits = vm_page_dump[i];
271 		while (bits) {
272 			bit = ffsl(bits) - 1;
273 			pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) +
274 			    bit) * PAGE_SIZE;
275 			/* Clear out undumpable pages now if needed */
276 			if (is_dumpable(pa))
277 				dumpsize += PAGE_SIZE;
278 			else
279 				dump_drop_page(pa);
280 			bits &= ~(1ul << bit);
281 		}
282 	}
283 	dumpsize += PAGE_SIZE;
284 
285 	/* Determine dump offset on device. */
286 	if (di->mediasize < SIZEOF_METADATA + dumpsize + sizeof(kdh) * 2) {
287 		error = E2BIG;
288 		goto fail;
289 	}
290 	dumplo = di->mediaoffset + di->mediasize - dumpsize;
291 	dumplo -= sizeof(kdh) * 2;
292 	progress = dumpsize;
293 
294 	/* Initialize mdhdr */
295 	bzero(&mdhdr, sizeof(mdhdr));
296 	strcpy(mdhdr.magic, MINIDUMP_MAGIC);
297 	mdhdr.version = MINIDUMP_VERSION;
298 	mdhdr.msgbufsize = msgbufp->msg_size;
299 	mdhdr.bitmapsize = vm_page_dump_size;
300 	mdhdr.pmapsize = pmapsize;
301 	mdhdr.kernbase = VM_MIN_KERNEL_ADDRESS;
302 	mdhdr.dmapphys = DMAP_MIN_PHYSADDR;
303 	mdhdr.dmapbase = DMAP_MIN_ADDRESS;
304 	mdhdr.dmapend = DMAP_MAX_ADDRESS;
305 
306 	mkdumpheader(&kdh, KERNELDUMPMAGIC, KERNELDUMP_AARCH64_VERSION,
307 	    dumpsize, di->blocksize);
308 
309 	printf("Dumping %llu out of %ju MB:", (long long)dumpsize >> 20,
310 	    ptoa((uintmax_t)physmem) / 1048576);
311 
312 	/* Dump leader */
313 	error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
314 	if (error)
315 		goto fail;
316 	dumplo += sizeof(kdh);
317 
318 	/* Dump my header */
319 	bzero(&tmpbuffer, sizeof(tmpbuffer));
320 	bcopy(&mdhdr, &tmpbuffer, sizeof(mdhdr));
321 	error = blk_write(di, (char *)&tmpbuffer, 0, PAGE_SIZE);
322 	if (error)
323 		goto fail;
324 
325 	/* Dump msgbuf up front */
326 	error = blk_write(di, (char *)msgbufp->msg_ptr, 0,
327 	    round_page(msgbufp->msg_size));
328 	if (error)
329 		goto fail;
330 
331 	/* Dump bitmap */
332 	error = blk_write(di, (char *)vm_page_dump, 0,
333 	    round_page(vm_page_dump_size));
334 	if (error)
335 		goto fail;
336 
337 	/* Dump kernel page directory pages */
338 	bzero(&tmpbuffer, sizeof(tmpbuffer));
339 	for (va = VM_MIN_KERNEL_ADDRESS; va < kernel_vm_end; va += L2_SIZE) {
340 		if (!pmap_get_tables(pmap_kernel(), va, &l1, &l2, &l3)) {
341 			/* We always write a page, even if it is zero */
342 			error = blk_write(di, (char *)&tmpbuffer, 0, PAGE_SIZE);
343 			if (error)
344 				goto fail;
345 			/* flush, in case we reuse tmpbuffer in the same block*/
346 			error = blk_flush(di);
347 			if (error)
348 				goto fail;
349 		} else if (l2 == NULL) {
350 			pa = (*l1 & ~ATTR_MASK) | (va & L1_OFFSET);
351 
352 			/* Generate fake l3 entries based upon the l1 entry */
353 			for (i = 0; i < Ln_ENTRIES; i++) {
354 				tmpbuffer[i] = pa + (i * PAGE_SIZE) |
355 				    ATTR_DEFAULT | L3_PAGE;
356 			}
357 			/* We always write a page, even if it is zero */
358 			error = blk_write(di, (char *)&tmpbuffer, 0, PAGE_SIZE);
359 			if (error)
360 				goto fail;
361 			/* flush, in case we reuse tmpbuffer in the same block*/
362 			error = blk_flush(di);
363 			if (error)
364 				goto fail;
365 			bzero(&tmpbuffer, sizeof(tmpbuffer));
366 		} else if ((*l2 & ATTR_DESCR_MASK) == L2_BLOCK) {
367 			/* TODO: Handle an invalid L2 entry */
368 			pa = (*l2 & ~ATTR_MASK) | (va & L2_OFFSET);
369 
370 			/* Generate fake l3 entries based upon the l1 entry */
371 			for (i = 0; i < Ln_ENTRIES; i++) {
372 				tmpbuffer[i] = pa + (i * PAGE_SIZE) |
373 				    ATTR_DEFAULT | L3_PAGE;
374 			}
375 			/* We always write a page, even if it is zero */
376 			error = blk_write(di, (char *)&tmpbuffer, 0, PAGE_SIZE);
377 			if (error)
378 				goto fail;
379 			/* flush, in case we reuse fakepd in the same block */
380 			error = blk_flush(di);
381 			if (error)
382 				goto fail;
383 			bzero(&tmpbuffer, sizeof(tmpbuffer));
384 			continue;
385 		} else {
386 			pa = *l2 & ~ATTR_MASK;
387 
388 			/* We always write a page, even if it is zero */
389 			error = blk_write(di, NULL, pa, PAGE_SIZE);
390 			if (error)
391 				goto fail;
392 		}
393 	}
394 
395 	/* Dump memory chunks */
396 	/* XXX cluster it up and use blk_dump() */
397 	for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
398 		bits = vm_page_dump[i];
399 		while (bits) {
400 			bit = ffsl(bits) - 1;
401 			pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) +
402 			    bit) * PAGE_SIZE;
403 			error = blk_write(di, 0, pa, PAGE_SIZE);
404 			if (error)
405 				goto fail;
406 			bits &= ~(1ul << bit);
407 		}
408 	}
409 
410 	error = blk_flush(di);
411 	if (error)
412 		goto fail;
413 
414 	/* Dump trailer */
415 	error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
416 	if (error)
417 		goto fail;
418 	dumplo += sizeof(kdh);
419 
420 	/* Signal completion, signoff and exit stage left. */
421 	dump_write(di, NULL, 0, 0, 0);
422 	printf("\nDump complete\n");
423 	return (0);
424 
425  fail:
426 	if (error < 0)
427 		error = -error;
428 
429 	printf("\n");
430 	if (error == ENOSPC) {
431 		printf("Dump map grown while dumping. ");
432 		if (retry_count < 5) {
433 			printf("Retrying...\n");
434 			goto retry;
435 		}
436 		printf("Dump failed.\n");
437 	}
438 	else if (error == ECANCELED)
439 		printf("Dump aborted\n");
440 	else if (error == E2BIG)
441 		printf("Dump failed. Partition too small.\n");
442 	else
443 		printf("** DUMP FAILED (ERROR %d) **\n", error);
444 	return (error);
445 }
446 
447 void
dump_add_page(vm_paddr_t pa)448 dump_add_page(vm_paddr_t pa)
449 {
450 	int idx, bit;
451 
452 	pa >>= PAGE_SHIFT;
453 	idx = pa >> 6;		/* 2^6 = 64 */
454 	bit = pa & 63;
455 	atomic_set_long(&vm_page_dump[idx], 1ul << bit);
456 }
457 
458 void
dump_drop_page(vm_paddr_t pa)459 dump_drop_page(vm_paddr_t pa)
460 {
461 	int idx, bit;
462 
463 	pa >>= PAGE_SHIFT;
464 	idx = pa >> 6;		/* 2^6 = 64 */
465 	bit = pa & 63;
466 	atomic_clear_long(&vm_page_dump[idx], 1ul << bit);
467 }
468