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