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 /* XXX should be MI */
85 mkdumpheader(struct kerneldumpheader *kdh, uint32_t archver, uint64_t dumplen,
89 bzero(kdh, sizeof(*kdh));
90 strncpy(kdh->magic, KERNELDUMPMAGIC, sizeof(kdh->magic));
91 strncpy(kdh->architecture, MACHINE_ARCH, sizeof(kdh->architecture));
92 kdh->version = htod32(KERNELDUMPVERSION);
93 kdh->architectureversion = htod32(archver);
94 kdh->dumplength = htod64(dumplen);
95 kdh->dumptime = htod64(time_second);
96 kdh->blocksize = htod32(blksz);
97 strncpy(kdh->hostname, hostname, sizeof(kdh->hostname));
98 strncpy(kdh->versionstring, version, sizeof(kdh->versionstring));
100 strncpy(kdh->panicstring, panicstr, sizeof(kdh->panicstring));
101 kdh->parity = kerneldump_parity(kdh);
104 #define PG2MB(pgs) (((pgs) + (1 << 8) - 1) >> 8)
107 blk_flush(struct dumperinfo *di)
114 error = di->dumper(di->priv, dump_va, 0, dumplo, fragsz);
121 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
127 if ((sz % PAGE_SIZE) != 0) {
128 printf("size not page aligned\n");
131 if (ptr != NULL && pa != 0) {
132 printf("cant have both va and pa!\n");
135 if (pa != 0 && (((uintptr_t)ptr) % PAGE_SIZE) != 0) {
136 printf("address not page aligned\n");
140 /* If we're doing a virtual dump, flush any pre-existing pa pages */
141 error = blk_flush(di);
146 len = (MAXDUMPPGS * PAGE_SIZE) - fragsz;
152 printf(" %ld", PG2MB(progress >> PAGE_SHIFT));
153 counter &= (1<<24) - 1;
156 error = di->dumper(di->priv, ptr, 0, dumplo, len);
163 for (i = 0; i < len; i += PAGE_SIZE)
164 dump_va = pmap_kenter_temporary(pa + i, (i + fragsz) >> PAGE_SHIFT);
168 if (fragsz == (MAXDUMPPGS * PAGE_SIZE)) {
169 error = blk_flush(di);
175 /* Check for user abort. */
180 printf(" (CTRL-C to abort) ");
186 /* A fake page table page, to avoid having to handle both 4K and 2M pages */
187 static pt_entry_t fakept[NPTEPG];
190 minidumpsys(struct dumperinfo *di)
197 uint64_t *pdp, *pd, *pt, pa;
199 struct minidumphdr mdhdr;
202 /* Walk page table pages, set bits in vm_page_dump */
204 pdp = (uint64_t *)PHYS_TO_DMAP(KPDPphys);
205 for (va = KERNBASE; va < kernel_vm_end; va += NBPDR) {
206 i = (va >> PDPSHIFT) & ((1ul << NPDPEPGSHIFT) - 1);
208 * We always write a page, even if it is zero. Each
209 * page written corresponds to 2MB of space
211 ptesize += PAGE_SIZE;
212 if ((pdp[i] & PG_V) == 0)
214 pd = (uint64_t *)PHYS_TO_DMAP(pdp[i] & PG_FRAME);
215 j = ((va >> PDRSHIFT) & ((1ul << NPDEPGSHIFT) - 1));
216 if ((pd[j] & (PG_PS | PG_V)) == (PG_PS | PG_V)) {
217 /* This is an entire 2M page. */
218 pa = pd[j] & PG_PS_FRAME;
219 for (k = 0; k < NPTEPG; k++) {
226 if ((pd[j] & PG_V) == PG_V) {
227 /* set bit for each valid page in this 2MB block */
228 pt = (uint64_t *)PHYS_TO_DMAP(pd[j] & PG_FRAME);
229 for (k = 0; k < NPTEPG; k++) {
230 if ((pt[k] & PG_V) == PG_V) {
231 pa = pt[k] & PG_FRAME;
237 /* nothing, we're going to dump a null page */
241 /* Calculate dump size. */
243 dumpsize += round_page(msgbufp->msg_size);
244 dumpsize += round_page(vm_page_dump_size);
245 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
246 bits = vm_page_dump[i];
249 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
250 /* Clear out undumpable pages now if needed */
251 if (is_dumpable(pa)) {
252 dumpsize += PAGE_SIZE;
256 bits &= ~(1ul << bit);
259 dumpsize += PAGE_SIZE;
261 /* Determine dump offset on device. */
262 if (di->mediasize < SIZEOF_METADATA + dumpsize + sizeof(kdh) * 2) {
266 dumplo = di->mediaoffset + di->mediasize - dumpsize;
267 dumplo -= sizeof(kdh) * 2;
270 /* Initialize mdhdr */
271 bzero(&mdhdr, sizeof(mdhdr));
272 strcpy(mdhdr.magic, MINIDUMP_MAGIC);
273 mdhdr.version = MINIDUMP_VERSION;
274 mdhdr.msgbufsize = msgbufp->msg_size;
275 mdhdr.bitmapsize = vm_page_dump_size;
276 mdhdr.ptesize = ptesize;
277 mdhdr.kernbase = KERNBASE;
278 mdhdr.dmapbase = DMAP_MIN_ADDRESS;
279 mdhdr.dmapend = DMAP_MAX_ADDRESS;
281 mkdumpheader(&kdh, KERNELDUMP_AMD64_VERSION, dumpsize, di->blocksize);
283 printf("Physical memory: %ju MB\n", ptoa((uintmax_t)physmem) / 1048576);
284 printf("Dumping %llu MB:", (long long)dumpsize >> 20);
287 error = di->dumper(di->priv, &kdh, 0, dumplo, sizeof(kdh));
290 dumplo += sizeof(kdh);
293 bzero(&fakept, sizeof(fakept));
294 bcopy(&mdhdr, &fakept, sizeof(mdhdr));
295 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
299 /* Dump msgbuf up front */
300 error = blk_write(di, (char *)msgbufp->msg_ptr, 0, round_page(msgbufp->msg_size));
305 error = blk_write(di, (char *)vm_page_dump, 0, round_page(vm_page_dump_size));
309 /* Dump kernel page table pages */
310 pdp = (uint64_t *)PHYS_TO_DMAP(KPDPphys);
311 for (va = KERNBASE; va < kernel_vm_end; va += NBPDR) {
312 i = (va >> PDPSHIFT) & ((1ul << NPDPEPGSHIFT) - 1);
313 /* We always write a page, even if it is zero */
314 if ((pdp[i] & PG_V) == 0) {
315 bzero(fakept, sizeof(fakept));
316 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
319 /* flush, in case we reuse fakept in the same block */
320 error = blk_flush(di);
325 pd = (uint64_t *)PHYS_TO_DMAP(pdp[i] & PG_FRAME);
326 j = ((va >> PDRSHIFT) & ((1ul << NPDEPGSHIFT) - 1));
327 if ((pd[j] & (PG_PS | PG_V)) == (PG_PS | PG_V)) {
328 /* This is a single 2M block. Generate a fake PTP */
329 pa = pd[j] & PG_PS_FRAME;
330 for (k = 0; k < NPTEPG; k++) {
331 fakept[k] = (pa + (k * PAGE_SIZE)) | PG_V | PG_RW | PG_A | PG_M;
333 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
336 /* flush, in case we reuse fakept in the same block */
337 error = blk_flush(di);
342 if ((pd[j] & PG_V) == PG_V) {
343 pt = (uint64_t *)PHYS_TO_DMAP(pd[j] & PG_FRAME);
344 error = blk_write(di, (char *)pt, 0, PAGE_SIZE);
348 bzero(fakept, sizeof(fakept));
349 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
352 /* flush, in case we reuse fakept in the same block */
353 error = blk_flush(di);
359 /* Dump memory chunks */
360 /* XXX cluster it up and use blk_dump() */
361 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
362 bits = vm_page_dump[i];
365 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
366 error = blk_write(di, 0, pa, PAGE_SIZE);
369 bits &= ~(1ul << bit);
373 error = blk_flush(di);
378 error = di->dumper(di->priv, &kdh, 0, dumplo, sizeof(kdh));
381 dumplo += sizeof(kdh);
383 /* Signal completion, signoff and exit stage left. */
384 di->dumper(di->priv, NULL, 0, 0, 0);
385 printf("\nDump complete\n");
392 if (error == ECANCELED)
393 printf("\nDump aborted\n");
394 else if (error == ENOSPC)
395 printf("\nDump failed. Partition too small.\n");
397 printf("\n** DUMP FAILED (ERROR %d) **\n", error);
401 dump_add_page(vm_paddr_t pa)
406 idx = pa >> 6; /* 2^6 = 64 */
408 atomic_set_long(&vm_page_dump[idx], 1ul << bit);
412 dump_drop_page(vm_paddr_t pa)
417 idx = pa >> 6; /* 2^6 = 64 */
419 atomic_clear_long(&vm_page_dump[idx], 1ul << bit);