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.
26 * $FreeBSD: src/sys/i386/i386/minidump_machdep.c,v 1.9 2009/05/29 21:27:12 jamie Exp $
29 #include <sys/param.h>
30 #include <sys/systm.h>
33 #include <sys/device.h>
34 #include <sys/globaldata.h>
35 #include <sys/kernel.h>
36 #include <sys/kerneldump.h>
37 #include <sys/msgbuf.h>
39 #include <vm/vm_kern.h>
41 #include <machine/atomic.h>
42 #include <machine/elf.h>
43 #include <machine/globaldata.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);
73 is_dumpable(vm_paddr_t pa)
77 for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
78 if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
84 #define PG2MB(pgs) (((pgs) + (1 << 8) - 1) >> 8)
87 blk_flush(struct dumperinfo *di)
94 error = dev_ddump(di->priv, dump_va, 0, dumplo, fragsz);
101 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
108 if ((sz % PAGE_SIZE) != 0) {
109 kprintf("size not page aligned\n");
112 if (ptr != NULL && pa != 0) {
113 kprintf("can't have both va and pa!\n");
116 if (pa != 0 && (((uintptr_t)ptr) % PAGE_SIZE) != 0) {
117 kprintf("address not page aligned\n");
121 /* If we're doing a virtual dump, flush any pre-existing pa pages */
122 error = blk_flush(di);
126 max_iosize = min(MAXPHYS, di->maxiosize);
128 len = max_iosize - fragsz;
134 kprintf(" %lld", PG2MB(progress >> PAGE_SHIFT));
135 counter &= (1<<24) - 1;
138 error = dev_ddump(di->priv, ptr, 0, dumplo, len);
145 for (i = 0; i < len; i += PAGE_SIZE) {
146 dump_va = pmap_kenter_temporary(pa + i,
147 (i + fragsz) >> PAGE_SHIFT);
153 if (fragsz == max_iosize) {
154 error = blk_flush(di);
160 /* Check for user abort. */
165 kprintf(" (CTRL-C to abort) ");
171 /* A fake page table page, to avoid having to handle both 4K and 2M pages */
172 static pt_entry_t fakept[NPTEPG];
175 minidumpsys(struct dumperinfo *di)
180 vm_offset_t kern_end;
187 struct minidumphdr mdhdr;
188 struct mdglobaldata *md;
193 md = (struct mdglobaldata *)globaldata_find(0);
195 kern_end = kernel_vm_end;
196 if (kern_end < (vm_offset_t)&(md[ncpus]))
197 kern_end = (vm_offset_t)&(md[ncpus]);
199 kern_end = 0xFFFFF000;
202 /* Walk page table pages, set bits in vm_page_dump */
203 for (va = KERNBASE; va < kern_end; va += NBPDR) {
205 * We always write a page, even if it is zero. Each
206 * page written corresponds to 2MB of space
208 ptesize += PAGE_SIZE;
209 pd = (pd_entry_t *)((uintptr_t)IdlePTD + KERNBASE); /* always mapped! */
211 if ((pd[j] & (PG_PS | PG_V)) == (PG_PS | PG_V)) {
212 /* This is an entire 2M page. */
213 pa = pd[j] & PG_FRAME & ~PDRMASK;
214 for (k = 0; k < NPTEPG; k++) {
221 if ((pd[j] & PG_V) == PG_V) {
222 /* set bit for each valid page in this 2MB block */
223 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;
275 mkdumpheader(&kdh, KERNELDUMPMAGIC, KERNELDUMP_I386_VERSION,
276 dumpsize, di->blocksize);
278 kprintf("Physical memory: %jd MB\n", (intmax_t)ptoa(physmem) / 1048576);
279 kprintf("Dumping %jd MB:", (intmax_t)dumpsize >> 20);
282 error = dev_ddump(di->priv, &kdh, 0, dumplo, sizeof(kdh));
285 dumplo += sizeof(kdh);
288 bzero(&fakept, sizeof(fakept));
289 bcopy(&mdhdr, &fakept, sizeof(mdhdr));
290 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
294 /* Dump msgbuf up front */
295 error = blk_write(di, (char *)msgbufp->msg_ptr, 0, round_page(msgbufp->msg_size));
300 error = blk_write(di, (char *)vm_page_dump, 0, round_page(vm_page_dump_size));
304 /* Dump kernel page table pages */
305 for (va = KERNBASE; va < kern_end; va += NBPDR) {
306 /* We always write a page, even if it is zero */
307 pd = (pd_entry_t *)((uintptr_t)IdlePTD + KERNBASE); /* always mapped! */
309 if ((pd[j] & (PG_PS | PG_V)) == (PG_PS | PG_V)) {
310 /* This is a single 2M block. Generate a fake PTP */
311 pa = pd[j] & PG_FRAME & ~PDRMASK;
312 for (k = 0; k < NPTEPG; k++) {
313 fakept[k] = (pa + (k * PAGE_SIZE)) | PG_V | PG_RW | PG_A | PG_M;
315 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
318 /* flush, in case we reuse fakept in the same block */
319 error = blk_flush(di);
324 if ((pd[j] & PG_V) == PG_V) {
325 pa = pd[j] & PG_FRAME;
326 error = blk_write(di, 0, pa, PAGE_SIZE);
330 bzero(fakept, sizeof(fakept));
331 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
334 /* flush, in case we reuse fakept in the same block */
335 error = blk_flush(di);
341 /* Dump memory chunks */
342 /* XXX cluster it up and use blk_dump() */
343 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
344 bits = vm_page_dump[i];
347 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
348 error = blk_write(di, 0, pa, PAGE_SIZE);
351 bits &= ~(1ul << bit);
355 error = blk_flush(di);
360 error = dev_ddump(di->priv, &kdh, 0, dumplo, sizeof(kdh));
363 dumplo += sizeof(kdh);
365 /* Signal completion, signoff and exit stage left. */
366 dev_ddump(di->priv, NULL, 0, 0, 0);
367 kprintf("\nDump complete\n");
374 if (error == ECANCELED)
375 kprintf("\nDump aborted\n");
376 else if (error == ENOSPC)
377 kprintf("\nDump failed. Partition too small.\n");
379 kprintf("\n** DUMP FAILED (ERROR %d) **\n", error);
383 dump_add_page(vm_paddr_t pa)
388 idx = pa >> 5; /* 2^5 = 32 */
390 atomic_set_int(&vm_page_dump[idx], 1ul << bit);
394 dump_drop_page(vm_paddr_t pa)
399 idx = pa >> 5; /* 2^5 = 32 */
401 atomic_clear_int(&vm_page_dump[idx], 1ul << bit);