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 <sys/param.h>
31 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/kerneldump.h>
36 #include <sys/msgbuf.h>
39 #include <machine/atomic.h>
40 #include <machine/elf.h>
41 #include <machine/md_var.h>
42 #include <machine/vmparam.h>
43 #include <machine/minidump.h>
45 CTASSERT(sizeof(struct kerneldumpheader) == 512);
48 * Don't touch the first SIZEOF_METADATA bytes on the dump device. This
49 * is to protect us from metadata and to protect metadata from us.
51 #define SIZEOF_METADATA (64*1024)
53 #define MD_ALIGN(x) (((off_t)(x) + PAGE_MASK) & ~PAGE_MASK)
54 #define DEV_ALIGN(x) (((off_t)(x) + (DEV_BSIZE-1)) & ~(DEV_BSIZE-1))
56 uint32_t *vm_page_dump;
57 int vm_page_dump_size;
59 static struct kerneldumpheader kdh;
62 /* Handle chunked writes. */
65 static uint64_t counter, progress;
67 CTASSERT(sizeof(*vm_page_dump) == 4);
69 #define xpmap_mtop(x) (x)
70 #define xpmap_ptom(x) (x)
75 is_dumpable(vm_paddr_t pa)
79 for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
80 if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
86 #define PG2MB(pgs) (((pgs) + (1 << 8) - 1) >> 8)
89 blk_flush(struct dumperinfo *di)
96 error = dump_write(di, dump_va, 0, dumplo, fragsz);
103 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
109 maxdumpsz = min(di->maxiosize, MAXDUMPPGS * PAGE_SIZE);
110 if (maxdumpsz == 0) /* seatbelt */
111 maxdumpsz = PAGE_SIZE;
113 if ((sz % PAGE_SIZE) != 0) {
114 printf("size not page aligned\n");
117 if (ptr != NULL && pa != 0) {
118 printf("cant have both va and pa!\n");
121 if (pa != 0 && (((uintptr_t)ptr) % PAGE_SIZE) != 0) {
122 printf("address not page aligned\n");
126 /* If we're doing a virtual dump, flush any pre-existing pa pages */
127 error = blk_flush(di);
132 len = maxdumpsz - fragsz;
138 printf(" %lld", PG2MB(progress >> PAGE_SHIFT));
139 counter &= (1<<24) - 1;
142 error = dump_write(di, ptr, 0, dumplo, len);
149 for (i = 0; i < len; i += PAGE_SIZE)
150 dump_va = pmap_kenter_temporary(pa + i, (i + fragsz) >> PAGE_SHIFT);
154 if (fragsz == maxdumpsz) {
155 error = blk_flush(di);
161 /* Check for user abort. */
166 printf(" (CTRL-C to abort) ");
172 /* A fake page table page, to avoid having to handle both 4K and 2M pages */
173 static pt_entry_t fakept[NPTEPG];
176 minidumpsys(struct dumperinfo *di)
187 struct minidumphdr mdhdr;
190 /* Walk page table pages, set bits in vm_page_dump */
192 for (va = KERNBASE; va < kernel_vm_end; va += NBPDR) {
194 * We always write a page, even if it is zero. Each
195 * page written corresponds to 2MB of space
197 ptesize += PAGE_SIZE;
198 pd = (pd_entry_t *)((uintptr_t)IdlePTD + KERNBASE); /* always mapped! */
200 if ((pd[j] & (PG_PS | PG_V)) == (PG_PS | PG_V)) {
201 /* This is an entire 2M page. */
202 pa = xpmap_mtop(pd[j] & PG_PS_FRAME);
203 for (k = 0; k < NPTEPG; k++) {
210 if ((pd[j] & PG_V) == PG_V) {
211 /* set bit for each valid page in this 2MB block */
212 pt = pmap_kenter_temporary(xpmap_mtop(pd[j] & PG_FRAME), 0);
213 for (k = 0; k < NPTEPG; k++) {
214 if ((pt[k] & PG_V) == PG_V) {
215 pa = xpmap_mtop(pt[k] & PG_FRAME);
221 /* nothing, we're going to dump a null page */
225 /* Calculate dump size. */
227 dumpsize += round_page(msgbufp->msg_size);
228 dumpsize += round_page(vm_page_dump_size);
229 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
230 bits = vm_page_dump[i];
233 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
234 /* Clear out undumpable pages now if needed */
235 if (is_dumpable(pa)) {
236 dumpsize += PAGE_SIZE;
240 bits &= ~(1ul << bit);
243 dumpsize += PAGE_SIZE;
245 /* Determine dump offset on device. */
246 if (di->mediasize < SIZEOF_METADATA + dumpsize + sizeof(kdh) * 2) {
250 dumplo = di->mediaoffset + di->mediasize - dumpsize;
251 dumplo -= sizeof(kdh) * 2;
254 /* Initialize mdhdr */
255 bzero(&mdhdr, sizeof(mdhdr));
256 strcpy(mdhdr.magic, MINIDUMP_MAGIC);
257 mdhdr.version = MINIDUMP_VERSION;
258 mdhdr.msgbufsize = msgbufp->msg_size;
259 mdhdr.bitmapsize = vm_page_dump_size;
260 mdhdr.ptesize = ptesize;
261 mdhdr.kernbase = KERNBASE;
266 mkdumpheader(&kdh, KERNELDUMPMAGIC, KERNELDUMP_I386_VERSION, dumpsize, di->blocksize);
268 printf("Physical memory: %ju MB\n", ptoa((uintmax_t)physmem) / 1048576);
269 printf("Dumping %llu MB:", (long long)dumpsize >> 20);
272 error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
275 dumplo += sizeof(kdh);
278 bzero(&fakept, sizeof(fakept));
279 bcopy(&mdhdr, &fakept, sizeof(mdhdr));
280 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
284 /* Dump msgbuf up front */
285 error = blk_write(di, (char *)msgbufp->msg_ptr, 0, round_page(msgbufp->msg_size));
290 error = blk_write(di, (char *)vm_page_dump, 0, round_page(vm_page_dump_size));
294 /* Dump kernel page table pages */
295 for (va = KERNBASE; va < kernel_vm_end; va += NBPDR) {
296 /* We always write a page, even if it is zero */
297 pd = (pd_entry_t *)((uintptr_t)IdlePTD + KERNBASE); /* always mapped! */
299 if ((pd[j] & (PG_PS | PG_V)) == (PG_PS | PG_V)) {
300 /* This is a single 2M block. Generate a fake PTP */
301 pa = pd[j] & PG_PS_FRAME;
302 for (k = 0; k < NPTEPG; k++) {
303 fakept[k] = (pa + (k * PAGE_SIZE)) | PG_V | PG_RW | PG_A | PG_M;
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);
314 if ((pd[j] & PG_V) == PG_V) {
315 pa = xpmap_mtop(pd[j] & PG_FRAME);
317 error = blk_write(di, 0, pa, PAGE_SIZE);
319 pt = pmap_kenter_temporary(pa, 0);
320 memcpy(fakept, pt, PAGE_SIZE);
321 for (i = 0; i < NPTEPG; i++)
322 fakept[i] = xpmap_mtop(fakept[i]);
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);
330 bzero(fakept, sizeof(fakept));
336 bzero(fakept, sizeof(fakept));
337 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
340 /* flush, in case we reuse fakept in the same block */
341 error = blk_flush(di);
347 /* Dump memory chunks */
348 /* XXX cluster it up and use blk_dump() */
349 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
350 bits = vm_page_dump[i];
353 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
354 error = blk_write(di, 0, pa, PAGE_SIZE);
357 bits &= ~(1ul << bit);
361 error = blk_flush(di);
366 error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
369 dumplo += sizeof(kdh);
371 /* Signal completion, signoff and exit stage left. */
372 dump_write(di, NULL, 0, 0, 0);
373 printf("\nDump complete\n");
380 if (error == ECANCELED)
381 printf("\nDump aborted\n");
382 else if (error == ENOSPC)
383 printf("\nDump failed. Partition too small.\n");
385 printf("\n** DUMP FAILED (ERROR %d) **\n", error);
389 dump_add_page(vm_paddr_t pa)
394 idx = pa >> 5; /* 2^5 = 32 */
396 atomic_set_int(&vm_page_dump[idx], 1ul << bit);
400 dump_drop_page(vm_paddr_t pa)
405 idx = pa >> 5; /* 2^5 = 32 */
407 atomic_clear_int(&vm_page_dump[idx], 1ul << bit);