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>
49 #include <vm/vm_dumpset.h>
51 #include <machine/atomic.h>
52 #include <machine/elf.h>
53 #include <machine/md_var.h>
54 #include <machine/minidump.h>
56 CTASSERT(sizeof(struct kerneldumpheader) == 512);
58 static struct kerneldumpheader kdh;
60 /* Handle chunked writes. */
63 static size_t counter, progress, dumpsize, wdog_next;
65 static int dump_retry_count = 5;
66 SYSCTL_INT(_machdep, OID_AUTO, dump_retry_count, CTLFLAG_RWTUN,
67 &dump_retry_count, 0, "Number of times dump has to retry before bailing out");
69 #define PG2MB(pgs) (((pgs) + (1 << 8) - 1) >> 8)
72 blk_flush(struct dumperinfo *di)
79 error = dump_append(di, dump_va, 0, fragsz);
88 } progress_track[10] = {
102 report_progress(size_t progress, size_t dumpsize)
106 sofar = 100 - ((progress * 100) / dumpsize);
107 for (i = 0; i < nitems(progress_track); i++) {
108 if (sofar < progress_track[i].min_per ||
109 sofar > progress_track[i].max_per)
111 if (progress_track[i].visited)
113 progress_track[i].visited = 1;
114 printf("..%d%%", sofar);
119 /* Pat the watchdog approximately every 128MB of the dump. */
120 #define WDOG_DUMP_INTERVAL (128 * 1024 * 1024)
123 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
129 maxdumpsz = min(di->maxiosize, MAXDUMPPGS * PAGE_SIZE);
130 if (maxdumpsz == 0) /* seatbelt */
131 maxdumpsz = PAGE_SIZE;
133 if ((sz % PAGE_SIZE) != 0) {
134 printf("size not page aligned\n");
137 if (ptr != NULL && pa != 0) {
138 printf("cant have both va and pa!\n");
141 if ((((uintptr_t)pa) % PAGE_SIZE) != 0) {
142 printf("address not page aligned %p\n", ptr);
146 /* If we're doing a virtual dump, flush any pre-existing pa pages */
147 error = blk_flush(di);
152 len = maxdumpsz - fragsz;
158 report_progress(progress, dumpsize);
159 counter &= (1<<24) - 1;
161 if (progress <= wdog_next) {
162 wdog_kern_pat(WD_LASTVAL);
163 if (wdog_next > WDOG_DUMP_INTERVAL)
164 wdog_next -= WDOG_DUMP_INTERVAL;
170 error = dump_append(di, ptr, 0, len);
176 for (i = 0; i < len; i += PAGE_SIZE)
177 dump_va = pmap_kenter_temporary(pa + i, (i + fragsz) >> PAGE_SHIFT);
181 if (fragsz == maxdumpsz) {
182 error = blk_flush(di);
188 /* Check for user abort. */
193 printf(" (CTRL-C to abort) ");
199 /* A fake page table page, to avoid having to handle both 4K and 2M pages */
200 static pd_entry_t fakepd[NPDEPG];
203 minidumpsys(struct dumperinfo *di)
208 uint64_t *pml4, *pdp, *pd, *pt, pa;
211 struct minidumphdr mdhdr;
217 for (i = 0; i < nitems(progress_track); i++)
218 progress_track[i].visited = 0;
219 /* Walk page table pages, set bits in vm_page_dump */
221 for (va = VM_MIN_KERNEL_ADDRESS; va < MAX(KERNBASE + nkpt * NBPDR,
224 * We always write a page, even if it is zero. Each
225 * page written corresponds to 1GB of space
227 pmapsize += PAGE_SIZE;
228 ii = pmap_pml4e_index(va);
229 pml4 = (uint64_t *)PHYS_TO_DMAP(KPML4phys) + ii;
230 pdp = (uint64_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
231 i = pmap_pdpe_index(va);
232 if ((pdp[i] & PG_V) == 0) {
238 * 1GB page is represented as 512 2MB pages in a dump.
240 if ((pdp[i] & PG_PS) != 0) {
242 pa = pdp[i] & PG_PS_FRAME;
243 for (n = 0; n < NPDEPG * NPTEPG; n++) {
244 if (vm_phys_is_dumpable(pa))
251 pd = (uint64_t *)PHYS_TO_DMAP(pdp[i] & PG_FRAME);
252 for (n = 0; n < NPDEPG; n++, va += NBPDR) {
253 j = pmap_pde_index(va);
255 if ((pd[j] & PG_V) == 0)
258 if ((pd[j] & PG_PS) != 0) {
259 /* This is an entire 2M page. */
260 pa = pd[j] & PG_PS_FRAME;
261 for (k = 0; k < NPTEPG; k++) {
262 if (vm_phys_is_dumpable(pa))
269 pa = pd[j] & PG_FRAME;
270 /* set bit for this PTE page */
271 if (vm_phys_is_dumpable(pa))
273 /* and for each valid page in this 2MB block */
274 pt = (uint64_t *)PHYS_TO_DMAP(pd[j] & PG_FRAME);
275 for (k = 0; k < NPTEPG; k++) {
276 if ((pt[k] & PG_V) == 0)
278 pa = pt[k] & PG_FRAME;
279 if (vm_phys_is_dumpable(pa))
285 /* Calculate dump size. */
287 dumpsize += round_page(msgbufp->msg_size);
288 dumpsize += round_page(sizeof(dump_avail));
289 dumpsize += round_page(BITSET_SIZE(vm_page_dump_pages));
290 VM_PAGE_DUMP_FOREACH(pa) {
291 /* Clear out undumpable pages now if needed */
292 if (vm_phys_is_dumpable(pa)) {
293 dumpsize += PAGE_SIZE;
298 dumpsize += PAGE_SIZE;
300 wdog_next = progress = dumpsize;
302 /* Initialize mdhdr */
303 bzero(&mdhdr, sizeof(mdhdr));
304 strcpy(mdhdr.magic, MINIDUMP_MAGIC);
305 mdhdr.version = MINIDUMP_VERSION;
306 mdhdr.msgbufsize = msgbufp->msg_size;
307 mdhdr.bitmapsize = round_page(BITSET_SIZE(vm_page_dump_pages));
308 mdhdr.pmapsize = pmapsize;
309 mdhdr.kernbase = VM_MIN_KERNEL_ADDRESS;
310 mdhdr.dmapbase = DMAP_MIN_ADDRESS;
311 mdhdr.dmapend = DMAP_MAX_ADDRESS;
312 mdhdr.dumpavailsize = round_page(sizeof(dump_avail));
314 dump_init_header(di, &kdh, KERNELDUMPMAGIC, KERNELDUMP_AMD64_VERSION,
317 error = dump_start(di, &kdh);
321 printf("Dumping %llu out of %ju MB:", (long long)dumpsize >> 20,
322 ptoa((uintmax_t)physmem) / 1048576);
325 bzero(&fakepd, sizeof(fakepd));
326 bcopy(&mdhdr, &fakepd, sizeof(mdhdr));
327 error = blk_write(di, (char *)&fakepd, 0, PAGE_SIZE);
331 /* Dump msgbuf up front */
332 error = blk_write(di, (char *)msgbufp->msg_ptr, 0, round_page(msgbufp->msg_size));
336 /* Dump dump_avail */
337 _Static_assert(sizeof(dump_avail) <= sizeof(fakepd),
338 "Large dump_avail not handled");
339 bzero(&fakepd, sizeof(fakepd));
340 memcpy(fakepd, dump_avail, sizeof(dump_avail));
341 error = blk_write(di, (char *)fakepd, 0, PAGE_SIZE);
346 error = blk_write(di, (char *)vm_page_dump, 0,
347 round_page(BITSET_SIZE(vm_page_dump_pages)));
351 /* Dump kernel page directory pages */
352 bzero(fakepd, sizeof(fakepd));
353 for (va = VM_MIN_KERNEL_ADDRESS; va < MAX(KERNBASE + nkpt * NBPDR,
354 kernel_vm_end); va += NBPDP) {
355 ii = pmap_pml4e_index(va);
356 pml4 = (uint64_t *)PHYS_TO_DMAP(KPML4phys) + ii;
357 pdp = (uint64_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
358 i = pmap_pdpe_index(va);
360 /* We always write a page, even if it is zero */
361 if ((pdp[i] & PG_V) == 0) {
362 error = blk_write(di, (char *)&fakepd, 0, PAGE_SIZE);
365 /* flush, in case we reuse fakepd in the same block */
366 error = blk_flush(di);
372 /* 1GB page is represented as 512 2MB pages in a dump */
373 if ((pdp[i] & PG_PS) != 0) {
374 /* PDPE and PDP have identical layout in this case */
376 for (j = 1; j < NPDEPG; j++)
377 fakepd[j] = fakepd[j - 1] + NBPDR;
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);
385 bzero(fakepd, sizeof(fakepd));
389 pd = (uint64_t *)PHYS_TO_DMAP(pdp[i] & PG_FRAME);
390 error = blk_write(di, (char *)pd, 0, PAGE_SIZE);
393 error = blk_flush(di);
398 /* Dump memory chunks */
399 VM_PAGE_DUMP_FOREACH(pa) {
400 error = blk_write(di, 0, pa, PAGE_SIZE);
405 error = blk_flush(di);
409 error = dump_finish(di, &kdh);
413 printf("\nDump complete\n");
421 if (error == ENOSPC) {
422 printf("Dump map grown while dumping. ");
423 if (retry_count < dump_retry_count) {
424 printf("Retrying...\n");
427 printf("Dump failed.\n");
429 else if (error == ECANCELED)
430 printf("Dump aborted\n");
431 else if (error == E2BIG) {
432 printf("Dump failed. Partition too small (about %lluMB were "
433 "needed this time).\n", (long long)dumpsize >> 20);
435 printf("** DUMP FAILED (ERROR %d) **\n", error);