2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2006 Peter Wemm
5 * Copyright (c) 2008 Semihalf, Grzegorz Bernacki
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * from: FreeBSD: src/sys/i386/i386/minidump_machdep.c,v 1.6 2008/08/17 23:27:27
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_watchdog.h"
37 #include <sys/param.h>
38 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/kerneldump.h>
43 #include <sys/msgbuf.h>
45 #include <sys/watchdog.h>
49 #include <machine/atomic.h>
50 #include <machine/cpu.h>
51 #include <machine/elf.h>
52 #include <machine/md_var.h>
53 #include <machine/minidump.h>
54 #include <machine/vmparam.h>
56 CTASSERT(sizeof(struct kerneldumpheader) == 512);
58 uint32_t *vm_page_dump;
59 int vm_page_dump_size;
61 static struct kerneldumpheader kdh;
63 /* Handle chunked writes. */
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_append(di, dump_va, 0, fragsz);
98 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
104 maxdumpsz = min(di->maxiosize, MAXDUMPPGS * PAGE_SIZE);
105 if (maxdumpsz == 0) /* seatbelt */
106 maxdumpsz = PAGE_SIZE;
108 if (ptr != NULL && pa != 0) {
109 printf("cant have both va and pa!\n");
113 if ((sz % PAGE_SIZE) != 0) {
114 printf("size not page aligned\n");
117 if ((pa & PAGE_MASK) != 0) {
118 printf("address not page aligned\n");
123 /* Flush any pre-existing pa pages before a virtual dump. */
124 error = blk_flush(di);
129 len = maxdumpsz - fragsz;
135 printf(" %lld", PG2MB(progress >> PAGE_SHIFT));
136 counter &= (1<<22) - 1;
140 wdog_kern_pat(WD_LASTVAL);
143 error = dump_append(di, ptr, 0, len);
149 for (i = 0; i < len; i += PAGE_SIZE)
150 dump_va = pmap_kenter_temporary(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) ");
173 /* A buffer for general use. Its size must be one page at least. */
174 static char dumpbuf[PAGE_SIZE];
175 CTASSERT(sizeof(dumpbuf) % sizeof(pt2_entry_t) == 0);
178 minidumpsys(struct dumperinfo *di)
180 struct minidumphdr mdhdr;
184 uint32_t pa, prev_pa = 0, count = 0;
190 * Flush caches. Note that in the SMP case this operates only on the
191 * current CPU's L1 cache. Before we reach this point, code in either
192 * the system shutdown or kernel debugger has called stop_cpus() to stop
193 * all cores other than this one. Part of the ARM handling of
194 * stop_cpus() is to call wbinv_all() on that core's local L1 cache. So
195 * by time we get to here, all that remains is to flush the L1 for the
196 * current CPU, then the L2.
198 dcache_wbinv_poc_all();
201 /* Walk page table pages, set bits in vm_page_dump */
203 for (va = KERNBASE; va < kernel_vm_end; va += PAGE_SIZE) {
204 pa = pmap_dump_kextract(va, NULL);
205 if (pa != 0 && is_dumpable(pa))
207 ptesize += sizeof(pt2_entry_t);
210 /* Calculate dump size. */
212 dumpsize += round_page(msgbufp->msg_size);
213 dumpsize += round_page(vm_page_dump_size);
214 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
215 bits = vm_page_dump[i];
218 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) +
220 /* Clear out undumpable pages now if needed */
222 dumpsize += PAGE_SIZE;
225 bits &= ~(1ul << bit);
228 dumpsize += PAGE_SIZE;
232 /* Initialize mdhdr */
233 bzero(&mdhdr, sizeof(mdhdr));
234 strcpy(mdhdr.magic, MINIDUMP_MAGIC);
235 mdhdr.version = MINIDUMP_VERSION;
236 mdhdr.msgbufsize = msgbufp->msg_size;
237 mdhdr.bitmapsize = vm_page_dump_size;
238 mdhdr.ptesize = ptesize;
239 mdhdr.kernbase = KERNBASE;
240 mdhdr.arch = __ARM_ARCH;
242 mdhdr.mmuformat = MINIDUMP_MMU_FORMAT_V6;
244 mdhdr.mmuformat = MINIDUMP_MMU_FORMAT_V4;
246 dump_init_header(di, &kdh, KERNELDUMPMAGIC, KERNELDUMP_ARM_VERSION,
249 printf("Physical memory: %u MB\n", ptoa((uintmax_t)physmem) / 1048576);
250 printf("Dumping %llu MB:", (long long)dumpsize >> 20);
252 error = dump_start(di, &kdh);
257 bzero(dumpbuf, sizeof(dumpbuf));
258 bcopy(&mdhdr, dumpbuf, sizeof(mdhdr));
259 error = blk_write(di, dumpbuf, 0, PAGE_SIZE);
263 /* Dump msgbuf up front */
264 error = blk_write(di, (char *)msgbufp->msg_ptr, 0,
265 round_page(msgbufp->msg_size));
270 error = blk_write(di, (char *)vm_page_dump, 0,
271 round_page(vm_page_dump_size));
275 /* Dump kernel page table pages */
277 for (va = KERNBASE; va < kernel_vm_end; va += PAGE_SIZE) {
278 pmap_dump_kextract(va, (pt2_entry_t *)addr);
279 addr += sizeof(pt2_entry_t);
280 if (addr == dumpbuf + sizeof(dumpbuf)) {
281 error = blk_write(di, dumpbuf, 0, sizeof(dumpbuf));
287 if (addr != dumpbuf) {
288 error = blk_write(di, dumpbuf, 0, addr - dumpbuf);
293 /* Dump memory chunks */
294 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
295 bits = vm_page_dump[i];
298 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) +
304 if (pa == (prev_pa + count * PAGE_SIZE))
307 error = blk_write(di, NULL, prev_pa,
315 bits &= ~(1ul << bit);
319 error = blk_write(di, NULL, prev_pa, count * PAGE_SIZE);
326 error = blk_flush(di);
330 error = dump_finish(di, &kdh);
334 printf("\nDump complete\n");
341 if (error == ECANCELED)
342 printf("\nDump aborted\n");
343 else if (error == E2BIG || error == ENOSPC)
344 printf("\nDump failed. Partition too small.\n");
346 printf("\n** DUMP FAILED (ERROR %d) **\n", error);
351 dump_add_page(vm_paddr_t pa)
356 idx = pa >> 5; /* 2^5 = 32 */
358 atomic_set_int(&vm_page_dump[idx], 1ul << bit);
362 dump_drop_page(vm_paddr_t pa)
367 idx = pa >> 5; /* 2^5 = 32 */
369 atomic_clear_int(&vm_page_dump[idx], 1ul << bit);