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);
56 * Don't touch the first SIZEOF_METADATA bytes on the dump device. This
57 * is to protect us from metadata and to protect metadata from us.
59 #define SIZEOF_METADATA (64*1024)
61 uint32_t *vm_page_dump;
62 int vm_page_dump_size;
64 static struct kerneldumpheader kdh;
66 static off_t origdumplo;
68 /* Handle chunked writes. */
69 static uint64_t counter, progress, dumpsize;
70 /* Just auxiliary bufffer */
71 static char tmpbuffer[PAGE_SIZE];
73 extern pd_entry_t *kernel_segmap;
75 CTASSERT(sizeof(*vm_page_dump) == 4);
78 is_dumpable(vm_paddr_t pa)
83 if ((m = vm_phys_paddr_to_vm_page(pa)) != NULL)
84 return ((m->flags & PG_NODUMP) == 0);
85 for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
86 if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
93 dump_add_page(vm_paddr_t pa)
98 idx = pa >> 5; /* 2^5 = 32 */
100 atomic_set_int(&vm_page_dump[idx], 1ul << bit);
104 dump_drop_page(vm_paddr_t pa)
109 idx = pa >> 5; /* 2^5 = 32 */
111 atomic_clear_int(&vm_page_dump[idx], 1ul << bit);
118 } progress_track[10] = {
132 report_progress(uint64_t progress, uint64_t dumpsize)
136 sofar = 100 - ((progress * 100) / dumpsize);
137 for (i = 0; i < nitems(progress_track); i++) {
138 if (sofar < progress_track[i].min_per ||
139 sofar > progress_track[i].max_per)
141 if (progress_track[i].visited)
143 progress_track[i].visited = 1;
144 printf("..%d%%", sofar);
150 write_buffer(struct dumperinfo *di, char *ptr, size_t sz)
156 maxdumpsz = di->maxiosize;
158 if (maxdumpsz == 0) /* seatbelt */
159 maxdumpsz = PAGE_SIZE;
164 len = min(maxdumpsz, sz);
169 report_progress(progress, dumpsize);
170 counter &= (1<<22) - 1;
173 wdog_kern_pat(WD_LASTVAL);
176 error = dump_write(di, ptr, 0, dumplo, len);
183 panic("pa is not supported");
186 /* Check for user abort. */
191 printf(" (CTRL-C to abort) ");
198 minidumpsys(struct dumperinfo *di)
200 struct minidumphdr mdhdr;
204 vm_offset_t prev_pte = 0;
212 mips_dcache_wbinv_all();
215 /* Walk page table pages, set bits in vm_page_dump */
218 for (va = VM_MIN_KERNEL_ADDRESS; va < kernel_vm_end; va += NBPDR) {
219 ptesize += PAGE_SIZE;
220 pte = pmap_pte(kernel_pmap, va);
221 KASSERT(pte != NULL, ("pte for %jx is NULL", (uintmax_t)va));
222 for (i = 0; i < NPTEPG; i++) {
223 if (pte_test(&pte[i], PTE_V)) {
224 pa = TLBLO_PTE_TO_PA(pte[i]);
232 * Now mark pages from 0 to phys_avail[0], that's where kernel
233 * and pages allocated by pmap_steal reside
235 for (pa = 0; pa < phys_avail[0]; pa += PAGE_SIZE) {
240 /* Calculate dump size. */
242 dumpsize += round_page(msgbufp->msg_size);
243 dumpsize += round_page(vm_page_dump_size);
245 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
246 bits = vm_page_dump[i];
249 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) +
251 /* Clear out undumpable pages now if needed */
253 dumpsize += PAGE_SIZE;
256 bits &= ~(1ul << bit);
260 dumpsize += PAGE_SIZE;
262 /* Determine dump offset on device. */
263 if (di->mediasize < SIZEOF_METADATA + dumpsize + di->blocksize * 2 +
264 kerneldumpcrypto_dumpkeysize(di->kdc)) {
269 origdumplo = dumplo = di->mediaoffset + di->mediasize - dumpsize;
270 dumplo -= di->blocksize * 2;
271 dumplo -= kerneldumpcrypto_dumpkeysize(di->kdc);
274 /* Initialize kernel dump crypto. */
275 error = kerneldumpcrypto_init(di->kdc);
279 /* Initialize mdhdr */
280 bzero(&mdhdr, sizeof(mdhdr));
281 strcpy(mdhdr.magic, MINIDUMP_MAGIC);
282 mdhdr.version = MINIDUMP_VERSION;
283 mdhdr.msgbufsize = msgbufp->msg_size;
284 mdhdr.bitmapsize = vm_page_dump_size;
285 mdhdr.ptesize = ptesize;
286 mdhdr.kernbase = VM_MIN_KERNEL_ADDRESS;
288 mkdumpheader(&kdh, KERNELDUMPMAGIC, KERNELDUMP_MIPS_VERSION, dumpsize,
289 kerneldumpcrypto_dumpkeysize(di->kdc), di->blocksize);
291 printf("Dumping %llu out of %ju MB:", (long long)dumpsize >> 20,
292 ptoa((uintmax_t)physmem) / 1048576);
295 error = dump_write_header(di, &kdh, 0, dumplo);
298 dumplo += di->blocksize;
301 error = dump_write_key(di, 0, dumplo);
304 dumplo += kerneldumpcrypto_dumpkeysize(di->kdc);
307 bzero(tmpbuffer, sizeof(tmpbuffer));
308 bcopy(&mdhdr, tmpbuffer, sizeof(mdhdr));
309 error = write_buffer(di, tmpbuffer, PAGE_SIZE);
313 /* Dump msgbuf up front */
314 error = write_buffer(di, (char *)msgbufp->msg_ptr,
315 round_page(msgbufp->msg_size));
320 error = write_buffer(di, (char *)vm_page_dump,
321 round_page(vm_page_dump_size));
325 /* Dump kernel page table pages */
326 for (va = VM_MIN_KERNEL_ADDRESS; va < kernel_vm_end; va += NBPDR) {
327 pte = pmap_pte(kernel_pmap, va);
328 KASSERT(pte != NULL, ("pte for %jx is NULL", (uintmax_t)va));
330 prev_pte = (vm_offset_t)pte;
334 if ((vm_offset_t)pte == (prev_pte + count * PAGE_SIZE))
337 error = write_buffer(di, (char*)prev_pte,
342 prev_pte = (vm_offset_t)pte;
348 error = write_buffer(di, (char*)prev_pte, count * PAGE_SIZE);
355 /* Dump memory chunks page by page*/
356 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
357 bits = vm_page_dump[i];
360 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) +
362 dump_va = pmap_kenter_temporary(pa, 0);
363 error = write_buffer(di, dump_va, PAGE_SIZE);
366 pmap_kenter_temporary_free(pa);
367 bits &= ~(1ul << bit);
372 error = dump_write_header(di, &kdh, 0, dumplo);
375 dumplo += di->blocksize;
377 /* Signal completion, signoff and exit stage left. */
378 dump_write(di, NULL, 0, 0, 0);
379 printf("\nDump complete\n");
386 if (error == ECANCELED)
387 printf("\nDump aborted\n");
388 else if (error == ENOSPC)
389 printf("\nDump failed. Partition too small.\n");
391 printf("\n** DUMP FAILED (ERROR %d) **\n", error);