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>
41 #include <sys/watchdog.h>
45 #include <machine/atomic.h>
46 #include <machine/elf.h>
47 #include <machine/md_var.h>
48 #include <machine/vmparam.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 #define MD_ALIGN(x) (((off_t)(x) + PAGE_MASK) & ~PAGE_MASK)
60 #define DEV_ALIGN(x) (((off_t)(x) + (DEV_BSIZE-1)) & ~(DEV_BSIZE-1))
62 uint64_t *vm_page_dump;
63 int vm_page_dump_size;
65 static struct kerneldumpheader kdh;
68 /* Handle chunked writes. */
71 static size_t counter, progress, dumpsize;
73 CTASSERT(sizeof(*vm_page_dump) == 8);
76 is_dumpable(vm_paddr_t pa)
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 < 10; i++) {
127 if (sofar < progress_track[i].min_per || sofar > progress_track[i].max_per)
129 if (progress_track[i].visited)
131 progress_track[i].visited = 1;
132 printf("..%d%%", sofar);
138 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
144 maxdumpsz = min(di->maxiosize, MAXDUMPPGS * PAGE_SIZE);
145 if (maxdumpsz == 0) /* seatbelt */
146 maxdumpsz = PAGE_SIZE;
148 if ((sz % PAGE_SIZE) != 0) {
149 printf("size not page aligned\n");
152 if (ptr != NULL && pa != 0) {
153 printf("cant have both va and pa!\n");
156 if (pa != 0 && (((uintptr_t)ptr) % PAGE_SIZE) != 0) {
157 printf("address not page aligned\n");
161 /* If we're doing a virtual dump, flush any pre-existing pa pages */
162 error = blk_flush(di);
167 len = maxdumpsz - fragsz;
173 report_progress(progress, dumpsize);
174 counter &= (1<<24) - 1;
177 wdog_kern_pat(WD_LASTVAL);
180 error = dump_write(di, ptr, 0, dumplo, len);
187 for (i = 0; i < len; i += PAGE_SIZE)
188 dump_va = pmap_kenter_temporary(pa + i, (i + fragsz) >> PAGE_SHIFT);
192 if (fragsz == maxdumpsz) {
193 error = blk_flush(di);
199 /* Check for user abort. */
204 printf(" (CTRL-C to abort) ");
210 /* A fake page table page, to avoid having to handle both 4K and 2M pages */
211 static pd_entry_t fakepd[NPDEPG];
214 minidumpsys(struct dumperinfo *di)
220 uint64_t *pdp, *pd, *pt, pa;
223 struct minidumphdr mdhdr;
229 /* Walk page table pages, set bits in vm_page_dump */
231 pdp = (uint64_t *)PHYS_TO_DMAP(KPDPphys);
232 for (va = VM_MIN_KERNEL_ADDRESS; va < MAX(KERNBASE + NKPT * NBPDR,
235 * We always write a page, even if it is zero. Each
236 * page written corresponds to 1GB of space
238 pmapsize += PAGE_SIZE;
239 i = (va >> PDPSHIFT) & ((1ul << NPDPEPGSHIFT) - 1);
240 if ((pdp[i] & PG_V) == 0) {
246 * 1GB page is represented as 512 2MB pages in a dump.
248 if ((pdp[i] & PG_PS) != 0) {
250 pa = pdp[i] & PG_PS_FRAME;
251 for (n = 0; n < NPDEPG * NPTEPG; n++) {
259 pd = (uint64_t *)PHYS_TO_DMAP(pdp[i] & PG_FRAME);
260 for (n = 0; n < NPDEPG; n++, va += NBPDR) {
261 j = (va >> PDRSHIFT) & ((1ul << NPDEPGSHIFT) - 1);
263 if ((pd[j] & PG_V) == 0)
266 if ((pd[j] & PG_PS) != 0) {
267 /* This is an entire 2M page. */
268 pa = pd[j] & PG_PS_FRAME;
269 for (k = 0; k < NPTEPG; k++) {
277 pa = pd[j] & PG_FRAME;
278 /* set bit for this PTE page */
281 /* and for each valid page in this 2MB block */
282 pt = (uint64_t *)PHYS_TO_DMAP(pd[j] & PG_FRAME);
283 for (k = 0; k < NPTEPG; k++) {
284 if ((pt[k] & PG_V) == 0)
286 pa = pt[k] & PG_FRAME;
293 /* Calculate dump size. */
295 dumpsize += round_page(msgbufp->msg_size);
296 dumpsize += round_page(vm_page_dump_size);
297 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
298 bits = vm_page_dump[i];
301 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
302 /* Clear out undumpable pages now if needed */
303 if (is_dumpable(pa)) {
304 dumpsize += PAGE_SIZE;
308 bits &= ~(1ul << bit);
311 dumpsize += PAGE_SIZE;
313 /* Determine dump offset on device. */
314 if (di->mediasize < SIZEOF_METADATA + dumpsize + sizeof(kdh) * 2) {
318 dumplo = di->mediaoffset + di->mediasize - dumpsize;
319 dumplo -= sizeof(kdh) * 2;
322 /* Initialize mdhdr */
323 bzero(&mdhdr, sizeof(mdhdr));
324 strcpy(mdhdr.magic, MINIDUMP_MAGIC);
325 mdhdr.version = MINIDUMP_VERSION;
326 mdhdr.msgbufsize = msgbufp->msg_size;
327 mdhdr.bitmapsize = vm_page_dump_size;
328 mdhdr.pmapsize = pmapsize;
329 mdhdr.kernbase = VM_MIN_KERNEL_ADDRESS;
330 mdhdr.dmapbase = DMAP_MIN_ADDRESS;
331 mdhdr.dmapend = DMAP_MAX_ADDRESS;
333 mkdumpheader(&kdh, KERNELDUMPMAGIC, KERNELDUMP_AMD64_VERSION, dumpsize, di->blocksize);
335 printf("Dumping %llu out of %ju MB:", (long long)dumpsize >> 20,
336 ptoa((uintmax_t)physmem) / 1048576);
339 error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
342 dumplo += sizeof(kdh);
345 bzero(&fakepd, sizeof(fakepd));
346 bcopy(&mdhdr, &fakepd, sizeof(mdhdr));
347 error = blk_write(di, (char *)&fakepd, 0, PAGE_SIZE);
351 /* Dump msgbuf up front */
352 error = blk_write(di, (char *)msgbufp->msg_ptr, 0, round_page(msgbufp->msg_size));
357 error = blk_write(di, (char *)vm_page_dump, 0, round_page(vm_page_dump_size));
361 /* Dump kernel page directory pages */
362 bzero(fakepd, sizeof(fakepd));
363 pdp = (uint64_t *)PHYS_TO_DMAP(KPDPphys);
364 for (va = VM_MIN_KERNEL_ADDRESS; va < MAX(KERNBASE + NKPT * NBPDR,
365 kernel_vm_end); va += NBPDP) {
366 i = (va >> PDPSHIFT) & ((1ul << NPDPEPGSHIFT) - 1);
368 /* We always write a page, even if it is zero */
369 if ((pdp[i] & PG_V) == 0) {
370 error = blk_write(di, (char *)&fakepd, 0, PAGE_SIZE);
373 /* flush, in case we reuse fakepd in the same block */
374 error = blk_flush(di);
380 /* 1GB page is represented as 512 2MB pages in a dump */
381 if ((pdp[i] & PG_PS) != 0) {
382 /* PDPE and PDP have identical layout in this case */
384 for (j = 1; j < NPDEPG; j++)
385 fakepd[j] = fakepd[j - 1] + NBPDR;
386 error = blk_write(di, (char *)&fakepd, 0, PAGE_SIZE);
389 /* flush, in case we reuse fakepd in the same block */
390 error = blk_flush(di);
393 bzero(fakepd, sizeof(fakepd));
397 pd = (uint64_t *)PHYS_TO_DMAP(pdp[i] & PG_FRAME);
398 error = blk_write(di, (char *)pd, 0, PAGE_SIZE);
401 error = blk_flush(di);
406 /* Dump memory chunks */
407 /* XXX cluster it up and use blk_dump() */
408 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
409 bits = vm_page_dump[i];
412 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
413 error = blk_write(di, 0, pa, PAGE_SIZE);
416 bits &= ~(1ul << bit);
420 error = blk_flush(di);
425 error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
428 dumplo += sizeof(kdh);
430 /* Signal completion, signoff and exit stage left. */
431 dump_write(di, NULL, 0, 0, 0);
432 printf("\nDump complete\n");
440 if (error == ENOSPC) {
441 printf("Dump map grown while dumping. ");
442 if (retry_count < 5) {
443 printf("Retrying...\n");
446 printf("Dump failed.\n");
448 else if (error == ECANCELED)
449 printf("Dump aborted\n");
450 else if (error == E2BIG)
451 printf("Dump failed. Partition too small.\n");
453 printf("** DUMP FAILED (ERROR %d) **\n", error);
457 dump_add_page(vm_paddr_t pa)
462 idx = pa >> 6; /* 2^6 = 64 */
464 atomic_set_long(&vm_page_dump[idx], 1ul << bit);
468 dump_drop_page(vm_paddr_t pa)
473 idx = pa >> 6; /* 2^6 = 64 */
475 atomic_clear_long(&vm_page_dump[idx], 1ul << bit);