2 * Copyright (c) 2006 Peter Wemm
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/i386/i386/minidump_machdep.c,v 1.6 2008/08/17 23:27:27
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include "opt_watchdog.h"
35 #include <sys/param.h>
36 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/kerneldump.h>
41 #include <sys/msgbuf.h>
43 #include <sys/watchdog.h>
47 #include <machine/atomic.h>
48 #include <machine/cpu.h>
49 #include <machine/elf.h>
50 #include <machine/md_var.h>
51 #include <machine/minidump.h>
52 #include <machine/vmparam.h>
54 CTASSERT(sizeof(struct kerneldumpheader) == 512);
56 uint32_t *vm_page_dump;
57 int vm_page_dump_size;
59 static struct kerneldumpheader kdh;
61 /* Handle chunked writes. */
64 static uint64_t counter, progress;
66 CTASSERT(sizeof(*vm_page_dump) == 4);
69 is_dumpable(vm_paddr_t pa)
73 for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
74 if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
80 #define PG2MB(pgs) (((pgs) + (1 << 8) - 1) >> 8)
83 blk_flush(struct dumperinfo *di)
90 error = dump_append(di, dump_va, 0, 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 (ptr != NULL && pa != 0) {
107 printf("cant have both va and pa!\n");
111 if ((sz % PAGE_SIZE) != 0) {
112 printf("size not page aligned\n");
115 if ((pa & PAGE_MASK) != 0) {
116 printf("address not page aligned\n");
121 /* Flush any pre-existing pa pages before a virtual dump. */
122 error = blk_flush(di);
127 len = maxdumpsz - fragsz;
133 printf(" %lld", PG2MB(progress >> PAGE_SHIFT));
134 counter &= (1<<22) - 1;
138 wdog_kern_pat(WD_LASTVAL);
141 error = dump_append(di, ptr, 0, len);
147 for (i = 0; i < len; i += PAGE_SIZE)
148 dump_va = pmap_kenter_temporary(pa + i,
149 (i + fragsz) >> PAGE_SHIFT);
153 if (fragsz == maxdumpsz) {
154 error = blk_flush(di);
160 /* Check for user abort. */
165 printf(" (CTRL-C to abort) ");
171 /* A buffer for general use. Its size must be one page at least. */
172 static char dumpbuf[PAGE_SIZE];
173 CTASSERT(sizeof(dumpbuf) % sizeof(pt2_entry_t) == 0);
176 minidumpsys(struct dumperinfo *di)
178 struct minidumphdr mdhdr;
182 uint32_t pa, prev_pa = 0, count = 0;
188 * Flush caches. Note that in the SMP case this operates only on the
189 * current CPU's L1 cache. Before we reach this point, code in either
190 * the system shutdown or kernel debugger has called stop_cpus() to stop
191 * all cores other than this one. Part of the ARM handling of
192 * stop_cpus() is to call wbinv_all() on that core's local L1 cache. So
193 * by time we get to here, all that remains is to flush the L1 for the
194 * current CPU, then the L2.
196 dcache_wbinv_poc_all();
199 /* Walk page table pages, set bits in vm_page_dump */
201 for (va = KERNBASE; va < kernel_vm_end; va += PAGE_SIZE) {
202 pa = pmap_dump_kextract(va, NULL);
203 if (pa != 0 && is_dumpable(pa))
205 ptesize += sizeof(pt2_entry_t);
208 /* Calculate dump size. */
210 dumpsize += round_page(msgbufp->msg_size);
211 dumpsize += round_page(vm_page_dump_size);
212 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
213 bits = vm_page_dump[i];
216 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) +
218 /* Clear out undumpable pages now if needed */
220 dumpsize += PAGE_SIZE;
223 bits &= ~(1ul << bit);
226 dumpsize += PAGE_SIZE;
230 /* Initialize mdhdr */
231 bzero(&mdhdr, sizeof(mdhdr));
232 strcpy(mdhdr.magic, MINIDUMP_MAGIC);
233 mdhdr.version = MINIDUMP_VERSION;
234 mdhdr.msgbufsize = msgbufp->msg_size;
235 mdhdr.bitmapsize = vm_page_dump_size;
236 mdhdr.ptesize = ptesize;
237 mdhdr.kernbase = KERNBASE;
238 mdhdr.arch = __ARM_ARCH;
240 mdhdr.mmuformat = MINIDUMP_MMU_FORMAT_V6;
242 mdhdr.mmuformat = MINIDUMP_MMU_FORMAT_V4;
244 dump_init_header(di, &kdh, KERNELDUMPMAGIC, KERNELDUMP_ARM_VERSION,
247 printf("Physical memory: %u MB\n", ptoa((uintmax_t)physmem) / 1048576);
248 printf("Dumping %llu MB:", (long long)dumpsize >> 20);
250 error = dump_start(di, &kdh);
255 bzero(dumpbuf, sizeof(dumpbuf));
256 bcopy(&mdhdr, dumpbuf, sizeof(mdhdr));
257 error = blk_write(di, dumpbuf, 0, PAGE_SIZE);
261 /* Dump msgbuf up front */
262 error = blk_write(di, (char *)msgbufp->msg_ptr, 0,
263 round_page(msgbufp->msg_size));
268 error = blk_write(di, (char *)vm_page_dump, 0,
269 round_page(vm_page_dump_size));
273 /* Dump kernel page table pages */
275 for (va = KERNBASE; va < kernel_vm_end; va += PAGE_SIZE) {
276 pmap_dump_kextract(va, (pt2_entry_t *)addr);
277 addr += sizeof(pt2_entry_t);
278 if (addr == dumpbuf + sizeof(dumpbuf)) {
279 error = blk_write(di, dumpbuf, 0, sizeof(dumpbuf));
285 if (addr != dumpbuf) {
286 error = blk_write(di, dumpbuf, 0, addr - dumpbuf);
291 /* Dump memory chunks */
292 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
293 bits = vm_page_dump[i];
296 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) +
302 if (pa == (prev_pa + count * PAGE_SIZE))
305 error = blk_write(di, NULL, prev_pa,
313 bits &= ~(1ul << bit);
317 error = blk_write(di, NULL, prev_pa, count * PAGE_SIZE);
324 error = blk_flush(di);
328 error = dump_finish(di, &kdh);
332 printf("\nDump complete\n");
339 if (error == ECANCELED)
340 printf("\nDump aborted\n");
341 else if (error == E2BIG || error == ENOSPC)
342 printf("\nDump failed. Partition too small.\n");
344 printf("\n** DUMP FAILED (ERROR %d) **\n", error);
349 dump_add_page(vm_paddr_t pa)
354 idx = pa >> 5; /* 2^5 = 32 */
356 atomic_set_int(&vm_page_dump[idx], 1ul << bit);
360 dump_drop_page(vm_paddr_t pa)
365 idx = pa >> 5; /* 2^5 = 32 */
367 atomic_clear_int(&vm_page_dump[idx], 1ul << bit);