2 * Copyright (c) 2008 Semihalf, Grzegorz Bernacki
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 * from: FreeBSD: src/sys/i386/i386/minidump_machdep.c,v 1.6 2008/08/17 23:27:27
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include "opt_watchdog.h"
34 #include <sys/param.h>
35 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/kerneldump.h>
40 #include <sys/msgbuf.h>
42 #include <sys/watchdog.h>
46 #include <machine/pmap.h>
47 #include <machine/atomic.h>
48 #include <machine/elf.h>
49 #include <machine/md_var.h>
50 #include <machine/vmparam.h>
51 #include <machine/minidump.h>
52 #include <machine/cpufunc.h>
54 CTASSERT(sizeof(struct kerneldumpheader) == 512);
57 * Don't touch the first SIZEOF_METADATA bytes on the dump device. This
58 * is to protect us from metadata and to protect metadata from us.
60 #define SIZEOF_METADATA (64*1024)
62 uint32_t *vm_page_dump;
63 int vm_page_dump_size;
65 static struct kerneldumpheader kdh;
68 /* Handle chunked writes. */
69 static size_t fragsz, offset;
71 static uint64_t counter, progress;
73 CTASSERT(sizeof(*vm_page_dump) == 4);
76 is_dumpable(vm_paddr_t pa)
80 for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
81 if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
87 #define PG2MB(pgs) (((pgs) + (1 << 8) - 1) >> 8)
90 blk_flush(struct dumperinfo *di)
97 error = dump_write(di, (char*)dump_va + offset, 0, dumplo, fragsz - offset);
98 dumplo += (fragsz - offset);
105 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
111 maxdumpsz = di->maxiosize;
113 if (maxdumpsz == 0) /* seatbelt */
114 maxdumpsz = PAGE_SIZE;
118 if (ptr != NULL && pa != 0) {
119 printf("cant have both va and pa!\n");
124 /* If we're doing a virtual dump, flush any pre-existing pa pages */
125 error = blk_flush(di);
132 offset = pa & PAGE_MASK;
135 len = maxdumpsz - fragsz;
142 printf(" %lld", PG2MB(progress >> PAGE_SHIFT));
143 counter &= (1<<22) - 1;
147 wdog_kern_pat(WD_LASTVAL);
150 error = dump_write(di, ptr, 0, dumplo, len);
157 for (i = 0; i < len; i += PAGE_SIZE)
158 dump_va = pmap_kenter_temp(pa + i,
159 (i + fragsz) >> PAGE_SHIFT);
163 if (fragsz == maxdumpsz) {
164 error = blk_flush(di);
170 /* Check for user abort. */
175 printf(" (CTRL-C to abort) ");
182 blk_write_cont(struct dumperinfo *di, vm_paddr_t pa, size_t sz)
186 error = blk_write(di, 0, pa, sz);
190 error = blk_flush(di);
197 /* A fake page table page, to avoid having to handle both 4K and 2M pages */
198 static pt_entry_t fakept[NPTEPG];
201 minidumpsys(struct dumperinfo *di)
203 struct minidumphdr mdhdr;
207 uint32_t pa, prev_pa = 0, count = 0;
210 pt_entry_t *pt, *ptp;
211 int i, k, bit, error;
215 cpu_idcache_wbinv_all();
216 cpu_l2cache_wbinv_all();
219 /* Walk page table pages, set bits in vm_page_dump */
221 for (va = KERNBASE; va < kernel_vm_end; va += NBPDR) {
223 * We always write a page, even if it is zero. Each
224 * page written corresponds to 2MB of space
226 ptesize += L2_TABLE_SIZE_REAL;
227 pmap_get_pde_pte(pmap_kernel(), va, &pdp, &ptp);
228 if (pmap_pde_v(pdp) && pmap_pde_section(pdp)) {
229 /* This is a section mapping 1M page. */
230 pa = (*pdp & L1_S_ADDR_MASK) | (va & ~L1_S_ADDR_MASK);
231 for (k = 0; k < (L1_S_SIZE / PAGE_SIZE); k++) {
238 if (pmap_pde_v(pdp) && pmap_pde_page(pdp)) {
239 /* Set bit for each valid page in this 1MB block */
240 addr = pmap_kenter_temp(*pdp & L1_C_ADDR_MASK, 0);
241 pt = (pt_entry_t*)(addr +
242 (((uint32_t)*pdp & L1_C_ADDR_MASK) & PAGE_MASK));
243 for (k = 0; k < 256; k++) {
244 if ((pt[k] & L2_TYPE_MASK) == L2_TYPE_L) {
245 pa = (pt[k] & L2_L_FRAME) |
247 for (i = 0; i < 16; i++) {
253 } else if ((pt[k] & L2_TYPE_MASK) == L2_TYPE_S) {
254 pa = (pt[k] & L2_S_FRAME) |
261 /* Nothing, we're going to dump a null page */
265 /* Calculate dump size. */
267 dumpsize += round_page(msgbufp->msg_size);
268 dumpsize += round_page(vm_page_dump_size);
270 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
271 bits = vm_page_dump[i];
274 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) +
276 /* Clear out undumpable pages now if needed */
278 dumpsize += PAGE_SIZE;
281 bits &= ~(1ul << bit);
285 dumpsize += PAGE_SIZE;
287 /* Determine dump offset on device. */
288 if (di->mediasize < SIZEOF_METADATA + dumpsize + sizeof(kdh) * 2) {
293 dumplo = di->mediaoffset + di->mediasize - dumpsize;
294 dumplo -= sizeof(kdh) * 2;
297 /* Initialize mdhdr */
298 bzero(&mdhdr, sizeof(mdhdr));
299 strcpy(mdhdr.magic, MINIDUMP_MAGIC);
300 mdhdr.version = MINIDUMP_VERSION;
301 mdhdr.msgbufsize = msgbufp->msg_size;
302 mdhdr.bitmapsize = vm_page_dump_size;
303 mdhdr.ptesize = ptesize;
304 mdhdr.kernbase = KERNBASE;
306 mkdumpheader(&kdh, KERNELDUMPMAGIC, KERNELDUMP_ARM_VERSION, dumpsize,
309 printf("Physical memory: %u MB\n", ptoa((uintmax_t)physmem) / 1048576);
310 printf("Dumping %llu MB:", (long long)dumpsize >> 20);
313 error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
316 dumplo += sizeof(kdh);
319 bzero(&fakept, sizeof(fakept));
320 bcopy(&mdhdr, &fakept, sizeof(mdhdr));
321 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
325 /* Dump msgbuf up front */
326 error = blk_write(di, (char *)msgbufp->msg_ptr, 0, round_page(msgbufp->msg_size));
331 error = blk_write(di, (char *)vm_page_dump, 0,
332 round_page(vm_page_dump_size));
336 /* Dump kernel page table pages */
337 for (va = KERNBASE; va < kernel_vm_end; va += NBPDR) {
338 /* We always write a page, even if it is zero */
339 pmap_get_pde_pte(pmap_kernel(), va, &pdp, &ptp);
341 if (pmap_pde_v(pdp) && pmap_pde_section(pdp)) {
343 error = blk_write_cont(di, prev_pa,
344 count * L2_TABLE_SIZE_REAL);
350 /* This is a single 2M block. Generate a fake PTP */
351 pa = (*pdp & L1_S_ADDR_MASK) | (va & ~L1_S_ADDR_MASK);
352 for (k = 0; k < (L1_S_SIZE / PAGE_SIZE); k++) {
353 fakept[k] = L2_S_PROTO | (pa + (k * PAGE_SIZE)) |
354 L2_S_PROT(PTE_KERNEL,
355 VM_PROT_READ | VM_PROT_WRITE);
357 error = blk_write(di, (char *)&fakept, 0,
361 /* Flush, in case we reuse fakept in the same block */
362 error = blk_flush(di);
367 if (pmap_pde_v(pdp) && pmap_pde_page(pdp)) {
368 pa = *pdp & L1_C_ADDR_MASK;
374 if (pa == (prev_pa + count * L2_TABLE_SIZE_REAL))
377 error = blk_write_cont(di, prev_pa,
378 count * L2_TABLE_SIZE_REAL);
387 error = blk_write_cont(di, prev_pa,
388 count * L2_TABLE_SIZE_REAL);
394 bzero(fakept, sizeof(fakept));
395 error = blk_write(di, (char *)&fakept, 0,
399 /* Flush, in case we reuse fakept in the same block */
400 error = blk_flush(di);
407 error = blk_write_cont(di, prev_pa, count * L2_TABLE_SIZE_REAL);
414 /* Dump memory chunks */
415 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
416 bits = vm_page_dump[i];
419 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) +
425 if (pa == (prev_pa + count * PAGE_SIZE))
428 error = blk_write_cont(di, prev_pa,
436 bits &= ~(1ul << bit);
440 error = blk_write_cont(di, prev_pa, count * PAGE_SIZE);
448 error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
451 dumplo += sizeof(kdh);
453 /* Signal completion, signoff and exit stage left. */
454 dump_write(di, NULL, 0, 0, 0);
455 printf("\nDump complete\n");
462 if (error == ECANCELED)
463 printf("\nDump aborted\n");
464 else if (error == ENOSPC)
465 printf("\nDump failed. Partition too small.\n");
467 printf("\n** DUMP FAILED (ERROR %d) **\n", error);
471 dump_add_page(vm_paddr_t pa)
476 idx = pa >> 5; /* 2^5 = 32 */
478 atomic_set_int(&vm_page_dump[idx], 1ul << bit);
482 dump_drop_page(vm_paddr_t pa)
487 idx = pa >> 5; /* 2^5 = 32 */
489 atomic_clear_int(&vm_page_dump[idx], 1ul << bit);