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);
56 * Don't touch the first SIZEOF_METADATA bytes on the dump device. This
57 * is to protect us from metadata and to protect metadata from us.
59 #define SIZEOF_METADATA (64*1024)
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);
73 static int dump_retry_count = 5;
74 SYSCTL_INT(_machdep, OID_AUTO, dump_retry_count, CTLFLAG_RWTUN,
75 &dump_retry_count, 0, "Number of times dump has to retry before bailing out");
78 is_dumpable(vm_paddr_t pa)
83 if ((m = vm_phys_paddr_to_vm_page(pa)) != NULL)
84 return ((m->flags & PG_NODUMP) == 0);
85 for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
86 if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
92 #define PG2MB(pgs) (((pgs) + (1 << 8) - 1) >> 8)
95 blk_flush(struct dumperinfo *di)
102 error = dump_write(di, dump_va, 0, dumplo, fragsz);
112 } progress_track[10] = {
126 report_progress(size_t progress, size_t dumpsize)
130 sofar = 100 - ((progress * 100) / dumpsize);
131 for (i = 0; i < nitems(progress_track); i++) {
132 if (sofar < progress_track[i].min_per ||
133 sofar > progress_track[i].max_per)
135 if (progress_track[i].visited)
137 progress_track[i].visited = 1;
138 printf("..%d%%", sofar);
144 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
150 maxdumpsz = min(di->maxiosize, MAXDUMPPGS * PAGE_SIZE);
151 if (maxdumpsz == 0) /* seatbelt */
152 maxdumpsz = PAGE_SIZE;
154 if ((sz % PAGE_SIZE) != 0) {
155 printf("size not page aligned\n");
158 if (ptr != NULL && pa != 0) {
159 printf("cant have both va and pa!\n");
162 if ((((uintptr_t)pa) % PAGE_SIZE) != 0) {
163 printf("address not page aligned %p\n", ptr);
167 /* If we're doing a virtual dump, flush any pre-existing pa pages */
168 error = blk_flush(di);
173 len = maxdumpsz - fragsz;
179 report_progress(progress, dumpsize);
180 counter &= (1<<24) - 1;
183 wdog_kern_pat(WD_LASTVAL);
186 error = dump_write(di, ptr, 0, dumplo, len);
193 for (i = 0; i < len; i += PAGE_SIZE)
194 dump_va = pmap_kenter_temporary(pa + i, (i + fragsz) >> PAGE_SHIFT);
198 if (fragsz == maxdumpsz) {
199 error = blk_flush(di);
205 /* Check for user abort. */
210 printf(" (CTRL-C to abort) ");
216 /* A fake page table page, to avoid having to handle both 4K and 2M pages */
217 static pd_entry_t fakepd[NPDEPG];
220 minidumpsys(struct dumperinfo *di)
226 uint64_t *pml4, *pdp, *pd, *pt, pa;
227 int i, ii, j, k, n, bit;
229 struct minidumphdr mdhdr;
235 for (i = 0; i < nitems(progress_track); i++)
236 progress_track[i].visited = 0;
237 /* Walk page table pages, set bits in vm_page_dump */
239 for (va = VM_MIN_KERNEL_ADDRESS; va < MAX(KERNBASE + nkpt * NBPDR,
242 * We always write a page, even if it is zero. Each
243 * page written corresponds to 1GB of space
245 pmapsize += PAGE_SIZE;
246 ii = pmap_pml4e_index(va);
247 pml4 = (uint64_t *)PHYS_TO_DMAP(KPML4phys) + ii;
248 pdp = (uint64_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
249 i = pmap_pdpe_index(va);
250 if ((pdp[i] & PG_V) == 0) {
256 * 1GB page is represented as 512 2MB pages in a dump.
258 if ((pdp[i] & PG_PS) != 0) {
260 pa = pdp[i] & PG_PS_FRAME;
261 for (n = 0; n < NPDEPG * NPTEPG; n++) {
269 pd = (uint64_t *)PHYS_TO_DMAP(pdp[i] & PG_FRAME);
270 for (n = 0; n < NPDEPG; n++, va += NBPDR) {
271 j = pmap_pde_index(va);
273 if ((pd[j] & PG_V) == 0)
276 if ((pd[j] & PG_PS) != 0) {
277 /* This is an entire 2M page. */
278 pa = pd[j] & PG_PS_FRAME;
279 for (k = 0; k < NPTEPG; k++) {
287 pa = pd[j] & PG_FRAME;
288 /* set bit for this PTE page */
291 /* and for each valid page in this 2MB block */
292 pt = (uint64_t *)PHYS_TO_DMAP(pd[j] & PG_FRAME);
293 for (k = 0; k < NPTEPG; k++) {
294 if ((pt[k] & PG_V) == 0)
296 pa = pt[k] & PG_FRAME;
303 /* Calculate dump size. */
305 dumpsize += round_page(msgbufp->msg_size);
306 dumpsize += round_page(vm_page_dump_size);
307 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
308 bits = vm_page_dump[i];
311 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
312 /* Clear out undumpable pages now if needed */
313 if (is_dumpable(pa)) {
314 dumpsize += PAGE_SIZE;
318 bits &= ~(1ul << bit);
321 dumpsize += PAGE_SIZE;
323 /* Determine dump offset on device. */
324 if (di->mediasize < SIZEOF_METADATA + dumpsize + di->blocksize * 2 +
325 kerneldumpcrypto_dumpkeysize(di->kdc)) {
329 dumplo = di->mediaoffset + di->mediasize - dumpsize;
330 dumplo -= di->blocksize * 2;
331 dumplo -= kerneldumpcrypto_dumpkeysize(di->kdc);
334 /* Initialize kernel dump crypto. */
335 error = kerneldumpcrypto_init(di->kdc);
339 /* Initialize mdhdr */
340 bzero(&mdhdr, sizeof(mdhdr));
341 strcpy(mdhdr.magic, MINIDUMP_MAGIC);
342 mdhdr.version = MINIDUMP_VERSION;
343 mdhdr.msgbufsize = msgbufp->msg_size;
344 mdhdr.bitmapsize = vm_page_dump_size;
345 mdhdr.pmapsize = pmapsize;
346 mdhdr.kernbase = VM_MIN_KERNEL_ADDRESS;
347 mdhdr.dmapbase = DMAP_MIN_ADDRESS;
348 mdhdr.dmapend = DMAP_MAX_ADDRESS;
350 mkdumpheader(&kdh, KERNELDUMPMAGIC, KERNELDUMP_AMD64_VERSION, dumpsize,
351 kerneldumpcrypto_dumpkeysize(di->kdc), di->blocksize);
353 printf("Dumping %llu out of %ju MB:", (long long)dumpsize >> 20,
354 ptoa((uintmax_t)physmem) / 1048576);
357 error = dump_write_header(di, &kdh, 0, dumplo);
360 dumplo += di->blocksize;
363 error = dump_write_key(di, 0, dumplo);
366 dumplo += kerneldumpcrypto_dumpkeysize(di->kdc);
369 bzero(&fakepd, sizeof(fakepd));
370 bcopy(&mdhdr, &fakepd, sizeof(mdhdr));
371 error = blk_write(di, (char *)&fakepd, 0, PAGE_SIZE);
375 /* Dump msgbuf up front */
376 error = blk_write(di, (char *)msgbufp->msg_ptr, 0, round_page(msgbufp->msg_size));
381 error = blk_write(di, (char *)vm_page_dump, 0, round_page(vm_page_dump_size));
385 /* Dump kernel page directory pages */
386 bzero(fakepd, sizeof(fakepd));
387 for (va = VM_MIN_KERNEL_ADDRESS; va < MAX(KERNBASE + nkpt * NBPDR,
388 kernel_vm_end); va += NBPDP) {
389 ii = pmap_pml4e_index(va);
390 pml4 = (uint64_t *)PHYS_TO_DMAP(KPML4phys) + ii;
391 pdp = (uint64_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
392 i = pmap_pdpe_index(va);
394 /* We always write a page, even if it is zero */
395 if ((pdp[i] & PG_V) == 0) {
396 error = blk_write(di, (char *)&fakepd, 0, PAGE_SIZE);
399 /* flush, in case we reuse fakepd in the same block */
400 error = blk_flush(di);
406 /* 1GB page is represented as 512 2MB pages in a dump */
407 if ((pdp[i] & PG_PS) != 0) {
408 /* PDPE and PDP have identical layout in this case */
410 for (j = 1; j < NPDEPG; j++)
411 fakepd[j] = fakepd[j - 1] + NBPDR;
412 error = blk_write(di, (char *)&fakepd, 0, PAGE_SIZE);
415 /* flush, in case we reuse fakepd in the same block */
416 error = blk_flush(di);
419 bzero(fakepd, sizeof(fakepd));
423 pd = (uint64_t *)PHYS_TO_DMAP(pdp[i] & PG_FRAME);
424 error = blk_write(di, (char *)pd, 0, PAGE_SIZE);
427 error = blk_flush(di);
432 /* Dump memory chunks */
433 /* XXX cluster it up and use blk_dump() */
434 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
435 bits = vm_page_dump[i];
438 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
439 error = blk_write(di, 0, pa, PAGE_SIZE);
442 bits &= ~(1ul << bit);
446 error = blk_flush(di);
451 error = dump_write_header(di, &kdh, 0, dumplo);
454 dumplo += di->blocksize;
456 /* Signal completion, signoff and exit stage left. */
457 dump_write(di, NULL, 0, 0, 0);
458 printf("\nDump complete\n");
466 if (error == ENOSPC) {
467 printf("Dump map grown while dumping. ");
468 if (retry_count < dump_retry_count) {
469 printf("Retrying...\n");
472 printf("Dump failed.\n");
474 else if (error == ECANCELED)
475 printf("Dump aborted\n");
476 else if (error == E2BIG)
477 printf("Dump failed. Partition too small.\n");
479 printf("** DUMP FAILED (ERROR %d) **\n", error);
484 dump_add_page(vm_paddr_t pa)
489 idx = pa >> 6; /* 2^6 = 64 */
491 atomic_set_long(&vm_page_dump[idx], 1ul << bit);
495 dump_drop_page(vm_paddr_t pa)
500 idx = pa >> 6; /* 2^6 = 64 */
502 atomic_clear_long(&vm_page_dump[idx], 1ul << bit);