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