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