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