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 extern uint64_t KPDPphys;
58 uint64_t *vm_page_dump;
59 int vm_page_dump_size;
61 static struct kerneldumpheader kdh;
64 /* Handle chunked writes. */
67 static size_t counter, progress;
69 CTASSERT(sizeof(*vm_page_dump) == 8);
72 is_dumpable(vm_paddr_t pa)
76 for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
77 if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
83 #define PG2MB(pgs) (((pgs) + (1 << 8) - 1) >> 8)
86 blk_flush(struct dumperinfo *di)
93 error = dump_write(di, dump_va, 0, dumplo, fragsz);
100 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
106 maxdumpsz = min(di->maxiosize, MAXDUMPPGS * PAGE_SIZE);
107 if (maxdumpsz == 0) /* seatbelt */
108 maxdumpsz = PAGE_SIZE;
110 if ((sz % PAGE_SIZE) != 0) {
111 printf("size not page aligned\n");
114 if (ptr != NULL && pa != 0) {
115 printf("cant have both va and pa!\n");
118 if (pa != 0 && (((uintptr_t)ptr) % PAGE_SIZE) != 0) {
119 printf("address not page aligned\n");
123 /* If we're doing a virtual dump, flush any pre-existing pa pages */
124 error = blk_flush(di);
129 len = maxdumpsz - fragsz;
135 printf(" %ld", PG2MB(progress >> PAGE_SHIFT));
136 counter &= (1<<24) - 1;
139 error = dump_write(di, ptr, 0, dumplo, len);
146 for (i = 0; i < len; i += PAGE_SIZE)
147 dump_va = pmap_kenter_temporary(pa + i, (i + fragsz) >> PAGE_SHIFT);
151 if (fragsz == maxdumpsz) {
152 error = blk_flush(di);
158 /* Check for user abort. */
163 printf(" (CTRL-C to abort) ");
169 /* A fake page table page, to avoid having to handle both 4K and 2M pages */
170 static pt_entry_t fakept[NPTEPG];
173 minidumpsys(struct dumperinfo *di)
180 uint64_t *pdp, *pd, *pt, pa;
182 struct minidumphdr mdhdr;
185 /* Walk page table pages, set bits in vm_page_dump */
187 pdp = (uint64_t *)PHYS_TO_DMAP(KPDPphys);
188 for (va = VM_MIN_KERNEL_ADDRESS; va < MAX(KERNBASE + NKPT * NBPDR,
189 kernel_vm_end); va += NBPDR) {
190 i = (va >> PDPSHIFT) & ((1ul << NPDPEPGSHIFT) - 1);
192 * We always write a page, even if it is zero. Each
193 * page written corresponds to 2MB of space
195 ptesize += PAGE_SIZE;
196 if ((pdp[i] & PG_V) == 0)
198 pd = (uint64_t *)PHYS_TO_DMAP(pdp[i] & PG_FRAME);
199 j = ((va >> PDRSHIFT) & ((1ul << NPDEPGSHIFT) - 1));
200 if ((pd[j] & (PG_PS | PG_V)) == (PG_PS | PG_V)) {
201 /* This is an entire 2M page. */
202 pa = 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 = (uint64_t *)PHYS_TO_DMAP(pd[j] & PG_FRAME);
213 for (k = 0; k < NPTEPG; k++) {
214 if ((pt[k] & PG_V) == PG_V) {
215 pa = 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 = VM_MIN_KERNEL_ADDRESS;
262 mdhdr.dmapbase = DMAP_MIN_ADDRESS;
263 mdhdr.dmapend = DMAP_MAX_ADDRESS;
265 mkdumpheader(&kdh, KERNELDUMPMAGIC, KERNELDUMP_AMD64_VERSION, dumpsize, di->blocksize);
267 printf("Physical memory: %ju MB\n", ptoa((uintmax_t)physmem) / 1048576);
268 printf("Dumping %llu MB:", (long long)dumpsize >> 20);
271 error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
274 dumplo += sizeof(kdh);
277 bzero(&fakept, sizeof(fakept));
278 bcopy(&mdhdr, &fakept, sizeof(mdhdr));
279 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
283 /* Dump msgbuf up front */
284 error = blk_write(di, (char *)msgbufp->msg_ptr, 0, round_page(msgbufp->msg_size));
289 error = blk_write(di, (char *)vm_page_dump, 0, round_page(vm_page_dump_size));
293 /* Dump kernel page table pages */
294 pdp = (uint64_t *)PHYS_TO_DMAP(KPDPphys);
295 for (va = VM_MIN_KERNEL_ADDRESS; va < MAX(KERNBASE + NKPT * NBPDR,
296 kernel_vm_end); va += NBPDR) {
297 i = (va >> PDPSHIFT) & ((1ul << NPDPEPGSHIFT) - 1);
298 /* We always write a page, even if it is zero */
299 if ((pdp[i] & PG_V) == 0) {
300 bzero(fakept, sizeof(fakept));
301 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
304 /* flush, in case we reuse fakept in the same block */
305 error = blk_flush(di);
310 pd = (uint64_t *)PHYS_TO_DMAP(pdp[i] & PG_FRAME);
311 j = ((va >> PDRSHIFT) & ((1ul << NPDEPGSHIFT) - 1));
312 if ((pd[j] & (PG_PS | PG_V)) == (PG_PS | PG_V)) {
313 /* This is a single 2M block. Generate a fake PTP */
314 pa = pd[j] & PG_PS_FRAME;
315 for (k = 0; k < NPTEPG; k++) {
316 fakept[k] = (pa + (k * PAGE_SIZE)) | PG_V | PG_RW | PG_A | PG_M;
318 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
321 /* flush, in case we reuse fakept in the same block */
322 error = blk_flush(di);
327 if ((pd[j] & PG_V) == PG_V) {
328 pt = (uint64_t *)PHYS_TO_DMAP(pd[j] & PG_FRAME);
329 error = blk_write(di, (char *)pt, 0, PAGE_SIZE);
333 bzero(fakept, sizeof(fakept));
334 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
337 /* flush, in case we reuse fakept in the same block */
338 error = blk_flush(di);
344 /* Dump memory chunks */
345 /* XXX cluster it up and use blk_dump() */
346 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
347 bits = vm_page_dump[i];
350 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
351 error = blk_write(di, 0, pa, PAGE_SIZE);
354 bits &= ~(1ul << bit);
358 error = blk_flush(di);
363 error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
366 dumplo += sizeof(kdh);
368 /* Signal completion, signoff and exit stage left. */
369 dump_write(di, NULL, 0, 0, 0);
370 printf("\nDump complete\n");
377 if (error == ECANCELED)
378 printf("\nDump aborted\n");
379 else if (error == ENOSPC)
380 printf("\nDump failed. Partition too small.\n");
382 printf("\n** DUMP FAILED (ERROR %d) **\n", error);
386 dump_add_page(vm_paddr_t pa)
391 idx = pa >> 6; /* 2^6 = 64 */
393 atomic_set_long(&vm_page_dump[idx], 1ul << bit);
397 dump_drop_page(vm_paddr_t pa)
402 idx = pa >> 6; /* 2^6 = 64 */
404 atomic_clear_long(&vm_page_dump[idx], 1ul << bit);