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$");
30 #include "opt_watchdog.h"
32 #include <sys/param.h>
33 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/kerneldump.h>
38 #include <sys/msgbuf.h>
40 #include <sys/watchdog.h>
44 #include <machine/atomic.h>
45 #include <machine/elf.h>
46 #include <machine/md_var.h>
47 #include <machine/vmparam.h>
48 #include <machine/minidump.h>
50 CTASSERT(sizeof(struct kerneldumpheader) == 512);
53 * Don't touch the first SIZEOF_METADATA bytes on the dump device. This
54 * is to protect us from metadata and to protect metadata from us.
56 #define SIZEOF_METADATA (64*1024)
58 #define MD_ALIGN(x) (((off_t)(x) + PAGE_MASK) & ~PAGE_MASK)
59 #define DEV_ALIGN(x) (((off_t)(x) + (DEV_BSIZE-1)) & ~(DEV_BSIZE-1))
61 extern uint64_t KPDPphys;
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)
81 for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
82 if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
88 #define PG2MB(pgs) (((pgs) + (1 << 8) - 1) >> 8)
91 blk_flush(struct dumperinfo *di)
98 error = dump_write(di, dump_va, 0, dumplo, fragsz);
108 } progress_track[10] = {
122 report_progress(size_t progress, size_t dumpsize)
126 sofar = 100 - ((progress * 100) / dumpsize);
127 for (i = 0; i < 10; i++) {
128 if (sofar < progress_track[i].min_per || 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 (pa != 0 && (((uintptr_t)ptr) % PAGE_SIZE) != 0) {
158 printf("address not page aligned\n");
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 *pdp, *pd, *pt, pa;
224 struct minidumphdr mdhdr;
230 /* Walk page table pages, set bits in vm_page_dump */
232 pdp = (uint64_t *)PHYS_TO_DMAP(KPDPphys);
233 for (va = VM_MIN_KERNEL_ADDRESS; va < MAX(KERNBASE + NKPT * NBPDR,
236 * We always write a page, even if it is zero. Each
237 * page written corresponds to 1GB of space
239 pmapsize += PAGE_SIZE;
240 i = (va >> PDPSHIFT) & ((1ul << NPDPEPGSHIFT) - 1);
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 = (va >> PDRSHIFT) & ((1ul << NPDEPGSHIFT) - 1);
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;
314 /* Determine dump offset on device. */
315 if (di->mediasize < SIZEOF_METADATA + dumpsize + sizeof(kdh) * 2) {
319 dumplo = di->mediaoffset + di->mediasize - dumpsize;
320 dumplo -= sizeof(kdh) * 2;
323 /* Initialize mdhdr */
324 bzero(&mdhdr, sizeof(mdhdr));
325 strcpy(mdhdr.magic, MINIDUMP_MAGIC);
326 mdhdr.version = MINIDUMP_VERSION;
327 mdhdr.msgbufsize = msgbufp->msg_size;
328 mdhdr.bitmapsize = vm_page_dump_size;
329 mdhdr.pmapsize = pmapsize;
330 mdhdr.kernbase = VM_MIN_KERNEL_ADDRESS;
331 mdhdr.dmapbase = DMAP_MIN_ADDRESS;
332 mdhdr.dmapend = DMAP_MAX_ADDRESS;
334 mkdumpheader(&kdh, KERNELDUMPMAGIC, KERNELDUMP_AMD64_VERSION, dumpsize, di->blocksize);
336 printf("Dumping %llu out of %ju MB:", (long long)dumpsize >> 20,
337 ptoa((uintmax_t)physmem) / 1048576);
340 error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
343 dumplo += sizeof(kdh);
346 bzero(&fakepd, sizeof(fakepd));
347 bcopy(&mdhdr, &fakepd, sizeof(mdhdr));
348 error = blk_write(di, (char *)&fakepd, 0, PAGE_SIZE);
352 /* Dump msgbuf up front */
353 error = blk_write(di, (char *)msgbufp->msg_ptr, 0, round_page(msgbufp->msg_size));
358 error = blk_write(di, (char *)vm_page_dump, 0, round_page(vm_page_dump_size));
362 /* Dump kernel page directory pages */
363 bzero(fakepd, sizeof(fakepd));
364 pdp = (uint64_t *)PHYS_TO_DMAP(KPDPphys);
365 for (va = VM_MIN_KERNEL_ADDRESS; va < MAX(KERNBASE + NKPT * NBPDR,
366 kernel_vm_end); va += NBPDP) {
367 i = (va >> PDPSHIFT) & ((1ul << NPDPEPGSHIFT) - 1);
369 /* We always write a page, even if it is zero */
370 if ((pdp[i] & PG_V) == 0) {
371 error = blk_write(di, (char *)&fakepd, 0, PAGE_SIZE);
374 /* flush, in case we reuse fakepd in the same block */
375 error = blk_flush(di);
381 /* 1GB page is represented as 512 2MB pages in a dump */
382 if ((pdp[i] & PG_PS) != 0) {
383 /* PDPE and PDP have identical layout in this case */
385 for (j = 1; j < NPDEPG; j++)
386 fakepd[j] = fakepd[j - 1] + NBPDR;
387 error = blk_write(di, (char *)&fakepd, 0, PAGE_SIZE);
390 /* flush, in case we reuse fakepd in the same block */
391 error = blk_flush(di);
394 bzero(fakepd, sizeof(fakepd));
398 pd = (uint64_t *)PHYS_TO_DMAP(pdp[i] & PG_FRAME);
399 error = blk_write(di, (char *)pd, 0, PAGE_SIZE);
402 error = blk_flush(di);
407 /* Dump memory chunks */
408 /* XXX cluster it up and use blk_dump() */
409 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
410 bits = vm_page_dump[i];
413 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
414 error = blk_write(di, 0, pa, PAGE_SIZE);
417 bits &= ~(1ul << bit);
421 error = blk_flush(di);
426 error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
429 dumplo += sizeof(kdh);
431 /* Signal completion, signoff and exit stage left. */
432 dump_write(di, NULL, 0, 0, 0);
433 printf("\nDump complete\n");
441 if (error == ENOSPC) {
442 printf("Dump map grown while dumping. ");
443 if (retry_count < 5) {
444 printf("Retrying...\n");
447 printf("Dump failed.\n");
449 else if (error == ECANCELED)
450 printf("Dump aborted\n");
451 else if (error == E2BIG)
452 printf("Dump failed. Partition too small.\n");
454 printf("** DUMP FAILED (ERROR %d) **\n", error);
458 dump_add_page(vm_paddr_t pa)
463 idx = pa >> 6; /* 2^6 = 64 */
465 atomic_set_long(&vm_page_dump[idx], 1ul << bit);
469 dump_drop_page(vm_paddr_t pa)
474 idx = pa >> 6; /* 2^6 = 64 */
476 atomic_clear_long(&vm_page_dump[idx], 1ul << bit);