2 * Copyright (c) 2010 Oleksandr Tymoshenko <gonzo@freebsd.org>
3 * Copyright (c) 2008 Semihalf, Grzegorz Bernacki
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * from: FreeBSD: src/sys/arm/arm/minidump_machdep.c v214223
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/kerneldump.h>
39 #include <sys/msgbuf.h>
40 #include <sys/watchdog.h>
41 #include <sys/vmmeter.h>
44 #include <vm/vm_page.h>
45 #include <vm/vm_phys.h>
46 #include <machine/atomic.h>
47 #include <machine/elf.h>
48 #include <machine/md_var.h>
49 #include <machine/vmparam.h>
50 #include <machine/minidump.h>
51 #include <machine/cache.h>
53 CTASSERT(sizeof(struct kerneldumpheader) == 512);
55 uint32_t *vm_page_dump;
56 int vm_page_dump_size;
58 static struct kerneldumpheader kdh;
61 /* Handle chunked writes. */
62 static uint64_t counter, progress, dumpsize;
63 /* Just auxiliary bufffer */
64 static char tmpbuffer[PAGE_SIZE];
66 extern pd_entry_t *kernel_segmap;
68 CTASSERT(sizeof(*vm_page_dump) == 4);
71 is_dumpable(vm_paddr_t pa)
76 if ((m = vm_phys_paddr_to_vm_page(pa)) != NULL)
77 return ((m->flags & PG_NODUMP) == 0);
78 for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
79 if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
86 dump_add_page(vm_paddr_t pa)
91 idx = pa >> 5; /* 2^5 = 32 */
93 atomic_set_int(&vm_page_dump[idx], 1ul << bit);
97 dump_drop_page(vm_paddr_t pa)
102 idx = pa >> 5; /* 2^5 = 32 */
104 atomic_clear_int(&vm_page_dump[idx], 1ul << bit);
111 } progress_track[10] = {
125 report_progress(uint64_t progress, uint64_t dumpsize)
129 sofar = 100 - ((progress * 100) / dumpsize);
130 for (i = 0; i < nitems(progress_track); i++) {
131 if (sofar < progress_track[i].min_per ||
132 sofar > progress_track[i].max_per)
134 if (progress_track[i].visited)
136 progress_track[i].visited = 1;
137 printf("..%d%%", sofar);
143 write_buffer(struct dumperinfo *di, char *ptr, size_t sz)
149 maxdumpsz = di->maxiosize;
151 if (maxdumpsz == 0) /* seatbelt */
152 maxdumpsz = PAGE_SIZE;
157 len = min(maxdumpsz, sz);
162 report_progress(progress, dumpsize);
163 counter &= (1<<22) - 1;
166 wdog_kern_pat(WD_LASTVAL);
169 error = dump_write(di, ptr, 0, dumplo, len);
176 panic("pa is not supported");
179 /* Check for user abort. */
184 printf(" (CTRL-C to abort) ");
191 minidumpsys(struct dumperinfo *di)
193 struct minidumphdr mdhdr;
197 vm_offset_t prev_pte = 0;
205 mips_dcache_wbinv_all();
208 /* Walk page table pages, set bits in vm_page_dump */
211 for (va = VM_MIN_KERNEL_ADDRESS; va < kernel_vm_end; va += NBPDR) {
212 ptesize += PAGE_SIZE;
213 pte = pmap_pte(kernel_pmap, va);
214 KASSERT(pte != NULL, ("pte for %jx is NULL", (uintmax_t)va));
215 for (i = 0; i < NPTEPG; i++) {
216 if (pte_test(&pte[i], PTE_V)) {
217 pa = TLBLO_PTE_TO_PA(pte[i]);
225 * Now mark pages from 0 to phys_avail[0], that's where kernel
226 * and pages allocated by pmap_steal reside
228 for (pa = 0; pa < phys_avail[0]; pa += PAGE_SIZE) {
233 /* Calculate dump size. */
235 dumpsize += round_page(msgbufp->msg_size);
236 dumpsize += round_page(vm_page_dump_size);
237 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
238 bits = vm_page_dump[i];
241 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) +
243 /* Clear out undumpable pages now if needed */
245 dumpsize += PAGE_SIZE;
248 bits &= ~(1ul << bit);
251 dumpsize += PAGE_SIZE;
255 /* Initialize mdhdr */
256 bzero(&mdhdr, sizeof(mdhdr));
257 strcpy(mdhdr.magic, MINIDUMP_MAGIC);
258 mdhdr.version = MINIDUMP_VERSION;
259 mdhdr.msgbufsize = msgbufp->msg_size;
260 mdhdr.bitmapsize = vm_page_dump_size;
261 mdhdr.ptesize = ptesize;
262 mdhdr.kernbase = VM_MIN_KERNEL_ADDRESS;
264 dump_init_header(di, &kdh, KERNELDUMPMAGIC, KERNELDUMP_MIPS_VERSION,
267 printf("Dumping %llu out of %ju MB:", (long long)dumpsize >> 20,
268 ptoa((uintmax_t)physmem) / 1048576);
270 error = dump_start(di, &kdh, &dumplo);
275 bzero(tmpbuffer, sizeof(tmpbuffer));
276 bcopy(&mdhdr, tmpbuffer, sizeof(mdhdr));
277 error = write_buffer(di, tmpbuffer, PAGE_SIZE);
281 /* Dump msgbuf up front */
282 error = write_buffer(di, (char *)msgbufp->msg_ptr,
283 round_page(msgbufp->msg_size));
288 error = write_buffer(di, (char *)vm_page_dump,
289 round_page(vm_page_dump_size));
293 /* Dump kernel page table pages */
294 for (va = VM_MIN_KERNEL_ADDRESS; va < kernel_vm_end; va += NBPDR) {
295 pte = pmap_pte(kernel_pmap, va);
296 KASSERT(pte != NULL, ("pte for %jx is NULL", (uintmax_t)va));
298 prev_pte = (vm_offset_t)pte;
301 if ((vm_offset_t)pte == (prev_pte + count * PAGE_SIZE))
304 error = write_buffer(di, (char*)prev_pte,
309 prev_pte = (vm_offset_t)pte;
315 error = write_buffer(di, (char*)prev_pte, count * PAGE_SIZE);
322 /* Dump memory chunks page by page*/
323 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
324 bits = vm_page_dump[i];
327 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) +
329 dump_va = pmap_kenter_temporary(pa, 0);
330 error = write_buffer(di, dump_va, PAGE_SIZE);
333 pmap_kenter_temporary_free(pa);
334 bits &= ~(1ul << bit);
338 error = dump_finish(di, &kdh, dumplo);
342 printf("\nDump complete\n");
349 if (error == ECANCELED)
350 printf("\nDump aborted\n");
351 else if (error == E2BIG || error == ENOSPC)
352 printf("\nDump failed. Partition too small.\n");
354 printf("\n** DUMP FAILED (ERROR %d) **\n", error);