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$");
32 #include "opt_watchdog.h"
34 #include <sys/param.h>
35 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/kerneldump.h>
40 #include <sys/msgbuf.h>
41 #include <sys/watchdog.h>
43 #include <vm/vm_param.h>
44 #include <vm/vm_page.h>
45 #include <vm/vm_phys.h>
47 #include <machine/atomic.h>
48 #include <machine/elf.h>
49 #include <machine/md_var.h>
50 #include <machine/vmparam.h>
51 #include <machine/minidump.h>
53 CTASSERT(sizeof(struct kerneldumpheader) == 512);
55 #define MD_ALIGN(x) (((off_t)(x) + PAGE_MASK) & ~PAGE_MASK)
56 #define DEV_ALIGN(x) roundup2((off_t)(x), DEV_BSIZE)
58 static struct kerneldumpheader kdh;
60 /* Handle chunked writes. */
63 static uint64_t counter, progress;
66 is_dumpable(vm_paddr_t pa)
70 for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
71 if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
77 #define PG2MB(pgs) (((pgs) + (1 << 8) - 1) >> 8)
80 blk_flush(struct dumperinfo *di)
87 error = dump_append(di, dump_va, 0, fragsz);
93 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
99 maxdumpsz = min(di->maxiosize, MAXDUMPPGS * PAGE_SIZE);
100 if (maxdumpsz == 0) /* seatbelt */
101 maxdumpsz = PAGE_SIZE;
103 if ((sz % PAGE_SIZE) != 0) {
104 printf("size not page aligned\n");
107 if (ptr != NULL && pa != 0) {
108 printf("cant have both va and pa!\n");
111 if (pa != 0 && (((uintptr_t)ptr) % PAGE_SIZE) != 0) {
112 printf("address not page aligned\n");
116 /* If we're doing a virtual dump, flush any pre-existing pa pages */
117 error = blk_flush(di);
122 len = maxdumpsz - fragsz;
128 printf(" %lld", PG2MB(progress >> PAGE_SHIFT));
129 counter &= (1<<24) - 1;
132 wdog_kern_pat(WD_LASTVAL);
135 error = dump_append(di, ptr, 0, len);
141 for (i = 0; i < len; i += PAGE_SIZE)
142 dump_va = pmap_kenter_temporary(pa + i, (i + fragsz) >> PAGE_SHIFT);
146 if (fragsz == maxdumpsz) {
147 error = blk_flush(di);
153 /* Check for user abort. */
158 printf(" (CTRL-C to abort) ");
164 /* A fake page table page, to avoid having to handle both 4K and 2M pages */
165 static pt_entry_t fakept[NPTEPG];
168 #define minidumpsys minidumpsys_pae
169 #define IdlePTD IdlePTD_pae
171 #define minidumpsys minidumpsys_nopae
172 #define IdlePTD IdlePTD_nopae
176 minidumpsys(struct dumperinfo *di)
186 struct minidumphdr mdhdr;
189 /* Walk page table pages, set bits in vm_page_dump */
191 for (va = KERNBASE; va < kernel_vm_end; va += NBPDR) {
193 * We always write a page, even if it is zero. Each
194 * page written corresponds to 2MB of space
196 ptesize += PAGE_SIZE;
197 pd = IdlePTD; /* always mapped! */
199 if ((pd[j] & (PG_PS | PG_V)) == (PG_PS | PG_V)) {
200 /* This is an entire 2M page. */
201 pa = pd[j] & PG_PS_FRAME;
202 for (k = 0; k < NPTEPG; k++) {
209 if ((pd[j] & PG_V) == PG_V) {
210 /* set bit for each valid page in this 2MB block */
211 pt = pmap_kenter_temporary(pd[j] & PG_FRAME, 0);
212 for (k = 0; k < NPTEPG; k++) {
213 if ((pt[k] & PG_V) == PG_V) {
214 pa = pt[k] & PG_FRAME;
220 /* nothing, we're going to dump a null page */
224 /* Calculate dump size. */
226 dumpsize += round_page(msgbufp->msg_size);
227 dumpsize += round_page(BITSET_SIZE(vm_page_dump_pages));
228 VM_PAGE_DUMP_FOREACH(pa) {
229 /* Clear out undumpable pages now if needed */
230 if (is_dumpable(pa)) {
231 dumpsize += PAGE_SIZE;
236 dumpsize += PAGE_SIZE;
240 /* Initialize mdhdr */
241 bzero(&mdhdr, sizeof(mdhdr));
242 strcpy(mdhdr.magic, MINIDUMP_MAGIC);
243 mdhdr.version = MINIDUMP_VERSION;
244 mdhdr.msgbufsize = msgbufp->msg_size;
245 mdhdr.bitmapsize = round_page(BITSET_SIZE(vm_page_dump_pages));
246 mdhdr.ptesize = ptesize;
247 mdhdr.kernbase = KERNBASE;
248 mdhdr.paemode = pae_mode;
250 dump_init_header(di, &kdh, KERNELDUMPMAGIC, KERNELDUMP_I386_VERSION,
253 error = dump_start(di, &kdh);
257 printf("Physical memory: %ju MB\n", ptoa((uintmax_t)physmem) / 1048576);
258 printf("Dumping %llu MB:", (long long)dumpsize >> 20);
261 bzero(&fakept, sizeof(fakept));
262 bcopy(&mdhdr, &fakept, sizeof(mdhdr));
263 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
267 /* Dump msgbuf up front */
268 error = blk_write(di, (char *)msgbufp->msg_ptr, 0, round_page(msgbufp->msg_size));
273 error = blk_write(di, (char *)vm_page_dump, 0,
274 round_page(BITSET_SIZE(vm_page_dump_pages)));
278 /* Dump kernel page table pages */
279 for (va = KERNBASE; va < kernel_vm_end; va += NBPDR) {
280 /* We always write a page, even if it is zero */
281 pd = IdlePTD; /* always mapped! */
283 if ((pd[j] & (PG_PS | PG_V)) == (PG_PS | PG_V)) {
284 /* This is a single 2M block. Generate a fake PTP */
285 pa = pd[j] & PG_PS_FRAME;
286 for (k = 0; k < NPTEPG; k++) {
287 fakept[k] = (pa + (k * PAGE_SIZE)) | PG_V | PG_RW | PG_A | PG_M;
289 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
292 /* flush, in case we reuse fakept in the same block */
293 error = blk_flush(di);
298 if ((pd[j] & PG_V) == PG_V) {
299 pa = pd[j] & PG_FRAME;
300 error = blk_write(di, 0, pa, PAGE_SIZE);
304 bzero(fakept, sizeof(fakept));
305 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
308 /* flush, in case we reuse fakept in the same block */
309 error = blk_flush(di);
315 /* Dump memory chunks */
316 VM_PAGE_DUMP_FOREACH(pa) {
317 error = blk_write(di, 0, pa, PAGE_SIZE);
322 error = blk_flush(di);
326 error = dump_finish(di, &kdh);
330 printf("\nDump complete\n");
337 if (error == ECANCELED)
338 printf("\nDump aborted\n");
339 else if (error == E2BIG || error == ENOSPC) {
340 printf("\nDump failed. Partition too small (about %lluMB were "
341 "needed this time).\n", (long long)dumpsize >> 20);
343 printf("\n** DUMP FAILED (ERROR %d) **\n", error);