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 <sys/param.h>
33 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/kerneldump.h>
38 #include <sys/msgbuf.h>
41 #include <machine/pmap.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>
47 #include <machine/cpufunc.h>
49 CTASSERT(sizeof(struct kerneldumpheader) == 512);
52 * Don't touch the first SIZEOF_METADATA bytes on the dump device. This
53 * is to protect us from metadata and to protect metadata from us.
55 #define SIZEOF_METADATA (64*1024)
57 uint32_t *vm_page_dump;
58 int vm_page_dump_size;
60 static struct kerneldumpheader kdh;
63 /* Handle chunked writes. */
64 static size_t fragsz, offset;
66 static uint64_t counter, progress;
68 CTASSERT(sizeof(*vm_page_dump) == 4);
71 is_dumpable(vm_paddr_t pa)
75 for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
76 if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
82 #define PG2MB(pgs) (((pgs) + (1 << 8) - 1) >> 8)
85 blk_flush(struct dumperinfo *di)
92 error = dump_write(di, (char*)dump_va + offset, 0, dumplo, fragsz - offset);
93 dumplo += (fragsz - offset);
100 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
106 maxdumpsz = di->maxiosize;
108 if (maxdumpsz == 0) /* seatbelt */
109 maxdumpsz = PAGE_SIZE;
113 if (ptr != NULL && pa != 0) {
114 printf("cant have both va and pa!\n");
119 /* If we're doing a virtual dump, flush any pre-existing pa pages */
120 error = blk_flush(di);
127 offset = pa & PAGE_MASK;
130 len = maxdumpsz - fragsz;
137 printf(" %lld", PG2MB(progress >> PAGE_SHIFT));
138 counter &= (1<<22) - 1;
142 error = dump_write(di, ptr, 0, dumplo, len);
149 for (i = 0; i < len; i += PAGE_SIZE)
150 dump_va = pmap_kenter_temp(pa + i,
151 (i + fragsz) >> PAGE_SHIFT);
155 if (fragsz == maxdumpsz) {
156 error = blk_flush(di);
162 /* Check for user abort. */
167 printf(" (CTRL-C to abort) ");
174 blk_write_cont(struct dumperinfo *di, vm_paddr_t pa, size_t sz)
178 error = blk_write(di, 0, pa, sz);
182 error = blk_flush(di);
189 /* A fake page table page, to avoid having to handle both 4K and 2M pages */
190 static pt_entry_t fakept[NPTEPG];
193 minidumpsys(struct dumperinfo *di)
195 struct minidumphdr mdhdr;
199 uint32_t pa, prev_pa = 0, count = 0;
202 pt_entry_t *pt, *ptp;
203 int i, k, bit, error;
207 cpu_idcache_wbinv_all();
208 cpu_l2cache_wbinv_all();
211 /* Walk page table pages, set bits in vm_page_dump */
213 for (va = KERNBASE; va < kernel_vm_end; va += NBPDR) {
215 * We always write a page, even if it is zero. Each
216 * page written corresponds to 2MB of space
218 ptesize += L2_TABLE_SIZE_REAL;
219 pmap_get_pde_pte(pmap_kernel(), va, &pdp, &ptp);
220 if (pmap_pde_v(pdp) && pmap_pde_section(pdp)) {
221 /* This is a section mapping 1M page. */
222 pa = (*pdp & L1_S_ADDR_MASK) | (va & ~L1_S_ADDR_MASK);
223 for (k = 0; k < (L1_S_SIZE / PAGE_SIZE); k++) {
230 if (pmap_pde_v(pdp) && pmap_pde_page(pdp)) {
231 /* Set bit for each valid page in this 1MB block */
232 addr = pmap_kenter_temp(*pdp & L1_C_ADDR_MASK, 0);
233 pt = (pt_entry_t*)(addr +
234 (((uint32_t)*pdp & L1_C_ADDR_MASK) & PAGE_MASK));
235 for (k = 0; k < 256; k++) {
236 if ((pt[k] & L2_TYPE_MASK) == L2_TYPE_L) {
237 pa = (pt[k] & L2_L_FRAME) |
239 for (i = 0; i < 16; i++) {
245 } else if ((pt[k] & L2_TYPE_MASK) == L2_TYPE_S) {
246 pa = (pt[k] & L2_S_FRAME) |
253 /* Nothing, we're going to dump a null page */
257 /* Calculate dump size. */
259 dumpsize += round_page(msgbufp->msg_size);
260 dumpsize += round_page(vm_page_dump_size);
262 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
263 bits = vm_page_dump[i];
266 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) +
268 /* Clear out undumpable pages now if needed */
270 dumpsize += PAGE_SIZE;
273 bits &= ~(1ul << bit);
277 dumpsize += PAGE_SIZE;
279 /* Determine dump offset on device. */
280 if (di->mediasize < SIZEOF_METADATA + dumpsize + sizeof(kdh) * 2) {
285 dumplo = di->mediaoffset + di->mediasize - dumpsize;
286 dumplo -= sizeof(kdh) * 2;
289 /* Initialize mdhdr */
290 bzero(&mdhdr, sizeof(mdhdr));
291 strcpy(mdhdr.magic, MINIDUMP_MAGIC);
292 mdhdr.version = MINIDUMP_VERSION;
293 mdhdr.msgbufsize = msgbufp->msg_size;
294 mdhdr.bitmapsize = vm_page_dump_size;
295 mdhdr.ptesize = ptesize;
296 mdhdr.kernbase = KERNBASE;
298 mkdumpheader(&kdh, KERNELDUMPMAGIC, KERNELDUMP_ARM_VERSION, dumpsize,
301 printf("Physical memory: %u MB\n", ptoa((uintmax_t)physmem) / 1048576);
302 printf("Dumping %llu MB:", (long long)dumpsize >> 20);
305 error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
308 dumplo += sizeof(kdh);
311 bzero(&fakept, sizeof(fakept));
312 bcopy(&mdhdr, &fakept, sizeof(mdhdr));
313 error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
317 /* Dump msgbuf up front */
318 error = blk_write(di, (char *)msgbufp->msg_ptr, 0, round_page(msgbufp->msg_size));
323 error = blk_write(di, (char *)vm_page_dump, 0,
324 round_page(vm_page_dump_size));
328 /* Dump kernel page table pages */
329 for (va = KERNBASE; va < kernel_vm_end; va += NBPDR) {
330 /* We always write a page, even if it is zero */
331 pmap_get_pde_pte(pmap_kernel(), va, &pdp, &ptp);
333 if (pmap_pde_v(pdp) && pmap_pde_section(pdp)) {
335 error = blk_write_cont(di, prev_pa,
336 count * L2_TABLE_SIZE_REAL);
342 /* This is a single 2M block. Generate a fake PTP */
343 pa = (*pdp & L1_S_ADDR_MASK) | (va & ~L1_S_ADDR_MASK);
344 for (k = 0; k < (L1_S_SIZE / PAGE_SIZE); k++) {
345 fakept[k] = L2_S_PROTO | (pa + (k * PAGE_SIZE)) |
346 L2_S_PROT(PTE_KERNEL,
347 VM_PROT_READ | VM_PROT_WRITE);
349 error = blk_write(di, (char *)&fakept, 0,
353 /* Flush, in case we reuse fakept in the same block */
354 error = blk_flush(di);
359 if (pmap_pde_v(pdp) && pmap_pde_page(pdp)) {
360 pa = *pdp & L1_C_ADDR_MASK;
366 if (pa == (prev_pa + count * L2_TABLE_SIZE_REAL))
369 error = blk_write_cont(di, prev_pa,
370 count * L2_TABLE_SIZE_REAL);
379 error = blk_write_cont(di, prev_pa,
380 count * L2_TABLE_SIZE_REAL);
386 bzero(fakept, sizeof(fakept));
387 error = blk_write(di, (char *)&fakept, 0,
391 /* Flush, in case we reuse fakept in the same block */
392 error = blk_flush(di);
399 error = blk_write_cont(di, prev_pa, count * L2_TABLE_SIZE_REAL);
406 /* Dump memory chunks */
407 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
408 bits = vm_page_dump[i];
411 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) +
417 if (pa == (prev_pa + count * PAGE_SIZE))
420 error = blk_write_cont(di, prev_pa,
428 bits &= ~(1ul << bit);
432 error = blk_write_cont(di, prev_pa, count * PAGE_SIZE);
440 error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
443 dumplo += sizeof(kdh);
445 /* Signal completion, signoff and exit stage left. */
446 dump_write(di, NULL, 0, 0, 0);
447 printf("\nDump complete\n");
454 if (error == ECANCELED)
455 printf("\nDump aborted\n");
456 else if (error == ENOSPC)
457 printf("\nDump failed. Partition too small.\n");
459 printf("\n** DUMP FAILED (ERROR %d) **\n", error);
463 dump_add_page(vm_paddr_t pa)
468 idx = pa >> 5; /* 2^5 = 32 */
470 atomic_set_int(&vm_page_dump[idx], 1ul << bit);
474 dump_drop_page(vm_paddr_t pa)
479 idx = pa >> 5; /* 2^5 = 32 */
481 atomic_clear_int(&vm_page_dump[idx], 1ul << bit);