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;
65 CTASSERT(sizeof(*vm_page_dump) == 4);
68 is_dumpable(vm_paddr_t pa)
72 for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
73 if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
79 #define PG2MB(pgs) (((pgs) + (1 << 8) - 1) >> 8)
82 blk_flush(struct dumperinfo *di)
89 error = dump_append(di, dump_va, 0, fragsz);
95 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
101 maxdumpsz = min(di->maxiosize, MAXDUMPPGS * PAGE_SIZE);
102 if (maxdumpsz == 0) /* seatbelt */
103 maxdumpsz = PAGE_SIZE;
105 if ((sz % PAGE_SIZE) != 0) {
106 printf("size not page aligned\n");
109 if (ptr != NULL && pa != 0) {
110 printf("cant have both va and pa!\n");
113 if (pa != 0 && (((uintptr_t)ptr) % PAGE_SIZE) != 0) {
114 printf("address not page aligned\n");
118 /* If we're doing a virtual dump, flush any pre-existing pa pages */
119 error = blk_flush(di);
124 len = maxdumpsz - fragsz;
130 printf(" %lld", PG2MB(progress >> PAGE_SHIFT));
131 counter &= (1<<24) - 1;
134 wdog_kern_pat(WD_LASTVAL);
137 error = dump_append(di, ptr, 0, len);
143 for (i = 0; i < len; i += PAGE_SIZE)
144 dump_va = pmap_kenter_temporary(pa + i, (i + fragsz) >> PAGE_SHIFT);
148 if (fragsz == maxdumpsz) {
149 error = blk_flush(di);
155 /* Check for user abort. */
160 printf(" (CTRL-C to abort) ");
166 /* A fake page table page, to avoid having to handle both 4K and 2M pages */
167 static pt_entry_t fakept[NPTEPG];
170 #define minidumpsys minidumpsys_pae
171 #define IdlePTD IdlePTD_pae
173 #define minidumpsys minidumpsys_nopae
174 #define IdlePTD IdlePTD_nopae
178 minidumpsys(struct dumperinfo *di)
189 struct minidumphdr mdhdr;
192 /* Walk page table pages, set bits in vm_page_dump */
194 for (va = KERNBASE; va < kernel_vm_end; va += NBPDR) {
196 * We always write a page, even if it is zero. Each
197 * page written corresponds to 2MB of space
199 ptesize += PAGE_SIZE;
200 pd = IdlePTD; /* always mapped! */
202 if ((pd[j] & (PG_PS | PG_V)) == (PG_PS | PG_V)) {
203 /* This is an entire 2M page. */
204 pa = pd[j] & PG_PS_FRAME;
205 for (k = 0; k < NPTEPG; k++) {
212 if ((pd[j] & PG_V) == PG_V) {
213 /* set bit for each valid page in this 2MB block */
214 pt = pmap_kenter_temporary(pd[j] & PG_FRAME, 0);
215 for (k = 0; k < NPTEPG; k++) {
216 if ((pt[k] & PG_V) == PG_V) {
217 pa = pt[k] & PG_FRAME;
223 /* nothing, we're going to dump a null page */
227 /* Calculate dump size. */
229 dumpsize += round_page(msgbufp->msg_size);
230 dumpsize += round_page(vm_page_dump_size);
231 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
232 bits = vm_page_dump[i];
235 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
236 /* Clear out undumpable pages now if needed */
237 if (is_dumpable(pa)) {
238 dumpsize += PAGE_SIZE;
242 bits &= ~(1ul << bit);
245 dumpsize += PAGE_SIZE;
249 /* Initialize mdhdr */
250 bzero(&mdhdr, sizeof(mdhdr));
251 strcpy(mdhdr.magic, MINIDUMP_MAGIC);
252 mdhdr.version = MINIDUMP_VERSION;
253 mdhdr.msgbufsize = msgbufp->msg_size;
254 mdhdr.bitmapsize = vm_page_dump_size;
255 mdhdr.ptesize = ptesize;
256 mdhdr.kernbase = KERNBASE;
257 mdhdr.paemode = pae_mode;
259 dump_init_header(di, &kdh, KERNELDUMPMAGIC, KERNELDUMP_I386_VERSION,
262 error = dump_start(di, &kdh);
266 printf("Physical memory: %ju MB\n", ptoa((uintmax_t)physmem) / 1048576);
267 printf("Dumping %llu MB:", (long long)dumpsize >> 20);
270 bzero(&fakept, sizeof(fakept));
271 bcopy(&mdhdr, &fakept, sizeof(mdhdr));
272 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
276 /* Dump msgbuf up front */
277 error = blk_write(di, (char *)msgbufp->msg_ptr, 0, round_page(msgbufp->msg_size));
282 error = blk_write(di, (char *)vm_page_dump, 0, round_page(vm_page_dump_size));
286 /* Dump kernel page table pages */
287 for (va = KERNBASE; va < kernel_vm_end; va += NBPDR) {
288 /* We always write a page, even if it is zero */
289 pd = IdlePTD; /* always mapped! */
291 if ((pd[j] & (PG_PS | PG_V)) == (PG_PS | PG_V)) {
292 /* This is a single 2M block. Generate a fake PTP */
293 pa = pd[j] & PG_PS_FRAME;
294 for (k = 0; k < NPTEPG; k++) {
295 fakept[k] = (pa + (k * PAGE_SIZE)) | PG_V | PG_RW | PG_A | PG_M;
297 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
300 /* flush, in case we reuse fakept in the same block */
301 error = blk_flush(di);
306 if ((pd[j] & PG_V) == PG_V) {
307 pa = pd[j] & PG_FRAME;
308 error = blk_write(di, 0, pa, PAGE_SIZE);
312 bzero(fakept, sizeof(fakept));
313 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
316 /* flush, in case we reuse fakept in the same block */
317 error = blk_flush(di);
323 /* Dump memory chunks */
324 /* XXX cluster it up and use blk_dump() */
325 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
326 bits = vm_page_dump[i];
329 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
330 error = blk_write(di, 0, pa, PAGE_SIZE);
333 bits &= ~(1ul << bit);
337 error = blk_flush(di);
341 error = dump_finish(di, &kdh);
345 printf("\nDump complete\n");
352 if (error == ECANCELED)
353 printf("\nDump aborted\n");
354 else if (error == E2BIG || error == ENOSPC) {
355 printf("\nDump failed. Partition too small (about %lluMB were "
356 "needed this time).\n", (long long)dumpsize >> 20);
358 printf("\n** DUMP FAILED (ERROR %d) **\n", error);