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>
48 #include <vm/vm_param.h>
49 #include <vm/vm_page.h>
50 #include <vm/vm_phys.h>
52 #include <machine/atomic.h>
53 #include <machine/cpu.h>
54 #include <machine/elf.h>
55 #include <machine/md_var.h>
56 #include <machine/minidump.h>
58 CTASSERT(sizeof(struct kerneldumpheader) == 512);
60 static struct kerneldumpheader kdh;
62 /* Handle chunked writes. */
65 static uint64_t counter, progress;
68 is_dumpable(vm_paddr_t pa)
72 for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
73 if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
79 #define PG2MB(pgs) (((pgs) + (1 << 8) - 1) >> 8)
82 blk_flush(struct dumperinfo *di)
89 error = dump_append(di, dump_va, 0, fragsz);
95 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
101 maxdumpsz = min(di->maxiosize, MAXDUMPPGS * PAGE_SIZE);
102 if (maxdumpsz == 0) /* seatbelt */
103 maxdumpsz = PAGE_SIZE;
105 if (ptr != NULL && pa != 0) {
106 printf("cant have both va and pa!\n");
110 if ((sz % PAGE_SIZE) != 0) {
111 printf("size not page aligned\n");
114 if ((pa & PAGE_MASK) != 0) {
115 printf("address not page aligned\n");
120 /* Flush any pre-existing pa pages before a virtual dump. */
121 error = blk_flush(di);
126 len = maxdumpsz - fragsz;
132 printf(" %lld", PG2MB(progress >> PAGE_SHIFT));
133 counter &= (1<<22) - 1;
137 wdog_kern_pat(WD_LASTVAL);
140 error = dump_append(di, ptr, 0, len);
146 for (i = 0; i < len; i += PAGE_SIZE)
147 dump_va = pmap_kenter_temporary(pa + i,
148 (i + fragsz) >> PAGE_SHIFT);
152 if (fragsz == maxdumpsz) {
153 error = blk_flush(di);
159 /* Check for user abort. */
164 printf(" (CTRL-C to abort) ");
170 /* A buffer for general use. Its size must be one page at least. */
171 static char dumpbuf[PAGE_SIZE];
172 CTASSERT(sizeof(dumpbuf) % sizeof(pt2_entry_t) == 0);
175 minidumpsys(struct dumperinfo *di)
177 struct minidumphdr mdhdr;
180 uint32_t pa, prev_pa = 0, count = 0;
186 * Flush caches. Note that in the SMP case this operates only on the
187 * current CPU's L1 cache. Before we reach this point, code in either
188 * the system shutdown or kernel debugger has called stop_cpus() to stop
189 * all cores other than this one. Part of the ARM handling of
190 * stop_cpus() is to call wbinv_all() on that core's local L1 cache. So
191 * by time we get to here, all that remains is to flush the L1 for the
192 * current CPU, then the L2.
194 dcache_wbinv_poc_all();
197 /* Walk page table pages, set bits in vm_page_dump */
199 for (va = KERNBASE; va < kernel_vm_end; va += PAGE_SIZE) {
200 pa = pmap_dump_kextract(va, NULL);
201 if (pa != 0 && is_dumpable(pa))
203 ptesize += sizeof(pt2_entry_t);
206 /* Calculate dump size. */
208 dumpsize += round_page(msgbufp->msg_size);
209 dumpsize += round_page(sizeof(dump_avail));
210 dumpsize += round_page(BITSET_SIZE(vm_page_dump_pages));
211 VM_PAGE_DUMP_FOREACH(pa) {
212 /* Clear out undumpable pages now if needed */
214 dumpsize += PAGE_SIZE;
218 dumpsize += PAGE_SIZE;
222 /* Initialize mdhdr */
223 bzero(&mdhdr, sizeof(mdhdr));
224 strcpy(mdhdr.magic, MINIDUMP_MAGIC);
225 mdhdr.version = MINIDUMP_VERSION;
226 mdhdr.msgbufsize = msgbufp->msg_size;
227 mdhdr.bitmapsize = round_page(BITSET_SIZE(vm_page_dump_pages));
228 mdhdr.ptesize = ptesize;
229 mdhdr.kernbase = KERNBASE;
230 mdhdr.arch = __ARM_ARCH;
232 mdhdr.mmuformat = MINIDUMP_MMU_FORMAT_V6;
234 mdhdr.mmuformat = MINIDUMP_MMU_FORMAT_V4;
236 mdhdr.dumpavailsize = round_page(sizeof(dump_avail));
237 dump_init_header(di, &kdh, KERNELDUMPMAGIC, KERNELDUMP_ARM_VERSION,
240 error = dump_start(di, &kdh);
244 printf("Physical memory: %u MB\n", ptoa((uintmax_t)physmem) / 1048576);
245 printf("Dumping %llu MB:", (long long)dumpsize >> 20);
248 bzero(dumpbuf, sizeof(dumpbuf));
249 bcopy(&mdhdr, dumpbuf, sizeof(mdhdr));
250 error = blk_write(di, dumpbuf, 0, PAGE_SIZE);
254 /* Dump msgbuf up front */
255 error = blk_write(di, (char *)msgbufp->msg_ptr, 0,
256 round_page(msgbufp->msg_size));
260 /* Dump dump_avail */
261 _Static_assert(sizeof(dump_avail) <= sizeof(dumpbuf),
262 "Large dump_avail not handled");
263 bzero(dumpbuf, sizeof(dumpbuf));
264 memcpy(dumpbuf, dump_avail, sizeof(dump_avail));
265 error = blk_write(di, dumpbuf, 0, PAGE_SIZE);
270 error = blk_write(di, (char *)vm_page_dump, 0,
271 round_page(BITSET_SIZE(vm_page_dump_pages)));
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 VM_PAGE_DUMP_FOREACH(pa) {
299 if (pa == (prev_pa + count * PAGE_SIZE))
302 error = blk_write(di, NULL, prev_pa,
312 error = blk_write(di, NULL, prev_pa, count * PAGE_SIZE);
319 error = blk_flush(di);
323 error = dump_finish(di, &kdh);
327 printf("\nDump complete\n");
334 if (error == ECANCELED)
335 printf("\nDump aborted\n");
336 else if (error == E2BIG || error == ENOSPC) {
337 printf("\nDump failed. Partition too small (about %lluMB were "
338 "needed this time).\n", (long long)dumpsize >> 20);
340 printf("\n** DUMP FAILED (ERROR %d) **\n", error);