2 * Copyright (c) 2006 Peter Wemm
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 #include "opt_watchdog.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/kerneldump.h>
39 #include <sys/msgbuf.h>
40 #include <sys/watchdog.h>
42 #include <vm/vm_page.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>
50 CTASSERT(sizeof(struct kerneldumpheader) == 512);
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.
56 #define SIZEOF_METADATA (64*1024)
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))
61 uint64_t *vm_page_dump;
62 int vm_page_dump_size;
64 static struct kerneldumpheader kdh;
67 /* Handle chunked writes. */
70 static size_t counter, progress, dumpsize;
72 CTASSERT(sizeof(*vm_page_dump) == 8);
75 is_dumpable(vm_paddr_t pa)
80 if ((m = vm_phys_paddr_to_vm_page(pa)) != NULL)
81 return ((m->flags & PG_NODUMP) == 0);
82 for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
83 if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
89 #define PG2MB(pgs) (((pgs) + (1 << 8) - 1) >> 8)
92 blk_flush(struct dumperinfo *di)
99 error = dump_write(di, dump_va, 0, dumplo, fragsz);
109 } progress_track[10] = {
123 report_progress(size_t progress, size_t dumpsize)
127 sofar = 100 - ((progress * 100) / dumpsize);
128 for (i = 0; i < nitems(progress_track); i++) {
129 if (sofar < progress_track[i].min_per ||
130 sofar > progress_track[i].max_per)
132 if (progress_track[i].visited)
134 progress_track[i].visited = 1;
135 printf("..%d%%", sofar);
141 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
147 maxdumpsz = min(di->maxiosize, MAXDUMPPGS * PAGE_SIZE);
148 if (maxdumpsz == 0) /* seatbelt */
149 maxdumpsz = PAGE_SIZE;
151 if ((sz % PAGE_SIZE) != 0) {
152 printf("size not page aligned\n");
155 if (ptr != NULL && pa != 0) {
156 printf("cant have both va and pa!\n");
159 if ((((uintptr_t)pa) % PAGE_SIZE) != 0) {
160 printf("address not page aligned %p\n", ptr);
164 /* If we're doing a virtual dump, flush any pre-existing pa pages */
165 error = blk_flush(di);
170 len = maxdumpsz - fragsz;
176 report_progress(progress, dumpsize);
177 counter &= (1<<24) - 1;
180 wdog_kern_pat(WD_LASTVAL);
183 error = dump_write(di, ptr, 0, dumplo, len);
190 for (i = 0; i < len; i += PAGE_SIZE)
191 dump_va = pmap_kenter_temporary(pa + i, (i + fragsz) >> PAGE_SHIFT);
195 if (fragsz == maxdumpsz) {
196 error = blk_flush(di);
202 /* Check for user abort. */
207 printf(" (CTRL-C to abort) ");
213 /* A fake page table page, to avoid having to handle both 4K and 2M pages */
214 static pd_entry_t fakepd[NPDEPG];
217 minidumpsys(struct dumperinfo *di)
223 uint64_t *pdp, *pd, *pt, pa;
226 struct minidumphdr mdhdr;
232 for (i = 0; i < nitems(progress_track); i++)
233 progress_track[i].visited = 0;
234 /* Walk page table pages, set bits in vm_page_dump */
236 pdp = (uint64_t *)PHYS_TO_DMAP(KPDPphys);
237 for (va = VM_MIN_KERNEL_ADDRESS; va < MAX(KERNBASE + NKPT * NBPDR,
240 * We always write a page, even if it is zero. Each
241 * page written corresponds to 1GB of space
243 pmapsize += PAGE_SIZE;
244 i = (va >> PDPSHIFT) & ((1ul << NPDPEPGSHIFT) - 1);
245 if ((pdp[i] & PG_V) == 0) {
251 * 1GB page is represented as 512 2MB pages in a dump.
253 if ((pdp[i] & PG_PS) != 0) {
255 pa = pdp[i] & PG_PS_FRAME;
256 for (n = 0; n < NPDEPG * NPTEPG; n++) {
264 pd = (uint64_t *)PHYS_TO_DMAP(pdp[i] & PG_FRAME);
265 for (n = 0; n < NPDEPG; n++, va += NBPDR) {
266 j = (va >> PDRSHIFT) & ((1ul << NPDEPGSHIFT) - 1);
268 if ((pd[j] & PG_V) == 0)
271 if ((pd[j] & PG_PS) != 0) {
272 /* This is an entire 2M page. */
273 pa = pd[j] & PG_PS_FRAME;
274 for (k = 0; k < NPTEPG; k++) {
282 pa = pd[j] & PG_FRAME;
283 /* set bit for this PTE page */
286 /* and for each valid page in this 2MB block */
287 pt = (uint64_t *)PHYS_TO_DMAP(pd[j] & PG_FRAME);
288 for (k = 0; k < NPTEPG; k++) {
289 if ((pt[k] & PG_V) == 0)
291 pa = pt[k] & PG_FRAME;
298 /* Calculate dump size. */
300 dumpsize += round_page(msgbufp->msg_size);
301 dumpsize += round_page(vm_page_dump_size);
302 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
303 bits = vm_page_dump[i];
306 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
307 /* Clear out undumpable pages now if needed */
308 if (is_dumpable(pa)) {
309 dumpsize += PAGE_SIZE;
313 bits &= ~(1ul << bit);
316 dumpsize += PAGE_SIZE;
318 /* Determine dump offset on device. */
319 if (di->mediasize < SIZEOF_METADATA + dumpsize + sizeof(kdh) * 2) {
323 dumplo = di->mediaoffset + di->mediasize - dumpsize;
324 dumplo -= sizeof(kdh) * 2;
327 /* Initialize mdhdr */
328 bzero(&mdhdr, sizeof(mdhdr));
329 strcpy(mdhdr.magic, MINIDUMP_MAGIC);
330 mdhdr.version = MINIDUMP_VERSION;
331 mdhdr.msgbufsize = msgbufp->msg_size;
332 mdhdr.bitmapsize = vm_page_dump_size;
333 mdhdr.pmapsize = pmapsize;
334 mdhdr.kernbase = VM_MIN_KERNEL_ADDRESS;
335 mdhdr.dmapbase = DMAP_MIN_ADDRESS;
336 mdhdr.dmapend = DMAP_MAX_ADDRESS;
338 mkdumpheader(&kdh, KERNELDUMPMAGIC, KERNELDUMP_AMD64_VERSION, dumpsize, di->blocksize);
340 printf("Dumping %llu out of %ju MB:", (long long)dumpsize >> 20,
341 ptoa((uintmax_t)physmem) / 1048576);
344 error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
347 dumplo += sizeof(kdh);
350 bzero(&fakepd, sizeof(fakepd));
351 bcopy(&mdhdr, &fakepd, sizeof(mdhdr));
352 error = blk_write(di, (char *)&fakepd, 0, PAGE_SIZE);
356 /* Dump msgbuf up front */
357 error = blk_write(di, (char *)msgbufp->msg_ptr, 0, round_page(msgbufp->msg_size));
362 error = blk_write(di, (char *)vm_page_dump, 0, round_page(vm_page_dump_size));
366 /* Dump kernel page directory pages */
367 bzero(fakepd, sizeof(fakepd));
368 pdp = (uint64_t *)PHYS_TO_DMAP(KPDPphys);
369 for (va = VM_MIN_KERNEL_ADDRESS; va < MAX(KERNBASE + NKPT * NBPDR,
370 kernel_vm_end); va += NBPDP) {
371 i = (va >> PDPSHIFT) & ((1ul << NPDPEPGSHIFT) - 1);
373 /* We always write a page, even if it is zero */
374 if ((pdp[i] & PG_V) == 0) {
375 error = blk_write(di, (char *)&fakepd, 0, PAGE_SIZE);
378 /* flush, in case we reuse fakepd in the same block */
379 error = blk_flush(di);
385 /* 1GB page is represented as 512 2MB pages in a dump */
386 if ((pdp[i] & PG_PS) != 0) {
387 /* PDPE and PDP have identical layout in this case */
389 for (j = 1; j < NPDEPG; j++)
390 fakepd[j] = fakepd[j - 1] + NBPDR;
391 error = blk_write(di, (char *)&fakepd, 0, PAGE_SIZE);
394 /* flush, in case we reuse fakepd in the same block */
395 error = blk_flush(di);
398 bzero(fakepd, sizeof(fakepd));
402 pd = (uint64_t *)PHYS_TO_DMAP(pdp[i] & PG_FRAME);
403 error = blk_write(di, (char *)pd, 0, PAGE_SIZE);
406 error = blk_flush(di);
411 /* Dump memory chunks */
412 /* XXX cluster it up and use blk_dump() */
413 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
414 bits = vm_page_dump[i];
417 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
418 error = blk_write(di, 0, pa, PAGE_SIZE);
421 bits &= ~(1ul << bit);
425 error = blk_flush(di);
430 error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
433 dumplo += sizeof(kdh);
435 /* Signal completion, signoff and exit stage left. */
436 dump_write(di, NULL, 0, 0, 0);
437 printf("\nDump complete\n");
445 if (error == ENOSPC) {
446 printf("Dump map grown while dumping. ");
447 if (retry_count < 5) {
448 printf("Retrying...\n");
451 printf("Dump failed.\n");
453 else if (error == ECANCELED)
454 printf("Dump aborted\n");
455 else if (error == E2BIG)
456 printf("Dump failed. Partition too small.\n");
458 printf("** DUMP FAILED (ERROR %d) **\n", error);
462 dump_add_page(vm_paddr_t pa)
467 idx = pa >> 6; /* 2^6 = 64 */
469 atomic_set_long(&vm_page_dump[idx], 1ul << bit);
473 dump_drop_page(vm_paddr_t pa)
478 idx = pa >> 6; /* 2^6 = 64 */
480 atomic_clear_long(&vm_page_dump[idx], 1ul << bit);