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 <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/conf.h>
40 #include <sys/cons.h>
41 #include <sys/kernel.h>
42 #include <sys/kerneldump.h>
43 #include <sys/msgbuf.h>
44 #include <sys/watchdog.h>
45 #include <sys/vmmeter.h>
46 
47 #include <vm/vm.h>
48 #include <vm/vm_param.h>
49 #include <vm/vm_page.h>
50 #include <vm/vm_phys.h>
51 #include <vm/vm_dumpset.h>
52 #include <vm/pmap.h>
53 
54 #include <machine/md_var.h>
55 #include <machine/pte.h>
56 #include <machine/minidump.h>
57 
58 CTASSERT(sizeof(struct kerneldumpheader) == 512);
59 
60 static struct kerneldumpheader kdh;
61 
62 /* Handle chunked writes. */
63 static size_t fragsz;
64 static void *dump_va;
65 static size_t counter, progress, dumpsize;
66 
67 static uint64_t tmpbuffer[Ln_ENTRIES];
68 
69 static int
is_dumpable(vm_paddr_t pa)70 is_dumpable(vm_paddr_t pa)
71 {
72 	vm_page_t m;
73 	int i;
74 
75 	if ((m = vm_phys_paddr_to_vm_page(pa)) != NULL)
76 		return ((m->flags & PG_NODUMP) == 0);
77 	for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
78 		if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
79 			return (1);
80 	}
81 	return (0);
82 }
83 
84 static int
blk_flush(struct dumperinfo * di)85 blk_flush(struct dumperinfo *di)
86 {
87 	int error;
88 
89 	if (fragsz == 0)
90 		return (0);
91 
92 	error = dump_append(di, dump_va, 0, fragsz);
93 	fragsz = 0;
94 	return (error);
95 }
96 
97 static struct {
98 	int min_per;
99 	int max_per;
100 	int visited;
101 } progress_track[10] = {
102 	{  0,  10, 0},
103 	{ 10,  20, 0},
104 	{ 20,  30, 0},
105 	{ 30,  40, 0},
106 	{ 40,  50, 0},
107 	{ 50,  60, 0},
108 	{ 60,  70, 0},
109 	{ 70,  80, 0},
110 	{ 80,  90, 0},
111 	{ 90, 100, 0}
112 };
113 
114 static void
report_progress(size_t progress,size_t dumpsize)115 report_progress(size_t progress, size_t dumpsize)
116 {
117 	int sofar, i;
118 
119 	sofar = 100 - ((progress * 100) / dumpsize);
120 	for (i = 0; i < nitems(progress_track); i++) {
121 		if (sofar < progress_track[i].min_per ||
122 		    sofar > progress_track[i].max_per)
123 			continue;
124 		if (progress_track[i].visited)
125 			return;
126 		progress_track[i].visited = 1;
127 		printf("..%d%%", sofar);
128 		return;
129 	}
130 }
131 
132 static int
blk_write(struct dumperinfo * di,char * ptr,vm_paddr_t pa,size_t sz)133 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
134 {
135 	size_t len;
136 	int error, c;
137 	u_int maxdumpsz;
138 
139 	maxdumpsz = min(di->maxiosize, MAXDUMPPGS * PAGE_SIZE);
140 	if (maxdumpsz == 0)	/* seatbelt */
141 		maxdumpsz = PAGE_SIZE;
142 	error = 0;
143 	if ((sz % PAGE_SIZE) != 0) {
144 		printf("size not page aligned\n");
145 		return (EINVAL);
146 	}
147 	if (ptr != NULL && pa != 0) {
148 		printf("cant have both va and pa!\n");
149 		return (EINVAL);
150 	}
151 	if ((((uintptr_t)pa) % PAGE_SIZE) != 0) {
152 		printf("address not page aligned %p\n", ptr);
153 		return (EINVAL);
154 	}
155 	if (ptr != NULL) {
156 		/*
157 		 * If we're doing a virtual dump, flush any
158 		 * pre-existing pa pages.
159 		 */
160 		error = blk_flush(di);
161 		if (error)
162 			return (error);
163 	}
164 	while (sz) {
165 		len = maxdumpsz - fragsz;
166 		if (len > sz)
167 			len = sz;
168 		counter += len;
169 		progress -= len;
170 		if (counter >> 22) {
171 			report_progress(progress, dumpsize);
172 			counter &= (1 << 22) - 1;
173 		}
174 
175 		wdog_kern_pat(WD_LASTVAL);
176 
177 		if (ptr) {
178 			error = dump_append(di, ptr, 0, len);
179 			if (error)
180 				return (error);
181 			ptr += len;
182 			sz -= len;
183 		} else {
184 			dump_va = (void *)PHYS_TO_DMAP(pa);
185 			fragsz += len;
186 			pa += len;
187 			sz -= len;
188 			error = blk_flush(di);
189 			if (error)
190 				return (error);
191 		}
192 
193 		/* Check for user abort. */
194 		c = cncheckc();
195 		if (c == 0x03)
196 			return (ECANCELED);
197 		if (c != -1)
198 			printf(" (CTRL-C to abort) ");
199 	}
200 
201 	return (0);
202 }
203 
204 int
minidumpsys(struct dumperinfo * di)205 minidumpsys(struct dumperinfo *di)
206 {
207 	struct minidumphdr mdhdr;
208 	pd_entry_t *l0, *l1, *l2;
209 	pt_entry_t *l3;
210 	vm_offset_t va;
211 	vm_paddr_t pa;
212 	uint32_t pmapsize;
213 	int error, i, j, retry_count;
214 
215 	retry_count = 0;
216  retry:
217 	retry_count++;
218 	error = 0;
219 	pmapsize = 0;
220 	for (va = VM_MIN_KERNEL_ADDRESS; va < kernel_vm_end; va += L2_SIZE) {
221 		pmapsize += PAGE_SIZE;
222 		if (!pmap_get_tables(pmap_kernel(), va, &l0, &l1, &l2, &l3))
223 			continue;
224 
225 		if ((*l1 & ATTR_DESCR_MASK) == L1_BLOCK) {
226 			pa = *l1 & ~ATTR_MASK;
227 			for (i = 0; i < Ln_ENTRIES * Ln_ENTRIES;
228 			    i++, pa += PAGE_SIZE)
229 				if (is_dumpable(pa))
230 					dump_add_page(pa);
231 			pmapsize += (Ln_ENTRIES - 1) * PAGE_SIZE;
232 			va += L1_SIZE - L2_SIZE;
233 		} else if ((*l2 & ATTR_DESCR_MASK) == L2_BLOCK) {
234 			pa = *l2 & ~ATTR_MASK;
235 			for (i = 0; i < Ln_ENTRIES; i++, pa += PAGE_SIZE) {
236 				if (is_dumpable(pa))
237 					dump_add_page(pa);
238 			}
239 		} else if ((*l2 & ATTR_DESCR_MASK) == L2_TABLE) {
240 			for (i = 0; i < Ln_ENTRIES; i++) {
241 				if ((l3[i] & ATTR_DESCR_MASK) != L3_PAGE)
242 					continue;
243 				pa = l3[i] & ~ATTR_MASK;
244 				if (is_dumpable(pa))
245 					dump_add_page(pa);
246 			}
247 		}
248 	}
249 
250 	/* Calculate dump size. */
251 	dumpsize = pmapsize;
252 	dumpsize += round_page(msgbufp->msg_size);
253 	dumpsize += round_page(sizeof(dump_avail));
254 	dumpsize += round_page(BITSET_SIZE(vm_page_dump_pages));
255 	VM_PAGE_DUMP_FOREACH(pa) {
256 		if (is_dumpable(pa))
257 			dumpsize += PAGE_SIZE;
258 		else
259 			dump_drop_page(pa);
260 	}
261 	dumpsize += PAGE_SIZE;
262 
263 	progress = dumpsize;
264 
265 	/* Initialize mdhdr */
266 	bzero(&mdhdr, sizeof(mdhdr));
267 	strcpy(mdhdr.magic, MINIDUMP_MAGIC);
268 	mdhdr.version = MINIDUMP_VERSION;
269 	mdhdr.msgbufsize = msgbufp->msg_size;
270 	mdhdr.bitmapsize = round_page(BITSET_SIZE(vm_page_dump_pages));
271 	mdhdr.pmapsize = pmapsize;
272 	mdhdr.kernbase = VM_MIN_KERNEL_ADDRESS;
273 	mdhdr.dmapphys = DMAP_MIN_PHYSADDR;
274 	mdhdr.dmapbase = DMAP_MIN_ADDRESS;
275 	mdhdr.dmapend = DMAP_MAX_ADDRESS;
276 	mdhdr.dumpavailsize = round_page(sizeof(dump_avail));
277 
278 	dump_init_header(di, &kdh, KERNELDUMPMAGIC, KERNELDUMP_AARCH64_VERSION,
279 	    dumpsize);
280 
281 	error = dump_start(di, &kdh);
282 	if (error != 0)
283 		goto fail;
284 
285 	printf("Dumping %llu out of %ju MB:", (long long)dumpsize >> 20,
286 	    ptoa((uintmax_t)physmem) / 1048576);
287 
288 	/* Dump my header */
289 	bzero(&tmpbuffer, sizeof(tmpbuffer));
290 	bcopy(&mdhdr, &tmpbuffer, sizeof(mdhdr));
291 	error = blk_write(di, (char *)&tmpbuffer, 0, PAGE_SIZE);
292 	if (error)
293 		goto fail;
294 
295 	/* Dump msgbuf up front */
296 	error = blk_write(di, (char *)msgbufp->msg_ptr, 0,
297 	    round_page(msgbufp->msg_size));
298 	if (error)
299 		goto fail;
300 
301 	/* Dump dump_avail */
302 	_Static_assert(sizeof(dump_avail) <= sizeof(tmpbuffer),
303 	    "Large dump_avail not handled");
304 	bzero(tmpbuffer, sizeof(tmpbuffer));
305 	memcpy(tmpbuffer, dump_avail, sizeof(dump_avail));
306 	error = blk_write(di, (char *)&tmpbuffer, 0, PAGE_SIZE);
307 	if (error)
308 		goto fail;
309 
310 	/* Dump bitmap */
311 	error = blk_write(di, (char *)vm_page_dump, 0,
312 	    round_page(BITSET_SIZE(vm_page_dump_pages)));
313 	if (error)
314 		goto fail;
315 
316 	/* Dump kernel page directory pages */
317 	bzero(&tmpbuffer, sizeof(tmpbuffer));
318 	for (va = VM_MIN_KERNEL_ADDRESS; va < kernel_vm_end; va += L2_SIZE) {
319 		if (!pmap_get_tables(pmap_kernel(), va, &l0, &l1, &l2, &l3)) {
320 			/* We always write a page, even if it is zero */
321 			error = blk_write(di, (char *)&tmpbuffer, 0, PAGE_SIZE);
322 			if (error)
323 				goto fail;
324 			/* flush, in case we reuse tmpbuffer in the same block*/
325 			error = blk_flush(di);
326 			if (error)
327 				goto fail;
328 		} else if ((*l1 & ATTR_DESCR_MASK) == L1_BLOCK) {
329 			/*
330 			 * Handle a 1GB block mapping: write out 512 fake L2
331 			 * pages.
332 			 */
333 			pa = (*l1 & ~ATTR_MASK) | (va & L1_OFFSET);
334 
335 			for (i = 0; i < Ln_ENTRIES; i++) {
336 				for (j = 0; j < Ln_ENTRIES; j++) {
337 					tmpbuffer[j] = pa + i * L2_SIZE +
338 					    j * PAGE_SIZE | ATTR_DEFAULT |
339 					    L3_PAGE;
340 				}
341 				error = blk_write(di, (char *)&tmpbuffer, 0,
342 				    PAGE_SIZE);
343 				if (error)
344 					goto fail;
345 			}
346 			/* flush, in case we reuse tmpbuffer in the same block*/
347 			error = blk_flush(di);
348 			if (error)
349 				goto fail;
350 			bzero(&tmpbuffer, sizeof(tmpbuffer));
351 			va += L1_SIZE - L2_SIZE;
352 		} else if ((*l2 & ATTR_DESCR_MASK) == L2_BLOCK) {
353 			pa = (*l2 & ~ATTR_MASK) | (va & L2_OFFSET);
354 
355 			/* Generate fake l3 entries based upon the l1 entry */
356 			for (i = 0; i < Ln_ENTRIES; i++) {
357 				tmpbuffer[i] = pa + (i * PAGE_SIZE) |
358 				    ATTR_DEFAULT | L3_PAGE;
359 			}
360 			error = blk_write(di, (char *)&tmpbuffer, 0, PAGE_SIZE);
361 			if (error)
362 				goto fail;
363 			/* flush, in case we reuse fakepd in the same block */
364 			error = blk_flush(di);
365 			if (error)
366 				goto fail;
367 			bzero(&tmpbuffer, sizeof(tmpbuffer));
368 			continue;
369 		} else {
370 			pa = *l2 & ~ATTR_MASK;
371 
372 			error = blk_write(di, NULL, pa, PAGE_SIZE);
373 			if (error)
374 				goto fail;
375 		}
376 	}
377 
378 	/* Dump memory chunks */
379 	VM_PAGE_DUMP_FOREACH(pa) {
380 		error = blk_write(di, 0, pa, PAGE_SIZE);
381 		if (error)
382 			goto fail;
383 	}
384 
385 	error = blk_flush(di);
386 	if (error)
387 		goto fail;
388 
389 	error = dump_finish(di, &kdh);
390 	if (error != 0)
391 		goto fail;
392 
393 	printf("\nDump complete\n");
394 	return (0);
395 
396 fail:
397 	if (error < 0)
398 		error = -error;
399 
400 	printf("\n");
401 	if (error == ENOSPC) {
402 		printf("Dump map grown while dumping. ");
403 		if (retry_count < 5) {
404 			printf("Retrying...\n");
405 			goto retry;
406 		}
407 		printf("Dump failed.\n");
408 	}
409 	else if (error == ECANCELED)
410 		printf("Dump aborted\n");
411 	else if (error == E2BIG) {
412 		printf("Dump failed. Partition too small (about %lluMB were "
413 		    "needed this time).\n", (long long)dumpsize >> 20);
414 	} else
415 		printf("** DUMP FAILED (ERROR %d) **\n", error);
416 	return (error);
417 }
418