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 "opt_watchdog.h"
32 #include <sys/param.h>
33 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/kerneldump.h>
38 #include <sys/msgbuf.h>
39 #include <sys/watchdog.h>
42 #include <machine/atomic.h>
43 #include <machine/elf.h>
44 #include <machine/md_var.h>
45 #include <machine/vmparam.h>
46 #include <machine/minidump.h>
48 CTASSERT(sizeof(struct kerneldumpheader) == 512);
50 #define MD_ALIGN(x) (((off_t)(x) + PAGE_MASK) & ~PAGE_MASK)
51 #define DEV_ALIGN(x) roundup2((off_t)(x), DEV_BSIZE)
53 uint32_t *vm_page_dump;
54 int vm_page_dump_size;
56 static struct kerneldumpheader kdh;
59 /* Handle chunked writes. */
62 static uint64_t counter, progress;
64 CTASSERT(sizeof(*vm_page_dump) == 4);
68 is_dumpable(vm_paddr_t pa)
72 for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
73 if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
79 #define PG2MB(pgs) (((pgs) + (1 << 8) - 1) >> 8)
82 blk_flush(struct dumperinfo *di)
89 error = dump_write(di, dump_va, 0, dumplo, fragsz);
96 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
102 maxdumpsz = min(di->maxiosize, MAXDUMPPGS * PAGE_SIZE);
103 if (maxdumpsz == 0) /* seatbelt */
104 maxdumpsz = PAGE_SIZE;
106 if ((sz % PAGE_SIZE) != 0) {
107 printf("size not page aligned\n");
110 if (ptr != NULL && pa != 0) {
111 printf("cant have both va and pa!\n");
114 if (pa != 0 && (((uintptr_t)ptr) % PAGE_SIZE) != 0) {
115 printf("address not page aligned\n");
119 /* If we're doing a virtual dump, flush any pre-existing pa pages */
120 error = blk_flush(di);
125 len = maxdumpsz - fragsz;
131 printf(" %lld", PG2MB(progress >> PAGE_SHIFT));
132 counter &= (1<<24) - 1;
135 wdog_kern_pat(WD_LASTVAL);
138 error = dump_write(di, ptr, 0, dumplo, len);
145 for (i = 0; i < len; i += PAGE_SIZE)
146 dump_va = pmap_kenter_temporary(pa + i, (i + fragsz) >> PAGE_SHIFT);
150 if (fragsz == maxdumpsz) {
151 error = blk_flush(di);
157 /* Check for user abort. */
162 printf(" (CTRL-C to abort) ");
168 /* A fake page table page, to avoid having to handle both 4K and 2M pages */
169 static pt_entry_t fakept[NPTEPG];
172 minidumpsys(struct dumperinfo *di)
183 struct minidumphdr mdhdr;
186 /* Walk page table pages, set bits in vm_page_dump */
188 for (va = KERNBASE; va < kernel_vm_end; va += NBPDR) {
190 * We always write a page, even if it is zero. Each
191 * page written corresponds to 2MB of space
193 ptesize += PAGE_SIZE;
194 pd = (pd_entry_t *)((uintptr_t)IdlePTD + KERNBASE); /* always mapped! */
196 if ((pd[j] & (PG_PS | PG_V)) == (PG_PS | PG_V)) {
197 /* This is an entire 2M page. */
198 pa = pd[j] & PG_PS_FRAME;
199 for (k = 0; k < NPTEPG; k++) {
206 if ((pd[j] & PG_V) == PG_V) {
207 /* set bit for each valid page in this 2MB block */
208 pt = pmap_kenter_temporary(pd[j] & PG_FRAME, 0);
209 for (k = 0; k < NPTEPG; k++) {
210 if ((pt[k] & PG_V) == PG_V) {
211 pa = pt[k] & PG_FRAME;
217 /* nothing, we're going to dump a null page */
221 /* Calculate dump size. */
223 dumpsize += round_page(msgbufp->msg_size);
224 dumpsize += round_page(vm_page_dump_size);
225 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
226 bits = vm_page_dump[i];
229 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
230 /* Clear out undumpable pages now if needed */
231 if (is_dumpable(pa)) {
232 dumpsize += PAGE_SIZE;
236 bits &= ~(1ul << bit);
239 dumpsize += PAGE_SIZE;
243 /* Initialize mdhdr */
244 bzero(&mdhdr, sizeof(mdhdr));
245 strcpy(mdhdr.magic, MINIDUMP_MAGIC);
246 mdhdr.version = MINIDUMP_VERSION;
247 mdhdr.msgbufsize = msgbufp->msg_size;
248 mdhdr.bitmapsize = vm_page_dump_size;
249 mdhdr.ptesize = ptesize;
250 mdhdr.kernbase = KERNBASE;
251 #if defined(PAE) || defined(PAE_TABLES)
255 dump_init_header(di, &kdh, KERNELDUMPMAGIC, KERNELDUMP_I386_VERSION,
258 printf("Physical memory: %ju MB\n", ptoa((uintmax_t)physmem) / 1048576);
259 printf("Dumping %llu MB:", (long long)dumpsize >> 20);
261 error = dump_start(di, &kdh, &dumplo);
266 bzero(&fakept, sizeof(fakept));
267 bcopy(&mdhdr, &fakept, sizeof(mdhdr));
268 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
272 /* Dump msgbuf up front */
273 error = blk_write(di, (char *)msgbufp->msg_ptr, 0, round_page(msgbufp->msg_size));
278 error = blk_write(di, (char *)vm_page_dump, 0, round_page(vm_page_dump_size));
282 /* Dump kernel page table pages */
283 for (va = KERNBASE; va < kernel_vm_end; va += NBPDR) {
284 /* We always write a page, even if it is zero */
285 pd = (pd_entry_t *)((uintptr_t)IdlePTD + KERNBASE); /* always mapped! */
287 if ((pd[j] & (PG_PS | PG_V)) == (PG_PS | PG_V)) {
288 /* This is a single 2M block. Generate a fake PTP */
289 pa = pd[j] & PG_PS_FRAME;
290 for (k = 0; k < NPTEPG; k++) {
291 fakept[k] = (pa + (k * PAGE_SIZE)) | PG_V | PG_RW | PG_A | PG_M;
293 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
296 /* flush, in case we reuse fakept in the same block */
297 error = blk_flush(di);
302 if ((pd[j] & PG_V) == PG_V) {
303 pa = pd[j] & PG_FRAME;
304 error = blk_write(di, 0, pa, PAGE_SIZE);
308 bzero(fakept, sizeof(fakept));
309 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
312 /* flush, in case we reuse fakept in the same block */
313 error = blk_flush(di);
319 /* Dump memory chunks */
320 /* XXX cluster it up and use blk_dump() */
321 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
322 bits = vm_page_dump[i];
325 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
326 error = blk_write(di, 0, pa, PAGE_SIZE);
329 bits &= ~(1ul << bit);
333 error = blk_flush(di);
337 error = dump_finish(di, &kdh, dumplo);
341 printf("\nDump complete\n");
348 if (error == ECANCELED)
349 printf("\nDump aborted\n");
350 else if (error == E2BIG || error == ENOSPC)
351 printf("\nDump failed. Partition too small.\n");
353 printf("\n** DUMP FAILED (ERROR %d) **\n", error);
358 dump_add_page(vm_paddr_t pa)
363 idx = pa >> 5; /* 2^5 = 32 */
365 atomic_set_int(&vm_page_dump[idx], 1ul << bit);
369 dump_drop_page(vm_paddr_t pa)
374 idx = pa >> 5; /* 2^5 = 32 */
376 atomic_clear_int(&vm_page_dump[idx], 1ul << bit);