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/sysctl.h>
41 #include <sys/watchdog.h>
42 #include <sys/vmmeter.h>
44 #include <vm/vm_param.h>
45 #include <vm/vm_page.h>
46 #include <vm/vm_phys.h>
48 #include <machine/atomic.h>
49 #include <machine/elf.h>
50 #include <machine/md_var.h>
51 #include <machine/minidump.h>
53 CTASSERT(sizeof(struct kerneldumpheader) == 512);
55 uint64_t *vm_page_dump;
56 int vm_page_dump_size;
58 static struct kerneldumpheader kdh;
60 /* Handle chunked writes. */
63 static size_t counter, progress, dumpsize;
65 CTASSERT(sizeof(*vm_page_dump) == 8);
66 static int dump_retry_count = 5;
67 SYSCTL_INT(_machdep, OID_AUTO, dump_retry_count, CTLFLAG_RWTUN,
68 &dump_retry_count, 0, "Number of times dump has to retry before bailing out");
71 is_dumpable(vm_paddr_t pa)
76 if ((m = vm_phys_paddr_to_vm_page(pa)) != NULL)
77 return ((m->flags & PG_NODUMP) == 0);
78 for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
79 if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
85 #define PG2MB(pgs) (((pgs) + (1 << 8) - 1) >> 8)
88 blk_flush(struct dumperinfo *di)
95 error = dump_append(di, dump_va, 0, fragsz);
104 } progress_track[10] = {
118 report_progress(size_t progress, size_t dumpsize)
122 sofar = 100 - ((progress * 100) / dumpsize);
123 for (i = 0; i < nitems(progress_track); i++) {
124 if (sofar < progress_track[i].min_per ||
125 sofar > progress_track[i].max_per)
127 if (progress_track[i].visited)
129 progress_track[i].visited = 1;
130 printf("..%d%%", sofar);
136 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
142 maxdumpsz = min(di->maxiosize, MAXDUMPPGS * PAGE_SIZE);
143 if (maxdumpsz == 0) /* seatbelt */
144 maxdumpsz = PAGE_SIZE;
146 if ((sz % PAGE_SIZE) != 0) {
147 printf("size not page aligned\n");
150 if (ptr != NULL && pa != 0) {
151 printf("cant have both va and pa!\n");
154 if ((((uintptr_t)pa) % PAGE_SIZE) != 0) {
155 printf("address not page aligned %p\n", ptr);
159 /* If we're doing a virtual dump, flush any pre-existing pa pages */
160 error = blk_flush(di);
165 len = maxdumpsz - fragsz;
171 report_progress(progress, dumpsize);
172 counter &= (1<<24) - 1;
175 wdog_kern_pat(WD_LASTVAL);
178 error = dump_append(di, ptr, 0, len);
184 for (i = 0; i < len; i += PAGE_SIZE)
185 dump_va = pmap_kenter_temporary(pa + i, (i + fragsz) >> PAGE_SHIFT);
189 if (fragsz == maxdumpsz) {
190 error = blk_flush(di);
196 /* Check for user abort. */
201 printf(" (CTRL-C to abort) ");
207 /* A fake page table page, to avoid having to handle both 4K and 2M pages */
208 static pd_entry_t fakepd[NPDEPG];
211 minidumpsys(struct dumperinfo *di)
217 uint64_t *pml4, *pdp, *pd, *pt, pa;
218 int i, ii, j, k, n, bit;
220 struct minidumphdr mdhdr;
226 for (i = 0; i < nitems(progress_track); i++)
227 progress_track[i].visited = 0;
228 /* Walk page table pages, set bits in vm_page_dump */
230 for (va = VM_MIN_KERNEL_ADDRESS; va < MAX(KERNBASE + nkpt * NBPDR,
233 * We always write a page, even if it is zero. Each
234 * page written corresponds to 1GB of space
236 pmapsize += PAGE_SIZE;
237 ii = pmap_pml4e_index(va);
238 pml4 = (uint64_t *)PHYS_TO_DMAP(KPML4phys) + ii;
239 pdp = (uint64_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
240 i = pmap_pdpe_index(va);
241 if ((pdp[i] & PG_V) == 0) {
247 * 1GB page is represented as 512 2MB pages in a dump.
249 if ((pdp[i] & PG_PS) != 0) {
251 pa = pdp[i] & PG_PS_FRAME;
252 for (n = 0; n < NPDEPG * NPTEPG; n++) {
260 pd = (uint64_t *)PHYS_TO_DMAP(pdp[i] & PG_FRAME);
261 for (n = 0; n < NPDEPG; n++, va += NBPDR) {
262 j = pmap_pde_index(va);
264 if ((pd[j] & PG_V) == 0)
267 if ((pd[j] & PG_PS) != 0) {
268 /* This is an entire 2M page. */
269 pa = pd[j] & PG_PS_FRAME;
270 for (k = 0; k < NPTEPG; k++) {
278 pa = pd[j] & PG_FRAME;
279 /* set bit for this PTE page */
282 /* and for each valid page in this 2MB block */
283 pt = (uint64_t *)PHYS_TO_DMAP(pd[j] & PG_FRAME);
284 for (k = 0; k < NPTEPG; k++) {
285 if ((pt[k] & PG_V) == 0)
287 pa = pt[k] & PG_FRAME;
294 /* Calculate dump size. */
296 dumpsize += round_page(msgbufp->msg_size);
297 dumpsize += round_page(vm_page_dump_size);
298 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
299 bits = vm_page_dump[i];
302 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
303 /* Clear out undumpable pages now if needed */
304 if (is_dumpable(pa)) {
305 dumpsize += PAGE_SIZE;
309 bits &= ~(1ul << bit);
312 dumpsize += PAGE_SIZE;
316 /* Initialize mdhdr */
317 bzero(&mdhdr, sizeof(mdhdr));
318 strcpy(mdhdr.magic, MINIDUMP_MAGIC);
319 mdhdr.version = MINIDUMP_VERSION;
320 mdhdr.msgbufsize = msgbufp->msg_size;
321 mdhdr.bitmapsize = vm_page_dump_size;
322 mdhdr.pmapsize = pmapsize;
323 mdhdr.kernbase = VM_MIN_KERNEL_ADDRESS;
324 mdhdr.dmapbase = DMAP_MIN_ADDRESS;
325 mdhdr.dmapend = DMAP_MAX_ADDRESS;
327 dump_init_header(di, &kdh, KERNELDUMPMAGIC, KERNELDUMP_AMD64_VERSION,
330 printf("Dumping %llu out of %ju MB:", (long long)dumpsize >> 20,
331 ptoa((uintmax_t)physmem) / 1048576);
333 error = dump_start(di, &kdh);
338 bzero(&fakepd, sizeof(fakepd));
339 bcopy(&mdhdr, &fakepd, sizeof(mdhdr));
340 error = blk_write(di, (char *)&fakepd, 0, PAGE_SIZE);
344 /* Dump msgbuf up front */
345 error = blk_write(di, (char *)msgbufp->msg_ptr, 0, round_page(msgbufp->msg_size));
350 error = blk_write(di, (char *)vm_page_dump, 0, round_page(vm_page_dump_size));
354 /* Dump kernel page directory pages */
355 bzero(fakepd, sizeof(fakepd));
356 for (va = VM_MIN_KERNEL_ADDRESS; va < MAX(KERNBASE + nkpt * NBPDR,
357 kernel_vm_end); va += NBPDP) {
358 ii = pmap_pml4e_index(va);
359 pml4 = (uint64_t *)PHYS_TO_DMAP(KPML4phys) + ii;
360 pdp = (uint64_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
361 i = pmap_pdpe_index(va);
363 /* We always write a page, even if it is zero */
364 if ((pdp[i] & PG_V) == 0) {
365 error = blk_write(di, (char *)&fakepd, 0, PAGE_SIZE);
368 /* flush, in case we reuse fakepd in the same block */
369 error = blk_flush(di);
375 /* 1GB page is represented as 512 2MB pages in a dump */
376 if ((pdp[i] & PG_PS) != 0) {
377 /* PDPE and PDP have identical layout in this case */
379 for (j = 1; j < NPDEPG; j++)
380 fakepd[j] = fakepd[j - 1] + NBPDR;
381 error = blk_write(di, (char *)&fakepd, 0, PAGE_SIZE);
384 /* flush, in case we reuse fakepd in the same block */
385 error = blk_flush(di);
388 bzero(fakepd, sizeof(fakepd));
392 pd = (uint64_t *)PHYS_TO_DMAP(pdp[i] & PG_FRAME);
393 error = blk_write(di, (char *)pd, 0, PAGE_SIZE);
396 error = blk_flush(di);
401 /* Dump memory chunks */
402 /* XXX cluster it up and use blk_dump() */
403 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
404 bits = vm_page_dump[i];
407 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
408 error = blk_write(di, 0, pa, PAGE_SIZE);
411 bits &= ~(1ul << bit);
415 error = blk_flush(di);
419 error = dump_finish(di, &kdh);
423 printf("\nDump complete\n");
431 if (error == ENOSPC) {
432 printf("Dump map grown while dumping. ");
433 if (retry_count < dump_retry_count) {
434 printf("Retrying...\n");
437 printf("Dump failed.\n");
439 else if (error == ECANCELED)
440 printf("Dump aborted\n");
441 else if (error == E2BIG)
442 printf("Dump failed. Partition too small.\n");
444 printf("** DUMP FAILED (ERROR %d) **\n", error);
449 dump_add_page(vm_paddr_t pa)
454 idx = pa >> 6; /* 2^6 = 64 */
456 atomic_set_long(&vm_page_dump[idx], 1ul << bit);
460 dump_drop_page(vm_paddr_t pa)
465 idx = pa >> 6; /* 2^6 = 64 */
467 atomic_clear_long(&vm_page_dump[idx], 1ul << bit);