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>
39 #include <sys/watchdog.h>
42 #include <machine/atomic.h>
43 #include <machine/elf.h>
44 #include <machine/md_var.h>
45 #include <machine/vmparam.h>
46 #include <machine/minidump.h>
48 CTASSERT(sizeof(struct kerneldumpheader) == 512);
51 * Don't touch the first SIZEOF_METADATA bytes on the dump device. This
52 * is to protect us from metadata and to protect metadata from us.
54 #define SIZEOF_METADATA (64*1024)
56 #define MD_ALIGN(x) (((off_t)(x) + PAGE_MASK) & ~PAGE_MASK)
57 #define DEV_ALIGN(x) (((off_t)(x) + (DEV_BSIZE-1)) & ~(DEV_BSIZE-1))
59 uint32_t *vm_page_dump;
60 int vm_page_dump_size;
62 static struct kerneldumpheader kdh;
65 /* Handle chunked writes. */
68 static uint64_t counter, progress;
70 CTASSERT(sizeof(*vm_page_dump) == 4);
74 is_dumpable(vm_paddr_t pa)
78 for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
79 if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
85 #define PG2MB(pgs) (((pgs) + (1 << 8) - 1) >> 8)
88 blk_flush(struct dumperinfo *di)
95 error = dump_write(di, dump_va, 0, dumplo, fragsz);
102 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
108 maxdumpsz = min(di->maxiosize, MAXDUMPPGS * PAGE_SIZE);
109 if (maxdumpsz == 0) /* seatbelt */
110 maxdumpsz = PAGE_SIZE;
112 if ((sz % PAGE_SIZE) != 0) {
113 printf("size not page aligned\n");
116 if (ptr != NULL && pa != 0) {
117 printf("cant have both va and pa!\n");
120 if (pa != 0 && (((uintptr_t)ptr) % PAGE_SIZE) != 0) {
121 printf("address not page aligned\n");
125 /* If we're doing a virtual dump, flush any pre-existing pa pages */
126 error = blk_flush(di);
131 len = maxdumpsz - fragsz;
137 printf(" %lld", PG2MB(progress >> PAGE_SHIFT));
138 counter &= (1<<24) - 1;
141 wdog_kern_pat(WD_LASTVAL);
144 error = dump_write(di, ptr, 0, dumplo, len);
151 for (i = 0; i < len; i += PAGE_SIZE)
152 dump_va = pmap_kenter_temporary(pa + i, (i + fragsz) >> PAGE_SHIFT);
156 if (fragsz == maxdumpsz) {
157 error = blk_flush(di);
163 /* Check for user abort. */
168 printf(" (CTRL-C to abort) ");
174 /* A fake page table page, to avoid having to handle both 4K and 2M pages */
175 static pt_entry_t fakept[NPTEPG];
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 = (pd_entry_t *)((uintptr_t)IdlePTD + KERNBASE); /* 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;
247 /* Determine dump offset on device. */
248 if (di->mediasize < SIZEOF_METADATA + dumpsize + sizeof(kdh) * 2) {
252 dumplo = di->mediaoffset + di->mediasize - dumpsize;
253 dumplo -= sizeof(kdh) * 2;
256 /* Initialize mdhdr */
257 bzero(&mdhdr, sizeof(mdhdr));
258 strcpy(mdhdr.magic, MINIDUMP_MAGIC);
259 mdhdr.version = MINIDUMP_VERSION;
260 mdhdr.msgbufsize = msgbufp->msg_size;
261 mdhdr.bitmapsize = vm_page_dump_size;
262 mdhdr.ptesize = ptesize;
263 mdhdr.kernbase = KERNBASE;
264 #if defined(PAE) || defined(PAE_TABLES)
268 mkdumpheader(&kdh, KERNELDUMPMAGIC, KERNELDUMP_I386_VERSION, dumpsize, di->blocksize);
270 printf("Physical memory: %ju MB\n", ptoa((uintmax_t)physmem) / 1048576);
271 printf("Dumping %llu MB:", (long long)dumpsize >> 20);
274 error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
277 dumplo += sizeof(kdh);
280 bzero(&fakept, sizeof(fakept));
281 bcopy(&mdhdr, &fakept, sizeof(mdhdr));
282 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
286 /* Dump msgbuf up front */
287 error = blk_write(di, (char *)msgbufp->msg_ptr, 0, round_page(msgbufp->msg_size));
292 error = blk_write(di, (char *)vm_page_dump, 0, round_page(vm_page_dump_size));
296 /* Dump kernel page table pages */
297 for (va = KERNBASE; va < kernel_vm_end; va += NBPDR) {
298 /* We always write a page, even if it is zero */
299 pd = (pd_entry_t *)((uintptr_t)IdlePTD + KERNBASE); /* always mapped! */
301 if ((pd[j] & (PG_PS | PG_V)) == (PG_PS | PG_V)) {
302 /* This is a single 2M block. Generate a fake PTP */
303 pa = pd[j] & PG_PS_FRAME;
304 for (k = 0; k < NPTEPG; k++) {
305 fakept[k] = (pa + (k * PAGE_SIZE)) | PG_V | PG_RW | PG_A | PG_M;
307 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
310 /* flush, in case we reuse fakept in the same block */
311 error = blk_flush(di);
316 if ((pd[j] & PG_V) == PG_V) {
317 pa = pd[j] & PG_FRAME;
318 error = blk_write(di, 0, pa, PAGE_SIZE);
322 bzero(fakept, sizeof(fakept));
323 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
326 /* flush, in case we reuse fakept in the same block */
327 error = blk_flush(di);
333 /* Dump memory chunks */
334 /* XXX cluster it up and use blk_dump() */
335 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
336 bits = vm_page_dump[i];
339 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
340 error = blk_write(di, 0, pa, PAGE_SIZE);
343 bits &= ~(1ul << bit);
347 error = blk_flush(di);
352 error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
355 dumplo += sizeof(kdh);
357 /* Signal completion, signoff and exit stage left. */
358 dump_write(di, NULL, 0, 0, 0);
359 printf("\nDump complete\n");
366 if (error == ECANCELED)
367 printf("\nDump aborted\n");
368 else if (error == ENOSPC)
369 printf("\nDump failed. Partition too small.\n");
371 printf("\n** DUMP FAILED (ERROR %d) **\n", error);
376 dump_add_page(vm_paddr_t pa)
381 idx = pa >> 5; /* 2^5 = 32 */
383 atomic_set_int(&vm_page_dump[idx], 1ul << bit);
387 dump_drop_page(vm_paddr_t pa)
392 idx = pa >> 5; /* 2^5 = 32 */
394 atomic_clear_int(&vm_page_dump[idx], 1ul << bit);