2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2006 Peter Wemm
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 #include "opt_watchdog.h"
35 #include <sys/param.h>
36 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/kerneldump.h>
41 #include <sys/msgbuf.h>
42 #include <sys/sysctl.h>
43 #include <sys/watchdog.h>
44 #include <sys/vmmeter.h>
46 #include <vm/vm_param.h>
47 #include <vm/vm_page.h>
48 #include <vm/vm_phys.h>
50 #include <machine/atomic.h>
51 #include <machine/elf.h>
52 #include <machine/md_var.h>
53 #include <machine/minidump.h>
55 CTASSERT(sizeof(struct kerneldumpheader) == 512);
57 uint64_t *vm_page_dump;
58 int vm_page_dump_size;
60 static struct kerneldumpheader kdh;
62 /* Handle chunked writes. */
65 static size_t counter, progress, dumpsize, wdog_next;
67 CTASSERT(sizeof(*vm_page_dump) == 8);
68 static int dump_retry_count = 5;
69 SYSCTL_INT(_machdep, OID_AUTO, dump_retry_count, CTLFLAG_RWTUN,
70 &dump_retry_count, 0, "Number of times dump has to retry before bailing out");
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_append(di, dump_va, 0, fragsz);
106 } progress_track[10] = {
120 report_progress(size_t progress, size_t dumpsize)
124 sofar = 100 - ((progress * 100) / dumpsize);
125 for (i = 0; i < nitems(progress_track); i++) {
126 if (sofar < progress_track[i].min_per ||
127 sofar > progress_track[i].max_per)
129 if (progress_track[i].visited)
131 progress_track[i].visited = 1;
132 printf("..%d%%", sofar);
137 /* Pat the watchdog approximately every 128MB of the dump. */
138 #define WDOG_DUMP_INTERVAL (128 * 1024 * 1024)
141 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
147 maxdumpsz = min(di->maxiosize, MAXDUMPPGS * PAGE_SIZE);
148 if (maxdumpsz == 0) /* seatbelt */
149 maxdumpsz = PAGE_SIZE;
151 if ((sz % PAGE_SIZE) != 0) {
152 printf("size not page aligned\n");
155 if (ptr != NULL && pa != 0) {
156 printf("cant have both va and pa!\n");
159 if ((((uintptr_t)pa) % PAGE_SIZE) != 0) {
160 printf("address not page aligned %p\n", ptr);
164 /* If we're doing a virtual dump, flush any pre-existing pa pages */
165 error = blk_flush(di);
170 len = maxdumpsz - fragsz;
176 report_progress(progress, dumpsize);
177 counter &= (1<<24) - 1;
179 if (progress <= wdog_next) {
180 wdog_kern_pat(WD_LASTVAL);
181 if (wdog_next > WDOG_DUMP_INTERVAL)
182 wdog_next -= WDOG_DUMP_INTERVAL;
188 error = dump_append(di, ptr, 0, len);
194 for (i = 0; i < len; i += PAGE_SIZE)
195 dump_va = pmap_kenter_temporary(pa + i, (i + fragsz) >> PAGE_SHIFT);
199 if (fragsz == maxdumpsz) {
200 error = blk_flush(di);
206 /* Check for user abort. */
211 printf(" (CTRL-C to abort) ");
217 /* A fake page table page, to avoid having to handle both 4K and 2M pages */
218 static pd_entry_t fakepd[NPDEPG];
221 minidumpsys(struct dumperinfo *di)
227 uint64_t *pml4, *pdp, *pd, *pt, pa;
228 int i, ii, j, k, n, bit;
230 struct minidumphdr mdhdr;
236 for (i = 0; i < nitems(progress_track); i++)
237 progress_track[i].visited = 0;
238 /* Walk page table pages, set bits in vm_page_dump */
240 for (va = VM_MIN_KERNEL_ADDRESS; va < MAX(KERNBASE + nkpt * NBPDR,
243 * We always write a page, even if it is zero. Each
244 * page written corresponds to 1GB of space
246 pmapsize += PAGE_SIZE;
247 ii = pmap_pml4e_index(va);
248 pml4 = (uint64_t *)PHYS_TO_DMAP(KPML4phys) + ii;
249 pdp = (uint64_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
250 i = pmap_pdpe_index(va);
251 if ((pdp[i] & PG_V) == 0) {
257 * 1GB page is represented as 512 2MB pages in a dump.
259 if ((pdp[i] & PG_PS) != 0) {
261 pa = pdp[i] & PG_PS_FRAME;
262 for (n = 0; n < NPDEPG * NPTEPG; n++) {
270 pd = (uint64_t *)PHYS_TO_DMAP(pdp[i] & PG_FRAME);
271 for (n = 0; n < NPDEPG; n++, va += NBPDR) {
272 j = pmap_pde_index(va);
274 if ((pd[j] & PG_V) == 0)
277 if ((pd[j] & PG_PS) != 0) {
278 /* This is an entire 2M page. */
279 pa = pd[j] & PG_PS_FRAME;
280 for (k = 0; k < NPTEPG; k++) {
288 pa = pd[j] & PG_FRAME;
289 /* set bit for this PTE page */
292 /* and for each valid page in this 2MB block */
293 pt = (uint64_t *)PHYS_TO_DMAP(pd[j] & PG_FRAME);
294 for (k = 0; k < NPTEPG; k++) {
295 if ((pt[k] & PG_V) == 0)
297 pa = pt[k] & PG_FRAME;
304 /* Calculate dump size. */
306 dumpsize += round_page(msgbufp->msg_size);
307 dumpsize += round_page(vm_page_dump_size);
308 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
309 bits = vm_page_dump[i];
312 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
313 /* Clear out undumpable pages now if needed */
314 if (is_dumpable(pa)) {
315 dumpsize += PAGE_SIZE;
319 bits &= ~(1ul << bit);
322 dumpsize += PAGE_SIZE;
324 wdog_next = progress = dumpsize;
326 /* Initialize mdhdr */
327 bzero(&mdhdr, sizeof(mdhdr));
328 strcpy(mdhdr.magic, MINIDUMP_MAGIC);
329 mdhdr.version = MINIDUMP_VERSION;
330 mdhdr.msgbufsize = msgbufp->msg_size;
331 mdhdr.bitmapsize = vm_page_dump_size;
332 mdhdr.pmapsize = pmapsize;
333 mdhdr.kernbase = VM_MIN_KERNEL_ADDRESS;
334 mdhdr.dmapbase = DMAP_MIN_ADDRESS;
335 mdhdr.dmapend = DMAP_MAX_ADDRESS;
337 dump_init_header(di, &kdh, KERNELDUMPMAGIC, KERNELDUMP_AMD64_VERSION,
340 error = dump_start(di, &kdh);
344 printf("Dumping %llu out of %ju MB:", (long long)dumpsize >> 20,
345 ptoa((uintmax_t)physmem) / 1048576);
348 bzero(&fakepd, sizeof(fakepd));
349 bcopy(&mdhdr, &fakepd, sizeof(mdhdr));
350 error = blk_write(di, (char *)&fakepd, 0, PAGE_SIZE);
354 /* Dump msgbuf up front */
355 error = blk_write(di, (char *)msgbufp->msg_ptr, 0, round_page(msgbufp->msg_size));
360 error = blk_write(di, (char *)vm_page_dump, 0, round_page(vm_page_dump_size));
364 /* Dump kernel page directory pages */
365 bzero(fakepd, sizeof(fakepd));
366 for (va = VM_MIN_KERNEL_ADDRESS; va < MAX(KERNBASE + nkpt * NBPDR,
367 kernel_vm_end); va += NBPDP) {
368 ii = pmap_pml4e_index(va);
369 pml4 = (uint64_t *)PHYS_TO_DMAP(KPML4phys) + ii;
370 pdp = (uint64_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
371 i = pmap_pdpe_index(va);
373 /* We always write a page, even if it is zero */
374 if ((pdp[i] & PG_V) == 0) {
375 error = blk_write(di, (char *)&fakepd, 0, PAGE_SIZE);
378 /* flush, in case we reuse fakepd in the same block */
379 error = blk_flush(di);
385 /* 1GB page is represented as 512 2MB pages in a dump */
386 if ((pdp[i] & PG_PS) != 0) {
387 /* PDPE and PDP have identical layout in this case */
389 for (j = 1; j < NPDEPG; j++)
390 fakepd[j] = fakepd[j - 1] + NBPDR;
391 error = blk_write(di, (char *)&fakepd, 0, PAGE_SIZE);
394 /* flush, in case we reuse fakepd in the same block */
395 error = blk_flush(di);
398 bzero(fakepd, sizeof(fakepd));
402 pd = (uint64_t *)PHYS_TO_DMAP(pdp[i] & PG_FRAME);
403 error = blk_write(di, (char *)pd, 0, PAGE_SIZE);
406 error = blk_flush(di);
411 /* Dump memory chunks */
412 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
413 bits = vm_page_dump[i];
416 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
417 error = blk_write(di, 0, pa, PAGE_SIZE);
420 bits &= ~(1ul << bit);
424 error = blk_flush(di);
428 error = dump_finish(di, &kdh);
432 printf("\nDump complete\n");
440 if (error == ENOSPC) {
441 printf("Dump map grown while dumping. ");
442 if (retry_count < dump_retry_count) {
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 (about %lluMB were "
452 "needed this time).\n", (long long)dumpsize >> 20);
454 printf("** DUMP FAILED (ERROR %d) **\n", error);
459 dump_add_page(vm_paddr_t pa)
464 idx = pa >> 6; /* 2^6 = 64 */
466 atomic_set_long(&vm_page_dump[idx], 1ul << bit);
470 dump_drop_page(vm_paddr_t pa)
475 idx = pa >> 6; /* 2^6 = 64 */
477 atomic_clear_long(&vm_page_dump[idx], 1ul << bit);