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_param.h>
43 #include <vm/vm_page.h>
44 #include <vm/vm_phys.h>
46 #include <machine/atomic.h>
47 #include <machine/elf.h>
48 #include <machine/md_var.h>
49 #include <machine/minidump.h>
51 CTASSERT(sizeof(struct kerneldumpheader) == 512);
54 * Don't touch the first SIZEOF_METADATA bytes on the dump device. This
55 * is to protect us from metadata and to protect metadata from us.
57 #define SIZEOF_METADATA (64*1024)
59 uint64_t *vm_page_dump;
60 int vm_page_dump_size;
62 static struct kerneldumpheader kdh;
65 /* Handle chunked writes. */
68 static size_t counter, progress, dumpsize;
70 CTASSERT(sizeof(*vm_page_dump) == 8);
73 is_dumpable(vm_paddr_t pa)
78 if ((m = vm_phys_paddr_to_vm_page(pa)) != NULL)
79 return ((m->flags & PG_NODUMP) == 0);
80 for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
81 if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
87 #define PG2MB(pgs) (((pgs) + (1 << 8) - 1) >> 8)
90 blk_flush(struct dumperinfo *di)
97 error = dump_write(di, dump_va, 0, dumplo, fragsz);
107 } progress_track[10] = {
121 report_progress(size_t progress, size_t dumpsize)
125 sofar = 100 - ((progress * 100) / dumpsize);
126 for (i = 0; i < nitems(progress_track); i++) {
127 if (sofar < progress_track[i].min_per ||
128 sofar > progress_track[i].max_per)
130 if (progress_track[i].visited)
132 progress_track[i].visited = 1;
133 printf("..%d%%", sofar);
139 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
145 maxdumpsz = min(di->maxiosize, MAXDUMPPGS * PAGE_SIZE);
146 if (maxdumpsz == 0) /* seatbelt */
147 maxdumpsz = PAGE_SIZE;
149 if ((sz % PAGE_SIZE) != 0) {
150 printf("size not page aligned\n");
153 if (ptr != NULL && pa != 0) {
154 printf("cant have both va and pa!\n");
157 if ((((uintptr_t)pa) % PAGE_SIZE) != 0) {
158 printf("address not page aligned %p\n", ptr);
162 /* If we're doing a virtual dump, flush any pre-existing pa pages */
163 error = blk_flush(di);
168 len = maxdumpsz - fragsz;
174 report_progress(progress, dumpsize);
175 counter &= (1<<24) - 1;
178 wdog_kern_pat(WD_LASTVAL);
181 error = dump_write(di, ptr, 0, dumplo, len);
188 for (i = 0; i < len; i += PAGE_SIZE)
189 dump_va = pmap_kenter_temporary(pa + i, (i + fragsz) >> PAGE_SHIFT);
193 if (fragsz == maxdumpsz) {
194 error = blk_flush(di);
200 /* Check for user abort. */
205 printf(" (CTRL-C to abort) ");
211 /* A fake page table page, to avoid having to handle both 4K and 2M pages */
212 static pd_entry_t fakepd[NPDEPG];
215 minidumpsys(struct dumperinfo *di)
221 uint64_t *pml4, *pdp, *pd, *pt, pa;
223 int i, ii, j, k, n, bit;
225 struct minidumphdr mdhdr;
231 for (i = 0; i < nitems(progress_track); i++)
232 progress_track[i].visited = 0;
233 /* Walk page table pages, set bits in vm_page_dump */
235 for (va = VM_MIN_KERNEL_ADDRESS; va < MAX(KERNBASE + nkpt * NBPDR,
238 * We always write a page, even if it is zero. Each
239 * page written corresponds to 1GB of space
241 pmapsize += PAGE_SIZE;
242 ii = pmap_pml4e_index(va);
243 pml4 = (uint64_t *)PHYS_TO_DMAP(KPML4phys) + ii;
244 pdp = (uint64_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
245 i = pmap_pdpe_index(va);
246 if ((pdp[i] & PG_V) == 0) {
252 * 1GB page is represented as 512 2MB pages in a dump.
254 if ((pdp[i] & PG_PS) != 0) {
256 pa = pdp[i] & PG_PS_FRAME;
257 for (n = 0; n < NPDEPG * NPTEPG; n++) {
265 pd = (uint64_t *)PHYS_TO_DMAP(pdp[i] & PG_FRAME);
266 for (n = 0; n < NPDEPG; n++, va += NBPDR) {
267 j = pmap_pde_index(va);
269 if ((pd[j] & PG_V) == 0)
272 if ((pd[j] & PG_PS) != 0) {
273 /* This is an entire 2M page. */
274 pa = pd[j] & PG_PS_FRAME;
275 for (k = 0; k < NPTEPG; k++) {
283 pa = pd[j] & PG_FRAME;
284 /* set bit for this PTE page */
287 /* and for each valid page in this 2MB block */
288 pt = (uint64_t *)PHYS_TO_DMAP(pd[j] & PG_FRAME);
289 for (k = 0; k < NPTEPG; k++) {
290 if ((pt[k] & PG_V) == 0)
292 pa = pt[k] & PG_FRAME;
299 /* Calculate dump size. */
301 dumpsize += round_page(msgbufp->msg_size);
302 dumpsize += round_page(vm_page_dump_size);
303 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
304 bits = vm_page_dump[i];
307 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
308 /* Clear out undumpable pages now if needed */
309 if (is_dumpable(pa)) {
310 dumpsize += PAGE_SIZE;
314 bits &= ~(1ul << bit);
317 dumpsize += PAGE_SIZE;
319 /* Determine dump offset on device. */
320 if (di->mediasize < SIZEOF_METADATA + dumpsize + di->blocksize * 2) {
324 dumplo = di->mediaoffset + di->mediasize - dumpsize;
325 dumplo -= di->blocksize * 2;
328 /* Initialize mdhdr */
329 bzero(&mdhdr, sizeof(mdhdr));
330 strcpy(mdhdr.magic, MINIDUMP_MAGIC);
331 mdhdr.version = MINIDUMP_VERSION;
332 mdhdr.msgbufsize = msgbufp->msg_size;
333 mdhdr.bitmapsize = vm_page_dump_size;
334 mdhdr.pmapsize = pmapsize;
335 mdhdr.kernbase = VM_MIN_KERNEL_ADDRESS;
336 mdhdr.dmapbase = DMAP_MIN_ADDRESS;
337 mdhdr.dmapend = DMAP_MAX_ADDRESS;
339 mkdumpheader(&kdh, KERNELDUMPMAGIC, KERNELDUMP_AMD64_VERSION, dumpsize, di->blocksize);
341 printf("Dumping %llu out of %ju MB:", (long long)dumpsize >> 20,
342 ptoa((uintmax_t)physmem) / 1048576);
345 error = dump_write_pad(di, &kdh, 0, dumplo, sizeof(kdh), &size);
351 bzero(&fakepd, sizeof(fakepd));
352 bcopy(&mdhdr, &fakepd, sizeof(mdhdr));
353 error = blk_write(di, (char *)&fakepd, 0, PAGE_SIZE);
357 /* Dump msgbuf up front */
358 error = blk_write(di, (char *)msgbufp->msg_ptr, 0, round_page(msgbufp->msg_size));
363 error = blk_write(di, (char *)vm_page_dump, 0, round_page(vm_page_dump_size));
367 /* Dump kernel page directory pages */
368 bzero(fakepd, sizeof(fakepd));
369 for (va = VM_MIN_KERNEL_ADDRESS; va < MAX(KERNBASE + nkpt * NBPDR,
370 kernel_vm_end); va += NBPDP) {
371 ii = pmap_pml4e_index(va);
372 pml4 = (uint64_t *)PHYS_TO_DMAP(KPML4phys) + ii;
373 pdp = (uint64_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
374 i = pmap_pdpe_index(va);
376 /* We always write a page, even if it is zero */
377 if ((pdp[i] & PG_V) == 0) {
378 error = blk_write(di, (char *)&fakepd, 0, PAGE_SIZE);
381 /* flush, in case we reuse fakepd in the same block */
382 error = blk_flush(di);
388 /* 1GB page is represented as 512 2MB pages in a dump */
389 if ((pdp[i] & PG_PS) != 0) {
390 /* PDPE and PDP have identical layout in this case */
392 for (j = 1; j < NPDEPG; j++)
393 fakepd[j] = fakepd[j - 1] + NBPDR;
394 error = blk_write(di, (char *)&fakepd, 0, PAGE_SIZE);
397 /* flush, in case we reuse fakepd in the same block */
398 error = blk_flush(di);
401 bzero(fakepd, sizeof(fakepd));
405 pd = (uint64_t *)PHYS_TO_DMAP(pdp[i] & PG_FRAME);
406 error = blk_write(di, (char *)pd, 0, PAGE_SIZE);
409 error = blk_flush(di);
414 /* Dump memory chunks */
415 /* XXX cluster it up and use blk_dump() */
416 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
417 bits = vm_page_dump[i];
420 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
421 error = blk_write(di, 0, pa, PAGE_SIZE);
424 bits &= ~(1ul << bit);
428 error = blk_flush(di);
433 error = dump_write_pad(di, &kdh, 0, dumplo, sizeof(kdh), &size);
438 /* Signal completion, signoff and exit stage left. */
439 dump_write(di, NULL, 0, 0, 0);
440 printf("\nDump complete\n");
448 if (error == ENOSPC) {
449 printf("Dump map grown while dumping. ");
450 if (retry_count < 5) {
451 printf("Retrying...\n");
454 printf("Dump failed.\n");
456 else if (error == ECANCELED)
457 printf("Dump aborted\n");
458 else if (error == E2BIG)
459 printf("Dump failed. Partition too small.\n");
461 printf("** DUMP FAILED (ERROR %d) **\n", error);
466 dump_add_page(vm_paddr_t pa)
471 idx = pa >> 6; /* 2^6 = 64 */
473 atomic_set_long(&vm_page_dump[idx], 1ul << bit);
477 dump_drop_page(vm_paddr_t pa)
482 idx = pa >> 6; /* 2^6 = 64 */
484 atomic_clear_long(&vm_page_dump[idx], 1ul << bit);