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);
70 is_dumpable(vm_paddr_t pa)
74 for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
75 if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
81 /* XXX should be MI */
83 mkdumpheader(struct kerneldumpheader *kdh, uint32_t archver, uint64_t dumplen,
87 bzero(kdh, sizeof(*kdh));
88 strncpy(kdh->magic, KERNELDUMPMAGIC, sizeof(kdh->magic));
89 strncpy(kdh->architecture, MACHINE_ARCH, sizeof(kdh->architecture));
90 kdh->version = htod32(KERNELDUMPVERSION);
91 kdh->architectureversion = htod32(archver);
92 kdh->dumplength = htod64(dumplen);
93 kdh->dumptime = htod64(time_second);
94 kdh->blocksize = htod32(blksz);
95 strncpy(kdh->hostname, hostname, sizeof(kdh->hostname));
96 strncpy(kdh->versionstring, version, sizeof(kdh->versionstring));
98 strncpy(kdh->panicstring, panicstr, sizeof(kdh->panicstring));
99 kdh->parity = kerneldump_parity(kdh);
102 #define PG2MB(pgs) (((pgs) + (1 << 8) - 1) >> 8)
105 blk_flush(struct dumperinfo *di)
112 error = dump_write(di, dump_va, 0, dumplo, fragsz);
119 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
125 if ((sz % PAGE_SIZE) != 0) {
126 printf("size not page aligned\n");
129 if (ptr != NULL && pa != 0) {
130 printf("cant have both va and pa!\n");
133 if (pa != 0 && (((uintptr_t)ptr) % PAGE_SIZE) != 0) {
134 printf("address not page aligned\n");
138 /* If we're doing a virtual dump, flush any pre-existing pa pages */
139 error = blk_flush(di);
144 len = (MAXDUMPPGS * PAGE_SIZE) - fragsz;
150 printf(" %lld", PG2MB(progress >> PAGE_SHIFT));
151 counter &= (1<<24) - 1;
154 error = dump_write(di, ptr, 0, dumplo, len);
161 for (i = 0; i < len; i += PAGE_SIZE)
162 dump_va = pmap_kenter_temporary(pa + i, (i + fragsz) >> PAGE_SHIFT);
166 if (fragsz == (MAXDUMPPGS * PAGE_SIZE)) {
167 error = blk_flush(di);
173 /* Check for user abort. */
178 printf(" (CTRL-C to abort) ");
184 /* A fake page table page, to avoid having to handle both 4K and 2M pages */
185 static pt_entry_t fakept[NPTEPG];
188 minidumpsys(struct dumperinfo *di)
199 struct minidumphdr mdhdr;
202 /* Walk page table pages, set bits in vm_page_dump */
204 for (va = KERNBASE; va < kernel_vm_end; va += NBPDR) {
206 * We always write a page, even if it is zero. Each
207 * page written corresponds to 2MB of space
209 ptesize += PAGE_SIZE;
210 pd = (pd_entry_t *)((uintptr_t)IdlePTD + KERNBASE); /* always mapped! */
212 if ((pd[j] & (PG_PS | PG_V)) == (PG_PS | PG_V)) {
213 /* This is an entire 2M page. */
214 pa = pd[j] & PG_PS_FRAME;
215 for (k = 0; k < NPTEPG; k++) {
222 if ((pd[j] & PG_V) == PG_V) {
223 /* set bit for each valid page in this 2MB block */
224 pt = pmap_kenter_temporary(pd[j] & PG_FRAME, 0);
225 for (k = 0; k < NPTEPG; k++) {
226 if ((pt[k] & PG_V) == PG_V) {
227 pa = pt[k] & PG_FRAME;
233 /* nothing, we're going to dump a null page */
237 /* Calculate dump size. */
239 dumpsize += round_page(msgbufp->msg_size);
240 dumpsize += round_page(vm_page_dump_size);
241 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
242 bits = vm_page_dump[i];
245 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
246 /* Clear out undumpable pages now if needed */
247 if (is_dumpable(pa)) {
248 dumpsize += PAGE_SIZE;
252 bits &= ~(1ul << bit);
255 dumpsize += PAGE_SIZE;
257 /* Determine dump offset on device. */
258 if (di->mediasize < SIZEOF_METADATA + dumpsize + sizeof(kdh) * 2) {
262 dumplo = di->mediaoffset + di->mediasize - dumpsize;
263 dumplo -= sizeof(kdh) * 2;
266 /* Initialize mdhdr */
267 bzero(&mdhdr, sizeof(mdhdr));
268 strcpy(mdhdr.magic, MINIDUMP_MAGIC);
269 mdhdr.version = MINIDUMP_VERSION;
270 mdhdr.msgbufsize = msgbufp->msg_size;
271 mdhdr.bitmapsize = vm_page_dump_size;
272 mdhdr.ptesize = ptesize;
273 mdhdr.kernbase = KERNBASE;
278 mkdumpheader(&kdh, KERNELDUMP_I386_VERSION, dumpsize, di->blocksize);
280 printf("Physical memory: %ju MB\n", ptoa((uintmax_t)physmem) / 1048576);
281 printf("Dumping %llu MB:", (long long)dumpsize >> 20);
284 error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
287 dumplo += sizeof(kdh);
290 bzero(&fakept, sizeof(fakept));
291 bcopy(&mdhdr, &fakept, sizeof(mdhdr));
292 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
296 /* Dump msgbuf up front */
297 error = blk_write(di, (char *)msgbufp->msg_ptr, 0, round_page(msgbufp->msg_size));
302 error = blk_write(di, (char *)vm_page_dump, 0, round_page(vm_page_dump_size));
306 /* Dump kernel page table pages */
307 for (va = KERNBASE; va < kernel_vm_end; va += NBPDR) {
308 /* We always write a page, even if it is zero */
309 pd = (pd_entry_t *)((uintptr_t)IdlePTD + KERNBASE); /* always mapped! */
311 if ((pd[j] & (PG_PS | PG_V)) == (PG_PS | PG_V)) {
312 /* This is a single 2M block. Generate a fake PTP */
313 pa = pd[j] & PG_PS_FRAME;
314 for (k = 0; k < NPTEPG; k++) {
315 fakept[k] = (pa + (k * PAGE_SIZE)) | PG_V | PG_RW | PG_A | PG_M;
317 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
320 /* flush, in case we reuse fakept in the same block */
321 error = blk_flush(di);
326 if ((pd[j] & PG_V) == PG_V) {
327 pa = pd[j] & PG_FRAME;
328 error = blk_write(di, 0, pa, PAGE_SIZE);
332 bzero(fakept, sizeof(fakept));
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);
343 /* Dump memory chunks */
344 /* XXX cluster it up and use blk_dump() */
345 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
346 bits = vm_page_dump[i];
349 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
350 error = blk_write(di, 0, pa, PAGE_SIZE);
353 bits &= ~(1ul << bit);
357 error = blk_flush(di);
362 error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
365 dumplo += sizeof(kdh);
367 /* Signal completion, signoff and exit stage left. */
368 dump_write(di, NULL, 0, 0, 0);
369 printf("\nDump complete\n");
376 if (error == ECANCELED)
377 printf("\nDump aborted\n");
378 else if (error == ENOSPC)
379 printf("\nDump failed. Partition too small.\n");
381 printf("\n** DUMP FAILED (ERROR %d) **\n", error);
385 dump_add_page(vm_paddr_t pa)
390 idx = pa >> 5; /* 2^5 = 32 */
392 atomic_set_int(&vm_page_dump[idx], 1ul << bit);
396 dump_drop_page(vm_paddr_t pa)
401 idx = pa >> 5; /* 2^5 = 32 */
403 atomic_clear_int(&vm_page_dump[idx], 1ul << bit);