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);
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;
69 /* Handle chunked writes. */
72 static uint64_t counter, progress;
74 CTASSERT(sizeof(*vm_page_dump) == 4);
77 is_dumpable(vm_paddr_t pa)
81 for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
82 if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
88 #define PG2MB(pgs) (((pgs) + (1 << 8) - 1) >> 8)
91 blk_flush(struct dumperinfo *di)
98 error = dump_write(di, dump_va, 0, dumplo, fragsz);
105 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
111 maxdumpsz = min(di->maxiosize, MAXDUMPPGS * PAGE_SIZE);
112 if (maxdumpsz == 0) /* seatbelt */
113 maxdumpsz = PAGE_SIZE;
115 if (ptr != NULL && pa != 0) {
116 printf("cant have both va and pa!\n");
120 if ((sz % PAGE_SIZE) != 0) {
121 printf("size not page aligned\n");
124 if ((pa & PAGE_MASK) != 0) {
125 printf("address not page aligned\n");
130 /* Flush any pre-existing pa pages before a virtual dump. */
131 error = blk_flush(di);
136 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_temporary(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) ");
181 /* A buffer for general use. Its size must be one page at least. */
182 static char dumpbuf[PAGE_SIZE];
183 CTASSERT(sizeof(dumpbuf) % sizeof(pt2_entry_t) == 0);
186 minidumpsys(struct dumperinfo *di)
188 struct minidumphdr mdhdr;
192 uint32_t pa, prev_pa = 0, count = 0;
198 * Flush caches. Note that in the SMP case this operates only on the
199 * current CPU's L1 cache. Before we reach this point, code in either
200 * the system shutdown or kernel debugger has called stop_cpus() to stop
201 * all cores other than this one. Part of the ARM handling of
202 * stop_cpus() is to call wbinv_all() on that core's local L1 cache. So
203 * by time we get to here, all that remains is to flush the L1 for the
204 * current CPU, then the L2.
206 dcache_wbinv_poc_all();
209 /* Walk page table pages, set bits in vm_page_dump */
211 for (va = KERNBASE; va < kernel_vm_end; va += PAGE_SIZE) {
212 pa = pmap_dump_kextract(va, NULL);
213 if (pa != 0 && is_dumpable(pa))
215 ptesize += sizeof(pt2_entry_t);
218 /* Calculate dump size. */
220 dumpsize += round_page(msgbufp->msg_size);
221 dumpsize += round_page(vm_page_dump_size);
223 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
224 bits = vm_page_dump[i];
227 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) +
229 /* Clear out undumpable pages now if needed */
231 dumpsize += PAGE_SIZE;
234 bits &= ~(1ul << bit);
238 dumpsize += PAGE_SIZE;
240 /* Determine dump offset on device. */
241 if (di->mediasize < SIZEOF_METADATA + dumpsize + sizeof(kdh) * 2) {
246 dumplo = di->mediaoffset + di->mediasize - dumpsize;
247 dumplo -= sizeof(kdh) * 2;
250 /* Initialize mdhdr */
251 bzero(&mdhdr, sizeof(mdhdr));
252 strcpy(mdhdr.magic, MINIDUMP_MAGIC);
253 mdhdr.version = MINIDUMP_VERSION;
254 mdhdr.msgbufsize = msgbufp->msg_size;
255 mdhdr.bitmapsize = vm_page_dump_size;
256 mdhdr.ptesize = ptesize;
257 mdhdr.kernbase = KERNBASE;
258 mdhdr.arch = __ARM_ARCH;
260 mdhdr.mmuformat = MINIDUMP_MMU_FORMAT_V6;
262 mdhdr.mmuformat = MINIDUMP_MMU_FORMAT_V4;
264 mkdumpheader(&kdh, KERNELDUMPMAGIC, KERNELDUMP_ARM_VERSION, dumpsize,
267 printf("Physical memory: %u MB\n", ptoa((uintmax_t)physmem) / 1048576);
268 printf("Dumping %llu MB:", (long long)dumpsize >> 20);
271 error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
274 dumplo += sizeof(kdh);
277 bzero(dumpbuf, sizeof(dumpbuf));
278 bcopy(&mdhdr, dumpbuf, sizeof(mdhdr));
279 error = blk_write(di, dumpbuf, 0, PAGE_SIZE);
283 /* Dump msgbuf up front */
284 error = blk_write(di, (char *)msgbufp->msg_ptr, 0,
285 round_page(msgbufp->msg_size));
290 error = blk_write(di, (char *)vm_page_dump, 0,
291 round_page(vm_page_dump_size));
295 /* Dump kernel page table pages */
297 for (va = KERNBASE; va < kernel_vm_end; va += PAGE_SIZE) {
298 pmap_dump_kextract(va, (pt2_entry_t *)addr);
299 addr += sizeof(pt2_entry_t);
300 if (addr == dumpbuf + sizeof(dumpbuf)) {
301 error = blk_write(di, dumpbuf, 0, sizeof(dumpbuf));
307 if (addr != dumpbuf) {
308 error = blk_write(di, dumpbuf, 0, addr - dumpbuf);
313 /* Dump memory chunks */
314 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
315 bits = vm_page_dump[i];
318 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) +
324 if (pa == (prev_pa + count * PAGE_SIZE))
327 error = blk_write(di, NULL, prev_pa,
335 bits &= ~(1ul << bit);
339 error = blk_write(di, NULL, prev_pa, count * PAGE_SIZE);
346 error = blk_flush(di);
351 error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
354 dumplo += sizeof(kdh);
356 /* Signal completion, signoff and exit stage left. */
357 dump_write(di, NULL, 0, 0, 0);
358 printf("\nDump complete\n");
365 if (error == ECANCELED)
366 printf("\nDump aborted\n");
367 else if (error == ENOSPC)
368 printf("\nDump failed. Partition too small.\n");
370 printf("\n** DUMP FAILED (ERROR %d) **\n", error);
375 dump_add_page(vm_paddr_t pa)
380 idx = pa >> 5; /* 2^5 = 32 */
382 atomic_set_int(&vm_page_dump[idx], 1ul << bit);
386 dump_drop_page(vm_paddr_t pa)
391 idx = pa >> 5; /* 2^5 = 32 */
393 atomic_clear_int(&vm_page_dump[idx], 1ul << bit);