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);
72 #define xpmap_mtop(x) (x)
73 #define xpmap_ptom(x) (x)
78 is_dumpable(vm_paddr_t pa)
82 for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
83 if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
89 #define PG2MB(pgs) (((pgs) + (1 << 8) - 1) >> 8)
92 blk_flush(struct dumperinfo *di)
99 error = dump_write(di, dump_va, 0, dumplo, fragsz);
106 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
112 maxdumpsz = min(di->maxiosize, MAXDUMPPGS * PAGE_SIZE);
113 if (maxdumpsz == 0) /* seatbelt */
114 maxdumpsz = PAGE_SIZE;
116 if ((sz % PAGE_SIZE) != 0) {
117 printf("size not page aligned\n");
120 if (ptr != NULL && pa != 0) {
121 printf("cant have both va and pa!\n");
124 if (pa != 0 && (((uintptr_t)ptr) % PAGE_SIZE) != 0) {
125 printf("address not page aligned\n");
129 /* If we're doing a virtual dump, flush any pre-existing pa pages */
130 error = blk_flush(di);
135 len = maxdumpsz - fragsz;
141 printf(" %lld", PG2MB(progress >> PAGE_SHIFT));
142 counter &= (1<<24) - 1;
145 wdog_kern_pat(WD_LASTVAL);
148 error = dump_write(di, ptr, 0, dumplo, len);
155 for (i = 0; i < len; i += PAGE_SIZE)
156 dump_va = pmap_kenter_temporary(pa + i, (i + fragsz) >> PAGE_SHIFT);
160 if (fragsz == maxdumpsz) {
161 error = blk_flush(di);
167 /* Check for user abort. */
172 printf(" (CTRL-C to abort) ");
178 /* A fake page table page, to avoid having to handle both 4K and 2M pages */
179 static pt_entry_t fakept[NPTEPG];
182 minidumpsys(struct dumperinfo *di)
193 struct minidumphdr mdhdr;
196 /* Walk page table pages, set bits in vm_page_dump */
198 for (va = KERNBASE; va < kernel_vm_end; va += NBPDR) {
200 * We always write a page, even if it is zero. Each
201 * page written corresponds to 2MB of space
203 ptesize += PAGE_SIZE;
204 pd = (pd_entry_t *)((uintptr_t)IdlePTD + KERNBASE); /* always mapped! */
206 if ((pd[j] & (PG_PS | PG_V)) == (PG_PS | PG_V)) {
207 /* This is an entire 2M page. */
208 pa = xpmap_mtop(pd[j] & PG_PS_FRAME);
209 for (k = 0; k < NPTEPG; k++) {
216 if ((pd[j] & PG_V) == PG_V) {
217 /* set bit for each valid page in this 2MB block */
218 pt = pmap_kenter_temporary(xpmap_mtop(pd[j] & PG_FRAME), 0);
219 for (k = 0; k < NPTEPG; k++) {
220 if ((pt[k] & PG_V) == PG_V) {
221 pa = xpmap_mtop(pt[k] & PG_FRAME);
227 /* nothing, we're going to dump a null page */
231 /* Calculate dump size. */
233 dumpsize += round_page(msgbufp->msg_size);
234 dumpsize += round_page(vm_page_dump_size);
235 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
236 bits = vm_page_dump[i];
239 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
240 /* Clear out undumpable pages now if needed */
241 if (is_dumpable(pa)) {
242 dumpsize += PAGE_SIZE;
246 bits &= ~(1ul << bit);
249 dumpsize += PAGE_SIZE;
251 /* Determine dump offset on device. */
252 if (di->mediasize < SIZEOF_METADATA + dumpsize + sizeof(kdh) * 2) {
256 dumplo = di->mediaoffset + di->mediasize - dumpsize;
257 dumplo -= sizeof(kdh) * 2;
260 /* Initialize mdhdr */
261 bzero(&mdhdr, sizeof(mdhdr));
262 strcpy(mdhdr.magic, MINIDUMP_MAGIC);
263 mdhdr.version = MINIDUMP_VERSION;
264 mdhdr.msgbufsize = msgbufp->msg_size;
265 mdhdr.bitmapsize = vm_page_dump_size;
266 mdhdr.ptesize = ptesize;
267 mdhdr.kernbase = KERNBASE;
268 #if defined(PAE) || defined(PAE_TABLES)
272 mkdumpheader(&kdh, KERNELDUMPMAGIC, KERNELDUMP_I386_VERSION, dumpsize, di->blocksize);
274 printf("Physical memory: %ju MB\n", ptoa((uintmax_t)physmem) / 1048576);
275 printf("Dumping %llu MB:", (long long)dumpsize >> 20);
278 error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
281 dumplo += sizeof(kdh);
284 bzero(&fakept, sizeof(fakept));
285 bcopy(&mdhdr, &fakept, sizeof(mdhdr));
286 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
290 /* Dump msgbuf up front */
291 error = blk_write(di, (char *)msgbufp->msg_ptr, 0, round_page(msgbufp->msg_size));
296 error = blk_write(di, (char *)vm_page_dump, 0, round_page(vm_page_dump_size));
300 /* Dump kernel page table pages */
301 for (va = KERNBASE; va < kernel_vm_end; va += NBPDR) {
302 /* We always write a page, even if it is zero */
303 pd = (pd_entry_t *)((uintptr_t)IdlePTD + KERNBASE); /* always mapped! */
305 if ((pd[j] & (PG_PS | PG_V)) == (PG_PS | PG_V)) {
306 /* This is a single 2M block. Generate a fake PTP */
307 pa = pd[j] & PG_PS_FRAME;
308 for (k = 0; k < NPTEPG; k++) {
309 fakept[k] = (pa + (k * PAGE_SIZE)) | PG_V | PG_RW | PG_A | PG_M;
311 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
314 /* flush, in case we reuse fakept in the same block */
315 error = blk_flush(di);
320 if ((pd[j] & PG_V) == PG_V) {
321 pa = xpmap_mtop(pd[j] & PG_FRAME);
323 error = blk_write(di, 0, pa, PAGE_SIZE);
325 pt = pmap_kenter_temporary(pa, 0);
326 memcpy(fakept, pt, PAGE_SIZE);
327 for (i = 0; i < NPTEPG; i++)
328 fakept[i] = xpmap_mtop(fakept[i]);
329 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
332 /* flush, in case we reuse fakept in the same block */
333 error = blk_flush(di);
336 bzero(fakept, sizeof(fakept));
342 bzero(fakept, sizeof(fakept));
343 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
346 /* flush, in case we reuse fakept in the same block */
347 error = blk_flush(di);
353 /* Dump memory chunks */
354 /* XXX cluster it up and use blk_dump() */
355 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
356 bits = vm_page_dump[i];
359 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
360 error = blk_write(di, 0, pa, PAGE_SIZE);
363 bits &= ~(1ul << bit);
367 error = blk_flush(di);
372 error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
375 dumplo += sizeof(kdh);
377 /* Signal completion, signoff and exit stage left. */
378 dump_write(di, NULL, 0, 0, 0);
379 printf("\nDump complete\n");
386 if (error == ECANCELED)
387 printf("\nDump aborted\n");
388 else if (error == ENOSPC)
389 printf("\nDump failed. Partition too small.\n");
391 printf("\n** DUMP FAILED (ERROR %d) **\n", error);
396 dump_add_page(vm_paddr_t pa)
401 idx = pa >> 5; /* 2^5 = 32 */
403 atomic_set_int(&vm_page_dump[idx], 1ul << bit);
407 dump_drop_page(vm_paddr_t pa)
412 idx = pa >> 5; /* 2^5 = 32 */
414 atomic_clear_int(&vm_page_dump[idx], 1ul << bit);