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 uint32_t *vm_page_dump;
62 int vm_page_dump_size;
64 static struct kerneldumpheader kdh;
67 /* Handle chunked writes. */
70 static uint64_t counter, progress;
72 CTASSERT(sizeof(*vm_page_dump) == 4);
74 #define xpmap_mtop(x) (x)
75 #define xpmap_ptom(x) (x)
80 is_dumpable(vm_paddr_t pa)
84 for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
85 if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
91 #define PG2MB(pgs) (((pgs) + (1 << 8) - 1) >> 8)
94 blk_flush(struct dumperinfo *di)
101 error = dump_write(di, dump_va, 0, dumplo, fragsz);
108 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
114 maxdumpsz = min(di->maxiosize, MAXDUMPPGS * PAGE_SIZE);
115 if (maxdumpsz == 0) /* seatbelt */
116 maxdumpsz = PAGE_SIZE;
118 if ((sz % PAGE_SIZE) != 0) {
119 printf("size not page aligned\n");
122 if (ptr != NULL && pa != 0) {
123 printf("cant have both va and pa!\n");
126 if (pa != 0 && (((uintptr_t)ptr) % PAGE_SIZE) != 0) {
127 printf("address not page aligned\n");
131 /* If we're doing a virtual dump, flush any pre-existing pa pages */
132 error = blk_flush(di);
137 len = maxdumpsz - fragsz;
143 printf(" %lld", PG2MB(progress >> PAGE_SHIFT));
144 counter &= (1<<24) - 1;
147 wdog_kern_pat(WD_LASTVAL);
150 error = dump_write(di, ptr, 0, dumplo, len);
157 for (i = 0; i < len; i += PAGE_SIZE)
158 dump_va = pmap_kenter_temporary(pa + i, (i + fragsz) >> PAGE_SHIFT);
162 if (fragsz == maxdumpsz) {
163 error = blk_flush(di);
169 /* Check for user abort. */
174 printf(" (CTRL-C to abort) ");
180 /* A fake page table page, to avoid having to handle both 4K and 2M pages */
181 static pt_entry_t fakept[NPTEPG];
184 minidumpsys(struct dumperinfo *di)
195 struct minidumphdr mdhdr;
198 /* Walk page table pages, set bits in vm_page_dump */
200 for (va = KERNBASE; va < kernel_vm_end; va += NBPDR) {
202 * We always write a page, even if it is zero. Each
203 * page written corresponds to 2MB of space
205 ptesize += PAGE_SIZE;
206 pd = (pd_entry_t *)((uintptr_t)IdlePTD + KERNBASE); /* always mapped! */
208 if ((pd[j] & (PG_PS | PG_V)) == (PG_PS | PG_V)) {
209 /* This is an entire 2M page. */
210 pa = xpmap_mtop(pd[j] & PG_PS_FRAME);
211 for (k = 0; k < NPTEPG; k++) {
218 if ((pd[j] & PG_V) == PG_V) {
219 /* set bit for each valid page in this 2MB block */
220 pt = pmap_kenter_temporary(xpmap_mtop(pd[j] & PG_FRAME), 0);
221 for (k = 0; k < NPTEPG; k++) {
222 if ((pt[k] & PG_V) == PG_V) {
223 pa = xpmap_mtop(pt[k] & PG_FRAME);
229 /* nothing, we're going to dump a null page */
233 /* Calculate dump size. */
235 dumpsize += round_page(msgbufp->msg_size);
236 dumpsize += round_page(vm_page_dump_size);
237 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
238 bits = vm_page_dump[i];
241 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
242 /* Clear out undumpable pages now if needed */
243 if (is_dumpable(pa)) {
244 dumpsize += PAGE_SIZE;
248 bits &= ~(1ul << bit);
251 dumpsize += PAGE_SIZE;
253 /* Determine dump offset on device. */
254 if (di->mediasize < SIZEOF_METADATA + dumpsize + sizeof(kdh) * 2) {
258 dumplo = di->mediaoffset + di->mediasize - dumpsize;
259 dumplo -= sizeof(kdh) * 2;
262 /* Initialize mdhdr */
263 bzero(&mdhdr, sizeof(mdhdr));
264 strcpy(mdhdr.magic, MINIDUMP_MAGIC);
265 mdhdr.version = MINIDUMP_VERSION;
266 mdhdr.msgbufsize = msgbufp->msg_size;
267 mdhdr.bitmapsize = vm_page_dump_size;
268 mdhdr.ptesize = ptesize;
269 mdhdr.kernbase = KERNBASE;
274 mkdumpheader(&kdh, KERNELDUMPMAGIC, KERNELDUMP_I386_VERSION, dumpsize, di->blocksize);
276 printf("Physical memory: %ju MB\n", ptoa((uintmax_t)physmem) / 1048576);
277 printf("Dumping %llu MB:", (long long)dumpsize >> 20);
280 error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
283 dumplo += sizeof(kdh);
286 bzero(&fakept, sizeof(fakept));
287 bcopy(&mdhdr, &fakept, sizeof(mdhdr));
288 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
292 /* Dump msgbuf up front */
293 error = blk_write(di, (char *)msgbufp->msg_ptr, 0, round_page(msgbufp->msg_size));
298 error = blk_write(di, (char *)vm_page_dump, 0, round_page(vm_page_dump_size));
302 /* Dump kernel page table pages */
303 for (va = KERNBASE; va < kernel_vm_end; va += NBPDR) {
304 /* We always write a page, even if it is zero */
305 pd = (pd_entry_t *)((uintptr_t)IdlePTD + KERNBASE); /* always mapped! */
307 if ((pd[j] & (PG_PS | PG_V)) == (PG_PS | PG_V)) {
308 /* This is a single 2M block. Generate a fake PTP */
309 pa = pd[j] & PG_PS_FRAME;
310 for (k = 0; k < NPTEPG; k++) {
311 fakept[k] = (pa + (k * PAGE_SIZE)) | PG_V | PG_RW | PG_A | PG_M;
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);
322 if ((pd[j] & PG_V) == PG_V) {
323 pa = xpmap_mtop(pd[j] & PG_FRAME);
325 error = blk_write(di, 0, pa, PAGE_SIZE);
327 pt = pmap_kenter_temporary(pa, 0);
328 memcpy(fakept, pt, PAGE_SIZE);
329 for (i = 0; i < NPTEPG; i++)
330 fakept[i] = xpmap_mtop(fakept[i]);
331 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
334 /* flush, in case we reuse fakept in the same block */
335 error = blk_flush(di);
338 bzero(fakept, sizeof(fakept));
344 bzero(fakept, sizeof(fakept));
345 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
348 /* flush, in case we reuse fakept in the same block */
349 error = blk_flush(di);
355 /* Dump memory chunks */
356 /* XXX cluster it up and use blk_dump() */
357 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
358 bits = vm_page_dump[i];
361 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
362 error = blk_write(di, 0, pa, PAGE_SIZE);
365 bits &= ~(1ul << bit);
369 error = blk_flush(di);
374 error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
377 dumplo += sizeof(kdh);
379 /* Signal completion, signoff and exit stage left. */
380 dump_write(di, NULL, 0, 0, 0);
381 printf("\nDump complete\n");
388 if (error == ECANCELED)
389 printf("\nDump aborted\n");
390 else if (error == ENOSPC)
391 printf("\nDump failed. Partition too small.\n");
393 printf("\n** DUMP FAILED (ERROR %d) **\n", error);
397 dump_add_page(vm_paddr_t pa)
402 idx = pa >> 5; /* 2^5 = 32 */
404 atomic_set_int(&vm_page_dump[idx], 1ul << bit);
408 dump_drop_page(vm_paddr_t pa)
413 idx = pa >> 5; /* 2^5 = 32 */
415 atomic_clear_int(&vm_page_dump[idx], 1ul << bit);