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/vmparam.h>
50 #include <machine/minidump.h>
52 CTASSERT(sizeof(struct kerneldumpheader) == 512);
55 * Don't touch the first SIZEOF_METADATA bytes on the dump device. This
56 * is to protect us from metadata and to protect metadata from us.
58 #define SIZEOF_METADATA (64*1024)
60 #define MD_ALIGN(x) (((off_t)(x) + PAGE_MASK) & ~PAGE_MASK)
61 #define DEV_ALIGN(x) (((off_t)(x) + (DEV_BSIZE-1)) & ~(DEV_BSIZE-1))
63 uint64_t *vm_page_dump;
64 int vm_page_dump_size;
66 static struct kerneldumpheader kdh;
69 /* Handle chunked writes. */
72 static size_t counter, progress, dumpsize;
74 CTASSERT(sizeof(*vm_page_dump) == 8);
77 is_dumpable(vm_paddr_t pa)
82 if ((m = vm_phys_paddr_to_vm_page(pa)) != NULL)
83 return ((m->flags & PG_NODUMP) == 0);
84 for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
85 if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
91 #define PG2MB(pgs) (((pgs) + (1 << 8) - 1) >> 8)
94 blk_flush(struct dumperinfo *di)
101 error = dump_write(di, dump_va, 0, dumplo, fragsz);
111 } progress_track[10] = {
125 report_progress(size_t progress, size_t dumpsize)
129 sofar = 100 - ((progress * 100) / dumpsize);
130 for (i = 0; i < nitems(progress_track); i++) {
131 if (sofar < progress_track[i].min_per ||
132 sofar > progress_track[i].max_per)
134 if (progress_track[i].visited)
136 progress_track[i].visited = 1;
137 printf("..%d%%", sofar);
143 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
149 maxdumpsz = min(di->maxiosize, MAXDUMPPGS * PAGE_SIZE);
150 if (maxdumpsz == 0) /* seatbelt */
151 maxdumpsz = PAGE_SIZE;
153 if ((sz % PAGE_SIZE) != 0) {
154 printf("size not page aligned\n");
157 if (ptr != NULL && pa != 0) {
158 printf("cant have both va and pa!\n");
161 if ((((uintptr_t)pa) % PAGE_SIZE) != 0) {
162 printf("address not page aligned %p\n", ptr);
166 /* If we're doing a virtual dump, flush any pre-existing pa pages */
167 error = blk_flush(di);
172 len = maxdumpsz - fragsz;
178 report_progress(progress, dumpsize);
179 counter &= (1<<24) - 1;
182 wdog_kern_pat(WD_LASTVAL);
185 error = dump_write(di, ptr, 0, dumplo, len);
192 for (i = 0; i < len; i += PAGE_SIZE)
193 dump_va = pmap_kenter_temporary(pa + i, (i + fragsz) >> PAGE_SHIFT);
197 if (fragsz == maxdumpsz) {
198 error = blk_flush(di);
204 /* Check for user abort. */
209 printf(" (CTRL-C to abort) ");
215 /* A fake page table page, to avoid having to handle both 4K and 2M pages */
216 static pd_entry_t fakepd[NPDEPG];
219 minidumpsys(struct dumperinfo *di)
225 uint64_t *pml4, *pdp, *pd, *pt, pa;
226 int i, ii, j, k, n, bit;
228 struct minidumphdr mdhdr;
234 for (i = 0; i < nitems(progress_track); i++)
235 progress_track[i].visited = 0;
236 /* Walk page table pages, set bits in vm_page_dump */
238 for (va = VM_MIN_KERNEL_ADDRESS; va < MAX(KERNBASE + nkpt * NBPDR,
241 * We always write a page, even if it is zero. Each
242 * page written corresponds to 1GB of space
244 pmapsize += PAGE_SIZE;
245 ii = (va >> PML4SHIFT) & ((1ul << NPML4EPGSHIFT) - 1);
246 pml4 = (uint64_t *)PHYS_TO_DMAP(KPML4phys) + ii;
247 pdp = (uint64_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
248 i = (va >> PDPSHIFT) & ((1ul << NPDPEPGSHIFT) - 1);
249 if ((pdp[i] & PG_V) == 0) {
255 * 1GB page is represented as 512 2MB pages in a dump.
257 if ((pdp[i] & PG_PS) != 0) {
259 pa = pdp[i] & PG_PS_FRAME;
260 for (n = 0; n < NPDEPG * NPTEPG; n++) {
268 pd = (uint64_t *)PHYS_TO_DMAP(pdp[i] & PG_FRAME);
269 for (n = 0; n < NPDEPG; n++, va += NBPDR) {
270 j = (va >> PDRSHIFT) & ((1ul << NPDEPGSHIFT) - 1);
272 if ((pd[j] & PG_V) == 0)
275 if ((pd[j] & PG_PS) != 0) {
276 /* This is an entire 2M page. */
277 pa = pd[j] & PG_PS_FRAME;
278 for (k = 0; k < NPTEPG; k++) {
286 pa = pd[j] & PG_FRAME;
287 /* set bit for this PTE page */
290 /* and for each valid page in this 2MB block */
291 pt = (uint64_t *)PHYS_TO_DMAP(pd[j] & PG_FRAME);
292 for (k = 0; k < NPTEPG; k++) {
293 if ((pt[k] & PG_V) == 0)
295 pa = pt[k] & PG_FRAME;
302 /* Calculate dump size. */
304 dumpsize += round_page(msgbufp->msg_size);
305 dumpsize += round_page(vm_page_dump_size);
306 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
307 bits = vm_page_dump[i];
310 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
311 /* Clear out undumpable pages now if needed */
312 if (is_dumpable(pa)) {
313 dumpsize += PAGE_SIZE;
317 bits &= ~(1ul << bit);
320 dumpsize += PAGE_SIZE;
322 /* Determine dump offset on device. */
323 if (di->mediasize < SIZEOF_METADATA + dumpsize + sizeof(kdh) * 2) {
327 dumplo = di->mediaoffset + di->mediasize - dumpsize;
328 dumplo -= sizeof(kdh) * 2;
331 /* Initialize mdhdr */
332 bzero(&mdhdr, sizeof(mdhdr));
333 strcpy(mdhdr.magic, MINIDUMP_MAGIC);
334 mdhdr.version = MINIDUMP_VERSION;
335 mdhdr.msgbufsize = msgbufp->msg_size;
336 mdhdr.bitmapsize = vm_page_dump_size;
337 mdhdr.pmapsize = pmapsize;
338 mdhdr.kernbase = VM_MIN_KERNEL_ADDRESS;
339 mdhdr.dmapbase = DMAP_MIN_ADDRESS;
340 mdhdr.dmapend = DMAP_MAX_ADDRESS;
342 mkdumpheader(&kdh, KERNELDUMPMAGIC, KERNELDUMP_AMD64_VERSION, dumpsize, di->blocksize);
344 printf("Dumping %llu out of %ju MB:", (long long)dumpsize >> 20,
345 ptoa((uintmax_t)physmem) / 1048576);
348 error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
351 dumplo += sizeof(kdh);
354 bzero(&fakepd, sizeof(fakepd));
355 bcopy(&mdhdr, &fakepd, sizeof(mdhdr));
356 error = blk_write(di, (char *)&fakepd, 0, PAGE_SIZE);
360 /* Dump msgbuf up front */
361 error = blk_write(di, (char *)msgbufp->msg_ptr, 0, round_page(msgbufp->msg_size));
366 error = blk_write(di, (char *)vm_page_dump, 0, round_page(vm_page_dump_size));
370 /* Dump kernel page directory pages */
371 bzero(fakepd, sizeof(fakepd));
372 for (va = VM_MIN_KERNEL_ADDRESS; va < MAX(KERNBASE + nkpt * NBPDR,
373 kernel_vm_end); va += NBPDP) {
374 ii = (va >> PML4SHIFT) & ((1ul << NPML4EPGSHIFT) - 1);
375 pml4 = (uint64_t *)PHYS_TO_DMAP(KPML4phys) + ii;
376 pdp = (uint64_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
377 i = (va >> PDPSHIFT) & ((1ul << NPDPEPGSHIFT) - 1);
379 /* We always write a page, even if it is zero */
380 if ((pdp[i] & PG_V) == 0) {
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);
391 /* 1GB page is represented as 512 2MB pages in a dump */
392 if ((pdp[i] & PG_PS) != 0) {
393 /* PDPE and PDP have identical layout in this case */
395 for (j = 1; j < NPDEPG; j++)
396 fakepd[j] = fakepd[j - 1] + NBPDR;
397 error = blk_write(di, (char *)&fakepd, 0, PAGE_SIZE);
400 /* flush, in case we reuse fakepd in the same block */
401 error = blk_flush(di);
404 bzero(fakepd, sizeof(fakepd));
408 pd = (uint64_t *)PHYS_TO_DMAP(pdp[i] & PG_FRAME);
409 error = blk_write(di, (char *)pd, 0, PAGE_SIZE);
412 error = blk_flush(di);
417 /* Dump memory chunks */
418 /* XXX cluster it up and use blk_dump() */
419 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
420 bits = vm_page_dump[i];
423 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
424 error = blk_write(di, 0, pa, PAGE_SIZE);
427 bits &= ~(1ul << bit);
431 error = blk_flush(di);
436 error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
439 dumplo += sizeof(kdh);
441 /* Signal completion, signoff and exit stage left. */
442 dump_write(di, NULL, 0, 0, 0);
443 printf("\nDump complete\n");
451 if (error == ENOSPC) {
452 printf("Dump map grown while dumping. ");
453 if (retry_count < 5) {
454 printf("Retrying...\n");
457 printf("Dump failed.\n");
459 else if (error == ECANCELED)
460 printf("Dump aborted\n");
461 else if (error == E2BIG)
462 printf("Dump failed. Partition too small.\n");
464 printf("** DUMP FAILED (ERROR %d) **\n", error);
468 dump_add_page(vm_paddr_t pa)
473 idx = pa >> 6; /* 2^6 = 64 */
475 atomic_set_long(&vm_page_dump[idx], 1ul << bit);
479 dump_drop_page(vm_paddr_t pa)
484 idx = pa >> 6; /* 2^6 = 64 */
486 atomic_clear_long(&vm_page_dump[idx], 1ul << bit);